gdbtypes.h: Get rid of the TYPE_FIXED_POINT_INFO macro
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
b811d2c2 3 Copyright (C) 1994-2020 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"
2b2558bf 43#include "dwarf2/stringify.h"
4de283e4
TT
44#include "bfd.h"
45#include "elf-bfd.h"
46#include "symtab.h"
47#include "gdbtypes.h"
48#include "objfiles.h"
d55e5aa6 49#include "dwarf2.h"
4de283e4
TT
50#include "buildsym.h"
51#include "demangle.h"
52#include "gdb-demangle.h"
4de283e4 53#include "filenames.h" /* for DOSish file names */
4de283e4
TT
54#include "language.h"
55#include "complaints.h"
82ca8957
TT
56#include "dwarf2/expr.h"
57#include "dwarf2/loc.h"
4de283e4
TT
58#include "cp-support.h"
59#include "hashtab.h"
60#include "command.h"
d55e5aa6 61#include "gdbcmd.h"
4de283e4
TT
62#include "block.h"
63#include "addrmap.h"
64#include "typeprint.h"
65#include "psympriv.h"
4de283e4 66#include "c-lang.h"
d55e5aa6 67#include "go-lang.h"
4de283e4
TT
68#include "valprint.h"
69#include "gdbcore.h" /* for gnutarget */
70#include "gdb/gdb-index.h"
4de283e4
TT
71#include "gdb_bfd.h"
72#include "f-lang.h"
73#include "source.h"
4de283e4 74#include "build-id.h"
d55e5aa6 75#include "namespace.h"
268a13a5
TT
76#include "gdbsupport/function-view.h"
77#include "gdbsupport/gdb_optional.h"
78#include "gdbsupport/underlying.h"
268a13a5 79#include "gdbsupport/hash_enum.h"
4de283e4 80#include "filename-seen-cache.h"
b32b108a 81#include "producer.h"
4de283e4 82#include <fcntl.h>
4de283e4 83#include <algorithm>
4de283e4 84#include <unordered_map>
268a13a5 85#include "gdbsupport/selftest.h"
c9317f21 86#include "rust-lang.h"
268a13a5 87#include "gdbsupport/pathstuff.h"
edd45eb0 88#include "count-one-bits.h"
0d79cdc4 89#include "debuginfod-support.h"
437afbb8 90
73be47f5
DE
91/* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
b4f54984
DE
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94static unsigned int dwarf_read_debug = 0;
45cfd468 95
6f738b01
SM
96/* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
97
98#define dwarf_read_debug_printf(fmt, ...) \
99 do \
100 { \
101 if (dwarf_read_debug >= 1) \
102 debug_prefixed_printf ("dwarf-read", __func__, fmt, ##__VA_ARGS__); \
103 } \
104 while (0)
105
106/* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
107
108#define dwarf_read_debug_printf_v(fmt, ...) \
109 do \
110 { \
111 if (dwarf_read_debug >= 2) \
112 debug_prefixed_printf ("dwarf-read", __func__, fmt, ##__VA_ARGS__); \
113 } \
114 while (0)
115
d97bc12b 116/* When non-zero, dump DIEs after they are read in. */
b4f54984 117static unsigned int dwarf_die_debug = 0;
d97bc12b 118
27e0867f 119/* When non-zero, dump line number entries as they are read in. */
8fdd972c 120unsigned int dwarf_line_debug = 0;
27e0867f 121
491144b5
CB
122/* When true, cross-check physname against demangler. */
123static bool check_physname = false;
900e11f9 124
491144b5
CB
125/* When true, do not reject deprecated .gdb_index sections. */
126static bool use_deprecated_index_sections = false;
481860b3 127
17ee85fc
TT
128/* This is used to store the data that is always per objfile. */
129static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
130
131/* These are used to store the dwarf2_per_bfd objects.
132
133 objfiles having the same BFD, which doesn't require relocations, are going to
134 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
135
136 Other objfiles are not going to share a dwarf2_per_bfd with any other
137 objfiles, so they'll have their own version kept in the _objfile_data_key
138 version. */
139static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
140static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
6502dd73 141
f1e6e072
TT
142/* The "aclass" indices for various kinds of computed DWARF symbols. */
143
144static int dwarf2_locexpr_index;
145static int dwarf2_loclist_index;
146static int dwarf2_locexpr_block_index;
147static int dwarf2_loclist_block_index;
148
41144253 149/* Size of .debug_loclists section header for 32-bit DWARF format. */
150#define LOCLIST_HEADER_SIZE32 12
151
152/* Size of .debug_loclists section header for 64-bit DWARF format. */
153#define LOCLIST_HEADER_SIZE64 20
154
d0ce17d8
CT
155/* Size of .debug_rnglists section header for 32-bit DWARF format. */
156#define RNGLIST_HEADER_SIZE32 12
157
158/* Size of .debug_rnglists section header for 64-bit DWARF format. */
159#define RNGLIST_HEADER_SIZE64 20
160
3f563c84
PA
161/* An index into a (C++) symbol name component in a symbol name as
162 recorded in the mapped_index's symbol table. For each C++ symbol
163 in the symbol table, we record one entry for the start of each
164 component in the symbol in a table of name components, and then
165 sort the table, in order to be able to binary search symbol names,
166 ignoring leading namespaces, both completion and regular look up.
167 For example, for symbol "A::B::C", we'll have an entry that points
168 to "A::B::C", another that points to "B::C", and another for "C".
169 Note that function symbols in GDB index have no parameter
170 information, just the function/method names. You can convert a
171 name_component to a "const char *" using the
172 'mapped_index::symbol_name_at(offset_type)' method. */
173
174struct name_component
175{
176 /* Offset in the symbol name where the component starts. Stored as
177 a (32-bit) offset instead of a pointer to save memory and improve
178 locality on 64-bit architectures. */
179 offset_type name_offset;
180
181 /* The symbol's index in the symbol and constant pool tables of a
182 mapped_index. */
183 offset_type idx;
184};
185
44ed8f3e
PA
186/* Base class containing bits shared by both .gdb_index and
187 .debug_name indexes. */
188
189struct mapped_index_base
190{
22ca247e
TT
191 mapped_index_base () = default;
192 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
193
44ed8f3e
PA
194 /* The name_component table (a sorted vector). See name_component's
195 description above. */
196 std::vector<name_component> name_components;
197
198 /* How NAME_COMPONENTS is sorted. */
199 enum case_sensitivity name_components_casing;
200
201 /* Return the number of names in the symbol table. */
202 virtual size_t symbol_name_count () const = 0;
203
204 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
205 virtual const char *symbol_name_at
206 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
44ed8f3e
PA
207
208 /* Return whether the name at IDX in the symbol table should be
209 ignored. */
210 virtual bool symbol_name_slot_invalid (offset_type idx) const
211 {
212 return false;
213 }
214
215 /* Build the symbol name component sorted vector, if we haven't
216 yet. */
fcf23d5b 217 void build_name_components (dwarf2_per_objfile *per_objfile);
44ed8f3e
PA
218
219 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
220 possible matches for LN_NO_PARAMS in the name component
221 vector. */
222 std::pair<std::vector<name_component>::const_iterator,
223 std::vector<name_component>::const_iterator>
3b00ef10 224 find_name_components_bounds (const lookup_name_info &ln_no_params,
fcf23d5b
SM
225 enum language lang,
226 dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
227
228 /* Prevent deleting/destroying via a base class pointer. */
229protected:
230 ~mapped_index_base() = default;
231};
232
9291a0cd
TT
233/* A description of the mapped index. The file format is described in
234 a comment by the code that writes the index. */
fc898b42 235struct mapped_index final : public mapped_index_base
9291a0cd 236{
f00a2de2
PA
237 /* A slot/bucket in the symbol table hash. */
238 struct symbol_table_slot
239 {
240 const offset_type name;
241 const offset_type vec;
242 };
243
559a7a62 244 /* Index data format version. */
3063847f 245 int version = 0;
559a7a62 246
f00a2de2
PA
247 /* The address table data. */
248 gdb::array_view<const gdb_byte> address_table;
b11b1f88 249
3876f04e 250 /* The symbol table, implemented as a hash table. */
f00a2de2 251 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 252
9291a0cd 253 /* A pointer to the constant pool. */
3063847f 254 const char *constant_pool = nullptr;
3f563c84 255
44ed8f3e
PA
256 bool symbol_name_slot_invalid (offset_type idx) const override
257 {
258 const auto &bucket = this->symbol_table[idx];
9ab08412 259 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 260 }
5c58de74 261
3f563c84
PA
262 /* Convenience method to get at the name of the symbol at IDX in the
263 symbol table. */
fcf23d5b
SM
264 const char *symbol_name_at
265 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
f00a2de2 266 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 267
44ed8f3e
PA
268 size_t symbol_name_count () const override
269 { return this->symbol_table.size (); }
9291a0cd
TT
270};
271
927aa2e7
JK
272/* A description of the mapped .debug_names.
273 Uninitialized map has CU_COUNT 0. */
fc898b42 274struct mapped_debug_names final : public mapped_index_base
927aa2e7
JK
275{
276 bfd_endian dwarf5_byte_order;
277 bool dwarf5_is_dwarf64;
278 bool augmentation_is_gdb;
279 uint8_t offset_size;
280 uint32_t cu_count = 0;
281 uint32_t tu_count, bucket_count, name_count;
282 const gdb_byte *cu_table_reordered, *tu_table_reordered;
283 const uint32_t *bucket_table_reordered, *hash_table_reordered;
284 const gdb_byte *name_table_string_offs_reordered;
285 const gdb_byte *name_table_entry_offs_reordered;
286 const gdb_byte *entry_pool;
287
288 struct index_val
289 {
290 ULONGEST dwarf_tag;
291 struct attr
292 {
293 /* Attribute name DW_IDX_*. */
294 ULONGEST dw_idx;
295
296 /* Attribute form DW_FORM_*. */
297 ULONGEST form;
298
299 /* Value if FORM is DW_FORM_implicit_const. */
300 LONGEST implicit_const;
301 };
302 std::vector<attr> attr_vec;
303 };
304
305 std::unordered_map<ULONGEST, index_val> abbrev_map;
306
fcf23d5b
SM
307 const char *namei_to_name
308 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
309
310 /* Implementation of the mapped_index_base virtual interface, for
311 the name_components cache. */
312
fcf23d5b
SM
313 const char *symbol_name_at
314 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
315 { return namei_to_name (idx, per_objfile); }
44ed8f3e
PA
316
317 size_t symbol_name_count () const override
318 { return this->name_count; }
927aa2e7
JK
319};
320
cd4fb1b2 321/* See dwarf2read.h. */
ed2dc618 322
cd4fb1b2 323dwarf2_per_objfile *
ed2dc618
SM
324get_dwarf2_per_objfile (struct objfile *objfile)
325{
5bfd760d 326 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 327}
c906108c 328
251d32d9 329/* Default names of the debugging sections. */
c906108c 330
233a11ab
CS
331/* Note that if the debugging section has been compressed, it might
332 have a name like .zdebug_info. */
333
9cdd5dbd
DE
334static const struct dwarf2_debug_sections dwarf2_elf_names =
335{
251d32d9
TG
336 { ".debug_info", ".zdebug_info" },
337 { ".debug_abbrev", ".zdebug_abbrev" },
338 { ".debug_line", ".zdebug_line" },
339 { ".debug_loc", ".zdebug_loc" },
43988095 340 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 341 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 342 { ".debug_macro", ".zdebug_macro" },
251d32d9 343 { ".debug_str", ".zdebug_str" },
18a8505e 344 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 345 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 346 { ".debug_ranges", ".zdebug_ranges" },
43988095 347 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 348 { ".debug_types", ".zdebug_types" },
3019eac3 349 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
350 { ".debug_frame", ".zdebug_frame" },
351 { ".eh_frame", NULL },
24d3216f 352 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
353 { ".debug_names", ".zdebug_names" },
354 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 355 23
251d32d9 356};
c906108c 357
80626a55 358/* List of DWO/DWP sections. */
3019eac3 359
80626a55 360static const struct dwop_section_names
3019eac3
DE
361{
362 struct dwarf2_section_names abbrev_dwo;
363 struct dwarf2_section_names info_dwo;
364 struct dwarf2_section_names line_dwo;
365 struct dwarf2_section_names loc_dwo;
43988095 366 struct dwarf2_section_names loclists_dwo;
09262596
DE
367 struct dwarf2_section_names macinfo_dwo;
368 struct dwarf2_section_names macro_dwo;
d0ce17d8 369 struct dwarf2_section_names rnglists_dwo;
3019eac3
DE
370 struct dwarf2_section_names str_dwo;
371 struct dwarf2_section_names str_offsets_dwo;
372 struct dwarf2_section_names types_dwo;
80626a55
DE
373 struct dwarf2_section_names cu_index;
374 struct dwarf2_section_names tu_index;
3019eac3 375}
80626a55 376dwop_section_names =
3019eac3
DE
377{
378 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
379 { ".debug_info.dwo", ".zdebug_info.dwo" },
380 { ".debug_line.dwo", ".zdebug_line.dwo" },
381 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 382 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
383 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
384 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
d0ce17d8 385 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
3019eac3
DE
386 { ".debug_str.dwo", ".zdebug_str.dwo" },
387 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
388 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
389 { ".debug_cu_index", ".zdebug_cu_index" },
390 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
391};
392
c906108c
SS
393/* local data types */
394
d0ce17d8
CT
395/* The location list and range list sections (.debug_loclists & .debug_rnglists)
396 begin with a header, which contains the following information. */
397struct loclists_rnglists_header
41144253 398{
399 /* A 4-byte or 12-byte length containing the length of the
400 set of entries for this compilation unit, not including the
401 length field itself. */
402 unsigned int length;
403
404 /* A 2-byte version identifier. */
405 short version;
406
407 /* A 1-byte unsigned integer containing the size in bytes of an address on
408 the target system. */
409 unsigned char addr_size;
410
411 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
412 on the target system. */
413 unsigned char segment_collector_size;
414
415 /* A 4-byte count of the number of offsets that follow the header. */
416 unsigned int offset_entry_count;
417};
418
3da10d80
KS
419/* Type used for delaying computation of method physnames.
420 See comments for compute_delayed_physnames. */
421struct delayed_method_info
422{
423 /* The type to which the method is attached, i.e., its parent class. */
424 struct type *type;
425
426 /* The index of the method in the type's function fieldlists. */
427 int fnfield_index;
428
429 /* The index of the method in the fieldlist. */
430 int index;
431
432 /* The name of the DIE. */
433 const char *name;
434
435 /* The DIE associated with this method. */
436 struct die_info *die;
437};
438
e7c27a73
DJ
439/* Internal state when decoding a particular compilation unit. */
440struct dwarf2_cu
441{
9e021579
SM
442 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
443 dwarf2_per_objfile *per_objfile);
fcd3b13d
SM
444
445 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
446
c24bdb02
KS
447 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
448 Create the set of symtabs used by this TU, or if this TU is sharing
449 symtabs with another TU and the symtabs have already been created
450 then restore those symtabs in the line header.
451 We don't need the pc/line-number mapping for type units. */
452 void setup_type_unit_groups (struct die_info *die);
453
454 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
455 buildsym_compunit constructor. */
456 struct compunit_symtab *start_symtab (const char *name,
457 const char *comp_dir,
458 CORE_ADDR low_pc);
459
460 /* Reset the builder. */
461 void reset_builder () { m_builder.reset (); }
462
293e7e51
SM
463 /* Return a type that is a generic pointer type, the size of which
464 matches the address size given in the compilation unit header for
465 this CU. */
466 struct type *addr_type () const;
467
468 /* Find an integer type the same size as the address size given in
469 the compilation unit header for this CU. UNSIGNED_P controls if
470 the integer is unsigned or not. */
471 struct type *addr_sized_int_type (bool unsigned_p) const;
472
d00adf39 473 /* The header of the compilation unit. */
fcd3b13d 474 struct comp_unit_head header {};
e142c38c 475
d00adf39 476 /* Base address of this compilation unit. */
2b24b6e4 477 gdb::optional<CORE_ADDR> base_address;
d00adf39 478
e142c38c 479 /* The language we are debugging. */
fcd3b13d
SM
480 enum language language = language_unknown;
481 const struct language_defn *language_defn = nullptr;
e142c38c 482
fcd3b13d 483 const char *producer = nullptr;
b0f35d58 484
c24bdb02 485private:
804d2729
TT
486 /* The symtab builder for this CU. This is only non-NULL when full
487 symbols are being read. */
c24bdb02 488 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 489
c24bdb02 490public:
e142c38c
DJ
491 /* The generic symbol table building routines have separate lists for
492 file scope symbols and all all other scopes (local scopes). So
493 we need to select the right one to pass to add_symbol_to_list().
494 We do it by keeping a pointer to the correct list in list_in_scope.
495
496 FIXME: The original dwarf code just treated the file scope as the
497 first local scope, and all other local scopes as nested local
498 scopes, and worked fine. Check to see if we really need to
499 distinguish these in buildsym.c. */
fcd3b13d 500 struct pending **list_in_scope = nullptr;
e142c38c 501
b64f50a1
JK
502 /* Hash table holding all the loaded partial DIEs
503 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 504 htab_t partial_dies = nullptr;
72bf9492
DJ
505
506 /* Storage for things with the same lifetime as this read-in compilation
507 unit, including partial DIEs. */
fcd3b13d 508 auto_obstack comp_unit_obstack;
72bf9492 509
69d751e3 510 /* Backlink to our per_cu entry. */
ae038cb0
DJ
511 struct dwarf2_per_cu_data *per_cu;
512
9e021579 513 /* The dwarf2_per_objfile that owns this. */
976ca316 514 dwarf2_per_objfile *per_objfile;
9e021579 515
ae038cb0 516 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 517 int last_used = 0;
ae038cb0 518
b64f50a1
JK
519 /* A hash table of DIE cu_offset for following references with
520 die_info->offset.sect_off as hash. */
fcd3b13d 521 htab_t die_hash = nullptr;
10b3939b
DJ
522
523 /* Full DIEs if read in. */
fcd3b13d 524 struct die_info *dies = nullptr;
10b3939b
DJ
525
526 /* A set of pointers to dwarf2_per_cu_data objects for compilation
527 units referenced by this one. Only set during full symbol processing;
528 partial symbol tables do not have dependencies. */
fcd3b13d 529 htab_t dependencies = nullptr;
10b3939b 530
cb1df416 531 /* Header data from the line table, during full symbol processing. */
fcd3b13d 532 struct line_header *line_header = nullptr;
4c8aa72d 533 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
5989a64e 534 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
4c8aa72d
PA
535 this is the DW_TAG_compile_unit die for this CU. We'll hold on
536 to the line header as long as this DIE is being processed. See
537 process_die_scope. */
fcd3b13d 538 die_info *line_header_die_owner = nullptr;
cb1df416 539
3da10d80
KS
540 /* A list of methods which need to have physnames computed
541 after all type information has been read. */
c89b44cd 542 std::vector<delayed_method_info> method_list;
3da10d80 543
96408a79 544 /* To be copied to symtab->call_site_htab. */
fcd3b13d 545 htab_t call_site_htab = nullptr;
96408a79 546
034e5797
DE
547 /* Non-NULL if this CU came from a DWO file.
548 There is an invariant here that is important to remember:
549 Except for attributes copied from the top level DIE in the "main"
550 (or "stub") file in preparation for reading the DWO file
18a8505e 551 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
552 Either there isn't a DWO file (in which case this is NULL and the point
553 is moot), or there is and either we're not going to read it (in which
554 case this is NULL) or there is and we are reading it (in which case this
555 is non-NULL). */
fcd3b13d 556 struct dwo_unit *dwo_unit = nullptr;
3019eac3 557
18a8505e 558 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 559 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 560 gdb::optional<ULONGEST> addr_base;
3019eac3 561
18a8505e 562 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 563 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 564 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
565 be used without needing to know whether DWO files are in use or not.
566 N.B. This does not apply to DW_AT_ranges appearing in
567 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
568 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 569 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 570 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 571 ULONGEST ranges_base = 0;
2e3cf129 572
41144253 573 /* The DW_AT_loclists_base attribute if present. */
574 ULONGEST loclist_base = 0;
575
c9317f21
TT
576 /* When reading debug info generated by older versions of rustc, we
577 have to rewrite some union types to be struct types with a
578 variant part. This rewriting must be done after the CU is fully
579 read in, because otherwise at the point of rewriting some struct
580 type might not have been fully processed. So, we keep a list of
581 all such types here and process them after expansion. */
582 std::vector<struct type *> rust_unions;
583
18a8505e
AT
584 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
585 files, the value is implicitly zero. For DWARF 5 version DWO files, the
586 value is often implicit and is the size of the header of
587 .debug_str_offsets section (8 or 4, depending on the address size). */
588 gdb::optional<ULONGEST> str_offsets_base;
589
ae038cb0 590 /* Mark used when releasing cached dies. */
9068261f 591 bool mark : 1;
ae038cb0 592
8be455d7
JK
593 /* This CU references .debug_loc. See the symtab->locations_valid field.
594 This test is imperfect as there may exist optimized debug code not using
595 any location list and still facing inlining issues if handled as
596 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 597 bool has_loclist : 1;
ba919b58 598
9068261f 599 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
600 if all the producer_is_* fields are valid. This information is cached
601 because profiling CU expansion showed excessive time spent in
602 producer_is_gxx_lt_4_6. */
9068261f
AB
603 bool checked_producer : 1;
604 bool producer_is_gxx_lt_4_6 : 1;
605 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 606 bool producer_is_icc : 1;
9068261f 607 bool producer_is_icc_lt_14 : 1;
c258c396 608 bool producer_is_codewarrior : 1;
4d4ec4e5 609
9068261f 610 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
611 debugging info for C++ namespaces. GCC 3.3.x did not produce
612 this information, but later versions do. */
613
9068261f 614 bool processing_has_namespace_info : 1;
d590ff25
YQ
615
616 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
617
618 /* If this CU was inherited by another CU (via specification,
619 abstract_origin, etc), this is the ancestor CU. */
620 dwarf2_cu *ancestor;
621
622 /* Get the buildsym_compunit for this CU. */
623 buildsym_compunit *get_builder ()
624 {
625 /* If this CU has a builder associated with it, use that. */
626 if (m_builder != nullptr)
627 return m_builder.get ();
628
629 /* Otherwise, search ancestors for a valid builder. */
630 if (ancestor != nullptr)
631 return ancestor->get_builder ();
632
633 return nullptr;
634 }
e7c27a73
DJ
635};
636
094b34ac
DE
637/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
638 This includes type_unit_group and quick_file_names. */
639
640struct stmt_list_hash
641{
642 /* The DWO unit this table is from or NULL if there is none. */
643 struct dwo_unit *dwo_unit;
644
645 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 646 sect_offset line_sect_off;
094b34ac
DE
647};
648
5989a64e 649/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
8adb8487
TT
650 an object of this type. This contains elements of type unit groups
651 that can be shared across objfiles. The non-shareable parts are in
652 type_unit_group_unshareable. */
f4dc4d17
DE
653
654struct type_unit_group
655{
0186c6a7 656 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
657 To simplify things we create an artificial CU that "includes" all the
658 type units using this stmt_list so that the rest of the code still has
197400e8 659 a "per_cu" handle on the symtab. */
094b34ac
DE
660 struct dwarf2_per_cu_data per_cu;
661
0186c6a7
DE
662 /* The TUs that share this DW_AT_stmt_list entry.
663 This is added to while parsing type units to build partial symtabs,
664 and is deleted afterwards and not used again. */
a8b3b8e9 665 std::vector<signatured_type *> *tus;
f4dc4d17 666
094b34ac
DE
667 /* The data used to construct the hash key. */
668 struct stmt_list_hash hash;
f4dc4d17
DE
669};
670
73869dc2 671/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
672
673struct dwo_sections
674{
675 struct dwarf2_section_info abbrev;
3019eac3
DE
676 struct dwarf2_section_info line;
677 struct dwarf2_section_info loc;
43988095 678 struct dwarf2_section_info loclists;
09262596
DE
679 struct dwarf2_section_info macinfo;
680 struct dwarf2_section_info macro;
d0ce17d8 681 struct dwarf2_section_info rnglists;
3019eac3
DE
682 struct dwarf2_section_info str;
683 struct dwarf2_section_info str_offsets;
80626a55
DE
684 /* In the case of a virtual DWO file, these two are unused. */
685 struct dwarf2_section_info info;
fd5866f6 686 std::vector<dwarf2_section_info> types;
3019eac3
DE
687};
688
c88ee1f0 689/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
690
691struct dwo_unit
692{
693 /* Backlink to the containing struct dwo_file. */
694 struct dwo_file *dwo_file;
695
696 /* The "id" that distinguishes this CU/TU.
697 .debug_info calls this "dwo_id", .debug_types calls this "signature".
698 Since signatures came first, we stick with it for consistency. */
699 ULONGEST signature;
700
701 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 702 struct dwarf2_section_info *section;
3019eac3 703
9c541725
PA
704 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
705 sect_offset sect_off;
3019eac3
DE
706 unsigned int length;
707
708 /* For types, offset in the type's DIE of the type defined by this TU. */
709 cu_offset type_offset_in_tu;
710};
711
73869dc2
DE
712/* include/dwarf2.h defines the DWP section codes.
713 It defines a max value but it doesn't define a min value, which we
714 use for error checking, so provide one. */
715
716enum dwp_v2_section_ids
717{
718 DW_SECT_MIN = 1
719};
720
80626a55 721/* Data for one DWO file.
57d63ce2
DE
722
723 This includes virtual DWO files (a virtual DWO file is a DWO file as it
724 appears in a DWP file). DWP files don't really have DWO files per se -
725 comdat folding of types "loses" the DWO file they came from, and from
726 a high level view DWP files appear to contain a mass of random types.
727 However, to maintain consistency with the non-DWP case we pretend DWP
728 files contain virtual DWO files, and we assign each TU with one virtual
729 DWO file (generally based on the line and abbrev section offsets -
730 a heuristic that seems to work in practice). */
3019eac3
DE
731
732struct dwo_file
733{
51ac9db5
SM
734 dwo_file () = default;
735 DISABLE_COPY_AND_ASSIGN (dwo_file);
736
18a8505e 737 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
738 For virtual DWO files the name is constructed from the section offsets
739 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
740 from related CU+TUs. */
51ac9db5 741 const char *dwo_name = nullptr;
0ac5b59e
DE
742
743 /* The DW_AT_comp_dir attribute. */
51ac9db5 744 const char *comp_dir = nullptr;
3019eac3 745
80626a55
DE
746 /* The bfd, when the file is open. Otherwise this is NULL.
747 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 748 gdb_bfd_ref_ptr dbfd;
3019eac3 749
73869dc2 750 /* The sections that make up this DWO file.
d2854d8d 751 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
73869dc2 752 sections (for lack of a better name). */
51ac9db5 753 struct dwo_sections sections {};
3019eac3 754
33c5cd75
DB
755 /* The CUs in the file.
756 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
757 an extension to handle LLVM's Link Time Optimization output (where
758 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 759 htab_up cus;
3019eac3
DE
760
761 /* Table of TUs in the file.
762 Each element is a struct dwo_unit. */
b0b6a987 763 htab_up tus;
3019eac3
DE
764};
765
80626a55
DE
766/* These sections are what may appear in a DWP file. */
767
768struct dwp_sections
769{
d2854d8d 770 /* These are used by all DWP versions (1, 2 and 5). */
80626a55
DE
771 struct dwarf2_section_info str;
772 struct dwarf2_section_info cu_index;
773 struct dwarf2_section_info tu_index;
73869dc2 774
d2854d8d 775 /* These are only used by DWP version 2 and version 5 files.
73869dc2
DE
776 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
777 sections are referenced by section number, and are not recorded here.
d2854d8d
CT
778 In DWP version 2 or 5 there is at most one copy of all these sections,
779 each section being (effectively) comprised of the concatenation of all of
780 the individual sections that exist in the version 1 format.
73869dc2
DE
781 To keep the code simple we treat each of these concatenated pieces as a
782 section itself (a virtual section?). */
783 struct dwarf2_section_info abbrev;
784 struct dwarf2_section_info info;
785 struct dwarf2_section_info line;
786 struct dwarf2_section_info loc;
d2854d8d 787 struct dwarf2_section_info loclists;
73869dc2
DE
788 struct dwarf2_section_info macinfo;
789 struct dwarf2_section_info macro;
d2854d8d 790 struct dwarf2_section_info rnglists;
73869dc2
DE
791 struct dwarf2_section_info str_offsets;
792 struct dwarf2_section_info types;
80626a55
DE
793};
794
73869dc2
DE
795/* These sections are what may appear in a virtual DWO file in DWP version 1.
796 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 797
73869dc2 798struct virtual_v1_dwo_sections
80626a55
DE
799{
800 struct dwarf2_section_info abbrev;
801 struct dwarf2_section_info line;
802 struct dwarf2_section_info loc;
803 struct dwarf2_section_info macinfo;
804 struct dwarf2_section_info macro;
805 struct dwarf2_section_info str_offsets;
806 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 807 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
808 struct dwarf2_section_info info_or_types;
809};
810
d2854d8d 811/* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
73869dc2
DE
812 In version 2, the sections of the DWO files are concatenated together
813 and stored in one section of that name. Thus each ELF section contains
814 several "virtual" sections. */
815
d2854d8d 816struct virtual_v2_or_v5_dwo_sections
73869dc2
DE
817{
818 bfd_size_type abbrev_offset;
819 bfd_size_type abbrev_size;
820
821 bfd_size_type line_offset;
822 bfd_size_type line_size;
823
824 bfd_size_type loc_offset;
825 bfd_size_type loc_size;
826
d2854d8d
CT
827 bfd_size_type loclists_offset;
828 bfd_size_type loclists_size;
829
73869dc2
DE
830 bfd_size_type macinfo_offset;
831 bfd_size_type macinfo_size;
832
833 bfd_size_type macro_offset;
834 bfd_size_type macro_size;
835
d2854d8d
CT
836 bfd_size_type rnglists_offset;
837 bfd_size_type rnglists_size;
838
73869dc2
DE
839 bfd_size_type str_offsets_offset;
840 bfd_size_type str_offsets_size;
841
842 /* Each DWP hash table entry records one CU or one TU.
843 That is recorded here, and copied to dwo_unit.section. */
844 bfd_size_type info_or_types_offset;
845 bfd_size_type info_or_types_size;
846};
847
80626a55
DE
848/* Contents of DWP hash tables. */
849
850struct dwp_hash_table
851{
73869dc2 852 uint32_t version, nr_columns;
80626a55 853 uint32_t nr_units, nr_slots;
73869dc2
DE
854 const gdb_byte *hash_table, *unit_table;
855 union
856 {
857 struct
858 {
859 const gdb_byte *indices;
860 } v1;
861 struct
862 {
863 /* This is indexed by column number and gives the id of the section
864 in that column. */
865#define MAX_NR_V2_DWO_SECTIONS \
866 (1 /* .debug_info or .debug_types */ \
867 + 1 /* .debug_abbrev */ \
868 + 1 /* .debug_line */ \
869 + 1 /* .debug_loc */ \
870 + 1 /* .debug_str_offsets */ \
871 + 1 /* .debug_macro or .debug_macinfo */)
872 int section_ids[MAX_NR_V2_DWO_SECTIONS];
873 const gdb_byte *offsets;
874 const gdb_byte *sizes;
875 } v2;
d2854d8d
CT
876 struct
877 {
878 /* This is indexed by column number and gives the id of the section
879 in that column. */
880#define MAX_NR_V5_DWO_SECTIONS \
881 (1 /* .debug_info */ \
882 + 1 /* .debug_abbrev */ \
883 + 1 /* .debug_line */ \
884 + 1 /* .debug_loclists */ \
885 + 1 /* .debug_str_offsets */ \
886 + 1 /* .debug_macro */ \
887 + 1 /* .debug_rnglists */)
888 int section_ids[MAX_NR_V5_DWO_SECTIONS];
889 const gdb_byte *offsets;
890 const gdb_byte *sizes;
891 } v5;
73869dc2 892 } section_pool;
80626a55
DE
893};
894
895/* Data for one DWP file. */
896
897struct dwp_file
898{
400174b1
TT
899 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
900 : name (name_),
901 dbfd (std::move (abfd))
902 {
903 }
904
80626a55
DE
905 /* Name of the file. */
906 const char *name;
907
73869dc2 908 /* File format version. */
400174b1 909 int version = 0;
73869dc2 910
93417882 911 /* The bfd. */
400174b1 912 gdb_bfd_ref_ptr dbfd;
80626a55
DE
913
914 /* Section info for this file. */
400174b1 915 struct dwp_sections sections {};
80626a55 916
57d63ce2 917 /* Table of CUs in the file. */
400174b1 918 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
919
920 /* Table of TUs in the file. */
400174b1 921 const struct dwp_hash_table *tus = nullptr;
80626a55 922
19ac8c2e 923 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
924 htab_up loaded_cus;
925 htab_up loaded_tus;
80626a55 926
73869dc2
DE
927 /* Table to map ELF section numbers to their sections.
928 This is only needed for the DWP V1 file format. */
400174b1
TT
929 unsigned int num_sections = 0;
930 asection **elf_sections = nullptr;
80626a55
DE
931};
932
0963b4bd
MS
933/* Struct used to pass misc. parameters to read_die_and_children, et
934 al. which are used for both .debug_info and .debug_types dies.
935 All parameters here are unchanging for the life of the call. This
dee91e82 936 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
937
938struct die_reader_specs
939{
a32a8923 940 /* The bfd of die_section. */
93311388
DE
941 bfd* abfd;
942
943 /* The CU of the DIE we are parsing. */
944 struct dwarf2_cu *cu;
945
80626a55 946 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
947 struct dwo_file *dwo_file;
948
dee91e82 949 /* The section the die comes from.
3019eac3 950 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
951 struct dwarf2_section_info *die_section;
952
953 /* die_section->buffer. */
d521ce57 954 const gdb_byte *buffer;
f664829e
DE
955
956 /* The end of the buffer. */
957 const gdb_byte *buffer_end;
a2ce51a0 958
685af9cd
TT
959 /* The abbreviation table to use when reading the DIEs. */
960 struct abbrev_table *abbrev_table;
93311388
DE
961};
962
c0ab21c2
TT
963/* A subclass of die_reader_specs that holds storage and has complex
964 constructor and destructor behavior. */
965
966class cutu_reader : public die_reader_specs
967{
968public:
969
ab432490
SM
970 cutu_reader (dwarf2_per_cu_data *this_cu,
971 dwarf2_per_objfile *per_objfile,
c0ab21c2 972 struct abbrev_table *abbrev_table,
2e671100 973 dwarf2_cu *existing_cu,
c0ab21c2
TT
974 bool skip_partial);
975
976 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 977 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
978 struct dwarf2_cu *parent_cu = nullptr,
979 struct dwo_file *dwo_file = nullptr);
980
c0ab21c2
TT
981 DISABLE_COPY_AND_ASSIGN (cutu_reader);
982
983 const gdb_byte *info_ptr = nullptr;
984 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
985 bool dummy_p = false;
986
6751ebae
TT
987 /* Release the new CU, putting it on the chain. This cannot be done
988 for dummy CUs. */
989 void keep ();
990
c0ab21c2 991private:
9e021579
SM
992 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
993 dwarf2_per_objfile *per_objfile,
2e671100 994 dwarf2_cu *existing_cu);
c0ab21c2
TT
995
996 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
997 std::unique_ptr<dwarf2_cu> m_new_cu;
998
999 /* The ordinary abbreviation table. */
1000 abbrev_table_up m_abbrev_table_holder;
1001
1002 /* The DWO abbreviation table. */
1003 abbrev_table_up m_dwo_abbrev_table;
1004};
dee91e82 1005
c906108c 1006/* When we construct a partial symbol table entry we only
0963b4bd 1007 need this much information. */
6f06d47b 1008struct partial_die_info : public allocate_on_obstack
c906108c 1009 {
6f06d47b
YQ
1010 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1011
1012 /* Disable assign but still keep copy ctor, which is needed
1013 load_partial_dies. */
1014 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1015
52356b79
YQ
1016 /* Adjust the partial die before generating a symbol for it. This
1017 function may set the is_external flag or change the DIE's
1018 name. */
1019 void fixup (struct dwarf2_cu *cu);
1020
48fbe735
YQ
1021 /* Read a minimal amount of information into the minimal die
1022 structure. */
1023 const gdb_byte *read (const struct die_reader_specs *reader,
1024 const struct abbrev_info &abbrev,
1025 const gdb_byte *info_ptr);
1026
7d00ffec
TT
1027 /* Compute the name of this partial DIE. This memoizes the
1028 result, so it is safe to call multiple times. */
1029 const char *name (dwarf2_cu *cu);
1030
72bf9492 1031 /* Offset of this DIE. */
6f06d47b 1032 const sect_offset sect_off;
72bf9492
DJ
1033
1034 /* DWARF-2 tag for this DIE. */
6f06d47b 1035 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1036
72bf9492 1037 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1038 const unsigned int has_children : 1;
1039
72bf9492
DJ
1040 unsigned int is_external : 1;
1041 unsigned int is_declaration : 1;
1042 unsigned int has_type : 1;
1043 unsigned int has_specification : 1;
1044 unsigned int has_pc_info : 1;
481860b3 1045 unsigned int may_be_inlined : 1;
72bf9492 1046
0c1b455e
TT
1047 /* This DIE has been marked DW_AT_main_subprogram. */
1048 unsigned int main_subprogram : 1;
1049
72bf9492
DJ
1050 /* Flag set if the SCOPE field of this structure has been
1051 computed. */
1052 unsigned int scope_set : 1;
1053
fa4028e9
JB
1054 /* Flag set if the DIE has a byte_size attribute. */
1055 unsigned int has_byte_size : 1;
1056
ff908ebf
AW
1057 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1058 unsigned int has_const_value : 1;
1059
98bfdba5
PA
1060 /* Flag set if any of the DIE's children are template arguments. */
1061 unsigned int has_template_arguments : 1;
1062
52356b79 1063 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1064 unsigned int fixup_called : 1;
1065
36586728
TT
1066 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1067 unsigned int is_dwz : 1;
1068
1069 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1070 unsigned int spec_is_dwz : 1;
1071
7d00ffec
TT
1072 unsigned int canonical_name : 1;
1073
72bf9492 1074 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1075 sometimes a default name for unnamed DIEs. */
7d00ffec 1076 const char *raw_name = nullptr;
72bf9492 1077
abc72ce4 1078 /* The linkage name, if present. */
6f06d47b 1079 const char *linkage_name = nullptr;
abc72ce4 1080
72bf9492
DJ
1081 /* The scope to prepend to our children. This is generally
1082 allocated on the comp_unit_obstack, so will disappear
1083 when this compilation unit leaves the cache. */
6f06d47b 1084 const char *scope = nullptr;
72bf9492 1085
95554aad
TT
1086 /* Some data associated with the partial DIE. The tag determines
1087 which field is live. */
1088 union
1089 {
1090 /* The location description associated with this DIE, if any. */
1091 struct dwarf_block *locdesc;
1092 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1093 sect_offset sect_off;
6f06d47b 1094 } d {};
72bf9492
DJ
1095
1096 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1097 CORE_ADDR lowpc = 0;
1098 CORE_ADDR highpc = 0;
72bf9492 1099
93311388 1100 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1101 DW_AT_sibling, if any. */
48fbe735
YQ
1102 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1103 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1104 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1105
1106 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1107 DW_AT_specification (or DW_AT_abstract_origin or
1108 DW_AT_extension). */
6f06d47b 1109 sect_offset spec_offset {};
72bf9492
DJ
1110
1111 /* Pointers to this DIE's parent, first child, and next sibling,
1112 if any. */
6f06d47b
YQ
1113 struct partial_die_info *die_parent = nullptr;
1114 struct partial_die_info *die_child = nullptr;
1115 struct partial_die_info *die_sibling = nullptr;
1116
1117 friend struct partial_die_info *
1118 dwarf2_cu::find_partial_die (sect_offset sect_off);
1119
1120 private:
1121 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1122 partial_die_info (sect_offset sect_off)
1123 : partial_die_info (sect_off, DW_TAG_padding, 0)
1124 {
1125 }
1126
1127 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1128 int has_children_)
1129 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1130 {
1131 is_external = 0;
1132 is_declaration = 0;
1133 has_type = 0;
1134 has_specification = 0;
1135 has_pc_info = 0;
1136 may_be_inlined = 0;
1137 main_subprogram = 0;
1138 scope_set = 0;
1139 has_byte_size = 0;
1140 has_const_value = 0;
1141 has_template_arguments = 0;
1142 fixup_called = 0;
1143 is_dwz = 0;
1144 spec_is_dwz = 0;
7d00ffec 1145 canonical_name = 0;
6f06d47b 1146 }
c906108c
SS
1147 };
1148
c906108c
SS
1149/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1150 but this would require a corresponding change in unpack_field_as_long
1151 and friends. */
1152static int bits_per_byte = 8;
1153
9c6a1327
TT
1154struct variant_part_builder;
1155
1156/* When reading a variant, we track a bit more information about the
1157 field, and store it in an object of this type. */
2ddeaf8a
TT
1158
1159struct variant_field
1160{
9c6a1327
TT
1161 int first_field = -1;
1162 int last_field = -1;
1163
1164 /* A variant can contain other variant parts. */
1165 std::vector<variant_part_builder> variant_parts;
1166
2ddeaf8a
TT
1167 /* If we see a DW_TAG_variant, then this will be set if this is the
1168 default branch. */
9c6a1327
TT
1169 bool default_branch = false;
1170 /* If we see a DW_AT_discr_value, then this will be the discriminant
1171 value. */
1172 ULONGEST discriminant_value = 0;
1173 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1174 data. */
1175 struct dwarf_block *discr_list_data = nullptr;
1176};
1177
1178/* This represents a DW_TAG_variant_part. */
1179
1180struct variant_part_builder
1181{
1182 /* The offset of the discriminant field. */
1183 sect_offset discriminant_offset {};
1184
1185 /* Variants that are direct children of this variant part. */
1186 std::vector<variant_field> variants;
1187
1188 /* True if we're currently reading a variant. */
1189 bool processing_variant = false;
2ddeaf8a
TT
1190};
1191
52059ffd
TT
1192struct nextfield
1193{
be2daae6
TT
1194 int accessibility = 0;
1195 int virtuality = 0;
9c6a1327
TT
1196 /* Variant parts need to find the discriminant, which is a DIE
1197 reference. We track the section offset of each field to make
1198 this link. */
1199 sect_offset offset;
be2daae6 1200 struct field field {};
52059ffd
TT
1201};
1202
1203struct fnfieldlist
1204{
be2daae6
TT
1205 const char *name = nullptr;
1206 std::vector<struct fn_field> fnfields;
52059ffd
TT
1207};
1208
c906108c
SS
1209/* The routines that read and process dies for a C struct or C++ class
1210 pass lists of data member fields and lists of member function fields
1211 in an instance of a field_info structure, as defined below. */
1212struct field_info
2de01bdb
SM
1213{
1214 /* List of data member and baseclasses fields. */
1215 std::vector<struct nextfield> fields;
1216 std::vector<struct nextfield> baseclasses;
1217
1218 /* Set if the accessibility of one of the fields is not public. */
264fc0e2 1219 bool non_public_fields = false;
2de01bdb
SM
1220
1221 /* Member function fieldlist array, contains name of possibly overloaded
1222 member function, number of overloaded member functions and a pointer
1223 to the head of the member function field chain. */
1224 std::vector<struct fnfieldlist> fnfieldlists;
1225
1226 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1227 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1228 std::vector<struct decl_field> typedef_field_list;
1229
1230 /* Nested types defined by this class and the number of elements in this
1231 list. */
1232 std::vector<struct decl_field> nested_types_list;
1233
1234 /* If non-null, this is the variant part we are currently
1235 reading. */
1236 variant_part_builder *current_variant_part = nullptr;
1237 /* This holds all the top-level variant parts attached to the type
1238 we're reading. */
1239 std::vector<variant_part_builder> variant_parts;
1240
1241 /* Return the total number of fields (including baseclasses). */
1242 int nfields () const
c5aa993b 1243 {
2de01bdb
SM
1244 return fields.size () + baseclasses.size ();
1245 }
1246};
c906108c 1247
ae038cb0
DJ
1248/* Loaded secondary compilation units are kept in memory until they
1249 have not been referenced for the processing of this many
1250 compilation units. Set this to zero to disable caching. Cache
1251 sizes of up to at least twenty will improve startup time for
1252 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1253static int dwarf_max_cache_age = 5;
920d2a44 1254static void
b4f54984
DE
1255show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1256 struct cmd_list_element *c, const char *value)
920d2a44 1257{
3e43a32a 1258 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1259 "DWARF compilation units is %s.\n"),
920d2a44
AC
1260 value);
1261}
4390d890 1262\f
c906108c
SS
1263/* local function prototypes */
1264
918dd910
JK
1265static void dwarf2_find_base_address (struct die_info *die,
1266 struct dwarf2_cu *cu);
1267
891813be 1268static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1269 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1270 const char *name);
0018ea6f 1271
f1902523
JK
1272static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1273 const gdb_byte *info_ptr,
3e225074 1274 struct die_info *type_unit_die);
f1902523 1275
976ca316 1276static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
c906108c 1277
72bf9492
DJ
1278static void scan_partial_symbols (struct partial_die_info *,
1279 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1280 int, struct dwarf2_cu *);
c906108c 1281
72bf9492
DJ
1282static void add_partial_symbol (struct partial_die_info *,
1283 struct dwarf2_cu *);
63d06c5c 1284
72bf9492
DJ
1285static void add_partial_namespace (struct partial_die_info *pdi,
1286 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1287 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1288
5d7cb8df 1289static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1290 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1291 struct dwarf2_cu *cu);
1292
72bf9492
DJ
1293static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1294 struct dwarf2_cu *cu);
91c24f0a 1295
bc30ff58
JB
1296static void add_partial_subprogram (struct partial_die_info *pdi,
1297 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1298 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1299
d521ce57 1300static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1301
dee91e82 1302static struct partial_die_info *load_partial_dies
d521ce57 1303 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1304
fb816e8b
TV
1305/* A pair of partial_die_info and compilation unit. */
1306struct cu_partial_die_info
1307{
1308 /* The compilation unit of the partial_die_info. */
1309 struct dwarf2_cu *cu;
1310 /* A partial_die_info. */
1311 struct partial_die_info *pdi;
122cf0f2
AB
1312
1313 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1314 : cu (cu),
1315 pdi (pdi)
405feb71 1316 { /* Nothing. */ }
122cf0f2
AB
1317
1318private:
1319 cu_partial_die_info () = delete;
fb816e8b
TV
1320};
1321
122cf0f2
AB
1322static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1323 struct dwarf2_cu *);
72bf9492 1324
d521ce57
TT
1325static const gdb_byte *read_attribute (const struct die_reader_specs *,
1326 struct attribute *, struct attr_abbrev *,
7a5f294d 1327 const gdb_byte *);
18a8505e
AT
1328
1329static void read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 1330 struct attribute *attr, dwarf_tag tag);
18a8505e
AT
1331
1332static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1333
976ca316
SM
1334static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1335 dwarf2_section_info *, sect_offset);
f4dc4d17 1336
ed2dc618 1337static const char *read_indirect_string
976ca316 1338 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
ed2dc618 1339 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1340
ed2dc618 1341static const char *read_indirect_string_at_offset
976ca316 1342 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
927aa2e7 1343
d521ce57
TT
1344static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1345 const gdb_byte *,
3019eac3
DE
1346 unsigned int *);
1347
18a8505e
AT
1348static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1349 ULONGEST str_index);
1350
1351static const char *read_stub_str_index (struct dwarf2_cu *cu,
1352 ULONGEST str_index);
3019eac3 1353
e142c38c 1354static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1355
e142c38c
DJ
1356static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1357 struct dwarf2_cu *);
c906108c 1358
7d45c7c3 1359static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
dda83cd7 1360 struct dwarf2_cu *cu);
7d45c7c3 1361
a084a2a6
AT
1362static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1363
05cf31d1 1364static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
dda83cd7 1365 struct dwarf2_cu *cu);
05cf31d1 1366
e142c38c 1367static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1368
e142c38c 1369static struct die_info *die_specification (struct die_info *die,
f2f0e013 1370 struct dwarf2_cu **);
63d06c5c 1371
9c541725 1372static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1373 struct dwarf2_cu *cu);
debd256d 1374
f3f5162e 1375static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1376 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1377 CORE_ADDR, int decode_mapping);
c906108c 1378
804d2729
TT
1379static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1380 const char *);
c906108c 1381
a14ed312 1382static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1383 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1384
ff39bb5e 1385static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1386 struct dwarf2_cu *);
c906108c 1387
ff39bb5e 1388static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1389 struct type *type,
1390 const char *name,
1391 struct obstack *obstack,
12df843f 1392 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1393 const gdb_byte **bytes,
98bfdba5 1394 struct dwarf2_locexpr_baton **baton);
2df3850c 1395
57567375
TT
1396static struct type *read_subrange_index_type (struct die_info *die,
1397 struct dwarf2_cu *cu);
1398
e7c27a73 1399static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1400
b4ba55a1
JB
1401static int need_gnat_info (struct dwarf2_cu *);
1402
3e43a32a
MS
1403static struct type *die_descriptive_type (struct die_info *,
1404 struct dwarf2_cu *);
b4ba55a1
JB
1405
1406static void set_descriptive_type (struct type *, struct die_info *,
1407 struct dwarf2_cu *);
1408
e7c27a73
DJ
1409static struct type *die_containing_type (struct die_info *,
1410 struct dwarf2_cu *);
c906108c 1411
ff39bb5e 1412static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1413 struct dwarf2_cu *);
c906108c 1414
f792889a 1415static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1416
673bfd45
DE
1417static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1418
0d5cff50 1419static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1420
6e70227d 1421static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1422 const char *suffix, int physname,
1423 struct dwarf2_cu *cu);
63d06c5c 1424
e7c27a73 1425static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1426
348e048f
DE
1427static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1428
e7c27a73 1429static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1430
e7c27a73 1431static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1432
96408a79
SA
1433static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1434
71a3c369
TT
1435static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1436
41144253 1437/* Return the .debug_loclists section to use for cu. */
1438static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1439
d0ce17d8
CT
1440/* Return the .debug_rnglists section to use for cu. */
1441static struct dwarf2_section_info *cu_debug_rnglists_section
1442 (struct dwarf2_cu *cu, dwarf_tag tag);
1443
3a2b436a 1444/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1445 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1446enum pc_bounds_kind
1447{
e385593e 1448 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1449 PC_BOUNDS_NOT_PRESENT,
1450
e385593e
JK
1451 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1452 were present but they do not form a valid range of PC addresses. */
1453 PC_BOUNDS_INVALID,
1454
3a2b436a
JK
1455 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1456 PC_BOUNDS_RANGES,
1457
1458 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1459 PC_BOUNDS_HIGH_LOW,
1460};
1461
1462static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1463 CORE_ADDR *, CORE_ADDR *,
1464 struct dwarf2_cu *,
891813be 1465 dwarf2_psymtab *);
c906108c 1466
fae299cd
DC
1467static void get_scope_pc_bounds (struct die_info *,
1468 CORE_ADDR *, CORE_ADDR *,
1469 struct dwarf2_cu *);
1470
801e3a5b 1471static void dwarf2_record_block_ranges (struct die_info *, struct block *,
dda83cd7 1472 CORE_ADDR, struct dwarf2_cu *);
801e3a5b 1473
a14ed312 1474static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1475 struct dwarf2_cu *);
c906108c 1476
a14ed312 1477static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1478 struct type *, struct dwarf2_cu *);
c906108c 1479
a14ed312 1480static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1481 struct die_info *, struct type *,
e7c27a73 1482 struct dwarf2_cu *);
c906108c 1483
a14ed312 1484static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1485 struct type *,
1486 struct dwarf2_cu *);
c906108c 1487
134d01f1 1488static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1489
e7c27a73 1490static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1491
e7c27a73 1492static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1493
5d7cb8df
JK
1494static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1495
804d2729 1496static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1497
27aa8d6a
SW
1498static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1499
74921315
KS
1500static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1501
f55ee35c
JK
1502static struct type *read_module_type (struct die_info *die,
1503 struct dwarf2_cu *cu);
1504
38d518c9 1505static const char *namespace_name (struct die_info *die,
e142c38c 1506 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1507
134d01f1 1508static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1509
7d79de9a
TT
1510static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1511 bool * = nullptr);
c906108c 1512
6e70227d 1513static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1514 struct dwarf2_cu *);
1515
bf6af496 1516static struct die_info *read_die_and_siblings_1
d521ce57 1517 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1518 struct die_info *);
639d11d3 1519
dee91e82 1520static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1521 const gdb_byte *info_ptr,
1522 const gdb_byte **new_info_ptr,
639d11d3
DC
1523 struct die_info *parent);
1524
d521ce57
TT
1525static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1526 struct die_info **, const gdb_byte *,
3e225074 1527 int);
3019eac3 1528
d521ce57 1529static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1530 struct die_info **, const gdb_byte *);
93311388 1531
e7c27a73 1532static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1533
15d034d0 1534static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1535 struct objfile *);
71c25dea 1536
15d034d0 1537static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1538
15d034d0 1539static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1540 struct die_info *die,
1541 struct dwarf2_cu *cu);
1542
ca69b9e6
DE
1543static const char *dwarf2_physname (const char *name, struct die_info *die,
1544 struct dwarf2_cu *cu);
1545
e142c38c 1546static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1547 struct dwarf2_cu **);
9219021c 1548
d97bc12b
DE
1549static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1550
1551static void dump_die_for_error (struct die_info *);
1552
1553static void dump_die_1 (struct ui_file *, int level, int max_level,
1554 struct die_info *);
c906108c 1555
d97bc12b 1556/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1557
51545339 1558static void store_in_ref_table (struct die_info *,
10b3939b 1559 struct dwarf2_cu *);
c906108c 1560
348e048f 1561static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1562 const struct attribute *,
348e048f
DE
1563 struct dwarf2_cu **);
1564
10b3939b 1565static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1566 const struct attribute *,
f2f0e013 1567 struct dwarf2_cu **);
c906108c 1568
348e048f 1569static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1570 const struct attribute *,
348e048f
DE
1571 struct dwarf2_cu **);
1572
ac9ec31b
DE
1573static struct type *get_signatured_type (struct die_info *, ULONGEST,
1574 struct dwarf2_cu *);
1575
1576static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1577 const struct attribute *,
ac9ec31b
DE
1578 struct dwarf2_cu *);
1579
ab432490
SM
1580static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1581 dwarf2_per_objfile *per_objfile);
348e048f 1582
ab432490
SM
1583static void read_signatured_type (signatured_type *sig_type,
1584 dwarf2_per_objfile *per_objfile);
348e048f 1585
63e43d3a
PMR
1586static int attr_to_dynamic_prop (const struct attribute *attr,
1587 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1588 struct dynamic_prop *prop, struct type *type);
63e43d3a 1589
c906108c
SS
1590/* memory allocation interface */
1591
7b5a2f43 1592static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1593
b60c80d6 1594static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1595
43f3e411 1596static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1597
8cf6f0b1
TT
1598static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1599 struct dwarf2_loclist_baton *baton,
ff39bb5e 1600 const struct attribute *attr);
8cf6f0b1 1601
ff39bb5e 1602static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1603 struct symbol *sym,
f1e6e072
TT
1604 struct dwarf2_cu *cu,
1605 int is_block);
4c2df51b 1606
d521ce57
TT
1607static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1608 const gdb_byte *info_ptr,
1609 struct abbrev_info *abbrev);
4bb7a0a7 1610
72bf9492
DJ
1611static hashval_t partial_die_hash (const void *item);
1612
1613static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1614
ae038cb0 1615static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618 1616 (sect_offset sect_off, unsigned int offset_in_dwz,
976ca316 1617 dwarf2_per_objfile *per_objfile);
ae038cb0 1618
9816fde3 1619static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1620 struct die_info *comp_unit_die,
1621 enum language pretend_language);
93311388 1622
f792889a 1623static struct type *set_die_type (struct die_info *, struct type *,
57567375 1624 struct dwarf2_cu *, bool = false);
1c379e20 1625
976ca316 1626static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
ae038cb0 1627
976ca316 1628static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1fd400ff 1629
ab432490
SM
1630static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1631 dwarf2_per_objfile *per_objfile,
4a636814 1632 dwarf2_cu *existing_cu,
ab432490
SM
1633 bool skip_partial,
1634 enum language pretend_language);
10b3939b 1635
8fc0b21d 1636static void process_full_comp_unit (dwarf2_cu *cu,
47b14e86 1637 enum language pretend_language);
10b3939b 1638
8fc0b21d 1639static void process_full_type_unit (dwarf2_cu *cu,
47b14e86 1640 enum language pretend_language);
f4dc4d17 1641
10b3939b
DJ
1642static void dwarf2_add_dependence (struct dwarf2_cu *,
1643 struct dwarf2_per_cu_data *);
1644
ae038cb0
DJ
1645static void dwarf2_mark (struct dwarf2_cu *);
1646
b64f50a1 1647static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1648 dwarf2_per_cu_data *per_cu,
1649 dwarf2_per_objfile *per_objfile);
673bfd45 1650
f792889a 1651static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1652
120ce1b5
SM
1653static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1654 dwarf2_per_objfile *per_objfile,
95554aad
TT
1655 enum language pretend_language);
1656
976ca316 1657static void process_queue (dwarf2_per_objfile *per_objfile);
9291a0cd 1658
b303c6f6
AB
1659/* Class, the destructor of which frees all allocated queue entries. This
1660 will only have work to do if an error was thrown while processing the
1661 dwarf. If no error was thrown then the queue entries should have all
1662 been processed, and freed, as we went along. */
1663
1664class dwarf2_queue_guard
1665{
1666public:
39856def
TT
1667 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1668 : m_per_objfile (per_objfile)
1669 {
1670 }
b303c6f6
AB
1671
1672 /* Free any entries remaining on the queue. There should only be
1673 entries left if we hit an error while processing the dwarf. */
1674 ~dwarf2_queue_guard ()
1675 {
39856def
TT
1676 /* Ensure that no memory is allocated by the queue. */
1677 std::queue<dwarf2_queue_item> empty;
5989a64e 1678 std::swap (m_per_objfile->per_bfd->queue, empty);
39856def 1679 }
b303c6f6 1680
39856def 1681 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1682
39856def
TT
1683private:
1684 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1685};
1686
39856def
TT
1687dwarf2_queue_item::~dwarf2_queue_item ()
1688{
1689 /* Anything still marked queued is likely to be in an
1690 inconsistent state, so discard it. */
1691 if (per_cu->queued)
1692 {
7188ed02 1693 per_objfile->remove_cu (per_cu);
39856def
TT
1694 per_cu->queued = 0;
1695 }
1696}
1697
d721ba37
PA
1698/* The return type of find_file_and_directory. Note, the enclosed
1699 string pointers are only valid while this object is valid. */
1700
1701struct file_and_directory
1702{
1703 /* The filename. This is never NULL. */
1704 const char *name;
1705
1706 /* The compilation directory. NULL if not known. If we needed to
1707 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1708 points directly to the DW_AT_comp_dir string attribute owned by
1709 the obstack that owns the DIE. */
1710 const char *comp_dir;
1711
1712 /* If we needed to build a new string for comp_dir, this is what
1713 owns the storage. */
1714 std::string comp_dir_storage;
1715};
1716
1717static file_and_directory find_file_and_directory (struct die_info *die,
1718 struct dwarf2_cu *cu);
9291a0cd 1719
298e9637 1720static htab_up allocate_signatured_type_table ();
1fd400ff 1721
298e9637 1722static htab_up allocate_dwo_unit_table ();
3019eac3 1723
57d63ce2 1724static struct dwo_unit *lookup_dwo_unit_in_dwp
976ca316
SM
1725 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1726 const char *comp_dir, ULONGEST signature, int is_debug_types);
a2ce51a0 1727
976ca316 1728static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
a2ce51a0 1729
3019eac3 1730static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1731 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1732 ULONGEST signature);
3019eac3
DE
1733
1734static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1735 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1736
1b555f17 1737static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
89e63ee4 1738
263db9a1
TT
1739/* A unique pointer to a dwo_file. */
1740
51ac9db5 1741typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1742
976ca316 1743static void process_cu_includes (dwarf2_per_objfile *per_objfile);
95554aad 1744
1b80a9fa 1745static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1746
1747static void free_line_header_voidp (void *arg);
4390d890
DE
1748\f
1749/* Various complaints about symbol reading that don't abort the process. */
1750
4390d890
DE
1751static void
1752dwarf2_debug_line_missing_file_complaint (void)
1753{
b98664d3 1754 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1755}
1756
1757static void
1758dwarf2_debug_line_missing_end_sequence_complaint (void)
1759{
b98664d3 1760 complaint (_(".debug_line section has line "
4390d890
DE
1761 "program sequence without an end"));
1762}
1763
1764static void
1765dwarf2_complex_location_expr_complaint (void)
1766{
b98664d3 1767 complaint (_("location expression too complex"));
4390d890
DE
1768}
1769
1770static void
1771dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1772 int arg3)
1773{
b98664d3 1774 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1775 arg1, arg2, arg3);
1776}
1777
4390d890
DE
1778static void
1779dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1780{
b98664d3 1781 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1782 arg1, arg2);
1783}
527f3840
JK
1784
1785/* Hash function for line_header_hash. */
1786
1787static hashval_t
1788line_header_hash (const struct line_header *ofs)
1789{
9c541725 1790 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1791}
1792
1793/* Hash function for htab_create_alloc_ex for line_header_hash. */
1794
1795static hashval_t
1796line_header_hash_voidp (const void *item)
1797{
9a3c8263 1798 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1799
1800 return line_header_hash (ofs);
1801}
1802
1803/* Equality function for line_header_hash. */
1804
1805static int
1806line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1807{
9a3c8263
SM
1808 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1809 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1810
9c541725 1811 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1812 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1813}
1814
4390d890 1815\f
9291a0cd 1816
330cdd98
PA
1817/* See declaration. */
1818
5989a64e
SM
1819dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1820 bool can_copy_)
c3699833
SM
1821 : obfd (obfd),
1822 can_copy (can_copy_)
330cdd98
PA
1823{
1824 if (names == NULL)
1825 names = &dwarf2_elf_names;
1826
330cdd98
PA
1827 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1828 locate_sections (obfd, sec, *names);
1829}
1830
5989a64e 1831dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98 1832{
b76e467d 1833 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1834 per_cu->imported_symtabs_free ();
fc8e7e75 1835
b2bdb8cf 1836 for (signatured_type *sig_type : all_type_units)
ae640021 1837 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1838
5989a64e 1839 /* Everything else should be on this->obstack. */
330cdd98
PA
1840}
1841
7188ed02 1842/* See read.h. */
330cdd98
PA
1843
1844void
7188ed02 1845dwarf2_per_objfile::remove_all_cus ()
330cdd98 1846{
7188ed02
SM
1847 for (auto pair : m_dwarf2_cus)
1848 delete pair.second;
330cdd98 1849
7188ed02 1850 m_dwarf2_cus.clear ();
330cdd98
PA
1851}
1852
11ed8cad
TT
1853/* A helper class that calls free_cached_comp_units on
1854 destruction. */
1855
1856class free_cached_comp_units
1857{
1858public:
1859
1860 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1861 : m_per_objfile (per_objfile)
1862 {
1863 }
1864
1865 ~free_cached_comp_units ()
1866 {
7188ed02 1867 m_per_objfile->remove_all_cus ();
11ed8cad
TT
1868 }
1869
1870 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1871
1872private:
1873
1874 dwarf2_per_objfile *m_per_objfile;
1875};
1876
af758d11
SM
1877/* See read.h. */
1878
1879bool
1880dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1881{
1882 gdb_assert (per_cu->index < this->m_symtabs.size ());
1883
1884 return this->m_symtabs[per_cu->index] != nullptr;
1885}
1886
1887/* See read.h. */
1888
1889compunit_symtab *
1890dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1891{
1892 gdb_assert (per_cu->index < this->m_symtabs.size ());
1893
1894 return this->m_symtabs[per_cu->index];
1895}
1896
1897/* See read.h. */
1898
1899void
1900dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1901 compunit_symtab *symtab)
1902{
1903 gdb_assert (per_cu->index < this->m_symtabs.size ());
1904 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1905
1906 this->m_symtabs[per_cu->index] = symtab;
1907}
1908
c906108c 1909/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1910 information and return true if we have enough to do something.
1911 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1912 ELF names are used. CAN_COPY is true for formats where symbol
1913 interposition is possible and so symbol values must follow copy
1914 relocation rules. */
c906108c
SS
1915
1916int
251d32d9 1917dwarf2_has_info (struct objfile *objfile,
dda83cd7 1918 const struct dwarf2_debug_sections *names,
4b610737 1919 bool can_copy)
c906108c 1920{
97cbe998
SDJ
1921 if (objfile->flags & OBJF_READNEVER)
1922 return 0;
1923
976ca316 1924 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 1925
976ca316 1926 if (per_objfile == NULL)
5989a64e 1927 {
17ee85fc
TT
1928 dwarf2_per_bfd *per_bfd;
1929
1930 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
dda83cd7 1931 doesn't require relocations and if there aren't partial symbols
17ee85fc
TT
1932 from some other reader. */
1933 if (!objfile_has_partial_symbols (objfile)
1934 && !gdb_bfd_requires_relocations (objfile->obfd))
1935 {
1936 /* See if one has been created for this BFD yet. */
1937 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1938
1939 if (per_bfd == nullptr)
1940 {
1941 /* No, create it now. */
1942 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1943 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1944 }
1945 }
1946 else
1947 {
1948 /* No sharing possible, create one specifically for this objfile. */
1949 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1950 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1951 }
5989a64e 1952
976ca316 1953 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 1954 }
5bfd760d 1955
976ca316
SM
1956 return (!per_objfile->per_bfd->info.is_virtual
1957 && per_objfile->per_bfd->info.s.section != NULL
1958 && !per_objfile->per_bfd->abbrev.is_virtual
1959 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1960}
1961
251d32d9
TG
1962/* When loading sections, we look either for uncompressed section or for
1963 compressed section names. */
233a11ab
CS
1964
1965static int
251d32d9 1966section_is_p (const char *section_name,
dda83cd7 1967 const struct dwarf2_section_names *names)
233a11ab 1968{
251d32d9
TG
1969 if (names->normal != NULL
1970 && strcmp (section_name, names->normal) == 0)
1971 return 1;
1972 if (names->compressed != NULL
1973 && strcmp (section_name, names->compressed) == 0)
1974 return 1;
1975 return 0;
233a11ab
CS
1976}
1977
330cdd98 1978/* See declaration. */
c906108c 1979
330cdd98 1980void
5989a64e
SM
1981dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1982 const dwarf2_debug_sections &names)
c906108c 1983{
fd361982 1984 flagword aflag = bfd_section_flags (sectp);
251d32d9 1985
dc7650b8
JK
1986 if ((aflag & SEC_HAS_CONTENTS) == 0)
1987 {
1988 }
950b7495
KS
1989 else if (elf_section_data (sectp)->this_hdr.sh_size
1990 > bfd_get_file_size (abfd))
1991 {
1992 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1993 warning (_("Discarding section %s which has a section size (%s"
1994 ") larger than the file size [in module %s]"),
1995 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1996 bfd_get_filename (abfd));
1997 }
330cdd98 1998 else if (section_is_p (sectp->name, &names.info))
c906108c 1999 {
330cdd98 2000 this->info.s.section = sectp;
fd361982 2001 this->info.size = bfd_section_size (sectp);
c906108c 2002 }
330cdd98 2003 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2004 {
330cdd98 2005 this->abbrev.s.section = sectp;
fd361982 2006 this->abbrev.size = bfd_section_size (sectp);
c906108c 2007 }
330cdd98 2008 else if (section_is_p (sectp->name, &names.line))
c906108c 2009 {
330cdd98 2010 this->line.s.section = sectp;
fd361982 2011 this->line.size = bfd_section_size (sectp);
c906108c 2012 }
330cdd98 2013 else if (section_is_p (sectp->name, &names.loc))
c906108c 2014 {
330cdd98 2015 this->loc.s.section = sectp;
fd361982 2016 this->loc.size = bfd_section_size (sectp);
c906108c 2017 }
330cdd98 2018 else if (section_is_p (sectp->name, &names.loclists))
43988095 2019 {
330cdd98 2020 this->loclists.s.section = sectp;
fd361982 2021 this->loclists.size = bfd_section_size (sectp);
43988095 2022 }
330cdd98 2023 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2024 {
330cdd98 2025 this->macinfo.s.section = sectp;
fd361982 2026 this->macinfo.size = bfd_section_size (sectp);
c906108c 2027 }
330cdd98 2028 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2029 {
330cdd98 2030 this->macro.s.section = sectp;
fd361982 2031 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2032 }
330cdd98 2033 else if (section_is_p (sectp->name, &names.str))
c906108c 2034 {
330cdd98 2035 this->str.s.section = sectp;
fd361982 2036 this->str.size = bfd_section_size (sectp);
c906108c 2037 }
18a8505e
AT
2038 else if (section_is_p (sectp->name, &names.str_offsets))
2039 {
2040 this->str_offsets.s.section = sectp;
2041 this->str_offsets.size = bfd_section_size (sectp);
2042 }
330cdd98 2043 else if (section_is_p (sectp->name, &names.line_str))
43988095 2044 {
330cdd98 2045 this->line_str.s.section = sectp;
fd361982 2046 this->line_str.size = bfd_section_size (sectp);
43988095 2047 }
330cdd98 2048 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2049 {
330cdd98 2050 this->addr.s.section = sectp;
fd361982 2051 this->addr.size = bfd_section_size (sectp);
3019eac3 2052 }
330cdd98 2053 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2054 {
330cdd98 2055 this->frame.s.section = sectp;
fd361982 2056 this->frame.size = bfd_section_size (sectp);
b6af0555 2057 }
330cdd98 2058 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2059 {
330cdd98 2060 this->eh_frame.s.section = sectp;
fd361982 2061 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2062 }
330cdd98 2063 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2064 {
330cdd98 2065 this->ranges.s.section = sectp;
fd361982 2066 this->ranges.size = bfd_section_size (sectp);
af34e669 2067 }
330cdd98 2068 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2069 {
330cdd98 2070 this->rnglists.s.section = sectp;
fd361982 2071 this->rnglists.size = bfd_section_size (sectp);
43988095 2072 }
330cdd98 2073 else if (section_is_p (sectp->name, &names.types))
348e048f 2074 {
8b70b953
TT
2075 struct dwarf2_section_info type_section;
2076
2077 memset (&type_section, 0, sizeof (type_section));
049412e3 2078 type_section.s.section = sectp;
fd361982 2079 type_section.size = bfd_section_size (sectp);
8b70b953 2080
fd5866f6 2081 this->types.push_back (type_section);
348e048f 2082 }
330cdd98 2083 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2084 {
330cdd98 2085 this->gdb_index.s.section = sectp;
fd361982 2086 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2087 }
927aa2e7
JK
2088 else if (section_is_p (sectp->name, &names.debug_names))
2089 {
2090 this->debug_names.s.section = sectp;
fd361982 2091 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2092 }
2093 else if (section_is_p (sectp->name, &names.debug_aranges))
2094 {
2095 this->debug_aranges.s.section = sectp;
fd361982 2096 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2097 }
dce234bc 2098
fd361982
AM
2099 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2100 && bfd_section_vma (sectp) == 0)
330cdd98 2101 this->has_section_at_zero = true;
c906108c
SS
2102}
2103
dce234bc 2104/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2105 SECTION_NAME. */
af34e669 2106
dce234bc 2107void
3017a003 2108dwarf2_get_section_info (struct objfile *objfile,
dda83cd7
SM
2109 enum dwarf2_section_enum sect,
2110 asection **sectp, const gdb_byte **bufp,
2111 bfd_size_type *sizep)
dce234bc 2112{
976ca316 2113 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 2114 struct dwarf2_section_info *info;
a3b2a86b
TT
2115
2116 /* We may see an objfile without any DWARF, in which case we just
2117 return nothing. */
976ca316 2118 if (per_objfile == NULL)
a3b2a86b
TT
2119 {
2120 *sectp = NULL;
2121 *bufp = NULL;
2122 *sizep = 0;
2123 return;
2124 }
3017a003
TG
2125 switch (sect)
2126 {
2127 case DWARF2_DEBUG_FRAME:
976ca316 2128 info = &per_objfile->per_bfd->frame;
3017a003
TG
2129 break;
2130 case DWARF2_EH_FRAME:
976ca316 2131 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
2132 break;
2133 default:
2134 gdb_assert_not_reached ("unexpected section");
2135 }
dce234bc 2136
96b79293 2137 info->read (objfile);
dce234bc 2138
96b79293 2139 *sectp = info->get_bfd_section ();
dce234bc
PP
2140 *bufp = info->buffer;
2141 *sizep = info->size;
2142}
2143
36586728
TT
2144/* A helper function to find the sections for a .dwz file. */
2145
2146static void
5bb6e9dd 2147locate_dwz_sections (bfd *abfd, asection *sectp, dwz_file *dwz_file)
36586728 2148{
36586728
TT
2149 /* Note that we only support the standard ELF names, because .dwz
2150 is ELF-only (at the time of writing). */
2151 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2152 {
049412e3 2153 dwz_file->abbrev.s.section = sectp;
fd361982 2154 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2155 }
2156 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2157 {
049412e3 2158 dwz_file->info.s.section = sectp;
fd361982 2159 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2160 }
2161 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2162 {
049412e3 2163 dwz_file->str.s.section = sectp;
fd361982 2164 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2165 }
2166 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2167 {
049412e3 2168 dwz_file->line.s.section = sectp;
fd361982 2169 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2170 }
2171 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2172 {
049412e3 2173 dwz_file->macro.s.section = sectp;
fd361982 2174 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2175 }
2ec9a5e0
TT
2176 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2177 {
049412e3 2178 dwz_file->gdb_index.s.section = sectp;
fd361982 2179 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2180 }
927aa2e7
JK
2181 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2182 {
2183 dwz_file->debug_names.s.section = sectp;
fd361982 2184 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2185 }
36586728
TT
2186}
2187
c4973306 2188/* See dwarf2read.h. */
36586728 2189
c4973306 2190struct dwz_file *
c3699833 2191dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
36586728 2192{
36586728 2193 const char *filename;
acd13123 2194 bfd_size_type buildid_len_arg;
dc294be5
TT
2195 size_t buildid_len;
2196 bfd_byte *buildid;
36586728 2197
c3699833
SM
2198 if (per_bfd->dwz_file != NULL)
2199 return per_bfd->dwz_file.get ();
36586728 2200
4db1a1dc 2201 bfd_set_error (bfd_error_no_error);
791afaa2 2202 gdb::unique_xmalloc_ptr<char> data
c3699833 2203 (bfd_get_alt_debug_link_info (per_bfd->obfd,
791afaa2 2204 &buildid_len_arg, &buildid));
4db1a1dc
TT
2205 if (data == NULL)
2206 {
2207 if (bfd_get_error () == bfd_error_no_error)
2208 return NULL;
2209 error (_("could not read '.gnu_debugaltlink' section: %s"),
2210 bfd_errmsg (bfd_get_error ()));
2211 }
791afaa2
TT
2212
2213 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2214
acd13123
TT
2215 buildid_len = (size_t) buildid_len_arg;
2216
791afaa2 2217 filename = data.get ();
d721ba37
PA
2218
2219 std::string abs_storage;
36586728
TT
2220 if (!IS_ABSOLUTE_PATH (filename))
2221 {
14278e1f 2222 gdb::unique_xmalloc_ptr<char> abs
c3699833 2223 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
36586728 2224
14278e1f 2225 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2226 filename = abs_storage.c_str ();
36586728
TT
2227 }
2228
dc294be5
TT
2229 /* First try the file name given in the section. If that doesn't
2230 work, try to use the build-id instead. */
ad80db5b 2231 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
dc294be5 2232 if (dwz_bfd != NULL)
36586728 2233 {
192b62ce 2234 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2235 dwz_bfd.reset (nullptr);
36586728
TT
2236 }
2237
dc294be5
TT
2238 if (dwz_bfd == NULL)
2239 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2240
0d79cdc4
AM
2241 if (dwz_bfd == nullptr)
2242 {
2243 gdb::unique_xmalloc_ptr<char> alt_filename;
c3699833 2244 const char *origname = bfd_get_filename (per_bfd->obfd);
0d79cdc4
AM
2245
2246 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2247 buildid_len,
2248 origname,
2249 &alt_filename));
2250
2251 if (fd.get () >= 0)
2252 {
2253 /* File successfully retrieved from server. */
ad80db5b 2254 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2255
2256 if (dwz_bfd == nullptr)
2257 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2258 alt_filename.get ());
2259 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2260 dwz_bfd.reset (nullptr);
2261 }
2262 }
2263
dc294be5
TT
2264 if (dwz_bfd == NULL)
2265 error (_("could not find '.gnu_debugaltlink' file for %s"),
c3699833 2266 bfd_get_filename (per_bfd->obfd));
dc294be5 2267
7ff8cb8c
TT
2268 std::unique_ptr<struct dwz_file> result
2269 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2270
5bb6e9dd
TT
2271 for (asection *sec : gdb_bfd_sections (result->dwz_bfd))
2272 locate_dwz_sections (result->dwz_bfd.get (), sec, result.get ());
36586728 2273
c3699833
SM
2274 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2275 per_bfd->dwz_file = std::move (result);
2276 return per_bfd->dwz_file.get ();
36586728 2277}
9291a0cd 2278\f
7b9f3c50
DE
2279/* DWARF quick_symbols_functions support. */
2280
2281/* TUs can share .debug_line entries, and there can be a lot more TUs than
2282 unique line tables, so we maintain a separate table of all .debug_line
2283 derived entries to support the sharing.
2284 All the quick functions need is the list of file names. We discard the
2285 line_header when we're done and don't need to record it here. */
2286struct quick_file_names
2287{
094b34ac
DE
2288 /* The data used to construct the hash key. */
2289 struct stmt_list_hash hash;
7b9f3c50
DE
2290
2291 /* The number of entries in file_names, real_names. */
2292 unsigned int num_file_names;
2293
2294 /* The file names from the line table, after being run through
2295 file_full_name. */
2296 const char **file_names;
2297
2298 /* The file names from the line table after being run through
2299 gdb_realpath. These are computed lazily. */
2300 const char **real_names;
2301};
2302
2303/* When using the index (and thus not using psymtabs), each CU has an
2304 object of this type. This is used to hold information needed by
2305 the various "quick" methods. */
2306struct dwarf2_per_cu_quick_data
2307{
2308 /* The file table. This can be NULL if there was no file table
2309 or it's currently not read in.
5989a64e 2310 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2311 struct quick_file_names *file_names;
2312
7b9f3c50
DE
2313 /* A temporary mark bit used when iterating over all CUs in
2314 expand_symtabs_matching. */
2315 unsigned int mark : 1;
2316
2317 /* True if we've tried to read the file table and found there isn't one.
2318 There will be no point in trying to read it again next time. */
2319 unsigned int no_file_data : 1;
2320};
2321
094b34ac
DE
2322/* Utility hash function for a stmt_list_hash. */
2323
2324static hashval_t
2325hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2326{
2327 hashval_t v = 0;
2328
2329 if (stmt_list_hash->dwo_unit != NULL)
2330 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2331 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2332 return v;
2333}
2334
2335/* Utility equality function for a stmt_list_hash. */
2336
2337static int
2338eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2339 const struct stmt_list_hash *rhs)
2340{
2341 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2342 return 0;
2343 if (lhs->dwo_unit != NULL
2344 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2345 return 0;
2346
9c541725 2347 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2348}
2349
7b9f3c50
DE
2350/* Hash function for a quick_file_names. */
2351
2352static hashval_t
2353hash_file_name_entry (const void *e)
2354{
9a3c8263
SM
2355 const struct quick_file_names *file_data
2356 = (const struct quick_file_names *) e;
7b9f3c50 2357
094b34ac 2358 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2359}
2360
2361/* Equality function for a quick_file_names. */
2362
2363static int
2364eq_file_name_entry (const void *a, const void *b)
2365{
9a3c8263
SM
2366 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2367 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2368
094b34ac 2369 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2370}
2371
2372/* Delete function for a quick_file_names. */
2373
2374static void
2375delete_file_name_entry (void *e)
2376{
9a3c8263 2377 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2378 int i;
2379
2380 for (i = 0; i < file_data->num_file_names; ++i)
2381 {
2382 xfree ((void*) file_data->file_names[i]);
2383 if (file_data->real_names)
2384 xfree ((void*) file_data->real_names[i]);
2385 }
2386
45940949
TT
2387 /* The space for the struct itself lives on the obstack, so we don't
2388 free it here. */
7b9f3c50
DE
2389}
2390
2391/* Create a quick_file_names hash table. */
2392
5895093f 2393static htab_up
7b9f3c50
DE
2394create_quick_file_names_table (unsigned int nr_initial_entries)
2395{
5895093f
TT
2396 return htab_up (htab_create_alloc (nr_initial_entries,
2397 hash_file_name_entry, eq_file_name_entry,
2398 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2399}
9291a0cd 2400
ab432490
SM
2401/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2402 function is unrelated to symtabs, symtab would have to be created afterwards.
2403 You should call age_cached_comp_units after processing the CU. */
918dd910 2404
1b555f17 2405static dwarf2_cu *
ab432490
SM
2406load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2407 bool skip_partial)
918dd910 2408{
3019eac3 2409 if (per_cu->is_debug_types)
ab432490 2410 load_full_type_unit (per_cu, per_objfile);
918dd910 2411 else
4a636814
SM
2412 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2413 skip_partial, language_minimal);
918dd910 2414
7188ed02
SM
2415 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2416 if (cu == nullptr)
1b555f17 2417 return nullptr; /* Dummy CU. */
2dc860c0 2418
7188ed02 2419 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2420
7188ed02 2421 return cu;
918dd910
JK
2422}
2423
1350c3b4 2424/* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2fdf6df6 2425
9291a0cd 2426static void
97a1449a 2427dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2428 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2429{
f4dc4d17
DE
2430 /* Skip type_unit_groups, reading the type units they contain
2431 is handled elsewhere. */
197400e8 2432 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2433 return;
2434
b303c6f6
AB
2435 /* The destructor of dwarf2_queue_guard frees any entries left on
2436 the queue. After this point we're guaranteed to leave this function
2437 with the dwarf queue empty. */
bf6e5d01 2438 dwarf2_queue_guard q_guard (per_objfile);
9291a0cd 2439
976ca316 2440 if (!per_objfile->symtab_set_p (per_cu))
95554aad 2441 {
976ca316
SM
2442 queue_comp_unit (per_cu, per_objfile, language_minimal);
2443 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
89e63ee4
DE
2444
2445 /* If we just loaded a CU from a DWO, and we're working with an index
2446 that may badly handle TUs, load all the TUs in that DWO as well.
2447 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2448 if (!per_cu->is_debug_types
1b555f17
SM
2449 && cu != NULL
2450 && cu->dwo_unit != NULL
976ca316
SM
2451 && per_objfile->per_bfd->index_table != NULL
2452 && per_objfile->per_bfd->index_table->version <= 7
89e63ee4 2453 /* DWP files aren't supported yet. */
976ca316 2454 && get_dwp_file (per_objfile) == NULL)
1b555f17 2455 queue_and_load_all_dwo_tus (cu);
95554aad 2456 }
9291a0cd 2457
976ca316 2458 process_queue (per_objfile);
9291a0cd
TT
2459
2460 /* Age the cache, releasing compilation units that have not
2461 been used recently. */
976ca316 2462 per_objfile->age_comp_units ();
9291a0cd
TT
2463}
2464
97a1449a
SM
2465/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2466 the per-objfile for which this symtab is instantiated.
2467
2468 Returns the resulting symbol table. */
2fdf6df6 2469
43f3e411 2470static struct compunit_symtab *
97a1449a 2471dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2472 dwarf2_per_objfile *per_objfile,
97a1449a 2473 bool skip_partial)
9291a0cd 2474{
976ca316 2475 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2476
976ca316 2477 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2478 {
976ca316 2479 free_cached_comp_units freer (per_objfile);
c83dd867 2480 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2481 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2482 process_cu_includes (per_objfile);
9291a0cd 2483 }
f194fefb 2484
976ca316 2485 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2486}
2487
ff4c9fec 2488/* See declaration. */
f4dc4d17 2489
ff4c9fec 2490dwarf2_per_cu_data *
5989a64e 2491dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2492{
b76e467d 2493 if (index >= this->all_comp_units.size ())
ff4c9fec 2494 {
b76e467d 2495 index -= this->all_comp_units.size ();
b2bdb8cf 2496 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2497 return &this->all_type_units[index]->per_cu;
2498 }
f4dc4d17 2499
ff4c9fec
SM
2500 return this->all_comp_units[index];
2501}
f4dc4d17 2502
ff4c9fec 2503/* See declaration. */
2fdf6df6 2504
ff4c9fec 2505dwarf2_per_cu_data *
5989a64e 2506dwarf2_per_bfd::get_cu (int index)
1fd400ff 2507{
b76e467d 2508 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2509
ff4c9fec 2510 return this->all_comp_units[index];
f4dc4d17
DE
2511}
2512
ff4c9fec 2513/* See declaration. */
f4dc4d17 2514
ff4c9fec 2515signatured_type *
5989a64e 2516dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2517{
b2bdb8cf 2518 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2519
ff4c9fec 2520 return this->all_type_units[index];
1fd400ff
TT
2521}
2522
d3473f0c
TT
2523/* See read.h. */
2524
2525dwarf2_per_cu_data *
5989a64e 2526dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2527{
2528 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2529 result->per_bfd = this;
d3473f0c
TT
2530 result->index = m_num_psymtabs++;
2531 return result;
2532}
2533
2534/* See read.h. */
2535
2536signatured_type *
5989a64e 2537dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2538{
2539 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2540 result->per_cu.per_bfd = this;
d3473f0c
TT
2541 result->per_cu.index = m_num_psymtabs++;
2542 return result;
2543}
2544
168c9250 2545/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2546 obstack, and constructed with the specified field values. */
4b514bc8
JK
2547
2548static dwarf2_per_cu_data *
168c9250
SM
2549create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2550 struct dwarf2_section_info *section,
2551 int is_dwz,
2552 sect_offset sect_off, ULONGEST length)
4b514bc8 2553{
168c9250 2554 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2555 the_cu->sect_off = sect_off;
2556 the_cu->length = length;
4b514bc8 2557 the_cu->section = section;
168c9250 2558 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2559 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2560 the_cu->is_dwz = is_dwz;
2561 return the_cu;
2562}
2563
2ec9a5e0
TT
2564/* A helper for create_cus_from_index that handles a given list of
2565 CUs. */
2fdf6df6 2566
74a0d9f6 2567static void
168c9250 2568create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2569 const gdb_byte *cu_list, offset_type n_elements,
2570 struct dwarf2_section_info *section,
b76e467d 2571 int is_dwz)
9291a0cd 2572{
12359b5e 2573 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2574 {
74a0d9f6 2575 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2576
2577 sect_offset sect_off
2578 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2579 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2580 cu_list += 2 * 8;
2581
b76e467d 2582 dwarf2_per_cu_data *per_cu
168c9250
SM
2583 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2584 length);
2585 per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2586 }
9291a0cd
TT
2587}
2588
2ec9a5e0 2589/* Read the CU list from the mapped index, and use it to create all
168c9250 2590 the CU objects for PER_BFD. */
2ec9a5e0 2591
74a0d9f6 2592static void
168c9250 2593create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2594 const gdb_byte *cu_list, offset_type cu_list_elements,
2595 const gdb_byte *dwz_list, offset_type dwz_elements)
2596{
168c9250
SM
2597 gdb_assert (per_bfd->all_comp_units.empty ());
2598 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2599
168c9250
SM
2600 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2601 &per_bfd->info, 0);
2ec9a5e0
TT
2602
2603 if (dwz_elements == 0)
74a0d9f6 2604 return;
2ec9a5e0 2605
168c9250
SM
2606 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2607 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2608 &dwz->info, 1);
2ec9a5e0
TT
2609}
2610
1fd400ff 2611/* Create the signatured type hash table from the index. */
673bfd45 2612
74a0d9f6 2613static void
12359b5e 2614create_signatured_type_table_from_index
168c9250
SM
2615 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2616 const gdb_byte *bytes, offset_type elements)
1fd400ff 2617{
168c9250
SM
2618 gdb_assert (per_bfd->all_type_units.empty ());
2619 per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2620
298e9637 2621 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2622
12359b5e 2623 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2624 {
52dc124a 2625 struct signatured_type *sig_type;
9c541725 2626 ULONGEST signature;
1fd400ff 2627 void **slot;
9c541725 2628 cu_offset type_offset_in_tu;
1fd400ff 2629
74a0d9f6 2630 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2631 sect_offset sect_off
2632 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2633 type_offset_in_tu
2634 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2635 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2636 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2637 bytes += 3 * 8;
2638
168c9250 2639 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2640 sig_type->signature = signature;
9c541725 2641 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2642 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2643 sig_type->per_cu.section = section;
9c541725 2644 sig_type->per_cu.sect_off = sect_off;
52dc124a 2645 sig_type->per_cu.v.quick
168c9250 2646 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2647 struct dwarf2_per_cu_quick_data);
2648
b0b6a987 2649 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2650 *slot = sig_type;
1fd400ff 2651
168c9250 2652 per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2653 }
2654
168c9250 2655 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2656}
2657
927aa2e7
JK
2658/* Create the signatured type hash table from .debug_names. */
2659
2660static void
2661create_signatured_type_table_from_debug_names
976ca316 2662 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2663 const mapped_debug_names &map,
2664 struct dwarf2_section_info *section,
2665 struct dwarf2_section_info *abbrev_section)
2666{
976ca316 2667 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2668
96b79293
TT
2669 section->read (objfile);
2670 abbrev_section->read (objfile);
927aa2e7 2671
976ca316
SM
2672 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2673 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2674
298e9637 2675 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2676
2677 for (uint32_t i = 0; i < map.tu_count; ++i)
2678 {
2679 struct signatured_type *sig_type;
927aa2e7 2680 void **slot;
927aa2e7
JK
2681
2682 sect_offset sect_off
2683 = (sect_offset) (extract_unsigned_integer
2684 (map.tu_table_reordered + i * map.offset_size,
2685 map.offset_size,
2686 map.dwarf5_byte_order));
2687
2688 comp_unit_head cu_header;
976ca316 2689 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2690 abbrev_section,
927aa2e7
JK
2691 section->buffer + to_underlying (sect_off),
2692 rcuh_kind::TYPE);
2693
976ca316 2694 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2695 sig_type->signature = cu_header.signature;
2696 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2697 sig_type->per_cu.is_debug_types = 1;
2698 sig_type->per_cu.section = section;
2699 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2700 sig_type->per_cu.v.quick
976ca316 2701 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2702 struct dwarf2_per_cu_quick_data);
2703
b0b6a987 2704 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2705 *slot = sig_type;
2706
976ca316 2707 per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2708 }
2709
976ca316 2710 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2711}
2712
9291a0cd
TT
2713/* Read the address map data from the mapped index, and use it to
2714 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2715
9291a0cd 2716static void
976ca316 2717create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2718 struct mapped_index *index)
9291a0cd 2719{
976ca316 2720 struct objfile *objfile = per_objfile->objfile;
08feed99 2721 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2722 const gdb_byte *iter, *end;
9291a0cd 2723 struct addrmap *mutable_map;
9291a0cd
TT
2724 CORE_ADDR baseaddr;
2725
8268c778
PA
2726 auto_obstack temp_obstack;
2727
9291a0cd
TT
2728 mutable_map = addrmap_create_mutable (&temp_obstack);
2729
f00a2de2
PA
2730 iter = index->address_table.data ();
2731 end = iter + index->address_table.size ();
9291a0cd 2732
b3b3bada 2733 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2734
2735 while (iter < end)
2736 {
2737 ULONGEST hi, lo, cu_index;
2738 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2739 iter += 8;
2740 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2741 iter += 8;
2742 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2743 iter += 4;
f652bce2 2744
24a55014 2745 if (lo > hi)
f652bce2 2746 {
b98664d3 2747 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2748 hex_string (lo), hex_string (hi));
24a55014 2749 continue;
f652bce2 2750 }
24a55014 2751
976ca316 2752 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2753 {
b98664d3 2754 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2755 (unsigned) cu_index);
24a55014 2756 continue;
f652bce2 2757 }
24a55014 2758
79748972
TT
2759 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2760 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2761 addrmap_set_empty (mutable_map, lo, hi - 1,
976ca316 2762 per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2763 }
2764
d320c2b5 2765 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2766 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2767}
2768
927aa2e7
JK
2769/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2770 populate the objfile's psymtabs_addrmap. */
2771
2772static void
976ca316 2773create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2774 struct dwarf2_section_info *section)
2775{
976ca316 2776 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2777 bfd *abfd = objfile->obfd;
08feed99 2778 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2779 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2780
2781 auto_obstack temp_obstack;
2782 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2783
2784 std::unordered_map<sect_offset,
2785 dwarf2_per_cu_data *,
2786 gdb::hash_enum<sect_offset>>
2787 debug_info_offset_to_per_cu;
976ca316 2788 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 2789 {
927aa2e7
JK
2790 const auto insertpair
2791 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2792 if (!insertpair.second)
2793 {
2794 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2795 "debug_info_offset %s, ignoring .debug_aranges."),
2796 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2797 return;
2798 }
2799 }
2800
96b79293 2801 section->read (objfile);
927aa2e7
JK
2802
2803 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2804
2805 const gdb_byte *addr = section->buffer;
2806
2807 while (addr < section->buffer + section->size)
2808 {
2809 const gdb_byte *const entry_addr = addr;
2810 unsigned int bytes_read;
2811
2812 const LONGEST entry_length = read_initial_length (abfd, addr,
2813 &bytes_read);
2814 addr += bytes_read;
2815
2816 const gdb_byte *const entry_end = addr + entry_length;
2817 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2818 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2819 if (addr + entry_length > section->buffer + section->size)
2820 {
47e3f474 2821 warning (_("Section .debug_aranges in %s entry at offset %s "
dda83cd7 2822 "length %s exceeds section length %s, "
927aa2e7 2823 "ignoring .debug_aranges."),
47e3f474
TV
2824 objfile_name (objfile),
2825 plongest (entry_addr - section->buffer),
927aa2e7
JK
2826 plongest (bytes_read + entry_length),
2827 pulongest (section->size));
2828 return;
2829 }
2830
2831 /* The version number. */
2832 const uint16_t version = read_2_bytes (abfd, addr);
2833 addr += 2;
2834 if (version != 2)
2835 {
47e3f474 2836 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2837 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2838 objfile_name (objfile),
2839 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2840 return;
2841 }
2842
2843 const uint64_t debug_info_offset
2844 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2845 addr += offset_size;
2846 const auto per_cu_it
2847 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2848 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2849 {
47e3f474 2850 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2851 "debug_info_offset %s does not exists, "
2852 "ignoring .debug_aranges."),
47e3f474
TV
2853 objfile_name (objfile),
2854 plongest (entry_addr - section->buffer),
927aa2e7
JK
2855 pulongest (debug_info_offset));
2856 return;
2857 }
2858 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2859
2860 const uint8_t address_size = *addr++;
2861 if (address_size < 1 || address_size > 8)
2862 {
47e3f474 2863 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2864 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2865 objfile_name (objfile),
2866 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2867 return;
2868 }
2869
2870 const uint8_t segment_selector_size = *addr++;
2871 if (segment_selector_size != 0)
2872 {
47e3f474 2873 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2874 "segment_selector_size %u is not supported, "
2875 "ignoring .debug_aranges."),
47e3f474
TV
2876 objfile_name (objfile),
2877 plongest (entry_addr - section->buffer),
927aa2e7
JK
2878 segment_selector_size);
2879 return;
2880 }
2881
2882 /* Must pad to an alignment boundary that is twice the address
dda83cd7
SM
2883 size. It is undocumented by the DWARF standard but GCC does
2884 use it. */
927aa2e7
JK
2885 for (size_t padding = ((-(addr - section->buffer))
2886 & (2 * address_size - 1));
dda83cd7 2887 padding > 0; padding--)
927aa2e7
JK
2888 if (*addr++ != 0)
2889 {
47e3f474 2890 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2891 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2892 objfile_name (objfile),
2893 plongest (entry_addr - section->buffer));
927aa2e7
JK
2894 return;
2895 }
2896
2897 for (;;)
2898 {
2899 if (addr + 2 * address_size > entry_end)
2900 {
47e3f474 2901 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2902 "address list is not properly terminated, "
2903 "ignoring .debug_aranges."),
47e3f474
TV
2904 objfile_name (objfile),
2905 plongest (entry_addr - section->buffer));
927aa2e7
JK
2906 return;
2907 }
2908 ULONGEST start = extract_unsigned_integer (addr, address_size,
2909 dwarf5_byte_order);
2910 addr += address_size;
2911 ULONGEST length = extract_unsigned_integer (addr, address_size,
2912 dwarf5_byte_order);
2913 addr += address_size;
2914 if (start == 0 && length == 0)
2915 break;
976ca316 2916 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
2917 {
2918 /* Symbol was eliminated due to a COMDAT group. */
2919 continue;
2920 }
2921 ULONGEST end = start + length;
79748972
TT
2922 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2923 - baseaddr);
2924 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2925 - baseaddr);
927aa2e7
JK
2926 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2927 }
2928 }
2929
d320c2b5 2930 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2931 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2932}
2933
9291a0cd
TT
2934/* Find a slot in the mapped index INDEX for the object named NAME.
2935 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2936 constant pool and return true. If NAME cannot be found, return
2937 false. */
2fdf6df6 2938
109483d9 2939static bool
9291a0cd
TT
2940find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2941 offset_type **vec_out)
2942{
0cf03b49 2943 offset_type hash;
9291a0cd 2944 offset_type slot, step;
559a7a62 2945 int (*cmp) (const char *, const char *);
9291a0cd 2946
791afaa2 2947 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2948 if (current_language->la_language == language_cplus
45280282
IB
2949 || current_language->la_language == language_fortran
2950 || current_language->la_language == language_d)
0cf03b49
JK
2951 {
2952 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2953 not contain any. */
a8719064 2954
72998fb3 2955 if (strchr (name, '(') != NULL)
0cf03b49 2956 {
109483d9 2957 without_params = cp_remove_params (name);
0cf03b49 2958
72998fb3 2959 if (without_params != NULL)
791afaa2 2960 name = without_params.get ();
0cf03b49
JK
2961 }
2962 }
2963
559a7a62 2964 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2965 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2966 simulate our NAME being searched is also lowercased. */
2967 hash = mapped_index_string_hash ((index->version == 4
dda83cd7 2968 && case_sensitivity == case_sensitive_off
559a7a62
JK
2969 ? 5 : index->version),
2970 name);
2971
f00a2de2
PA
2972 slot = hash & (index->symbol_table.size () - 1);
2973 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2974 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2975
2976 for (;;)
2977 {
9291a0cd 2978 const char *str;
f00a2de2
PA
2979
2980 const auto &bucket = index->symbol_table[slot];
2981 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2982 return false;
9291a0cd 2983
f00a2de2 2984 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2985 if (!cmp (name, str))
9291a0cd
TT
2986 {
2987 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2988 + MAYBE_SWAP (bucket.vec));
109483d9 2989 return true;
9291a0cd
TT
2990 }
2991
f00a2de2 2992 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2993 }
2994}
2995
4485a1c1
SM
2996/* A helper function that reads the .gdb_index from BUFFER and fills
2997 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2998 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2999 ok to use deprecated sections.
3000
3001 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3002 out parameters that are filled in with information about the CU and
3003 TU lists in the section.
3004
4485a1c1 3005 Returns true if all went well, false otherwise. */
2fdf6df6 3006
d33bc52e 3007static bool
3810f182 3008read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
3009 bool deprecated_ok,
3010 gdb::array_view<const gdb_byte> buffer,
3011 struct mapped_index *map,
3012 const gdb_byte **cu_list,
3013 offset_type *cu_list_elements,
3014 const gdb_byte **types_list,
3015 offset_type *types_list_elements)
3016{
3017 const gdb_byte *addr = &buffer[0];
82430852 3018
9291a0cd 3019 /* Version check. */
4485a1c1 3020 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3021 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3022 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3023 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3024 indices. */
831adc1f 3025 if (version < 4)
481860b3
GB
3026 {
3027 static int warning_printed = 0;
3028 if (!warning_printed)
3029 {
3030 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3031 filename);
481860b3
GB
3032 warning_printed = 1;
3033 }
3034 return 0;
3035 }
3036 /* Index version 4 uses a different hash function than index version
3037 5 and later.
3038
3039 Versions earlier than 6 did not emit psymbols for inlined
3040 functions. Using these files will cause GDB not to be able to
3041 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3042 indices unless the user has done
3043 "set use-deprecated-index-sections on". */
2ec9a5e0 3044 if (version < 6 && !deprecated_ok)
481860b3
GB
3045 {
3046 static int warning_printed = 0;
3047 if (!warning_printed)
3048 {
e615022a
DE
3049 warning (_("\
3050Skipping deprecated .gdb_index section in %s.\n\
3051Do \"set use-deprecated-index-sections on\" before the file is read\n\
3052to use the section anyway."),
2ec9a5e0 3053 filename);
481860b3
GB
3054 warning_printed = 1;
3055 }
3056 return 0;
3057 }
796a7ff8 3058 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3059 of the TU (for symbols coming from TUs),
3060 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3061 Plus gold-generated indices can have duplicate entries for global symbols,
3062 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3063 These are just performance bugs, and we can't distinguish gdb-generated
3064 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3065
481860b3 3066 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3067 longer backward compatible. */
796a7ff8 3068 if (version > 8)
594e8718 3069 return 0;
9291a0cd 3070
559a7a62 3071 map->version = version;
9291a0cd 3072
4485a1c1 3073 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3074
4485a1c1 3075 int i = 0;
2ec9a5e0
TT
3076 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3077 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3078 / 8);
1fd400ff
TT
3079 ++i;
3080
2ec9a5e0
TT
3081 *types_list = addr + MAYBE_SWAP (metadata[i]);
3082 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3083 - MAYBE_SWAP (metadata[i]))
3084 / 8);
987d643c 3085 ++i;
1fd400ff 3086
f00a2de2
PA
3087 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3088 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3089 map->address_table
3090 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3091 ++i;
3092
f00a2de2
PA
3093 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3094 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3095 map->symbol_table
3096 = gdb::array_view<mapped_index::symbol_table_slot>
3097 ((mapped_index::symbol_table_slot *) symbol_table,
3098 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3099
f00a2de2 3100 ++i;
f9d83a0b 3101 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3102
2ec9a5e0
TT
3103 return 1;
3104}
3105
4485a1c1
SM
3106/* Callback types for dwarf2_read_gdb_index. */
3107
3108typedef gdb::function_view
5989a64e 3109 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3110 get_gdb_index_contents_ftype;
3111typedef gdb::function_view
3112 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3113 get_gdb_index_contents_dwz_ftype;
3114
927aa2e7 3115/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3116 elements of all the CUs and return 1. Otherwise, return 0. */
3117
3118static int
4485a1c1 3119dwarf2_read_gdb_index
976ca316 3120 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
3121 get_gdb_index_contents_ftype get_gdb_index_contents,
3122 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3123{
2ec9a5e0
TT
3124 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3125 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3126 struct dwz_file *dwz;
976ca316 3127 struct objfile *objfile = per_objfile->objfile;
f8c41851 3128 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 3129
4485a1c1 3130 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 3131 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
3132
3133 if (main_index_contents.empty ())
3134 return 0;
3135
3063847f 3136 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3137 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3138 use_deprecated_index_sections,
3139 main_index_contents, map.get (), &cu_list,
3140 &cu_list_elements, &types_list,
3141 &types_list_elements))
2ec9a5e0
TT
3142 return 0;
3143
0fefef59 3144 /* Don't use the index if it's empty. */
3063847f 3145 if (map->symbol_table.empty ())
0fefef59
DE
3146 return 0;
3147
2ec9a5e0
TT
3148 /* If there is a .dwz file, read it so we can get its CU list as
3149 well. */
f8c41851 3150 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 3151 if (dwz != NULL)
2ec9a5e0 3152 {
2ec9a5e0
TT
3153 struct mapped_index dwz_map;
3154 const gdb_byte *dwz_types_ignore;
3155 offset_type dwz_types_elements_ignore;
3156
4485a1c1
SM
3157 gdb::array_view<const gdb_byte> dwz_index_content
3158 = get_gdb_index_contents_dwz (objfile, dwz);
3159
3160 if (dwz_index_content.empty ())
3161 return 0;
3162
3810f182 3163 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3164 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3165 &dwz_list, &dwz_list_elements,
3166 &dwz_types_ignore,
3167 &dwz_types_elements_ignore))
2ec9a5e0
TT
3168 {
3169 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3170 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3171 return 0;
3172 }
3173 }
3174
f8c41851
SM
3175 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3176 dwz_list_elements);
1fd400ff 3177
8b70b953
TT
3178 if (types_list_elements)
3179 {
8b70b953
TT
3180 /* We can only handle a single .debug_types when we have an
3181 index. */
f8c41851 3182 if (per_bfd->types.size () != 1)
8b70b953
TT
3183 return 0;
3184
f8c41851 3185 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 3186
f8c41851 3187 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 3188 types_list_elements);
8b70b953 3189 }
9291a0cd 3190
976ca316 3191 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 3192
f8c41851
SM
3193 per_bfd->index_table = std::move (map);
3194 per_bfd->using_index = 1;
3195 per_bfd->quick_file_names_table =
3196 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3197
3198 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3199 objfiles using the same BFD. */
3200 gdb_assert (per_bfd->partial_symtabs == nullptr);
3201 per_bfd->partial_symtabs = objfile->partial_symtabs;
9291a0cd
TT
3202
3203 return 1;
3204}
3205
dee91e82 3206/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3207
dee91e82
DE
3208static void
3209dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3210 const gdb_byte *info_ptr,
3e225074 3211 struct die_info *comp_unit_die)
9291a0cd 3212{
dee91e82 3213 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3214 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 3215 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 3216 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3217 struct attribute *attr;
7b9f3c50
DE
3218 void **slot;
3219 struct quick_file_names *qfn;
9291a0cd 3220
0186c6a7
DE
3221 gdb_assert (! this_cu->is_debug_types);
3222
07261596
TT
3223 /* Our callers never want to match partial units -- instead they
3224 will match the enclosing full CU. */
3225 if (comp_unit_die->tag == DW_TAG_partial_unit)
3226 {
3227 this_cu->v.quick->no_file_data = 1;
3228 return;
3229 }
3230
0186c6a7 3231 lh_cu = this_cu;
7b9f3c50 3232 slot = NULL;
dee91e82 3233
fff8551c 3234 line_header_up lh;
9c541725 3235 sect_offset line_offset {};
fff8551c 3236
dee91e82 3237 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
d4df075e 3238 if (attr != nullptr && attr->form_is_unsigned ())
9291a0cd 3239 {
7b9f3c50
DE
3240 struct quick_file_names find_entry;
3241
d4df075e 3242 line_offset = (sect_offset) attr->as_unsigned ();
7b9f3c50
DE
3243
3244 /* We may have already read in this line header (TU line header sharing).
3245 If we have we're done. */
094b34ac 3246 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3247 find_entry.hash.line_sect_off = line_offset;
976ca316 3248 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3249 &find_entry, INSERT);
3250 if (*slot != NULL)
3251 {
9a3c8263 3252 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3253 return;
7b9f3c50
DE
3254 }
3255
3019eac3 3256 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3257 }
3258 if (lh == NULL)
3259 {
094b34ac 3260 lh_cu->v.quick->no_file_data = 1;
dee91e82 3261 return;
9291a0cd
TT
3262 }
3263
976ca316 3264 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3265 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3266 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3267 gdb_assert (slot != NULL);
3268 *slot = qfn;
9291a0cd 3269
d721ba37 3270 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3271
aa391654
TT
3272 int offset = 0;
3273 if (strcmp (fnd.name, "<unknown>") != 0)
3274 ++offset;
3275
7ba99d21 3276 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3277 qfn->file_names =
976ca316 3278 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 3279 qfn->num_file_names);
aa391654
TT
3280 if (offset != 0)
3281 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3282 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3283 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3284 fnd.comp_dir).release ();
7b9f3c50 3285 qfn->real_names = NULL;
9291a0cd 3286
094b34ac 3287 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3288}
3289
3290/* A helper for the "quick" functions which attempts to read the line
3291 table for THIS_CU. */
3292
3293static struct quick_file_names *
ab432490
SM
3294dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3295 dwarf2_per_objfile *per_objfile)
dee91e82 3296{
0186c6a7
DE
3297 /* This should never be called for TUs. */
3298 gdb_assert (! this_cu->is_debug_types);
3299 /* Nor type unit groups. */
197400e8 3300 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3301
dee91e82
DE
3302 if (this_cu->v.quick->file_names != NULL)
3303 return this_cu->v.quick->file_names;
3304 /* If we know there is no line data, no point in looking again. */
3305 if (this_cu->v.quick->no_file_data)
3306 return NULL;
3307
ab432490 3308 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3309 if (!reader.dummy_p)
3e225074 3310 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3311
3312 if (this_cu->v.quick->no_file_data)
3313 return NULL;
3314 return this_cu->v.quick->file_names;
9291a0cd
TT
3315}
3316
3317/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3318 real path for a given file name from the line table. */
2fdf6df6 3319
9291a0cd 3320static const char *
976ca316 3321dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3322 struct quick_file_names *qfn, int index)
9291a0cd 3323{
7b9f3c50 3324 if (qfn->real_names == NULL)
976ca316 3325 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3326 qfn->num_file_names, const char *);
9291a0cd 3327
7b9f3c50 3328 if (qfn->real_names[index] == NULL)
14278e1f 3329 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3330
7b9f3c50 3331 return qfn->real_names[index];
9291a0cd
TT
3332}
3333
3334static struct symtab *
3335dw2_find_last_source_symtab (struct objfile *objfile)
3336{
976ca316
SM
3337 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3338 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3339 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3340
43f3e411
DE
3341 if (cust == NULL)
3342 return NULL;
ed2dc618 3343
43f3e411 3344 return compunit_primary_filetab (cust);
9291a0cd
TT
3345}
3346
7b9f3c50
DE
3347/* Traversal function for dw2_forget_cached_source_info. */
3348
3349static int
3350dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3351{
7b9f3c50 3352 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3353
7b9f3c50 3354 if (file_data->real_names)
9291a0cd 3355 {
7b9f3c50 3356 int i;
9291a0cd 3357
7b9f3c50 3358 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3359 {
7b9f3c50
DE
3360 xfree ((void*) file_data->real_names[i]);
3361 file_data->real_names[i] = NULL;
9291a0cd
TT
3362 }
3363 }
7b9f3c50
DE
3364
3365 return 1;
3366}
3367
3368static void
3369dw2_forget_cached_source_info (struct objfile *objfile)
3370{
976ca316 3371 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3372
976ca316 3373 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3374 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3375}
3376
f8eba3c6
TT
3377/* Helper function for dw2_map_symtabs_matching_filename that expands
3378 the symtabs and calls the iterator. */
3379
3380static int
3381dw2_map_expand_apply (struct objfile *objfile,
3382 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3383 const char *name, const char *real_path,
14bc53a8 3384 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3385{
43f3e411 3386 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3387
3388 /* Don't visit already-expanded CUs. */
af758d11
SM
3389 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3390 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3391 return 0;
3392
3393 /* This may expand more than one symtab, and we want to iterate over
3394 all of them. */
97a1449a 3395 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3396
14bc53a8
PA
3397 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3398 last_made, callback);
f8eba3c6
TT
3399}
3400
3401/* Implementation of the map_symtabs_matching_filename method. */
3402
14bc53a8
PA
3403static bool
3404dw2_map_symtabs_matching_filename
3405 (struct objfile *objfile, const char *name, const char *real_path,
3406 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3407{
c011a4f4 3408 const char *name_basename = lbasename (name);
976ca316 3409 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 3410
848e3e78
DE
3411 /* The rule is CUs specify all the files, including those used by
3412 any TU, so there's no need to scan TUs here. */
f4dc4d17 3413
976ca316 3414 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3415 {
3d7bb9d9 3416 /* We only need to look at symtabs not already expanded. */
976ca316 3417 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3418 continue;
3419
976ca316 3420 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3421 if (file_data == NULL)
9291a0cd
TT
3422 continue;
3423
b76e467d 3424 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3425 {
7b9f3c50 3426 const char *this_name = file_data->file_names[j];
da235a7c 3427 const char *this_real_name;
9291a0cd 3428
af529f8f 3429 if (compare_filenames_for_search (this_name, name))
9291a0cd 3430 {
f5b95b50 3431 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3432 callback))
3433 return true;
288e77a7 3434 continue;
4aac40c8 3435 }
9291a0cd 3436
c011a4f4
DE
3437 /* Before we invoke realpath, which can get expensive when many
3438 files are involved, do a quick comparison of the basenames. */
3439 if (! basenames_may_differ
3440 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3441 continue;
3442
976ca316 3443 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
da235a7c 3444 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3445 {
da235a7c 3446 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3447 callback))
3448 return true;
288e77a7 3449 continue;
da235a7c 3450 }
9291a0cd 3451
da235a7c
JK
3452 if (real_path != NULL)
3453 {
af529f8f
JK
3454 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3455 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3456 if (this_real_name != NULL
af529f8f 3457 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3458 {
f5b95b50 3459 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3460 callback))
3461 return true;
288e77a7 3462 continue;
9291a0cd
TT
3463 }
3464 }
3465 }
3466 }
3467
14bc53a8 3468 return false;
9291a0cd
TT
3469}
3470
da51c347
DE
3471/* Struct used to manage iterating over all CUs looking for a symbol. */
3472
3473struct dw2_symtab_iterator
9291a0cd 3474{
ed2dc618 3475 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3476 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3477 /* If set, only look for symbols that match that block. Valid values are
3478 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3479 gdb::optional<block_enum> block_index;
da51c347
DE
3480 /* The kind of symbol we're looking for. */
3481 domain_enum domain;
3482 /* The list of CUs from the index entry of the symbol,
3483 or NULL if not found. */
3484 offset_type *vec;
3485 /* The next element in VEC to look at. */
3486 int next;
3487 /* The number of elements in VEC, or zero if there is no match. */
3488 int length;
8943b874
DE
3489 /* Have we seen a global version of the symbol?
3490 If so we can ignore all further global instances.
3491 This is to work around gold/15646, inefficient gold-generated
3492 indices. */
3493 int global_seen;
da51c347 3494};
9291a0cd 3495
e5f3ece2 3496/* Initialize the index symtab iterator ITER, common part. */
2fdf6df6 3497
9291a0cd 3498static void
e5f3ece2
TV
3499dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3500 dwarf2_per_objfile *per_objfile,
3501 gdb::optional<block_enum> block_index,
3502 domain_enum domain)
da51c347 3503{
976ca316 3504 iter->per_objfile = per_objfile;
da51c347
DE
3505 iter->block_index = block_index;
3506 iter->domain = domain;
3507 iter->next = 0;
8943b874 3508 iter->global_seen = 0;
e5f3ece2
TV
3509 iter->vec = NULL;
3510 iter->length = 0;
3511}
da51c347 3512
e5f3ece2
TV
3513/* Initialize the index symtab iterator ITER, const char *NAME variant. */
3514
3515static void
3516dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3517 dwarf2_per_objfile *per_objfile,
3518 gdb::optional<block_enum> block_index,
3519 domain_enum domain,
3520 const char *name)
3521{
3522 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
ed2dc618 3523
e5f3ece2 3524 mapped_index *index = per_objfile->per_bfd->index_table.get ();
ed2dc618 3525 /* index is NULL if OBJF_READNOW. */
e5f3ece2
TV
3526 if (index == NULL)
3527 return;
3528
3529 if (find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347 3530 iter->length = MAYBE_SWAP (*iter->vec);
e5f3ece2
TV
3531}
3532
3533/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3534
3535static void
3536dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3537 dwarf2_per_objfile *per_objfile,
3538 gdb::optional<block_enum> block_index,
3539 domain_enum domain, offset_type namei)
3540{
3541 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3542
3543 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3544 /* index is NULL if OBJF_READNOW. */
3545 if (index == NULL)
3546 return;
3547
3548 gdb_assert (!index->symbol_name_slot_invalid (namei));
3549 const auto &bucket = index->symbol_table[namei];
3550
3551 iter->vec = (offset_type *) (index->constant_pool
3552 + MAYBE_SWAP (bucket.vec));
3553 iter->length = MAYBE_SWAP (*iter->vec);
da51c347
DE
3554}
3555
3556/* Return the next matching CU or NULL if there are no more. */
3557
3558static struct dwarf2_per_cu_data *
3559dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3560{
976ca316 3561 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3562
da51c347
DE
3563 for ( ; iter->next < iter->length; ++iter->next)
3564 {
3565 offset_type cu_index_and_attrs =
3566 MAYBE_SWAP (iter->vec[iter->next + 1]);
3567 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3568 gdb_index_symbol_kind symbol_kind =
3569 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3570 /* Only check the symbol attributes if they're present.
3571 Indices prior to version 7 don't record them,
3572 and indices >= 7 may elide them for certain symbols
3573 (gold does this). */
3574 int attrs_valid =
976ca316 3575 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3576 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3577
3190f0c6 3578 /* Don't crash on bad data. */
976ca316
SM
3579 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3580 + per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3581 {
b98664d3 3582 complaint (_(".gdb_index entry has bad CU index"
976ca316 3583 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3584 continue;
3585 }
3586
976ca316 3587 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3588
da51c347 3589 /* Skip if already read in. */
976ca316 3590 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3591 continue;
3592
8943b874
DE
3593 /* Check static vs global. */
3594 if (attrs_valid)
3595 {
2b79f376
SM
3596 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3597
3598 if (iter->block_index.has_value ())
3599 {
3600 bool want_static = *iter->block_index == STATIC_BLOCK;
3601
3602 if (is_static != want_static)
3603 continue;
3604 }
3605
8943b874 3606 /* Work around gold/15646. */
f030440d
TV
3607 if (!is_static
3608 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3609 {
3610 if (iter->global_seen)
3611 continue;
3612
3613 iter->global_seen = 1;
3614 }
8943b874 3615 }
da51c347
DE
3616
3617 /* Only check the symbol's kind if it has one. */
3618 if (attrs_valid)
3619 {
3620 switch (iter->domain)
3621 {
3622 case VAR_DOMAIN:
3623 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3624 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3625 /* Some types are also in VAR_DOMAIN. */
3626 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3627 continue;
3628 break;
3629 case STRUCT_DOMAIN:
3630 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3631 continue;
3632 break;
3633 case LABEL_DOMAIN:
3634 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3635 continue;
3636 break;
59c35742
AB
3637 case MODULE_DOMAIN:
3638 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3639 continue;
3640 break;
da51c347
DE
3641 default:
3642 break;
3643 }
3644 }
3645
3646 ++iter->next;
3647 return per_cu;
3648 }
3649
3650 return NULL;
3651}
3652
43f3e411 3653static struct compunit_symtab *
c7f839cb 3654dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3655 const char *name, domain_enum domain)
9291a0cd 3656{
43f3e411 3657 struct compunit_symtab *stab_best = NULL;
976ca316 3658 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 3659
b5ec771e
PA
3660 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3661
ed2dc618
SM
3662 struct dw2_symtab_iterator iter;
3663 struct dwarf2_per_cu_data *per_cu;
da51c347 3664
976ca316 3665 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
9291a0cd 3666
ed2dc618
SM
3667 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3668 {
3669 struct symbol *sym, *with_opaque = NULL;
97a1449a 3670 struct compunit_symtab *stab
976ca316 3671 = dw2_instantiate_symtab (per_cu, per_objfile, false);
ed2dc618 3672 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3673 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3674
ed2dc618
SM
3675 sym = block_find_symbol (block, name, domain,
3676 block_find_non_opaque_type_preferred,
3677 &with_opaque);
b2e2f908 3678
ed2dc618
SM
3679 /* Some caution must be observed with overloaded functions
3680 and methods, since the index will not contain any overload
3681 information (but NAME might contain it). */
da51c347 3682
ed2dc618
SM
3683 if (sym != NULL
3684 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3685 return stab;
3686 if (with_opaque != NULL
3687 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3688 stab_best = stab;
da51c347 3689
ed2dc618 3690 /* Keep looking through other CUs. */
9291a0cd 3691 }
9291a0cd 3692
da51c347 3693 return stab_best;
9291a0cd
TT
3694}
3695
3696static void
3697dw2_print_stats (struct objfile *objfile)
3698{
976ca316
SM
3699 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3700 int total = (per_objfile->per_bfd->all_comp_units.size ()
3701 + per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3702 int count = 0;
9291a0cd 3703
ed2dc618 3704 for (int i = 0; i < total; ++i)
9291a0cd 3705 {
976ca316 3706 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3707
976ca316 3708 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3709 ++count;
3710 }
e4a48d9d 3711 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3712 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3713}
3714
779bd270
DE
3715/* This dumps minimal information about the index.
3716 It is called via "mt print objfiles".
3717 One use is to verify .gdb_index has been loaded by the
3718 gdb.dwarf2/gdb-index.exp testcase. */
3719
9291a0cd
TT
3720static void
3721dw2_dump (struct objfile *objfile)
3722{
976ca316 3723 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3724
976ca316 3725 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3726 printf_filtered (".gdb_index:");
976ca316 3727 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3728 {
3729 printf_filtered (" version %d\n",
976ca316 3730 per_objfile->per_bfd->index_table->version);
779bd270
DE
3731 }
3732 else
3733 printf_filtered (" faked for \"readnow\"\n");
3734 printf_filtered ("\n");
9291a0cd
TT
3735}
3736
9291a0cd
TT
3737static void
3738dw2_expand_symtabs_for_function (struct objfile *objfile,
3739 const char *func_name)
3740{
976ca316 3741 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
da51c347 3742
ed2dc618
SM
3743 struct dw2_symtab_iterator iter;
3744 struct dwarf2_per_cu_data *per_cu;
da51c347 3745
976ca316 3746 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3747
ed2dc618 3748 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3749 dw2_instantiate_symtab (per_cu, per_objfile, false);
da51c347 3750
9291a0cd
TT
3751}
3752
3753static void
3754dw2_expand_all_symtabs (struct objfile *objfile)
3755{
976ca316
SM
3756 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3757 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3758 + per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3759
ed2dc618 3760 for (int i = 0; i < total_units; ++i)
9291a0cd 3761 {
976ca316 3762 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3763
58f0c718
TT
3764 /* We don't want to directly expand a partial CU, because if we
3765 read it with the wrong language, then assertion failures can
3766 be triggered later on. See PR symtab/23010. So, tell
3767 dw2_instantiate_symtab to skip partial CUs -- any important
3768 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3769 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3770 }
3771}
3772
3773static void
652a8996
JK
3774dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3775 const char *fullname)
9291a0cd 3776{
976ca316 3777 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3778
3779 /* We don't need to consider type units here.
3780 This is only called for examining code, e.g. expand_line_sal.
3781 There can be an order of magnitude (or more) more type units
3782 than comp units, and we avoid them if we can. */
3783
976ca316 3784 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3785 {
3d7bb9d9 3786 /* We only need to look at symtabs not already expanded. */
976ca316 3787 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3788 continue;
3789
976ca316 3790 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3791 if (file_data == NULL)
9291a0cd
TT
3792 continue;
3793
b76e467d 3794 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3795 {
652a8996
JK
3796 const char *this_fullname = file_data->file_names[j];
3797
3798 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3799 {
976ca316 3800 dw2_instantiate_symtab (per_cu, per_objfile, false);
9291a0cd
TT
3801 break;
3802 }
3803 }
3804 }
3805}
3806
9a0bacfb
TV
3807static void
3808dw2_expand_symtabs_matching_symbol
3809 (mapped_index_base &index,
3810 const lookup_name_info &lookup_name_in,
3811 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3812 enum search_domain kind,
fcf23d5b
SM
3813 gdb::function_view<bool (offset_type)> match_callback,
3814 dwarf2_per_objfile *per_objfile);
9a0bacfb
TV
3815
3816static void
3817dw2_expand_symtabs_matching_one
97a1449a
SM
3818 (dwarf2_per_cu_data *per_cu,
3819 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3820 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3821 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3822
9291a0cd 3823static void
199b4314
TT
3824dw2_map_matching_symbols
3825 (struct objfile *objfile,
b054970d 3826 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3827 int global,
3828 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3829 symbol_compare_ftype *ordered_compare)
9291a0cd 3830{
1aa98955 3831 /* Used for Ada. */
976ca316 3832 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3833
9a0bacfb
TV
3834 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3835
976ca316 3836 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3837 {
976ca316 3838 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3839
9a0bacfb
TV
3840 const char *match_name = name.ada ().lookup_name ().c_str ();
3841 auto matcher = [&] (const char *symname)
3842 {
3843 if (ordered_compare == nullptr)
3844 return true;
3845 return ordered_compare (symname, match_name) == 0;
3846 };
3847
3848 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3849 [&] (offset_type namei)
3850 {
3851 struct dw2_symtab_iterator iter;
3852 struct dwarf2_per_cu_data *per_cu;
3853
976ca316 3854 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3855 namei);
9a0bacfb 3856 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3857 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3858 nullptr);
9a0bacfb 3859 return true;
976ca316 3860 }, per_objfile);
9a0bacfb
TV
3861 }
3862 else
3863 {
3864 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3865 proceed assuming all symtabs have been read in. */
3866 }
1aa98955
TV
3867
3868 for (compunit_symtab *cust : objfile->compunits ())
3869 {
3870 const struct block *block;
3871
3872 if (cust == NULL)
3873 continue;
3874 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3875 if (!iterate_over_symbols_terminated (block, name,
3876 domain, callback))
3877 return;
3878 }
9291a0cd
TT
3879}
3880
e1ef7d7a
PA
3881/* Starting from a search name, return the string that finds the upper
3882 bound of all strings that start with SEARCH_NAME in a sorted name
3883 list. Returns the empty string to indicate that the upper bound is
3884 the end of the list. */
3885
3886static std::string
3887make_sort_after_prefix_name (const char *search_name)
3888{
3889 /* When looking to complete "func", we find the upper bound of all
3890 symbols that start with "func" by looking for where we'd insert
3891 the closest string that would follow "func" in lexicographical
3892 order. Usually, that's "func"-with-last-character-incremented,
3893 i.e. "fund". Mind non-ASCII characters, though. Usually those
3894 will be UTF-8 multi-byte sequences, but we can't be certain.
3895 Especially mind the 0xff character, which is a valid character in
3896 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3897 rule out compilers allowing it in identifiers. Note that
3898 conveniently, strcmp/strcasecmp are specified to compare
3899 characters interpreted as unsigned char. So what we do is treat
3900 the whole string as a base 256 number composed of a sequence of
3901 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3902 to 0, and carries 1 to the following more-significant position.
3903 If the very first character in SEARCH_NAME ends up incremented
3904 and carries/overflows, then the upper bound is the end of the
3905 list. The string after the empty string is also the empty
3906 string.
3907
3908 Some examples of this operation:
3909
3910 SEARCH_NAME => "+1" RESULT
3911
3912 "abc" => "abd"
3913 "ab\xff" => "ac"
3914 "\xff" "a" "\xff" => "\xff" "b"
3915 "\xff" => ""
3916 "\xff\xff" => ""
3917 "" => ""
3918
3919 Then, with these symbols for example:
3920
3921 func
3922 func1
3923 fund
3924
3925 completing "func" looks for symbols between "func" and
3926 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3927 which finds "func" and "func1", but not "fund".
3928
3929 And with:
3930
3931 funcÿ (Latin1 'ÿ' [0xff])
3932 funcÿ1
3933 fund
3934
3935 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3936 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3937
3938 And with:
3939
3940 ÿÿ (Latin1 'ÿ' [0xff])
3941 ÿÿ1
3942
3943 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3944 the end of the list.
3945 */
3946 std::string after = search_name;
3947 while (!after.empty () && (unsigned char) after.back () == 0xff)
3948 after.pop_back ();
3949 if (!after.empty ())
3950 after.back () = (unsigned char) after.back () + 1;
3951 return after;
3952}
3953
5c58de74 3954/* See declaration. */
61d96d7e 3955
5c58de74
PA
3956std::pair<std::vector<name_component>::const_iterator,
3957 std::vector<name_component>::const_iterator>
44ed8f3e 3958mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3959 (const lookup_name_info &lookup_name_without_params, language lang,
3960 dwarf2_per_objfile *per_objfile) const
3f563c84 3961{
5c58de74
PA
3962 auto *name_cmp
3963 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3964
3b00ef10 3965 const char *lang_name
e0802d59 3966 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3967
3f563c84
PA
3968 /* Comparison function object for lower_bound that matches against a
3969 given symbol name. */
3970 auto lookup_compare_lower = [&] (const name_component &elem,
3971 const char *name)
3972 {
fcf23d5b 3973 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3974 const char *elem_name = elem_qualified + elem.name_offset;
3975 return name_cmp (elem_name, name) < 0;
3976 };
3977
3978 /* Comparison function object for upper_bound that matches against a
3979 given symbol name. */
3980 auto lookup_compare_upper = [&] (const char *name,
3981 const name_component &elem)
3982 {
fcf23d5b 3983 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3984 const char *elem_name = elem_qualified + elem.name_offset;
3985 return name_cmp (name, elem_name) < 0;
3986 };
3987
5c58de74
PA
3988 auto begin = this->name_components.begin ();
3989 auto end = this->name_components.end ();
3f563c84
PA
3990
3991 /* Find the lower bound. */
3992 auto lower = [&] ()
3993 {
3b00ef10 3994 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3995 return begin;
3996 else
3b00ef10 3997 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3998 } ();
3999
4000 /* Find the upper bound. */
4001 auto upper = [&] ()
4002 {
5c58de74 4003 if (lookup_name_without_params.completion_mode ())
3f563c84 4004 {
e1ef7d7a
PA
4005 /* In completion mode, we want UPPER to point past all
4006 symbols names that have the same prefix. I.e., with
4007 these symbols, and completing "func":
4008
4009 function << lower bound
4010 function1
4011 other_function << upper bound
4012
4013 We find the upper bound by looking for the insertion
4014 point of "func"-with-last-character-incremented,
4015 i.e. "fund". */
3b00ef10 4016 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4017 if (after.empty ())
3f563c84 4018 return end;
e6b2f5ef
PA
4019 return std::lower_bound (lower, end, after.c_str (),
4020 lookup_compare_lower);
3f563c84
PA
4021 }
4022 else
3b00ef10 4023 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4024 } ();
4025
5c58de74
PA
4026 return {lower, upper};
4027}
4028
4029/* See declaration. */
4030
4031void
fcf23d5b 4032mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
4033{
4034 if (!this->name_components.empty ())
4035 return;
4036
4037 this->name_components_casing = case_sensitivity;
4038 auto *name_cmp
4039 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4040
4041 /* The code below only knows how to break apart components of C++
4042 symbol names (and other languages that use '::' as
3b00ef10 4043 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4044 auto count = this->symbol_name_count ();
4045 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4046 {
44ed8f3e 4047 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4048 continue;
4049
fcf23d5b 4050 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
4051
4052 /* Add each name component to the name component table. */
4053 unsigned int previous_len = 0;
3b00ef10
TT
4054
4055 if (strstr (name, "::") != nullptr)
4056 {
4057 for (unsigned int current_len = cp_find_first_component (name);
4058 name[current_len] != '\0';
4059 current_len += cp_find_first_component (name + current_len))
4060 {
4061 gdb_assert (name[current_len] == ':');
4062 this->name_components.push_back ({previous_len, idx});
4063 /* Skip the '::'. */
4064 current_len += 2;
4065 previous_len = current_len;
4066 }
4067 }
4068 else
5c58de74 4069 {
3b00ef10
TT
4070 /* Handle the Ada encoded (aka mangled) form here. */
4071 for (const char *iter = strstr (name, "__");
4072 iter != nullptr;
4073 iter = strstr (iter, "__"))
4074 {
4075 this->name_components.push_back ({previous_len, idx});
4076 iter += 2;
4077 previous_len = iter - name;
4078 }
5c58de74 4079 }
3b00ef10 4080
5c58de74
PA
4081 this->name_components.push_back ({previous_len, idx});
4082 }
4083
4084 /* Sort name_components elements by name. */
4085 auto name_comp_compare = [&] (const name_component &left,
4086 const name_component &right)
4087 {
fcf23d5b
SM
4088 const char *left_qualified
4089 = this->symbol_name_at (left.idx, per_objfile);
4090 const char *right_qualified
4091 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
4092
4093 const char *left_name = left_qualified + left.name_offset;
4094 const char *right_name = right_qualified + right.name_offset;
4095
4096 return name_cmp (left_name, right_name) < 0;
4097 };
4098
4099 std::sort (this->name_components.begin (),
4100 this->name_components.end (),
4101 name_comp_compare);
4102}
4103
4104/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4105 mapped_index_base instead of the containing objfile. This is split
4106 to a separate function in order to be able to unit test the
4107 name_components matching using a mock mapped_index_base. For each
5c58de74 4108 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4109 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4110
4111static void
4112dw2_expand_symtabs_matching_symbol
44ed8f3e 4113 (mapped_index_base &index,
5c58de74
PA
4114 const lookup_name_info &lookup_name_in,
4115 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4116 enum search_domain kind,
fcf23d5b
SM
4117 gdb::function_view<bool (offset_type)> match_callback,
4118 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4119{
4120 lookup_name_info lookup_name_without_params
4121 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4122
4123 /* Build the symbol name component sorted vector, if we haven't
4124 yet. */
fcf23d5b 4125 index.build_name_components (per_objfile);
5c58de74 4126
3f563c84
PA
4127 /* The same symbol may appear more than once in the range though.
4128 E.g., if we're looking for symbols that complete "w", and we have
4129 a symbol named "w1::w2", we'll find the two name components for
4130 that same symbol in the range. To be sure we only call the
4131 callback once per symbol, we first collect the symbol name
4132 indexes that matched in a temporary vector and ignore
4133 duplicates. */
4134 std::vector<offset_type> matches;
3f563c84 4135
3b00ef10
TT
4136 struct name_and_matcher
4137 {
4138 symbol_name_matcher_ftype *matcher;
ecc6c606 4139 const char *name;
3b00ef10
TT
4140
4141 bool operator== (const name_and_matcher &other) const
3f563c84 4142 {
ecc6c606 4143 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4144 }
4145 };
4146
4147 /* A vector holding all the different symbol name matchers, for all
4148 languages. */
4149 std::vector<name_and_matcher> matchers;
4150
4151 for (int i = 0; i < nr_languages; i++)
4152 {
4153 enum language lang_e = (enum language) i;
4154
4155 const language_defn *lang = language_def (lang_e);
4156 symbol_name_matcher_ftype *name_matcher
c9debfb9 4157 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 4158
3b00ef10 4159 name_and_matcher key {
dda83cd7 4160 name_matcher,
3b00ef10
TT
4161 lookup_name_without_params.language_lookup_name (lang_e)
4162 };
4163
4164 /* Don't insert the same comparison routine more than once.
4165 Note that we do this linear walk. This is not a problem in
4166 practice because the number of supported languages is
4167 low. */
4168 if (std::find (matchers.begin (), matchers.end (), key)
4169 != matchers.end ())
9291a0cd 4170 continue;
3b00ef10
TT
4171 matchers.push_back (std::move (key));
4172
4173 auto bounds
4174 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 4175 lang_e, per_objfile);
3b00ef10
TT
4176
4177 /* Now for each symbol name in range, check to see if we have a name
4178 match, and if so, call the MATCH_CALLBACK callback. */
4179
4180 for (; bounds.first != bounds.second; ++bounds.first)
4181 {
fcf23d5b
SM
4182 const char *qualified
4183 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
4184
4185 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4186 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4187 continue;
9291a0cd 4188
3b00ef10
TT
4189 matches.push_back (bounds.first->idx);
4190 }
3f563c84
PA
4191 }
4192
4193 std::sort (matches.begin (), matches.end ());
4194
4195 /* Finally call the callback, once per match. */
4196 ULONGEST prev = -1;
4197 for (offset_type idx : matches)
4198 {
4199 if (prev != idx)
4200 {
3b00ef10
TT
4201 if (!match_callback (idx))
4202 break;
3f563c84
PA
4203 prev = idx;
4204 }
4205 }
4206
4207 /* Above we use a type wider than idx's for 'prev', since 0 and
4208 (offset_type)-1 are both possible values. */
4209 static_assert (sizeof (prev) > sizeof (offset_type), "");
4210}
4211
c62446b1
PA
4212#if GDB_SELF_TEST
4213
4214namespace selftests { namespace dw2_expand_symtabs_matching {
4215
a3c5fafd
PA
4216/* A mock .gdb_index/.debug_names-like name index table, enough to
4217 exercise dw2_expand_symtabs_matching_symbol, which works with the
4218 mapped_index_base interface. Builds an index from the symbol list
4219 passed as parameter to the constructor. */
4220class mock_mapped_index : public mapped_index_base
c62446b1
PA
4221{
4222public:
a3c5fafd
PA
4223 mock_mapped_index (gdb::array_view<const char *> symbols)
4224 : m_symbol_table (symbols)
c62446b1
PA
4225 {}
4226
a3c5fafd 4227 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4228
a3c5fafd 4229 /* Return the number of names in the symbol table. */
632e107b 4230 size_t symbol_name_count () const override
c62446b1 4231 {
a3c5fafd 4232 return m_symbol_table.size ();
c62446b1
PA
4233 }
4234
a3c5fafd 4235 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
4236 const char *symbol_name_at
4237 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
4238 {
4239 return m_symbol_table[idx];
4240 }
c62446b1 4241
a3c5fafd
PA
4242private:
4243 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4244};
4245
4246/* Convenience function that converts a NULL pointer to a "<null>"
4247 string, to pass to print routines. */
4248
4249static const char *
4250string_or_null (const char *str)
4251{
4252 return str != NULL ? str : "<null>";
4253}
4254
4255/* Check if a lookup_name_info built from
4256 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4257 index. EXPECTED_LIST is the list of expected matches, in expected
4258 matching order. If no match expected, then an empty list is
4259 specified. Returns true on success. On failure prints a warning
4260 indicating the file:line that failed, and returns false. */
4261
4262static bool
4263check_match (const char *file, int line,
4264 mock_mapped_index &mock_index,
4265 const char *name, symbol_name_match_type match_type,
4266 bool completion_mode,
fcf23d5b
SM
4267 std::initializer_list<const char *> expected_list,
4268 dwarf2_per_objfile *per_objfile)
c62446b1
PA
4269{
4270 lookup_name_info lookup_name (name, match_type, completion_mode);
4271
4272 bool matched = true;
4273
4274 auto mismatch = [&] (const char *expected_str,
4275 const char *got)
4276 {
4277 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4278 "expected=\"%s\", got=\"%s\"\n"),
4279 file, line,
4280 (match_type == symbol_name_match_type::FULL
4281 ? "FULL" : "WILD"),
4282 name, string_or_null (expected_str), string_or_null (got));
4283 matched = false;
4284 };
4285
4286 auto expected_it = expected_list.begin ();
4287 auto expected_end = expected_list.end ();
4288
a3c5fafd 4289 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4290 NULL, ALL_DOMAIN,
4291 [&] (offset_type idx)
4292 {
fcf23d5b 4293 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
4294 const char *expected_str
4295 = expected_it == expected_end ? NULL : *expected_it++;
4296
4297 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4298 mismatch (expected_str, matched_name);
3b00ef10 4299 return true;
fcf23d5b 4300 }, per_objfile);
c62446b1
PA
4301
4302 const char *expected_str
4303 = expected_it == expected_end ? NULL : *expected_it++;
4304 if (expected_str != NULL)
4305 mismatch (expected_str, NULL);
4306
4307 return matched;
4308}
4309
4310/* The symbols added to the mock mapped_index for testing (in
4311 canonical form). */
4312static const char *test_symbols[] = {
4313 "function",
4314 "std::bar",
4315 "std::zfunction",
4316 "std::zfunction2",
4317 "w1::w2",
4318 "ns::foo<char*>",
4319 "ns::foo<int>",
4320 "ns::foo<long>",
a20714ff
PA
4321 "ns2::tmpl<int>::foo2",
4322 "(anonymous namespace)::A::B::C",
c62446b1 4323
e1ef7d7a
PA
4324 /* These are used to check that the increment-last-char in the
4325 matching algorithm for completion doesn't match "t1_fund" when
4326 completing "t1_func". */
4327 "t1_func",
4328 "t1_func1",
4329 "t1_fund",
4330 "t1_fund1",
4331
4332 /* A UTF-8 name with multi-byte sequences to make sure that
4333 cp-name-parser understands this as a single identifier ("função"
4334 is "function" in PT). */
4335 u8"u8função",
4336
4337 /* \377 (0xff) is Latin1 'ÿ'. */
4338 "yfunc\377",
4339
4340 /* \377 (0xff) is Latin1 'ÿ'. */
4341 "\377",
4342 "\377\377123",
4343
c62446b1
PA
4344 /* A name with all sorts of complications. Starts with "z" to make
4345 it easier for the completion tests below. */
4346#define Z_SYM_NAME \
4347 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4348 "::tuple<(anonymous namespace)::ui*, " \
4349 "std::default_delete<(anonymous namespace)::ui>, void>"
4350
4351 Z_SYM_NAME
4352};
4353
a3c5fafd
PA
4354/* Returns true if the mapped_index_base::find_name_component_bounds
4355 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4356 in completion mode. */
5c58de74
PA
4357
4358static bool
a3c5fafd 4359check_find_bounds_finds (mapped_index_base &index,
5c58de74 4360 const char *search_name,
fcf23d5b
SM
4361 gdb::array_view<const char *> expected_syms,
4362 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4363{
4364 lookup_name_info lookup_name (search_name,
4365 symbol_name_match_type::FULL, true);
4366
3b00ef10 4367 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4368 language_cplus,
4369 per_objfile);
5c58de74
PA
4370
4371 size_t distance = std::distance (bounds.first, bounds.second);
4372 if (distance != expected_syms.size ())
4373 return false;
4374
4375 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4376 {
4377 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4378 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4379 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4380 return false;
4381 }
4382
4383 return true;
4384}
4385
4386/* Test the lower-level mapped_index::find_name_component_bounds
4387 method. */
4388
c62446b1 4389static void
5c58de74
PA
4390test_mapped_index_find_name_component_bounds ()
4391{
4392 mock_mapped_index mock_index (test_symbols);
4393
fcf23d5b 4394 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4395
4396 /* Test the lower-level mapped_index::find_name_component_bounds
4397 method in completion mode. */
4398 {
4399 static const char *expected_syms[] = {
4400 "t1_func",
4401 "t1_func1",
5c58de74
PA
4402 };
4403
fcf23d5b
SM
4404 SELF_CHECK (check_find_bounds_finds
4405 (mock_index, "t1_func", expected_syms,
4406 NULL /* per_objfile */));
5c58de74
PA
4407 }
4408
4409 /* Check that the increment-last-char in the name matching algorithm
4410 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4411 {
4412 static const char *expected_syms1[] = {
4413 "\377",
4414 "\377\377123",
4415 };
fcf23d5b
SM
4416 SELF_CHECK (check_find_bounds_finds
4417 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4418
4419 static const char *expected_syms2[] = {
4420 "\377\377123",
4421 };
fcf23d5b
SM
4422 SELF_CHECK (check_find_bounds_finds
4423 (mock_index, "\377\377", expected_syms2,
4424 NULL /* per_objfile */));
5c58de74
PA
4425 }
4426}
4427
4428/* Test dw2_expand_symtabs_matching_symbol. */
4429
4430static void
4431test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4432{
4433 mock_mapped_index mock_index (test_symbols);
4434
4435 /* We let all tests run until the end even if some fails, for debug
4436 convenience. */
4437 bool any_mismatch = false;
4438
4439 /* Create the expected symbols list (an initializer_list). Needed
4440 because lists have commas, and we need to pass them to CHECK,
4441 which is a macro. */
4442#define EXPECT(...) { __VA_ARGS__ }
4443
4444 /* Wrapper for check_match that passes down the current
4445 __FILE__/__LINE__. */
4446#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4447 any_mismatch |= !check_match (__FILE__, __LINE__, \
4448 mock_index, \
4449 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4450 EXPECTED_LIST, NULL)
c62446b1
PA
4451
4452 /* Identity checks. */
4453 for (const char *sym : test_symbols)
4454 {
4455 /* Should be able to match all existing symbols. */
4456 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4457 EXPECT (sym));
4458
4459 /* Should be able to match all existing symbols with
4460 parameters. */
4461 std::string with_params = std::string (sym) + "(int)";
4462 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4463 EXPECT (sym));
4464
4465 /* Should be able to match all existing symbols with
4466 parameters and qualifiers. */
4467 with_params = std::string (sym) + " ( int ) const";
4468 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4469 EXPECT (sym));
4470
4471 /* This should really find sym, but cp-name-parser.y doesn't
4472 know about lvalue/rvalue qualifiers yet. */
4473 with_params = std::string (sym) + " ( int ) &&";
4474 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4475 {});
4476 }
4477
e1ef7d7a
PA
4478 /* Check that the name matching algorithm for completion doesn't get
4479 confused with Latin1 'ÿ' / 0xff. */
4480 {
4481 static const char str[] = "\377";
4482 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4483 EXPECT ("\377", "\377\377123"));
4484 }
4485
4486 /* Check that the increment-last-char in the matching algorithm for
4487 completion doesn't match "t1_fund" when completing "t1_func". */
4488 {
4489 static const char str[] = "t1_func";
4490 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4491 EXPECT ("t1_func", "t1_func1"));
4492 }
4493
c62446b1
PA
4494 /* Check that completion mode works at each prefix of the expected
4495 symbol name. */
4496 {
4497 static const char str[] = "function(int)";
4498 size_t len = strlen (str);
4499 std::string lookup;
4500
4501 for (size_t i = 1; i < len; i++)
4502 {
4503 lookup.assign (str, i);
4504 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4505 EXPECT ("function"));
4506 }
4507 }
4508
4509 /* While "w" is a prefix of both components, the match function
4510 should still only be called once. */
4511 {
4512 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4513 EXPECT ("w1::w2"));
a20714ff
PA
4514 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4515 EXPECT ("w1::w2"));
c62446b1
PA
4516 }
4517
4518 /* Same, with a "complicated" symbol. */
4519 {
4520 static const char str[] = Z_SYM_NAME;
4521 size_t len = strlen (str);
4522 std::string lookup;
4523
4524 for (size_t i = 1; i < len; i++)
4525 {
4526 lookup.assign (str, i);
4527 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4528 EXPECT (Z_SYM_NAME));
4529 }
4530 }
4531
4532 /* In FULL mode, an incomplete symbol doesn't match. */
4533 {
4534 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4535 {});
4536 }
4537
4538 /* A complete symbol with parameters matches any overload, since the
4539 index has no overload info. */
4540 {
4541 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4542 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4543 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4544 EXPECT ("std::zfunction", "std::zfunction2"));
4545 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4546 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4547 }
4548
4549 /* Check that whitespace is ignored appropriately. A symbol with a
4550 template argument list. */
4551 {
4552 static const char expected[] = "ns::foo<int>";
4553 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4554 EXPECT (expected));
a20714ff
PA
4555 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4556 EXPECT (expected));
c62446b1
PA
4557 }
4558
4559 /* Check that whitespace is ignored appropriately. A symbol with a
4560 template argument list that includes a pointer. */
4561 {
4562 static const char expected[] = "ns::foo<char*>";
4563 /* Try both completion and non-completion modes. */
4564 static const bool completion_mode[2] = {false, true};
4565 for (size_t i = 0; i < 2; i++)
4566 {
4567 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4568 completion_mode[i], EXPECT (expected));
a20714ff
PA
4569 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4570 completion_mode[i], EXPECT (expected));
c62446b1
PA
4571
4572 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4573 completion_mode[i], EXPECT (expected));
a20714ff
PA
4574 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4575 completion_mode[i], EXPECT (expected));
c62446b1
PA
4576 }
4577 }
4578
4579 {
4580 /* Check method qualifiers are ignored. */
4581 static const char expected[] = "ns::foo<char*>";
4582 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4583 symbol_name_match_type::FULL, true, EXPECT (expected));
4584 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4585 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4586 CHECK_MATCH ("foo < char * > ( int ) const",
4587 symbol_name_match_type::WILD, true, EXPECT (expected));
4588 CHECK_MATCH ("foo < char * > ( int ) &&",
4589 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4590 }
4591
4592 /* Test lookup names that don't match anything. */
4593 {
a20714ff
PA
4594 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4595 {});
4596
c62446b1
PA
4597 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4598 {});
4599 }
4600
a20714ff
PA
4601 /* Some wild matching tests, exercising "(anonymous namespace)",
4602 which should not be confused with a parameter list. */
4603 {
4604 static const char *syms[] = {
4605 "A::B::C",
4606 "B::C",
4607 "C",
4608 "A :: B :: C ( int )",
4609 "B :: C ( int )",
4610 "C ( int )",
4611 };
4612
4613 for (const char *s : syms)
4614 {
4615 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4616 EXPECT ("(anonymous namespace)::A::B::C"));
4617 }
4618 }
4619
4620 {
4621 static const char expected[] = "ns2::tmpl<int>::foo2";
4622 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4623 EXPECT (expected));
4624 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4625 EXPECT (expected));
4626 }
4627
c62446b1
PA
4628 SELF_CHECK (!any_mismatch);
4629
4630#undef EXPECT
4631#undef CHECK_MATCH
4632}
4633
5c58de74
PA
4634static void
4635run_test ()
4636{
4637 test_mapped_index_find_name_component_bounds ();
4638 test_dw2_expand_symtabs_matching_symbol ();
4639}
4640
c62446b1
PA
4641}} // namespace selftests::dw2_expand_symtabs_matching
4642
4643#endif /* GDB_SELF_TEST */
4644
4b514bc8
JK
4645/* If FILE_MATCHER is NULL or if PER_CU has
4646 dwarf2_per_cu_quick_data::MARK set (see
4647 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4648 EXPANSION_NOTIFY on it. */
4649
4650static void
4651dw2_expand_symtabs_matching_one
97a1449a
SM
4652 (dwarf2_per_cu_data *per_cu,
4653 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4654 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4655 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4656{
4657 if (file_matcher == NULL || per_cu->v.quick->mark)
4658 {
af758d11 4659 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4660
97a1449a
SM
4661 compunit_symtab *symtab
4662 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4663 gdb_assert (symtab != nullptr);
4b514bc8 4664
af758d11
SM
4665 if (expansion_notify != NULL && symtab_was_null)
4666 expansion_notify (symtab);
4b514bc8
JK
4667 }
4668}
4669
3f563c84
PA
4670/* Helper for dw2_expand_matching symtabs. Called on each symbol
4671 matched, to expand corresponding CUs that were marked. IDX is the
4672 index of the symbol name that matched. */
4673
4674static void
4675dw2_expand_marked_cus
976ca316 4676 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4677 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4678 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4679 search_domain kind)
4680{
3f563c84
PA
4681 offset_type *vec, vec_len, vec_idx;
4682 bool global_seen = false;
976ca316 4683 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4684
61920122 4685 vec = (offset_type *) (index.constant_pool
f00a2de2 4686 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4687 vec_len = MAYBE_SWAP (vec[0]);
4688 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4689 {
61920122
PA
4690 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4691 /* This value is only valid for index versions >= 7. */
4692 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4693 gdb_index_symbol_kind symbol_kind =
4694 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4695 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4696 /* Only check the symbol attributes if they're present.
4697 Indices prior to version 7 don't record them,
4698 and indices >= 7 may elide them for certain symbols
4699 (gold does this). */
4700 int attrs_valid =
4701 (index.version >= 7
4702 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4703
4704 /* Work around gold/15646. */
f030440d
TV
4705 if (attrs_valid
4706 && !is_static
4707 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4708 {
f030440d 4709 if (global_seen)
61920122 4710 continue;
f030440d
TV
4711
4712 global_seen = true;
61920122 4713 }
3190f0c6 4714
61920122
PA
4715 /* Only check the symbol's kind if it has one. */
4716 if (attrs_valid)
4717 {
4718 switch (kind)
8943b874 4719 {
61920122
PA
4720 case VARIABLES_DOMAIN:
4721 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4722 continue;
4723 break;
4724 case FUNCTIONS_DOMAIN:
4725 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4726 continue;
61920122
PA
4727 break;
4728 case TYPES_DOMAIN:
4729 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4730 continue;
4731 break;
59c35742
AB
4732 case MODULES_DOMAIN:
4733 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4734 continue;
4735 break;
61920122
PA
4736 default:
4737 break;
8943b874 4738 }
61920122 4739 }
8943b874 4740
61920122 4741 /* Don't crash on bad data. */
976ca316
SM
4742 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4743 + per_objfile->per_bfd->all_type_units.size ()))
61920122 4744 {
b98664d3 4745 complaint (_(".gdb_index entry has bad CU index"
976ca316 4746 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4747 continue;
4748 }
4749
976ca316
SM
4750 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4751 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4b514bc8 4752 expansion_notify);
61920122
PA
4753 }
4754}
4755
4b514bc8
JK
4756/* If FILE_MATCHER is non-NULL, set all the
4757 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4758 that match FILE_MATCHER. */
4759
61920122 4760static void
4b514bc8 4761dw_expand_symtabs_matching_file_matcher
976ca316 4762 (dwarf2_per_objfile *per_objfile,
ed2dc618 4763 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4764{
4b514bc8 4765 if (file_matcher == NULL)
61920122
PA
4766 return;
4767
4b514bc8
JK
4768 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4769 htab_eq_pointer,
4770 NULL, xcalloc, xfree));
4771 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4772 htab_eq_pointer,
4773 NULL, xcalloc, xfree));
61920122 4774
4b514bc8
JK
4775 /* The rule is CUs specify all the files, including those used by
4776 any TU, so there's no need to scan TUs here. */
61920122 4777
976ca316 4778 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4779 {
927aa2e7
JK
4780 QUIT;
4781
4782 per_cu->v.quick->mark = 0;
4783
4784 /* We only need to look at symtabs not already expanded. */
976ca316 4785 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4786 continue;
4787
976ca316 4788 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4789 if (file_data == NULL)
4790 continue;
4791
4792 if (htab_find (visited_not_found.get (), file_data) != NULL)
4793 continue;
4794 else if (htab_find (visited_found.get (), file_data) != NULL)
4795 {
4796 per_cu->v.quick->mark = 1;
4797 continue;
4798 }
4799
b76e467d 4800 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4801 {
4802 const char *this_real_name;
4803
4804 if (file_matcher (file_data->file_names[j], false))
4805 {
4806 per_cu->v.quick->mark = 1;
4807 break;
4808 }
4809
4810 /* Before we invoke realpath, which can get expensive when many
4811 files are involved, do a quick comparison of the basenames. */
4812 if (!basenames_may_differ
4813 && !file_matcher (lbasename (file_data->file_names[j]),
4814 true))
4815 continue;
4816
976ca316 4817 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4818 if (file_matcher (this_real_name, false))
4819 {
4820 per_cu->v.quick->mark = 1;
4821 break;
4822 }
4823 }
4824
b76e467d
SM
4825 void **slot = htab_find_slot (per_cu->v.quick->mark
4826 ? visited_found.get ()
4827 : visited_not_found.get (),
4828 file_data, INSERT);
927aa2e7
JK
4829 *slot = file_data;
4830 }
4831}
4832
4833static void
4834dw2_expand_symtabs_matching
4835 (struct objfile *objfile,
4836 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4837 const lookup_name_info *lookup_name,
927aa2e7
JK
4838 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4839 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4840 enum search_domain kind)
4841{
976ca316 4842 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4843
4844 /* index_table is NULL if OBJF_READNOW. */
976ca316 4845 if (!per_objfile->per_bfd->index_table)
927aa2e7
JK
4846 return;
4847
976ca316 4848 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4849
c1a66c06
TV
4850 if (symbol_matcher == NULL && lookup_name == NULL)
4851 {
976ca316 4852 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4853 {
4854 QUIT;
4855
976ca316 4856 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
97a1449a 4857 file_matcher, expansion_notify);
c1a66c06
TV
4858 }
4859 return;
4860 }
4861
976ca316 4862 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4863
c1a66c06 4864 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4865 symbol_matcher,
4866 kind, [&] (offset_type idx)
4867 {
976ca316
SM
4868 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4869 kind);
3b00ef10 4870 return true;
976ca316 4871 }, per_objfile);
927aa2e7
JK
4872}
4873
4874/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4875 symtab. */
4876
4877static struct compunit_symtab *
4878recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4879 CORE_ADDR pc)
4880{
4881 int i;
4882
4883 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4884 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4885 return cust;
4886
4887 if (cust->includes == NULL)
4888 return NULL;
4889
4890 for (i = 0; cust->includes[i]; ++i)
4891 {
4892 struct compunit_symtab *s = cust->includes[i];
4893
4894 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4895 if (s != NULL)
4896 return s;
4897 }
4898
4899 return NULL;
4900}
4901
4902static struct compunit_symtab *
4903dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4904 struct bound_minimal_symbol msymbol,
4905 CORE_ADDR pc,
4906 struct obj_section *section,
4907 int warn_if_readin)
4908{
4909 struct dwarf2_per_cu_data *data;
4910 struct compunit_symtab *result;
4911
d320c2b5 4912 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4913 return NULL;
4914
b3b3bada 4915 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4916 data = (struct dwarf2_per_cu_data *) addrmap_find
4917 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4918 if (!data)
4919 return NULL;
4920
af758d11
SM
4921 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4922 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4923 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4924 paddress (objfile->arch (), pc));
927aa2e7 4925
97a1449a
SM
4926 result = recursively_find_pc_sect_compunit_symtab
4927 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4928
927aa2e7
JK
4929 gdb_assert (result != NULL);
4930 return result;
4931}
4932
4933static void
4934dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4935 void *data, int need_fullname)
4936{
976ca316 4937 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4938
976ca316 4939 if (!per_objfile->per_bfd->filenames_cache)
927aa2e7 4940 {
976ca316 4941 per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4942
4943 htab_up visited (htab_create_alloc (10,
4944 htab_hash_pointer, htab_eq_pointer,
4945 NULL, xcalloc, xfree));
4946
4947 /* The rule is CUs specify all the files, including those used
4948 by any TU, so there's no need to scan TUs here. We can
4949 ignore file names coming from already-expanded CUs. */
4950
976ca316 4951 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4952 {
976ca316 4953 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4954 {
4955 void **slot = htab_find_slot (visited.get (),
4956 per_cu->v.quick->file_names,
4957 INSERT);
4958
4959 *slot = per_cu->v.quick->file_names;
4960 }
4961 }
4962
976ca316 4963 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4964 {
927aa2e7 4965 /* We only need to look at symtabs not already expanded. */
976ca316 4966 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4967 continue;
4968
ab432490 4969 quick_file_names *file_data
976ca316 4970 = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4971 if (file_data == NULL)
4972 continue;
4973
b76e467d 4974 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4975 if (*slot)
4976 {
4977 /* Already visited. */
4978 continue;
4979 }
4980 *slot = file_data;
4981
4982 for (int j = 0; j < file_data->num_file_names; ++j)
4983 {
4984 const char *filename = file_data->file_names[j];
976ca316 4985 per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4986 }
4987 }
4988 }
4989
976ca316 4990 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4991 {
4992 gdb::unique_xmalloc_ptr<char> this_real_name;
4993
4994 if (need_fullname)
4995 this_real_name = gdb_realpath (filename);
4996 (*fun) (filename, this_real_name.get (), data);
4997 });
4998}
4999
5000static int
5001dw2_has_symbols (struct objfile *objfile)
5002{
5003 return 1;
5004}
5005
5006const struct quick_symbol_functions dwarf2_gdb_index_functions =
5007{
5008 dw2_has_symbols,
5009 dw2_find_last_source_symtab,
5010 dw2_forget_cached_source_info,
5011 dw2_map_symtabs_matching_filename,
5012 dw2_lookup_symbol,
d3214198 5013 NULL,
927aa2e7
JK
5014 dw2_print_stats,
5015 dw2_dump,
927aa2e7
JK
5016 dw2_expand_symtabs_for_function,
5017 dw2_expand_all_symtabs,
5018 dw2_expand_symtabs_with_fullname,
5019 dw2_map_matching_symbols,
5020 dw2_expand_symtabs_matching,
5021 dw2_find_pc_sect_compunit_symtab,
5022 NULL,
5023 dw2_map_symbol_filenames
5024};
5025
5026/* DWARF-5 debug_names reader. */
5027
5028/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5029static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5030
5031/* A helper function that reads the .debug_names section in SECTION
5032 and fills in MAP. FILENAME is the name of the file containing the
5033 section; it is used for error reporting.
5034
5035 Returns true if all went well, false otherwise. */
5036
5037static bool
5038read_debug_names_from_section (struct objfile *objfile,
5039 const char *filename,
5040 struct dwarf2_section_info *section,
5041 mapped_debug_names &map)
5042{
96b79293 5043 if (section->empty ())
927aa2e7
JK
5044 return false;
5045
5046 /* Older elfutils strip versions could keep the section in the main
5047 executable while splitting it for the separate debug info file. */
96b79293 5048 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
5049 return false;
5050
96b79293 5051 section->read (objfile);
927aa2e7 5052
08feed99 5053 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
5054
5055 const gdb_byte *addr = section->buffer;
5056
96b79293 5057 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
5058
5059 unsigned int bytes_read;
5060 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5061 addr += bytes_read;
5062
5063 map.dwarf5_is_dwarf64 = bytes_read != 4;
5064 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5065 if (bytes_read + length != section->size)
5066 {
5067 /* There may be multiple per-CU indices. */
5068 warning (_("Section .debug_names in %s length %s does not match "
5069 "section length %s, ignoring .debug_names."),
5070 filename, plongest (bytes_read + length),
5071 pulongest (section->size));
5072 return false;
5073 }
5074
5075 /* The version number. */
5076 uint16_t version = read_2_bytes (abfd, addr);
5077 addr += 2;
5078 if (version != 5)
5079 {
5080 warning (_("Section .debug_names in %s has unsupported version %d, "
5081 "ignoring .debug_names."),
5082 filename, version);
5083 return false;
5084 }
5085
5086 /* Padding. */
5087 uint16_t padding = read_2_bytes (abfd, addr);
5088 addr += 2;
5089 if (padding != 0)
5090 {
5091 warning (_("Section .debug_names in %s has unsupported padding %d, "
5092 "ignoring .debug_names."),
5093 filename, padding);
5094 return false;
5095 }
5096
5097 /* comp_unit_count - The number of CUs in the CU list. */
5098 map.cu_count = read_4_bytes (abfd, addr);
5099 addr += 4;
5100
5101 /* local_type_unit_count - The number of TUs in the local TU
5102 list. */
5103 map.tu_count = read_4_bytes (abfd, addr);
5104 addr += 4;
5105
5106 /* foreign_type_unit_count - The number of TUs in the foreign TU
5107 list. */
5108 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5109 addr += 4;
5110 if (foreign_tu_count != 0)
5111 {
5112 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5113 "ignoring .debug_names."),
5114 filename, static_cast<unsigned long> (foreign_tu_count));
5115 return false;
5116 }
5117
5118 /* bucket_count - The number of hash buckets in the hash lookup
5119 table. */
5120 map.bucket_count = read_4_bytes (abfd, addr);
5121 addr += 4;
5122
5123 /* name_count - The number of unique names in the index. */
5124 map.name_count = read_4_bytes (abfd, addr);
5125 addr += 4;
5126
5127 /* abbrev_table_size - The size in bytes of the abbreviations
5128 table. */
5129 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5130 addr += 4;
5131
5132 /* augmentation_string_size - The size in bytes of the augmentation
5133 string. This value is rounded up to a multiple of 4. */
5134 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5135 addr += 4;
5136 map.augmentation_is_gdb = ((augmentation_string_size
5137 == sizeof (dwarf5_augmentation))
5138 && memcmp (addr, dwarf5_augmentation,
5139 sizeof (dwarf5_augmentation)) == 0);
5140 augmentation_string_size += (-augmentation_string_size) & 3;
5141 addr += augmentation_string_size;
5142
5143 /* List of CUs */
5144 map.cu_table_reordered = addr;
5145 addr += map.cu_count * map.offset_size;
5146
5147 /* List of Local TUs */
5148 map.tu_table_reordered = addr;
5149 addr += map.tu_count * map.offset_size;
5150
5151 /* Hash Lookup Table */
5152 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5153 addr += map.bucket_count * 4;
5154 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5155 addr += map.name_count * 4;
5156
5157 /* Name Table */
5158 map.name_table_string_offs_reordered = addr;
5159 addr += map.name_count * map.offset_size;
5160 map.name_table_entry_offs_reordered = addr;
5161 addr += map.name_count * map.offset_size;
5162
5163 const gdb_byte *abbrev_table_start = addr;
5164 for (;;)
5165 {
927aa2e7
JK
5166 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5167 addr += bytes_read;
5168 if (index_num == 0)
5169 break;
5170
5171 const auto insertpair
5172 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5173 if (!insertpair.second)
5174 {
5175 warning (_("Section .debug_names in %s has duplicate index %s, "
5176 "ignoring .debug_names."),
5177 filename, pulongest (index_num));
5178 return false;
5179 }
5180 mapped_debug_names::index_val &indexval = insertpair.first->second;
5181 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5182 addr += bytes_read;
5183
5184 for (;;)
5185 {
5186 mapped_debug_names::index_val::attr attr;
5187 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5188 addr += bytes_read;
5189 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5190 addr += bytes_read;
5191 if (attr.form == DW_FORM_implicit_const)
5192 {
5193 attr.implicit_const = read_signed_leb128 (abfd, addr,
5194 &bytes_read);
5195 addr += bytes_read;
5196 }
5197 if (attr.dw_idx == 0 && attr.form == 0)
5198 break;
5199 indexval.attr_vec.push_back (std::move (attr));
5200 }
5201 }
5202 if (addr != abbrev_table_start + abbrev_table_size)
5203 {
5204 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5205 "of size %s vs. written as %u, ignoring .debug_names."),
5206 filename, plongest (addr - abbrev_table_start),
5207 abbrev_table_size);
927aa2e7
JK
5208 return false;
5209 }
5210 map.entry_pool = addr;
5211
5212 return true;
5213}
5214
5215/* A helper for create_cus_from_debug_names that handles the MAP's CU
5216 list. */
5217
5218static void
168c9250 5219create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5220 const mapped_debug_names &map,
5221 dwarf2_section_info &section,
b76e467d 5222 bool is_dwz)
927aa2e7 5223{
3ee6bb11
TV
5224 if (!map.augmentation_is_gdb)
5225 {
5226 for (uint32_t i = 0; i < map.cu_count; ++i)
5227 {
5228 sect_offset sect_off
5229 = (sect_offset) (extract_unsigned_integer
5230 (map.cu_table_reordered + i * map.offset_size,
5231 map.offset_size,
5232 map.dwarf5_byte_order));
5233 /* We don't know the length of the CU, because the CU list in a
5234 .debug_names index can be incomplete, so we can't use the start of
5235 the next CU as end of this CU. We create the CUs here with length 0,
5236 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5237 dwarf2_per_cu_data *per_cu
168c9250
SM
5238 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5239 per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5240 }
5241 }
5242
927aa2e7
JK
5243 sect_offset sect_off_prev;
5244 for (uint32_t i = 0; i <= map.cu_count; ++i)
5245 {
5246 sect_offset sect_off_next;
5247 if (i < map.cu_count)
5248 {
5249 sect_off_next
5250 = (sect_offset) (extract_unsigned_integer
5251 (map.cu_table_reordered + i * map.offset_size,
5252 map.offset_size,
5253 map.dwarf5_byte_order));
5254 }
5255 else
5256 sect_off_next = (sect_offset) section.size;
5257 if (i >= 1)
5258 {
5259 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5260 dwarf2_per_cu_data *per_cu
168c9250 5261 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5262 sect_off_prev, length);
168c9250 5263 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5264 }
5265 sect_off_prev = sect_off_next;
5266 }
5267}
5268
5269/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5270 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5271
5272static void
168c9250 5273create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5274 const mapped_debug_names &map,
5275 const mapped_debug_names &dwz_map)
5276{
168c9250
SM
5277 gdb_assert (per_bfd->all_comp_units.empty ());
5278 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5279
168c9250 5280 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5281 false /* is_dwz */);
927aa2e7
JK
5282
5283 if (dwz_map.cu_count == 0)
5284 return;
5285
168c9250
SM
5286 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5287 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5288 true /* is_dwz */);
927aa2e7
JK
5289}
5290
5291/* Read .debug_names. If everything went ok, initialize the "quick"
5292 elements of all the CUs and return true. Otherwise, return false. */
5293
5294static bool
976ca316 5295dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 5296{
fcf23d5b
SM
5297 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5298 mapped_debug_names dwz_map;
976ca316 5299 struct objfile *objfile = per_objfile->objfile;
f8c41851 5300 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
5301
5302 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
976ca316 5303 &per_objfile->per_bfd->debug_names, *map))
927aa2e7
JK
5304 return false;
5305
5306 /* Don't use the index if it's empty. */
22ca247e 5307 if (map->name_count == 0)
927aa2e7
JK
5308 return false;
5309
5310 /* If there is a .dwz file, read it so we can get its CU list as
5311 well. */
f8c41851 5312 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
5313 if (dwz != NULL)
5314 {
5315 if (!read_debug_names_from_section (objfile,
00f93c44 5316 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5317 &dwz->debug_names, dwz_map))
5318 {
5319 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5320 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5321 return false;
5322 }
5323 }
5324
f8c41851 5325 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 5326
22ca247e 5327 if (map->tu_count != 0)
927aa2e7
JK
5328 {
5329 /* We can only handle a single .debug_types when we have an
5330 index. */
f8c41851 5331 if (per_bfd->types.size () != 1)
927aa2e7
JK
5332 return false;
5333
f8c41851 5334 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
5335
5336 create_signatured_type_table_from_debug_names
f8c41851 5337 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
5338 }
5339
f8c41851 5340 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 5341
f8c41851
SM
5342 per_bfd->debug_names_table = std::move (map);
5343 per_bfd->using_index = 1;
5344 per_bfd->quick_file_names_table =
976ca316 5345 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
927aa2e7 5346
f8c41851
SM
5347 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5348 objfiles using the same BFD. */
5349 gdb_assert (per_bfd->partial_symtabs == nullptr);
5350 per_bfd->partial_symtabs = objfile->partial_symtabs;
5351
927aa2e7
JK
5352 return true;
5353}
5354
927aa2e7
JK
5355/* Type used to manage iterating over all CUs looking for a symbol for
5356 .debug_names. */
5357
5358class dw2_debug_names_iterator
5359{
5360public:
927aa2e7 5361 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5362 gdb::optional<block_enum> block_index,
5363 domain_enum domain,
fcf23d5b 5364 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5365 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5366 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5367 m_per_objfile (per_objfile)
927aa2e7
JK
5368 {}
5369
5370 dw2_debug_names_iterator (const mapped_debug_names &map,
fcf23d5b 5371 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5372 : m_map (map),
5373 m_search (search),
fcf23d5b
SM
5374 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5375 m_per_objfile (per_objfile)
927aa2e7
JK
5376 {}
5377
3b00ef10
TT
5378 dw2_debug_names_iterator (const mapped_debug_names &map,
5379 block_enum block_index, domain_enum domain,
fcf23d5b 5380 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5381 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5382 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5383 m_per_objfile (per_objfile)
3b00ef10
TT
5384 {}
5385
927aa2e7
JK
5386 /* Return the next matching CU or NULL if there are no more. */
5387 dwarf2_per_cu_data *next ();
5388
5389private:
5390 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5391 const char *name,
5392 dwarf2_per_objfile *per_objfile);
927aa2e7 5393 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5394 uint32_t namei,
5395 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5396
5397 /* The internalized form of .debug_names. */
5398 const mapped_debug_names &m_map;
5399
2b79f376
SM
5400 /* If set, only look for symbols that match that block. Valid values are
5401 GLOBAL_BLOCK and STATIC_BLOCK. */
5402 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5403
5404 /* The kind of symbol we're looking for. */
5405 const domain_enum m_domain = UNDEF_DOMAIN;
5406 const search_domain m_search = ALL_DOMAIN;
5407
5408 /* The list of CUs from the index entry of the symbol, or NULL if
5409 not found. */
5410 const gdb_byte *m_addr;
fcf23d5b
SM
5411
5412 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5413};
5414
5415const char *
fcf23d5b 5416mapped_debug_names::namei_to_name
976ca316 5417 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5418{
5419 const ULONGEST namei_string_offs
5420 = extract_unsigned_integer ((name_table_string_offs_reordered
5421 + namei * offset_size),
5422 offset_size,
5423 dwarf5_byte_order);
976ca316 5424 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5425}
5426
5427/* Find a slot in .debug_names for the object named NAME. If NAME is
5428 found, return pointer to its pool data. If NAME cannot be found,
5429 return NULL. */
5430
5431const gdb_byte *
5432dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5433 (const mapped_debug_names &map, const char *name,
5434 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5435{
5436 int (*cmp) (const char *, const char *);
5437
54ee4252 5438 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5439 if (current_language->la_language == language_cplus
5440 || current_language->la_language == language_fortran
5441 || current_language->la_language == language_d)
5442 {
5443 /* NAME is already canonical. Drop any qualifiers as
5444 .debug_names does not contain any. */
5445
5446 if (strchr (name, '(') != NULL)
5447 {
54ee4252 5448 without_params = cp_remove_params (name);
927aa2e7 5449 if (without_params != NULL)
54ee4252 5450 name = without_params.get ();
927aa2e7
JK
5451 }
5452 }
5453
5454 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5455
5456 const uint32_t full_hash = dwarf5_djb_hash (name);
5457 uint32_t namei
5458 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5459 (map.bucket_table_reordered
5460 + (full_hash % map.bucket_count)), 4,
5461 map.dwarf5_byte_order);
5462 if (namei == 0)
5463 return NULL;
5464 --namei;
5465 if (namei >= map.name_count)
5466 {
b98664d3 5467 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5468 "[in module %s]"),
5469 namei, map.name_count,
fcf23d5b 5470 objfile_name (per_objfile->objfile));
927aa2e7
JK
5471 return NULL;
5472 }
5473
5474 for (;;)
5475 {
5476 const uint32_t namei_full_hash
5477 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5478 (map.hash_table_reordered + namei), 4,
5479 map.dwarf5_byte_order);
5480 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5481 return NULL;
5482
5483 if (full_hash == namei_full_hash)
5484 {
fcf23d5b 5485 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5486
5487#if 0 /* An expensive sanity check. */
5488 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5489 {
b98664d3 5490 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5491 "[in module %s]"),
5492 namei, objfile_name (dwarf2_per_objfile->objfile));
5493 return NULL;
5494 }
5495#endif
5496
5497 if (cmp (namei_string, name) == 0)
5498 {
5499 const ULONGEST namei_entry_offs
5500 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5501 + namei * map.offset_size),
5502 map.offset_size, map.dwarf5_byte_order);
5503 return map.entry_pool + namei_entry_offs;
5504 }
5505 }
5506
5507 ++namei;
5508 if (namei >= map.name_count)
5509 return NULL;
5510 }
5511}
5512
5513const gdb_byte *
5514dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5515 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5516{
5517 if (namei >= map.name_count)
5518 {
b98664d3 5519 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5520 "[in module %s]"),
5521 namei, map.name_count,
fcf23d5b 5522 objfile_name (per_objfile->objfile));
927aa2e7
JK
5523 return NULL;
5524 }
5525
5526 const ULONGEST namei_entry_offs
5527 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5528 + namei * map.offset_size),
5529 map.offset_size, map.dwarf5_byte_order);
5530 return map.entry_pool + namei_entry_offs;
5531}
5532
5533/* See dw2_debug_names_iterator. */
5534
5535dwarf2_per_cu_data *
5536dw2_debug_names_iterator::next ()
5537{
5538 if (m_addr == NULL)
5539 return NULL;
5540
fcf23d5b
SM
5541 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5542 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5543 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5544
5545 again:
5546
5547 unsigned int bytes_read;
5548 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5549 m_addr += bytes_read;
5550 if (abbrev == 0)
5551 return NULL;
5552
5553 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5554 if (indexval_it == m_map.abbrev_map.cend ())
5555 {
b98664d3 5556 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5557 "[in module %s]"),
ed2dc618 5558 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5559 return NULL;
5560 }
5561 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5562 enum class symbol_linkage {
5563 unknown,
5564 static_,
5565 extern_,
23c13d42 5566 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5567 dwarf2_per_cu_data *per_cu = NULL;
5568 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5569 {
5570 ULONGEST ull;
5571 switch (attr.form)
5572 {
5573 case DW_FORM_implicit_const:
5574 ull = attr.implicit_const;
5575 break;
5576 case DW_FORM_flag_present:
5577 ull = 1;
5578 break;
5579 case DW_FORM_udata:
5580 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5581 m_addr += bytes_read;
5582 break;
6dc55ce9 5583 case DW_FORM_ref4:
5584 ull = read_4_bytes (abfd, m_addr);
5585 m_addr += 4;
5586 break;
5587 case DW_FORM_ref8:
5588 ull = read_8_bytes (abfd, m_addr);
5589 m_addr += 8;
5590 break;
5591 case DW_FORM_ref_sig8:
5592 ull = read_8_bytes (abfd, m_addr);
5593 m_addr += 8;
5594 break;
927aa2e7 5595 default:
b98664d3 5596 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5597 dwarf_form_name (attr.form),
ed2dc618 5598 objfile_name (objfile));
927aa2e7
JK
5599 return NULL;
5600 }
5601 switch (attr.dw_idx)
5602 {
5603 case DW_IDX_compile_unit:
5604 /* Don't crash on bad data. */
fcf23d5b 5605 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5606 {
b98664d3 5607 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5608 " [in module %s]"),
5609 pulongest (ull),
fcf23d5b 5610 objfile_name (objfile));
927aa2e7
JK
5611 continue;
5612 }
fcf23d5b 5613 per_cu = per_bfd->get_cutu (ull);
927aa2e7 5614 break;
8af5c486
JK
5615 case DW_IDX_type_unit:
5616 /* Don't crash on bad data. */
fcf23d5b 5617 if (ull >= per_bfd->all_type_units.size ())
8af5c486 5618 {
b98664d3 5619 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5620 " [in module %s]"),
5621 pulongest (ull),
fcf23d5b 5622 objfile_name (objfile));
8af5c486
JK
5623 continue;
5624 }
fcf23d5b 5625 per_cu = &per_bfd->get_tu (ull)->per_cu;
8af5c486 5626 break;
6dc55ce9 5627 case DW_IDX_die_offset:
5628 /* In a per-CU index (as opposed to a per-module index), index
5629 entries without CU attribute implicitly refer to the single CU. */
5630 if (per_cu == NULL)
fcf23d5b 5631 per_cu = per_bfd->get_cu (0);
6dc55ce9 5632 break;
927aa2e7
JK
5633 case DW_IDX_GNU_internal:
5634 if (!m_map.augmentation_is_gdb)
5635 break;
23c13d42 5636 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5637 break;
5638 case DW_IDX_GNU_external:
5639 if (!m_map.augmentation_is_gdb)
5640 break;
23c13d42 5641 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5642 break;
5643 }
5644 }
5645
5646 /* Skip if already read in. */
fcf23d5b 5647 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5648 goto again;
5649
5650 /* Check static vs global. */
23c13d42 5651 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5652 {
2b79f376 5653 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5654 const bool symbol_is_static =
5655 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5656 if (want_static != symbol_is_static)
2b79f376 5657 goto again;
927aa2e7
JK
5658 }
5659
5660 /* Match dw2_symtab_iter_next, symbol_kind
5661 and debug_names::psymbol_tag. */
5662 switch (m_domain)
5663 {
5664 case VAR_DOMAIN:
5665 switch (indexval.dwarf_tag)
5666 {
5667 case DW_TAG_variable:
5668 case DW_TAG_subprogram:
5669 /* Some types are also in VAR_DOMAIN. */
5670 case DW_TAG_typedef:
5671 case DW_TAG_structure_type:
5672 break;
5673 default:
5674 goto again;
5675 }
5676 break;
5677 case STRUCT_DOMAIN:
5678 switch (indexval.dwarf_tag)
5679 {
5680 case DW_TAG_typedef:
5681 case DW_TAG_structure_type:
5682 break;
5683 default:
5684 goto again;
5685 }
5686 break;
5687 case LABEL_DOMAIN:
5688 switch (indexval.dwarf_tag)
5689 {
5690 case 0:
5691 case DW_TAG_variable:
5692 break;
5693 default:
5694 goto again;
5695 }
5696 break;
59c35742
AB
5697 case MODULE_DOMAIN:
5698 switch (indexval.dwarf_tag)
5699 {
5700 case DW_TAG_module:
5701 break;
5702 default:
5703 goto again;
5704 }
5705 break;
927aa2e7
JK
5706 default:
5707 break;
5708 }
5709
5710 /* Match dw2_expand_symtabs_matching, symbol_kind and
5711 debug_names::psymbol_tag. */
5712 switch (m_search)
4b514bc8 5713 {
927aa2e7
JK
5714 case VARIABLES_DOMAIN:
5715 switch (indexval.dwarf_tag)
4b514bc8 5716 {
927aa2e7
JK
5717 case DW_TAG_variable:
5718 break;
5719 default:
5720 goto again;
4b514bc8 5721 }
927aa2e7
JK
5722 break;
5723 case FUNCTIONS_DOMAIN:
5724 switch (indexval.dwarf_tag)
4b514bc8 5725 {
927aa2e7
JK
5726 case DW_TAG_subprogram:
5727 break;
5728 default:
5729 goto again;
4b514bc8 5730 }
927aa2e7
JK
5731 break;
5732 case TYPES_DOMAIN:
5733 switch (indexval.dwarf_tag)
5734 {
5735 case DW_TAG_typedef:
5736 case DW_TAG_structure_type:
5737 break;
5738 default:
5739 goto again;
5740 }
5741 break;
59c35742
AB
5742 case MODULES_DOMAIN:
5743 switch (indexval.dwarf_tag)
5744 {
5745 case DW_TAG_module:
5746 break;
5747 default:
5748 goto again;
5749 }
927aa2e7
JK
5750 default:
5751 break;
4b514bc8 5752 }
927aa2e7
JK
5753
5754 return per_cu;
4b514bc8 5755}
61920122 5756
927aa2e7 5757static struct compunit_symtab *
c7f839cb 5758dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5759 const char *name, domain_enum domain)
4b514bc8 5760{
976ca316 5761 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
61920122 5762
976ca316 5763 const auto &mapp = per_objfile->per_bfd->debug_names_table;
927aa2e7 5764 if (!mapp)
61920122 5765 {
927aa2e7
JK
5766 /* index is NULL if OBJF_READNOW. */
5767 return NULL;
5768 }
5769 const auto &map = *mapp;
9291a0cd 5770
976ca316 5771 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
9703b513 5772
927aa2e7
JK
5773 struct compunit_symtab *stab_best = NULL;
5774 struct dwarf2_per_cu_data *per_cu;
5775 while ((per_cu = iter.next ()) != NULL)
5776 {
5777 struct symbol *sym, *with_opaque = NULL;
97a1449a 5778 compunit_symtab *stab
976ca316 5779 = dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7 5780 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5781 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5782
927aa2e7
JK
5783 sym = block_find_symbol (block, name, domain,
5784 block_find_non_opaque_type_preferred,
5785 &with_opaque);
9703b513 5786
927aa2e7
JK
5787 /* Some caution must be observed with overloaded functions and
5788 methods, since the index will not contain any overload
5789 information (but NAME might contain it). */
a3ec0bb1 5790
927aa2e7 5791 if (sym != NULL
987012b8 5792 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5793 return stab;
5794 if (with_opaque != NULL
987012b8 5795 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5796 stab_best = stab;
9703b513 5797
927aa2e7 5798 /* Keep looking through other CUs. */
9703b513
TT
5799 }
5800
927aa2e7 5801 return stab_best;
9703b513
TT
5802}
5803
927aa2e7
JK
5804/* This dumps minimal information about .debug_names. It is called
5805 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5806 uses this to verify that .debug_names has been loaded. */
9291a0cd 5807
927aa2e7
JK
5808static void
5809dw2_debug_names_dump (struct objfile *objfile)
5810{
976ca316 5811 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5812
976ca316 5813 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5814 printf_filtered (".debug_names:");
976ca316 5815 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5816 printf_filtered (" exists\n");
5817 else
5818 printf_filtered (" faked for \"readnow\"\n");
5819 printf_filtered ("\n");
9291a0cd
TT
5820}
5821
9291a0cd 5822static void
927aa2e7
JK
5823dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5824 const char *func_name)
9291a0cd 5825{
976ca316 5826 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 5827
976ca316
SM
5828 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5829 if (per_objfile->per_bfd->debug_names_table)
24c79950 5830 {
976ca316 5831 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
24c79950 5832
fcf23d5b 5833 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
976ca316 5834 per_objfile);
24c79950 5835
927aa2e7
JK
5836 struct dwarf2_per_cu_data *per_cu;
5837 while ((per_cu = iter.next ()) != NULL)
976ca316 5838 dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7
JK
5839 }
5840}
24c79950 5841
3b00ef10
TT
5842static void
5843dw2_debug_names_map_matching_symbols
5844 (struct objfile *objfile,
5845 const lookup_name_info &name, domain_enum domain,
5846 int global,
5847 gdb::function_view<symbol_found_callback_ftype> callback,
5848 symbol_compare_ftype *ordered_compare)
5849{
976ca316 5850 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5851
5852 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5853 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5854 return;
5855
976ca316 5856 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5857 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5858
5859 const char *match_name = name.ada ().lookup_name ().c_str ();
5860 auto matcher = [&] (const char *symname)
5861 {
5862 if (ordered_compare == nullptr)
5863 return true;
5864 return ordered_compare (symname, match_name) == 0;
5865 };
5866
5867 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5868 [&] (offset_type namei)
5869 {
5870 /* The name was matched, now expand corresponding CUs that were
5871 marked. */
fcf23d5b 5872 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
976ca316 5873 per_objfile);
3b00ef10
TT
5874
5875 struct dwarf2_per_cu_data *per_cu;
5876 while ((per_cu = iter.next ()) != NULL)
976ca316 5877 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5878 nullptr);
3b00ef10 5879 return true;
976ca316 5880 }, per_objfile);
3b00ef10
TT
5881
5882 /* It's a shame we couldn't do this inside the
5883 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5884 that have already been expanded. Instead, this loop matches what
5885 the psymtab code does. */
976ca316 5886 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3b00ef10 5887 {
976ca316 5888 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
af758d11 5889 if (symtab != nullptr)
3b00ef10
TT
5890 {
5891 const struct block *block
af758d11 5892 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5893 if (!iterate_over_symbols_terminated (block, name,
5894 domain, callback))
5895 break;
5896 }
5897 }
5898}
5899
927aa2e7
JK
5900static void
5901dw2_debug_names_expand_symtabs_matching
5902 (struct objfile *objfile,
5903 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5904 const lookup_name_info *lookup_name,
927aa2e7
JK
5905 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5906 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5907 enum search_domain kind)
5908{
976ca316 5909 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5910
927aa2e7 5911 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5912 if (!per_objfile->per_bfd->debug_names_table)
927aa2e7 5913 return;
9291a0cd 5914
976ca316 5915 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5916
c1a66c06
TV
5917 if (symbol_matcher == NULL && lookup_name == NULL)
5918 {
976ca316 5919 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5920 {
5921 QUIT;
5922
976ca316
SM
5923 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5924 expansion_notify);
c1a66c06
TV
5925 }
5926 return;
5927 }
5928
976ca316 5929 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5930
c1a66c06 5931 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5932 symbol_matcher,
5933 kind, [&] (offset_type namei)
927aa2e7 5934 {
927aa2e7
JK
5935 /* The name was matched, now expand corresponding CUs that were
5936 marked. */
976ca316 5937 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
bbf2f4df 5938
927aa2e7
JK
5939 struct dwarf2_per_cu_data *per_cu;
5940 while ((per_cu = iter.next ()) != NULL)
976ca316
SM
5941 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5942 expansion_notify);
3b00ef10 5943 return true;
976ca316 5944 }, per_objfile);
9291a0cd
TT
5945}
5946
927aa2e7 5947const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5948{
5949 dw2_has_symbols,
5950 dw2_find_last_source_symtab,
5951 dw2_forget_cached_source_info,
f8eba3c6 5952 dw2_map_symtabs_matching_filename,
927aa2e7 5953 dw2_debug_names_lookup_symbol,
d3214198 5954 NULL,
9291a0cd 5955 dw2_print_stats,
927aa2e7 5956 dw2_debug_names_dump,
927aa2e7 5957 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5958 dw2_expand_all_symtabs,
652a8996 5959 dw2_expand_symtabs_with_fullname,
3b00ef10 5960 dw2_debug_names_map_matching_symbols,
927aa2e7 5961 dw2_debug_names_expand_symtabs_matching,
43f3e411 5962 dw2_find_pc_sect_compunit_symtab,
71a3c369 5963 NULL,
9291a0cd
TT
5964 dw2_map_symbol_filenames
5965};
5966
4485a1c1 5967/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5968 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5969
5970template <typename T>
5971static gdb::array_view<const gdb_byte>
5972get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5973{
5974 dwarf2_section_info *section = &section_owner->gdb_index;
5975
96b79293 5976 if (section->empty ())
4485a1c1
SM
5977 return {};
5978
5979 /* Older elfutils strip versions could keep the section in the main
5980 executable while splitting it for the separate debug info file. */
96b79293 5981 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5982 return {};
5983
96b79293 5984 section->read (obj);
4485a1c1 5985
8bebfcda
PA
5986 /* dwarf2_section_info::size is a bfd_size_type, while
5987 gdb::array_view works with size_t. On 32-bit hosts, with
5988 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5989 is 32-bit. So we need an explicit narrowing conversion here.
5990 This is fine, because it's impossible to allocate or mmap an
5991 array/buffer larger than what size_t can represent. */
5992 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5993}
5994
87d6a7aa
SM
5995/* Lookup the index cache for the contents of the index associated to
5996 DWARF2_OBJ. */
5997
5998static gdb::array_view<const gdb_byte>
5989a64e 5999get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
6000{
6001 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6002 if (build_id == nullptr)
6003 return {};
6004
6005 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 6006 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
6007}
6008
6009/* Same as the above, but for DWZ. */
6010
6011static gdb::array_view<const gdb_byte>
6012get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6013{
6014 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6015 if (build_id == nullptr)
6016 return {};
6017
6018 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6019}
6020
3c0aa29a 6021/* See symfile.h. */
9291a0cd 6022
3c0aa29a
PA
6023bool
6024dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6025{
976ca316
SM
6026 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6027 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 6028
9291a0cd
TT
6029 /* If we're about to read full symbols, don't bother with the
6030 indices. In this case we also don't care if some other debug
6031 format is making psymtabs, because they are all about to be
6032 expanded anyway. */
6033 if ((objfile->flags & OBJF_READNOW))
6034 {
17ee85fc
TT
6035 /* When using READNOW, the using_index flag (set below) indicates that
6036 PER_BFD was already initialized, when we loaded some other objfile. */
6037 if (per_bfd->using_index)
6038 {
6039 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6040 per_objfile->resize_symtabs ();
17ee85fc
TT
6041 return true;
6042 }
6043
6044 per_bfd->using_index = 1;
976ca316
SM
6045 create_all_comp_units (per_objfile);
6046 create_all_type_units (per_objfile);
17ee85fc
TT
6047 per_bfd->quick_file_names_table
6048 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 6049 per_objfile->resize_symtabs ();
9291a0cd 6050
17ee85fc
TT
6051 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6052 + per_bfd->all_type_units.size ()); ++i)
9291a0cd 6053 {
17ee85fc 6054 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
9291a0cd 6055
17ee85fc 6056 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 6057 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6058 }
6059
6060 /* Return 1 so that gdb sees the "quick" functions. However,
6061 these functions will be no-ops because we will have expanded
6062 all symtabs. */
3c0aa29a
PA
6063 *index_kind = dw_index_kind::GDB_INDEX;
6064 return true;
9291a0cd
TT
6065 }
6066
17ee85fc
TT
6067 /* Was a debug names index already read when we processed an objfile sharing
6068 PER_BFD? */
6069 if (per_bfd->debug_names_table != nullptr)
6070 {
6071 *index_kind = dw_index_kind::DEBUG_NAMES;
f8c41851 6072 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6073 per_objfile->resize_symtabs ();
17ee85fc
TT
6074 return true;
6075 }
6076
6077 /* Was a GDB index already read when we processed an objfile sharing
6078 PER_BFD? */
6079 if (per_bfd->index_table != nullptr)
6080 {
6081 *index_kind = dw_index_kind::GDB_INDEX;
f8c41851 6082 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6083 per_objfile->resize_symtabs ();
17ee85fc
TT
6084 return true;
6085 }
6086
efb763a5
SM
6087 /* There might already be partial symtabs built for this BFD. This happens
6088 when loading the same binary twice with the index-cache enabled. If so,
6089 don't try to read an index. The objfile / per_objfile initialization will
6090 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6091 code path. */
6092 if (per_bfd->partial_symtabs != nullptr)
6093 return false;
6094
976ca316 6095 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a
PA
6096 {
6097 *index_kind = dw_index_kind::DEBUG_NAMES;
976ca316 6098 per_objfile->resize_symtabs ();
3c0aa29a
PA
6099 return true;
6100 }
927aa2e7 6101
976ca316 6102 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 6103 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 6104 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6105 {
6106 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6107 per_objfile->resize_symtabs ();
3c0aa29a
PA
6108 return true;
6109 }
9291a0cd 6110
87d6a7aa 6111 /* ... otherwise, try to find the index in the index cache. */
976ca316 6112 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
6113 get_gdb_index_contents_from_cache,
6114 get_gdb_index_contents_from_cache_dwz))
6115 {
6116 global_index_cache.hit ();
6117 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6118 per_objfile->resize_symtabs ();
87d6a7aa
SM
6119 return true;
6120 }
6121
6122 global_index_cache.miss ();
3c0aa29a 6123 return false;
9291a0cd
TT
6124}
6125
6126\f
6127
dce234bc
PP
6128/* Build a partial symbol table. */
6129
6130void
f29dff0a 6131dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6132{
976ca316
SM
6133 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6134 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
6135
6136 if (per_bfd->partial_symtabs != nullptr)
6137 {
6138 /* Partial symbols were already read, so now we can simply
6139 attach them. */
6140 objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6141 per_objfile->resize_symtabs ();
17ee85fc
TT
6142 return;
6143 }
c9bf0622 6144
a70b8144 6145 try
c9bf0622
TT
6146 {
6147 /* This isn't really ideal: all the data we allocate on the
6148 objfile's obstack is still uselessly kept around. However,
6149 freeing it seems unsafe. */
906768f9 6150 psymtab_discarder psymtabs (objfile);
976ca316 6151 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 6152 psymtabs.keep ();
87d6a7aa 6153
976ca316 6154 per_objfile->resize_symtabs ();
af758d11 6155
87d6a7aa 6156 /* (maybe) store an index in the cache. */
976ca316 6157 global_index_cache.store (per_objfile);
c9bf0622 6158 }
230d2906 6159 catch (const gdb_exception_error &except)
492d29ea
PA
6160 {
6161 exception_print (gdb_stderr, except);
6162 }
17ee85fc
TT
6163
6164 /* Finish by setting the local reference to partial symtabs, so that
6165 we don't try to read them again if reading another objfile with the same
6166 BFD. If we can't in fact share, this won't make a difference anyway as
6167 the dwarf2_per_bfd object won't be shared. */
6168 per_bfd->partial_symtabs = objfile->partial_symtabs;
c906108c 6169}
c906108c 6170
3b80fe9b
DE
6171/* Find the base address of the compilation unit for range lists and
6172 location lists. It will normally be specified by DW_AT_low_pc.
6173 In DWARF-3 draft 4, the base address could be overridden by
6174 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6175 compilation units with discontinuous ranges. */
6176
6177static void
6178dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6179{
6180 struct attribute *attr;
6181
2b24b6e4 6182 cu->base_address.reset ();
3b80fe9b
DE
6183
6184 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6185 if (attr != nullptr)
95f982e5 6186 cu->base_address = attr->as_address ();
3b80fe9b
DE
6187 else
6188 {
6189 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6190 if (attr != nullptr)
95f982e5 6191 cu->base_address = attr->as_address ();
3b80fe9b
DE
6192 }
6193}
6194
36586728
TT
6195/* Helper function that returns the proper abbrev section for
6196 THIS_CU. */
6197
6198static struct dwarf2_section_info *
6199get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6200{
6201 struct dwarf2_section_info *abbrev;
c3699833 6202 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6203
6204 if (this_cu->is_dwz)
c3699833 6205 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6206 else
c3699833 6207 abbrev = &per_bfd->abbrev;
36586728
TT
6208
6209 return abbrev;
6210}
6211
f4dc4d17
DE
6212/* Fetch the abbreviation table offset from a comp or type unit header. */
6213
6214static sect_offset
976ca316 6215read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 6216 struct dwarf2_section_info *section,
9c541725 6217 sect_offset sect_off)
f4dc4d17 6218{
96b79293 6219 bfd *abfd = section->get_bfd_owner ();
d521ce57 6220 const gdb_byte *info_ptr;
ac298888 6221 unsigned int initial_length_size, offset_size;
43988095 6222 uint16_t version;
f4dc4d17 6223
976ca316 6224 section->read (per_objfile->objfile);
9c541725 6225 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6226 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6227 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6228 info_ptr += initial_length_size;
6229
6230 version = read_2_bytes (abfd, info_ptr);
6231 info_ptr += 2;
6232 if (version >= 5)
6233 {
6234 /* Skip unit type and address size. */
6235 info_ptr += 2;
6236 }
6237
24aa364d 6238 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6239}
6240
b83470bf
TT
6241/* A partial symtab that is used only for include files. */
6242struct dwarf2_include_psymtab : public partial_symtab
6243{
6244 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6245 : partial_symtab (filename, objfile)
6246 {
6247 }
6248
6249 void read_symtab (struct objfile *objfile) override
6250 {
194d088f
TV
6251 /* It's an include file, no symbols to read for it.
6252 Everything is in the includer symtab. */
6253
6254 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6255 expansion of the includer psymtab. We use the dependencies[0] field to
6256 model the includer. But if we go the regular route of calling
6257 expand_psymtab here, and having expand_psymtab call expand_dependencies
6258 to expand the includer, we'll only use expand_psymtab on the includer
6259 (making it a non-toplevel psymtab), while if we expand the includer via
6260 another path, we'll use read_symtab (making it a toplevel psymtab).
6261 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6262 psymtab, and trigger read_symtab on the includer here directly. */
6263 includer ()->read_symtab (objfile);
b83470bf
TT
6264 }
6265
6266 void expand_psymtab (struct objfile *objfile) override
6267 {
194d088f
TV
6268 /* This is not called by read_symtab, and should not be called by any
6269 expand_dependencies. */
6270 gdb_assert (false);
b83470bf
TT
6271 }
6272
5717c425 6273 bool readin_p (struct objfile *objfile) const override
b83470bf 6274 {
5717c425 6275 return includer ()->readin_p (objfile);
b83470bf
TT
6276 }
6277
5717c425 6278 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6279 {
6280 return nullptr;
6281 }
6282
6283private:
194d088f
TV
6284 partial_symtab *includer () const
6285 {
6286 /* An include psymtab has exactly one dependency: the psymtab that
6287 includes it. */
6288 gdb_assert (this->number_of_dependencies == 1);
6289 return this->dependencies[0];
6290 }
b83470bf
TT
6291};
6292
aaa75496
JB
6293/* Allocate a new partial symtab for file named NAME and mark this new
6294 partial symtab as being an include of PST. */
6295
6296static void
891813be 6297dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
dda83cd7 6298 struct objfile *objfile)
aaa75496 6299{
b83470bf 6300 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6301
fbd9ab74 6302 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6303 subpst->dirname = pst->dirname;
fbd9ab74 6304
a9342b62 6305 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6306 subpst->dependencies[0] = pst;
6307 subpst->number_of_dependencies = 1;
aaa75496
JB
6308}
6309
6310/* Read the Line Number Program data and extract the list of files
6311 included by the source file represented by PST. Build an include
d85a05f0 6312 partial symtab for each of these included files. */
aaa75496
JB
6313
6314static void
6315dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6316 struct die_info *die,
891813be 6317 dwarf2_psymtab *pst)
aaa75496 6318{
fff8551c 6319 line_header_up lh;
d85a05f0 6320 struct attribute *attr;
aaa75496 6321
d85a05f0 6322 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e
TT
6323 if (attr != nullptr && attr->form_is_unsigned ())
6324 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
aaa75496
JB
6325 if (lh == NULL)
6326 return; /* No linetable, so no includes. */
6327
79748972
TT
6328 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6329 that we pass in the raw text_low here; that is ok because we're
6330 only decoding the line table to make include partial symtabs, and
6331 so the addresses aren't really used. */
4ae976d1 6332 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6333 pst->raw_text_low (), 1);
aaa75496
JB
6334}
6335
348e048f 6336static hashval_t
52dc124a 6337hash_signatured_type (const void *item)
348e048f 6338{
9a3c8263
SM
6339 const struct signatured_type *sig_type
6340 = (const struct signatured_type *) item;
9a619af0 6341
348e048f 6342 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6343 return sig_type->signature;
348e048f
DE
6344}
6345
6346static int
52dc124a 6347eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6348{
9a3c8263
SM
6349 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6350 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6351
348e048f
DE
6352 return lhs->signature == rhs->signature;
6353}
6354
1fd400ff
TT
6355/* Allocate a hash table for signatured types. */
6356
b0b6a987 6357static htab_up
298e9637 6358allocate_signatured_type_table ()
1fd400ff 6359{
b0b6a987
TT
6360 return htab_up (htab_create_alloc (41,
6361 hash_signatured_type,
6362 eq_signatured_type,
6363 NULL, xcalloc, xfree));
1fd400ff
TT
6364}
6365
d467dd73 6366/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6367
6368static int
d467dd73 6369add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6370{
9a3c8263 6371 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6372 std::vector<signatured_type *> *all_type_units
6373 = (std::vector<signatured_type *> *) datum;
1fd400ff 6374
b2bdb8cf 6375 all_type_units->push_back (sigt);
1fd400ff
TT
6376
6377 return 1;
6378}
6379
78d4d2c5 6380/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6381 and fill them into TYPES_HTAB. It will process only type units,
6382 therefore DW_UT_type. */
c88ee1f0 6383
78d4d2c5 6384static void
976ca316 6385create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6386 struct dwo_file *dwo_file,
b0b6a987 6387 dwarf2_section_info *section, htab_up &types_htab,
43988095 6388 rcuh_kind section_kind)
348e048f 6389{
976ca316 6390 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6391 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6392 bfd *abfd;
6393 const gdb_byte *info_ptr, *end_ptr;
348e048f 6394
4bdcc0c1
DE
6395 abbrev_section = (dwo_file != NULL
6396 ? &dwo_file->sections.abbrev
976ca316 6397 : &per_objfile->per_bfd->abbrev);
4bdcc0c1 6398
6f738b01
SM
6399 dwarf_read_debug_printf ("Reading %s for %s:",
6400 section->get_name (),
6401 abbrev_section->get_file_name ());
09406207 6402
96b79293 6403 section->read (objfile);
78d4d2c5 6404 info_ptr = section->buffer;
348e048f 6405
78d4d2c5
JK
6406 if (info_ptr == NULL)
6407 return;
348e048f 6408
78d4d2c5
JK
6409 /* We can't set abfd until now because the section may be empty or
6410 not present, in which case the bfd is unknown. */
96b79293 6411 abfd = section->get_bfd_owner ();
348e048f 6412
c0ab21c2
TT
6413 /* We don't use cutu_reader here because we don't need to read
6414 any dies: the signature is in the header. */
3019eac3 6415
78d4d2c5
JK
6416 end_ptr = info_ptr + section->size;
6417 while (info_ptr < end_ptr)
6418 {
78d4d2c5
JK
6419 struct signatured_type *sig_type;
6420 struct dwo_unit *dwo_tu;
6421 void **slot;
6422 const gdb_byte *ptr = info_ptr;
6423 struct comp_unit_head header;
6424 unsigned int length;
8b70b953 6425
9c541725 6426 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6427
a49dd8dd
JK
6428 /* Initialize it due to a false compiler warning. */
6429 header.signature = -1;
9c541725 6430 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6431
78d4d2c5
JK
6432 /* We need to read the type's signature in order to build the hash
6433 table, but we don't need anything else just yet. */
348e048f 6434
976ca316 6435 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6436 abbrev_section, ptr, section_kind);
348e048f 6437
4057dfde 6438 length = header.get_length ();
6caca83c 6439
78d4d2c5
JK
6440 /* Skip dummy type units. */
6441 if (ptr >= info_ptr + length
43988095 6442 || peek_abbrev_code (abfd, ptr) == 0
d2854d8d
CT
6443 || (header.unit_type != DW_UT_type
6444 && header.unit_type != DW_UT_split_type))
78d4d2c5
JK
6445 {
6446 info_ptr += length;
6447 continue;
6448 }
dee91e82 6449
78d4d2c5
JK
6450 if (types_htab == NULL)
6451 {
6452 if (dwo_file)
298e9637 6453 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6454 else
298e9637 6455 types_htab = allocate_signatured_type_table ();
78d4d2c5 6456 }
8b70b953 6457
78d4d2c5
JK
6458 if (dwo_file)
6459 {
6460 sig_type = NULL;
976ca316 6461 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
78d4d2c5 6462 dwo_tu->dwo_file = dwo_file;
43988095 6463 dwo_tu->signature = header.signature;
9c541725 6464 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6465 dwo_tu->section = section;
9c541725 6466 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6467 dwo_tu->length = length;
6468 }
6469 else
6470 {
6471 /* N.B.: type_offset is not usable if this type uses a DWO file.
6472 The real type_offset is in the DWO file. */
6473 dwo_tu = NULL;
976ca316 6474 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
43988095 6475 sig_type->signature = header.signature;
9c541725 6476 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6477 sig_type->per_cu.is_debug_types = 1;
6478 sig_type->per_cu.section = section;
9c541725 6479 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6480 sig_type->per_cu.length = length;
6481 }
6482
b0b6a987 6483 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6484 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6485 INSERT);
6486 gdb_assert (slot != NULL);
6487 if (*slot != NULL)
6488 {
9c541725 6489 sect_offset dup_sect_off;
0349ea22 6490
3019eac3
DE
6491 if (dwo_file)
6492 {
78d4d2c5
JK
6493 const struct dwo_unit *dup_tu
6494 = (const struct dwo_unit *) *slot;
6495
9c541725 6496 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6497 }
6498 else
6499 {
78d4d2c5
JK
6500 const struct signatured_type *dup_tu
6501 = (const struct signatured_type *) *slot;
6502
9c541725 6503 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6504 }
8b70b953 6505
b98664d3 6506 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6507 " the entry at offset %s, signature %s"),
6508 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6509 hex_string (header.signature));
78d4d2c5
JK
6510 }
6511 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6512
6f738b01
SM
6513 dwarf_read_debug_printf_v (" offset %s, signature %s",
6514 sect_offset_str (sect_off),
6515 hex_string (header.signature));
3019eac3 6516
78d4d2c5
JK
6517 info_ptr += length;
6518 }
6519}
3019eac3 6520
78d4d2c5
JK
6521/* Create the hash table of all entries in the .debug_types
6522 (or .debug_types.dwo) section(s).
6523 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6524 otherwise it is NULL.
b3c8eb43 6525
78d4d2c5 6526 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6527
78d4d2c5 6528 Note: This function processes DWO files only, not DWP files. */
348e048f 6529
78d4d2c5 6530static void
976ca316 6531create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6532 struct dwo_file *dwo_file,
fd5866f6 6533 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6534 htab_up &types_htab)
78d4d2c5 6535{
fd5866f6 6536 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6537 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6538 rcuh_kind::TYPE);
3019eac3
DE
6539}
6540
6541/* Create the hash table of all entries in the .debug_types section,
6542 and initialize all_type_units.
6543 The result is zero if there is an error (e.g. missing .debug_types section),
6544 otherwise non-zero. */
6545
6546static int
976ca316 6547create_all_type_units (dwarf2_per_objfile *per_objfile)
3019eac3 6548{
b0b6a987 6549 htab_up types_htab;
3019eac3 6550
976ca316
SM
6551 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6552 types_htab, rcuh_kind::COMPILE);
6553 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6554 types_htab);
3019eac3
DE
6555 if (types_htab == NULL)
6556 {
976ca316 6557 per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6558 return 0;
6559 }
6560
976ca316 6561 per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6562
976ca316
SM
6563 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6564 per_objfile->per_bfd->all_type_units.reserve
6565 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6566
976ca316 6567 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6568 add_signatured_type_cu_to_table,
976ca316 6569 &per_objfile->per_bfd->all_type_units);
1fd400ff 6570
348e048f
DE
6571 return 1;
6572}
6573
5989a64e 6574/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6575 If SLOT is non-NULL, it is the entry to use in the hash table.
6576 Otherwise we find one. */
6577
6578static struct signatured_type *
976ca316 6579add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6580{
976ca316
SM
6581 if (per_objfile->per_bfd->all_type_units.size ()
6582 == per_objfile->per_bfd->all_type_units.capacity ())
6583 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6584
976ca316 6585 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6586
976ca316 6587 per_objfile->resize_symtabs ();
af758d11 6588
976ca316 6589 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6590 sig_type->signature = sig;
6591 sig_type->per_cu.is_debug_types = 1;
976ca316 6592 if (per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6593 {
6594 sig_type->per_cu.v.quick =
976ca316 6595 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6596 struct dwarf2_per_cu_quick_data);
6597 }
6598
6599 if (slot == NULL)
6600 {
976ca316 6601 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6602 sig_type, INSERT);
6603 }
6604 gdb_assert (*slot == NULL);
6605 *slot = sig_type;
6606 /* The rest of sig_type must be filled in by the caller. */
6607 return sig_type;
6608}
6609
a2ce51a0
DE
6610/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6611 Fill in SIG_ENTRY with DWO_ENTRY. */
6612
6613static void
976ca316 6614fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6615 struct signatured_type *sig_entry,
6616 struct dwo_unit *dwo_entry)
6617{
976ca316 6618 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6619
7ee85ab1 6620 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6621 gdb_assert (! sig_entry->per_cu.queued);
976ca316 6622 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6623 if (per_bfd->using_index)
6aa5f3a6
DE
6624 {
6625 gdb_assert (sig_entry->per_cu.v.quick != NULL);
976ca316 6626 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6627 }
6628 else
6629 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6630 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6631 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6632 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6633 gdb_assert (sig_entry->dwo_unit == NULL);
6634
6635 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6636 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6637 sig_entry->per_cu.length = dwo_entry->length;
6638 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6639 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6640 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6641 sig_entry->dwo_unit = dwo_entry;
6642}
6643
6644/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6645 If we haven't read the TU yet, create the signatured_type data structure
6646 for a TU to be read in directly from a DWO file, bypassing the stub.
6647 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6648 using .gdb_index, then when reading a CU we want to stay in the DWO file
6649 containing that CU. Otherwise we could end up reading several other DWO
6650 files (due to comdat folding) to process the transitive closure of all the
6651 mentioned TUs, and that can be slow. The current DWO file will have every
6652 type signature that it needs.
a2ce51a0
DE
6653 We only do this for .gdb_index because in the psymtab case we already have
6654 to read all the DWOs to build the type unit groups. */
6655
6656static struct signatured_type *
6657lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6658{
976ca316 6659 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6660 struct dwo_file *dwo_file;
6661 struct dwo_unit find_dwo_entry, *dwo_entry;
6662 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6663 void **slot;
a2ce51a0 6664
976ca316 6665 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6666
6aa5f3a6
DE
6667 /* If TU skeletons have been removed then we may not have read in any
6668 TUs yet. */
976ca316
SM
6669 if (per_objfile->per_bfd->signatured_types == NULL)
6670 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6671
6672 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6673 Use the global signatured_types array to do our own comdat-folding
6674 of types. If this is the first time we're reading this TU, and
6675 the TU has an entry in .gdb_index, replace the recorded data from
6676 .gdb_index with this TU. */
a2ce51a0 6677
a2ce51a0 6678 find_sig_entry.signature = sig;
976ca316 6679 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6680 &find_sig_entry, INSERT);
9a3c8263 6681 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6682
6683 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6684 read. Don't reassign the global entry to point to this DWO if that's
6685 the case. Also note that if the TU is already being read, it may not
6686 have come from a DWO, the program may be a mix of Fission-compiled
6687 code and non-Fission-compiled code. */
6688
6689 /* Have we already tried to read this TU?
6690 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6691 needn't exist in the global table yet). */
6692 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6693 return sig_entry;
6694
6aa5f3a6
DE
6695 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6696 dwo_unit of the TU itself. */
6697 dwo_file = cu->dwo_unit->dwo_file;
6698
a2ce51a0
DE
6699 /* Ok, this is the first time we're reading this TU. */
6700 if (dwo_file->tus == NULL)
6701 return NULL;
6702 find_dwo_entry.signature = sig;
b0b6a987
TT
6703 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6704 &find_dwo_entry);
a2ce51a0
DE
6705 if (dwo_entry == NULL)
6706 return NULL;
6707
6aa5f3a6
DE
6708 /* If the global table doesn't have an entry for this TU, add one. */
6709 if (sig_entry == NULL)
976ca316 6710 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6711
976ca316 6712 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
89e63ee4 6713 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6714 return sig_entry;
6715}
6716
a2ce51a0
DE
6717/* Subroutine of lookup_signatured_type.
6718 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6719 then try the DWP file. If the TU stub (skeleton) has been removed then
6720 it won't be in .gdb_index. */
a2ce51a0
DE
6721
6722static struct signatured_type *
6723lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6724{
976ca316
SM
6725 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6726 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6727 struct dwo_unit *dwo_entry;
6728 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6729 void **slot;
a2ce51a0 6730
976ca316 6731 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6732 gdb_assert (dwp_file != NULL);
6733
6aa5f3a6
DE
6734 /* If TU skeletons have been removed then we may not have read in any
6735 TUs yet. */
976ca316
SM
6736 if (per_objfile->per_bfd->signatured_types == NULL)
6737 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6738
6aa5f3a6 6739 find_sig_entry.signature = sig;
976ca316 6740 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6741 &find_sig_entry, INSERT);
9a3c8263 6742 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6743
6744 /* Have we already tried to read this TU?
6745 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6746 needn't exist in the global table yet). */
6747 if (sig_entry != NULL)
6748 return sig_entry;
6749
a2ce51a0
DE
6750 if (dwp_file->tus == NULL)
6751 return NULL;
976ca316
SM
6752 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6753 1 /* is_debug_types */);
a2ce51a0
DE
6754 if (dwo_entry == NULL)
6755 return NULL;
6756
976ca316
SM
6757 sig_entry = add_type_unit (per_objfile, sig, slot);
6758 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6759
a2ce51a0
DE
6760 return sig_entry;
6761}
6762
380bca97 6763/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6764 Returns NULL if signature SIG is not present in the table.
6765 It is up to the caller to complain about this. */
348e048f
DE
6766
6767static struct signatured_type *
a2ce51a0 6768lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6769{
976ca316 6770 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6771
976ca316 6772 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6773 {
6774 /* We're in a DWO/DWP file, and we're using .gdb_index.
6775 These cases require special processing. */
976ca316 6776 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6777 return lookup_dwo_signatured_type (cu, sig);
6778 else
6779 return lookup_dwp_signatured_type (cu, sig);
6780 }
6781 else
6782 {
6783 struct signatured_type find_entry, *entry;
348e048f 6784
976ca316 6785 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6786 return NULL;
6787 find_entry.signature = sig;
9a3c8263 6788 entry = ((struct signatured_type *)
976ca316 6789 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6790 &find_entry));
a2ce51a0
DE
6791 return entry;
6792 }
348e048f 6793}
18a8505e 6794
42e7ad6c 6795/* Low level DIE reading support. */
348e048f 6796
d85a05f0
DJ
6797/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6798
6799static void
6800init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6801 struct dwarf2_cu *cu,
3019eac3 6802 struct dwarf2_section_info *section,
685af9cd
TT
6803 struct dwo_file *dwo_file,
6804 struct abbrev_table *abbrev_table)
d85a05f0 6805{
fceca515 6806 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6807 reader->abfd = section->get_bfd_owner ();
d85a05f0 6808 reader->cu = cu;
3019eac3 6809 reader->dwo_file = dwo_file;
dee91e82
DE
6810 reader->die_section = section;
6811 reader->buffer = section->buffer;
f664829e 6812 reader->buffer_end = section->buffer + section->size;
685af9cd 6813 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6814}
6815
c0ab21c2 6816/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6817 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6818 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6819 already.
6820
6821 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6822 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6823 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6824 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6825 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6826 STUB_COMP_DIR may be non-NULL.
3e225074 6827 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6828 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6829 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6830 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6831 kept around for at least as long as *RESULT_READER.
6832
b0c7bfa9
DE
6833 The result is non-zero if a valid (non-dummy) DIE was found. */
6834
6835static int
4ab09049 6836read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6837 struct dwo_unit *dwo_unit,
b0c7bfa9 6838 struct die_info *stub_comp_unit_die,
a2ce51a0 6839 const char *stub_comp_dir,
b0c7bfa9 6840 struct die_reader_specs *result_reader,
d521ce57 6841 const gdb_byte **result_info_ptr,
b0c7bfa9 6842 struct die_info **result_comp_unit_die,
685af9cd 6843 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6844{
976ca316 6845 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6846 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6847 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6848 bfd *abfd;
d521ce57 6849 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6850 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6851 int i,num_extra_attrs;
6852 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6853 struct die_info *comp_unit_die;
6854
b0aeadb3
DE
6855 /* At most one of these may be provided. */
6856 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6857
b0c7bfa9
DE
6858 /* These attributes aren't processed until later:
6859 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6860 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6861 referenced later. However, these attributes are found in the stub
6862 which we won't have later. In order to not impose this complication
6863 on the rest of the code, we read them here and copy them to the
6864 DWO CU/TU die. */
b0c7bfa9
DE
6865
6866 stmt_list = NULL;
6867 low_pc = NULL;
6868 high_pc = NULL;
6869 ranges = NULL;
6870 comp_dir = NULL;
6871
6872 if (stub_comp_unit_die != NULL)
6873 {
6874 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6875 DWO file. */
4ab09049 6876 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6877 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6878 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6879 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6880 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6881 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6882
a39fdb41 6883 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6884
18a8505e
AT
6885 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6886 here (if needed). We need the value before we can process
6887 DW_AT_ranges. */
a39fdb41 6888 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6889 }
a2ce51a0
DE
6890 else if (stub_comp_dir != NULL)
6891 {
6892 /* Reconstruct the comp_dir attribute to simplify the code below. */
fe56917a 6893 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6894 comp_dir->name = DW_AT_comp_dir;
6895 comp_dir->form = DW_FORM_string;
c6481205 6896 comp_dir->set_string_noncanonical (stub_comp_dir);
a2ce51a0 6897 }
b0c7bfa9
DE
6898
6899 /* Set up for reading the DWO CU/TU. */
6900 cu->dwo_unit = dwo_unit;
685af9cd 6901 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6902 section->read (objfile);
6903 abfd = section->get_bfd_owner ();
9c541725
PA
6904 begin_info_ptr = info_ptr = (section->buffer
6905 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6906 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6907
4ab09049 6908 if (per_cu->is_debug_types)
b0c7bfa9 6909 {
4ab09049 6910 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6911
976ca316
SM
6912 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6913 section, dwo_abbrev_section,
43988095 6914 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6915 /* This is not an assert because it can be caused by bad debug info. */
43988095 6916 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6917 {
6918 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6919 " TU at offset %s [in module %s]"),
a2ce51a0 6920 hex_string (sig_type->signature),
43988095 6921 hex_string (cu->header.signature),
9d8780f0 6922 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6923 bfd_get_filename (abfd));
6924 }
9c541725 6925 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6926 /* For DWOs coming from DWP files, we don't know the CU length
6927 nor the type's offset in the TU until now. */
4057dfde 6928 dwo_unit->length = cu->header.get_length ();
9c541725 6929 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6930
6931 /* Establish the type offset that can be used to lookup the type.
6932 For DWO files, we don't know it until now. */
9c541725
PA
6933 sig_type->type_offset_in_section
6934 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6935 }
6936 else
6937 {
976ca316
SM
6938 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6939 section, dwo_abbrev_section,
43988095 6940 info_ptr, rcuh_kind::COMPILE);
9c541725 6941 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6942 /* For DWOs coming from DWP files, we don't know the CU length
6943 until now. */
4057dfde 6944 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6945 }
6946
606decb2 6947 dwo_abbrev_section->read (objfile);
685af9cd 6948 *result_dwo_abbrev_table
606decb2 6949 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
685af9cd
TT
6950 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6951 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6952
6953 /* Read in the die, but leave space to copy over the attributes
6954 from the stub. This has the benefit of simplifying the rest of
6955 the code - all the work to maintain the illusion of a single
6956 DW_TAG_{compile,type}_unit DIE is done here. */
6957 num_extra_attrs = ((stmt_list != NULL)
6958 + (low_pc != NULL)
6959 + (high_pc != NULL)
6960 + (ranges != NULL)
6961 + (comp_dir != NULL));
6962 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6963 num_extra_attrs);
b0c7bfa9
DE
6964
6965 /* Copy over the attributes from the stub to the DIE we just read in. */
6966 comp_unit_die = *result_comp_unit_die;
6967 i = comp_unit_die->num_attrs;
6968 if (stmt_list != NULL)
6969 comp_unit_die->attrs[i++] = *stmt_list;
6970 if (low_pc != NULL)
6971 comp_unit_die->attrs[i++] = *low_pc;
6972 if (high_pc != NULL)
6973 comp_unit_die->attrs[i++] = *high_pc;
6974 if (ranges != NULL)
6975 comp_unit_die->attrs[i++] = *ranges;
6976 if (comp_dir != NULL)
6977 comp_unit_die->attrs[i++] = *comp_dir;
6978 comp_unit_die->num_attrs += num_extra_attrs;
6979
b4f54984 6980 if (dwarf_die_debug)
bf6af496
DE
6981 {
6982 fprintf_unfiltered (gdb_stdlog,
6983 "Read die from %s@0x%x of %s:\n",
96b79293 6984 section->get_name (),
bf6af496
DE
6985 (unsigned) (begin_info_ptr - section->buffer),
6986 bfd_get_filename (abfd));
b4f54984 6987 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6988 }
6989
b0c7bfa9
DE
6990 /* Skip dummy compilation units. */
6991 if (info_ptr >= begin_info_ptr + dwo_unit->length
6992 || peek_abbrev_code (abfd, info_ptr) == 0)
6993 return 0;
6994
6995 *result_info_ptr = info_ptr;
6996 return 1;
6997}
6998
a084a2a6
AT
6999/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7000 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7001 signature is part of the header. */
7002static gdb::optional<ULONGEST>
7003lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7004{
7005 if (cu->header.version >= 5)
7006 return cu->header.signature;
7007 struct attribute *attr;
7008 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
529908cb 7009 if (attr == nullptr || !attr->form_is_unsigned ())
a084a2a6 7010 return gdb::optional<ULONGEST> ();
529908cb 7011 return attr->as_unsigned ();
a084a2a6
AT
7012}
7013
c0ab21c2 7014/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 7015 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7016 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7017
7018static struct dwo_unit *
4ab09049 7019lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 7020{
4ab09049 7021 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 7022 struct dwo_unit *dwo_unit;
c0ab21c2 7023 const char *comp_dir;
b0c7bfa9 7024
a2ce51a0
DE
7025 gdb_assert (cu != NULL);
7026
b0c7bfa9 7027 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7028 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7029 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 7030
4ab09049
SM
7031 if (per_cu->is_debug_types)
7032 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
7033 else
7034 {
a084a2a6 7035 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 7036
a084a2a6 7037 if (!signature.has_value ())
b0c7bfa9
DE
7038 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7039 " [in module %s]"),
4ab09049
SM
7040 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7041
7042 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
7043 }
7044
b0c7bfa9
DE
7045 return dwo_unit;
7046}
7047
c0ab21c2 7048/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 7049 See it for a description of the parameters.
fcd3b13d 7050 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 7051
c0ab21c2 7052void
9e021579
SM
7053cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7054 dwarf2_per_objfile *per_objfile,
2e671100 7055 dwarf2_cu *existing_cu)
a2ce51a0 7056{
a2ce51a0 7057 struct signatured_type *sig_type;
a2ce51a0
DE
7058
7059 /* Verify we can do the following downcast, and that we have the
7060 data we need. */
7061 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7062 sig_type = (struct signatured_type *) this_cu;
7063 gdb_assert (sig_type->dwo_unit != NULL);
7064
2e671100
SM
7065 dwarf2_cu *cu;
7066
7067 if (existing_cu != nullptr)
6aa5f3a6 7068 {
2e671100
SM
7069 cu = existing_cu;
7070 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 7071 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 7072 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
7073 }
7074 else
7075 {
7188ed02 7076 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 7077 in per_objfile yet. */
7188ed02 7078 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 7079 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 7080 cu = m_new_cu.get ();
6aa5f3a6
DE
7081 }
7082
7083 /* A future optimization, if needed, would be to use an existing
7084 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7085 could share abbrev tables. */
a2ce51a0 7086
2e671100 7087 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
7088 NULL /* stub_comp_unit_die */,
7089 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 7090 this, &info_ptr,
3e225074 7091 &comp_unit_die,
c0ab21c2 7092 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7093 {
7094 /* Dummy die. */
c0ab21c2 7095 dummy_p = true;
a2ce51a0 7096 }
a2ce51a0
DE
7097}
7098
fd820528 7099/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7100 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7101
f4dc4d17
DE
7102 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7103 Otherwise the table specified in the comp unit header is read in and used.
7104 This is an optimization for when we already have the abbrev table.
7105
2e671100
SM
7106 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7107 allocated. */
aaa75496 7108
ab432490 7109cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7110 dwarf2_per_objfile *per_objfile,
c0ab21c2 7111 struct abbrev_table *abbrev_table,
2e671100 7112 dwarf2_cu *existing_cu,
c0ab21c2
TT
7113 bool skip_partial)
7114 : die_reader_specs {},
6751ebae 7115 m_this_cu (this_cu)
c906108c 7116{
976ca316 7117 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7118 struct dwarf2_section_info *section = this_cu->section;
96b79293 7119 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 7120 const gdb_byte *begin_info_ptr;
dee91e82 7121 struct signatured_type *sig_type = NULL;
4bdcc0c1 7122 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7123 /* Non-zero if CU currently points to a DWO file and we need to
7124 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7125 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7126 int rereading_dwo_cu = 0;
c906108c 7127
b4f54984 7128 if (dwarf_die_debug)
9d8780f0 7129 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7130 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7131 sect_offset_str (this_cu->sect_off));
09406207 7132
a2ce51a0
DE
7133 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7134 file (instead of going through the stub), short-circuit all of this. */
7135 if (this_cu->reading_dwo_directly)
7136 {
7137 /* Narrow down the scope of possibilities to have to understand. */
7138 gdb_assert (this_cu->is_debug_types);
7139 gdb_assert (abbrev_table == NULL);
976ca316 7140 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
7141 return;
7142 }
7143
dee91e82 7144 /* This is cheap if the section is already read in. */
96b79293 7145 section->read (objfile);
dee91e82 7146
9c541725 7147 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7148
7149 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7150
2e671100
SM
7151 dwarf2_cu *cu;
7152
7153 if (existing_cu != nullptr)
dee91e82 7154 {
2e671100 7155 cu = existing_cu;
42e7ad6c
DE
7156 /* If this CU is from a DWO file we need to start over, we need to
7157 refetch the attributes from the skeleton CU.
7158 This could be optimized by retrieving those attributes from when we
7159 were here the first time: the previous comp_unit_die was stored in
7160 comp_unit_obstack. But there's no data yet that we need this
7161 optimization. */
7162 if (cu->dwo_unit != NULL)
7163 rereading_dwo_cu = 1;
dee91e82
DE
7164 }
7165 else
7166 {
7188ed02 7167 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 7168 in per_objfile yet. */
976ca316
SM
7169 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7170 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 7171 cu = m_new_cu.get ();
42e7ad6c 7172 }
dee91e82 7173
b0c7bfa9 7174 /* Get the header. */
9c541725 7175 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7176 {
7177 /* We already have the header, there's no need to read it in again. */
9c541725 7178 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7179 }
7180 else
7181 {
3019eac3 7182 if (this_cu->is_debug_types)
dee91e82 7183 {
976ca316
SM
7184 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7185 section, abbrev_section,
7186 info_ptr, rcuh_kind::TYPE);
dee91e82 7187
42e7ad6c
DE
7188 /* Since per_cu is the first member of struct signatured_type,
7189 we can go from a pointer to one to a pointer to the other. */
7190 sig_type = (struct signatured_type *) this_cu;
43988095 7191 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7192 gdb_assert (sig_type->type_offset_in_tu
7193 == cu->header.type_cu_offset_in_tu);
7194 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7195
42e7ad6c
DE
7196 /* LENGTH has not been set yet for type units if we're
7197 using .gdb_index. */
4057dfde 7198 this_cu->length = cu->header.get_length ();
3019eac3
DE
7199
7200 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7201 sig_type->type_offset_in_section =
7202 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7203
7204 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7205 }
7206 else
7207 {
976ca316
SM
7208 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7209 section, abbrev_section,
43988095
JK
7210 info_ptr,
7211 rcuh_kind::COMPILE);
dee91e82 7212
9c541725 7213 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7214 if (this_cu->length == 0)
7215 this_cu->length = cu->header.get_length ();
7216 else
7217 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7218 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7219 }
7220 }
10b3939b 7221
6caca83c 7222 /* Skip dummy compilation units. */
dee91e82 7223 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7224 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7225 {
7226 dummy_p = true;
7227 return;
7228 }
6caca83c 7229
433df2d4
DE
7230 /* If we don't have them yet, read the abbrevs for this compilation unit.
7231 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7232 done. */
f4dc4d17 7233 if (abbrev_table != NULL)
685af9cd
TT
7234 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7235 else
f4dc4d17 7236 {
606decb2 7237 abbrev_section->read (objfile);
c0ab21c2 7238 m_abbrev_table_holder
606decb2 7239 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
c0ab21c2 7240 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7241 }
af703f96 7242
dee91e82 7243 /* Read the top level CU/TU die. */
c0ab21c2 7244 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7245 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7246
58f0c718 7247 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7248 {
7249 dummy_p = true;
7250 return;
7251 }
58f0c718 7252
b0c7bfa9 7253 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7254 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7255 table from the DWO file and pass the ownership over to us. It will be
7256 referenced from READER, so we must make sure to free it after we're done
7257 with READER.
7258
b0c7bfa9
DE
7259 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7260 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7261 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7262 if (dwo_name != nullptr)
3019eac3 7263 {
3019eac3 7264 struct dwo_unit *dwo_unit;
b0c7bfa9 7265 struct die_info *dwo_comp_unit_die;
3019eac3 7266
3e225074 7267 if (comp_unit_die->has_children)
6a506a2d 7268 {
b98664d3 7269 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7270 " has children (offset %s) [in module %s]"),
7271 sect_offset_str (this_cu->sect_off),
7272 bfd_get_filename (abfd));
6a506a2d 7273 }
4ab09049 7274 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7275 if (dwo_unit != NULL)
3019eac3 7276 {
4ab09049 7277 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7278 comp_unit_die, NULL,
c0ab21c2 7279 this, &info_ptr,
3e225074 7280 &dwo_comp_unit_die,
c0ab21c2 7281 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7282 {
7283 /* Dummy die. */
c0ab21c2 7284 dummy_p = true;
6a506a2d
DE
7285 return;
7286 }
7287 comp_unit_die = dwo_comp_unit_die;
7288 }
7289 else
7290 {
7291 /* Yikes, we couldn't find the rest of the DIE, we only have
7292 the stub. A complaint has already been logged. There's
7293 not much more we can do except pass on the stub DIE to
7294 die_reader_func. We don't want to throw an error on bad
7295 debug info. */
3019eac3
DE
7296 }
7297 }
c0ab21c2 7298}
3019eac3 7299
6751ebae
TT
7300void
7301cutu_reader::keep ()
c0ab21c2 7302{
b0c7bfa9 7303 /* Done, clean up. */
6751ebae
TT
7304 gdb_assert (!dummy_p);
7305 if (m_new_cu != NULL)
348e048f 7306 {
7188ed02 7307 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
dda83cd7 7308 now. */
7188ed02
SM
7309 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7310 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 7311 }
dee91e82
DE
7312}
7313
18a8505e
AT
7314/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7315 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7316 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7317
7318 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7319 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7320
7321 We fill in THIS_CU->length.
7322
dee91e82 7323 THIS_CU->cu is always freed when done.
3019eac3 7324 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7325 to care whether it refers to the "main" CU or the DWO CU.
7326
7327 When parent_cu is passed, it is used to provide a default value for
7328 str_offsets_base and addr_base from the parent. */
dee91e82 7329
ab432490 7330cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7331 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
7332 struct dwarf2_cu *parent_cu,
7333 struct dwo_file *dwo_file)
7334 : die_reader_specs {},
7335 m_this_cu (this_cu)
dee91e82 7336{
976ca316 7337 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7338 struct dwarf2_section_info *section = this_cu->section;
96b79293 7339 bfd *abfd = section->get_bfd_owner ();
33e80786 7340 struct dwarf2_section_info *abbrev_section;
d521ce57 7341 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7342
b4f54984 7343 if (dwarf_die_debug)
9d8780f0 7344 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7345 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7346 sect_offset_str (this_cu->sect_off));
09406207 7347
976ca316 7348 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7349
33e80786
DE
7350 abbrev_section = (dwo_file != NULL
7351 ? &dwo_file->sections.abbrev
7352 : get_abbrev_section_for_cu (this_cu));
7353
dee91e82 7354 /* This is cheap if the section is already read in. */
96b79293 7355 section->read (objfile);
dee91e82 7356
976ca316 7357 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 7358
9c541725 7359 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
7360 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7361 section, abbrev_section, info_ptr,
43988095
JK
7362 (this_cu->is_debug_types
7363 ? rcuh_kind::TYPE
7364 : rcuh_kind::COMPILE));
dee91e82 7365
18a8505e
AT
7366 if (parent_cu != nullptr)
7367 {
c0ab21c2
TT
7368 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7369 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7370 }
4057dfde 7371 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7372
7373 /* Skip dummy compilation units. */
7374 if (info_ptr >= begin_info_ptr + this_cu->length
7375 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7376 {
7377 dummy_p = true;
7378 return;
7379 }
72bf9492 7380
606decb2 7381 abbrev_section->read (objfile);
c0ab21c2 7382 m_abbrev_table_holder
606decb2 7383 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
dee91e82 7384
c0ab21c2
TT
7385 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7386 m_abbrev_table_holder.get ());
3e225074 7387 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7388}
7389
0018ea6f
DE
7390\f
7391/* Type Unit Groups.
dee91e82 7392
0018ea6f
DE
7393 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7394 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7395 so that all types coming from the same compilation (.o file) are grouped
7396 together. A future step could be to put the types in the same symtab as
7397 the CU the types ultimately came from. */
ff013f42 7398
f4dc4d17
DE
7399static hashval_t
7400hash_type_unit_group (const void *item)
7401{
9a3c8263
SM
7402 const struct type_unit_group *tu_group
7403 = (const struct type_unit_group *) item;
f4dc4d17 7404
094b34ac 7405 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7406}
348e048f
DE
7407
7408static int
f4dc4d17 7409eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7410{
9a3c8263
SM
7411 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7412 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7413
094b34ac 7414 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7415}
348e048f 7416
f4dc4d17
DE
7417/* Allocate a hash table for type unit groups. */
7418
eaa5fa8b 7419static htab_up
298e9637 7420allocate_type_unit_groups_table ()
f4dc4d17 7421{
eaa5fa8b
TT
7422 return htab_up (htab_create_alloc (3,
7423 hash_type_unit_group,
7424 eq_type_unit_group,
7425 NULL, xcalloc, xfree));
f4dc4d17 7426}
dee91e82 7427
f4dc4d17
DE
7428/* Type units that don't have DW_AT_stmt_list are grouped into their own
7429 partial symtabs. We combine several TUs per psymtab to not let the size
7430 of any one psymtab grow too big. */
7431#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7432#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7433
094b34ac 7434/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7435 Create the type_unit_group object used to hold one or more TUs. */
7436
7437static struct type_unit_group *
094b34ac 7438create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7439{
976ca316
SM
7440 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7441 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
094b34ac 7442 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7443 struct type_unit_group *tu_group;
f4dc4d17 7444
976ca316 7445 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
094b34ac 7446 per_cu = &tu_group->per_cu;
1859c670 7447 per_cu->per_bfd = per_bfd;
f4dc4d17 7448
1859c670 7449 if (per_bfd->using_index)
094b34ac 7450 {
1859c670 7451 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7452 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7453 }
7454 else
7455 {
9c541725 7456 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7457 dwarf2_psymtab *pst;
528e1572 7458 std::string name;
094b34ac
DE
7459
7460 /* Give the symtab a useful name for debug purposes. */
7461 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7462 name = string_printf ("<type_units_%d>",
7463 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7464 else
528e1572 7465 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7466
976ca316 7467 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
6d94535f 7468 pst->anonymous = true;
094b34ac 7469 }
f4dc4d17 7470
094b34ac 7471 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7472 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7473
7474 return tu_group;
7475}
7476
094b34ac
DE
7477/* Look up the type_unit_group for type unit CU, and create it if necessary.
7478 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7479
7480static struct type_unit_group *
ff39bb5e 7481get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7482{
976ca316
SM
7483 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7484 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7485 struct type_unit_group *tu_group;
7486 void **slot;
7487 unsigned int line_offset;
7488 struct type_unit_group type_unit_group_for_lookup;
7489
976ca316
SM
7490 if (per_objfile->per_bfd->type_unit_groups == NULL)
7491 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7492
7493 /* Do we need to create a new group, or can we use an existing one? */
7494
529908cb 7495 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
f4dc4d17 7496 {
529908cb 7497 line_offset = stmt_list->as_unsigned ();
f4dc4d17
DE
7498 ++tu_stats->nr_symtab_sharers;
7499 }
7500 else
7501 {
7502 /* Ugh, no stmt_list. Rare, but we have to handle it.
7503 We can do various things here like create one group per TU or
7504 spread them over multiple groups to split up the expansion work.
7505 To avoid worst case scenarios (too many groups or too large groups)
7506 we, umm, group them in bunches. */
7507 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7508 | (tu_stats->nr_stmt_less_type_units
7509 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7510 ++tu_stats->nr_stmt_less_type_units;
7511 }
7512
094b34ac 7513 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7514 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7515 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7516 &type_unit_group_for_lookup, INSERT);
7517 if (*slot != NULL)
7518 {
9a3c8263 7519 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7520 gdb_assert (tu_group != NULL);
7521 }
7522 else
7523 {
9c541725 7524 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7525 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7526 *slot = tu_group;
7527 ++tu_stats->nr_symtabs;
7528 }
7529
7530 return tu_group;
7531}
0018ea6f
DE
7532\f
7533/* Partial symbol tables. */
7534
7535/* Create a psymtab named NAME and assign it to PER_CU.
7536
7537 The caller must fill in the following details:
7538 dirname, textlow, texthigh. */
7539
891813be 7540static dwarf2_psymtab *
7aa104c4
SM
7541create_partial_symtab (dwarf2_per_cu_data *per_cu,
7542 dwarf2_per_objfile *per_objfile,
7543 const char *name)
0018ea6f 7544{
7aa104c4 7545 struct objfile *objfile = per_objfile->objfile;
891813be 7546 dwarf2_psymtab *pst;
0018ea6f 7547
9f4e76a4 7548 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7549
6d94535f 7550 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7551
7552 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7553 per_cu->v.psymtab = pst;
7554
7555 return pst;
7556}
7557
c0ab21c2 7558/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7559
7560static void
7561process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7562 const gdb_byte *info_ptr,
0018ea6f 7563 struct die_info *comp_unit_die,
c0ab21c2 7564 enum language pretend_language)
0018ea6f
DE
7565{
7566 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7567 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7568 struct objfile *objfile = per_objfile->objfile;
08feed99 7569 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7570 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7571 CORE_ADDR baseaddr;
7572 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7573 dwarf2_psymtab *pst;
3a2b436a 7574 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7575 const char *filename;
0018ea6f 7576
0018ea6f
DE
7577 gdb_assert (! per_cu->is_debug_types);
7578
c0ab21c2 7579 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7580
0018ea6f 7581 /* Allocate a new partial symbol table structure. */
2e927613
TV
7582 gdb::unique_xmalloc_ptr<char> debug_filename;
7583 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7584 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7585 if (filename == NULL)
0018ea6f 7586 filename = "";
2e927613
TV
7587 else if (strcmp (filename, artificial) == 0)
7588 {
7589 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7590 sect_offset_str (per_cu->sect_off),
7591 (char *) NULL));
2e927613
TV
7592 filename = debug_filename.get ();
7593 }
0018ea6f 7594
7aa104c4 7595 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7596
7597 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7598 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7599
b3b3bada 7600 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7601
7602 dwarf2_find_base_address (comp_unit_die, cu);
7603
7604 /* Possibly set the default values of LOWPC and HIGHPC from
7605 `DW_AT_ranges'. */
3a2b436a
JK
7606 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7607 &best_highpc, cu, pst);
7608 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7609 {
7610 CORE_ADDR low
7611 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7612 - baseaddr);
7613 CORE_ADDR high
7614 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7615 - baseaddr - 1);
7616 /* Store the contiguous range if it is not empty; it can be
7617 empty for CUs with no code. */
d320c2b5
TT
7618 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7619 low, high, pst);
79748972 7620 }
0018ea6f
DE
7621
7622 /* Check if comp unit has_children.
7623 If so, read the rest of the partial symbols from this comp unit.
7624 If not, there's no more debug_info for this comp unit. */
3e225074 7625 if (comp_unit_die->has_children)
0018ea6f
DE
7626 {
7627 struct partial_die_info *first_die;
7628 CORE_ADDR lowpc, highpc;
7629
7630 lowpc = ((CORE_ADDR) -1);
7631 highpc = ((CORE_ADDR) 0);
7632
7633 first_die = load_partial_dies (reader, info_ptr, 1);
7634
7635 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7636 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7637
7638 /* If we didn't find a lowpc, set it to highpc to avoid
7639 complaints from `maint check'. */
7640 if (lowpc == ((CORE_ADDR) -1))
7641 lowpc = highpc;
7642
7643 /* If the compilation unit didn't have an explicit address range,
7644 then use the information extracted from its child dies. */
e385593e 7645 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7646 {
7647 best_lowpc = lowpc;
7648 best_highpc = highpc;
7649 }
7650 }
4ae976d1 7651 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7652 best_lowpc + baseaddr)
7653 - baseaddr);
4ae976d1 7654 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7655 best_highpc + baseaddr)
7656 - baseaddr);
0018ea6f 7657
ae7754b2 7658 pst->end ();
0018ea6f 7659
ae640021 7660 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7661 {
7662 int i;
ae640021 7663 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7664
7665 /* Fill in 'dependencies' here; we fill in 'users' in a
7666 post-pass. */
7667 pst->number_of_dependencies = len;
a9342b62
TT
7668 pst->dependencies
7669 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7670 for (i = 0; i < len; ++i)
7671 {
7672 pst->dependencies[i]
7673 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7674 }
0018ea6f 7675
ae640021 7676 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7677 }
7678
7679 /* Get the list of files included in the current compilation unit,
7680 and build a psymtab for each of them. */
7681 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7682
6f738b01
SM
7683 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7684 ", %d global, %d static syms",
7685 per_cu->is_debug_types ? "type" : "comp",
7686 sect_offset_str (per_cu->sect_off),
7687 paddress (gdbarch, pst->text_low (objfile)),
7688 paddress (gdbarch, pst->text_high (objfile)),
7689 (int) pst->global_psymbols.size (),
7690 (int) pst->static_psymbols.size ());
0018ea6f
DE
7691}
7692
7693/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7694 Process compilation unit THIS_CU for a psymtab. */
7695
7696static void
ab432490
SM
7697process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7698 dwarf2_per_objfile *per_objfile,
135f5437 7699 bool want_partial_unit,
b93601f3 7700 enum language pretend_language)
0018ea6f
DE
7701{
7702 /* If this compilation unit was already read in, free the
7703 cached copy in order to read it in again. This is
7704 necessary because we skipped some symbols when we first
7705 read in the compilation unit (see load_partial_dies).
7706 This problem could be avoided, but the benefit is unclear. */
7188ed02 7707 per_objfile->remove_cu (this_cu);
0018ea6f 7708
2e671100 7709 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7710
58990295
TV
7711 switch (reader.comp_unit_die->tag)
7712 {
7713 case DW_TAG_compile_unit:
7714 this_cu->unit_type = DW_UT_compile;
7715 break;
7716 case DW_TAG_partial_unit:
7717 this_cu->unit_type = DW_UT_partial;
7718 break;
7719 default:
7720 abort ();
7721 }
7722
c0ab21c2 7723 if (reader.dummy_p)
f1902523 7724 {
c0ab21c2 7725 /* Nothing. */
f1902523 7726 }
c0ab21c2 7727 else if (this_cu->is_debug_types)
3e225074
TT
7728 build_type_psymtabs_reader (&reader, reader.info_ptr,
7729 reader.comp_unit_die);
135f5437
TT
7730 else if (want_partial_unit
7731 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7732 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7733 reader.comp_unit_die,
c0ab21c2 7734 pretend_language);
0018ea6f 7735
7188ed02 7736 this_cu->lang = reader.cu->language;
58990295 7737
0018ea6f 7738 /* Age out any secondary CUs. */
7188ed02 7739 per_objfile->age_comp_units ();
0018ea6f 7740}
f4dc4d17
DE
7741
7742/* Reader function for build_type_psymtabs. */
7743
7744static void
7745build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7746 const gdb_byte *info_ptr,
3e225074 7747 struct die_info *type_unit_die)
f4dc4d17 7748{
976ca316 7749 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7750 struct dwarf2_cu *cu = reader->cu;
7751 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7752 struct signatured_type *sig_type;
f4dc4d17
DE
7753 struct type_unit_group *tu_group;
7754 struct attribute *attr;
7755 struct partial_die_info *first_die;
7756 CORE_ADDR lowpc, highpc;
891813be 7757 dwarf2_psymtab *pst;
f4dc4d17 7758
0186c6a7
DE
7759 gdb_assert (per_cu->is_debug_types);
7760 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7761
3e225074 7762 if (! type_unit_die->has_children)
f4dc4d17
DE
7763 return;
7764
052c8bb8 7765 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7766 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7767
df07e2c7 7768 if (tu_group->tus == nullptr)
a8b3b8e9 7769 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7770 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7771
7772 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7773 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7774 pst->anonymous = true;
f4dc4d17
DE
7775
7776 first_die = load_partial_dies (reader, info_ptr, 1);
7777
7778 lowpc = (CORE_ADDR) -1;
7779 highpc = (CORE_ADDR) 0;
7780 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7781
ae7754b2 7782 pst->end ();
f4dc4d17
DE
7783}
7784
73051182
DE
7785/* Struct used to sort TUs by their abbreviation table offset. */
7786
7787struct tu_abbrev_offset
7788{
b2bdb8cf
SM
7789 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7790 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7791 {}
7792
7793 signatured_type *sig_type;
73051182
DE
7794 sect_offset abbrev_offset;
7795};
7796
484cf504 7797/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7798
484cf504
TT
7799static bool
7800sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7801 const struct tu_abbrev_offset &b)
73051182 7802{
484cf504 7803 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7804}
7805
7806/* Efficiently read all the type units.
7807 This does the bulk of the work for build_type_psymtabs.
7808
7809 The efficiency is because we sort TUs by the abbrev table they use and
7810 only read each abbrev table once. In one program there are 200K TUs
7811 sharing 8K abbrev tables.
7812
7813 The main purpose of this function is to support building the
5989a64e 7814 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7815 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7816 can collapse the search space by grouping them by stmt_list.
7817 The savings can be significant, in the same program from above the 200K TUs
7818 share 8K stmt_list tables.
7819
7820 FUNC is expected to call get_type_unit_group, which will create the
7821 struct type_unit_group if necessary and add it to
5989a64e 7822 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7823
7824static void
976ca316 7825build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
73051182 7826{
976ca316 7827 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7828 abbrev_table_up abbrev_table;
73051182 7829 sect_offset abbrev_offset;
73051182
DE
7830
7831 /* It's up to the caller to not call us multiple times. */
976ca316 7832 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7833
976ca316 7834 if (per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7835 return;
7836
7837 /* TUs typically share abbrev tables, and there can be way more TUs than
7838 abbrev tables. Sort by abbrev table to reduce the number of times we
7839 read each abbrev table in.
7840 Alternatives are to punt or to maintain a cache of abbrev tables.
7841 This is simpler and efficient enough for now.
7842
7843 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7844 symtab to use). Typically TUs with the same abbrev offset have the same
7845 stmt_list value too so in practice this should work well.
7846
7847 The basic algorithm here is:
7848
7849 sort TUs by abbrev table
7850 for each TU with same abbrev table:
7851 read abbrev table if first user
7852 read TU top level DIE
7853 [IWBN if DWO skeletons had DW_AT_stmt_list]
7854 call FUNC */
7855
6f738b01 7856 dwarf_read_debug_printf ("Building type unit groups ...");
73051182
DE
7857
7858 /* Sort in a separate table to maintain the order of all_type_units
7859 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7860 std::vector<tu_abbrev_offset> sorted_by_abbrev;
976ca316 7861 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7862
976ca316 7863 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
b2bdb8cf 7864 sorted_by_abbrev.emplace_back
976ca316 7865 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
b2bdb8cf 7866 sig_type->per_cu.sect_off));
73051182 7867
484cf504
TT
7868 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7869 sort_tu_by_abbrev_offset);
73051182 7870
9c541725 7871 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7872
b2bdb8cf 7873 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7874 {
73051182
DE
7875 /* Switch to the next abbrev table if necessary. */
7876 if (abbrev_table == NULL
b2bdb8cf 7877 || tu.abbrev_offset != abbrev_offset)
73051182 7878 {
b2bdb8cf 7879 abbrev_offset = tu.abbrev_offset;
606decb2 7880 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
73051182 7881 abbrev_table =
606decb2 7882 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7883 ++tu_stats->nr_uniq_abbrev_tables;
7884 }
7885
976ca316 7886 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
2e671100 7887 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7888 if (!reader.dummy_p)
7889 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7890 reader.comp_unit_die);
73051182 7891 }
6aa5f3a6 7892}
73051182 7893
6aa5f3a6
DE
7894/* Print collected type unit statistics. */
7895
7896static void
976ca316 7897print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7898{
976ca316 7899 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6 7900
6f738b01
SM
7901 dwarf_read_debug_printf ("Type unit statistics:");
7902 dwarf_read_debug_printf (" %zu TUs",
7903 per_objfile->per_bfd->all_type_units.size ());
7904 dwarf_read_debug_printf (" %d uniq abbrev tables",
7905 tu_stats->nr_uniq_abbrev_tables);
7906 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7907 tu_stats->nr_symtabs);
7908 dwarf_read_debug_printf (" %d symtab sharers",
7909 tu_stats->nr_symtab_sharers);
7910 dwarf_read_debug_printf (" %d type units without a stmt_list",
7911 tu_stats->nr_stmt_less_type_units);
7912 dwarf_read_debug_printf (" %d all_type_units reallocs",
7913 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7914}
7915
f4dc4d17
DE
7916/* Traversal function for build_type_psymtabs. */
7917
7918static int
7919build_type_psymtab_dependencies (void **slot, void *info)
7920{
976ca316
SM
7921 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7922 struct objfile *objfile = per_objfile->objfile;
f4dc4d17 7923 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7924 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7925 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7926 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7927 int i;
7928
7929 gdb_assert (len > 0);
197400e8 7930 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7931
7932 pst->number_of_dependencies = len;
a9342b62 7933 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7934 for (i = 0; i < len; ++i)
f4dc4d17 7935 {
df07e2c7 7936 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7937 gdb_assert (iter->per_cu.is_debug_types);
7938 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7939 iter->type_unit_group = tu_group;
f4dc4d17
DE
7940 }
7941
df07e2c7
AB
7942 delete tu_group->tus;
7943 tu_group->tus = nullptr;
348e048f
DE
7944
7945 return 1;
7946}
7947
7948/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7949 Build partial symbol tables for the .debug_types comp-units. */
7950
7951static void
976ca316 7952build_type_psymtabs (dwarf2_per_objfile *per_objfile)
348e048f 7953{
976ca316 7954 if (! create_all_type_units (per_objfile))
348e048f
DE
7955 return;
7956
976ca316 7957 build_type_psymtabs_1 (per_objfile);
6aa5f3a6 7958}
f4dc4d17 7959
6aa5f3a6
DE
7960/* Traversal function for process_skeletonless_type_unit.
7961 Read a TU in a DWO file and build partial symbols for it. */
7962
7963static int
7964process_skeletonless_type_unit (void **slot, void *info)
7965{
7966 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 7967 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
7968 struct signatured_type find_entry, *entry;
7969
7970 /* If this TU doesn't exist in the global table, add it and read it in. */
7971
976ca316
SM
7972 if (per_objfile->per_bfd->signatured_types == NULL)
7973 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7974
7975 find_entry.signature = dwo_unit->signature;
976ca316 7976 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7977 &find_entry, INSERT);
6aa5f3a6
DE
7978 /* If we've already seen this type there's nothing to do. What's happening
7979 is we're doing our own version of comdat-folding here. */
7980 if (*slot != NULL)
7981 return 1;
7982
7983 /* This does the job that create_all_type_units would have done for
7984 this TU. */
976ca316
SM
7985 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7986 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7987 *slot = entry;
7988
7989 /* This does the job that build_type_psymtabs_1 would have done. */
976ca316 7990 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
7991 if (!reader.dummy_p)
7992 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7993 reader.comp_unit_die);
6aa5f3a6
DE
7994
7995 return 1;
7996}
7997
7998/* Traversal function for process_skeletonless_type_units. */
7999
8000static int
8001process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8002{
8003 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8004
8005 if (dwo_file->tus != NULL)
b0b6a987
TT
8006 htab_traverse_noresize (dwo_file->tus.get (),
8007 process_skeletonless_type_unit, info);
6aa5f3a6
DE
8008
8009 return 1;
8010}
8011
8012/* Scan all TUs of DWO files, verifying we've processed them.
8013 This is needed in case a TU was emitted without its skeleton.
8014 Note: This can't be done until we know what all the DWO files are. */
8015
8016static void
976ca316 8017process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
8018{
8019 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
8020 if (get_dwp_file (per_objfile) == NULL
8021 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 8022 {
976ca316 8023 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 8024 process_dwo_file_for_skeletonless_type_units,
976ca316 8025 per_objfile);
6aa5f3a6 8026 }
348e048f
DE
8027}
8028
ed2dc618 8029/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8030
8031static void
976ca316 8032set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 8033{
976ca316 8034 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 8035 {
891813be 8036 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 8037
36586728
TT
8038 if (pst == NULL)
8039 continue;
8040
b76e467d 8041 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8042 {
8043 /* Set the 'user' field only if it is not already set. */
8044 if (pst->dependencies[j]->user == NULL)
8045 pst->dependencies[j]->user = pst;
8046 }
8047 }
8048}
8049
93311388
DE
8050/* Build the partial symbol table by doing a quick pass through the
8051 .debug_info and .debug_abbrev sections. */
72bf9492 8052
93311388 8053static void
976ca316 8054dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 8055{
976ca316 8056 struct objfile *objfile = per_objfile->objfile;
93311388 8057
6f738b01
SM
8058 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8059 objfile_name (objfile));
45cfd468 8060
76935768 8061 scoped_restore restore_reading_psyms
976ca316 8062 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
76935768 8063 true);
98bfdba5 8064
976ca316 8065 per_objfile->per_bfd->info.read (objfile);
91c24f0a 8066
93311388
DE
8067 /* Any cached compilation units will be linked by the per-objfile
8068 read_in_chain. Make sure to free them when we're done. */
976ca316 8069 free_cached_comp_units freer (per_objfile);
72bf9492 8070
976ca316 8071 build_type_psymtabs (per_objfile);
348e048f 8072
976ca316 8073 create_all_comp_units (per_objfile);
c906108c 8074
60606b2c
TT
8075 /* Create a temporary address map on a temporary obstack. We later
8076 copy this to the final obstack. */
8268c778 8077 auto_obstack temp_obstack;
791afaa2
TT
8078
8079 scoped_restore save_psymtabs_addrmap
d320c2b5 8080 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8081 addrmap_create_mutable (&temp_obstack));
72bf9492 8082
976ca316 8083 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
8084 {
8085 if (per_cu->v.psymtab != NULL)
8086 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8087 continue;
976ca316 8088 process_psymtab_comp_unit (per_cu, per_objfile, false,
ab432490 8089 language_minimal);
3d5afab3 8090 }
ff013f42 8091
6aa5f3a6 8092 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 8093 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
8094
8095 /* Now that all TUs have been processed we can fill in the dependencies. */
976ca316 8096 if (per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 8097 {
976ca316
SM
8098 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8099 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
8100 }
8101
6f738b01 8102 if (dwarf_read_debug > 0)
976ca316 8103 print_tu_stats (per_objfile);
6aa5f3a6 8104
976ca316 8105 set_partial_user (per_objfile);
95554aad 8106
d320c2b5
TT
8107 objfile->partial_symtabs->psymtabs_addrmap
8108 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8109 objfile->partial_symtabs->obstack ());
791afaa2
TT
8110 /* At this point we want to keep the address map. */
8111 save_psymtabs_addrmap.release ();
ff013f42 8112
6f738b01
SM
8113 dwarf_read_debug_printf ("Done building psymtabs of %s",
8114 objfile_name (objfile));
ae038cb0
DJ
8115}
8116
dee91e82
DE
8117/* Load the partial DIEs for a secondary CU into memory.
8118 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8119
dee91e82 8120static void
ab432490 8121load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
8122 dwarf2_per_objfile *per_objfile,
8123 dwarf2_cu *existing_cu)
dee91e82 8124{
2e671100 8125 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
8126
8127 if (!reader.dummy_p)
8128 {
8129 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8130 language_minimal);
8131
8132 /* Check if comp unit has_children.
8133 If so, read the rest of the partial symbols from this comp unit.
8134 If not, there's no more debug_info for this comp unit. */
3e225074 8135 if (reader.comp_unit_die->has_children)
c0ab21c2 8136 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8137
8138 reader.keep ();
c0ab21c2 8139 }
ae038cb0
DJ
8140}
8141
ae038cb0 8142static void
976ca316 8143read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 8144 struct dwarf2_section_info *section,
f1902523 8145 struct dwarf2_section_info *abbrev_section,
b76e467d 8146 unsigned int is_dwz)
ae038cb0 8147{
d521ce57 8148 const gdb_byte *info_ptr;
976ca316 8149 struct objfile *objfile = per_objfile->objfile;
be391dca 8150
6f738b01
SM
8151 dwarf_read_debug_printf ("Reading %s for %s",
8152 section->get_name (),
8153 section->get_file_name ());
bf6af496 8154
96b79293 8155 section->read (objfile);
ae038cb0 8156
36586728 8157 info_ptr = section->buffer;
6e70227d 8158
36586728 8159 while (info_ptr < section->buffer + section->size)
ae038cb0 8160 {
ae038cb0 8161 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8162
9c541725 8163 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8164
f1902523 8165 comp_unit_head cu_header;
976ca316 8166 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618
SM
8167 abbrev_section, info_ptr,
8168 rcuh_kind::COMPILE);
ae038cb0
DJ
8169
8170 /* Save the compilation unit for later lookup. */
f1902523 8171 if (cu_header.unit_type != DW_UT_type)
976ca316 8172 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8173 else
8174 {
976ca316 8175 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8176 sig_type->signature = cu_header.signature;
8177 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8178 this_cu = &sig_type->per_cu;
8179 }
8180 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8181 this_cu->sect_off = sect_off;
f1902523 8182 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8183 this_cu->is_dwz = is_dwz;
8a0459fd 8184 this_cu->section = section;
ae038cb0 8185
976ca316 8186 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8187
8188 info_ptr = info_ptr + this_cu->length;
8189 }
36586728
TT
8190}
8191
8192/* Create a list of all compilation units in OBJFILE.
8193 This is only done for -readnow and building partial symtabs. */
8194
8195static void
976ca316 8196create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 8197{
976ca316
SM
8198 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8199 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8200 &per_objfile->per_bfd->abbrev, 0);
36586728 8201
976ca316 8202 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 8203 if (dwz != NULL)
976ca316 8204 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
c906108c
SS
8205}
8206
5734ee8b 8207/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8208 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8209 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8210 DW_AT_ranges). See the comments of add_partial_subprogram on how
8211 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8212
72bf9492
DJ
8213static void
8214scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8215 CORE_ADDR *highpc, int set_addrmap,
8216 struct dwarf2_cu *cu)
c906108c 8217{
72bf9492 8218 struct partial_die_info *pdi;
c906108c 8219
91c24f0a
DC
8220 /* Now, march along the PDI's, descending into ones which have
8221 interesting children but skipping the children of the other ones,
8222 until we reach the end of the compilation unit. */
c906108c 8223
72bf9492 8224 pdi = first_die;
91c24f0a 8225
72bf9492
DJ
8226 while (pdi != NULL)
8227 {
52356b79 8228 pdi->fixup (cu);
c906108c 8229
f55ee35c 8230 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8231 children, so we need to look at them. Ditto for anonymous
8232 enums. */
933c6fe4 8233
7d00ffec 8234 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8235 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8236 || pdi->tag == DW_TAG_imported_unit
8237 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8238 {
72bf9492 8239 switch (pdi->tag)
c906108c
SS
8240 {
8241 case DW_TAG_subprogram:
b1dc1806 8242 case DW_TAG_inlined_subroutine:
cdc07690 8243 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
8244 if (cu->language == language_cplus)
8245 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8246 set_addrmap, cu);
c906108c 8247 break;
72929c62 8248 case DW_TAG_constant:
c906108c
SS
8249 case DW_TAG_variable:
8250 case DW_TAG_typedef:
91c24f0a 8251 case DW_TAG_union_type:
317d2668
TV
8252 if (!pdi->is_declaration
8253 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8254 {
72bf9492 8255 add_partial_symbol (pdi, cu);
63d06c5c
DC
8256 }
8257 break;
c906108c 8258 case DW_TAG_class_type:
680b30c7 8259 case DW_TAG_interface_type:
c906108c 8260 case DW_TAG_structure_type:
72bf9492 8261 if (!pdi->is_declaration)
c906108c 8262 {
72bf9492 8263 add_partial_symbol (pdi, cu);
c906108c 8264 }
b7fee5a3
KS
8265 if ((cu->language == language_rust
8266 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8267 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8268 set_addrmap, cu);
c906108c 8269 break;
91c24f0a 8270 case DW_TAG_enumeration_type:
72bf9492
DJ
8271 if (!pdi->is_declaration)
8272 add_partial_enumeration (pdi, cu);
c906108c
SS
8273 break;
8274 case DW_TAG_base_type:
dda83cd7 8275 case DW_TAG_subrange_type:
c906108c 8276 /* File scope base type definitions are added to the partial
dda83cd7 8277 symbol table. */
72bf9492 8278 add_partial_symbol (pdi, cu);
c906108c 8279 break;
d9fa45fe 8280 case DW_TAG_namespace:
cdc07690 8281 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8282 break;
5d7cb8df 8283 case DW_TAG_module:
59c35742
AB
8284 if (!pdi->is_declaration)
8285 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8286 break;
95554aad
TT
8287 case DW_TAG_imported_unit:
8288 {
8289 struct dwarf2_per_cu_data *per_cu;
8290
f4dc4d17
DE
8291 /* For now we don't handle imported units in type units. */
8292 if (cu->per_cu->is_debug_types)
8293 {
8294 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8295 " supported in type units [in module %s]"),
5e22e966 8296 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8297 }
8298
e3b94546 8299 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8300 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8301
8302 /* Go read the partial unit, if needed. */
8303 if (per_cu->v.psymtab == NULL)
ab432490
SM
8304 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8305 cu->language);
95554aad 8306
ae640021 8307 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8308 }
8309 break;
74921315
KS
8310 case DW_TAG_imported_declaration:
8311 add_partial_symbol (pdi, cu);
8312 break;
c906108c
SS
8313 default:
8314 break;
8315 }
8316 }
8317
72bf9492
DJ
8318 /* If the die has a sibling, skip to the sibling. */
8319
8320 pdi = pdi->die_sibling;
8321 }
8322}
8323
8324/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8325
72bf9492 8326 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8327 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8328 Enumerators are an exception; they use the scope of their parent
8329 enumeration type, i.e. the name of the enumeration type is not
8330 prepended to the enumerator.
91c24f0a 8331
72bf9492
DJ
8332 There are two complexities. One is DW_AT_specification; in this
8333 case "parent" means the parent of the target of the specification,
8334 instead of the direct parent of the DIE. The other is compilers
8335 which do not emit DW_TAG_namespace; in this case we try to guess
8336 the fully qualified name of structure types from their members'
8337 linkage names. This must be done using the DIE's children rather
8338 than the children of any DW_AT_specification target. We only need
8339 to do this for structures at the top level, i.e. if the target of
8340 any DW_AT_specification (if any; otherwise the DIE itself) does not
8341 have a parent. */
8342
8343/* Compute the scope prefix associated with PDI's parent, in
8344 compilation unit CU. The result will be allocated on CU's
8345 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8346 field. NULL is returned if no prefix is necessary. */
15d034d0 8347static const char *
72bf9492
DJ
8348partial_die_parent_scope (struct partial_die_info *pdi,
8349 struct dwarf2_cu *cu)
8350{
15d034d0 8351 const char *grandparent_scope;
72bf9492 8352 struct partial_die_info *parent, *real_pdi;
91c24f0a 8353
72bf9492
DJ
8354 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8355 then this means the parent of the specification DIE. */
8356
8357 real_pdi = pdi;
72bf9492 8358 while (real_pdi->has_specification)
fb816e8b 8359 {
122cf0f2
AB
8360 auto res = find_partial_die (real_pdi->spec_offset,
8361 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8362 real_pdi = res.pdi;
8363 cu = res.cu;
8364 }
72bf9492
DJ
8365
8366 parent = real_pdi->die_parent;
8367 if (parent == NULL)
8368 return NULL;
8369
8370 if (parent->scope_set)
8371 return parent->scope;
8372
52356b79 8373 parent->fixup (cu);
72bf9492 8374
10b3939b 8375 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8376
acebe513
UW
8377 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8378 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8379 Work around this problem here. */
8380 if (cu->language == language_cplus
6e70227d 8381 && parent->tag == DW_TAG_namespace
7d00ffec 8382 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
8383 && grandparent_scope == NULL)
8384 {
8385 parent->scope = NULL;
8386 parent->scope_set = 1;
8387 return NULL;
8388 }
8389
0a4b0913 8390 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8391 if (pdi->tag == DW_TAG_enumerator)
8392 /* Enumerators should not get the name of the enumeration as a prefix. */
8393 parent->scope = grandparent_scope;
8394 else if (parent->tag == DW_TAG_namespace
f55ee35c 8395 || parent->tag == DW_TAG_module
72bf9492
DJ
8396 || parent->tag == DW_TAG_structure_type
8397 || parent->tag == DW_TAG_class_type
680b30c7 8398 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8399 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8400 || parent->tag == DW_TAG_enumeration_type
8401 || (cu->language == language_fortran
8402 && parent->tag == DW_TAG_subprogram
8403 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8404 {
8405 if (grandparent_scope == NULL)
7d00ffec 8406 parent->scope = parent->name (cu);
72bf9492 8407 else
3e43a32a
MS
8408 parent->scope = typename_concat (&cu->comp_unit_obstack,
8409 grandparent_scope,
7d00ffec 8410 parent->name (cu), 0, cu);
72bf9492 8411 }
72bf9492
DJ
8412 else
8413 {
8414 /* FIXME drow/2004-04-01: What should we be doing with
8415 function-local names? For partial symbols, we should probably be
8416 ignoring them. */
fa9c3fa0
TT
8417 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8418 dwarf_tag_name (parent->tag),
8419 sect_offset_str (pdi->sect_off));
72bf9492 8420 parent->scope = grandparent_scope;
c906108c
SS
8421 }
8422
72bf9492
DJ
8423 parent->scope_set = 1;
8424 return parent->scope;
8425}
8426
8427/* Return the fully scoped name associated with PDI, from compilation unit
8428 CU. The result will be allocated with malloc. */
4568ecf9 8429
43816ebc 8430static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8431partial_die_full_name (struct partial_die_info *pdi,
8432 struct dwarf2_cu *cu)
8433{
15d034d0 8434 const char *parent_scope;
72bf9492 8435
98bfdba5
PA
8436 /* If this is a template instantiation, we can not work out the
8437 template arguments from partial DIEs. So, unfortunately, we have
8438 to go through the full DIEs. At least any work we do building
8439 types here will be reused if full symbols are loaded later. */
8440 if (pdi->has_template_arguments)
8441 {
52356b79 8442 pdi->fixup (cu);
98bfdba5 8443
7d00ffec 8444 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8445 {
8446 struct die_info *die;
8447 struct attribute attr;
8448 struct dwarf2_cu *ref_cu = cu;
8449
b64f50a1 8450 /* DW_FORM_ref_addr is using section offset. */
b4069958 8451 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8452 attr.form = DW_FORM_ref_addr;
9c541725 8453 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8454 die = follow_die_ref (NULL, &attr, &ref_cu);
8455
43816ebc 8456 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8457 }
8458 }
8459
72bf9492
DJ
8460 parent_scope = partial_die_parent_scope (pdi, cu);
8461 if (parent_scope == NULL)
8462 return NULL;
8463 else
43816ebc 8464 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8465 pdi->name (cu),
8466 0, cu));
c906108c
SS
8467}
8468
8469static void
72bf9492 8470add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8471{
976ca316
SM
8472 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8473 struct objfile *objfile = per_objfile->objfile;
08feed99 8474 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8475 CORE_ADDR addr = 0;
15d034d0 8476 const char *actual_name = NULL;
e142c38c
DJ
8477 CORE_ADDR baseaddr;
8478
b3b3bada 8479 baseaddr = objfile->text_section_offset ();
c906108c 8480
43816ebc
TT
8481 gdb::unique_xmalloc_ptr<char> built_actual_name
8482 = partial_die_full_name (pdi, cu);
15d034d0 8483 if (built_actual_name != NULL)
43816ebc 8484 actual_name = built_actual_name.get ();
63d06c5c 8485
72bf9492 8486 if (actual_name == NULL)
7d00ffec 8487 actual_name = pdi->name (cu);
72bf9492 8488
76e288d1
TT
8489 partial_symbol psymbol;
8490 memset (&psymbol, 0, sizeof (psymbol));
8491 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8492 psymbol.ginfo.section = -1;
8493
8494 /* The code below indicates that the psymbol should be installed by
8495 setting this. */
8496 gdb::optional<psymbol_placement> where;
8497
c906108c
SS
8498 switch (pdi->tag)
8499 {
b1dc1806 8500 case DW_TAG_inlined_subroutine:
c906108c 8501 case DW_TAG_subprogram:
79748972
TT
8502 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8503 - baseaddr);
0a4b0913
AB
8504 if (pdi->is_external
8505 || cu->language == language_ada
8506 || (cu->language == language_fortran
8507 && pdi->die_parent != NULL
8508 && pdi->die_parent->tag == DW_TAG_subprogram))
8509 {
dda83cd7
SM
8510 /* Normally, only "external" DIEs are part of the global scope.
8511 But in Ada and Fortran, we want to be able to access nested
8512 procedures globally. So all Ada and Fortran subprograms are
8513 stored in the global scope. */
76e288d1 8514 where = psymbol_placement::GLOBAL;
c906108c
SS
8515 }
8516 else
76e288d1
TT
8517 where = psymbol_placement::STATIC;
8518
8519 psymbol.domain = VAR_DOMAIN;
8520 psymbol.aclass = LOC_BLOCK;
8521 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8522 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8523
8524 if (pdi->main_subprogram && actual_name != NULL)
8525 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8526 break;
72929c62 8527 case DW_TAG_constant:
76e288d1
TT
8528 psymbol.domain = VAR_DOMAIN;
8529 psymbol.aclass = LOC_STATIC;
8530 where = (pdi->is_external
8531 ? psymbol_placement::GLOBAL
8532 : psymbol_placement::STATIC);
72929c62 8533 break;
c906108c 8534 case DW_TAG_variable:
95554aad
TT
8535 if (pdi->d.locdesc)
8536 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8537
95554aad 8538 if (pdi->d.locdesc
caac4577 8539 && addr == 0
976ca316 8540 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8541 {
8542 /* A global or static variable may also have been stripped
8543 out by the linker if unused, in which case its address
8544 will be nullified; do not add such variables into partial
8545 symbol table then. */
8546 }
8547 else if (pdi->is_external)
c906108c
SS
8548 {
8549 /* Global Variable.
8550 Don't enter into the minimal symbol tables as there is
8551 a minimal symbol table entry from the ELF symbols already.
8552 Enter into partial symbol table if it has a location
8553 descriptor or a type.
8554 If the location descriptor is missing, new_symbol will create
8555 a LOC_UNRESOLVED symbol, the address of the variable will then
8556 be determined from the minimal symbol table whenever the variable
8557 is referenced.
8558 The address for the partial symbol table entry is not
8559 used by GDB, but it comes in handy for debugging partial symbol
8560 table building. */
8561
95554aad 8562 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8563 {
8564 psymbol.domain = VAR_DOMAIN;
8565 psymbol.aclass = LOC_STATIC;
8566 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8567 psymbol.ginfo.value.address = addr;
8568 where = psymbol_placement::GLOBAL;
8569 }
c906108c
SS
8570 }
8571 else
8572 {
ff908ebf
AW
8573 int has_loc = pdi->d.locdesc != NULL;
8574
8575 /* Static Variable. Skip symbols whose value we cannot know (those
8576 without location descriptors or constant values). */
8577 if (!has_loc && !pdi->has_const_value)
43816ebc 8578 return;
ff908ebf 8579
76e288d1
TT
8580 psymbol.domain = VAR_DOMAIN;
8581 psymbol.aclass = LOC_STATIC;
8582 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8583 if (has_loc)
8584 psymbol.ginfo.value.address = addr;
8585 where = psymbol_placement::STATIC;
c906108c
SS
8586 }
8587 break;
d8f62e84 8588 case DW_TAG_array_type:
c906108c
SS
8589 case DW_TAG_typedef:
8590 case DW_TAG_base_type:
a02abb62 8591 case DW_TAG_subrange_type:
76e288d1
TT
8592 psymbol.domain = VAR_DOMAIN;
8593 psymbol.aclass = LOC_TYPEDEF;
8594 where = psymbol_placement::STATIC;
c906108c 8595 break;
74921315 8596 case DW_TAG_imported_declaration:
72bf9492 8597 case DW_TAG_namespace:
76e288d1
TT
8598 psymbol.domain = VAR_DOMAIN;
8599 psymbol.aclass = LOC_TYPEDEF;
8600 where = psymbol_placement::GLOBAL;
72bf9492 8601 break;
530e8392 8602 case DW_TAG_module:
a5fd13a9 8603 /* With Fortran 77 there might be a "BLOCK DATA" module
dda83cd7
SM
8604 available without any name. If so, we skip the module as it
8605 doesn't bring any value. */
a5fd13a9 8606 if (actual_name != nullptr)
76e288d1
TT
8607 {
8608 psymbol.domain = MODULE_DOMAIN;
8609 psymbol.aclass = LOC_TYPEDEF;
8610 where = psymbol_placement::GLOBAL;
8611 }
530e8392 8612 break;
c906108c 8613 case DW_TAG_class_type:
680b30c7 8614 case DW_TAG_interface_type:
c906108c
SS
8615 case DW_TAG_structure_type:
8616 case DW_TAG_union_type:
8617 case DW_TAG_enumeration_type:
fa4028e9 8618 /* Skip external references. The DWARF standard says in the section
dda83cd7
SM
8619 about "Structure, Union, and Class Type Entries": "An incomplete
8620 structure, union or class type is represented by a structure,
8621 union or class entry that does not have a byte size attribute
8622 and that has a DW_AT_declaration attribute." */
fa4028e9 8623 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8624 return;
fa4028e9 8625
63d06c5c
DC
8626 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8627 static vs. global. */
76e288d1
TT
8628 psymbol.domain = STRUCT_DOMAIN;
8629 psymbol.aclass = LOC_TYPEDEF;
8630 where = (cu->language == language_cplus
8631 ? psymbol_placement::GLOBAL
8632 : psymbol_placement::STATIC);
c906108c
SS
8633 break;
8634 case DW_TAG_enumerator:
76e288d1
TT
8635 psymbol.domain = VAR_DOMAIN;
8636 psymbol.aclass = LOC_CONST;
8637 where = (cu->language == language_cplus
8638 ? psymbol_placement::GLOBAL
8639 : psymbol_placement::STATIC);
c906108c
SS
8640 break;
8641 default:
8642 break;
8643 }
76e288d1
TT
8644
8645 if (where.has_value ())
8646 {
f049a313
TT
8647 if (built_actual_name != nullptr)
8648 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8649 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8650 psymbol.ginfo.set_linkage_name (actual_name);
8651 else
8652 {
8653 psymbol.ginfo.set_demangled_name (actual_name,
8654 &objfile->objfile_obstack);
8655 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8656 }
932539d7 8657 cu->per_cu->v.psymtab->add_psymbol (psymbol, *where, objfile);
76e288d1 8658 }
c906108c
SS
8659}
8660
5c4e30ca
DC
8661/* Read a partial die corresponding to a namespace; also, add a symbol
8662 corresponding to that namespace to the symbol table. NAMESPACE is
8663 the name of the enclosing namespace. */
91c24f0a 8664
72bf9492
DJ
8665static void
8666add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8667 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8668 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8669{
72bf9492 8670 /* Add a symbol for the namespace. */
e7c27a73 8671
72bf9492 8672 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8673
8674 /* Now scan partial symbols in that namespace. */
8675
91c24f0a 8676 if (pdi->has_children)
cdc07690 8677 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8678}
8679
5d7cb8df
JK
8680/* Read a partial die corresponding to a Fortran module. */
8681
8682static void
8683add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8684 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8685{
530e8392
KB
8686 /* Add a symbol for the namespace. */
8687
8688 add_partial_symbol (pdi, cu);
8689
f55ee35c 8690 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8691
8692 if (pdi->has_children)
cdc07690 8693 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8694}
8695
b1dc1806
XR
8696/* Read a partial die corresponding to a subprogram or an inlined
8697 subprogram and create a partial symbol for that subprogram.
8698 When the CU language allows it, this routine also defines a partial
8699 symbol for each nested subprogram that this subprogram contains.
8700 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8701 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8702
cdc07690
YQ
8703 PDI may also be a lexical block, in which case we simply search
8704 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8705 Again, this is only performed when the CU language allows this
8706 type of definitions. */
8707
8708static void
8709add_partial_subprogram (struct partial_die_info *pdi,
8710 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8711 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8712{
b1dc1806 8713 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8714 {
8715 if (pdi->has_pc_info)
dda83cd7
SM
8716 {
8717 if (pdi->lowpc < *lowpc)
8718 *lowpc = pdi->lowpc;
8719 if (pdi->highpc > *highpc)
8720 *highpc = pdi->highpc;
cdc07690 8721 if (set_addrmap)
5734ee8b 8722 {
5e22e966 8723 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8724 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8725 CORE_ADDR baseaddr;
b926417a
TT
8726 CORE_ADDR this_highpc;
8727 CORE_ADDR this_lowpc;
5734ee8b 8728
b3b3bada 8729 baseaddr = objfile->text_section_offset ();
b926417a
TT
8730 this_lowpc
8731 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8732 pdi->lowpc + baseaddr)
8733 - baseaddr);
8734 this_highpc
8735 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8736 pdi->highpc + baseaddr)
8737 - baseaddr);
d320c2b5 8738 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8739 this_lowpc, this_highpc - 1,
9291a0cd 8740 cu->per_cu->v.psymtab);
5734ee8b 8741 }
dda83cd7 8742 }
481860b3
GB
8743
8744 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8745 {
dda83cd7 8746 if (!pdi->is_declaration)
e8d05480
JB
8747 /* Ignore subprogram DIEs that do not have a name, they are
8748 illegal. Do not emit a complaint at this point, we will
8749 do so when we convert this psymtab into a symtab. */
7d00ffec 8750 if (pdi->name (cu))
e8d05480 8751 add_partial_symbol (pdi, cu);
dda83cd7 8752 }
bc30ff58 8753 }
6e70227d 8754
bc30ff58
JB
8755 if (! pdi->has_children)
8756 return;
8757
0a4b0913 8758 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8759 {
8760 pdi = pdi->die_child;
8761 while (pdi != NULL)
8762 {
52356b79 8763 pdi->fixup (cu);
bc30ff58 8764 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8765 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8766 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8767 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8768 pdi = pdi->die_sibling;
8769 }
8770 }
8771}
8772
91c24f0a
DC
8773/* Read a partial die corresponding to an enumeration type. */
8774
72bf9492
DJ
8775static void
8776add_partial_enumeration (struct partial_die_info *enum_pdi,
8777 struct dwarf2_cu *cu)
91c24f0a 8778{
72bf9492 8779 struct partial_die_info *pdi;
91c24f0a 8780
7d00ffec 8781 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8782 add_partial_symbol (enum_pdi, cu);
8783
8784 pdi = enum_pdi->die_child;
8785 while (pdi)
91c24f0a 8786 {
7d00ffec 8787 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8788 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8789 else
72bf9492
DJ
8790 add_partial_symbol (pdi, cu);
8791 pdi = pdi->die_sibling;
91c24f0a 8792 }
91c24f0a
DC
8793}
8794
6caca83c
CC
8795/* Return the initial uleb128 in the die at INFO_PTR. */
8796
8797static unsigned int
d521ce57 8798peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8799{
8800 unsigned int bytes_read;
8801
8802 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8803}
8804
685af9cd
TT
8805/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8806 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8807
4bb7a0a7
DJ
8808 Return the corresponding abbrev, or NULL if the number is zero (indicating
8809 an empty DIE). In either case *BYTES_READ will be set to the length of
8810 the initial number. */
8811
8812static struct abbrev_info *
685af9cd
TT
8813peek_die_abbrev (const die_reader_specs &reader,
8814 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8815{
685af9cd 8816 dwarf2_cu *cu = reader.cu;
5e22e966 8817 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8818 unsigned int abbrev_number
8819 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8820
8821 if (abbrev_number == 0)
8822 return NULL;
8823
685af9cd 8824 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8825 if (!abbrev)
8826 {
422b9917 8827 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8828 " at offset %s [in module %s]"),
422b9917 8829 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8830 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8831 }
8832
8833 return abbrev;
8834}
8835
93311388
DE
8836/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8837 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8838 DIE. Any children of the skipped DIEs will also be skipped. */
8839
d521ce57
TT
8840static const gdb_byte *
8841skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8842{
4bb7a0a7
DJ
8843 while (1)
8844 {
685af9cd
TT
8845 unsigned int bytes_read;
8846 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8847
4bb7a0a7
DJ
8848 if (abbrev == NULL)
8849 return info_ptr + bytes_read;
8850 else
dee91e82 8851 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8852 }
8853}
8854
93311388
DE
8855/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8856 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8857 abbrev corresponding to that skipped uleb128 should be passed in
8858 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8859 children. */
8860
d521ce57
TT
8861static const gdb_byte *
8862skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8863 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8864{
8865 unsigned int bytes_read;
8866 struct attribute attr;
dee91e82
DE
8867 bfd *abfd = reader->abfd;
8868 struct dwarf2_cu *cu = reader->cu;
d521ce57 8869 const gdb_byte *buffer = reader->buffer;
f664829e 8870 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8871 unsigned int form, i;
8872
8873 for (i = 0; i < abbrev->num_attrs; i++)
8874 {
8875 /* The only abbrev we care about is DW_AT_sibling. */
8876 if (abbrev->attrs[i].name == DW_AT_sibling)
8877 {
7a5f294d 8878 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8879 if (attr.form == DW_FORM_ref_addr)
b98664d3 8880 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8881 else
b9502d3f 8882 {
0826b30a 8883 sect_offset off = attr.get_ref_die_offset ();
9c541725 8884 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8885
8886 if (sibling_ptr < info_ptr)
b98664d3 8887 complaint (_("DW_AT_sibling points backwards"));
22869d73 8888 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8889 reader->die_section->overflow_complaint ();
b9502d3f
WN
8890 else
8891 return sibling_ptr;
8892 }
4bb7a0a7
DJ
8893 }
8894
8895 /* If it isn't DW_AT_sibling, skip this attribute. */
8896 form = abbrev->attrs[i].form;
8897 skip_attribute:
8898 switch (form)
8899 {
4bb7a0a7 8900 case DW_FORM_ref_addr:
ae411497
TT
8901 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8902 and later it is offset sized. */
8903 if (cu->header.version == 2)
8904 info_ptr += cu->header.addr_size;
8905 else
8906 info_ptr += cu->header.offset_size;
8907 break;
36586728
TT
8908 case DW_FORM_GNU_ref_alt:
8909 info_ptr += cu->header.offset_size;
8910 break;
ae411497 8911 case DW_FORM_addr:
4bb7a0a7
DJ
8912 info_ptr += cu->header.addr_size;
8913 break;
8914 case DW_FORM_data1:
8915 case DW_FORM_ref1:
8916 case DW_FORM_flag:
8fe0f950 8917 case DW_FORM_strx1:
4bb7a0a7
DJ
8918 info_ptr += 1;
8919 break;
2dc7f7b3 8920 case DW_FORM_flag_present:
43988095 8921 case DW_FORM_implicit_const:
2dc7f7b3 8922 break;
4bb7a0a7
DJ
8923 case DW_FORM_data2:
8924 case DW_FORM_ref2:
8fe0f950 8925 case DW_FORM_strx2:
4bb7a0a7
DJ
8926 info_ptr += 2;
8927 break;
8fe0f950
AT
8928 case DW_FORM_strx3:
8929 info_ptr += 3;
8930 break;
4bb7a0a7
DJ
8931 case DW_FORM_data4:
8932 case DW_FORM_ref4:
8fe0f950 8933 case DW_FORM_strx4:
4bb7a0a7
DJ
8934 info_ptr += 4;
8935 break;
8936 case DW_FORM_data8:
8937 case DW_FORM_ref8:
55f1336d 8938 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8939 info_ptr += 8;
8940 break;
0224619f
JK
8941 case DW_FORM_data16:
8942 info_ptr += 16;
8943 break;
4bb7a0a7 8944 case DW_FORM_string:
9b1c24c8 8945 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8946 info_ptr += bytes_read;
8947 break;
2dc7f7b3 8948 case DW_FORM_sec_offset:
4bb7a0a7 8949 case DW_FORM_strp:
36586728 8950 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8951 info_ptr += cu->header.offset_size;
8952 break;
2dc7f7b3 8953 case DW_FORM_exprloc:
4bb7a0a7
DJ
8954 case DW_FORM_block:
8955 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8956 info_ptr += bytes_read;
8957 break;
8958 case DW_FORM_block1:
8959 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8960 break;
8961 case DW_FORM_block2:
8962 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8963 break;
8964 case DW_FORM_block4:
8965 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8966 break;
336d760d 8967 case DW_FORM_addrx:
cf532bd1 8968 case DW_FORM_strx:
4bb7a0a7
DJ
8969 case DW_FORM_sdata:
8970 case DW_FORM_udata:
8971 case DW_FORM_ref_udata:
3019eac3
DE
8972 case DW_FORM_GNU_addr_index:
8973 case DW_FORM_GNU_str_index:
18a8505e 8974 case DW_FORM_rnglistx:
41144253 8975 case DW_FORM_loclistx:
d521ce57 8976 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8977 break;
8978 case DW_FORM_indirect:
8979 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8980 info_ptr += bytes_read;
8981 /* We need to continue parsing from here, so just go back to
8982 the top. */
8983 goto skip_attribute;
8984
8985 default:
3e43a32a
MS
8986 error (_("Dwarf Error: Cannot handle %s "
8987 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8988 dwarf_form_name (form),
8989 bfd_get_filename (abfd));
8990 }
8991 }
8992
8993 if (abbrev->has_children)
dee91e82 8994 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8995 else
8996 return info_ptr;
8997}
8998
93311388 8999/* Locate ORIG_PDI's sibling.
dee91e82 9000 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9001
d521ce57 9002static const gdb_byte *
dee91e82
DE
9003locate_pdi_sibling (const struct die_reader_specs *reader,
9004 struct partial_die_info *orig_pdi,
d521ce57 9005 const gdb_byte *info_ptr)
91c24f0a
DC
9006{
9007 /* Do we know the sibling already? */
72bf9492 9008
91c24f0a
DC
9009 if (orig_pdi->sibling)
9010 return orig_pdi->sibling;
9011
9012 /* Are there any children to deal with? */
9013
9014 if (!orig_pdi->has_children)
9015 return info_ptr;
9016
4bb7a0a7 9017 /* Skip the children the long way. */
91c24f0a 9018
dee91e82 9019 return skip_children (reader, info_ptr);
91c24f0a
DC
9020}
9021
257e7a09 9022/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9023 not NULL. */
c906108c 9024
891813be
TT
9025void
9026dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 9027{
976ca316 9028 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 9029
976ca316 9030 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 9031
077cbab2
TT
9032 /* If this psymtab is constructed from a debug-only objfile, the
9033 has_section_at_zero flag will not necessarily be correct. We
9034 can get the correct value for this flag by looking at the data
9035 associated with the (presumably stripped) associated objfile. */
9036 if (objfile->separate_debug_objfile_backlink)
c906108c 9037 {
976ca316 9038 dwarf2_per_objfile *per_objfile_backlink
077cbab2 9039 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 9040
976ca316
SM
9041 per_objfile->per_bfd->has_section_at_zero
9042 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 9043 }
98bfdba5 9044
8566b89b 9045 expand_psymtab (objfile);
95554aad 9046
976ca316 9047 process_cu_includes (per_objfile);
c906108c 9048}
9cdd5dbd
DE
9049\f
9050/* Reading in full CUs. */
c906108c 9051
10b3939b
DJ
9052/* Add PER_CU to the queue. */
9053
9054static void
120ce1b5
SM
9055queue_comp_unit (dwarf2_per_cu_data *per_cu,
9056 dwarf2_per_objfile *per_objfile,
95554aad 9057 enum language pretend_language)
10b3939b 9058{
10b3939b 9059 per_cu->queued = 1;
120ce1b5 9060 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
9061}
9062
89e63ee4
DE
9063/* If PER_CU is not yet queued, add it to the queue.
9064 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9065 dependency.
0907af0c 9066 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9067 meaning either PER_CU is already queued or it is already loaded.
9068
9069 N.B. There is an invariant here that if a CU is queued then it is loaded.
9070 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9071
9072static int
89e63ee4 9073maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
9074 dwarf2_per_cu_data *per_cu,
9075 dwarf2_per_objfile *per_objfile,
0907af0c
DE
9076 enum language pretend_language)
9077{
9078 /* We may arrive here during partial symbol reading, if we need full
9079 DIEs to process an unusual case (e.g. template arguments). Do
9080 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 9081 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 9082 {
7188ed02
SM
9083 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9084
9085 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
9086 return 1;
9087 return 0;
9088 }
9089
9090 /* Mark the dependence relation so that we don't flush PER_CU
9091 too early. */
89e63ee4
DE
9092 if (dependent_cu != NULL)
9093 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9094
9095 /* If it's already on the queue, we have nothing to do. */
9096 if (per_cu->queued)
9097 return 0;
9098
9099 /* If the compilation unit is already loaded, just mark it as
9100 used. */
7188ed02
SM
9101 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9102 if (cu != nullptr)
0907af0c 9103 {
7188ed02 9104 cu->last_used = 0;
0907af0c
DE
9105 return 0;
9106 }
9107
9108 /* Add it to the queue. */
120ce1b5 9109 queue_comp_unit (per_cu, per_objfile, pretend_language);
0907af0c
DE
9110
9111 return 1;
9112}
9113
10b3939b
DJ
9114/* Process the queue. */
9115
9116static void
976ca316 9117process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 9118{
6f738b01
SM
9119 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9120 objfile_name (per_objfile->objfile));
45cfd468 9121
03dd20cc
DJ
9122 /* The queue starts out with one item, but following a DIE reference
9123 may load a new CU, adding it to the end of the queue. */
976ca316 9124 while (!per_objfile->per_bfd->queue.empty ())
10b3939b 9125 {
976ca316 9126 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
7188ed02 9127 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 9128
976ca316 9129 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 9130 {
976ca316 9131 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 9132
7188ed02
SM
9133 /* Skip dummy CUs. */
9134 if (cu != nullptr)
73be47f5 9135 {
7188ed02
SM
9136 unsigned int debug_print_threshold;
9137 char buf[100];
9138
9139 if (per_cu->is_debug_types)
9140 {
9141 struct signatured_type *sig_type =
9142 (struct signatured_type *) per_cu;
9143
9144 sprintf (buf, "TU %s at offset %s",
9145 hex_string (sig_type->signature),
9146 sect_offset_str (per_cu->sect_off));
9147 /* There can be 100s of TUs.
9148 Only print them in verbose mode. */
9149 debug_print_threshold = 2;
9150 }
9151 else
9152 {
9153 sprintf (buf, "CU at offset %s",
9154 sect_offset_str (per_cu->sect_off));
9155 debug_print_threshold = 1;
9156 }
247f5c4f 9157
7188ed02 9158 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 9159 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
f4dc4d17 9160
7188ed02
SM
9161 if (per_cu->is_debug_types)
9162 process_full_type_unit (cu, item.pretend_language);
9163 else
9164 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 9165
7188ed02 9166 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 9167 dwarf_read_debug_printf ("Done expanding %s", buf);
7188ed02 9168 }
f4dc4d17 9169 }
10b3939b 9170
7188ed02 9171 per_cu->queued = 0;
976ca316 9172 per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9173 }
9174
6f738b01
SM
9175 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9176 objfile_name (per_objfile->objfile));
10b3939b
DJ
9177}
9178
10b3939b
DJ
9179/* Read in full symbols for PST, and anything it depends on. */
9180
8566b89b
TT
9181void
9182dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9183{
af758d11 9184 gdb_assert (!readin_p (objfile));
95554aad 9185
17ee85fc
TT
9186 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9187 free_cached_comp_units freer (per_objfile);
48993951 9188 expand_dependencies (objfile);
aaa75496 9189
97a1449a 9190 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9191 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9192}
9193
af758d11
SM
9194/* See psympriv.h. */
9195
9196bool
9197dwarf2_psymtab::readin_p (struct objfile *objfile) const
9198{
9199 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9200 return per_objfile->symtab_set_p (per_cu_data);
9201}
9202
9203/* See psympriv.h. */
9204
9205compunit_symtab *
9206dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9207{
9208 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9209 return per_objfile->get_symtab (per_cu_data);
9210}
9211
dee91e82
DE
9212/* Trivial hash function for die_info: the hash value of a DIE
9213 is its offset in .debug_info for this objfile. */
10b3939b 9214
dee91e82
DE
9215static hashval_t
9216die_hash (const void *item)
10b3939b 9217{
9a3c8263 9218 const struct die_info *die = (const struct die_info *) item;
6502dd73 9219
9c541725 9220 return to_underlying (die->sect_off);
dee91e82 9221}
63d06c5c 9222
dee91e82
DE
9223/* Trivial comparison function for die_info structures: two DIEs
9224 are equal if they have the same offset. */
98bfdba5 9225
dee91e82
DE
9226static int
9227die_eq (const void *item_lhs, const void *item_rhs)
9228{
9a3c8263
SM
9229 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9230 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9231
9c541725 9232 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9233}
c906108c 9234
4a636814
SM
9235/* Load the DIEs associated with PER_CU into memory.
9236
9237 In some cases, the caller, while reading partial symbols, will need to load
9238 the full symbols for the CU for some reason. It will already have a
9239 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9240 rather than creating a new one. */
c906108c 9241
dee91e82 9242static void
ab432490
SM
9243load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9244 dwarf2_per_objfile *per_objfile,
4a636814 9245 dwarf2_cu *existing_cu,
c0ab21c2
TT
9246 bool skip_partial,
9247 enum language pretend_language)
dee91e82 9248{
c0ab21c2
TT
9249 gdb_assert (! this_cu->is_debug_types);
9250
7188ed02 9251 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
9252 if (reader.dummy_p)
9253 return;
9254
9255 struct dwarf2_cu *cu = reader.cu;
9256 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9257
dee91e82
DE
9258 gdb_assert (cu->die_hash == NULL);
9259 cu->die_hash =
9260 htab_create_alloc_ex (cu->header.length / 12,
9261 die_hash,
9262 die_eq,
9263 NULL,
9264 &cu->comp_unit_obstack,
9265 hashtab_obstack_allocate,
9266 dummy_obstack_deallocate);
e142c38c 9267
3e225074 9268 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9269 reader.comp_unit_die->child
9270 = read_die_and_siblings (&reader, reader.info_ptr,
9271 &info_ptr, reader.comp_unit_die);
9272 cu->dies = reader.comp_unit_die;
dee91e82 9273 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9274
9275 /* We try not to read any attributes in this function, because not
9cdd5dbd 9276 all CUs needed for references have been loaded yet, and symbol
10b3939b 9277 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9278 or we won't be able to build types correctly.
9279 Similarly, if we do not read the producer, we can not apply
9280 producer-specific interpretation. */
c0ab21c2 9281 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9282
9283 reader.keep ();
10b3939b
DJ
9284}
9285
3da10d80
KS
9286/* Add a DIE to the delayed physname list. */
9287
9288static void
9289add_to_method_list (struct type *type, int fnfield_index, int index,
9290 const char *name, struct die_info *die,
9291 struct dwarf2_cu *cu)
9292{
9293 struct delayed_method_info mi;
9294 mi.type = type;
9295 mi.fnfield_index = fnfield_index;
9296 mi.index = index;
9297 mi.name = name;
9298 mi.die = die;
c89b44cd 9299 cu->method_list.push_back (mi);
3da10d80
KS
9300}
9301
3693fdb3
PA
9302/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9303 "const" / "volatile". If so, decrements LEN by the length of the
9304 modifier and return true. Otherwise return false. */
9305
9306template<size_t N>
9307static bool
9308check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9309{
9310 size_t mod_len = sizeof (mod) - 1;
9311 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9312 {
9313 len -= mod_len;
9314 return true;
9315 }
9316 return false;
9317}
9318
3da10d80
KS
9319/* Compute the physnames of any methods on the CU's method list.
9320
9321 The computation of method physnames is delayed in order to avoid the
9322 (bad) condition that one of the method's formal parameters is of an as yet
9323 incomplete type. */
9324
9325static void
9326compute_delayed_physnames (struct dwarf2_cu *cu)
9327{
3693fdb3 9328 /* Only C++ delays computing physnames. */
c89b44cd 9329 if (cu->method_list.empty ())
3693fdb3
PA
9330 return;
9331 gdb_assert (cu->language == language_cplus);
9332
52941706 9333 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9334 {
1d06ead6 9335 const char *physname;
3da10d80 9336 struct fn_fieldlist *fn_flp
c89b44cd
TT
9337 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9338 physname = dwarf2_physname (mi.name, mi.die, cu);
9339 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9340 = physname ? physname : "";
3693fdb3
PA
9341
9342 /* Since there's no tag to indicate whether a method is a
9343 const/volatile overload, extract that information out of the
9344 demangled name. */
9345 if (physname != NULL)
9346 {
9347 size_t len = strlen (physname);
9348
9349 while (1)
9350 {
9351 if (physname[len] == ')') /* shortcut */
9352 break;
9353 else if (check_modifier (physname, len, " const"))
c89b44cd 9354 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9355 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9356 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9357 else
9358 break;
9359 }
9360 }
3da10d80 9361 }
c89b44cd
TT
9362
9363 /* The list is no longer needed. */
9364 cu->method_list.clear ();
3da10d80
KS
9365}
9366
a766d390
DE
9367/* Go objects should be embedded in a DW_TAG_module DIE,
9368 and it's not clear if/how imported objects will appear.
9369 To keep Go support simple until that's worked out,
9370 go back through what we've read and create something usable.
9371 We could do this while processing each DIE, and feels kinda cleaner,
9372 but that way is more invasive.
9373 This is to, for example, allow the user to type "p var" or "b main"
9374 without having to specify the package name, and allow lookups
9375 of module.object to work in contexts that use the expression
9376 parser. */
9377
9378static void
9379fixup_go_packaging (struct dwarf2_cu *cu)
9380{
421d1616 9381 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9382 struct pending *list;
9383 int i;
9384
c24bdb02 9385 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9386 list != NULL;
9387 list = list->next)
a766d390
DE
9388 {
9389 for (i = 0; i < list->nsyms; ++i)
9390 {
9391 struct symbol *sym = list->symbol[i];
9392
c1b5c1eb 9393 if (sym->language () == language_go
a766d390
DE
9394 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9395 {
421d1616
TT
9396 gdb::unique_xmalloc_ptr<char> this_package_name
9397 (go_symbol_package_name (sym));
a766d390
DE
9398
9399 if (this_package_name == NULL)
9400 continue;
9401 if (package_name == NULL)
421d1616 9402 package_name = std::move (this_package_name);
a766d390
DE
9403 else
9404 {
5e22e966 9405 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9406 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9407 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9408 (symbol_symtab (sym) != NULL
9409 ? symtab_to_filename_for_display
9410 (symbol_symtab (sym))
e3b94546 9411 : objfile_name (objfile)),
421d1616 9412 this_package_name.get (), package_name.get ());
a766d390
DE
9413 }
9414 }
9415 }
9416 }
9417
9418 if (package_name != NULL)
9419 {
5e22e966 9420 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9421 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9422 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9423 saved_package_name);
a766d390
DE
9424 struct symbol *sym;
9425
8c14c3a3 9426 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9427 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9428 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9429 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9430 e.g., "main" finds the "main" module and not C's main(). */
9431 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9432 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9433 SYMBOL_TYPE (sym) = type;
9434
c24bdb02 9435 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9436 }
9437}
9438
c9317f21
TT
9439/* Allocate a fully-qualified name consisting of the two parts on the
9440 obstack. */
9441
9442static const char *
9443rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9444{
9445 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9446}
9447
9c6a1327
TT
9448/* A helper that allocates a variant part to attach to a Rust enum
9449 type. OBSTACK is where the results should be allocated. TYPE is
9450 the type we're processing. DISCRIMINANT_INDEX is the index of the
57d02173
TT
9451 discriminant. It must be the index of one of the fields of TYPE,
9452 or -1 to mean there is no discriminant (univariant enum).
9c6a1327
TT
9453 DEFAULT_INDEX is the index of the default field; or -1 if there is
9454 no default. RANGES is indexed by "effective" field number (the
9455 field index, but omitting the discriminant and default fields) and
9456 must hold the discriminant values used by the variants. Note that
9457 RANGES must have a lifetime at least as long as OBSTACK -- either
9458 already allocated on it, or static. */
c9317f21 9459
9c6a1327
TT
9460static void
9461alloc_rust_variant (struct obstack *obstack, struct type *type,
9462 int discriminant_index, int default_index,
9463 gdb::array_view<discriminant_range> ranges)
9464{
57d02173
TT
9465 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9466 gdb_assert (discriminant_index == -1
9467 || (discriminant_index >= 0
9468 && discriminant_index < type->num_fields ()));
c9317f21 9469 gdb_assert (default_index == -1
1f704f76 9470 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9471
9c6a1327 9472 /* We have one variant for each non-discriminant field. */
57d02173
TT
9473 int n_variants = type->num_fields ();
9474 if (discriminant_index != -1)
9475 --n_variants;
c9317f21 9476
9c6a1327
TT
9477 variant *variants = new (obstack) variant[n_variants];
9478 int var_idx = 0;
9479 int range_idx = 0;
1f704f76 9480 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9481 {
9482 if (i == discriminant_index)
9483 continue;
c9317f21 9484
9c6a1327
TT
9485 variants[var_idx].first_field = i;
9486 variants[var_idx].last_field = i + 1;
9487
9488 /* The default field does not need a range, but other fields do.
9489 We skipped the discriminant above. */
9490 if (i != default_index)
9491 {
9492 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9493 ++range_idx;
9494 }
c9317f21 9495
9c6a1327
TT
9496 ++var_idx;
9497 }
9498
9499 gdb_assert (range_idx == ranges.size ());
9500 gdb_assert (var_idx == n_variants);
9501
9502 variant_part *part = new (obstack) variant_part;
9503 part->discriminant_index = discriminant_index;
57d02173
TT
9504 /* If there is no discriminant, then whether it is signed is of no
9505 consequence. */
9506 part->is_unsigned
9507 = (discriminant_index == -1
9508 ? false
c6d940a9 9509 : type->field (discriminant_index).type ()->is_unsigned ());
9c6a1327
TT
9510 part->variants = gdb::array_view<variant> (variants, n_variants);
9511
9512 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9513 gdb::array_view<variant_part> *prop_value
9514 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9515
9c6a1327 9516 struct dynamic_prop prop;
8c2e4e06 9517 prop.set_variant_parts (prop_value);
9c6a1327 9518
5c54719c 9519 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9520}
9521
9522/* Some versions of rustc emitted enums in an unusual way.
9523
9524 Ordinary enums were emitted as unions. The first element of each
9525 structure in the union was named "RUST$ENUM$DISR". This element
9526 held the discriminant.
9527
9528 These versions of Rust also implemented the "non-zero"
9529 optimization. When the enum had two values, and one is empty and
9530 the other holds a pointer that cannot be zero, the pointer is used
9531 as the discriminant, with a zero value meaning the empty variant.
9532 Here, the union's first member is of the form
9533 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9534 where the fieldnos are the indices of the fields that should be
9535 traversed in order to find the field (which may be several fields deep)
9536 and the variantname is the name of the variant of the case when the
9537 field is zero.
9538
9539 This function recognizes whether TYPE is of one of these forms,
9540 and, if so, smashes it to be a variant type. */
9541
9542static void
9543quirk_rust_enum (struct type *type, struct objfile *objfile)
9544{
78134374 9545 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9546
9547 /* We don't need to deal with empty enums. */
1f704f76 9548 if (type->num_fields () == 0)
c9317f21
TT
9549 return;
9550
9551#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9552 if (type->num_fields () == 1
c9317f21
TT
9553 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9554 {
9555 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9556
9557 /* Decode the field name to find the offset of the
9558 discriminant. */
9559 ULONGEST bit_offset = 0;
940da03e 9560 struct type *field_type = type->field (0).type ();
c9317f21
TT
9561 while (name[0] >= '0' && name[0] <= '9')
9562 {
9563 char *tail;
9564 unsigned long index = strtoul (name, &tail, 10);
9565 name = tail;
9566 if (*name != '$'
1f704f76 9567 || index >= field_type->num_fields ()
c9317f21
TT
9568 || (TYPE_FIELD_LOC_KIND (field_type, index)
9569 != FIELD_LOC_KIND_BITPOS))
9570 {
b98664d3 9571 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9572 "[in module %s]"),
9573 TYPE_FIELD_NAME (type, 0),
9574 objfile_name (objfile));
9575 return;
9576 }
9577 ++name;
9578
9579 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 9580 field_type = field_type->field (index).type ();
c9317f21
TT
9581 }
9582
9c6a1327
TT
9583 /* Smash this type to be a structure type. We have to do this
9584 because the type has already been recorded. */
67607e24 9585 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9586 type->set_num_fields (3);
9c6a1327 9587 /* Save the field we care about. */
ceacbf6e 9588 struct field saved_field = type->field (0);
3cabb6b0
SM
9589 type->set_fields
9590 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9591
9c6a1327 9592 /* Put the discriminant at index 0. */
5d14b6e5 9593 type->field (0).set_type (field_type);
9c6a1327
TT
9594 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9595 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9596 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9597
9598 /* The order of fields doesn't really matter, so put the real
9599 field at index 1 and the data-less field at index 2. */
ceacbf6e 9600 type->field (1) = saved_field;
9c6a1327 9601 TYPE_FIELD_NAME (type, 1)
940da03e
SM
9602 = rust_last_path_segment (type->field (1).type ()->name ());
9603 type->field (1).type ()->set_name
7d93a1e0 9604 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9605 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9606
9607 const char *dataless_name
7d93a1e0 9608 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9609 name);
9610 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9611 dataless_name);
5d14b6e5 9612 type->field (2).set_type (dataless_type);
c9317f21
TT
9613 /* NAME points into the original discriminant name, which
9614 already has the correct lifetime. */
9c6a1327 9615 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9616 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9617
9c6a1327
TT
9618 /* Indicate that this is a variant type. */
9619 static discriminant_range ranges[1] = { { 0, 0 } };
9620 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9621 }
77c2dba3
TT
9622 /* A union with a single anonymous field is probably an old-style
9623 univariant enum. */
1f704f76 9624 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9625 {
c9317f21
TT
9626 /* Smash this type to be a structure type. We have to do this
9627 because the type has already been recorded. */
67607e24 9628 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9629
940da03e 9630 struct type *field_type = type->field (0).type ();
c9317f21 9631 const char *variant_name
7d93a1e0 9632 = rust_last_path_segment (field_type->name ());
9c6a1327 9633 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9634 field_type->set_name
9635 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9636 type->name (), variant_name));
57d02173
TT
9637
9638 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
c9317f21
TT
9639 }
9640 else
9641 {
9642 struct type *disr_type = nullptr;
1f704f76 9643 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9644 {
940da03e 9645 disr_type = type->field (i).type ();
c9317f21 9646
78134374 9647 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9648 {
9649 /* All fields of a true enum will be structs. */
9650 return;
9651 }
1f704f76 9652 else if (disr_type->num_fields () == 0)
c9317f21
TT
9653 {
9654 /* Could be data-less variant, so keep going. */
a037790e 9655 disr_type = nullptr;
c9317f21
TT
9656 }
9657 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9658 "RUST$ENUM$DISR") != 0)
9659 {
9660 /* Not a Rust enum. */
9661 return;
9662 }
9663 else
9664 {
9665 /* Found one. */
9666 break;
9667 }
9668 }
9669
9670 /* If we got here without a discriminant, then it's probably
9671 just a union. */
9672 if (disr_type == nullptr)
9673 return;
9674
9675 /* Smash this type to be a structure type. We have to do this
9676 because the type has already been recorded. */
67607e24 9677 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9678
9c6a1327 9679 /* Make space for the discriminant field. */
ceacbf6e 9680 struct field *disr_field = &disr_type->field (0);
9c6a1327 9681 field *new_fields
1f704f76 9682 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9683 * sizeof (struct field)));
80fc5e77 9684 memcpy (new_fields + 1, type->fields (),
1f704f76 9685 type->num_fields () * sizeof (struct field));
3cabb6b0 9686 type->set_fields (new_fields);
1f704f76 9687 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9688
9689 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9690 type->field (0) = *disr_field;
9c6a1327
TT
9691 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9692 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9693
9694 /* We need a way to find the correct discriminant given a
9695 variant name. For convenience we build a map here. */
b6cdac4b 9696 struct type *enum_type = disr_field->type ();
c9317f21 9697 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9698 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9699 {
9700 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9701 {
9702 const char *name
9703 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9704 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9705 }
9706 }
9707
1f704f76 9708 int n_fields = type->num_fields ();
9c6a1327
TT
9709 /* We don't need a range entry for the discriminant, but we do
9710 need one for every other field, as there is no default
9711 variant. */
9712 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9713 discriminant_range,
9714 n_fields - 1);
c9317f21
TT
9715 /* Skip the discriminant here. */
9716 for (int i = 1; i < n_fields; ++i)
9717 {
9718 /* Find the final word in the name of this variant's type.
9719 That name can be used to look up the correct
9720 discriminant. */
9721 const char *variant_name
940da03e 9722 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9723
9724 auto iter = discriminant_map.find (variant_name);
9725 if (iter != discriminant_map.end ())
9c6a1327 9726 {
57d02173
TT
9727 ranges[i - 1].low = iter->second;
9728 ranges[i - 1].high = iter->second;
9c6a1327 9729 }
c9317f21 9730
57d02173
TT
9731 /* In Rust, each element should have the size of the
9732 enclosing enum. */
9733 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9734
bedda9ac 9735 /* Remove the discriminant field, if it exists. */
940da03e 9736 struct type *sub_type = type->field (i).type ();
1f704f76 9737 if (sub_type->num_fields () > 0)
bedda9ac 9738 {
5e33d5f4 9739 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9740 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9741 }
9c6a1327 9742 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9743 sub_type->set_name
9744 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9745 type->name (), variant_name));
c9317f21 9746 }
9c6a1327
TT
9747
9748 /* Indicate that this is a variant type. */
a1520ad8 9749 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9750 gdb::array_view<discriminant_range> (ranges,
9751 n_fields - 1));
c9317f21
TT
9752 }
9753}
9754
9755/* Rewrite some Rust unions to be structures with variants parts. */
9756
9757static void
9758rust_union_quirks (struct dwarf2_cu *cu)
9759{
9760 gdb_assert (cu->language == language_rust);
52941706 9761 for (type *type_ : cu->rust_unions)
5e22e966 9762 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9763 /* We don't need this any more. */
9764 cu->rust_unions.clear ();
c9317f21
TT
9765}
9766
8adb8487
TT
9767/* See read.h. */
9768
9769type_unit_group_unshareable *
9770dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9771{
9772 auto iter = this->m_type_units.find (tu_group);
9773 if (iter != this->m_type_units.end ())
9774 return iter->second.get ();
9775
9776 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9777 type_unit_group_unshareable *result = uniq.get ();
9778 this->m_type_units[tu_group] = std::move (uniq);
9779 return result;
9780}
9781
e286671b
TT
9782struct type *
9783dwarf2_per_objfile::get_type_for_signatured_type
9784 (signatured_type *sig_type) const
9785{
9786 auto iter = this->m_type_map.find (sig_type);
9787 if (iter == this->m_type_map.end ())
9788 return nullptr;
9789
9790 return iter->second;
9791}
9792
9793void dwarf2_per_objfile::set_type_for_signatured_type
9794 (signatured_type *sig_type, struct type *type)
9795{
9796 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9797
9798 this->m_type_map[sig_type] = type;
9799}
9800
95554aad
TT
9801/* A helper function for computing the list of all symbol tables
9802 included by PER_CU. */
9803
9804static void
4c39bc03 9805recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9806 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9807 dwarf2_per_cu_data *per_cu,
9808 dwarf2_per_objfile *per_objfile,
43f3e411 9809 struct compunit_symtab *immediate_parent)
95554aad 9810{
af758d11 9811 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9812 if (*slot != NULL)
9813 {
9814 /* This inclusion and its children have been processed. */
9815 return;
9816 }
9817
9818 *slot = per_cu;
af758d11 9819
95554aad 9820 /* Only add a CU if it has a symbol table. */
43182c09 9821 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9822 if (cust != NULL)
ec94af83
DE
9823 {
9824 /* If this is a type unit only add its symbol table if we haven't
9825 seen it yet (type unit per_cu's can share symtabs). */
9826 if (per_cu->is_debug_types)
9827 {
43f3e411 9828 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9829 if (*slot == NULL)
9830 {
43f3e411 9831 *slot = cust;
4c39bc03 9832 result->push_back (cust);
43f3e411
DE
9833 if (cust->user == NULL)
9834 cust->user = immediate_parent;
ec94af83
DE
9835 }
9836 }
9837 else
f9125b6c 9838 {
4c39bc03 9839 result->push_back (cust);
43f3e411
DE
9840 if (cust->user == NULL)
9841 cust->user = immediate_parent;
f9125b6c 9842 }
ec94af83 9843 }
95554aad 9844
ae640021
AB
9845 if (!per_cu->imported_symtabs_empty ())
9846 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9847 {
9848 recursively_compute_inclusions (result, all_children,
43182c09
SM
9849 all_type_symtabs, ptr, per_objfile,
9850 cust);
ae640021 9851 }
95554aad
TT
9852}
9853
43f3e411 9854/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9855 PER_CU. */
9856
9857static void
43182c09
SM
9858compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9859 dwarf2_per_objfile *per_objfile)
95554aad 9860{
f4dc4d17
DE
9861 gdb_assert (! per_cu->is_debug_types);
9862
ae640021 9863 if (!per_cu->imported_symtabs_empty ())
95554aad 9864 {
ae640021 9865 int len;
4c39bc03 9866 std::vector<compunit_symtab *> result_symtabs;
43182c09 9867 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9868
9869 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9870 if (cust == NULL)
95554aad
TT
9871 return;
9872
280a9412
TT
9873 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9874 htab_eq_pointer,
9875 NULL, xcalloc, xfree));
9876 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9877 htab_eq_pointer,
9878 NULL, xcalloc, xfree));
95554aad 9879
ae640021 9880 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83 9881 {
280a9412
TT
9882 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9883 all_type_symtabs.get (), ptr,
9884 per_objfile, cust);
ec94af83 9885 }
95554aad 9886
ec94af83 9887 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9888 len = result_symtabs.size ();
43f3e411 9889 cust->includes
f6e649dd 9890 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9891 struct compunit_symtab *, len + 1);
4c39bc03
TT
9892 memcpy (cust->includes, result_symtabs.data (),
9893 len * sizeof (compunit_symtab *));
43f3e411 9894 cust->includes[len] = NULL;
95554aad
TT
9895 }
9896}
9897
9898/* Compute the 'includes' field for the symtabs of all the CUs we just
9899 read. */
9900
9901static void
976ca316 9902process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9903{
976ca316 9904 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9905 {
9906 if (! iter->is_debug_types)
976ca316 9907 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9908 }
95554aad 9909
976ca316 9910 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9911}
9912
8fc0b21d 9913/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9914 already been loaded into memory. */
9915
9916static void
8fc0b21d 9917process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9918{
976ca316
SM
9919 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9920 struct objfile *objfile = per_objfile->objfile;
08feed99 9921 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9922 CORE_ADDR lowpc, highpc;
43f3e411 9923 struct compunit_symtab *cust;
10b3939b 9924 CORE_ADDR baseaddr;
4359dff1 9925 struct block *static_block;
3e29f34a 9926 CORE_ADDR addr;
10b3939b 9927
b3b3bada 9928 baseaddr = objfile->text_section_offset ();
10b3939b 9929
c89b44cd
TT
9930 /* Clear the list here in case something was left over. */
9931 cu->method_list.clear ();
10b3939b 9932
95554aad
TT
9933 cu->language = pretend_language;
9934 cu->language_defn = language_def (cu->language);
9935
1c47ec3e
TV
9936 dwarf2_find_base_address (cu->dies, cu);
9937
c906108c 9938 /* Do line number decoding in read_file_scope () */
10b3939b 9939 process_die (cu->dies, cu);
c906108c 9940
a766d390
DE
9941 /* For now fudge the Go package. */
9942 if (cu->language == language_go)
9943 fixup_go_packaging (cu);
9944
5f48f8f3 9945 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9946 should be complete, and it should now be safe to compute all of the
9947 physnames. */
9948 compute_delayed_physnames (cu);
3da10d80 9949
c9317f21
TT
9950 if (cu->language == language_rust)
9951 rust_union_quirks (cu);
9952
fae299cd
DC
9953 /* Some compilers don't define a DW_AT_high_pc attribute for the
9954 compilation unit. If the DW_AT_high_pc is missing, synthesize
9955 it, by scanning the DIE's below the compilation unit. */
10b3939b 9956 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9957
3e29f34a 9958 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9959 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9960
9961 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9962 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9963 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9964 addrmap to help ensure it has an accurate map of pc values belonging to
9965 this comp unit. */
9966 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9967
c24bdb02 9968 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9969 SECT_OFF_TEXT (objfile),
9970 0);
c906108c 9971
43f3e411 9972 if (cust != NULL)
c906108c 9973 {
df15bd07 9974 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9975
8be455d7
JK
9976 /* Set symtab language to language from DW_AT_language. If the
9977 compilation is from a C file generated by language preprocessors, do
9978 not set the language if it was already deduced by start_subfile. */
43f3e411 9979 if (!(cu->language == language_c
40e3ad0e 9980 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9981 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9982
9983 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9984 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9985 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9986 there were bugs in prologue debug info, fixed later in GCC-4.5
9987 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9988
9989 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9990 needed, it would be wrong due to missing DW_AT_producer there.
9991
9992 Still one can confuse GDB by using non-standard GCC compilation
9993 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9994 */
ab260dad 9995 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9996 cust->locations_valid = 1;
e0d00bc7
JK
9997
9998 if (gcc_4_minor >= 5)
43f3e411 9999 cust->epilogue_unwind_valid = 1;
96408a79 10000
43f3e411 10001 cust->call_site_htab = cu->call_site_htab;
c906108c 10002 }
9291a0cd 10003
976ca316 10004 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 10005
95554aad 10006 /* Push it for inclusion processing later. */
976ca316 10007 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
10008
10009 /* Not needed any more. */
c24bdb02 10010 cu->reset_builder ();
f4dc4d17 10011}
45cfd468 10012
8fc0b21d 10013/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
10014 already been loaded into memory. */
10015
10016static void
8fc0b21d 10017process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
10018 enum language pretend_language)
10019{
976ca316
SM
10020 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10021 struct objfile *objfile = per_objfile->objfile;
43f3e411 10022 struct compunit_symtab *cust;
0186c6a7
DE
10023 struct signatured_type *sig_type;
10024
8fc0b21d
SM
10025 gdb_assert (cu->per_cu->is_debug_types);
10026 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 10027
c89b44cd
TT
10028 /* Clear the list here in case something was left over. */
10029 cu->method_list.clear ();
f4dc4d17 10030
f4dc4d17
DE
10031 cu->language = pretend_language;
10032 cu->language_defn = language_def (cu->language);
10033
10034 /* The symbol tables are set up in read_type_unit_scope. */
10035 process_die (cu->dies, cu);
10036
10037 /* For now fudge the Go package. */
10038 if (cu->language == language_go)
10039 fixup_go_packaging (cu);
10040
5f48f8f3 10041 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10042 should be complete, and it should now be safe to compute all of the
10043 physnames. */
10044 compute_delayed_physnames (cu);
f4dc4d17 10045
c9317f21
TT
10046 if (cu->language == language_rust)
10047 rust_union_quirks (cu);
10048
f4dc4d17
DE
10049 /* TUs share symbol tables.
10050 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10051 of it with end_expandable_symtab. Otherwise, complete the addition of
10052 this TU's symbols to the existing symtab. */
8adb8487 10053 type_unit_group_unshareable *tug_unshare =
976ca316 10054 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 10055 if (tug_unshare->compunit_symtab == NULL)
45cfd468 10056 {
c24bdb02
KS
10057 buildsym_compunit *builder = cu->get_builder ();
10058 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 10059 tug_unshare->compunit_symtab = cust;
f4dc4d17 10060
43f3e411 10061 if (cust != NULL)
f4dc4d17
DE
10062 {
10063 /* Set symtab language to language from DW_AT_language. If the
10064 compilation is from a C file generated by language preprocessors,
10065 do not set the language if it was already deduced by
10066 start_subfile. */
43f3e411
DE
10067 if (!(cu->language == language_c
10068 && COMPUNIT_FILETABS (cust)->language != language_c))
10069 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10070 }
10071 }
10072 else
10073 {
c24bdb02 10074 cu->get_builder ()->augment_type_symtab ();
8adb8487 10075 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
10076 }
10077
976ca316 10078 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
10079
10080 /* Not needed any more. */
c24bdb02 10081 cu->reset_builder ();
c906108c
SS
10082}
10083
95554aad
TT
10084/* Process an imported unit DIE. */
10085
10086static void
10087process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10088{
10089 struct attribute *attr;
10090
f4dc4d17
DE
10091 /* For now we don't handle imported units in type units. */
10092 if (cu->per_cu->is_debug_types)
10093 {
10094 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10095 " supported in type units [in module %s]"),
5e22e966 10096 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
10097 }
10098
95554aad
TT
10099 attr = dwarf2_attr (die, DW_AT_import, cu);
10100 if (attr != NULL)
10101 {
0826b30a 10102 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 10103 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 10104 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 10105 dwarf2_per_cu_data *per_cu
ab432490 10106 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 10107
58990295
TV
10108 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10109 into another compilation unit, at root level. Regard this as a hint,
10110 and ignore it. */
10111 if (die->parent && die->parent->parent == NULL
10112 && per_cu->unit_type == DW_UT_compile
10113 && per_cu->lang == language_cplus)
10114 return;
10115
69d751e3 10116 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 10117 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
10118 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10119 false, cu->language);
95554aad 10120
ae640021 10121 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10122 }
10123}
10124
4c8aa72d
PA
10125/* RAII object that represents a process_die scope: i.e.,
10126 starts/finishes processing a DIE. */
10127class process_die_scope
adde2bff 10128{
4c8aa72d
PA
10129public:
10130 process_die_scope (die_info *die, dwarf2_cu *cu)
10131 : m_die (die), m_cu (cu)
10132 {
10133 /* We should only be processing DIEs not already in process. */
10134 gdb_assert (!m_die->in_process);
10135 m_die->in_process = true;
10136 }
8c3cb9fa 10137
4c8aa72d
PA
10138 ~process_die_scope ()
10139 {
10140 m_die->in_process = false;
10141
10142 /* If we're done processing the DIE for the CU that owns the line
10143 header, we don't need the line header anymore. */
10144 if (m_cu->line_header_die_owner == m_die)
10145 {
10146 delete m_cu->line_header;
10147 m_cu->line_header = NULL;
10148 m_cu->line_header_die_owner = NULL;
10149 }
10150 }
10151
10152private:
10153 die_info *m_die;
10154 dwarf2_cu *m_cu;
10155};
adde2bff 10156
c906108c
SS
10157/* Process a die and its children. */
10158
10159static void
e7c27a73 10160process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10161{
4c8aa72d 10162 process_die_scope scope (die, cu);
adde2bff 10163
c906108c
SS
10164 switch (die->tag)
10165 {
10166 case DW_TAG_padding:
10167 break;
10168 case DW_TAG_compile_unit:
95554aad 10169 case DW_TAG_partial_unit:
e7c27a73 10170 read_file_scope (die, cu);
c906108c 10171 break;
348e048f
DE
10172 case DW_TAG_type_unit:
10173 read_type_unit_scope (die, cu);
10174 break;
c906108c 10175 case DW_TAG_subprogram:
0a4b0913
AB
10176 /* Nested subprograms in Fortran get a prefix. */
10177 if (cu->language == language_fortran
10178 && die->parent != NULL
10179 && die->parent->tag == DW_TAG_subprogram)
10180 cu->processing_has_namespace_info = true;
10181 /* Fall through. */
c906108c 10182 case DW_TAG_inlined_subroutine:
edb3359d 10183 read_func_scope (die, cu);
c906108c
SS
10184 break;
10185 case DW_TAG_lexical_block:
14898363
L
10186 case DW_TAG_try_block:
10187 case DW_TAG_catch_block:
e7c27a73 10188 read_lexical_block_scope (die, cu);
c906108c 10189 break;
216f72a1 10190 case DW_TAG_call_site:
96408a79
SA
10191 case DW_TAG_GNU_call_site:
10192 read_call_site_scope (die, cu);
10193 break;
c906108c 10194 case DW_TAG_class_type:
680b30c7 10195 case DW_TAG_interface_type:
c906108c
SS
10196 case DW_TAG_structure_type:
10197 case DW_TAG_union_type:
134d01f1 10198 process_structure_scope (die, cu);
c906108c
SS
10199 break;
10200 case DW_TAG_enumeration_type:
134d01f1 10201 process_enumeration_scope (die, cu);
c906108c 10202 break;
134d01f1 10203
f792889a
DJ
10204 /* These dies have a type, but processing them does not create
10205 a symbol or recurse to process the children. Therefore we can
10206 read them on-demand through read_type_die. */
c906108c 10207 case DW_TAG_subroutine_type:
72019c9c 10208 case DW_TAG_set_type:
c906108c 10209 case DW_TAG_pointer_type:
c906108c 10210 case DW_TAG_ptr_to_member_type:
c906108c 10211 case DW_TAG_reference_type:
4297a3f0 10212 case DW_TAG_rvalue_reference_type:
c906108c 10213 case DW_TAG_string_type:
c906108c 10214 break;
134d01f1 10215
d8f62e84
TT
10216 case DW_TAG_array_type:
10217 /* We only need to handle this case for Ada -- in other
10218 languages, it's normal for the compiler to emit a typedef
10219 instead. */
10220 if (cu->language != language_ada)
10221 break;
10222 /* FALLTHROUGH */
c906108c 10223 case DW_TAG_base_type:
a02abb62 10224 case DW_TAG_subrange_type:
cb249c71 10225 case DW_TAG_typedef:
134d01f1 10226 /* Add a typedef symbol for the type definition, if it has a
dda83cd7 10227 DW_AT_name. */
f792889a 10228 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10229 break;
c906108c 10230 case DW_TAG_common_block:
e7c27a73 10231 read_common_block (die, cu);
c906108c
SS
10232 break;
10233 case DW_TAG_common_inclusion:
10234 break;
d9fa45fe 10235 case DW_TAG_namespace:
9068261f 10236 cu->processing_has_namespace_info = true;
e7c27a73 10237 read_namespace (die, cu);
d9fa45fe 10238 break;
5d7cb8df 10239 case DW_TAG_module:
9068261f 10240 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10241 read_module (die, cu);
10242 break;
d9fa45fe 10243 case DW_TAG_imported_declaration:
9068261f 10244 cu->processing_has_namespace_info = true;
74921315
KS
10245 if (read_namespace_alias (die, cu))
10246 break;
86a73007
TT
10247 /* The declaration is not a global namespace alias. */
10248 /* Fall through. */
d9fa45fe 10249 case DW_TAG_imported_module:
9068261f 10250 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10251 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10252 || cu->language != language_fortran))
b98664d3 10253 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10254 dwarf_tag_name (die->tag));
10255 read_import_statement (die, cu);
d9fa45fe 10256 break;
95554aad
TT
10257
10258 case DW_TAG_imported_unit:
10259 process_imported_unit_die (die, cu);
10260 break;
10261
71a3c369
TT
10262 case DW_TAG_variable:
10263 read_variable (die, cu);
10264 break;
10265
c906108c 10266 default:
e7c27a73 10267 new_symbol (die, NULL, cu);
c906108c
SS
10268 break;
10269 }
10270}
ca69b9e6
DE
10271\f
10272/* DWARF name computation. */
c906108c 10273
94af9270
KS
10274/* A helper function for dwarf2_compute_name which determines whether DIE
10275 needs to have the name of the scope prepended to the name listed in the
10276 die. */
10277
10278static int
10279die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10280{
1c809c68
TT
10281 struct attribute *attr;
10282
94af9270
KS
10283 switch (die->tag)
10284 {
10285 case DW_TAG_namespace:
10286 case DW_TAG_typedef:
10287 case DW_TAG_class_type:
10288 case DW_TAG_interface_type:
10289 case DW_TAG_structure_type:
10290 case DW_TAG_union_type:
10291 case DW_TAG_enumeration_type:
10292 case DW_TAG_enumerator:
10293 case DW_TAG_subprogram:
08a76f8a 10294 case DW_TAG_inlined_subroutine:
94af9270 10295 case DW_TAG_member:
74921315 10296 case DW_TAG_imported_declaration:
94af9270
KS
10297 return 1;
10298
10299 case DW_TAG_variable:
c2b0a229 10300 case DW_TAG_constant:
94af9270
KS
10301 /* We only need to prefix "globally" visible variables. These include
10302 any variable marked with DW_AT_external or any variable that
10303 lives in a namespace. [Variables in anonymous namespaces
10304 require prefixing, but they are not DW_AT_external.] */
10305
10306 if (dwarf2_attr (die, DW_AT_specification, cu))
10307 {
10308 struct dwarf2_cu *spec_cu = cu;
9a619af0 10309
94af9270
KS
10310 return die_needs_namespace (die_specification (die, &spec_cu),
10311 spec_cu);
10312 }
10313
1c809c68 10314 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10315 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10316 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10317 return 0;
10318 /* A variable in a lexical block of some kind does not need a
10319 namespace, even though in C++ such variables may be external
10320 and have a mangled name. */
10321 if (die->parent->tag == DW_TAG_lexical_block
10322 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10323 || die->parent->tag == DW_TAG_catch_block
10324 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10325 return 0;
10326 return 1;
94af9270
KS
10327
10328 default:
10329 return 0;
10330 }
10331}
10332
73b9be8b
KS
10333/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10334 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10335 defined for the given DIE. */
10336
10337static struct attribute *
10338dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10339{
10340 struct attribute *attr;
10341
10342 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10343 if (attr == NULL)
10344 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10345
10346 return attr;
10347}
10348
10349/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10350 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10351 defined for the given DIE. */
10352
10353static const char *
10354dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10355{
10356 const char *linkage_name;
10357
10358 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10359 if (linkage_name == NULL)
10360 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10361
787de330
TT
10362 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10363 See https://github.com/rust-lang/rust/issues/32925. */
10364 if (cu->language == language_rust && linkage_name != NULL
10365 && strchr (linkage_name, '{') != NULL)
10366 linkage_name = NULL;
10367
73b9be8b
KS
10368 return linkage_name;
10369}
10370
94af9270 10371/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10372 compute the physname for the object, which include a method's:
9c37b5ae 10373 - formal parameters (C++),
a766d390 10374 - receiver type (Go),
a766d390
DE
10375
10376 The term "physname" is a bit confusing.
10377 For C++, for example, it is the demangled name.
10378 For Go, for example, it's the mangled name.
94af9270 10379
af6b7be1
JB
10380 For Ada, return the DIE's linkage name rather than the fully qualified
10381 name. PHYSNAME is ignored..
10382
5989a64e 10383 The result is allocated on the objfile->per_bfd's obstack and
45940949 10384 canonicalized. */
94af9270
KS
10385
10386static const char *
15d034d0
TT
10387dwarf2_compute_name (const char *name,
10388 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10389 int physname)
10390{
5e22e966 10391 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10392
94af9270
KS
10393 if (name == NULL)
10394 name = dwarf2_name (die, cu);
10395
2ee7123e
DE
10396 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10397 but otherwise compute it by typename_concat inside GDB.
10398 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10399 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10400 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10401 will set the demangled name to the result of dwarf2_full_name, and it is
10402 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10403 if (cu->language == language_ada
10404 || (cu->language == language_fortran && physname))
10405 {
10406 /* For Ada unit, we prefer the linkage name over the name, as
10407 the former contains the exported name, which the user expects
10408 to be able to reference. Ideally, we want the user to be able
10409 to reference this entity using either natural or linkage name,
10410 but we haven't started looking at this enhancement yet. */
73b9be8b 10411 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10412
2ee7123e
DE
10413 if (linkage_name != NULL)
10414 return linkage_name;
f55ee35c
JK
10415 }
10416
94af9270
KS
10417 /* These are the only languages we know how to qualify names in. */
10418 if (name != NULL
9c37b5ae 10419 && (cu->language == language_cplus
c44af4eb
TT
10420 || cu->language == language_fortran || cu->language == language_d
10421 || cu->language == language_rust))
94af9270
KS
10422 {
10423 if (die_needs_namespace (die, cu))
10424 {
0d5cff50 10425 const char *prefix;
34a68019 10426 const char *canonical_name = NULL;
94af9270 10427
d7e74731
PA
10428 string_file buf;
10429
94af9270 10430 prefix = determine_prefix (die, cu);
94af9270
KS
10431 if (*prefix != '\0')
10432 {
43816ebc
TT
10433 gdb::unique_xmalloc_ptr<char> prefixed_name
10434 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10435
43816ebc 10436 buf.puts (prefixed_name.get ());
94af9270
KS
10437 }
10438 else
d7e74731 10439 buf.puts (name);
94af9270 10440
98bfdba5
PA
10441 /* Template parameters may be specified in the DIE's DW_AT_name, or
10442 as children with DW_TAG_template_type_param or
10443 DW_TAG_value_type_param. If the latter, add them to the name
10444 here. If the name already has template parameters, then
10445 skip this step; some versions of GCC emit both, and
10446 it is more efficient to use the pre-computed name.
10447
10448 Something to keep in mind about this process: it is very
10449 unlikely, or in some cases downright impossible, to produce
10450 something that will match the mangled name of a function.
10451 If the definition of the function has the same debug info,
10452 we should be able to match up with it anyway. But fallbacks
10453 using the minimal symbol, for instance to find a method
10454 implemented in a stripped copy of libstdc++, will not work.
10455 If we do not have debug info for the definition, we will have to
10456 match them up some other way.
10457
10458 When we do name matching there is a related problem with function
10459 templates; two instantiated function templates are allowed to
10460 differ only by their return types, which we do not add here. */
10461
10462 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10463 {
10464 struct attribute *attr;
10465 struct die_info *child;
10466 int first = 1;
10467
10468 die->building_fullname = 1;
10469
10470 for (child = die->child; child != NULL; child = child->sibling)
10471 {
10472 struct type *type;
12df843f 10473 LONGEST value;
d521ce57 10474 const gdb_byte *bytes;
98bfdba5
PA
10475 struct dwarf2_locexpr_baton *baton;
10476 struct value *v;
10477
10478 if (child->tag != DW_TAG_template_type_param
10479 && child->tag != DW_TAG_template_value_param)
10480 continue;
10481
10482 if (first)
10483 {
d7e74731 10484 buf.puts ("<");
98bfdba5
PA
10485 first = 0;
10486 }
10487 else
d7e74731 10488 buf.puts (", ");
98bfdba5
PA
10489
10490 attr = dwarf2_attr (child, DW_AT_type, cu);
10491 if (attr == NULL)
10492 {
b98664d3 10493 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10494 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10495 continue;
10496 }
10497 type = die_type (child, cu);
10498
10499 if (child->tag == DW_TAG_template_type_param)
10500 {
c1ec8cea
TT
10501 c_print_type (type, "", &buf, -1, 0, cu->language,
10502 &type_print_raw_options);
98bfdba5
PA
10503 continue;
10504 }
10505
10506 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10507 if (attr == NULL)
10508 {
b98664d3 10509 complaint (_("template parameter missing "
3e43a32a 10510 "DW_AT_const_value"));
d7e74731 10511 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10512 continue;
10513 }
10514
10515 dwarf2_const_value_attr (attr, type, name,
10516 &cu->comp_unit_obstack, cu,
10517 &value, &bytes, &baton);
10518
20ce4123 10519 if (type->has_no_signedness ())
98bfdba5
PA
10520 /* GDB prints characters as NUMBER 'CHAR'. If that's
10521 changed, this can use value_print instead. */
d7e74731 10522 c_printchar (value, type, &buf);
98bfdba5
PA
10523 else
10524 {
10525 struct value_print_options opts;
10526
10527 if (baton != NULL)
10528 v = dwarf2_evaluate_loc_desc (type, NULL,
10529 baton->data,
10530 baton->size,
9f47c707
SM
10531 baton->per_cu,
10532 baton->per_objfile);
98bfdba5
PA
10533 else if (bytes != NULL)
10534 {
10535 v = allocate_value (type);
10536 memcpy (value_contents_writeable (v), bytes,
10537 TYPE_LENGTH (type));
10538 }
10539 else
10540 v = value_from_longest (type, value);
10541
3e43a32a
MS
10542 /* Specify decimal so that we do not depend on
10543 the radix. */
98bfdba5
PA
10544 get_formatted_print_options (&opts, 'd');
10545 opts.raw = 1;
d7e74731 10546 value_print (v, &buf, &opts);
98bfdba5 10547 release_value (v);
98bfdba5
PA
10548 }
10549 }
10550
10551 die->building_fullname = 0;
10552
10553 if (!first)
10554 {
10555 /* Close the argument list, with a space if necessary
10556 (nested templates). */
d7e74731
PA
10557 if (!buf.empty () && buf.string ().back () == '>')
10558 buf.puts (" >");
98bfdba5 10559 else
d7e74731 10560 buf.puts (">");
98bfdba5
PA
10561 }
10562 }
10563
9c37b5ae 10564 /* For C++ methods, append formal parameter type
94af9270 10565 information, if PHYSNAME. */
6e70227d 10566
94af9270 10567 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10568 && cu->language == language_cplus)
94af9270
KS
10569 {
10570 struct type *type = read_type_die (die, cu);
10571
d7e74731 10572 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10573 &type_print_raw_options);
94af9270 10574
9c37b5ae 10575 if (cu->language == language_cplus)
94af9270 10576 {
60430eff
DJ
10577 /* Assume that an artificial first parameter is
10578 "this", but do not crash if it is not. RealView
10579 marks unnamed (and thus unused) parameters as
10580 artificial; there is no way to differentiate
10581 the two cases. */
1f704f76 10582 if (type->num_fields () > 0
94af9270 10583 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
10584 && type->field (0).type ()->code () == TYPE_CODE_PTR
10585 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 10586 buf.puts (" const");
94af9270
KS
10587 }
10588 }
10589
d7e74731 10590 const std::string &intermediate_name = buf.string ();
94af9270
KS
10591
10592 if (cu->language == language_cplus)
34a68019 10593 canonical_name
322a8516 10594 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10595 objfile);
34a68019
TT
10596
10597 /* If we only computed INTERMEDIATE_NAME, or if
10598 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10599 intern it. */
322a8516 10600 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10601 name = objfile->intern (intermediate_name);
34a68019
TT
10602 else
10603 name = canonical_name;
94af9270
KS
10604 }
10605 }
10606
10607 return name;
10608}
10609
0114d602
DJ
10610/* Return the fully qualified name of DIE, based on its DW_AT_name.
10611 If scope qualifiers are appropriate they will be added. The result
34a68019 10612 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10613 not have a name. NAME may either be from a previous call to
10614 dwarf2_name or NULL.
10615
9c37b5ae 10616 The output string will be canonicalized (if C++). */
0114d602
DJ
10617
10618static const char *
15d034d0 10619dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10620{
94af9270
KS
10621 return dwarf2_compute_name (name, die, cu, 0);
10622}
0114d602 10623
94af9270
KS
10624/* Construct a physname for the given DIE in CU. NAME may either be
10625 from a previous call to dwarf2_name or NULL. The result will be
10626 allocated on the objfile_objstack or NULL if the DIE does not have a
10627 name.
0114d602 10628
9c37b5ae 10629 The output string will be canonicalized (if C++). */
0114d602 10630
94af9270 10631static const char *
15d034d0 10632dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10633{
5e22e966 10634 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10635 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10636 int need_copy = 1;
10637
10638 /* In this case dwarf2_compute_name is just a shortcut not building anything
10639 on its own. */
10640 if (!die_needs_namespace (die, cu))
10641 return dwarf2_compute_name (name, die, cu, 1);
10642
906bb4c5
TT
10643 if (cu->language != language_rust)
10644 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10645
10646 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10647 has computed. */
791afaa2 10648 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10649 if (mangled != NULL)
900e11f9 10650 {
900e11f9 10651
d3355e4d 10652 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
59cc4834
JB
10653 {
10654 /* Do nothing (do not demangle the symbol name). */
10655 }
a766d390
DE
10656 else
10657 {
0eb876f5
JB
10658 /* Use DMGL_RET_DROP for C++ template functions to suppress
10659 their return type. It is easier for GDB users to search
10660 for such functions as `name(params)' than `long name(params)'.
10661 In such case the minimal symbol names do not match the full
10662 symbol names but for template functions there is never a need
10663 to look up their definition from their declaration so
10664 the only disadvantage remains the minimal symbol variant
10665 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10666 demangled.reset (gdb_demangle (mangled,
10667 (DMGL_PARAMS | DMGL_ANSI
10668 | DMGL_RET_DROP)));
a766d390 10669 }
900e11f9 10670 if (demangled)
791afaa2 10671 canon = demangled.get ();
900e11f9
JK
10672 else
10673 {
10674 canon = mangled;
10675 need_copy = 0;
10676 }
10677 }
10678
10679 if (canon == NULL || check_physname)
10680 {
10681 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10682
10683 if (canon != NULL && strcmp (physname, canon) != 0)
10684 {
10685 /* It may not mean a bug in GDB. The compiler could also
10686 compute DW_AT_linkage_name incorrectly. But in such case
10687 GDB would need to be bug-to-bug compatible. */
10688
b98664d3 10689 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10690 "(from linkage <%s>) - DIE at %s [in module %s]"),
10691 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10692 objfile_name (objfile));
900e11f9
JK
10693
10694 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10695 is available here - over computed PHYSNAME. It is safer
10696 against both buggy GDB and buggy compilers. */
10697
10698 retval = canon;
10699 }
10700 else
10701 {
10702 retval = physname;
10703 need_copy = 0;
10704 }
10705 }
10706 else
10707 retval = canon;
10708
10709 if (need_copy)
be1e3d3e 10710 retval = objfile->intern (retval);
900e11f9 10711
900e11f9 10712 return retval;
0114d602
DJ
10713}
10714
74921315
KS
10715/* Inspect DIE in CU for a namespace alias. If one exists, record
10716 a new symbol for it.
10717
10718 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10719
10720static int
10721read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10722{
10723 struct attribute *attr;
10724
10725 /* If the die does not have a name, this is not a namespace
10726 alias. */
10727 attr = dwarf2_attr (die, DW_AT_name, cu);
10728 if (attr != NULL)
10729 {
10730 int num;
10731 struct die_info *d = die;
10732 struct dwarf2_cu *imported_cu = cu;
10733
10734 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10735 keep inspecting DIEs until we hit the underlying import. */
10736#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10737 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10738 {
10739 attr = dwarf2_attr (d, DW_AT_import, cu);
10740 if (attr == NULL)
10741 break;
10742
10743 d = follow_die_ref (d, attr, &imported_cu);
10744 if (d->tag != DW_TAG_imported_declaration)
10745 break;
10746 }
10747
10748 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10749 {
b98664d3 10750 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10751 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10752 return 0;
10753 }
10754
10755 if (attr != NULL)
10756 {
10757 struct type *type;
0826b30a 10758 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10759
aa66c379 10760 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10761 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10762 {
10763 /* This declaration is a global namespace alias. Add
10764 a symbol for it whose type is the aliased namespace. */
10765 new_symbol (die, type, cu);
10766 return 1;
10767 }
10768 }
10769 }
10770
10771 return 0;
10772}
10773
22cee43f 10774/* Return the using directives repository (global or local?) to use in the
804d2729 10775 current context for CU.
22cee43f
PMR
10776
10777 For Ada, imported declarations can materialize renamings, which *may* be
10778 global. However it is impossible (for now?) in DWARF to distinguish
10779 "external" imported declarations and "static" ones. As all imported
10780 declarations seem to be static in all other languages, make them all CU-wide
10781 global only in Ada. */
10782
10783static struct using_direct **
804d2729 10784using_directives (struct dwarf2_cu *cu)
22cee43f 10785{
c24bdb02
KS
10786 if (cu->language == language_ada
10787 && cu->get_builder ()->outermost_context_p ())
10788 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10789 else
c24bdb02 10790 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10791}
10792
27aa8d6a
SW
10793/* Read the import statement specified by the given die and record it. */
10794
10795static void
10796read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10797{
5e22e966 10798 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10799 struct attribute *import_attr;
32019081 10800 struct die_info *imported_die, *child_die;
de4affc9 10801 struct dwarf2_cu *imported_cu;
27aa8d6a 10802 const char *imported_name;
794684b6 10803 const char *imported_name_prefix;
13387711
SW
10804 const char *canonical_name;
10805 const char *import_alias;
10806 const char *imported_declaration = NULL;
794684b6 10807 const char *import_prefix;
eb1e02fd 10808 std::vector<const char *> excludes;
13387711 10809
27aa8d6a
SW
10810 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10811 if (import_attr == NULL)
10812 {
b98664d3 10813 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10814 dwarf_tag_name (die->tag));
10815 return;
10816 }
10817
de4affc9
CC
10818 imported_cu = cu;
10819 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10820 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10821 if (imported_name == NULL)
10822 {
10823 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10824
dda83cd7
SM
10825 The import in the following code:
10826 namespace A
10827 {
10828 typedef int B;
10829 }
10830
10831 int main ()
10832 {
10833 using A::B;
10834 B b;
10835 return b;
10836 }
10837
10838 ...
10839 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10840 <52> DW_AT_decl_file : 1
10841 <53> DW_AT_decl_line : 6
10842 <54> DW_AT_import : <0x75>
10843 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10844 <59> DW_AT_name : B
10845 <5b> DW_AT_decl_file : 1
10846 <5c> DW_AT_decl_line : 2
10847 <5d> DW_AT_type : <0x6e>
10848 ...
10849 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10850 <76> DW_AT_byte_size : 4
10851 <77> DW_AT_encoding : 5 (signed)
10852
10853 imports the wrong die ( 0x75 instead of 0x58 ).
10854 This case will be ignored until the gcc bug is fixed. */
27aa8d6a
SW
10855 return;
10856 }
10857
82856980
SW
10858 /* Figure out the local name after import. */
10859 import_alias = dwarf2_name (die, cu);
27aa8d6a 10860
794684b6
SW
10861 /* Figure out where the statement is being imported to. */
10862 import_prefix = determine_prefix (die, cu);
10863
10864 /* Figure out what the scope of the imported die is and prepend it
10865 to the name of the imported die. */
de4affc9 10866 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10867
f55ee35c
JK
10868 if (imported_die->tag != DW_TAG_namespace
10869 && imported_die->tag != DW_TAG_module)
794684b6 10870 {
13387711
SW
10871 imported_declaration = imported_name;
10872 canonical_name = imported_name_prefix;
794684b6 10873 }
13387711 10874 else if (strlen (imported_name_prefix) > 0)
12aaed36 10875 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10876 imported_name_prefix,
10877 (cu->language == language_d ? "." : "::"),
10878 imported_name, (char *) NULL);
13387711
SW
10879 else
10880 canonical_name = imported_name;
794684b6 10881
32019081
JK
10882 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10883 for (child_die = die->child; child_die && child_die->tag;
436c571c 10884 child_die = child_die->sibling)
32019081
JK
10885 {
10886 /* DWARF-4: A Fortran use statement with a “rename list” may be
10887 represented by an imported module entry with an import attribute
10888 referring to the module and owned entries corresponding to those
10889 entities that are renamed as part of being imported. */
10890
10891 if (child_die->tag != DW_TAG_imported_declaration)
10892 {
b98664d3 10893 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10894 "- DIE at %s [in module %s]"),
10895 sect_offset_str (child_die->sect_off),
10896 objfile_name (objfile));
32019081
JK
10897 continue;
10898 }
10899
10900 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10901 if (import_attr == NULL)
10902 {
b98664d3 10903 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10904 dwarf_tag_name (child_die->tag));
10905 continue;
10906 }
10907
10908 imported_cu = cu;
10909 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10910 &imported_cu);
10911 imported_name = dwarf2_name (imported_die, imported_cu);
10912 if (imported_name == NULL)
10913 {
b98664d3 10914 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10915 "imported name - DIE at %s [in module %s]"),
10916 sect_offset_str (child_die->sect_off),
10917 objfile_name (objfile));
32019081
JK
10918 continue;
10919 }
10920
eb1e02fd 10921 excludes.push_back (imported_name);
32019081
JK
10922
10923 process_die (child_die, cu);
10924 }
10925
804d2729 10926 add_using_directive (using_directives (cu),
22cee43f
PMR
10927 import_prefix,
10928 canonical_name,
10929 import_alias,
10930 imported_declaration,
10931 excludes,
10932 0,
10933 &objfile->objfile_obstack);
27aa8d6a
SW
10934}
10935
5230b05a
WT
10936/* ICC<14 does not output the required DW_AT_declaration on incomplete
10937 types, but gives them a size of zero. Starting with version 14,
10938 ICC is compatible with GCC. */
10939
9068261f 10940static bool
5230b05a
WT
10941producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10942{
10943 if (!cu->checked_producer)
10944 check_producer (cu);
10945
10946 return cu->producer_is_icc_lt_14;
10947}
10948
eb77c9df
AB
10949/* ICC generates a DW_AT_type for C void functions. This was observed on
10950 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10951 which says that void functions should not have a DW_AT_type. */
10952
10953static bool
10954producer_is_icc (struct dwarf2_cu *cu)
10955{
10956 if (!cu->checked_producer)
10957 check_producer (cu);
10958
10959 return cu->producer_is_icc;
10960}
10961
1b80a9fa
JK
10962/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10963 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10964 this, it was first present in GCC release 4.3.0. */
10965
9068261f 10966static bool
1b80a9fa
JK
10967producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10968{
10969 if (!cu->checked_producer)
10970 check_producer (cu);
10971
10972 return cu->producer_is_gcc_lt_4_3;
10973}
10974
d721ba37
PA
10975static file_and_directory
10976find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10977{
d721ba37
PA
10978 file_and_directory res;
10979
9291a0cd
TT
10980 /* Find the filename. Do not use dwarf2_name here, since the filename
10981 is not a source language identifier. */
d721ba37
PA
10982 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10983 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10984
d721ba37
PA
10985 if (res.comp_dir == NULL
10986 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10987 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10988 {
d721ba37
PA
10989 res.comp_dir_storage = ldirname (res.name);
10990 if (!res.comp_dir_storage.empty ())
10991 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10992 }
d721ba37 10993 if (res.comp_dir != NULL)
9291a0cd
TT
10994 {
10995 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10996 directory, get rid of it. */
d721ba37 10997 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10998
d721ba37
PA
10999 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11000 res.comp_dir = cp + 1;
9291a0cd
TT
11001 }
11002
d721ba37
PA
11003 if (res.name == NULL)
11004 res.name = "<unknown>";
11005
11006 return res;
9291a0cd
TT
11007}
11008
f4dc4d17
DE
11009/* Handle DW_AT_stmt_list for a compilation unit.
11010 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11011 COMP_DIR is the compilation directory. LOWPC is passed to
11012 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11013
11014static void
11015handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11016 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11017{
976ca316 11018 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 11019 struct attribute *attr;
527f3840
JK
11020 struct line_header line_header_local;
11021 hashval_t line_header_local_hash;
527f3840
JK
11022 void **slot;
11023 int decode_mapping;
2ab95328 11024
f4dc4d17
DE
11025 gdb_assert (! cu->per_cu->is_debug_types);
11026
2ab95328 11027 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e 11028 if (attr == NULL || !attr->form_is_unsigned ())
527f3840
JK
11029 return;
11030
d4df075e 11031 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
527f3840
JK
11032
11033 /* The line header hash table is only created if needed (it exists to
11034 prevent redundant reading of the line table for partial_units).
11035 If we're given a partial_unit, we'll need it. If we're given a
11036 compile_unit, then use the line header hash table if it's already
11037 created, but don't create one just yet. */
11038
976ca316 11039 if (per_objfile->line_header_hash == NULL
527f3840 11040 && die->tag == DW_TAG_partial_unit)
2ab95328 11041 {
976ca316 11042 per_objfile->line_header_hash
d15acc42
TT
11043 .reset (htab_create_alloc (127, line_header_hash_voidp,
11044 line_header_eq_voidp,
11045 free_line_header_voidp,
11046 xcalloc, xfree));
527f3840 11047 }
2ab95328 11048
9c541725 11049 line_header_local.sect_off = line_offset;
527f3840
JK
11050 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11051 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 11052 if (per_objfile->line_header_hash != NULL)
527f3840 11053 {
976ca316 11054 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11055 &line_header_local,
11056 line_header_local_hash, NO_INSERT);
11057
11058 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11059 is not present in *SLOT (since if there is something in *SLOT then
11060 it will be for a partial_unit). */
11061 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11062 {
527f3840 11063 gdb_assert (*slot != NULL);
9a3c8263 11064 cu->line_header = (struct line_header *) *slot;
527f3840 11065 return;
dee91e82 11066 }
2ab95328 11067 }
527f3840
JK
11068
11069 /* dwarf_decode_line_header does not yet provide sufficient information.
11070 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11071 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11072 if (lh == NULL)
527f3840 11073 return;
4c8aa72d
PA
11074
11075 cu->line_header = lh.release ();
11076 cu->line_header_die_owner = die;
527f3840 11077
976ca316 11078 if (per_objfile->line_header_hash == NULL)
527f3840
JK
11079 slot = NULL;
11080 else
11081 {
976ca316 11082 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11083 &line_header_local,
11084 line_header_local_hash, INSERT);
11085 gdb_assert (slot != NULL);
11086 }
11087 if (slot != NULL && *slot == NULL)
11088 {
11089 /* This newly decoded line number information unit will be owned
11090 by line_header_hash hash table. */
11091 *slot = cu->line_header;
4c8aa72d 11092 cu->line_header_die_owner = NULL;
527f3840
JK
11093 }
11094 else
11095 {
11096 /* We cannot free any current entry in (*slot) as that struct line_header
dda83cd7 11097 may be already used by multiple CUs. Create only temporary decoded
527f3840
JK
11098 line_header for this CU - it may happen at most once for each line
11099 number information unit. And if we're not using line_header_hash
11100 then this is what we want as well. */
11101 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11102 }
11103 decode_mapping = (die->tag != DW_TAG_partial_unit);
11104 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11105 decode_mapping);
fff8551c 11106
2ab95328
TT
11107}
11108
95554aad 11109/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11110
c906108c 11111static void
e7c27a73 11112read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11113{
976ca316
SM
11114 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11115 struct objfile *objfile = per_objfile->objfile;
08feed99 11116 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 11117 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11118 CORE_ADDR highpc = ((CORE_ADDR) 0);
11119 struct attribute *attr;
c906108c 11120 struct die_info *child_die;
e142c38c 11121 CORE_ADDR baseaddr;
6e70227d 11122
380618d6 11123 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11124 baseaddr = objfile->text_section_offset ();
c906108c 11125
fae299cd 11126 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11127
11128 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11129 from finish_block. */
2acceee2 11130 if (lowpc == ((CORE_ADDR) -1))
c906108c 11131 lowpc = highpc;
3e29f34a 11132 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11133
d721ba37 11134 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11135
f4b8a18d
KW
11136 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11137 standardised yet. As a workaround for the language detection we fall
11138 back to the DW_AT_producer string. */
11139 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11140 cu->language = language_opencl;
11141
3019eac3
DE
11142 /* Similar hack for Go. */
11143 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11144 set_cu_language (DW_LANG_Go, cu);
11145
c24bdb02 11146 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11147
11148 /* Decode line number information if present. We do this before
11149 processing child DIEs, so that the line header table is available
11150 for DW_AT_decl_file. */
d721ba37 11151 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11152
11153 /* Process all dies in compilation unit. */
11154 if (die->child != NULL)
11155 {
11156 child_die = die->child;
11157 while (child_die && child_die->tag)
11158 {
11159 process_die (child_die, cu);
436c571c 11160 child_die = child_die->sibling;
3019eac3
DE
11161 }
11162 }
11163
11164 /* Decode macro information, if present. Dwarf 2 macro information
11165 refers to information in the line number info statement program
11166 header, so we can only read it if we've read the header
11167 successfully. */
0af92d60
JK
11168 attr = dwarf2_attr (die, DW_AT_macros, cu);
11169 if (attr == NULL)
11170 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
529908cb 11171 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3
DE
11172 {
11173 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11174 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11175
529908cb 11176 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
3019eac3
DE
11177 }
11178 else
11179 {
11180 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
529908cb 11181 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3 11182 {
529908cb 11183 unsigned int macro_offset = attr->as_unsigned ();
3019eac3 11184
43f3e411 11185 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11186 }
11187 }
3019eac3
DE
11188}
11189
c24bdb02
KS
11190void
11191dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11192{
f4dc4d17
DE
11193 struct type_unit_group *tu_group;
11194 int first_time;
3019eac3 11195 struct attribute *attr;
9c541725 11196 unsigned int i;
0186c6a7 11197 struct signatured_type *sig_type;
3019eac3 11198
f4dc4d17 11199 gdb_assert (per_cu->is_debug_types);
0186c6a7 11200 sig_type = (struct signatured_type *) per_cu;
3019eac3 11201
c24bdb02 11202 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11203
f4dc4d17 11204 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11205 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11206 if (sig_type->type_unit_group == NULL)
c24bdb02 11207 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11208 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11209
11210 /* If we've already processed this stmt_list there's no real need to
11211 do it again, we could fake it and just recreate the part we need
11212 (file name,index -> symtab mapping). If data shows this optimization
11213 is useful we can do it then. */
8adb8487
TT
11214 type_unit_group_unshareable *tug_unshare
11215 = per_objfile->get_type_unit_group_unshareable (tu_group);
11216 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11217
11218 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11219 debug info. */
fff8551c 11220 line_header_up lh;
d4df075e 11221 if (attr != NULL && attr->form_is_unsigned ())
3019eac3 11222 {
d4df075e 11223 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
c24bdb02 11224 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11225 }
11226 if (lh == NULL)
11227 {
11228 if (first_time)
c24bdb02 11229 start_symtab ("", NULL, 0);
f4dc4d17
DE
11230 else
11231 {
8adb8487 11232 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11233 gdb_assert (m_builder == nullptr);
8adb8487 11234 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11235 m_builder.reset (new struct buildsym_compunit
11236 (COMPUNIT_OBJFILE (cust), "",
11237 COMPUNIT_DIRNAME (cust),
11238 compunit_language (cust),
11239 0, cust));
770479f2 11240 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11241 }
f4dc4d17 11242 return;
3019eac3
DE
11243 }
11244
c24bdb02
KS
11245 line_header = lh.release ();
11246 line_header_die_owner = die;
3019eac3 11247
f4dc4d17
DE
11248 if (first_time)
11249 {
c24bdb02 11250 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11251
1fd60fc0
DE
11252 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11253 still initializing it, and our caller (a few levels up)
11254 process_full_type_unit still needs to know if this is the first
11255 time. */
11256
8adb8487 11257 tug_unshare->symtabs
4ac93832
TT
11258 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11259 struct symtab *, line_header->file_names_size ());
3019eac3 11260
7ba99d21
AT
11261 auto &file_names = line_header->file_names ();
11262 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11263 {
7ba99d21 11264 file_entry &fe = file_names[i];
c24bdb02
KS
11265 dwarf2_start_subfile (this, fe.name,
11266 fe.include_dir (line_header));
11267 buildsym_compunit *b = get_builder ();
11268 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11269 {
4c8aa72d
PA
11270 /* NOTE: start_subfile will recognize when it's been
11271 passed a file it has already seen. So we can't
11272 assume there's a simple mapping from
11273 cu->line_header->file_names to subfiles, plus
11274 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11275 b->get_current_subfile ()->symtab
11276 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11277 }
11278
c24bdb02 11279 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11280 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11281 }
11282 }
11283 else
3019eac3 11284 {
c24bdb02 11285 gdb_assert (m_builder == nullptr);
8adb8487 11286 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11287 m_builder.reset (new struct buildsym_compunit
11288 (COMPUNIT_OBJFILE (cust), "",
11289 COMPUNIT_DIRNAME (cust),
11290 compunit_language (cust),
11291 0, cust));
770479f2 11292 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11293
7ba99d21
AT
11294 auto &file_names = line_header->file_names ();
11295 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11296 {
7ba99d21 11297 file_entry &fe = file_names[i];
8adb8487 11298 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11299 }
3019eac3
DE
11300 }
11301
f4dc4d17
DE
11302 /* The main symtab is allocated last. Type units don't have DW_AT_name
11303 so they don't have a "real" (so to speak) symtab anyway.
11304 There is later code that will assign the main symtab to all symbols
11305 that don't have one. We need to handle the case of a symbol with a
11306 missing symtab (DW_AT_decl_file) anyway. */
11307}
3019eac3 11308
f4dc4d17
DE
11309/* Process DW_TAG_type_unit.
11310 For TUs we want to skip the first top level sibling if it's not the
11311 actual type being defined by this TU. In this case the first top
11312 level sibling is there to provide context only. */
3019eac3 11313
f4dc4d17
DE
11314static void
11315read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11316{
11317 struct die_info *child_die;
3019eac3 11318
f4dc4d17
DE
11319 prepare_one_comp_unit (cu, die, language_minimal);
11320
11321 /* Initialize (or reinitialize) the machinery for building symtabs.
11322 We do this before processing child DIEs, so that the line header table
11323 is available for DW_AT_decl_file. */
c24bdb02 11324 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11325
11326 if (die->child != NULL)
11327 {
11328 child_die = die->child;
11329 while (child_die && child_die->tag)
11330 {
11331 process_die (child_die, cu);
436c571c 11332 child_die = child_die->sibling;
f4dc4d17
DE
11333 }
11334 }
3019eac3
DE
11335}
11336\f
80626a55
DE
11337/* DWO/DWP files.
11338
11339 http://gcc.gnu.org/wiki/DebugFission
11340 http://gcc.gnu.org/wiki/DebugFissionDWP
11341
11342 To simplify handling of both DWO files ("object" files with the DWARF info)
11343 and DWP files (a file with the DWOs packaged up into one file), we treat
11344 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11345
11346static hashval_t
11347hash_dwo_file (const void *item)
11348{
9a3c8263 11349 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11350 hashval_t hash;
3019eac3 11351
a2ce51a0
DE
11352 hash = htab_hash_string (dwo_file->dwo_name);
11353 if (dwo_file->comp_dir != NULL)
11354 hash += htab_hash_string (dwo_file->comp_dir);
11355 return hash;
3019eac3
DE
11356}
11357
11358static int
11359eq_dwo_file (const void *item_lhs, const void *item_rhs)
11360{
9a3c8263
SM
11361 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11362 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11363
a2ce51a0
DE
11364 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11365 return 0;
11366 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11367 return lhs->comp_dir == rhs->comp_dir;
11368 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11369}
11370
11371/* Allocate a hash table for DWO files. */
11372
51ac9db5 11373static htab_up
298e9637 11374allocate_dwo_file_hash_table ()
3019eac3 11375{
51ac9db5
SM
11376 auto delete_dwo_file = [] (void *item)
11377 {
11378 struct dwo_file *dwo_file = (struct dwo_file *) item;
11379
11380 delete dwo_file;
11381 };
11382
bc68fb19
TT
11383 return htab_up (htab_create_alloc (41,
11384 hash_dwo_file,
11385 eq_dwo_file,
11386 delete_dwo_file,
11387 xcalloc, xfree));
3019eac3
DE
11388}
11389
80626a55
DE
11390/* Lookup DWO file DWO_NAME. */
11391
11392static void **
976ca316 11393lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11394 const char *dwo_name,
11395 const char *comp_dir)
80626a55
DE
11396{
11397 struct dwo_file find_entry;
11398 void **slot;
11399
976ca316
SM
11400 if (per_objfile->per_bfd->dwo_files == NULL)
11401 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11402
0ac5b59e
DE
11403 find_entry.dwo_name = dwo_name;
11404 find_entry.comp_dir = comp_dir;
976ca316 11405 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11406 INSERT);
80626a55
DE
11407
11408 return slot;
11409}
11410
3019eac3
DE
11411static hashval_t
11412hash_dwo_unit (const void *item)
11413{
9a3c8263 11414 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11415
11416 /* This drops the top 32 bits of the id, but is ok for a hash. */
11417 return dwo_unit->signature;
11418}
11419
11420static int
11421eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11422{
9a3c8263
SM
11423 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11424 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11425
11426 /* The signature is assumed to be unique within the DWO file.
11427 So while object file CU dwo_id's always have the value zero,
11428 that's OK, assuming each object file DWO file has only one CU,
11429 and that's the rule for now. */
11430 return lhs->signature == rhs->signature;
11431}
11432
11433/* Allocate a hash table for DWO CUs,TUs.
11434 There is one of these tables for each of CUs,TUs for each DWO file. */
11435
b0b6a987 11436static htab_up
298e9637 11437allocate_dwo_unit_table ()
3019eac3
DE
11438{
11439 /* Start out with a pretty small number.
11440 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11441 return htab_up (htab_create_alloc (3,
11442 hash_dwo_unit,
11443 eq_dwo_unit,
11444 NULL, xcalloc, xfree));
3019eac3
DE
11445}
11446
19c3d4c9 11447/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11448
11449static void
19c3d4c9
DE
11450create_dwo_cu_reader (const struct die_reader_specs *reader,
11451 const gdb_byte *info_ptr,
11452 struct die_info *comp_unit_die,
c0ab21c2
TT
11453 struct dwo_file *dwo_file,
11454 struct dwo_unit *dwo_unit)
3019eac3
DE
11455{
11456 struct dwarf2_cu *cu = reader->cu;
9c541725 11457 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11458 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11459
a084a2a6
AT
11460 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11461 if (!signature.has_value ())
3019eac3 11462 {
b98664d3 11463 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11464 " its dwo_id [in module %s]"),
9d8780f0 11465 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11466 return;
11467 }
11468
3019eac3 11469 dwo_unit->dwo_file = dwo_file;
a084a2a6 11470 dwo_unit->signature = *signature;
8a0459fd 11471 dwo_unit->section = section;
9c541725 11472 dwo_unit->sect_off = sect_off;
3019eac3
DE
11473 dwo_unit->length = cu->per_cu->length;
11474
6f738b01
SM
11475 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11476 sect_offset_str (sect_off),
11477 hex_string (dwo_unit->signature));
3019eac3
DE
11478}
11479
33c5cd75 11480/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11481 Note: This function processes DWO files only, not DWP files. */
3019eac3 11482
33c5cd75 11483static void
976ca316 11484create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11485 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11486 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11487{
976ca316
SM
11488 struct objfile *objfile = per_objfile->objfile;
11489 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11490 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11491
96b79293 11492 section.read (objfile);
33c5cd75 11493 info_ptr = section.buffer;
3019eac3
DE
11494
11495 if (info_ptr == NULL)
33c5cd75 11496 return;
3019eac3 11497
6f738b01
SM
11498 dwarf_read_debug_printf ("Reading %s for %s:",
11499 section.get_name (),
11500 section.get_file_name ());
3019eac3 11501
33c5cd75 11502 end_ptr = info_ptr + section.size;
3019eac3
DE
11503 while (info_ptr < end_ptr)
11504 {
11505 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11506 struct dwo_unit read_unit {};
33c5cd75
DB
11507 struct dwo_unit *dwo_unit;
11508 void **slot;
11509 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11510
11511 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11512 per_cu.per_bfd = per_bfd;
3019eac3 11513 per_cu.is_debug_types = 0;
33c5cd75
DB
11514 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11515 per_cu.section = &section;
11516
976ca316 11517 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11518 if (!reader.dummy_p)
11519 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11520 &dwo_file, &read_unit);
33c5cd75
DB
11521 info_ptr += per_cu.length;
11522
11523 // If the unit could not be parsed, skip it.
c0ab21c2 11524 if (read_unit.dwo_file == NULL)
33c5cd75 11525 continue;
3019eac3 11526
33c5cd75 11527 if (cus_htab == NULL)
298e9637 11528 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11529
1859c670 11530 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11531 struct dwo_unit);
c0ab21c2 11532 *dwo_unit = read_unit;
b0b6a987 11533 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11534 gdb_assert (slot != NULL);
11535 if (*slot != NULL)
19c3d4c9 11536 {
33c5cd75
DB
11537 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11538 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11539
b98664d3 11540 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11541 " the entry at offset %s, signature %s"),
11542 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11543 hex_string (dwo_unit->signature));
19c3d4c9 11544 }
33c5cd75 11545 *slot = (void *)dwo_unit;
3019eac3 11546 }
3019eac3
DE
11547}
11548
80626a55
DE
11549/* DWP file .debug_{cu,tu}_index section format:
11550 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
d2854d8d
CT
11551 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11552
11553 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11554 officially standard DWP format was published with DWARF v5 and is called
11555 Version 5. There are no versions 3 or 4.
80626a55 11556
d2415c6c
DE
11557 DWP Version 1:
11558
80626a55
DE
11559 Both index sections have the same format, and serve to map a 64-bit
11560 signature to a set of section numbers. Each section begins with a header,
11561 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11562 indexes, and a pool of 32-bit section numbers. The index sections will be
11563 aligned at 8-byte boundaries in the file.
11564
d2415c6c
DE
11565 The index section header consists of:
11566
11567 V, 32 bit version number
11568 -, 32 bits unused
11569 N, 32 bit number of compilation units or type units in the index
11570 M, 32 bit number of slots in the hash table
80626a55 11571
d2415c6c 11572 Numbers are recorded using the byte order of the application binary.
80626a55 11573
d2415c6c
DE
11574 The hash table begins at offset 16 in the section, and consists of an array
11575 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11576 order of the application binary). Unused slots in the hash table are 0.
11577 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11578
d2415c6c
DE
11579 The parallel table begins immediately after the hash table
11580 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11581 array of 32-bit indexes (using the byte order of the application binary),
11582 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11583 table contains a 32-bit index into the pool of section numbers. For unused
11584 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11585
73869dc2
DE
11586 The pool of section numbers begins immediately following the hash table
11587 (at offset 16 + 12 * M from the beginning of the section). The pool of
11588 section numbers consists of an array of 32-bit words (using the byte order
11589 of the application binary). Each item in the array is indexed starting
11590 from 0. The hash table entry provides the index of the first section
11591 number in the set. Additional section numbers in the set follow, and the
11592 set is terminated by a 0 entry (section number 0 is not used in ELF).
11593
11594 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11595 section must be the first entry in the set, and the .debug_abbrev.dwo must
11596 be the second entry. Other members of the set may follow in any order.
11597
11598 ---
11599
d2854d8d 11600 DWP Versions 2 and 5:
73869dc2 11601
d2854d8d 11602 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
73869dc2
DE
11603 and the entries in the index tables are now offsets into these sections.
11604 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11605 section.
11606
11607 Index Section Contents:
11608 Header
11609 Hash Table of Signatures dwp_hash_table.hash_table
11610 Parallel Table of Indices dwp_hash_table.unit_table
d2854d8d
CT
11611 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11612 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
73869dc2
DE
11613
11614 The index section header consists of:
11615
11616 V, 32 bit version number
11617 L, 32 bit number of columns in the table of section offsets
11618 N, 32 bit number of compilation units or type units in the index
11619 M, 32 bit number of slots in the hash table
11620
11621 Numbers are recorded using the byte order of the application binary.
11622
11623 The hash table has the same format as version 1.
11624 The parallel table of indices has the same format as version 1,
11625 except that the entries are origin-1 indices into the table of sections
11626 offsets and the table of section sizes.
11627
11628 The table of offsets begins immediately following the parallel table
11629 (at offset 16 + 12 * M from the beginning of the section). The table is
11630 a two-dimensional array of 32-bit words (using the byte order of the
11631 application binary), with L columns and N+1 rows, in row-major order.
11632 Each row in the array is indexed starting from 0. The first row provides
11633 a key to the remaining rows: each column in this row provides an identifier
11634 for a debug section, and the offsets in the same column of subsequent rows
d2854d8d 11635 refer to that section. The section identifiers for Version 2 are:
73869dc2
DE
11636
11637 DW_SECT_INFO 1 .debug_info.dwo
11638 DW_SECT_TYPES 2 .debug_types.dwo
11639 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11640 DW_SECT_LINE 4 .debug_line.dwo
11641 DW_SECT_LOC 5 .debug_loc.dwo
11642 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11643 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11644 DW_SECT_MACRO 8 .debug_macro.dwo
11645
d2854d8d
CT
11646 The section identifiers for Version 5 are:
11647
11648 DW_SECT_INFO_V5 1 .debug_info.dwo
11649 DW_SECT_RESERVED_V5 2 --
11650 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11651 DW_SECT_LINE_V5 4 .debug_line.dwo
11652 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11653 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11654 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11655 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11656
73869dc2
DE
11657 The offsets provided by the CU and TU index sections are the base offsets
11658 for the contributions made by each CU or TU to the corresponding section
11659 in the package file. Each CU and TU header contains an abbrev_offset
11660 field, used to find the abbreviations table for that CU or TU within the
11661 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11662 be interpreted as relative to the base offset given in the index section.
11663 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11664 should be interpreted as relative to the base offset for .debug_line.dwo,
11665 and offsets into other debug sections obtained from DWARF attributes should
11666 also be interpreted as relative to the corresponding base offset.
11667
11668 The table of sizes begins immediately following the table of offsets.
11669 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11670 with L columns and N rows, in row-major order. Each row in the array is
11671 indexed starting from 1 (row 0 is shared by the two tables).
11672
11673 ---
11674
11675 Hash table lookup is handled the same in version 1 and 2:
11676
11677 We assume that N and M will not exceed 2^32 - 1.
11678 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11679
d2415c6c
DE
11680 Given a 64-bit compilation unit signature or a type signature S, an entry
11681 in the hash table is located as follows:
80626a55 11682
d2415c6c
DE
11683 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11684 the low-order k bits all set to 1.
80626a55 11685
d2415c6c 11686 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11687
d2415c6c
DE
11688 3) If the hash table entry at index H matches the signature, use that
11689 entry. If the hash table entry at index H is unused (all zeroes),
11690 terminate the search: the signature is not present in the table.
80626a55 11691
d2415c6c 11692 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11693
d2415c6c 11694 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11695 to stop at an unused slot or find the match. */
80626a55
DE
11696
11697/* Create a hash table to map DWO IDs to their CU/TU entry in
11698 .debug_{info,types}.dwo in DWP_FILE.
11699 Returns NULL if there isn't one.
11700 Note: This function processes DWP files only, not DWO files. */
11701
11702static struct dwp_hash_table *
976ca316 11703create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11704 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11705{
976ca316 11706 struct objfile *objfile = per_objfile->objfile;
400174b1 11707 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11708 const gdb_byte *index_ptr, *index_end;
80626a55 11709 struct dwarf2_section_info *index;
73869dc2 11710 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11711 struct dwp_hash_table *htab;
11712
11713 if (is_debug_types)
11714 index = &dwp_file->sections.tu_index;
11715 else
11716 index = &dwp_file->sections.cu_index;
11717
96b79293 11718 if (index->empty ())
80626a55 11719 return NULL;
96b79293 11720 index->read (objfile);
80626a55
DE
11721
11722 index_ptr = index->buffer;
11723 index_end = index_ptr + index->size;
11724
d2854d8d
CT
11725 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11726 For now it's safe to just read 4 bytes (particularly as it's difficult to
11727 tell if you're dealing with Version 5 before you've read the version). */
80626a55 11728 version = read_4_bytes (dbfd, index_ptr);
73869dc2 11729 index_ptr += 4;
d2854d8d 11730 if (version == 2 || version == 5)
73869dc2
DE
11731 nr_columns = read_4_bytes (dbfd, index_ptr);
11732 else
11733 nr_columns = 0;
11734 index_ptr += 4;
80626a55
DE
11735 nr_units = read_4_bytes (dbfd, index_ptr);
11736 index_ptr += 4;
11737 nr_slots = read_4_bytes (dbfd, index_ptr);
11738 index_ptr += 4;
11739
d2854d8d 11740 if (version != 1 && version != 2 && version != 5)
80626a55 11741 {
21aa081e 11742 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11743 " [in module %s]"),
21aa081e 11744 pulongest (version), dwp_file->name);
80626a55
DE
11745 }
11746 if (nr_slots != (nr_slots & -nr_slots))
11747 {
21aa081e 11748 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11749 " is not power of 2 [in module %s]"),
21aa081e 11750 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11751 }
11752
976ca316 11753 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11754 htab->version = version;
11755 htab->nr_columns = nr_columns;
80626a55
DE
11756 htab->nr_units = nr_units;
11757 htab->nr_slots = nr_slots;
11758 htab->hash_table = index_ptr;
11759 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11760
11761 /* Exit early if the table is empty. */
11762 if (nr_slots == 0 || nr_units == 0
d2854d8d
CT
11763 || (version == 2 && nr_columns == 0)
11764 || (version == 5 && nr_columns == 0))
73869dc2
DE
11765 {
11766 /* All must be zero. */
11767 if (nr_slots != 0 || nr_units != 0
d2854d8d
CT
11768 || (version == 2 && nr_columns != 0)
11769 || (version == 5 && nr_columns != 0))
73869dc2 11770 {
b98664d3 11771 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11772 " all zero [in modules %s]"),
11773 dwp_file->name);
11774 }
11775 return htab;
11776 }
11777
11778 if (version == 1)
11779 {
11780 htab->section_pool.v1.indices =
11781 htab->unit_table + sizeof (uint32_t) * nr_slots;
11782 /* It's harder to decide whether the section is too small in v1.
11783 V1 is deprecated anyway so we punt. */
11784 }
d2854d8d 11785 else if (version == 2)
73869dc2
DE
11786 {
11787 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11788 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11789 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11790 /* Reverse map for error checking. */
11791 int ids_seen[DW_SECT_MAX + 1];
11792 int i;
11793
11794 if (nr_columns < 2)
11795 {
11796 error (_("Dwarf Error: bad DWP hash table, too few columns"
11797 " in section table [in module %s]"),
11798 dwp_file->name);
11799 }
11800 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11801 {
11802 error (_("Dwarf Error: bad DWP hash table, too many columns"
11803 " in section table [in module %s]"),
11804 dwp_file->name);
11805 }
04fd5eed
GB
11806 memset (ids, 255, sizeof_ids);
11807 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11808 for (i = 0; i < nr_columns; ++i)
11809 {
11810 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11811
11812 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11813 {
11814 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11815 " in section table [in module %s]"),
11816 id, dwp_file->name);
11817 }
11818 if (ids_seen[id] != -1)
11819 {
11820 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11821 " id %d in section table [in module %s]"),
11822 id, dwp_file->name);
11823 }
11824 ids_seen[id] = i;
11825 ids[i] = id;
11826 }
11827 /* Must have exactly one info or types section. */
11828 if (((ids_seen[DW_SECT_INFO] != -1)
11829 + (ids_seen[DW_SECT_TYPES] != -1))
11830 != 1)
11831 {
11832 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11833 " DWO info/types section [in module %s]"),
11834 dwp_file->name);
11835 }
11836 /* Must have an abbrev section. */
11837 if (ids_seen[DW_SECT_ABBREV] == -1)
11838 {
11839 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11840 " section [in module %s]"),
11841 dwp_file->name);
11842 }
11843 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11844 htab->section_pool.v2.sizes =
11845 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11846 * nr_units * nr_columns);
11847 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11848 * nr_units * nr_columns))
11849 > index_end)
11850 {
11851 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11852 " [in module %s]"),
11853 dwp_file->name);
11854 }
11855 }
d2854d8d
CT
11856 else /* version == 5 */
11857 {
11858 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11859 int *ids = htab->section_pool.v5.section_ids;
11860 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11861 /* Reverse map for error checking. */
11862 int ids_seen[DW_SECT_MAX_V5 + 1];
11863
11864 if (nr_columns < 2)
11865 {
11866 error (_("Dwarf Error: bad DWP hash table, too few columns"
11867 " in section table [in module %s]"),
11868 dwp_file->name);
11869 }
11870 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11871 {
11872 error (_("Dwarf Error: bad DWP hash table, too many columns"
11873 " in section table [in module %s]"),
11874 dwp_file->name);
11875 }
11876 memset (ids, 255, sizeof_ids);
11877 memset (ids_seen, 255, sizeof (ids_seen));
11878 for (int i = 0; i < nr_columns; ++i)
11879 {
11880 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11881
11882 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11883 {
11884 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11885 " in section table [in module %s]"),
11886 id, dwp_file->name);
11887 }
11888 if (ids_seen[id] != -1)
11889 {
11890 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11891 " id %d in section table [in module %s]"),
11892 id, dwp_file->name);
11893 }
11894 ids_seen[id] = i;
11895 ids[i] = id;
11896 }
11897 /* Must have seen an info section. */
11898 if (ids_seen[DW_SECT_INFO_V5] == -1)
11899 {
11900 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11901 " DWO info/types section [in module %s]"),
11902 dwp_file->name);
11903 }
11904 /* Must have an abbrev section. */
11905 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11906 {
11907 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11908 " section [in module %s]"),
11909 dwp_file->name);
11910 }
11911 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11912 htab->section_pool.v5.sizes
11913 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11914 * nr_units * nr_columns);
11915 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11916 * nr_units * nr_columns))
11917 > index_end)
11918 {
11919 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11920 " [in module %s]"),
11921 dwp_file->name);
11922 }
11923 }
80626a55
DE
11924
11925 return htab;
11926}
11927
11928/* Update SECTIONS with the data from SECTP.
11929
5bb6e9dd
TT
11930 This function is like the other "locate" section routines, but in
11931 this context the sections to read comes from the DWP V1 hash table,
11932 not the full ELF section table.
80626a55
DE
11933
11934 The result is non-zero for success, or zero if an error was found. */
11935
11936static int
73869dc2
DE
11937locate_v1_virtual_dwo_sections (asection *sectp,
11938 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11939{
11940 const struct dwop_section_names *names = &dwop_section_names;
11941
11942 if (section_is_p (sectp->name, &names->abbrev_dwo))
11943 {
11944 /* There can be only one. */
049412e3 11945 if (sections->abbrev.s.section != NULL)
80626a55 11946 return 0;
049412e3 11947 sections->abbrev.s.section = sectp;
fd361982 11948 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11949 }
11950 else if (section_is_p (sectp->name, &names->info_dwo)
11951 || section_is_p (sectp->name, &names->types_dwo))
11952 {
11953 /* There can be only one. */
049412e3 11954 if (sections->info_or_types.s.section != NULL)
80626a55 11955 return 0;
049412e3 11956 sections->info_or_types.s.section = sectp;
fd361982 11957 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11958 }
11959 else if (section_is_p (sectp->name, &names->line_dwo))
11960 {
11961 /* There can be only one. */
049412e3 11962 if (sections->line.s.section != NULL)
80626a55 11963 return 0;
049412e3 11964 sections->line.s.section = sectp;
fd361982 11965 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11966 }
11967 else if (section_is_p (sectp->name, &names->loc_dwo))
11968 {
11969 /* There can be only one. */
049412e3 11970 if (sections->loc.s.section != NULL)
80626a55 11971 return 0;
049412e3 11972 sections->loc.s.section = sectp;
fd361982 11973 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11974 }
11975 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11976 {
11977 /* There can be only one. */
049412e3 11978 if (sections->macinfo.s.section != NULL)
80626a55 11979 return 0;
049412e3 11980 sections->macinfo.s.section = sectp;
fd361982 11981 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11982 }
11983 else if (section_is_p (sectp->name, &names->macro_dwo))
11984 {
11985 /* There can be only one. */
049412e3 11986 if (sections->macro.s.section != NULL)
80626a55 11987 return 0;
049412e3 11988 sections->macro.s.section = sectp;
fd361982 11989 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11990 }
11991 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11992 {
11993 /* There can be only one. */
049412e3 11994 if (sections->str_offsets.s.section != NULL)
80626a55 11995 return 0;
049412e3 11996 sections->str_offsets.s.section = sectp;
fd361982 11997 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11998 }
11999 else
12000 {
12001 /* No other kind of section is valid. */
12002 return 0;
12003 }
12004
12005 return 1;
12006}
12007
73869dc2
DE
12008/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12009 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12010 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12011 This is for DWP version 1 files. */
80626a55
DE
12012
12013static struct dwo_unit *
976ca316 12014create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 12015 struct dwp_file *dwp_file,
73869dc2
DE
12016 uint32_t unit_index,
12017 const char *comp_dir,
12018 ULONGEST signature, int is_debug_types)
80626a55 12019{
73869dc2
DE
12020 const struct dwp_hash_table *dwp_htab =
12021 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12022 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12023 const char *kind = is_debug_types ? "TU" : "CU";
12024 struct dwo_file *dwo_file;
12025 struct dwo_unit *dwo_unit;
73869dc2 12026 struct virtual_v1_dwo_sections sections;
80626a55 12027 void **dwo_file_slot;
80626a55
DE
12028 int i;
12029
73869dc2
DE
12030 gdb_assert (dwp_file->version == 1);
12031
6f738b01
SM
12032 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12033 kind, pulongest (unit_index), hex_string (signature),
12034 dwp_file->name);
80626a55 12035
19ac8c2e 12036 /* Fetch the sections of this DWO unit.
80626a55
DE
12037 Put a limit on the number of sections we look for so that bad data
12038 doesn't cause us to loop forever. */
12039
73869dc2 12040#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12041 (1 /* .debug_info or .debug_types */ \
12042 + 1 /* .debug_abbrev */ \
12043 + 1 /* .debug_line */ \
12044 + 1 /* .debug_loc */ \
12045 + 1 /* .debug_str_offsets */ \
19ac8c2e 12046 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12047 + 1 /* trailing zero */)
12048
12049 memset (&sections, 0, sizeof (sections));
80626a55 12050
73869dc2 12051 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12052 {
12053 asection *sectp;
12054 uint32_t section_nr =
12055 read_4_bytes (dbfd,
73869dc2
DE
12056 dwp_htab->section_pool.v1.indices
12057 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12058
12059 if (section_nr == 0)
12060 break;
12061 if (section_nr >= dwp_file->num_sections)
12062 {
12063 error (_("Dwarf Error: bad DWP hash table, section number too large"
12064 " [in module %s]"),
12065 dwp_file->name);
12066 }
12067
12068 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12069 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12070 {
12071 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12072 " [in module %s]"),
12073 dwp_file->name);
12074 }
12075 }
12076
12077 if (i < 2
96b79293
TT
12078 || sections.info_or_types.empty ()
12079 || sections.abbrev.empty ())
80626a55
DE
12080 {
12081 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12082 " [in module %s]"),
12083 dwp_file->name);
12084 }
73869dc2 12085 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12086 {
12087 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12088 " [in module %s]"),
12089 dwp_file->name);
12090 }
12091
12092 /* It's easier for the rest of the code if we fake a struct dwo_file and
12093 have dwo_unit "live" in that. At least for now.
12094
12095 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12096 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12097 file, we can combine them back into a virtual DWO file to save space
12098 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12099 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12100
791afaa2
TT
12101 std::string virtual_dwo_name =
12102 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
12103 sections.abbrev.get_id (),
12104 sections.line.get_id (),
12105 sections.loc.get_id (),
12106 sections.str_offsets.get_id ());
80626a55 12107 /* Can we use an existing virtual DWO file? */
976ca316 12108 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12109 comp_dir);
80626a55
DE
12110 /* Create one if necessary. */
12111 if (*dwo_file_slot == NULL)
12112 {
6f738b01
SM
12113 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12114 virtual_dwo_name.c_str ());
12115
51ac9db5 12116 dwo_file = new struct dwo_file;
976ca316 12117 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 12118 dwo_file->comp_dir = comp_dir;
80626a55
DE
12119 dwo_file->sections.abbrev = sections.abbrev;
12120 dwo_file->sections.line = sections.line;
12121 dwo_file->sections.loc = sections.loc;
12122 dwo_file->sections.macinfo = sections.macinfo;
12123 dwo_file->sections.macro = sections.macro;
12124 dwo_file->sections.str_offsets = sections.str_offsets;
12125 /* The "str" section is global to the entire DWP file. */
12126 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12127 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12128 there's no need to record it in dwo_file.
12129 Also, we can't simply record type sections in dwo_file because
12130 we record a pointer into the vector in dwo_unit. As we collect more
12131 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12132 for it, invalidating all copies of pointers into the previous
12133 contents. */
80626a55
DE
12134 *dwo_file_slot = dwo_file;
12135 }
12136 else
12137 {
6f738b01
SM
12138 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12139 virtual_dwo_name.c_str ());
12140
9a3c8263 12141 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12142 }
80626a55 12143
976ca316 12144 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
12145 dwo_unit->dwo_file = dwo_file;
12146 dwo_unit->signature = signature;
8d749320 12147 dwo_unit->section =
976ca316 12148 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 12149 *dwo_unit->section = sections.info_or_types;
57d63ce2 12150 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12151
12152 return dwo_unit;
12153}
12154
d2854d8d
CT
12155/* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12156 simplify them. Given a pointer to the containing section SECTION, and
12157 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12158 virtual section of just that piece. */
73869dc2
DE
12159
12160static struct dwarf2_section_info
d2854d8d
CT
12161create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12162 struct dwarf2_section_info *section,
12163 bfd_size_type offset, bfd_size_type size)
73869dc2
DE
12164{
12165 struct dwarf2_section_info result;
12166 asection *sectp;
12167
12168 gdb_assert (section != NULL);
12169 gdb_assert (!section->is_virtual);
12170
12171 memset (&result, 0, sizeof (result));
12172 result.s.containing_section = section;
dc4ccb6f 12173 result.is_virtual = true;
73869dc2
DE
12174
12175 if (size == 0)
12176 return result;
12177
96b79293 12178 sectp = section->get_bfd_section ();
73869dc2
DE
12179
12180 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12181 bounds of the real section. This is a pretty-rare event, so just
12182 flag an error (easier) instead of a warning and trying to cope. */
12183 if (sectp == NULL
fd361982 12184 || offset + size > bfd_section_size (sectp))
73869dc2 12185 {
d2854d8d 12186 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
73869dc2 12187 " in section %s [in module %s]"),
fd361982 12188 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 12189 objfile_name (per_objfile->objfile));
73869dc2
DE
12190 }
12191
12192 result.virtual_offset = offset;
12193 result.size = size;
12194 return result;
12195}
12196
12197/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12198 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12199 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12200 This is for DWP version 2 files. */
12201
12202static struct dwo_unit *
976ca316 12203create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 12204 struct dwp_file *dwp_file,
73869dc2
DE
12205 uint32_t unit_index,
12206 const char *comp_dir,
12207 ULONGEST signature, int is_debug_types)
12208{
73869dc2
DE
12209 const struct dwp_hash_table *dwp_htab =
12210 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12211 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12212 const char *kind = is_debug_types ? "TU" : "CU";
12213 struct dwo_file *dwo_file;
12214 struct dwo_unit *dwo_unit;
d2854d8d 12215 struct virtual_v2_or_v5_dwo_sections sections;
73869dc2 12216 void **dwo_file_slot;
73869dc2
DE
12217 int i;
12218
12219 gdb_assert (dwp_file->version == 2);
12220
6f738b01
SM
12221 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12222 kind, pulongest (unit_index), hex_string (signature),
12223 dwp_file->name);
73869dc2
DE
12224
12225 /* Fetch the section offsets of this DWO unit. */
12226
12227 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12228
12229 for (i = 0; i < dwp_htab->nr_columns; ++i)
12230 {
12231 uint32_t offset = read_4_bytes (dbfd,
12232 dwp_htab->section_pool.v2.offsets
12233 + (((unit_index - 1) * dwp_htab->nr_columns
12234 + i)
12235 * sizeof (uint32_t)));
12236 uint32_t size = read_4_bytes (dbfd,
12237 dwp_htab->section_pool.v2.sizes
12238 + (((unit_index - 1) * dwp_htab->nr_columns
12239 + i)
12240 * sizeof (uint32_t)));
12241
12242 switch (dwp_htab->section_pool.v2.section_ids[i])
12243 {
12244 case DW_SECT_INFO:
12245 case DW_SECT_TYPES:
12246 sections.info_or_types_offset = offset;
12247 sections.info_or_types_size = size;
12248 break;
12249 case DW_SECT_ABBREV:
12250 sections.abbrev_offset = offset;
12251 sections.abbrev_size = size;
12252 break;
12253 case DW_SECT_LINE:
12254 sections.line_offset = offset;
12255 sections.line_size = size;
12256 break;
12257 case DW_SECT_LOC:
12258 sections.loc_offset = offset;
12259 sections.loc_size = size;
12260 break;
12261 case DW_SECT_STR_OFFSETS:
12262 sections.str_offsets_offset = offset;
12263 sections.str_offsets_size = size;
12264 break;
12265 case DW_SECT_MACINFO:
12266 sections.macinfo_offset = offset;
12267 sections.macinfo_size = size;
12268 break;
12269 case DW_SECT_MACRO:
12270 sections.macro_offset = offset;
12271 sections.macro_size = size;
12272 break;
12273 }
12274 }
12275
12276 /* It's easier for the rest of the code if we fake a struct dwo_file and
12277 have dwo_unit "live" in that. At least for now.
12278
12279 The DWP file can be made up of a random collection of CUs and TUs.
12280 However, for each CU + set of TUs that came from the same original DWO
12281 file, we can combine them back into a virtual DWO file to save space
12282 (fewer struct dwo_file objects to allocate). Remember that for really
12283 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12284
791afaa2
TT
12285 std::string virtual_dwo_name =
12286 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12287 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12288 (long) (sections.line_size ? sections.line_offset : 0),
12289 (long) (sections.loc_size ? sections.loc_offset : 0),
12290 (long) (sections.str_offsets_size
12291 ? sections.str_offsets_offset : 0));
73869dc2 12292 /* Can we use an existing virtual DWO file? */
976ca316 12293 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12294 comp_dir);
73869dc2
DE
12295 /* Create one if necessary. */
12296 if (*dwo_file_slot == NULL)
12297 {
6f738b01
SM
12298 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12299 virtual_dwo_name.c_str ());
12300
51ac9db5 12301 dwo_file = new struct dwo_file;
976ca316 12302 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12303 dwo_file->comp_dir = comp_dir;
12304 dwo_file->sections.abbrev =
d2854d8d
CT
12305 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12306 sections.abbrev_offset,
12307 sections.abbrev_size);
73869dc2 12308 dwo_file->sections.line =
d2854d8d
CT
12309 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12310 sections.line_offset,
12311 sections.line_size);
73869dc2 12312 dwo_file->sections.loc =
d2854d8d
CT
12313 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12314 sections.loc_offset, sections.loc_size);
73869dc2 12315 dwo_file->sections.macinfo =
d2854d8d
CT
12316 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12317 sections.macinfo_offset,
12318 sections.macinfo_size);
73869dc2 12319 dwo_file->sections.macro =
d2854d8d
CT
12320 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12321 sections.macro_offset,
12322 sections.macro_size);
73869dc2 12323 dwo_file->sections.str_offsets =
d2854d8d
CT
12324 create_dwp_v2_or_v5_section (per_objfile,
12325 &dwp_file->sections.str_offsets,
12326 sections.str_offsets_offset,
12327 sections.str_offsets_size);
73869dc2
DE
12328 /* The "str" section is global to the entire DWP file. */
12329 dwo_file->sections.str = dwp_file->sections.str;
12330 /* The info or types section is assigned below to dwo_unit,
12331 there's no need to record it in dwo_file.
12332 Also, we can't simply record type sections in dwo_file because
12333 we record a pointer into the vector in dwo_unit. As we collect more
12334 types we'll grow the vector and eventually have to reallocate space
12335 for it, invalidating all copies of pointers into the previous
12336 contents. */
12337 *dwo_file_slot = dwo_file;
12338 }
12339 else
12340 {
6f738b01
SM
12341 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12342 virtual_dwo_name.c_str ());
12343
9a3c8263 12344 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12345 }
73869dc2 12346
976ca316 12347 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12348 dwo_unit->dwo_file = dwo_file;
12349 dwo_unit->signature = signature;
8d749320 12350 dwo_unit->section =
976ca316 12351 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
d2854d8d 12352 *dwo_unit->section = create_dwp_v2_or_v5_section
dda83cd7 12353 (per_objfile,
d2854d8d
CT
12354 is_debug_types
12355 ? &dwp_file->sections.types
12356 : &dwp_file->sections.info,
12357 sections.info_or_types_offset,
12358 sections.info_or_types_size);
12359 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12360
12361 return dwo_unit;
12362}
12363
12364/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12365 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12366 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12367 This is for DWP version 5 files. */
12368
12369static struct dwo_unit *
12370create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12371 struct dwp_file *dwp_file,
12372 uint32_t unit_index,
12373 const char *comp_dir,
12374 ULONGEST signature, int is_debug_types)
12375{
12376 const struct dwp_hash_table *dwp_htab
12377 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12378 bfd *dbfd = dwp_file->dbfd.get ();
12379 const char *kind = is_debug_types ? "TU" : "CU";
12380 struct dwo_file *dwo_file;
12381 struct dwo_unit *dwo_unit;
12382 struct virtual_v2_or_v5_dwo_sections sections {};
12383 void **dwo_file_slot;
12384
12385 gdb_assert (dwp_file->version == 5);
12386
6f738b01
SM
12387 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12388 kind, pulongest (unit_index), hex_string (signature),
12389 dwp_file->name);
d2854d8d
CT
12390
12391 /* Fetch the section offsets of this DWO unit. */
12392
12393 /* memset (&sections, 0, sizeof (sections)); */
12394
12395 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12396 {
12397 uint32_t offset = read_4_bytes (dbfd,
dda83cd7
SM
12398 dwp_htab->section_pool.v5.offsets
12399 + (((unit_index - 1)
12400 * dwp_htab->nr_columns
12401 + i)
12402 * sizeof (uint32_t)));
d2854d8d 12403 uint32_t size = read_4_bytes (dbfd,
dda83cd7
SM
12404 dwp_htab->section_pool.v5.sizes
12405 + (((unit_index - 1) * dwp_htab->nr_columns
12406 + i)
12407 * sizeof (uint32_t)));
d2854d8d
CT
12408
12409 switch (dwp_htab->section_pool.v5.section_ids[i])
dda83cd7
SM
12410 {
12411 case DW_SECT_ABBREV_V5:
12412 sections.abbrev_offset = offset;
12413 sections.abbrev_size = size;
12414 break;
12415 case DW_SECT_INFO_V5:
12416 sections.info_or_types_offset = offset;
12417 sections.info_or_types_size = size;
12418 break;
12419 case DW_SECT_LINE_V5:
12420 sections.line_offset = offset;
12421 sections.line_size = size;
12422 break;
12423 case DW_SECT_LOCLISTS_V5:
12424 sections.loclists_offset = offset;
12425 sections.loclists_size = size;
12426 break;
12427 case DW_SECT_MACRO_V5:
12428 sections.macro_offset = offset;
12429 sections.macro_size = size;
12430 break;
12431 case DW_SECT_RNGLISTS_V5:
12432 sections.rnglists_offset = offset;
12433 sections.rnglists_size = size;
12434 break;
12435 case DW_SECT_STR_OFFSETS_V5:
12436 sections.str_offsets_offset = offset;
12437 sections.str_offsets_size = size;
12438 break;
12439 case DW_SECT_RESERVED_V5:
12440 default:
12441 break;
12442 }
d2854d8d
CT
12443 }
12444
12445 /* It's easier for the rest of the code if we fake a struct dwo_file and
12446 have dwo_unit "live" in that. At least for now.
12447
12448 The DWP file can be made up of a random collection of CUs and TUs.
12449 However, for each CU + set of TUs that came from the same original DWO
12450 file, we can combine them back into a virtual DWO file to save space
12451 (fewer struct dwo_file objects to allocate). Remember that for really
12452 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12453
12454 std::string virtual_dwo_name =
12455 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
dda83cd7
SM
12456 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12457 (long) (sections.line_size ? sections.line_offset : 0),
12458 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12459 (long) (sections.str_offsets_size
12460 ? sections.str_offsets_offset : 0),
12461 (long) (sections.macro_size ? sections.macro_offset : 0),
12462 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
d2854d8d
CT
12463 /* Can we use an existing virtual DWO file? */
12464 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
dda83cd7
SM
12465 virtual_dwo_name.c_str (),
12466 comp_dir);
d2854d8d
CT
12467 /* Create one if necessary. */
12468 if (*dwo_file_slot == NULL)
12469 {
6f738b01
SM
12470 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12471 virtual_dwo_name.c_str ());
12472
d2854d8d
CT
12473 dwo_file = new struct dwo_file;
12474 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12475 dwo_file->comp_dir = comp_dir;
12476 dwo_file->sections.abbrev =
dda83cd7
SM
12477 create_dwp_v2_or_v5_section (per_objfile,
12478 &dwp_file->sections.abbrev,
12479 sections.abbrev_offset,
12480 sections.abbrev_size);
d2854d8d 12481 dwo_file->sections.line =
dda83cd7
SM
12482 create_dwp_v2_or_v5_section (per_objfile,
12483 &dwp_file->sections.line,
12484 sections.line_offset, sections.line_size);
d2854d8d 12485 dwo_file->sections.macro =
dda83cd7
SM
12486 create_dwp_v2_or_v5_section (per_objfile,
12487 &dwp_file->sections.macro,
12488 sections.macro_offset,
12489 sections.macro_size);
d2854d8d 12490 dwo_file->sections.loclists =
dda83cd7
SM
12491 create_dwp_v2_or_v5_section (per_objfile,
12492 &dwp_file->sections.loclists,
12493 sections.loclists_offset,
12494 sections.loclists_size);
d2854d8d 12495 dwo_file->sections.rnglists =
dda83cd7
SM
12496 create_dwp_v2_or_v5_section (per_objfile,
12497 &dwp_file->sections.rnglists,
12498 sections.rnglists_offset,
12499 sections.rnglists_size);
d2854d8d 12500 dwo_file->sections.str_offsets =
dda83cd7
SM
12501 create_dwp_v2_or_v5_section (per_objfile,
12502 &dwp_file->sections.str_offsets,
12503 sections.str_offsets_offset,
12504 sections.str_offsets_size);
d2854d8d
CT
12505 /* The "str" section is global to the entire DWP file. */
12506 dwo_file->sections.str = dwp_file->sections.str;
12507 /* The info or types section is assigned below to dwo_unit,
dda83cd7
SM
12508 there's no need to record it in dwo_file.
12509 Also, we can't simply record type sections in dwo_file because
12510 we record a pointer into the vector in dwo_unit. As we collect more
12511 types we'll grow the vector and eventually have to reallocate space
12512 for it, invalidating all copies of pointers into the previous
12513 contents. */
d2854d8d
CT
12514 *dwo_file_slot = dwo_file;
12515 }
12516 else
12517 {
6f738b01
SM
12518 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12519 virtual_dwo_name.c_str ());
12520
d2854d8d
CT
12521 dwo_file = (struct dwo_file *) *dwo_file_slot;
12522 }
12523
12524 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12525 dwo_unit->dwo_file = dwo_file;
12526 dwo_unit->signature = signature;
12527 dwo_unit->section
12528 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12529 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
dda83cd7
SM
12530 &dwp_file->sections.info,
12531 sections.info_or_types_offset,
12532 sections.info_or_types_size);
73869dc2
DE
12533 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12534
12535 return dwo_unit;
12536}
12537
57d63ce2
DE
12538/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12539 Returns NULL if the signature isn't found. */
80626a55
DE
12540
12541static struct dwo_unit *
976ca316 12542lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12543 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12544 ULONGEST signature, int is_debug_types)
80626a55 12545{
57d63ce2
DE
12546 const struct dwp_hash_table *dwp_htab =
12547 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12548 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12549 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12550 uint32_t hash = signature & mask;
12551 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12552 unsigned int i;
12553 void **slot;
870f88f7 12554 struct dwo_unit find_dwo_cu;
80626a55
DE
12555
12556 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12557 find_dwo_cu.signature = signature;
19ac8c2e 12558 slot = htab_find_slot (is_debug_types
48b490f2
TT
12559 ? dwp_file->loaded_tus.get ()
12560 : dwp_file->loaded_cus.get (),
19ac8c2e 12561 &find_dwo_cu, INSERT);
80626a55
DE
12562
12563 if (*slot != NULL)
9a3c8263 12564 return (struct dwo_unit *) *slot;
80626a55
DE
12565
12566 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12567 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12568 {
12569 ULONGEST signature_in_table;
12570
12571 signature_in_table =
57d63ce2 12572 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12573 if (signature_in_table == signature)
12574 {
57d63ce2
DE
12575 uint32_t unit_index =
12576 read_4_bytes (dbfd,
12577 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12578
73869dc2
DE
12579 if (dwp_file->version == 1)
12580 {
976ca316
SM
12581 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12582 unit_index, comp_dir,
12583 signature, is_debug_types);
73869dc2 12584 }
d2854d8d 12585 else if (dwp_file->version == 2)
73869dc2 12586 {
976ca316
SM
12587 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12588 unit_index, comp_dir,
12589 signature, is_debug_types);
73869dc2 12590 }
d2854d8d
CT
12591 else /* version == 5 */
12592 {
12593 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12594 unit_index, comp_dir,
12595 signature, is_debug_types);
12596 }
9a3c8263 12597 return (struct dwo_unit *) *slot;
80626a55
DE
12598 }
12599 if (signature_in_table == 0)
12600 return NULL;
12601 hash = (hash + hash2) & mask;
12602 }
12603
12604 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12605 " [in module %s]"),
12606 dwp_file->name);
12607}
12608
ab5088bf 12609/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12610 Open the file specified by FILE_NAME and hand it off to BFD for
12611 preliminary analysis. Return a newly initialized bfd *, which
12612 includes a canonicalized copy of FILE_NAME.
80626a55 12613 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12614 SEARCH_CWD is true if the current directory is to be searched.
12615 It will be searched before debug-file-directory.
13aaf454
DE
12616 If successful, the file is added to the bfd include table of the
12617 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12618 If unable to find/open the file, return NULL.
3019eac3
DE
12619 NOTE: This function is derived from symfile_bfd_open. */
12620
192b62ce 12621static gdb_bfd_ref_ptr
976ca316 12622try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12623 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12624{
24b9144d 12625 int desc;
9c02c129
DE
12626 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12627 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12628 to debug_file_directory. */
e0cc99a6 12629 const char *search_path;
9c02c129
DE
12630 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12631
e0cc99a6 12632 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12633 if (search_cwd)
12634 {
12635 if (*debug_file_directory != '\0')
e0cc99a6
TT
12636 {
12637 search_path_holder.reset (concat (".", dirname_separator_string,
12638 debug_file_directory,
12639 (char *) NULL));
12640 search_path = search_path_holder.get ();
12641 }
6ac97d4c 12642 else
e0cc99a6 12643 search_path = ".";
6ac97d4c 12644 }
9c02c129 12645 else
e0cc99a6 12646 search_path = debug_file_directory;
3019eac3 12647
24b9144d 12648 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12649 if (is_dwp)
12650 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12651
12652 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12653 desc = openp (search_path, flags, file_name,
3019eac3
DE
12654 O_RDONLY | O_BINARY, &absolute_name);
12655 if (desc < 0)
12656 return NULL;
12657
e0cc99a6
TT
12658 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12659 gnutarget, desc));
9c02c129
DE
12660 if (sym_bfd == NULL)
12661 return NULL;
192b62ce 12662 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12663
192b62ce
TT
12664 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12665 return NULL;
3019eac3 12666
13aaf454
DE
12667 /* Success. Record the bfd as having been included by the objfile's bfd.
12668 This is important because things like demangled_names_hash lives in the
12669 objfile's per_bfd space and may have references to things like symbol
12670 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12671 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12672
3019eac3
DE
12673 return sym_bfd;
12674}
12675
ab5088bf 12676/* Try to open DWO file FILE_NAME.
3019eac3
DE
12677 COMP_DIR is the DW_AT_comp_dir attribute.
12678 The result is the bfd handle of the file.
12679 If there is a problem finding or opening the file, return NULL.
12680 Upon success, the canonicalized path of the file is stored in the bfd,
12681 same as symfile_bfd_open. */
12682
192b62ce 12683static gdb_bfd_ref_ptr
976ca316 12684open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12685 const char *file_name, const char *comp_dir)
3019eac3 12686{
80626a55 12687 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12688 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12689 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12690
12691 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12692
12693 if (comp_dir != NULL)
12694 {
43816ebc
TT
12695 gdb::unique_xmalloc_ptr<char> path_to_try
12696 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12697
12698 /* NOTE: If comp_dir is a relative path, this will also try the
12699 search path, which seems useful. */
976ca316 12700 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12701 0 /*is_dwp*/,
192b62ce 12702 1 /*search_cwd*/));
3019eac3
DE
12703 if (abfd != NULL)
12704 return abfd;
12705 }
12706
12707 /* That didn't work, try debug-file-directory, which, despite its name,
12708 is a list of paths. */
12709
12710 if (*debug_file_directory == '\0')
12711 return NULL;
12712
976ca316 12713 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12714 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12715}
12716
80626a55
DE
12717/* This function is mapped across the sections and remembers the offset and
12718 size of each of the DWO debugging sections we are interested in. */
12719
12720static void
5bb6e9dd
TT
12721dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12722 dwo_sections *dwo_sections)
80626a55 12723{
80626a55
DE
12724 const struct dwop_section_names *names = &dwop_section_names;
12725
12726 if (section_is_p (sectp->name, &names->abbrev_dwo))
12727 {
049412e3 12728 dwo_sections->abbrev.s.section = sectp;
fd361982 12729 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12730 }
12731 else if (section_is_p (sectp->name, &names->info_dwo))
12732 {
049412e3 12733 dwo_sections->info.s.section = sectp;
fd361982 12734 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12735 }
12736 else if (section_is_p (sectp->name, &names->line_dwo))
12737 {
049412e3 12738 dwo_sections->line.s.section = sectp;
fd361982 12739 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12740 }
12741 else if (section_is_p (sectp->name, &names->loc_dwo))
12742 {
049412e3 12743 dwo_sections->loc.s.section = sectp;
fd361982 12744 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12745 }
41144253 12746 else if (section_is_p (sectp->name, &names->loclists_dwo))
12747 {
12748 dwo_sections->loclists.s.section = sectp;
12749 dwo_sections->loclists.size = bfd_section_size (sectp);
12750 }
80626a55
DE
12751 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12752 {
049412e3 12753 dwo_sections->macinfo.s.section = sectp;
fd361982 12754 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12755 }
12756 else if (section_is_p (sectp->name, &names->macro_dwo))
12757 {
049412e3 12758 dwo_sections->macro.s.section = sectp;
fd361982 12759 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55 12760 }
d0ce17d8
CT
12761 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12762 {
12763 dwo_sections->rnglists.s.section = sectp;
12764 dwo_sections->rnglists.size = bfd_section_size (sectp);
12765 }
80626a55
DE
12766 else if (section_is_p (sectp->name, &names->str_dwo))
12767 {
049412e3 12768 dwo_sections->str.s.section = sectp;
fd361982 12769 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12770 }
12771 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12772 {
049412e3 12773 dwo_sections->str_offsets.s.section = sectp;
fd361982 12774 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12775 }
12776 else if (section_is_p (sectp->name, &names->types_dwo))
12777 {
12778 struct dwarf2_section_info type_section;
12779
12780 memset (&type_section, 0, sizeof (type_section));
049412e3 12781 type_section.s.section = sectp;
fd361982 12782 type_section.size = bfd_section_size (sectp);
fd5866f6 12783 dwo_sections->types.push_back (type_section);
80626a55
DE
12784 }
12785}
12786
ab5088bf 12787/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12788 by PER_CU. This is for the non-DWP case.
80626a55 12789 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12790
12791static struct dwo_file *
4ab09049
SM
12792open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12793 const char *comp_dir)
3019eac3 12794{
976ca316 12795 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12796
976ca316 12797 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12798 if (dbfd == NULL)
12799 {
6f738b01
SM
12800 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12801
80626a55
DE
12802 return NULL;
12803 }
263db9a1 12804
51ac9db5 12805 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12806 dwo_file->dwo_name = dwo_name;
12807 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12808 dwo_file->dbfd = std::move (dbfd);
3019eac3 12809
5bb6e9dd
TT
12810 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12811 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12812 &dwo_file->sections);
3019eac3 12813
976ca316
SM
12814 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12815 dwo_file->cus);
3019eac3 12816
d2854d8d
CT
12817 if (cu->per_cu->dwarf_version < 5)
12818 {
12819 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12820 dwo_file->sections.types, dwo_file->tus);
12821 }
12822 else
12823 {
12824 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12825 &dwo_file->sections.info, dwo_file->tus,
12826 rcuh_kind::TYPE);
12827 }
3019eac3 12828
6f738b01 12829 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
80626a55 12830
263db9a1 12831 return dwo_file.release ();
3019eac3
DE
12832}
12833
80626a55 12834/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12835 size of each of the DWP debugging sections common to version 1 and 2 that
12836 we are interested in. */
3019eac3 12837
80626a55 12838static void
73869dc2 12839dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
5bb6e9dd 12840 dwp_file *dwp_file)
3019eac3 12841{
80626a55
DE
12842 const struct dwop_section_names *names = &dwop_section_names;
12843 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12844
80626a55 12845 /* Record the ELF section number for later lookup: this is what the
73869dc2 12846 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12847 gdb_assert (elf_section_nr < dwp_file->num_sections);
12848 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12849
80626a55
DE
12850 /* Look for specific sections that we need. */
12851 if (section_is_p (sectp->name, &names->str_dwo))
12852 {
049412e3 12853 dwp_file->sections.str.s.section = sectp;
fd361982 12854 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12855 }
12856 else if (section_is_p (sectp->name, &names->cu_index))
12857 {
049412e3 12858 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12859 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12860 }
12861 else if (section_is_p (sectp->name, &names->tu_index))
12862 {
049412e3 12863 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12864 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12865 }
12866}
3019eac3 12867
73869dc2
DE
12868/* This function is mapped across the sections and remembers the offset and
12869 size of each of the DWP version 2 debugging sections that we are interested
12870 in. This is split into a separate function because we don't know if we
d2854d8d 12871 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
73869dc2
DE
12872
12873static void
12874dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12875{
9a3c8263 12876 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12877 const struct dwop_section_names *names = &dwop_section_names;
12878 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12879
12880 /* Record the ELF section number for later lookup: this is what the
12881 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12882 gdb_assert (elf_section_nr < dwp_file->num_sections);
12883 dwp_file->elf_sections[elf_section_nr] = sectp;
12884
12885 /* Look for specific sections that we need. */
12886 if (section_is_p (sectp->name, &names->abbrev_dwo))
12887 {
049412e3 12888 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12889 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12890 }
12891 else if (section_is_p (sectp->name, &names->info_dwo))
12892 {
049412e3 12893 dwp_file->sections.info.s.section = sectp;
fd361982 12894 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12895 }
12896 else if (section_is_p (sectp->name, &names->line_dwo))
12897 {
049412e3 12898 dwp_file->sections.line.s.section = sectp;
fd361982 12899 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12900 }
12901 else if (section_is_p (sectp->name, &names->loc_dwo))
12902 {
049412e3 12903 dwp_file->sections.loc.s.section = sectp;
fd361982 12904 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12905 }
12906 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12907 {
049412e3 12908 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12909 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12910 }
12911 else if (section_is_p (sectp->name, &names->macro_dwo))
12912 {
049412e3 12913 dwp_file->sections.macro.s.section = sectp;
fd361982 12914 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12915 }
12916 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12917 {
049412e3 12918 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12919 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12920 }
12921 else if (section_is_p (sectp->name, &names->types_dwo))
12922 {
049412e3 12923 dwp_file->sections.types.s.section = sectp;
fd361982 12924 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12925 }
12926}
12927
d2854d8d
CT
12928/* This function is mapped across the sections and remembers the offset and
12929 size of each of the DWP version 5 debugging sections that we are interested
12930 in. This is split into a separate function because we don't know if we
12931 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12932
12933static void
12934dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12935{
12936 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12937 const struct dwop_section_names *names = &dwop_section_names;
12938 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12939
12940 /* Record the ELF section number for later lookup: this is what the
12941 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12942 gdb_assert (elf_section_nr < dwp_file->num_sections);
12943 dwp_file->elf_sections[elf_section_nr] = sectp;
12944
12945 /* Look for specific sections that we need. */
12946 if (section_is_p (sectp->name, &names->abbrev_dwo))
12947 {
12948 dwp_file->sections.abbrev.s.section = sectp;
12949 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12950 }
12951 else if (section_is_p (sectp->name, &names->info_dwo))
12952 {
12953 dwp_file->sections.info.s.section = sectp;
12954 dwp_file->sections.info.size = bfd_section_size (sectp);
12955 }
12956 else if (section_is_p (sectp->name, &names->line_dwo))
12957 {
12958 dwp_file->sections.line.s.section = sectp;
12959 dwp_file->sections.line.size = bfd_section_size (sectp);
12960 }
12961 else if (section_is_p (sectp->name, &names->loclists_dwo))
12962 {
12963 dwp_file->sections.loclists.s.section = sectp;
12964 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12965 }
12966 else if (section_is_p (sectp->name, &names->macro_dwo))
12967 {
12968 dwp_file->sections.macro.s.section = sectp;
12969 dwp_file->sections.macro.size = bfd_section_size (sectp);
12970 }
12971 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12972 {
12973 dwp_file->sections.rnglists.s.section = sectp;
12974 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12975 }
12976 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12977 {
12978 dwp_file->sections.str_offsets.s.section = sectp;
12979 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12980 }
12981}
12982
80626a55 12983/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12984
80626a55
DE
12985static hashval_t
12986hash_dwp_loaded_cutus (const void *item)
12987{
9a3c8263 12988 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12989
80626a55
DE
12990 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12991 return dwo_unit->signature;
3019eac3
DE
12992}
12993
80626a55 12994/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12995
80626a55
DE
12996static int
12997eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12998{
9a3c8263
SM
12999 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13000 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13001
80626a55
DE
13002 return dua->signature == dub->signature;
13003}
3019eac3 13004
80626a55 13005/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13006
48b490f2 13007static htab_up
298e9637 13008allocate_dwp_loaded_cutus_table ()
80626a55 13009{
48b490f2
TT
13010 return htab_up (htab_create_alloc (3,
13011 hash_dwp_loaded_cutus,
13012 eq_dwp_loaded_cutus,
13013 NULL, xcalloc, xfree));
80626a55 13014}
3019eac3 13015
ab5088bf
DE
13016/* Try to open DWP file FILE_NAME.
13017 The result is the bfd handle of the file.
13018 If there is a problem finding or opening the file, return NULL.
13019 Upon success, the canonicalized path of the file is stored in the bfd,
13020 same as symfile_bfd_open. */
13021
192b62ce 13022static gdb_bfd_ref_ptr
976ca316 13023open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 13024{
976ca316 13025 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 13026 1 /*is_dwp*/,
192b62ce 13027 1 /*search_cwd*/));
6ac97d4c
DE
13028 if (abfd != NULL)
13029 return abfd;
13030
13031 /* Work around upstream bug 15652.
13032 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13033 [Whether that's a "bug" is debatable, but it is getting in our way.]
13034 We have no real idea where the dwp file is, because gdb's realpath-ing
13035 of the executable's path may have discarded the needed info.
13036 [IWBN if the dwp file name was recorded in the executable, akin to
13037 .gnu_debuglink, but that doesn't exist yet.]
13038 Strip the directory from FILE_NAME and search again. */
13039 if (*debug_file_directory != '\0')
13040 {
13041 /* Don't implicitly search the current directory here.
13042 If the user wants to search "." to handle this case,
13043 it must be added to debug-file-directory. */
976ca316
SM
13044 return try_open_dwop_file (per_objfile, lbasename (file_name),
13045 1 /*is_dwp*/,
6ac97d4c
DE
13046 0 /*search_cwd*/);
13047 }
13048
13049 return NULL;
ab5088bf
DE
13050}
13051
80626a55
DE
13052/* Initialize the use of the DWP file for the current objfile.
13053 By convention the name of the DWP file is ${objfile}.dwp.
13054 The result is NULL if it can't be found. */
a766d390 13055
400174b1 13056static std::unique_ptr<struct dwp_file>
976ca316 13057open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 13058{
976ca316 13059 struct objfile *objfile = per_objfile->objfile;
80626a55 13060
82bf32bc
JK
13061 /* Try to find first .dwp for the binary file before any symbolic links
13062 resolving. */
6c447423
DE
13063
13064 /* If the objfile is a debug file, find the name of the real binary
13065 file and get the name of dwp file from there. */
d721ba37 13066 std::string dwp_name;
6c447423
DE
13067 if (objfile->separate_debug_objfile_backlink != NULL)
13068 {
13069 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13070 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13071
d721ba37 13072 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13073 }
13074 else
d721ba37
PA
13075 dwp_name = objfile->original_name;
13076
13077 dwp_name += ".dwp";
80626a55 13078
976ca316 13079 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13080 if (dbfd == NULL
13081 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13082 {
13083 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13084 dwp_name = objfile_name (objfile);
13085 dwp_name += ".dwp";
976ca316 13086 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
13087 }
13088
80626a55
DE
13089 if (dbfd == NULL)
13090 {
6f738b01
SM
13091 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13092
400174b1 13093 return std::unique_ptr<dwp_file> ();
3019eac3 13094 }
400174b1
TT
13095
13096 const char *name = bfd_get_filename (dbfd.get ());
13097 std::unique_ptr<struct dwp_file> dwp_file
13098 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13099
0a0f4c01 13100 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 13101 dwp_file->elf_sections =
976ca316 13102 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
13103 dwp_file->num_sections, asection *);
13104
5bb6e9dd
TT
13105 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13106 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13107 dwp_file.get ());
80626a55 13108
976ca316 13109 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 13110
976ca316 13111 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 13112
73869dc2 13113 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13114 if (dwp_file->cus && dwp_file->tus
13115 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13116 {
13117 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13118 pretty bizarre. We use pulongest here because that's the established
4d65956b 13119 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13120 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13121 " TU version %s [in DWP file %s]"),
13122 pulongest (dwp_file->cus->version),
d721ba37 13123 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13124 }
08302ed2
DE
13125
13126 if (dwp_file->cus)
13127 dwp_file->version = dwp_file->cus->version;
13128 else if (dwp_file->tus)
13129 dwp_file->version = dwp_file->tus->version;
13130 else
13131 dwp_file->version = 2;
73869dc2 13132
5bb6e9dd
TT
13133 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13134 {
13135 if (dwp_file->version == 2)
13136 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13137 dwp_file.get ());
13138 else
13139 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13140 dwp_file.get ());
13141 }
73869dc2 13142
298e9637
SM
13143 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13144 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 13145
6f738b01
SM
13146 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13147 dwarf_read_debug_printf (" %s CUs, %s TUs",
13148 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13149 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13150
13151 return dwp_file;
3019eac3 13152}
c906108c 13153
ab5088bf
DE
13154/* Wrapper around open_and_init_dwp_file, only open it once. */
13155
13156static struct dwp_file *
976ca316 13157get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 13158{
976ca316 13159 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 13160 {
976ca316
SM
13161 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13162 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 13163 }
976ca316 13164 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
13165}
13166
80626a55
DE
13167/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13168 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13169 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13170 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13171 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13172
13173 This is called, for example, when wanting to read a variable with a
13174 complex location. Therefore we don't want to do file i/o for every call.
13175 Therefore we don't want to look for a DWO file on every call.
13176 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13177 then we check if we've already seen DWO_NAME, and only THEN do we check
13178 for a DWO file.
13179
1c658ad5 13180 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13181 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13182
3019eac3 13183static struct dwo_unit *
4ab09049 13184lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 13185 ULONGEST signature, int is_debug_types)
3019eac3 13186{
976ca316
SM
13187 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13188 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
13189 const char *kind = is_debug_types ? "TU" : "CU";
13190 void **dwo_file_slot;
3019eac3 13191 struct dwo_file *dwo_file;
80626a55 13192 struct dwp_file *dwp_file;
cb1df416 13193
6a506a2d
DE
13194 /* First see if there's a DWP file.
13195 If we have a DWP file but didn't find the DWO inside it, don't
13196 look for the original DWO file. It makes gdb behave differently
13197 depending on whether one is debugging in the build tree. */
cf2c3c16 13198
976ca316 13199 dwp_file = get_dwp_file (per_objfile);
80626a55 13200 if (dwp_file != NULL)
cf2c3c16 13201 {
80626a55
DE
13202 const struct dwp_hash_table *dwp_htab =
13203 is_debug_types ? dwp_file->tus : dwp_file->cus;
13204
13205 if (dwp_htab != NULL)
13206 {
13207 struct dwo_unit *dwo_cutu =
976ca316
SM
13208 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13209 is_debug_types);
80626a55
DE
13210
13211 if (dwo_cutu != NULL)
13212 {
6f738b01
SM
13213 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13214 kind, hex_string (signature),
13215 host_address_to_string (dwo_cutu));
13216
80626a55
DE
13217 return dwo_cutu;
13218 }
13219 }
13220 }
6a506a2d 13221 else
80626a55 13222 {
6a506a2d 13223 /* No DWP file, look for the DWO file. */
80626a55 13224
976ca316 13225 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 13226 if (*dwo_file_slot == NULL)
80626a55 13227 {
6a506a2d 13228 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 13229 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 13230 }
6a506a2d 13231 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13232 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13233
6a506a2d 13234 if (dwo_file != NULL)
19c3d4c9 13235 {
6a506a2d
DE
13236 struct dwo_unit *dwo_cutu = NULL;
13237
13238 if (is_debug_types && dwo_file->tus)
13239 {
13240 struct dwo_unit find_dwo_cutu;
13241
13242 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13243 find_dwo_cutu.signature = signature;
9a3c8263 13244 dwo_cutu
b0b6a987
TT
13245 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13246 &find_dwo_cutu);
6a506a2d 13247 }
33c5cd75 13248 else if (!is_debug_types && dwo_file->cus)
80626a55 13249 {
33c5cd75
DB
13250 struct dwo_unit find_dwo_cutu;
13251
13252 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13253 find_dwo_cutu.signature = signature;
b0b6a987 13254 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 13255 &find_dwo_cutu);
6a506a2d
DE
13256 }
13257
13258 if (dwo_cutu != NULL)
13259 {
6f738b01
SM
13260 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13261 kind, dwo_name, hex_string (signature),
13262 host_address_to_string (dwo_cutu));
13263
6a506a2d 13264 return dwo_cutu;
80626a55
DE
13265 }
13266 }
2e276125 13267 }
9cdd5dbd 13268
80626a55
DE
13269 /* We didn't find it. This could mean a dwo_id mismatch, or
13270 someone deleted the DWO/DWP file, or the search path isn't set up
13271 correctly to find the file. */
13272
6f738b01
SM
13273 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13274 kind, dwo_name, hex_string (signature));
3019eac3 13275
6656a72d
DE
13276 /* This is a warning and not a complaint because it can be caused by
13277 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13278 {
13279 /* Print the name of the DWP file if we looked there, helps the user
13280 better diagnose the problem. */
791afaa2 13281 std::string dwp_text;
43942612
DE
13282
13283 if (dwp_file != NULL)
791afaa2
TT
13284 dwp_text = string_printf (" [in DWP file %s]",
13285 lbasename (dwp_file->name));
43942612 13286
9d8780f0 13287 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 13288 " [in module %s]"),
4ab09049
SM
13289 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13290 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 13291 }
3019eac3 13292 return NULL;
5fb290d7
DJ
13293}
13294
80626a55
DE
13295/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13296 See lookup_dwo_cutu_unit for details. */
13297
13298static struct dwo_unit *
4ab09049 13299lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
13300 ULONGEST signature)
13301{
4ab09049
SM
13302 gdb_assert (!cu->per_cu->is_debug_types);
13303
13304 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
13305}
13306
13307/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13308 See lookup_dwo_cutu_unit for details. */
13309
13310static struct dwo_unit *
4ab09049 13311lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 13312{
4ab09049
SM
13313 gdb_assert (cu->per_cu->is_debug_types);
13314
13315 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13316
13317 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
13318}
13319
89e63ee4
DE
13320/* Traversal function for queue_and_load_all_dwo_tus. */
13321
13322static int
13323queue_and_load_dwo_tu (void **slot, void *info)
13324{
13325 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 13326 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 13327 ULONGEST signature = dwo_unit->signature;
d460f660 13328 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
13329
13330 if (sig_type != NULL)
13331 {
13332 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13333
13334 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13335 a real dependency of PER_CU on SIG_TYPE. That is detected later
13336 while processing PER_CU. */
120ce1b5 13337 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
13338 load_full_type_unit (sig_cu, cu->per_objfile);
13339 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13340 }
13341
13342 return 1;
13343}
13344
1b555f17 13345/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
13346 The DWO may have the only definition of the type, though it may not be
13347 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13348 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13349
13350static void
1b555f17 13351queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
13352{
13353 struct dwo_unit *dwo_unit;
13354 struct dwo_file *dwo_file;
13355
1b555f17
SM
13356 gdb_assert (cu != nullptr);
13357 gdb_assert (!cu->per_cu->is_debug_types);
13358 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 13359
1b555f17 13360 dwo_unit = cu->dwo_unit;
89e63ee4
DE
13361 gdb_assert (dwo_unit != NULL);
13362
13363 dwo_file = dwo_unit->dwo_file;
13364 if (dwo_file->tus != NULL)
1b555f17 13365 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
13366}
13367
3019eac3 13368/* Read in various DIEs. */
348e048f 13369
d389af10 13370/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13371 Inherit only the children of the DW_AT_abstract_origin DIE not being
13372 already referenced by DW_AT_abstract_origin from the children of the
13373 current DIE. */
d389af10
JK
13374
13375static void
13376inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13377{
13378 struct die_info *child_die;
791afaa2 13379 sect_offset *offsetp;
d389af10
JK
13380 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13381 struct die_info *origin_die;
13382 /* Iterator of the ORIGIN_DIE children. */
13383 struct die_info *origin_child_die;
d389af10 13384 struct attribute *attr;
cd02d79d
PA
13385 struct dwarf2_cu *origin_cu;
13386 struct pending **origin_previous_list_in_scope;
d389af10
JK
13387
13388 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13389 if (!attr)
13390 return;
13391
cd02d79d
PA
13392 /* Note that following die references may follow to a die in a
13393 different cu. */
13394
13395 origin_cu = cu;
13396 origin_die = follow_die_ref (die, attr, &origin_cu);
13397
13398 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13399 symbols in. */
13400 origin_previous_list_in_scope = origin_cu->list_in_scope;
13401 origin_cu->list_in_scope = cu->list_in_scope;
13402
edb3359d
DJ
13403 if (die->tag != origin_die->tag
13404 && !(die->tag == DW_TAG_inlined_subroutine
13405 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13406 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13407 sect_offset_str (die->sect_off),
13408 sect_offset_str (origin_die->sect_off));
d389af10 13409
791afaa2 13410 std::vector<sect_offset> offsets;
d389af10 13411
3ea89b92
PMR
13412 for (child_die = die->child;
13413 child_die && child_die->tag;
436c571c 13414 child_die = child_die->sibling)
3ea89b92
PMR
13415 {
13416 struct die_info *child_origin_die;
13417 struct dwarf2_cu *child_origin_cu;
13418
13419 /* We are trying to process concrete instance entries:
216f72a1 13420 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13421 it's not relevant to our analysis here. i.e. detecting DIEs that are
13422 present in the abstract instance but not referenced in the concrete
13423 one. */
216f72a1 13424 if (child_die->tag == DW_TAG_call_site
dda83cd7 13425 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13426 continue;
13427
c38f313d
DJ
13428 /* For each CHILD_DIE, find the corresponding child of
13429 ORIGIN_DIE. If there is more than one layer of
13430 DW_AT_abstract_origin, follow them all; there shouldn't be,
13431 but GCC versions at least through 4.4 generate this (GCC PR
13432 40573). */
3ea89b92
PMR
13433 child_origin_die = child_die;
13434 child_origin_cu = cu;
c38f313d
DJ
13435 while (1)
13436 {
cd02d79d
PA
13437 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13438 child_origin_cu);
c38f313d
DJ
13439 if (attr == NULL)
13440 break;
cd02d79d
PA
13441 child_origin_die = follow_die_ref (child_origin_die, attr,
13442 &child_origin_cu);
c38f313d
DJ
13443 }
13444
d389af10
JK
13445 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13446 counterpart may exist. */
c38f313d 13447 if (child_origin_die != child_die)
d389af10 13448 {
edb3359d
DJ
13449 if (child_die->tag != child_origin_die->tag
13450 && !(child_die->tag == DW_TAG_inlined_subroutine
13451 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13452 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13453 "different tags"),
9d8780f0
SM
13454 sect_offset_str (child_die->sect_off),
13455 sect_offset_str (child_origin_die->sect_off));
c38f313d 13456 if (child_origin_die->parent != origin_die)
b98664d3 13457 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13458 "different parents"),
9d8780f0
SM
13459 sect_offset_str (child_die->sect_off),
13460 sect_offset_str (child_origin_die->sect_off));
c38f313d 13461 else
791afaa2 13462 offsets.push_back (child_origin_die->sect_off);
d389af10 13463 }
d389af10 13464 }
791afaa2
TT
13465 std::sort (offsets.begin (), offsets.end ());
13466 sect_offset *offsets_end = offsets.data () + offsets.size ();
13467 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13468 if (offsetp[-1] == *offsetp)
b98664d3 13469 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13470 "to DIE %s as their abstract origin"),
13471 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13472
791afaa2 13473 offsetp = offsets.data ();
d389af10
JK
13474 origin_child_die = origin_die->child;
13475 while (origin_child_die && origin_child_die->tag)
13476 {
13477 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13478 while (offsetp < offsets_end
9c541725 13479 && *offsetp < origin_child_die->sect_off)
d389af10 13480 offsetp++;
b64f50a1 13481 if (offsetp >= offsets_end
9c541725 13482 || *offsetp > origin_child_die->sect_off)
d389af10 13483 {
adde2bff
DE
13484 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13485 Check whether we're already processing ORIGIN_CHILD_DIE.
13486 This can happen with mutually referenced abstract_origins.
13487 PR 16581. */
13488 if (!origin_child_die->in_process)
13489 process_die (origin_child_die, origin_cu);
d389af10 13490 }
436c571c 13491 origin_child_die = origin_child_die->sibling;
d389af10 13492 }
cd02d79d 13493 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13494
13495 if (cu != origin_cu)
13496 compute_delayed_physnames (origin_cu);
d389af10
JK
13497}
13498
c906108c 13499static void
e7c27a73 13500read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13501{
5e22e966 13502 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13503 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13504 struct context_stack *newobj;
c906108c
SS
13505 CORE_ADDR lowpc;
13506 CORE_ADDR highpc;
13507 struct die_info *child_die;
edb3359d 13508 struct attribute *attr, *call_line, *call_file;
15d034d0 13509 const char *name;
e142c38c 13510 CORE_ADDR baseaddr;
801e3a5b 13511 struct block *block;
edb3359d 13512 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13513 std::vector<struct symbol *> template_args;
34eaf542 13514 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13515
13516 if (inlined_func)
13517 {
13518 /* If we do not have call site information, we can't show the
13519 caller of this inlined function. That's too confusing, so
13520 only use the scope for local variables. */
13521 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13522 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13523 if (call_line == NULL || call_file == NULL)
13524 {
13525 read_lexical_block_scope (die, cu);
13526 return;
13527 }
13528 }
c906108c 13529
b3b3bada 13530 baseaddr = objfile->text_section_offset ();
e142c38c 13531
94af9270 13532 name = dwarf2_name (die, cu);
c906108c 13533
e8d05480
JB
13534 /* Ignore functions with missing or empty names. These are actually
13535 illegal according to the DWARF standard. */
13536 if (name == NULL)
13537 {
b98664d3 13538 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13539 sect_offset_str (die->sect_off));
e8d05480
JB
13540 return;
13541 }
13542
13543 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13544 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13545 <= PC_BOUNDS_INVALID)
e8d05480 13546 {
ae4d0c03 13547 attr = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 13548 if (attr == nullptr || !attr->as_boolean ())
b98664d3 13549 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13550 "for subprogram DIE at %s"),
13551 sect_offset_str (die->sect_off));
e8d05480
JB
13552 return;
13553 }
c906108c 13554
3e29f34a
MR
13555 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13556 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13557
34eaf542
TT
13558 /* If we have any template arguments, then we must allocate a
13559 different sort of symbol. */
436c571c 13560 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13561 {
13562 if (child_die->tag == DW_TAG_template_type_param
13563 || child_die->tag == DW_TAG_template_value_param)
13564 {
8c14c3a3 13565 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13566 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13567 break;
13568 }
13569 }
13570
c24bdb02 13571 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13572 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13573 (struct symbol *) templ_func);
4c2df51b 13574
81873cc8 13575 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13576 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13577 cu->language);
13578
4cecd739
DJ
13579 /* If there is a location expression for DW_AT_frame_base, record
13580 it. */
e142c38c 13581 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13582 if (attr != nullptr)
fe978cb0 13583 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13584
63e43d3a
PMR
13585 /* If there is a location for the static link, record it. */
13586 newobj->static_link = NULL;
13587 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13588 if (attr != nullptr)
63e43d3a 13589 {
224c3ddb
SM
13590 newobj->static_link
13591 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13592 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13593 cu->addr_type ());
63e43d3a
PMR
13594 }
13595
c24bdb02 13596 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13597
639d11d3 13598 if (die->child != NULL)
c906108c 13599 {
639d11d3 13600 child_die = die->child;
c906108c
SS
13601 while (child_die && child_die->tag)
13602 {
34eaf542
TT
13603 if (child_die->tag == DW_TAG_template_type_param
13604 || child_die->tag == DW_TAG_template_value_param)
13605 {
13606 struct symbol *arg = new_symbol (child_die, NULL, cu);
13607
f1078f66 13608 if (arg != NULL)
2f4732b0 13609 template_args.push_back (arg);
34eaf542
TT
13610 }
13611 else
13612 process_die (child_die, cu);
436c571c 13613 child_die = child_die->sibling;
c906108c
SS
13614 }
13615 }
13616
d389af10
JK
13617 inherit_abstract_dies (die, cu);
13618
4a811a97
UW
13619 /* If we have a DW_AT_specification, we might need to import using
13620 directives from the context of the specification DIE. See the
13621 comment in determine_prefix. */
13622 if (cu->language == language_cplus
13623 && dwarf2_attr (die, DW_AT_specification, cu))
13624 {
13625 struct dwarf2_cu *spec_cu = cu;
13626 struct die_info *spec_die = die_specification (die, &spec_cu);
13627
13628 while (spec_die)
13629 {
13630 child_die = spec_die->child;
13631 while (child_die && child_die->tag)
13632 {
13633 if (child_die->tag == DW_TAG_imported_module)
13634 process_die (child_die, spec_cu);
436c571c 13635 child_die = child_die->sibling;
4a811a97
UW
13636 }
13637
13638 /* In some cases, GCC generates specification DIEs that
13639 themselves contain DW_AT_specification attributes. */
13640 spec_die = die_specification (spec_die, &spec_cu);
13641 }
13642 }
13643
c24bdb02 13644 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13645 /* Make a block for the local symbols within. */
c24bdb02 13646 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13647 cstk.static_link, lowpc, highpc);
801e3a5b 13648
df8a16a1 13649 /* For C++, set the block's scope. */
45280282
IB
13650 if ((cu->language == language_cplus
13651 || cu->language == language_fortran
c44af4eb
TT
13652 || cu->language == language_d
13653 || cu->language == language_rust)
4d4ec4e5 13654 && cu->processing_has_namespace_info)
195a3f6c
TT
13655 block_set_scope (block, determine_prefix (die, cu),
13656 &objfile->objfile_obstack);
df8a16a1 13657
801e3a5b
JB
13658 /* If we have address ranges, record them. */
13659 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13660
a60f3166 13661 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13662
34eaf542 13663 /* Attach template arguments to function. */
2f4732b0 13664 if (!template_args.empty ())
34eaf542
TT
13665 {
13666 gdb_assert (templ_func != NULL);
13667
2f4732b0 13668 templ_func->n_template_arguments = template_args.size ();
34eaf542 13669 templ_func->template_arguments
dda83cd7 13670 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
8d749320 13671 templ_func->n_template_arguments);
34eaf542 13672 memcpy (templ_func->template_arguments,
2f4732b0 13673 template_args.data (),
34eaf542 13674 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13675
13676 /* Make sure that the symtab is set on the new symbols. Even
13677 though they don't appear in this symtab directly, other parts
13678 of gdb assume that symbols do, and this is reasonably
13679 true. */
8634679f 13680 for (symbol *sym : template_args)
3e1d3d8c 13681 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13682 }
13683
208d8187
JB
13684 /* In C++, we can have functions nested inside functions (e.g., when
13685 a function declares a class that has methods). This means that
13686 when we finish processing a function scope, we may need to go
13687 back to building a containing block's symbol lists. */
c24bdb02
KS
13688 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13689 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13690
921e78cf
JB
13691 /* If we've finished processing a top-level function, subsequent
13692 symbols go in the file symbol list. */
c24bdb02
KS
13693 if (cu->get_builder ()->outermost_context_p ())
13694 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13695}
13696
13697/* Process all the DIES contained within a lexical block scope. Start
13698 a new scope, process the dies, and then close the scope. */
13699
13700static void
e7c27a73 13701read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13702{
5e22e966 13703 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13704 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13705 CORE_ADDR lowpc, highpc;
13706 struct die_info *child_die;
e142c38c
DJ
13707 CORE_ADDR baseaddr;
13708
b3b3bada 13709 baseaddr = objfile->text_section_offset ();
c906108c
SS
13710
13711 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13712 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13713 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13714 be nasty. Might be easier to properly extend generic blocks to
af34e669 13715 describe ranges. */
e385593e
JK
13716 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13717 {
13718 case PC_BOUNDS_NOT_PRESENT:
13719 /* DW_TAG_lexical_block has no attributes, process its children as if
13720 there was no wrapping by that DW_TAG_lexical_block.
13721 GCC does no longer produces such DWARF since GCC r224161. */
13722 for (child_die = die->child;
13723 child_die != NULL && child_die->tag;
436c571c 13724 child_die = child_die->sibling)
4f7bc5ed
TT
13725 {
13726 /* We might already be processing this DIE. This can happen
13727 in an unusual circumstance -- where a subroutine A
13728 appears lexically in another subroutine B, but A actually
13729 inlines B. The recursion is broken here, rather than in
13730 inherit_abstract_dies, because it seems better to simply
13731 drop concrete children here. */
13732 if (!child_die->in_process)
13733 process_die (child_die, cu);
13734 }
e385593e
JK
13735 return;
13736 case PC_BOUNDS_INVALID:
13737 return;
13738 }
3e29f34a
MR
13739 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13740 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13741
c24bdb02 13742 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13743 if (die->child != NULL)
c906108c 13744 {
639d11d3 13745 child_die = die->child;
c906108c
SS
13746 while (child_die && child_die->tag)
13747 {
e7c27a73 13748 process_die (child_die, cu);
436c571c 13749 child_die = child_die->sibling;
c906108c
SS
13750 }
13751 }
3ea89b92 13752 inherit_abstract_dies (die, cu);
c24bdb02 13753 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13754
c24bdb02
KS
13755 if (*cu->get_builder ()->get_local_symbols () != NULL
13756 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13757 {
801e3a5b 13758 struct block *block
dda83cd7 13759 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13760 cstk.start_addr, highpc);
801e3a5b
JB
13761
13762 /* Note that recording ranges after traversing children, as we
dda83cd7
SM
13763 do here, means that recording a parent's ranges entails
13764 walking across all its children's ranges as they appear in
13765 the address map, which is quadratic behavior.
13766
13767 It would be nicer to record the parent's ranges before
13768 traversing its children, simply overriding whatever you find
13769 there. But since we don't even decide whether to create a
13770 block until after we've traversed its children, that's hard
13771 to do. */
801e3a5b 13772 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13773 }
c24bdb02
KS
13774 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13775 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13776}
13777
216f72a1 13778/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13779
13780static void
13781read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13782{
5e22e966 13783 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13784 struct objfile *objfile = per_objfile->objfile;
08feed99 13785 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13786 CORE_ADDR pc, baseaddr;
13787 struct attribute *attr;
13788 struct call_site *call_site, call_site_local;
13789 void **slot;
13790 int nparams;
13791 struct die_info *child_die;
13792
b3b3bada 13793 baseaddr = objfile->text_section_offset ();
96408a79 13794
216f72a1
JK
13795 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13796 if (attr == NULL)
13797 {
13798 /* This was a pre-DWARF-5 GNU extension alias
13799 for DW_AT_call_return_pc. */
13800 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13801 }
96408a79
SA
13802 if (!attr)
13803 {
b98664d3 13804 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13805 "DIE %s [in module %s]"),
13806 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13807 return;
13808 }
95f982e5 13809 pc = attr->as_address () + baseaddr;
3e29f34a 13810 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13811
13812 if (cu->call_site_htab == NULL)
13813 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13814 NULL, &objfile->objfile_obstack,
13815 hashtab_obstack_allocate, NULL);
13816 call_site_local.pc = pc;
13817 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13818 if (*slot != NULL)
13819 {
b98664d3 13820 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13821 "DIE %s [in module %s]"),
13822 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13823 objfile_name (objfile));
96408a79
SA
13824 return;
13825 }
13826
13827 /* Count parameters at the caller. */
13828
13829 nparams = 0;
13830 for (child_die = die->child; child_die && child_die->tag;
436c571c 13831 child_die = child_die->sibling)
96408a79 13832 {
216f72a1 13833 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13834 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13835 {
b98664d3 13836 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13837 "DW_TAG_call_site child DIE %s [in module %s]"),
13838 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13839 objfile_name (objfile));
96408a79
SA
13840 continue;
13841 }
13842
13843 nparams++;
13844 }
13845
224c3ddb
SM
13846 call_site
13847 = ((struct call_site *)
13848 obstack_alloc (&objfile->objfile_obstack,
13849 sizeof (*call_site)
13850 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13851 *slot = call_site;
13852 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13853 call_site->pc = pc;
13854
216f72a1
JK
13855 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13856 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13857 {
13858 struct die_info *func_die;
13859
13860 /* Skip also over DW_TAG_inlined_subroutine. */
13861 for (func_die = die->parent;
13862 func_die && func_die->tag != DW_TAG_subprogram
13863 && func_die->tag != DW_TAG_subroutine_type;
13864 func_die = func_die->parent);
13865
216f72a1
JK
13866 /* DW_AT_call_all_calls is a superset
13867 of DW_AT_call_all_tail_calls. */
96408a79 13868 if (func_die
dda83cd7
SM
13869 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13870 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13871 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13872 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13873 {
13874 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13875 not complete. But keep CALL_SITE for look ups via call_site_htab,
13876 both the initial caller containing the real return address PC and
13877 the final callee containing the current PC of a chain of tail
13878 calls do not need to have the tail call list complete. But any
13879 function candidate for a virtual tail call frame searched via
13880 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13881 determined unambiguously. */
13882 }
13883 else
13884 {
13885 struct type *func_type = NULL;
13886
13887 if (func_die)
13888 func_type = get_die_type (func_die, cu);
13889 if (func_type != NULL)
13890 {
78134374 13891 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13892
13893 /* Enlist this call site to the function. */
13894 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13895 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13896 }
13897 else
b98664d3 13898 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13899 "DIE %s [in module %s]"),
13900 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13901 }
13902 }
13903
216f72a1
JK
13904 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13905 if (attr == NULL)
13906 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13907 if (attr == NULL)
13908 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13909 if (attr == NULL)
216f72a1
JK
13910 {
13911 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13912 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13913 }
96408a79 13914 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9d2246fc 13915 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
96408a79 13916 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13917 else if (attr->form_is_block ())
96408a79
SA
13918 {
13919 struct dwarf2_locexpr_baton *dlbaton;
9d2246fc 13920 struct dwarf_block *block = attr->as_block ();
96408a79 13921
8d749320 13922 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9d2246fc
TT
13923 dlbaton->data = block->data;
13924 dlbaton->size = block->size;
a50264ba 13925 dlbaton->per_objfile = per_objfile;
96408a79
SA
13926 dlbaton->per_cu = cu->per_cu;
13927
13928 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13929 }
cd6c91b4 13930 else if (attr->form_is_ref ())
96408a79 13931 {
96408a79
SA
13932 struct dwarf2_cu *target_cu = cu;
13933 struct die_info *target_die;
13934
ac9ec31b 13935 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13936 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13937 if (die_is_declaration (target_die, target_cu))
13938 {
7d45c7c3 13939 const char *target_physname;
9112db09
JK
13940
13941 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13942 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13943 if (target_physname == NULL)
9112db09 13944 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13945 if (target_physname == NULL)
b98664d3 13946 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13947 "physname, for referencing DIE %s [in module %s]"),
9d8780f0 13948 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13949 else
7d455152 13950 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13951 }
13952 else
13953 {
13954 CORE_ADDR lowpc;
13955
13956 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13957 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13958 <= PC_BOUNDS_INVALID)
b98664d3 13959 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13960 "low pc, for referencing DIE %s [in module %s]"),
9d8780f0 13961 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13962 else
3e29f34a
MR
13963 {
13964 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13965 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13966 }
96408a79
SA
13967 }
13968 }
13969 else
b98664d3 13970 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13971 "block nor reference, for DIE %s [in module %s]"),
13972 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13973
13974 call_site->per_cu = cu->per_cu;
9f47c707 13975 call_site->per_objfile = per_objfile;
96408a79
SA
13976
13977 for (child_die = die->child;
13978 child_die && child_die->tag;
436c571c 13979 child_die = child_die->sibling)
96408a79 13980 {
96408a79 13981 struct call_site_parameter *parameter;
1788b2d3 13982 struct attribute *loc, *origin;
96408a79 13983
216f72a1 13984 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13985 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13986 {
13987 /* Already printed the complaint above. */
13988 continue;
13989 }
13990
13991 gdb_assert (call_site->parameter_count < nparams);
13992 parameter = &call_site->parameter[call_site->parameter_count];
13993
1788b2d3
JK
13994 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13995 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13996 register is contained in DW_AT_call_value. */
96408a79 13997
24c5c679 13998 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13999 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14000 if (origin == NULL)
14001 {
14002 /* This was a pre-DWARF-5 GNU extension alias
14003 for DW_AT_call_parameter. */
14004 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14005 }
cd6c91b4 14006 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 14007 {
1788b2d3 14008 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 14009
0826b30a 14010 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 14011 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
14012 {
14013 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14014 binding can be done only inside one CU. Such referenced DIE
14015 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14016 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14017 "DW_TAG_call_site child DIE %s [in module %s]"),
14018 sect_offset_str (child_die->sect_off),
9c541725 14019 objfile_name (objfile));
d76b7dbc
JK
14020 continue;
14021 }
9c541725
PA
14022 parameter->u.param_cu_off
14023 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 14024 }
4fc6c0d5 14025 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 14026 {
b98664d3 14027 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14028 "DW_TAG_call_site child DIE %s [in module %s]"),
14029 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14030 continue;
14031 }
24c5c679 14032 else
96408a79 14033 {
9d2246fc
TT
14034 struct dwarf_block *block = loc->as_block ();
14035
24c5c679 14036 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9d2246fc 14037 (block->data, &block->data[block->size]);
24c5c679
JK
14038 if (parameter->u.dwarf_reg != -1)
14039 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9d2246fc
TT
14040 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14041 &block->data[block->size],
24c5c679
JK
14042 &parameter->u.fb_offset))
14043 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14044 else
14045 {
b98664d3 14046 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14047 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14048 "DW_TAG_call_site child DIE %s "
24c5c679 14049 "[in module %s]"),
9d8780f0 14050 sect_offset_str (child_die->sect_off),
9c541725 14051 objfile_name (objfile));
24c5c679
JK
14052 continue;
14053 }
96408a79
SA
14054 }
14055
216f72a1
JK
14056 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14057 if (attr == NULL)
14058 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 14059 if (attr == NULL || !attr->form_is_block ())
96408a79 14060 {
b98664d3 14061 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14062 "DW_TAG_call_site child DIE %s [in module %s]"),
14063 sect_offset_str (child_die->sect_off),
9c541725 14064 objfile_name (objfile));
96408a79
SA
14065 continue;
14066 }
9d2246fc
TT
14067
14068 struct dwarf_block *block = attr->as_block ();
14069 parameter->value = block->data;
14070 parameter->value_size = block->size;
96408a79
SA
14071
14072 /* Parameters are not pre-cleared by memset above. */
14073 parameter->data_value = NULL;
14074 parameter->data_value_size = 0;
14075 call_site->parameter_count++;
14076
216f72a1
JK
14077 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14078 if (attr == NULL)
14079 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14080 if (attr != nullptr)
96408a79 14081 {
4fc6c0d5 14082 if (!attr->form_is_block ())
b98664d3 14083 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14084 "DW_TAG_call_site child DIE %s [in module %s]"),
14085 sect_offset_str (child_die->sect_off),
9c541725 14086 objfile_name (objfile));
96408a79
SA
14087 else
14088 {
9d2246fc
TT
14089 block = attr->as_block ();
14090 parameter->data_value = block->data;
14091 parameter->data_value_size = block->size;
96408a79
SA
14092 }
14093 }
14094 }
14095}
14096
71a3c369
TT
14097/* Helper function for read_variable. If DIE represents a virtual
14098 table, then return the type of the concrete object that is
14099 associated with the virtual table. Otherwise, return NULL. */
14100
14101static struct type *
14102rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14103{
14104 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14105 if (attr == NULL)
14106 return NULL;
14107
14108 /* Find the type DIE. */
14109 struct die_info *type_die = NULL;
14110 struct dwarf2_cu *type_cu = cu;
14111
cd6c91b4 14112 if (attr->form_is_ref ())
71a3c369
TT
14113 type_die = follow_die_ref (die, attr, &type_cu);
14114 if (type_die == NULL)
14115 return NULL;
14116
14117 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14118 return NULL;
14119 return die_containing_type (type_die, type_cu);
14120}
14121
14122/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14123
14124static void
14125read_variable (struct die_info *die, struct dwarf2_cu *cu)
14126{
14127 struct rust_vtable_symbol *storage = NULL;
14128
14129 if (cu->language == language_rust)
14130 {
14131 struct type *containing_type = rust_containing_type (die, cu);
14132
14133 if (containing_type != NULL)
14134 {
5e22e966 14135 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 14136
8c14c3a3 14137 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 14138 storage->concrete_type = containing_type;
cf724bc9 14139 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14140 }
14141 }
14142
e4a62c65
TV
14143 struct symbol *res = new_symbol (die, NULL, cu, storage);
14144 struct attribute *abstract_origin
14145 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14146 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14147 if (res == NULL && loc && abstract_origin)
14148 {
14149 /* We have a variable without a name, but with a location and an abstract
14150 origin. This may be a concrete instance of an abstract variable
14151 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14152 later. */
14153 struct dwarf2_cu *origin_cu = cu;
14154 struct die_info *origin_die
14155 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
14156 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14157 per_objfile->per_bfd->abstract_to_concrete
14158 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14159 }
71a3c369
TT
14160}
14161
43988095
JK
14162/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14163 reading .debug_rnglists.
14164 Callback's type should be:
14165 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14166 Return true if the attributes are present and valid, otherwise,
14167 return false. */
14168
14169template <typename Callback>
14170static bool
14171dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
d0ce17d8 14172 dwarf_tag tag, Callback &&callback)
43988095 14173{
976ca316
SM
14174 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14175 struct objfile *objfile = per_objfile->objfile;
43988095 14176 bfd *obfd = objfile->obfd;
43988095 14177 /* Base address selection entry. */
2b24b6e4 14178 gdb::optional<CORE_ADDR> base;
43988095 14179 const gdb_byte *buffer;
43988095
JK
14180 CORE_ADDR baseaddr;
14181 bool overflow = false;
d0ce17d8
CT
14182 ULONGEST addr_index;
14183 struct dwarf2_section_info *rnglists_section;
43988095 14184
43988095 14185 base = cu->base_address;
d0ce17d8
CT
14186 rnglists_section = cu_debug_rnglists_section (cu, tag);
14187 rnglists_section->read (objfile);
43988095 14188
d0ce17d8 14189 if (offset >= rnglists_section->size)
43988095 14190 {
b98664d3 14191 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14192 offset);
14193 return false;
14194 }
d0ce17d8 14195 buffer = rnglists_section->buffer + offset;
43988095 14196
b3b3bada 14197 baseaddr = objfile->text_section_offset ();
43988095
JK
14198
14199 while (1)
14200 {
7814882a
JK
14201 /* Initialize it due to a false compiler warning. */
14202 CORE_ADDR range_beginning = 0, range_end = 0;
d0ce17d8
CT
14203 const gdb_byte *buf_end = (rnglists_section->buffer
14204 + rnglists_section->size);
43988095
JK
14205 unsigned int bytes_read;
14206
14207 if (buffer == buf_end)
14208 {
14209 overflow = true;
14210 break;
14211 }
14212 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14213 switch (rlet)
14214 {
14215 case DW_RLE_end_of_list:
14216 break;
14217 case DW_RLE_base_address:
14218 if (buffer + cu->header.addr_size > buf_end)
14219 {
14220 overflow = true;
14221 break;
14222 }
c8a7a66f 14223 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14224 buffer += bytes_read;
14225 break;
dda83cd7
SM
14226 case DW_RLE_base_addressx:
14227 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14228 buffer += bytes_read;
14229 base = read_addr_index (cu, addr_index);
14230 break;
43988095
JK
14231 case DW_RLE_start_length:
14232 if (buffer + cu->header.addr_size > buf_end)
14233 {
14234 overflow = true;
14235 break;
14236 }
c8a7a66f
TT
14237 range_beginning = cu->header.read_address (obfd, buffer,
14238 &bytes_read);
43988095
JK
14239 buffer += bytes_read;
14240 range_end = (range_beginning
14241 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14242 buffer += bytes_read;
14243 if (buffer > buf_end)
14244 {
14245 overflow = true;
14246 break;
14247 }
14248 break;
d0ce17d8 14249 case DW_RLE_startx_length:
dda83cd7
SM
14250 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14251 buffer += bytes_read;
14252 range_beginning = read_addr_index (cu, addr_index);
14253 if (buffer > buf_end)
14254 {
14255 overflow = true;
14256 break;
14257 }
14258 range_end = (range_beginning
14259 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14260 buffer += bytes_read;
14261 break;
43988095
JK
14262 case DW_RLE_offset_pair:
14263 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14264 buffer += bytes_read;
14265 if (buffer > buf_end)
14266 {
14267 overflow = true;
14268 break;
14269 }
14270 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14271 buffer += bytes_read;
14272 if (buffer > buf_end)
14273 {
14274 overflow = true;
14275 break;
14276 }
14277 break;
14278 case DW_RLE_start_end:
14279 if (buffer + 2 * cu->header.addr_size > buf_end)
14280 {
14281 overflow = true;
14282 break;
14283 }
c8a7a66f
TT
14284 range_beginning = cu->header.read_address (obfd, buffer,
14285 &bytes_read);
43988095 14286 buffer += bytes_read;
c8a7a66f 14287 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14288 buffer += bytes_read;
14289 break;
d0ce17d8 14290 case DW_RLE_startx_endx:
dda83cd7
SM
14291 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14292 buffer += bytes_read;
14293 range_beginning = read_addr_index (cu, addr_index);
14294 if (buffer > buf_end)
14295 {
14296 overflow = true;
14297 break;
14298 }
14299 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14300 buffer += bytes_read;
14301 range_end = read_addr_index (cu, addr_index);
14302 break;
43988095 14303 default:
b98664d3 14304 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14305 return false;
14306 }
14307 if (rlet == DW_RLE_end_of_list || overflow)
14308 break;
14309 if (rlet == DW_RLE_base_address)
14310 continue;
14311
43988095
JK
14312 if (range_beginning > range_end)
14313 {
14314 /* Inverted range entries are invalid. */
b98664d3 14315 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14316 return false;
14317 }
14318
14319 /* Empty range entries have no effect. */
14320 if (range_beginning == range_end)
14321 continue;
14322
d0ce17d8
CT
14323 /* Only DW_RLE_offset_pair needs the base address added. */
14324 if (rlet == DW_RLE_offset_pair)
14325 {
14326 if (!base.has_value ())
14327 {
14328 /* We have no valid base address for the DW_RLE_offset_pair. */
14329 complaint (_("Invalid .debug_rnglists data (no base address for "
14330 "DW_RLE_offset_pair)"));
14331 return false;
14332 }
14333
14334 range_beginning += *base;
14335 range_end += *base;
14336 }
43988095
JK
14337
14338 /* A not-uncommon case of bad debug info.
14339 Don't pollute the addrmap with bad data. */
14340 if (range_beginning + baseaddr == 0
976ca316 14341 && !per_objfile->per_bfd->has_section_at_zero)
43988095 14342 {
b98664d3 14343 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14344 " [in module %s]"), objfile_name (objfile));
14345 continue;
14346 }
14347
14348 callback (range_beginning, range_end);
14349 }
14350
14351 if (overflow)
14352 {
b98664d3 14353 complaint (_("Offset %d is not terminated "
43988095
JK
14354 "for DW_AT_ranges attribute"),
14355 offset);
14356 return false;
14357 }
14358
14359 return true;
14360}
14361
14362/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14363 Callback's type should be:
14364 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14365 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14366
43988095 14367template <typename Callback>
43039443 14368static int
d0ce17d8 14369dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
43988095 14370 Callback &&callback)
43039443 14371{
5e22e966
SM
14372 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14373 struct objfile *objfile = per_objfile->objfile;
43039443
JK
14374 struct comp_unit_head *cu_header = &cu->header;
14375 bfd *obfd = objfile->obfd;
14376 unsigned int addr_size = cu_header->addr_size;
14377 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14378 /* Base address selection entry. */
2b24b6e4 14379 gdb::optional<CORE_ADDR> base;
43039443 14380 unsigned int dummy;
d521ce57 14381 const gdb_byte *buffer;
ff013f42 14382 CORE_ADDR baseaddr;
43039443 14383
43988095 14384 if (cu_header->version >= 5)
d0ce17d8 14385 return dwarf2_rnglists_process (offset, cu, tag, callback);
43988095 14386
d00adf39 14387 base = cu->base_address;
43039443 14388
5e22e966
SM
14389 per_objfile->per_bfd->ranges.read (objfile);
14390 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 14391 {
b98664d3 14392 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14393 offset);
14394 return 0;
14395 }
5e22e966 14396 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 14397
b3b3bada 14398 baseaddr = objfile->text_section_offset ();
ff013f42 14399
43039443
JK
14400 while (1)
14401 {
14402 CORE_ADDR range_beginning, range_end;
14403
c8a7a66f 14404 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 14405 buffer += addr_size;
c8a7a66f 14406 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
14407 buffer += addr_size;
14408 offset += 2 * addr_size;
14409
14410 /* An end of list marker is a pair of zero addresses. */
14411 if (range_beginning == 0 && range_end == 0)
14412 /* Found the end of list entry. */
14413 break;
14414
14415 /* Each base address selection entry is a pair of 2 values.
14416 The first is the largest possible address, the second is
14417 the base address. Check for a base address here. */
14418 if ((range_beginning & mask) == mask)
14419 {
28d2bfb9
AB
14420 /* If we found the largest possible address, then we already
14421 have the base address in range_end. */
14422 base = range_end;
43039443
JK
14423 continue;
14424 }
14425
2b24b6e4 14426 if (!base.has_value ())
43039443
JK
14427 {
14428 /* We have no valid base address for the ranges
14429 data. */
b98664d3 14430 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14431 return 0;
14432 }
14433
9277c30c
UW
14434 if (range_beginning > range_end)
14435 {
14436 /* Inverted range entries are invalid. */
b98664d3 14437 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14438 return 0;
14439 }
14440
14441 /* Empty range entries have no effect. */
14442 if (range_beginning == range_end)
14443 continue;
14444
2b24b6e4
TT
14445 range_beginning += *base;
14446 range_end += *base;
43039443 14447
01093045
DE
14448 /* A not-uncommon case of bad debug info.
14449 Don't pollute the addrmap with bad data. */
14450 if (range_beginning + baseaddr == 0
5e22e966 14451 && !per_objfile->per_bfd->has_section_at_zero)
01093045 14452 {
b98664d3 14453 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14454 " [in module %s]"), objfile_name (objfile));
01093045
DE
14455 continue;
14456 }
14457
5f46c5a5
JK
14458 callback (range_beginning, range_end);
14459 }
14460
14461 return 1;
14462}
14463
14464/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14465 Return 1 if the attributes are present and valid, otherwise, return 0.
14466 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14467
14468static int
14469dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14470 CORE_ADDR *high_return, struct dwarf2_cu *cu,
d0ce17d8 14471 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
5f46c5a5 14472{
5e22e966 14473 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14474 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14475 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14476 int low_set = 0;
14477 CORE_ADDR low = 0;
14478 CORE_ADDR high = 0;
14479 int retval;
14480
d0ce17d8 14481 retval = dwarf2_ranges_process (offset, cu, tag,
5f46c5a5
JK
14482 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14483 {
9277c30c 14484 if (ranges_pst != NULL)
3e29f34a
MR
14485 {
14486 CORE_ADDR lowpc;
14487 CORE_ADDR highpc;
14488
79748972
TT
14489 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14490 range_beginning + baseaddr)
14491 - baseaddr);
14492 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14493 range_end + baseaddr)
14494 - baseaddr);
d320c2b5
TT
14495 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14496 lowpc, highpc - 1, ranges_pst);
3e29f34a 14497 }
ff013f42 14498
43039443
JK
14499 /* FIXME: This is recording everything as a low-high
14500 segment of consecutive addresses. We should have a
14501 data structure for discontiguous block ranges
14502 instead. */
14503 if (! low_set)
14504 {
14505 low = range_beginning;
14506 high = range_end;
14507 low_set = 1;
14508 }
14509 else
14510 {
14511 if (range_beginning < low)
14512 low = range_beginning;
14513 if (range_end > high)
14514 high = range_end;
14515 }
5f46c5a5
JK
14516 });
14517 if (!retval)
14518 return 0;
43039443
JK
14519
14520 if (! low_set)
14521 /* If the first entry is an end-of-list marker, the range
14522 describes an empty scope, i.e. no instructions. */
14523 return 0;
14524
14525 if (low_return)
14526 *low_return = low;
14527 if (high_return)
14528 *high_return = high;
14529 return 1;
14530}
14531
3a2b436a
JK
14532/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14533 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14534 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14535
3a2b436a 14536static enum pc_bounds_kind
af34e669 14537dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14538 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14539 dwarf2_psymtab *pst)
c906108c 14540{
976ca316 14541 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14542 struct attribute *attr;
91da1414 14543 struct attribute *attr_high;
af34e669
DJ
14544 CORE_ADDR low = 0;
14545 CORE_ADDR high = 0;
e385593e 14546 enum pc_bounds_kind ret;
c906108c 14547
91da1414
MW
14548 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14549 if (attr_high)
af34e669 14550 {
e142c38c 14551 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14552 if (attr != nullptr)
dda83cd7 14553 {
95f982e5
TT
14554 low = attr->as_address ();
14555 high = attr_high->as_address ();
cd6c91b4 14556 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14557 high += low;
91da1414 14558 }
af34e669
DJ
14559 else
14560 /* Found high w/o low attribute. */
e385593e 14561 return PC_BOUNDS_INVALID;
af34e669
DJ
14562
14563 /* Found consecutive range of addresses. */
3a2b436a 14564 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14565 }
c906108c 14566 else
af34e669 14567 {
e142c38c 14568 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14569 if (attr != nullptr && attr->form_is_unsigned ())
af34e669 14570 {
18a8505e 14571 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259 14572 We take advantage of the fact that DW_AT_ranges does not appear
d0ce17d8
CT
14573 in DW_TAG_compile_unit of DWO files.
14574
14575 Attributes of the form DW_FORM_rnglistx have already had their
14576 value changed by read_rnglist_index and already include
14577 DW_AT_rnglists_base, so don't need to add the ranges base,
14578 either. */
14579 int need_ranges_base = (die->tag != DW_TAG_compile_unit
14580 && attr->form != DW_FORM_rnglistx);
529908cb 14581 unsigned int ranges_offset = (attr->as_unsigned ()
ab435259
DE
14582 + (need_ranges_base
14583 ? cu->ranges_base
14584 : 0));
2e3cf129 14585
af34e669 14586 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14587 .debug_ranges section. */
d0ce17d8
CT
14588 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14589 die->tag))
e385593e 14590 return PC_BOUNDS_INVALID;
43039443 14591 /* Found discontinuous range of addresses. */
3a2b436a 14592 ret = PC_BOUNDS_RANGES;
af34e669 14593 }
e385593e
JK
14594 else
14595 return PC_BOUNDS_NOT_PRESENT;
af34e669 14596 }
c906108c 14597
48fbe735 14598 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14599 if (high <= low)
e385593e 14600 return PC_BOUNDS_INVALID;
c906108c
SS
14601
14602 /* When using the GNU linker, .gnu.linkonce. sections are used to
14603 eliminate duplicate copies of functions and vtables and such.
14604 The linker will arbitrarily choose one and discard the others.
14605 The AT_*_pc values for such functions refer to local labels in
14606 these sections. If the section from that file was discarded, the
14607 labels are not in the output, so the relocs get a value of 0.
14608 If this is a discarded function, mark the pc bounds as invalid,
14609 so that GDB will ignore it. */
976ca316 14610 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14611 return PC_BOUNDS_INVALID;
c906108c
SS
14612
14613 *lowpc = low;
96408a79
SA
14614 if (highpc)
14615 *highpc = high;
af34e669 14616 return ret;
c906108c
SS
14617}
14618
b084d499
JB
14619/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14620 its low and high PC addresses. Do nothing if these addresses could not
14621 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14622 and HIGHPC to the high address if greater than HIGHPC. */
14623
14624static void
14625dwarf2_get_subprogram_pc_bounds (struct die_info *die,
dda83cd7
SM
14626 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14627 struct dwarf2_cu *cu)
b084d499
JB
14628{
14629 CORE_ADDR low, high;
14630 struct die_info *child = die->child;
14631
e385593e 14632 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14633 {
325fac50
PA
14634 *lowpc = std::min (*lowpc, low);
14635 *highpc = std::max (*highpc, high);
b084d499
JB
14636 }
14637
14638 /* If the language does not allow nested subprograms (either inside
14639 subprograms or lexical blocks), we're done. */
14640 if (cu->language != language_ada)
14641 return;
6e70227d 14642
b084d499
JB
14643 /* Check all the children of the given DIE. If it contains nested
14644 subprograms, then check their pc bounds. Likewise, we need to
14645 check lexical blocks as well, as they may also contain subprogram
14646 definitions. */
14647 while (child && child->tag)
14648 {
14649 if (child->tag == DW_TAG_subprogram
dda83cd7
SM
14650 || child->tag == DW_TAG_lexical_block)
14651 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14652 child = child->sibling;
b084d499
JB
14653 }
14654}
14655
fae299cd
DC
14656/* Get the low and high pc's represented by the scope DIE, and store
14657 them in *LOWPC and *HIGHPC. If the correct values can't be
14658 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14659
14660static void
14661get_scope_pc_bounds (struct die_info *die,
14662 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14663 struct dwarf2_cu *cu)
14664{
14665 CORE_ADDR best_low = (CORE_ADDR) -1;
14666 CORE_ADDR best_high = (CORE_ADDR) 0;
14667 CORE_ADDR current_low, current_high;
14668
3a2b436a 14669 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14670 >= PC_BOUNDS_RANGES)
fae299cd
DC
14671 {
14672 best_low = current_low;
14673 best_high = current_high;
14674 }
14675 else
14676 {
14677 struct die_info *child = die->child;
14678
14679 while (child && child->tag)
14680 {
14681 switch (child->tag) {
14682 case DW_TAG_subprogram:
dda83cd7 14683 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14684 break;
14685 case DW_TAG_namespace:
f55ee35c 14686 case DW_TAG_module:
fae299cd
DC
14687 /* FIXME: carlton/2004-01-16: Should we do this for
14688 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14689 that current GCC's always emit the DIEs corresponding
14690 to definitions of methods of classes as children of a
14691 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14692 the DIEs giving the declarations, which could be
14693 anywhere). But I don't see any reason why the
14694 standards says that they have to be there. */
14695 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14696
14697 if (current_low != ((CORE_ADDR) -1))
14698 {
325fac50
PA
14699 best_low = std::min (best_low, current_low);
14700 best_high = std::max (best_high, current_high);
fae299cd
DC
14701 }
14702 break;
14703 default:
0963b4bd 14704 /* Ignore. */
fae299cd
DC
14705 break;
14706 }
14707
436c571c 14708 child = child->sibling;
fae299cd
DC
14709 }
14710 }
14711
14712 *lowpc = best_low;
14713 *highpc = best_high;
14714}
14715
801e3a5b
JB
14716/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14717 in DIE. */
380bca97 14718
801e3a5b
JB
14719static void
14720dwarf2_record_block_ranges (struct die_info *die, struct block *block,
dda83cd7 14721 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
801e3a5b 14722{
5e22e966 14723 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14724 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14725 struct attribute *attr;
91da1414 14726 struct attribute *attr_high;
801e3a5b 14727
91da1414
MW
14728 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14729 if (attr_high)
801e3a5b 14730 {
801e3a5b 14731 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14732 if (attr != nullptr)
dda83cd7 14733 {
95f982e5
TT
14734 CORE_ADDR low = attr->as_address ();
14735 CORE_ADDR high = attr_high->as_address ();
31aa7e4e 14736
cd6c91b4 14737 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14738 high += low;
9a619af0 14739
3e29f34a
MR
14740 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14741 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14742 cu->get_builder ()->record_block_range (block, low, high - 1);
dda83cd7 14743 }
801e3a5b
JB
14744 }
14745
14746 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14747 if (attr != nullptr && attr->form_is_unsigned ())
801e3a5b 14748 {
18a8505e 14749 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259 14750 We take advantage of the fact that DW_AT_ranges does not appear
d0ce17d8
CT
14751 in DW_TAG_compile_unit of DWO files.
14752
14753 Attributes of the form DW_FORM_rnglistx have already had their
14754 value changed by read_rnglist_index and already include
14755 DW_AT_rnglists_base, so don't need to add the ranges base,
14756 either. */
14757 int need_ranges_base = (die->tag != DW_TAG_compile_unit
14758 && attr->form != DW_FORM_rnglistx);
801e3a5b
JB
14759
14760 /* The value of the DW_AT_ranges attribute is the offset of the
dda83cd7 14761 address range list in the .debug_ranges section. */
529908cb 14762 unsigned long offset = (attr->as_unsigned ()
ab435259 14763 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14764
2d5f09ec 14765 std::vector<blockrange> blockvec;
d0ce17d8 14766 dwarf2_ranges_process (offset, cu, die->tag,
5f46c5a5
JK
14767 [&] (CORE_ADDR start, CORE_ADDR end)
14768 {
58fdfd2c
JK
14769 start += baseaddr;
14770 end += baseaddr;
5f46c5a5
JK
14771 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14772 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14773 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14774 blockvec.emplace_back (start, end);
5f46c5a5 14775 });
2d5f09ec
KB
14776
14777 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14778 }
14779}
14780
685b1105
JK
14781/* Check whether the producer field indicates either of GCC < 4.6, or the
14782 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14783
685b1105
JK
14784static void
14785check_producer (struct dwarf2_cu *cu)
60d5a603 14786{
38360086 14787 int major, minor;
60d5a603
JK
14788
14789 if (cu->producer == NULL)
14790 {
14791 /* For unknown compilers expect their behavior is DWARF version
14792 compliant.
14793
14794 GCC started to support .debug_types sections by -gdwarf-4 since
14795 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14796 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14797 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14798 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14799 }
b1ffba5a 14800 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14801 {
38360086
MW
14802 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14803 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14804 }
5230b05a 14805 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14806 {
14807 cu->producer_is_icc = true;
14808 cu->producer_is_icc_lt_14 = major < 14;
14809 }
c258c396
JD
14810 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14811 cu->producer_is_codewarrior = true;
685b1105
JK
14812 else
14813 {
14814 /* For other non-GCC compilers, expect their behavior is DWARF version
14815 compliant. */
60d5a603
JK
14816 }
14817
9068261f 14818 cu->checked_producer = true;
685b1105 14819}
ba919b58 14820
685b1105
JK
14821/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14822 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14823 during 4.6.0 experimental. */
14824
9068261f 14825static bool
685b1105
JK
14826producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14827{
14828 if (!cu->checked_producer)
14829 check_producer (cu);
14830
14831 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14832}
14833
c258c396
JD
14834
14835/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14836 with incorrect is_stmt attributes. */
14837
14838static bool
14839producer_is_codewarrior (struct dwarf2_cu *cu)
14840{
14841 if (!cu->checked_producer)
14842 check_producer (cu);
14843
14844 return cu->producer_is_codewarrior;
14845}
14846
bf23a268
TT
14847/* Return the accessibility of DIE, as given by DW_AT_accessibility.
14848 If that attribute is not available, return the appropriate
14849 default. */
60d5a603
JK
14850
14851static enum dwarf_access_attribute
bf23a268 14852dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
60d5a603 14853{
bf23a268
TT
14854 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14855 if (attr != nullptr)
14856 {
14857 LONGEST value = attr->constant_value (-1);
14858 if (value == DW_ACCESS_public
14859 || value == DW_ACCESS_protected
14860 || value == DW_ACCESS_private)
14861 return (dwarf_access_attribute) value;
14862 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14863 plongest (value));
14864 }
14865
60d5a603
JK
14866 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14867 {
14868 /* The default DWARF 2 accessibility for members is public, the default
14869 accessibility for inheritance is private. */
14870
14871 if (die->tag != DW_TAG_inheritance)
14872 return DW_ACCESS_public;
14873 else
14874 return DW_ACCESS_private;
14875 }
14876 else
14877 {
14878 /* DWARF 3+ defines the default accessibility a different way. The same
14879 rules apply now for DW_TAG_inheritance as for the members and it only
14880 depends on the container kind. */
14881
14882 if (die->parent->tag == DW_TAG_class_type)
14883 return DW_ACCESS_private;
14884 else
14885 return DW_ACCESS_public;
14886 }
14887}
14888
74ac6d43
TT
14889/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14890 offset. If the attribute was not found return 0, otherwise return
14891 1. If it was found but could not properly be handled, set *OFFSET
14892 to 0. */
14893
14894static int
14895handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14896 LONGEST *offset)
14897{
14898 struct attribute *attr;
14899
14900 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14901 if (attr != NULL)
14902 {
14903 *offset = 0;
14904
14905 /* Note that we do not check for a section offset first here.
14906 This is because DW_AT_data_member_location is new in DWARF 4,
14907 so if we see it, we can assume that a constant form is really
14908 a constant and not a section offset. */
cd6c91b4 14909 if (attr->form_is_constant ())
0826b30a 14910 *offset = attr->constant_value (0);
cd6c91b4 14911 else if (attr->form_is_section_offset ())
74ac6d43 14912 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14913 else if (attr->form_is_block ())
9d2246fc 14914 *offset = decode_locdesc (attr->as_block (), cu);
74ac6d43
TT
14915 else
14916 dwarf2_complex_location_expr_complaint ();
14917
14918 return 1;
14919 }
14920
14921 return 0;
14922}
14923
7d79de9a
TT
14924/* Look for DW_AT_data_member_location and store the results in FIELD. */
14925
14926static void
14927handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14928 struct field *field)
14929{
14930 struct attribute *attr;
14931
14932 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14933 if (attr != NULL)
14934 {
14935 if (attr->form_is_constant ())
14936 {
14937 LONGEST offset = attr->constant_value (0);
14938 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14939 }
14940 else if (attr->form_is_section_offset ())
14941 dwarf2_complex_location_expr_complaint ();
14942 else if (attr->form_is_block ())
14943 {
14944 bool handled;
9d2246fc 14945 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
7d79de9a
TT
14946 if (handled)
14947 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14948 else
14949 {
5e22e966
SM
14950 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14951 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14952 struct dwarf2_locexpr_baton *dlbaton
14953 = XOBNEW (&objfile->objfile_obstack,
14954 struct dwarf2_locexpr_baton);
9d2246fc
TT
14955 dlbaton->data = attr->as_block ()->data;
14956 dlbaton->size = attr->as_block ()->size;
7d79de9a
TT
14957 /* When using this baton, we want to compute the address
14958 of the field, not the value. This is why
14959 is_reference is set to false here. */
14960 dlbaton->is_reference = false;
5e22e966 14961 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14962 dlbaton->per_cu = cu->per_cu;
14963
14964 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14965 }
14966 }
14967 else
14968 dwarf2_complex_location_expr_complaint ();
14969 }
14970}
14971
c906108c
SS
14972/* Add an aggregate field to the field list. */
14973
14974static void
107d2387 14975dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14976 struct dwarf2_cu *cu)
6e70227d 14977{
5e22e966 14978 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14979 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14980 struct nextfield *new_field;
14981 struct attribute *attr;
14982 struct field *fp;
15d034d0 14983 const char *fieldname = "";
c906108c 14984
7d0ccb61
DJ
14985 if (die->tag == DW_TAG_inheritance)
14986 {
be2daae6
TT
14987 fip->baseclasses.emplace_back ();
14988 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14989 }
14990 else
14991 {
be2daae6
TT
14992 fip->fields.emplace_back ();
14993 new_field = &fip->fields.back ();
7d0ccb61 14994 }
be2daae6 14995
9c6a1327
TT
14996 new_field->offset = die->sect_off;
14997
bf23a268 14998 new_field->accessibility = dwarf2_access_attribute (die, cu);
c906108c 14999 if (new_field->accessibility != DW_ACCESS_public)
264fc0e2 15000 fip->non_public_fields = true;
60d5a603 15001
e142c38c 15002 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 15003 if (attr != nullptr)
23dca5c3 15004 new_field->virtuality = attr->as_virtuality ();
60d5a603
JK
15005 else
15006 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15007
15008 fp = &new_field->field;
a9a9bd0f 15009
e142c38c 15010 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15011 {
a9a9bd0f 15012 /* Data member other than a C++ static data member. */
6e70227d 15013
c906108c 15014 /* Get type of field. */
5d14b6e5 15015 fp->set_type (die_type (die, cu));
c906108c 15016
d6a843b5 15017 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15018
c906108c 15019 /* Get bit size of field (zero if none). */
e142c38c 15020 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 15021 if (attr != nullptr)
c906108c 15022 {
529908cb 15023 FIELD_BITSIZE (*fp) = attr->constant_value (0);
c906108c
SS
15024 }
15025 else
15026 {
15027 FIELD_BITSIZE (*fp) = 0;
15028 }
15029
15030 /* Get bit offset of field. */
7d79de9a 15031 handle_data_member_location (die, cu, fp);
e142c38c 15032 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7c184d33 15033 if (attr != nullptr && attr->form_is_constant ())
c906108c 15034 {
d5a22e77 15035 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
15036 {
15037 /* For big endian bits, the DW_AT_bit_offset gives the
dda83cd7
SM
15038 additional bit offset from the MSB of the containing
15039 anonymous object to the MSB of the field. We don't
15040 have to do anything special since we don't need to
15041 know the size of the anonymous object. */
529908cb 15042 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
7c184d33 15043 + attr->constant_value (0)));
c906108c
SS
15044 }
15045 else
15046 {
15047 /* For little endian bits, compute the bit offset to the
dda83cd7
SM
15048 MSB of the anonymous object, subtract off the number of
15049 bits from the MSB of the field to the MSB of the
15050 object, and then subtract off the number of bits of
15051 the field itself. The result is the bit offset of
15052 the LSB of the field. */
c906108c 15053 int anonymous_size;
7c184d33 15054 int bit_offset = attr->constant_value (0);
c906108c 15055
e142c38c 15056 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7c184d33 15057 if (attr != nullptr && attr->form_is_constant ())
c906108c
SS
15058 {
15059 /* The size of the anonymous object containing
15060 the bit field is explicit, so use the
15061 indicated size (in bytes). */
7c184d33 15062 anonymous_size = attr->constant_value (0);
c906108c
SS
15063 }
15064 else
15065 {
15066 /* The size of the anonymous object containing
15067 the bit field must be inferred from the type
15068 attribute of the data member containing the
15069 bit field. */
5d14b6e5 15070 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 15071 }
f41f5e61
PA
15072 SET_FIELD_BITPOS (*fp,
15073 (FIELD_BITPOS (*fp)
15074 + anonymous_size * bits_per_byte
15075 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15076 }
15077 }
da5b30da
AA
15078 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15079 if (attr != NULL)
15080 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 15081 + attr->constant_value (0)));
c906108c
SS
15082
15083 /* Get name of field. */
39cbfefa
DJ
15084 fieldname = dwarf2_name (die, cu);
15085 if (fieldname == NULL)
15086 fieldname = "";
d8151005
DJ
15087
15088 /* The name is already allocated along with this objfile, so we don't
15089 need to duplicate it for the type. */
15090 fp->name = fieldname;
c906108c
SS
15091
15092 /* Change accessibility for artificial fields (e.g. virtual table
dda83cd7 15093 pointer or virtual base class pointer) to private. */
e142c38c 15094 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15095 {
d48cc9dd 15096 FIELD_ARTIFICIAL (*fp) = 1;
c906108c 15097 new_field->accessibility = DW_ACCESS_private;
264fc0e2 15098 fip->non_public_fields = true;
c906108c
SS
15099 }
15100 }
a9a9bd0f 15101 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15102 {
a9a9bd0f
DC
15103 /* C++ static member. */
15104
15105 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15106 is a declaration, but all versions of G++ as of this writing
15107 (so through at least 3.2.1) incorrectly generate
15108 DW_TAG_variable tags. */
6e70227d 15109
ff355380 15110 const char *physname;
c906108c 15111
a9a9bd0f 15112 /* Get name of field. */
39cbfefa
DJ
15113 fieldname = dwarf2_name (die, cu);
15114 if (fieldname == NULL)
c906108c
SS
15115 return;
15116
254e6b9e 15117 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15118 if (attr
15119 /* Only create a symbol if this is an external value.
15120 new_symbol checks this and puts the value in the global symbol
15121 table, which we want. If it is not external, new_symbol
15122 will try to put the value in cu->list_in_scope which is wrong. */
15123 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15124 {
15125 /* A static const member, not much different than an enum as far as
15126 we're concerned, except that we can support more types. */
15127 new_symbol (die, NULL, cu);
15128 }
15129
2df3850c 15130 /* Get physical name. */
ff355380 15131 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15132
d8151005
DJ
15133 /* The name is already allocated along with this objfile, so we don't
15134 need to duplicate it for the type. */
15135 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 15136 fp->set_type (die_type (die, cu));
d8151005 15137 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15138 }
15139 else if (die->tag == DW_TAG_inheritance)
15140 {
74ac6d43 15141 /* C++ base class field. */
7d79de9a 15142 handle_data_member_location (die, cu, fp);
c906108c 15143 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
15144 fp->set_type (die_type (die, cu));
15145 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 15146 }
2ddeaf8a
TT
15147 else
15148 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15149}
15150
883fd55a
KS
15151/* Can the type given by DIE define another type? */
15152
15153static bool
15154type_can_define_types (const struct die_info *die)
15155{
15156 switch (die->tag)
15157 {
15158 case DW_TAG_typedef:
15159 case DW_TAG_class_type:
15160 case DW_TAG_structure_type:
15161 case DW_TAG_union_type:
15162 case DW_TAG_enumeration_type:
15163 return true;
15164
15165 default:
15166 return false;
15167 }
15168}
15169
15170/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15171
15172static void
883fd55a
KS
15173dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15174 struct dwarf2_cu *cu)
6e70227d 15175{
be2daae6
TT
15176 struct decl_field fp;
15177 memset (&fp, 0, sizeof (fp));
98751a41 15178
883fd55a 15179 gdb_assert (type_can_define_types (die));
98751a41 15180
883fd55a 15181 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15182 fp.name = dwarf2_name (die, cu);
15183 fp.type = read_type_die (die, cu);
98751a41 15184
c191a687 15185 /* Save accessibility. */
bf23a268 15186 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
c191a687
KS
15187 switch (accessibility)
15188 {
15189 case DW_ACCESS_public:
15190 /* The assumed value if neither private nor protected. */
15191 break;
15192 case DW_ACCESS_private:
be2daae6 15193 fp.is_private = 1;
c191a687
KS
15194 break;
15195 case DW_ACCESS_protected:
be2daae6 15196 fp.is_protected = 1;
c191a687 15197 break;
c191a687
KS
15198 }
15199
883fd55a 15200 if (die->tag == DW_TAG_typedef)
be2daae6 15201 fip->typedef_field_list.push_back (fp);
883fd55a 15202 else
be2daae6 15203 fip->nested_types_list.push_back (fp);
98751a41
JK
15204}
15205
9c6a1327
TT
15206/* A convenience typedef that's used when finding the discriminant
15207 field for a variant part. */
1b95cdb7
SM
15208typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15209 offset_map_type;
9c6a1327
TT
15210
15211/* Compute the discriminant range for a given variant. OBSTACK is
15212 where the results will be stored. VARIANT is the variant to
15213 process. IS_UNSIGNED indicates whether the discriminant is signed
15214 or unsigned. */
15215
15216static const gdb::array_view<discriminant_range>
15217convert_variant_range (struct obstack *obstack, const variant_field &variant,
15218 bool is_unsigned)
15219{
15220 std::vector<discriminant_range> ranges;
15221
15222 if (variant.default_branch)
15223 return {};
15224
15225 if (variant.discr_list_data == nullptr)
15226 {
15227 discriminant_range r
15228 = {variant.discriminant_value, variant.discriminant_value};
15229 ranges.push_back (r);
15230 }
15231 else
15232 {
15233 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15234 variant.discr_list_data->size);
15235 while (!data.empty ())
15236 {
15237 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15238 {
15239 complaint (_("invalid discriminant marker: %d"), data[0]);
15240 break;
15241 }
15242 bool is_range = data[0] == DW_DSC_range;
15243 data = data.slice (1);
15244
15245 ULONGEST low, high;
15246 unsigned int bytes_read;
15247
15248 if (data.empty ())
15249 {
15250 complaint (_("DW_AT_discr_list missing low value"));
15251 break;
15252 }
15253 if (is_unsigned)
15254 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15255 else
15256 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15257 &bytes_read);
15258 data = data.slice (bytes_read);
15259
15260 if (is_range)
15261 {
15262 if (data.empty ())
15263 {
15264 complaint (_("DW_AT_discr_list missing high value"));
15265 break;
15266 }
15267 if (is_unsigned)
15268 high = read_unsigned_leb128 (nullptr, data.data (),
15269 &bytes_read);
15270 else
15271 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15272 &bytes_read);
15273 data = data.slice (bytes_read);
15274 }
15275 else
15276 high = low;
15277
15278 ranges.push_back ({ low, high });
15279 }
15280 }
15281
15282 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15283 ranges.size ());
15284 std::copy (ranges.begin (), ranges.end (), result);
15285 return gdb::array_view<discriminant_range> (result, ranges.size ());
15286}
15287
15288static const gdb::array_view<variant_part> create_variant_parts
15289 (struct obstack *obstack,
15290 const offset_map_type &offset_map,
15291 struct field_info *fi,
15292 const std::vector<variant_part_builder> &variant_parts);
15293
15294/* Fill in a "struct variant" for a given variant field. RESULT is
15295 the variant to fill in. OBSTACK is where any needed allocations
15296 will be done. OFFSET_MAP holds the mapping from section offsets to
15297 fields for the type. FI describes the fields of the type we're
15298 processing. FIELD is the variant field we're converting. */
15299
15300static void
15301create_one_variant (variant &result, struct obstack *obstack,
15302 const offset_map_type &offset_map,
15303 struct field_info *fi, const variant_field &field)
15304{
15305 result.discriminants = convert_variant_range (obstack, field, false);
15306 result.first_field = field.first_field + fi->baseclasses.size ();
15307 result.last_field = field.last_field + fi->baseclasses.size ();
15308 result.parts = create_variant_parts (obstack, offset_map, fi,
15309 field.variant_parts);
15310}
15311
15312/* Fill in a "struct variant_part" for a given variant part. RESULT
15313 is the variant part to fill in. OBSTACK is where any needed
15314 allocations will be done. OFFSET_MAP holds the mapping from
15315 section offsets to fields for the type. FI describes the fields of
15316 the type we're processing. BUILDER is the variant part to be
15317 converted. */
15318
15319static void
15320create_one_variant_part (variant_part &result,
15321 struct obstack *obstack,
15322 const offset_map_type &offset_map,
15323 struct field_info *fi,
15324 const variant_part_builder &builder)
15325{
15326 auto iter = offset_map.find (builder.discriminant_offset);
15327 if (iter == offset_map.end ())
15328 {
15329 result.discriminant_index = -1;
15330 /* Doesn't matter. */
15331 result.is_unsigned = false;
15332 }
15333 else
15334 {
15335 result.discriminant_index = iter->second;
15336 result.is_unsigned
c6d940a9 15337 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
9c6a1327
TT
15338 }
15339
15340 size_t n = builder.variants.size ();
15341 variant *output = new (obstack) variant[n];
15342 for (size_t i = 0; i < n; ++i)
15343 create_one_variant (output[i], obstack, offset_map, fi,
15344 builder.variants[i]);
15345
15346 result.variants = gdb::array_view<variant> (output, n);
15347}
15348
15349/* Create a vector of variant parts that can be attached to a type.
15350 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15351 holds the mapping from section offsets to fields for the type. FI
15352 describes the fields of the type we're processing. VARIANT_PARTS
15353 is the vector to convert. */
15354
15355static const gdb::array_view<variant_part>
15356create_variant_parts (struct obstack *obstack,
15357 const offset_map_type &offset_map,
15358 struct field_info *fi,
15359 const std::vector<variant_part_builder> &variant_parts)
15360{
15361 if (variant_parts.empty ())
15362 return {};
15363
15364 size_t n = variant_parts.size ();
15365 variant_part *result = new (obstack) variant_part[n];
15366 for (size_t i = 0; i < n; ++i)
15367 create_one_variant_part (result[i], obstack, offset_map, fi,
15368 variant_parts[i]);
15369
15370 return gdb::array_view<variant_part> (result, n);
15371}
15372
15373/* Compute the variant part vector for FIP, attaching it to TYPE when
15374 done. */
15375
15376static void
15377add_variant_property (struct field_info *fip, struct type *type,
15378 struct dwarf2_cu *cu)
15379{
15380 /* Map section offsets of fields to their field index. Note the
15381 field index here does not take the number of baseclasses into
15382 account. */
15383 offset_map_type offset_map;
15384 for (int i = 0; i < fip->fields.size (); ++i)
15385 offset_map[fip->fields[i].offset] = i;
15386
5e22e966 15387 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
15388 gdb::array_view<variant_part> parts
15389 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15390 fip->variant_parts);
15391
15392 struct dynamic_prop prop;
8c2e4e06
SM
15393 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15394 obstack_copy (&objfile->objfile_obstack, &parts,
15395 sizeof (parts)));
9c6a1327 15396
5c54719c 15397 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
15398}
15399
c906108c
SS
15400/* Create the vector of fields, and attach it to the type. */
15401
15402static void
fba45db2 15403dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15404 struct dwarf2_cu *cu)
c906108c 15405{
317f7127 15406 int nfields = fip->nfields ();
c906108c
SS
15407
15408 /* Record the field count, allocate space for the array of fields,
15409 and create blank accessibility bitfields if necessary. */
5e33d5f4 15410 type->set_num_fields (nfields);
3cabb6b0
SM
15411 type->set_fields
15412 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 15413
b4ba55a1 15414 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15415 {
15416 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15417
15418 TYPE_FIELD_PRIVATE_BITS (type) =
15419 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15420 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15421
15422 TYPE_FIELD_PROTECTED_BITS (type) =
15423 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15424 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15425
774b6a14
TT
15426 TYPE_FIELD_IGNORE_BITS (type) =
15427 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15428 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15429 }
15430
15431 /* If the type has baseclasses, allocate and clear a bit vector for
15432 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15433 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15434 {
be2daae6 15435 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15436 unsigned char *pointer;
c906108c
SS
15437
15438 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15439 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15440 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15441 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15442 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15443 }
15444
9c6a1327
TT
15445 if (!fip->variant_parts.empty ())
15446 add_variant_property (fip, type, cu);
2ddeaf8a 15447
be2daae6
TT
15448 /* Copy the saved-up fields into the field vector. */
15449 for (int i = 0; i < nfields; ++i)
c906108c 15450 {
be2daae6
TT
15451 struct nextfield &field
15452 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15453 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15454
ceacbf6e 15455 type->field (i) = field.field;
be2daae6 15456 switch (field.accessibility)
c906108c 15457 {
c5aa993b 15458 case DW_ACCESS_private:
b4ba55a1 15459 if (cu->language != language_ada)
be2daae6 15460 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15461 break;
c906108c 15462
c5aa993b 15463 case DW_ACCESS_protected:
b4ba55a1 15464 if (cu->language != language_ada)
be2daae6 15465 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15466 break;
c906108c 15467
c5aa993b
JM
15468 case DW_ACCESS_public:
15469 break;
c906108c 15470
c5aa993b
JM
15471 default:
15472 /* Unknown accessibility. Complain and treat it as public. */
15473 {
b98664d3 15474 complaint (_("unsupported accessibility %d"),
be2daae6 15475 field.accessibility);
c5aa993b
JM
15476 }
15477 break;
c906108c 15478 }
be2daae6 15479 if (i < fip->baseclasses.size ())
c906108c 15480 {
be2daae6 15481 switch (field.virtuality)
c906108c 15482 {
c5aa993b
JM
15483 case DW_VIRTUALITY_virtual:
15484 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15485 if (cu->language == language_ada)
a73c6dcd 15486 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15487 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15488 break;
c906108c
SS
15489 }
15490 }
c906108c
SS
15491 }
15492}
15493
7d27a96d
TT
15494/* Return true if this member function is a constructor, false
15495 otherwise. */
15496
15497static int
15498dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15499{
15500 const char *fieldname;
fe978cb0 15501 const char *type_name;
7d27a96d
TT
15502 int len;
15503
15504 if (die->parent == NULL)
15505 return 0;
15506
15507 if (die->parent->tag != DW_TAG_structure_type
15508 && die->parent->tag != DW_TAG_union_type
15509 && die->parent->tag != DW_TAG_class_type)
15510 return 0;
15511
15512 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15513 type_name = dwarf2_name (die->parent, cu);
15514 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15515 return 0;
15516
15517 len = strlen (fieldname);
fe978cb0
PA
15518 return (strncmp (fieldname, type_name, len) == 0
15519 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15520}
15521
c906108c
SS
15522/* Add a member function to the proper fieldlist. */
15523
15524static void
107d2387 15525dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15526 struct type *type, struct dwarf2_cu *cu)
c906108c 15527{
5e22e966 15528 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15529 struct attribute *attr;
c906108c 15530 int i;
be2daae6 15531 struct fnfieldlist *flp = nullptr;
c906108c 15532 struct fn_field *fnp;
15d034d0 15533 const char *fieldname;
f792889a 15534 struct type *this_type;
c906108c 15535
b4ba55a1 15536 if (cu->language == language_ada)
a73c6dcd 15537 error (_("unexpected member function in Ada type"));
b4ba55a1 15538
2df3850c 15539 /* Get name of member function. */
39cbfefa
DJ
15540 fieldname = dwarf2_name (die, cu);
15541 if (fieldname == NULL)
2df3850c 15542 return;
c906108c 15543
c906108c 15544 /* Look up member function name in fieldlist. */
be2daae6 15545 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15546 {
27bfe10e 15547 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15548 {
15549 flp = &fip->fnfieldlists[i];
15550 break;
15551 }
c906108c
SS
15552 }
15553
be2daae6
TT
15554 /* Create a new fnfieldlist if necessary. */
15555 if (flp == nullptr)
c906108c 15556 {
be2daae6
TT
15557 fip->fnfieldlists.emplace_back ();
15558 flp = &fip->fnfieldlists.back ();
c906108c 15559 flp->name = fieldname;
be2daae6 15560 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15561 }
15562
be2daae6
TT
15563 /* Create a new member function field and add it to the vector of
15564 fnfieldlists. */
15565 flp->fnfields.emplace_back ();
15566 fnp = &flp->fnfields.back ();
3da10d80
KS
15567
15568 /* Delay processing of the physname until later. */
9c37b5ae 15569 if (cu->language == language_cplus)
be2daae6
TT
15570 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15571 die, cu);
3da10d80
KS
15572 else
15573 {
1d06ead6 15574 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15575 fnp->physname = physname ? physname : "";
15576 }
15577
c906108c 15578 fnp->type = alloc_type (objfile);
f792889a 15579 this_type = read_type_die (die, cu);
78134374 15580 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15581 {
1f704f76 15582 int nparams = this_type->num_fields ();
c906108c 15583
f792889a 15584 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15585 of the method itself (TYPE_CODE_METHOD). */
15586 smash_to_method_type (fnp->type, type,
f792889a 15587 TYPE_TARGET_TYPE (this_type),
80fc5e77 15588 this_type->fields (),
1f704f76 15589 this_type->num_fields (),
a409645d 15590 this_type->has_varargs ());
c906108c
SS
15591
15592 /* Handle static member functions.
dda83cd7
SM
15593 Dwarf2 has no clean way to discern C++ static and non-static
15594 member functions. G++ helps GDB by marking the first
15595 parameter for non-static member functions (which is the this
15596 pointer) as artificial. We obtain this information from
15597 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15598 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15599 fnp->voffset = VOFFSET_STATIC;
15600 }
15601 else
b98664d3 15602 complaint (_("member function type missing for '%s'"),
3da10d80 15603 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15604
15605 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15606 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15607 fnp->fcontext = die_containing_type (die, cu);
c906108c 15608
3e43a32a
MS
15609 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15610 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15611
15612 /* Get accessibility. */
bf23a268 15613 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
60d5a603 15614 switch (accessibility)
c906108c 15615 {
60d5a603
JK
15616 case DW_ACCESS_private:
15617 fnp->is_private = 1;
15618 break;
15619 case DW_ACCESS_protected:
15620 fnp->is_protected = 1;
15621 break;
c906108c
SS
15622 }
15623
b02dede2 15624 /* Check for artificial methods. */
e142c38c 15625 attr = dwarf2_attr (die, DW_AT_artificial, cu);
c45bc3f8 15626 if (attr && attr->as_boolean ())
b02dede2
DJ
15627 fnp->is_artificial = 1;
15628
e35000a7
TBA
15629 /* Check for defaulted methods. */
15630 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
e8e5c158
TT
15631 if (attr != nullptr)
15632 fnp->defaulted = attr->defaulted ();
e35000a7
TBA
15633
15634 /* Check for deleted methods. */
15635 attr = dwarf2_attr (die, DW_AT_deleted, cu);
c45bc3f8 15636 if (attr != nullptr && attr->as_boolean ())
e35000a7
TBA
15637 fnp->is_deleted = 1;
15638
7d27a96d
TT
15639 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15640
0d564a31 15641 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15642 function. For older versions of GCC, this is an offset in the
15643 appropriate virtual table, as specified by DW_AT_containing_type.
15644 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15645 to the object address. */
15646
e142c38c 15647 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15648 if (attr != nullptr)
8e19ed76 15649 {
9d2246fc 15650 if (attr->form_is_block () && attr->as_block ()->size > 0)
dda83cd7 15651 {
9d2246fc
TT
15652 struct dwarf_block *block = attr->as_block ();
15653
15654 if (block->data[0] == DW_OP_constu)
aec5aa8b
TT
15655 {
15656 /* Old-style GCC. */
9d2246fc 15657 fnp->voffset = decode_locdesc (block, cu) + 2;
aec5aa8b 15658 }
9d2246fc
TT
15659 else if (block->data[0] == DW_OP_deref
15660 || (block->size > 1
15661 && block->data[0] == DW_OP_deref_size
15662 && block->data[1] == cu->header.addr_size))
aec5aa8b 15663 {
9d2246fc 15664 fnp->voffset = decode_locdesc (block, cu);
aec5aa8b
TT
15665 if ((fnp->voffset % cu->header.addr_size) != 0)
15666 dwarf2_complex_location_expr_complaint ();
15667 else
15668 fnp->voffset /= cu->header.addr_size;
15669 fnp->voffset += 2;
15670 }
15671 else
15672 dwarf2_complex_location_expr_complaint ();
15673
15674 if (!fnp->fcontext)
7e993ebf
KS
15675 {
15676 /* If there is no `this' field and no DW_AT_containing_type,
15677 we cannot actually find a base class context for the
15678 vtable! */
1f704f76 15679 if (this_type->num_fields () == 0
7e993ebf
KS
15680 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15681 {
b98664d3 15682 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15683 "function \"%s\" (offset %s)"),
15684 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15685 }
15686 else
15687 {
15688 fnp->fcontext
940da03e 15689 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15690 }
15691 }
aec5aa8b 15692 }
cd6c91b4 15693 else if (attr->form_is_section_offset ())
dda83cd7 15694 {
4d3c2250 15695 dwarf2_complex_location_expr_complaint ();
dda83cd7 15696 }
8e19ed76 15697 else
dda83cd7 15698 {
4d3c2250
KB
15699 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15700 fieldname);
dda83cd7 15701 }
0d564a31 15702 }
d48cc9dd
DJ
15703 else
15704 {
15705 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
23dca5c3 15706 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
d48cc9dd
DJ
15707 {
15708 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15709 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15710 "but the vtable offset is not specified"),
9d8780f0 15711 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15712 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15713 TYPE_CPLUS_DYNAMIC (type) = 1;
15714 }
15715 }
c906108c
SS
15716}
15717
15718/* Create the vector of member function fields, and attach it to the type. */
15719
15720static void
fba45db2 15721dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15722 struct dwarf2_cu *cu)
c906108c 15723{
b4ba55a1 15724 if (cu->language == language_ada)
a73c6dcd 15725 error (_("unexpected member functions in Ada type"));
b4ba55a1 15726
c906108c
SS
15727 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15728 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15729 TYPE_ALLOC (type,
15730 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15731
be2daae6 15732 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15733 {
be2daae6 15734 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15735 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15736
be2daae6
TT
15737 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15738 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15739 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15740 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15741
15742 for (int k = 0; k < nf.fnfields.size (); ++k)
15743 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15744 }
15745
be2daae6 15746 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15747}
15748
1168df01
JB
15749/* Returns non-zero if NAME is the name of a vtable member in CU's
15750 language, zero otherwise. */
15751static int
15752is_vtable_name (const char *name, struct dwarf2_cu *cu)
15753{
15754 static const char vptr[] = "_vptr";
15755
9c37b5ae
TT
15756 /* Look for the C++ form of the vtable. */
15757 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15758 return 1;
15759
15760 return 0;
15761}
15762
c0dd20ea 15763/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15764 functions, with the ABI-specified layout. If TYPE describes
15765 such a structure, smash it into a member function type.
61049d3b
DJ
15766
15767 GCC shouldn't do this; it should just output pointer to member DIEs.
15768 This is GCC PR debug/28767. */
c0dd20ea 15769
0b92b5bb
TT
15770static void
15771quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15772{
09e2d7c7 15773 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15774
15775 /* Check for a structure with no name and two children. */
1f704f76 15776 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15777 return;
c0dd20ea
DJ
15778
15779 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15780 if (TYPE_FIELD_NAME (type, 0) == NULL
15781 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15782 || TYPE_FIELD_NAME (type, 1) == NULL
15783 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15784 return;
c0dd20ea
DJ
15785
15786 /* Find the type of the method. */
940da03e 15787 pfn_type = type->field (0).type ();
c0dd20ea 15788 if (pfn_type == NULL
78134374
SM
15789 || pfn_type->code () != TYPE_CODE_PTR
15790 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15791 return;
c0dd20ea
DJ
15792
15793 /* Look for the "this" argument. */
15794 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15795 if (pfn_type->num_fields () == 0
940da03e
SM
15796 /* || pfn_type->field (0).type () == NULL */
15797 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15798 return;
c0dd20ea 15799
940da03e 15800 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15801 new_type = alloc_type (objfile);
09e2d7c7 15802 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15803 pfn_type->fields (), pfn_type->num_fields (),
a409645d 15804 pfn_type->has_varargs ());
0b92b5bb 15805 smash_to_methodptr_type (type, new_type);
c0dd20ea 15806}
1168df01 15807
57567375
TT
15808/* While some versions of GCC will generate complicated DWARF for an
15809 array (see quirk_ada_thick_pointer), more recent versions were
15810 modified to emit an explicit thick pointer structure. However, in
15811 this case, the array still has DWARF expressions for its ranges,
15812 and these must be ignored. */
15813
15814static void
15815quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15816 struct type *type)
15817{
15818 gdb_assert (cu->language == language_ada);
15819
15820 /* Check for a structure with two children. */
15821 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15822 return;
15823
15824 /* Check for P_ARRAY and P_BOUNDS members. */
15825 if (TYPE_FIELD_NAME (type, 0) == NULL
15826 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15827 || TYPE_FIELD_NAME (type, 1) == NULL
15828 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15829 return;
15830
15831 /* Make sure we're looking at a pointer to an array. */
15832 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15833 return;
15834 struct type *ary_type = TYPE_TARGET_TYPE (type->field (0).type ());
15835
15836 while (ary_type->code () == TYPE_CODE_ARRAY)
15837 {
15838 /* The Ada code already knows how to handle these types, so all
15839 that we need to do is turn the bounds into static bounds. */
15840 struct type *index_type = ary_type->index_type ();
15841
15842 index_type->bounds ()->low.set_const_val (1);
15843 index_type->bounds ()->high.set_const_val (0);
15844
15845 /* Handle multi-dimensional arrays. */
15846 ary_type = TYPE_TARGET_TYPE (ary_type);
15847 }
15848}
15849
2b4424c3
TT
15850/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15851 appropriate error checking and issuing complaints if there is a
15852 problem. */
15853
15854static ULONGEST
15855get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15856{
15857 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15858
15859 if (attr == nullptr)
15860 return 0;
15861
cd6c91b4 15862 if (!attr->form_is_constant ())
2b4424c3 15863 {
b98664d3 15864 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15865 " - DIE at %s [in module %s]"),
15866 sect_offset_str (die->sect_off),
5e22e966 15867 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15868 return 0;
15869 }
15870
529908cb
TT
15871 LONGEST val = attr->constant_value (0);
15872 if (val < 0)
2b4424c3 15873 {
529908cb
TT
15874 complaint (_("DW_AT_alignment value must not be negative"
15875 " - DIE at %s [in module %s]"),
15876 sect_offset_str (die->sect_off),
15877 objfile_name (cu->per_objfile->objfile));
15878 return 0;
2b4424c3 15879 }
529908cb 15880 ULONGEST align = val;
2b4424c3
TT
15881
15882 if (align == 0)
15883 {
b98664d3 15884 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15885 " - DIE at %s [in module %s]"),
15886 sect_offset_str (die->sect_off),
5e22e966 15887 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15888 return 0;
15889 }
15890 if ((align & (align - 1)) != 0)
15891 {
b98664d3 15892 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15893 " - DIE at %s [in module %s]"),
15894 sect_offset_str (die->sect_off),
5e22e966 15895 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15896 return 0;
15897 }
15898
15899 return align;
15900}
15901
15902/* If the DIE has a DW_AT_alignment attribute, use its value to set
15903 the alignment for TYPE. */
15904
15905static void
15906maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15907 struct type *type)
15908{
15909 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15910 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15911 " - DIE at %s [in module %s]"),
15912 sect_offset_str (die->sect_off),
5e22e966 15913 objfile_name (cu->per_objfile->objfile));
2b4424c3 15914}
685b1105 15915
e35000a7
TBA
15916/* Check if the given VALUE is a valid enum dwarf_calling_convention
15917 constant for a type, according to DWARF5 spec, Table 5.5. */
15918
15919static bool
15920is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15921{
15922 switch (value)
15923 {
15924 case DW_CC_normal:
15925 case DW_CC_pass_by_reference:
15926 case DW_CC_pass_by_value:
15927 return true;
15928
15929 default:
15930 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15931 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15932 return false;
15933 }
15934}
15935
d0922fcf
TBA
15936/* Check if the given VALUE is a valid enum dwarf_calling_convention
15937 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15938 also according to GNU-specific values (see include/dwarf2.h). */
15939
15940static bool
15941is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15942{
15943 switch (value)
15944 {
15945 case DW_CC_normal:
15946 case DW_CC_program:
15947 case DW_CC_nocall:
15948 return true;
15949
15950 case DW_CC_GNU_renesas_sh:
15951 case DW_CC_GNU_borland_fastcall_i386:
15952 case DW_CC_GDB_IBM_OpenCL:
15953 return true;
15954
15955 default:
15956 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15957 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15958 return false;
15959 }
15960}
15961
c906108c 15962/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15963 (definition) to create a type for the structure or union. Fill in
15964 the type's name and general properties; the members will not be
83655187
DE
15965 processed until process_structure_scope. A symbol table entry for
15966 the type will also not be done until process_structure_scope (assuming
15967 the type has a name).
c906108c 15968
c767944b
DJ
15969 NOTE: we need to call these functions regardless of whether or not the
15970 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15971 structure or union. This gets the type entered into our set of
83655187 15972 user defined types. */
c906108c 15973
f792889a 15974static struct type *
134d01f1 15975read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15976{
5e22e966 15977 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15978 struct type *type;
15979 struct attribute *attr;
15d034d0 15980 const char *name;
c906108c 15981
348e048f
DE
15982 /* If the definition of this type lives in .debug_types, read that type.
15983 Don't follow DW_AT_specification though, that will take us back up
15984 the chain and we want to go down. */
052c8bb8 15985 attr = die->attr (DW_AT_signature);
435d3d88 15986 if (attr != nullptr)
348e048f 15987 {
ac9ec31b 15988 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15989
ac9ec31b 15990 /* The type's CU may not be the same as CU.
02142a6c 15991 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15992 return set_die_type (die, type, cu);
15993 }
15994
c0dd20ea 15995 type = alloc_type (objfile);
c906108c 15996 INIT_CPLUS_SPECIFIC (type);
93311388 15997
39cbfefa
DJ
15998 name = dwarf2_name (die, cu);
15999 if (name != NULL)
c906108c 16000 {
987504bb 16001 if (cu->language == language_cplus
c44af4eb
TT
16002 || cu->language == language_d
16003 || cu->language == language_rust)
63d06c5c 16004 {
15d034d0 16005 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
16006
16007 /* dwarf2_full_name might have already finished building the DIE's
16008 type. If so, there is no need to continue. */
16009 if (get_die_type (die, cu) != NULL)
16010 return get_die_type (die, cu);
16011
d0e39ea2 16012 type->set_name (full_name);
63d06c5c
DC
16013 }
16014 else
16015 {
d8151005
DJ
16016 /* The name is already allocated along with this objfile, so
16017 we don't need to duplicate it for the type. */
d0e39ea2 16018 type->set_name (name);
63d06c5c 16019 }
c906108c
SS
16020 }
16021
16022 if (die->tag == DW_TAG_structure_type)
16023 {
67607e24 16024 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16025 }
16026 else if (die->tag == DW_TAG_union_type)
16027 {
67607e24 16028 type->set_code (TYPE_CODE_UNION);
c906108c
SS
16029 }
16030 else
16031 {
67607e24 16032 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16033 }
16034
0cc2414c
TT
16035 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16036 TYPE_DECLARED_CLASS (type) = 1;
16037
e35000a7
TBA
16038 /* Store the calling convention in the type if it's available in
16039 the die. Otherwise the calling convention remains set to
16040 the default value DW_CC_normal. */
16041 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16042 if (attr != nullptr
529908cb 16043 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
e35000a7
TBA
16044 {
16045 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16046 TYPE_CPLUS_CALLING_CONVENTION (type)
529908cb 16047 = (enum dwarf_calling_convention) (attr->constant_value (0));
e35000a7
TBA
16048 }
16049
e142c38c 16050 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16051 if (attr != nullptr)
c906108c 16052 {
cd6c91b4 16053 if (attr->form_is_constant ())
dda83cd7 16054 TYPE_LENGTH (type) = attr->constant_value (0);
155bfbd3
JB
16055 else
16056 {
f8e89861 16057 struct dynamic_prop prop;
293e7e51 16058 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 16059 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
dda83cd7 16060 TYPE_LENGTH (type) = 0;
155bfbd3 16061 }
c906108c
SS
16062 }
16063 else
16064 {
16065 TYPE_LENGTH (type) = 0;
16066 }
16067
2b4424c3
TT
16068 maybe_set_alignment (cu, die, type);
16069
5230b05a 16070 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 16071 {
5230b05a
WT
16072 /* ICC<14 does not output the required DW_AT_declaration on
16073 incomplete types, but gives them a size of zero. */
b4b73759 16074 type->set_is_stub (true);
685b1105
JK
16075 }
16076 else
9baccff6 16077 type->set_stub_is_supported (true);
685b1105 16078
dc718098 16079 if (die_is_declaration (die, cu))
b4b73759 16080 type->set_is_stub (true);
a6c727b2
DJ
16081 else if (attr == NULL && die->child == NULL
16082 && producer_is_realview (cu->producer))
16083 /* RealView does not output the required DW_AT_declaration
16084 on incomplete types. */
b4b73759 16085 type->set_is_stub (true);
dc718098 16086
c906108c
SS
16087 /* We need to add the type field to the die immediately so we don't
16088 infinitely recurse when dealing with pointers to the structure
0963b4bd 16089 type within the structure itself. */
1c379e20 16090 set_die_type (die, type, cu);
c906108c 16091
7e314c57
JK
16092 /* set_die_type should be already done. */
16093 set_descriptive_type (type, die, cu);
16094
c767944b
DJ
16095 return type;
16096}
16097
9c6a1327
TT
16098static void handle_struct_member_die
16099 (struct die_info *child_die,
16100 struct type *type,
16101 struct field_info *fi,
16102 std::vector<struct symbol *> *template_args,
16103 struct dwarf2_cu *cu);
16104
16105/* A helper for handle_struct_member_die that handles
16106 DW_TAG_variant_part. */
16107
16108static void
16109handle_variant_part (struct die_info *die, struct type *type,
16110 struct field_info *fi,
16111 std::vector<struct symbol *> *template_args,
16112 struct dwarf2_cu *cu)
16113{
16114 variant_part_builder *new_part;
16115 if (fi->current_variant_part == nullptr)
16116 {
16117 fi->variant_parts.emplace_back ();
16118 new_part = &fi->variant_parts.back ();
16119 }
16120 else if (!fi->current_variant_part->processing_variant)
16121 {
16122 complaint (_("nested DW_TAG_variant_part seen "
16123 "- DIE at %s [in module %s]"),
16124 sect_offset_str (die->sect_off),
5e22e966 16125 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16126 return;
16127 }
16128 else
16129 {
16130 variant_field &current = fi->current_variant_part->variants.back ();
16131 current.variant_parts.emplace_back ();
16132 new_part = &current.variant_parts.back ();
16133 }
16134
16135 /* When we recurse, we want callees to add to this new variant
16136 part. */
16137 scoped_restore save_current_variant_part
16138 = make_scoped_restore (&fi->current_variant_part, new_part);
16139
16140 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16141 if (discr == NULL)
16142 {
16143 /* It's a univariant form, an extension we support. */
16144 }
16145 else if (discr->form_is_ref ())
16146 {
16147 struct dwarf2_cu *target_cu = cu;
16148 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16149
16150 new_part->discriminant_offset = target_die->sect_off;
16151 }
16152 else
16153 {
16154 complaint (_("DW_AT_discr does not have DIE reference form"
16155 " - DIE at %s [in module %s]"),
16156 sect_offset_str (die->sect_off),
5e22e966 16157 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16158 }
16159
16160 for (die_info *child_die = die->child;
16161 child_die != NULL;
16162 child_die = child_die->sibling)
16163 handle_struct_member_die (child_die, type, fi, template_args, cu);
16164}
16165
16166/* A helper for handle_struct_member_die that handles
16167 DW_TAG_variant. */
16168
16169static void
16170handle_variant (struct die_info *die, struct type *type,
16171 struct field_info *fi,
16172 std::vector<struct symbol *> *template_args,
16173 struct dwarf2_cu *cu)
16174{
16175 if (fi->current_variant_part == nullptr)
16176 {
16177 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16178 "- DIE at %s [in module %s]"),
16179 sect_offset_str (die->sect_off),
5e22e966 16180 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16181 return;
16182 }
16183 if (fi->current_variant_part->processing_variant)
16184 {
16185 complaint (_("nested DW_TAG_variant seen "
16186 "- DIE at %s [in module %s]"),
16187 sect_offset_str (die->sect_off),
5e22e966 16188 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16189 return;
16190 }
16191
16192 scoped_restore save_processing_variant
16193 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16194 true);
16195
16196 fi->current_variant_part->variants.emplace_back ();
16197 variant_field &variant = fi->current_variant_part->variants.back ();
16198 variant.first_field = fi->fields.size ();
16199
16200 /* In a variant we want to get the discriminant and also add a
16201 field for our sole member child. */
16202 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
cae21f8e 16203 if (discr == nullptr || !discr->form_is_constant ())
9c6a1327
TT
16204 {
16205 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
9d2246fc 16206 if (discr == nullptr || discr->as_block ()->size == 0)
9c6a1327
TT
16207 variant.default_branch = true;
16208 else
9d2246fc 16209 variant.discr_list_data = discr->as_block ();
9c6a1327
TT
16210 }
16211 else
cae21f8e 16212 variant.discriminant_value = discr->constant_value (0);
9c6a1327
TT
16213
16214 for (die_info *variant_child = die->child;
16215 variant_child != NULL;
16216 variant_child = variant_child->sibling)
16217 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16218
16219 variant.last_field = fi->fields.size ();
16220}
16221
2ddeaf8a
TT
16222/* A helper for process_structure_scope that handles a single member
16223 DIE. */
16224
16225static void
16226handle_struct_member_die (struct die_info *child_die, struct type *type,
16227 struct field_info *fi,
16228 std::vector<struct symbol *> *template_args,
16229 struct dwarf2_cu *cu)
16230{
16231 if (child_die->tag == DW_TAG_member
9c6a1327 16232 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
16233 {
16234 /* NOTE: carlton/2002-11-05: A C++ static data member
16235 should be a DW_TAG_member that is a declaration, but
16236 all versions of G++ as of this writing (so through at
16237 least 3.2.1) incorrectly generate DW_TAG_variable
16238 tags for them instead. */
16239 dwarf2_add_field (fi, child_die, cu);
16240 }
16241 else if (child_die->tag == DW_TAG_subprogram)
16242 {
16243 /* Rust doesn't have member functions in the C++ sense.
16244 However, it does emit ordinary functions as children
16245 of a struct DIE. */
16246 if (cu->language == language_rust)
16247 read_func_scope (child_die, cu);
16248 else
16249 {
16250 /* C++ member function. */
16251 dwarf2_add_member_fn (fi, child_die, type, cu);
16252 }
16253 }
16254 else if (child_die->tag == DW_TAG_inheritance)
16255 {
16256 /* C++ base class field. */
16257 dwarf2_add_field (fi, child_die, cu);
16258 }
16259 else if (type_can_define_types (child_die))
16260 dwarf2_add_type_defn (fi, child_die, cu);
16261 else if (child_die->tag == DW_TAG_template_type_param
16262 || child_die->tag == DW_TAG_template_value_param)
16263 {
16264 struct symbol *arg = new_symbol (child_die, NULL, cu);
16265
16266 if (arg != NULL)
16267 template_args->push_back (arg);
16268 }
9c6a1327
TT
16269 else if (child_die->tag == DW_TAG_variant_part)
16270 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 16271 else if (child_die->tag == DW_TAG_variant)
9c6a1327 16272 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
16273}
16274
c767944b
DJ
16275/* Finish creating a structure or union type, including filling in
16276 its members and creating a symbol for it. */
16277
16278static void
16279process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16280{
5e22e966 16281 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 16282 struct die_info *child_die;
c767944b
DJ
16283 struct type *type;
16284
16285 type = get_die_type (die, cu);
16286 if (type == NULL)
16287 type = read_structure_type (die, cu);
16288
3e1d3d8c 16289 bool has_template_parameters = false;
e142c38c 16290 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16291 {
16292 struct field_info fi;
2f4732b0 16293 std::vector<struct symbol *> template_args;
c906108c 16294
639d11d3 16295 child_die = die->child;
c906108c
SS
16296
16297 while (child_die && child_die->tag)
16298 {
2ddeaf8a 16299 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 16300 child_die = child_die->sibling;
c906108c
SS
16301 }
16302
34eaf542 16303 /* Attach template arguments to type. */
2f4732b0 16304 if (!template_args.empty ())
34eaf542 16305 {
3e1d3d8c 16306 has_template_parameters = true;
34eaf542 16307 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16308 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16309 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16310 = XOBNEWVEC (&objfile->objfile_obstack,
16311 struct symbol *,
16312 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16313 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16314 template_args.data (),
34eaf542
TT
16315 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16316 * sizeof (struct symbol *)));
34eaf542
TT
16317 }
16318
c906108c 16319 /* Attach fields and member functions to the type. */
317f7127 16320 if (fi.nfields () > 0)
e7c27a73 16321 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16322 if (!fi.fnfieldlists.empty ())
c906108c 16323 {
e7c27a73 16324 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16325
c5aa993b 16326 /* Get the type which refers to the base class (possibly this
c906108c 16327 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16328 class from the DW_AT_containing_type attribute. This use of
16329 DW_AT_containing_type is a GNU extension. */
c906108c 16330
e142c38c 16331 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16332 {
e7c27a73 16333 struct type *t = die_containing_type (die, cu);
c906108c 16334
ae6ae975 16335 set_type_vptr_basetype (type, t);
c906108c
SS
16336 if (type == t)
16337 {
c906108c
SS
16338 int i;
16339
16340 /* Our own class provides vtbl ptr. */
1f704f76 16341 for (i = t->num_fields () - 1;
c906108c
SS
16342 i >= TYPE_N_BASECLASSES (t);
16343 --i)
16344 {
0d5cff50 16345 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16346
dda83cd7 16347 if (is_vtable_name (fieldname, cu))
c906108c 16348 {
ae6ae975 16349 set_type_vptr_fieldno (type, i);
c906108c
SS
16350 break;
16351 }
16352 }
16353
16354 /* Complain if virtual function table field not found. */
16355 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16356 complaint (_("virtual function table pointer "
3e43a32a 16357 "not found when defining class '%s'"),
7d93a1e0 16358 type->name () ? type->name () : "");
c906108c
SS
16359 }
16360 else
16361 {
ae6ae975 16362 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16363 }
16364 }
f6235d4c 16365 else if (cu->producer
61012eef 16366 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16367 {
16368 /* The IBM XLC compiler does not provide direct indication
dda83cd7
SM
16369 of the containing type, but the vtable pointer is
16370 always named __vfp. */
f6235d4c
EZ
16371
16372 int i;
16373
1f704f76 16374 for (i = type->num_fields () - 1;
f6235d4c
EZ
16375 i >= TYPE_N_BASECLASSES (type);
16376 --i)
16377 {
16378 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16379 {
ae6ae975
DE
16380 set_type_vptr_fieldno (type, i);
16381 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16382 break;
16383 }
16384 }
16385 }
c906108c 16386 }
98751a41
JK
16387
16388 /* Copy fi.typedef_field_list linked list elements content into the
16389 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16390 if (!fi.typedef_field_list.empty ())
98751a41 16391 {
be2daae6 16392 int count = fi.typedef_field_list.size ();
98751a41 16393
a0d7a4ff 16394 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16395 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16396 = ((struct decl_field *)
be2daae6
TT
16397 TYPE_ALLOC (type,
16398 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16399 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16400
be2daae6
TT
16401 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16402 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16403 }
c767944b 16404
883fd55a
KS
16405 /* Copy fi.nested_types_list linked list elements content into the
16406 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16407 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16408 {
be2daae6 16409 int count = fi.nested_types_list.size ();
883fd55a
KS
16410
16411 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16412 TYPE_NESTED_TYPES_ARRAY (type)
16413 = ((struct decl_field *)
be2daae6
TT
16414 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16415 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16416
be2daae6
TT
16417 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16418 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16419 }
c906108c 16420 }
63d06c5c 16421
bb5ed363 16422 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16423 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16424 cu->rust_unions.push_back (type);
57567375
TT
16425 else if (cu->language == language_ada)
16426 quirk_ada_thick_pointer_struct (die, cu, type);
0b92b5bb 16427
90aeadfc
DC
16428 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16429 snapshots) has been known to create a die giving a declaration
16430 for a class that has, as a child, a die giving a definition for a
16431 nested class. So we have to process our children even if the
16432 current die is a declaration. Normally, of course, a declaration
16433 won't have any children at all. */
134d01f1 16434
ca040673
DE
16435 child_die = die->child;
16436
90aeadfc
DC
16437 while (child_die != NULL && child_die->tag)
16438 {
16439 if (child_die->tag == DW_TAG_member
16440 || child_die->tag == DW_TAG_variable
34eaf542
TT
16441 || child_die->tag == DW_TAG_inheritance
16442 || child_die->tag == DW_TAG_template_value_param
16443 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16444 {
90aeadfc 16445 /* Do nothing. */
134d01f1 16446 }
90aeadfc
DC
16447 else
16448 process_die (child_die, cu);
134d01f1 16449
436c571c 16450 child_die = child_die->sibling;
134d01f1
DJ
16451 }
16452
fa4028e9
JB
16453 /* Do not consider external references. According to the DWARF standard,
16454 these DIEs are identified by the fact that they have no byte_size
16455 attribute, and a declaration attribute. */
16456 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
16457 || !die_is_declaration (die, cu)
16458 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
16459 {
16460 struct symbol *sym = new_symbol (die, type, cu);
16461
16462 if (has_template_parameters)
16463 {
a776957c
TT
16464 struct symtab *symtab;
16465 if (sym != nullptr)
16466 symtab = symbol_symtab (sym);
16467 else if (cu->line_header != nullptr)
16468 {
16469 /* Any related symtab will do. */
16470 symtab
7ba99d21 16471 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16472 }
16473 else
16474 {
16475 symtab = nullptr;
16476 complaint (_("could not find suitable "
16477 "symtab for template parameter"
16478 " - DIE at %s [in module %s]"),
16479 sect_offset_str (die->sect_off),
16480 objfile_name (objfile));
16481 }
16482
16483 if (symtab != nullptr)
16484 {
16485 /* Make sure that the symtab is set on the new symbols.
16486 Even though they don't appear in this symtab directly,
16487 other parts of gdb assume that symbols do, and this is
16488 reasonably true. */
16489 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16490 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16491 }
3e1d3d8c
TT
16492 }
16493 }
134d01f1
DJ
16494}
16495
ed6acedd
TT
16496/* Assuming DIE is an enumeration type, and TYPE is its associated
16497 type, update TYPE using some information only available in DIE's
16498 children. In particular, the fields are computed. */
55426c9d
JB
16499
16500static void
16501update_enumeration_type_from_children (struct die_info *die,
16502 struct type *type,
16503 struct dwarf2_cu *cu)
16504{
60f7655a 16505 struct die_info *child_die;
55426c9d
JB
16506 int unsigned_enum = 1;
16507 int flag_enum = 1;
55426c9d 16508
8268c778 16509 auto_obstack obstack;
ed6acedd 16510 std::vector<struct field> fields;
55426c9d 16511
60f7655a
DE
16512 for (child_die = die->child;
16513 child_die != NULL && child_die->tag;
436c571c 16514 child_die = child_die->sibling)
55426c9d
JB
16515 {
16516 struct attribute *attr;
16517 LONGEST value;
16518 const gdb_byte *bytes;
16519 struct dwarf2_locexpr_baton *baton;
16520 const char *name;
60f7655a 16521
55426c9d
JB
16522 if (child_die->tag != DW_TAG_enumerator)
16523 continue;
16524
16525 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16526 if (attr == NULL)
16527 continue;
16528
16529 name = dwarf2_name (child_die, cu);
16530 if (name == NULL)
16531 name = "<anonymous enumerator>";
16532
16533 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16534 &value, &bytes, &baton);
16535 if (value < 0)
16536 {
16537 unsigned_enum = 0;
16538 flag_enum = 0;
16539 }
55426c9d 16540 else
edd45eb0
SM
16541 {
16542 if (count_one_bits_ll (value) >= 2)
16543 flag_enum = 0;
edd45eb0 16544 }
55426c9d 16545
ed6acedd
TT
16546 fields.emplace_back ();
16547 struct field &field = fields.back ();
16548 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16549 SET_FIELD_ENUMVAL (field, value);
16550 }
16551
16552 if (!fields.empty ())
16553 {
5e33d5f4 16554 type->set_num_fields (fields.size ());
3cabb6b0
SM
16555 type->set_fields
16556 ((struct field *)
16557 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16558 memcpy (type->fields (), fields.data (),
ed6acedd 16559 sizeof (struct field) * fields.size ());
55426c9d
JB
16560 }
16561
16562 if (unsigned_enum)
653223d3
SM
16563 type->set_is_unsigned (true);
16564
55426c9d
JB
16565 if (flag_enum)
16566 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16567}
16568
134d01f1
DJ
16569/* Given a DW_AT_enumeration_type die, set its type. We do not
16570 complete the type's fields yet, or create any symbols. */
c906108c 16571
f792889a 16572static struct type *
134d01f1 16573read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16574{
5e22e966 16575 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16576 struct type *type;
c906108c 16577 struct attribute *attr;
0114d602 16578 const char *name;
134d01f1 16579
348e048f
DE
16580 /* If the definition of this type lives in .debug_types, read that type.
16581 Don't follow DW_AT_specification though, that will take us back up
16582 the chain and we want to go down. */
052c8bb8 16583 attr = die->attr (DW_AT_signature);
435d3d88 16584 if (attr != nullptr)
348e048f 16585 {
ac9ec31b 16586 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16587
ac9ec31b 16588 /* The type's CU may not be the same as CU.
02142a6c 16589 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16590 return set_die_type (die, type, cu);
16591 }
16592
c906108c
SS
16593 type = alloc_type (objfile);
16594
67607e24 16595 type->set_code (TYPE_CODE_ENUM);
94af9270 16596 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16597 if (name != NULL)
d0e39ea2 16598 type->set_name (name);
c906108c 16599
0626fc76
TT
16600 attr = dwarf2_attr (die, DW_AT_type, cu);
16601 if (attr != NULL)
16602 {
16603 struct type *underlying_type = die_type (die, cu);
16604
16605 TYPE_TARGET_TYPE (type) = underlying_type;
16606 }
16607
e142c38c 16608 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16609 if (attr != nullptr)
c906108c 16610 {
529908cb 16611 TYPE_LENGTH (type) = attr->constant_value (0);
c906108c
SS
16612 }
16613 else
16614 {
16615 TYPE_LENGTH (type) = 0;
16616 }
16617
2b4424c3
TT
16618 maybe_set_alignment (cu, die, type);
16619
137033e9
JB
16620 /* The enumeration DIE can be incomplete. In Ada, any type can be
16621 declared as private in the package spec, and then defined only
16622 inside the package body. Such types are known as Taft Amendment
16623 Types. When another package uses such a type, an incomplete DIE
16624 may be generated by the compiler. */
02eb380e 16625 if (die_is_declaration (die, cu))
b4b73759 16626 type->set_is_stub (true);
02eb380e 16627
0626fc76
TT
16628 /* If this type has an underlying type that is not a stub, then we
16629 may use its attributes. We always use the "unsigned" attribute
16630 in this situation, because ordinarily we guess whether the type
16631 is unsigned -- but the guess can be wrong and the underlying type
16632 can tell us the reality. However, we defer to a local size
16633 attribute if one exists, because this lets the compiler override
16634 the underlying type if needed. */
e46d3488 16635 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
0626fc76 16636 {
9e7c9a03
HD
16637 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16638 underlying_type = check_typedef (underlying_type);
653223d3
SM
16639
16640 type->set_is_unsigned (underlying_type->is_unsigned ());
16641
0626fc76 16642 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16643 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
653223d3 16644
2b4424c3 16645 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16646 && TYPE_RAW_ALIGN (underlying_type) != 0)
16647 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16648 }
16649
3d567982
TT
16650 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16651
ed6acedd
TT
16652 set_die_type (die, type, cu);
16653
16654 /* Finish the creation of this type by using the enum's children.
16655 Note that, as usual, this must come after set_die_type to avoid
16656 infinite recursion when trying to compute the names of the
16657 enumerators. */
16658 update_enumeration_type_from_children (die, type, cu);
16659
16660 return type;
134d01f1
DJ
16661}
16662
16663/* Given a pointer to a die which begins an enumeration, process all
16664 the dies that define the members of the enumeration, and create the
16665 symbol for the enumeration type.
16666
16667 NOTE: We reverse the order of the element list. */
16668
16669static void
16670process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16671{
f792889a 16672 struct type *this_type;
134d01f1 16673
f792889a
DJ
16674 this_type = get_die_type (die, cu);
16675 if (this_type == NULL)
16676 this_type = read_enumeration_type (die, cu);
9dc481d3 16677
639d11d3 16678 if (die->child != NULL)
c906108c 16679 {
9dc481d3 16680 struct die_info *child_die;
15d034d0 16681 const char *name;
9dc481d3 16682
639d11d3 16683 child_die = die->child;
c906108c
SS
16684 while (child_die && child_die->tag)
16685 {
16686 if (child_die->tag != DW_TAG_enumerator)
16687 {
e7c27a73 16688 process_die (child_die, cu);
c906108c
SS
16689 }
16690 else
16691 {
39cbfefa
DJ
16692 name = dwarf2_name (child_die, cu);
16693 if (name)
ed6acedd 16694 new_symbol (child_die, this_type, cu);
c906108c
SS
16695 }
16696
436c571c 16697 child_die = child_die->sibling;
c906108c 16698 }
c906108c 16699 }
134d01f1 16700
6c83ed52
TT
16701 /* If we are reading an enum from a .debug_types unit, and the enum
16702 is a declaration, and the enum is not the signatured type in the
16703 unit, then we do not want to add a symbol for it. Adding a
16704 symbol would in some cases obscure the true definition of the
16705 enum, giving users an incomplete type when the definition is
16706 actually available. Note that we do not want to do this for all
16707 enums which are just declarations, because C++0x allows forward
16708 enum declarations. */
3019eac3 16709 if (cu->per_cu->is_debug_types
6c83ed52
TT
16710 && die_is_declaration (die, cu))
16711 {
52dc124a 16712 struct signatured_type *sig_type;
6c83ed52 16713
c0f78cd4 16714 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16715 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16716 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16717 return;
16718 }
16719
f792889a 16720 new_symbol (die, this_type, cu);
c906108c
SS
16721}
16722
57567375
TT
16723/* Helper function for quirk_ada_thick_pointer that examines a bounds
16724 expression for an index type and finds the corresponding field
16725 offset in the hidden "P_BOUNDS" structure. Returns true on success
16726 and updates *FIELD, false if it fails to recognize an
16727 expression. */
16728
16729static bool
16730recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16731 int *bounds_offset, struct field *field,
16732 struct dwarf2_cu *cu)
16733{
16734 struct attribute *attr = dwarf2_attr (die, name, cu);
16735 if (attr == nullptr || !attr->form_is_block ())
16736 return false;
16737
16738 const struct dwarf_block *block = attr->as_block ();
16739 const gdb_byte *start = block->data;
16740 const gdb_byte *end = block->data + block->size;
16741
16742 /* The expression to recognize generally looks like:
16743
16744 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16745 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16746
16747 However, the second "plus_uconst" may be missing:
16748
16749 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16750 DW_OP_deref_size: 4)
16751
16752 This happens when the field is at the start of the structure.
16753
16754 Also, the final deref may not be sized:
16755
16756 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16757 DW_OP_deref)
16758
16759 This happens when the size of the index type happens to be the
16760 same as the architecture's word size. This can occur with or
16761 without the second plus_uconst. */
16762
16763 if (end - start < 2)
16764 return false;
16765 if (*start++ != DW_OP_push_object_address)
16766 return false;
16767 if (*start++ != DW_OP_plus_uconst)
16768 return false;
16769
16770 uint64_t this_bound_off;
16771 start = gdb_read_uleb128 (start, end, &this_bound_off);
16772 if (start == nullptr || (int) this_bound_off != this_bound_off)
16773 return false;
16774 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16775 is consistent among all bounds. */
16776 if (*bounds_offset == -1)
16777 *bounds_offset = this_bound_off;
16778 else if (*bounds_offset != this_bound_off)
16779 return false;
16780
16781 if (start == end || *start++ != DW_OP_deref)
16782 return false;
16783
16784 int offset = 0;
16785 if (start ==end)
16786 return false;
16787 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16788 {
16789 /* This means an offset of 0. */
16790 }
16791 else if (*start++ != DW_OP_plus_uconst)
16792 return false;
16793 else
16794 {
16795 /* The size is the parameter to DW_OP_plus_uconst. */
16796 uint64_t val;
16797 start = gdb_read_uleb128 (start, end, &val);
16798 if (start == nullptr)
16799 return false;
16800 if ((int) val != val)
16801 return false;
16802 offset = val;
16803 }
16804
16805 if (start == end)
16806 return false;
16807
16808 uint64_t size;
16809 if (*start == DW_OP_deref_size)
16810 {
16811 start = gdb_read_uleb128 (start + 1, end, &size);
16812 if (start == nullptr)
16813 return false;
16814 }
16815 else if (*start == DW_OP_deref)
16816 {
16817 size = cu->header.addr_size;
16818 ++start;
16819 }
16820 else
16821 return false;
16822
16823 SET_FIELD_BITPOS (*field, 8 * offset);
16824 if (size != TYPE_LENGTH (field->type ()))
16825 FIELD_BITSIZE (*field) = 8 * size;
16826
16827 return true;
16828}
16829
16830/* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16831 some kinds of Ada arrays:
16832
16833 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16834 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16835 <11e0> DW_AT_data_location: 2 byte block: 97 6
16836 (DW_OP_push_object_address; DW_OP_deref)
16837 <11e3> DW_AT_type : <0x1173>
16838 <11e7> DW_AT_sibling : <0x1201>
16839 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16840 <11ec> DW_AT_type : <0x1206>
16841 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16842 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16843 DW_OP_deref_size: 4)
16844 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16845 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16846 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16847
16848 This actually represents a "thick pointer", which is a structure
16849 with two elements: one that is a pointer to the array data, and one
16850 that is a pointer to another structure; this second structure holds
16851 the array bounds.
16852
16853 This returns a new type on success, or nullptr if this didn't
16854 recognize the type. */
16855
16856static struct type *
16857quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16858 struct type *type)
16859{
16860 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16861 /* So far we've only seen this with block form. */
16862 if (attr == nullptr || !attr->form_is_block ())
16863 return nullptr;
16864
16865 /* Note that this will fail if the structure layout is changed by
16866 the compiler. However, we have no good way to recognize some
16867 other layout, because we don't know what expression the compiler
16868 might choose to emit should this happen. */
16869 struct dwarf_block *blk = attr->as_block ();
16870 if (blk->size != 2
16871 || blk->data[0] != DW_OP_push_object_address
16872 || blk->data[1] != DW_OP_deref)
16873 return nullptr;
16874
16875 int bounds_offset = -1;
16876 int max_align = -1;
16877 std::vector<struct field> range_fields;
16878 for (struct die_info *child_die = die->child;
16879 child_die;
16880 child_die = child_die->sibling)
16881 {
16882 if (child_die->tag == DW_TAG_subrange_type)
16883 {
16884 struct type *underlying = read_subrange_index_type (child_die, cu);
16885
16886 int this_align = type_align (underlying);
16887 if (this_align > max_align)
16888 max_align = this_align;
16889
16890 range_fields.emplace_back ();
16891 range_fields.emplace_back ();
16892
16893 struct field &lower = range_fields[range_fields.size () - 2];
16894 struct field &upper = range_fields[range_fields.size () - 1];
16895
16896 lower.set_type (underlying);
16897 FIELD_ARTIFICIAL (lower) = 1;
16898
16899 upper.set_type (underlying);
16900 FIELD_ARTIFICIAL (upper) = 1;
16901
16902 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16903 &bounds_offset, &lower, cu)
16904 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16905 &bounds_offset, &upper, cu))
16906 return nullptr;
16907 }
16908 }
16909
16910 /* This shouldn't really happen, but double-check that we found
16911 where the bounds are stored. */
16912 if (bounds_offset == -1)
16913 return nullptr;
16914
16915 struct objfile *objfile = cu->per_objfile->objfile;
16916 for (int i = 0; i < range_fields.size (); i += 2)
16917 {
16918 char name[20];
16919
16920 /* Set the name of each field in the bounds. */
16921 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16922 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16923 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16924 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16925 }
16926
16927 struct type *bounds = alloc_type (objfile);
16928 bounds->set_code (TYPE_CODE_STRUCT);
16929
16930 bounds->set_num_fields (range_fields.size ());
16931 bounds->set_fields
16932 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16933 * sizeof (struct field))));
16934 memcpy (bounds->fields (), range_fields.data (),
16935 bounds->num_fields () * sizeof (struct field));
16936
16937 int last_fieldno = range_fields.size () - 1;
16938 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16939 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16940 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16941
16942 /* Rewrite the existing array type in place. Specifically, we
16943 remove any dynamic properties we might have read, and we replace
16944 the index types. */
16945 struct type *iter = type;
16946 for (int i = 0; i < range_fields.size (); i += 2)
16947 {
16948 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16949 iter->main_type->dyn_prop_list = nullptr;
16950 iter->set_index_type
16951 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16952 iter = TYPE_TARGET_TYPE (iter);
16953 }
16954
16955 struct type *result = alloc_type (objfile);
16956 result->set_code (TYPE_CODE_STRUCT);
16957
16958 result->set_num_fields (2);
16959 result->set_fields
16960 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16961 * sizeof (struct field))));
16962
16963 /* The names are chosen to coincide with what the compiler does with
16964 -fgnat-encodings=all, which the Ada code in gdb already
16965 understands. */
16966 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16967 result->field (0).set_type (lookup_pointer_type (type));
16968
16969 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16970 result->field (1).set_type (lookup_pointer_type (bounds));
16971 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16972
16973 result->set_name (type->name ());
16974 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16975 + TYPE_LENGTH (result->field (1).type ()));
16976
16977 return result;
16978}
16979
c906108c
SS
16980/* Extract all information from a DW_TAG_array_type DIE and put it in
16981 the DIE's type field. For now, this only handles one dimensional
16982 arrays. */
16983
f792889a 16984static struct type *
e7c27a73 16985read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16986{
5e22e966 16987 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16988 struct die_info *child_die;
7e314c57 16989 struct type *type;
c906108c 16990 struct type *element_type, *range_type, *index_type;
c906108c 16991 struct attribute *attr;
15d034d0 16992 const char *name;
a405673c 16993 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16994 unsigned int bit_stride = 0;
c906108c 16995
e7c27a73 16996 element_type = die_type (die, cu);
c906108c 16997
7e314c57
JK
16998 /* The die_type call above may have already set the type for this DIE. */
16999 type = get_die_type (die, cu);
17000 if (type)
17001 return type;
17002
dc53a7ad
JB
17003 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17004 if (attr != NULL)
a405673c
JB
17005 {
17006 int stride_ok;
293e7e51 17007 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
17008
17009 byte_stride_prop
17010 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
17011 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17012 prop_type);
a405673c
JB
17013 if (!stride_ok)
17014 {
b98664d3 17015 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
17016 " - DIE at %s [in module %s]"),
17017 sect_offset_str (die->sect_off),
5e22e966 17018 objfile_name (cu->per_objfile->objfile));
a405673c
JB
17019 /* Ignore this attribute. We will likely not be able to print
17020 arrays of this type correctly, but there is little we can do
17021 to help if we cannot read the attribute's value. */
17022 byte_stride_prop = NULL;
17023 }
17024 }
dc53a7ad
JB
17025
17026 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17027 if (attr != NULL)
529908cb 17028 bit_stride = attr->constant_value (0);
dc53a7ad 17029
c906108c
SS
17030 /* Irix 6.2 native cc creates array types without children for
17031 arrays with unspecified length. */
639d11d3 17032 if (die->child == NULL)
c906108c 17033 {
46bf5051 17034 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17035 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 17036 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 17037 byte_stride_prop, bit_stride);
f792889a 17038 return set_die_type (die, type, cu);
c906108c
SS
17039 }
17040
791afaa2 17041 std::vector<struct type *> range_types;
639d11d3 17042 child_die = die->child;
c906108c
SS
17043 while (child_die && child_die->tag)
17044 {
17045 if (child_die->tag == DW_TAG_subrange_type)
17046 {
f792889a 17047 struct type *child_type = read_type_die (child_die, cu);
9a619af0 17048
dda83cd7
SM
17049 if (child_type != NULL)
17050 {
0963b4bd 17051 /* The range type was succesfully read. Save it for the
dda83cd7 17052 array type creation. */
791afaa2 17053 range_types.push_back (child_type);
dda83cd7 17054 }
c906108c 17055 }
436c571c 17056 child_die = child_die->sibling;
c906108c
SS
17057 }
17058
17059 /* Dwarf2 dimensions are output from left to right, create the
17060 necessary array types in backwards order. */
7ca2d3a3 17061
c906108c 17062 type = element_type;
7ca2d3a3
DL
17063
17064 if (read_array_order (die, cu) == DW_ORD_col_major)
17065 {
17066 int i = 0;
9a619af0 17067
791afaa2 17068 while (i < range_types.size ())
10f6a3ad
TT
17069 {
17070 type = create_array_type_with_stride (NULL, type, range_types[i++],
17071 byte_stride_prop, bit_stride);
17072 bit_stride = 0;
17073 byte_stride_prop = nullptr;
17074 }
7ca2d3a3
DL
17075 }
17076 else
17077 {
791afaa2 17078 size_t ndim = range_types.size ();
7ca2d3a3 17079 while (ndim-- > 0)
10f6a3ad
TT
17080 {
17081 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17082 byte_stride_prop, bit_stride);
17083 bit_stride = 0;
17084 byte_stride_prop = nullptr;
17085 }
7ca2d3a3 17086 }
c906108c 17087
f5f8a009
EZ
17088 /* Understand Dwarf2 support for vector types (like they occur on
17089 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17090 array type. This is not part of the Dwarf2/3 standard yet, but a
17091 custom vendor extension. The main difference between a regular
17092 array and the vector variant is that vectors are passed by value
17093 to functions. */
e142c38c 17094 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 17095 if (attr != nullptr)
ea37ba09 17096 make_vector_type (type);
f5f8a009 17097
dbc98a8b
KW
17098 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17099 implementation may choose to implement triple vectors using this
17100 attribute. */
17101 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb 17102 if (attr != nullptr && attr->form_is_unsigned ())
dbc98a8b 17103 {
529908cb
TT
17104 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17105 TYPE_LENGTH (type) = attr->as_unsigned ();
dbc98a8b 17106 else
b98664d3 17107 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 17108 "than the total size of elements"));
dbc98a8b
KW
17109 }
17110
39cbfefa
DJ
17111 name = dwarf2_name (die, cu);
17112 if (name)
d0e39ea2 17113 type->set_name (name);
6e70227d 17114
2b4424c3
TT
17115 maybe_set_alignment (cu, die, type);
17116
57567375
TT
17117 struct type *replacement_type = nullptr;
17118 if (cu->language == language_ada)
17119 {
17120 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17121 if (replacement_type != nullptr)
17122 type = replacement_type;
17123 }
17124
0963b4bd 17125 /* Install the type in the die. */
57567375 17126 set_die_type (die, type, cu, replacement_type != nullptr);
7e314c57
JK
17127
17128 /* set_die_type should be already done. */
b4ba55a1
JB
17129 set_descriptive_type (type, die, cu);
17130
7e314c57 17131 return type;
c906108c
SS
17132}
17133
7ca2d3a3 17134static enum dwarf_array_dim_ordering
6e70227d 17135read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
17136{
17137 struct attribute *attr;
17138
17139 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17140
435d3d88 17141 if (attr != nullptr)
1bc397c5
TT
17142 {
17143 LONGEST val = attr->constant_value (-1);
17144 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17145 return (enum dwarf_array_dim_ordering) val;
17146 }
7ca2d3a3 17147
0963b4bd
MS
17148 /* GNU F77 is a special case, as at 08/2004 array type info is the
17149 opposite order to the dwarf2 specification, but data is still
17150 laid out as per normal fortran.
7ca2d3a3 17151
0963b4bd
MS
17152 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17153 version checking. */
7ca2d3a3 17154
905e0470
PM
17155 if (cu->language == language_fortran
17156 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
17157 {
17158 return DW_ORD_row_major;
17159 }
17160
3a3440fb 17161 switch (cu->language_defn->array_ordering ())
7ca2d3a3
DL
17162 {
17163 case array_column_major:
17164 return DW_ORD_col_major;
17165 case array_row_major:
17166 default:
17167 return DW_ORD_row_major;
17168 };
17169}
17170
72019c9c 17171/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 17172 the DIE's type field. */
72019c9c 17173
f792889a 17174static struct type *
72019c9c
GM
17175read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17176{
7e314c57
JK
17177 struct type *domain_type, *set_type;
17178 struct attribute *attr;
f792889a 17179
7e314c57
JK
17180 domain_type = die_type (die, cu);
17181
17182 /* The die_type call above may have already set the type for this DIE. */
17183 set_type = get_die_type (die, cu);
17184 if (set_type)
17185 return set_type;
17186
17187 set_type = create_set_type (NULL, domain_type);
17188
17189 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb
TT
17190 if (attr != nullptr && attr->form_is_unsigned ())
17191 TYPE_LENGTH (set_type) = attr->as_unsigned ();
7e314c57 17192
2b4424c3
TT
17193 maybe_set_alignment (cu, die, set_type);
17194
f792889a 17195 return set_die_type (die, set_type, cu);
72019c9c 17196}
7ca2d3a3 17197
0971de02
TT
17198/* A helper for read_common_block that creates a locexpr baton.
17199 SYM is the symbol which we are marking as computed.
17200 COMMON_DIE is the DIE for the common block.
17201 COMMON_LOC is the location expression attribute for the common
17202 block itself.
17203 MEMBER_LOC is the location expression attribute for the particular
17204 member of the common block that we are processing.
17205 CU is the CU from which the above come. */
17206
17207static void
17208mark_common_block_symbol_computed (struct symbol *sym,
17209 struct die_info *common_die,
17210 struct attribute *common_loc,
17211 struct attribute *member_loc,
17212 struct dwarf2_cu *cu)
17213{
5e22e966 17214 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 17215 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
17216 struct dwarf2_locexpr_baton *baton;
17217 gdb_byte *ptr;
17218 unsigned int cu_off;
08feed99 17219 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
17220 LONGEST offset = 0;
17221
17222 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
17223 gdb_assert (common_loc->form_is_block ());
17224 gdb_assert (member_loc->form_is_block ()
cd6c91b4 17225 || member_loc->form_is_constant ());
0971de02 17226
8d749320 17227 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 17228 baton->per_objfile = per_objfile;
0971de02
TT
17229 baton->per_cu = cu->per_cu;
17230 gdb_assert (baton->per_cu);
17231
17232 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17233
cd6c91b4 17234 if (member_loc->form_is_constant ())
0971de02 17235 {
0826b30a 17236 offset = member_loc->constant_value (0);
0971de02
TT
17237 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17238 }
17239 else
9d2246fc 17240 baton->size += member_loc->as_block ()->size;
0971de02 17241
224c3ddb 17242 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
17243 baton->data = ptr;
17244
17245 *ptr++ = DW_OP_call4;
9c541725 17246 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
17247 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17248 ptr += 4;
17249
cd6c91b4 17250 if (member_loc->form_is_constant ())
0971de02
TT
17251 {
17252 *ptr++ = DW_OP_addr;
17253 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17254 ptr += cu->header.addr_size;
17255 }
17256 else
17257 {
17258 /* We have to copy the data here, because DW_OP_call4 will only
17259 use a DW_AT_location attribute. */
9d2246fc
TT
17260 struct dwarf_block *block = member_loc->as_block ();
17261 memcpy (ptr, block->data, block->size);
17262 ptr += block->size;
0971de02
TT
17263 }
17264
17265 *ptr++ = DW_OP_plus;
17266 gdb_assert (ptr - baton->data == baton->size);
17267
0971de02 17268 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 17269 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
17270}
17271
4357ac6c
TT
17272/* Create appropriate locally-scoped variables for all the
17273 DW_TAG_common_block entries. Also create a struct common_block
17274 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 17275 is used to separate the common blocks name namespace from regular
4357ac6c 17276 variable names. */
c906108c
SS
17277
17278static void
e7c27a73 17279read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17280{
0971de02
TT
17281 struct attribute *attr;
17282
17283 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 17284 if (attr != nullptr)
0971de02
TT
17285 {
17286 /* Support the .debug_loc offsets. */
4fc6c0d5 17287 if (attr->form_is_block ())
dda83cd7 17288 {
0971de02 17289 /* Ok. */
dda83cd7 17290 }
cd6c91b4 17291 else if (attr->form_is_section_offset ())
dda83cd7 17292 {
0971de02
TT
17293 dwarf2_complex_location_expr_complaint ();
17294 attr = NULL;
dda83cd7 17295 }
0971de02 17296 else
dda83cd7 17297 {
0971de02
TT
17298 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17299 "common block member");
17300 attr = NULL;
dda83cd7 17301 }
0971de02
TT
17302 }
17303
639d11d3 17304 if (die->child != NULL)
c906108c 17305 {
5e22e966 17306 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
17307 struct die_info *child_die;
17308 size_t n_entries = 0, size;
17309 struct common_block *common_block;
17310 struct symbol *sym;
74ac6d43 17311
4357ac6c
TT
17312 for (child_die = die->child;
17313 child_die && child_die->tag;
436c571c 17314 child_die = child_die->sibling)
4357ac6c
TT
17315 ++n_entries;
17316
17317 size = (sizeof (struct common_block)
17318 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
17319 common_block
17320 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17321 size);
4357ac6c
TT
17322 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17323 common_block->n_entries = 0;
17324
17325 for (child_die = die->child;
17326 child_die && child_die->tag;
436c571c 17327 child_die = child_die->sibling)
4357ac6c
TT
17328 {
17329 /* Create the symbol in the DW_TAG_common_block block in the current
17330 symbol scope. */
e7c27a73 17331 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
17332 if (sym != NULL)
17333 {
17334 struct attribute *member_loc;
17335
17336 common_block->contents[common_block->n_entries++] = sym;
17337
17338 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17339 cu);
17340 if (member_loc)
17341 {
17342 /* GDB has handled this for a long time, but it is
17343 not specified by DWARF. It seems to have been
17344 emitted by gfortran at least as recently as:
17345 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 17346 complaint (_("Variable in common block has "
0971de02 17347 "DW_AT_data_member_location "
9d8780f0
SM
17348 "- DIE at %s [in module %s]"),
17349 sect_offset_str (child_die->sect_off),
518817b3 17350 objfile_name (objfile));
0971de02 17351
cd6c91b4 17352 if (member_loc->form_is_section_offset ())
0971de02 17353 dwarf2_complex_location_expr_complaint ();
cd6c91b4 17354 else if (member_loc->form_is_constant ()
4fc6c0d5 17355 || member_loc->form_is_block ())
0971de02 17356 {
435d3d88 17357 if (attr != nullptr)
0971de02
TT
17358 mark_common_block_symbol_computed (sym, die, attr,
17359 member_loc, cu);
17360 }
17361 else
17362 dwarf2_complex_location_expr_complaint ();
17363 }
17364 }
c906108c 17365 }
4357ac6c
TT
17366
17367 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17368 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
17369 }
17370}
17371
0114d602 17372/* Create a type for a C++ namespace. */
d9fa45fe 17373
0114d602
DJ
17374static struct type *
17375read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 17376{
5e22e966 17377 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17378 const char *previous_prefix, *name;
9219021c 17379 int is_anonymous;
0114d602
DJ
17380 struct type *type;
17381
17382 /* For extensions, reuse the type of the original namespace. */
17383 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17384 {
17385 struct die_info *ext_die;
17386 struct dwarf2_cu *ext_cu = cu;
9a619af0 17387
0114d602
DJ
17388 ext_die = dwarf2_extension (die, &ext_cu);
17389 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
17390
17391 /* EXT_CU may not be the same as CU.
02142a6c 17392 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
17393 return set_die_type (die, type, cu);
17394 }
9219021c 17395
e142c38c 17396 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
17397
17398 /* Now build the name of the current namespace. */
17399
0114d602
DJ
17400 previous_prefix = determine_prefix (die, cu);
17401 if (previous_prefix[0] != '\0')
17402 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 17403 previous_prefix, name, 0, cu);
0114d602
DJ
17404
17405 /* Create the type. */
19f392bc 17406 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 17407
60531b24 17408 return set_die_type (die, type, cu);
0114d602
DJ
17409}
17410
22cee43f 17411/* Read a namespace scope. */
0114d602
DJ
17412
17413static void
17414read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17415{
5e22e966 17416 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17417 int is_anonymous;
9219021c 17418
5c4e30ca
DC
17419 /* Add a symbol associated to this if we haven't seen the namespace
17420 before. Also, add a using directive if it's an anonymous
17421 namespace. */
9219021c 17422
f2f0e013 17423 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17424 {
17425 struct type *type;
17426
0114d602 17427 type = read_type_die (die, cu);
e7c27a73 17428 new_symbol (die, type, cu);
5c4e30ca 17429
e8e80198 17430 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17431 if (is_anonymous)
0114d602
DJ
17432 {
17433 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17434
eb1e02fd 17435 std::vector<const char *> excludes;
804d2729 17436 add_using_directive (using_directives (cu),
7d93a1e0 17437 previous_prefix, type->name (), NULL,
eb1e02fd 17438 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17439 }
5c4e30ca 17440 }
9219021c 17441
639d11d3 17442 if (die->child != NULL)
d9fa45fe 17443 {
639d11d3 17444 struct die_info *child_die = die->child;
6e70227d 17445
d9fa45fe
DC
17446 while (child_die && child_die->tag)
17447 {
e7c27a73 17448 process_die (child_die, cu);
436c571c 17449 child_die = child_die->sibling;
d9fa45fe
DC
17450 }
17451 }
38d518c9
EZ
17452}
17453
f55ee35c
JK
17454/* Read a Fortran module as type. This DIE can be only a declaration used for
17455 imported module. Still we need that type as local Fortran "use ... only"
17456 declaration imports depend on the created type in determine_prefix. */
17457
17458static struct type *
17459read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17460{
5e22e966 17461 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 17462 const char *module_name;
f55ee35c
JK
17463 struct type *type;
17464
17465 module_name = dwarf2_name (die, cu);
19f392bc 17466 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17467
f55ee35c
JK
17468 return set_die_type (die, type, cu);
17469}
17470
5d7cb8df
JK
17471/* Read a Fortran module. */
17472
17473static void
17474read_module (struct die_info *die, struct dwarf2_cu *cu)
17475{
17476 struct die_info *child_die = die->child;
530e8392
KB
17477 struct type *type;
17478
17479 type = read_type_die (die, cu);
17480 new_symbol (die, type, cu);
5d7cb8df 17481
5d7cb8df
JK
17482 while (child_die && child_die->tag)
17483 {
17484 process_die (child_die, cu);
436c571c 17485 child_die = child_die->sibling;
5d7cb8df
JK
17486 }
17487}
17488
38d518c9
EZ
17489/* Return the name of the namespace represented by DIE. Set
17490 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17491 namespace. */
17492
17493static const char *
e142c38c 17494namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17495{
17496 struct die_info *current_die;
17497 const char *name = NULL;
17498
17499 /* Loop through the extensions until we find a name. */
17500
17501 for (current_die = die;
17502 current_die != NULL;
f2f0e013 17503 current_die = dwarf2_extension (die, &cu))
38d518c9 17504 {
96553a0c
DE
17505 /* We don't use dwarf2_name here so that we can detect the absence
17506 of a name -> anonymous namespace. */
7d45c7c3 17507 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17508
38d518c9
EZ
17509 if (name != NULL)
17510 break;
17511 }
17512
17513 /* Is it an anonymous namespace? */
17514
17515 *is_anonymous = (name == NULL);
17516 if (*is_anonymous)
2b1dbab0 17517 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17518
17519 return name;
d9fa45fe
DC
17520}
17521
c906108c
SS
17522/* Extract all information from a DW_TAG_pointer_type DIE and add to
17523 the user defined type vector. */
17524
f792889a 17525static struct type *
e7c27a73 17526read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17527{
5e22e966 17528 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 17529 struct comp_unit_head *cu_header = &cu->header;
c906108c 17530 struct type *type;
8b2dbe47
KB
17531 struct attribute *attr_byte_size;
17532 struct attribute *attr_address_class;
17533 int byte_size, addr_class;
7e314c57
JK
17534 struct type *target_type;
17535
17536 target_type = die_type (die, cu);
c906108c 17537
7e314c57
JK
17538 /* The die_type call above may have already set the type for this DIE. */
17539 type = get_die_type (die, cu);
17540 if (type)
17541 return type;
17542
17543 type = lookup_pointer_type (target_type);
8b2dbe47 17544
e142c38c 17545 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47 17546 if (attr_byte_size)
529908cb 17547 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
c906108c 17548 else
8b2dbe47
KB
17549 byte_size = cu_header->addr_size;
17550
e142c38c 17551 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47 17552 if (attr_address_class)
529908cb 17553 addr_class = attr_address_class->constant_value (DW_ADDR_none);
8b2dbe47
KB
17554 else
17555 addr_class = DW_ADDR_none;
17556
2b4424c3
TT
17557 ULONGEST alignment = get_alignment (cu, die);
17558
17559 /* If the pointer size, alignment, or address class is different
17560 than the default, create a type variant marked as such and set
17561 the length accordingly. */
17562 if (TYPE_LENGTH (type) != byte_size
17563 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17564 && alignment != TYPE_RAW_ALIGN (type))
17565 || addr_class != DW_ADDR_none)
c906108c 17566 {
5e2b427d 17567 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47 17568 {
314ad88d
PA
17569 type_instance_flags type_flags
17570 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17571 addr_class);
876cecd0
TT
17572 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17573 == 0);
8b2dbe47
KB
17574 type = make_type_with_address_space (type, type_flags);
17575 }
17576 else if (TYPE_LENGTH (type) != byte_size)
17577 {
b98664d3 17578 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17579 }
2b4424c3
TT
17580 else if (TYPE_RAW_ALIGN (type) != alignment)
17581 {
b98664d3 17582 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17583 " - DIE at %s [in module %s]"),
17584 sect_offset_str (die->sect_off),
5e22e966 17585 objfile_name (cu->per_objfile->objfile));
2b4424c3 17586 }
6e70227d 17587 else
9a619af0
MS
17588 {
17589 /* Should we also complain about unhandled address classes? */
17590 }
c906108c 17591 }
8b2dbe47
KB
17592
17593 TYPE_LENGTH (type) = byte_size;
2b4424c3 17594 set_type_align (type, alignment);
f792889a 17595 return set_die_type (die, type, cu);
c906108c
SS
17596}
17597
17598/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17599 the user defined type vector. */
17600
f792889a 17601static struct type *
e7c27a73 17602read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17603{
17604 struct type *type;
17605 struct type *to_type;
17606 struct type *domain;
17607
e7c27a73
DJ
17608 to_type = die_type (die, cu);
17609 domain = die_containing_type (die, cu);
0d5de010 17610
7e314c57
JK
17611 /* The calls above may have already set the type for this DIE. */
17612 type = get_die_type (die, cu);
17613 if (type)
17614 return type;
17615
78134374 17616 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 17617 type = lookup_methodptr_type (to_type);
78134374 17618 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 17619 {
5e22e966 17620 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
17621
17622 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 17623 to_type->fields (), to_type->num_fields (),
a409645d 17624 to_type->has_varargs ());
7078baeb
TT
17625 type = lookup_methodptr_type (new_type);
17626 }
0d5de010
DJ
17627 else
17628 type = lookup_memberptr_type (to_type, domain);
c906108c 17629
f792889a 17630 return set_die_type (die, type, cu);
c906108c
SS
17631}
17632
4297a3f0 17633/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17634 the user defined type vector. */
17635
f792889a 17636static struct type *
4297a3f0 17637read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
dda83cd7 17638 enum type_code refcode)
c906108c 17639{
e7c27a73 17640 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17641 struct type *type, *target_type;
c906108c
SS
17642 struct attribute *attr;
17643
4297a3f0
AV
17644 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17645
7e314c57
JK
17646 target_type = die_type (die, cu);
17647
17648 /* The die_type call above may have already set the type for this DIE. */
17649 type = get_die_type (die, cu);
17650 if (type)
17651 return type;
17652
4297a3f0 17653 type = lookup_reference_type (target_type, refcode);
e142c38c 17654 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17655 if (attr != nullptr)
c906108c 17656 {
529908cb 17657 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
c906108c
SS
17658 }
17659 else
17660 {
107d2387 17661 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17662 }
2b4424c3 17663 maybe_set_alignment (cu, die, type);
f792889a 17664 return set_die_type (die, type, cu);
c906108c
SS
17665}
17666
cf363f18
MW
17667/* Add the given cv-qualifiers to the element type of the array. GCC
17668 outputs DWARF type qualifiers that apply to an array, not the
17669 element type. But GDB relies on the array element type to carry
17670 the cv-qualifiers. This mimics section 6.7.3 of the C99
17671 specification. */
17672
17673static struct type *
17674add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17675 struct type *base_type, int cnst, int voltl)
17676{
17677 struct type *el_type, *inner_array;
17678
17679 base_type = copy_type (base_type);
17680 inner_array = base_type;
17681
78134374 17682 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17683 {
17684 TYPE_TARGET_TYPE (inner_array) =
17685 copy_type (TYPE_TARGET_TYPE (inner_array));
17686 inner_array = TYPE_TARGET_TYPE (inner_array);
17687 }
17688
17689 el_type = TYPE_TARGET_TYPE (inner_array);
17690 cnst |= TYPE_CONST (el_type);
17691 voltl |= TYPE_VOLATILE (el_type);
17692 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17693
17694 return set_die_type (die, base_type, cu);
17695}
17696
f792889a 17697static struct type *
e7c27a73 17698read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17699{
f792889a 17700 struct type *base_type, *cv_type;
c906108c 17701
e7c27a73 17702 base_type = die_type (die, cu);
7e314c57
JK
17703
17704 /* The die_type call above may have already set the type for this DIE. */
17705 cv_type = get_die_type (die, cu);
17706 if (cv_type)
17707 return cv_type;
17708
2f608a3a
KW
17709 /* In case the const qualifier is applied to an array type, the element type
17710 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 17711 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 17712 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17713
f792889a
DJ
17714 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17715 return set_die_type (die, cv_type, cu);
c906108c
SS
17716}
17717
f792889a 17718static struct type *
e7c27a73 17719read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17720{
f792889a 17721 struct type *base_type, *cv_type;
c906108c 17722
e7c27a73 17723 base_type = die_type (die, cu);
7e314c57
JK
17724
17725 /* The die_type call above may have already set the type for this DIE. */
17726 cv_type = get_die_type (die, cu);
17727 if (cv_type)
17728 return cv_type;
17729
cf363f18
MW
17730 /* In case the volatile qualifier is applied to an array type, the
17731 element type is so qualified, not the array type (section 6.7.3
17732 of C99). */
78134374 17733 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17734 return add_array_cv_type (die, cu, base_type, 0, 1);
17735
f792889a
DJ
17736 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17737 return set_die_type (die, cv_type, cu);
c906108c
SS
17738}
17739
06d66ee9
TT
17740/* Handle DW_TAG_restrict_type. */
17741
17742static struct type *
17743read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17744{
17745 struct type *base_type, *cv_type;
17746
17747 base_type = die_type (die, cu);
17748
17749 /* The die_type call above may have already set the type for this DIE. */
17750 cv_type = get_die_type (die, cu);
17751 if (cv_type)
17752 return cv_type;
17753
17754 cv_type = make_restrict_type (base_type);
17755 return set_die_type (die, cv_type, cu);
17756}
17757
a2c2acaf
MW
17758/* Handle DW_TAG_atomic_type. */
17759
17760static struct type *
17761read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17762{
17763 struct type *base_type, *cv_type;
17764
17765 base_type = die_type (die, cu);
17766
17767 /* The die_type call above may have already set the type for this DIE. */
17768 cv_type = get_die_type (die, cu);
17769 if (cv_type)
17770 return cv_type;
17771
17772 cv_type = make_atomic_type (base_type);
17773 return set_die_type (die, cv_type, cu);
17774}
17775
c906108c
SS
17776/* Extract all information from a DW_TAG_string_type DIE and add to
17777 the user defined type vector. It isn't really a user defined type,
17778 but it behaves like one, with other DIE's using an AT_user_def_type
17779 attribute to reference it. */
17780
f792889a 17781static struct type *
e7c27a73 17782read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17783{
5e22e966 17784 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17785 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17786 struct type *type, *range_type, *index_type, *char_type;
17787 struct attribute *attr;
216a7e6b
AB
17788 struct dynamic_prop prop;
17789 bool length_is_constant = true;
17790 LONGEST length;
17791
17792 /* There are a couple of places where bit sizes might be made use of
17793 when parsing a DW_TAG_string_type, however, no producer that we know
17794 of make use of these. Handling bit sizes that are a multiple of the
17795 byte size is easy enough, but what about other bit sizes? Lets deal
17796 with that problem when we have to. Warn about these attributes being
17797 unsupported, then parse the type and ignore them like we always
17798 have. */
17799 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17800 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17801 {
17802 static bool warning_printed = false;
17803 if (!warning_printed)
17804 {
17805 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17806 "currently supported on DW_TAG_string_type."));
17807 warning_printed = true;
17808 }
17809 }
c906108c 17810
e142c38c 17811 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17812 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17813 {
17814 /* The string length describes the location at which the length of
17815 the string can be found. The size of the length field can be
17816 specified with one of the attributes below. */
17817 struct type *prop_type;
17818 struct attribute *len
17819 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17820 if (len == nullptr)
17821 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17822 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17823 {
17824 /* Pass 0 as the default as we know this attribute is constant
17825 and the default value will not be returned. */
0826b30a 17826 LONGEST sz = len->constant_value (0);
293e7e51 17827 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17828 }
17829 else
17830 {
17831 /* If the size is not specified then we assume it is the size of
17832 an address on this target. */
293e7e51 17833 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17834 }
17835
17836 /* Convert the attribute into a dynamic property. */
17837 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17838 length = 1;
17839 else
17840 length_is_constant = false;
17841 }
17842 else if (attr != nullptr)
17843 {
17844 /* This DW_AT_string_length just contains the length with no
17845 indirection. There's no need to create a dynamic property in this
17846 case. Pass 0 for the default value as we know it will not be
17847 returned in this case. */
0826b30a 17848 length = attr->constant_value (0);
216a7e6b
AB
17849 }
17850 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17851 {
216a7e6b 17852 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17853 length = attr->constant_value (1);
c906108c
SS
17854 }
17855 else
17856 {
216a7e6b
AB
17857 /* Use 1 as a fallback length if we have nothing else. */
17858 length = 1;
c906108c 17859 }
6ccb9162 17860
46bf5051 17861 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17862 if (length_is_constant)
17863 range_type = create_static_range_type (NULL, index_type, 1, length);
17864 else
17865 {
17866 struct dynamic_prop low_bound;
17867
8c2e4e06 17868 low_bound.set_const_val (1);
216a7e6b
AB
17869 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17870 }
3b7538c0
UW
17871 char_type = language_string_char_type (cu->language_defn, gdbarch);
17872 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17873
f792889a 17874 return set_die_type (die, type, cu);
c906108c
SS
17875}
17876
4d804846
JB
17877/* Assuming that DIE corresponds to a function, returns nonzero
17878 if the function is prototyped. */
17879
17880static int
17881prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17882{
17883 struct attribute *attr;
17884
17885 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c45bc3f8 17886 if (attr && attr->as_boolean ())
4d804846
JB
17887 return 1;
17888
17889 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17890 is only meaningful for C, but the concept also extends to other
4d804846
JB
17891 languages that allow unprototyped functions (Eg: Objective C).
17892 For all other languages, assume that functions are always
17893 prototyped. */
17894 if (cu->language != language_c
17895 && cu->language != language_objc
17896 && cu->language != language_opencl)
17897 return 1;
17898
17899 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17900 prototyped and unprototyped functions; default to prototyped,
17901 since that is more common in modern code (and RealView warns
17902 about unprototyped functions). */
17903 if (producer_is_realview (cu->producer))
17904 return 1;
17905
17906 return 0;
17907}
17908
c906108c
SS
17909/* Handle DIES due to C code like:
17910
17911 struct foo
c5aa993b
JM
17912 {
17913 int (*funcp)(int a, long l);
17914 int b;
17915 };
c906108c 17916
0963b4bd 17917 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17918
f792889a 17919static struct type *
e7c27a73 17920read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17921{
5e22e966 17922 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17923 struct type *type; /* Type that this function returns. */
17924 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17925 struct attribute *attr;
17926
e7c27a73 17927 type = die_type (die, cu);
7e314c57
JK
17928
17929 /* The die_type call above may have already set the type for this DIE. */
17930 ftype = get_die_type (die, cu);
17931 if (ftype)
17932 return ftype;
17933
0c8b41f1 17934 ftype = lookup_function_type (type);
c906108c 17935
4d804846 17936 if (prototyped_function_p (die, cu))
27e69b7a 17937 ftype->set_is_prototyped (true);
c906108c 17938
c055b101
CV
17939 /* Store the calling convention in the type if it's available in
17940 the subroutine die. Otherwise set the calling convention to
17941 the default value DW_CC_normal. */
17942 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf 17943 if (attr != nullptr
529908cb 17944 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
d0922fcf 17945 TYPE_CALLING_CONVENTION (ftype)
529908cb 17946 = (enum dwarf_calling_convention) attr->constant_value (0);
54fcddd0
UW
17947 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17948 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17949 else
17950 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17951
743649fd
MW
17952 /* Record whether the function returns normally to its caller or not
17953 if the DWARF producer set that information. */
17954 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
c45bc3f8 17955 if (attr && attr->as_boolean ())
743649fd
MW
17956 TYPE_NO_RETURN (ftype) = 1;
17957
76c10ea2
GM
17958 /* We need to add the subroutine type to the die immediately so
17959 we don't infinitely recurse when dealing with parameters
0963b4bd 17960 declared as the same subroutine type. */
76c10ea2 17961 set_die_type (die, ftype, cu);
6e70227d 17962
639d11d3 17963 if (die->child != NULL)
c906108c 17964 {
bb5ed363 17965 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17966 struct die_info *child_die;
8072405b 17967 int nparams, iparams;
c906108c
SS
17968
17969 /* Count the number of parameters.
dda83cd7
SM
17970 FIXME: GDB currently ignores vararg functions, but knows about
17971 vararg member functions. */
8072405b 17972 nparams = 0;
639d11d3 17973 child_die = die->child;
c906108c
SS
17974 while (child_die && child_die->tag)
17975 {
17976 if (child_die->tag == DW_TAG_formal_parameter)
17977 nparams++;
17978 else if (child_die->tag == DW_TAG_unspecified_parameters)
1d6286ed
SM
17979 ftype->set_has_varargs (true);
17980
436c571c 17981 child_die = child_die->sibling;
c906108c
SS
17982 }
17983
17984 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17985 ftype->set_num_fields (nparams);
3cabb6b0
SM
17986 ftype->set_fields
17987 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17988
8072405b
JK
17989 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17990 even if we error out during the parameters reading below. */
17991 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 17992 ftype->field (iparams).set_type (void_type);
8072405b
JK
17993
17994 iparams = 0;
639d11d3 17995 child_die = die->child;
c906108c
SS
17996 while (child_die && child_die->tag)
17997 {
17998 if (child_die->tag == DW_TAG_formal_parameter)
17999 {
3ce3b1ba
PA
18000 struct type *arg_type;
18001
18002 /* DWARF version 2 has no clean way to discern C++
18003 static and non-static member functions. G++ helps
18004 GDB by marking the first parameter for non-static
18005 member functions (which is the this pointer) as
18006 artificial. We pass this information to
18007 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18008
18009 DWARF version 3 added DW_AT_object_pointer, which GCC
18010 4.5 does not yet generate. */
e142c38c 18011 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 18012 if (attr != nullptr)
c45bc3f8 18013 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
c906108c 18014 else
9c37b5ae 18015 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
18016 arg_type = die_type (child_die, cu);
18017
18018 /* RealView does not mark THIS as const, which the testsuite
18019 expects. GCC marks THIS as const in method definitions,
18020 but not in the class specifications (GCC PR 43053). */
18021 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18022 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18023 {
18024 int is_this = 0;
18025 struct dwarf2_cu *arg_cu = cu;
18026 const char *name = dwarf2_name (child_die, cu);
18027
18028 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 18029 if (attr != nullptr)
3ce3b1ba
PA
18030 {
18031 /* If the compiler emits this, use it. */
18032 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18033 is_this = 1;
18034 }
18035 else if (name && strcmp (name, "this") == 0)
18036 /* Function definitions will have the argument names. */
18037 is_this = 1;
18038 else if (name == NULL && iparams == 0)
18039 /* Declarations may not have the names, so like
18040 elsewhere in GDB, assume an artificial first
18041 argument is "this". */
18042 is_this = 1;
18043
18044 if (is_this)
18045 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18046 arg_type, 0);
18047 }
18048
5d14b6e5 18049 ftype->field (iparams).set_type (arg_type);
c906108c
SS
18050 iparams++;
18051 }
436c571c 18052 child_die = child_die->sibling;
c906108c
SS
18053 }
18054 }
18055
76c10ea2 18056 return ftype;
c906108c
SS
18057}
18058
f792889a 18059static struct type *
e7c27a73 18060read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18061{
5e22e966 18062 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 18063 const char *name = NULL;
3c8e0968 18064 struct type *this_type, *target_type;
c906108c 18065
94af9270 18066 name = dwarf2_full_name (NULL, die, cu);
19f392bc 18067 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
8f53807e 18068 this_type->set_target_is_stub (true);
f792889a 18069 set_die_type (die, this_type, cu);
3c8e0968
DE
18070 target_type = die_type (die, cu);
18071 if (target_type != this_type)
18072 TYPE_TARGET_TYPE (this_type) = target_type;
18073 else
18074 {
18075 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18076 spec and cause infinite loops in GDB. */
b98664d3 18077 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
18078 "- DIE at %s [in module %s]"),
18079 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
18080 TYPE_TARGET_TYPE (this_type) = NULL;
18081 }
e4003a34
TV
18082 if (name == NULL)
18083 {
18084 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18085 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18086 Handle these by just returning the target type, rather than
18087 constructing an anonymous typedef type and trying to handle this
18088 elsewhere. */
18089 set_die_type (die, target_type, cu);
18090 return target_type;
18091 }
f792889a 18092 return this_type;
c906108c
SS
18093}
18094
09584414
JB
18095/* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18096 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18097
18098 If the numerator and/or numerator attribute is missing,
18099 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18100 untouched. */
18101
18102static void
18103get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
18104 LONGEST *numerator, LONGEST *denominator)
18105{
18106 struct attribute *num_attr, *denom_attr;
18107
18108 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18109 if (num_attr == nullptr)
18110 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18111 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18112
18113 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18114 if (denom_attr == nullptr)
18115 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18116 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18117
18118 if (num_attr == nullptr || denom_attr == nullptr)
18119 return;
18120
18121 *numerator = num_attr->constant_value (1);
18122 *denominator = denom_attr->constant_value (1);
18123}
18124
18125/* Same as get_dwarf2_rational_constant, but extracting an unsigned
18126 rational constant, rather than a signed one.
18127
18128 If the rational constant has a negative value, a complaint
18129 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18130
18131static void
18132get_dwarf2_unsigned_rational_constant (struct die_info *die,
18133 struct dwarf2_cu *cu,
18134 ULONGEST *numerator,
18135 ULONGEST *denominator)
18136{
18137 LONGEST num = 1, denom = 1;
18138
18139 get_dwarf2_rational_constant (die, cu, &num, &denom);
18140 if (num < 0 && denom < 0)
18141 {
18142 num = -num;
18143 denom = -denom;
18144 }
18145 else if (num < 0)
18146 {
18147 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18148 " in DIE at %s"),
18149 sect_offset_str (die->sect_off));
18150 return;
18151 }
18152 else if (denom < 0)
18153 {
18154 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18155 " in DIE at %s"),
18156 sect_offset_str (die->sect_off));
18157 return;
18158 }
18159
18160 *numerator = num;
18161 *denominator = denom;
18162}
18163
18164/* Assuming DIE corresponds to a fixed point type, finish the creation
2a12c336 18165 of the corresponding TYPE by setting its type-specific data.
09584414
JB
18166 CU is the DIE's CU. */
18167
18168static void
18169finish_fixed_point_type (struct type *type, struct die_info *die,
18170 struct dwarf2_cu *cu)
18171{
18172 struct attribute *attr;
18173 /* Numerator and denominator of our fixed-point type's scaling factor.
18174 The default is a scaling factor of 1, which we use as a fallback
18175 when we are not able to decode it (problem with the debugging info,
18176 unsupported forms, bug in GDB, etc...). Using that as the default
18177 allows us to at least print the unscaled value, which might still
18178 be useful to a user. */
18179 ULONGEST scale_num = 1;
18180 ULONGEST scale_denom = 1;
18181
18182 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18183 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18184
18185 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18186 if (!attr)
18187 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18188 if (!attr)
18189 attr = dwarf2_attr (die, DW_AT_small, cu);
18190
18191 if (attr == nullptr)
18192 {
18193 /* Scaling factor not found. Assume a scaling factor of 1,
18194 and hope for the best. At least the user will be able to see
18195 the encoded value. */
18196 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18197 sect_offset_str (die->sect_off));
18198 }
18199 else if (attr->name == DW_AT_binary_scale)
18200 {
18201 LONGEST scale_exp = attr->constant_value (0);
18202 ULONGEST *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18203
a43b29c9 18204 *num_or_denom = 1 << std::abs (scale_exp);
09584414
JB
18205 }
18206 else if (attr->name == DW_AT_decimal_scale)
18207 {
18208 LONGEST scale_exp = attr->constant_value (0);
18209 ULONGEST *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18210
a43b29c9 18211 *num_or_denom = uinteger_pow (10, std::abs (scale_exp));
09584414
JB
18212 }
18213 else if (attr->name == DW_AT_small)
18214 {
18215 struct die_info *scale_die;
18216 struct dwarf2_cu *scale_cu = cu;
18217
18218 scale_die = follow_die_ref (die, attr, &scale_cu);
18219 if (scale_die->tag == DW_TAG_constant)
18220 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18221 &scale_num, &scale_denom);
18222 else
18223 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18224 " (DIE at %s)"),
18225 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18226 }
18227 else
18228 {
18229 complaint (_("unsupported scale attribute %s for fixed-point type"
18230 " (DIE at %s)"),
18231 dwarf_attr_name (attr->name),
18232 sect_offset_str (die->sect_off));
18233 }
18234
2a12c336 18235 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
09584414
JB
18236
18237 gdb_mpz tmp_z (scale_num);
18238 mpz_set (mpq_numref (scaling_factor.val), tmp_z.val);
18239
18240 tmp_z = scale_denom;
18241 mpz_set (mpq_denref (scaling_factor.val), tmp_z.val);
18242
18243 mpq_canonicalize (scaling_factor.val);
18244}
18245
9b790ce7
UW
18246/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18247 (which may be different from NAME) to the architecture back-end to allow
18248 it to guess the correct format if necessary. */
18249
18250static struct type *
18251dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 18252 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 18253{
08feed99 18254 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
18255 const struct floatformat **format;
18256 struct type *type;
18257
18258 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18259 if (format)
103a685e 18260 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 18261 else
77b7c781 18262 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
18263
18264 return type;
18265}
18266
eb77c9df
AB
18267/* Allocate an integer type of size BITS and name NAME. */
18268
18269static struct type *
18270dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18271 int bits, int unsigned_p, const char *name)
18272{
18273 struct type *type;
18274
18275 /* Versions of Intel's C Compiler generate an integer type called "void"
18276 instead of using DW_TAG_unspecified_type. This has been seen on
18277 at least versions 14, 17, and 18. */
35ee2dc2
AB
18278 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18279 && strcmp (name, "void") == 0)
eb77c9df
AB
18280 type = objfile_type (objfile)->builtin_void;
18281 else
18282 type = init_integer_type (objfile, bits, unsigned_p, name);
18283
18284 return type;
18285}
18286
09584414
JB
18287/* Return true if DIE has a DW_AT_small attribute whose value is
18288 a constant rational, where both the numerator and denominator
18289 are equal to zero.
18290
18291 CU is the DIE's Compilation Unit. */
18292
18293static bool
18294has_zero_over_zero_small_attribute (struct die_info *die,
18295 struct dwarf2_cu *cu)
18296{
18297 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18298 if (attr == nullptr)
18299 return false;
18300
18301 struct dwarf2_cu *scale_cu = cu;
18302 struct die_info *scale_die
18303 = follow_die_ref (die, attr, &scale_cu);
18304
18305 if (scale_die->tag != DW_TAG_constant)
18306 return false;
18307
18308 LONGEST num = 1, denom = 1;
18309 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18310 return (num == 0 && denom == 0);
18311}
18312
8bdc1658
AB
18313/* Initialise and return a floating point type of size BITS suitable for
18314 use as a component of a complex number. The NAME_HINT is passed through
18315 when initialising the floating point type and is the name of the complex
18316 type.
18317
18318 As DWARF doesn't currently provide an explicit name for the components
18319 of a complex number, but it can be helpful to have these components
18320 named, we try to select a suitable name based on the size of the
18321 component. */
18322static struct type *
18323dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18324 struct objfile *objfile,
103a685e
TT
18325 int bits, const char *name_hint,
18326 enum bfd_endian byte_order)
8bdc1658 18327{
08feed99 18328 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
18329 struct type *tt = nullptr;
18330
35add35e
AB
18331 /* Try to find a suitable floating point builtin type of size BITS.
18332 We're going to use the name of this type as the name for the complex
18333 target type that we are about to create. */
1db455a7 18334 switch (cu->language)
8bdc1658 18335 {
1db455a7
AB
18336 case language_fortran:
18337 switch (bits)
18338 {
18339 case 32:
18340 tt = builtin_f_type (gdbarch)->builtin_real;
18341 break;
18342 case 64:
18343 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18344 break;
18345 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18346 case 128:
18347 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18348 break;
18349 }
8bdc1658 18350 break;
1db455a7
AB
18351 default:
18352 switch (bits)
18353 {
18354 case 32:
18355 tt = builtin_type (gdbarch)->builtin_float;
18356 break;
18357 case 64:
18358 tt = builtin_type (gdbarch)->builtin_double;
18359 break;
18360 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18361 case 128:
18362 tt = builtin_type (gdbarch)->builtin_long_double;
18363 break;
18364 }
8bdc1658
AB
18365 break;
18366 }
18367
35add35e
AB
18368 /* If the type we found doesn't match the size we were looking for, then
18369 pretend we didn't find a type at all, the complex target type we
18370 create will then be nameless. */
a12e5744 18371 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
18372 tt = nullptr;
18373
7d93a1e0 18374 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 18375 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
18376}
18377
c906108c
SS
18378/* Find a representation of a given base type and install
18379 it in the TYPE field of the die. */
18380
f792889a 18381static struct type *
e7c27a73 18382read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18383{
5e22e966 18384 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
18385 struct type *type;
18386 struct attribute *attr;
19f392bc 18387 int encoding = 0, bits = 0;
15d034d0 18388 const char *name;
34877895 18389 gdbarch *arch;
c906108c 18390
e142c38c 18391 attr = dwarf2_attr (die, DW_AT_encoding, cu);
529908cb
TT
18392 if (attr != nullptr && attr->form_is_constant ())
18393 encoding = attr->constant_value (0);
e142c38c 18394 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18395 if (attr != nullptr)
529908cb 18396 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
39cbfefa 18397 name = dwarf2_name (die, cu);
6ccb9162 18398 if (!name)
34877895 18399 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 18400
08feed99 18401 arch = objfile->arch ();
103a685e
TT
18402 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18403
34877895 18404 attr = dwarf2_attr (die, DW_AT_endianity, cu);
529908cb 18405 if (attr != nullptr && attr->form_is_constant ())
103a685e 18406 {
529908cb 18407 int endianity = attr->constant_value (0);
103a685e
TT
18408
18409 switch (endianity)
18410 {
18411 case DW_END_big:
18412 byte_order = BFD_ENDIAN_BIG;
18413 break;
18414 case DW_END_little:
18415 byte_order = BFD_ENDIAN_LITTLE;
18416 break;
18417 default:
18418 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18419 break;
18420 }
18421 }
6ccb9162 18422
09584414
JB
18423 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18424 && cu->language == language_ada
18425 && has_zero_over_zero_small_attribute (die, cu))
18426 {
18427 /* brobecker/2018-02-24: This is a fixed point type for which
18428 the scaling factor is represented as fraction whose value
18429 does not make sense (zero divided by zero), so we should
18430 normally never see these. However, there is a small category
18431 of fixed point types for which GNAT is unable to provide
18432 the scaling factor via the standard DWARF mechanisms, and
18433 for which the info is provided via the GNAT encodings instead.
18434 This is likely what this DIE is about.
18435
18436 Ideally, GNAT should be declaring this type the same way
18437 it declares other fixed point types when using the legacy
18438 GNAT encoding, which is to use a simple signed or unsigned
18439 base type. A report to the GNAT team has been created to
18440 look into it. In the meantime, pretend this type is a simple
18441 signed or unsigned integral, rather than a fixed point type,
18442 to avoid any confusion later on as to how to process this type. */
18443 encoding = (encoding == DW_ATE_signed_fixed
18444 ? DW_ATE_signed
18445 : DW_ATE_unsigned);
18446 }
18447
6ccb9162 18448 switch (encoding)
c906108c 18449 {
6ccb9162
UW
18450 case DW_ATE_address:
18451 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 18452 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 18453 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
18454 break;
18455 case DW_ATE_boolean:
19f392bc 18456 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
18457 break;
18458 case DW_ATE_complex_float:
103a685e
TT
18459 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18460 byte_order);
78134374 18461 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
18462 {
18463 if (name == nullptr)
18464 {
18465 struct obstack *obstack
5e22e966 18466 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 18467 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
18468 nullptr);
18469 }
18470 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18471 }
18472 else
18473 type = init_complex_type (name, type);
6ccb9162
UW
18474 break;
18475 case DW_ATE_decimal_float:
19f392bc 18476 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
18477 break;
18478 case DW_ATE_float:
103a685e 18479 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
18480 break;
18481 case DW_ATE_signed:
eb77c9df 18482 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18483 break;
18484 case DW_ATE_unsigned:
3b2b8fea
TT
18485 if (cu->language == language_fortran
18486 && name
61012eef 18487 && startswith (name, "character("))
19f392bc
UW
18488 type = init_character_type (objfile, bits, 1, name);
18489 else
eb77c9df 18490 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
18491 break;
18492 case DW_ATE_signed_char:
6e70227d 18493 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
18494 || cu->language == language_pascal
18495 || cu->language == language_fortran)
19f392bc
UW
18496 type = init_character_type (objfile, bits, 0, name);
18497 else
eb77c9df 18498 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18499 break;
18500 case DW_ATE_unsigned_char:
868a0084 18501 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 18502 || cu->language == language_pascal
c44af4eb
TT
18503 || cu->language == language_fortran
18504 || cu->language == language_rust)
19f392bc
UW
18505 type = init_character_type (objfile, bits, 1, name);
18506 else
eb77c9df 18507 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 18508 break;
75079b2b 18509 case DW_ATE_UTF:
53e710ac 18510 {
53e710ac
PA
18511 if (bits == 16)
18512 type = builtin_type (arch)->builtin_char16;
18513 else if (bits == 32)
18514 type = builtin_type (arch)->builtin_char32;
18515 else
18516 {
b98664d3 18517 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 18518 bits);
eb77c9df 18519 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
18520 }
18521 return set_die_type (die, type, cu);
18522 }
75079b2b 18523 break;
09584414
JB
18524 case DW_ATE_signed_fixed:
18525 type = init_fixed_point_type (objfile, bits, 0, name);
18526 finish_fixed_point_type (type, die, cu);
18527 break;
18528 case DW_ATE_unsigned_fixed:
18529 type = init_fixed_point_type (objfile, bits, 1, name);
18530 finish_fixed_point_type (type, die, cu);
18531 break;
75079b2b 18532
6ccb9162 18533 default:
b98664d3 18534 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 18535 dwarf_type_encoding_name (encoding));
77b7c781 18536 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18537 break;
c906108c 18538 }
6ccb9162 18539
0114d602 18540 if (name && strcmp (name, "char") == 0)
15152a54 18541 type->set_has_no_signedness (true);
0114d602 18542
2b4424c3
TT
18543 maybe_set_alignment (cu, die, type);
18544
db558e34 18545 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
34877895 18546
20a5fcbd
TT
18547 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18548 {
18549 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
529908cb 18550 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
20a5fcbd 18551 {
529908cb 18552 unsigned real_bit_size = attr->as_unsigned ();
20a5fcbd
TT
18553 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18554 /* Only use the attributes if they make sense together. */
18555 if (attr == nullptr
529908cb
TT
18556 || (attr->as_unsigned () + real_bit_size
18557 <= 8 * TYPE_LENGTH (type)))
20a5fcbd
TT
18558 {
18559 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18560 = real_bit_size;
18561 if (attr != nullptr)
18562 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
529908cb 18563 = attr->as_unsigned ();
20a5fcbd
TT
18564 }
18565 }
18566 }
18567
f792889a 18568 return set_die_type (die, type, cu);
c906108c
SS
18569}
18570
80180f79
SA
18571/* Parse dwarf attribute if it's a block, reference or constant and put the
18572 resulting value of the attribute into struct bound_prop.
18573 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18574
18575static int
18576attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
18577 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18578 struct type *default_type)
80180f79
SA
18579{
18580 struct dwarf2_property_baton *baton;
5e22e966 18581 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
18582 struct objfile *objfile = per_objfile->objfile;
18583 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 18584
9a49df9d
AB
18585 gdb_assert (default_type != NULL);
18586
80180f79
SA
18587 if (attr == NULL || prop == NULL)
18588 return 0;
18589
4fc6c0d5 18590 if (attr->form_is_block ())
80180f79 18591 {
8d749320 18592 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18593 baton->property_type = default_type;
80180f79 18594 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18595 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18596
18597 struct dwarf_block *block = attr->as_block ();
18598 baton->locexpr.size = block->size;
18599 baton->locexpr.data = block->data;
216a7e6b
AB
18600 switch (attr->name)
18601 {
18602 case DW_AT_string_length:
18603 baton->locexpr.is_reference = true;
18604 break;
18605 default:
18606 baton->locexpr.is_reference = false;
18607 break;
18608 }
8c2e4e06
SM
18609
18610 prop->set_locexpr (baton);
18611 gdb_assert (prop->baton () != NULL);
80180f79 18612 }
cd6c91b4 18613 else if (attr->form_is_ref ())
80180f79
SA
18614 {
18615 struct dwarf2_cu *target_cu = cu;
18616 struct die_info *target_die;
18617 struct attribute *target_attr;
18618
18619 target_die = follow_die_ref (die, attr, &target_cu);
18620 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18621 if (target_attr == NULL)
18622 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18623 target_cu);
80180f79
SA
18624 if (target_attr == NULL)
18625 return 0;
18626
df25ebbd 18627 switch (target_attr->name)
80180f79 18628 {
df25ebbd 18629 case DW_AT_location:
cd6c91b4 18630 if (target_attr->form_is_section_offset ())
df25ebbd 18631 {
8d749320 18632 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18633 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18634 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
8c2e4e06
SM
18635 prop->set_loclist (baton);
18636 gdb_assert (prop->baton () != NULL);
df25ebbd 18637 }
4fc6c0d5 18638 else if (target_attr->form_is_block ())
df25ebbd 18639 {
8d749320 18640 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18641 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18642 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18643 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18644 struct dwarf_block *block = target_attr->as_block ();
18645 baton->locexpr.size = block->size;
18646 baton->locexpr.data = block->data;
9a49df9d 18647 baton->locexpr.is_reference = true;
8c2e4e06
SM
18648 prop->set_locexpr (baton);
18649 gdb_assert (prop->baton () != NULL);
df25ebbd
JB
18650 }
18651 else
18652 {
18653 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18654 "dynamic property");
18655 return 0;
18656 }
18657 break;
18658 case DW_AT_data_member_location:
18659 {
18660 LONGEST offset;
18661
18662 if (!handle_data_member_location (target_die, target_cu,
18663 &offset))
18664 return 0;
18665
8d749320 18666 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18667 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18668 target_cu);
df25ebbd
JB
18669 baton->offset_info.offset = offset;
18670 baton->offset_info.type = die_type (target_die, target_cu);
8c2e4e06 18671 prop->set_addr_offset (baton);
df25ebbd
JB
18672 break;
18673 }
80180f79
SA
18674 }
18675 }
cd6c91b4 18676 else if (attr->form_is_constant ())
8c2e4e06 18677 prop->set_const_val (attr->constant_value (0));
80180f79
SA
18678 else
18679 {
18680 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18681 dwarf2_name (die, cu));
18682 return 0;
18683 }
18684
18685 return 1;
18686}
18687
09ba997f 18688/* See read.h. */
9a49df9d 18689
09ba997f 18690struct type *
293e7e51 18691dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 18692{
9a49df9d
AB
18693 struct type *int_type;
18694
18695 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18696#define TRY_TYPE(F) \
18697 int_type = (unsigned_p \
18698 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18699 : objfile_type (objfile)->builtin_ ## F); \
18700 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18701 return int_type
18702
18703 TRY_TYPE (char);
18704 TRY_TYPE (short);
18705 TRY_TYPE (int);
18706 TRY_TYPE (long);
18707 TRY_TYPE (long_long);
18708
18709#undef TRY_TYPE
18710
18711 gdb_assert_not_reached ("unable to find suitable integer type");
18712}
18713
09ba997f 18714/* See read.h. */
11a8b164 18715
09ba997f 18716struct type *
293e7e51 18717dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 18718{
293e7e51
SM
18719 int addr_size = this->per_cu->addr_size ();
18720 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
18721}
18722
b86352cf
AB
18723/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18724 present (which is valid) then compute the default type based on the
18725 compilation units address size. */
18726
18727static struct type *
18728read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18729{
18730 struct type *index_type = die_type (die, cu);
18731
18732 /* Dwarf-2 specifications explicitly allows to create subrange types
18733 without specifying a base type.
18734 In that case, the base type must be set to the type of
18735 the lower bound, upper bound or count, in that order, if any of these
18736 three attributes references an object that has a type.
18737 If no base type is found, the Dwarf-2 specifications say that
18738 a signed integer type of size equal to the size of an address should
18739 be used.
18740 For the following C code: `extern char gdb_int [];'
18741 GCC produces an empty range DIE.
18742 FIXME: muller/2010-05-28: Possible references to object for low bound,
18743 high bound or count are not yet handled by this code. */
78134374 18744 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 18745 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
18746
18747 return index_type;
18748}
18749
a02abb62
JB
18750/* Read the given DW_AT_subrange DIE. */
18751
f792889a 18752static struct type *
a02abb62
JB
18753read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18754{
4c9ad8c2 18755 struct type *base_type, *orig_base_type;
a02abb62
JB
18756 struct type *range_type;
18757 struct attribute *attr;
729efb13 18758 struct dynamic_prop low, high;
4fae6e18 18759 int low_default_is_valid;
c451ebe5 18760 int high_bound_is_count = 0;
15d034d0 18761 const char *name;
d359392f 18762 ULONGEST negative_mask;
e77813c8 18763
b86352cf
AB
18764 orig_base_type = read_subrange_index_type (die, cu);
18765
4c9ad8c2
TT
18766 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18767 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18768 creating the range type, but we use the result of check_typedef
18769 when examining properties of the type. */
18770 base_type = check_typedef (orig_base_type);
a02abb62 18771
7e314c57
JK
18772 /* The die_type call above may have already set the type for this DIE. */
18773 range_type = get_die_type (die, cu);
18774 if (range_type)
18775 return range_type;
18776
8c2e4e06 18777 high.set_const_val (0);
729efb13 18778
4fae6e18
JK
18779 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18780 omitting DW_AT_lower_bound. */
18781 switch (cu->language)
6e70227d 18782 {
4fae6e18
JK
18783 case language_c:
18784 case language_cplus:
8c2e4e06 18785 low.set_const_val (0);
4fae6e18
JK
18786 low_default_is_valid = 1;
18787 break;
18788 case language_fortran:
8c2e4e06 18789 low.set_const_val (1);
4fae6e18
JK
18790 low_default_is_valid = 1;
18791 break;
18792 case language_d:
4fae6e18 18793 case language_objc:
c44af4eb 18794 case language_rust:
8c2e4e06 18795 low.set_const_val (0);
4fae6e18
JK
18796 low_default_is_valid = (cu->header.version >= 4);
18797 break;
18798 case language_ada:
18799 case language_m2:
18800 case language_pascal:
8c2e4e06 18801 low.set_const_val (1);
4fae6e18
JK
18802 low_default_is_valid = (cu->header.version >= 4);
18803 break;
18804 default:
8c2e4e06 18805 low.set_const_val (0);
4fae6e18
JK
18806 low_default_is_valid = 0;
18807 break;
a02abb62
JB
18808 }
18809
e142c38c 18810 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18811 if (attr != nullptr)
9a49df9d 18812 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18813 else if (!low_default_is_valid)
b98664d3 18814 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18815 "- DIE at %s [in module %s]"),
18816 sect_offset_str (die->sect_off),
5e22e966 18817 objfile_name (cu->per_objfile->objfile));
a02abb62 18818
506f5c41
TV
18819 struct attribute *attr_ub, *attr_count;
18820 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18821 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18822 {
506f5c41 18823 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18824 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18825 {
c451ebe5 18826 /* If bounds are constant do the final calculation here. */
8c2e4e06
SM
18827 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18828 high.set_const_val (low.const_val () + high.const_val () - 1);
c451ebe5
SA
18829 else
18830 high_bound_is_count = 1;
c2ff108b 18831 }
506f5c41
TV
18832 else
18833 {
18834 if (attr_ub != NULL)
18835 complaint (_("Unresolved DW_AT_upper_bound "
18836 "- DIE at %s [in module %s]"),
18837 sect_offset_str (die->sect_off),
5e22e966 18838 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
18839 if (attr_count != NULL)
18840 complaint (_("Unresolved DW_AT_count "
18841 "- DIE at %s [in module %s]"),
18842 sect_offset_str (die->sect_off),
5e22e966 18843 objfile_name (cu->per_objfile->objfile));
506f5c41 18844 }
e77813c8 18845 }
a02abb62 18846
4e962e74
TT
18847 LONGEST bias = 0;
18848 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 18849 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 18850 bias = bias_attr->constant_value (0);
4e962e74 18851
dbb9c2b1
JB
18852 /* Normally, the DWARF producers are expected to use a signed
18853 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18854 But this is unfortunately not always the case, as witnessed
18855 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18856 is used instead. To work around that ambiguity, we treat
18857 the bounds as signed, and thus sign-extend their values, when
18858 the base type is signed. */
6e70227d 18859 negative_mask =
d359392f 18860 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
8c2e4e06 18861 if (low.kind () == PROP_CONST
c6d940a9 18862 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
8c2e4e06
SM
18863 low.set_const_val (low.const_val () | negative_mask);
18864 if (high.kind () == PROP_CONST
c6d940a9 18865 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
8c2e4e06 18866 high.set_const_val (high.const_val () | negative_mask);
43bbcdc2 18867
5bbd8269
AB
18868 /* Check for bit and byte strides. */
18869 struct dynamic_prop byte_stride_prop;
18870 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18871 if (attr_byte_stride != nullptr)
18872 {
293e7e51 18873 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18874 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18875 prop_type);
18876 }
18877
18878 struct dynamic_prop bit_stride_prop;
18879 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18880 if (attr_bit_stride != nullptr)
18881 {
18882 /* It only makes sense to have either a bit or byte stride. */
18883 if (attr_byte_stride != nullptr)
18884 {
18885 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18886 "- DIE at %s [in module %s]"),
18887 sect_offset_str (die->sect_off),
5e22e966 18888 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
18889 attr_bit_stride = nullptr;
18890 }
18891 else
18892 {
293e7e51 18893 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18894 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18895 prop_type);
18896 }
18897 }
18898
18899 if (attr_byte_stride != nullptr
18900 || attr_bit_stride != nullptr)
18901 {
18902 bool byte_stride_p = (attr_byte_stride != nullptr);
18903 struct dynamic_prop *stride
18904 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18905
18906 range_type
18907 = create_range_type_with_stride (NULL, orig_base_type, &low,
18908 &high, bias, stride, byte_stride_p);
18909 }
18910 else
18911 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18912
c451ebe5 18913 if (high_bound_is_count)
599088e3 18914 range_type->bounds ()->flag_upper_bound_is_count = 1;
c451ebe5 18915
c2ff108b
JK
18916 /* Ada expects an empty array on no boundary attributes. */
18917 if (attr == NULL && cu->language != language_ada)
8c2e4e06 18918 range_type->bounds ()->high.set_undefined ();
c2ff108b 18919
39cbfefa
DJ
18920 name = dwarf2_name (die, cu);
18921 if (name)
d0e39ea2 18922 range_type->set_name (name);
6e70227d 18923
e142c38c 18924 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18925 if (attr != nullptr)
529908cb 18926 TYPE_LENGTH (range_type) = attr->constant_value (0);
a02abb62 18927
2b4424c3
TT
18928 maybe_set_alignment (cu, die, range_type);
18929
7e314c57
JK
18930 set_die_type (die, range_type, cu);
18931
18932 /* set_die_type should be already done. */
b4ba55a1
JB
18933 set_descriptive_type (range_type, die, cu);
18934
7e314c57 18935 return range_type;
a02abb62 18936}
6e70227d 18937
f792889a 18938static struct type *
81a17f79
JB
18939read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18940{
18941 struct type *type;
81a17f79 18942
5e22e966 18943 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 18944 type->set_name (dwarf2_name (die, cu));
81a17f79 18945
74a2f8ff 18946 /* In Ada, an unspecified type is typically used when the description
85102364 18947 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18948 such a type, we treat it as a stub, and try to resolve it later on,
18949 when needed. */
18950 if (cu->language == language_ada)
b4b73759 18951 type->set_is_stub (true);
74a2f8ff 18952
f792889a 18953 return set_die_type (die, type, cu);
81a17f79 18954}
a02abb62 18955
639d11d3
DC
18956/* Read a single die and all its descendents. Set the die's sibling
18957 field to NULL; set other fields in the die correctly, and set all
18958 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18959 location of the info_ptr after reading all of those dies. PARENT
18960 is the parent of the die in question. */
18961
18962static struct die_info *
dee91e82 18963read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18964 const gdb_byte *info_ptr,
18965 const gdb_byte **new_info_ptr,
dee91e82 18966 struct die_info *parent)
639d11d3
DC
18967{
18968 struct die_info *die;
d521ce57 18969 const gdb_byte *cur_ptr;
639d11d3 18970
3e225074 18971 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
18972 if (die == NULL)
18973 {
18974 *new_info_ptr = cur_ptr;
18975 return NULL;
18976 }
93311388 18977 store_in_ref_table (die, reader->cu);
639d11d3 18978
3e225074 18979 if (die->has_children)
bf6af496 18980 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18981 else
18982 {
18983 die->child = NULL;
18984 *new_info_ptr = cur_ptr;
18985 }
18986
18987 die->sibling = NULL;
18988 die->parent = parent;
18989 return die;
18990}
18991
18992/* Read a die, all of its descendents, and all of its siblings; set
18993 all of the fields of all of the dies correctly. Arguments are as
18994 in read_die_and_children. */
18995
18996static struct die_info *
bf6af496 18997read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18998 const gdb_byte *info_ptr,
18999 const gdb_byte **new_info_ptr,
bf6af496 19000 struct die_info *parent)
639d11d3
DC
19001{
19002 struct die_info *first_die, *last_sibling;
d521ce57 19003 const gdb_byte *cur_ptr;
639d11d3 19004
c906108c 19005 cur_ptr = info_ptr;
639d11d3
DC
19006 first_die = last_sibling = NULL;
19007
19008 while (1)
c906108c 19009 {
639d11d3 19010 struct die_info *die
dee91e82 19011 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 19012
1d325ec1 19013 if (die == NULL)
c906108c 19014 {
639d11d3
DC
19015 *new_info_ptr = cur_ptr;
19016 return first_die;
c906108c 19017 }
1d325ec1
DJ
19018
19019 if (!first_die)
19020 first_die = die;
c906108c 19021 else
1d325ec1
DJ
19022 last_sibling->sibling = die;
19023
19024 last_sibling = die;
c906108c 19025 }
c906108c
SS
19026}
19027
bf6af496
DE
19028/* Read a die, all of its descendents, and all of its siblings; set
19029 all of the fields of all of the dies correctly. Arguments are as
19030 in read_die_and_children.
19031 This the main entry point for reading a DIE and all its children. */
19032
19033static struct die_info *
19034read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
19035 const gdb_byte *info_ptr,
19036 const gdb_byte **new_info_ptr,
bf6af496
DE
19037 struct die_info *parent)
19038{
19039 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19040 new_info_ptr, parent);
19041
b4f54984 19042 if (dwarf_die_debug)
bf6af496
DE
19043 {
19044 fprintf_unfiltered (gdb_stdlog,
19045 "Read die from %s@0x%x of %s:\n",
96b79293 19046 reader->die_section->get_name (),
bf6af496
DE
19047 (unsigned) (info_ptr - reader->die_section->buffer),
19048 bfd_get_filename (reader->abfd));
b4f54984 19049 dump_die (die, dwarf_die_debug);
bf6af496
DE
19050 }
19051
19052 return die;
19053}
19054
3019eac3
DE
19055/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19056 attributes.
19057 The caller is responsible for filling in the extra attributes
19058 and updating (*DIEP)->num_attrs.
19059 Set DIEP to point to a newly allocated die with its information,
3e225074 19060 except for its child, sibling, and parent fields. */
93311388 19061
d521ce57 19062static const gdb_byte *
3019eac3 19063read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 19064 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 19065 int num_extra_attrs)
93311388 19066{
b64f50a1 19067 unsigned int abbrev_number, bytes_read, i;
93311388
DE
19068 struct abbrev_info *abbrev;
19069 struct die_info *die;
19070 struct dwarf2_cu *cu = reader->cu;
19071 bfd *abfd = reader->abfd;
19072
9c541725 19073 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
19074 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19075 info_ptr += bytes_read;
19076 if (!abbrev_number)
19077 {
19078 *diep = NULL;
93311388
DE
19079 return info_ptr;
19080 }
19081
685af9cd 19082 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 19083 if (!abbrev)
348e048f
DE
19084 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19085 abbrev_number,
19086 bfd_get_filename (abfd));
19087
3019eac3 19088 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 19089 die->sect_off = sect_off;
93311388
DE
19090 die->tag = abbrev->tag;
19091 die->abbrev = abbrev_number;
3e225074 19092 die->has_children = abbrev->has_children;
93311388 19093
3019eac3
DE
19094 /* Make the result usable.
19095 The caller needs to update num_attrs after adding the extra
19096 attributes. */
93311388
DE
19097 die->num_attrs = abbrev->num_attrs;
19098
7a5f294d 19099 bool any_need_reprocess = false;
93311388 19100 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e 19101 {
7a5f294d
TT
19102 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19103 info_ptr);
19104 if (die->attrs[i].requires_reprocessing_p ())
19105 any_need_reprocess = true;
18a8505e
AT
19106 }
19107
052c8bb8 19108 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
529908cb
TT
19109 if (attr != nullptr && attr->form_is_unsigned ())
19110 cu->str_offsets_base = attr->as_unsigned ();
93311388 19111
41144253 19112 attr = die->attr (DW_AT_loclists_base);
19113 if (attr != nullptr)
529908cb 19114 cu->loclist_base = attr->as_unsigned ();
41144253 19115
a39fdb41 19116 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
19117 if (maybe_addr_base.has_value ())
19118 cu->addr_base = *maybe_addr_base;
d0ce17d8
CT
19119
19120 attr = die->attr (DW_AT_rnglists_base);
19121 if (attr != nullptr)
529908cb 19122 cu->ranges_base = attr->as_unsigned ();
d0ce17d8 19123
7a5f294d
TT
19124 if (any_need_reprocess)
19125 {
19126 for (i = 0; i < abbrev->num_attrs; ++i)
19127 {
19128 if (die->attrs[i].requires_reprocessing_p ())
19129 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19130 }
19131 }
93311388 19132 *diep = die;
93311388
DE
19133 return info_ptr;
19134}
19135
3019eac3
DE
19136/* Read a die and all its attributes.
19137 Set DIEP to point to a newly allocated die with its information,
3e225074 19138 except for its child, sibling, and parent fields. */
3019eac3 19139
d521ce57 19140static const gdb_byte *
3019eac3 19141read_full_die (const struct die_reader_specs *reader,
3e225074 19142 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 19143{
d521ce57 19144 const gdb_byte *result;
bf6af496 19145
3e225074 19146 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 19147
b4f54984 19148 if (dwarf_die_debug)
bf6af496
DE
19149 {
19150 fprintf_unfiltered (gdb_stdlog,
19151 "Read die from %s@0x%x of %s:\n",
96b79293 19152 reader->die_section->get_name (),
bf6af496
DE
19153 (unsigned) (info_ptr - reader->die_section->buffer),
19154 bfd_get_filename (reader->abfd));
b4f54984 19155 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
19156 }
19157
19158 return result;
3019eac3 19159}
433df2d4 19160\f
c906108c 19161
72bf9492
DJ
19162/* Returns nonzero if TAG represents a type that we might generate a partial
19163 symbol for. */
19164
19165static int
d8f62e84 19166is_type_tag_for_partial (int tag, enum language lang)
72bf9492
DJ
19167{
19168 switch (tag)
19169 {
19170#if 0
19171 /* Some types that would be reasonable to generate partial symbols for,
d8f62e84
TT
19172 that we don't at present. Note that normally this does not
19173 matter, mainly because C compilers don't give names to these
19174 types, but instead emit DW_TAG_typedef. */
72bf9492
DJ
19175 case DW_TAG_file_type:
19176 case DW_TAG_ptr_to_member_type:
19177 case DW_TAG_set_type:
19178 case DW_TAG_string_type:
19179 case DW_TAG_subroutine_type:
19180#endif
d8f62e84
TT
19181
19182 /* GNAT may emit an array with a name, but no typedef, so we
19183 need to make a symbol in this case. */
19184 case DW_TAG_array_type:
19185 return lang == language_ada;
19186
72bf9492
DJ
19187 case DW_TAG_base_type:
19188 case DW_TAG_class_type:
680b30c7 19189 case DW_TAG_interface_type:
72bf9492
DJ
19190 case DW_TAG_enumeration_type:
19191 case DW_TAG_structure_type:
19192 case DW_TAG_subrange_type:
19193 case DW_TAG_typedef:
19194 case DW_TAG_union_type:
19195 return 1;
19196 default:
19197 return 0;
19198 }
19199}
19200
19201/* Load all DIEs that are interesting for partial symbols into memory. */
19202
19203static struct partial_die_info *
dee91e82 19204load_partial_dies (const struct die_reader_specs *reader,
d521ce57 19205 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 19206{
dee91e82 19207 struct dwarf2_cu *cu = reader->cu;
5e22e966 19208 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 19209 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 19210 unsigned int bytes_read;
5afb4e99 19211 unsigned int load_all = 0;
72bf9492
DJ
19212 int nesting_level = 1;
19213
19214 parent_die = NULL;
19215 last_die = NULL;
19216
7adf1e79
DE
19217 gdb_assert (cu->per_cu != NULL);
19218 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
19219 load_all = 1;
19220
72bf9492
DJ
19221 cu->partial_dies
19222 = htab_create_alloc_ex (cu->header.length / 12,
19223 partial_die_hash,
19224 partial_die_eq,
19225 NULL,
19226 &cu->comp_unit_obstack,
19227 hashtab_obstack_allocate,
19228 dummy_obstack_deallocate);
19229
72bf9492
DJ
19230 while (1)
19231 {
685af9cd 19232 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
19233
19234 /* A NULL abbrev means the end of a series of children. */
19235 if (abbrev == NULL)
19236 {
19237 if (--nesting_level == 0)
cd9983dd
YQ
19238 return first_die;
19239
72bf9492
DJ
19240 info_ptr += bytes_read;
19241 last_die = parent_die;
19242 parent_die = parent_die->die_parent;
19243 continue;
19244 }
19245
98bfdba5
PA
19246 /* Check for template arguments. We never save these; if
19247 they're seen, we just mark the parent, and go on our way. */
19248 if (parent_die != NULL
19249 && cu->language == language_cplus
19250 && (abbrev->tag == DW_TAG_template_type_param
19251 || abbrev->tag == DW_TAG_template_value_param))
19252 {
19253 parent_die->has_template_arguments = 1;
19254
19255 if (!load_all)
19256 {
19257 /* We don't need a partial DIE for the template argument. */
dee91e82 19258 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19259 continue;
19260 }
19261 }
19262
0d99eb77 19263 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
19264 Skip their other children. */
19265 if (!load_all
19266 && cu->language == language_cplus
19267 && parent_die != NULL
f9b5d5ea
TV
19268 && parent_die->tag == DW_TAG_subprogram
19269 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 19270 {
dee91e82 19271 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19272 continue;
19273 }
19274
5afb4e99
DJ
19275 /* Check whether this DIE is interesting enough to save. Normally
19276 we would not be interested in members here, but there may be
19277 later variables referencing them via DW_AT_specification (for
19278 static members). */
19279 if (!load_all
d8f62e84 19280 && !is_type_tag_for_partial (abbrev->tag, cu->language)
72929c62 19281 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
19282 && abbrev->tag != DW_TAG_enumerator
19283 && abbrev->tag != DW_TAG_subprogram
b1dc1806 19284 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 19285 && abbrev->tag != DW_TAG_lexical_block
72bf9492 19286 && abbrev->tag != DW_TAG_variable
5afb4e99 19287 && abbrev->tag != DW_TAG_namespace
f55ee35c 19288 && abbrev->tag != DW_TAG_module
95554aad 19289 && abbrev->tag != DW_TAG_member
74921315
KS
19290 && abbrev->tag != DW_TAG_imported_unit
19291 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
19292 {
19293 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19294 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
19295 continue;
19296 }
19297
6f06d47b
YQ
19298 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19299 abbrev);
cd9983dd 19300
48fbe735 19301 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
19302
19303 /* This two-pass algorithm for processing partial symbols has a
19304 high cost in cache pressure. Thus, handle some simple cases
19305 here which cover the majority of C partial symbols. DIEs
19306 which neither have specification tags in them, nor could have
19307 specification tags elsewhere pointing at them, can simply be
19308 processed and discarded.
19309
19310 This segment is also optional; scan_partial_symbols and
19311 add_partial_symbol will handle these DIEs if we chain
19312 them in normally. When compilers which do not emit large
19313 quantities of duplicate debug information are more common,
19314 this code can probably be removed. */
19315
19316 /* Any complete simple types at the top level (pretty much all
19317 of them, for a language without namespaces), can be processed
19318 directly. */
19319 if (parent_die == NULL
cd9983dd
YQ
19320 && pdi.has_specification == 0
19321 && pdi.is_declaration == 0
19322 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19323 || pdi.tag == DW_TAG_base_type
d8f62e84 19324 || pdi.tag == DW_TAG_array_type
cd9983dd 19325 || pdi.tag == DW_TAG_subrange_type))
72bf9492 19326 {
7d00ffec 19327 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
19328 add_partial_symbol (&pdi, cu);
19329
cd9983dd 19330 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19331 continue;
19332 }
19333
d8228535
JK
19334 /* The exception for DW_TAG_typedef with has_children above is
19335 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 19336 type_name_or_error will error on such types later.
d8228535
JK
19337
19338 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19339 it could not find the child DIEs referenced later, this is checked
19340 above. In correct DWARF DW_TAG_typedef should have no children. */
19341
cd9983dd 19342 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 19343 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 19344 "- DIE at %s [in module %s]"),
cd9983dd 19345 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 19346
72bf9492
DJ
19347 /* If we're at the second level, and we're an enumerator, and
19348 our parent has no specification (meaning possibly lives in a
19349 namespace elsewhere), then we can add the partial symbol now
19350 instead of queueing it. */
cd9983dd 19351 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
19352 && parent_die != NULL
19353 && parent_die->die_parent == NULL
19354 && parent_die->tag == DW_TAG_enumeration_type
19355 && parent_die->has_specification == 0)
19356 {
7d00ffec 19357 if (pdi.raw_name == NULL)
b98664d3 19358 complaint (_("malformed enumerator DIE ignored"));
72bf9492 19359 else if (building_psymtab)
f0fbb768 19360 add_partial_symbol (&pdi, cu);
72bf9492 19361
cd9983dd 19362 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19363 continue;
19364 }
19365
cd9983dd 19366 struct partial_die_info *part_die
6f06d47b 19367 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 19368
72bf9492
DJ
19369 /* We'll save this DIE so link it in. */
19370 part_die->die_parent = parent_die;
19371 part_die->die_sibling = NULL;
19372 part_die->die_child = NULL;
19373
19374 if (last_die && last_die == parent_die)
19375 last_die->die_child = part_die;
19376 else if (last_die)
19377 last_die->die_sibling = part_die;
19378
19379 last_die = part_die;
19380
19381 if (first_die == NULL)
19382 first_die = part_die;
19383
19384 /* Maybe add the DIE to the hash table. Not all DIEs that we
19385 find interesting need to be in the hash table, because we
19386 also have the parent/sibling/child chains; only those that we
19387 might refer to by offset later during partial symbol reading.
19388
19389 For now this means things that might have be the target of a
19390 DW_AT_specification, DW_AT_abstract_origin, or
19391 DW_AT_extension. DW_AT_extension will refer only to
19392 namespaces; DW_AT_abstract_origin refers to functions (and
19393 many things under the function DIE, but we do not recurse
19394 into function DIEs during partial symbol reading) and
19395 possibly variables as well; DW_AT_specification refers to
19396 declarations. Declarations ought to have the DW_AT_declaration
19397 flag. It happens that GCC forgets to put it in sometimes, but
19398 only for functions, not for types.
19399
19400 Adding more things than necessary to the hash table is harmless
19401 except for the performance cost. Adding too few will result in
5afb4e99
DJ
19402 wasted time in find_partial_die, when we reread the compilation
19403 unit with load_all_dies set. */
72bf9492 19404
5afb4e99 19405 if (load_all
72929c62 19406 || abbrev->tag == DW_TAG_constant
5afb4e99 19407 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
19408 || abbrev->tag == DW_TAG_variable
19409 || abbrev->tag == DW_TAG_namespace
19410 || part_die->is_declaration)
19411 {
19412 void **slot;
19413
19414 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
19415 to_underlying (part_die->sect_off),
19416 INSERT);
72bf9492
DJ
19417 *slot = part_die;
19418 }
19419
72bf9492 19420 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 19421 we have no reason to follow the children of structures; for other
98bfdba5
PA
19422 languages we have to, so that we can get at method physnames
19423 to infer fully qualified class names, for DW_AT_specification,
19424 and for C++ template arguments. For C++, we also look one level
19425 inside functions to find template arguments (if the name of the
19426 function does not already contain the template arguments).
bc30ff58 19427
0a4b0913
AB
19428 For Ada and Fortran, we need to scan the children of subprograms
19429 and lexical blocks as well because these languages allow the
19430 definition of nested entities that could be interesting for the
19431 debugger, such as nested subprograms for instance. */
72bf9492 19432 if (last_die->has_children
5afb4e99
DJ
19433 && (load_all
19434 || last_die->tag == DW_TAG_namespace
f55ee35c 19435 || last_die->tag == DW_TAG_module
72bf9492 19436 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
19437 || (cu->language == language_cplus
19438 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
19439 && (last_die->raw_name == NULL
19440 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
19441 || (cu->language != language_c
19442 && (last_die->tag == DW_TAG_class_type
680b30c7 19443 || last_die->tag == DW_TAG_interface_type
72bf9492 19444 || last_die->tag == DW_TAG_structure_type
bc30ff58 19445 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
19446 || ((cu->language == language_ada
19447 || cu->language == language_fortran)
bc30ff58
JB
19448 && (last_die->tag == DW_TAG_subprogram
19449 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
19450 {
19451 nesting_level++;
19452 parent_die = last_die;
19453 continue;
19454 }
19455
19456 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19457 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
19458
19459 /* Back to the top, do it again. */
19460 }
19461}
19462
6f06d47b
YQ
19463partial_die_info::partial_die_info (sect_offset sect_off_,
19464 struct abbrev_info *abbrev)
19465 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19466{
19467}
19468
7d00ffec
TT
19469/* See class definition. */
19470
19471const char *
19472partial_die_info::name (dwarf2_cu *cu)
19473{
19474 if (!canonical_name && raw_name != nullptr)
19475 {
19476 struct objfile *objfile = cu->per_objfile->objfile;
19477 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19478 canonical_name = 1;
19479 }
19480
19481 return raw_name;
19482}
19483
35cc7ed7
YQ
19484/* Read a minimal amount of information into the minimal die structure.
19485 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 19486
48fbe735
YQ
19487const gdb_byte *
19488partial_die_info::read (const struct die_reader_specs *reader,
19489 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 19490{
dee91e82 19491 struct dwarf2_cu *cu = reader->cu;
976ca316 19492 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 19493 unsigned int i;
c5aa993b 19494 int has_low_pc_attr = 0;
c906108c 19495 int has_high_pc_attr = 0;
91da1414 19496 int high_pc_relative = 0;
c906108c 19497
fd0a254f 19498 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 19499 {
e7da7f8f 19500 attribute attr;
7a5f294d 19501 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18a8505e 19502 /* String and address offsets that need to do the reprocessing have
dda83cd7 19503 already been read at this point, so there is no need to wait until
18a8505e 19504 the loop terminates to do the reprocessing. */
7a5f294d 19505 if (attr.requires_reprocessing_p ())
d0ce17d8 19506 read_attribute_reprocess (reader, &attr, tag);
c906108c 19507 /* Store the data if it is of an attribute we want to keep in a
dda83cd7 19508 partial symbol table. */
c906108c
SS
19509 switch (attr.name)
19510 {
19511 case DW_AT_name:
48fbe735 19512 switch (tag)
71c25dea
TT
19513 {
19514 case DW_TAG_compile_unit:
95554aad 19515 case DW_TAG_partial_unit:
348e048f 19516 case DW_TAG_type_unit:
71c25dea
TT
19517 /* Compilation units have a DW_AT_name that is a filename, not
19518 a source language identifier. */
19519 case DW_TAG_enumeration_type:
19520 case DW_TAG_enumerator:
19521 /* These tags always have simple identifiers already; no need
19522 to canonicalize them. */
7d00ffec 19523 canonical_name = 1;
2c830f54 19524 raw_name = attr.as_string ();
71c25dea
TT
19525 break;
19526 default:
7d00ffec 19527 canonical_name = 0;
2c830f54 19528 raw_name = attr.as_string ();
71c25dea
TT
19529 break;
19530 }
c906108c 19531 break;
31ef98ae 19532 case DW_AT_linkage_name:
c906108c 19533 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19534 /* Note that both forms of linkage name might appear. We
19535 assume they will be the same, and we only store the last
19536 one we see. */
95f982e5 19537 linkage_name = attr.as_string ();
c906108c
SS
19538 break;
19539 case DW_AT_low_pc:
19540 has_low_pc_attr = 1;
95f982e5 19541 lowpc = attr.as_address ();
c906108c
SS
19542 break;
19543 case DW_AT_high_pc:
19544 has_high_pc_attr = 1;
95f982e5 19545 highpc = attr.as_address ();
cd6c91b4 19546 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 19547 high_pc_relative = 1;
c906108c
SS
19548 break;
19549 case DW_AT_location:
dda83cd7
SM
19550 /* Support the .debug_loc offsets. */
19551 if (attr.form_is_block ())
19552 {
9d2246fc 19553 d.locdesc = attr.as_block ();
dda83cd7
SM
19554 }
19555 else if (attr.form_is_section_offset ())
19556 {
4d3c2250 19557 dwarf2_complex_location_expr_complaint ();
dda83cd7
SM
19558 }
19559 else
19560 {
4d3c2250
KB
19561 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19562 "partial symbol information");
dda83cd7 19563 }
c906108c 19564 break;
c906108c 19565 case DW_AT_external:
c45bc3f8 19566 is_external = attr.as_boolean ();
c906108c
SS
19567 break;
19568 case DW_AT_declaration:
c45bc3f8 19569 is_declaration = attr.as_boolean ();
c906108c
SS
19570 break;
19571 case DW_AT_type:
48fbe735 19572 has_type = 1;
c906108c
SS
19573 break;
19574 case DW_AT_abstract_origin:
19575 case DW_AT_specification:
72bf9492 19576 case DW_AT_extension:
48fbe735 19577 has_specification = 1;
0826b30a 19578 spec_offset = attr.get_ref_die_offset ();
48fbe735 19579 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19580 || cu->per_cu->is_dwz);
c906108c
SS
19581 break;
19582 case DW_AT_sibling:
19583 /* Ignore absolute siblings, they might point outside of
19584 the current compile unit. */
19585 if (attr.form == DW_FORM_ref_addr)
b98664d3 19586 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19587 else
b9502d3f 19588 {
48fbe735 19589 const gdb_byte *buffer = reader->buffer;
0826b30a 19590 sect_offset off = attr.get_ref_die_offset ();
9c541725 19591 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19592
19593 if (sibling_ptr < info_ptr)
b98664d3 19594 complaint (_("DW_AT_sibling points backwards"));
22869d73 19595 else if (sibling_ptr > reader->buffer_end)
a0194fa8 19596 reader->die_section->overflow_complaint ();
b9502d3f 19597 else
48fbe735 19598 sibling = sibling_ptr;
b9502d3f 19599 }
c906108c 19600 break;
dda83cd7
SM
19601 case DW_AT_byte_size:
19602 has_byte_size = 1;
19603 break;
19604 case DW_AT_const_value:
19605 has_const_value = 1;
19606 break;
68511cec
CES
19607 case DW_AT_calling_convention:
19608 /* DWARF doesn't provide a way to identify a program's source-level
19609 entry point. DW_AT_calling_convention attributes are only meant
19610 to describe functions' calling conventions.
19611
19612 However, because it's a necessary piece of information in
0c1b455e
TT
19613 Fortran, and before DWARF 4 DW_CC_program was the only
19614 piece of debugging information whose definition refers to
19615 a 'main program' at all, several compilers marked Fortran
19616 main programs with DW_CC_program --- even when those
19617 functions use the standard calling conventions.
19618
19619 Although DWARF now specifies a way to provide this
19620 information, we support this practice for backward
19621 compatibility. */
529908cb 19622 if (attr.constant_value (0) == DW_CC_program
0c1b455e 19623 && cu->language == language_fortran)
48fbe735 19624 main_subprogram = 1;
68511cec 19625 break;
481860b3 19626 case DW_AT_inline:
529908cb
TT
19627 {
19628 LONGEST value = attr.constant_value (-1);
19629 if (value == DW_INL_inlined
19630 || value == DW_INL_declared_inlined)
19631 may_be_inlined = 1;
19632 }
481860b3 19633 break;
95554aad
TT
19634
19635 case DW_AT_import:
48fbe735 19636 if (tag == DW_TAG_imported_unit)
36586728 19637 {
0826b30a 19638 d.sect_off = attr.get_ref_die_offset ();
48fbe735 19639 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19640 || cu->per_cu->is_dwz);
19641 }
95554aad
TT
19642 break;
19643
0c1b455e 19644 case DW_AT_main_subprogram:
c45bc3f8 19645 main_subprogram = attr.as_boolean ();
0c1b455e
TT
19646 break;
19647
05caa1d2
TT
19648 case DW_AT_ranges:
19649 {
d0ce17d8
CT
19650 /* DW_AT_rnglists_base does not apply to DIEs from the DWO
19651 skeleton. We take advantage of the fact the DW_AT_ranges
19652 does not appear in DW_TAG_compile_unit of DWO files.
19653
19654 Attributes of the form DW_FORM_rnglistx have already had
dda83cd7 19655 their value changed by read_rnglist_index and already
d0ce17d8
CT
19656 include DW_AT_rnglists_base, so don't need to add the ranges
19657 base, either. */
19658 int need_ranges_base = (tag != DW_TAG_compile_unit
19659 && attr.form != DW_FORM_rnglistx);
529908cb
TT
19660 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19661 but that requires a full DIE, so instead we just
19662 reimplement it. */
19663 unsigned int ranges_offset = (attr.constant_value (0)
05caa1d2
TT
19664 + (need_ranges_base
19665 ? cu->ranges_base
19666 : 0));
19667
19668 /* Value of the DW_AT_ranges attribute is the offset in the
19669 .debug_ranges section. */
19670 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
d0ce17d8 19671 nullptr, tag))
05caa1d2
TT
19672 has_pc_info = 1;
19673 }
19674 break;
19675
c906108c
SS
19676 default:
19677 break;
19678 }
19679 }
19680
10d06d82
TT
19681 /* For Ada, if both the name and the linkage name appear, we prefer
19682 the latter. This lets "catch exception" work better, regardless
19683 of the order in which the name and linkage name were emitted.
19684 Really, though, this is just a workaround for the fact that gdb
19685 doesn't store both the name and the linkage name. */
19686 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 19687 raw_name = linkage_name;
10d06d82 19688
91da1414 19689 if (high_pc_relative)
48fbe735 19690 highpc += lowpc;
91da1414 19691
9373cf26
JK
19692 if (has_low_pc_attr && has_high_pc_attr)
19693 {
19694 /* When using the GNU linker, .gnu.linkonce. sections are used to
19695 eliminate duplicate copies of functions and vtables and such.
19696 The linker will arbitrarily choose one and discard the others.
19697 The AT_*_pc values for such functions refer to local labels in
19698 these sections. If the section from that file was discarded, the
19699 labels are not in the output, so the relocs get a value of 0.
19700 If this is a discarded function, mark the pc bounds as invalid,
19701 so that GDB will ignore it. */
976ca316 19702 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 19703 {
976ca316 19704 struct objfile *objfile = per_objfile->objfile;
08feed99 19705 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19706
b98664d3 19707 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19708 "for DIE at %s [in module %s]"),
48fbe735
YQ
19709 paddress (gdbarch, lowpc),
19710 sect_offset_str (sect_off),
9d8780f0 19711 objfile_name (objfile));
9373cf26
JK
19712 }
19713 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19714 else if (lowpc >= highpc)
9373cf26 19715 {
976ca316 19716 struct objfile *objfile = per_objfile->objfile;
08feed99 19717 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19718
b98664d3 19719 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19720 "for DIE at %s [in module %s]"),
48fbe735
YQ
19721 paddress (gdbarch, lowpc),
19722 paddress (gdbarch, highpc),
19723 sect_offset_str (sect_off),
9c541725 19724 objfile_name (objfile));
9373cf26
JK
19725 }
19726 else
48fbe735 19727 has_pc_info = 1;
9373cf26 19728 }
85cbf3d3 19729
c906108c
SS
19730 return info_ptr;
19731}
19732
72bf9492
DJ
19733/* Find a cached partial DIE at OFFSET in CU. */
19734
d590ff25
YQ
19735struct partial_die_info *
19736dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19737{
19738 struct partial_die_info *lookup_die = NULL;
6f06d47b 19739 struct partial_die_info part_die (sect_off);
72bf9492 19740
9a3c8263 19741 lookup_die = ((struct partial_die_info *)
d590ff25 19742 htab_find_with_hash (partial_dies, &part_die,
9c541725 19743 to_underlying (sect_off)));
72bf9492 19744
72bf9492
DJ
19745 return lookup_die;
19746}
19747
348e048f
DE
19748/* Find a partial DIE at OFFSET, which may or may not be in CU,
19749 except in the case of .debug_types DIEs which do not reference
19750 outside their CU (they do however referencing other types via
55f1336d 19751 DW_FORM_ref_sig8). */
72bf9492 19752
122cf0f2 19753static const struct cu_partial_die_info
9c541725 19754find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19755{
976ca316
SM
19756 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19757 struct objfile *objfile = per_objfile->objfile;
5afb4e99 19758 struct partial_die_info *pd = NULL;
72bf9492 19759
36586728 19760 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 19761 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 19762 {
d590ff25 19763 pd = cu->find_partial_die (sect_off);
5afb4e99 19764 if (pd != NULL)
fb816e8b 19765 return { cu, pd };
0d99eb77
DE
19766 /* We missed recording what we needed.
19767 Load all dies and try again. */
5afb4e99 19768 }
0d99eb77
DE
19769 else
19770 {
19771 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19772 if (cu->per_cu->is_debug_types)
0d99eb77 19773 {
9d8780f0
SM
19774 error (_("Dwarf Error: Type Unit at offset %s contains"
19775 " external reference to offset %s [in module %s].\n"),
19776 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19777 bfd_get_filename (objfile->obfd));
19778 }
7188ed02
SM
19779 dwarf2_per_cu_data *per_cu
19780 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 19781 per_objfile);
72bf9492 19782
976ca316 19783 cu = per_objfile->get_cu (per_cu);
7188ed02 19784 if (cu == NULL || cu->partial_dies == NULL)
976ca316 19785 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 19786
976ca316 19787 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
19788
19789 cu->last_used = 0;
19790 pd = cu->find_partial_die (sect_off);
0d99eb77 19791 }
5afb4e99 19792
dee91e82
DE
19793 /* If we didn't find it, and not all dies have been loaded,
19794 load them all and try again. */
19795
7188ed02 19796 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 19797 {
7188ed02 19798 cu->per_cu->load_all_dies = 1;
fd820528
DE
19799
19800 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19801 THIS_CU->cu may already be in use. So we can't just free it and
19802 replace its DIEs with the ones we read in. Instead, we leave those
19803 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19804 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19805 set. */
976ca316 19806 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 19807
7188ed02 19808 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
19809 }
19810
19811 if (pd == NULL)
521894aa 19812 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
9d8780f0 19813 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 19814 return { cu, pd };
72bf9492
DJ
19815}
19816
abc72ce4
DE
19817/* See if we can figure out if the class lives in a namespace. We do
19818 this by looking for a member function; its demangled name will
19819 contain namespace info, if there is any. */
19820
19821static void
19822guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19823 struct dwarf2_cu *cu)
19824{
19825 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19826 what template types look like, because the demangler
19827 frequently doesn't give the same name as the debug info. We
19828 could fix this by only using the demangled name to get the
19829 prefix (but see comment in read_structure_type). */
19830
19831 struct partial_die_info *real_pdi;
19832 struct partial_die_info *child_pdi;
19833
19834 /* If this DIE (this DIE's specification, if any) has a parent, then
19835 we should not do this. We'll prepend the parent's fully qualified
19836 name when we create the partial symbol. */
19837
19838 real_pdi = struct_pdi;
19839 while (real_pdi->has_specification)
fb816e8b 19840 {
122cf0f2
AB
19841 auto res = find_partial_die (real_pdi->spec_offset,
19842 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19843 real_pdi = res.pdi;
19844 cu = res.cu;
19845 }
abc72ce4
DE
19846
19847 if (real_pdi->die_parent != NULL)
19848 return;
19849
19850 for (child_pdi = struct_pdi->die_child;
19851 child_pdi != NULL;
19852 child_pdi = child_pdi->die_sibling)
19853 {
19854 if (child_pdi->tag == DW_TAG_subprogram
19855 && child_pdi->linkage_name != NULL)
19856 {
43816ebc 19857 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
19858 (cu->language_defn->class_name_from_physname
19859 (child_pdi->linkage_name));
abc72ce4
DE
19860 if (actual_class_name != NULL)
19861 {
5e22e966 19862 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19863 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19864 struct_pdi->canonical_name = 1;
abc72ce4
DE
19865 }
19866 break;
19867 }
19868 }
19869}
19870
25c11aca
TV
19871/* Return true if a DIE with TAG may have the DW_AT_const_value
19872 attribute. */
19873
19874static bool
19875can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19876{
19877 switch (tag)
19878 {
19879 case DW_TAG_constant:
19880 case DW_TAG_enumerator:
19881 case DW_TAG_formal_parameter:
19882 case DW_TAG_template_value_param:
19883 case DW_TAG_variable:
19884 return true;
19885 }
19886
19887 return false;
19888}
19889
52356b79
YQ
19890void
19891partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19892{
abc72ce4
DE
19893 /* Once we've fixed up a die, there's no point in doing so again.
19894 This also avoids a memory leak if we were to call
19895 guess_partial_die_structure_name multiple times. */
52356b79 19896 if (fixup_called)
abc72ce4
DE
19897 return;
19898
72bf9492
DJ
19899 /* If we found a reference attribute and the DIE has no name, try
19900 to find a name in the referred to DIE. */
19901
7d00ffec 19902 if (raw_name == NULL && has_specification)
72bf9492
DJ
19903 {
19904 struct partial_die_info *spec_die;
72bf9492 19905
122cf0f2 19906 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19907 spec_die = res.pdi;
19908 cu = res.cu;
72bf9492 19909
52356b79 19910 spec_die->fixup (cu);
72bf9492 19911
7d00ffec 19912 if (spec_die->raw_name)
72bf9492 19913 {
7d00ffec
TT
19914 raw_name = spec_die->raw_name;
19915 canonical_name = spec_die->canonical_name;
72bf9492
DJ
19916
19917 /* Copy DW_AT_external attribute if it is set. */
19918 if (spec_die->is_external)
52356b79 19919 is_external = spec_die->is_external;
72bf9492
DJ
19920 }
19921 }
19922
25c11aca
TV
19923 if (!has_const_value && has_specification
19924 && can_have_DW_AT_const_value_p (tag))
19925 {
19926 struct partial_die_info *spec_die;
19927
19928 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19929 spec_die = res.pdi;
19930 cu = res.cu;
19931
19932 spec_die->fixup (cu);
19933
19934 if (spec_die->has_const_value)
19935 {
19936 /* Copy DW_AT_const_value attribute if it is set. */
19937 has_const_value = spec_die->has_const_value;
19938 }
19939 }
19940
72bf9492 19941 /* Set default names for some unnamed DIEs. */
72bf9492 19942
7d00ffec
TT
19943 if (raw_name == NULL && tag == DW_TAG_namespace)
19944 {
19945 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19946 canonical_name = 1;
19947 }
72bf9492 19948
abc72ce4
DE
19949 /* If there is no parent die to provide a namespace, and there are
19950 children, see if we can determine the namespace from their linkage
122d1940 19951 name. */
abc72ce4 19952 if (cu->language == language_cplus
5e22e966 19953 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
19954 && die_parent == NULL
19955 && has_children
19956 && (tag == DW_TAG_class_type
19957 || tag == DW_TAG_structure_type
19958 || tag == DW_TAG_union_type))
19959 guess_partial_die_structure_name (this, cu);
abc72ce4 19960
53832f31
TT
19961 /* GCC might emit a nameless struct or union that has a linkage
19962 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 19963 if (raw_name == NULL
52356b79
YQ
19964 && (tag == DW_TAG_class_type
19965 || tag == DW_TAG_interface_type
19966 || tag == DW_TAG_structure_type
19967 || tag == DW_TAG_union_type)
19968 && linkage_name != NULL)
53832f31 19969 {
43816ebc
TT
19970 gdb::unique_xmalloc_ptr<char> demangled
19971 (gdb_demangle (linkage_name, DMGL_TYPES));
19972 if (demangled != nullptr)
53832f31 19973 {
96408a79
SA
19974 const char *base;
19975
19976 /* Strip any leading namespaces/classes, keep only the base name.
19977 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
19978 base = strrchr (demangled.get (), ':');
19979 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
19980 base++;
19981 else
43816ebc 19982 base = demangled.get ();
96408a79 19983
5e22e966 19984 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19985 raw_name = objfile->intern (base);
19986 canonical_name = 1;
53832f31
TT
19987 }
19988 }
19989
52356b79 19990 fixup_called = 1;
72bf9492
DJ
19991}
19992
d0ce17d8
CT
19993/* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19994 contents from the given SECTION in the HEADER. */
41144253 19995static void
d0ce17d8
CT
19996read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19997 struct dwarf2_section_info *section)
41144253 19998{
19999 unsigned int bytes_read;
20000 bfd *abfd = section->get_bfd_owner ();
20001 const gdb_byte *info_ptr = section->buffer;
20002 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20003 info_ptr += bytes_read;
20004 header->version = read_2_bytes (abfd, info_ptr);
20005 info_ptr += 2;
20006 header->addr_size = read_1_byte (abfd, info_ptr);
20007 info_ptr += 1;
20008 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20009 info_ptr += 1;
20010 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20011}
20012
20013/* Return the DW_AT_loclists_base value for the CU. */
20014static ULONGEST
20015lookup_loclist_base (struct dwarf2_cu *cu)
20016{
20017 /* For the .dwo unit, the loclist_base points to the first offset following
20018 the header. The header consists of the following entities-
20019 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20020 bit format)
20021 2. version (2 bytes)
20022 3. address size (1 byte)
20023 4. segment selector size (1 byte)
20024 5. offset entry count (4 bytes)
20025 These sizes are derived as per the DWARFv5 standard. */
20026 if (cu->dwo_unit != nullptr)
20027 {
20028 if (cu->header.initial_length_size == 4)
20029 return LOCLIST_HEADER_SIZE32;
20030 return LOCLIST_HEADER_SIZE64;
20031 }
20032 return cu->loclist_base;
20033}
20034
20035/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20036 array of offsets in the .debug_loclists section. */
20037static CORE_ADDR
20038read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20039{
976ca316
SM
20040 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20041 struct objfile *objfile = per_objfile->objfile;
41144253 20042 bfd *abfd = objfile->obfd;
20043 ULONGEST loclist_base = lookup_loclist_base (cu);
20044 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20045
20046 section->read (objfile);
20047 if (section->buffer == NULL)
20048 complaint (_("DW_FORM_loclistx used without .debug_loclists "
dda83cd7 20049 "section [in module %s]"), objfile_name (objfile));
d0ce17d8
CT
20050 struct loclists_rnglists_header header;
20051 read_loclists_rnglists_header (&header, section);
41144253 20052 if (loclist_index >= header.offset_entry_count)
20053 complaint (_("DW_FORM_loclistx pointing outside of "
dda83cd7
SM
20054 ".debug_loclists offset array [in module %s]"),
20055 objfile_name (objfile));
41144253 20056 if (loclist_base + loclist_index * cu->header.offset_size
20057 >= section->size)
20058 complaint (_("DW_FORM_loclistx pointing outside of "
dda83cd7
SM
20059 ".debug_loclists section [in module %s]"),
20060 objfile_name (objfile));
41144253 20061 const gdb_byte *info_ptr
20062 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
20063
20064 if (cu->header.offset_size == 4)
20065 return bfd_get_32 (abfd, info_ptr) + loclist_base;
20066 else
20067 return bfd_get_64 (abfd, info_ptr) + loclist_base;
20068}
20069
d0ce17d8
CT
20070/* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20071 array of offsets in the .debug_rnglists section. */
20072static CORE_ADDR
20073read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20074 dwarf_tag tag)
20075{
20076 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20077 struct objfile *objfile = dwarf2_per_objfile->objfile;
20078 bfd *abfd = objfile->obfd;
20079 ULONGEST rnglist_header_size =
20080 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20081 : RNGLIST_HEADER_SIZE64);
20082 ULONGEST rnglist_base =
20083 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->ranges_base;
20084 ULONGEST start_offset =
20085 rnglist_base + rnglist_index * cu->header.offset_size;
20086
20087 /* Get rnglists section. */
20088 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20089
20090 /* Read the rnglists section content. */
20091 section->read (objfile);
20092 if (section->buffer == nullptr)
20093 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20094 "[in module %s]"),
20095 objfile_name (objfile));
20096
20097 /* Verify the rnglist index is valid. */
20098 struct loclists_rnglists_header header;
20099 read_loclists_rnglists_header (&header, section);
20100 if (rnglist_index >= header.offset_entry_count)
20101 error (_("DW_FORM_rnglistx index pointing outside of "
20102 ".debug_rnglists offset array [in module %s]"),
20103 objfile_name (objfile));
20104
20105 /* Validate that the offset is within the section's range. */
20106 if (start_offset >= section->size)
20107 error (_("DW_FORM_rnglistx pointing outside of "
dda83cd7 20108 ".debug_rnglists section [in module %s]"),
d0ce17d8
CT
20109 objfile_name (objfile));
20110
20111 /* Validate that reading won't go beyond the end of the section. */
20112 if (start_offset + cu->header.offset_size > rnglist_base + section->size)
20113 error (_("Reading DW_FORM_rnglistx index beyond end of"
20114 ".debug_rnglists section [in module %s]"),
20115 objfile_name (objfile));
20116
20117 const gdb_byte *info_ptr = section->buffer + start_offset;
20118
20119 if (cu->header.offset_size == 4)
20120 return read_4_bytes (abfd, info_ptr) + rnglist_base;
20121 else
20122 return read_8_bytes (abfd, info_ptr) + rnglist_base;
20123}
20124
18a8505e
AT
20125/* Process the attributes that had to be skipped in the first round. These
20126 attributes are the ones that need str_offsets_base or addr_base attributes.
20127 They could not have been processed in the first round, because at the time
20128 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
20129static void
20130read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 20131 struct attribute *attr, dwarf_tag tag)
18a8505e
AT
20132{
20133 struct dwarf2_cu *cu = reader->cu;
20134 switch (attr->form)
20135 {
20136 case DW_FORM_addrx:
20137 case DW_FORM_GNU_addr_index:
36d378cf
TT
20138 attr->set_address (read_addr_index (cu,
20139 attr->as_unsigned_reprocess ()));
dda83cd7 20140 break;
41144253 20141 case DW_FORM_loclistx:
529908cb 20142 attr->set_address (read_loclist_index (cu, attr->as_unsigned ()));
41144253 20143 break;
d0ce17d8 20144 case DW_FORM_rnglistx:
529908cb 20145 attr->set_address (read_rnglist_index (cu, attr->as_unsigned (), tag));
dda83cd7 20146 break;
18a8505e
AT
20147 case DW_FORM_strx:
20148 case DW_FORM_strx1:
20149 case DW_FORM_strx2:
20150 case DW_FORM_strx3:
20151 case DW_FORM_strx4:
20152 case DW_FORM_GNU_str_index:
20153 {
fe56917a 20154 unsigned int str_index = attr->as_unsigned_reprocess ();
c6481205 20155 gdb_assert (!attr->canonical_string_p ());
18a8505e 20156 if (reader->dwo_file != NULL)
c6481205
TT
20157 attr->set_string_noncanonical (read_dwo_str_index (reader,
20158 str_index));
18a8505e 20159 else
c6481205
TT
20160 attr->set_string_noncanonical (read_stub_str_index (cu,
20161 str_index));
18a8505e
AT
20162 break;
20163 }
20164 default:
20165 gdb_assert_not_reached (_("Unexpected DWARF form."));
20166 }
20167}
20168
a8329558 20169/* Read an attribute value described by an attribute form. */
c906108c 20170
d521ce57 20171static const gdb_byte *
dee91e82
DE
20172read_attribute_value (const struct die_reader_specs *reader,
20173 struct attribute *attr, unsigned form,
7a5f294d 20174 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 20175{
dee91e82 20176 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
20177 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20178 struct objfile *objfile = per_objfile->objfile;
dee91e82 20179 bfd *abfd = reader->abfd;
e7c27a73 20180 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
20181 unsigned int bytes_read;
20182 struct dwarf_block *blk;
20183
aead7601 20184 attr->form = (enum dwarf_form) form;
a8329558 20185 switch (form)
c906108c 20186 {
c906108c 20187 case DW_FORM_ref_addr:
ae411497 20188 if (cu->header.version == 2)
414ad644
TT
20189 attr->set_unsigned (cu->header.read_address (abfd, info_ptr,
20190 &bytes_read));
ae411497 20191 else
414ad644
TT
20192 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20193 &bytes_read));
ae411497
TT
20194 info_ptr += bytes_read;
20195 break;
36586728 20196 case DW_FORM_GNU_ref_alt:
414ad644
TT
20197 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20198 &bytes_read));
36586728
TT
20199 info_ptr += bytes_read;
20200 break;
ae411497 20201 case DW_FORM_addr:
08feed99
TT
20202 {
20203 struct gdbarch *gdbarch = objfile->arch ();
36d378cf
TT
20204 CORE_ADDR addr = cu->header.read_address (abfd, info_ptr, &bytes_read);
20205 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20206 attr->set_address (addr);
08feed99
TT
20207 info_ptr += bytes_read;
20208 }
c906108c
SS
20209 break;
20210 case DW_FORM_block2:
7b5a2f43 20211 blk = dwarf_alloc_block (cu);
c906108c
SS
20212 blk->size = read_2_bytes (abfd, info_ptr);
20213 info_ptr += 2;
20214 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20215 info_ptr += blk->size;
9d2246fc 20216 attr->set_block (blk);
c906108c
SS
20217 break;
20218 case DW_FORM_block4:
7b5a2f43 20219 blk = dwarf_alloc_block (cu);
c906108c
SS
20220 blk->size = read_4_bytes (abfd, info_ptr);
20221 info_ptr += 4;
20222 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20223 info_ptr += blk->size;
9d2246fc 20224 attr->set_block (blk);
c906108c
SS
20225 break;
20226 case DW_FORM_data2:
414ad644 20227 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
c906108c
SS
20228 info_ptr += 2;
20229 break;
20230 case DW_FORM_data4:
414ad644 20231 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
c906108c
SS
20232 info_ptr += 4;
20233 break;
20234 case DW_FORM_data8:
414ad644 20235 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
c906108c
SS
20236 info_ptr += 8;
20237 break;
0224619f
JK
20238 case DW_FORM_data16:
20239 blk = dwarf_alloc_block (cu);
20240 blk->size = 16;
20241 blk->data = read_n_bytes (abfd, info_ptr, 16);
20242 info_ptr += 16;
9d2246fc 20243 attr->set_block (blk);
0224619f 20244 break;
2dc7f7b3 20245 case DW_FORM_sec_offset:
414ad644
TT
20246 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20247 &bytes_read));
2dc7f7b3
TT
20248 info_ptr += bytes_read;
20249 break;
41144253 20250 case DW_FORM_loclistx:
20251 {
7a5f294d
TT
20252 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20253 &bytes_read));
20254 info_ptr += bytes_read;
41144253 20255 }
20256 break;
c906108c 20257 case DW_FORM_string:
c6481205
TT
20258 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20259 &bytes_read));
c906108c
SS
20260 info_ptr += bytes_read;
20261 break;
4bdf3d34 20262 case DW_FORM_strp:
36586728
TT
20263 if (!cu->per_cu->is_dwz)
20264 {
c6481205
TT
20265 attr->set_string_noncanonical
20266 (read_indirect_string (per_objfile,
20267 abfd, info_ptr, cu_header,
20268 &bytes_read));
36586728
TT
20269 info_ptr += bytes_read;
20270 break;
20271 }
20272 /* FALLTHROUGH */
43988095
JK
20273 case DW_FORM_line_strp:
20274 if (!cu->per_cu->is_dwz)
20275 {
c6481205
TT
20276 attr->set_string_noncanonical
20277 (per_objfile->read_line_string (info_ptr, cu_header,
20278 &bytes_read));
43988095
JK
20279 info_ptr += bytes_read;
20280 break;
20281 }
20282 /* FALLTHROUGH */
36586728
TT
20283 case DW_FORM_GNU_strp_alt:
20284 {
976ca316 20285 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8266302d
TT
20286 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20287 &bytes_read);
36586728 20288
c6481205
TT
20289 attr->set_string_noncanonical
20290 (dwz->read_string (objfile, str_offset));
36586728
TT
20291 info_ptr += bytes_read;
20292 }
4bdf3d34 20293 break;
2dc7f7b3 20294 case DW_FORM_exprloc:
c906108c 20295 case DW_FORM_block:
7b5a2f43 20296 blk = dwarf_alloc_block (cu);
c906108c
SS
20297 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20298 info_ptr += bytes_read;
20299 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20300 info_ptr += blk->size;
9d2246fc 20301 attr->set_block (blk);
c906108c
SS
20302 break;
20303 case DW_FORM_block1:
7b5a2f43 20304 blk = dwarf_alloc_block (cu);
c906108c
SS
20305 blk->size = read_1_byte (abfd, info_ptr);
20306 info_ptr += 1;
20307 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20308 info_ptr += blk->size;
9d2246fc 20309 attr->set_block (blk);
c906108c
SS
20310 break;
20311 case DW_FORM_data1:
c906108c 20312 case DW_FORM_flag:
414ad644 20313 attr->set_unsigned (read_1_byte (abfd, info_ptr));
c906108c
SS
20314 info_ptr += 1;
20315 break;
2dc7f7b3 20316 case DW_FORM_flag_present:
414ad644 20317 attr->set_unsigned (1);
2dc7f7b3 20318 break;
c906108c 20319 case DW_FORM_sdata:
1bc397c5 20320 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20321 info_ptr += bytes_read;
20322 break;
18a8505e 20323 case DW_FORM_rnglistx:
7a5f294d
TT
20324 {
20325 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20326 &bytes_read));
20327 info_ptr += bytes_read;
20328 }
20329 break;
d0ce17d8 20330 case DW_FORM_udata:
414ad644 20331 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20332 info_ptr += bytes_read;
20333 break;
20334 case DW_FORM_ref1:
414ad644
TT
20335 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20336 + read_1_byte (abfd, info_ptr)));
c906108c
SS
20337 info_ptr += 1;
20338 break;
20339 case DW_FORM_ref2:
414ad644
TT
20340 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20341 + read_2_bytes (abfd, info_ptr)));
c906108c
SS
20342 info_ptr += 2;
20343 break;
20344 case DW_FORM_ref4:
414ad644
TT
20345 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20346 + read_4_bytes (abfd, info_ptr)));
c906108c
SS
20347 info_ptr += 4;
20348 break;
613e1657 20349 case DW_FORM_ref8:
414ad644
TT
20350 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20351 + read_8_bytes (abfd, info_ptr)));
613e1657
KB
20352 info_ptr += 8;
20353 break;
55f1336d 20354 case DW_FORM_ref_sig8:
630ed6b9 20355 attr->set_signature (read_8_bytes (abfd, info_ptr));
348e048f
DE
20356 info_ptr += 8;
20357 break;
c906108c 20358 case DW_FORM_ref_udata:
414ad644
TT
20359 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20360 + read_unsigned_leb128 (abfd, info_ptr,
20361 &bytes_read)));
c906108c
SS
20362 info_ptr += bytes_read;
20363 break;
c906108c 20364 case DW_FORM_indirect:
a8329558
KW
20365 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20366 info_ptr += bytes_read;
43988095
JK
20367 if (form == DW_FORM_implicit_const)
20368 {
20369 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20370 info_ptr += bytes_read;
20371 }
20372 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
7a5f294d 20373 info_ptr);
43988095
JK
20374 break;
20375 case DW_FORM_implicit_const:
1bc397c5 20376 attr->set_signed (implicit_const);
a8329558 20377 break;
336d760d 20378 case DW_FORM_addrx:
3019eac3 20379 case DW_FORM_GNU_addr_index:
fe56917a
TT
20380 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20381 &bytes_read));
3019eac3
DE
20382 info_ptr += bytes_read;
20383 break;
cf532bd1 20384 case DW_FORM_strx:
15f18d14
AT
20385 case DW_FORM_strx1:
20386 case DW_FORM_strx2:
20387 case DW_FORM_strx3:
20388 case DW_FORM_strx4:
3019eac3 20389 case DW_FORM_GNU_str_index:
3019eac3 20390 {
15f18d14
AT
20391 ULONGEST str_index;
20392 if (form == DW_FORM_strx1)
20393 {
20394 str_index = read_1_byte (abfd, info_ptr);
20395 info_ptr += 1;
20396 }
20397 else if (form == DW_FORM_strx2)
20398 {
20399 str_index = read_2_bytes (abfd, info_ptr);
20400 info_ptr += 2;
20401 }
20402 else if (form == DW_FORM_strx3)
20403 {
20404 str_index = read_3_bytes (abfd, info_ptr);
20405 info_ptr += 3;
20406 }
20407 else if (form == DW_FORM_strx4)
20408 {
20409 str_index = read_4_bytes (abfd, info_ptr);
20410 info_ptr += 4;
20411 }
20412 else
20413 {
20414 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20415 info_ptr += bytes_read;
20416 }
fe56917a 20417 attr->set_unsigned_reprocess (str_index);
7a5f294d 20418 }
3019eac3 20419 break;
c906108c 20420 default:
8a3fe4f8 20421 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
20422 dwarf_form_name (form),
20423 bfd_get_filename (abfd));
c906108c 20424 }
28e94949 20425
36586728 20426 /* Super hack. */
cd6c91b4 20427 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
20428 attr->form = DW_FORM_GNU_ref_alt;
20429
28e94949
JB
20430 /* We have seen instances where the compiler tried to emit a byte
20431 size attribute of -1 which ended up being encoded as an unsigned
20432 0xffffffff. Although 0xffffffff is technically a valid size value,
20433 an object of this size seems pretty unlikely so we can relatively
20434 safely treat these cases as if the size attribute was invalid and
20435 treat them as zero by default. */
20436 if (attr->name == DW_AT_byte_size
20437 && form == DW_FORM_data4
529908cb 20438 && attr->as_unsigned () >= 0xffffffff)
01c66ae6
JB
20439 {
20440 complaint
dda83cd7
SM
20441 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20442 hex_string (attr->as_unsigned ()));
414ad644 20443 attr->set_unsigned (0);
01c66ae6 20444 }
28e94949 20445
c906108c
SS
20446 return info_ptr;
20447}
20448
a8329558
KW
20449/* Read an attribute described by an abbreviated attribute. */
20450
d521ce57 20451static const gdb_byte *
dee91e82
DE
20452read_attribute (const struct die_reader_specs *reader,
20453 struct attribute *attr, struct attr_abbrev *abbrev,
7a5f294d 20454 const gdb_byte *info_ptr)
a8329558
KW
20455{
20456 attr->name = abbrev->name;
c6481205 20457 attr->string_is_canonical = 0;
fe56917a 20458 attr->requires_reprocessing = 0;
43988095 20459 return read_attribute_value (reader, attr, abbrev->form,
7a5f294d 20460 abbrev->implicit_const, info_ptr);
a8329558
KW
20461}
20462
43988095
JK
20463/* Return pointer to string at .debug_str offset STR_OFFSET. */
20464
20465static const char *
976ca316 20466read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 20467 LONGEST str_offset)
43988095 20468{
976ca316
SM
20469 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20470 str_offset, "DW_FORM_strp");
c906108c
SS
20471}
20472
43988095
JK
20473/* Return pointer to string at .debug_str offset as read from BUF.
20474 BUF is assumed to be in a compilation unit described by CU_HEADER.
20475 Return *BYTES_READ_PTR count of bytes read from BUF. */
20476
d521ce57 20477static const char *
976ca316 20478read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 20479 const gdb_byte *buf,
cf2c3c16
TT
20480 const struct comp_unit_head *cu_header,
20481 unsigned int *bytes_read_ptr)
20482{
8266302d 20483 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 20484
976ca316 20485 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
20486}
20487
86c0bb4c 20488/* See read.h. */
43988095 20489
86c0bb4c
TT
20490const char *
20491dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
20492 const struct comp_unit_head *cu_header,
20493 unsigned int *bytes_read_ptr)
43988095 20494{
86c0bb4c 20495 bfd *abfd = objfile->obfd;
8266302d 20496 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 20497
5989a64e 20498 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
20499}
20500
3019eac3 20501/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20502 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20503 ADDR_SIZE is the size of addresses from the CU header. */
20504
20505static CORE_ADDR
976ca316
SM
20506read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20507 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 20508{
976ca316 20509 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
20510 bfd *abfd = objfile->obfd;
20511 const gdb_byte *info_ptr;
18a8505e 20512 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 20513
976ca316
SM
20514 per_objfile->per_bfd->addr.read (objfile);
20515 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 20516 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20517 objfile_name (objfile));
18a8505e 20518 if (addr_base_or_zero + addr_index * addr_size
976ca316 20519 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
20520 error (_("DW_FORM_addr_index pointing outside of "
20521 ".debug_addr section [in module %s]"),
4262abfb 20522 objfile_name (objfile));
976ca316
SM
20523 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20524 + addr_index * addr_size);
3019eac3
DE
20525 if (addr_size == 4)
20526 return bfd_get_32 (abfd, info_ptr);
20527 else
20528 return bfd_get_64 (abfd, info_ptr);
20529}
20530
20531/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20532
20533static CORE_ADDR
20534read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20535{
5e22e966 20536 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 20537 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20538}
20539
20540/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20541
20542static CORE_ADDR
d521ce57 20543read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20544 unsigned int *bytes_read)
20545{
5e22e966 20546 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
20547 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20548
20549 return read_addr_index (cu, addr_index);
20550}
20551
450a1bfc 20552/* See read.h. */
3019eac3
DE
20553
20554CORE_ADDR
82ca3f51 20555dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 20556 dwarf2_per_objfile *per_objfile,
82ca3f51 20557 unsigned int addr_index)
3019eac3 20558{
976ca316 20559 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 20560 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20561 int addr_size;
20562
3019eac3
DE
20563 /* We need addr_base and addr_size.
20564 If we don't have PER_CU->cu, we have to get it.
20565 Nasty, but the alternative is storing the needed info in PER_CU,
20566 which at this point doesn't seem justified: it's not clear how frequently
20567 it would get used and it would increase the size of every PER_CU.
20568 Entry points like dwarf2_per_cu_addr_size do a similar thing
20569 so we're not in uncharted territory here.
20570 Alas we need to be a bit more complicated as addr_base is contained
20571 in the DIE.
20572
20573 We don't need to read the entire CU(/TU).
20574 We just need the header and top level die.
a1b64ce1 20575
3019eac3 20576 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20577 For now we skip this optimization. */
3019eac3
DE
20578
20579 if (cu != NULL)
20580 {
20581 addr_base = cu->addr_base;
20582 addr_size = cu->header.addr_size;
20583 }
20584 else
20585 {
976ca316 20586 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
20587 addr_base = reader.cu->addr_base;
20588 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20589 }
20590
976ca316 20591 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
20592}
20593
18a8505e
AT
20594/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20595 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20596 DWO file. */
3019eac3 20597
d521ce57 20598static const char *
18a8505e
AT
20599read_str_index (struct dwarf2_cu *cu,
20600 struct dwarf2_section_info *str_section,
20601 struct dwarf2_section_info *str_offsets_section,
20602 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20603{
976ca316
SM
20604 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20605 struct objfile *objfile = per_objfile->objfile;
c5164cbc 20606 const char *objf_name = objfile_name (objfile);
3019eac3 20607 bfd *abfd = objfile->obfd;
d521ce57 20608 const gdb_byte *info_ptr;
3019eac3 20609 ULONGEST str_offset;
cf532bd1 20610 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20611
96b79293
TT
20612 str_section->read (objfile);
20613 str_offsets_section->read (objfile);
73869dc2 20614 if (str_section->buffer == NULL)
18a8505e 20615 error (_("%s used without %s section"
9d8780f0 20616 " in CU at offset %s [in module %s]"),
96b79293 20617 form_name, str_section->get_name (),
dda83cd7 20618 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20619 if (str_offsets_section->buffer == NULL)
18a8505e 20620 error (_("%s used without %s section"
9d8780f0 20621 " in CU at offset %s [in module %s]"),
96b79293 20622 form_name, str_section->get_name (),
dda83cd7 20623 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20624 info_ptr = (str_offsets_section->buffer
18a8505e 20625 + str_offsets_base
3019eac3
DE
20626 + str_index * cu->header.offset_size);
20627 if (cu->header.offset_size == 4)
20628 str_offset = bfd_get_32 (abfd, info_ptr);
20629 else
20630 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20631 if (str_offset >= str_section->size)
57d63ce2 20632 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20633 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20634 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20635 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20636}
20637
18a8505e
AT
20638/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20639
20640static const char *
20641read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20642{
20643 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20644 ? reader->cu->header.addr_size : 0;
20645 return read_str_index (reader->cu,
20646 &reader->dwo_file->sections.str,
20647 &reader->dwo_file->sections.str_offsets,
20648 str_offsets_base, str_index);
20649}
20650
20651/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20652
20653static const char *
20654read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20655{
5e22e966 20656 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
20657 const char *objf_name = objfile_name (objfile);
20658 static const char form_name[] = "DW_FORM_GNU_str_index";
20659 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20660
20661 if (!cu->str_offsets_base.has_value ())
20662 error (_("%s used in Fission stub without %s"
20663 " in CU at offset 0x%lx [in module %s]"),
20664 form_name, str_offsets_attr_name,
20665 (long) cu->header.offset_size, objf_name);
20666
20667 return read_str_index (cu,
5e22e966
SM
20668 &cu->per_objfile->per_bfd->str,
20669 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
20670 *cu->str_offsets_base, str_index);
20671}
20672
3019eac3
DE
20673/* Return the length of an LEB128 number in BUF. */
20674
20675static int
20676leb128_size (const gdb_byte *buf)
20677{
20678 const gdb_byte *begin = buf;
20679 gdb_byte byte;
20680
20681 while (1)
20682 {
20683 byte = *buf++;
20684 if ((byte & 128) == 0)
20685 return buf - begin;
20686 }
20687}
20688
c906108c 20689static void
e142c38c 20690set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20691{
20692 switch (lang)
20693 {
20694 case DW_LANG_C89:
76bee0cc 20695 case DW_LANG_C99:
0cfd832f 20696 case DW_LANG_C11:
c906108c 20697 case DW_LANG_C:
d1be3247 20698 case DW_LANG_UPC:
e142c38c 20699 cu->language = language_c;
c906108c 20700 break;
9c37b5ae 20701 case DW_LANG_Java:
c906108c 20702 case DW_LANG_C_plus_plus:
0cfd832f
MW
20703 case DW_LANG_C_plus_plus_11:
20704 case DW_LANG_C_plus_plus_14:
e142c38c 20705 cu->language = language_cplus;
c906108c 20706 break;
6aecb9c2
JB
20707 case DW_LANG_D:
20708 cu->language = language_d;
20709 break;
c906108c
SS
20710 case DW_LANG_Fortran77:
20711 case DW_LANG_Fortran90:
b21b22e0 20712 case DW_LANG_Fortran95:
f7de9aab
MW
20713 case DW_LANG_Fortran03:
20714 case DW_LANG_Fortran08:
e142c38c 20715 cu->language = language_fortran;
c906108c 20716 break;
a766d390
DE
20717 case DW_LANG_Go:
20718 cu->language = language_go;
20719 break;
c906108c 20720 case DW_LANG_Mips_Assembler:
e142c38c 20721 cu->language = language_asm;
c906108c
SS
20722 break;
20723 case DW_LANG_Ada83:
8aaf0b47 20724 case DW_LANG_Ada95:
bc5f45f8
JB
20725 cu->language = language_ada;
20726 break;
72019c9c
GM
20727 case DW_LANG_Modula2:
20728 cu->language = language_m2;
20729 break;
fe8e67fd
PM
20730 case DW_LANG_Pascal83:
20731 cu->language = language_pascal;
20732 break;
22566fbd
DJ
20733 case DW_LANG_ObjC:
20734 cu->language = language_objc;
20735 break;
c44af4eb
TT
20736 case DW_LANG_Rust:
20737 case DW_LANG_Rust_old:
20738 cu->language = language_rust;
20739 break;
c906108c
SS
20740 case DW_LANG_Cobol74:
20741 case DW_LANG_Cobol85:
c906108c 20742 default:
e142c38c 20743 cu->language = language_minimal;
c906108c
SS
20744 break;
20745 }
e142c38c 20746 cu->language_defn = language_def (cu->language);
c906108c
SS
20747}
20748
20749/* Return the named attribute or NULL if not there. */
20750
20751static struct attribute *
e142c38c 20752dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20753{
a48e046c 20754 for (;;)
c906108c 20755 {
a48e046c
TT
20756 unsigned int i;
20757 struct attribute *spec = NULL;
20758
20759 for (i = 0; i < die->num_attrs; ++i)
20760 {
20761 if (die->attrs[i].name == name)
20762 return &die->attrs[i];
20763 if (die->attrs[i].name == DW_AT_specification
20764 || die->attrs[i].name == DW_AT_abstract_origin)
20765 spec = &die->attrs[i];
20766 }
20767
20768 if (!spec)
20769 break;
c906108c 20770
f2f0e013 20771 die = follow_die_ref (die, spec, &cu);
f2f0e013 20772 }
c5aa993b 20773
c906108c
SS
20774 return NULL;
20775}
20776
7d45c7c3
KB
20777/* Return the string associated with a string-typed attribute, or NULL if it
20778 is either not found or is of an incorrect type. */
20779
20780static const char *
20781dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20782{
20783 struct attribute *attr;
20784 const char *str = NULL;
20785
20786 attr = dwarf2_attr (die, name, cu);
20787
20788 if (attr != NULL)
20789 {
95f982e5 20790 str = attr->as_string ();
e61108c9 20791 if (str == nullptr)
dda83cd7 20792 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20793 "DIE at %s in module %s"),
20794 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 20795 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
20796 }
20797
20798 return str;
20799}
20800
a084a2a6 20801/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20802 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20803static const char *
20804dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20805{
20806 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20807 if (dwo_name == nullptr)
20808 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20809 return dwo_name;
20810}
20811
05cf31d1
JB
20812/* Return non-zero iff the attribute NAME is defined for the given DIE,
20813 and holds a non-zero value. This function should only be used for
2dc7f7b3 20814 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20815
20816static int
20817dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20818{
20819 struct attribute *attr = dwarf2_attr (die, name, cu);
20820
c45bc3f8 20821 return attr != nullptr && attr->as_boolean ();
05cf31d1
JB
20822}
20823
3ca72b44 20824static int
e142c38c 20825die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20826{
05cf31d1
JB
20827 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20828 which value is non-zero. However, we have to be careful with
20829 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20830 (via dwarf2_flag_true_p) follows this attribute. So we may
20831 end up accidently finding a declaration attribute that belongs
20832 to a different DIE referenced by the specification attribute,
20833 even though the given DIE does not have a declaration attribute. */
20834 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20835 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20836}
20837
63d06c5c 20838/* Return the die giving the specification for DIE, if there is
f2f0e013 20839 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20840 containing the return value on output. If there is no
20841 specification, but there is an abstract origin, that is
20842 returned. */
63d06c5c
DC
20843
20844static struct die_info *
f2f0e013 20845die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20846{
f2f0e013
DJ
20847 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20848 *spec_cu);
63d06c5c 20849
edb3359d
DJ
20850 if (spec_attr == NULL)
20851 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20852
63d06c5c
DC
20853 if (spec_attr == NULL)
20854 return NULL;
20855 else
f2f0e013 20856 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20857}
c906108c 20858
527f3840
JK
20859/* Stub for free_line_header to match void * callback types. */
20860
20861static void
20862free_line_header_voidp (void *arg)
20863{
9a3c8263 20864 struct line_header *lh = (struct line_header *) arg;
527f3840 20865
fff8551c 20866 delete lh;
527f3840
JK
20867}
20868
83769d0b 20869/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20870
20871static struct dwarf2_section_info *
20872get_debug_line_section (struct dwarf2_cu *cu)
20873{
20874 struct dwarf2_section_info *section;
976ca316 20875 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
20876
20877 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20878 DWO file. */
20879 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20880 section = &cu->dwo_unit->dwo_file->sections.line;
20881 else if (cu->per_cu->is_dwz)
20882 {
976ca316 20883 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
36586728
TT
20884
20885 section = &dwz->line;
20886 }
20887 else
976ca316 20888 section = &per_objfile->per_bfd->line;
36586728
TT
20889
20890 return section;
20891}
20892
debd256d 20893/* Read the statement program header starting at OFFSET in
3019eac3 20894 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20895 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20896 Returns NULL if there is a problem reading the header, e.g., if it
20897 has a version we don't understand.
debd256d
JB
20898
20899 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20900 the returned object point into the dwarf line section buffer,
20901 and must not be freed. */
ae2de4f8 20902
fff8551c 20903static line_header_up
9c541725 20904dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20905{
3019eac3 20906 struct dwarf2_section_info *section;
976ca316 20907 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 20908
36586728 20909 section = get_debug_line_section (cu);
976ca316 20910 section->read (per_objfile->objfile);
3019eac3 20911 if (section->buffer == NULL)
debd256d 20912 {
3019eac3 20913 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20914 complaint (_("missing .debug_line.dwo section"));
3019eac3 20915 else
b98664d3 20916 complaint (_("missing .debug_line section"));
debd256d
JB
20917 return 0;
20918 }
20919
0df7ad3a 20920 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 20921 per_objfile, section, &cu->header);
debd256d 20922}
c906108c 20923
c6da4cef 20924/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20925 Return the file name of the psymtab for the given file_entry.
c6da4cef 20926 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20927 If space for the result is malloc'd, *NAME_HOLDER will be set.
20928 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20929
d521ce57 20930static const char *
7ba99d21 20931psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 20932 const dwarf2_psymtab *pst,
c89b44cd
TT
20933 const char *comp_dir,
20934 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20935{
d521ce57
TT
20936 const char *include_name = fe.name;
20937 const char *include_name_to_compare = include_name;
72b9f47f 20938 const char *pst_filename;
c6da4cef
DE
20939 int file_is_pst;
20940
8c43009f 20941 const char *dir_name = fe.include_dir (lh);
c6da4cef 20942
c89b44cd 20943 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20944 if (!IS_ABSOLUTE_PATH (include_name)
20945 && (dir_name != NULL || comp_dir != NULL))
20946 {
20947 /* Avoid creating a duplicate psymtab for PST.
20948 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20949 Before we do the comparison, however, we need to account
20950 for DIR_NAME and COMP_DIR.
20951 First prepend dir_name (if non-NULL). If we still don't
20952 have an absolute path prepend comp_dir (if non-NULL).
20953 However, the directory we record in the include-file's
20954 psymtab does not contain COMP_DIR (to match the
20955 corresponding symtab(s)).
20956
20957 Example:
20958
20959 bash$ cd /tmp
20960 bash$ gcc -g ./hello.c
20961 include_name = "hello.c"
20962 dir_name = "."
20963 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20964 DW_AT_name = "./hello.c"
20965
20966 */
c6da4cef
DE
20967
20968 if (dir_name != NULL)
20969 {
c89b44cd
TT
20970 name_holder->reset (concat (dir_name, SLASH_STRING,
20971 include_name, (char *) NULL));
20972 include_name = name_holder->get ();
c6da4cef 20973 include_name_to_compare = include_name;
c6da4cef
DE
20974 }
20975 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20976 {
c89b44cd
TT
20977 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20978 include_name, (char *) NULL));
20979 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20980 }
20981 }
20982
20983 pst_filename = pst->filename;
c89b44cd 20984 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20985 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20986 {
c89b44cd
TT
20987 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20988 pst_filename, (char *) NULL));
20989 pst_filename = copied_name.get ();
c6da4cef
DE
20990 }
20991
1e3fad37 20992 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20993
c6da4cef
DE
20994 if (file_is_pst)
20995 return NULL;
20996 return include_name;
20997}
20998
d9b3de22
DE
20999/* State machine to track the state of the line number program. */
21000
6f77053d 21001class lnp_state_machine
d9b3de22 21002{
6f77053d
PA
21003public:
21004 /* Initialize a machine state for the start of a line number
21005 program. */
804d2729
TT
21006 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21007 bool record_lines_p);
6f77053d 21008
8c43009f
PA
21009 file_entry *current_file ()
21010 {
21011 /* lh->file_names is 0-based, but the file name numbers in the
21012 statement program are 1-based. */
6f77053d
PA
21013 return m_line_header->file_name_at (m_file);
21014 }
21015
21016 /* Record the line in the state machine. END_SEQUENCE is true if
21017 we're processing the end of a sequence. */
21018 void record_line (bool end_sequence);
21019
a8caed5d 21020 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
7ab6656f 21021 nop-out rest of the lines in this sequence. */
6f77053d
PA
21022 void check_line_address (struct dwarf2_cu *cu,
21023 const gdb_byte *line_ptr,
7ab6656f 21024 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
21025
21026 void handle_set_discriminator (unsigned int discriminator)
21027 {
21028 m_discriminator = discriminator;
21029 m_line_has_non_zero_discriminator |= discriminator != 0;
21030 }
21031
21032 /* Handle DW_LNE_set_address. */
21033 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21034 {
21035 m_op_index = 0;
21036 address += baseaddr;
21037 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21038 }
21039
21040 /* Handle DW_LNS_advance_pc. */
21041 void handle_advance_pc (CORE_ADDR adjust);
21042
21043 /* Handle a special opcode. */
21044 void handle_special_opcode (unsigned char op_code);
21045
21046 /* Handle DW_LNS_advance_line. */
21047 void handle_advance_line (int line_delta)
21048 {
21049 advance_line (line_delta);
21050 }
21051
21052 /* Handle DW_LNS_set_file. */
21053 void handle_set_file (file_name_index file);
21054
21055 /* Handle DW_LNS_negate_stmt. */
21056 void handle_negate_stmt ()
21057 {
21058 m_is_stmt = !m_is_stmt;
21059 }
21060
21061 /* Handle DW_LNS_const_add_pc. */
21062 void handle_const_add_pc ();
21063
21064 /* Handle DW_LNS_fixed_advance_pc. */
21065 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21066 {
21067 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21068 m_op_index = 0;
21069 }
21070
21071 /* Handle DW_LNS_copy. */
21072 void handle_copy ()
21073 {
21074 record_line (false);
21075 m_discriminator = 0;
21076 }
21077
21078 /* Handle DW_LNE_end_sequence. */
21079 void handle_end_sequence ()
21080 {
804d2729 21081 m_currently_recording_lines = true;
6f77053d
PA
21082 }
21083
21084private:
21085 /* Advance the line by LINE_DELTA. */
21086 void advance_line (int line_delta)
21087 {
21088 m_line += line_delta;
21089
21090 if (line_delta != 0)
21091 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21092 }
21093
804d2729
TT
21094 struct dwarf2_cu *m_cu;
21095
6f77053d
PA
21096 gdbarch *m_gdbarch;
21097
21098 /* True if we're recording lines.
21099 Otherwise we're building partial symtabs and are just interested in
21100 finding include files mentioned by the line number program. */
21101 bool m_record_lines_p;
21102
8c43009f 21103 /* The line number header. */
6f77053d 21104 line_header *m_line_header;
8c43009f 21105
6f77053d
PA
21106 /* These are part of the standard DWARF line number state machine,
21107 and initialized according to the DWARF spec. */
d9b3de22 21108
6f77053d 21109 unsigned char m_op_index = 0;
7ba99d21
AT
21110 /* The line table index of the current file. */
21111 file_name_index m_file = 1;
6f77053d
PA
21112 unsigned int m_line = 1;
21113
21114 /* These are initialized in the constructor. */
21115
21116 CORE_ADDR m_address;
21117 bool m_is_stmt;
21118 unsigned int m_discriminator;
d9b3de22
DE
21119
21120 /* Additional bits of state we need to track. */
21121
21122 /* The last file that we called dwarf2_start_subfile for.
21123 This is only used for TLLs. */
6f77053d 21124 unsigned int m_last_file = 0;
d9b3de22 21125 /* The last file a line number was recorded for. */
6f77053d 21126 struct subfile *m_last_subfile = NULL;
d9b3de22 21127
1313c56e
AB
21128 /* The address of the last line entry. */
21129 CORE_ADDR m_last_address;
21130
21131 /* Set to true when a previous line at the same address (using
21132 m_last_address) had m_is_stmt true. This is reset to false when a
21133 line entry at a new address (m_address different to m_last_address) is
21134 processed. */
21135 bool m_stmt_at_address = false;
21136
804d2729
TT
21137 /* When true, record the lines we decode. */
21138 bool m_currently_recording_lines = false;
d9b3de22
DE
21139
21140 /* The last line number that was recorded, used to coalesce
21141 consecutive entries for the same line. This can happen, for
21142 example, when discriminators are present. PR 17276. */
6f77053d
PA
21143 unsigned int m_last_line = 0;
21144 bool m_line_has_non_zero_discriminator = false;
8c43009f 21145};
d9b3de22 21146
6f77053d
PA
21147void
21148lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21149{
21150 CORE_ADDR addr_adj = (((m_op_index + adjust)
21151 / m_line_header->maximum_ops_per_instruction)
21152 * m_line_header->minimum_instruction_length);
21153 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21154 m_op_index = ((m_op_index + adjust)
21155 % m_line_header->maximum_ops_per_instruction);
21156}
d9b3de22 21157
6f77053d
PA
21158void
21159lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21160{
6f77053d 21161 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
21162 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21163 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21164 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
21165 / m_line_header->maximum_ops_per_instruction)
21166 * m_line_header->minimum_instruction_length);
21167 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 21168 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 21169 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21170
258bf0ee 21171 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
21172 advance_line (line_delta);
21173 record_line (false);
21174 m_discriminator = 0;
21175}
d9b3de22 21176
6f77053d
PA
21177void
21178lnp_state_machine::handle_set_file (file_name_index file)
21179{
21180 m_file = file;
21181
21182 const file_entry *fe = current_file ();
21183 if (fe == NULL)
21184 dwarf2_debug_line_missing_file_complaint ();
21185 else if (m_record_lines_p)
21186 {
21187 const char *dir = fe->include_dir (m_line_header);
21188
c24bdb02 21189 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21190 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21191 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21192 }
21193}
21194
21195void
21196lnp_state_machine::handle_const_add_pc ()
21197{
21198 CORE_ADDR adjust
21199 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21200
21201 CORE_ADDR addr_adj
21202 = (((m_op_index + adjust)
21203 / m_line_header->maximum_ops_per_instruction)
21204 * m_line_header->minimum_instruction_length);
21205
21206 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21207 m_op_index = ((m_op_index + adjust)
21208 % m_line_header->maximum_ops_per_instruction);
21209}
d9b3de22 21210
a05a36a5
DE
21211/* Return non-zero if we should add LINE to the line number table.
21212 LINE is the line to add, LAST_LINE is the last line that was added,
21213 LAST_SUBFILE is the subfile for LAST_LINE.
21214 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21215 had a non-zero discriminator.
21216
21217 We have to be careful in the presence of discriminators.
21218 E.g., for this line:
21219
21220 for (i = 0; i < 100000; i++);
21221
21222 clang can emit four line number entries for that one line,
21223 each with a different discriminator.
21224 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21225
21226 However, we want gdb to coalesce all four entries into one.
21227 Otherwise the user could stepi into the middle of the line and
21228 gdb would get confused about whether the pc really was in the
21229 middle of the line.
21230
21231 Things are further complicated by the fact that two consecutive
21232 line number entries for the same line is a heuristic used by gcc
21233 to denote the end of the prologue. So we can't just discard duplicate
21234 entries, we have to be selective about it. The heuristic we use is
21235 that we only collapse consecutive entries for the same line if at least
21236 one of those entries has a non-zero discriminator. PR 17276.
21237
21238 Note: Addresses in the line number state machine can never go backwards
21239 within one sequence, thus this coalescing is ok. */
21240
21241static int
804d2729
TT
21242dwarf_record_line_p (struct dwarf2_cu *cu,
21243 unsigned int line, unsigned int last_line,
a05a36a5
DE
21244 int line_has_non_zero_discriminator,
21245 struct subfile *last_subfile)
21246{
c24bdb02 21247 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21248 return 1;
21249 if (line != last_line)
21250 return 1;
21251 /* Same line for the same file that we've seen already.
21252 As a last check, for pr 17276, only record the line if the line
21253 has never had a non-zero discriminator. */
21254 if (!line_has_non_zero_discriminator)
21255 return 1;
21256 return 0;
21257}
21258
804d2729
TT
21259/* Use the CU's builder to record line number LINE beginning at
21260 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21261
21262static void
d9b3de22 21263dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 21264 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 21265 struct dwarf2_cu *cu)
252a6764
DE
21266{
21267 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21268
27e0867f
DE
21269 if (dwarf_line_debug)
21270 {
21271 fprintf_unfiltered (gdb_stdlog,
21272 "Recording line %u, file %s, address %s\n",
21273 line, lbasename (subfile->name),
21274 paddress (gdbarch, address));
21275 }
21276
804d2729 21277 if (cu != nullptr)
8c95582d 21278 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
21279}
21280
21281/* Subroutine of dwarf_decode_lines_1 to simplify it.
21282 Mark the end of a set of line number records.
d9b3de22 21283 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21284 If SUBFILE is NULL the request is ignored. */
21285
21286static void
21287dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21288 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21289{
27e0867f
DE
21290 if (subfile == NULL)
21291 return;
21292
21293 if (dwarf_line_debug)
21294 {
21295 fprintf_unfiltered (gdb_stdlog,
21296 "Finishing current line, file %s, address %s\n",
21297 lbasename (subfile->name),
21298 paddress (gdbarch, address));
21299 }
21300
8c95582d 21301 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
21302}
21303
6f77053d
PA
21304void
21305lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21306{
d9b3de22
DE
21307 if (dwarf_line_debug)
21308 {
21309 fprintf_unfiltered (gdb_stdlog,
21310 "Processing actual line %u: file %u,"
94a72be7 21311 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 21312 m_line, m_file,
6f77053d 21313 paddress (m_gdbarch, m_address),
94a72be7
AB
21314 m_is_stmt, m_discriminator,
21315 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
21316 }
21317
6f77053d 21318 file_entry *fe = current_file ();
8c43009f
PA
21319
21320 if (fe == NULL)
d9b3de22
DE
21321 dwarf2_debug_line_missing_file_complaint ();
21322 /* For now we ignore lines not starting on an instruction boundary.
21323 But not when processing end_sequence for compatibility with the
21324 previous version of the code. */
6f77053d 21325 else if (m_op_index == 0 || end_sequence)
d9b3de22 21326 {
8c43009f 21327 fe->included_p = 1;
8c95582d 21328 if (m_record_lines_p)
d9b3de22 21329 {
1313c56e
AB
21330 /* When we switch files we insert an end maker in the first file,
21331 switch to the second file and add a new line entry. The
21332 problem is that the end marker inserted in the first file will
21333 discard any previous line entries at the same address. If the
21334 line entries in the first file are marked as is-stmt, while
21335 the new line in the second file is non-stmt, then this means
21336 the end marker will discard is-stmt lines so we can have a
21337 non-stmt line. This means that there are less addresses at
21338 which the user can insert a breakpoint.
21339
21340 To improve this we track the last address in m_last_address,
21341 and whether we have seen an is-stmt at this address. Then
21342 when switching files, if we have seen a stmt at the current
21343 address, and we are switching to create a non-stmt line, then
21344 discard the new line. */
21345 bool file_changed
21346 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21347 bool ignore_this_line
dda83cd7
SM
21348 = ((file_changed && !end_sequence && m_last_address == m_address
21349 && !m_is_stmt && m_stmt_at_address)
21350 || (!end_sequence && m_line == 0));
1313c56e
AB
21351
21352 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 21353 {
804d2729
TT
21354 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21355 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21356 }
21357
1313c56e 21358 if (!end_sequence && !ignore_this_line)
d9b3de22 21359 {
8c95582d
AB
21360 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21361
804d2729 21362 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21363 m_line_has_non_zero_discriminator,
21364 m_last_subfile))
d9b3de22 21365 {
c24bdb02 21366 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21367 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21368 builder->get_current_subfile (),
8c95582d 21369 m_line, m_address, is_stmt,
804d2729 21370 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21371 }
c24bdb02 21372 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21373 m_last_line = m_line;
d9b3de22
DE
21374 }
21375 }
21376 }
1313c56e
AB
21377
21378 /* Track whether we have seen any m_is_stmt true at m_address in case we
21379 have multiple line table entries all at m_address. */
21380 if (m_last_address != m_address)
21381 {
21382 m_stmt_at_address = false;
21383 m_last_address = m_address;
21384 }
21385 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
21386}
21387
804d2729
TT
21388lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21389 line_header *lh, bool record_lines_p)
d9b3de22 21390{
804d2729 21391 m_cu = cu;
6f77053d
PA
21392 m_gdbarch = arch;
21393 m_record_lines_p = record_lines_p;
21394 m_line_header = lh;
d9b3de22 21395
804d2729 21396 m_currently_recording_lines = true;
d9b3de22 21397
d9b3de22
DE
21398 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21399 was a line entry for it so that the backend has a chance to adjust it
21400 and also record it in case it needs it. This is currently used by MIPS
21401 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21402 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21403 m_is_stmt = lh->default_is_stmt;
21404 m_discriminator = 0;
1313c56e
AB
21405
21406 m_last_address = m_address;
21407 m_stmt_at_address = false;
252a6764
DE
21408}
21409
6f77053d
PA
21410void
21411lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21412 const gdb_byte *line_ptr,
7ab6656f 21413 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21414{
a8caed5d
FS
21415 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21416 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21417 located at 0x0. In this case, additionally check that if
21418 ADDRESS < UNRELOCATED_LOWPC. */
924c2928 21419
a8caed5d
FS
21420 if ((address == 0 && address < unrelocated_lowpc)
21421 || address == (CORE_ADDR) -1)
924c2928
DE
21422 {
21423 /* This line table is for a function which has been
21424 GCd by the linker. Ignore it. PR gdb/12528 */
21425
5e22e966 21426 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
21427 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21428
b98664d3 21429 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21430 line_offset, objfile_name (objfile));
804d2729
TT
21431 m_currently_recording_lines = false;
21432 /* Note: m_currently_recording_lines is left as false until we see
21433 DW_LNE_end_sequence. */
924c2928
DE
21434 }
21435}
21436
f3f5162e 21437/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21438 Process the line number information in LH.
21439 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21440 program in order to set included_p for every referenced header. */
debd256d 21441
c906108c 21442static void
43f3e411
DE
21443dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21444 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21445{
d521ce57
TT
21446 const gdb_byte *line_ptr, *extended_end;
21447 const gdb_byte *line_end;
a8c50c1f 21448 unsigned int bytes_read, extended_len;
699ca60a 21449 unsigned char op_code, extended_op;
e142c38c 21450 CORE_ADDR baseaddr;
5e22e966 21451 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21452 bfd *abfd = objfile->obfd;
08feed99 21453 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
21454 /* True if we're recording line info (as opposed to building partial
21455 symtabs and just interested in finding include files mentioned by
21456 the line number program). */
21457 bool record_lines_p = !decode_for_pst_p;
e142c38c 21458
b3b3bada 21459 baseaddr = objfile->text_section_offset ();
c906108c 21460
debd256d
JB
21461 line_ptr = lh->statement_program_start;
21462 line_end = lh->statement_program_end;
c906108c
SS
21463
21464 /* Read the statement sequences until there's nothing left. */
21465 while (line_ptr < line_end)
21466 {
6f77053d
PA
21467 /* The DWARF line number program state machine. Reset the state
21468 machine at the start of each sequence. */
804d2729 21469 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21470 bool end_sequence = false;
d9b3de22 21471
8c43009f 21472 if (record_lines_p)
c906108c 21473 {
8c43009f
PA
21474 /* Start a subfile for the current file of the state
21475 machine. */
21476 const file_entry *fe = state_machine.current_file ();
21477
21478 if (fe != NULL)
804d2729 21479 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21480 }
21481
a738430d 21482 /* Decode the table. */
d9b3de22 21483 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21484 {
21485 op_code = read_1_byte (abfd, line_ptr);
21486 line_ptr += 1;
9aa1fe7e 21487
debd256d 21488 if (op_code >= lh->opcode_base)
6e70227d 21489 {
8e07a239 21490 /* Special opcode. */
6f77053d 21491 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21492 }
21493 else switch (op_code)
c906108c
SS
21494 {
21495 case DW_LNS_extended_op:
3e43a32a
MS
21496 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21497 &bytes_read);
473b7be6 21498 line_ptr += bytes_read;
a8c50c1f 21499 extended_end = line_ptr + extended_len;
c906108c
SS
21500 extended_op = read_1_byte (abfd, line_ptr);
21501 line_ptr += 1;
8f34b746
TV
21502 if (DW_LNE_lo_user <= extended_op
21503 && extended_op <= DW_LNE_hi_user)
21504 {
21505 /* Vendor extension, ignore. */
21506 line_ptr = extended_end;
21507 break;
21508 }
c906108c
SS
21509 switch (extended_op)
21510 {
21511 case DW_LNE_end_sequence:
6f77053d
PA
21512 state_machine.handle_end_sequence ();
21513 end_sequence = true;
c906108c
SS
21514 break;
21515 case DW_LNE_set_address:
d9b3de22
DE
21516 {
21517 CORE_ADDR address
c8a7a66f 21518 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 21519 line_ptr += bytes_read;
6f77053d
PA
21520
21521 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21522 lowpc - baseaddr, address);
6f77053d 21523 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21524 }
c906108c
SS
21525 break;
21526 case DW_LNE_define_file:
dda83cd7
SM
21527 {
21528 const char *cur_file;
ecfb656c
PA
21529 unsigned int mod_time, length;
21530 dir_index dindex;
6e70227d 21531
dda83cd7 21532 cur_file = read_direct_string (abfd, line_ptr,
3e43a32a 21533 &bytes_read);
dda83cd7
SM
21534 line_ptr += bytes_read;
21535 dindex = (dir_index)
21536 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21537 line_ptr += bytes_read;
21538 mod_time =
21539 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21540 line_ptr += bytes_read;
21541 length =
21542 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21543 line_ptr += bytes_read;
21544 lh->add_file_name (cur_file, dindex, mod_time, length);
21545 }
c906108c 21546 break;
d0c6ba3d 21547 case DW_LNE_set_discriminator:
6f77053d
PA
21548 {
21549 /* The discriminator is not interesting to the
21550 debugger; just ignore it. We still need to
21551 check its value though:
21552 if there are consecutive entries for the same
21553 (non-prologue) line we want to coalesce them.
21554 PR 17276. */
21555 unsigned int discr
21556 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21557 line_ptr += bytes_read;
21558
21559 state_machine.handle_set_discriminator (discr);
21560 }
d0c6ba3d 21561 break;
c906108c 21562 default:
b98664d3 21563 complaint (_("mangled .debug_line section"));
debd256d 21564 return;
c906108c 21565 }
a8c50c1f
DJ
21566 /* Make sure that we parsed the extended op correctly. If e.g.
21567 we expected a different address size than the producer used,
21568 we may have read the wrong number of bytes. */
21569 if (line_ptr != extended_end)
21570 {
b98664d3 21571 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21572 return;
21573 }
c906108c
SS
21574 break;
21575 case DW_LNS_copy:
6f77053d 21576 state_machine.handle_copy ();
c906108c
SS
21577 break;
21578 case DW_LNS_advance_pc:
2dc7f7b3
TT
21579 {
21580 CORE_ADDR adjust
21581 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21582 line_ptr += bytes_read;
6f77053d
PA
21583
21584 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21585 }
c906108c
SS
21586 break;
21587 case DW_LNS_advance_line:
a05a36a5
DE
21588 {
21589 int line_delta
21590 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21591 line_ptr += bytes_read;
6f77053d
PA
21592
21593 state_machine.handle_advance_line (line_delta);
a05a36a5 21594 }
c906108c
SS
21595 break;
21596 case DW_LNS_set_file:
d9b3de22 21597 {
6f77053d 21598 file_name_index file
ecfb656c
PA
21599 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21600 &bytes_read);
d9b3de22 21601 line_ptr += bytes_read;
8c43009f 21602
6f77053d 21603 state_machine.handle_set_file (file);
d9b3de22 21604 }
c906108c
SS
21605 break;
21606 case DW_LNS_set_column:
0ad93d4f 21607 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21608 line_ptr += bytes_read;
21609 break;
21610 case DW_LNS_negate_stmt:
6f77053d 21611 state_machine.handle_negate_stmt ();
c906108c
SS
21612 break;
21613 case DW_LNS_set_basic_block:
c906108c 21614 break;
c2c6d25f
JM
21615 /* Add to the address register of the state machine the
21616 address increment value corresponding to special opcode
a738430d
MK
21617 255. I.e., this value is scaled by the minimum
21618 instruction length since special opcode 255 would have
b021a221 21619 scaled the increment. */
c906108c 21620 case DW_LNS_const_add_pc:
6f77053d 21621 state_machine.handle_const_add_pc ();
c906108c
SS
21622 break;
21623 case DW_LNS_fixed_advance_pc:
3e29f34a 21624 {
6f77053d 21625 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21626 line_ptr += 2;
6f77053d
PA
21627
21628 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21629 }
c906108c 21630 break;
9aa1fe7e 21631 default:
a738430d
MK
21632 {
21633 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21634 int i;
a738430d 21635
debd256d 21636 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21637 {
21638 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21639 line_ptr += bytes_read;
21640 }
21641 }
c906108c
SS
21642 }
21643 }
d9b3de22
DE
21644
21645 if (!end_sequence)
21646 dwarf2_debug_line_missing_end_sequence_complaint ();
21647
21648 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21649 in which case we still finish recording the last line). */
6f77053d 21650 state_machine.record_line (true);
c906108c 21651 }
f3f5162e
DE
21652}
21653
21654/* Decode the Line Number Program (LNP) for the given line_header
21655 structure and CU. The actual information extracted and the type
21656 of structures created from the LNP depends on the value of PST.
21657
21658 1. If PST is NULL, then this procedure uses the data from the program
21659 to create all necessary symbol tables, and their linetables.
21660
21661 2. If PST is not NULL, this procedure reads the program to determine
21662 the list of files included by the unit represented by PST, and
21663 builds all the associated partial symbol tables.
21664
21665 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21666 It is used for relative paths in the line table.
21667 NOTE: When processing partial symtabs (pst != NULL),
21668 comp_dir == pst->dirname.
21669
21670 NOTE: It is important that psymtabs have the same file name (via strcmp)
21671 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21672 symtab we don't use it in the name of the psymtabs we create.
21673 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21674 A good testcase for this is mb-inline.exp.
21675
527f3840
JK
21676 LOWPC is the lowest address in CU (or 0 if not known).
21677
21678 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21679 for its PC<->lines mapping information. Otherwise only the filename
21680 table is read in. */
f3f5162e
DE
21681
21682static void
21683dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21684 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21685 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21686{
5e22e966 21687 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21688 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21689
527f3840
JK
21690 if (decode_mapping)
21691 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21692
21693 if (decode_for_pst_p)
21694 {
aaa75496 21695 /* Now that we're done scanning the Line Header Program, we can
dda83cd7 21696 create the psymtab of each included file. */
7ba99d21 21697 for (auto &file_entry : lh->file_names ())
dda83cd7
SM
21698 if (file_entry.included_p == 1)
21699 {
c89b44cd 21700 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21701 const char *include_name =
7ba99d21
AT
21702 psymtab_include_file_name (lh, file_entry, pst,
21703 comp_dir, &name_holder);
c6da4cef 21704 if (include_name != NULL)
dda83cd7
SM
21705 dwarf2_create_include_psymtab (include_name, pst, objfile);
21706 }
aaa75496 21707 }
cb1df416
DJ
21708 else
21709 {
21710 /* Make sure a symtab is created for every file, even files
21711 which contain only variables (i.e. no code with associated
21712 line numbers). */
c24bdb02
KS
21713 buildsym_compunit *builder = cu->get_builder ();
21714 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21715
7ba99d21 21716 for (auto &fe : lh->file_names ())
cb1df416 21717 {
804d2729 21718 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21719 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21720 {
c24bdb02 21721 builder->get_current_subfile ()->symtab
804d2729 21722 = allocate_symtab (cust,
c24bdb02 21723 builder->get_current_subfile ()->name);
43f3e411 21724 }
c24bdb02 21725 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21726 }
21727 }
c906108c
SS
21728}
21729
21730/* Start a subfile for DWARF. FILENAME is the name of the file and
21731 DIRNAME the name of the source directory which contains FILENAME
4d663531 21732 or NULL if not known.
c906108c
SS
21733 This routine tries to keep line numbers from identical absolute and
21734 relative file names in a common subfile.
21735
21736 Using the `list' example from the GDB testsuite, which resides in
21737 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21738 of /srcdir/list0.c yields the following debugging information for list0.c:
21739
c5aa993b 21740 DW_AT_name: /srcdir/list0.c
4d663531 21741 DW_AT_comp_dir: /compdir
357e46e7 21742 files.files[0].name: list0.h
c5aa993b 21743 files.files[0].dir: /srcdir
357e46e7 21744 files.files[1].name: list0.c
c5aa993b 21745 files.files[1].dir: /srcdir
c906108c
SS
21746
21747 The line number information for list0.c has to end up in a single
4f1520fb
FR
21748 subfile, so that `break /srcdir/list0.c:1' works as expected.
21749 start_subfile will ensure that this happens provided that we pass the
21750 concatenation of files.files[1].dir and files.files[1].name as the
21751 subfile's name. */
c906108c
SS
21752
21753static void
804d2729
TT
21754dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21755 const char *dirname)
c906108c 21756{
43816ebc 21757 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21758
4d663531 21759 /* In order not to lose the line information directory,
4f1520fb
FR
21760 we concatenate it to the filename when it makes sense.
21761 Note that the Dwarf3 standard says (speaking of filenames in line
21762 information): ``The directory index is ignored for file names
21763 that represent full path names''. Thus ignoring dirname in the
21764 `else' branch below isn't an issue. */
c906108c 21765
d5166ae1 21766 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21767 {
43816ebc
TT
21768 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21769 filename = copy.get ();
d521ce57 21770 }
c906108c 21771
c24bdb02 21772 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21773}
21774
804d2729
TT
21775/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21776 buildsym_compunit constructor. */
f4dc4d17 21777
c24bdb02
KS
21778struct compunit_symtab *
21779dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21780 CORE_ADDR low_pc)
f4dc4d17 21781{
c24bdb02 21782 gdb_assert (m_builder == nullptr);
43f3e411 21783
c24bdb02 21784 m_builder.reset (new struct buildsym_compunit
f6e649dd 21785 (this->per_objfile->objfile,
c24bdb02 21786 name, comp_dir, language, low_pc));
93b8bea4 21787
c24bdb02 21788 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21789
c24bdb02
KS
21790 get_builder ()->record_debugformat ("DWARF 2");
21791 get_builder ()->record_producer (producer);
f4dc4d17 21792
c24bdb02 21793 processing_has_namespace_info = false;
43f3e411 21794
c24bdb02 21795 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21796}
21797
4c2df51b
DJ
21798static void
21799var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21800 struct dwarf2_cu *cu)
4c2df51b 21801{
5e22e966 21802 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
21803 struct comp_unit_head *cu_header = &cu->header;
21804
4c2df51b
DJ
21805 /* NOTE drow/2003-01-30: There used to be a comment and some special
21806 code here to turn a symbol with DW_AT_external and a
21807 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21808 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21809 with some versions of binutils) where shared libraries could have
21810 relocations against symbols in their debug information - the
21811 minimal symbol would have the right address, but the debug info
21812 would not. It's no longer necessary, because we will explicitly
21813 apply relocations when we read in the debug information now. */
21814
21815 /* A DW_AT_location attribute with no contents indicates that a
21816 variable has been optimized away. */
9d2246fc 21817 if (attr->form_is_block () && attr->as_block ()->size == 0)
4c2df51b 21818 {
f1e6e072 21819 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21820 return;
21821 }
21822
21823 /* Handle one degenerate form of location expression specially, to
21824 preserve GDB's previous behavior when section offsets are
336d760d
AT
21825 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21826 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 21827
9d2246fc
TT
21828 if (attr->form_is_block ())
21829 {
21830 struct dwarf_block *block = attr->as_block ();
21831
21832 if ((block->data[0] == DW_OP_addr
21833 && block->size == 1 + cu_header->addr_size)
21834 || ((block->data[0] == DW_OP_GNU_addr_index
dda83cd7 21835 || block->data[0] == DW_OP_addrx)
9d2246fc
TT
21836 && (block->size
21837 == 1 + leb128_size (&block->data[1]))))
21838 {
21839 unsigned int dummy;
21840
21841 if (block->data[0] == DW_OP_addr)
21842 SET_SYMBOL_VALUE_ADDRESS
21843 (sym, cu->header.read_address (objfile->obfd,
21844 block->data + 1,
38583298 21845 &dummy));
9d2246fc
TT
21846 else
21847 SET_SYMBOL_VALUE_ADDRESS
21848 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21849 &dummy));
21850 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21851 fixup_symbol_section (sym, objfile);
21852 SET_SYMBOL_VALUE_ADDRESS
21853 (sym,
21854 SYMBOL_VALUE_ADDRESS (sym)
21855 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21856 return;
21857 }
4c2df51b
DJ
21858 }
21859
21860 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21861 expression evaluator, and use LOC_COMPUTED only when necessary
21862 (i.e. when the value of a register or memory location is
21863 referenced, or a thread-local block, etc.). Then again, it might
21864 not be worthwhile. I'm assuming that it isn't unless performance
21865 or memory numbers show me otherwise. */
21866
f1e6e072 21867 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21868
f1e6e072 21869 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21870 cu->has_loclist = true;
4c2df51b
DJ
21871}
21872
c906108c
SS
21873/* Given a pointer to a DWARF information entry, figure out if we need
21874 to make a symbol table entry for it, and if so, create a new entry
21875 and return a pointer to it.
21876 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21877 used the passed type.
21878 If SPACE is not NULL, use it to hold the new symbol. If it is
21879 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21880
21881static struct symbol *
5e2db402
TT
21882new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21883 struct symbol *space)
c906108c 21884{
976ca316
SM
21885 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21886 struct objfile *objfile = per_objfile->objfile;
08feed99 21887 struct gdbarch *gdbarch = objfile->arch ();
c906108c 21888 struct symbol *sym = NULL;
15d034d0 21889 const char *name;
c906108c
SS
21890 struct attribute *attr = NULL;
21891 struct attribute *attr2 = NULL;
e142c38c 21892 CORE_ADDR baseaddr;
e37fd15a
SW
21893 struct pending **list_to_add = NULL;
21894
edb3359d 21895 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 21896
b3b3bada 21897 baseaddr = objfile->text_section_offset ();
c906108c 21898
94af9270 21899 name = dwarf2_name (die, cu);
c906108c
SS
21900 if (name)
21901 {
34eaf542 21902 int suppress_add = 0;
94af9270 21903
34eaf542
TT
21904 if (space)
21905 sym = space;
21906 else
8c14c3a3 21907 sym = new (&objfile->objfile_obstack) symbol;
c906108c 21908 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21909
21910 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 21911 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
21912 /* Fortran does not have mangling standard and the mangling does differ
21913 between gfortran, iFort etc. */
bcfe6157
TT
21914 const char *physname
21915 = (cu->language == language_fortran
21916 ? dwarf2_full_name (name, die, cu)
21917 : dwarf2_physname (name, die, cu));
21918 const char *linkagename = dw2_linkage_name (die, cu);
21919
21920 if (linkagename == nullptr || cu->language == language_ada)
21921 sym->set_linkage_name (physname);
21922 else
21923 {
21924 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21925 sym->set_linkage_name (linkagename);
21926 }
f55ee35c 21927
c906108c 21928 /* Default assumptions.
dda83cd7 21929 Use the passed type or decode it from the die. */
176620f1 21930 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21931 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21932 if (type != NULL)
21933 SYMBOL_TYPE (sym) = type;
21934 else
e7c27a73 21935 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21936 attr = dwarf2_attr (die,
21937 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21938 cu);
435d3d88 21939 if (attr != nullptr)
529908cb 21940 SYMBOL_LINE (sym) = attr->constant_value (0);
cb1df416 21941
edb3359d
DJ
21942 attr = dwarf2_attr (die,
21943 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21944 cu);
529908cb 21945 if (attr != nullptr && attr->form_is_unsigned ())
cb1df416 21946 {
529908cb
TT
21947 file_name_index file_index
21948 = (file_name_index) attr->as_unsigned ();
8c43009f 21949 struct file_entry *fe;
9a619af0 21950
ecfb656c
PA
21951 if (cu->line_header != NULL)
21952 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21953 else
21954 fe = NULL;
21955
21956 if (fe == NULL)
b98664d3 21957 complaint (_("file index out of range"));
8c43009f
PA
21958 else
21959 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21960 }
21961
c906108c
SS
21962 switch (die->tag)
21963 {
21964 case DW_TAG_label:
e142c38c 21965 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21966 if (attr != nullptr)
3e29f34a
MR
21967 {
21968 CORE_ADDR addr;
21969
95f982e5 21970 addr = attr->as_address ();
3e29f34a 21971 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21972 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
8f5c6526 21973 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
3e29f34a 21974 }
8f5c6526
TV
21975 else
21976 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
0f5238ed
TT
21977 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21978 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
d3cb6808 21979 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21980 break;
21981 case DW_TAG_subprogram:
21982 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21983 finish_block. */
f1e6e072 21984 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21985 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 21986 if ((attr2 != nullptr && attr2->as_boolean ())
0a4b0913
AB
21987 || cu->language == language_ada
21988 || cu->language == language_fortran)
c906108c 21989 {
dda83cd7
SM
21990 /* Subprograms marked external are stored as a global symbol.
21991 Ada and Fortran subprograms, whether marked external or
21992 not, are always stored as a global symbol, because we want
21993 to be able to access them globally. For instance, we want
21994 to be able to break on a nested subprogram without having
21995 to specify the context. */
c24bdb02 21996 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21997 }
21998 else
21999 {
e37fd15a 22000 list_to_add = cu->list_in_scope;
c906108c
SS
22001 }
22002 break;
edb3359d
DJ
22003 case DW_TAG_inlined_subroutine:
22004 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22005 finish_block. */
f1e6e072 22006 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 22007 SYMBOL_INLINED (sym) = 1;
481860b3 22008 list_to_add = cu->list_in_scope;
edb3359d 22009 break;
34eaf542
TT
22010 case DW_TAG_template_value_param:
22011 suppress_add = 1;
22012 /* Fall through. */
72929c62 22013 case DW_TAG_constant:
c906108c 22014 case DW_TAG_variable:
254e6b9e 22015 case DW_TAG_member:
0963b4bd
MS
22016 /* Compilation with minimal debug info may result in
22017 variables with missing type entries. Change the
22018 misleading `void' type to something sensible. */
78134374 22019 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 22020 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 22021
e142c38c 22022 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
22023 /* In the case of DW_TAG_member, we should only be called for
22024 static const members. */
22025 if (die->tag == DW_TAG_member)
22026 {
3863f96c
DE
22027 /* dwarf2_add_field uses die_is_declaration,
22028 so we do the same. */
254e6b9e
DE
22029 gdb_assert (die_is_declaration (die, cu));
22030 gdb_assert (attr);
22031 }
435d3d88 22032 if (attr != nullptr)
c906108c 22033 {
e7c27a73 22034 dwarf2_const_value (attr, sym, cu);
e142c38c 22035 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 22036 if (!suppress_add)
34eaf542 22037 {
c45bc3f8 22038 if (attr2 != nullptr && attr2->as_boolean ())
c24bdb02 22039 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 22040 else
e37fd15a 22041 list_to_add = cu->list_in_scope;
34eaf542 22042 }
c906108c
SS
22043 break;
22044 }
e142c38c 22045 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22046 if (attr != nullptr)
c906108c 22047 {
e7c27a73 22048 var_decode_location (attr, sym, cu);
e142c38c 22049 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
22050
22051 /* Fortran explicitly imports any global symbols to the local
22052 scope by DW_TAG_common_block. */
22053 if (cu->language == language_fortran && die->parent
22054 && die->parent->tag == DW_TAG_common_block)
22055 attr2 = NULL;
22056
caac4577
JG
22057 if (SYMBOL_CLASS (sym) == LOC_STATIC
22058 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 22059 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
22060 {
22061 /* When a static variable is eliminated by the linker,
22062 the corresponding debug information is not stripped
22063 out, but the variable address is set to null;
22064 do not add such variables into symbol table. */
22065 }
c45bc3f8 22066 else if (attr2 != nullptr && attr2->as_boolean ())
1c809c68 22067 {
4b610737
TT
22068 if (SYMBOL_CLASS (sym) == LOC_STATIC
22069 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 22070 && per_objfile->per_bfd->can_copy)
4b610737
TT
22071 {
22072 /* A global static variable might be subject to
22073 copy relocation. We first check for a local
22074 minsym, though, because maybe the symbol was
22075 marked hidden, in which case this would not
22076 apply. */
22077 bound_minimal_symbol found
22078 = (lookup_minimal_symbol_linkage
987012b8 22079 (sym->linkage_name (), objfile));
4b610737
TT
22080 if (found.minsym != nullptr)
22081 sym->maybe_copied = 1;
22082 }
f55ee35c 22083
1c809c68
TT
22084 /* A variable with DW_AT_external is never static,
22085 but it may be block-scoped. */
804d2729 22086 list_to_add
c24bdb02
KS
22087 = ((cu->list_in_scope
22088 == cu->get_builder ()->get_file_symbols ())
22089 ? cu->get_builder ()->get_global_symbols ()
804d2729 22090 : cu->list_in_scope);
1c809c68 22091 }
c906108c 22092 else
e37fd15a 22093 list_to_add = cu->list_in_scope;
c906108c
SS
22094 }
22095 else
22096 {
22097 /* We do not know the address of this symbol.
dda83cd7
SM
22098 If it is an external symbol and we have type information
22099 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22100 The address of the variable will then be determined from
22101 the minimal symbol table whenever the variable is
22102 referenced. */
e142c38c 22103 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
22104
22105 /* Fortran explicitly imports any global symbols to the local
22106 scope by DW_TAG_common_block. */
22107 if (cu->language == language_fortran && die->parent
22108 && die->parent->tag == DW_TAG_common_block)
22109 {
22110 /* SYMBOL_CLASS doesn't matter here because
22111 read_common_block is going to reset it. */
22112 if (!suppress_add)
22113 list_to_add = cu->list_in_scope;
22114 }
c45bc3f8 22115 else if (attr2 != nullptr && attr2->as_boolean ()
0971de02 22116 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 22117 {
0fe7935b
DJ
22118 /* A variable with DW_AT_external is never static, but it
22119 may be block-scoped. */
804d2729 22120 list_to_add
c24bdb02
KS
22121 = ((cu->list_in_scope
22122 == cu->get_builder ()->get_file_symbols ())
22123 ? cu->get_builder ()->get_global_symbols ()
804d2729 22124 : cu->list_in_scope);
0fe7935b 22125
f1e6e072 22126 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 22127 }
442ddf59
JK
22128 else if (!die_is_declaration (die, cu))
22129 {
22130 /* Use the default LOC_OPTIMIZED_OUT class. */
22131 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22132 if (!suppress_add)
22133 list_to_add = cu->list_in_scope;
442ddf59 22134 }
c906108c
SS
22135 }
22136 break;
22137 case DW_TAG_formal_parameter:
a60f3166
TT
22138 {
22139 /* If we are inside a function, mark this as an argument. If
22140 not, we might be looking at an argument to an inlined function
22141 when we do not have enough information to show inlined frames;
22142 pretend it's a local variable in that case so that the user can
22143 still see it. */
804d2729 22144 struct context_stack *curr
c24bdb02 22145 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
22146 if (curr != nullptr && curr->name != nullptr)
22147 SYMBOL_IS_ARGUMENT (sym) = 1;
22148 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22149 if (attr != nullptr)
a60f3166
TT
22150 {
22151 var_decode_location (attr, sym, cu);
22152 }
22153 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22154 if (attr != nullptr)
a60f3166
TT
22155 {
22156 dwarf2_const_value (attr, sym, cu);
22157 }
f346a30d 22158
a60f3166
TT
22159 list_to_add = cu->list_in_scope;
22160 }
c906108c
SS
22161 break;
22162 case DW_TAG_unspecified_parameters:
22163 /* From varargs functions; gdb doesn't seem to have any
22164 interest in this information, so just ignore it for now.
22165 (FIXME?) */
22166 break;
34eaf542
TT
22167 case DW_TAG_template_type_param:
22168 suppress_add = 1;
22169 /* Fall through. */
c906108c 22170 case DW_TAG_class_type:
680b30c7 22171 case DW_TAG_interface_type:
c906108c
SS
22172 case DW_TAG_structure_type:
22173 case DW_TAG_union_type:
72019c9c 22174 case DW_TAG_set_type:
c906108c 22175 case DW_TAG_enumeration_type:
f1e6e072 22176 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22177 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22178
63d06c5c 22179 {
9c37b5ae 22180 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22181 really ever be static objects: otherwise, if you try
22182 to, say, break of a class's method and you're in a file
22183 which doesn't mention that class, it won't work unless
22184 the check for all static symbols in lookup_symbol_aux
22185 saves you. See the OtherFileClass tests in
22186 gdb.c++/namespace.exp. */
22187
e37fd15a 22188 if (!suppress_add)
34eaf542 22189 {
c24bdb02 22190 buildsym_compunit *builder = cu->get_builder ();
804d2729 22191 list_to_add
c24bdb02 22192 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22193 && cu->language == language_cplus
c24bdb02 22194 ? builder->get_global_symbols ()
804d2729 22195 : cu->list_in_scope);
63d06c5c 22196
64382290 22197 /* The semantics of C++ state that "struct foo {
9c37b5ae 22198 ... }" also defines a typedef for "foo". */
64382290 22199 if (cu->language == language_cplus
45280282 22200 || cu->language == language_ada
c44af4eb
TT
22201 || cu->language == language_d
22202 || cu->language == language_rust)
64382290
TT
22203 {
22204 /* The symbol's name is already allocated along
22205 with this objfile, so we don't need to
22206 duplicate it for the type. */
7d93a1e0 22207 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 22208 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 22209 }
63d06c5c
DC
22210 }
22211 }
c906108c
SS
22212 break;
22213 case DW_TAG_typedef:
f1e6e072 22214 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22215 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22216 list_to_add = cu->list_in_scope;
63d06c5c 22217 break;
d8f62e84 22218 case DW_TAG_array_type:
c906108c 22219 case DW_TAG_base_type:
dda83cd7 22220 case DW_TAG_subrange_type:
f1e6e072 22221 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22222 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22223 list_to_add = cu->list_in_scope;
c906108c
SS
22224 break;
22225 case DW_TAG_enumerator:
e142c38c 22226 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22227 if (attr != nullptr)
c906108c 22228 {
e7c27a73 22229 dwarf2_const_value (attr, sym, cu);
c906108c 22230 }
63d06c5c
DC
22231 {
22232 /* NOTE: carlton/2003-11-10: See comment above in the
22233 DW_TAG_class_type, etc. block. */
22234
804d2729 22235 list_to_add
c24bdb02 22236 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22237 && cu->language == language_cplus
c24bdb02 22238 ? cu->get_builder ()->get_global_symbols ()
804d2729 22239 : cu->list_in_scope);
63d06c5c 22240 }
c906108c 22241 break;
74921315 22242 case DW_TAG_imported_declaration:
5c4e30ca 22243 case DW_TAG_namespace:
f1e6e072 22244 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22245 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22246 break;
530e8392
KB
22247 case DW_TAG_module:
22248 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22249 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22250 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22251 break;
4357ac6c 22252 case DW_TAG_common_block:
f1e6e072 22253 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22254 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22255 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22256 break;
c906108c
SS
22257 default:
22258 /* Not a tag we recognize. Hopefully we aren't processing
22259 trash data, but since we must specifically ignore things
22260 we don't recognize, there is nothing else we should do at
0963b4bd 22261 this point. */
b98664d3 22262 complaint (_("unsupported tag: '%s'"),
4d3c2250 22263 dwarf_tag_name (die->tag));
c906108c
SS
22264 break;
22265 }
df8a16a1 22266
e37fd15a
SW
22267 if (suppress_add)
22268 {
22269 sym->hash_next = objfile->template_symbols;
22270 objfile->template_symbols = sym;
22271 list_to_add = NULL;
22272 }
22273
22274 if (list_to_add != NULL)
d3cb6808 22275 add_symbol_to_list (sym, list_to_add);
e37fd15a 22276
df8a16a1
DJ
22277 /* For the benefit of old versions of GCC, check for anonymous
22278 namespaces based on the demangled name. */
4d4ec4e5 22279 if (!cu->processing_has_namespace_info
94af9270 22280 && cu->language == language_cplus)
c24bdb02 22281 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22282 }
22283 return (sym);
22284}
22285
98bfdba5
PA
22286/* Given an attr with a DW_FORM_dataN value in host byte order,
22287 zero-extend it as appropriate for the symbol's type. The DWARF
22288 standard (v4) is not entirely clear about the meaning of using
22289 DW_FORM_dataN for a constant with a signed type, where the type is
22290 wider than the data. The conclusion of a discussion on the DWARF
22291 list was that this is unspecified. We choose to always zero-extend
22292 because that is the interpretation long in use by GCC. */
c906108c 22293
98bfdba5 22294static gdb_byte *
ff39bb5e 22295dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22296 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22297{
5e22e966 22298 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
22299 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22300 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
529908cb 22301 LONGEST l = attr->constant_value (0);
98bfdba5
PA
22302
22303 if (bits < sizeof (*value) * 8)
22304 {
22305 l &= ((LONGEST) 1 << bits) - 1;
22306 *value = l;
22307 }
22308 else if (bits == sizeof (*value) * 8)
22309 *value = l;
22310 else
22311 {
224c3ddb 22312 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22313 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22314 return bytes;
22315 }
22316
22317 return NULL;
22318}
22319
22320/* Read a constant value from an attribute. Either set *VALUE, or if
22321 the value does not fit in *VALUE, set *BYTES - either already
22322 allocated on the objfile obstack, or newly allocated on OBSTACK,
22323 or, set *BATON, if we translated the constant to a location
22324 expression. */
22325
22326static void
ff39bb5e 22327dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22328 const char *name, struct obstack *obstack,
22329 struct dwarf2_cu *cu,
d521ce57 22330 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22331 struct dwarf2_locexpr_baton **baton)
22332{
5e22e966 22333 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 22334 struct objfile *objfile = per_objfile->objfile;
98bfdba5 22335 struct comp_unit_head *cu_header = &cu->header;
c906108c 22336 struct dwarf_block *blk;
98bfdba5
PA
22337 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22338 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22339
22340 *value = 0;
22341 *bytes = NULL;
22342 *baton = NULL;
c906108c
SS
22343
22344 switch (attr->form)
22345 {
22346 case DW_FORM_addr:
336d760d 22347 case DW_FORM_addrx:
3019eac3 22348 case DW_FORM_GNU_addr_index:
ac56253d 22349 {
ac56253d
TT
22350 gdb_byte *data;
22351
98bfdba5
PA
22352 if (TYPE_LENGTH (type) != cu_header->addr_size)
22353 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22354 cu_header->addr_size,
98bfdba5 22355 TYPE_LENGTH (type));
ac56253d
TT
22356 /* Symbols of this form are reasonably rare, so we just
22357 piggyback on the existing location code rather than writing
22358 a new implementation of symbol_computed_ops. */
8d749320 22359 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 22360 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
22361 (*baton)->per_cu = cu->per_cu;
22362 gdb_assert ((*baton)->per_cu);
ac56253d 22363
98bfdba5 22364 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22365 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22366 (*baton)->data = data;
ac56253d
TT
22367
22368 data[0] = DW_OP_addr;
22369 store_unsigned_integer (&data[1], cu_header->addr_size,
36d378cf 22370 byte_order, attr->as_address ());
ac56253d 22371 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22372 }
c906108c 22373 break;
4ac36638 22374 case DW_FORM_string:
93b5768b 22375 case DW_FORM_strp:
cf532bd1 22376 case DW_FORM_strx:
3019eac3 22377 case DW_FORM_GNU_str_index:
36586728 22378 case DW_FORM_GNU_strp_alt:
c6481205 22379 /* The string is already allocated on the objfile obstack, point
98bfdba5 22380 directly to it. */
2c830f54 22381 *bytes = (const gdb_byte *) attr->as_string ();
93b5768b 22382 break;
c906108c
SS
22383 case DW_FORM_block1:
22384 case DW_FORM_block2:
22385 case DW_FORM_block4:
22386 case DW_FORM_block:
2dc7f7b3 22387 case DW_FORM_exprloc:
0224619f 22388 case DW_FORM_data16:
9d2246fc 22389 blk = attr->as_block ();
98bfdba5
PA
22390 if (TYPE_LENGTH (type) != blk->size)
22391 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22392 TYPE_LENGTH (type));
22393 *bytes = blk->data;
c906108c 22394 break;
2df3850c
JM
22395
22396 /* The DW_AT_const_value attributes are supposed to carry the
22397 symbol's value "represented as it would be on the target
22398 architecture." By the time we get here, it's already been
22399 converted to host endianness, so we just need to sign- or
22400 zero-extend it as appropriate. */
22401 case DW_FORM_data1:
3aef2284 22402 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22403 break;
c906108c 22404 case DW_FORM_data2:
3aef2284 22405 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22406 break;
c906108c 22407 case DW_FORM_data4:
3aef2284 22408 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22409 break;
c906108c 22410 case DW_FORM_data8:
3aef2284 22411 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22412 break;
22413
c906108c 22414 case DW_FORM_sdata:
663c44ac 22415 case DW_FORM_implicit_const:
1bc397c5 22416 *value = attr->as_signed ();
2df3850c
JM
22417 break;
22418
c906108c 22419 case DW_FORM_udata:
529908cb 22420 *value = attr->as_unsigned ();
c906108c 22421 break;
2df3850c 22422
c906108c 22423 default:
b98664d3 22424 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22425 dwarf_form_name (attr->form));
98bfdba5 22426 *value = 0;
c906108c
SS
22427 break;
22428 }
22429}
22430
2df3850c 22431
98bfdba5
PA
22432/* Copy constant value from an attribute to a symbol. */
22433
2df3850c 22434static void
ff39bb5e 22435dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22436 struct dwarf2_cu *cu)
2df3850c 22437{
5e22e966 22438 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 22439 LONGEST value;
d521ce57 22440 const gdb_byte *bytes;
98bfdba5 22441 struct dwarf2_locexpr_baton *baton;
2df3850c 22442
98bfdba5 22443 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22444 sym->print_name (),
98bfdba5
PA
22445 &objfile->objfile_obstack, cu,
22446 &value, &bytes, &baton);
2df3850c 22447
98bfdba5
PA
22448 if (baton != NULL)
22449 {
98bfdba5 22450 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22451 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22452 }
22453 else if (bytes != NULL)
22454 {
22455 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22456 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22457 }
22458 else
22459 {
22460 SYMBOL_VALUE (sym) = value;
f1e6e072 22461 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22462 }
2df3850c
JM
22463}
22464
c906108c
SS
22465/* Return the type of the die in question using its DW_AT_type attribute. */
22466
22467static struct type *
e7c27a73 22468die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22469{
c906108c 22470 struct attribute *type_attr;
c906108c 22471
e142c38c 22472 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22473 if (!type_attr)
22474 {
5e22e966 22475 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22476 /* A missing DW_AT_type represents a void type. */
518817b3 22477 return objfile_type (objfile)->builtin_void;
c906108c 22478 }
348e048f 22479
673bfd45 22480 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22481}
22482
b4ba55a1
JB
22483/* True iff CU's producer generates GNAT Ada auxiliary information
22484 that allows to find parallel types through that information instead
22485 of having to do expensive parallel lookups by type name. */
22486
22487static int
22488need_gnat_info (struct dwarf2_cu *cu)
22489{
de4cb04a
JB
22490 /* Assume that the Ada compiler was GNAT, which always produces
22491 the auxiliary information. */
22492 return (cu->language == language_ada);
b4ba55a1
JB
22493}
22494
b4ba55a1
JB
22495/* Return the auxiliary type of the die in question using its
22496 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22497 attribute is not present. */
22498
22499static struct type *
22500die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22501{
b4ba55a1 22502 struct attribute *type_attr;
b4ba55a1
JB
22503
22504 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22505 if (!type_attr)
22506 return NULL;
22507
673bfd45 22508 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22509}
22510
22511/* If DIE has a descriptive_type attribute, then set the TYPE's
22512 descriptive type accordingly. */
22513
22514static void
22515set_descriptive_type (struct type *type, struct die_info *die,
22516 struct dwarf2_cu *cu)
22517{
22518 struct type *descriptive_type = die_descriptive_type (die, cu);
22519
22520 if (descriptive_type)
22521 {
22522 ALLOCATE_GNAT_AUX_TYPE (type);
22523 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22524 }
22525}
22526
c906108c
SS
22527/* Return the containing type of the die in question using its
22528 DW_AT_containing_type attribute. */
22529
22530static struct type *
e7c27a73 22531die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22532{
c906108c 22533 struct attribute *type_attr;
5e22e966 22534 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22535
e142c38c 22536 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22537 if (!type_attr)
22538 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22539 "[in module %s]"), objfile_name (objfile));
33ac96f0 22540
673bfd45 22541 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22542}
22543
ac9ec31b
DE
22544/* Return an error marker type to use for the ill formed type in DIE/CU. */
22545
22546static struct type *
22547build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22548{
976ca316
SM
22549 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22550 struct objfile *objfile = per_objfile->objfile;
528e1572 22551 char *saved;
ac9ec31b 22552
528e1572
SM
22553 std::string message
22554 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22555 objfile_name (objfile),
22556 sect_offset_str (cu->header.sect_off),
22557 sect_offset_str (die->sect_off));
efba19b0 22558 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22559
19f392bc 22560 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22561}
22562
673bfd45 22563/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22564 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22565 DW_AT_containing_type.
673bfd45
DE
22566 If there is no type substitute an error marker. */
22567
c906108c 22568static struct type *
ff39bb5e 22569lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22570 struct dwarf2_cu *cu)
c906108c 22571{
976ca316
SM
22572 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22573 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
22574 struct type *this_type;
22575
ac9ec31b
DE
22576 gdb_assert (attr->name == DW_AT_type
22577 || attr->name == DW_AT_GNAT_descriptive_type
22578 || attr->name == DW_AT_containing_type);
22579
673bfd45
DE
22580 /* First see if we have it cached. */
22581
36586728
TT
22582 if (attr->form == DW_FORM_GNU_ref_alt)
22583 {
22584 struct dwarf2_per_cu_data *per_cu;
0826b30a 22585 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 22586
976ca316
SM
22587 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22588 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 22589 }
cd6c91b4 22590 else if (attr->form_is_ref ())
673bfd45 22591 {
0826b30a 22592 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 22593
976ca316 22594 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 22595 }
55f1336d 22596 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22597 {
630ed6b9 22598 ULONGEST signature = attr->as_signature ();
673bfd45 22599
ac9ec31b 22600 return get_signatured_type (die, signature, cu);
673bfd45
DE
22601 }
22602 else
22603 {
b98664d3 22604 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22605 " at %s [in module %s]"),
22606 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22607 objfile_name (objfile));
ac9ec31b 22608 return build_error_marker_type (cu, die);
673bfd45
DE
22609 }
22610
22611 /* If not cached we need to read it in. */
22612
22613 if (this_type == NULL)
22614 {
ac9ec31b 22615 struct die_info *type_die = NULL;
673bfd45
DE
22616 struct dwarf2_cu *type_cu = cu;
22617
cd6c91b4 22618 if (attr->form_is_ref ())
ac9ec31b
DE
22619 type_die = follow_die_ref (die, attr, &type_cu);
22620 if (type_die == NULL)
22621 return build_error_marker_type (cu, die);
22622 /* If we find the type now, it's probably because the type came
3019eac3
DE
22623 from an inter-CU reference and the type's CU got expanded before
22624 ours. */
ac9ec31b 22625 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22626 }
22627
22628 /* If we still don't have a type use an error marker. */
22629
22630 if (this_type == NULL)
ac9ec31b 22631 return build_error_marker_type (cu, die);
673bfd45 22632
f792889a 22633 return this_type;
c906108c
SS
22634}
22635
673bfd45
DE
22636/* Return the type in DIE, CU.
22637 Returns NULL for invalid types.
22638
02142a6c 22639 This first does a lookup in die_type_hash,
673bfd45
DE
22640 and only reads the die in if necessary.
22641
22642 NOTE: This can be called when reading in partial or full symbols. */
22643
f792889a 22644static struct type *
e7c27a73 22645read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22646{
f792889a
DJ
22647 struct type *this_type;
22648
22649 this_type = get_die_type (die, cu);
22650 if (this_type)
22651 return this_type;
22652
673bfd45
DE
22653 return read_type_die_1 (die, cu);
22654}
22655
22656/* Read the type in DIE, CU.
22657 Returns NULL for invalid types. */
22658
22659static struct type *
22660read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22661{
22662 struct type *this_type = NULL;
22663
c906108c
SS
22664 switch (die->tag)
22665 {
22666 case DW_TAG_class_type:
680b30c7 22667 case DW_TAG_interface_type:
c906108c
SS
22668 case DW_TAG_structure_type:
22669 case DW_TAG_union_type:
f792889a 22670 this_type = read_structure_type (die, cu);
c906108c
SS
22671 break;
22672 case DW_TAG_enumeration_type:
f792889a 22673 this_type = read_enumeration_type (die, cu);
c906108c
SS
22674 break;
22675 case DW_TAG_subprogram:
22676 case DW_TAG_subroutine_type:
edb3359d 22677 case DW_TAG_inlined_subroutine:
f792889a 22678 this_type = read_subroutine_type (die, cu);
c906108c
SS
22679 break;
22680 case DW_TAG_array_type:
f792889a 22681 this_type = read_array_type (die, cu);
c906108c 22682 break;
72019c9c 22683 case DW_TAG_set_type:
f792889a 22684 this_type = read_set_type (die, cu);
72019c9c 22685 break;
c906108c 22686 case DW_TAG_pointer_type:
f792889a 22687 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22688 break;
22689 case DW_TAG_ptr_to_member_type:
f792889a 22690 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22691 break;
22692 case DW_TAG_reference_type:
4297a3f0
AV
22693 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22694 break;
22695 case DW_TAG_rvalue_reference_type:
22696 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22697 break;
22698 case DW_TAG_const_type:
f792889a 22699 this_type = read_tag_const_type (die, cu);
c906108c
SS
22700 break;
22701 case DW_TAG_volatile_type:
f792889a 22702 this_type = read_tag_volatile_type (die, cu);
c906108c 22703 break;
06d66ee9
TT
22704 case DW_TAG_restrict_type:
22705 this_type = read_tag_restrict_type (die, cu);
22706 break;
c906108c 22707 case DW_TAG_string_type:
f792889a 22708 this_type = read_tag_string_type (die, cu);
c906108c
SS
22709 break;
22710 case DW_TAG_typedef:
f792889a 22711 this_type = read_typedef (die, cu);
c906108c 22712 break;
a02abb62 22713 case DW_TAG_subrange_type:
f792889a 22714 this_type = read_subrange_type (die, cu);
a02abb62 22715 break;
c906108c 22716 case DW_TAG_base_type:
f792889a 22717 this_type = read_base_type (die, cu);
c906108c 22718 break;
81a17f79 22719 case DW_TAG_unspecified_type:
f792889a 22720 this_type = read_unspecified_type (die, cu);
81a17f79 22721 break;
0114d602
DJ
22722 case DW_TAG_namespace:
22723 this_type = read_namespace_type (die, cu);
22724 break;
f55ee35c
JK
22725 case DW_TAG_module:
22726 this_type = read_module_type (die, cu);
22727 break;
a2c2acaf
MW
22728 case DW_TAG_atomic_type:
22729 this_type = read_tag_atomic_type (die, cu);
22730 break;
c906108c 22731 default:
b98664d3 22732 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22733 dwarf_tag_name (die->tag));
c906108c
SS
22734 break;
22735 }
63d06c5c 22736
f792889a 22737 return this_type;
63d06c5c
DC
22738}
22739
abc72ce4
DE
22740/* See if we can figure out if the class lives in a namespace. We do
22741 this by looking for a member function; its demangled name will
22742 contain namespace info, if there is any.
22743 Return the computed name or NULL.
22744 Space for the result is allocated on the objfile's obstack.
22745 This is the full-die version of guess_partial_die_structure_name.
22746 In this case we know DIE has no useful parent. */
22747
43816ebc 22748static const char *
abc72ce4
DE
22749guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22750{
22751 struct die_info *spec_die;
22752 struct dwarf2_cu *spec_cu;
22753 struct die_info *child;
5e22e966 22754 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
22755
22756 spec_cu = cu;
22757 spec_die = die_specification (die, &spec_cu);
22758 if (spec_die != NULL)
22759 {
22760 die = spec_die;
22761 cu = spec_cu;
22762 }
22763
22764 for (child = die->child;
22765 child != NULL;
22766 child = child->sibling)
22767 {
22768 if (child->tag == DW_TAG_subprogram)
22769 {
73b9be8b 22770 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22771
7d45c7c3 22772 if (linkage_name != NULL)
abc72ce4 22773 {
43816ebc 22774 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 22775 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 22776 const char *name = NULL;
abc72ce4
DE
22777
22778 if (actual_name != NULL)
22779 {
15d034d0 22780 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22781
22782 if (die_name != NULL
43816ebc 22783 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22784 {
22785 /* Strip off the class name from the full name.
22786 We want the prefix. */
22787 int die_name_len = strlen (die_name);
43816ebc
TT
22788 int actual_name_len = strlen (actual_name.get ());
22789 const char *ptr = actual_name.get ();
abc72ce4
DE
22790
22791 /* Test for '::' as a sanity check. */
22792 if (actual_name_len > die_name_len + 2
43816ebc 22793 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22794 name = obstack_strndup (
e3b94546 22795 &objfile->per_bfd->storage_obstack,
43816ebc 22796 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22797 }
22798 }
abc72ce4
DE
22799 return name;
22800 }
22801 }
22802 }
22803
22804 return NULL;
22805}
22806
96408a79
SA
22807/* GCC might emit a nameless typedef that has a linkage name. Determine the
22808 prefix part in such case. See
22809 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22810
a121b7c1 22811static const char *
96408a79
SA
22812anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22813{
22814 struct attribute *attr;
e6a959d6 22815 const char *base;
96408a79
SA
22816
22817 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22818 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22819 return NULL;
22820
7d45c7c3 22821 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22822 return NULL;
22823
73b9be8b 22824 attr = dw2_linkage_name_attr (die, cu);
2c830f54
TT
22825 const char *attr_name = attr->as_string ();
22826 if (attr == NULL || attr_name == NULL)
96408a79
SA
22827 return NULL;
22828
22829 /* dwarf2_name had to be already called. */
3b64bf15 22830 gdb_assert (attr->canonical_string_p ());
96408a79
SA
22831
22832 /* Strip the base name, keep any leading namespaces/classes. */
2c830f54
TT
22833 base = strrchr (attr_name, ':');
22834 if (base == NULL || base == attr_name || base[-1] != ':')
96408a79
SA
22835 return "";
22836
5e22e966 22837 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9 22838 return obstack_strndup (&objfile->per_bfd->storage_obstack,
2c830f54
TT
22839 attr_name,
22840 &base[-1] - attr_name);
96408a79
SA
22841}
22842
fdde2d81 22843/* Return the name of the namespace/class that DIE is defined within,
0114d602 22844 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22845
0114d602
DJ
22846 For example, if we're within the method foo() in the following
22847 code:
22848
22849 namespace N {
22850 class C {
22851 void foo () {
22852 }
22853 };
22854 }
22855
22856 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22857
0d5cff50 22858static const char *
e142c38c 22859determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22860{
976ca316 22861 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
22862 struct die_info *parent, *spec_die;
22863 struct dwarf2_cu *spec_cu;
22864 struct type *parent_type;
a121b7c1 22865 const char *retval;
63d06c5c 22866
9c37b5ae 22867 if (cu->language != language_cplus
c44af4eb
TT
22868 && cu->language != language_fortran && cu->language != language_d
22869 && cu->language != language_rust)
0114d602
DJ
22870 return "";
22871
96408a79
SA
22872 retval = anonymous_struct_prefix (die, cu);
22873 if (retval)
22874 return retval;
22875
0114d602
DJ
22876 /* We have to be careful in the presence of DW_AT_specification.
22877 For example, with GCC 3.4, given the code
22878
22879 namespace N {
22880 void foo() {
22881 // Definition of N::foo.
22882 }
22883 }
22884
22885 then we'll have a tree of DIEs like this:
22886
22887 1: DW_TAG_compile_unit
22888 2: DW_TAG_namespace // N
22889 3: DW_TAG_subprogram // declaration of N::foo
22890 4: DW_TAG_subprogram // definition of N::foo
22891 DW_AT_specification // refers to die #3
22892
22893 Thus, when processing die #4, we have to pretend that we're in
22894 the context of its DW_AT_specification, namely the contex of die
22895 #3. */
22896 spec_cu = cu;
22897 spec_die = die_specification (die, &spec_cu);
22898 if (spec_die == NULL)
22899 parent = die->parent;
22900 else
63d06c5c 22901 {
0114d602
DJ
22902 parent = spec_die->parent;
22903 cu = spec_cu;
63d06c5c 22904 }
0114d602
DJ
22905
22906 if (parent == NULL)
22907 return "";
98bfdba5
PA
22908 else if (parent->building_fullname)
22909 {
22910 const char *name;
22911 const char *parent_name;
22912
22913 /* It has been seen on RealView 2.2 built binaries,
22914 DW_TAG_template_type_param types actually _defined_ as
22915 children of the parent class:
22916
22917 enum E {};
22918 template class <class Enum> Class{};
22919 Class<enum E> class_e;
22920
dda83cd7
SM
22921 1: DW_TAG_class_type (Class)
22922 2: DW_TAG_enumeration_type (E)
22923 3: DW_TAG_enumerator (enum1:0)
22924 3: DW_TAG_enumerator (enum2:1)
22925 ...
22926 2: DW_TAG_template_type_param
22927 DW_AT_type DW_FORM_ref_udata (E)
98bfdba5
PA
22928
22929 Besides being broken debug info, it can put GDB into an
22930 infinite loop. Consider:
22931
22932 When we're building the full name for Class<E>, we'll start
22933 at Class, and go look over its template type parameters,
22934 finding E. We'll then try to build the full name of E, and
22935 reach here. We're now trying to build the full name of E,
22936 and look over the parent DIE for containing scope. In the
22937 broken case, if we followed the parent DIE of E, we'd again
22938 find Class, and once again go look at its template type
22939 arguments, etc., etc. Simply don't consider such parent die
22940 as source-level parent of this die (it can't be, the language
22941 doesn't allow it), and break the loop here. */
22942 name = dwarf2_name (die, cu);
22943 parent_name = dwarf2_name (parent, cu);
b98664d3 22944 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22945 name ? name : "<unknown>",
22946 parent_name ? parent_name : "<unknown>");
22947 return "";
22948 }
63d06c5c 22949 else
0114d602
DJ
22950 switch (parent->tag)
22951 {
63d06c5c 22952 case DW_TAG_namespace:
0114d602 22953 parent_type = read_type_die (parent, cu);
acebe513
UW
22954 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22955 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22956 Work around this problem here. */
22957 if (cu->language == language_cplus
7d93a1e0 22958 && strcmp (parent_type->name (), "::") == 0)
acebe513 22959 return "";
0114d602 22960 /* We give a name to even anonymous namespaces. */
7d93a1e0 22961 return parent_type->name ();
63d06c5c 22962 case DW_TAG_class_type:
680b30c7 22963 case DW_TAG_interface_type:
63d06c5c 22964 case DW_TAG_structure_type:
0114d602 22965 case DW_TAG_union_type:
f55ee35c 22966 case DW_TAG_module:
0114d602 22967 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
22968 if (parent_type->name () != NULL)
22969 return parent_type->name ();
0114d602
DJ
22970 else
22971 /* An anonymous structure is only allowed non-static data
22972 members; no typedefs, no member functions, et cetera.
22973 So it does not need a prefix. */
22974 return "";
abc72ce4 22975 case DW_TAG_compile_unit:
95554aad 22976 case DW_TAG_partial_unit:
abc72ce4
DE
22977 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22978 if (cu->language == language_cplus
976ca316 22979 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
22980 && die->child != NULL
22981 && (die->tag == DW_TAG_class_type
22982 || die->tag == DW_TAG_structure_type
22983 || die->tag == DW_TAG_union_type))
22984 {
43816ebc 22985 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
22986 if (name != NULL)
22987 return name;
22988 }
22989 return "";
0a4b0913
AB
22990 case DW_TAG_subprogram:
22991 /* Nested subroutines in Fortran get a prefix with the name
22992 of the parent's subroutine. */
22993 if (cu->language == language_fortran)
22994 {
22995 if ((die->tag == DW_TAG_subprogram)
22996 && (dwarf2_name (parent, cu) != NULL))
22997 return dwarf2_name (parent, cu);
22998 }
22999 return determine_prefix (parent, cu);
3d567982
TT
23000 case DW_TAG_enumeration_type:
23001 parent_type = read_type_die (parent, cu);
23002 if (TYPE_DECLARED_CLASS (parent_type))
23003 {
7d93a1e0
SM
23004 if (parent_type->name () != NULL)
23005 return parent_type->name ();
3d567982
TT
23006 return "";
23007 }
23008 /* Fall through. */
63d06c5c 23009 default:
8176b9b8 23010 return determine_prefix (parent, cu);
63d06c5c 23011 }
63d06c5c
DC
23012}
23013
3e43a32a
MS
23014/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23015 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23016 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23017 an obconcat, otherwise allocate storage for the result. The CU argument is
23018 used to determine the language and hence, the appropriate separator. */
987504bb 23019
f55ee35c 23020#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
23021
23022static char *
f55ee35c 23023typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
dda83cd7 23024 int physname, struct dwarf2_cu *cu)
63d06c5c 23025{
f55ee35c 23026 const char *lead = "";
5c315b68 23027 const char *sep;
63d06c5c 23028
3e43a32a
MS
23029 if (suffix == NULL || suffix[0] == '\0'
23030 || prefix == NULL || prefix[0] == '\0')
987504bb 23031 sep = "";
45280282
IB
23032 else if (cu->language == language_d)
23033 {
23034 /* For D, the 'main' function could be defined in any module, but it
23035 should never be prefixed. */
23036 if (strcmp (suffix, "D main") == 0)
23037 {
23038 prefix = "";
23039 sep = "";
23040 }
23041 else
23042 sep = ".";
23043 }
f55ee35c
JK
23044 else if (cu->language == language_fortran && physname)
23045 {
23046 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23047 DW_AT_MIPS_linkage_name is preferred and used instead. */
23048
23049 lead = "__";
23050 sep = "_MOD_";
23051 }
987504bb
JJ
23052 else
23053 sep = "::";
63d06c5c 23054
6dd47d34
DE
23055 if (prefix == NULL)
23056 prefix = "";
23057 if (suffix == NULL)
23058 suffix = "";
23059
987504bb
JJ
23060 if (obs == NULL)
23061 {
3e43a32a 23062 char *retval
224c3ddb
SM
23063 = ((char *)
23064 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 23065
f55ee35c
JK
23066 strcpy (retval, lead);
23067 strcat (retval, prefix);
6dd47d34
DE
23068 strcat (retval, sep);
23069 strcat (retval, suffix);
63d06c5c
DC
23070 return retval;
23071 }
987504bb
JJ
23072 else
23073 {
23074 /* We have an obstack. */
f55ee35c 23075 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 23076 }
63d06c5c
DC
23077}
23078
71c25dea
TT
23079/* Get name of a die, return NULL if not found. */
23080
15d034d0
TT
23081static const char *
23082dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 23083 struct objfile *objfile)
71c25dea
TT
23084{
23085 if (name && cu->language == language_cplus)
23086 {
596dc4ad
TT
23087 gdb::unique_xmalloc_ptr<char> canon_name
23088 = cp_canonicalize_string (name);
71c25dea 23089
596dc4ad
TT
23090 if (canon_name != nullptr)
23091 name = objfile->intern (canon_name.get ());
71c25dea
TT
23092 }
23093
23094 return name;
c906108c
SS
23095}
23096
96553a0c
DE
23097/* Get name of a die, return NULL if not found.
23098 Anonymous namespaces are converted to their magic string. */
9219021c 23099
15d034d0 23100static const char *
e142c38c 23101dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
23102{
23103 struct attribute *attr;
5e22e966 23104 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 23105
e142c38c 23106 attr = dwarf2_attr (die, DW_AT_name, cu);
2c830f54
TT
23107 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23108 if (attr_name == nullptr
96553a0c 23109 && die->tag != DW_TAG_namespace
53832f31
TT
23110 && die->tag != DW_TAG_class_type
23111 && die->tag != DW_TAG_interface_type
23112 && die->tag != DW_TAG_structure_type
23113 && die->tag != DW_TAG_union_type)
71c25dea
TT
23114 return NULL;
23115
23116 switch (die->tag)
23117 {
23118 case DW_TAG_compile_unit:
95554aad 23119 case DW_TAG_partial_unit:
71c25dea
TT
23120 /* Compilation units have a DW_AT_name that is a filename, not
23121 a source language identifier. */
23122 case DW_TAG_enumeration_type:
23123 case DW_TAG_enumerator:
23124 /* These tags always have simple identifiers already; no need
23125 to canonicalize them. */
2c830f54 23126 return attr_name;
907af001 23127
96553a0c 23128 case DW_TAG_namespace:
2c830f54
TT
23129 if (attr_name != nullptr)
23130 return attr_name;
96553a0c
DE
23131 return CP_ANONYMOUS_NAMESPACE_STR;
23132
907af001
UW
23133 case DW_TAG_class_type:
23134 case DW_TAG_interface_type:
23135 case DW_TAG_structure_type:
23136 case DW_TAG_union_type:
23137 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23138 structures or unions. These were of the form "._%d" in GCC 4.1,
23139 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23140 and GCC 4.4. We work around this problem by ignoring these. */
2c830f54
TT
23141 if (attr_name != nullptr
23142 && (startswith (attr_name, "._")
23143 || startswith (attr_name, "<anonymous")))
907af001 23144 return NULL;
53832f31
TT
23145
23146 /* GCC might emit a nameless typedef that has a linkage name. See
23147 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
2c830f54 23148 if (!attr || attr_name == NULL)
53832f31 23149 {
73b9be8b 23150 attr = dw2_linkage_name_attr (die, cu);
95eb9e54 23151 attr_name = attr == nullptr ? nullptr : attr->as_string ();
2c830f54 23152 if (attr == NULL || attr_name == NULL)
53832f31
TT
23153 return NULL;
23154
2c830f54 23155 /* Avoid demangling attr_name the second time on a second
df5c6c50 23156 call for the same DIE. */
3b64bf15 23157 if (!attr->canonical_string_p ())
53832f31 23158 {
43816ebc 23159 gdb::unique_xmalloc_ptr<char> demangled
2c830f54 23160 (gdb_demangle (attr_name, DMGL_TYPES));
4f180d53
AT
23161 if (demangled == nullptr)
23162 return nullptr;
43816ebc 23163
c6481205 23164 attr->set_string_canonical (objfile->intern (demangled.get ()));
95eb9e54 23165 attr_name = attr->as_string ();
53832f31 23166 }
67430cd0 23167
2c830f54
TT
23168 /* Strip any leading namespaces/classes, keep only the
23169 base name. DW_AT_name for named DIEs does not
23170 contain the prefixes. */
23171 const char *base = strrchr (attr_name, ':');
23172 if (base && base > attr_name && base[-1] == ':')
67430cd0
TT
23173 return &base[1];
23174 else
2c830f54 23175 return attr_name;
53832f31 23176 }
907af001
UW
23177 break;
23178
71c25dea 23179 default:
907af001
UW
23180 break;
23181 }
23182
3b64bf15 23183 if (!attr->canonical_string_p ())
c6481205
TT
23184 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23185 objfile));
2c830f54 23186 return attr->as_string ();
9219021c
DC
23187}
23188
23189/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23190 is none. *EXT_CU is the CU containing DIE on input, and the CU
23191 containing the return value on output. */
9219021c
DC
23192
23193static struct die_info *
f2f0e013 23194dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23195{
23196 struct attribute *attr;
9219021c 23197
f2f0e013 23198 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23199 if (attr == NULL)
23200 return NULL;
23201
f2f0e013 23202 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23203}
23204
f9aca02d 23205static void
d97bc12b 23206dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23207{
23208 unsigned int i;
23209
d97bc12b 23210 print_spaces (indent, f);
9d8780f0 23211 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23212 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23213 sect_offset_str (die->sect_off));
d97bc12b
DE
23214
23215 if (die->parent != NULL)
23216 {
23217 print_spaces (indent, f);
9d8780f0
SM
23218 fprintf_unfiltered (f, " parent at offset: %s\n",
23219 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23220 }
23221
23222 print_spaces (indent, f);
23223 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23224 dwarf_bool_name (die->child != NULL));
c906108c 23225
d97bc12b
DE
23226 print_spaces (indent, f);
23227 fprintf_unfiltered (f, " attributes:\n");
23228
c906108c
SS
23229 for (i = 0; i < die->num_attrs; ++i)
23230 {
d97bc12b
DE
23231 print_spaces (indent, f);
23232 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23233 dwarf_attr_name (die->attrs[i].name),
23234 dwarf_form_name (die->attrs[i].form));
d97bc12b 23235
c906108c
SS
23236 switch (die->attrs[i].form)
23237 {
c906108c 23238 case DW_FORM_addr:
336d760d 23239 case DW_FORM_addrx:
3019eac3 23240 case DW_FORM_GNU_addr_index:
d97bc12b 23241 fprintf_unfiltered (f, "address: ");
36d378cf 23242 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
c906108c
SS
23243 break;
23244 case DW_FORM_block2:
23245 case DW_FORM_block4:
23246 case DW_FORM_block:
23247 case DW_FORM_block1:
56eb65bd 23248 fprintf_unfiltered (f, "block: size %s",
9d2246fc 23249 pulongest (die->attrs[i].as_block ()->size));
c906108c 23250 break;
2dc7f7b3 23251 case DW_FORM_exprloc:
56eb65bd 23252 fprintf_unfiltered (f, "expression: size %s",
9d2246fc 23253 pulongest (die->attrs[i].as_block ()->size));
2dc7f7b3 23254 break;
0224619f
JK
23255 case DW_FORM_data16:
23256 fprintf_unfiltered (f, "constant of 16 bytes");
23257 break;
4568ecf9
DE
23258 case DW_FORM_ref_addr:
23259 fprintf_unfiltered (f, "ref address: ");
529908cb 23260 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
4568ecf9 23261 break;
36586728
TT
23262 case DW_FORM_GNU_ref_alt:
23263 fprintf_unfiltered (f, "alt ref address: ");
529908cb 23264 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
36586728 23265 break;
10b3939b
DJ
23266 case DW_FORM_ref1:
23267 case DW_FORM_ref2:
23268 case DW_FORM_ref4:
4568ecf9
DE
23269 case DW_FORM_ref8:
23270 case DW_FORM_ref_udata:
d97bc12b 23271 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
529908cb 23272 (long) (die->attrs[i].as_unsigned ()));
10b3939b 23273 break;
c906108c
SS
23274 case DW_FORM_data1:
23275 case DW_FORM_data2:
23276 case DW_FORM_data4:
ce5d95e1 23277 case DW_FORM_data8:
c906108c 23278 case DW_FORM_udata:
43bbcdc2 23279 fprintf_unfiltered (f, "constant: %s",
529908cb 23280 pulongest (die->attrs[i].as_unsigned ()));
c906108c 23281 break;
2dc7f7b3
TT
23282 case DW_FORM_sec_offset:
23283 fprintf_unfiltered (f, "section offset: %s",
529908cb 23284 pulongest (die->attrs[i].as_unsigned ()));
2dc7f7b3 23285 break;
55f1336d 23286 case DW_FORM_ref_sig8:
ac9ec31b 23287 fprintf_unfiltered (f, "signature: %s",
630ed6b9 23288 hex_string (die->attrs[i].as_signature ()));
348e048f 23289 break;
c906108c 23290 case DW_FORM_string:
4bdf3d34 23291 case DW_FORM_strp:
43988095 23292 case DW_FORM_line_strp:
cf532bd1 23293 case DW_FORM_strx:
3019eac3 23294 case DW_FORM_GNU_str_index:
36586728 23295 case DW_FORM_GNU_strp_alt:
8285870a 23296 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c6481205
TT
23297 die->attrs[i].as_string ()
23298 ? die->attrs[i].as_string () : "",
23299 die->attrs[i].canonical_string_p () ? "is" : "not");
c906108c
SS
23300 break;
23301 case DW_FORM_flag:
c45bc3f8 23302 if (die->attrs[i].as_boolean ())
d97bc12b 23303 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23304 else
d97bc12b 23305 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23306 break;
2dc7f7b3
TT
23307 case DW_FORM_flag_present:
23308 fprintf_unfiltered (f, "flag: TRUE");
23309 break;
a8329558 23310 case DW_FORM_indirect:
0963b4bd
MS
23311 /* The reader will have reduced the indirect form to
23312 the "base form" so this form should not occur. */
5f48f8f3 23313 fprintf_unfiltered (f,
3e43a32a 23314 "unexpected attribute form: DW_FORM_indirect");
a8329558 23315 break;
1bc397c5 23316 case DW_FORM_sdata:
663c44ac
JK
23317 case DW_FORM_implicit_const:
23318 fprintf_unfiltered (f, "constant: %s",
1bc397c5 23319 plongest (die->attrs[i].as_signed ()));
663c44ac 23320 break;
c906108c 23321 default:
d97bc12b 23322 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23323 die->attrs[i].form);
d97bc12b 23324 break;
c906108c 23325 }
d97bc12b 23326 fprintf_unfiltered (f, "\n");
c906108c
SS
23327 }
23328}
23329
f9aca02d 23330static void
d97bc12b 23331dump_die_for_error (struct die_info *die)
c906108c 23332{
d97bc12b
DE
23333 dump_die_shallow (gdb_stderr, 0, die);
23334}
23335
23336static void
23337dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23338{
23339 int indent = level * 4;
23340
23341 gdb_assert (die != NULL);
23342
23343 if (level >= max_level)
23344 return;
23345
23346 dump_die_shallow (f, indent, die);
23347
23348 if (die->child != NULL)
c906108c 23349 {
d97bc12b
DE
23350 print_spaces (indent, f);
23351 fprintf_unfiltered (f, " Children:");
23352 if (level + 1 < max_level)
23353 {
23354 fprintf_unfiltered (f, "\n");
23355 dump_die_1 (f, level + 1, max_level, die->child);
23356 }
23357 else
23358 {
3e43a32a
MS
23359 fprintf_unfiltered (f,
23360 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23361 }
23362 }
23363
23364 if (die->sibling != NULL && level > 0)
23365 {
23366 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23367 }
23368}
23369
d97bc12b
DE
23370/* This is called from the pdie macro in gdbinit.in.
23371 It's not static so gcc will keep a copy callable from gdb. */
23372
23373void
23374dump_die (struct die_info *die, int max_level)
23375{
23376 dump_die_1 (gdb_stdlog, 0, max_level, die);
23377}
23378
f9aca02d 23379static void
51545339 23380store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23381{
51545339 23382 void **slot;
c906108c 23383
9c541725
PA
23384 slot = htab_find_slot_with_hash (cu->die_hash, die,
23385 to_underlying (die->sect_off),
b64f50a1 23386 INSERT);
51545339
DJ
23387
23388 *slot = die;
c906108c
SS
23389}
23390
348e048f
DE
23391/* Follow reference or signature attribute ATTR of SRC_DIE.
23392 On entry *REF_CU is the CU of SRC_DIE.
23393 On exit *REF_CU is the CU of the result. */
23394
23395static struct die_info *
ff39bb5e 23396follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23397 struct dwarf2_cu **ref_cu)
23398{
23399 struct die_info *die;
23400
cd6c91b4 23401 if (attr->form_is_ref ())
348e048f 23402 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23403 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23404 die = follow_die_sig (src_die, attr, ref_cu);
23405 else
23406 {
23407 dump_die_for_error (src_die);
23408 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 23409 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
23410 }
23411
23412 return die;
03dd20cc
DJ
23413}
23414
5c631832 23415/* Follow reference OFFSET.
673bfd45
DE
23416 On entry *REF_CU is the CU of the source die referencing OFFSET.
23417 On exit *REF_CU is the CU of the result.
23418 Returns NULL if OFFSET is invalid. */
f504f079 23419
f9aca02d 23420static struct die_info *
9c541725 23421follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23422 struct dwarf2_cu **ref_cu)
c906108c 23423{
10b3939b 23424 struct die_info temp_die;
f2f0e013 23425 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 23426 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 23427
348e048f
DE
23428 gdb_assert (cu->per_cu != NULL);
23429
98bfdba5
PA
23430 target_cu = cu;
23431
3019eac3 23432 if (cu->per_cu->is_debug_types)
348e048f
DE
23433 {
23434 /* .debug_types CUs cannot reference anything outside their CU.
23435 If they need to, they have to reference a signatured type via
55f1336d 23436 DW_FORM_ref_sig8. */
4057dfde 23437 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 23438 return NULL;
348e048f 23439 }
36586728 23440 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 23441 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
23442 {
23443 struct dwarf2_per_cu_data *per_cu;
9a619af0 23444
9c541725 23445 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 23446 per_objfile);
03dd20cc
DJ
23447
23448 /* If necessary, add it to the queue and load its DIEs. */
976ca316 23449 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
23450 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23451 false, cu->language);
03dd20cc 23452
976ca316 23453 target_cu = per_objfile->get_cu (per_cu);
10b3939b 23454 }
98bfdba5
PA
23455 else if (cu->dies == NULL)
23456 {
23457 /* We're loading full DIEs during partial symbol reading. */
976ca316 23458 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
4a636814
SM
23459 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23460 language_minimal);
98bfdba5 23461 }
c906108c 23462
f2f0e013 23463 *ref_cu = target_cu;
9c541725 23464 temp_die.sect_off = sect_off;
c24bdb02
KS
23465
23466 if (target_cu != cu)
23467 target_cu->ancestor = cu;
23468
9a3c8263 23469 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23470 &temp_die,
23471 to_underlying (sect_off));
5c631832 23472}
10b3939b 23473
5c631832
JK
23474/* Follow reference attribute ATTR of SRC_DIE.
23475 On entry *REF_CU is the CU of SRC_DIE.
23476 On exit *REF_CU is the CU of the result. */
23477
23478static struct die_info *
ff39bb5e 23479follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23480 struct dwarf2_cu **ref_cu)
23481{
0826b30a 23482 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
23483 struct dwarf2_cu *cu = *ref_cu;
23484 struct die_info *die;
23485
9c541725 23486 die = follow_die_offset (sect_off,
36586728
TT
23487 (attr->form == DW_FORM_GNU_ref_alt
23488 || cu->per_cu->is_dwz),
23489 ref_cu);
5c631832 23490 if (!die)
9d8780f0
SM
23491 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23492 "at %s [in module %s]"),
23493 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 23494 objfile_name (cu->per_objfile->objfile));
348e048f 23495
5c631832
JK
23496 return die;
23497}
23498
d4c9a4f8 23499/* See read.h. */
5c631832
JK
23500
23501struct dwarf2_locexpr_baton
9c541725 23502dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 23503 dwarf2_per_cu_data *per_cu,
976ca316 23504 dwarf2_per_objfile *per_objfile,
041d9819
SM
23505 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23506 bool resolve_abstract_p)
5c631832 23507{
5c631832
JK
23508 struct die_info *die;
23509 struct attribute *attr;
23510 struct dwarf2_locexpr_baton retval;
976ca316 23511 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 23512
976ca316 23513 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 23514 if (cu == nullptr)
976ca316 23515 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
23516
23517 if (cu == nullptr)
cc12ce38
DE
23518 {
23519 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23520 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23521 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23522 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23523 }
918dd910 23524
9c541725 23525 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23526 if (!die)
9d8780f0
SM
23527 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23528 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23529
23530 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23531 if (!attr && resolve_abstract_p
976ca316
SM
23532 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23533 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65 23534 {
041d9819 23535 CORE_ADDR pc = get_frame_pc ();
b3b3bada 23536 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 23537 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 23538
3360b6e7 23539 for (const auto &cand_off
976ca316 23540 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 23541 {
3360b6e7
TV
23542 struct dwarf2_cu *cand_cu = cu;
23543 struct die_info *cand
23544 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23545 if (!cand
23546 || !cand->parent
e4a62c65
TV
23547 || cand->parent->tag != DW_TAG_subprogram)
23548 continue;
23549
23550 CORE_ADDR pc_low, pc_high;
23551 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23552 if (pc_low == ((CORE_ADDR) -1))
23553 continue;
23554 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23555 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23556 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23557 continue;
23558
23559 die = cand;
23560 attr = dwarf2_attr (die, DW_AT_location, cu);
23561 break;
23562 }
23563 }
23564
5c631832
JK
23565 if (!attr)
23566 {
e103e986
JK
23567 /* DWARF: "If there is no such attribute, then there is no effect.".
23568 DATA is ignored if SIZE is 0. */
5c631832 23569
e103e986 23570 retval.data = NULL;
5c631832
JK
23571 retval.size = 0;
23572 }
cd6c91b4 23573 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23574 {
23575 struct dwarf2_loclist_baton loclist_baton;
041d9819 23576 CORE_ADDR pc = get_frame_pc ();
8cf6f0b1
TT
23577 size_t size;
23578
23579 fill_in_loclist_baton (cu, &loclist_baton, attr);
23580
23581 retval.data = dwarf2_find_location_expression (&loclist_baton,
23582 &size, pc);
23583 retval.size = size;
23584 }
5c631832
JK
23585 else
23586 {
4fc6c0d5 23587 if (!attr->form_is_block ())
9d8780f0 23588 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23589 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23590 sect_offset_str (sect_off), objfile_name (objfile));
5c631832 23591
9d2246fc
TT
23592 struct dwarf_block *block = attr->as_block ();
23593 retval.data = block->data;
23594 retval.size = block->size;
5c631832 23595 }
976ca316 23596 retval.per_objfile = per_objfile;
5c631832 23597 retval.per_cu = cu->per_cu;
918dd910 23598
976ca316 23599 per_objfile->age_comp_units ();
918dd910 23600
5c631832 23601 return retval;
348e048f
DE
23602}
23603
d4c9a4f8 23604/* See read.h. */
8b9737bf
TT
23605
23606struct dwarf2_locexpr_baton
23607dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 23608 dwarf2_per_cu_data *per_cu,
14095eb3 23609 dwarf2_per_objfile *per_objfile,
041d9819 23610 gdb::function_view<CORE_ADDR ()> get_frame_pc)
8b9737bf 23611{
9c541725 23612 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23613
14095eb3 23614 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
041d9819 23615 get_frame_pc);
8b9737bf
TT
23616}
23617
b6807d98
TT
23618/* Write a constant of a given type as target-ordered bytes into
23619 OBSTACK. */
23620
23621static const gdb_byte *
23622write_constant_as_bytes (struct obstack *obstack,
23623 enum bfd_endian byte_order,
23624 struct type *type,
23625 ULONGEST value,
23626 LONGEST *len)
23627{
23628 gdb_byte *result;
23629
23630 *len = TYPE_LENGTH (type);
224c3ddb 23631 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23632 store_unsigned_integer (result, *len, byte_order, value);
23633
23634 return result;
23635}
23636
d4c9a4f8 23637/* See read.h. */
b6807d98
TT
23638
23639const gdb_byte *
9c541725 23640dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 23641 dwarf2_per_cu_data *per_cu,
14095eb3 23642 dwarf2_per_objfile *per_objfile,
d4c9a4f8 23643 obstack *obstack,
b6807d98
TT
23644 LONGEST *len)
23645{
b6807d98
TT
23646 struct die_info *die;
23647 struct attribute *attr;
23648 const gdb_byte *result = NULL;
23649 struct type *type;
23650 LONGEST value;
23651 enum bfd_endian byte_order;
14095eb3 23652 struct objfile *objfile = per_objfile->objfile;
b6807d98 23653
7188ed02 23654 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23655 if (cu == nullptr)
23656 cu = load_cu (per_cu, per_objfile, false);
23657
23658 if (cu == nullptr)
cc12ce38
DE
23659 {
23660 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23661 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23662 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23663 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23664 }
b6807d98 23665
9c541725 23666 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23667 if (!die)
9d8780f0
SM
23668 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23669 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23670
23671 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23672 if (attr == NULL)
23673 return NULL;
23674
e3b94546 23675 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23676 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23677
23678 switch (attr->form)
23679 {
23680 case DW_FORM_addr:
336d760d 23681 case DW_FORM_addrx:
b6807d98
TT
23682 case DW_FORM_GNU_addr_index:
23683 {
23684 gdb_byte *tem;
23685
23686 *len = cu->header.addr_size;
224c3ddb 23687 tem = (gdb_byte *) obstack_alloc (obstack, *len);
36d378cf 23688 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
b6807d98
TT
23689 result = tem;
23690 }
23691 break;
23692 case DW_FORM_string:
23693 case DW_FORM_strp:
cf532bd1 23694 case DW_FORM_strx:
b6807d98
TT
23695 case DW_FORM_GNU_str_index:
23696 case DW_FORM_GNU_strp_alt:
c6481205 23697 /* The string is already allocated on the objfile obstack, point
b6807d98 23698 directly to it. */
2c830f54
TT
23699 {
23700 const char *attr_name = attr->as_string ();
23701 result = (const gdb_byte *) attr_name;
23702 *len = strlen (attr_name);
23703 }
b6807d98
TT
23704 break;
23705 case DW_FORM_block1:
23706 case DW_FORM_block2:
23707 case DW_FORM_block4:
23708 case DW_FORM_block:
23709 case DW_FORM_exprloc:
0224619f 23710 case DW_FORM_data16:
9d2246fc
TT
23711 {
23712 struct dwarf_block *block = attr->as_block ();
23713 result = block->data;
23714 *len = block->size;
23715 }
b6807d98
TT
23716 break;
23717
23718 /* The DW_AT_const_value attributes are supposed to carry the
23719 symbol's value "represented as it would be on the target
23720 architecture." By the time we get here, it's already been
23721 converted to host endianness, so we just need to sign- or
23722 zero-extend it as appropriate. */
23723 case DW_FORM_data1:
23724 type = die_type (die, cu);
23725 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23726 if (result == NULL)
23727 result = write_constant_as_bytes (obstack, byte_order,
23728 type, value, len);
23729 break;
23730 case DW_FORM_data2:
23731 type = die_type (die, cu);
23732 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23733 if (result == NULL)
23734 result = write_constant_as_bytes (obstack, byte_order,
23735 type, value, len);
23736 break;
23737 case DW_FORM_data4:
23738 type = die_type (die, cu);
23739 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23740 if (result == NULL)
23741 result = write_constant_as_bytes (obstack, byte_order,
23742 type, value, len);
23743 break;
23744 case DW_FORM_data8:
23745 type = die_type (die, cu);
23746 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23747 if (result == NULL)
23748 result = write_constant_as_bytes (obstack, byte_order,
23749 type, value, len);
23750 break;
23751
23752 case DW_FORM_sdata:
663c44ac 23753 case DW_FORM_implicit_const:
b6807d98
TT
23754 type = die_type (die, cu);
23755 result = write_constant_as_bytes (obstack, byte_order,
1bc397c5 23756 type, attr->as_signed (), len);
b6807d98
TT
23757 break;
23758
23759 case DW_FORM_udata:
23760 type = die_type (die, cu);
23761 result = write_constant_as_bytes (obstack, byte_order,
529908cb 23762 type, attr->as_unsigned (), len);
b6807d98
TT
23763 break;
23764
23765 default:
b98664d3 23766 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23767 dwarf_form_name (attr->form));
23768 break;
23769 }
23770
23771 return result;
23772}
23773
d4c9a4f8 23774/* See read.h. */
7942e96e
AA
23775
23776struct type *
9c541725 23777dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
23778 dwarf2_per_cu_data *per_cu,
23779 dwarf2_per_objfile *per_objfile)
7942e96e 23780{
7942e96e
AA
23781 struct die_info *die;
23782
7188ed02 23783 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23784 if (cu == nullptr)
23785 cu = load_cu (per_cu, per_objfile, false);
23786
23787 if (cu == nullptr)
23788 return nullptr;
7942e96e 23789
9c541725 23790 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23791 if (!die)
23792 return NULL;
23793
23794 return die_type (die, cu);
23795}
23796
8cb5117c 23797/* See read.h. */
8a9b8146
TT
23798
23799struct type *
b64f50a1 23800dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
23801 dwarf2_per_cu_data *per_cu,
23802 dwarf2_per_objfile *per_objfile)
8a9b8146 23803{
9c541725 23804 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 23805 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
23806}
23807
ac9ec31b 23808/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23809 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23810 On exit *REF_CU is the CU of the result.
23811 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23812
23813static struct die_info *
ac9ec31b
DE
23814follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23815 struct dwarf2_cu **ref_cu)
348e048f 23816{
348e048f 23817 struct die_info temp_die;
c24bdb02 23818 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 23819 struct die_info *die;
976ca316 23820 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 23821
348e048f 23822
ac9ec31b
DE
23823 /* While it might be nice to assert sig_type->type == NULL here,
23824 we can get here for DW_AT_imported_declaration where we need
23825 the DIE not the type. */
348e048f
DE
23826
23827 /* If necessary, add it to the queue and load its DIEs. */
23828
976ca316 23829 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
120ce1b5 23830 language_minimal))
976ca316 23831 read_signatured_type (sig_type, per_objfile);
348e048f 23832
976ca316 23833 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
69d751e3 23834 gdb_assert (sig_cu != NULL);
9c541725
PA
23835 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23836 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23837 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23838 to_underlying (temp_die.sect_off));
348e048f
DE
23839 if (die)
23840 {
796a7ff8
DE
23841 /* For .gdb_index version 7 keep track of included TUs.
23842 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
23843 if (per_objfile->per_bfd->index_table != NULL
23844 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 23845 {
ae640021 23846 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23847 }
23848
348e048f 23849 *ref_cu = sig_cu;
c24bdb02
KS
23850 if (sig_cu != cu)
23851 sig_cu->ancestor = cu;
23852
348e048f
DE
23853 return die;
23854 }
23855
ac9ec31b
DE
23856 return NULL;
23857}
23858
23859/* Follow signatured type referenced by ATTR in SRC_DIE.
23860 On entry *REF_CU is the CU of SRC_DIE.
23861 On exit *REF_CU is the CU of the result.
23862 The result is the DIE of the type.
23863 If the referenced type cannot be found an error is thrown. */
23864
23865static struct die_info *
ff39bb5e 23866follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23867 struct dwarf2_cu **ref_cu)
23868{
630ed6b9 23869 ULONGEST signature = attr->as_signature ();
ac9ec31b
DE
23870 struct signatured_type *sig_type;
23871 struct die_info *die;
23872
23873 gdb_assert (attr->form == DW_FORM_ref_sig8);
23874
a2ce51a0 23875 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23876 /* sig_type will be NULL if the signatured type is missing from
23877 the debug info. */
23878 if (sig_type == NULL)
23879 {
23880 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
dda83cd7
SM
23881 " from DIE at %s [in module %s]"),
23882 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23883 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23884 }
23885
23886 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23887 if (die == NULL)
23888 {
23889 dump_die_for_error (src_die);
23890 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23891 " from DIE at %s [in module %s]"),
23892 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23893 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23894 }
23895
23896 return die;
23897}
23898
23899/* Get the type specified by SIGNATURE referenced in DIE/CU,
23900 reading in and processing the type unit if necessary. */
23901
23902static struct type *
23903get_signatured_type (struct die_info *die, ULONGEST signature,
23904 struct dwarf2_cu *cu)
23905{
976ca316 23906 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
23907 struct signatured_type *sig_type;
23908 struct dwarf2_cu *type_cu;
23909 struct die_info *type_die;
23910 struct type *type;
23911
a2ce51a0 23912 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23913 /* sig_type will be NULL if the signatured type is missing from
23914 the debug info. */
23915 if (sig_type == NULL)
23916 {
b98664d3 23917 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23918 " from DIE at %s [in module %s]"),
23919 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23920 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23921 return build_error_marker_type (cu, die);
23922 }
23923
23924 /* If we already know the type we're done. */
976ca316 23925 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
23926 if (type != nullptr)
23927 return type;
ac9ec31b
DE
23928
23929 type_cu = cu;
23930 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23931 if (type_die != NULL)
23932 {
23933 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23934 is created. This is important, for example, because for c++ classes
23935 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23936 type = read_type_die (type_die, type_cu);
23937 if (type == NULL)
23938 {
b98664d3 23939 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23940 " referenced from DIE at %s [in module %s]"),
23941 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23942 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23943 type = build_error_marker_type (cu, die);
23944 }
23945 }
23946 else
23947 {
b98664d3 23948 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23949 " from DIE at %s [in module %s]"),
23950 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23951 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23952 type = build_error_marker_type (cu, die);
23953 }
e286671b 23954
976ca316 23955 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
23956
23957 return type;
23958}
23959
23960/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23961 reading in and processing the type unit if necessary. */
23962
23963static struct type *
ff39bb5e 23964get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23965 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23966{
23967 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 23968 if (attr->form_is_ref ())
ac9ec31b
DE
23969 {
23970 struct dwarf2_cu *type_cu = cu;
23971 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23972
23973 return read_type_die (type_die, type_cu);
23974 }
23975 else if (attr->form == DW_FORM_ref_sig8)
23976 {
630ed6b9 23977 return get_signatured_type (die, attr->as_signature (), cu);
ac9ec31b
DE
23978 }
23979 else
23980 {
976ca316 23981 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 23982
b98664d3 23983 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23984 " at %s [in module %s]"),
23985 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 23986 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23987 return build_error_marker_type (cu, die);
23988 }
348e048f
DE
23989}
23990
e5fe5e75 23991/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23992
23993static void
ab432490
SM
23994load_full_type_unit (dwarf2_per_cu_data *per_cu,
23995 dwarf2_per_objfile *per_objfile)
348e048f 23996{
52dc124a 23997 struct signatured_type *sig_type;
348e048f 23998
f4dc4d17 23999 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 24000 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 24001
6721b2ec
DE
24002 /* We have the per_cu, but we need the signatured_type.
24003 Fortunately this is an easy translation. */
24004 gdb_assert (per_cu->is_debug_types);
24005 sig_type = (struct signatured_type *) per_cu;
348e048f 24006
7188ed02 24007 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24008
ab432490 24009 read_signatured_type (sig_type, per_objfile);
348e048f 24010
7188ed02 24011 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
24012}
24013
3019eac3
DE
24014/* Read in a signatured type and build its CU and DIEs.
24015 If the type is a stub for the real type in a DWO file,
24016 read in the real type from the DWO file as well. */
dee91e82
DE
24017
24018static void
ab432490
SM
24019read_signatured_type (signatured_type *sig_type,
24020 dwarf2_per_objfile *per_objfile)
dee91e82
DE
24021{
24022 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24023
3019eac3 24024 gdb_assert (per_cu->is_debug_types);
7188ed02 24025 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24026
2e671100 24027 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
24028
24029 if (!reader.dummy_p)
24030 {
24031 struct dwarf2_cu *cu = reader.cu;
24032 const gdb_byte *info_ptr = reader.info_ptr;
24033
24034 gdb_assert (cu->die_hash == NULL);
24035 cu->die_hash =
24036 htab_create_alloc_ex (cu->header.length / 12,
24037 die_hash,
24038 die_eq,
24039 NULL,
24040 &cu->comp_unit_obstack,
24041 hashtab_obstack_allocate,
24042 dummy_obstack_deallocate);
24043
3e225074 24044 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
24045 reader.comp_unit_die->child
24046 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24047 reader.comp_unit_die);
24048 cu->dies = reader.comp_unit_die;
24049 /* comp_unit_die is not stored in die_hash, no need. */
24050
24051 /* We try not to read any attributes in this function, because
24052 not all CUs needed for references have been loaded yet, and
24053 symbol table processing isn't initialized. But we have to
24054 set the CU language, or we won't be able to build types
24055 correctly. Similarly, if we do not read the producer, we can
24056 not apply producer-specific interpretation. */
24057 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
24058
24059 reader.keep ();
c0ab21c2
TT
24060 }
24061
7ee85ab1 24062 sig_type->per_cu.tu_read = 1;
c906108c
SS
24063}
24064
c906108c
SS
24065/* Decode simple location descriptions.
24066 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
24067 the location and return the value. If COMPUTED is non-null, it is
24068 set to true to indicate that decoding was successful, and false
24069 otherwise. If COMPUTED is null, then this function may emit a
24070 complaint. */
c906108c
SS
24071
24072static CORE_ADDR
7d79de9a 24073decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 24074{
5e22e966 24075 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
24076 size_t i;
24077 size_t size = blk->size;
d521ce57 24078 const gdb_byte *data = blk->data;
21ae7a4d
JK
24079 CORE_ADDR stack[64];
24080 int stacki;
24081 unsigned int bytes_read, unsnd;
24082 gdb_byte op;
c906108c 24083
7d79de9a
TT
24084 if (computed != nullptr)
24085 *computed = false;
24086
21ae7a4d
JK
24087 i = 0;
24088 stacki = 0;
24089 stack[stacki] = 0;
24090 stack[++stacki] = 0;
24091
24092 while (i < size)
24093 {
24094 op = data[i++];
24095 switch (op)
24096 {
24097 case DW_OP_lit0:
24098 case DW_OP_lit1:
24099 case DW_OP_lit2:
24100 case DW_OP_lit3:
24101 case DW_OP_lit4:
24102 case DW_OP_lit5:
24103 case DW_OP_lit6:
24104 case DW_OP_lit7:
24105 case DW_OP_lit8:
24106 case DW_OP_lit9:
24107 case DW_OP_lit10:
24108 case DW_OP_lit11:
24109 case DW_OP_lit12:
24110 case DW_OP_lit13:
24111 case DW_OP_lit14:
24112 case DW_OP_lit15:
24113 case DW_OP_lit16:
24114 case DW_OP_lit17:
24115 case DW_OP_lit18:
24116 case DW_OP_lit19:
24117 case DW_OP_lit20:
24118 case DW_OP_lit21:
24119 case DW_OP_lit22:
24120 case DW_OP_lit23:
24121 case DW_OP_lit24:
24122 case DW_OP_lit25:
24123 case DW_OP_lit26:
24124 case DW_OP_lit27:
24125 case DW_OP_lit28:
24126 case DW_OP_lit29:
24127 case DW_OP_lit30:
24128 case DW_OP_lit31:
24129 stack[++stacki] = op - DW_OP_lit0;
24130 break;
f1bea926 24131
21ae7a4d
JK
24132 case DW_OP_reg0:
24133 case DW_OP_reg1:
24134 case DW_OP_reg2:
24135 case DW_OP_reg3:
24136 case DW_OP_reg4:
24137 case DW_OP_reg5:
24138 case DW_OP_reg6:
24139 case DW_OP_reg7:
24140 case DW_OP_reg8:
24141 case DW_OP_reg9:
24142 case DW_OP_reg10:
24143 case DW_OP_reg11:
24144 case DW_OP_reg12:
24145 case DW_OP_reg13:
24146 case DW_OP_reg14:
24147 case DW_OP_reg15:
24148 case DW_OP_reg16:
24149 case DW_OP_reg17:
24150 case DW_OP_reg18:
24151 case DW_OP_reg19:
24152 case DW_OP_reg20:
24153 case DW_OP_reg21:
24154 case DW_OP_reg22:
24155 case DW_OP_reg23:
24156 case DW_OP_reg24:
24157 case DW_OP_reg25:
24158 case DW_OP_reg26:
24159 case DW_OP_reg27:
24160 case DW_OP_reg28:
24161 case DW_OP_reg29:
24162 case DW_OP_reg30:
24163 case DW_OP_reg31:
24164 stack[++stacki] = op - DW_OP_reg0;
24165 if (i < size)
7d79de9a
TT
24166 {
24167 if (computed == nullptr)
24168 dwarf2_complex_location_expr_complaint ();
24169 else
24170 return 0;
24171 }
21ae7a4d 24172 break;
c906108c 24173
21ae7a4d
JK
24174 case DW_OP_regx:
24175 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24176 i += bytes_read;
24177 stack[++stacki] = unsnd;
24178 if (i < size)
7d79de9a
TT
24179 {
24180 if (computed == nullptr)
24181 dwarf2_complex_location_expr_complaint ();
24182 else
24183 return 0;
24184 }
21ae7a4d 24185 break;
c906108c 24186
21ae7a4d 24187 case DW_OP_addr:
c8a7a66f
TT
24188 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24189 &bytes_read);
21ae7a4d
JK
24190 i += bytes_read;
24191 break;
d53d4ac5 24192
21ae7a4d
JK
24193 case DW_OP_const1u:
24194 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24195 i += 1;
24196 break;
24197
24198 case DW_OP_const1s:
24199 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24200 i += 1;
24201 break;
24202
24203 case DW_OP_const2u:
24204 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24205 i += 2;
24206 break;
24207
24208 case DW_OP_const2s:
24209 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24210 i += 2;
24211 break;
d53d4ac5 24212
21ae7a4d
JK
24213 case DW_OP_const4u:
24214 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24215 i += 4;
24216 break;
24217
24218 case DW_OP_const4s:
24219 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24220 i += 4;
24221 break;
24222
585861ea
JK
24223 case DW_OP_const8u:
24224 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24225 i += 8;
24226 break;
24227
21ae7a4d
JK
24228 case DW_OP_constu:
24229 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24230 &bytes_read);
24231 i += bytes_read;
24232 break;
24233
24234 case DW_OP_consts:
24235 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24236 i += bytes_read;
24237 break;
24238
24239 case DW_OP_dup:
24240 stack[stacki + 1] = stack[stacki];
24241 stacki++;
24242 break;
24243
24244 case DW_OP_plus:
24245 stack[stacki - 1] += stack[stacki];
24246 stacki--;
24247 break;
24248
24249 case DW_OP_plus_uconst:
24250 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24251 &bytes_read);
24252 i += bytes_read;
24253 break;
24254
24255 case DW_OP_minus:
24256 stack[stacki - 1] -= stack[stacki];
24257 stacki--;
24258 break;
24259
24260 case DW_OP_deref:
24261 /* If we're not the last op, then we definitely can't encode
24262 this using GDB's address_class enum. This is valid for partial
24263 global symbols, although the variable's address will be bogus
24264 in the psymtab. */
24265 if (i < size)
7d79de9a
TT
24266 {
24267 if (computed == nullptr)
24268 dwarf2_complex_location_expr_complaint ();
24269 else
24270 return 0;
24271 }
21ae7a4d
JK
24272 break;
24273
dda83cd7 24274 case DW_OP_GNU_push_tls_address:
4aa4e28b 24275 case DW_OP_form_tls_address:
21ae7a4d
JK
24276 /* The top of the stack has the offset from the beginning
24277 of the thread control block at which the variable is located. */
24278 /* Nothing should follow this operator, so the top of stack would
24279 be returned. */
24280 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24281 address will be bogus in the psymtab. Make it always at least
24282 non-zero to not look as a variable garbage collected by linker
24283 which have DW_OP_addr 0. */
21ae7a4d 24284 if (i < size)
7d79de9a
TT
24285 {
24286 if (computed == nullptr)
24287 dwarf2_complex_location_expr_complaint ();
24288 else
24289 return 0;
24290 }
585861ea 24291 stack[stacki]++;
dda83cd7 24292 break;
21ae7a4d
JK
24293
24294 case DW_OP_GNU_uninit:
7d79de9a
TT
24295 if (computed != nullptr)
24296 return 0;
21ae7a4d
JK
24297 break;
24298
336d760d 24299 case DW_OP_addrx:
3019eac3 24300 case DW_OP_GNU_addr_index:
49f6c839 24301 case DW_OP_GNU_const_index:
3019eac3
DE
24302 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24303 &bytes_read);
24304 i += bytes_read;
24305 break;
24306
21ae7a4d 24307 default:
7d79de9a
TT
24308 if (computed == nullptr)
24309 {
24310 const char *name = get_DW_OP_name (op);
21ae7a4d 24311
7d79de9a
TT
24312 if (name)
24313 complaint (_("unsupported stack op: '%s'"),
24314 name);
24315 else
24316 complaint (_("unsupported stack op: '%02x'"),
24317 op);
24318 }
21ae7a4d
JK
24319
24320 return (stack[stacki]);
d53d4ac5 24321 }
3c6e0cb3 24322
21ae7a4d 24323 /* Enforce maximum stack depth of SIZE-1 to avoid writing
dda83cd7 24324 outside of the allocated space. Also enforce minimum>0. */
21ae7a4d
JK
24325 if (stacki >= ARRAY_SIZE (stack) - 1)
24326 {
7d79de9a
TT
24327 if (computed == nullptr)
24328 complaint (_("location description stack overflow"));
21ae7a4d
JK
24329 return 0;
24330 }
24331
24332 if (stacki <= 0)
24333 {
7d79de9a
TT
24334 if (computed == nullptr)
24335 complaint (_("location description stack underflow"));
21ae7a4d
JK
24336 return 0;
24337 }
24338 }
7d79de9a
TT
24339
24340 if (computed != nullptr)
24341 *computed = true;
21ae7a4d 24342 return (stack[stacki]);
c906108c
SS
24343}
24344
24345/* memory allocation interface */
24346
c906108c 24347static struct dwarf_block *
7b5a2f43 24348dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24349{
8d749320 24350 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24351}
24352
c906108c 24353static struct die_info *
b60c80d6 24354dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24355{
24356 struct die_info *die;
b60c80d6
DJ
24357 size_t size = sizeof (struct die_info);
24358
24359 if (num_attrs > 1)
24360 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24361
b60c80d6 24362 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24363 memset (die, 0, sizeof (struct die_info));
24364 return (die);
24365}
2e276125
JB
24366
24367\f
a036ba48 24368
c90ec28a 24369/* Macro support. */
cf2c3c16 24370
9eac9650
TT
24371/* An overload of dwarf_decode_macros that finds the correct section
24372 and ensures it is read in before calling the other overload. */
24373
24374static void
24375dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24376 int section_is_gnu)
24377{
976ca316
SM
24378 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24379 struct objfile *objfile = per_objfile->objfile;
5a0e026f 24380 const struct line_header *lh = cu->line_header;
9eac9650
TT
24381 unsigned int offset_size = cu->header.offset_size;
24382 struct dwarf2_section_info *section;
24383 const char *section_name;
24384
24385 if (cu->dwo_unit != nullptr)
24386 {
24387 if (section_is_gnu)
24388 {
24389 section = &cu->dwo_unit->dwo_file->sections.macro;
24390 section_name = ".debug_macro.dwo";
24391 }
24392 else
24393 {
24394 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24395 section_name = ".debug_macinfo.dwo";
24396 }
24397 }
24398 else
24399 {
24400 if (section_is_gnu)
24401 {
976ca316 24402 section = &per_objfile->per_bfd->macro;
9eac9650
TT
24403 section_name = ".debug_macro";
24404 }
24405 else
24406 {
976ca316 24407 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
24408 section_name = ".debug_macinfo";
24409 }
24410 }
24411
24412 section->read (objfile);
24413 if (section->buffer == nullptr)
24414 {
24415 complaint (_("missing %s section"), section_name);
24416 return;
24417 }
24418
24419 buildsym_compunit *builder = cu->get_builder ();
24420
048fde1e 24421 struct dwarf2_section_info *str_offsets_section;
24422 struct dwarf2_section_info *str_section;
24423 ULONGEST str_offsets_base;
24424
24425 if (cu->dwo_unit != nullptr)
24426 {
24427 str_offsets_section = &cu->dwo_unit->dwo_file
24428 ->sections.str_offsets;
24429 str_section = &cu->dwo_unit->dwo_file->sections.str;
24430 str_offsets_base = cu->header.addr_size;
24431 }
24432 else
24433 {
24434 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24435 str_section = &per_objfile->per_bfd->str;
24436 str_offsets_base = *cu->str_offsets_base;
24437 }
24438
976ca316 24439 dwarf_decode_macros (per_objfile, builder, section, lh,
048fde1e 24440 offset_size, offset, str_section, str_offsets_section,
24441 str_offsets_base, section_is_gnu);
9eac9650
TT
24442}
24443
3019eac3
DE
24444/* Return the .debug_loc section to use for CU.
24445 For DWO files use .debug_loc.dwo. */
24446
24447static struct dwarf2_section_info *
24448cu_debug_loc_section (struct dwarf2_cu *cu)
24449{
976ca316 24450 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24451
3019eac3 24452 if (cu->dwo_unit)
43988095
JK
24453 {
24454 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24455
43988095
JK
24456 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24457 }
976ca316
SM
24458 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24459 : &per_objfile->per_bfd->loc);
3019eac3
DE
24460}
24461
d0ce17d8
CT
24462/* Return the .debug_rnglists section to use for CU. */
24463static struct dwarf2_section_info *
24464cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24465{
24466 if (cu->header.version < 5)
24467 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24468 cu->header.version);
24469 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24470
24471 /* Make sure we read the .debug_rnglists section from the file that
24472 contains the DW_AT_ranges attribute we are reading. Normally that
24473 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24474 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24475 program. */
24476 if (cu->dwo_unit != nullptr
24477 && tag != DW_TAG_compile_unit
24478 && tag != DW_TAG_skeleton_unit)
24479 {
24480 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24481
24482 if (sections->rnglists.size > 0)
24483 return &sections->rnglists;
24484 else
24485 error (_(".debug_rnglists section is missing from .dwo file."));
24486 }
24487 return &dwarf2_per_objfile->per_bfd->rnglists;
24488}
24489
8cf6f0b1
TT
24490/* A helper function that fills in a dwarf2_loclist_baton. */
24491
24492static void
24493fill_in_loclist_baton (struct dwarf2_cu *cu,
24494 struct dwarf2_loclist_baton *baton,
ff39bb5e 24495 const struct attribute *attr)
8cf6f0b1 24496{
976ca316 24497 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
24498 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24499
976ca316 24500 section->read (per_objfile->objfile);
8cf6f0b1 24501
976ca316 24502 baton->per_objfile = per_objfile;
8cf6f0b1
TT
24503 baton->per_cu = cu->per_cu;
24504 gdb_assert (baton->per_cu);
24505 /* We don't know how long the location list is, but make sure we
24506 don't run off the edge of the section. */
d4df075e
TT
24507 baton->size = section->size - attr->as_unsigned ();
24508 baton->data = section->buffer + attr->as_unsigned ();
2b24b6e4
TT
24509 if (cu->base_address.has_value ())
24510 baton->base_address = *cu->base_address;
24511 else
24512 baton->base_address = 0;
f664829e 24513 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24514}
24515
4c2df51b 24516static void
ff39bb5e 24517dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24518 struct dwarf2_cu *cu, int is_block)
4c2df51b 24519{
976ca316
SM
24520 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24521 struct objfile *objfile = per_objfile->objfile;
3019eac3 24522 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24523
cd6c91b4 24524 if (attr->form_is_section_offset ()
3019eac3 24525 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24526 the section. If so, fall through to the complaint in the
24527 other branch. */
d4df075e 24528 && attr->as_unsigned () < section->get_size (objfile))
4c2df51b 24529 {
0d53c4c4 24530 struct dwarf2_loclist_baton *baton;
4c2df51b 24531
8d749320 24532 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24533
8cf6f0b1 24534 fill_in_loclist_baton (cu, baton, attr);
be391dca 24535
2b24b6e4 24536 if (!cu->base_address.has_value ())
b98664d3 24537 complaint (_("Location list used without "
3e43a32a 24538 "specifying the CU base address."));
4c2df51b 24539
f1e6e072
TT
24540 SYMBOL_ACLASS_INDEX (sym) = (is_block
24541 ? dwarf2_loclist_block_index
24542 : dwarf2_loclist_index);
0d53c4c4
DJ
24543 SYMBOL_LOCATION_BATON (sym) = baton;
24544 }
24545 else
24546 {
24547 struct dwarf2_locexpr_baton *baton;
24548
8d749320 24549 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 24550 baton->per_objfile = per_objfile;
ae0d2f24
UW
24551 baton->per_cu = cu->per_cu;
24552 gdb_assert (baton->per_cu);
0d53c4c4 24553
4fc6c0d5 24554 if (attr->form_is_block ())
0d53c4c4
DJ
24555 {
24556 /* Note that we're just copying the block's data pointer
24557 here, not the actual data. We're still pointing into the
6502dd73
DJ
24558 info_buffer for SYM's objfile; right now we never release
24559 that buffer, but when we do clean up properly this may
24560 need to change. */
9d2246fc
TT
24561 struct dwarf_block *block = attr->as_block ();
24562 baton->size = block->size;
24563 baton->data = block->data;
0d53c4c4
DJ
24564 }
24565 else
24566 {
24567 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24568 sym->natural_name ());
0d53c4c4 24569 baton->size = 0;
0d53c4c4 24570 }
6e70227d 24571
f1e6e072
TT
24572 SYMBOL_ACLASS_INDEX (sym) = (is_block
24573 ? dwarf2_locexpr_block_index
24574 : dwarf2_locexpr_index);
0d53c4c4
DJ
24575 SYMBOL_LOCATION_BATON (sym) = baton;
24576 }
4c2df51b 24577}
6502dd73 24578
2e6a9f79 24579/* See read.h. */
96408a79 24580
2e6a9f79
SM
24581const comp_unit_head *
24582dwarf2_per_cu_data::get_header () const
96408a79 24583{
2e6a9f79
SM
24584 if (!m_header_read_in)
24585 {
24586 const gdb_byte *info_ptr
24587 = this->section->buffer + to_underlying (this->sect_off);
96408a79 24588
2e6a9f79 24589 memset (&m_header, 0, sizeof (m_header));
96408a79 24590
2e6a9f79
SM
24591 read_comp_unit_head (&m_header, info_ptr, this->section,
24592 rcuh_kind::COMPILE);
24593 }
96408a79 24594
2e6a9f79 24595 return &m_header;
96408a79
SA
24596}
24597
09ba997f 24598/* See read.h. */
ae0d2f24 24599
98714339 24600int
09ba997f 24601dwarf2_per_cu_data::addr_size () const
ae0d2f24 24602{
2e6a9f79 24603 return this->get_header ()->addr_size;
ae0d2f24
UW
24604}
24605
09ba997f 24606/* See read.h. */
9eae7c52
TT
24607
24608int
09ba997f 24609dwarf2_per_cu_data::offset_size () const
9eae7c52 24610{
2e6a9f79 24611 return this->get_header ()->offset_size;
96408a79
SA
24612}
24613
09ba997f 24614/* See read.h. */
96408a79
SA
24615
24616int
09ba997f 24617dwarf2_per_cu_data::ref_addr_size () const
96408a79 24618{
2e6a9f79 24619 const comp_unit_head *header = this->get_header ();
96408a79 24620
2e6a9f79
SM
24621 if (header->version == 2)
24622 return header->addr_size;
96408a79 24623 else
2e6a9f79 24624 return header->offset_size;
181cebd4
JK
24625}
24626
09ba997f 24627/* See read.h. */
9aa1f1e3 24628
09ba997f 24629struct type *
293e7e51 24630dwarf2_cu::addr_type () const
9a49df9d 24631{
293e7e51 24632 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
24633 struct type *void_type = objfile_type (objfile)->builtin_void;
24634 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 24635 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
24636
24637 if (TYPE_LENGTH (addr_type) == addr_size)
24638 return addr_type;
24639
c6d940a9 24640 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
9a49df9d
AB
24641 return addr_type;
24642}
24643
22b6cd70
TT
24644/* A helper function for dwarf2_find_containing_comp_unit that returns
24645 the index of the result, and that searches a vector. It will
24646 return a result even if the offset in question does not actually
24647 occur in any CU. This is separate so that it can be unit
24648 tested. */
ae038cb0 24649
22b6cd70
TT
24650static int
24651dwarf2_find_containing_comp_unit
24652 (sect_offset sect_off,
24653 unsigned int offset_in_dwz,
24654 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 24655{
ae038cb0
DJ
24656 int low, high;
24657
ae038cb0 24658 low = 0;
22b6cd70 24659 high = all_comp_units.size () - 1;
ae038cb0
DJ
24660 while (high > low)
24661 {
36586728 24662 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24663 int mid = low + (high - low) / 2;
9a619af0 24664
22b6cd70 24665 mid_cu = all_comp_units[mid];
36586728 24666 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24667 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24668 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24669 high = mid;
24670 else
24671 low = mid + 1;
24672 }
24673 gdb_assert (low == high);
22b6cd70
TT
24674 return low;
24675}
24676
24677/* Locate the .debug_info compilation unit from CU's objfile which contains
24678 the DIE at OFFSET. Raises an error on failure. */
24679
24680static struct dwarf2_per_cu_data *
24681dwarf2_find_containing_comp_unit (sect_offset sect_off,
24682 unsigned int offset_in_dwz,
976ca316 24683 dwarf2_per_objfile *per_objfile)
22b6cd70 24684{
976ca316
SM
24685 int low = dwarf2_find_containing_comp_unit
24686 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24687 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
22b6cd70 24688
45b8ae0c 24689 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24690 {
36586728 24691 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24692 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24693 "offset %s [in module %s]"),
24694 sect_offset_str (sect_off),
976ca316 24695 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 24696
976ca316 24697 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 24698 <= sect_off);
976ca316 24699 return per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
24700 }
24701 else
24702 {
976ca316 24703 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 24704 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24705 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24706 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24707 return this_cu;
24708 }
24709}
24710
22b6cd70
TT
24711#if GDB_SELF_TEST
24712
24713namespace selftests {
24714namespace find_containing_comp_unit {
24715
24716static void
24717run_test ()
24718{
24719 struct dwarf2_per_cu_data one {};
24720 struct dwarf2_per_cu_data two {};
24721 struct dwarf2_per_cu_data three {};
24722 struct dwarf2_per_cu_data four {};
24723
24724 one.length = 5;
24725 two.sect_off = sect_offset (one.length);
24726 two.length = 7;
24727
24728 three.length = 5;
24729 three.is_dwz = 1;
24730 four.sect_off = sect_offset (three.length);
24731 four.length = 7;
24732 four.is_dwz = 1;
24733
24734 std::vector<dwarf2_per_cu_data *> units;
24735 units.push_back (&one);
24736 units.push_back (&two);
24737 units.push_back (&three);
24738 units.push_back (&four);
24739
24740 int result;
24741
24742 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24743 SELF_CHECK (units[result] == &one);
24744 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24745 SELF_CHECK (units[result] == &one);
24746 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24747 SELF_CHECK (units[result] == &two);
24748
24749 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24750 SELF_CHECK (units[result] == &three);
24751 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24752 SELF_CHECK (units[result] == &three);
24753 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24754 SELF_CHECK (units[result] == &four);
24755}
24756
24757}
24758}
24759
24760#endif /* GDB_SELF_TEST */
24761
9e021579 24762/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 24763
9e021579
SM
24764dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24765 dwarf2_per_objfile *per_objfile)
24766 : per_cu (per_cu),
24767 per_objfile (per_objfile),
9068261f
AB
24768 mark (false),
24769 has_loclist (false),
24770 checked_producer (false),
24771 producer_is_gxx_lt_4_6 (false),
24772 producer_is_gcc_lt_4_3 (false),
eb77c9df 24773 producer_is_icc (false),
9068261f 24774 producer_is_icc_lt_14 (false),
c258c396 24775 producer_is_codewarrior (false),
9068261f 24776 processing_has_namespace_info (false)
93311388 24777{
9816fde3
JK
24778}
24779
24780/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24781
24782static void
95554aad
TT
24783prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24784 enum language pretend_language)
9816fde3
JK
24785{
24786 struct attribute *attr;
24787
24788 /* Set the language we're debugging. */
24789 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 24790 if (attr != nullptr)
529908cb 24791 set_cu_language (attr->constant_value (0), cu);
9816fde3 24792 else
9cded63f 24793 {
95554aad 24794 cu->language = pretend_language;
9cded63f
TT
24795 cu->language_defn = language_def (cu->language);
24796 }
dee91e82 24797
7d45c7c3 24798 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24799}
24800
7188ed02 24801/* See read.h. */
ae038cb0 24802
7188ed02
SM
24803dwarf2_cu *
24804dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24805{
7188ed02
SM
24806 auto it = m_dwarf2_cus.find (per_cu);
24807 if (it == m_dwarf2_cus.end ())
24808 return nullptr;
ae038cb0 24809
7188ed02
SM
24810 return it->second;
24811}
24812
24813/* See read.h. */
24814
24815void
24816dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24817{
24818 gdb_assert (this->get_cu (per_cu) == nullptr);
24819
24820 m_dwarf2_cus[per_cu] = cu;
24821}
24822
24823/* See read.h. */
24824
24825void
24826dwarf2_per_objfile::age_comp_units ()
24827{
24828 /* Start by clearing all marks. */
24829 for (auto pair : m_dwarf2_cus)
24830 pair.second->mark = false;
24831
24832 /* Traverse all CUs, mark them and their dependencies if used recently
24833 enough. */
24834 for (auto pair : m_dwarf2_cus)
ae038cb0 24835 {
7188ed02
SM
24836 dwarf2_cu *cu = pair.second;
24837
24838 cu->last_used++;
24839 if (cu->last_used <= dwarf_max_cache_age)
24840 dwarf2_mark (cu);
ae038cb0
DJ
24841 }
24842
7188ed02
SM
24843 /* Delete all CUs still not marked. */
24844 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 24845 {
7188ed02 24846 dwarf2_cu *cu = it->second;
ae038cb0 24847
7188ed02 24848 if (!cu->mark)
ae038cb0 24849 {
7188ed02
SM
24850 delete cu;
24851 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
24852 }
24853 else
7188ed02 24854 it++;
ae038cb0
DJ
24855 }
24856}
24857
7188ed02 24858/* See read.h. */
ae038cb0 24859
7188ed02
SM
24860void
24861dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24862{
7188ed02
SM
24863 auto it = m_dwarf2_cus.find (per_cu);
24864 if (it == m_dwarf2_cus.end ())
24865 return;
ae038cb0 24866
7188ed02 24867 delete it->second;
ae038cb0 24868
7188ed02
SM
24869 m_dwarf2_cus.erase (it);
24870}
ae038cb0 24871
7188ed02
SM
24872dwarf2_per_objfile::~dwarf2_per_objfile ()
24873{
24874 remove_all_cus ();
ae038cb0
DJ
24875}
24876
dee91e82
DE
24877/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24878 We store these in a hash table separate from the DIEs, and preserve them
24879 when the DIEs are flushed out of cache.
24880
24881 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 24882 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
24883 or the type may come from a DWO file. Furthermore, while it's more logical
24884 to use per_cu->section+offset, with Fission the section with the data is in
24885 the DWO file but we don't know that section at the point we need it.
24886 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24887 because we can enter the lookup routine, get_die_type_at_offset, from
24888 outside this file, and thus won't necessarily have PER_CU->cu.
24889 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 24890
dee91e82 24891struct dwarf2_per_cu_offset_and_type
1c379e20 24892{
dee91e82 24893 const struct dwarf2_per_cu_data *per_cu;
9c541725 24894 sect_offset sect_off;
1c379e20
DJ
24895 struct type *type;
24896};
24897
dee91e82 24898/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24899
24900static hashval_t
dee91e82 24901per_cu_offset_and_type_hash (const void *item)
1c379e20 24902{
9a3c8263
SM
24903 const struct dwarf2_per_cu_offset_and_type *ofs
24904 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 24905
9c541725 24906 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
24907}
24908
dee91e82 24909/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24910
24911static int
dee91e82 24912per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 24913{
9a3c8263
SM
24914 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24915 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24916 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24917 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 24918
dee91e82 24919 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 24920 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
24921}
24922
24923/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
24924 table if necessary. For convenience, return TYPE.
24925
24926 The DIEs reading must have careful ordering to:
85102364 24927 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
24928 reading current DIE.
24929 * Not trying to dereference contents of still incompletely read in types
24930 while reading in other DIEs.
24931 * Enable referencing still incompletely read in types just by a pointer to
24932 the type without accessing its fields.
24933
24934 Therefore caller should follow these rules:
24935 * Try to fetch any prerequisite types we may need to build this DIE type
24936 before building the type and calling set_die_type.
e71ec853 24937 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
24938 possible before fetching more types to complete the current type.
24939 * Make the type as complete as possible before fetching more types. */
1c379e20 24940
f792889a 24941static struct type *
57567375
TT
24942set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24943 bool skip_data_location)
1c379e20 24944{
976ca316 24945 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 24946 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 24947 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
24948 struct attribute *attr;
24949 struct dynamic_prop prop;
1c379e20 24950
b4ba55a1
JB
24951 /* For Ada types, make sure that the gnat-specific data is always
24952 initialized (if not already set). There are a few types where
24953 we should not be doing so, because the type-specific area is
24954 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24955 where the type-specific area is used to store the floatformat).
24956 But this is not a problem, because the gnat-specific information
24957 is actually not needed for these types. */
24958 if (need_gnat_info (cu)
78134374
SM
24959 && type->code () != TYPE_CODE_FUNC
24960 && type->code () != TYPE_CODE_FLT
24961 && type->code () != TYPE_CODE_METHODPTR
24962 && type->code () != TYPE_CODE_MEMBERPTR
24963 && type->code () != TYPE_CODE_METHOD
09584414 24964 && type->code () != TYPE_CODE_FIXED_POINT
b4ba55a1
JB
24965 && !HAVE_GNAT_AUX_INFO (type))
24966 INIT_GNAT_SPECIFIC (type);
24967
3f2f83dd
KB
24968 /* Read DW_AT_allocated and set in type. */
24969 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 24970 if (attr != NULL)
3f2f83dd 24971 {
293e7e51 24972 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 24973 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 24974 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 24975 }
3f2f83dd
KB
24976
24977 /* Read DW_AT_associated and set in type. */
24978 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 24979 if (attr != NULL)
3f2f83dd 24980 {
293e7e51 24981 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 24982 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 24983 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 24984 }
3f2f83dd 24985
3cdcd0ce 24986 /* Read DW_AT_data_location and set in type. */
57567375
TT
24987 if (!skip_data_location)
24988 {
24989 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24990 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24991 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24992 }
3cdcd0ce 24993
976ca316
SM
24994 if (per_objfile->die_type_hash == NULL)
24995 per_objfile->die_type_hash
0335378b
TT
24996 = htab_up (htab_create_alloc (127,
24997 per_cu_offset_and_type_hash,
24998 per_cu_offset_and_type_eq,
24999 NULL, xcalloc, xfree));
1c379e20 25000
dee91e82 25001 ofs.per_cu = cu->per_cu;
9c541725 25002 ofs.sect_off = die->sect_off;
1c379e20 25003 ofs.type = type;
dee91e82 25004 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 25005 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 25006 if (*slot)
b98664d3 25007 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25008 sect_offset_str (die->sect_off));
8d749320
SM
25009 *slot = XOBNEW (&objfile->objfile_obstack,
25010 struct dwarf2_per_cu_offset_and_type);
1c379e20 25011 **slot = ofs;
f792889a 25012 return type;
1c379e20
DJ
25013}
25014
9c541725 25015/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25016 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25017
25018static struct type *
9c541725 25019get_die_type_at_offset (sect_offset sect_off,
aa66c379 25020 dwarf2_per_cu_data *per_cu,
976ca316 25021 dwarf2_per_objfile *per_objfile)
1c379e20 25022{
dee91e82 25023 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 25024
976ca316 25025 if (per_objfile->die_type_hash == NULL)
f792889a 25026 return NULL;
1c379e20 25027
dee91e82 25028 ofs.per_cu = per_cu;
9c541725 25029 ofs.sect_off = sect_off;
9a3c8263 25030 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 25031 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
25032 if (slot)
25033 return slot->type;
25034 else
25035 return NULL;
25036}
25037
02142a6c 25038/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25039 or return NULL if DIE does not have a saved type. */
25040
25041static struct type *
25042get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25043{
aa66c379 25044 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
25045}
25046
10b3939b
DJ
25047/* Add a dependence relationship from CU to REF_PER_CU. */
25048
25049static void
25050dwarf2_add_dependence (struct dwarf2_cu *cu,
25051 struct dwarf2_per_cu_data *ref_per_cu)
25052{
25053 void **slot;
25054
25055 if (cu->dependencies == NULL)
25056 cu->dependencies
25057 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25058 NULL, &cu->comp_unit_obstack,
25059 hashtab_obstack_allocate,
25060 dummy_obstack_deallocate);
25061
25062 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25063 if (*slot == NULL)
25064 *slot = ref_per_cu;
25065}
1c379e20 25066
f504f079
DE
25067/* Subroutine of dwarf2_mark to pass to htab_traverse.
25068 Set the mark field in every compilation unit in the
7188ed02
SM
25069 cache that we must keep because we are keeping CU.
25070
25071 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 25072
10b3939b
DJ
25073static int
25074dwarf2_mark_helper (void **slot, void *data)
25075{
7188ed02
SM
25076 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25077 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25078 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
25079
25080 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25081 reading of the chain. As such dependencies remain valid it is not much
25082 useful to track and undo them during QUIT cleanups. */
7188ed02 25083 if (cu == nullptr)
d07ed419
JK
25084 return 1;
25085
7188ed02 25086 if (cu->mark)
10b3939b 25087 return 1;
10b3939b 25088
7188ed02
SM
25089 cu->mark = true;
25090
25091 if (cu->dependencies != nullptr)
25092 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
25093
25094 return 1;
25095}
25096
f504f079
DE
25097/* Set the mark field in CU and in every other compilation unit in the
25098 cache that we must keep because we are keeping CU. */
25099
ae038cb0
DJ
25100static void
25101dwarf2_mark (struct dwarf2_cu *cu)
25102{
25103 if (cu->mark)
25104 return;
7188ed02 25105
9068261f 25106 cu->mark = true;
ae038cb0 25107
7188ed02
SM
25108 if (cu->dependencies != nullptr)
25109 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
25110}
25111
72bf9492
DJ
25112/* Trivial hash function for partial_die_info: the hash value of a DIE
25113 is its offset in .debug_info for this objfile. */
25114
25115static hashval_t
25116partial_die_hash (const void *item)
25117{
9a3c8263
SM
25118 const struct partial_die_info *part_die
25119 = (const struct partial_die_info *) item;
9a619af0 25120
9c541725 25121 return to_underlying (part_die->sect_off);
72bf9492
DJ
25122}
25123
25124/* Trivial comparison function for partial_die_info structures: two DIEs
25125 are equal if they have the same offset. */
25126
25127static int
25128partial_die_eq (const void *item_lhs, const void *item_rhs)
25129{
9a3c8263
SM
25130 const struct partial_die_info *part_die_lhs
25131 = (const struct partial_die_info *) item_lhs;
25132 const struct partial_die_info *part_die_rhs
25133 = (const struct partial_die_info *) item_rhs;
9a619af0 25134
9c541725 25135 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25136}
25137
3c3bb058
AB
25138struct cmd_list_element *set_dwarf_cmdlist;
25139struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 25140
9291a0cd 25141static void
cd4fb1b2
SM
25142show_check_physname (struct ui_file *file, int from_tty,
25143 struct cmd_list_element *c, const char *value)
9291a0cd 25144{
cd4fb1b2
SM
25145 fprintf_filtered (file,
25146 _("Whether to check \"physname\" is %s.\n"),
25147 value);
9291a0cd
TT
25148}
25149
6c265988 25150void _initialize_dwarf2_read ();
cd4fb1b2 25151void
6c265988 25152_initialize_dwarf2_read ()
9291a0cd 25153{
0743fc83 25154 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 25155Set DWARF specific variables.\n\
590042fc 25156Configure DWARF variables such as the cache size."),
0743fc83
TT
25157 &set_dwarf_cmdlist, "maintenance set dwarf ",
25158 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25159
0743fc83 25160 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
25161Show DWARF specific variables.\n\
25162Show DWARF variables such as the cache size."),
0743fc83
TT
25163 &show_dwarf_cmdlist, "maintenance show dwarf ",
25164 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25165
cd4fb1b2
SM
25166 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25167 &dwarf_max_cache_age, _("\
25168Set the upper bound on the age of cached DWARF compilation units."), _("\
25169Show the upper bound on the age of cached DWARF compilation units."), _("\
25170A higher limit means that cached compilation units will be stored\n\
25171in memory longer, and more total memory will be used. Zero disables\n\
25172caching, which can slow down startup."),
25173 NULL,
25174 show_dwarf_max_cache_age,
25175 &set_dwarf_cmdlist,
25176 &show_dwarf_cmdlist);
156942c7 25177
cd4fb1b2
SM
25178 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25179Set debugging of the DWARF reader."), _("\
25180Show debugging of the DWARF reader."), _("\
25181When enabled (non-zero), debugging messages are printed during DWARF\n\
25182reading and symtab expansion. A value of 1 (one) provides basic\n\
25183information. A value greater than 1 provides more verbose information."),
25184 NULL,
25185 NULL,
25186 &setdebuglist, &showdebuglist);
9291a0cd 25187
cd4fb1b2
SM
25188 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25189Set debugging of the DWARF DIE reader."), _("\
25190Show debugging of the DWARF DIE reader."), _("\
25191When enabled (non-zero), DIEs are dumped after they are read in.\n\
25192The value is the maximum depth to print."),
25193 NULL,
25194 NULL,
25195 &setdebuglist, &showdebuglist);
9291a0cd 25196
cd4fb1b2
SM
25197 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25198Set debugging of the dwarf line reader."), _("\
25199Show debugging of the dwarf line reader."), _("\
25200When enabled (non-zero), line number entries are dumped as they are read in.\n\
25201A value of 1 (one) provides basic information.\n\
25202A value greater than 1 provides more verbose information."),
25203 NULL,
25204 NULL,
25205 &setdebuglist, &showdebuglist);
437afbb8 25206
cd4fb1b2
SM
25207 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25208Set cross-checking of \"physname\" code against demangler."), _("\
25209Show cross-checking of \"physname\" code against demangler."), _("\
25210When enabled, GDB's internal \"physname\" code is checked against\n\
25211the demangler."),
25212 NULL, show_check_physname,
25213 &setdebuglist, &showdebuglist);
900e11f9 25214
e615022a
DE
25215 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25216 no_class, &use_deprecated_index_sections, _("\
25217Set whether to use deprecated gdb_index sections."), _("\
25218Show whether to use deprecated gdb_index sections."), _("\
25219When enabled, deprecated .gdb_index sections are used anyway.\n\
25220Normally they are ignored either because of a missing feature or\n\
25221performance issue.\n\
25222Warning: This option must be enabled before gdb reads the file."),
25223 NULL,
25224 NULL,
25225 &setlist, &showlist);
25226
f1e6e072
TT
25227 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25228 &dwarf2_locexpr_funcs);
25229 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25230 &dwarf2_loclist_funcs);
25231
25232 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25233 &dwarf2_block_frame_base_locexpr_funcs);
25234 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25235 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25236
25237#if GDB_SELF_TEST
25238 selftests::register_test ("dw2_expand_symtabs_matching",
25239 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
25240 selftests::register_test ("dwarf2_find_containing_comp_unit",
25241 selftests::find_containing_comp_unit::run_test);
c62446b1 25242#endif
6502dd73 25243}
This page took 5.701431 seconds and 4 git commands to generate.