Set per_bfd->partial_symtabs earlier
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
3666a048 3 Copyright (C) 1994-2021 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
82ca8957 32#include "dwarf2/read.h"
3054dd54 33#include "dwarf2/abbrev.h"
162dce55 34#include "dwarf2/attribute.h"
4057dfde 35#include "dwarf2/comp-unit.h"
82ca8957
TT
36#include "dwarf2/index-cache.h"
37#include "dwarf2/index-common.h"
f4382c45 38#include "dwarf2/leb.h"
8fdd972c 39#include "dwarf2/line-header.h"
9fda78b6 40#include "dwarf2/dwz.h"
c90ec28a 41#include "dwarf2/macro.h"
c2d50fd0 42#include "dwarf2/die.h"
c2a62a3d 43#include "dwarf2/sect-names.h"
2b2558bf 44#include "dwarf2/stringify.h"
70182375 45#include "dwarf2/public.h"
4de283e4
TT
46#include "bfd.h"
47#include "elf-bfd.h"
48#include "symtab.h"
49#include "gdbtypes.h"
50#include "objfiles.h"
d55e5aa6 51#include "dwarf2.h"
4de283e4
TT
52#include "buildsym.h"
53#include "demangle.h"
54#include "gdb-demangle.h"
4de283e4 55#include "filenames.h" /* for DOSish file names */
4de283e4
TT
56#include "language.h"
57#include "complaints.h"
82ca8957
TT
58#include "dwarf2/expr.h"
59#include "dwarf2/loc.h"
4de283e4
TT
60#include "cp-support.h"
61#include "hashtab.h"
62#include "command.h"
d55e5aa6 63#include "gdbcmd.h"
4de283e4
TT
64#include "block.h"
65#include "addrmap.h"
66#include "typeprint.h"
67#include "psympriv.h"
4de283e4 68#include "c-lang.h"
d55e5aa6 69#include "go-lang.h"
4de283e4
TT
70#include "valprint.h"
71#include "gdbcore.h" /* for gnutarget */
72#include "gdb/gdb-index.h"
4de283e4
TT
73#include "gdb_bfd.h"
74#include "f-lang.h"
75#include "source.h"
4de283e4 76#include "build-id.h"
d55e5aa6 77#include "namespace.h"
268a13a5
TT
78#include "gdbsupport/function-view.h"
79#include "gdbsupport/gdb_optional.h"
80#include "gdbsupport/underlying.h"
268a13a5 81#include "gdbsupport/hash_enum.h"
4de283e4 82#include "filename-seen-cache.h"
b32b108a 83#include "producer.h"
4de283e4 84#include <fcntl.h>
4de283e4 85#include <algorithm>
4de283e4 86#include <unordered_map>
268a13a5 87#include "gdbsupport/selftest.h"
c9317f21 88#include "rust-lang.h"
268a13a5 89#include "gdbsupport/pathstuff.h"
edd45eb0 90#include "count-one-bits.h"
437afbb8 91
73be47f5
DE
92/* When == 1, print basic high level tracing messages.
93 When > 1, be more verbose.
b4f54984
DE
94 This is in contrast to the low level DIE reading of dwarf_die_debug. */
95static unsigned int dwarf_read_debug = 0;
45cfd468 96
6f738b01
SM
97/* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
98
99#define dwarf_read_debug_printf(fmt, ...) \
74b773fc
SM
100 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
101 ##__VA_ARGS__)
6f738b01
SM
102
103/* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
104
105#define dwarf_read_debug_printf_v(fmt, ...) \
74b773fc
SM
106 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
107 ##__VA_ARGS__)
6f738b01 108
d97bc12b 109/* When non-zero, dump DIEs after they are read in. */
b4f54984 110static unsigned int dwarf_die_debug = 0;
d97bc12b 111
27e0867f 112/* When non-zero, dump line number entries as they are read in. */
8fdd972c 113unsigned int dwarf_line_debug = 0;
27e0867f 114
491144b5
CB
115/* When true, cross-check physname against demangler. */
116static bool check_physname = false;
900e11f9 117
491144b5
CB
118/* When true, do not reject deprecated .gdb_index sections. */
119static bool use_deprecated_index_sections = false;
481860b3 120
17ee85fc
TT
121/* This is used to store the data that is always per objfile. */
122static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
123
124/* These are used to store the dwarf2_per_bfd objects.
125
126 objfiles having the same BFD, which doesn't require relocations, are going to
127 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
128
129 Other objfiles are not going to share a dwarf2_per_bfd with any other
130 objfiles, so they'll have their own version kept in the _objfile_data_key
131 version. */
132static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
133static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
6502dd73 134
f1e6e072
TT
135/* The "aclass" indices for various kinds of computed DWARF symbols. */
136
137static int dwarf2_locexpr_index;
138static int dwarf2_loclist_index;
139static int dwarf2_locexpr_block_index;
140static int dwarf2_loclist_block_index;
141
41144253 142/* Size of .debug_loclists section header for 32-bit DWARF format. */
143#define LOCLIST_HEADER_SIZE32 12
144
145/* Size of .debug_loclists section header for 64-bit DWARF format. */
146#define LOCLIST_HEADER_SIZE64 20
147
d0ce17d8
CT
148/* Size of .debug_rnglists section header for 32-bit DWARF format. */
149#define RNGLIST_HEADER_SIZE32 12
150
151/* Size of .debug_rnglists section header for 64-bit DWARF format. */
152#define RNGLIST_HEADER_SIZE64 20
153
3f563c84
PA
154/* An index into a (C++) symbol name component in a symbol name as
155 recorded in the mapped_index's symbol table. For each C++ symbol
156 in the symbol table, we record one entry for the start of each
157 component in the symbol in a table of name components, and then
158 sort the table, in order to be able to binary search symbol names,
159 ignoring leading namespaces, both completion and regular look up.
160 For example, for symbol "A::B::C", we'll have an entry that points
161 to "A::B::C", another that points to "B::C", and another for "C".
162 Note that function symbols in GDB index have no parameter
163 information, just the function/method names. You can convert a
164 name_component to a "const char *" using the
165 'mapped_index::symbol_name_at(offset_type)' method. */
166
167struct name_component
168{
169 /* Offset in the symbol name where the component starts. Stored as
170 a (32-bit) offset instead of a pointer to save memory and improve
171 locality on 64-bit architectures. */
172 offset_type name_offset;
173
174 /* The symbol's index in the symbol and constant pool tables of a
175 mapped_index. */
176 offset_type idx;
177};
178
44ed8f3e
PA
179/* Base class containing bits shared by both .gdb_index and
180 .debug_name indexes. */
181
182struct mapped_index_base
183{
22ca247e
TT
184 mapped_index_base () = default;
185 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
186
44ed8f3e
PA
187 /* The name_component table (a sorted vector). See name_component's
188 description above. */
189 std::vector<name_component> name_components;
190
191 /* How NAME_COMPONENTS is sorted. */
192 enum case_sensitivity name_components_casing;
193
194 /* Return the number of names in the symbol table. */
195 virtual size_t symbol_name_count () const = 0;
196
197 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
198 virtual const char *symbol_name_at
199 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
44ed8f3e
PA
200
201 /* Return whether the name at IDX in the symbol table should be
202 ignored. */
203 virtual bool symbol_name_slot_invalid (offset_type idx) const
204 {
205 return false;
206 }
207
208 /* Build the symbol name component sorted vector, if we haven't
209 yet. */
fcf23d5b 210 void build_name_components (dwarf2_per_objfile *per_objfile);
44ed8f3e
PA
211
212 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
213 possible matches for LN_NO_PARAMS in the name component
214 vector. */
215 std::pair<std::vector<name_component>::const_iterator,
216 std::vector<name_component>::const_iterator>
3b00ef10 217 find_name_components_bounds (const lookup_name_info &ln_no_params,
fcf23d5b
SM
218 enum language lang,
219 dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
220
221 /* Prevent deleting/destroying via a base class pointer. */
222protected:
223 ~mapped_index_base() = default;
224};
225
9291a0cd
TT
226/* A description of the mapped index. The file format is described in
227 a comment by the code that writes the index. */
fc898b42 228struct mapped_index final : public mapped_index_base
9291a0cd 229{
f00a2de2
PA
230 /* A slot/bucket in the symbol table hash. */
231 struct symbol_table_slot
232 {
233 const offset_type name;
234 const offset_type vec;
235 };
236
559a7a62 237 /* Index data format version. */
3063847f 238 int version = 0;
559a7a62 239
f00a2de2
PA
240 /* The address table data. */
241 gdb::array_view<const gdb_byte> address_table;
b11b1f88 242
3876f04e 243 /* The symbol table, implemented as a hash table. */
f00a2de2 244 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 245
9291a0cd 246 /* A pointer to the constant pool. */
3063847f 247 const char *constant_pool = nullptr;
3f563c84 248
44ed8f3e
PA
249 bool symbol_name_slot_invalid (offset_type idx) const override
250 {
251 const auto &bucket = this->symbol_table[idx];
9ab08412 252 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 253 }
5c58de74 254
3f563c84
PA
255 /* Convenience method to get at the name of the symbol at IDX in the
256 symbol table. */
fcf23d5b
SM
257 const char *symbol_name_at
258 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
f00a2de2 259 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 260
44ed8f3e
PA
261 size_t symbol_name_count () const override
262 { return this->symbol_table.size (); }
9291a0cd
TT
263};
264
927aa2e7
JK
265/* A description of the mapped .debug_names.
266 Uninitialized map has CU_COUNT 0. */
fc898b42 267struct mapped_debug_names final : public mapped_index_base
927aa2e7
JK
268{
269 bfd_endian dwarf5_byte_order;
270 bool dwarf5_is_dwarf64;
271 bool augmentation_is_gdb;
272 uint8_t offset_size;
273 uint32_t cu_count = 0;
274 uint32_t tu_count, bucket_count, name_count;
275 const gdb_byte *cu_table_reordered, *tu_table_reordered;
276 const uint32_t *bucket_table_reordered, *hash_table_reordered;
277 const gdb_byte *name_table_string_offs_reordered;
278 const gdb_byte *name_table_entry_offs_reordered;
279 const gdb_byte *entry_pool;
280
281 struct index_val
282 {
283 ULONGEST dwarf_tag;
284 struct attr
285 {
286 /* Attribute name DW_IDX_*. */
287 ULONGEST dw_idx;
288
289 /* Attribute form DW_FORM_*. */
290 ULONGEST form;
291
292 /* Value if FORM is DW_FORM_implicit_const. */
293 LONGEST implicit_const;
294 };
295 std::vector<attr> attr_vec;
296 };
297
298 std::unordered_map<ULONGEST, index_val> abbrev_map;
299
fcf23d5b
SM
300 const char *namei_to_name
301 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
302
303 /* Implementation of the mapped_index_base virtual interface, for
304 the name_components cache. */
305
fcf23d5b
SM
306 const char *symbol_name_at
307 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
308 { return namei_to_name (idx, per_objfile); }
44ed8f3e
PA
309
310 size_t symbol_name_count () const override
311 { return this->name_count; }
927aa2e7
JK
312};
313
cd4fb1b2 314/* See dwarf2read.h. */
ed2dc618 315
cd4fb1b2 316dwarf2_per_objfile *
ed2dc618
SM
317get_dwarf2_per_objfile (struct objfile *objfile)
318{
5bfd760d 319 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 320}
c906108c 321
251d32d9 322/* Default names of the debugging sections. */
c906108c 323
233a11ab
CS
324/* Note that if the debugging section has been compressed, it might
325 have a name like .zdebug_info. */
326
9938d15a 327const struct dwarf2_debug_sections dwarf2_elf_names =
9cdd5dbd 328{
251d32d9
TG
329 { ".debug_info", ".zdebug_info" },
330 { ".debug_abbrev", ".zdebug_abbrev" },
331 { ".debug_line", ".zdebug_line" },
332 { ".debug_loc", ".zdebug_loc" },
43988095 333 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 334 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 335 { ".debug_macro", ".zdebug_macro" },
251d32d9 336 { ".debug_str", ".zdebug_str" },
18a8505e 337 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 338 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 339 { ".debug_ranges", ".zdebug_ranges" },
43988095 340 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 341 { ".debug_types", ".zdebug_types" },
3019eac3 342 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
343 { ".debug_frame", ".zdebug_frame" },
344 { ".eh_frame", NULL },
24d3216f 345 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
346 { ".debug_names", ".zdebug_names" },
347 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 348 23
251d32d9 349};
c906108c 350
80626a55 351/* List of DWO/DWP sections. */
3019eac3 352
80626a55 353static const struct dwop_section_names
3019eac3
DE
354{
355 struct dwarf2_section_names abbrev_dwo;
356 struct dwarf2_section_names info_dwo;
357 struct dwarf2_section_names line_dwo;
358 struct dwarf2_section_names loc_dwo;
43988095 359 struct dwarf2_section_names loclists_dwo;
09262596
DE
360 struct dwarf2_section_names macinfo_dwo;
361 struct dwarf2_section_names macro_dwo;
d0ce17d8 362 struct dwarf2_section_names rnglists_dwo;
3019eac3
DE
363 struct dwarf2_section_names str_dwo;
364 struct dwarf2_section_names str_offsets_dwo;
365 struct dwarf2_section_names types_dwo;
80626a55
DE
366 struct dwarf2_section_names cu_index;
367 struct dwarf2_section_names tu_index;
3019eac3 368}
80626a55 369dwop_section_names =
3019eac3
DE
370{
371 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
372 { ".debug_info.dwo", ".zdebug_info.dwo" },
373 { ".debug_line.dwo", ".zdebug_line.dwo" },
374 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 375 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
376 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
377 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
d0ce17d8 378 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
3019eac3
DE
379 { ".debug_str.dwo", ".zdebug_str.dwo" },
380 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
381 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
382 { ".debug_cu_index", ".zdebug_cu_index" },
383 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
384};
385
c906108c
SS
386/* local data types */
387
d0ce17d8
CT
388/* The location list and range list sections (.debug_loclists & .debug_rnglists)
389 begin with a header, which contains the following information. */
390struct loclists_rnglists_header
41144253 391{
392 /* A 4-byte or 12-byte length containing the length of the
393 set of entries for this compilation unit, not including the
394 length field itself. */
395 unsigned int length;
396
397 /* A 2-byte version identifier. */
398 short version;
399
400 /* A 1-byte unsigned integer containing the size in bytes of an address on
401 the target system. */
402 unsigned char addr_size;
403
404 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
405 on the target system. */
406 unsigned char segment_collector_size;
407
408 /* A 4-byte count of the number of offsets that follow the header. */
409 unsigned int offset_entry_count;
410};
411
3da10d80
KS
412/* Type used for delaying computation of method physnames.
413 See comments for compute_delayed_physnames. */
414struct delayed_method_info
415{
416 /* The type to which the method is attached, i.e., its parent class. */
417 struct type *type;
418
419 /* The index of the method in the type's function fieldlists. */
420 int fnfield_index;
421
422 /* The index of the method in the fieldlist. */
423 int index;
424
425 /* The name of the DIE. */
426 const char *name;
427
428 /* The DIE associated with this method. */
429 struct die_info *die;
430};
431
e7c27a73
DJ
432/* Internal state when decoding a particular compilation unit. */
433struct dwarf2_cu
434{
9e021579
SM
435 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
436 dwarf2_per_objfile *per_objfile);
fcd3b13d
SM
437
438 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
439
c24bdb02
KS
440 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
441 Create the set of symtabs used by this TU, or if this TU is sharing
442 symtabs with another TU and the symtabs have already been created
443 then restore those symtabs in the line header.
444 We don't need the pc/line-number mapping for type units. */
445 void setup_type_unit_groups (struct die_info *die);
446
447 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
448 buildsym_compunit constructor. */
449 struct compunit_symtab *start_symtab (const char *name,
450 const char *comp_dir,
451 CORE_ADDR low_pc);
452
453 /* Reset the builder. */
454 void reset_builder () { m_builder.reset (); }
455
293e7e51
SM
456 /* Return a type that is a generic pointer type, the size of which
457 matches the address size given in the compilation unit header for
458 this CU. */
459 struct type *addr_type () const;
460
461 /* Find an integer type the same size as the address size given in
462 the compilation unit header for this CU. UNSIGNED_P controls if
463 the integer is unsigned or not. */
464 struct type *addr_sized_int_type (bool unsigned_p) const;
465
d00adf39 466 /* The header of the compilation unit. */
fcd3b13d 467 struct comp_unit_head header {};
e142c38c 468
d00adf39 469 /* Base address of this compilation unit. */
2b24b6e4 470 gdb::optional<CORE_ADDR> base_address;
d00adf39 471
e142c38c 472 /* The language we are debugging. */
fcd3b13d
SM
473 enum language language = language_unknown;
474 const struct language_defn *language_defn = nullptr;
e142c38c 475
fcd3b13d 476 const char *producer = nullptr;
b0f35d58 477
c24bdb02 478private:
804d2729
TT
479 /* The symtab builder for this CU. This is only non-NULL when full
480 symbols are being read. */
c24bdb02 481 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 482
c24bdb02 483public:
e142c38c
DJ
484 /* The generic symbol table building routines have separate lists for
485 file scope symbols and all all other scopes (local scopes). So
486 we need to select the right one to pass to add_symbol_to_list().
487 We do it by keeping a pointer to the correct list in list_in_scope.
488
489 FIXME: The original dwarf code just treated the file scope as the
490 first local scope, and all other local scopes as nested local
491 scopes, and worked fine. Check to see if we really need to
492 distinguish these in buildsym.c. */
fcd3b13d 493 struct pending **list_in_scope = nullptr;
e142c38c 494
b64f50a1
JK
495 /* Hash table holding all the loaded partial DIEs
496 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 497 htab_t partial_dies = nullptr;
72bf9492
DJ
498
499 /* Storage for things with the same lifetime as this read-in compilation
500 unit, including partial DIEs. */
fcd3b13d 501 auto_obstack comp_unit_obstack;
72bf9492 502
69d751e3 503 /* Backlink to our per_cu entry. */
ae038cb0
DJ
504 struct dwarf2_per_cu_data *per_cu;
505
9e021579 506 /* The dwarf2_per_objfile that owns this. */
976ca316 507 dwarf2_per_objfile *per_objfile;
9e021579 508
ae038cb0 509 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 510 int last_used = 0;
ae038cb0 511
b64f50a1
JK
512 /* A hash table of DIE cu_offset for following references with
513 die_info->offset.sect_off as hash. */
fcd3b13d 514 htab_t die_hash = nullptr;
10b3939b
DJ
515
516 /* Full DIEs if read in. */
fcd3b13d 517 struct die_info *dies = nullptr;
10b3939b
DJ
518
519 /* A set of pointers to dwarf2_per_cu_data objects for compilation
520 units referenced by this one. Only set during full symbol processing;
521 partial symbol tables do not have dependencies. */
fcd3b13d 522 htab_t dependencies = nullptr;
10b3939b 523
cb1df416 524 /* Header data from the line table, during full symbol processing. */
fcd3b13d 525 struct line_header *line_header = nullptr;
4c8aa72d 526 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
5989a64e 527 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
4c8aa72d
PA
528 this is the DW_TAG_compile_unit die for this CU. We'll hold on
529 to the line header as long as this DIE is being processed. See
530 process_die_scope. */
fcd3b13d 531 die_info *line_header_die_owner = nullptr;
cb1df416 532
3da10d80
KS
533 /* A list of methods which need to have physnames computed
534 after all type information has been read. */
c89b44cd 535 std::vector<delayed_method_info> method_list;
3da10d80 536
96408a79 537 /* To be copied to symtab->call_site_htab. */
fcd3b13d 538 htab_t call_site_htab = nullptr;
96408a79 539
034e5797
DE
540 /* Non-NULL if this CU came from a DWO file.
541 There is an invariant here that is important to remember:
542 Except for attributes copied from the top level DIE in the "main"
543 (or "stub") file in preparation for reading the DWO file
18a8505e 544 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
545 Either there isn't a DWO file (in which case this is NULL and the point
546 is moot), or there is and either we're not going to read it (in which
547 case this is NULL) or there is and we are reading it (in which case this
548 is non-NULL). */
fcd3b13d 549 struct dwo_unit *dwo_unit = nullptr;
3019eac3 550
18a8505e 551 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 552 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 553 gdb::optional<ULONGEST> addr_base;
3019eac3 554
2b0c7f41
SM
555 /* The DW_AT_GNU_ranges_base attribute, if present.
556
557 This is only relevant in the context of pre-DWARF 5 split units. In this
558 context, there is a .debug_ranges section in the linked executable,
559 containing all the ranges data for all the compilation units. Each
560 skeleton/stub unit has (if needed) a DW_AT_GNU_ranges_base attribute that
561 indicates the base of its contribution to that section. The DW_AT_ranges
562 attributes in the split-unit are of the form DW_FORM_sec_offset and point
563 into the .debug_ranges section of the linked file. However, they are not
564 "true" DW_FORM_sec_offset, because they are relative to the base of their
565 compilation unit's contribution, rather than relative to the beginning of
566 the section. The DW_AT_GNU_ranges_base value must be added to it to make
567 it relative to the beginning of the section.
568
569 Note that the value is zero when we are not in a pre-DWARF 5 split-unit
570 case, so this value can be added without needing to know whether we are in
571 this case or not.
572
573 N.B. If a DW_AT_ranges attribute is found on the DW_TAG_compile_unit in the
574 skeleton/stub, it must not have the base added, as it already points to the
575 right place. And since the DW_TAG_compile_unit DIE in the split-unit can't
576 have a DW_AT_ranges attribute, we can use the
577
578 die->tag != DW_AT_compile_unit
579
580 to determine whether the base should be added or not. */
581 ULONGEST gnu_ranges_base = 0;
582
583 /* The DW_AT_rnglists_base attribute, if present.
584
585 This is used when processing attributes of form DW_FORM_rnglistx in
586 non-split units. Attributes of this form found in a split unit don't
587 use it, as split-unit files have their own non-shared .debug_rnglists.dwo
588 section. */
589 ULONGEST rnglists_base = 0;
2e3cf129 590
41144253 591 /* The DW_AT_loclists_base attribute if present. */
592 ULONGEST loclist_base = 0;
593
c9317f21
TT
594 /* When reading debug info generated by older versions of rustc, we
595 have to rewrite some union types to be struct types with a
596 variant part. This rewriting must be done after the CU is fully
597 read in, because otherwise at the point of rewriting some struct
598 type might not have been fully processed. So, we keep a list of
599 all such types here and process them after expansion. */
600 std::vector<struct type *> rust_unions;
601
18a8505e
AT
602 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
603 files, the value is implicitly zero. For DWARF 5 version DWO files, the
604 value is often implicit and is the size of the header of
605 .debug_str_offsets section (8 or 4, depending on the address size). */
606 gdb::optional<ULONGEST> str_offsets_base;
607
ae038cb0 608 /* Mark used when releasing cached dies. */
9068261f 609 bool mark : 1;
ae038cb0 610
8be455d7
JK
611 /* This CU references .debug_loc. See the symtab->locations_valid field.
612 This test is imperfect as there may exist optimized debug code not using
613 any location list and still facing inlining issues if handled as
614 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 615 bool has_loclist : 1;
ba919b58 616
9068261f 617 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
618 if all the producer_is_* fields are valid. This information is cached
619 because profiling CU expansion showed excessive time spent in
620 producer_is_gxx_lt_4_6. */
9068261f
AB
621 bool checked_producer : 1;
622 bool producer_is_gxx_lt_4_6 : 1;
623 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 624 bool producer_is_icc : 1;
9068261f 625 bool producer_is_icc_lt_14 : 1;
c258c396 626 bool producer_is_codewarrior : 1;
4d4ec4e5 627
9068261f 628 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
629 debugging info for C++ namespaces. GCC 3.3.x did not produce
630 this information, but later versions do. */
631
9068261f 632 bool processing_has_namespace_info : 1;
d590ff25
YQ
633
634 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
635
636 /* If this CU was inherited by another CU (via specification,
637 abstract_origin, etc), this is the ancestor CU. */
638 dwarf2_cu *ancestor;
639
640 /* Get the buildsym_compunit for this CU. */
641 buildsym_compunit *get_builder ()
642 {
643 /* If this CU has a builder associated with it, use that. */
644 if (m_builder != nullptr)
645 return m_builder.get ();
646
647 /* Otherwise, search ancestors for a valid builder. */
648 if (ancestor != nullptr)
649 return ancestor->get_builder ();
650
651 return nullptr;
652 }
e7c27a73
DJ
653};
654
094b34ac
DE
655/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
656 This includes type_unit_group and quick_file_names. */
657
658struct stmt_list_hash
659{
660 /* The DWO unit this table is from or NULL if there is none. */
661 struct dwo_unit *dwo_unit;
662
663 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 664 sect_offset line_sect_off;
094b34ac
DE
665};
666
5989a64e 667/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
8adb8487
TT
668 an object of this type. This contains elements of type unit groups
669 that can be shared across objfiles. The non-shareable parts are in
670 type_unit_group_unshareable. */
f4dc4d17
DE
671
672struct type_unit_group
673{
0186c6a7 674 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
675 To simplify things we create an artificial CU that "includes" all the
676 type units using this stmt_list so that the rest of the code still has
197400e8 677 a "per_cu" handle on the symtab. */
094b34ac
DE
678 struct dwarf2_per_cu_data per_cu;
679
0186c6a7
DE
680 /* The TUs that share this DW_AT_stmt_list entry.
681 This is added to while parsing type units to build partial symtabs,
682 and is deleted afterwards and not used again. */
a8b3b8e9 683 std::vector<signatured_type *> *tus;
f4dc4d17 684
094b34ac
DE
685 /* The data used to construct the hash key. */
686 struct stmt_list_hash hash;
f4dc4d17
DE
687};
688
73869dc2 689/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
690
691struct dwo_sections
692{
693 struct dwarf2_section_info abbrev;
3019eac3
DE
694 struct dwarf2_section_info line;
695 struct dwarf2_section_info loc;
43988095 696 struct dwarf2_section_info loclists;
09262596
DE
697 struct dwarf2_section_info macinfo;
698 struct dwarf2_section_info macro;
d0ce17d8 699 struct dwarf2_section_info rnglists;
3019eac3
DE
700 struct dwarf2_section_info str;
701 struct dwarf2_section_info str_offsets;
80626a55
DE
702 /* In the case of a virtual DWO file, these two are unused. */
703 struct dwarf2_section_info info;
fd5866f6 704 std::vector<dwarf2_section_info> types;
3019eac3
DE
705};
706
c88ee1f0 707/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
708
709struct dwo_unit
710{
711 /* Backlink to the containing struct dwo_file. */
712 struct dwo_file *dwo_file;
713
714 /* The "id" that distinguishes this CU/TU.
715 .debug_info calls this "dwo_id", .debug_types calls this "signature".
716 Since signatures came first, we stick with it for consistency. */
717 ULONGEST signature;
718
719 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 720 struct dwarf2_section_info *section;
3019eac3 721
9c541725
PA
722 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
723 sect_offset sect_off;
3019eac3
DE
724 unsigned int length;
725
726 /* For types, offset in the type's DIE of the type defined by this TU. */
727 cu_offset type_offset_in_tu;
728};
729
73869dc2
DE
730/* include/dwarf2.h defines the DWP section codes.
731 It defines a max value but it doesn't define a min value, which we
732 use for error checking, so provide one. */
733
734enum dwp_v2_section_ids
735{
736 DW_SECT_MIN = 1
737};
738
80626a55 739/* Data for one DWO file.
57d63ce2
DE
740
741 This includes virtual DWO files (a virtual DWO file is a DWO file as it
742 appears in a DWP file). DWP files don't really have DWO files per se -
743 comdat folding of types "loses" the DWO file they came from, and from
744 a high level view DWP files appear to contain a mass of random types.
745 However, to maintain consistency with the non-DWP case we pretend DWP
746 files contain virtual DWO files, and we assign each TU with one virtual
747 DWO file (generally based on the line and abbrev section offsets -
748 a heuristic that seems to work in practice). */
3019eac3
DE
749
750struct dwo_file
751{
51ac9db5
SM
752 dwo_file () = default;
753 DISABLE_COPY_AND_ASSIGN (dwo_file);
754
18a8505e 755 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
756 For virtual DWO files the name is constructed from the section offsets
757 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
758 from related CU+TUs. */
51ac9db5 759 const char *dwo_name = nullptr;
0ac5b59e
DE
760
761 /* The DW_AT_comp_dir attribute. */
51ac9db5 762 const char *comp_dir = nullptr;
3019eac3 763
80626a55
DE
764 /* The bfd, when the file is open. Otherwise this is NULL.
765 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 766 gdb_bfd_ref_ptr dbfd;
3019eac3 767
73869dc2 768 /* The sections that make up this DWO file.
d2854d8d 769 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
73869dc2 770 sections (for lack of a better name). */
51ac9db5 771 struct dwo_sections sections {};
3019eac3 772
33c5cd75
DB
773 /* The CUs in the file.
774 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
775 an extension to handle LLVM's Link Time Optimization output (where
776 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 777 htab_up cus;
3019eac3
DE
778
779 /* Table of TUs in the file.
780 Each element is a struct dwo_unit. */
b0b6a987 781 htab_up tus;
3019eac3
DE
782};
783
80626a55
DE
784/* These sections are what may appear in a DWP file. */
785
786struct dwp_sections
787{
d2854d8d 788 /* These are used by all DWP versions (1, 2 and 5). */
80626a55
DE
789 struct dwarf2_section_info str;
790 struct dwarf2_section_info cu_index;
791 struct dwarf2_section_info tu_index;
73869dc2 792
d2854d8d 793 /* These are only used by DWP version 2 and version 5 files.
73869dc2
DE
794 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
795 sections are referenced by section number, and are not recorded here.
d2854d8d
CT
796 In DWP version 2 or 5 there is at most one copy of all these sections,
797 each section being (effectively) comprised of the concatenation of all of
798 the individual sections that exist in the version 1 format.
73869dc2
DE
799 To keep the code simple we treat each of these concatenated pieces as a
800 section itself (a virtual section?). */
801 struct dwarf2_section_info abbrev;
802 struct dwarf2_section_info info;
803 struct dwarf2_section_info line;
804 struct dwarf2_section_info loc;
d2854d8d 805 struct dwarf2_section_info loclists;
73869dc2
DE
806 struct dwarf2_section_info macinfo;
807 struct dwarf2_section_info macro;
d2854d8d 808 struct dwarf2_section_info rnglists;
73869dc2
DE
809 struct dwarf2_section_info str_offsets;
810 struct dwarf2_section_info types;
80626a55
DE
811};
812
73869dc2
DE
813/* These sections are what may appear in a virtual DWO file in DWP version 1.
814 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 815
73869dc2 816struct virtual_v1_dwo_sections
80626a55
DE
817{
818 struct dwarf2_section_info abbrev;
819 struct dwarf2_section_info line;
820 struct dwarf2_section_info loc;
821 struct dwarf2_section_info macinfo;
822 struct dwarf2_section_info macro;
823 struct dwarf2_section_info str_offsets;
824 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 825 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
826 struct dwarf2_section_info info_or_types;
827};
828
d2854d8d 829/* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
73869dc2
DE
830 In version 2, the sections of the DWO files are concatenated together
831 and stored in one section of that name. Thus each ELF section contains
832 several "virtual" sections. */
833
d2854d8d 834struct virtual_v2_or_v5_dwo_sections
73869dc2
DE
835{
836 bfd_size_type abbrev_offset;
837 bfd_size_type abbrev_size;
838
839 bfd_size_type line_offset;
840 bfd_size_type line_size;
841
842 bfd_size_type loc_offset;
843 bfd_size_type loc_size;
844
d2854d8d
CT
845 bfd_size_type loclists_offset;
846 bfd_size_type loclists_size;
847
73869dc2
DE
848 bfd_size_type macinfo_offset;
849 bfd_size_type macinfo_size;
850
851 bfd_size_type macro_offset;
852 bfd_size_type macro_size;
853
d2854d8d
CT
854 bfd_size_type rnglists_offset;
855 bfd_size_type rnglists_size;
856
73869dc2
DE
857 bfd_size_type str_offsets_offset;
858 bfd_size_type str_offsets_size;
859
860 /* Each DWP hash table entry records one CU or one TU.
861 That is recorded here, and copied to dwo_unit.section. */
862 bfd_size_type info_or_types_offset;
863 bfd_size_type info_or_types_size;
864};
865
80626a55
DE
866/* Contents of DWP hash tables. */
867
868struct dwp_hash_table
869{
73869dc2 870 uint32_t version, nr_columns;
80626a55 871 uint32_t nr_units, nr_slots;
73869dc2
DE
872 const gdb_byte *hash_table, *unit_table;
873 union
874 {
875 struct
876 {
877 const gdb_byte *indices;
878 } v1;
879 struct
880 {
881 /* This is indexed by column number and gives the id of the section
882 in that column. */
883#define MAX_NR_V2_DWO_SECTIONS \
884 (1 /* .debug_info or .debug_types */ \
885 + 1 /* .debug_abbrev */ \
886 + 1 /* .debug_line */ \
887 + 1 /* .debug_loc */ \
888 + 1 /* .debug_str_offsets */ \
889 + 1 /* .debug_macro or .debug_macinfo */)
890 int section_ids[MAX_NR_V2_DWO_SECTIONS];
891 const gdb_byte *offsets;
892 const gdb_byte *sizes;
893 } v2;
d2854d8d
CT
894 struct
895 {
896 /* This is indexed by column number and gives the id of the section
897 in that column. */
898#define MAX_NR_V5_DWO_SECTIONS \
899 (1 /* .debug_info */ \
900 + 1 /* .debug_abbrev */ \
901 + 1 /* .debug_line */ \
902 + 1 /* .debug_loclists */ \
903 + 1 /* .debug_str_offsets */ \
904 + 1 /* .debug_macro */ \
905 + 1 /* .debug_rnglists */)
906 int section_ids[MAX_NR_V5_DWO_SECTIONS];
907 const gdb_byte *offsets;
908 const gdb_byte *sizes;
909 } v5;
73869dc2 910 } section_pool;
80626a55
DE
911};
912
913/* Data for one DWP file. */
914
915struct dwp_file
916{
400174b1
TT
917 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
918 : name (name_),
919 dbfd (std::move (abfd))
920 {
921 }
922
80626a55
DE
923 /* Name of the file. */
924 const char *name;
925
73869dc2 926 /* File format version. */
400174b1 927 int version = 0;
73869dc2 928
93417882 929 /* The bfd. */
400174b1 930 gdb_bfd_ref_ptr dbfd;
80626a55
DE
931
932 /* Section info for this file. */
400174b1 933 struct dwp_sections sections {};
80626a55 934
57d63ce2 935 /* Table of CUs in the file. */
400174b1 936 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
937
938 /* Table of TUs in the file. */
400174b1 939 const struct dwp_hash_table *tus = nullptr;
80626a55 940
19ac8c2e 941 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
942 htab_up loaded_cus;
943 htab_up loaded_tus;
80626a55 944
73869dc2
DE
945 /* Table to map ELF section numbers to their sections.
946 This is only needed for the DWP V1 file format. */
400174b1
TT
947 unsigned int num_sections = 0;
948 asection **elf_sections = nullptr;
80626a55
DE
949};
950
0963b4bd
MS
951/* Struct used to pass misc. parameters to read_die_and_children, et
952 al. which are used for both .debug_info and .debug_types dies.
953 All parameters here are unchanging for the life of the call. This
dee91e82 954 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
955
956struct die_reader_specs
957{
a32a8923 958 /* The bfd of die_section. */
0280fdcc 959 bfd *abfd;
93311388
DE
960
961 /* The CU of the DIE we are parsing. */
962 struct dwarf2_cu *cu;
963
80626a55 964 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
965 struct dwo_file *dwo_file;
966
dee91e82 967 /* The section the die comes from.
3019eac3 968 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
969 struct dwarf2_section_info *die_section;
970
971 /* die_section->buffer. */
d521ce57 972 const gdb_byte *buffer;
f664829e
DE
973
974 /* The end of the buffer. */
975 const gdb_byte *buffer_end;
a2ce51a0 976
685af9cd
TT
977 /* The abbreviation table to use when reading the DIEs. */
978 struct abbrev_table *abbrev_table;
93311388
DE
979};
980
c0ab21c2
TT
981/* A subclass of die_reader_specs that holds storage and has complex
982 constructor and destructor behavior. */
983
984class cutu_reader : public die_reader_specs
985{
986public:
987
ab432490
SM
988 cutu_reader (dwarf2_per_cu_data *this_cu,
989 dwarf2_per_objfile *per_objfile,
c0ab21c2 990 struct abbrev_table *abbrev_table,
2e671100 991 dwarf2_cu *existing_cu,
c0ab21c2
TT
992 bool skip_partial);
993
994 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 995 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
996 struct dwarf2_cu *parent_cu = nullptr,
997 struct dwo_file *dwo_file = nullptr);
998
c0ab21c2
TT
999 DISABLE_COPY_AND_ASSIGN (cutu_reader);
1000
1001 const gdb_byte *info_ptr = nullptr;
1002 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
1003 bool dummy_p = false;
1004
6751ebae
TT
1005 /* Release the new CU, putting it on the chain. This cannot be done
1006 for dummy CUs. */
1007 void keep ();
1008
c0ab21c2 1009private:
9e021579
SM
1010 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
1011 dwarf2_per_objfile *per_objfile,
2e671100 1012 dwarf2_cu *existing_cu);
c0ab21c2
TT
1013
1014 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
1015 std::unique_ptr<dwarf2_cu> m_new_cu;
1016
1017 /* The ordinary abbreviation table. */
1018 abbrev_table_up m_abbrev_table_holder;
1019
1020 /* The DWO abbreviation table. */
1021 abbrev_table_up m_dwo_abbrev_table;
1022};
dee91e82 1023
c906108c 1024/* When we construct a partial symbol table entry we only
0963b4bd 1025 need this much information. */
6f06d47b 1026struct partial_die_info : public allocate_on_obstack
c906108c 1027 {
7c32eebb 1028 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
6f06d47b
YQ
1029
1030 /* Disable assign but still keep copy ctor, which is needed
1031 load_partial_dies. */
1032 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1033
52356b79
YQ
1034 /* Adjust the partial die before generating a symbol for it. This
1035 function may set the is_external flag or change the DIE's
1036 name. */
1037 void fixup (struct dwarf2_cu *cu);
1038
48fbe735
YQ
1039 /* Read a minimal amount of information into the minimal die
1040 structure. */
1041 const gdb_byte *read (const struct die_reader_specs *reader,
1042 const struct abbrev_info &abbrev,
1043 const gdb_byte *info_ptr);
1044
7d00ffec
TT
1045 /* Compute the name of this partial DIE. This memoizes the
1046 result, so it is safe to call multiple times. */
1047 const char *name (dwarf2_cu *cu);
1048
72bf9492 1049 /* Offset of this DIE. */
6f06d47b 1050 const sect_offset sect_off;
72bf9492
DJ
1051
1052 /* DWARF-2 tag for this DIE. */
6f06d47b 1053 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1054
72bf9492 1055 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1056 const unsigned int has_children : 1;
1057
72bf9492
DJ
1058 unsigned int is_external : 1;
1059 unsigned int is_declaration : 1;
1060 unsigned int has_type : 1;
1061 unsigned int has_specification : 1;
1062 unsigned int has_pc_info : 1;
481860b3 1063 unsigned int may_be_inlined : 1;
72bf9492 1064
0c1b455e
TT
1065 /* This DIE has been marked DW_AT_main_subprogram. */
1066 unsigned int main_subprogram : 1;
1067
72bf9492
DJ
1068 /* Flag set if the SCOPE field of this structure has been
1069 computed. */
1070 unsigned int scope_set : 1;
1071
fa4028e9
JB
1072 /* Flag set if the DIE has a byte_size attribute. */
1073 unsigned int has_byte_size : 1;
1074
ff908ebf
AW
1075 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1076 unsigned int has_const_value : 1;
1077
98bfdba5
PA
1078 /* Flag set if any of the DIE's children are template arguments. */
1079 unsigned int has_template_arguments : 1;
1080
52356b79 1081 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1082 unsigned int fixup_called : 1;
1083
36586728
TT
1084 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1085 unsigned int is_dwz : 1;
1086
1087 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1088 unsigned int spec_is_dwz : 1;
1089
7d00ffec
TT
1090 unsigned int canonical_name : 1;
1091
72bf9492 1092 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1093 sometimes a default name for unnamed DIEs. */
7d00ffec 1094 const char *raw_name = nullptr;
72bf9492 1095
abc72ce4 1096 /* The linkage name, if present. */
6f06d47b 1097 const char *linkage_name = nullptr;
abc72ce4 1098
72bf9492
DJ
1099 /* The scope to prepend to our children. This is generally
1100 allocated on the comp_unit_obstack, so will disappear
1101 when this compilation unit leaves the cache. */
6f06d47b 1102 const char *scope = nullptr;
72bf9492 1103
95554aad
TT
1104 /* Some data associated with the partial DIE. The tag determines
1105 which field is live. */
1106 union
1107 {
1108 /* The location description associated with this DIE, if any. */
1109 struct dwarf_block *locdesc;
1110 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1111 sect_offset sect_off;
6f06d47b 1112 } d {};
72bf9492
DJ
1113
1114 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1115 CORE_ADDR lowpc = 0;
1116 CORE_ADDR highpc = 0;
72bf9492 1117
93311388 1118 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1119 DW_AT_sibling, if any. */
48fbe735
YQ
1120 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1121 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1122 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1123
1124 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1125 DW_AT_specification (or DW_AT_abstract_origin or
1126 DW_AT_extension). */
6f06d47b 1127 sect_offset spec_offset {};
72bf9492
DJ
1128
1129 /* Pointers to this DIE's parent, first child, and next sibling,
1130 if any. */
6f06d47b
YQ
1131 struct partial_die_info *die_parent = nullptr;
1132 struct partial_die_info *die_child = nullptr;
1133 struct partial_die_info *die_sibling = nullptr;
1134
1135 friend struct partial_die_info *
1136 dwarf2_cu::find_partial_die (sect_offset sect_off);
1137
1138 private:
1139 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1140 partial_die_info (sect_offset sect_off)
1141 : partial_die_info (sect_off, DW_TAG_padding, 0)
1142 {
1143 }
1144
1145 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1146 int has_children_)
1147 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1148 {
1149 is_external = 0;
1150 is_declaration = 0;
1151 has_type = 0;
1152 has_specification = 0;
1153 has_pc_info = 0;
1154 may_be_inlined = 0;
1155 main_subprogram = 0;
1156 scope_set = 0;
1157 has_byte_size = 0;
1158 has_const_value = 0;
1159 has_template_arguments = 0;
1160 fixup_called = 0;
1161 is_dwz = 0;
1162 spec_is_dwz = 0;
7d00ffec 1163 canonical_name = 0;
6f06d47b 1164 }
c906108c
SS
1165 };
1166
c906108c
SS
1167/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1168 but this would require a corresponding change in unpack_field_as_long
1169 and friends. */
1170static int bits_per_byte = 8;
1171
9c6a1327
TT
1172struct variant_part_builder;
1173
1174/* When reading a variant, we track a bit more information about the
1175 field, and store it in an object of this type. */
2ddeaf8a
TT
1176
1177struct variant_field
1178{
9c6a1327
TT
1179 int first_field = -1;
1180 int last_field = -1;
1181
1182 /* A variant can contain other variant parts. */
1183 std::vector<variant_part_builder> variant_parts;
1184
2ddeaf8a
TT
1185 /* If we see a DW_TAG_variant, then this will be set if this is the
1186 default branch. */
9c6a1327
TT
1187 bool default_branch = false;
1188 /* If we see a DW_AT_discr_value, then this will be the discriminant
1189 value. */
1190 ULONGEST discriminant_value = 0;
1191 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1192 data. */
1193 struct dwarf_block *discr_list_data = nullptr;
1194};
1195
1196/* This represents a DW_TAG_variant_part. */
1197
1198struct variant_part_builder
1199{
1200 /* The offset of the discriminant field. */
1201 sect_offset discriminant_offset {};
1202
1203 /* Variants that are direct children of this variant part. */
1204 std::vector<variant_field> variants;
1205
1206 /* True if we're currently reading a variant. */
1207 bool processing_variant = false;
2ddeaf8a
TT
1208};
1209
52059ffd
TT
1210struct nextfield
1211{
be2daae6
TT
1212 int accessibility = 0;
1213 int virtuality = 0;
9c6a1327
TT
1214 /* Variant parts need to find the discriminant, which is a DIE
1215 reference. We track the section offset of each field to make
1216 this link. */
1217 sect_offset offset;
be2daae6 1218 struct field field {};
52059ffd
TT
1219};
1220
1221struct fnfieldlist
1222{
be2daae6
TT
1223 const char *name = nullptr;
1224 std::vector<struct fn_field> fnfields;
52059ffd
TT
1225};
1226
c906108c
SS
1227/* The routines that read and process dies for a C struct or C++ class
1228 pass lists of data member fields and lists of member function fields
1229 in an instance of a field_info structure, as defined below. */
1230struct field_info
2de01bdb
SM
1231{
1232 /* List of data member and baseclasses fields. */
1233 std::vector<struct nextfield> fields;
1234 std::vector<struct nextfield> baseclasses;
1235
1236 /* Set if the accessibility of one of the fields is not public. */
264fc0e2 1237 bool non_public_fields = false;
2de01bdb
SM
1238
1239 /* Member function fieldlist array, contains name of possibly overloaded
1240 member function, number of overloaded member functions and a pointer
1241 to the head of the member function field chain. */
1242 std::vector<struct fnfieldlist> fnfieldlists;
1243
1244 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1245 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1246 std::vector<struct decl_field> typedef_field_list;
1247
1248 /* Nested types defined by this class and the number of elements in this
1249 list. */
1250 std::vector<struct decl_field> nested_types_list;
1251
1252 /* If non-null, this is the variant part we are currently
1253 reading. */
1254 variant_part_builder *current_variant_part = nullptr;
1255 /* This holds all the top-level variant parts attached to the type
1256 we're reading. */
1257 std::vector<variant_part_builder> variant_parts;
1258
1259 /* Return the total number of fields (including baseclasses). */
1260 int nfields () const
c5aa993b 1261 {
2de01bdb
SM
1262 return fields.size () + baseclasses.size ();
1263 }
1264};
c906108c 1265
ae038cb0
DJ
1266/* Loaded secondary compilation units are kept in memory until they
1267 have not been referenced for the processing of this many
1268 compilation units. Set this to zero to disable caching. Cache
1269 sizes of up to at least twenty will improve startup time for
1270 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1271static int dwarf_max_cache_age = 5;
920d2a44 1272static void
b4f54984
DE
1273show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1274 struct cmd_list_element *c, const char *value)
920d2a44 1275{
3e43a32a 1276 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1277 "DWARF compilation units is %s.\n"),
920d2a44
AC
1278 value);
1279}
4390d890 1280\f
c906108c
SS
1281/* local function prototypes */
1282
918dd910
JK
1283static void dwarf2_find_base_address (struct die_info *die,
1284 struct dwarf2_cu *cu);
1285
891813be 1286static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1287 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1288 const char *name);
0018ea6f 1289
f1902523
JK
1290static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1291 const gdb_byte *info_ptr,
3e225074 1292 struct die_info *type_unit_die);
f1902523 1293
976ca316 1294static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
c906108c 1295
72bf9492
DJ
1296static void scan_partial_symbols (struct partial_die_info *,
1297 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1298 int, struct dwarf2_cu *);
c906108c 1299
72bf9492
DJ
1300static void add_partial_symbol (struct partial_die_info *,
1301 struct dwarf2_cu *);
63d06c5c 1302
72bf9492
DJ
1303static void add_partial_namespace (struct partial_die_info *pdi,
1304 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1305 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1306
5d7cb8df 1307static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1308 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1309 struct dwarf2_cu *cu);
1310
72bf9492
DJ
1311static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1312 struct dwarf2_cu *cu);
91c24f0a 1313
bc30ff58
JB
1314static void add_partial_subprogram (struct partial_die_info *pdi,
1315 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1316 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1317
d521ce57 1318static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1319
dee91e82 1320static struct partial_die_info *load_partial_dies
d521ce57 1321 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1322
fb816e8b
TV
1323/* A pair of partial_die_info and compilation unit. */
1324struct cu_partial_die_info
1325{
1326 /* The compilation unit of the partial_die_info. */
1327 struct dwarf2_cu *cu;
1328 /* A partial_die_info. */
1329 struct partial_die_info *pdi;
122cf0f2
AB
1330
1331 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1332 : cu (cu),
1333 pdi (pdi)
405feb71 1334 { /* Nothing. */ }
122cf0f2
AB
1335
1336private:
1337 cu_partial_die_info () = delete;
fb816e8b
TV
1338};
1339
122cf0f2
AB
1340static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1341 struct dwarf2_cu *);
72bf9492 1342
d521ce57 1343static const gdb_byte *read_attribute (const struct die_reader_specs *,
4444f407
TT
1344 struct attribute *,
1345 const struct attr_abbrev *,
7a5f294d 1346 const gdb_byte *);
18a8505e
AT
1347
1348static void read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 1349 struct attribute *attr, dwarf_tag tag);
18a8505e
AT
1350
1351static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1352
976ca316
SM
1353static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1354 dwarf2_section_info *, sect_offset);
f4dc4d17 1355
ed2dc618 1356static const char *read_indirect_string
976ca316 1357 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
ed2dc618 1358 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1359
ed2dc618 1360static const char *read_indirect_string_at_offset
976ca316 1361 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
927aa2e7 1362
d521ce57
TT
1363static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1364 const gdb_byte *,
3019eac3
DE
1365 unsigned int *);
1366
18a8505e
AT
1367static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1368 ULONGEST str_index);
1369
1370static const char *read_stub_str_index (struct dwarf2_cu *cu,
1371 ULONGEST str_index);
3019eac3 1372
e142c38c 1373static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1374
e142c38c
DJ
1375static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1376 struct dwarf2_cu *);
c906108c 1377
7d45c7c3 1378static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
dda83cd7 1379 struct dwarf2_cu *cu);
7d45c7c3 1380
a084a2a6
AT
1381static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1382
05cf31d1 1383static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
dda83cd7 1384 struct dwarf2_cu *cu);
05cf31d1 1385
e142c38c 1386static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1387
e142c38c 1388static struct die_info *die_specification (struct die_info *die,
f2f0e013 1389 struct dwarf2_cu **);
63d06c5c 1390
9c541725 1391static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1392 struct dwarf2_cu *cu);
debd256d 1393
f3f5162e 1394static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1395 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1396 CORE_ADDR, int decode_mapping);
c906108c 1397
804d2729
TT
1398static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1399 const char *);
c906108c 1400
a14ed312 1401static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1402 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1403
ff39bb5e 1404static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1405 struct dwarf2_cu *);
c906108c 1406
ff39bb5e 1407static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1408 struct type *type,
1409 const char *name,
1410 struct obstack *obstack,
12df843f 1411 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1412 const gdb_byte **bytes,
98bfdba5 1413 struct dwarf2_locexpr_baton **baton);
2df3850c 1414
57567375
TT
1415static struct type *read_subrange_index_type (struct die_info *die,
1416 struct dwarf2_cu *cu);
1417
e7c27a73 1418static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1419
b4ba55a1
JB
1420static int need_gnat_info (struct dwarf2_cu *);
1421
3e43a32a
MS
1422static struct type *die_descriptive_type (struct die_info *,
1423 struct dwarf2_cu *);
b4ba55a1
JB
1424
1425static void set_descriptive_type (struct type *, struct die_info *,
1426 struct dwarf2_cu *);
1427
e7c27a73
DJ
1428static struct type *die_containing_type (struct die_info *,
1429 struct dwarf2_cu *);
c906108c 1430
ff39bb5e 1431static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1432 struct dwarf2_cu *);
c906108c 1433
f792889a 1434static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1435
673bfd45
DE
1436static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1437
0d5cff50 1438static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1439
6e70227d 1440static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1441 const char *suffix, int physname,
1442 struct dwarf2_cu *cu);
63d06c5c 1443
e7c27a73 1444static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1445
348e048f
DE
1446static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1447
e7c27a73 1448static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1449
e7c27a73 1450static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1451
96408a79
SA
1452static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1453
71a3c369
TT
1454static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1455
41144253 1456/* Return the .debug_loclists section to use for cu. */
1457static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1458
d0ce17d8
CT
1459/* Return the .debug_rnglists section to use for cu. */
1460static struct dwarf2_section_info *cu_debug_rnglists_section
1461 (struct dwarf2_cu *cu, dwarf_tag tag);
1462
3a2b436a 1463/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1464 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1465enum pc_bounds_kind
1466{
e385593e 1467 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1468 PC_BOUNDS_NOT_PRESENT,
1469
e385593e
JK
1470 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1471 were present but they do not form a valid range of PC addresses. */
1472 PC_BOUNDS_INVALID,
1473
3a2b436a
JK
1474 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1475 PC_BOUNDS_RANGES,
1476
1477 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1478 PC_BOUNDS_HIGH_LOW,
1479};
1480
1481static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1482 CORE_ADDR *, CORE_ADDR *,
1483 struct dwarf2_cu *,
891813be 1484 dwarf2_psymtab *);
c906108c 1485
fae299cd
DC
1486static void get_scope_pc_bounds (struct die_info *,
1487 CORE_ADDR *, CORE_ADDR *,
1488 struct dwarf2_cu *);
1489
801e3a5b 1490static void dwarf2_record_block_ranges (struct die_info *, struct block *,
dda83cd7 1491 CORE_ADDR, struct dwarf2_cu *);
801e3a5b 1492
a14ed312 1493static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1494 struct dwarf2_cu *);
c906108c 1495
a14ed312 1496static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1497 struct type *, struct dwarf2_cu *);
c906108c 1498
a14ed312 1499static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1500 struct die_info *, struct type *,
e7c27a73 1501 struct dwarf2_cu *);
c906108c 1502
a14ed312 1503static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1504 struct type *,
1505 struct dwarf2_cu *);
c906108c 1506
134d01f1 1507static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1508
e7c27a73 1509static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1510
e7c27a73 1511static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1512
5d7cb8df
JK
1513static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1514
804d2729 1515static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1516
27aa8d6a
SW
1517static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1518
74921315
KS
1519static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1520
f55ee35c
JK
1521static struct type *read_module_type (struct die_info *die,
1522 struct dwarf2_cu *cu);
1523
38d518c9 1524static const char *namespace_name (struct die_info *die,
e142c38c 1525 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1526
134d01f1 1527static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1528
7d79de9a
TT
1529static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1530 bool * = nullptr);
c906108c 1531
6e70227d 1532static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1533 struct dwarf2_cu *);
1534
bf6af496 1535static struct die_info *read_die_and_siblings_1
d521ce57 1536 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1537 struct die_info *);
639d11d3 1538
dee91e82 1539static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1540 const gdb_byte *info_ptr,
1541 const gdb_byte **new_info_ptr,
639d11d3
DC
1542 struct die_info *parent);
1543
d521ce57
TT
1544static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1545 struct die_info **, const gdb_byte *,
3e225074 1546 int);
3019eac3 1547
d521ce57 1548static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1549 struct die_info **, const gdb_byte *);
93311388 1550
e7c27a73 1551static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1552
15d034d0 1553static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1554 struct objfile *);
71c25dea 1555
15d034d0 1556static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1557
15d034d0 1558static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1559 struct die_info *die,
1560 struct dwarf2_cu *cu);
1561
ca69b9e6
DE
1562static const char *dwarf2_physname (const char *name, struct die_info *die,
1563 struct dwarf2_cu *cu);
1564
e142c38c 1565static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1566 struct dwarf2_cu **);
9219021c 1567
d97bc12b
DE
1568static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1569
1570static void dump_die_for_error (struct die_info *);
1571
1572static void dump_die_1 (struct ui_file *, int level, int max_level,
1573 struct die_info *);
c906108c 1574
d97bc12b 1575/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1576
51545339 1577static void store_in_ref_table (struct die_info *,
10b3939b 1578 struct dwarf2_cu *);
c906108c 1579
348e048f 1580static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1581 const struct attribute *,
348e048f
DE
1582 struct dwarf2_cu **);
1583
10b3939b 1584static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1585 const struct attribute *,
f2f0e013 1586 struct dwarf2_cu **);
c906108c 1587
348e048f 1588static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1589 const struct attribute *,
348e048f
DE
1590 struct dwarf2_cu **);
1591
ac9ec31b
DE
1592static struct type *get_signatured_type (struct die_info *, ULONGEST,
1593 struct dwarf2_cu *);
1594
1595static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1596 const struct attribute *,
ac9ec31b
DE
1597 struct dwarf2_cu *);
1598
ab432490
SM
1599static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1600 dwarf2_per_objfile *per_objfile);
348e048f 1601
ab432490
SM
1602static void read_signatured_type (signatured_type *sig_type,
1603 dwarf2_per_objfile *per_objfile);
348e048f 1604
63e43d3a
PMR
1605static int attr_to_dynamic_prop (const struct attribute *attr,
1606 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1607 struct dynamic_prop *prop, struct type *type);
63e43d3a 1608
c906108c
SS
1609/* memory allocation interface */
1610
7b5a2f43 1611static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1612
b60c80d6 1613static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1614
43f3e411 1615static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1616
8cf6f0b1
TT
1617static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1618 struct dwarf2_loclist_baton *baton,
ff39bb5e 1619 const struct attribute *attr);
8cf6f0b1 1620
ff39bb5e 1621static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1622 struct symbol *sym,
f1e6e072
TT
1623 struct dwarf2_cu *cu,
1624 int is_block);
4c2df51b 1625
d521ce57
TT
1626static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1627 const gdb_byte *info_ptr,
7c32eebb 1628 const struct abbrev_info *abbrev);
4bb7a0a7 1629
72bf9492
DJ
1630static hashval_t partial_die_hash (const void *item);
1631
1632static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1633
ae038cb0 1634static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618 1635 (sect_offset sect_off, unsigned int offset_in_dwz,
976ca316 1636 dwarf2_per_objfile *per_objfile);
ae038cb0 1637
9816fde3 1638static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1639 struct die_info *comp_unit_die,
1640 enum language pretend_language);
93311388 1641
f792889a 1642static struct type *set_die_type (struct die_info *, struct type *,
57567375 1643 struct dwarf2_cu *, bool = false);
1c379e20 1644
976ca316 1645static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
ae038cb0 1646
976ca316 1647static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1fd400ff 1648
ab432490
SM
1649static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1650 dwarf2_per_objfile *per_objfile,
4a636814 1651 dwarf2_cu *existing_cu,
ab432490
SM
1652 bool skip_partial,
1653 enum language pretend_language);
10b3939b 1654
8fc0b21d 1655static void process_full_comp_unit (dwarf2_cu *cu,
47b14e86 1656 enum language pretend_language);
10b3939b 1657
8fc0b21d 1658static void process_full_type_unit (dwarf2_cu *cu,
47b14e86 1659 enum language pretend_language);
f4dc4d17 1660
10b3939b
DJ
1661static void dwarf2_add_dependence (struct dwarf2_cu *,
1662 struct dwarf2_per_cu_data *);
1663
ae038cb0
DJ
1664static void dwarf2_mark (struct dwarf2_cu *);
1665
b64f50a1 1666static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1667 dwarf2_per_cu_data *per_cu,
1668 dwarf2_per_objfile *per_objfile);
673bfd45 1669
f792889a 1670static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1671
120ce1b5
SM
1672static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1673 dwarf2_per_objfile *per_objfile,
95554aad
TT
1674 enum language pretend_language);
1675
976ca316 1676static void process_queue (dwarf2_per_objfile *per_objfile);
9291a0cd 1677
b303c6f6
AB
1678/* Class, the destructor of which frees all allocated queue entries. This
1679 will only have work to do if an error was thrown while processing the
1680 dwarf. If no error was thrown then the queue entries should have all
1681 been processed, and freed, as we went along. */
1682
1683class dwarf2_queue_guard
1684{
1685public:
39856def
TT
1686 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1687 : m_per_objfile (per_objfile)
1688 {
08ac5771
SM
1689 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1690
1691 m_per_objfile->per_bfd->queue.emplace ();
39856def 1692 }
b303c6f6
AB
1693
1694 /* Free any entries remaining on the queue. There should only be
1695 entries left if we hit an error while processing the dwarf. */
1696 ~dwarf2_queue_guard ()
1697 {
08ac5771
SM
1698 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1699
1700 m_per_objfile->per_bfd->queue.reset ();
39856def 1701 }
b303c6f6 1702
39856def 1703 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1704
39856def
TT
1705private:
1706 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1707};
1708
39856def
TT
1709dwarf2_queue_item::~dwarf2_queue_item ()
1710{
1711 /* Anything still marked queued is likely to be in an
1712 inconsistent state, so discard it. */
1713 if (per_cu->queued)
1714 {
7188ed02 1715 per_objfile->remove_cu (per_cu);
39856def
TT
1716 per_cu->queued = 0;
1717 }
1718}
1719
d721ba37
PA
1720/* The return type of find_file_and_directory. Note, the enclosed
1721 string pointers are only valid while this object is valid. */
1722
1723struct file_and_directory
1724{
1725 /* The filename. This is never NULL. */
1726 const char *name;
1727
1728 /* The compilation directory. NULL if not known. If we needed to
1729 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1730 points directly to the DW_AT_comp_dir string attribute owned by
1731 the obstack that owns the DIE. */
1732 const char *comp_dir;
1733
1734 /* If we needed to build a new string for comp_dir, this is what
1735 owns the storage. */
1736 std::string comp_dir_storage;
1737};
1738
1739static file_and_directory find_file_and_directory (struct die_info *die,
1740 struct dwarf2_cu *cu);
9291a0cd 1741
298e9637 1742static htab_up allocate_signatured_type_table ();
1fd400ff 1743
298e9637 1744static htab_up allocate_dwo_unit_table ();
3019eac3 1745
57d63ce2 1746static struct dwo_unit *lookup_dwo_unit_in_dwp
976ca316
SM
1747 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1748 const char *comp_dir, ULONGEST signature, int is_debug_types);
a2ce51a0 1749
976ca316 1750static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
a2ce51a0 1751
3019eac3 1752static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1753 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1754 ULONGEST signature);
3019eac3
DE
1755
1756static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1757 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1758
1b555f17 1759static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
89e63ee4 1760
263db9a1
TT
1761/* A unique pointer to a dwo_file. */
1762
51ac9db5 1763typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1764
976ca316 1765static void process_cu_includes (dwarf2_per_objfile *per_objfile);
95554aad 1766
1b80a9fa 1767static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1768
1769static void free_line_header_voidp (void *arg);
4390d890
DE
1770\f
1771/* Various complaints about symbol reading that don't abort the process. */
1772
4390d890
DE
1773static void
1774dwarf2_debug_line_missing_file_complaint (void)
1775{
b98664d3 1776 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1777}
1778
1779static void
1780dwarf2_debug_line_missing_end_sequence_complaint (void)
1781{
b98664d3 1782 complaint (_(".debug_line section has line "
4390d890
DE
1783 "program sequence without an end"));
1784}
1785
1786static void
1787dwarf2_complex_location_expr_complaint (void)
1788{
b98664d3 1789 complaint (_("location expression too complex"));
4390d890
DE
1790}
1791
1792static void
1793dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1794 int arg3)
1795{
b98664d3 1796 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1797 arg1, arg2, arg3);
1798}
1799
4390d890
DE
1800static void
1801dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1802{
b98664d3 1803 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1804 arg1, arg2);
1805}
527f3840
JK
1806
1807/* Hash function for line_header_hash. */
1808
1809static hashval_t
1810line_header_hash (const struct line_header *ofs)
1811{
9c541725 1812 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1813}
1814
1815/* Hash function for htab_create_alloc_ex for line_header_hash. */
1816
1817static hashval_t
1818line_header_hash_voidp (const void *item)
1819{
9a3c8263 1820 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1821
1822 return line_header_hash (ofs);
1823}
1824
1825/* Equality function for line_header_hash. */
1826
1827static int
1828line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1829{
9a3c8263
SM
1830 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1831 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1832
9c541725 1833 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1834 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1835}
1836
4390d890 1837\f
9291a0cd 1838
330cdd98
PA
1839/* See declaration. */
1840
5989a64e
SM
1841dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1842 bool can_copy_)
c3699833
SM
1843 : obfd (obfd),
1844 can_copy (can_copy_)
330cdd98
PA
1845{
1846 if (names == NULL)
1847 names = &dwarf2_elf_names;
1848
330cdd98
PA
1849 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1850 locate_sections (obfd, sec, *names);
1851}
1852
5989a64e 1853dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98 1854{
b76e467d 1855 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1856 per_cu->imported_symtabs_free ();
fc8e7e75 1857
b2bdb8cf 1858 for (signatured_type *sig_type : all_type_units)
ae640021 1859 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1860
5989a64e 1861 /* Everything else should be on this->obstack. */
330cdd98
PA
1862}
1863
7188ed02 1864/* See read.h. */
330cdd98
PA
1865
1866void
7188ed02 1867dwarf2_per_objfile::remove_all_cus ()
330cdd98 1868{
08ac5771
SM
1869 gdb_assert (!this->per_bfd->queue.has_value ());
1870
7188ed02
SM
1871 for (auto pair : m_dwarf2_cus)
1872 delete pair.second;
330cdd98 1873
7188ed02 1874 m_dwarf2_cus.clear ();
330cdd98
PA
1875}
1876
11ed8cad
TT
1877/* A helper class that calls free_cached_comp_units on
1878 destruction. */
1879
1880class free_cached_comp_units
1881{
1882public:
1883
1884 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1885 : m_per_objfile (per_objfile)
1886 {
1887 }
1888
1889 ~free_cached_comp_units ()
1890 {
7188ed02 1891 m_per_objfile->remove_all_cus ();
11ed8cad
TT
1892 }
1893
1894 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1895
1896private:
1897
1898 dwarf2_per_objfile *m_per_objfile;
1899};
1900
af758d11
SM
1901/* See read.h. */
1902
1903bool
1904dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1905{
1906 gdb_assert (per_cu->index < this->m_symtabs.size ());
1907
1908 return this->m_symtabs[per_cu->index] != nullptr;
1909}
1910
1911/* See read.h. */
1912
1913compunit_symtab *
1914dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1915{
1916 gdb_assert (per_cu->index < this->m_symtabs.size ());
1917
1918 return this->m_symtabs[per_cu->index];
1919}
1920
1921/* See read.h. */
1922
1923void
1924dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1925 compunit_symtab *symtab)
1926{
1927 gdb_assert (per_cu->index < this->m_symtabs.size ());
1928 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1929
1930 this->m_symtabs[per_cu->index] = symtab;
1931}
1932
c906108c 1933/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1934 information and return true if we have enough to do something.
1935 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1936 ELF names are used. CAN_COPY is true for formats where symbol
1937 interposition is possible and so symbol values must follow copy
1938 relocation rules. */
c906108c
SS
1939
1940int
251d32d9 1941dwarf2_has_info (struct objfile *objfile,
dda83cd7 1942 const struct dwarf2_debug_sections *names,
4b610737 1943 bool can_copy)
c906108c 1944{
97cbe998
SDJ
1945 if (objfile->flags & OBJF_READNEVER)
1946 return 0;
1947
976ca316 1948 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 1949
976ca316 1950 if (per_objfile == NULL)
5989a64e 1951 {
17ee85fc
TT
1952 dwarf2_per_bfd *per_bfd;
1953
1954 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
dda83cd7 1955 doesn't require relocations and if there aren't partial symbols
17ee85fc 1956 from some other reader. */
a8ad4f3c 1957 if (!objfile->has_partial_symbols ()
17ee85fc
TT
1958 && !gdb_bfd_requires_relocations (objfile->obfd))
1959 {
1960 /* See if one has been created for this BFD yet. */
1961 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1962
1963 if (per_bfd == nullptr)
1964 {
1965 /* No, create it now. */
1966 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1967 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1968 }
1969 }
1970 else
1971 {
1972 /* No sharing possible, create one specifically for this objfile. */
1973 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1974 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1975 }
5989a64e 1976
976ca316 1977 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 1978 }
5bfd760d 1979
976ca316
SM
1980 return (!per_objfile->per_bfd->info.is_virtual
1981 && per_objfile->per_bfd->info.s.section != NULL
1982 && !per_objfile->per_bfd->abbrev.is_virtual
1983 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1984}
1985
330cdd98 1986/* See declaration. */
c906108c 1987
330cdd98 1988void
5989a64e
SM
1989dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1990 const dwarf2_debug_sections &names)
c906108c 1991{
fd361982 1992 flagword aflag = bfd_section_flags (sectp);
251d32d9 1993
dc7650b8
JK
1994 if ((aflag & SEC_HAS_CONTENTS) == 0)
1995 {
1996 }
950b7495
KS
1997 else if (elf_section_data (sectp)->this_hdr.sh_size
1998 > bfd_get_file_size (abfd))
1999 {
2000 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2001 warning (_("Discarding section %s which has a section size (%s"
2002 ") larger than the file size [in module %s]"),
2003 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2004 bfd_get_filename (abfd));
2005 }
fbedd546 2006 else if (names.info.matches (sectp->name))
c906108c 2007 {
330cdd98 2008 this->info.s.section = sectp;
fd361982 2009 this->info.size = bfd_section_size (sectp);
c906108c 2010 }
fbedd546 2011 else if (names.abbrev.matches (sectp->name))
c906108c 2012 {
330cdd98 2013 this->abbrev.s.section = sectp;
fd361982 2014 this->abbrev.size = bfd_section_size (sectp);
c906108c 2015 }
fbedd546 2016 else if (names.line.matches (sectp->name))
c906108c 2017 {
330cdd98 2018 this->line.s.section = sectp;
fd361982 2019 this->line.size = bfd_section_size (sectp);
c906108c 2020 }
fbedd546 2021 else if (names.loc.matches (sectp->name))
c906108c 2022 {
330cdd98 2023 this->loc.s.section = sectp;
fd361982 2024 this->loc.size = bfd_section_size (sectp);
c906108c 2025 }
fbedd546 2026 else if (names.loclists.matches (sectp->name))
43988095 2027 {
330cdd98 2028 this->loclists.s.section = sectp;
fd361982 2029 this->loclists.size = bfd_section_size (sectp);
43988095 2030 }
fbedd546 2031 else if (names.macinfo.matches (sectp->name))
c906108c 2032 {
330cdd98 2033 this->macinfo.s.section = sectp;
fd361982 2034 this->macinfo.size = bfd_section_size (sectp);
c906108c 2035 }
fbedd546 2036 else if (names.macro.matches (sectp->name))
cf2c3c16 2037 {
330cdd98 2038 this->macro.s.section = sectp;
fd361982 2039 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2040 }
fbedd546 2041 else if (names.str.matches (sectp->name))
c906108c 2042 {
330cdd98 2043 this->str.s.section = sectp;
fd361982 2044 this->str.size = bfd_section_size (sectp);
c906108c 2045 }
fbedd546 2046 else if (names.str_offsets.matches (sectp->name))
18a8505e
AT
2047 {
2048 this->str_offsets.s.section = sectp;
2049 this->str_offsets.size = bfd_section_size (sectp);
2050 }
fbedd546 2051 else if (names.line_str.matches (sectp->name))
43988095 2052 {
330cdd98 2053 this->line_str.s.section = sectp;
fd361982 2054 this->line_str.size = bfd_section_size (sectp);
43988095 2055 }
fbedd546 2056 else if (names.addr.matches (sectp->name))
3019eac3 2057 {
330cdd98 2058 this->addr.s.section = sectp;
fd361982 2059 this->addr.size = bfd_section_size (sectp);
3019eac3 2060 }
fbedd546 2061 else if (names.frame.matches (sectp->name))
b6af0555 2062 {
330cdd98 2063 this->frame.s.section = sectp;
fd361982 2064 this->frame.size = bfd_section_size (sectp);
b6af0555 2065 }
fbedd546 2066 else if (names.eh_frame.matches (sectp->name))
b6af0555 2067 {
330cdd98 2068 this->eh_frame.s.section = sectp;
fd361982 2069 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2070 }
fbedd546 2071 else if (names.ranges.matches (sectp->name))
af34e669 2072 {
330cdd98 2073 this->ranges.s.section = sectp;
fd361982 2074 this->ranges.size = bfd_section_size (sectp);
af34e669 2075 }
fbedd546 2076 else if (names.rnglists.matches (sectp->name))
43988095 2077 {
330cdd98 2078 this->rnglists.s.section = sectp;
fd361982 2079 this->rnglists.size = bfd_section_size (sectp);
43988095 2080 }
fbedd546 2081 else if (names.types.matches (sectp->name))
348e048f 2082 {
8b70b953
TT
2083 struct dwarf2_section_info type_section;
2084
2085 memset (&type_section, 0, sizeof (type_section));
049412e3 2086 type_section.s.section = sectp;
fd361982 2087 type_section.size = bfd_section_size (sectp);
8b70b953 2088
fd5866f6 2089 this->types.push_back (type_section);
348e048f 2090 }
fbedd546 2091 else if (names.gdb_index.matches (sectp->name))
9291a0cd 2092 {
330cdd98 2093 this->gdb_index.s.section = sectp;
fd361982 2094 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2095 }
fbedd546 2096 else if (names.debug_names.matches (sectp->name))
927aa2e7
JK
2097 {
2098 this->debug_names.s.section = sectp;
fd361982 2099 this->debug_names.size = bfd_section_size (sectp);
927aa2e7 2100 }
fbedd546 2101 else if (names.debug_aranges.matches (sectp->name))
927aa2e7
JK
2102 {
2103 this->debug_aranges.s.section = sectp;
fd361982 2104 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2105 }
dce234bc 2106
fd361982
AM
2107 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2108 && bfd_section_vma (sectp) == 0)
330cdd98 2109 this->has_section_at_zero = true;
c906108c
SS
2110}
2111
dce234bc 2112/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2113 SECTION_NAME. */
af34e669 2114
dce234bc 2115void
3017a003 2116dwarf2_get_section_info (struct objfile *objfile,
dda83cd7
SM
2117 enum dwarf2_section_enum sect,
2118 asection **sectp, const gdb_byte **bufp,
2119 bfd_size_type *sizep)
dce234bc 2120{
976ca316 2121 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 2122 struct dwarf2_section_info *info;
a3b2a86b
TT
2123
2124 /* We may see an objfile without any DWARF, in which case we just
2125 return nothing. */
976ca316 2126 if (per_objfile == NULL)
a3b2a86b
TT
2127 {
2128 *sectp = NULL;
2129 *bufp = NULL;
2130 *sizep = 0;
2131 return;
2132 }
3017a003
TG
2133 switch (sect)
2134 {
2135 case DWARF2_DEBUG_FRAME:
976ca316 2136 info = &per_objfile->per_bfd->frame;
3017a003
TG
2137 break;
2138 case DWARF2_EH_FRAME:
976ca316 2139 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
2140 break;
2141 default:
2142 gdb_assert_not_reached ("unexpected section");
2143 }
dce234bc 2144
96b79293 2145 info->read (objfile);
dce234bc 2146
96b79293 2147 *sectp = info->get_bfd_section ();
dce234bc
PP
2148 *bufp = info->buffer;
2149 *sizep = info->size;
2150}
2151
9291a0cd 2152\f
39298a5d 2153/* DWARF quick_symbol_functions support. */
7b9f3c50
DE
2154
2155/* TUs can share .debug_line entries, and there can be a lot more TUs than
2156 unique line tables, so we maintain a separate table of all .debug_line
2157 derived entries to support the sharing.
2158 All the quick functions need is the list of file names. We discard the
2159 line_header when we're done and don't need to record it here. */
2160struct quick_file_names
2161{
094b34ac
DE
2162 /* The data used to construct the hash key. */
2163 struct stmt_list_hash hash;
7b9f3c50
DE
2164
2165 /* The number of entries in file_names, real_names. */
2166 unsigned int num_file_names;
2167
2168 /* The file names from the line table, after being run through
2169 file_full_name. */
2170 const char **file_names;
2171
2172 /* The file names from the line table after being run through
2173 gdb_realpath. These are computed lazily. */
2174 const char **real_names;
2175};
2176
2177/* When using the index (and thus not using psymtabs), each CU has an
2178 object of this type. This is used to hold information needed by
2179 the various "quick" methods. */
2180struct dwarf2_per_cu_quick_data
2181{
2182 /* The file table. This can be NULL if there was no file table
2183 or it's currently not read in.
5989a64e 2184 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2185 struct quick_file_names *file_names;
2186
7b9f3c50
DE
2187 /* A temporary mark bit used when iterating over all CUs in
2188 expand_symtabs_matching. */
2189 unsigned int mark : 1;
2190
2191 /* True if we've tried to read the file table and found there isn't one.
2192 There will be no point in trying to read it again next time. */
2193 unsigned int no_file_data : 1;
2194};
2195
39298a5d
TT
2196struct dwarf2_base_index_functions : public quick_symbol_functions
2197{
2198 bool has_symbols (struct objfile *objfile) override;
2199
2200 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2201
2202 void forget_cached_source_info (struct objfile *objfile) override;
2203
2204 bool map_symtabs_matching_filename
2205 (struct objfile *objfile, const char *name, const char *real_path,
2206 gdb::function_view<bool (symtab *)> callback) override;
2207
2208 enum language lookup_global_symbol_language (struct objfile *objfile,
2209 const char *name,
2210 domain_enum domain,
2211 bool *symbol_found_p) override
2212 {
2213 *symbol_found_p = false;
2214 return language_unknown;
2215 }
2216
2217 void print_stats (struct objfile *objfile) override;
2218
2219 void expand_all_symtabs (struct objfile *objfile) override;
2220
2221 void expand_symtabs_with_fullname (struct objfile *objfile,
2222 const char *fullname) override;
2223
2224 struct compunit_symtab *find_pc_sect_compunit_symtab
2225 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2226 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2227
2228 struct compunit_symtab *find_compunit_symtab_by_address
2229 (struct objfile *objfile, CORE_ADDR address) override
2230 {
2231 return nullptr;
2232 }
2233
2234 void map_symbol_filenames (struct objfile *objfile,
2235 symbol_filename_ftype *fun, void *data,
2236 int need_fullname) override;
2237};
2238
2239struct dwarf2_gdb_index : public dwarf2_base_index_functions
2240{
2241 struct compunit_symtab *lookup_symbol (struct objfile *objfile,
2242 block_enum block_index,
2243 const char *name,
2244 domain_enum domain) override;
2245
2246 void dump (struct objfile *objfile) override;
2247
2248 void expand_symtabs_for_function (struct objfile *objfile,
2249 const char *func_name) override;
2250
2251 void map_matching_symbols
2252 (struct objfile *,
2253 const lookup_name_info &lookup_name,
2254 domain_enum domain,
2255 int global,
2256 gdb::function_view<symbol_found_callback_ftype> callback,
2257 symbol_compare_ftype *ordered_compare) override;
2258
2259 void expand_symtabs_matching
2260 (struct objfile *objfile,
2261 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2262 const lookup_name_info *lookup_name,
2263 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2264 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2265 enum search_domain kind) override;
2266};
2267
2268struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2269{
2270 struct compunit_symtab *lookup_symbol (struct objfile *objfile,
2271 block_enum block_index,
2272 const char *name,
2273 domain_enum domain) override;
2274
2275 void dump (struct objfile *objfile) override;
2276
2277 void expand_symtabs_for_function (struct objfile *objfile,
2278 const char *func_name) override;
2279
2280 void map_matching_symbols
2281 (struct objfile *,
2282 const lookup_name_info &lookup_name,
2283 domain_enum domain,
2284 int global,
2285 gdb::function_view<symbol_found_callback_ftype> callback,
2286 symbol_compare_ftype *ordered_compare) override;
2287
2288 void expand_symtabs_matching
2289 (struct objfile *objfile,
2290 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2291 const lookup_name_info *lookup_name,
2292 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2293 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2294 enum search_domain kind) override;
2295};
2296
2297quick_symbol_functions_up
2298make_dwarf_gdb_index ()
2299{
2300 return quick_symbol_functions_up (new dwarf2_gdb_index);
2301}
2302
2303quick_symbol_functions_up
2304make_dwarf_debug_names ()
2305{
2306 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2307}
2308
094b34ac
DE
2309/* Utility hash function for a stmt_list_hash. */
2310
2311static hashval_t
2312hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2313{
2314 hashval_t v = 0;
2315
2316 if (stmt_list_hash->dwo_unit != NULL)
2317 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2318 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2319 return v;
2320}
2321
2322/* Utility equality function for a stmt_list_hash. */
2323
2324static int
2325eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2326 const struct stmt_list_hash *rhs)
2327{
2328 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2329 return 0;
2330 if (lhs->dwo_unit != NULL
2331 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2332 return 0;
2333
9c541725 2334 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2335}
2336
7b9f3c50
DE
2337/* Hash function for a quick_file_names. */
2338
2339static hashval_t
2340hash_file_name_entry (const void *e)
2341{
9a3c8263
SM
2342 const struct quick_file_names *file_data
2343 = (const struct quick_file_names *) e;
7b9f3c50 2344
094b34ac 2345 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2346}
2347
2348/* Equality function for a quick_file_names. */
2349
2350static int
2351eq_file_name_entry (const void *a, const void *b)
2352{
9a3c8263
SM
2353 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2354 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2355
094b34ac 2356 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2357}
2358
2359/* Delete function for a quick_file_names. */
2360
2361static void
2362delete_file_name_entry (void *e)
2363{
9a3c8263 2364 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2365 int i;
2366
2367 for (i = 0; i < file_data->num_file_names; ++i)
2368 {
2369 xfree ((void*) file_data->file_names[i]);
2370 if (file_data->real_names)
2371 xfree ((void*) file_data->real_names[i]);
2372 }
2373
45940949
TT
2374 /* The space for the struct itself lives on the obstack, so we don't
2375 free it here. */
7b9f3c50
DE
2376}
2377
2378/* Create a quick_file_names hash table. */
2379
5895093f 2380static htab_up
7b9f3c50
DE
2381create_quick_file_names_table (unsigned int nr_initial_entries)
2382{
5895093f
TT
2383 return htab_up (htab_create_alloc (nr_initial_entries,
2384 hash_file_name_entry, eq_file_name_entry,
2385 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2386}
9291a0cd 2387
ab432490
SM
2388/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2389 function is unrelated to symtabs, symtab would have to be created afterwards.
2390 You should call age_cached_comp_units after processing the CU. */
918dd910 2391
1b555f17 2392static dwarf2_cu *
ab432490
SM
2393load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2394 bool skip_partial)
918dd910 2395{
3019eac3 2396 if (per_cu->is_debug_types)
ab432490 2397 load_full_type_unit (per_cu, per_objfile);
918dd910 2398 else
4a636814
SM
2399 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2400 skip_partial, language_minimal);
918dd910 2401
7188ed02
SM
2402 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2403 if (cu == nullptr)
1b555f17 2404 return nullptr; /* Dummy CU. */
2dc860c0 2405
7188ed02 2406 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2407
7188ed02 2408 return cu;
918dd910
JK
2409}
2410
1350c3b4 2411/* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2fdf6df6 2412
9291a0cd 2413static void
97a1449a 2414dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2415 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2416{
f4dc4d17
DE
2417 /* Skip type_unit_groups, reading the type units they contain
2418 is handled elsewhere. */
197400e8 2419 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2420 return;
2421
08ac5771
SM
2422 {
2423 /* The destructor of dwarf2_queue_guard frees any entries left on
2424 the queue. After this point we're guaranteed to leave this function
2425 with the dwarf queue empty. */
2426 dwarf2_queue_guard q_guard (per_objfile);
89e63ee4 2427
08ac5771
SM
2428 if (!per_objfile->symtab_set_p (per_cu))
2429 {
2430 queue_comp_unit (per_cu, per_objfile, language_minimal);
2431 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2432
2433 /* If we just loaded a CU from a DWO, and we're working with an index
2434 that may badly handle TUs, load all the TUs in that DWO as well.
2435 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2436 if (!per_cu->is_debug_types
2437 && cu != NULL
2438 && cu->dwo_unit != NULL
2439 && per_objfile->per_bfd->index_table != NULL
2440 && per_objfile->per_bfd->index_table->version <= 7
2441 /* DWP files aren't supported yet. */
2442 && get_dwp_file (per_objfile) == NULL)
2443 queue_and_load_all_dwo_tus (cu);
2444 }
9291a0cd 2445
08ac5771
SM
2446 process_queue (per_objfile);
2447 }
9291a0cd
TT
2448
2449 /* Age the cache, releasing compilation units that have not
2450 been used recently. */
976ca316 2451 per_objfile->age_comp_units ();
9291a0cd
TT
2452}
2453
97a1449a
SM
2454/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2455 the per-objfile for which this symtab is instantiated.
2456
2457 Returns the resulting symbol table. */
2fdf6df6 2458
43f3e411 2459static struct compunit_symtab *
97a1449a 2460dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2461 dwarf2_per_objfile *per_objfile,
97a1449a 2462 bool skip_partial)
9291a0cd 2463{
976ca316 2464 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2465
976ca316 2466 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2467 {
976ca316 2468 free_cached_comp_units freer (per_objfile);
c83dd867 2469 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2470 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2471 process_cu_includes (per_objfile);
9291a0cd 2472 }
f194fefb 2473
976ca316 2474 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2475}
2476
ff4c9fec 2477/* See declaration. */
f4dc4d17 2478
ff4c9fec 2479dwarf2_per_cu_data *
5989a64e 2480dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2481{
b76e467d 2482 if (index >= this->all_comp_units.size ())
ff4c9fec 2483 {
b76e467d 2484 index -= this->all_comp_units.size ();
b2bdb8cf 2485 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2486 return &this->all_type_units[index]->per_cu;
2487 }
f4dc4d17 2488
ff4c9fec
SM
2489 return this->all_comp_units[index];
2490}
f4dc4d17 2491
ff4c9fec 2492/* See declaration. */
2fdf6df6 2493
ff4c9fec 2494dwarf2_per_cu_data *
5989a64e 2495dwarf2_per_bfd::get_cu (int index)
1fd400ff 2496{
b76e467d 2497 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2498
ff4c9fec 2499 return this->all_comp_units[index];
f4dc4d17
DE
2500}
2501
ff4c9fec 2502/* See declaration. */
f4dc4d17 2503
ff4c9fec 2504signatured_type *
5989a64e 2505dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2506{
b2bdb8cf 2507 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2508
ff4c9fec 2509 return this->all_type_units[index];
1fd400ff
TT
2510}
2511
d3473f0c
TT
2512/* See read.h. */
2513
2514dwarf2_per_cu_data *
5989a64e 2515dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2516{
2517 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2518 result->per_bfd = this;
d3473f0c
TT
2519 result->index = m_num_psymtabs++;
2520 return result;
2521}
2522
2523/* See read.h. */
2524
2525signatured_type *
5989a64e 2526dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2527{
2528 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2529 result->per_cu.per_bfd = this;
d3473f0c
TT
2530 result->per_cu.index = m_num_psymtabs++;
2531 return result;
2532}
2533
168c9250 2534/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2535 obstack, and constructed with the specified field values. */
4b514bc8
JK
2536
2537static dwarf2_per_cu_data *
168c9250
SM
2538create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2539 struct dwarf2_section_info *section,
2540 int is_dwz,
2541 sect_offset sect_off, ULONGEST length)
4b514bc8 2542{
168c9250 2543 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2544 the_cu->sect_off = sect_off;
2545 the_cu->length = length;
4b514bc8 2546 the_cu->section = section;
168c9250 2547 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2548 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2549 the_cu->is_dwz = is_dwz;
2550 return the_cu;
2551}
2552
2ec9a5e0
TT
2553/* A helper for create_cus_from_index that handles a given list of
2554 CUs. */
2fdf6df6 2555
74a0d9f6 2556static void
168c9250 2557create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2558 const gdb_byte *cu_list, offset_type n_elements,
2559 struct dwarf2_section_info *section,
b76e467d 2560 int is_dwz)
9291a0cd 2561{
12359b5e 2562 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2563 {
74a0d9f6 2564 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2565
2566 sect_offset sect_off
2567 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2568 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2569 cu_list += 2 * 8;
2570
b76e467d 2571 dwarf2_per_cu_data *per_cu
168c9250
SM
2572 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2573 length);
2574 per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2575 }
9291a0cd
TT
2576}
2577
2ec9a5e0 2578/* Read the CU list from the mapped index, and use it to create all
168c9250 2579 the CU objects for PER_BFD. */
2ec9a5e0 2580
74a0d9f6 2581static void
168c9250 2582create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2583 const gdb_byte *cu_list, offset_type cu_list_elements,
2584 const gdb_byte *dwz_list, offset_type dwz_elements)
2585{
168c9250
SM
2586 gdb_assert (per_bfd->all_comp_units.empty ());
2587 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2588
168c9250
SM
2589 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2590 &per_bfd->info, 0);
2ec9a5e0
TT
2591
2592 if (dwz_elements == 0)
74a0d9f6 2593 return;
2ec9a5e0 2594
168c9250
SM
2595 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2596 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2597 &dwz->info, 1);
2ec9a5e0
TT
2598}
2599
1fd400ff 2600/* Create the signatured type hash table from the index. */
673bfd45 2601
74a0d9f6 2602static void
12359b5e 2603create_signatured_type_table_from_index
168c9250
SM
2604 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2605 const gdb_byte *bytes, offset_type elements)
1fd400ff 2606{
168c9250
SM
2607 gdb_assert (per_bfd->all_type_units.empty ());
2608 per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2609
298e9637 2610 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2611
12359b5e 2612 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2613 {
52dc124a 2614 struct signatured_type *sig_type;
9c541725 2615 ULONGEST signature;
1fd400ff 2616 void **slot;
9c541725 2617 cu_offset type_offset_in_tu;
1fd400ff 2618
74a0d9f6 2619 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2620 sect_offset sect_off
2621 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2622 type_offset_in_tu
2623 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2624 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2625 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2626 bytes += 3 * 8;
2627
168c9250 2628 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2629 sig_type->signature = signature;
9c541725 2630 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2631 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2632 sig_type->per_cu.section = section;
9c541725 2633 sig_type->per_cu.sect_off = sect_off;
52dc124a 2634 sig_type->per_cu.v.quick
168c9250 2635 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2636 struct dwarf2_per_cu_quick_data);
2637
b0b6a987 2638 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2639 *slot = sig_type;
1fd400ff 2640
168c9250 2641 per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2642 }
2643
168c9250 2644 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2645}
2646
927aa2e7
JK
2647/* Create the signatured type hash table from .debug_names. */
2648
2649static void
2650create_signatured_type_table_from_debug_names
976ca316 2651 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2652 const mapped_debug_names &map,
2653 struct dwarf2_section_info *section,
2654 struct dwarf2_section_info *abbrev_section)
2655{
976ca316 2656 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2657
96b79293
TT
2658 section->read (objfile);
2659 abbrev_section->read (objfile);
927aa2e7 2660
976ca316
SM
2661 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2662 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2663
298e9637 2664 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2665
2666 for (uint32_t i = 0; i < map.tu_count; ++i)
2667 {
2668 struct signatured_type *sig_type;
927aa2e7 2669 void **slot;
927aa2e7
JK
2670
2671 sect_offset sect_off
2672 = (sect_offset) (extract_unsigned_integer
2673 (map.tu_table_reordered + i * map.offset_size,
2674 map.offset_size,
2675 map.dwarf5_byte_order));
2676
2677 comp_unit_head cu_header;
976ca316 2678 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2679 abbrev_section,
927aa2e7
JK
2680 section->buffer + to_underlying (sect_off),
2681 rcuh_kind::TYPE);
2682
976ca316 2683 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2684 sig_type->signature = cu_header.signature;
2685 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2686 sig_type->per_cu.is_debug_types = 1;
2687 sig_type->per_cu.section = section;
2688 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2689 sig_type->per_cu.v.quick
976ca316 2690 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2691 struct dwarf2_per_cu_quick_data);
2692
b0b6a987 2693 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2694 *slot = sig_type;
2695
976ca316 2696 per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2697 }
2698
976ca316 2699 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2700}
2701
9291a0cd
TT
2702/* Read the address map data from the mapped index, and use it to
2703 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2704
9291a0cd 2705static void
976ca316 2706create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2707 struct mapped_index *index)
9291a0cd 2708{
976ca316 2709 struct objfile *objfile = per_objfile->objfile;
08feed99 2710 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2711 const gdb_byte *iter, *end;
9291a0cd 2712 struct addrmap *mutable_map;
9291a0cd
TT
2713 CORE_ADDR baseaddr;
2714
8268c778
PA
2715 auto_obstack temp_obstack;
2716
9291a0cd
TT
2717 mutable_map = addrmap_create_mutable (&temp_obstack);
2718
f00a2de2
PA
2719 iter = index->address_table.data ();
2720 end = iter + index->address_table.size ();
9291a0cd 2721
b3b3bada 2722 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2723
2724 while (iter < end)
2725 {
2726 ULONGEST hi, lo, cu_index;
2727 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2728 iter += 8;
2729 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2730 iter += 8;
2731 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2732 iter += 4;
f652bce2 2733
24a55014 2734 if (lo > hi)
f652bce2 2735 {
b98664d3 2736 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2737 hex_string (lo), hex_string (hi));
24a55014 2738 continue;
f652bce2 2739 }
24a55014 2740
976ca316 2741 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2742 {
b98664d3 2743 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2744 (unsigned) cu_index);
24a55014 2745 continue;
f652bce2 2746 }
24a55014 2747
79748972
TT
2748 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2749 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2750 addrmap_set_empty (mutable_map, lo, hi - 1,
976ca316 2751 per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2752 }
2753
d320c2b5 2754 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2755 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2756}
2757
927aa2e7
JK
2758/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2759 populate the objfile's psymtabs_addrmap. */
2760
2761static void
976ca316 2762create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2763 struct dwarf2_section_info *section)
2764{
976ca316 2765 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2766 bfd *abfd = objfile->obfd;
08feed99 2767 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2768 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2769
2770 auto_obstack temp_obstack;
2771 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2772
2773 std::unordered_map<sect_offset,
2774 dwarf2_per_cu_data *,
2775 gdb::hash_enum<sect_offset>>
2776 debug_info_offset_to_per_cu;
976ca316 2777 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 2778 {
927aa2e7
JK
2779 const auto insertpair
2780 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2781 if (!insertpair.second)
2782 {
2783 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2784 "debug_info_offset %s, ignoring .debug_aranges."),
2785 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2786 return;
2787 }
2788 }
2789
96b79293 2790 section->read (objfile);
927aa2e7
JK
2791
2792 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2793
2794 const gdb_byte *addr = section->buffer;
2795
2796 while (addr < section->buffer + section->size)
2797 {
2798 const gdb_byte *const entry_addr = addr;
2799 unsigned int bytes_read;
2800
2801 const LONGEST entry_length = read_initial_length (abfd, addr,
2802 &bytes_read);
2803 addr += bytes_read;
2804
2805 const gdb_byte *const entry_end = addr + entry_length;
2806 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2807 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2808 if (addr + entry_length > section->buffer + section->size)
2809 {
47e3f474 2810 warning (_("Section .debug_aranges in %s entry at offset %s "
dda83cd7 2811 "length %s exceeds section length %s, "
927aa2e7 2812 "ignoring .debug_aranges."),
47e3f474
TV
2813 objfile_name (objfile),
2814 plongest (entry_addr - section->buffer),
927aa2e7
JK
2815 plongest (bytes_read + entry_length),
2816 pulongest (section->size));
2817 return;
2818 }
2819
2820 /* The version number. */
2821 const uint16_t version = read_2_bytes (abfd, addr);
2822 addr += 2;
2823 if (version != 2)
2824 {
47e3f474 2825 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2826 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2827 objfile_name (objfile),
2828 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2829 return;
2830 }
2831
2832 const uint64_t debug_info_offset
2833 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2834 addr += offset_size;
2835 const auto per_cu_it
2836 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2837 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2838 {
47e3f474 2839 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2840 "debug_info_offset %s does not exists, "
2841 "ignoring .debug_aranges."),
47e3f474
TV
2842 objfile_name (objfile),
2843 plongest (entry_addr - section->buffer),
927aa2e7
JK
2844 pulongest (debug_info_offset));
2845 return;
2846 }
2847 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2848
2849 const uint8_t address_size = *addr++;
2850 if (address_size < 1 || address_size > 8)
2851 {
47e3f474 2852 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2853 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2854 objfile_name (objfile),
2855 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2856 return;
2857 }
2858
2859 const uint8_t segment_selector_size = *addr++;
2860 if (segment_selector_size != 0)
2861 {
47e3f474 2862 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2863 "segment_selector_size %u is not supported, "
2864 "ignoring .debug_aranges."),
47e3f474
TV
2865 objfile_name (objfile),
2866 plongest (entry_addr - section->buffer),
927aa2e7
JK
2867 segment_selector_size);
2868 return;
2869 }
2870
2871 /* Must pad to an alignment boundary that is twice the address
dda83cd7
SM
2872 size. It is undocumented by the DWARF standard but GCC does
2873 use it. */
927aa2e7
JK
2874 for (size_t padding = ((-(addr - section->buffer))
2875 & (2 * address_size - 1));
dda83cd7 2876 padding > 0; padding--)
927aa2e7
JK
2877 if (*addr++ != 0)
2878 {
47e3f474 2879 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2880 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2881 objfile_name (objfile),
2882 plongest (entry_addr - section->buffer));
927aa2e7
JK
2883 return;
2884 }
2885
2886 for (;;)
2887 {
2888 if (addr + 2 * address_size > entry_end)
2889 {
47e3f474 2890 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2891 "address list is not properly terminated, "
2892 "ignoring .debug_aranges."),
47e3f474
TV
2893 objfile_name (objfile),
2894 plongest (entry_addr - section->buffer));
927aa2e7
JK
2895 return;
2896 }
2897 ULONGEST start = extract_unsigned_integer (addr, address_size,
2898 dwarf5_byte_order);
2899 addr += address_size;
2900 ULONGEST length = extract_unsigned_integer (addr, address_size,
2901 dwarf5_byte_order);
2902 addr += address_size;
2903 if (start == 0 && length == 0)
2904 break;
976ca316 2905 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
2906 {
2907 /* Symbol was eliminated due to a COMDAT group. */
2908 continue;
2909 }
2910 ULONGEST end = start + length;
79748972
TT
2911 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2912 - baseaddr);
2913 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2914 - baseaddr);
927aa2e7
JK
2915 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2916 }
2917 }
2918
d320c2b5 2919 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2920 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2921}
2922
9291a0cd
TT
2923/* Find a slot in the mapped index INDEX for the object named NAME.
2924 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2925 constant pool and return true. If NAME cannot be found, return
2926 false. */
2fdf6df6 2927
109483d9 2928static bool
9291a0cd
TT
2929find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2930 offset_type **vec_out)
2931{
0cf03b49 2932 offset_type hash;
9291a0cd 2933 offset_type slot, step;
559a7a62 2934 int (*cmp) (const char *, const char *);
9291a0cd 2935
791afaa2 2936 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2937 if (current_language->la_language == language_cplus
45280282
IB
2938 || current_language->la_language == language_fortran
2939 || current_language->la_language == language_d)
0cf03b49
JK
2940 {
2941 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2942 not contain any. */
a8719064 2943
72998fb3 2944 if (strchr (name, '(') != NULL)
0cf03b49 2945 {
109483d9 2946 without_params = cp_remove_params (name);
0cf03b49 2947
72998fb3 2948 if (without_params != NULL)
791afaa2 2949 name = without_params.get ();
0cf03b49
JK
2950 }
2951 }
2952
559a7a62 2953 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2954 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2955 simulate our NAME being searched is also lowercased. */
2956 hash = mapped_index_string_hash ((index->version == 4
dda83cd7 2957 && case_sensitivity == case_sensitive_off
559a7a62
JK
2958 ? 5 : index->version),
2959 name);
2960
f00a2de2
PA
2961 slot = hash & (index->symbol_table.size () - 1);
2962 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2963 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2964
2965 for (;;)
2966 {
9291a0cd 2967 const char *str;
f00a2de2
PA
2968
2969 const auto &bucket = index->symbol_table[slot];
2970 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2971 return false;
9291a0cd 2972
f00a2de2 2973 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2974 if (!cmp (name, str))
9291a0cd
TT
2975 {
2976 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2977 + MAYBE_SWAP (bucket.vec));
109483d9 2978 return true;
9291a0cd
TT
2979 }
2980
f00a2de2 2981 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2982 }
2983}
2984
4485a1c1
SM
2985/* A helper function that reads the .gdb_index from BUFFER and fills
2986 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2987 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2988 ok to use deprecated sections.
2989
2990 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2991 out parameters that are filled in with information about the CU and
2992 TU lists in the section.
2993
4485a1c1 2994 Returns true if all went well, false otherwise. */
2fdf6df6 2995
d33bc52e 2996static bool
3810f182 2997read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2998 bool deprecated_ok,
2999 gdb::array_view<const gdb_byte> buffer,
3000 struct mapped_index *map,
3001 const gdb_byte **cu_list,
3002 offset_type *cu_list_elements,
3003 const gdb_byte **types_list,
3004 offset_type *types_list_elements)
3005{
3006 const gdb_byte *addr = &buffer[0];
82430852 3007
9291a0cd 3008 /* Version check. */
4485a1c1 3009 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3010 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3011 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3012 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3013 indices. */
831adc1f 3014 if (version < 4)
481860b3
GB
3015 {
3016 static int warning_printed = 0;
3017 if (!warning_printed)
3018 {
3019 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3020 filename);
481860b3
GB
3021 warning_printed = 1;
3022 }
3023 return 0;
3024 }
3025 /* Index version 4 uses a different hash function than index version
3026 5 and later.
3027
3028 Versions earlier than 6 did not emit psymbols for inlined
3029 functions. Using these files will cause GDB not to be able to
3030 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3031 indices unless the user has done
3032 "set use-deprecated-index-sections on". */
2ec9a5e0 3033 if (version < 6 && !deprecated_ok)
481860b3
GB
3034 {
3035 static int warning_printed = 0;
3036 if (!warning_printed)
3037 {
e615022a
DE
3038 warning (_("\
3039Skipping deprecated .gdb_index section in %s.\n\
3040Do \"set use-deprecated-index-sections on\" before the file is read\n\
3041to use the section anyway."),
2ec9a5e0 3042 filename);
481860b3
GB
3043 warning_printed = 1;
3044 }
3045 return 0;
3046 }
796a7ff8 3047 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3048 of the TU (for symbols coming from TUs),
3049 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3050 Plus gold-generated indices can have duplicate entries for global symbols,
3051 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3052 These are just performance bugs, and we can't distinguish gdb-generated
3053 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3054
481860b3 3055 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3056 longer backward compatible. */
796a7ff8 3057 if (version > 8)
594e8718 3058 return 0;
9291a0cd 3059
559a7a62 3060 map->version = version;
9291a0cd 3061
4485a1c1 3062 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3063
4485a1c1 3064 int i = 0;
2ec9a5e0
TT
3065 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3066 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3067 / 8);
1fd400ff
TT
3068 ++i;
3069
2ec9a5e0
TT
3070 *types_list = addr + MAYBE_SWAP (metadata[i]);
3071 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3072 - MAYBE_SWAP (metadata[i]))
3073 / 8);
987d643c 3074 ++i;
1fd400ff 3075
f00a2de2
PA
3076 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3077 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3078 map->address_table
3079 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3080 ++i;
3081
f00a2de2
PA
3082 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3083 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3084 map->symbol_table
3085 = gdb::array_view<mapped_index::symbol_table_slot>
3086 ((mapped_index::symbol_table_slot *) symbol_table,
3087 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3088
f00a2de2 3089 ++i;
f9d83a0b 3090 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3091
2ec9a5e0
TT
3092 return 1;
3093}
3094
4485a1c1
SM
3095/* Callback types for dwarf2_read_gdb_index. */
3096
3097typedef gdb::function_view
5989a64e 3098 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3099 get_gdb_index_contents_ftype;
3100typedef gdb::function_view
3101 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3102 get_gdb_index_contents_dwz_ftype;
3103
927aa2e7 3104/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3105 elements of all the CUs and return 1. Otherwise, return 0. */
3106
3107static int
4485a1c1 3108dwarf2_read_gdb_index
976ca316 3109 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
3110 get_gdb_index_contents_ftype get_gdb_index_contents,
3111 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3112{
2ec9a5e0
TT
3113 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3114 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3115 struct dwz_file *dwz;
976ca316 3116 struct objfile *objfile = per_objfile->objfile;
f8c41851 3117 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 3118
4485a1c1 3119 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 3120 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
3121
3122 if (main_index_contents.empty ())
3123 return 0;
3124
3063847f 3125 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3126 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3127 use_deprecated_index_sections,
3128 main_index_contents, map.get (), &cu_list,
3129 &cu_list_elements, &types_list,
3130 &types_list_elements))
2ec9a5e0
TT
3131 return 0;
3132
0fefef59 3133 /* Don't use the index if it's empty. */
3063847f 3134 if (map->symbol_table.empty ())
0fefef59
DE
3135 return 0;
3136
2ec9a5e0
TT
3137 /* If there is a .dwz file, read it so we can get its CU list as
3138 well. */
f8c41851 3139 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 3140 if (dwz != NULL)
2ec9a5e0 3141 {
2ec9a5e0
TT
3142 struct mapped_index dwz_map;
3143 const gdb_byte *dwz_types_ignore;
3144 offset_type dwz_types_elements_ignore;
3145
4485a1c1
SM
3146 gdb::array_view<const gdb_byte> dwz_index_content
3147 = get_gdb_index_contents_dwz (objfile, dwz);
3148
3149 if (dwz_index_content.empty ())
3150 return 0;
3151
3810f182 3152 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3153 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3154 &dwz_list, &dwz_list_elements,
3155 &dwz_types_ignore,
3156 &dwz_types_elements_ignore))
2ec9a5e0
TT
3157 {
3158 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3159 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3160 return 0;
3161 }
3162 }
3163
f8c41851
SM
3164 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3165 dwz_list_elements);
1fd400ff 3166
8b70b953
TT
3167 if (types_list_elements)
3168 {
8b70b953
TT
3169 /* We can only handle a single .debug_types when we have an
3170 index. */
f8c41851 3171 if (per_bfd->types.size () != 1)
8b70b953
TT
3172 return 0;
3173
f8c41851 3174 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 3175
f8c41851 3176 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 3177 types_list_elements);
8b70b953 3178 }
9291a0cd 3179
976ca316 3180 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 3181
f8c41851
SM
3182 per_bfd->index_table = std::move (map);
3183 per_bfd->using_index = 1;
3184 per_bfd->quick_file_names_table =
3185 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3186
3187 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3188 objfiles using the same BFD. */
3189 gdb_assert (per_bfd->partial_symtabs == nullptr);
3190 per_bfd->partial_symtabs = objfile->partial_symtabs;
9291a0cd
TT
3191
3192 return 1;
3193}
3194
dee91e82 3195/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3196
dee91e82
DE
3197static void
3198dw2_get_file_names_reader (const struct die_reader_specs *reader,
3e225074 3199 struct die_info *comp_unit_die)
9291a0cd 3200{
dee91e82 3201 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3202 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 3203 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 3204 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3205 struct attribute *attr;
7b9f3c50
DE
3206 void **slot;
3207 struct quick_file_names *qfn;
9291a0cd 3208
0186c6a7
DE
3209 gdb_assert (! this_cu->is_debug_types);
3210
07261596
TT
3211 /* Our callers never want to match partial units -- instead they
3212 will match the enclosing full CU. */
3213 if (comp_unit_die->tag == DW_TAG_partial_unit)
3214 {
3215 this_cu->v.quick->no_file_data = 1;
3216 return;
3217 }
3218
0186c6a7 3219 lh_cu = this_cu;
7b9f3c50 3220 slot = NULL;
dee91e82 3221
fff8551c 3222 line_header_up lh;
9c541725 3223 sect_offset line_offset {};
fff8551c 3224
dee91e82 3225 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
d4df075e 3226 if (attr != nullptr && attr->form_is_unsigned ())
9291a0cd 3227 {
7b9f3c50
DE
3228 struct quick_file_names find_entry;
3229
d4df075e 3230 line_offset = (sect_offset) attr->as_unsigned ();
7b9f3c50
DE
3231
3232 /* We may have already read in this line header (TU line header sharing).
3233 If we have we're done. */
094b34ac 3234 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3235 find_entry.hash.line_sect_off = line_offset;
976ca316 3236 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3237 &find_entry, INSERT);
3238 if (*slot != NULL)
3239 {
9a3c8263 3240 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3241 return;
7b9f3c50
DE
3242 }
3243
3019eac3 3244 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3245 }
3246 if (lh == NULL)
3247 {
094b34ac 3248 lh_cu->v.quick->no_file_data = 1;
dee91e82 3249 return;
9291a0cd
TT
3250 }
3251
976ca316 3252 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3253 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3254 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3255 gdb_assert (slot != NULL);
3256 *slot = qfn;
9291a0cd 3257
d721ba37 3258 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3259
aa391654
TT
3260 int offset = 0;
3261 if (strcmp (fnd.name, "<unknown>") != 0)
3262 ++offset;
3263
7ba99d21 3264 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3265 qfn->file_names =
976ca316 3266 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 3267 qfn->num_file_names);
aa391654
TT
3268 if (offset != 0)
3269 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3270 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3271 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3272 fnd.comp_dir).release ();
7b9f3c50 3273 qfn->real_names = NULL;
9291a0cd 3274
094b34ac 3275 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3276}
3277
3278/* A helper for the "quick" functions which attempts to read the line
3279 table for THIS_CU. */
3280
3281static struct quick_file_names *
ab432490
SM
3282dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3283 dwarf2_per_objfile *per_objfile)
dee91e82 3284{
0186c6a7
DE
3285 /* This should never be called for TUs. */
3286 gdb_assert (! this_cu->is_debug_types);
3287 /* Nor type unit groups. */
197400e8 3288 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3289
dee91e82
DE
3290 if (this_cu->v.quick->file_names != NULL)
3291 return this_cu->v.quick->file_names;
3292 /* If we know there is no line data, no point in looking again. */
3293 if (this_cu->v.quick->no_file_data)
3294 return NULL;
3295
ab432490 3296 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3297 if (!reader.dummy_p)
eb5dd737 3298 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
dee91e82
DE
3299
3300 if (this_cu->v.quick->no_file_data)
3301 return NULL;
3302 return this_cu->v.quick->file_names;
9291a0cd
TT
3303}
3304
3305/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3306 real path for a given file name from the line table. */
2fdf6df6 3307
9291a0cd 3308static const char *
976ca316 3309dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3310 struct quick_file_names *qfn, int index)
9291a0cd 3311{
7b9f3c50 3312 if (qfn->real_names == NULL)
976ca316 3313 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3314 qfn->num_file_names, const char *);
9291a0cd 3315
7b9f3c50 3316 if (qfn->real_names[index] == NULL)
14278e1f 3317 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3318
7b9f3c50 3319 return qfn->real_names[index];
9291a0cd
TT
3320}
3321
39298a5d
TT
3322struct symtab *
3323dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
9291a0cd 3324{
976ca316
SM
3325 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3326 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3327 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3328
43f3e411
DE
3329 if (cust == NULL)
3330 return NULL;
ed2dc618 3331
43f3e411 3332 return compunit_primary_filetab (cust);
9291a0cd
TT
3333}
3334
7b9f3c50
DE
3335/* Traversal function for dw2_forget_cached_source_info. */
3336
3337static int
3338dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3339{
7b9f3c50 3340 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3341
7b9f3c50 3342 if (file_data->real_names)
9291a0cd 3343 {
7b9f3c50 3344 int i;
9291a0cd 3345
7b9f3c50 3346 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3347 {
7b9f3c50
DE
3348 xfree ((void*) file_data->real_names[i]);
3349 file_data->real_names[i] = NULL;
9291a0cd
TT
3350 }
3351 }
7b9f3c50
DE
3352
3353 return 1;
3354}
3355
39298a5d
TT
3356void
3357dwarf2_base_index_functions::forget_cached_source_info
3358 (struct objfile *objfile)
7b9f3c50 3359{
976ca316 3360 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3361
976ca316 3362 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3363 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3364}
3365
f8eba3c6
TT
3366/* Helper function for dw2_map_symtabs_matching_filename that expands
3367 the symtabs and calls the iterator. */
3368
3369static int
3370dw2_map_expand_apply (struct objfile *objfile,
3371 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3372 const char *name, const char *real_path,
14bc53a8 3373 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3374{
43f3e411 3375 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3376
3377 /* Don't visit already-expanded CUs. */
af758d11
SM
3378 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3379 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3380 return 0;
3381
3382 /* This may expand more than one symtab, and we want to iterate over
3383 all of them. */
97a1449a 3384 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3385
14bc53a8
PA
3386 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3387 last_made, callback);
f8eba3c6
TT
3388}
3389
3390/* Implementation of the map_symtabs_matching_filename method. */
3391
39298a5d
TT
3392bool
3393dwarf2_base_index_functions::map_symtabs_matching_filename
14bc53a8
PA
3394 (struct objfile *objfile, const char *name, const char *real_path,
3395 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3396{
c011a4f4 3397 const char *name_basename = lbasename (name);
976ca316 3398 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 3399
848e3e78
DE
3400 /* The rule is CUs specify all the files, including those used by
3401 any TU, so there's no need to scan TUs here. */
f4dc4d17 3402
976ca316 3403 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3404 {
3d7bb9d9 3405 /* We only need to look at symtabs not already expanded. */
976ca316 3406 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3407 continue;
3408
976ca316 3409 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3410 if (file_data == NULL)
9291a0cd
TT
3411 continue;
3412
b76e467d 3413 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3414 {
7b9f3c50 3415 const char *this_name = file_data->file_names[j];
da235a7c 3416 const char *this_real_name;
9291a0cd 3417
af529f8f 3418 if (compare_filenames_for_search (this_name, name))
9291a0cd 3419 {
f5b95b50 3420 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3421 callback))
3422 return true;
288e77a7 3423 continue;
4aac40c8 3424 }
9291a0cd 3425
c011a4f4
DE
3426 /* Before we invoke realpath, which can get expensive when many
3427 files are involved, do a quick comparison of the basenames. */
3428 if (! basenames_may_differ
3429 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3430 continue;
3431
976ca316 3432 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
da235a7c 3433 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3434 {
da235a7c 3435 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3436 callback))
3437 return true;
288e77a7 3438 continue;
da235a7c 3439 }
9291a0cd 3440
da235a7c
JK
3441 if (real_path != NULL)
3442 {
af529f8f
JK
3443 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3444 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3445 if (this_real_name != NULL
af529f8f 3446 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3447 {
f5b95b50 3448 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3449 callback))
3450 return true;
288e77a7 3451 continue;
9291a0cd
TT
3452 }
3453 }
3454 }
3455 }
3456
14bc53a8 3457 return false;
9291a0cd
TT
3458}
3459
da51c347
DE
3460/* Struct used to manage iterating over all CUs looking for a symbol. */
3461
3462struct dw2_symtab_iterator
9291a0cd 3463{
ed2dc618 3464 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3465 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3466 /* If set, only look for symbols that match that block. Valid values are
3467 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3468 gdb::optional<block_enum> block_index;
da51c347
DE
3469 /* The kind of symbol we're looking for. */
3470 domain_enum domain;
3471 /* The list of CUs from the index entry of the symbol,
3472 or NULL if not found. */
3473 offset_type *vec;
3474 /* The next element in VEC to look at. */
3475 int next;
3476 /* The number of elements in VEC, or zero if there is no match. */
3477 int length;
8943b874
DE
3478 /* Have we seen a global version of the symbol?
3479 If so we can ignore all further global instances.
3480 This is to work around gold/15646, inefficient gold-generated
3481 indices. */
3482 int global_seen;
da51c347 3483};
9291a0cd 3484
e5f3ece2 3485/* Initialize the index symtab iterator ITER, common part. */
2fdf6df6 3486
9291a0cd 3487static void
e5f3ece2
TV
3488dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3489 dwarf2_per_objfile *per_objfile,
3490 gdb::optional<block_enum> block_index,
3491 domain_enum domain)
da51c347 3492{
976ca316 3493 iter->per_objfile = per_objfile;
da51c347
DE
3494 iter->block_index = block_index;
3495 iter->domain = domain;
3496 iter->next = 0;
8943b874 3497 iter->global_seen = 0;
e5f3ece2
TV
3498 iter->vec = NULL;
3499 iter->length = 0;
3500}
da51c347 3501
e5f3ece2
TV
3502/* Initialize the index symtab iterator ITER, const char *NAME variant. */
3503
3504static void
3505dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3506 dwarf2_per_objfile *per_objfile,
3507 gdb::optional<block_enum> block_index,
3508 domain_enum domain,
3509 const char *name)
3510{
3511 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
ed2dc618 3512
e5f3ece2 3513 mapped_index *index = per_objfile->per_bfd->index_table.get ();
ed2dc618 3514 /* index is NULL if OBJF_READNOW. */
e5f3ece2
TV
3515 if (index == NULL)
3516 return;
3517
3518 if (find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347 3519 iter->length = MAYBE_SWAP (*iter->vec);
e5f3ece2
TV
3520}
3521
3522/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3523
3524static void
3525dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3526 dwarf2_per_objfile *per_objfile,
3527 gdb::optional<block_enum> block_index,
3528 domain_enum domain, offset_type namei)
3529{
3530 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3531
3532 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3533 /* index is NULL if OBJF_READNOW. */
3534 if (index == NULL)
3535 return;
3536
3537 gdb_assert (!index->symbol_name_slot_invalid (namei));
3538 const auto &bucket = index->symbol_table[namei];
3539
3540 iter->vec = (offset_type *) (index->constant_pool
3541 + MAYBE_SWAP (bucket.vec));
3542 iter->length = MAYBE_SWAP (*iter->vec);
da51c347
DE
3543}
3544
3545/* Return the next matching CU or NULL if there are no more. */
3546
3547static struct dwarf2_per_cu_data *
3548dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3549{
976ca316 3550 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3551
da51c347
DE
3552 for ( ; iter->next < iter->length; ++iter->next)
3553 {
3554 offset_type cu_index_and_attrs =
3555 MAYBE_SWAP (iter->vec[iter->next + 1]);
3556 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3557 gdb_index_symbol_kind symbol_kind =
3558 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3559 /* Only check the symbol attributes if they're present.
3560 Indices prior to version 7 don't record them,
3561 and indices >= 7 may elide them for certain symbols
3562 (gold does this). */
3563 int attrs_valid =
976ca316 3564 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3565 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3566
3190f0c6 3567 /* Don't crash on bad data. */
976ca316
SM
3568 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3569 + per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3570 {
b98664d3 3571 complaint (_(".gdb_index entry has bad CU index"
976ca316 3572 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3573 continue;
3574 }
3575
976ca316 3576 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3577
da51c347 3578 /* Skip if already read in. */
976ca316 3579 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3580 continue;
3581
8943b874
DE
3582 /* Check static vs global. */
3583 if (attrs_valid)
3584 {
2b79f376
SM
3585 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3586
3587 if (iter->block_index.has_value ())
3588 {
3589 bool want_static = *iter->block_index == STATIC_BLOCK;
3590
3591 if (is_static != want_static)
3592 continue;
3593 }
3594
8943b874 3595 /* Work around gold/15646. */
f030440d
TV
3596 if (!is_static
3597 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3598 {
3599 if (iter->global_seen)
3600 continue;
3601
3602 iter->global_seen = 1;
3603 }
8943b874 3604 }
da51c347
DE
3605
3606 /* Only check the symbol's kind if it has one. */
3607 if (attrs_valid)
3608 {
3609 switch (iter->domain)
3610 {
3611 case VAR_DOMAIN:
3612 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3613 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3614 /* Some types are also in VAR_DOMAIN. */
3615 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3616 continue;
3617 break;
3618 case STRUCT_DOMAIN:
3619 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3620 continue;
3621 break;
3622 case LABEL_DOMAIN:
3623 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3624 continue;
3625 break;
59c35742
AB
3626 case MODULE_DOMAIN:
3627 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3628 continue;
3629 break;
da51c347
DE
3630 default:
3631 break;
3632 }
3633 }
3634
3635 ++iter->next;
3636 return per_cu;
3637 }
3638
3639 return NULL;
3640}
3641
39298a5d
TT
3642struct compunit_symtab *
3643dwarf2_gdb_index::lookup_symbol (struct objfile *objfile,
3644 block_enum block_index,
3645 const char *name, domain_enum domain)
9291a0cd 3646{
43f3e411 3647 struct compunit_symtab *stab_best = NULL;
976ca316 3648 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 3649
b5ec771e
PA
3650 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3651
ed2dc618
SM
3652 struct dw2_symtab_iterator iter;
3653 struct dwarf2_per_cu_data *per_cu;
da51c347 3654
976ca316 3655 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
9291a0cd 3656
ed2dc618
SM
3657 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3658 {
3659 struct symbol *sym, *with_opaque = NULL;
97a1449a 3660 struct compunit_symtab *stab
976ca316 3661 = dw2_instantiate_symtab (per_cu, per_objfile, false);
ed2dc618 3662 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3663 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3664
ed2dc618
SM
3665 sym = block_find_symbol (block, name, domain,
3666 block_find_non_opaque_type_preferred,
3667 &with_opaque);
b2e2f908 3668
ed2dc618
SM
3669 /* Some caution must be observed with overloaded functions
3670 and methods, since the index will not contain any overload
3671 information (but NAME might contain it). */
da51c347 3672
ed2dc618
SM
3673 if (sym != NULL
3674 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3675 return stab;
3676 if (with_opaque != NULL
3677 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3678 stab_best = stab;
da51c347 3679
ed2dc618 3680 /* Keep looking through other CUs. */
9291a0cd 3681 }
9291a0cd 3682
da51c347 3683 return stab_best;
9291a0cd
TT
3684}
3685
39298a5d
TT
3686void
3687dwarf2_base_index_functions::print_stats (struct objfile *objfile)
9291a0cd 3688{
976ca316
SM
3689 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3690 int total = (per_objfile->per_bfd->all_comp_units.size ()
3691 + per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3692 int count = 0;
9291a0cd 3693
ed2dc618 3694 for (int i = 0; i < total; ++i)
9291a0cd 3695 {
976ca316 3696 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3697
976ca316 3698 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3699 ++count;
3700 }
e4a48d9d 3701 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3702 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3703}
3704
779bd270
DE
3705/* This dumps minimal information about the index.
3706 It is called via "mt print objfiles".
3707 One use is to verify .gdb_index has been loaded by the
3708 gdb.dwarf2/gdb-index.exp testcase. */
3709
39298a5d
TT
3710void
3711dwarf2_gdb_index::dump (struct objfile *objfile)
9291a0cd 3712{
976ca316 3713 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3714
976ca316 3715 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3716 printf_filtered (".gdb_index:");
976ca316 3717 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3718 {
3719 printf_filtered (" version %d\n",
976ca316 3720 per_objfile->per_bfd->index_table->version);
779bd270
DE
3721 }
3722 else
3723 printf_filtered (" faked for \"readnow\"\n");
3724 printf_filtered ("\n");
9291a0cd
TT
3725}
3726
39298a5d
TT
3727void
3728dwarf2_gdb_index::expand_symtabs_for_function (struct objfile *objfile,
3729 const char *func_name)
9291a0cd 3730{
976ca316 3731 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
da51c347 3732
ed2dc618
SM
3733 struct dw2_symtab_iterator iter;
3734 struct dwarf2_per_cu_data *per_cu;
da51c347 3735
976ca316 3736 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3737
ed2dc618 3738 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3739 dw2_instantiate_symtab (per_cu, per_objfile, false);
da51c347 3740
9291a0cd
TT
3741}
3742
39298a5d
TT
3743void
3744dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
9291a0cd 3745{
976ca316
SM
3746 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3747 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3748 + per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3749
ed2dc618 3750 for (int i = 0; i < total_units; ++i)
9291a0cd 3751 {
976ca316 3752 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3753
58f0c718
TT
3754 /* We don't want to directly expand a partial CU, because if we
3755 read it with the wrong language, then assertion failures can
3756 be triggered later on. See PR symtab/23010. So, tell
3757 dw2_instantiate_symtab to skip partial CUs -- any important
3758 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3759 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3760 }
3761}
3762
39298a5d
TT
3763void
3764dwarf2_base_index_functions::expand_symtabs_with_fullname
3765 (struct objfile *objfile, const char *fullname)
9291a0cd 3766{
976ca316 3767 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3768
3769 /* We don't need to consider type units here.
3770 This is only called for examining code, e.g. expand_line_sal.
3771 There can be an order of magnitude (or more) more type units
3772 than comp units, and we avoid them if we can. */
3773
976ca316 3774 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3775 {
3d7bb9d9 3776 /* We only need to look at symtabs not already expanded. */
976ca316 3777 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3778 continue;
3779
976ca316 3780 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3781 if (file_data == NULL)
9291a0cd
TT
3782 continue;
3783
b76e467d 3784 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3785 {
652a8996
JK
3786 const char *this_fullname = file_data->file_names[j];
3787
3788 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3789 {
976ca316 3790 dw2_instantiate_symtab (per_cu, per_objfile, false);
9291a0cd
TT
3791 break;
3792 }
3793 }
3794 }
3795}
3796
9a0bacfb
TV
3797static void
3798dw2_expand_symtabs_matching_symbol
3799 (mapped_index_base &index,
3800 const lookup_name_info &lookup_name_in,
3801 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3802 enum search_domain kind,
fcf23d5b
SM
3803 gdb::function_view<bool (offset_type)> match_callback,
3804 dwarf2_per_objfile *per_objfile);
9a0bacfb
TV
3805
3806static void
3807dw2_expand_symtabs_matching_one
97a1449a
SM
3808 (dwarf2_per_cu_data *per_cu,
3809 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3810 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3811 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3812
9291a0cd 3813static void
199b4314
TT
3814dw2_map_matching_symbols
3815 (struct objfile *objfile,
b054970d 3816 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3817 int global,
3818 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3819 symbol_compare_ftype *ordered_compare)
9291a0cd 3820{
1aa98955 3821 /* Used for Ada. */
976ca316 3822 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3823
9a0bacfb
TV
3824 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3825
976ca316 3826 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3827 {
976ca316 3828 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3829
9a0bacfb
TV
3830 const char *match_name = name.ada ().lookup_name ().c_str ();
3831 auto matcher = [&] (const char *symname)
3832 {
3833 if (ordered_compare == nullptr)
3834 return true;
3835 return ordered_compare (symname, match_name) == 0;
3836 };
3837
3838 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3839 [&] (offset_type namei)
3840 {
3841 struct dw2_symtab_iterator iter;
3842 struct dwarf2_per_cu_data *per_cu;
3843
976ca316 3844 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3845 namei);
9a0bacfb 3846 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3847 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3848 nullptr);
9a0bacfb 3849 return true;
976ca316 3850 }, per_objfile);
9a0bacfb
TV
3851 }
3852 else
3853 {
3854 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3855 proceed assuming all symtabs have been read in. */
3856 }
1aa98955
TV
3857
3858 for (compunit_symtab *cust : objfile->compunits ())
3859 {
3860 const struct block *block;
3861
3862 if (cust == NULL)
3863 continue;
3864 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3865 if (!iterate_over_symbols_terminated (block, name,
3866 domain, callback))
3867 return;
3868 }
9291a0cd
TT
3869}
3870
39298a5d
TT
3871void
3872dwarf2_gdb_index::map_matching_symbols
3873 (struct objfile *objfile,
3874 const lookup_name_info &name, domain_enum domain,
3875 int global,
3876 gdb::function_view<symbol_found_callback_ftype> callback,
3877 symbol_compare_ftype *ordered_compare)
3878{
3879 dw2_map_matching_symbols (objfile, name, domain, global, callback,
3880 ordered_compare);
3881}
3882
e1ef7d7a
PA
3883/* Starting from a search name, return the string that finds the upper
3884 bound of all strings that start with SEARCH_NAME in a sorted name
3885 list. Returns the empty string to indicate that the upper bound is
3886 the end of the list. */
3887
3888static std::string
3889make_sort_after_prefix_name (const char *search_name)
3890{
3891 /* When looking to complete "func", we find the upper bound of all
3892 symbols that start with "func" by looking for where we'd insert
3893 the closest string that would follow "func" in lexicographical
3894 order. Usually, that's "func"-with-last-character-incremented,
3895 i.e. "fund". Mind non-ASCII characters, though. Usually those
3896 will be UTF-8 multi-byte sequences, but we can't be certain.
3897 Especially mind the 0xff character, which is a valid character in
3898 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3899 rule out compilers allowing it in identifiers. Note that
3900 conveniently, strcmp/strcasecmp are specified to compare
3901 characters interpreted as unsigned char. So what we do is treat
3902 the whole string as a base 256 number composed of a sequence of
3903 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3904 to 0, and carries 1 to the following more-significant position.
3905 If the very first character in SEARCH_NAME ends up incremented
3906 and carries/overflows, then the upper bound is the end of the
3907 list. The string after the empty string is also the empty
3908 string.
3909
3910 Some examples of this operation:
3911
3912 SEARCH_NAME => "+1" RESULT
3913
3914 "abc" => "abd"
3915 "ab\xff" => "ac"
3916 "\xff" "a" "\xff" => "\xff" "b"
3917 "\xff" => ""
3918 "\xff\xff" => ""
3919 "" => ""
3920
3921 Then, with these symbols for example:
3922
3923 func
3924 func1
3925 fund
3926
3927 completing "func" looks for symbols between "func" and
3928 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3929 which finds "func" and "func1", but not "fund".
3930
3931 And with:
3932
3933 funcÿ (Latin1 'ÿ' [0xff])
3934 funcÿ1
3935 fund
3936
3937 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3938 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3939
3940 And with:
3941
3942 ÿÿ (Latin1 'ÿ' [0xff])
3943 ÿÿ1
3944
3945 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3946 the end of the list.
3947 */
3948 std::string after = search_name;
3949 while (!after.empty () && (unsigned char) after.back () == 0xff)
3950 after.pop_back ();
3951 if (!after.empty ())
3952 after.back () = (unsigned char) after.back () + 1;
3953 return after;
3954}
3955
5c58de74 3956/* See declaration. */
61d96d7e 3957
5c58de74
PA
3958std::pair<std::vector<name_component>::const_iterator,
3959 std::vector<name_component>::const_iterator>
44ed8f3e 3960mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3961 (const lookup_name_info &lookup_name_without_params, language lang,
3962 dwarf2_per_objfile *per_objfile) const
3f563c84 3963{
5c58de74
PA
3964 auto *name_cmp
3965 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3966
3b00ef10 3967 const char *lang_name
e0802d59 3968 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3969
3f563c84
PA
3970 /* Comparison function object for lower_bound that matches against a
3971 given symbol name. */
3972 auto lookup_compare_lower = [&] (const name_component &elem,
3973 const char *name)
3974 {
fcf23d5b 3975 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3976 const char *elem_name = elem_qualified + elem.name_offset;
3977 return name_cmp (elem_name, name) < 0;
3978 };
3979
3980 /* Comparison function object for upper_bound that matches against a
3981 given symbol name. */
3982 auto lookup_compare_upper = [&] (const char *name,
3983 const name_component &elem)
3984 {
fcf23d5b 3985 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3986 const char *elem_name = elem_qualified + elem.name_offset;
3987 return name_cmp (name, elem_name) < 0;
3988 };
3989
5c58de74
PA
3990 auto begin = this->name_components.begin ();
3991 auto end = this->name_components.end ();
3f563c84
PA
3992
3993 /* Find the lower bound. */
3994 auto lower = [&] ()
3995 {
3b00ef10 3996 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3997 return begin;
3998 else
3b00ef10 3999 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4000 } ();
4001
4002 /* Find the upper bound. */
4003 auto upper = [&] ()
4004 {
5c58de74 4005 if (lookup_name_without_params.completion_mode ())
3f563c84 4006 {
e1ef7d7a
PA
4007 /* In completion mode, we want UPPER to point past all
4008 symbols names that have the same prefix. I.e., with
4009 these symbols, and completing "func":
4010
4011 function << lower bound
4012 function1
4013 other_function << upper bound
4014
4015 We find the upper bound by looking for the insertion
4016 point of "func"-with-last-character-incremented,
4017 i.e. "fund". */
3b00ef10 4018 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4019 if (after.empty ())
3f563c84 4020 return end;
e6b2f5ef
PA
4021 return std::lower_bound (lower, end, after.c_str (),
4022 lookup_compare_lower);
3f563c84
PA
4023 }
4024 else
3b00ef10 4025 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4026 } ();
4027
5c58de74
PA
4028 return {lower, upper};
4029}
4030
4031/* See declaration. */
4032
4033void
fcf23d5b 4034mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
4035{
4036 if (!this->name_components.empty ())
4037 return;
4038
4039 this->name_components_casing = case_sensitivity;
4040 auto *name_cmp
4041 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4042
4043 /* The code below only knows how to break apart components of C++
4044 symbol names (and other languages that use '::' as
3b00ef10 4045 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4046 auto count = this->symbol_name_count ();
4047 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4048 {
44ed8f3e 4049 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4050 continue;
4051
fcf23d5b 4052 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
4053
4054 /* Add each name component to the name component table. */
4055 unsigned int previous_len = 0;
3b00ef10
TT
4056
4057 if (strstr (name, "::") != nullptr)
4058 {
4059 for (unsigned int current_len = cp_find_first_component (name);
4060 name[current_len] != '\0';
4061 current_len += cp_find_first_component (name + current_len))
4062 {
4063 gdb_assert (name[current_len] == ':');
4064 this->name_components.push_back ({previous_len, idx});
4065 /* Skip the '::'. */
4066 current_len += 2;
4067 previous_len = current_len;
4068 }
4069 }
4070 else
5c58de74 4071 {
3b00ef10
TT
4072 /* Handle the Ada encoded (aka mangled) form here. */
4073 for (const char *iter = strstr (name, "__");
4074 iter != nullptr;
4075 iter = strstr (iter, "__"))
4076 {
4077 this->name_components.push_back ({previous_len, idx});
4078 iter += 2;
4079 previous_len = iter - name;
4080 }
5c58de74 4081 }
3b00ef10 4082
5c58de74
PA
4083 this->name_components.push_back ({previous_len, idx});
4084 }
4085
4086 /* Sort name_components elements by name. */
4087 auto name_comp_compare = [&] (const name_component &left,
4088 const name_component &right)
4089 {
fcf23d5b
SM
4090 const char *left_qualified
4091 = this->symbol_name_at (left.idx, per_objfile);
4092 const char *right_qualified
4093 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
4094
4095 const char *left_name = left_qualified + left.name_offset;
4096 const char *right_name = right_qualified + right.name_offset;
4097
4098 return name_cmp (left_name, right_name) < 0;
4099 };
4100
4101 std::sort (this->name_components.begin (),
4102 this->name_components.end (),
4103 name_comp_compare);
4104}
4105
4106/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4107 mapped_index_base instead of the containing objfile. This is split
4108 to a separate function in order to be able to unit test the
4109 name_components matching using a mock mapped_index_base. For each
5c58de74 4110 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4111 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4112
4113static void
4114dw2_expand_symtabs_matching_symbol
44ed8f3e 4115 (mapped_index_base &index,
5c58de74
PA
4116 const lookup_name_info &lookup_name_in,
4117 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4118 enum search_domain kind,
fcf23d5b
SM
4119 gdb::function_view<bool (offset_type)> match_callback,
4120 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4121{
4122 lookup_name_info lookup_name_without_params
4123 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4124
4125 /* Build the symbol name component sorted vector, if we haven't
4126 yet. */
fcf23d5b 4127 index.build_name_components (per_objfile);
5c58de74 4128
3f563c84
PA
4129 /* The same symbol may appear more than once in the range though.
4130 E.g., if we're looking for symbols that complete "w", and we have
4131 a symbol named "w1::w2", we'll find the two name components for
4132 that same symbol in the range. To be sure we only call the
4133 callback once per symbol, we first collect the symbol name
4134 indexes that matched in a temporary vector and ignore
4135 duplicates. */
4136 std::vector<offset_type> matches;
3f563c84 4137
3b00ef10
TT
4138 struct name_and_matcher
4139 {
4140 symbol_name_matcher_ftype *matcher;
ecc6c606 4141 const char *name;
3b00ef10
TT
4142
4143 bool operator== (const name_and_matcher &other) const
3f563c84 4144 {
ecc6c606 4145 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4146 }
4147 };
4148
4149 /* A vector holding all the different symbol name matchers, for all
4150 languages. */
4151 std::vector<name_and_matcher> matchers;
4152
4153 for (int i = 0; i < nr_languages; i++)
4154 {
4155 enum language lang_e = (enum language) i;
4156
4157 const language_defn *lang = language_def (lang_e);
4158 symbol_name_matcher_ftype *name_matcher
c9debfb9 4159 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 4160
3b00ef10 4161 name_and_matcher key {
dda83cd7 4162 name_matcher,
3b00ef10
TT
4163 lookup_name_without_params.language_lookup_name (lang_e)
4164 };
4165
4166 /* Don't insert the same comparison routine more than once.
4167 Note that we do this linear walk. This is not a problem in
4168 practice because the number of supported languages is
4169 low. */
4170 if (std::find (matchers.begin (), matchers.end (), key)
4171 != matchers.end ())
9291a0cd 4172 continue;
3b00ef10
TT
4173 matchers.push_back (std::move (key));
4174
4175 auto bounds
4176 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 4177 lang_e, per_objfile);
3b00ef10
TT
4178
4179 /* Now for each symbol name in range, check to see if we have a name
4180 match, and if so, call the MATCH_CALLBACK callback. */
4181
4182 for (; bounds.first != bounds.second; ++bounds.first)
4183 {
fcf23d5b
SM
4184 const char *qualified
4185 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
4186
4187 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4188 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4189 continue;
9291a0cd 4190
3b00ef10
TT
4191 matches.push_back (bounds.first->idx);
4192 }
3f563c84
PA
4193 }
4194
4195 std::sort (matches.begin (), matches.end ());
4196
4197 /* Finally call the callback, once per match. */
4198 ULONGEST prev = -1;
4199 for (offset_type idx : matches)
4200 {
4201 if (prev != idx)
4202 {
3b00ef10
TT
4203 if (!match_callback (idx))
4204 break;
3f563c84
PA
4205 prev = idx;
4206 }
4207 }
4208
4209 /* Above we use a type wider than idx's for 'prev', since 0 and
4210 (offset_type)-1 are both possible values. */
4211 static_assert (sizeof (prev) > sizeof (offset_type), "");
4212}
4213
c62446b1
PA
4214#if GDB_SELF_TEST
4215
4216namespace selftests { namespace dw2_expand_symtabs_matching {
4217
a3c5fafd
PA
4218/* A mock .gdb_index/.debug_names-like name index table, enough to
4219 exercise dw2_expand_symtabs_matching_symbol, which works with the
4220 mapped_index_base interface. Builds an index from the symbol list
4221 passed as parameter to the constructor. */
4222class mock_mapped_index : public mapped_index_base
c62446b1
PA
4223{
4224public:
a3c5fafd
PA
4225 mock_mapped_index (gdb::array_view<const char *> symbols)
4226 : m_symbol_table (symbols)
c62446b1
PA
4227 {}
4228
a3c5fafd 4229 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4230
a3c5fafd 4231 /* Return the number of names in the symbol table. */
632e107b 4232 size_t symbol_name_count () const override
c62446b1 4233 {
a3c5fafd 4234 return m_symbol_table.size ();
c62446b1
PA
4235 }
4236
a3c5fafd 4237 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
4238 const char *symbol_name_at
4239 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
4240 {
4241 return m_symbol_table[idx];
4242 }
c62446b1 4243
a3c5fafd
PA
4244private:
4245 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4246};
4247
4248/* Convenience function that converts a NULL pointer to a "<null>"
4249 string, to pass to print routines. */
4250
4251static const char *
4252string_or_null (const char *str)
4253{
4254 return str != NULL ? str : "<null>";
4255}
4256
4257/* Check if a lookup_name_info built from
4258 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4259 index. EXPECTED_LIST is the list of expected matches, in expected
4260 matching order. If no match expected, then an empty list is
4261 specified. Returns true on success. On failure prints a warning
4262 indicating the file:line that failed, and returns false. */
4263
4264static bool
4265check_match (const char *file, int line,
4266 mock_mapped_index &mock_index,
4267 const char *name, symbol_name_match_type match_type,
4268 bool completion_mode,
fcf23d5b
SM
4269 std::initializer_list<const char *> expected_list,
4270 dwarf2_per_objfile *per_objfile)
c62446b1
PA
4271{
4272 lookup_name_info lookup_name (name, match_type, completion_mode);
4273
4274 bool matched = true;
4275
4276 auto mismatch = [&] (const char *expected_str,
4277 const char *got)
4278 {
4279 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4280 "expected=\"%s\", got=\"%s\"\n"),
4281 file, line,
4282 (match_type == symbol_name_match_type::FULL
4283 ? "FULL" : "WILD"),
4284 name, string_or_null (expected_str), string_or_null (got));
4285 matched = false;
4286 };
4287
4288 auto expected_it = expected_list.begin ();
4289 auto expected_end = expected_list.end ();
4290
a3c5fafd 4291 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4292 NULL, ALL_DOMAIN,
4293 [&] (offset_type idx)
4294 {
fcf23d5b 4295 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
4296 const char *expected_str
4297 = expected_it == expected_end ? NULL : *expected_it++;
4298
4299 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4300 mismatch (expected_str, matched_name);
3b00ef10 4301 return true;
fcf23d5b 4302 }, per_objfile);
c62446b1
PA
4303
4304 const char *expected_str
4305 = expected_it == expected_end ? NULL : *expected_it++;
4306 if (expected_str != NULL)
4307 mismatch (expected_str, NULL);
4308
4309 return matched;
4310}
4311
4312/* The symbols added to the mock mapped_index for testing (in
4313 canonical form). */
4314static const char *test_symbols[] = {
4315 "function",
4316 "std::bar",
4317 "std::zfunction",
4318 "std::zfunction2",
4319 "w1::w2",
4320 "ns::foo<char*>",
4321 "ns::foo<int>",
4322 "ns::foo<long>",
a20714ff
PA
4323 "ns2::tmpl<int>::foo2",
4324 "(anonymous namespace)::A::B::C",
c62446b1 4325
e1ef7d7a
PA
4326 /* These are used to check that the increment-last-char in the
4327 matching algorithm for completion doesn't match "t1_fund" when
4328 completing "t1_func". */
4329 "t1_func",
4330 "t1_func1",
4331 "t1_fund",
4332 "t1_fund1",
4333
4334 /* A UTF-8 name with multi-byte sequences to make sure that
4335 cp-name-parser understands this as a single identifier ("função"
4336 is "function" in PT). */
4337 u8"u8função",
4338
4339 /* \377 (0xff) is Latin1 'ÿ'. */
4340 "yfunc\377",
4341
4342 /* \377 (0xff) is Latin1 'ÿ'. */
4343 "\377",
4344 "\377\377123",
4345
c62446b1
PA
4346 /* A name with all sorts of complications. Starts with "z" to make
4347 it easier for the completion tests below. */
4348#define Z_SYM_NAME \
4349 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4350 "::tuple<(anonymous namespace)::ui*, " \
4351 "std::default_delete<(anonymous namespace)::ui>, void>"
4352
4353 Z_SYM_NAME
4354};
4355
a3c5fafd
PA
4356/* Returns true if the mapped_index_base::find_name_component_bounds
4357 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4358 in completion mode. */
5c58de74
PA
4359
4360static bool
a3c5fafd 4361check_find_bounds_finds (mapped_index_base &index,
5c58de74 4362 const char *search_name,
fcf23d5b
SM
4363 gdb::array_view<const char *> expected_syms,
4364 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4365{
4366 lookup_name_info lookup_name (search_name,
4367 symbol_name_match_type::FULL, true);
4368
3b00ef10 4369 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4370 language_cplus,
4371 per_objfile);
5c58de74
PA
4372
4373 size_t distance = std::distance (bounds.first, bounds.second);
4374 if (distance != expected_syms.size ())
4375 return false;
4376
4377 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4378 {
4379 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4380 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4381 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4382 return false;
4383 }
4384
4385 return true;
4386}
4387
4388/* Test the lower-level mapped_index::find_name_component_bounds
4389 method. */
4390
c62446b1 4391static void
5c58de74
PA
4392test_mapped_index_find_name_component_bounds ()
4393{
4394 mock_mapped_index mock_index (test_symbols);
4395
fcf23d5b 4396 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4397
4398 /* Test the lower-level mapped_index::find_name_component_bounds
4399 method in completion mode. */
4400 {
4401 static const char *expected_syms[] = {
4402 "t1_func",
4403 "t1_func1",
5c58de74
PA
4404 };
4405
fcf23d5b
SM
4406 SELF_CHECK (check_find_bounds_finds
4407 (mock_index, "t1_func", expected_syms,
4408 NULL /* per_objfile */));
5c58de74
PA
4409 }
4410
4411 /* Check that the increment-last-char in the name matching algorithm
4412 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4413 {
4414 static const char *expected_syms1[] = {
4415 "\377",
4416 "\377\377123",
4417 };
fcf23d5b
SM
4418 SELF_CHECK (check_find_bounds_finds
4419 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4420
4421 static const char *expected_syms2[] = {
4422 "\377\377123",
4423 };
fcf23d5b
SM
4424 SELF_CHECK (check_find_bounds_finds
4425 (mock_index, "\377\377", expected_syms2,
4426 NULL /* per_objfile */));
5c58de74
PA
4427 }
4428}
4429
4430/* Test dw2_expand_symtabs_matching_symbol. */
4431
4432static void
4433test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4434{
4435 mock_mapped_index mock_index (test_symbols);
4436
4437 /* We let all tests run until the end even if some fails, for debug
4438 convenience. */
4439 bool any_mismatch = false;
4440
4441 /* Create the expected symbols list (an initializer_list). Needed
4442 because lists have commas, and we need to pass them to CHECK,
4443 which is a macro. */
4444#define EXPECT(...) { __VA_ARGS__ }
4445
4446 /* Wrapper for check_match that passes down the current
4447 __FILE__/__LINE__. */
4448#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4449 any_mismatch |= !check_match (__FILE__, __LINE__, \
4450 mock_index, \
4451 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4452 EXPECTED_LIST, NULL)
c62446b1
PA
4453
4454 /* Identity checks. */
4455 for (const char *sym : test_symbols)
4456 {
4457 /* Should be able to match all existing symbols. */
4458 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4459 EXPECT (sym));
4460
4461 /* Should be able to match all existing symbols with
4462 parameters. */
4463 std::string with_params = std::string (sym) + "(int)";
4464 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4465 EXPECT (sym));
4466
4467 /* Should be able to match all existing symbols with
4468 parameters and qualifiers. */
4469 with_params = std::string (sym) + " ( int ) const";
4470 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4471 EXPECT (sym));
4472
4473 /* This should really find sym, but cp-name-parser.y doesn't
4474 know about lvalue/rvalue qualifiers yet. */
4475 with_params = std::string (sym) + " ( int ) &&";
4476 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4477 {});
4478 }
4479
e1ef7d7a
PA
4480 /* Check that the name matching algorithm for completion doesn't get
4481 confused with Latin1 'ÿ' / 0xff. */
4482 {
4483 static const char str[] = "\377";
4484 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4485 EXPECT ("\377", "\377\377123"));
4486 }
4487
4488 /* Check that the increment-last-char in the matching algorithm for
4489 completion doesn't match "t1_fund" when completing "t1_func". */
4490 {
4491 static const char str[] = "t1_func";
4492 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4493 EXPECT ("t1_func", "t1_func1"));
4494 }
4495
c62446b1
PA
4496 /* Check that completion mode works at each prefix of the expected
4497 symbol name. */
4498 {
4499 static const char str[] = "function(int)";
4500 size_t len = strlen (str);
4501 std::string lookup;
4502
4503 for (size_t i = 1; i < len; i++)
4504 {
4505 lookup.assign (str, i);
4506 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4507 EXPECT ("function"));
4508 }
4509 }
4510
4511 /* While "w" is a prefix of both components, the match function
4512 should still only be called once. */
4513 {
4514 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4515 EXPECT ("w1::w2"));
a20714ff
PA
4516 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4517 EXPECT ("w1::w2"));
c62446b1
PA
4518 }
4519
4520 /* Same, with a "complicated" symbol. */
4521 {
4522 static const char str[] = Z_SYM_NAME;
4523 size_t len = strlen (str);
4524 std::string lookup;
4525
4526 for (size_t i = 1; i < len; i++)
4527 {
4528 lookup.assign (str, i);
4529 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4530 EXPECT (Z_SYM_NAME));
4531 }
4532 }
4533
4534 /* In FULL mode, an incomplete symbol doesn't match. */
4535 {
4536 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4537 {});
4538 }
4539
4540 /* A complete symbol with parameters matches any overload, since the
4541 index has no overload info. */
4542 {
4543 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4544 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4545 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4546 EXPECT ("std::zfunction", "std::zfunction2"));
4547 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4548 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4549 }
4550
4551 /* Check that whitespace is ignored appropriately. A symbol with a
4552 template argument list. */
4553 {
4554 static const char expected[] = "ns::foo<int>";
4555 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4556 EXPECT (expected));
a20714ff
PA
4557 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4558 EXPECT (expected));
c62446b1
PA
4559 }
4560
4561 /* Check that whitespace is ignored appropriately. A symbol with a
4562 template argument list that includes a pointer. */
4563 {
4564 static const char expected[] = "ns::foo<char*>";
4565 /* Try both completion and non-completion modes. */
4566 static const bool completion_mode[2] = {false, true};
4567 for (size_t i = 0; i < 2; i++)
4568 {
4569 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4570 completion_mode[i], EXPECT (expected));
a20714ff
PA
4571 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4572 completion_mode[i], EXPECT (expected));
c62446b1
PA
4573
4574 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4575 completion_mode[i], EXPECT (expected));
a20714ff
PA
4576 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4577 completion_mode[i], EXPECT (expected));
c62446b1
PA
4578 }
4579 }
4580
4581 {
4582 /* Check method qualifiers are ignored. */
4583 static const char expected[] = "ns::foo<char*>";
4584 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4585 symbol_name_match_type::FULL, true, EXPECT (expected));
4586 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4587 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4588 CHECK_MATCH ("foo < char * > ( int ) const",
4589 symbol_name_match_type::WILD, true, EXPECT (expected));
4590 CHECK_MATCH ("foo < char * > ( int ) &&",
4591 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4592 }
4593
4594 /* Test lookup names that don't match anything. */
4595 {
a20714ff
PA
4596 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4597 {});
4598
c62446b1
PA
4599 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4600 {});
4601 }
4602
a20714ff
PA
4603 /* Some wild matching tests, exercising "(anonymous namespace)",
4604 which should not be confused with a parameter list. */
4605 {
4606 static const char *syms[] = {
4607 "A::B::C",
4608 "B::C",
4609 "C",
4610 "A :: B :: C ( int )",
4611 "B :: C ( int )",
4612 "C ( int )",
4613 };
4614
4615 for (const char *s : syms)
4616 {
4617 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4618 EXPECT ("(anonymous namespace)::A::B::C"));
4619 }
4620 }
4621
4622 {
4623 static const char expected[] = "ns2::tmpl<int>::foo2";
4624 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4625 EXPECT (expected));
4626 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4627 EXPECT (expected));
4628 }
4629
c62446b1
PA
4630 SELF_CHECK (!any_mismatch);
4631
4632#undef EXPECT
4633#undef CHECK_MATCH
4634}
4635
5c58de74
PA
4636static void
4637run_test ()
4638{
4639 test_mapped_index_find_name_component_bounds ();
4640 test_dw2_expand_symtabs_matching_symbol ();
4641}
4642
c62446b1
PA
4643}} // namespace selftests::dw2_expand_symtabs_matching
4644
4645#endif /* GDB_SELF_TEST */
4646
4b514bc8
JK
4647/* If FILE_MATCHER is NULL or if PER_CU has
4648 dwarf2_per_cu_quick_data::MARK set (see
4649 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4650 EXPANSION_NOTIFY on it. */
4651
4652static void
4653dw2_expand_symtabs_matching_one
97a1449a
SM
4654 (dwarf2_per_cu_data *per_cu,
4655 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4656 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4657 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4658{
4659 if (file_matcher == NULL || per_cu->v.quick->mark)
4660 {
af758d11 4661 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4662
97a1449a
SM
4663 compunit_symtab *symtab
4664 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4665 gdb_assert (symtab != nullptr);
4b514bc8 4666
af758d11
SM
4667 if (expansion_notify != NULL && symtab_was_null)
4668 expansion_notify (symtab);
4b514bc8
JK
4669 }
4670}
4671
3f563c84
PA
4672/* Helper for dw2_expand_matching symtabs. Called on each symbol
4673 matched, to expand corresponding CUs that were marked. IDX is the
4674 index of the symbol name that matched. */
4675
4676static void
4677dw2_expand_marked_cus
976ca316 4678 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4679 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4680 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4681 search_domain kind)
4682{
3f563c84
PA
4683 offset_type *vec, vec_len, vec_idx;
4684 bool global_seen = false;
976ca316 4685 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4686
61920122 4687 vec = (offset_type *) (index.constant_pool
f00a2de2 4688 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4689 vec_len = MAYBE_SWAP (vec[0]);
4690 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4691 {
61920122
PA
4692 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4693 /* This value is only valid for index versions >= 7. */
4694 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4695 gdb_index_symbol_kind symbol_kind =
4696 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4697 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4698 /* Only check the symbol attributes if they're present.
4699 Indices prior to version 7 don't record them,
4700 and indices >= 7 may elide them for certain symbols
4701 (gold does this). */
4702 int attrs_valid =
4703 (index.version >= 7
4704 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4705
4706 /* Work around gold/15646. */
f030440d
TV
4707 if (attrs_valid
4708 && !is_static
4709 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4710 {
f030440d 4711 if (global_seen)
61920122 4712 continue;
f030440d
TV
4713
4714 global_seen = true;
61920122 4715 }
3190f0c6 4716
61920122
PA
4717 /* Only check the symbol's kind if it has one. */
4718 if (attrs_valid)
4719 {
4720 switch (kind)
8943b874 4721 {
61920122
PA
4722 case VARIABLES_DOMAIN:
4723 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4724 continue;
4725 break;
4726 case FUNCTIONS_DOMAIN:
4727 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4728 continue;
61920122
PA
4729 break;
4730 case TYPES_DOMAIN:
4731 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4732 continue;
4733 break;
59c35742
AB
4734 case MODULES_DOMAIN:
4735 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4736 continue;
4737 break;
61920122
PA
4738 default:
4739 break;
8943b874 4740 }
61920122 4741 }
8943b874 4742
61920122 4743 /* Don't crash on bad data. */
976ca316
SM
4744 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4745 + per_objfile->per_bfd->all_type_units.size ()))
61920122 4746 {
b98664d3 4747 complaint (_(".gdb_index entry has bad CU index"
976ca316 4748 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4749 continue;
4750 }
4751
976ca316
SM
4752 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4753 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4b514bc8 4754 expansion_notify);
61920122
PA
4755 }
4756}
4757
4b514bc8
JK
4758/* If FILE_MATCHER is non-NULL, set all the
4759 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4760 that match FILE_MATCHER. */
4761
61920122 4762static void
4b514bc8 4763dw_expand_symtabs_matching_file_matcher
976ca316 4764 (dwarf2_per_objfile *per_objfile,
ed2dc618 4765 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4766{
4b514bc8 4767 if (file_matcher == NULL)
61920122
PA
4768 return;
4769
4b514bc8
JK
4770 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4771 htab_eq_pointer,
4772 NULL, xcalloc, xfree));
4773 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4774 htab_eq_pointer,
4775 NULL, xcalloc, xfree));
61920122 4776
4b514bc8
JK
4777 /* The rule is CUs specify all the files, including those used by
4778 any TU, so there's no need to scan TUs here. */
61920122 4779
976ca316 4780 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4781 {
927aa2e7
JK
4782 QUIT;
4783
4784 per_cu->v.quick->mark = 0;
4785
4786 /* We only need to look at symtabs not already expanded. */
976ca316 4787 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4788 continue;
4789
976ca316 4790 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4791 if (file_data == NULL)
4792 continue;
4793
4794 if (htab_find (visited_not_found.get (), file_data) != NULL)
4795 continue;
4796 else if (htab_find (visited_found.get (), file_data) != NULL)
4797 {
4798 per_cu->v.quick->mark = 1;
4799 continue;
4800 }
4801
b76e467d 4802 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4803 {
4804 const char *this_real_name;
4805
4806 if (file_matcher (file_data->file_names[j], false))
4807 {
4808 per_cu->v.quick->mark = 1;
4809 break;
4810 }
4811
4812 /* Before we invoke realpath, which can get expensive when many
4813 files are involved, do a quick comparison of the basenames. */
4814 if (!basenames_may_differ
4815 && !file_matcher (lbasename (file_data->file_names[j]),
4816 true))
4817 continue;
4818
976ca316 4819 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4820 if (file_matcher (this_real_name, false))
4821 {
4822 per_cu->v.quick->mark = 1;
4823 break;
4824 }
4825 }
4826
b76e467d
SM
4827 void **slot = htab_find_slot (per_cu->v.quick->mark
4828 ? visited_found.get ()
4829 : visited_not_found.get (),
4830 file_data, INSERT);
927aa2e7
JK
4831 *slot = file_data;
4832 }
4833}
4834
4835static void
4836dw2_expand_symtabs_matching
4837 (struct objfile *objfile,
4838 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4839 const lookup_name_info *lookup_name,
927aa2e7
JK
4840 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4841 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4842 enum search_domain kind)
4843{
976ca316 4844 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4845
4846 /* index_table is NULL if OBJF_READNOW. */
976ca316 4847 if (!per_objfile->per_bfd->index_table)
927aa2e7
JK
4848 return;
4849
976ca316 4850 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4851
c1a66c06
TV
4852 if (symbol_matcher == NULL && lookup_name == NULL)
4853 {
976ca316 4854 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4855 {
4856 QUIT;
4857
976ca316 4858 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
97a1449a 4859 file_matcher, expansion_notify);
c1a66c06
TV
4860 }
4861 return;
4862 }
4863
976ca316 4864 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4865
c1a66c06 4866 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4867 symbol_matcher,
4868 kind, [&] (offset_type idx)
4869 {
976ca316
SM
4870 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4871 kind);
3b00ef10 4872 return true;
976ca316 4873 }, per_objfile);
927aa2e7
JK
4874}
4875
39298a5d
TT
4876void
4877dwarf2_gdb_index::expand_symtabs_matching
4878 (struct objfile *objfile,
4879 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4880 const lookup_name_info *lookup_name,
4881 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4882 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4883 enum search_domain kind)
4884{
4885 dw2_expand_symtabs_matching (objfile, file_matcher, lookup_name,
4886 symbol_matcher, expansion_notify, kind);
4887}
4888
927aa2e7
JK
4889/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4890 symtab. */
4891
4892static struct compunit_symtab *
4893recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4894 CORE_ADDR pc)
4895{
4896 int i;
4897
4898 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4899 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4900 return cust;
4901
4902 if (cust->includes == NULL)
4903 return NULL;
4904
4905 for (i = 0; cust->includes[i]; ++i)
4906 {
4907 struct compunit_symtab *s = cust->includes[i];
4908
4909 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4910 if (s != NULL)
4911 return s;
4912 }
4913
4914 return NULL;
4915}
4916
39298a5d
TT
4917struct compunit_symtab *
4918dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4919 (struct objfile *objfile,
4920 struct bound_minimal_symbol msymbol,
4921 CORE_ADDR pc,
4922 struct obj_section *section,
4923 int warn_if_readin)
927aa2e7
JK
4924{
4925 struct dwarf2_per_cu_data *data;
4926 struct compunit_symtab *result;
4927
d320c2b5 4928 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4929 return NULL;
4930
b3b3bada 4931 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4932 data = (struct dwarf2_per_cu_data *) addrmap_find
4933 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4934 if (!data)
4935 return NULL;
4936
af758d11
SM
4937 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4938 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4939 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4940 paddress (objfile->arch (), pc));
927aa2e7 4941
97a1449a
SM
4942 result = recursively_find_pc_sect_compunit_symtab
4943 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4944
927aa2e7
JK
4945 gdb_assert (result != NULL);
4946 return result;
4947}
4948
39298a5d
TT
4949void
4950dwarf2_base_index_functions::map_symbol_filenames (struct objfile *objfile,
4951 symbol_filename_ftype *fun,
4952 void *data,
4953 int need_fullname)
927aa2e7 4954{
976ca316 4955 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4956
976ca316 4957 if (!per_objfile->per_bfd->filenames_cache)
927aa2e7 4958 {
976ca316 4959 per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4960
4961 htab_up visited (htab_create_alloc (10,
4962 htab_hash_pointer, htab_eq_pointer,
4963 NULL, xcalloc, xfree));
4964
4965 /* The rule is CUs specify all the files, including those used
4966 by any TU, so there's no need to scan TUs here. We can
4967 ignore file names coming from already-expanded CUs. */
4968
976ca316 4969 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4970 {
976ca316 4971 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4972 {
4973 void **slot = htab_find_slot (visited.get (),
4974 per_cu->v.quick->file_names,
4975 INSERT);
4976
4977 *slot = per_cu->v.quick->file_names;
4978 }
4979 }
4980
976ca316 4981 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4982 {
927aa2e7 4983 /* We only need to look at symtabs not already expanded. */
976ca316 4984 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4985 continue;
4986
ab432490 4987 quick_file_names *file_data
976ca316 4988 = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4989 if (file_data == NULL)
4990 continue;
4991
b76e467d 4992 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4993 if (*slot)
4994 {
4995 /* Already visited. */
4996 continue;
4997 }
4998 *slot = file_data;
4999
5000 for (int j = 0; j < file_data->num_file_names; ++j)
5001 {
5002 const char *filename = file_data->file_names[j];
976ca316 5003 per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
5004 }
5005 }
5006 }
5007
976ca316 5008 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
5009 {
5010 gdb::unique_xmalloc_ptr<char> this_real_name;
5011
5012 if (need_fullname)
5013 this_real_name = gdb_realpath (filename);
5014 (*fun) (filename, this_real_name.get (), data);
5015 });
5016}
5017
39298a5d
TT
5018bool
5019dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
927aa2e7 5020{
fae2120b 5021 return true;
927aa2e7
JK
5022}
5023
927aa2e7
JK
5024/* DWARF-5 debug_names reader. */
5025
5026/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5027static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5028
5029/* A helper function that reads the .debug_names section in SECTION
5030 and fills in MAP. FILENAME is the name of the file containing the
5031 section; it is used for error reporting.
5032
5033 Returns true if all went well, false otherwise. */
5034
5035static bool
5036read_debug_names_from_section (struct objfile *objfile,
5037 const char *filename,
5038 struct dwarf2_section_info *section,
5039 mapped_debug_names &map)
5040{
96b79293 5041 if (section->empty ())
927aa2e7
JK
5042 return false;
5043
5044 /* Older elfutils strip versions could keep the section in the main
5045 executable while splitting it for the separate debug info file. */
96b79293 5046 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
5047 return false;
5048
96b79293 5049 section->read (objfile);
927aa2e7 5050
08feed99 5051 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
5052
5053 const gdb_byte *addr = section->buffer;
5054
96b79293 5055 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
5056
5057 unsigned int bytes_read;
5058 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5059 addr += bytes_read;
5060
5061 map.dwarf5_is_dwarf64 = bytes_read != 4;
5062 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5063 if (bytes_read + length != section->size)
5064 {
5065 /* There may be multiple per-CU indices. */
5066 warning (_("Section .debug_names in %s length %s does not match "
5067 "section length %s, ignoring .debug_names."),
5068 filename, plongest (bytes_read + length),
5069 pulongest (section->size));
5070 return false;
5071 }
5072
5073 /* The version number. */
5074 uint16_t version = read_2_bytes (abfd, addr);
5075 addr += 2;
5076 if (version != 5)
5077 {
5078 warning (_("Section .debug_names in %s has unsupported version %d, "
5079 "ignoring .debug_names."),
5080 filename, version);
5081 return false;
5082 }
5083
5084 /* Padding. */
5085 uint16_t padding = read_2_bytes (abfd, addr);
5086 addr += 2;
5087 if (padding != 0)
5088 {
5089 warning (_("Section .debug_names in %s has unsupported padding %d, "
5090 "ignoring .debug_names."),
5091 filename, padding);
5092 return false;
5093 }
5094
5095 /* comp_unit_count - The number of CUs in the CU list. */
5096 map.cu_count = read_4_bytes (abfd, addr);
5097 addr += 4;
5098
5099 /* local_type_unit_count - The number of TUs in the local TU
5100 list. */
5101 map.tu_count = read_4_bytes (abfd, addr);
5102 addr += 4;
5103
5104 /* foreign_type_unit_count - The number of TUs in the foreign TU
5105 list. */
5106 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5107 addr += 4;
5108 if (foreign_tu_count != 0)
5109 {
5110 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5111 "ignoring .debug_names."),
5112 filename, static_cast<unsigned long> (foreign_tu_count));
5113 return false;
5114 }
5115
5116 /* bucket_count - The number of hash buckets in the hash lookup
5117 table. */
5118 map.bucket_count = read_4_bytes (abfd, addr);
5119 addr += 4;
5120
5121 /* name_count - The number of unique names in the index. */
5122 map.name_count = read_4_bytes (abfd, addr);
5123 addr += 4;
5124
5125 /* abbrev_table_size - The size in bytes of the abbreviations
5126 table. */
5127 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5128 addr += 4;
5129
5130 /* augmentation_string_size - The size in bytes of the augmentation
5131 string. This value is rounded up to a multiple of 4. */
5132 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5133 addr += 4;
5134 map.augmentation_is_gdb = ((augmentation_string_size
5135 == sizeof (dwarf5_augmentation))
5136 && memcmp (addr, dwarf5_augmentation,
5137 sizeof (dwarf5_augmentation)) == 0);
5138 augmentation_string_size += (-augmentation_string_size) & 3;
5139 addr += augmentation_string_size;
5140
5141 /* List of CUs */
5142 map.cu_table_reordered = addr;
5143 addr += map.cu_count * map.offset_size;
5144
5145 /* List of Local TUs */
5146 map.tu_table_reordered = addr;
5147 addr += map.tu_count * map.offset_size;
5148
5149 /* Hash Lookup Table */
5150 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5151 addr += map.bucket_count * 4;
5152 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5153 addr += map.name_count * 4;
5154
5155 /* Name Table */
5156 map.name_table_string_offs_reordered = addr;
5157 addr += map.name_count * map.offset_size;
5158 map.name_table_entry_offs_reordered = addr;
5159 addr += map.name_count * map.offset_size;
5160
5161 const gdb_byte *abbrev_table_start = addr;
5162 for (;;)
5163 {
927aa2e7
JK
5164 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5165 addr += bytes_read;
5166 if (index_num == 0)
5167 break;
5168
5169 const auto insertpair
5170 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5171 if (!insertpair.second)
5172 {
5173 warning (_("Section .debug_names in %s has duplicate index %s, "
5174 "ignoring .debug_names."),
5175 filename, pulongest (index_num));
5176 return false;
5177 }
5178 mapped_debug_names::index_val &indexval = insertpair.first->second;
5179 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5180 addr += bytes_read;
5181
5182 for (;;)
5183 {
5184 mapped_debug_names::index_val::attr attr;
5185 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5186 addr += bytes_read;
5187 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5188 addr += bytes_read;
5189 if (attr.form == DW_FORM_implicit_const)
5190 {
5191 attr.implicit_const = read_signed_leb128 (abfd, addr,
5192 &bytes_read);
5193 addr += bytes_read;
5194 }
5195 if (attr.dw_idx == 0 && attr.form == 0)
5196 break;
5197 indexval.attr_vec.push_back (std::move (attr));
5198 }
5199 }
5200 if (addr != abbrev_table_start + abbrev_table_size)
5201 {
5202 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5203 "of size %s vs. written as %u, ignoring .debug_names."),
5204 filename, plongest (addr - abbrev_table_start),
5205 abbrev_table_size);
927aa2e7
JK
5206 return false;
5207 }
5208 map.entry_pool = addr;
5209
5210 return true;
5211}
5212
5213/* A helper for create_cus_from_debug_names that handles the MAP's CU
5214 list. */
5215
5216static void
168c9250 5217create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5218 const mapped_debug_names &map,
5219 dwarf2_section_info &section,
b76e467d 5220 bool is_dwz)
927aa2e7 5221{
3ee6bb11
TV
5222 if (!map.augmentation_is_gdb)
5223 {
fc9a13fb
TV
5224 for (uint32_t i = 0; i < map.cu_count; ++i)
5225 {
5226 sect_offset sect_off
5227 = (sect_offset) (extract_unsigned_integer
5228 (map.cu_table_reordered + i * map.offset_size,
5229 map.offset_size,
5230 map.dwarf5_byte_order));
5231 /* We don't know the length of the CU, because the CU list in a
5232 .debug_names index can be incomplete, so we can't use the start
5233 of the next CU as end of this CU. We create the CUs here with
5234 length 0, and in cutu_reader::cutu_reader we'll fill in the
5235 actual length. */
5236 dwarf2_per_cu_data *per_cu
5237 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5238 sect_off, 0);
5239 per_bfd->all_comp_units.push_back (per_cu);
5240 }
d3b54e63 5241 return;
3ee6bb11
TV
5242 }
5243
927aa2e7
JK
5244 sect_offset sect_off_prev;
5245 for (uint32_t i = 0; i <= map.cu_count; ++i)
5246 {
5247 sect_offset sect_off_next;
5248 if (i < map.cu_count)
5249 {
5250 sect_off_next
5251 = (sect_offset) (extract_unsigned_integer
5252 (map.cu_table_reordered + i * map.offset_size,
5253 map.offset_size,
5254 map.dwarf5_byte_order));
5255 }
5256 else
5257 sect_off_next = (sect_offset) section.size;
5258 if (i >= 1)
5259 {
5260 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5261 dwarf2_per_cu_data *per_cu
168c9250 5262 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5263 sect_off_prev, length);
168c9250 5264 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5265 }
5266 sect_off_prev = sect_off_next;
5267 }
5268}
5269
5270/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5271 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5272
5273static void
168c9250 5274create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5275 const mapped_debug_names &map,
5276 const mapped_debug_names &dwz_map)
5277{
168c9250
SM
5278 gdb_assert (per_bfd->all_comp_units.empty ());
5279 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5280
168c9250 5281 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5282 false /* is_dwz */);
927aa2e7
JK
5283
5284 if (dwz_map.cu_count == 0)
5285 return;
5286
168c9250
SM
5287 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5288 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5289 true /* is_dwz */);
927aa2e7
JK
5290}
5291
5292/* Read .debug_names. If everything went ok, initialize the "quick"
5293 elements of all the CUs and return true. Otherwise, return false. */
5294
5295static bool
976ca316 5296dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 5297{
fcf23d5b
SM
5298 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5299 mapped_debug_names dwz_map;
976ca316 5300 struct objfile *objfile = per_objfile->objfile;
f8c41851 5301 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
5302
5303 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
976ca316 5304 &per_objfile->per_bfd->debug_names, *map))
927aa2e7
JK
5305 return false;
5306
5307 /* Don't use the index if it's empty. */
22ca247e 5308 if (map->name_count == 0)
927aa2e7
JK
5309 return false;
5310
5311 /* If there is a .dwz file, read it so we can get its CU list as
5312 well. */
f8c41851 5313 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
5314 if (dwz != NULL)
5315 {
5316 if (!read_debug_names_from_section (objfile,
00f93c44 5317 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5318 &dwz->debug_names, dwz_map))
5319 {
5320 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5321 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5322 return false;
5323 }
5324 }
5325
f8c41851 5326 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 5327
22ca247e 5328 if (map->tu_count != 0)
927aa2e7
JK
5329 {
5330 /* We can only handle a single .debug_types when we have an
5331 index. */
f8c41851 5332 if (per_bfd->types.size () != 1)
927aa2e7
JK
5333 return false;
5334
f8c41851 5335 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
5336
5337 create_signatured_type_table_from_debug_names
f8c41851 5338 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
5339 }
5340
f8c41851 5341 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 5342
f8c41851
SM
5343 per_bfd->debug_names_table = std::move (map);
5344 per_bfd->using_index = 1;
5345 per_bfd->quick_file_names_table =
976ca316 5346 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
927aa2e7 5347
f8c41851
SM
5348 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5349 objfiles using the same BFD. */
5350 gdb_assert (per_bfd->partial_symtabs == nullptr);
5351 per_bfd->partial_symtabs = objfile->partial_symtabs;
5352
927aa2e7
JK
5353 return true;
5354}
5355
927aa2e7
JK
5356/* Type used to manage iterating over all CUs looking for a symbol for
5357 .debug_names. */
5358
5359class dw2_debug_names_iterator
5360{
5361public:
927aa2e7 5362 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5363 gdb::optional<block_enum> block_index,
5364 domain_enum domain,
fcf23d5b 5365 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5366 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5367 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5368 m_per_objfile (per_objfile)
927aa2e7
JK
5369 {}
5370
5371 dw2_debug_names_iterator (const mapped_debug_names &map,
fcf23d5b 5372 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5373 : m_map (map),
5374 m_search (search),
fcf23d5b
SM
5375 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5376 m_per_objfile (per_objfile)
927aa2e7
JK
5377 {}
5378
3b00ef10
TT
5379 dw2_debug_names_iterator (const mapped_debug_names &map,
5380 block_enum block_index, domain_enum domain,
fcf23d5b 5381 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5382 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5383 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5384 m_per_objfile (per_objfile)
3b00ef10
TT
5385 {}
5386
927aa2e7
JK
5387 /* Return the next matching CU or NULL if there are no more. */
5388 dwarf2_per_cu_data *next ();
5389
5390private:
5391 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5392 const char *name,
5393 dwarf2_per_objfile *per_objfile);
927aa2e7 5394 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5395 uint32_t namei,
5396 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5397
5398 /* The internalized form of .debug_names. */
5399 const mapped_debug_names &m_map;
5400
2b79f376
SM
5401 /* If set, only look for symbols that match that block. Valid values are
5402 GLOBAL_BLOCK and STATIC_BLOCK. */
5403 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5404
5405 /* The kind of symbol we're looking for. */
5406 const domain_enum m_domain = UNDEF_DOMAIN;
5407 const search_domain m_search = ALL_DOMAIN;
5408
5409 /* The list of CUs from the index entry of the symbol, or NULL if
5410 not found. */
5411 const gdb_byte *m_addr;
fcf23d5b
SM
5412
5413 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5414};
5415
5416const char *
fcf23d5b 5417mapped_debug_names::namei_to_name
976ca316 5418 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5419{
5420 const ULONGEST namei_string_offs
5421 = extract_unsigned_integer ((name_table_string_offs_reordered
5422 + namei * offset_size),
5423 offset_size,
5424 dwarf5_byte_order);
976ca316 5425 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5426}
5427
5428/* Find a slot in .debug_names for the object named NAME. If NAME is
5429 found, return pointer to its pool data. If NAME cannot be found,
5430 return NULL. */
5431
5432const gdb_byte *
5433dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5434 (const mapped_debug_names &map, const char *name,
5435 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5436{
5437 int (*cmp) (const char *, const char *);
5438
54ee4252 5439 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5440 if (current_language->la_language == language_cplus
5441 || current_language->la_language == language_fortran
5442 || current_language->la_language == language_d)
5443 {
5444 /* NAME is already canonical. Drop any qualifiers as
5445 .debug_names does not contain any. */
5446
5447 if (strchr (name, '(') != NULL)
5448 {
54ee4252 5449 without_params = cp_remove_params (name);
927aa2e7 5450 if (without_params != NULL)
54ee4252 5451 name = without_params.get ();
927aa2e7
JK
5452 }
5453 }
5454
5455 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5456
5457 const uint32_t full_hash = dwarf5_djb_hash (name);
5458 uint32_t namei
5459 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5460 (map.bucket_table_reordered
5461 + (full_hash % map.bucket_count)), 4,
5462 map.dwarf5_byte_order);
5463 if (namei == 0)
5464 return NULL;
5465 --namei;
5466 if (namei >= map.name_count)
5467 {
b98664d3 5468 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5469 "[in module %s]"),
5470 namei, map.name_count,
fcf23d5b 5471 objfile_name (per_objfile->objfile));
927aa2e7
JK
5472 return NULL;
5473 }
5474
5475 for (;;)
5476 {
5477 const uint32_t namei_full_hash
5478 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5479 (map.hash_table_reordered + namei), 4,
5480 map.dwarf5_byte_order);
5481 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5482 return NULL;
5483
5484 if (full_hash == namei_full_hash)
5485 {
fcf23d5b 5486 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5487
5488#if 0 /* An expensive sanity check. */
5489 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5490 {
b98664d3 5491 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5492 "[in module %s]"),
5493 namei, objfile_name (dwarf2_per_objfile->objfile));
5494 return NULL;
5495 }
5496#endif
5497
5498 if (cmp (namei_string, name) == 0)
5499 {
5500 const ULONGEST namei_entry_offs
5501 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5502 + namei * map.offset_size),
5503 map.offset_size, map.dwarf5_byte_order);
5504 return map.entry_pool + namei_entry_offs;
5505 }
5506 }
5507
5508 ++namei;
5509 if (namei >= map.name_count)
5510 return NULL;
5511 }
5512}
5513
5514const gdb_byte *
5515dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5516 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5517{
5518 if (namei >= map.name_count)
5519 {
b98664d3 5520 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5521 "[in module %s]"),
5522 namei, map.name_count,
fcf23d5b 5523 objfile_name (per_objfile->objfile));
927aa2e7
JK
5524 return NULL;
5525 }
5526
5527 const ULONGEST namei_entry_offs
5528 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5529 + namei * map.offset_size),
5530 map.offset_size, map.dwarf5_byte_order);
5531 return map.entry_pool + namei_entry_offs;
5532}
5533
5534/* See dw2_debug_names_iterator. */
5535
5536dwarf2_per_cu_data *
5537dw2_debug_names_iterator::next ()
5538{
5539 if (m_addr == NULL)
5540 return NULL;
5541
fcf23d5b
SM
5542 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5543 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5544 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5545
5546 again:
5547
5548 unsigned int bytes_read;
5549 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5550 m_addr += bytes_read;
5551 if (abbrev == 0)
5552 return NULL;
5553
5554 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5555 if (indexval_it == m_map.abbrev_map.cend ())
5556 {
b98664d3 5557 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5558 "[in module %s]"),
ed2dc618 5559 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5560 return NULL;
5561 }
5562 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5563 enum class symbol_linkage {
5564 unknown,
5565 static_,
5566 extern_,
23c13d42 5567 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5568 dwarf2_per_cu_data *per_cu = NULL;
5569 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5570 {
5571 ULONGEST ull;
5572 switch (attr.form)
5573 {
5574 case DW_FORM_implicit_const:
5575 ull = attr.implicit_const;
5576 break;
5577 case DW_FORM_flag_present:
5578 ull = 1;
5579 break;
5580 case DW_FORM_udata:
5581 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5582 m_addr += bytes_read;
5583 break;
6dc55ce9 5584 case DW_FORM_ref4:
5585 ull = read_4_bytes (abfd, m_addr);
5586 m_addr += 4;
5587 break;
5588 case DW_FORM_ref8:
5589 ull = read_8_bytes (abfd, m_addr);
5590 m_addr += 8;
5591 break;
5592 case DW_FORM_ref_sig8:
5593 ull = read_8_bytes (abfd, m_addr);
5594 m_addr += 8;
5595 break;
927aa2e7 5596 default:
b98664d3 5597 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5598 dwarf_form_name (attr.form),
ed2dc618 5599 objfile_name (objfile));
927aa2e7
JK
5600 return NULL;
5601 }
5602 switch (attr.dw_idx)
5603 {
5604 case DW_IDX_compile_unit:
5605 /* Don't crash on bad data. */
fcf23d5b 5606 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5607 {
b98664d3 5608 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5609 " [in module %s]"),
5610 pulongest (ull),
fcf23d5b 5611 objfile_name (objfile));
927aa2e7
JK
5612 continue;
5613 }
fcf23d5b 5614 per_cu = per_bfd->get_cutu (ull);
927aa2e7 5615 break;
8af5c486
JK
5616 case DW_IDX_type_unit:
5617 /* Don't crash on bad data. */
fcf23d5b 5618 if (ull >= per_bfd->all_type_units.size ())
8af5c486 5619 {
b98664d3 5620 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5621 " [in module %s]"),
5622 pulongest (ull),
fcf23d5b 5623 objfile_name (objfile));
8af5c486
JK
5624 continue;
5625 }
fcf23d5b 5626 per_cu = &per_bfd->get_tu (ull)->per_cu;
8af5c486 5627 break;
6dc55ce9 5628 case DW_IDX_die_offset:
5629 /* In a per-CU index (as opposed to a per-module index), index
5630 entries without CU attribute implicitly refer to the single CU. */
5631 if (per_cu == NULL)
fcf23d5b 5632 per_cu = per_bfd->get_cu (0);
6dc55ce9 5633 break;
927aa2e7
JK
5634 case DW_IDX_GNU_internal:
5635 if (!m_map.augmentation_is_gdb)
5636 break;
23c13d42 5637 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5638 break;
5639 case DW_IDX_GNU_external:
5640 if (!m_map.augmentation_is_gdb)
5641 break;
23c13d42 5642 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5643 break;
5644 }
5645 }
5646
5647 /* Skip if already read in. */
fcf23d5b 5648 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5649 goto again;
5650
5651 /* Check static vs global. */
23c13d42 5652 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5653 {
2b79f376 5654 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5655 const bool symbol_is_static =
5656 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5657 if (want_static != symbol_is_static)
2b79f376 5658 goto again;
927aa2e7
JK
5659 }
5660
5661 /* Match dw2_symtab_iter_next, symbol_kind
5662 and debug_names::psymbol_tag. */
5663 switch (m_domain)
5664 {
5665 case VAR_DOMAIN:
5666 switch (indexval.dwarf_tag)
5667 {
5668 case DW_TAG_variable:
5669 case DW_TAG_subprogram:
5670 /* Some types are also in VAR_DOMAIN. */
5671 case DW_TAG_typedef:
5672 case DW_TAG_structure_type:
5673 break;
5674 default:
5675 goto again;
5676 }
5677 break;
5678 case STRUCT_DOMAIN:
5679 switch (indexval.dwarf_tag)
5680 {
5681 case DW_TAG_typedef:
5682 case DW_TAG_structure_type:
5683 break;
5684 default:
5685 goto again;
5686 }
5687 break;
5688 case LABEL_DOMAIN:
5689 switch (indexval.dwarf_tag)
5690 {
5691 case 0:
5692 case DW_TAG_variable:
5693 break;
5694 default:
5695 goto again;
5696 }
5697 break;
59c35742
AB
5698 case MODULE_DOMAIN:
5699 switch (indexval.dwarf_tag)
5700 {
5701 case DW_TAG_module:
5702 break;
5703 default:
5704 goto again;
5705 }
5706 break;
927aa2e7
JK
5707 default:
5708 break;
5709 }
5710
5711 /* Match dw2_expand_symtabs_matching, symbol_kind and
5712 debug_names::psymbol_tag. */
5713 switch (m_search)
4b514bc8 5714 {
927aa2e7
JK
5715 case VARIABLES_DOMAIN:
5716 switch (indexval.dwarf_tag)
4b514bc8 5717 {
927aa2e7
JK
5718 case DW_TAG_variable:
5719 break;
5720 default:
5721 goto again;
4b514bc8 5722 }
927aa2e7
JK
5723 break;
5724 case FUNCTIONS_DOMAIN:
5725 switch (indexval.dwarf_tag)
4b514bc8 5726 {
927aa2e7
JK
5727 case DW_TAG_subprogram:
5728 break;
5729 default:
5730 goto again;
4b514bc8 5731 }
927aa2e7
JK
5732 break;
5733 case TYPES_DOMAIN:
5734 switch (indexval.dwarf_tag)
5735 {
5736 case DW_TAG_typedef:
5737 case DW_TAG_structure_type:
5738 break;
5739 default:
5740 goto again;
5741 }
5742 break;
59c35742
AB
5743 case MODULES_DOMAIN:
5744 switch (indexval.dwarf_tag)
5745 {
5746 case DW_TAG_module:
5747 break;
5748 default:
5749 goto again;
5750 }
927aa2e7
JK
5751 default:
5752 break;
4b514bc8 5753 }
927aa2e7
JK
5754
5755 return per_cu;
4b514bc8 5756}
61920122 5757
39298a5d
TT
5758struct compunit_symtab *
5759dwarf2_debug_names_index::lookup_symbol
5760 (struct objfile *objfile, block_enum block_index,
5761 const char *name, domain_enum domain)
4b514bc8 5762{
976ca316 5763 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
61920122 5764
976ca316 5765 const auto &mapp = per_objfile->per_bfd->debug_names_table;
927aa2e7 5766 if (!mapp)
61920122 5767 {
927aa2e7
JK
5768 /* index is NULL if OBJF_READNOW. */
5769 return NULL;
5770 }
5771 const auto &map = *mapp;
9291a0cd 5772
976ca316 5773 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
9703b513 5774
927aa2e7
JK
5775 struct compunit_symtab *stab_best = NULL;
5776 struct dwarf2_per_cu_data *per_cu;
5777 while ((per_cu = iter.next ()) != NULL)
5778 {
5779 struct symbol *sym, *with_opaque = NULL;
97a1449a 5780 compunit_symtab *stab
976ca316 5781 = dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7 5782 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5783 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5784
927aa2e7
JK
5785 sym = block_find_symbol (block, name, domain,
5786 block_find_non_opaque_type_preferred,
5787 &with_opaque);
9703b513 5788
927aa2e7
JK
5789 /* Some caution must be observed with overloaded functions and
5790 methods, since the index will not contain any overload
5791 information (but NAME might contain it). */
a3ec0bb1 5792
927aa2e7 5793 if (sym != NULL
987012b8 5794 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5795 return stab;
5796 if (with_opaque != NULL
987012b8 5797 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5798 stab_best = stab;
9703b513 5799
927aa2e7 5800 /* Keep looking through other CUs. */
9703b513
TT
5801 }
5802
927aa2e7 5803 return stab_best;
9703b513
TT
5804}
5805
927aa2e7
JK
5806/* This dumps minimal information about .debug_names. It is called
5807 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5808 uses this to verify that .debug_names has been loaded. */
9291a0cd 5809
39298a5d
TT
5810void
5811dwarf2_debug_names_index::dump (struct objfile *objfile)
927aa2e7 5812{
976ca316 5813 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5814
976ca316 5815 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5816 printf_filtered (".debug_names:");
976ca316 5817 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5818 printf_filtered (" exists\n");
5819 else
5820 printf_filtered (" faked for \"readnow\"\n");
5821 printf_filtered ("\n");
9291a0cd
TT
5822}
5823
39298a5d
TT
5824void
5825dwarf2_debug_names_index::expand_symtabs_for_function
5826 (struct objfile *objfile, const char *func_name)
9291a0cd 5827{
976ca316 5828 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 5829
976ca316
SM
5830 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5831 if (per_objfile->per_bfd->debug_names_table)
24c79950 5832 {
976ca316 5833 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
24c79950 5834
fcf23d5b 5835 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
976ca316 5836 per_objfile);
24c79950 5837
927aa2e7
JK
5838 struct dwarf2_per_cu_data *per_cu;
5839 while ((per_cu = iter.next ()) != NULL)
976ca316 5840 dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7
JK
5841 }
5842}
24c79950 5843
39298a5d
TT
5844void
5845dwarf2_debug_names_index::map_matching_symbols
3b00ef10
TT
5846 (struct objfile *objfile,
5847 const lookup_name_info &name, domain_enum domain,
5848 int global,
5849 gdb::function_view<symbol_found_callback_ftype> callback,
5850 symbol_compare_ftype *ordered_compare)
5851{
976ca316 5852 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5853
5854 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5855 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5856 return;
5857
976ca316 5858 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5859 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5860
5861 const char *match_name = name.ada ().lookup_name ().c_str ();
5862 auto matcher = [&] (const char *symname)
5863 {
5864 if (ordered_compare == nullptr)
5865 return true;
5866 return ordered_compare (symname, match_name) == 0;
5867 };
5868
5869 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5870 [&] (offset_type namei)
5871 {
5872 /* The name was matched, now expand corresponding CUs that were
5873 marked. */
fcf23d5b 5874 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
976ca316 5875 per_objfile);
3b00ef10
TT
5876
5877 struct dwarf2_per_cu_data *per_cu;
5878 while ((per_cu = iter.next ()) != NULL)
976ca316 5879 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5880 nullptr);
3b00ef10 5881 return true;
976ca316 5882 }, per_objfile);
3b00ef10
TT
5883
5884 /* It's a shame we couldn't do this inside the
5885 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5886 that have already been expanded. Instead, this loop matches what
5887 the psymtab code does. */
976ca316 5888 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3b00ef10 5889 {
976ca316 5890 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
af758d11 5891 if (symtab != nullptr)
3b00ef10
TT
5892 {
5893 const struct block *block
af758d11 5894 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5895 if (!iterate_over_symbols_terminated (block, name,
5896 domain, callback))
5897 break;
5898 }
5899 }
5900}
5901
39298a5d
TT
5902void
5903dwarf2_debug_names_index::expand_symtabs_matching
927aa2e7
JK
5904 (struct objfile *objfile,
5905 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5906 const lookup_name_info *lookup_name,
927aa2e7
JK
5907 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5908 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5909 enum search_domain kind)
5910{
976ca316 5911 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5912
927aa2e7 5913 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5914 if (!per_objfile->per_bfd->debug_names_table)
927aa2e7 5915 return;
9291a0cd 5916
976ca316 5917 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5918
c1a66c06
TV
5919 if (symbol_matcher == NULL && lookup_name == NULL)
5920 {
976ca316 5921 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5922 {
5923 QUIT;
5924
976ca316
SM
5925 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5926 expansion_notify);
c1a66c06
TV
5927 }
5928 return;
5929 }
5930
976ca316 5931 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5932
c1a66c06 5933 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5934 symbol_matcher,
5935 kind, [&] (offset_type namei)
927aa2e7 5936 {
927aa2e7
JK
5937 /* The name was matched, now expand corresponding CUs that were
5938 marked. */
976ca316 5939 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
bbf2f4df 5940
927aa2e7
JK
5941 struct dwarf2_per_cu_data *per_cu;
5942 while ((per_cu = iter.next ()) != NULL)
976ca316
SM
5943 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5944 expansion_notify);
3b00ef10 5945 return true;
976ca316 5946 }, per_objfile);
9291a0cd
TT
5947}
5948
4485a1c1 5949/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5950 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5951
5952template <typename T>
5953static gdb::array_view<const gdb_byte>
5954get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5955{
5956 dwarf2_section_info *section = &section_owner->gdb_index;
5957
96b79293 5958 if (section->empty ())
4485a1c1
SM
5959 return {};
5960
5961 /* Older elfutils strip versions could keep the section in the main
5962 executable while splitting it for the separate debug info file. */
96b79293 5963 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5964 return {};
5965
96b79293 5966 section->read (obj);
4485a1c1 5967
8bebfcda
PA
5968 /* dwarf2_section_info::size is a bfd_size_type, while
5969 gdb::array_view works with size_t. On 32-bit hosts, with
5970 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5971 is 32-bit. So we need an explicit narrowing conversion here.
5972 This is fine, because it's impossible to allocate or mmap an
5973 array/buffer larger than what size_t can represent. */
5974 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5975}
5976
87d6a7aa
SM
5977/* Lookup the index cache for the contents of the index associated to
5978 DWARF2_OBJ. */
5979
5980static gdb::array_view<const gdb_byte>
5989a64e 5981get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5982{
5983 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5984 if (build_id == nullptr)
5985 return {};
5986
5987 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5988 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5989}
5990
5991/* Same as the above, but for DWZ. */
5992
5993static gdb::array_view<const gdb_byte>
5994get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5995{
5996 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5997 if (build_id == nullptr)
5998 return {};
5999
6000 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6001}
6002
3c0aa29a 6003/* See symfile.h. */
9291a0cd 6004
3c0aa29a
PA
6005bool
6006dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6007{
976ca316
SM
6008 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6009 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 6010
850ed749
SM
6011 dwarf_read_debug_printf ("called");
6012
9291a0cd
TT
6013 /* If we're about to read full symbols, don't bother with the
6014 indices. In this case we also don't care if some other debug
6015 format is making psymtabs, because they are all about to be
6016 expanded anyway. */
6017 if ((objfile->flags & OBJF_READNOW))
6018 {
850ed749
SM
6019 dwarf_read_debug_printf ("readnow requested");
6020
17ee85fc
TT
6021 /* When using READNOW, the using_index flag (set below) indicates that
6022 PER_BFD was already initialized, when we loaded some other objfile. */
6023 if (per_bfd->using_index)
6024 {
850ed749 6025 dwarf_read_debug_printf ("using_index already set");
17ee85fc 6026 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6027 per_objfile->resize_symtabs ();
17ee85fc
TT
6028 return true;
6029 }
6030
6031 per_bfd->using_index = 1;
976ca316
SM
6032 create_all_comp_units (per_objfile);
6033 create_all_type_units (per_objfile);
17ee85fc
TT
6034 per_bfd->quick_file_names_table
6035 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 6036 per_objfile->resize_symtabs ();
9291a0cd 6037
17ee85fc
TT
6038 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6039 + per_bfd->all_type_units.size ()); ++i)
9291a0cd 6040 {
17ee85fc 6041 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
9291a0cd 6042
17ee85fc 6043 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 6044 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6045 }
6046
6047 /* Return 1 so that gdb sees the "quick" functions. However,
6048 these functions will be no-ops because we will have expanded
6049 all symtabs. */
3c0aa29a
PA
6050 *index_kind = dw_index_kind::GDB_INDEX;
6051 return true;
9291a0cd
TT
6052 }
6053
17ee85fc
TT
6054 /* Was a debug names index already read when we processed an objfile sharing
6055 PER_BFD? */
6056 if (per_bfd->debug_names_table != nullptr)
6057 {
850ed749 6058 dwarf_read_debug_printf ("re-using shared debug names table");
17ee85fc 6059 *index_kind = dw_index_kind::DEBUG_NAMES;
f8c41851 6060 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6061 per_objfile->resize_symtabs ();
17ee85fc
TT
6062 return true;
6063 }
6064
6065 /* Was a GDB index already read when we processed an objfile sharing
6066 PER_BFD? */
6067 if (per_bfd->index_table != nullptr)
6068 {
850ed749 6069 dwarf_read_debug_printf ("re-using shared index table");
17ee85fc 6070 *index_kind = dw_index_kind::GDB_INDEX;
f8c41851 6071 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6072 per_objfile->resize_symtabs ();
17ee85fc
TT
6073 return true;
6074 }
6075
efb763a5
SM
6076 /* There might already be partial symtabs built for this BFD. This happens
6077 when loading the same binary twice with the index-cache enabled. If so,
6078 don't try to read an index. The objfile / per_objfile initialization will
6079 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6080 code path. */
6081 if (per_bfd->partial_symtabs != nullptr)
850ed749
SM
6082 {
6083 dwarf_read_debug_printf ("re-using shared partial symtabs");
6084 return false;
6085 }
efb763a5 6086
976ca316 6087 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a 6088 {
850ed749 6089 dwarf_read_debug_printf ("found debug names");
3c0aa29a 6090 *index_kind = dw_index_kind::DEBUG_NAMES;
976ca316 6091 per_objfile->resize_symtabs ();
3c0aa29a
PA
6092 return true;
6093 }
927aa2e7 6094
976ca316 6095 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 6096 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 6097 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a 6098 {
850ed749 6099 dwarf_read_debug_printf ("found gdb index from file");
3c0aa29a 6100 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6101 per_objfile->resize_symtabs ();
3c0aa29a
PA
6102 return true;
6103 }
9291a0cd 6104
87d6a7aa 6105 /* ... otherwise, try to find the index in the index cache. */
976ca316 6106 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
6107 get_gdb_index_contents_from_cache,
6108 get_gdb_index_contents_from_cache_dwz))
6109 {
850ed749 6110 dwarf_read_debug_printf ("found gdb index from cache");
87d6a7aa
SM
6111 global_index_cache.hit ();
6112 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6113 per_objfile->resize_symtabs ();
87d6a7aa
SM
6114 return true;
6115 }
6116
6117 global_index_cache.miss ();
3c0aa29a 6118 return false;
9291a0cd
TT
6119}
6120
6121\f
6122
dce234bc
PP
6123/* Build a partial symbol table. */
6124
6125void
f29dff0a 6126dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6127{
976ca316
SM
6128 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6129 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
6130
6131 if (per_bfd->partial_symtabs != nullptr)
6132 {
6133 /* Partial symbols were already read, so now we can simply
6134 attach them. */
6135 objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6136 per_objfile->resize_symtabs ();
17ee85fc
TT
6137 return;
6138 }
c9bf0622 6139
51962708
TT
6140 /* Set the local reference to partial symtabs, so that we don't try
6141 to read them again if reading another objfile with the same BFD.
6142 If we can't in fact share, this won't make a difference anyway as
6143 the dwarf2_per_bfd object won't be shared. */
6144 per_bfd->partial_symtabs = objfile->partial_symtabs;
6145
a70b8144 6146 try
c9bf0622
TT
6147 {
6148 /* This isn't really ideal: all the data we allocate on the
6149 objfile's obstack is still uselessly kept around. However,
6150 freeing it seems unsafe. */
484b1090 6151 psymtab_discarder psymtabs (objfile->partial_symtabs.get ());
976ca316 6152 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 6153 psymtabs.keep ();
87d6a7aa 6154
976ca316 6155 per_objfile->resize_symtabs ();
af758d11 6156
87d6a7aa 6157 /* (maybe) store an index in the cache. */
976ca316 6158 global_index_cache.store (per_objfile);
c9bf0622 6159 }
230d2906 6160 catch (const gdb_exception_error &except)
492d29ea
PA
6161 {
6162 exception_print (gdb_stderr, except);
6163 }
c906108c 6164}
c906108c 6165
3b80fe9b
DE
6166/* Find the base address of the compilation unit for range lists and
6167 location lists. It will normally be specified by DW_AT_low_pc.
6168 In DWARF-3 draft 4, the base address could be overridden by
6169 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6170 compilation units with discontinuous ranges. */
6171
6172static void
6173dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6174{
6175 struct attribute *attr;
6176
2b24b6e4 6177 cu->base_address.reset ();
3b80fe9b
DE
6178
6179 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6180 if (attr != nullptr)
95f982e5 6181 cu->base_address = attr->as_address ();
3b80fe9b
DE
6182 else
6183 {
6184 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6185 if (attr != nullptr)
95f982e5 6186 cu->base_address = attr->as_address ();
3b80fe9b
DE
6187 }
6188}
6189
36586728
TT
6190/* Helper function that returns the proper abbrev section for
6191 THIS_CU. */
6192
6193static struct dwarf2_section_info *
6194get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6195{
6196 struct dwarf2_section_info *abbrev;
c3699833 6197 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6198
6199 if (this_cu->is_dwz)
a7308ce0 6200 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
36586728 6201 else
c3699833 6202 abbrev = &per_bfd->abbrev;
36586728
TT
6203
6204 return abbrev;
6205}
6206
f4dc4d17
DE
6207/* Fetch the abbreviation table offset from a comp or type unit header. */
6208
6209static sect_offset
976ca316 6210read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 6211 struct dwarf2_section_info *section,
9c541725 6212 sect_offset sect_off)
f4dc4d17 6213{
96b79293 6214 bfd *abfd = section->get_bfd_owner ();
d521ce57 6215 const gdb_byte *info_ptr;
ac298888 6216 unsigned int initial_length_size, offset_size;
43988095 6217 uint16_t version;
f4dc4d17 6218
976ca316 6219 section->read (per_objfile->objfile);
9c541725 6220 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6221 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6222 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6223 info_ptr += initial_length_size;
6224
6225 version = read_2_bytes (abfd, info_ptr);
6226 info_ptr += 2;
6227 if (version >= 5)
6228 {
6229 /* Skip unit type and address size. */
6230 info_ptr += 2;
6231 }
6232
24aa364d 6233 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6234}
6235
b83470bf
TT
6236/* A partial symtab that is used only for include files. */
6237struct dwarf2_include_psymtab : public partial_symtab
6238{
6239 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6240 : partial_symtab (filename, objfile)
6241 {
6242 }
6243
6244 void read_symtab (struct objfile *objfile) override
6245 {
194d088f
TV
6246 /* It's an include file, no symbols to read for it.
6247 Everything is in the includer symtab. */
6248
6249 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6250 expansion of the includer psymtab. We use the dependencies[0] field to
6251 model the includer. But if we go the regular route of calling
6252 expand_psymtab here, and having expand_psymtab call expand_dependencies
6253 to expand the includer, we'll only use expand_psymtab on the includer
6254 (making it a non-toplevel psymtab), while if we expand the includer via
6255 another path, we'll use read_symtab (making it a toplevel psymtab).
6256 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6257 psymtab, and trigger read_symtab on the includer here directly. */
6258 includer ()->read_symtab (objfile);
b83470bf
TT
6259 }
6260
6261 void expand_psymtab (struct objfile *objfile) override
6262 {
194d088f
TV
6263 /* This is not called by read_symtab, and should not be called by any
6264 expand_dependencies. */
6265 gdb_assert (false);
b83470bf
TT
6266 }
6267
5717c425 6268 bool readin_p (struct objfile *objfile) const override
b83470bf 6269 {
5717c425 6270 return includer ()->readin_p (objfile);
b83470bf
TT
6271 }
6272
5717c425 6273 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6274 {
6275 return nullptr;
6276 }
6277
6278private:
194d088f
TV
6279 partial_symtab *includer () const
6280 {
6281 /* An include psymtab has exactly one dependency: the psymtab that
6282 includes it. */
6283 gdb_assert (this->number_of_dependencies == 1);
6284 return this->dependencies[0];
6285 }
b83470bf
TT
6286};
6287
aaa75496
JB
6288/* Allocate a new partial symtab for file named NAME and mark this new
6289 partial symtab as being an include of PST. */
6290
6291static void
891813be 6292dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
dda83cd7 6293 struct objfile *objfile)
aaa75496 6294{
b83470bf 6295 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6296
fbd9ab74 6297 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6298 subpst->dirname = pst->dirname;
fbd9ab74 6299
a9342b62 6300 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6301 subpst->dependencies[0] = pst;
6302 subpst->number_of_dependencies = 1;
aaa75496
JB
6303}
6304
6305/* Read the Line Number Program data and extract the list of files
6306 included by the source file represented by PST. Build an include
d85a05f0 6307 partial symtab for each of these included files. */
aaa75496
JB
6308
6309static void
6310dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6311 struct die_info *die,
891813be 6312 dwarf2_psymtab *pst)
aaa75496 6313{
fff8551c 6314 line_header_up lh;
d85a05f0 6315 struct attribute *attr;
aaa75496 6316
d85a05f0 6317 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e
TT
6318 if (attr != nullptr && attr->form_is_unsigned ())
6319 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
aaa75496
JB
6320 if (lh == NULL)
6321 return; /* No linetable, so no includes. */
6322
79748972
TT
6323 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6324 that we pass in the raw text_low here; that is ok because we're
6325 only decoding the line table to make include partial symtabs, and
6326 so the addresses aren't really used. */
4ae976d1 6327 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6328 pst->raw_text_low (), 1);
aaa75496
JB
6329}
6330
348e048f 6331static hashval_t
52dc124a 6332hash_signatured_type (const void *item)
348e048f 6333{
9a3c8263
SM
6334 const struct signatured_type *sig_type
6335 = (const struct signatured_type *) item;
9a619af0 6336
348e048f 6337 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6338 return sig_type->signature;
348e048f
DE
6339}
6340
6341static int
52dc124a 6342eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6343{
9a3c8263
SM
6344 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6345 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6346
348e048f
DE
6347 return lhs->signature == rhs->signature;
6348}
6349
1fd400ff
TT
6350/* Allocate a hash table for signatured types. */
6351
b0b6a987 6352static htab_up
298e9637 6353allocate_signatured_type_table ()
1fd400ff 6354{
b0b6a987
TT
6355 return htab_up (htab_create_alloc (41,
6356 hash_signatured_type,
6357 eq_signatured_type,
6358 NULL, xcalloc, xfree));
1fd400ff
TT
6359}
6360
d467dd73 6361/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6362
6363static int
d467dd73 6364add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6365{
9a3c8263 6366 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6367 std::vector<signatured_type *> *all_type_units
6368 = (std::vector<signatured_type *> *) datum;
1fd400ff 6369
b2bdb8cf 6370 all_type_units->push_back (sigt);
1fd400ff
TT
6371
6372 return 1;
6373}
6374
78d4d2c5 6375/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6376 and fill them into TYPES_HTAB. It will process only type units,
6377 therefore DW_UT_type. */
c88ee1f0 6378
78d4d2c5 6379static void
976ca316 6380create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6381 struct dwo_file *dwo_file,
b0b6a987 6382 dwarf2_section_info *section, htab_up &types_htab,
43988095 6383 rcuh_kind section_kind)
348e048f 6384{
976ca316 6385 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6386 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6387 bfd *abfd;
6388 const gdb_byte *info_ptr, *end_ptr;
348e048f 6389
4bdcc0c1
DE
6390 abbrev_section = (dwo_file != NULL
6391 ? &dwo_file->sections.abbrev
976ca316 6392 : &per_objfile->per_bfd->abbrev);
4bdcc0c1 6393
4800761a 6394 dwarf_read_debug_printf ("Reading %s for %s",
6f738b01
SM
6395 section->get_name (),
6396 abbrev_section->get_file_name ());
09406207 6397
96b79293 6398 section->read (objfile);
78d4d2c5 6399 info_ptr = section->buffer;
348e048f 6400
78d4d2c5
JK
6401 if (info_ptr == NULL)
6402 return;
348e048f 6403
78d4d2c5
JK
6404 /* We can't set abfd until now because the section may be empty or
6405 not present, in which case the bfd is unknown. */
96b79293 6406 abfd = section->get_bfd_owner ();
348e048f 6407
c0ab21c2
TT
6408 /* We don't use cutu_reader here because we don't need to read
6409 any dies: the signature is in the header. */
3019eac3 6410
78d4d2c5
JK
6411 end_ptr = info_ptr + section->size;
6412 while (info_ptr < end_ptr)
6413 {
78d4d2c5
JK
6414 struct signatured_type *sig_type;
6415 struct dwo_unit *dwo_tu;
6416 void **slot;
6417 const gdb_byte *ptr = info_ptr;
6418 struct comp_unit_head header;
6419 unsigned int length;
8b70b953 6420
9c541725 6421 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6422
a49dd8dd
JK
6423 /* Initialize it due to a false compiler warning. */
6424 header.signature = -1;
9c541725 6425 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6426
78d4d2c5
JK
6427 /* We need to read the type's signature in order to build the hash
6428 table, but we don't need anything else just yet. */
348e048f 6429
976ca316 6430 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6431 abbrev_section, ptr, section_kind);
348e048f 6432
4057dfde 6433 length = header.get_length ();
6caca83c 6434
78d4d2c5
JK
6435 /* Skip dummy type units. */
6436 if (ptr >= info_ptr + length
43988095 6437 || peek_abbrev_code (abfd, ptr) == 0
d2854d8d
CT
6438 || (header.unit_type != DW_UT_type
6439 && header.unit_type != DW_UT_split_type))
78d4d2c5
JK
6440 {
6441 info_ptr += length;
6442 continue;
6443 }
dee91e82 6444
78d4d2c5
JK
6445 if (types_htab == NULL)
6446 {
6447 if (dwo_file)
298e9637 6448 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6449 else
298e9637 6450 types_htab = allocate_signatured_type_table ();
78d4d2c5 6451 }
8b70b953 6452
78d4d2c5
JK
6453 if (dwo_file)
6454 {
6455 sig_type = NULL;
976ca316 6456 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
78d4d2c5 6457 dwo_tu->dwo_file = dwo_file;
43988095 6458 dwo_tu->signature = header.signature;
9c541725 6459 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6460 dwo_tu->section = section;
9c541725 6461 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6462 dwo_tu->length = length;
6463 }
6464 else
6465 {
6466 /* N.B.: type_offset is not usable if this type uses a DWO file.
6467 The real type_offset is in the DWO file. */
6468 dwo_tu = NULL;
976ca316 6469 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
43988095 6470 sig_type->signature = header.signature;
9c541725 6471 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6472 sig_type->per_cu.is_debug_types = 1;
6473 sig_type->per_cu.section = section;
9c541725 6474 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6475 sig_type->per_cu.length = length;
6476 }
6477
b0b6a987 6478 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6479 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6480 INSERT);
6481 gdb_assert (slot != NULL);
6482 if (*slot != NULL)
6483 {
9c541725 6484 sect_offset dup_sect_off;
0349ea22 6485
3019eac3
DE
6486 if (dwo_file)
6487 {
78d4d2c5
JK
6488 const struct dwo_unit *dup_tu
6489 = (const struct dwo_unit *) *slot;
6490
9c541725 6491 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6492 }
6493 else
6494 {
78d4d2c5
JK
6495 const struct signatured_type *dup_tu
6496 = (const struct signatured_type *) *slot;
6497
9c541725 6498 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6499 }
8b70b953 6500
b98664d3 6501 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6502 " the entry at offset %s, signature %s"),
6503 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6504 hex_string (header.signature));
78d4d2c5
JK
6505 }
6506 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6507
6f738b01
SM
6508 dwarf_read_debug_printf_v (" offset %s, signature %s",
6509 sect_offset_str (sect_off),
6510 hex_string (header.signature));
3019eac3 6511
78d4d2c5
JK
6512 info_ptr += length;
6513 }
6514}
3019eac3 6515
78d4d2c5
JK
6516/* Create the hash table of all entries in the .debug_types
6517 (or .debug_types.dwo) section(s).
6518 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6519 otherwise it is NULL.
b3c8eb43 6520
78d4d2c5 6521 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6522
78d4d2c5 6523 Note: This function processes DWO files only, not DWP files. */
348e048f 6524
78d4d2c5 6525static void
976ca316 6526create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6527 struct dwo_file *dwo_file,
fd5866f6 6528 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6529 htab_up &types_htab)
78d4d2c5 6530{
fd5866f6 6531 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6532 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6533 rcuh_kind::TYPE);
3019eac3
DE
6534}
6535
6536/* Create the hash table of all entries in the .debug_types section,
6537 and initialize all_type_units.
6538 The result is zero if there is an error (e.g. missing .debug_types section),
6539 otherwise non-zero. */
6540
6541static int
976ca316 6542create_all_type_units (dwarf2_per_objfile *per_objfile)
3019eac3 6543{
b0b6a987 6544 htab_up types_htab;
3019eac3 6545
976ca316
SM
6546 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6547 types_htab, rcuh_kind::COMPILE);
6548 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6549 types_htab);
3019eac3
DE
6550 if (types_htab == NULL)
6551 {
976ca316 6552 per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6553 return 0;
6554 }
6555
976ca316 6556 per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6557
976ca316
SM
6558 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6559 per_objfile->per_bfd->all_type_units.reserve
6560 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6561
976ca316 6562 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6563 add_signatured_type_cu_to_table,
976ca316 6564 &per_objfile->per_bfd->all_type_units);
1fd400ff 6565
348e048f
DE
6566 return 1;
6567}
6568
5989a64e 6569/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6570 If SLOT is non-NULL, it is the entry to use in the hash table.
6571 Otherwise we find one. */
6572
6573static struct signatured_type *
976ca316 6574add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6575{
976ca316
SM
6576 if (per_objfile->per_bfd->all_type_units.size ()
6577 == per_objfile->per_bfd->all_type_units.capacity ())
6578 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6579
976ca316 6580 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6581
976ca316 6582 per_objfile->resize_symtabs ();
af758d11 6583
976ca316 6584 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6585 sig_type->signature = sig;
6586 sig_type->per_cu.is_debug_types = 1;
976ca316 6587 if (per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6588 {
6589 sig_type->per_cu.v.quick =
976ca316 6590 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6591 struct dwarf2_per_cu_quick_data);
6592 }
6593
6594 if (slot == NULL)
6595 {
976ca316 6596 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6597 sig_type, INSERT);
6598 }
6599 gdb_assert (*slot == NULL);
6600 *slot = sig_type;
6601 /* The rest of sig_type must be filled in by the caller. */
6602 return sig_type;
6603}
6604
a2ce51a0
DE
6605/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6606 Fill in SIG_ENTRY with DWO_ENTRY. */
6607
6608static void
976ca316 6609fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6610 struct signatured_type *sig_entry,
6611 struct dwo_unit *dwo_entry)
6612{
976ca316 6613 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6614
7ee85ab1 6615 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6616 gdb_assert (! sig_entry->per_cu.queued);
976ca316 6617 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6618 if (per_bfd->using_index)
6aa5f3a6
DE
6619 {
6620 gdb_assert (sig_entry->per_cu.v.quick != NULL);
976ca316 6621 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6622 }
6623 else
6624 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6625 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6626 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6627 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6628 gdb_assert (sig_entry->dwo_unit == NULL);
6629
6630 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6631 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6632 sig_entry->per_cu.length = dwo_entry->length;
6633 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6634 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6635 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6636 sig_entry->dwo_unit = dwo_entry;
6637}
6638
6639/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6640 If we haven't read the TU yet, create the signatured_type data structure
6641 for a TU to be read in directly from a DWO file, bypassing the stub.
6642 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6643 using .gdb_index, then when reading a CU we want to stay in the DWO file
6644 containing that CU. Otherwise we could end up reading several other DWO
6645 files (due to comdat folding) to process the transitive closure of all the
6646 mentioned TUs, and that can be slow. The current DWO file will have every
6647 type signature that it needs.
a2ce51a0
DE
6648 We only do this for .gdb_index because in the psymtab case we already have
6649 to read all the DWOs to build the type unit groups. */
6650
6651static struct signatured_type *
6652lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6653{
976ca316 6654 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6655 struct dwo_file *dwo_file;
6656 struct dwo_unit find_dwo_entry, *dwo_entry;
6657 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6658 void **slot;
a2ce51a0 6659
976ca316 6660 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6661
6aa5f3a6
DE
6662 /* If TU skeletons have been removed then we may not have read in any
6663 TUs yet. */
976ca316
SM
6664 if (per_objfile->per_bfd->signatured_types == NULL)
6665 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6666
6667 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6668 Use the global signatured_types array to do our own comdat-folding
6669 of types. If this is the first time we're reading this TU, and
6670 the TU has an entry in .gdb_index, replace the recorded data from
6671 .gdb_index with this TU. */
a2ce51a0 6672
a2ce51a0 6673 find_sig_entry.signature = sig;
976ca316 6674 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6675 &find_sig_entry, INSERT);
9a3c8263 6676 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6677
6678 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6679 read. Don't reassign the global entry to point to this DWO if that's
6680 the case. Also note that if the TU is already being read, it may not
6681 have come from a DWO, the program may be a mix of Fission-compiled
6682 code and non-Fission-compiled code. */
6683
6684 /* Have we already tried to read this TU?
6685 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6686 needn't exist in the global table yet). */
6687 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6688 return sig_entry;
6689
6aa5f3a6
DE
6690 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6691 dwo_unit of the TU itself. */
6692 dwo_file = cu->dwo_unit->dwo_file;
6693
a2ce51a0
DE
6694 /* Ok, this is the first time we're reading this TU. */
6695 if (dwo_file->tus == NULL)
6696 return NULL;
6697 find_dwo_entry.signature = sig;
b0b6a987
TT
6698 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6699 &find_dwo_entry);
a2ce51a0
DE
6700 if (dwo_entry == NULL)
6701 return NULL;
6702
6aa5f3a6
DE
6703 /* If the global table doesn't have an entry for this TU, add one. */
6704 if (sig_entry == NULL)
976ca316 6705 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6706
976ca316 6707 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
89e63ee4 6708 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6709 return sig_entry;
6710}
6711
a2ce51a0
DE
6712/* Subroutine of lookup_signatured_type.
6713 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6714 then try the DWP file. If the TU stub (skeleton) has been removed then
6715 it won't be in .gdb_index. */
a2ce51a0
DE
6716
6717static struct signatured_type *
6718lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6719{
976ca316
SM
6720 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6721 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6722 struct dwo_unit *dwo_entry;
6723 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6724 void **slot;
a2ce51a0 6725
976ca316 6726 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6727 gdb_assert (dwp_file != NULL);
6728
6aa5f3a6
DE
6729 /* If TU skeletons have been removed then we may not have read in any
6730 TUs yet. */
976ca316
SM
6731 if (per_objfile->per_bfd->signatured_types == NULL)
6732 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6733
6aa5f3a6 6734 find_sig_entry.signature = sig;
976ca316 6735 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6736 &find_sig_entry, INSERT);
9a3c8263 6737 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6738
6739 /* Have we already tried to read this TU?
6740 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6741 needn't exist in the global table yet). */
6742 if (sig_entry != NULL)
6743 return sig_entry;
6744
a2ce51a0
DE
6745 if (dwp_file->tus == NULL)
6746 return NULL;
976ca316
SM
6747 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6748 1 /* is_debug_types */);
a2ce51a0
DE
6749 if (dwo_entry == NULL)
6750 return NULL;
6751
976ca316
SM
6752 sig_entry = add_type_unit (per_objfile, sig, slot);
6753 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6754
a2ce51a0
DE
6755 return sig_entry;
6756}
6757
380bca97 6758/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6759 Returns NULL if signature SIG is not present in the table.
6760 It is up to the caller to complain about this. */
348e048f
DE
6761
6762static struct signatured_type *
a2ce51a0 6763lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6764{
976ca316 6765 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6766
976ca316 6767 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6768 {
6769 /* We're in a DWO/DWP file, and we're using .gdb_index.
6770 These cases require special processing. */
976ca316 6771 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6772 return lookup_dwo_signatured_type (cu, sig);
6773 else
6774 return lookup_dwp_signatured_type (cu, sig);
6775 }
6776 else
6777 {
6778 struct signatured_type find_entry, *entry;
348e048f 6779
976ca316 6780 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6781 return NULL;
6782 find_entry.signature = sig;
9a3c8263 6783 entry = ((struct signatured_type *)
976ca316 6784 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6785 &find_entry));
a2ce51a0
DE
6786 return entry;
6787 }
348e048f 6788}
18a8505e 6789
42e7ad6c 6790/* Low level DIE reading support. */
348e048f 6791
d85a05f0
DJ
6792/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6793
6794static void
6795init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6796 struct dwarf2_cu *cu,
3019eac3 6797 struct dwarf2_section_info *section,
685af9cd
TT
6798 struct dwo_file *dwo_file,
6799 struct abbrev_table *abbrev_table)
d85a05f0 6800{
fceca515 6801 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6802 reader->abfd = section->get_bfd_owner ();
d85a05f0 6803 reader->cu = cu;
3019eac3 6804 reader->dwo_file = dwo_file;
dee91e82
DE
6805 reader->die_section = section;
6806 reader->buffer = section->buffer;
f664829e 6807 reader->buffer_end = section->buffer + section->size;
685af9cd 6808 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6809}
6810
c0ab21c2 6811/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6812 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6813 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6814 already.
6815
6816 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6817 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6818 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6819 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6820 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6821 STUB_COMP_DIR may be non-NULL.
3e225074 6822 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6823 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6824 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6825 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6826 kept around for at least as long as *RESULT_READER.
6827
b0c7bfa9
DE
6828 The result is non-zero if a valid (non-dummy) DIE was found. */
6829
6830static int
4ab09049 6831read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6832 struct dwo_unit *dwo_unit,
b0c7bfa9 6833 struct die_info *stub_comp_unit_die,
a2ce51a0 6834 const char *stub_comp_dir,
b0c7bfa9 6835 struct die_reader_specs *result_reader,
d521ce57 6836 const gdb_byte **result_info_ptr,
b0c7bfa9 6837 struct die_info **result_comp_unit_die,
685af9cd 6838 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6839{
976ca316 6840 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6841 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6842 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6843 bfd *abfd;
d521ce57 6844 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6845 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6846 int i,num_extra_attrs;
6847 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6848 struct die_info *comp_unit_die;
6849
b0aeadb3
DE
6850 /* At most one of these may be provided. */
6851 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6852
b0c7bfa9
DE
6853 /* These attributes aren't processed until later:
6854 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6855 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6856 referenced later. However, these attributes are found in the stub
6857 which we won't have later. In order to not impose this complication
6858 on the rest of the code, we read them here and copy them to the
6859 DWO CU/TU die. */
b0c7bfa9
DE
6860
6861 stmt_list = NULL;
6862 low_pc = NULL;
6863 high_pc = NULL;
6864 ranges = NULL;
6865 comp_dir = NULL;
6866
6867 if (stub_comp_unit_die != NULL)
6868 {
6869 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6870 DWO file. */
4ab09049 6871 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6872 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6873 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6874 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6875 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6876 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6877
a39fdb41 6878 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6879
2b0c7f41
SM
6880 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6881 We need the value before we can process DW_AT_ranges values from the
6882 DWO. */
6883 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6884
6885 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6886 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6887 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6888 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6889 section. */
6890 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
b0c7bfa9 6891 }
a2ce51a0
DE
6892 else if (stub_comp_dir != NULL)
6893 {
6894 /* Reconstruct the comp_dir attribute to simplify the code below. */
fe56917a 6895 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6896 comp_dir->name = DW_AT_comp_dir;
6897 comp_dir->form = DW_FORM_string;
c6481205 6898 comp_dir->set_string_noncanonical (stub_comp_dir);
a2ce51a0 6899 }
b0c7bfa9
DE
6900
6901 /* Set up for reading the DWO CU/TU. */
6902 cu->dwo_unit = dwo_unit;
685af9cd 6903 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6904 section->read (objfile);
6905 abfd = section->get_bfd_owner ();
9c541725
PA
6906 begin_info_ptr = info_ptr = (section->buffer
6907 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6908 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6909
4ab09049 6910 if (per_cu->is_debug_types)
b0c7bfa9 6911 {
4ab09049 6912 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6913
976ca316
SM
6914 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6915 section, dwo_abbrev_section,
43988095 6916 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6917 /* This is not an assert because it can be caused by bad debug info. */
43988095 6918 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6919 {
6920 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6921 " TU at offset %s [in module %s]"),
a2ce51a0 6922 hex_string (sig_type->signature),
43988095 6923 hex_string (cu->header.signature),
9d8780f0 6924 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6925 bfd_get_filename (abfd));
6926 }
9c541725 6927 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6928 /* For DWOs coming from DWP files, we don't know the CU length
6929 nor the type's offset in the TU until now. */
4057dfde 6930 dwo_unit->length = cu->header.get_length ();
9c541725 6931 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6932
6933 /* Establish the type offset that can be used to lookup the type.
6934 For DWO files, we don't know it until now. */
9c541725
PA
6935 sig_type->type_offset_in_section
6936 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6937 }
6938 else
6939 {
976ca316
SM
6940 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6941 section, dwo_abbrev_section,
43988095 6942 info_ptr, rcuh_kind::COMPILE);
9c541725 6943 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6944 /* For DWOs coming from DWP files, we don't know the CU length
6945 until now. */
4057dfde 6946 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6947 }
6948
606decb2 6949 dwo_abbrev_section->read (objfile);
685af9cd 6950 *result_dwo_abbrev_table
606decb2 6951 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
685af9cd
TT
6952 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6953 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6954
6955 /* Read in the die, but leave space to copy over the attributes
6956 from the stub. This has the benefit of simplifying the rest of
6957 the code - all the work to maintain the illusion of a single
6958 DW_TAG_{compile,type}_unit DIE is done here. */
6959 num_extra_attrs = ((stmt_list != NULL)
6960 + (low_pc != NULL)
6961 + (high_pc != NULL)
6962 + (ranges != NULL)
6963 + (comp_dir != NULL));
6964 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6965 num_extra_attrs);
b0c7bfa9
DE
6966
6967 /* Copy over the attributes from the stub to the DIE we just read in. */
6968 comp_unit_die = *result_comp_unit_die;
6969 i = comp_unit_die->num_attrs;
6970 if (stmt_list != NULL)
6971 comp_unit_die->attrs[i++] = *stmt_list;
6972 if (low_pc != NULL)
6973 comp_unit_die->attrs[i++] = *low_pc;
6974 if (high_pc != NULL)
6975 comp_unit_die->attrs[i++] = *high_pc;
6976 if (ranges != NULL)
6977 comp_unit_die->attrs[i++] = *ranges;
6978 if (comp_dir != NULL)
6979 comp_unit_die->attrs[i++] = *comp_dir;
6980 comp_unit_die->num_attrs += num_extra_attrs;
6981
b4f54984 6982 if (dwarf_die_debug)
bf6af496
DE
6983 {
6984 fprintf_unfiltered (gdb_stdlog,
6985 "Read die from %s@0x%x of %s:\n",
96b79293 6986 section->get_name (),
bf6af496
DE
6987 (unsigned) (begin_info_ptr - section->buffer),
6988 bfd_get_filename (abfd));
b4f54984 6989 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6990 }
6991
b0c7bfa9
DE
6992 /* Skip dummy compilation units. */
6993 if (info_ptr >= begin_info_ptr + dwo_unit->length
6994 || peek_abbrev_code (abfd, info_ptr) == 0)
6995 return 0;
6996
6997 *result_info_ptr = info_ptr;
6998 return 1;
6999}
7000
a084a2a6
AT
7001/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7002 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7003 signature is part of the header. */
7004static gdb::optional<ULONGEST>
7005lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7006{
7007 if (cu->header.version >= 5)
7008 return cu->header.signature;
7009 struct attribute *attr;
7010 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
529908cb 7011 if (attr == nullptr || !attr->form_is_unsigned ())
a084a2a6 7012 return gdb::optional<ULONGEST> ();
529908cb 7013 return attr->as_unsigned ();
a084a2a6
AT
7014}
7015
c0ab21c2 7016/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 7017 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7018 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7019
7020static struct dwo_unit *
4ab09049 7021lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 7022{
4ab09049 7023 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 7024 struct dwo_unit *dwo_unit;
c0ab21c2 7025 const char *comp_dir;
b0c7bfa9 7026
a2ce51a0
DE
7027 gdb_assert (cu != NULL);
7028
b0c7bfa9 7029 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7030 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7031 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 7032
4ab09049
SM
7033 if (per_cu->is_debug_types)
7034 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
7035 else
7036 {
a084a2a6 7037 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 7038
a084a2a6 7039 if (!signature.has_value ())
b0c7bfa9
DE
7040 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7041 " [in module %s]"),
4ab09049
SM
7042 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7043
7044 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
7045 }
7046
b0c7bfa9
DE
7047 return dwo_unit;
7048}
7049
c0ab21c2 7050/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 7051 See it for a description of the parameters.
fcd3b13d 7052 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 7053
c0ab21c2 7054void
9e021579
SM
7055cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7056 dwarf2_per_objfile *per_objfile,
2e671100 7057 dwarf2_cu *existing_cu)
a2ce51a0 7058{
a2ce51a0 7059 struct signatured_type *sig_type;
a2ce51a0
DE
7060
7061 /* Verify we can do the following downcast, and that we have the
7062 data we need. */
7063 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7064 sig_type = (struct signatured_type *) this_cu;
7065 gdb_assert (sig_type->dwo_unit != NULL);
7066
2e671100
SM
7067 dwarf2_cu *cu;
7068
7069 if (existing_cu != nullptr)
6aa5f3a6 7070 {
2e671100
SM
7071 cu = existing_cu;
7072 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 7073 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 7074 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
7075 }
7076 else
7077 {
7188ed02 7078 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 7079 in per_objfile yet. */
7188ed02 7080 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 7081 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 7082 cu = m_new_cu.get ();
6aa5f3a6
DE
7083 }
7084
7085 /* A future optimization, if needed, would be to use an existing
7086 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7087 could share abbrev tables. */
a2ce51a0 7088
2e671100 7089 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
7090 NULL /* stub_comp_unit_die */,
7091 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 7092 this, &info_ptr,
3e225074 7093 &comp_unit_die,
c0ab21c2 7094 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7095 {
7096 /* Dummy die. */
c0ab21c2 7097 dummy_p = true;
a2ce51a0 7098 }
a2ce51a0
DE
7099}
7100
fd820528 7101/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7102 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7103
f4dc4d17
DE
7104 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7105 Otherwise the table specified in the comp unit header is read in and used.
7106 This is an optimization for when we already have the abbrev table.
7107
2e671100
SM
7108 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7109 allocated. */
aaa75496 7110
ab432490 7111cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7112 dwarf2_per_objfile *per_objfile,
c0ab21c2 7113 struct abbrev_table *abbrev_table,
2e671100 7114 dwarf2_cu *existing_cu,
c0ab21c2
TT
7115 bool skip_partial)
7116 : die_reader_specs {},
6751ebae 7117 m_this_cu (this_cu)
c906108c 7118{
976ca316 7119 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7120 struct dwarf2_section_info *section = this_cu->section;
96b79293 7121 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 7122 const gdb_byte *begin_info_ptr;
dee91e82 7123 struct signatured_type *sig_type = NULL;
4bdcc0c1 7124 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7125 /* Non-zero if CU currently points to a DWO file and we need to
7126 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7127 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7128 int rereading_dwo_cu = 0;
c906108c 7129
b4f54984 7130 if (dwarf_die_debug)
9d8780f0 7131 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7132 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7133 sect_offset_str (this_cu->sect_off));
09406207 7134
a2ce51a0
DE
7135 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7136 file (instead of going through the stub), short-circuit all of this. */
7137 if (this_cu->reading_dwo_directly)
7138 {
7139 /* Narrow down the scope of possibilities to have to understand. */
7140 gdb_assert (this_cu->is_debug_types);
7141 gdb_assert (abbrev_table == NULL);
976ca316 7142 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
7143 return;
7144 }
7145
dee91e82 7146 /* This is cheap if the section is already read in. */
96b79293 7147 section->read (objfile);
dee91e82 7148
9c541725 7149 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7150
7151 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7152
2e671100
SM
7153 dwarf2_cu *cu;
7154
7155 if (existing_cu != nullptr)
dee91e82 7156 {
2e671100 7157 cu = existing_cu;
42e7ad6c
DE
7158 /* If this CU is from a DWO file we need to start over, we need to
7159 refetch the attributes from the skeleton CU.
7160 This could be optimized by retrieving those attributes from when we
7161 were here the first time: the previous comp_unit_die was stored in
7162 comp_unit_obstack. But there's no data yet that we need this
7163 optimization. */
7164 if (cu->dwo_unit != NULL)
7165 rereading_dwo_cu = 1;
dee91e82
DE
7166 }
7167 else
7168 {
7188ed02 7169 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 7170 in per_objfile yet. */
976ca316
SM
7171 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7172 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 7173 cu = m_new_cu.get ();
42e7ad6c 7174 }
dee91e82 7175
b0c7bfa9 7176 /* Get the header. */
9c541725 7177 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7178 {
7179 /* We already have the header, there's no need to read it in again. */
9c541725 7180 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7181 }
7182 else
7183 {
3019eac3 7184 if (this_cu->is_debug_types)
dee91e82 7185 {
976ca316
SM
7186 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7187 section, abbrev_section,
7188 info_ptr, rcuh_kind::TYPE);
dee91e82 7189
42e7ad6c
DE
7190 /* Since per_cu is the first member of struct signatured_type,
7191 we can go from a pointer to one to a pointer to the other. */
7192 sig_type = (struct signatured_type *) this_cu;
43988095 7193 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7194 gdb_assert (sig_type->type_offset_in_tu
7195 == cu->header.type_cu_offset_in_tu);
7196 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7197
42e7ad6c
DE
7198 /* LENGTH has not been set yet for type units if we're
7199 using .gdb_index. */
4057dfde 7200 this_cu->length = cu->header.get_length ();
3019eac3
DE
7201
7202 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7203 sig_type->type_offset_in_section =
7204 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7205
7206 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7207 }
7208 else
7209 {
976ca316
SM
7210 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7211 section, abbrev_section,
43988095
JK
7212 info_ptr,
7213 rcuh_kind::COMPILE);
dee91e82 7214
9c541725 7215 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7216 if (this_cu->length == 0)
7217 this_cu->length = cu->header.get_length ();
7218 else
7219 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7220 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7221 }
7222 }
10b3939b 7223
6caca83c 7224 /* Skip dummy compilation units. */
dee91e82 7225 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7226 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7227 {
7228 dummy_p = true;
7229 return;
7230 }
6caca83c 7231
433df2d4
DE
7232 /* If we don't have them yet, read the abbrevs for this compilation unit.
7233 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7234 done. */
f4dc4d17 7235 if (abbrev_table != NULL)
685af9cd
TT
7236 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7237 else
f4dc4d17 7238 {
606decb2 7239 abbrev_section->read (objfile);
c0ab21c2 7240 m_abbrev_table_holder
606decb2 7241 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
c0ab21c2 7242 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7243 }
af703f96 7244
dee91e82 7245 /* Read the top level CU/TU die. */
c0ab21c2 7246 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7247 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7248
58f0c718 7249 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7250 {
7251 dummy_p = true;
7252 return;
7253 }
58f0c718 7254
b0c7bfa9 7255 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7256 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7257 table from the DWO file and pass the ownership over to us. It will be
7258 referenced from READER, so we must make sure to free it after we're done
7259 with READER.
7260
b0c7bfa9
DE
7261 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7262 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7263 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7264 if (dwo_name != nullptr)
3019eac3 7265 {
3019eac3 7266 struct dwo_unit *dwo_unit;
b0c7bfa9 7267 struct die_info *dwo_comp_unit_die;
3019eac3 7268
3e225074 7269 if (comp_unit_die->has_children)
6a506a2d 7270 {
b98664d3 7271 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7272 " has children (offset %s) [in module %s]"),
7273 sect_offset_str (this_cu->sect_off),
7274 bfd_get_filename (abfd));
6a506a2d 7275 }
4ab09049 7276 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7277 if (dwo_unit != NULL)
3019eac3 7278 {
4ab09049 7279 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7280 comp_unit_die, NULL,
c0ab21c2 7281 this, &info_ptr,
3e225074 7282 &dwo_comp_unit_die,
c0ab21c2 7283 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7284 {
7285 /* Dummy die. */
c0ab21c2 7286 dummy_p = true;
6a506a2d
DE
7287 return;
7288 }
7289 comp_unit_die = dwo_comp_unit_die;
7290 }
7291 else
7292 {
7293 /* Yikes, we couldn't find the rest of the DIE, we only have
7294 the stub. A complaint has already been logged. There's
7295 not much more we can do except pass on the stub DIE to
7296 die_reader_func. We don't want to throw an error on bad
7297 debug info. */
3019eac3
DE
7298 }
7299 }
c0ab21c2 7300}
3019eac3 7301
6751ebae
TT
7302void
7303cutu_reader::keep ()
c0ab21c2 7304{
b0c7bfa9 7305 /* Done, clean up. */
6751ebae
TT
7306 gdb_assert (!dummy_p);
7307 if (m_new_cu != NULL)
348e048f 7308 {
7188ed02 7309 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
dda83cd7 7310 now. */
7188ed02
SM
7311 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7312 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 7313 }
dee91e82
DE
7314}
7315
18a8505e
AT
7316/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7317 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7318 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7319
7320 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7321 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7322
7323 We fill in THIS_CU->length.
7324
dee91e82 7325 THIS_CU->cu is always freed when done.
3019eac3 7326 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7327 to care whether it refers to the "main" CU or the DWO CU.
7328
7329 When parent_cu is passed, it is used to provide a default value for
7330 str_offsets_base and addr_base from the parent. */
dee91e82 7331
ab432490 7332cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7333 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
7334 struct dwarf2_cu *parent_cu,
7335 struct dwo_file *dwo_file)
7336 : die_reader_specs {},
7337 m_this_cu (this_cu)
dee91e82 7338{
976ca316 7339 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7340 struct dwarf2_section_info *section = this_cu->section;
96b79293 7341 bfd *abfd = section->get_bfd_owner ();
33e80786 7342 struct dwarf2_section_info *abbrev_section;
d521ce57 7343 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7344
b4f54984 7345 if (dwarf_die_debug)
9d8780f0 7346 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7347 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7348 sect_offset_str (this_cu->sect_off));
09406207 7349
976ca316 7350 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7351
33e80786
DE
7352 abbrev_section = (dwo_file != NULL
7353 ? &dwo_file->sections.abbrev
7354 : get_abbrev_section_for_cu (this_cu));
7355
dee91e82 7356 /* This is cheap if the section is already read in. */
96b79293 7357 section->read (objfile);
dee91e82 7358
976ca316 7359 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 7360
9c541725 7361 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
7362 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7363 section, abbrev_section, info_ptr,
43988095
JK
7364 (this_cu->is_debug_types
7365 ? rcuh_kind::TYPE
7366 : rcuh_kind::COMPILE));
dee91e82 7367
18a8505e
AT
7368 if (parent_cu != nullptr)
7369 {
c0ab21c2
TT
7370 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7371 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7372 }
4057dfde 7373 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7374
7375 /* Skip dummy compilation units. */
7376 if (info_ptr >= begin_info_ptr + this_cu->length
7377 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7378 {
7379 dummy_p = true;
7380 return;
7381 }
72bf9492 7382
606decb2 7383 abbrev_section->read (objfile);
c0ab21c2 7384 m_abbrev_table_holder
606decb2 7385 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
dee91e82 7386
c0ab21c2
TT
7387 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7388 m_abbrev_table_holder.get ());
3e225074 7389 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7390}
7391
0018ea6f
DE
7392\f
7393/* Type Unit Groups.
dee91e82 7394
0018ea6f
DE
7395 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7396 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7397 so that all types coming from the same compilation (.o file) are grouped
7398 together. A future step could be to put the types in the same symtab as
7399 the CU the types ultimately came from. */
ff013f42 7400
f4dc4d17
DE
7401static hashval_t
7402hash_type_unit_group (const void *item)
7403{
9a3c8263
SM
7404 const struct type_unit_group *tu_group
7405 = (const struct type_unit_group *) item;
f4dc4d17 7406
094b34ac 7407 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7408}
348e048f
DE
7409
7410static int
f4dc4d17 7411eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7412{
9a3c8263
SM
7413 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7414 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7415
094b34ac 7416 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7417}
348e048f 7418
f4dc4d17
DE
7419/* Allocate a hash table for type unit groups. */
7420
eaa5fa8b 7421static htab_up
298e9637 7422allocate_type_unit_groups_table ()
f4dc4d17 7423{
eaa5fa8b
TT
7424 return htab_up (htab_create_alloc (3,
7425 hash_type_unit_group,
7426 eq_type_unit_group,
7427 NULL, xcalloc, xfree));
f4dc4d17 7428}
dee91e82 7429
f4dc4d17
DE
7430/* Type units that don't have DW_AT_stmt_list are grouped into their own
7431 partial symtabs. We combine several TUs per psymtab to not let the size
7432 of any one psymtab grow too big. */
7433#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7434#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7435
094b34ac 7436/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7437 Create the type_unit_group object used to hold one or more TUs. */
7438
7439static struct type_unit_group *
094b34ac 7440create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7441{
976ca316
SM
7442 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7443 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
094b34ac 7444 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7445 struct type_unit_group *tu_group;
f4dc4d17 7446
976ca316 7447 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
094b34ac 7448 per_cu = &tu_group->per_cu;
1859c670 7449 per_cu->per_bfd = per_bfd;
f4dc4d17 7450
1859c670 7451 if (per_bfd->using_index)
094b34ac 7452 {
1859c670 7453 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7454 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7455 }
7456 else
7457 {
9c541725 7458 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7459 dwarf2_psymtab *pst;
528e1572 7460 std::string name;
094b34ac
DE
7461
7462 /* Give the symtab a useful name for debug purposes. */
7463 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7464 name = string_printf ("<type_units_%d>",
7465 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7466 else
528e1572 7467 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7468
976ca316 7469 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
6d94535f 7470 pst->anonymous = true;
094b34ac 7471 }
f4dc4d17 7472
094b34ac 7473 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7474 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7475
7476 return tu_group;
7477}
7478
094b34ac
DE
7479/* Look up the type_unit_group for type unit CU, and create it if necessary.
7480 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7481
7482static struct type_unit_group *
ff39bb5e 7483get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7484{
976ca316
SM
7485 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7486 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7487 struct type_unit_group *tu_group;
7488 void **slot;
7489 unsigned int line_offset;
7490 struct type_unit_group type_unit_group_for_lookup;
7491
976ca316
SM
7492 if (per_objfile->per_bfd->type_unit_groups == NULL)
7493 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7494
7495 /* Do we need to create a new group, or can we use an existing one? */
7496
529908cb 7497 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
f4dc4d17 7498 {
529908cb 7499 line_offset = stmt_list->as_unsigned ();
f4dc4d17
DE
7500 ++tu_stats->nr_symtab_sharers;
7501 }
7502 else
7503 {
7504 /* Ugh, no stmt_list. Rare, but we have to handle it.
7505 We can do various things here like create one group per TU or
7506 spread them over multiple groups to split up the expansion work.
7507 To avoid worst case scenarios (too many groups or too large groups)
7508 we, umm, group them in bunches. */
7509 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7510 | (tu_stats->nr_stmt_less_type_units
7511 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7512 ++tu_stats->nr_stmt_less_type_units;
7513 }
7514
094b34ac 7515 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7516 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7517 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7518 &type_unit_group_for_lookup, INSERT);
7519 if (*slot != NULL)
7520 {
9a3c8263 7521 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7522 gdb_assert (tu_group != NULL);
7523 }
7524 else
7525 {
9c541725 7526 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7527 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7528 *slot = tu_group;
7529 ++tu_stats->nr_symtabs;
7530 }
7531
7532 return tu_group;
7533}
0018ea6f
DE
7534\f
7535/* Partial symbol tables. */
7536
7537/* Create a psymtab named NAME and assign it to PER_CU.
7538
7539 The caller must fill in the following details:
7540 dirname, textlow, texthigh. */
7541
891813be 7542static dwarf2_psymtab *
7aa104c4
SM
7543create_partial_symtab (dwarf2_per_cu_data *per_cu,
7544 dwarf2_per_objfile *per_objfile,
7545 const char *name)
0018ea6f 7546{
7aa104c4 7547 struct objfile *objfile = per_objfile->objfile;
891813be 7548 dwarf2_psymtab *pst;
0018ea6f 7549
9f4e76a4 7550 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7551
6d94535f 7552 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7553
7554 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7555 per_cu->v.psymtab = pst;
7556
7557 return pst;
7558}
7559
c0ab21c2 7560/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7561
7562static void
7563process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7564 const gdb_byte *info_ptr,
0018ea6f 7565 struct die_info *comp_unit_die,
c0ab21c2 7566 enum language pretend_language)
0018ea6f
DE
7567{
7568 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7569 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7570 struct objfile *objfile = per_objfile->objfile;
08feed99 7571 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7572 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7573 CORE_ADDR baseaddr;
7574 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7575 dwarf2_psymtab *pst;
3a2b436a 7576 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7577 const char *filename;
0018ea6f 7578
0018ea6f
DE
7579 gdb_assert (! per_cu->is_debug_types);
7580
c0ab21c2 7581 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7582
0018ea6f 7583 /* Allocate a new partial symbol table structure. */
2e927613
TV
7584 gdb::unique_xmalloc_ptr<char> debug_filename;
7585 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7586 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7587 if (filename == NULL)
0018ea6f 7588 filename = "";
2e927613
TV
7589 else if (strcmp (filename, artificial) == 0)
7590 {
7591 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7592 sect_offset_str (per_cu->sect_off),
7593 (char *) NULL));
2e927613
TV
7594 filename = debug_filename.get ();
7595 }
0018ea6f 7596
7aa104c4 7597 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7598
7599 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7600 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7601
b3b3bada 7602 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7603
7604 dwarf2_find_base_address (comp_unit_die, cu);
7605
7606 /* Possibly set the default values of LOWPC and HIGHPC from
7607 `DW_AT_ranges'. */
3a2b436a
JK
7608 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7609 &best_highpc, cu, pst);
7610 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7611 {
7612 CORE_ADDR low
7613 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7614 - baseaddr);
7615 CORE_ADDR high
7616 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7617 - baseaddr - 1);
7618 /* Store the contiguous range if it is not empty; it can be
7619 empty for CUs with no code. */
d320c2b5
TT
7620 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7621 low, high, pst);
79748972 7622 }
0018ea6f
DE
7623
7624 /* Check if comp unit has_children.
7625 If so, read the rest of the partial symbols from this comp unit.
7626 If not, there's no more debug_info for this comp unit. */
3e225074 7627 if (comp_unit_die->has_children)
0018ea6f
DE
7628 {
7629 struct partial_die_info *first_die;
7630 CORE_ADDR lowpc, highpc;
7631
7632 lowpc = ((CORE_ADDR) -1);
7633 highpc = ((CORE_ADDR) 0);
7634
7635 first_die = load_partial_dies (reader, info_ptr, 1);
7636
7637 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7638 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7639
7640 /* If we didn't find a lowpc, set it to highpc to avoid
7641 complaints from `maint check'. */
7642 if (lowpc == ((CORE_ADDR) -1))
7643 lowpc = highpc;
7644
7645 /* If the compilation unit didn't have an explicit address range,
7646 then use the information extracted from its child dies. */
e385593e 7647 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7648 {
7649 best_lowpc = lowpc;
7650 best_highpc = highpc;
7651 }
7652 }
4ae976d1 7653 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7654 best_lowpc + baseaddr)
7655 - baseaddr);
4ae976d1 7656 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7657 best_highpc + baseaddr)
7658 - baseaddr);
0018ea6f 7659
ae7754b2 7660 pst->end ();
0018ea6f 7661
ae640021 7662 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7663 {
7664 int i;
ae640021 7665 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7666
7667 /* Fill in 'dependencies' here; we fill in 'users' in a
7668 post-pass. */
7669 pst->number_of_dependencies = len;
a9342b62
TT
7670 pst->dependencies
7671 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7672 for (i = 0; i < len; ++i)
7673 {
7674 pst->dependencies[i]
7675 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7676 }
0018ea6f 7677
ae640021 7678 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7679 }
7680
7681 /* Get the list of files included in the current compilation unit,
7682 and build a psymtab for each of them. */
7683 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7684
6f738b01
SM
7685 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7686 ", %d global, %d static syms",
7687 per_cu->is_debug_types ? "type" : "comp",
7688 sect_offset_str (per_cu->sect_off),
7689 paddress (gdbarch, pst->text_low (objfile)),
7690 paddress (gdbarch, pst->text_high (objfile)),
7691 (int) pst->global_psymbols.size (),
7692 (int) pst->static_psymbols.size ());
0018ea6f
DE
7693}
7694
7695/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7696 Process compilation unit THIS_CU for a psymtab. */
7697
7698static void
ab432490
SM
7699process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7700 dwarf2_per_objfile *per_objfile,
135f5437 7701 bool want_partial_unit,
b93601f3 7702 enum language pretend_language)
0018ea6f
DE
7703{
7704 /* If this compilation unit was already read in, free the
7705 cached copy in order to read it in again. This is
7706 necessary because we skipped some symbols when we first
7707 read in the compilation unit (see load_partial_dies).
7708 This problem could be avoided, but the benefit is unclear. */
7188ed02 7709 per_objfile->remove_cu (this_cu);
0018ea6f 7710
2e671100 7711 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7712
58990295
TV
7713 switch (reader.comp_unit_die->tag)
7714 {
7715 case DW_TAG_compile_unit:
7716 this_cu->unit_type = DW_UT_compile;
7717 break;
7718 case DW_TAG_partial_unit:
7719 this_cu->unit_type = DW_UT_partial;
7720 break;
e77b0004
TV
7721 case DW_TAG_type_unit:
7722 this_cu->unit_type = DW_UT_type;
7723 break;
58990295
TV
7724 default:
7725 abort ();
7726 }
7727
c0ab21c2 7728 if (reader.dummy_p)
f1902523 7729 {
c0ab21c2 7730 /* Nothing. */
f1902523 7731 }
c0ab21c2 7732 else if (this_cu->is_debug_types)
3e225074
TT
7733 build_type_psymtabs_reader (&reader, reader.info_ptr,
7734 reader.comp_unit_die);
135f5437
TT
7735 else if (want_partial_unit
7736 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7737 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7738 reader.comp_unit_die,
c0ab21c2 7739 pretend_language);
0018ea6f 7740
7188ed02 7741 this_cu->lang = reader.cu->language;
58990295 7742
0018ea6f 7743 /* Age out any secondary CUs. */
7188ed02 7744 per_objfile->age_comp_units ();
0018ea6f 7745}
f4dc4d17
DE
7746
7747/* Reader function for build_type_psymtabs. */
7748
7749static void
7750build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7751 const gdb_byte *info_ptr,
3e225074 7752 struct die_info *type_unit_die)
f4dc4d17 7753{
976ca316 7754 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7755 struct dwarf2_cu *cu = reader->cu;
7756 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7757 struct signatured_type *sig_type;
f4dc4d17
DE
7758 struct type_unit_group *tu_group;
7759 struct attribute *attr;
7760 struct partial_die_info *first_die;
7761 CORE_ADDR lowpc, highpc;
891813be 7762 dwarf2_psymtab *pst;
f4dc4d17 7763
0186c6a7
DE
7764 gdb_assert (per_cu->is_debug_types);
7765 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7766
3e225074 7767 if (! type_unit_die->has_children)
f4dc4d17
DE
7768 return;
7769
052c8bb8 7770 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7771 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7772
df07e2c7 7773 if (tu_group->tus == nullptr)
a8b3b8e9 7774 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7775 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7776
7777 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7778 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7779 pst->anonymous = true;
f4dc4d17
DE
7780
7781 first_die = load_partial_dies (reader, info_ptr, 1);
7782
7783 lowpc = (CORE_ADDR) -1;
7784 highpc = (CORE_ADDR) 0;
7785 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7786
ae7754b2 7787 pst->end ();
f4dc4d17
DE
7788}
7789
73051182
DE
7790/* Struct used to sort TUs by their abbreviation table offset. */
7791
7792struct tu_abbrev_offset
7793{
b2bdb8cf
SM
7794 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7795 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7796 {}
7797
7798 signatured_type *sig_type;
73051182
DE
7799 sect_offset abbrev_offset;
7800};
7801
484cf504 7802/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7803
484cf504
TT
7804static bool
7805sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7806 const struct tu_abbrev_offset &b)
73051182 7807{
484cf504 7808 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7809}
7810
7811/* Efficiently read all the type units.
7812 This does the bulk of the work for build_type_psymtabs.
7813
7814 The efficiency is because we sort TUs by the abbrev table they use and
7815 only read each abbrev table once. In one program there are 200K TUs
7816 sharing 8K abbrev tables.
7817
7818 The main purpose of this function is to support building the
5989a64e 7819 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7820 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7821 can collapse the search space by grouping them by stmt_list.
7822 The savings can be significant, in the same program from above the 200K TUs
7823 share 8K stmt_list tables.
7824
7825 FUNC is expected to call get_type_unit_group, which will create the
7826 struct type_unit_group if necessary and add it to
5989a64e 7827 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7828
7829static void
976ca316 7830build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
73051182 7831{
976ca316 7832 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7833 abbrev_table_up abbrev_table;
73051182 7834 sect_offset abbrev_offset;
73051182
DE
7835
7836 /* It's up to the caller to not call us multiple times. */
976ca316 7837 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7838
976ca316 7839 if (per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7840 return;
7841
7842 /* TUs typically share abbrev tables, and there can be way more TUs than
7843 abbrev tables. Sort by abbrev table to reduce the number of times we
7844 read each abbrev table in.
7845 Alternatives are to punt or to maintain a cache of abbrev tables.
7846 This is simpler and efficient enough for now.
7847
7848 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7849 symtab to use). Typically TUs with the same abbrev offset have the same
7850 stmt_list value too so in practice this should work well.
7851
7852 The basic algorithm here is:
7853
7854 sort TUs by abbrev table
7855 for each TU with same abbrev table:
7856 read abbrev table if first user
7857 read TU top level DIE
7858 [IWBN if DWO skeletons had DW_AT_stmt_list]
7859 call FUNC */
7860
6f738b01 7861 dwarf_read_debug_printf ("Building type unit groups ...");
73051182
DE
7862
7863 /* Sort in a separate table to maintain the order of all_type_units
7864 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7865 std::vector<tu_abbrev_offset> sorted_by_abbrev;
976ca316 7866 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7867
976ca316 7868 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
b2bdb8cf 7869 sorted_by_abbrev.emplace_back
976ca316 7870 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
b2bdb8cf 7871 sig_type->per_cu.sect_off));
73051182 7872
484cf504
TT
7873 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7874 sort_tu_by_abbrev_offset);
73051182 7875
9c541725 7876 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7877
b2bdb8cf 7878 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7879 {
73051182
DE
7880 /* Switch to the next abbrev table if necessary. */
7881 if (abbrev_table == NULL
b2bdb8cf 7882 || tu.abbrev_offset != abbrev_offset)
73051182 7883 {
b2bdb8cf 7884 abbrev_offset = tu.abbrev_offset;
606decb2 7885 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
73051182 7886 abbrev_table =
606decb2 7887 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7888 ++tu_stats->nr_uniq_abbrev_tables;
7889 }
7890
976ca316 7891 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
2e671100 7892 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7893 if (!reader.dummy_p)
7894 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7895 reader.comp_unit_die);
73051182 7896 }
6aa5f3a6 7897}
73051182 7898
6aa5f3a6
DE
7899/* Print collected type unit statistics. */
7900
7901static void
976ca316 7902print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7903{
976ca316 7904 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6 7905
6f738b01
SM
7906 dwarf_read_debug_printf ("Type unit statistics:");
7907 dwarf_read_debug_printf (" %zu TUs",
7908 per_objfile->per_bfd->all_type_units.size ());
7909 dwarf_read_debug_printf (" %d uniq abbrev tables",
7910 tu_stats->nr_uniq_abbrev_tables);
7911 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7912 tu_stats->nr_symtabs);
7913 dwarf_read_debug_printf (" %d symtab sharers",
7914 tu_stats->nr_symtab_sharers);
7915 dwarf_read_debug_printf (" %d type units without a stmt_list",
7916 tu_stats->nr_stmt_less_type_units);
7917 dwarf_read_debug_printf (" %d all_type_units reallocs",
7918 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7919}
7920
f4dc4d17
DE
7921/* Traversal function for build_type_psymtabs. */
7922
7923static int
7924build_type_psymtab_dependencies (void **slot, void *info)
7925{
976ca316
SM
7926 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7927 struct objfile *objfile = per_objfile->objfile;
f4dc4d17 7928 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7929 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7930 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7931 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7932 int i;
7933
7934 gdb_assert (len > 0);
197400e8 7935 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7936
7937 pst->number_of_dependencies = len;
a9342b62 7938 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7939 for (i = 0; i < len; ++i)
f4dc4d17 7940 {
df07e2c7 7941 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7942 gdb_assert (iter->per_cu.is_debug_types);
7943 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7944 iter->type_unit_group = tu_group;
f4dc4d17
DE
7945 }
7946
df07e2c7
AB
7947 delete tu_group->tus;
7948 tu_group->tus = nullptr;
348e048f
DE
7949
7950 return 1;
7951}
7952
7953/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7954 Build partial symbol tables for the .debug_types comp-units. */
7955
7956static void
976ca316 7957build_type_psymtabs (dwarf2_per_objfile *per_objfile)
348e048f 7958{
976ca316 7959 if (! create_all_type_units (per_objfile))
348e048f
DE
7960 return;
7961
976ca316 7962 build_type_psymtabs_1 (per_objfile);
6aa5f3a6 7963}
f4dc4d17 7964
6aa5f3a6
DE
7965/* Traversal function for process_skeletonless_type_unit.
7966 Read a TU in a DWO file and build partial symbols for it. */
7967
7968static int
7969process_skeletonless_type_unit (void **slot, void *info)
7970{
7971 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 7972 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
7973 struct signatured_type find_entry, *entry;
7974
7975 /* If this TU doesn't exist in the global table, add it and read it in. */
7976
976ca316
SM
7977 if (per_objfile->per_bfd->signatured_types == NULL)
7978 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7979
7980 find_entry.signature = dwo_unit->signature;
976ca316 7981 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7982 &find_entry, INSERT);
6aa5f3a6
DE
7983 /* If we've already seen this type there's nothing to do. What's happening
7984 is we're doing our own version of comdat-folding here. */
7985 if (*slot != NULL)
7986 return 1;
7987
7988 /* This does the job that create_all_type_units would have done for
7989 this TU. */
976ca316
SM
7990 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7991 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7992 *slot = entry;
7993
7994 /* This does the job that build_type_psymtabs_1 would have done. */
976ca316 7995 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
7996 if (!reader.dummy_p)
7997 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7998 reader.comp_unit_die);
6aa5f3a6
DE
7999
8000 return 1;
8001}
8002
8003/* Traversal function for process_skeletonless_type_units. */
8004
8005static int
8006process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8007{
8008 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8009
8010 if (dwo_file->tus != NULL)
b0b6a987
TT
8011 htab_traverse_noresize (dwo_file->tus.get (),
8012 process_skeletonless_type_unit, info);
6aa5f3a6
DE
8013
8014 return 1;
8015}
8016
8017/* Scan all TUs of DWO files, verifying we've processed them.
8018 This is needed in case a TU was emitted without its skeleton.
8019 Note: This can't be done until we know what all the DWO files are. */
8020
8021static void
976ca316 8022process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
8023{
8024 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
8025 if (get_dwp_file (per_objfile) == NULL
8026 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 8027 {
976ca316 8028 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 8029 process_dwo_file_for_skeletonless_type_units,
976ca316 8030 per_objfile);
6aa5f3a6 8031 }
348e048f
DE
8032}
8033
ed2dc618 8034/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8035
8036static void
976ca316 8037set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 8038{
976ca316 8039 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 8040 {
891813be 8041 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 8042
36586728
TT
8043 if (pst == NULL)
8044 continue;
8045
b76e467d 8046 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8047 {
8048 /* Set the 'user' field only if it is not already set. */
8049 if (pst->dependencies[j]->user == NULL)
8050 pst->dependencies[j]->user = pst;
8051 }
8052 }
8053}
8054
93311388
DE
8055/* Build the partial symbol table by doing a quick pass through the
8056 .debug_info and .debug_abbrev sections. */
72bf9492 8057
93311388 8058static void
976ca316 8059dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 8060{
976ca316 8061 struct objfile *objfile = per_objfile->objfile;
93311388 8062
6f738b01
SM
8063 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8064 objfile_name (objfile));
45cfd468 8065
76935768 8066 scoped_restore restore_reading_psyms
976ca316 8067 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
76935768 8068 true);
98bfdba5 8069
976ca316 8070 per_objfile->per_bfd->info.read (objfile);
91c24f0a 8071
93311388
DE
8072 /* Any cached compilation units will be linked by the per-objfile
8073 read_in_chain. Make sure to free them when we're done. */
976ca316 8074 free_cached_comp_units freer (per_objfile);
72bf9492 8075
976ca316 8076 build_type_psymtabs (per_objfile);
348e048f 8077
976ca316 8078 create_all_comp_units (per_objfile);
c906108c 8079
60606b2c
TT
8080 /* Create a temporary address map on a temporary obstack. We later
8081 copy this to the final obstack. */
8268c778 8082 auto_obstack temp_obstack;
791afaa2
TT
8083
8084 scoped_restore save_psymtabs_addrmap
d320c2b5 8085 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8086 addrmap_create_mutable (&temp_obstack));
72bf9492 8087
976ca316 8088 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
8089 {
8090 if (per_cu->v.psymtab != NULL)
8091 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8092 continue;
976ca316 8093 process_psymtab_comp_unit (per_cu, per_objfile, false,
ab432490 8094 language_minimal);
3d5afab3 8095 }
ff013f42 8096
6aa5f3a6 8097 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 8098 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
8099
8100 /* Now that all TUs have been processed we can fill in the dependencies. */
976ca316 8101 if (per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 8102 {
976ca316
SM
8103 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8104 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
8105 }
8106
6f738b01 8107 if (dwarf_read_debug > 0)
976ca316 8108 print_tu_stats (per_objfile);
6aa5f3a6 8109
976ca316 8110 set_partial_user (per_objfile);
95554aad 8111
d320c2b5
TT
8112 objfile->partial_symtabs->psymtabs_addrmap
8113 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8114 objfile->partial_symtabs->obstack ());
791afaa2
TT
8115 /* At this point we want to keep the address map. */
8116 save_psymtabs_addrmap.release ();
ff013f42 8117
6f738b01
SM
8118 dwarf_read_debug_printf ("Done building psymtabs of %s",
8119 objfile_name (objfile));
ae038cb0
DJ
8120}
8121
dee91e82
DE
8122/* Load the partial DIEs for a secondary CU into memory.
8123 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8124
dee91e82 8125static void
ab432490 8126load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
8127 dwarf2_per_objfile *per_objfile,
8128 dwarf2_cu *existing_cu)
dee91e82 8129{
2e671100 8130 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
8131
8132 if (!reader.dummy_p)
8133 {
8134 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8135 language_minimal);
8136
8137 /* Check if comp unit has_children.
8138 If so, read the rest of the partial symbols from this comp unit.
8139 If not, there's no more debug_info for this comp unit. */
3e225074 8140 if (reader.comp_unit_die->has_children)
c0ab21c2 8141 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8142
8143 reader.keep ();
c0ab21c2 8144 }
ae038cb0
DJ
8145}
8146
ae038cb0 8147static void
976ca316 8148read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 8149 struct dwarf2_section_info *section,
f1902523 8150 struct dwarf2_section_info *abbrev_section,
b76e467d 8151 unsigned int is_dwz)
ae038cb0 8152{
d521ce57 8153 const gdb_byte *info_ptr;
976ca316 8154 struct objfile *objfile = per_objfile->objfile;
be391dca 8155
6f738b01
SM
8156 dwarf_read_debug_printf ("Reading %s for %s",
8157 section->get_name (),
8158 section->get_file_name ());
bf6af496 8159
96b79293 8160 section->read (objfile);
ae038cb0 8161
36586728 8162 info_ptr = section->buffer;
6e70227d 8163
36586728 8164 while (info_ptr < section->buffer + section->size)
ae038cb0 8165 {
ae038cb0 8166 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8167
9c541725 8168 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8169
f1902523 8170 comp_unit_head cu_header;
976ca316 8171 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618
SM
8172 abbrev_section, info_ptr,
8173 rcuh_kind::COMPILE);
ae038cb0
DJ
8174
8175 /* Save the compilation unit for later lookup. */
f1902523 8176 if (cu_header.unit_type != DW_UT_type)
976ca316 8177 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8178 else
8179 {
976ca316 8180 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8181 sig_type->signature = cu_header.signature;
8182 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8183 this_cu = &sig_type->per_cu;
8184 }
8185 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8186 this_cu->sect_off = sect_off;
f1902523 8187 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8188 this_cu->is_dwz = is_dwz;
8a0459fd 8189 this_cu->section = section;
ae038cb0 8190
976ca316 8191 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8192
8193 info_ptr = info_ptr + this_cu->length;
8194 }
36586728
TT
8195}
8196
8197/* Create a list of all compilation units in OBJFILE.
8198 This is only done for -readnow and building partial symtabs. */
8199
8200static void
976ca316 8201create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 8202{
976ca316
SM
8203 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8204 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8205 &per_objfile->per_bfd->abbrev, 0);
36586728 8206
976ca316 8207 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 8208 if (dwz != NULL)
976ca316 8209 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
c906108c
SS
8210}
8211
5734ee8b 8212/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8213 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8214 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8215 DW_AT_ranges). See the comments of add_partial_subprogram on how
8216 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8217
72bf9492
DJ
8218static void
8219scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8220 CORE_ADDR *highpc, int set_addrmap,
8221 struct dwarf2_cu *cu)
c906108c 8222{
72bf9492 8223 struct partial_die_info *pdi;
c906108c 8224
91c24f0a
DC
8225 /* Now, march along the PDI's, descending into ones which have
8226 interesting children but skipping the children of the other ones,
8227 until we reach the end of the compilation unit. */
c906108c 8228
72bf9492 8229 pdi = first_die;
91c24f0a 8230
72bf9492
DJ
8231 while (pdi != NULL)
8232 {
52356b79 8233 pdi->fixup (cu);
c906108c 8234
f55ee35c 8235 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8236 children, so we need to look at them. Ditto for anonymous
8237 enums. */
933c6fe4 8238
7d00ffec 8239 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8240 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8241 || pdi->tag == DW_TAG_imported_unit
8242 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8243 {
72bf9492 8244 switch (pdi->tag)
c906108c
SS
8245 {
8246 case DW_TAG_subprogram:
b1dc1806 8247 case DW_TAG_inlined_subroutine:
cdc07690 8248 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
8249 if (cu->language == language_cplus)
8250 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8251 set_addrmap, cu);
c906108c 8252 break;
72929c62 8253 case DW_TAG_constant:
c906108c
SS
8254 case DW_TAG_variable:
8255 case DW_TAG_typedef:
91c24f0a 8256 case DW_TAG_union_type:
317d2668
TV
8257 if (!pdi->is_declaration
8258 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8259 {
72bf9492 8260 add_partial_symbol (pdi, cu);
63d06c5c
DC
8261 }
8262 break;
c906108c 8263 case DW_TAG_class_type:
680b30c7 8264 case DW_TAG_interface_type:
c906108c 8265 case DW_TAG_structure_type:
72bf9492 8266 if (!pdi->is_declaration)
c906108c 8267 {
72bf9492 8268 add_partial_symbol (pdi, cu);
c906108c 8269 }
b7fee5a3
KS
8270 if ((cu->language == language_rust
8271 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8272 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8273 set_addrmap, cu);
c906108c 8274 break;
91c24f0a 8275 case DW_TAG_enumeration_type:
72bf9492
DJ
8276 if (!pdi->is_declaration)
8277 add_partial_enumeration (pdi, cu);
c906108c
SS
8278 break;
8279 case DW_TAG_base_type:
dda83cd7 8280 case DW_TAG_subrange_type:
c906108c 8281 /* File scope base type definitions are added to the partial
dda83cd7 8282 symbol table. */
72bf9492 8283 add_partial_symbol (pdi, cu);
c906108c 8284 break;
d9fa45fe 8285 case DW_TAG_namespace:
cdc07690 8286 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8287 break;
5d7cb8df 8288 case DW_TAG_module:
59c35742
AB
8289 if (!pdi->is_declaration)
8290 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8291 break;
95554aad
TT
8292 case DW_TAG_imported_unit:
8293 {
8294 struct dwarf2_per_cu_data *per_cu;
8295
f4dc4d17
DE
8296 /* For now we don't handle imported units in type units. */
8297 if (cu->per_cu->is_debug_types)
8298 {
8299 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8300 " supported in type units [in module %s]"),
5e22e966 8301 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8302 }
8303
e3b94546 8304 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8305 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8306
8307 /* Go read the partial unit, if needed. */
8308 if (per_cu->v.psymtab == NULL)
ab432490
SM
8309 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8310 cu->language);
95554aad 8311
ae640021 8312 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8313 }
8314 break;
74921315
KS
8315 case DW_TAG_imported_declaration:
8316 add_partial_symbol (pdi, cu);
8317 break;
c906108c
SS
8318 default:
8319 break;
8320 }
8321 }
8322
72bf9492
DJ
8323 /* If the die has a sibling, skip to the sibling. */
8324
8325 pdi = pdi->die_sibling;
8326 }
8327}
8328
8329/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8330
72bf9492 8331 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8332 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8333 Enumerators are an exception; they use the scope of their parent
8334 enumeration type, i.e. the name of the enumeration type is not
8335 prepended to the enumerator.
91c24f0a 8336
72bf9492
DJ
8337 There are two complexities. One is DW_AT_specification; in this
8338 case "parent" means the parent of the target of the specification,
8339 instead of the direct parent of the DIE. The other is compilers
8340 which do not emit DW_TAG_namespace; in this case we try to guess
8341 the fully qualified name of structure types from their members'
8342 linkage names. This must be done using the DIE's children rather
8343 than the children of any DW_AT_specification target. We only need
8344 to do this for structures at the top level, i.e. if the target of
8345 any DW_AT_specification (if any; otherwise the DIE itself) does not
8346 have a parent. */
8347
8348/* Compute the scope prefix associated with PDI's parent, in
8349 compilation unit CU. The result will be allocated on CU's
8350 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8351 field. NULL is returned if no prefix is necessary. */
15d034d0 8352static const char *
72bf9492
DJ
8353partial_die_parent_scope (struct partial_die_info *pdi,
8354 struct dwarf2_cu *cu)
8355{
15d034d0 8356 const char *grandparent_scope;
72bf9492 8357 struct partial_die_info *parent, *real_pdi;
91c24f0a 8358
72bf9492
DJ
8359 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8360 then this means the parent of the specification DIE. */
8361
8362 real_pdi = pdi;
72bf9492 8363 while (real_pdi->has_specification)
fb816e8b 8364 {
122cf0f2
AB
8365 auto res = find_partial_die (real_pdi->spec_offset,
8366 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8367 real_pdi = res.pdi;
8368 cu = res.cu;
8369 }
72bf9492
DJ
8370
8371 parent = real_pdi->die_parent;
8372 if (parent == NULL)
8373 return NULL;
8374
8375 if (parent->scope_set)
8376 return parent->scope;
8377
52356b79 8378 parent->fixup (cu);
72bf9492 8379
10b3939b 8380 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8381
acebe513
UW
8382 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8383 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8384 Work around this problem here. */
8385 if (cu->language == language_cplus
6e70227d 8386 && parent->tag == DW_TAG_namespace
7d00ffec 8387 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
8388 && grandparent_scope == NULL)
8389 {
8390 parent->scope = NULL;
8391 parent->scope_set = 1;
8392 return NULL;
8393 }
8394
0a4b0913 8395 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8396 if (pdi->tag == DW_TAG_enumerator)
8397 /* Enumerators should not get the name of the enumeration as a prefix. */
8398 parent->scope = grandparent_scope;
8399 else if (parent->tag == DW_TAG_namespace
f55ee35c 8400 || parent->tag == DW_TAG_module
72bf9492
DJ
8401 || parent->tag == DW_TAG_structure_type
8402 || parent->tag == DW_TAG_class_type
680b30c7 8403 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8404 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8405 || parent->tag == DW_TAG_enumeration_type
8406 || (cu->language == language_fortran
8407 && parent->tag == DW_TAG_subprogram
8408 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8409 {
8410 if (grandparent_scope == NULL)
7d00ffec 8411 parent->scope = parent->name (cu);
72bf9492 8412 else
3e43a32a
MS
8413 parent->scope = typename_concat (&cu->comp_unit_obstack,
8414 grandparent_scope,
7d00ffec 8415 parent->name (cu), 0, cu);
72bf9492 8416 }
72bf9492
DJ
8417 else
8418 {
8419 /* FIXME drow/2004-04-01: What should we be doing with
8420 function-local names? For partial symbols, we should probably be
8421 ignoring them. */
fa9c3fa0
TT
8422 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8423 dwarf_tag_name (parent->tag),
8424 sect_offset_str (pdi->sect_off));
72bf9492 8425 parent->scope = grandparent_scope;
c906108c
SS
8426 }
8427
72bf9492
DJ
8428 parent->scope_set = 1;
8429 return parent->scope;
8430}
8431
8432/* Return the fully scoped name associated with PDI, from compilation unit
8433 CU. The result will be allocated with malloc. */
4568ecf9 8434
43816ebc 8435static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8436partial_die_full_name (struct partial_die_info *pdi,
8437 struct dwarf2_cu *cu)
8438{
15d034d0 8439 const char *parent_scope;
72bf9492 8440
98bfdba5
PA
8441 /* If this is a template instantiation, we can not work out the
8442 template arguments from partial DIEs. So, unfortunately, we have
8443 to go through the full DIEs. At least any work we do building
8444 types here will be reused if full symbols are loaded later. */
8445 if (pdi->has_template_arguments)
8446 {
52356b79 8447 pdi->fixup (cu);
98bfdba5 8448
7d00ffec 8449 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8450 {
8451 struct die_info *die;
8452 struct attribute attr;
8453 struct dwarf2_cu *ref_cu = cu;
8454
b64f50a1 8455 /* DW_FORM_ref_addr is using section offset. */
b4069958 8456 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8457 attr.form = DW_FORM_ref_addr;
9c541725 8458 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8459 die = follow_die_ref (NULL, &attr, &ref_cu);
8460
43816ebc 8461 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8462 }
8463 }
8464
72bf9492
DJ
8465 parent_scope = partial_die_parent_scope (pdi, cu);
8466 if (parent_scope == NULL)
8467 return NULL;
8468 else
43816ebc 8469 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8470 pdi->name (cu),
8471 0, cu));
c906108c
SS
8472}
8473
8474static void
72bf9492 8475add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8476{
976ca316
SM
8477 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8478 struct objfile *objfile = per_objfile->objfile;
08feed99 8479 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8480 CORE_ADDR addr = 0;
15d034d0 8481 const char *actual_name = NULL;
e142c38c
DJ
8482 CORE_ADDR baseaddr;
8483
b3b3bada 8484 baseaddr = objfile->text_section_offset ();
c906108c 8485
43816ebc
TT
8486 gdb::unique_xmalloc_ptr<char> built_actual_name
8487 = partial_die_full_name (pdi, cu);
15d034d0 8488 if (built_actual_name != NULL)
43816ebc 8489 actual_name = built_actual_name.get ();
63d06c5c 8490
72bf9492 8491 if (actual_name == NULL)
7d00ffec 8492 actual_name = pdi->name (cu);
72bf9492 8493
76e288d1
TT
8494 partial_symbol psymbol;
8495 memset (&psymbol, 0, sizeof (psymbol));
8496 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
a52d653e 8497 psymbol.ginfo.set_section_index (-1);
76e288d1
TT
8498
8499 /* The code below indicates that the psymbol should be installed by
8500 setting this. */
8501 gdb::optional<psymbol_placement> where;
8502
c906108c
SS
8503 switch (pdi->tag)
8504 {
b1dc1806 8505 case DW_TAG_inlined_subroutine:
c906108c 8506 case DW_TAG_subprogram:
79748972
TT
8507 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8508 - baseaddr);
0a4b0913
AB
8509 if (pdi->is_external
8510 || cu->language == language_ada
8511 || (cu->language == language_fortran
8512 && pdi->die_parent != NULL
8513 && pdi->die_parent->tag == DW_TAG_subprogram))
8514 {
dda83cd7
SM
8515 /* Normally, only "external" DIEs are part of the global scope.
8516 But in Ada and Fortran, we want to be able to access nested
8517 procedures globally. So all Ada and Fortran subprograms are
8518 stored in the global scope. */
76e288d1 8519 where = psymbol_placement::GLOBAL;
c906108c
SS
8520 }
8521 else
76e288d1
TT
8522 where = psymbol_placement::STATIC;
8523
8524 psymbol.domain = VAR_DOMAIN;
8525 psymbol.aclass = LOC_BLOCK;
a52d653e 8526 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1 8527 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8528
8529 if (pdi->main_subprogram && actual_name != NULL)
8530 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8531 break;
72929c62 8532 case DW_TAG_constant:
76e288d1
TT
8533 psymbol.domain = VAR_DOMAIN;
8534 psymbol.aclass = LOC_STATIC;
8535 where = (pdi->is_external
8536 ? psymbol_placement::GLOBAL
8537 : psymbol_placement::STATIC);
72929c62 8538 break;
c906108c 8539 case DW_TAG_variable:
95554aad
TT
8540 if (pdi->d.locdesc)
8541 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8542
95554aad 8543 if (pdi->d.locdesc
caac4577 8544 && addr == 0
976ca316 8545 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8546 {
8547 /* A global or static variable may also have been stripped
8548 out by the linker if unused, in which case its address
8549 will be nullified; do not add such variables into partial
8550 symbol table then. */
8551 }
8552 else if (pdi->is_external)
c906108c
SS
8553 {
8554 /* Global Variable.
8555 Don't enter into the minimal symbol tables as there is
8556 a minimal symbol table entry from the ELF symbols already.
8557 Enter into partial symbol table if it has a location
8558 descriptor or a type.
8559 If the location descriptor is missing, new_symbol will create
8560 a LOC_UNRESOLVED symbol, the address of the variable will then
8561 be determined from the minimal symbol table whenever the variable
8562 is referenced.
8563 The address for the partial symbol table entry is not
8564 used by GDB, but it comes in handy for debugging partial symbol
8565 table building. */
8566
95554aad 8567 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8568 {
8569 psymbol.domain = VAR_DOMAIN;
8570 psymbol.aclass = LOC_STATIC;
a52d653e 8571 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8572 psymbol.ginfo.value.address = addr;
8573 where = psymbol_placement::GLOBAL;
8574 }
c906108c
SS
8575 }
8576 else
8577 {
ff908ebf
AW
8578 int has_loc = pdi->d.locdesc != NULL;
8579
8580 /* Static Variable. Skip symbols whose value we cannot know (those
8581 without location descriptors or constant values). */
8582 if (!has_loc && !pdi->has_const_value)
43816ebc 8583 return;
ff908ebf 8584
76e288d1
TT
8585 psymbol.domain = VAR_DOMAIN;
8586 psymbol.aclass = LOC_STATIC;
a52d653e 8587 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8588 if (has_loc)
8589 psymbol.ginfo.value.address = addr;
8590 where = psymbol_placement::STATIC;
c906108c
SS
8591 }
8592 break;
d8f62e84 8593 case DW_TAG_array_type:
c906108c
SS
8594 case DW_TAG_typedef:
8595 case DW_TAG_base_type:
a02abb62 8596 case DW_TAG_subrange_type:
76e288d1
TT
8597 psymbol.domain = VAR_DOMAIN;
8598 psymbol.aclass = LOC_TYPEDEF;
8599 where = psymbol_placement::STATIC;
c906108c 8600 break;
74921315 8601 case DW_TAG_imported_declaration:
72bf9492 8602 case DW_TAG_namespace:
76e288d1
TT
8603 psymbol.domain = VAR_DOMAIN;
8604 psymbol.aclass = LOC_TYPEDEF;
8605 where = psymbol_placement::GLOBAL;
72bf9492 8606 break;
530e8392 8607 case DW_TAG_module:
a5fd13a9 8608 /* With Fortran 77 there might be a "BLOCK DATA" module
dda83cd7
SM
8609 available without any name. If so, we skip the module as it
8610 doesn't bring any value. */
a5fd13a9 8611 if (actual_name != nullptr)
76e288d1
TT
8612 {
8613 psymbol.domain = MODULE_DOMAIN;
8614 psymbol.aclass = LOC_TYPEDEF;
8615 where = psymbol_placement::GLOBAL;
8616 }
530e8392 8617 break;
c906108c 8618 case DW_TAG_class_type:
680b30c7 8619 case DW_TAG_interface_type:
c906108c
SS
8620 case DW_TAG_structure_type:
8621 case DW_TAG_union_type:
8622 case DW_TAG_enumeration_type:
fa4028e9 8623 /* Skip external references. The DWARF standard says in the section
dda83cd7
SM
8624 about "Structure, Union, and Class Type Entries": "An incomplete
8625 structure, union or class type is represented by a structure,
8626 union or class entry that does not have a byte size attribute
8627 and that has a DW_AT_declaration attribute." */
fa4028e9 8628 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8629 return;
fa4028e9 8630
63d06c5c
DC
8631 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8632 static vs. global. */
76e288d1
TT
8633 psymbol.domain = STRUCT_DOMAIN;
8634 psymbol.aclass = LOC_TYPEDEF;
8635 where = (cu->language == language_cplus
8636 ? psymbol_placement::GLOBAL
8637 : psymbol_placement::STATIC);
c906108c
SS
8638 break;
8639 case DW_TAG_enumerator:
76e288d1
TT
8640 psymbol.domain = VAR_DOMAIN;
8641 psymbol.aclass = LOC_CONST;
8642 where = (cu->language == language_cplus
8643 ? psymbol_placement::GLOBAL
8644 : psymbol_placement::STATIC);
c906108c
SS
8645 break;
8646 default:
8647 break;
8648 }
76e288d1
TT
8649
8650 if (where.has_value ())
8651 {
f049a313
TT
8652 if (built_actual_name != nullptr)
8653 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8654 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8655 psymbol.ginfo.set_linkage_name (actual_name);
8656 else
8657 {
8658 psymbol.ginfo.set_demangled_name (actual_name,
8659 &objfile->objfile_obstack);
8660 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8661 }
932539d7 8662 cu->per_cu->v.psymtab->add_psymbol (psymbol, *where, objfile);
76e288d1 8663 }
c906108c
SS
8664}
8665
5c4e30ca
DC
8666/* Read a partial die corresponding to a namespace; also, add a symbol
8667 corresponding to that namespace to the symbol table. NAMESPACE is
8668 the name of the enclosing namespace. */
91c24f0a 8669
72bf9492
DJ
8670static void
8671add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8672 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8673 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8674{
72bf9492 8675 /* Add a symbol for the namespace. */
e7c27a73 8676
72bf9492 8677 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8678
8679 /* Now scan partial symbols in that namespace. */
8680
91c24f0a 8681 if (pdi->has_children)
cdc07690 8682 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8683}
8684
5d7cb8df
JK
8685/* Read a partial die corresponding to a Fortran module. */
8686
8687static void
8688add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8689 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8690{
530e8392
KB
8691 /* Add a symbol for the namespace. */
8692
8693 add_partial_symbol (pdi, cu);
8694
f55ee35c 8695 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8696
8697 if (pdi->has_children)
cdc07690 8698 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8699}
8700
b1dc1806
XR
8701/* Read a partial die corresponding to a subprogram or an inlined
8702 subprogram and create a partial symbol for that subprogram.
8703 When the CU language allows it, this routine also defines a partial
8704 symbol for each nested subprogram that this subprogram contains.
8705 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8706 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8707
cdc07690
YQ
8708 PDI may also be a lexical block, in which case we simply search
8709 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8710 Again, this is only performed when the CU language allows this
8711 type of definitions. */
8712
8713static void
8714add_partial_subprogram (struct partial_die_info *pdi,
8715 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8716 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8717{
b1dc1806 8718 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8719 {
8720 if (pdi->has_pc_info)
dda83cd7
SM
8721 {
8722 if (pdi->lowpc < *lowpc)
8723 *lowpc = pdi->lowpc;
8724 if (pdi->highpc > *highpc)
8725 *highpc = pdi->highpc;
cdc07690 8726 if (set_addrmap)
5734ee8b 8727 {
5e22e966 8728 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8729 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8730 CORE_ADDR baseaddr;
b926417a
TT
8731 CORE_ADDR this_highpc;
8732 CORE_ADDR this_lowpc;
5734ee8b 8733
b3b3bada 8734 baseaddr = objfile->text_section_offset ();
b926417a
TT
8735 this_lowpc
8736 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8737 pdi->lowpc + baseaddr)
8738 - baseaddr);
8739 this_highpc
8740 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8741 pdi->highpc + baseaddr)
8742 - baseaddr);
d320c2b5 8743 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8744 this_lowpc, this_highpc - 1,
9291a0cd 8745 cu->per_cu->v.psymtab);
5734ee8b 8746 }
dda83cd7 8747 }
481860b3
GB
8748
8749 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8750 {
dda83cd7 8751 if (!pdi->is_declaration)
e8d05480
JB
8752 /* Ignore subprogram DIEs that do not have a name, they are
8753 illegal. Do not emit a complaint at this point, we will
8754 do so when we convert this psymtab into a symtab. */
7d00ffec 8755 if (pdi->name (cu))
e8d05480 8756 add_partial_symbol (pdi, cu);
dda83cd7 8757 }
bc30ff58 8758 }
6e70227d 8759
bc30ff58
JB
8760 if (! pdi->has_children)
8761 return;
8762
0a4b0913 8763 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8764 {
8765 pdi = pdi->die_child;
8766 while (pdi != NULL)
8767 {
52356b79 8768 pdi->fixup (cu);
bc30ff58 8769 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8770 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8771 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8772 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8773 pdi = pdi->die_sibling;
8774 }
8775 }
8776}
8777
91c24f0a
DC
8778/* Read a partial die corresponding to an enumeration type. */
8779
72bf9492
DJ
8780static void
8781add_partial_enumeration (struct partial_die_info *enum_pdi,
8782 struct dwarf2_cu *cu)
91c24f0a 8783{
72bf9492 8784 struct partial_die_info *pdi;
91c24f0a 8785
7d00ffec 8786 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8787 add_partial_symbol (enum_pdi, cu);
8788
8789 pdi = enum_pdi->die_child;
8790 while (pdi)
91c24f0a 8791 {
7d00ffec 8792 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8793 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8794 else
72bf9492
DJ
8795 add_partial_symbol (pdi, cu);
8796 pdi = pdi->die_sibling;
91c24f0a 8797 }
91c24f0a
DC
8798}
8799
6caca83c
CC
8800/* Return the initial uleb128 in the die at INFO_PTR. */
8801
8802static unsigned int
d521ce57 8803peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8804{
8805 unsigned int bytes_read;
8806
8807 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8808}
8809
685af9cd
TT
8810/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8811 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8812
4bb7a0a7
DJ
8813 Return the corresponding abbrev, or NULL if the number is zero (indicating
8814 an empty DIE). In either case *BYTES_READ will be set to the length of
8815 the initial number. */
8816
7c32eebb 8817static const struct abbrev_info *
685af9cd
TT
8818peek_die_abbrev (const die_reader_specs &reader,
8819 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8820{
685af9cd 8821 dwarf2_cu *cu = reader.cu;
0280fdcc 8822 bfd *abfd = reader.abfd;
685af9cd
TT
8823 unsigned int abbrev_number
8824 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8825
8826 if (abbrev_number == 0)
8827 return NULL;
8828
7c32eebb
TT
8829 const abbrev_info *abbrev
8830 = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8831 if (!abbrev)
8832 {
422b9917 8833 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8834 " at offset %s [in module %s]"),
422b9917 8835 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8836 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8837 }
8838
8839 return abbrev;
8840}
8841
93311388
DE
8842/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8843 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8844 DIE. Any children of the skipped DIEs will also be skipped. */
8845
d521ce57
TT
8846static const gdb_byte *
8847skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8848{
4bb7a0a7
DJ
8849 while (1)
8850 {
685af9cd 8851 unsigned int bytes_read;
7c32eebb
TT
8852 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8853 &bytes_read);
685af9cd 8854
4bb7a0a7
DJ
8855 if (abbrev == NULL)
8856 return info_ptr + bytes_read;
8857 else
dee91e82 8858 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8859 }
8860}
8861
93311388
DE
8862/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8863 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8864 abbrev corresponding to that skipped uleb128 should be passed in
8865 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8866 children. */
8867
d521ce57
TT
8868static const gdb_byte *
8869skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7c32eebb 8870 const struct abbrev_info *abbrev)
4bb7a0a7
DJ
8871{
8872 unsigned int bytes_read;
8873 struct attribute attr;
dee91e82
DE
8874 bfd *abfd = reader->abfd;
8875 struct dwarf2_cu *cu = reader->cu;
d521ce57 8876 const gdb_byte *buffer = reader->buffer;
f664829e 8877 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8878 unsigned int form, i;
8879
8880 for (i = 0; i < abbrev->num_attrs; i++)
8881 {
8882 /* The only abbrev we care about is DW_AT_sibling. */
8883 if (abbrev->attrs[i].name == DW_AT_sibling)
8884 {
7a5f294d 8885 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8886 if (attr.form == DW_FORM_ref_addr)
b98664d3 8887 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8888 else
b9502d3f 8889 {
0826b30a 8890 sect_offset off = attr.get_ref_die_offset ();
9c541725 8891 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8892
8893 if (sibling_ptr < info_ptr)
b98664d3 8894 complaint (_("DW_AT_sibling points backwards"));
22869d73 8895 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8896 reader->die_section->overflow_complaint ();
b9502d3f
WN
8897 else
8898 return sibling_ptr;
8899 }
4bb7a0a7
DJ
8900 }
8901
8902 /* If it isn't DW_AT_sibling, skip this attribute. */
8903 form = abbrev->attrs[i].form;
8904 skip_attribute:
8905 switch (form)
8906 {
4bb7a0a7 8907 case DW_FORM_ref_addr:
ae411497
TT
8908 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8909 and later it is offset sized. */
8910 if (cu->header.version == 2)
8911 info_ptr += cu->header.addr_size;
8912 else
8913 info_ptr += cu->header.offset_size;
8914 break;
36586728
TT
8915 case DW_FORM_GNU_ref_alt:
8916 info_ptr += cu->header.offset_size;
8917 break;
ae411497 8918 case DW_FORM_addr:
4bb7a0a7
DJ
8919 info_ptr += cu->header.addr_size;
8920 break;
8921 case DW_FORM_data1:
8922 case DW_FORM_ref1:
8923 case DW_FORM_flag:
8fe0f950 8924 case DW_FORM_strx1:
4bb7a0a7
DJ
8925 info_ptr += 1;
8926 break;
2dc7f7b3 8927 case DW_FORM_flag_present:
43988095 8928 case DW_FORM_implicit_const:
2dc7f7b3 8929 break;
4bb7a0a7
DJ
8930 case DW_FORM_data2:
8931 case DW_FORM_ref2:
8fe0f950 8932 case DW_FORM_strx2:
4bb7a0a7
DJ
8933 info_ptr += 2;
8934 break;
8fe0f950
AT
8935 case DW_FORM_strx3:
8936 info_ptr += 3;
8937 break;
4bb7a0a7
DJ
8938 case DW_FORM_data4:
8939 case DW_FORM_ref4:
8fe0f950 8940 case DW_FORM_strx4:
4bb7a0a7
DJ
8941 info_ptr += 4;
8942 break;
8943 case DW_FORM_data8:
8944 case DW_FORM_ref8:
55f1336d 8945 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8946 info_ptr += 8;
8947 break;
0224619f
JK
8948 case DW_FORM_data16:
8949 info_ptr += 16;
8950 break;
4bb7a0a7 8951 case DW_FORM_string:
9b1c24c8 8952 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8953 info_ptr += bytes_read;
8954 break;
2dc7f7b3 8955 case DW_FORM_sec_offset:
4bb7a0a7 8956 case DW_FORM_strp:
36586728 8957 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8958 info_ptr += cu->header.offset_size;
8959 break;
2dc7f7b3 8960 case DW_FORM_exprloc:
4bb7a0a7
DJ
8961 case DW_FORM_block:
8962 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8963 info_ptr += bytes_read;
8964 break;
8965 case DW_FORM_block1:
8966 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8967 break;
8968 case DW_FORM_block2:
8969 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8970 break;
8971 case DW_FORM_block4:
8972 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8973 break;
336d760d 8974 case DW_FORM_addrx:
cf532bd1 8975 case DW_FORM_strx:
4bb7a0a7
DJ
8976 case DW_FORM_sdata:
8977 case DW_FORM_udata:
8978 case DW_FORM_ref_udata:
3019eac3
DE
8979 case DW_FORM_GNU_addr_index:
8980 case DW_FORM_GNU_str_index:
18a8505e 8981 case DW_FORM_rnglistx:
41144253 8982 case DW_FORM_loclistx:
d521ce57 8983 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8984 break;
8985 case DW_FORM_indirect:
8986 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8987 info_ptr += bytes_read;
8988 /* We need to continue parsing from here, so just go back to
8989 the top. */
8990 goto skip_attribute;
8991
8992 default:
3e43a32a
MS
8993 error (_("Dwarf Error: Cannot handle %s "
8994 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8995 dwarf_form_name (form),
8996 bfd_get_filename (abfd));
8997 }
8998 }
8999
9000 if (abbrev->has_children)
dee91e82 9001 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9002 else
9003 return info_ptr;
9004}
9005
93311388 9006/* Locate ORIG_PDI's sibling.
dee91e82 9007 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9008
d521ce57 9009static const gdb_byte *
dee91e82
DE
9010locate_pdi_sibling (const struct die_reader_specs *reader,
9011 struct partial_die_info *orig_pdi,
d521ce57 9012 const gdb_byte *info_ptr)
91c24f0a
DC
9013{
9014 /* Do we know the sibling already? */
72bf9492 9015
91c24f0a
DC
9016 if (orig_pdi->sibling)
9017 return orig_pdi->sibling;
9018
9019 /* Are there any children to deal with? */
9020
9021 if (!orig_pdi->has_children)
9022 return info_ptr;
9023
4bb7a0a7 9024 /* Skip the children the long way. */
91c24f0a 9025
dee91e82 9026 return skip_children (reader, info_ptr);
91c24f0a
DC
9027}
9028
257e7a09 9029/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9030 not NULL. */
c906108c 9031
891813be
TT
9032void
9033dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 9034{
976ca316 9035 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 9036
976ca316 9037 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 9038
077cbab2
TT
9039 /* If this psymtab is constructed from a debug-only objfile, the
9040 has_section_at_zero flag will not necessarily be correct. We
9041 can get the correct value for this flag by looking at the data
9042 associated with the (presumably stripped) associated objfile. */
9043 if (objfile->separate_debug_objfile_backlink)
c906108c 9044 {
976ca316 9045 dwarf2_per_objfile *per_objfile_backlink
077cbab2 9046 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 9047
976ca316
SM
9048 per_objfile->per_bfd->has_section_at_zero
9049 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 9050 }
98bfdba5 9051
8566b89b 9052 expand_psymtab (objfile);
95554aad 9053
976ca316 9054 process_cu_includes (per_objfile);
c906108c 9055}
9cdd5dbd
DE
9056\f
9057/* Reading in full CUs. */
c906108c 9058
10b3939b
DJ
9059/* Add PER_CU to the queue. */
9060
9061static void
120ce1b5
SM
9062queue_comp_unit (dwarf2_per_cu_data *per_cu,
9063 dwarf2_per_objfile *per_objfile,
95554aad 9064 enum language pretend_language)
10b3939b 9065{
10b3939b 9066 per_cu->queued = 1;
08ac5771
SM
9067
9068 gdb_assert (per_objfile->per_bfd->queue.has_value ());
9069 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
9070}
9071
616c069a
SM
9072/* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
9073
89e63ee4
DE
9074 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9075 dependency.
69d751e3 9076
616c069a
SM
9077 Return true if maybe_queue_comp_unit requires the caller to load the CU's
9078 DIEs, false otherwise.
9079
9080 Explanation: there is an invariant that if a CU is queued for expansion
9081 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
9082 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
9083 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
9084 are not yet loaded, the the caller must load the CU's DIEs to ensure the
9085 invariant is respected.
9086
9087 The caller is therefore not required to load the CU's DIEs (we return false)
9088 if:
9089
9090 - the CU is already expanded, and therefore does not get enqueued
9091 - the CU gets enqueued for expansion, but its DIEs are already loaded
9092
9093 Note that the caller should not use this function's return value as an
9094 indicator of whether the CU's DIEs are loaded right now, it should check
9095 that by calling `dwarf2_per_objfile::get_cu` instead. */
0907af0c
DE
9096
9097static int
89e63ee4 9098maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
9099 dwarf2_per_cu_data *per_cu,
9100 dwarf2_per_objfile *per_objfile,
0907af0c
DE
9101 enum language pretend_language)
9102{
9103 /* We may arrive here during partial symbol reading, if we need full
9104 DIEs to process an unusual case (e.g. template arguments). Do
9105 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 9106 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 9107 {
7188ed02
SM
9108 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9109
9110 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
9111 return 1;
9112 return 0;
9113 }
9114
9115 /* Mark the dependence relation so that we don't flush PER_CU
9116 too early. */
89e63ee4
DE
9117 if (dependent_cu != NULL)
9118 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9119
9120 /* If it's already on the queue, we have nothing to do. */
9121 if (per_cu->queued)
de53369b
SM
9122 {
9123 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
9124 loaded. */
9125 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
616c069a
SM
9126
9127 /* If the CU is queued for expansion, it should not already be
9128 expanded. */
9129 gdb_assert (!per_objfile->symtab_set_p (per_cu));
9130
9131 /* The DIEs are already loaded, the caller doesn't need to do it. */
de53369b
SM
9132 return 0;
9133 }
0907af0c 9134
616c069a
SM
9135 bool queued = false;
9136 if (!per_objfile->symtab_set_p (per_cu))
9137 {
9138 /* Add it to the queue. */
9139 queue_comp_unit (per_cu, per_objfile, pretend_language);
9140 queued = true;
9141 }
9142
0907af0c
DE
9143 /* If the compilation unit is already loaded, just mark it as
9144 used. */
7188ed02
SM
9145 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9146 if (cu != nullptr)
616c069a 9147 cu->last_used = 0;
0907af0c 9148
616c069a
SM
9149 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
9150 and the DIEs are not already loaded. */
9151 return queued && cu == nullptr;
0907af0c
DE
9152}
9153
10b3939b
DJ
9154/* Process the queue. */
9155
9156static void
976ca316 9157process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 9158{
6f738b01
SM
9159 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9160 objfile_name (per_objfile->objfile));
45cfd468 9161
03dd20cc
DJ
9162 /* The queue starts out with one item, but following a DIE reference
9163 may load a new CU, adding it to the end of the queue. */
08ac5771 9164 while (!per_objfile->per_bfd->queue->empty ())
10b3939b 9165 {
08ac5771 9166 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
7188ed02 9167 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 9168
976ca316 9169 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 9170 {
976ca316 9171 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 9172
7188ed02
SM
9173 /* Skip dummy CUs. */
9174 if (cu != nullptr)
73be47f5 9175 {
7188ed02
SM
9176 unsigned int debug_print_threshold;
9177 char buf[100];
9178
9179 if (per_cu->is_debug_types)
9180 {
9181 struct signatured_type *sig_type =
9182 (struct signatured_type *) per_cu;
9183
9184 sprintf (buf, "TU %s at offset %s",
9185 hex_string (sig_type->signature),
9186 sect_offset_str (per_cu->sect_off));
9187 /* There can be 100s of TUs.
9188 Only print them in verbose mode. */
9189 debug_print_threshold = 2;
9190 }
9191 else
9192 {
9193 sprintf (buf, "CU at offset %s",
9194 sect_offset_str (per_cu->sect_off));
9195 debug_print_threshold = 1;
9196 }
247f5c4f 9197
7188ed02 9198 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 9199 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
f4dc4d17 9200
7188ed02
SM
9201 if (per_cu->is_debug_types)
9202 process_full_type_unit (cu, item.pretend_language);
9203 else
9204 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 9205
7188ed02 9206 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 9207 dwarf_read_debug_printf ("Done expanding %s", buf);
7188ed02 9208 }
f4dc4d17 9209 }
10b3939b 9210
7188ed02 9211 per_cu->queued = 0;
08ac5771 9212 per_objfile->per_bfd->queue->pop ();
10b3939b
DJ
9213 }
9214
6f738b01
SM
9215 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9216 objfile_name (per_objfile->objfile));
10b3939b
DJ
9217}
9218
10b3939b
DJ
9219/* Read in full symbols for PST, and anything it depends on. */
9220
8566b89b
TT
9221void
9222dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9223{
af758d11 9224 gdb_assert (!readin_p (objfile));
95554aad 9225
17ee85fc
TT
9226 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9227 free_cached_comp_units freer (per_objfile);
48993951 9228 expand_dependencies (objfile);
aaa75496 9229
97a1449a 9230 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9231 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9232}
9233
af758d11
SM
9234/* See psympriv.h. */
9235
9236bool
9237dwarf2_psymtab::readin_p (struct objfile *objfile) const
9238{
9239 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9240 return per_objfile->symtab_set_p (per_cu_data);
9241}
9242
9243/* See psympriv.h. */
9244
9245compunit_symtab *
9246dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9247{
9248 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9249 return per_objfile->get_symtab (per_cu_data);
9250}
9251
dee91e82
DE
9252/* Trivial hash function for die_info: the hash value of a DIE
9253 is its offset in .debug_info for this objfile. */
10b3939b 9254
dee91e82
DE
9255static hashval_t
9256die_hash (const void *item)
10b3939b 9257{
9a3c8263 9258 const struct die_info *die = (const struct die_info *) item;
6502dd73 9259
9c541725 9260 return to_underlying (die->sect_off);
dee91e82 9261}
63d06c5c 9262
dee91e82
DE
9263/* Trivial comparison function for die_info structures: two DIEs
9264 are equal if they have the same offset. */
98bfdba5 9265
dee91e82
DE
9266static int
9267die_eq (const void *item_lhs, const void *item_rhs)
9268{
9a3c8263
SM
9269 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9270 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9271
9c541725 9272 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9273}
c906108c 9274
4a636814
SM
9275/* Load the DIEs associated with PER_CU into memory.
9276
9277 In some cases, the caller, while reading partial symbols, will need to load
9278 the full symbols for the CU for some reason. It will already have a
9279 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9280 rather than creating a new one. */
c906108c 9281
dee91e82 9282static void
ab432490
SM
9283load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9284 dwarf2_per_objfile *per_objfile,
4a636814 9285 dwarf2_cu *existing_cu,
c0ab21c2
TT
9286 bool skip_partial,
9287 enum language pretend_language)
dee91e82 9288{
c0ab21c2
TT
9289 gdb_assert (! this_cu->is_debug_types);
9290
7188ed02 9291 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
9292 if (reader.dummy_p)
9293 return;
9294
9295 struct dwarf2_cu *cu = reader.cu;
9296 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9297
dee91e82
DE
9298 gdb_assert (cu->die_hash == NULL);
9299 cu->die_hash =
9300 htab_create_alloc_ex (cu->header.length / 12,
9301 die_hash,
9302 die_eq,
9303 NULL,
9304 &cu->comp_unit_obstack,
9305 hashtab_obstack_allocate,
9306 dummy_obstack_deallocate);
e142c38c 9307
3e225074 9308 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9309 reader.comp_unit_die->child
9310 = read_die_and_siblings (&reader, reader.info_ptr,
9311 &info_ptr, reader.comp_unit_die);
9312 cu->dies = reader.comp_unit_die;
dee91e82 9313 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9314
9315 /* We try not to read any attributes in this function, because not
9cdd5dbd 9316 all CUs needed for references have been loaded yet, and symbol
10b3939b 9317 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9318 or we won't be able to build types correctly.
9319 Similarly, if we do not read the producer, we can not apply
9320 producer-specific interpretation. */
c0ab21c2 9321 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9322
9323 reader.keep ();
10b3939b
DJ
9324}
9325
3da10d80
KS
9326/* Add a DIE to the delayed physname list. */
9327
9328static void
9329add_to_method_list (struct type *type, int fnfield_index, int index,
9330 const char *name, struct die_info *die,
9331 struct dwarf2_cu *cu)
9332{
9333 struct delayed_method_info mi;
9334 mi.type = type;
9335 mi.fnfield_index = fnfield_index;
9336 mi.index = index;
9337 mi.name = name;
9338 mi.die = die;
c89b44cd 9339 cu->method_list.push_back (mi);
3da10d80
KS
9340}
9341
3693fdb3
PA
9342/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9343 "const" / "volatile". If so, decrements LEN by the length of the
9344 modifier and return true. Otherwise return false. */
9345
9346template<size_t N>
9347static bool
9348check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9349{
9350 size_t mod_len = sizeof (mod) - 1;
9351 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9352 {
9353 len -= mod_len;
9354 return true;
9355 }
9356 return false;
9357}
9358
3da10d80
KS
9359/* Compute the physnames of any methods on the CU's method list.
9360
9361 The computation of method physnames is delayed in order to avoid the
9362 (bad) condition that one of the method's formal parameters is of an as yet
9363 incomplete type. */
9364
9365static void
9366compute_delayed_physnames (struct dwarf2_cu *cu)
9367{
3693fdb3 9368 /* Only C++ delays computing physnames. */
c89b44cd 9369 if (cu->method_list.empty ())
3693fdb3
PA
9370 return;
9371 gdb_assert (cu->language == language_cplus);
9372
52941706 9373 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9374 {
1d06ead6 9375 const char *physname;
3da10d80 9376 struct fn_fieldlist *fn_flp
c89b44cd
TT
9377 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9378 physname = dwarf2_physname (mi.name, mi.die, cu);
9379 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9380 = physname ? physname : "";
3693fdb3
PA
9381
9382 /* Since there's no tag to indicate whether a method is a
9383 const/volatile overload, extract that information out of the
9384 demangled name. */
9385 if (physname != NULL)
9386 {
9387 size_t len = strlen (physname);
9388
9389 while (1)
9390 {
9391 if (physname[len] == ')') /* shortcut */
9392 break;
9393 else if (check_modifier (physname, len, " const"))
c89b44cd 9394 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9395 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9396 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9397 else
9398 break;
9399 }
9400 }
3da10d80 9401 }
c89b44cd
TT
9402
9403 /* The list is no longer needed. */
9404 cu->method_list.clear ();
3da10d80
KS
9405}
9406
a766d390
DE
9407/* Go objects should be embedded in a DW_TAG_module DIE,
9408 and it's not clear if/how imported objects will appear.
9409 To keep Go support simple until that's worked out,
9410 go back through what we've read and create something usable.
9411 We could do this while processing each DIE, and feels kinda cleaner,
9412 but that way is more invasive.
9413 This is to, for example, allow the user to type "p var" or "b main"
9414 without having to specify the package name, and allow lookups
9415 of module.object to work in contexts that use the expression
9416 parser. */
9417
9418static void
9419fixup_go_packaging (struct dwarf2_cu *cu)
9420{
421d1616 9421 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9422 struct pending *list;
9423 int i;
9424
c24bdb02 9425 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9426 list != NULL;
9427 list = list->next)
a766d390
DE
9428 {
9429 for (i = 0; i < list->nsyms; ++i)
9430 {
9431 struct symbol *sym = list->symbol[i];
9432
c1b5c1eb 9433 if (sym->language () == language_go
a766d390
DE
9434 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9435 {
421d1616
TT
9436 gdb::unique_xmalloc_ptr<char> this_package_name
9437 (go_symbol_package_name (sym));
a766d390
DE
9438
9439 if (this_package_name == NULL)
9440 continue;
9441 if (package_name == NULL)
421d1616 9442 package_name = std::move (this_package_name);
a766d390
DE
9443 else
9444 {
5e22e966 9445 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9446 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9447 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9448 (symbol_symtab (sym) != NULL
9449 ? symtab_to_filename_for_display
9450 (symbol_symtab (sym))
e3b94546 9451 : objfile_name (objfile)),
421d1616 9452 this_package_name.get (), package_name.get ());
a766d390
DE
9453 }
9454 }
9455 }
9456 }
9457
9458 if (package_name != NULL)
9459 {
5e22e966 9460 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9461 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9462 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9463 saved_package_name);
a766d390
DE
9464 struct symbol *sym;
9465
8c14c3a3 9466 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9467 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9468 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9469 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9470 e.g., "main" finds the "main" module and not C's main(). */
9471 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9472 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9473 SYMBOL_TYPE (sym) = type;
9474
c24bdb02 9475 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9476 }
9477}
9478
c9317f21
TT
9479/* Allocate a fully-qualified name consisting of the two parts on the
9480 obstack. */
9481
9482static const char *
9483rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9484{
9485 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9486}
9487
9c6a1327
TT
9488/* A helper that allocates a variant part to attach to a Rust enum
9489 type. OBSTACK is where the results should be allocated. TYPE is
9490 the type we're processing. DISCRIMINANT_INDEX is the index of the
57d02173
TT
9491 discriminant. It must be the index of one of the fields of TYPE,
9492 or -1 to mean there is no discriminant (univariant enum).
9c6a1327
TT
9493 DEFAULT_INDEX is the index of the default field; or -1 if there is
9494 no default. RANGES is indexed by "effective" field number (the
9495 field index, but omitting the discriminant and default fields) and
9496 must hold the discriminant values used by the variants. Note that
9497 RANGES must have a lifetime at least as long as OBSTACK -- either
9498 already allocated on it, or static. */
c9317f21 9499
9c6a1327
TT
9500static void
9501alloc_rust_variant (struct obstack *obstack, struct type *type,
9502 int discriminant_index, int default_index,
9503 gdb::array_view<discriminant_range> ranges)
9504{
57d02173
TT
9505 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9506 gdb_assert (discriminant_index == -1
9507 || (discriminant_index >= 0
9508 && discriminant_index < type->num_fields ()));
c9317f21 9509 gdb_assert (default_index == -1
1f704f76 9510 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9511
9c6a1327 9512 /* We have one variant for each non-discriminant field. */
57d02173
TT
9513 int n_variants = type->num_fields ();
9514 if (discriminant_index != -1)
9515 --n_variants;
c9317f21 9516
9c6a1327
TT
9517 variant *variants = new (obstack) variant[n_variants];
9518 int var_idx = 0;
9519 int range_idx = 0;
1f704f76 9520 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9521 {
9522 if (i == discriminant_index)
9523 continue;
c9317f21 9524
9c6a1327
TT
9525 variants[var_idx].first_field = i;
9526 variants[var_idx].last_field = i + 1;
9527
9528 /* The default field does not need a range, but other fields do.
9529 We skipped the discriminant above. */
9530 if (i != default_index)
9531 {
9532 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9533 ++range_idx;
9534 }
c9317f21 9535
9c6a1327
TT
9536 ++var_idx;
9537 }
9538
9539 gdb_assert (range_idx == ranges.size ());
9540 gdb_assert (var_idx == n_variants);
9541
9542 variant_part *part = new (obstack) variant_part;
9543 part->discriminant_index = discriminant_index;
57d02173
TT
9544 /* If there is no discriminant, then whether it is signed is of no
9545 consequence. */
9546 part->is_unsigned
9547 = (discriminant_index == -1
9548 ? false
c6d940a9 9549 : type->field (discriminant_index).type ()->is_unsigned ());
9c6a1327
TT
9550 part->variants = gdb::array_view<variant> (variants, n_variants);
9551
9552 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9553 gdb::array_view<variant_part> *prop_value
9554 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9555
9c6a1327 9556 struct dynamic_prop prop;
8c2e4e06 9557 prop.set_variant_parts (prop_value);
9c6a1327 9558
5c54719c 9559 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9560}
9561
9562/* Some versions of rustc emitted enums in an unusual way.
9563
9564 Ordinary enums were emitted as unions. The first element of each
9565 structure in the union was named "RUST$ENUM$DISR". This element
9566 held the discriminant.
9567
9568 These versions of Rust also implemented the "non-zero"
9569 optimization. When the enum had two values, and one is empty and
9570 the other holds a pointer that cannot be zero, the pointer is used
9571 as the discriminant, with a zero value meaning the empty variant.
9572 Here, the union's first member is of the form
9573 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9574 where the fieldnos are the indices of the fields that should be
9575 traversed in order to find the field (which may be several fields deep)
9576 and the variantname is the name of the variant of the case when the
9577 field is zero.
9578
9579 This function recognizes whether TYPE is of one of these forms,
9580 and, if so, smashes it to be a variant type. */
9581
9582static void
9583quirk_rust_enum (struct type *type, struct objfile *objfile)
9584{
78134374 9585 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9586
9587 /* We don't need to deal with empty enums. */
1f704f76 9588 if (type->num_fields () == 0)
c9317f21
TT
9589 return;
9590
9591#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9592 if (type->num_fields () == 1
c9317f21
TT
9593 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9594 {
9595 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9596
9597 /* Decode the field name to find the offset of the
9598 discriminant. */
9599 ULONGEST bit_offset = 0;
940da03e 9600 struct type *field_type = type->field (0).type ();
c9317f21
TT
9601 while (name[0] >= '0' && name[0] <= '9')
9602 {
9603 char *tail;
9604 unsigned long index = strtoul (name, &tail, 10);
9605 name = tail;
9606 if (*name != '$'
1f704f76 9607 || index >= field_type->num_fields ()
c9317f21
TT
9608 || (TYPE_FIELD_LOC_KIND (field_type, index)
9609 != FIELD_LOC_KIND_BITPOS))
9610 {
b98664d3 9611 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9612 "[in module %s]"),
9613 TYPE_FIELD_NAME (type, 0),
9614 objfile_name (objfile));
9615 return;
9616 }
9617 ++name;
9618
9619 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 9620 field_type = field_type->field (index).type ();
c9317f21
TT
9621 }
9622
9c6a1327
TT
9623 /* Smash this type to be a structure type. We have to do this
9624 because the type has already been recorded. */
67607e24 9625 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9626 type->set_num_fields (3);
9c6a1327 9627 /* Save the field we care about. */
ceacbf6e 9628 struct field saved_field = type->field (0);
3cabb6b0
SM
9629 type->set_fields
9630 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9631
9c6a1327 9632 /* Put the discriminant at index 0. */
5d14b6e5 9633 type->field (0).set_type (field_type);
9c6a1327
TT
9634 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9635 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9636 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9637
9638 /* The order of fields doesn't really matter, so put the real
9639 field at index 1 and the data-less field at index 2. */
ceacbf6e 9640 type->field (1) = saved_field;
9c6a1327 9641 TYPE_FIELD_NAME (type, 1)
940da03e
SM
9642 = rust_last_path_segment (type->field (1).type ()->name ());
9643 type->field (1).type ()->set_name
7d93a1e0 9644 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9645 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9646
9647 const char *dataless_name
7d93a1e0 9648 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9649 name);
9650 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9651 dataless_name);
5d14b6e5 9652 type->field (2).set_type (dataless_type);
c9317f21
TT
9653 /* NAME points into the original discriminant name, which
9654 already has the correct lifetime. */
9c6a1327 9655 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9656 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9657
9c6a1327
TT
9658 /* Indicate that this is a variant type. */
9659 static discriminant_range ranges[1] = { { 0, 0 } };
9660 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9661 }
77c2dba3
TT
9662 /* A union with a single anonymous field is probably an old-style
9663 univariant enum. */
1f704f76 9664 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9665 {
c9317f21
TT
9666 /* Smash this type to be a structure type. We have to do this
9667 because the type has already been recorded. */
67607e24 9668 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9669
940da03e 9670 struct type *field_type = type->field (0).type ();
c9317f21 9671 const char *variant_name
7d93a1e0 9672 = rust_last_path_segment (field_type->name ());
9c6a1327 9673 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9674 field_type->set_name
9675 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9676 type->name (), variant_name));
57d02173
TT
9677
9678 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
c9317f21
TT
9679 }
9680 else
9681 {
9682 struct type *disr_type = nullptr;
1f704f76 9683 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9684 {
940da03e 9685 disr_type = type->field (i).type ();
c9317f21 9686
78134374 9687 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9688 {
9689 /* All fields of a true enum will be structs. */
9690 return;
9691 }
1f704f76 9692 else if (disr_type->num_fields () == 0)
c9317f21
TT
9693 {
9694 /* Could be data-less variant, so keep going. */
a037790e 9695 disr_type = nullptr;
c9317f21
TT
9696 }
9697 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9698 "RUST$ENUM$DISR") != 0)
9699 {
9700 /* Not a Rust enum. */
9701 return;
9702 }
9703 else
9704 {
9705 /* Found one. */
9706 break;
9707 }
9708 }
9709
9710 /* If we got here without a discriminant, then it's probably
9711 just a union. */
9712 if (disr_type == nullptr)
9713 return;
9714
9715 /* Smash this type to be a structure type. We have to do this
9716 because the type has already been recorded. */
67607e24 9717 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9718
9c6a1327 9719 /* Make space for the discriminant field. */
ceacbf6e 9720 struct field *disr_field = &disr_type->field (0);
9c6a1327 9721 field *new_fields
1f704f76 9722 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9723 * sizeof (struct field)));
80fc5e77 9724 memcpy (new_fields + 1, type->fields (),
1f704f76 9725 type->num_fields () * sizeof (struct field));
3cabb6b0 9726 type->set_fields (new_fields);
1f704f76 9727 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9728
9729 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9730 type->field (0) = *disr_field;
9c6a1327
TT
9731 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9732 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9733
9734 /* We need a way to find the correct discriminant given a
9735 variant name. For convenience we build a map here. */
b6cdac4b 9736 struct type *enum_type = disr_field->type ();
c9317f21 9737 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9738 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9739 {
9740 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9741 {
9742 const char *name
9743 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9744 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9745 }
9746 }
9747
1f704f76 9748 int n_fields = type->num_fields ();
9c6a1327
TT
9749 /* We don't need a range entry for the discriminant, but we do
9750 need one for every other field, as there is no default
9751 variant. */
9752 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9753 discriminant_range,
9754 n_fields - 1);
c9317f21
TT
9755 /* Skip the discriminant here. */
9756 for (int i = 1; i < n_fields; ++i)
9757 {
9758 /* Find the final word in the name of this variant's type.
9759 That name can be used to look up the correct
9760 discriminant. */
9761 const char *variant_name
940da03e 9762 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9763
9764 auto iter = discriminant_map.find (variant_name);
9765 if (iter != discriminant_map.end ())
9c6a1327 9766 {
57d02173
TT
9767 ranges[i - 1].low = iter->second;
9768 ranges[i - 1].high = iter->second;
9c6a1327 9769 }
c9317f21 9770
57d02173
TT
9771 /* In Rust, each element should have the size of the
9772 enclosing enum. */
9773 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9774
bedda9ac 9775 /* Remove the discriminant field, if it exists. */
940da03e 9776 struct type *sub_type = type->field (i).type ();
1f704f76 9777 if (sub_type->num_fields () > 0)
bedda9ac 9778 {
5e33d5f4 9779 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9780 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9781 }
9c6a1327 9782 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9783 sub_type->set_name
9784 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9785 type->name (), variant_name));
c9317f21 9786 }
9c6a1327
TT
9787
9788 /* Indicate that this is a variant type. */
a1520ad8 9789 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9790 gdb::array_view<discriminant_range> (ranges,
9791 n_fields - 1));
c9317f21
TT
9792 }
9793}
9794
9795/* Rewrite some Rust unions to be structures with variants parts. */
9796
9797static void
9798rust_union_quirks (struct dwarf2_cu *cu)
9799{
9800 gdb_assert (cu->language == language_rust);
52941706 9801 for (type *type_ : cu->rust_unions)
5e22e966 9802 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9803 /* We don't need this any more. */
9804 cu->rust_unions.clear ();
c9317f21
TT
9805}
9806
8adb8487
TT
9807/* See read.h. */
9808
9809type_unit_group_unshareable *
9810dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9811{
9812 auto iter = this->m_type_units.find (tu_group);
9813 if (iter != this->m_type_units.end ())
9814 return iter->second.get ();
9815
9816 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9817 type_unit_group_unshareable *result = uniq.get ();
9818 this->m_type_units[tu_group] = std::move (uniq);
9819 return result;
9820}
9821
e286671b
TT
9822struct type *
9823dwarf2_per_objfile::get_type_for_signatured_type
9824 (signatured_type *sig_type) const
9825{
9826 auto iter = this->m_type_map.find (sig_type);
9827 if (iter == this->m_type_map.end ())
9828 return nullptr;
9829
9830 return iter->second;
9831}
9832
9833void dwarf2_per_objfile::set_type_for_signatured_type
9834 (signatured_type *sig_type, struct type *type)
9835{
9836 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9837
9838 this->m_type_map[sig_type] = type;
9839}
9840
95554aad
TT
9841/* A helper function for computing the list of all symbol tables
9842 included by PER_CU. */
9843
9844static void
4c39bc03 9845recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9846 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9847 dwarf2_per_cu_data *per_cu,
9848 dwarf2_per_objfile *per_objfile,
43f3e411 9849 struct compunit_symtab *immediate_parent)
95554aad 9850{
af758d11 9851 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9852 if (*slot != NULL)
9853 {
9854 /* This inclusion and its children have been processed. */
9855 return;
9856 }
9857
9858 *slot = per_cu;
af758d11 9859
95554aad 9860 /* Only add a CU if it has a symbol table. */
43182c09 9861 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9862 if (cust != NULL)
ec94af83
DE
9863 {
9864 /* If this is a type unit only add its symbol table if we haven't
9865 seen it yet (type unit per_cu's can share symtabs). */
9866 if (per_cu->is_debug_types)
9867 {
43f3e411 9868 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9869 if (*slot == NULL)
9870 {
43f3e411 9871 *slot = cust;
4c39bc03 9872 result->push_back (cust);
43f3e411
DE
9873 if (cust->user == NULL)
9874 cust->user = immediate_parent;
ec94af83
DE
9875 }
9876 }
9877 else
f9125b6c 9878 {
4c39bc03 9879 result->push_back (cust);
43f3e411
DE
9880 if (cust->user == NULL)
9881 cust->user = immediate_parent;
f9125b6c 9882 }
ec94af83 9883 }
95554aad 9884
ae640021
AB
9885 if (!per_cu->imported_symtabs_empty ())
9886 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9887 {
9888 recursively_compute_inclusions (result, all_children,
43182c09
SM
9889 all_type_symtabs, ptr, per_objfile,
9890 cust);
ae640021 9891 }
95554aad
TT
9892}
9893
43f3e411 9894/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9895 PER_CU. */
9896
9897static void
43182c09
SM
9898compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9899 dwarf2_per_objfile *per_objfile)
95554aad 9900{
f4dc4d17
DE
9901 gdb_assert (! per_cu->is_debug_types);
9902
ae640021 9903 if (!per_cu->imported_symtabs_empty ())
95554aad 9904 {
ae640021 9905 int len;
4c39bc03 9906 std::vector<compunit_symtab *> result_symtabs;
43182c09 9907 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9908
9909 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9910 if (cust == NULL)
95554aad
TT
9911 return;
9912
280a9412
TT
9913 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9914 htab_eq_pointer,
9915 NULL, xcalloc, xfree));
9916 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9917 htab_eq_pointer,
9918 NULL, xcalloc, xfree));
95554aad 9919
ae640021 9920 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83 9921 {
280a9412
TT
9922 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9923 all_type_symtabs.get (), ptr,
9924 per_objfile, cust);
ec94af83 9925 }
95554aad 9926
ec94af83 9927 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9928 len = result_symtabs.size ();
43f3e411 9929 cust->includes
f6e649dd 9930 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9931 struct compunit_symtab *, len + 1);
4c39bc03
TT
9932 memcpy (cust->includes, result_symtabs.data (),
9933 len * sizeof (compunit_symtab *));
43f3e411 9934 cust->includes[len] = NULL;
95554aad
TT
9935 }
9936}
9937
9938/* Compute the 'includes' field for the symtabs of all the CUs we just
9939 read. */
9940
9941static void
976ca316 9942process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9943{
976ca316 9944 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9945 {
9946 if (! iter->is_debug_types)
976ca316 9947 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9948 }
95554aad 9949
976ca316 9950 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9951}
9952
8fc0b21d 9953/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9954 already been loaded into memory. */
9955
9956static void
8fc0b21d 9957process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9958{
976ca316
SM
9959 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9960 struct objfile *objfile = per_objfile->objfile;
08feed99 9961 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9962 CORE_ADDR lowpc, highpc;
43f3e411 9963 struct compunit_symtab *cust;
10b3939b 9964 CORE_ADDR baseaddr;
4359dff1 9965 struct block *static_block;
3e29f34a 9966 CORE_ADDR addr;
10b3939b 9967
b3b3bada 9968 baseaddr = objfile->text_section_offset ();
10b3939b 9969
c89b44cd
TT
9970 /* Clear the list here in case something was left over. */
9971 cu->method_list.clear ();
10b3939b 9972
95554aad
TT
9973 cu->language = pretend_language;
9974 cu->language_defn = language_def (cu->language);
9975
1c47ec3e
TV
9976 dwarf2_find_base_address (cu->dies, cu);
9977
c906108c 9978 /* Do line number decoding in read_file_scope () */
10b3939b 9979 process_die (cu->dies, cu);
c906108c 9980
a766d390
DE
9981 /* For now fudge the Go package. */
9982 if (cu->language == language_go)
9983 fixup_go_packaging (cu);
9984
5f48f8f3 9985 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9986 should be complete, and it should now be safe to compute all of the
9987 physnames. */
9988 compute_delayed_physnames (cu);
3da10d80 9989
c9317f21
TT
9990 if (cu->language == language_rust)
9991 rust_union_quirks (cu);
9992
fae299cd
DC
9993 /* Some compilers don't define a DW_AT_high_pc attribute for the
9994 compilation unit. If the DW_AT_high_pc is missing, synthesize
9995 it, by scanning the DIE's below the compilation unit. */
10b3939b 9996 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9997
3e29f34a 9998 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9999 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10000
10001 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10002 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10003 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10004 addrmap to help ensure it has an accurate map of pc values belonging to
10005 this comp unit. */
10006 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10007
c24bdb02 10008 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10009 SECT_OFF_TEXT (objfile),
10010 0);
c906108c 10011
43f3e411 10012 if (cust != NULL)
c906108c 10013 {
df15bd07 10014 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10015
8be455d7
JK
10016 /* Set symtab language to language from DW_AT_language. If the
10017 compilation is from a C file generated by language preprocessors, do
10018 not set the language if it was already deduced by start_subfile. */
43f3e411 10019 if (!(cu->language == language_c
40e3ad0e 10020 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10021 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10022
10023 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10024 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10025 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10026 there were bugs in prologue debug info, fixed later in GCC-4.5
10027 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10028
10029 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10030 needed, it would be wrong due to missing DW_AT_producer there.
10031
10032 Still one can confuse GDB by using non-standard GCC compilation
10033 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10034 */
ab260dad 10035 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10036 cust->locations_valid = 1;
e0d00bc7
JK
10037
10038 if (gcc_4_minor >= 5)
43f3e411 10039 cust->epilogue_unwind_valid = 1;
96408a79 10040
43f3e411 10041 cust->call_site_htab = cu->call_site_htab;
c906108c 10042 }
9291a0cd 10043
976ca316 10044 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 10045
95554aad 10046 /* Push it for inclusion processing later. */
976ca316 10047 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
10048
10049 /* Not needed any more. */
c24bdb02 10050 cu->reset_builder ();
f4dc4d17 10051}
45cfd468 10052
8fc0b21d 10053/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
10054 already been loaded into memory. */
10055
10056static void
8fc0b21d 10057process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
10058 enum language pretend_language)
10059{
976ca316
SM
10060 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10061 struct objfile *objfile = per_objfile->objfile;
43f3e411 10062 struct compunit_symtab *cust;
0186c6a7
DE
10063 struct signatured_type *sig_type;
10064
8fc0b21d
SM
10065 gdb_assert (cu->per_cu->is_debug_types);
10066 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 10067
c89b44cd
TT
10068 /* Clear the list here in case something was left over. */
10069 cu->method_list.clear ();
f4dc4d17 10070
f4dc4d17
DE
10071 cu->language = pretend_language;
10072 cu->language_defn = language_def (cu->language);
10073
10074 /* The symbol tables are set up in read_type_unit_scope. */
10075 process_die (cu->dies, cu);
10076
10077 /* For now fudge the Go package. */
10078 if (cu->language == language_go)
10079 fixup_go_packaging (cu);
10080
5f48f8f3 10081 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10082 should be complete, and it should now be safe to compute all of the
10083 physnames. */
10084 compute_delayed_physnames (cu);
f4dc4d17 10085
c9317f21
TT
10086 if (cu->language == language_rust)
10087 rust_union_quirks (cu);
10088
f4dc4d17
DE
10089 /* TUs share symbol tables.
10090 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10091 of it with end_expandable_symtab. Otherwise, complete the addition of
10092 this TU's symbols to the existing symtab. */
8adb8487 10093 type_unit_group_unshareable *tug_unshare =
976ca316 10094 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 10095 if (tug_unshare->compunit_symtab == NULL)
45cfd468 10096 {
c24bdb02
KS
10097 buildsym_compunit *builder = cu->get_builder ();
10098 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 10099 tug_unshare->compunit_symtab = cust;
f4dc4d17 10100
43f3e411 10101 if (cust != NULL)
f4dc4d17
DE
10102 {
10103 /* Set symtab language to language from DW_AT_language. If the
10104 compilation is from a C file generated by language preprocessors,
10105 do not set the language if it was already deduced by
10106 start_subfile. */
43f3e411
DE
10107 if (!(cu->language == language_c
10108 && COMPUNIT_FILETABS (cust)->language != language_c))
10109 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10110 }
10111 }
10112 else
10113 {
c24bdb02 10114 cu->get_builder ()->augment_type_symtab ();
8adb8487 10115 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
10116 }
10117
976ca316 10118 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
10119
10120 /* Not needed any more. */
c24bdb02 10121 cu->reset_builder ();
c906108c
SS
10122}
10123
95554aad
TT
10124/* Process an imported unit DIE. */
10125
10126static void
10127process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10128{
10129 struct attribute *attr;
10130
f4dc4d17
DE
10131 /* For now we don't handle imported units in type units. */
10132 if (cu->per_cu->is_debug_types)
10133 {
10134 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10135 " supported in type units [in module %s]"),
5e22e966 10136 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
10137 }
10138
95554aad
TT
10139 attr = dwarf2_attr (die, DW_AT_import, cu);
10140 if (attr != NULL)
10141 {
0826b30a 10142 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 10143 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 10144 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 10145 dwarf2_per_cu_data *per_cu
ab432490 10146 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 10147
58990295
TV
10148 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10149 into another compilation unit, at root level. Regard this as a hint,
10150 and ignore it. */
10151 if (die->parent && die->parent->parent == NULL
10152 && per_cu->unit_type == DW_UT_compile
10153 && per_cu->lang == language_cplus)
10154 return;
10155
69d751e3 10156 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 10157 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
10158 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10159 false, cu->language);
95554aad 10160
ae640021 10161 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10162 }
10163}
10164
4c8aa72d
PA
10165/* RAII object that represents a process_die scope: i.e.,
10166 starts/finishes processing a DIE. */
10167class process_die_scope
adde2bff 10168{
4c8aa72d
PA
10169public:
10170 process_die_scope (die_info *die, dwarf2_cu *cu)
10171 : m_die (die), m_cu (cu)
10172 {
10173 /* We should only be processing DIEs not already in process. */
10174 gdb_assert (!m_die->in_process);
10175 m_die->in_process = true;
10176 }
8c3cb9fa 10177
4c8aa72d
PA
10178 ~process_die_scope ()
10179 {
10180 m_die->in_process = false;
10181
10182 /* If we're done processing the DIE for the CU that owns the line
10183 header, we don't need the line header anymore. */
10184 if (m_cu->line_header_die_owner == m_die)
10185 {
10186 delete m_cu->line_header;
10187 m_cu->line_header = NULL;
10188 m_cu->line_header_die_owner = NULL;
10189 }
10190 }
10191
10192private:
10193 die_info *m_die;
10194 dwarf2_cu *m_cu;
10195};
adde2bff 10196
c906108c
SS
10197/* Process a die and its children. */
10198
10199static void
e7c27a73 10200process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10201{
4c8aa72d 10202 process_die_scope scope (die, cu);
adde2bff 10203
c906108c
SS
10204 switch (die->tag)
10205 {
10206 case DW_TAG_padding:
10207 break;
10208 case DW_TAG_compile_unit:
95554aad 10209 case DW_TAG_partial_unit:
e7c27a73 10210 read_file_scope (die, cu);
c906108c 10211 break;
348e048f
DE
10212 case DW_TAG_type_unit:
10213 read_type_unit_scope (die, cu);
10214 break;
c906108c 10215 case DW_TAG_subprogram:
0a4b0913
AB
10216 /* Nested subprograms in Fortran get a prefix. */
10217 if (cu->language == language_fortran
10218 && die->parent != NULL
10219 && die->parent->tag == DW_TAG_subprogram)
10220 cu->processing_has_namespace_info = true;
10221 /* Fall through. */
c906108c 10222 case DW_TAG_inlined_subroutine:
edb3359d 10223 read_func_scope (die, cu);
c906108c
SS
10224 break;
10225 case DW_TAG_lexical_block:
14898363
L
10226 case DW_TAG_try_block:
10227 case DW_TAG_catch_block:
e7c27a73 10228 read_lexical_block_scope (die, cu);
c906108c 10229 break;
216f72a1 10230 case DW_TAG_call_site:
96408a79
SA
10231 case DW_TAG_GNU_call_site:
10232 read_call_site_scope (die, cu);
10233 break;
c906108c 10234 case DW_TAG_class_type:
680b30c7 10235 case DW_TAG_interface_type:
c906108c
SS
10236 case DW_TAG_structure_type:
10237 case DW_TAG_union_type:
134d01f1 10238 process_structure_scope (die, cu);
c906108c
SS
10239 break;
10240 case DW_TAG_enumeration_type:
134d01f1 10241 process_enumeration_scope (die, cu);
c906108c 10242 break;
134d01f1 10243
f792889a
DJ
10244 /* These dies have a type, but processing them does not create
10245 a symbol or recurse to process the children. Therefore we can
10246 read them on-demand through read_type_die. */
c906108c 10247 case DW_TAG_subroutine_type:
72019c9c 10248 case DW_TAG_set_type:
c906108c 10249 case DW_TAG_pointer_type:
c906108c 10250 case DW_TAG_ptr_to_member_type:
c906108c 10251 case DW_TAG_reference_type:
4297a3f0 10252 case DW_TAG_rvalue_reference_type:
c906108c 10253 case DW_TAG_string_type:
c906108c 10254 break;
134d01f1 10255
d8f62e84
TT
10256 case DW_TAG_array_type:
10257 /* We only need to handle this case for Ada -- in other
10258 languages, it's normal for the compiler to emit a typedef
10259 instead. */
10260 if (cu->language != language_ada)
10261 break;
10262 /* FALLTHROUGH */
c906108c 10263 case DW_TAG_base_type:
a02abb62 10264 case DW_TAG_subrange_type:
cb249c71 10265 case DW_TAG_typedef:
134d01f1 10266 /* Add a typedef symbol for the type definition, if it has a
dda83cd7 10267 DW_AT_name. */
f792889a 10268 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10269 break;
c906108c 10270 case DW_TAG_common_block:
e7c27a73 10271 read_common_block (die, cu);
c906108c
SS
10272 break;
10273 case DW_TAG_common_inclusion:
10274 break;
d9fa45fe 10275 case DW_TAG_namespace:
9068261f 10276 cu->processing_has_namespace_info = true;
e7c27a73 10277 read_namespace (die, cu);
d9fa45fe 10278 break;
5d7cb8df 10279 case DW_TAG_module:
9068261f 10280 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10281 read_module (die, cu);
10282 break;
d9fa45fe 10283 case DW_TAG_imported_declaration:
9068261f 10284 cu->processing_has_namespace_info = true;
74921315
KS
10285 if (read_namespace_alias (die, cu))
10286 break;
86a73007
TT
10287 /* The declaration is not a global namespace alias. */
10288 /* Fall through. */
d9fa45fe 10289 case DW_TAG_imported_module:
9068261f 10290 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10291 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10292 || cu->language != language_fortran))
b98664d3 10293 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10294 dwarf_tag_name (die->tag));
10295 read_import_statement (die, cu);
d9fa45fe 10296 break;
95554aad
TT
10297
10298 case DW_TAG_imported_unit:
10299 process_imported_unit_die (die, cu);
10300 break;
10301
71a3c369
TT
10302 case DW_TAG_variable:
10303 read_variable (die, cu);
10304 break;
10305
c906108c 10306 default:
e7c27a73 10307 new_symbol (die, NULL, cu);
c906108c
SS
10308 break;
10309 }
10310}
ca69b9e6
DE
10311\f
10312/* DWARF name computation. */
c906108c 10313
94af9270
KS
10314/* A helper function for dwarf2_compute_name which determines whether DIE
10315 needs to have the name of the scope prepended to the name listed in the
10316 die. */
10317
10318static int
10319die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10320{
1c809c68
TT
10321 struct attribute *attr;
10322
94af9270
KS
10323 switch (die->tag)
10324 {
10325 case DW_TAG_namespace:
10326 case DW_TAG_typedef:
10327 case DW_TAG_class_type:
10328 case DW_TAG_interface_type:
10329 case DW_TAG_structure_type:
10330 case DW_TAG_union_type:
10331 case DW_TAG_enumeration_type:
10332 case DW_TAG_enumerator:
10333 case DW_TAG_subprogram:
08a76f8a 10334 case DW_TAG_inlined_subroutine:
94af9270 10335 case DW_TAG_member:
74921315 10336 case DW_TAG_imported_declaration:
94af9270
KS
10337 return 1;
10338
10339 case DW_TAG_variable:
c2b0a229 10340 case DW_TAG_constant:
94af9270
KS
10341 /* We only need to prefix "globally" visible variables. These include
10342 any variable marked with DW_AT_external or any variable that
10343 lives in a namespace. [Variables in anonymous namespaces
10344 require prefixing, but they are not DW_AT_external.] */
10345
10346 if (dwarf2_attr (die, DW_AT_specification, cu))
10347 {
10348 struct dwarf2_cu *spec_cu = cu;
9a619af0 10349
94af9270
KS
10350 return die_needs_namespace (die_specification (die, &spec_cu),
10351 spec_cu);
10352 }
10353
1c809c68 10354 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10355 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10356 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10357 return 0;
10358 /* A variable in a lexical block of some kind does not need a
10359 namespace, even though in C++ such variables may be external
10360 and have a mangled name. */
10361 if (die->parent->tag == DW_TAG_lexical_block
10362 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10363 || die->parent->tag == DW_TAG_catch_block
10364 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10365 return 0;
10366 return 1;
94af9270
KS
10367
10368 default:
10369 return 0;
10370 }
10371}
10372
73b9be8b
KS
10373/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10374 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10375 defined for the given DIE. */
10376
10377static struct attribute *
10378dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10379{
10380 struct attribute *attr;
10381
10382 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10383 if (attr == NULL)
10384 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10385
10386 return attr;
10387}
10388
10389/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10390 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10391 defined for the given DIE. */
10392
10393static const char *
10394dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10395{
10396 const char *linkage_name;
10397
10398 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10399 if (linkage_name == NULL)
10400 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10401
787de330
TT
10402 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10403 See https://github.com/rust-lang/rust/issues/32925. */
10404 if (cu->language == language_rust && linkage_name != NULL
10405 && strchr (linkage_name, '{') != NULL)
10406 linkage_name = NULL;
10407
73b9be8b
KS
10408 return linkage_name;
10409}
10410
94af9270 10411/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10412 compute the physname for the object, which include a method's:
9c37b5ae 10413 - formal parameters (C++),
a766d390 10414 - receiver type (Go),
a766d390
DE
10415
10416 The term "physname" is a bit confusing.
10417 For C++, for example, it is the demangled name.
10418 For Go, for example, it's the mangled name.
94af9270 10419
af6b7be1
JB
10420 For Ada, return the DIE's linkage name rather than the fully qualified
10421 name. PHYSNAME is ignored..
10422
5989a64e 10423 The result is allocated on the objfile->per_bfd's obstack and
45940949 10424 canonicalized. */
94af9270
KS
10425
10426static const char *
15d034d0
TT
10427dwarf2_compute_name (const char *name,
10428 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10429 int physname)
10430{
5e22e966 10431 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10432
94af9270
KS
10433 if (name == NULL)
10434 name = dwarf2_name (die, cu);
10435
2ee7123e
DE
10436 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10437 but otherwise compute it by typename_concat inside GDB.
10438 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10439 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10440 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10441 will set the demangled name to the result of dwarf2_full_name, and it is
10442 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10443 if (cu->language == language_ada
10444 || (cu->language == language_fortran && physname))
10445 {
10446 /* For Ada unit, we prefer the linkage name over the name, as
10447 the former contains the exported name, which the user expects
10448 to be able to reference. Ideally, we want the user to be able
10449 to reference this entity using either natural or linkage name,
10450 but we haven't started looking at this enhancement yet. */
73b9be8b 10451 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10452
2ee7123e
DE
10453 if (linkage_name != NULL)
10454 return linkage_name;
f55ee35c
JK
10455 }
10456
94af9270
KS
10457 /* These are the only languages we know how to qualify names in. */
10458 if (name != NULL
9c37b5ae 10459 && (cu->language == language_cplus
c44af4eb
TT
10460 || cu->language == language_fortran || cu->language == language_d
10461 || cu->language == language_rust))
94af9270
KS
10462 {
10463 if (die_needs_namespace (die, cu))
10464 {
0d5cff50 10465 const char *prefix;
34a68019 10466 const char *canonical_name = NULL;
94af9270 10467
d7e74731
PA
10468 string_file buf;
10469
94af9270 10470 prefix = determine_prefix (die, cu);
94af9270
KS
10471 if (*prefix != '\0')
10472 {
43816ebc
TT
10473 gdb::unique_xmalloc_ptr<char> prefixed_name
10474 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10475
43816ebc 10476 buf.puts (prefixed_name.get ());
94af9270
KS
10477 }
10478 else
d7e74731 10479 buf.puts (name);
94af9270 10480
98bfdba5
PA
10481 /* Template parameters may be specified in the DIE's DW_AT_name, or
10482 as children with DW_TAG_template_type_param or
10483 DW_TAG_value_type_param. If the latter, add them to the name
10484 here. If the name already has template parameters, then
10485 skip this step; some versions of GCC emit both, and
10486 it is more efficient to use the pre-computed name.
10487
10488 Something to keep in mind about this process: it is very
10489 unlikely, or in some cases downright impossible, to produce
10490 something that will match the mangled name of a function.
10491 If the definition of the function has the same debug info,
10492 we should be able to match up with it anyway. But fallbacks
10493 using the minimal symbol, for instance to find a method
10494 implemented in a stripped copy of libstdc++, will not work.
10495 If we do not have debug info for the definition, we will have to
10496 match them up some other way.
10497
10498 When we do name matching there is a related problem with function
10499 templates; two instantiated function templates are allowed to
10500 differ only by their return types, which we do not add here. */
10501
10502 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10503 {
10504 struct attribute *attr;
10505 struct die_info *child;
10506 int first = 1;
2c75ccb2 10507 const language_defn *cplus_lang = language_def (cu->language);
98bfdba5
PA
10508
10509 die->building_fullname = 1;
10510
10511 for (child = die->child; child != NULL; child = child->sibling)
10512 {
10513 struct type *type;
12df843f 10514 LONGEST value;
d521ce57 10515 const gdb_byte *bytes;
98bfdba5
PA
10516 struct dwarf2_locexpr_baton *baton;
10517 struct value *v;
10518
10519 if (child->tag != DW_TAG_template_type_param
10520 && child->tag != DW_TAG_template_value_param)
10521 continue;
10522
10523 if (first)
10524 {
d7e74731 10525 buf.puts ("<");
98bfdba5
PA
10526 first = 0;
10527 }
10528 else
d7e74731 10529 buf.puts (", ");
98bfdba5
PA
10530
10531 attr = dwarf2_attr (child, DW_AT_type, cu);
10532 if (attr == NULL)
10533 {
b98664d3 10534 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10535 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10536 continue;
10537 }
10538 type = die_type (child, cu);
10539
10540 if (child->tag == DW_TAG_template_type_param)
10541 {
2c75ccb2
AB
10542 cplus_lang->print_type (type, "", &buf, -1, 0,
10543 &type_print_raw_options);
98bfdba5
PA
10544 continue;
10545 }
10546
10547 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10548 if (attr == NULL)
10549 {
b98664d3 10550 complaint (_("template parameter missing "
3e43a32a 10551 "DW_AT_const_value"));
d7e74731 10552 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10553 continue;
10554 }
10555
10556 dwarf2_const_value_attr (attr, type, name,
10557 &cu->comp_unit_obstack, cu,
10558 &value, &bytes, &baton);
10559
20ce4123 10560 if (type->has_no_signedness ())
98bfdba5
PA
10561 /* GDB prints characters as NUMBER 'CHAR'. If that's
10562 changed, this can use value_print instead. */
2c75ccb2 10563 cplus_lang->printchar (value, type, &buf);
98bfdba5
PA
10564 else
10565 {
10566 struct value_print_options opts;
10567
10568 if (baton != NULL)
10569 v = dwarf2_evaluate_loc_desc (type, NULL,
10570 baton->data,
10571 baton->size,
9f47c707
SM
10572 baton->per_cu,
10573 baton->per_objfile);
98bfdba5
PA
10574 else if (bytes != NULL)
10575 {
10576 v = allocate_value (type);
10577 memcpy (value_contents_writeable (v), bytes,
10578 TYPE_LENGTH (type));
10579 }
10580 else
10581 v = value_from_longest (type, value);
10582
3e43a32a
MS
10583 /* Specify decimal so that we do not depend on
10584 the radix. */
98bfdba5
PA
10585 get_formatted_print_options (&opts, 'd');
10586 opts.raw = 1;
d7e74731 10587 value_print (v, &buf, &opts);
98bfdba5 10588 release_value (v);
98bfdba5
PA
10589 }
10590 }
10591
10592 die->building_fullname = 0;
10593
10594 if (!first)
10595 {
10596 /* Close the argument list, with a space if necessary
10597 (nested templates). */
d7e74731
PA
10598 if (!buf.empty () && buf.string ().back () == '>')
10599 buf.puts (" >");
98bfdba5 10600 else
d7e74731 10601 buf.puts (">");
98bfdba5
PA
10602 }
10603 }
10604
9c37b5ae 10605 /* For C++ methods, append formal parameter type
94af9270 10606 information, if PHYSNAME. */
6e70227d 10607
94af9270 10608 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10609 && cu->language == language_cplus)
94af9270
KS
10610 {
10611 struct type *type = read_type_die (die, cu);
10612
d7e74731 10613 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10614 &type_print_raw_options);
94af9270 10615
9c37b5ae 10616 if (cu->language == language_cplus)
94af9270 10617 {
60430eff
DJ
10618 /* Assume that an artificial first parameter is
10619 "this", but do not crash if it is not. RealView
10620 marks unnamed (and thus unused) parameters as
10621 artificial; there is no way to differentiate
10622 the two cases. */
1f704f76 10623 if (type->num_fields () > 0
94af9270 10624 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
10625 && type->field (0).type ()->code () == TYPE_CODE_PTR
10626 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 10627 buf.puts (" const");
94af9270
KS
10628 }
10629 }
10630
d7e74731 10631 const std::string &intermediate_name = buf.string ();
94af9270
KS
10632
10633 if (cu->language == language_cplus)
34a68019 10634 canonical_name
322a8516 10635 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10636 objfile);
34a68019
TT
10637
10638 /* If we only computed INTERMEDIATE_NAME, or if
10639 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10640 intern it. */
322a8516 10641 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10642 name = objfile->intern (intermediate_name);
34a68019
TT
10643 else
10644 name = canonical_name;
94af9270
KS
10645 }
10646 }
10647
10648 return name;
10649}
10650
0114d602
DJ
10651/* Return the fully qualified name of DIE, based on its DW_AT_name.
10652 If scope qualifiers are appropriate they will be added. The result
34a68019 10653 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10654 not have a name. NAME may either be from a previous call to
10655 dwarf2_name or NULL.
10656
9c37b5ae 10657 The output string will be canonicalized (if C++). */
0114d602
DJ
10658
10659static const char *
15d034d0 10660dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10661{
94af9270
KS
10662 return dwarf2_compute_name (name, die, cu, 0);
10663}
0114d602 10664
94af9270
KS
10665/* Construct a physname for the given DIE in CU. NAME may either be
10666 from a previous call to dwarf2_name or NULL. The result will be
10667 allocated on the objfile_objstack or NULL if the DIE does not have a
10668 name.
0114d602 10669
9c37b5ae 10670 The output string will be canonicalized (if C++). */
0114d602 10671
94af9270 10672static const char *
15d034d0 10673dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10674{
5e22e966 10675 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10676 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10677 int need_copy = 1;
10678
10679 /* In this case dwarf2_compute_name is just a shortcut not building anything
10680 on its own. */
10681 if (!die_needs_namespace (die, cu))
10682 return dwarf2_compute_name (name, die, cu, 1);
10683
906bb4c5
TT
10684 if (cu->language != language_rust)
10685 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10686
10687 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10688 has computed. */
791afaa2 10689 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10690 if (mangled != NULL)
900e11f9 10691 {
900e11f9 10692
d3355e4d 10693 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
59cc4834
JB
10694 {
10695 /* Do nothing (do not demangle the symbol name). */
10696 }
a766d390
DE
10697 else
10698 {
0eb876f5
JB
10699 /* Use DMGL_RET_DROP for C++ template functions to suppress
10700 their return type. It is easier for GDB users to search
10701 for such functions as `name(params)' than `long name(params)'.
10702 In such case the minimal symbol names do not match the full
10703 symbol names but for template functions there is never a need
10704 to look up their definition from their declaration so
10705 the only disadvantage remains the minimal symbol variant
10706 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10707 demangled.reset (gdb_demangle (mangled,
10708 (DMGL_PARAMS | DMGL_ANSI
10709 | DMGL_RET_DROP)));
a766d390 10710 }
900e11f9 10711 if (demangled)
791afaa2 10712 canon = demangled.get ();
900e11f9
JK
10713 else
10714 {
10715 canon = mangled;
10716 need_copy = 0;
10717 }
10718 }
10719
10720 if (canon == NULL || check_physname)
10721 {
10722 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10723
10724 if (canon != NULL && strcmp (physname, canon) != 0)
10725 {
10726 /* It may not mean a bug in GDB. The compiler could also
10727 compute DW_AT_linkage_name incorrectly. But in such case
10728 GDB would need to be bug-to-bug compatible. */
10729
b98664d3 10730 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10731 "(from linkage <%s>) - DIE at %s [in module %s]"),
10732 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10733 objfile_name (objfile));
900e11f9
JK
10734
10735 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10736 is available here - over computed PHYSNAME. It is safer
10737 against both buggy GDB and buggy compilers. */
10738
10739 retval = canon;
10740 }
10741 else
10742 {
10743 retval = physname;
10744 need_copy = 0;
10745 }
10746 }
10747 else
10748 retval = canon;
10749
10750 if (need_copy)
be1e3d3e 10751 retval = objfile->intern (retval);
900e11f9 10752
900e11f9 10753 return retval;
0114d602
DJ
10754}
10755
74921315
KS
10756/* Inspect DIE in CU for a namespace alias. If one exists, record
10757 a new symbol for it.
10758
10759 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10760
10761static int
10762read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10763{
10764 struct attribute *attr;
10765
10766 /* If the die does not have a name, this is not a namespace
10767 alias. */
10768 attr = dwarf2_attr (die, DW_AT_name, cu);
10769 if (attr != NULL)
10770 {
10771 int num;
10772 struct die_info *d = die;
10773 struct dwarf2_cu *imported_cu = cu;
10774
10775 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10776 keep inspecting DIEs until we hit the underlying import. */
10777#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10778 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10779 {
10780 attr = dwarf2_attr (d, DW_AT_import, cu);
10781 if (attr == NULL)
10782 break;
10783
10784 d = follow_die_ref (d, attr, &imported_cu);
10785 if (d->tag != DW_TAG_imported_declaration)
10786 break;
10787 }
10788
10789 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10790 {
b98664d3 10791 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10792 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10793 return 0;
10794 }
10795
10796 if (attr != NULL)
10797 {
10798 struct type *type;
0826b30a 10799 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10800
aa66c379 10801 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10802 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10803 {
10804 /* This declaration is a global namespace alias. Add
10805 a symbol for it whose type is the aliased namespace. */
10806 new_symbol (die, type, cu);
10807 return 1;
10808 }
10809 }
10810 }
10811
10812 return 0;
10813}
10814
22cee43f 10815/* Return the using directives repository (global or local?) to use in the
804d2729 10816 current context for CU.
22cee43f
PMR
10817
10818 For Ada, imported declarations can materialize renamings, which *may* be
10819 global. However it is impossible (for now?) in DWARF to distinguish
10820 "external" imported declarations and "static" ones. As all imported
10821 declarations seem to be static in all other languages, make them all CU-wide
10822 global only in Ada. */
10823
10824static struct using_direct **
804d2729 10825using_directives (struct dwarf2_cu *cu)
22cee43f 10826{
c24bdb02
KS
10827 if (cu->language == language_ada
10828 && cu->get_builder ()->outermost_context_p ())
10829 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10830 else
c24bdb02 10831 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10832}
10833
27aa8d6a
SW
10834/* Read the import statement specified by the given die and record it. */
10835
10836static void
10837read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10838{
5e22e966 10839 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10840 struct attribute *import_attr;
32019081 10841 struct die_info *imported_die, *child_die;
de4affc9 10842 struct dwarf2_cu *imported_cu;
27aa8d6a 10843 const char *imported_name;
794684b6 10844 const char *imported_name_prefix;
13387711
SW
10845 const char *canonical_name;
10846 const char *import_alias;
10847 const char *imported_declaration = NULL;
794684b6 10848 const char *import_prefix;
eb1e02fd 10849 std::vector<const char *> excludes;
13387711 10850
27aa8d6a
SW
10851 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10852 if (import_attr == NULL)
10853 {
b98664d3 10854 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10855 dwarf_tag_name (die->tag));
10856 return;
10857 }
10858
de4affc9
CC
10859 imported_cu = cu;
10860 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10861 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10862 if (imported_name == NULL)
10863 {
10864 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10865
dda83cd7
SM
10866 The import in the following code:
10867 namespace A
10868 {
10869 typedef int B;
10870 }
10871
10872 int main ()
10873 {
10874 using A::B;
10875 B b;
10876 return b;
10877 }
10878
10879 ...
10880 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10881 <52> DW_AT_decl_file : 1
10882 <53> DW_AT_decl_line : 6
10883 <54> DW_AT_import : <0x75>
10884 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10885 <59> DW_AT_name : B
10886 <5b> DW_AT_decl_file : 1
10887 <5c> DW_AT_decl_line : 2
10888 <5d> DW_AT_type : <0x6e>
10889 ...
10890 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10891 <76> DW_AT_byte_size : 4
10892 <77> DW_AT_encoding : 5 (signed)
10893
10894 imports the wrong die ( 0x75 instead of 0x58 ).
10895 This case will be ignored until the gcc bug is fixed. */
27aa8d6a
SW
10896 return;
10897 }
10898
82856980
SW
10899 /* Figure out the local name after import. */
10900 import_alias = dwarf2_name (die, cu);
27aa8d6a 10901
794684b6
SW
10902 /* Figure out where the statement is being imported to. */
10903 import_prefix = determine_prefix (die, cu);
10904
10905 /* Figure out what the scope of the imported die is and prepend it
10906 to the name of the imported die. */
de4affc9 10907 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10908
f55ee35c
JK
10909 if (imported_die->tag != DW_TAG_namespace
10910 && imported_die->tag != DW_TAG_module)
794684b6 10911 {
13387711
SW
10912 imported_declaration = imported_name;
10913 canonical_name = imported_name_prefix;
794684b6 10914 }
13387711 10915 else if (strlen (imported_name_prefix) > 0)
12aaed36 10916 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10917 imported_name_prefix,
10918 (cu->language == language_d ? "." : "::"),
10919 imported_name, (char *) NULL);
13387711
SW
10920 else
10921 canonical_name = imported_name;
794684b6 10922
32019081
JK
10923 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10924 for (child_die = die->child; child_die && child_die->tag;
436c571c 10925 child_die = child_die->sibling)
32019081
JK
10926 {
10927 /* DWARF-4: A Fortran use statement with a “rename list” may be
10928 represented by an imported module entry with an import attribute
10929 referring to the module and owned entries corresponding to those
10930 entities that are renamed as part of being imported. */
10931
10932 if (child_die->tag != DW_TAG_imported_declaration)
10933 {
b98664d3 10934 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10935 "- DIE at %s [in module %s]"),
10936 sect_offset_str (child_die->sect_off),
10937 objfile_name (objfile));
32019081
JK
10938 continue;
10939 }
10940
10941 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10942 if (import_attr == NULL)
10943 {
b98664d3 10944 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10945 dwarf_tag_name (child_die->tag));
10946 continue;
10947 }
10948
10949 imported_cu = cu;
10950 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10951 &imported_cu);
10952 imported_name = dwarf2_name (imported_die, imported_cu);
10953 if (imported_name == NULL)
10954 {
b98664d3 10955 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10956 "imported name - DIE at %s [in module %s]"),
10957 sect_offset_str (child_die->sect_off),
10958 objfile_name (objfile));
32019081
JK
10959 continue;
10960 }
10961
eb1e02fd 10962 excludes.push_back (imported_name);
32019081
JK
10963
10964 process_die (child_die, cu);
10965 }
10966
804d2729 10967 add_using_directive (using_directives (cu),
22cee43f
PMR
10968 import_prefix,
10969 canonical_name,
10970 import_alias,
10971 imported_declaration,
10972 excludes,
10973 0,
10974 &objfile->objfile_obstack);
27aa8d6a
SW
10975}
10976
5230b05a
WT
10977/* ICC<14 does not output the required DW_AT_declaration on incomplete
10978 types, but gives them a size of zero. Starting with version 14,
10979 ICC is compatible with GCC. */
10980
9068261f 10981static bool
5230b05a
WT
10982producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10983{
10984 if (!cu->checked_producer)
10985 check_producer (cu);
10986
10987 return cu->producer_is_icc_lt_14;
10988}
10989
eb77c9df
AB
10990/* ICC generates a DW_AT_type for C void functions. This was observed on
10991 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10992 which says that void functions should not have a DW_AT_type. */
10993
10994static bool
10995producer_is_icc (struct dwarf2_cu *cu)
10996{
10997 if (!cu->checked_producer)
10998 check_producer (cu);
10999
11000 return cu->producer_is_icc;
11001}
11002
1b80a9fa
JK
11003/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11004 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11005 this, it was first present in GCC release 4.3.0. */
11006
9068261f 11007static bool
1b80a9fa
JK
11008producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11009{
11010 if (!cu->checked_producer)
11011 check_producer (cu);
11012
11013 return cu->producer_is_gcc_lt_4_3;
11014}
11015
d721ba37
PA
11016static file_and_directory
11017find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11018{
d721ba37
PA
11019 file_and_directory res;
11020
9291a0cd
TT
11021 /* Find the filename. Do not use dwarf2_name here, since the filename
11022 is not a source language identifier. */
d721ba37
PA
11023 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11024 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11025
d721ba37
PA
11026 if (res.comp_dir == NULL
11027 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11028 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11029 {
d721ba37
PA
11030 res.comp_dir_storage = ldirname (res.name);
11031 if (!res.comp_dir_storage.empty ())
11032 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11033 }
d721ba37 11034 if (res.comp_dir != NULL)
9291a0cd
TT
11035 {
11036 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11037 directory, get rid of it. */
d721ba37 11038 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11039
d721ba37
PA
11040 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11041 res.comp_dir = cp + 1;
9291a0cd
TT
11042 }
11043
d721ba37
PA
11044 if (res.name == NULL)
11045 res.name = "<unknown>";
11046
11047 return res;
9291a0cd
TT
11048}
11049
f4dc4d17
DE
11050/* Handle DW_AT_stmt_list for a compilation unit.
11051 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11052 COMP_DIR is the compilation directory. LOWPC is passed to
11053 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11054
11055static void
11056handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11057 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11058{
976ca316 11059 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 11060 struct attribute *attr;
527f3840
JK
11061 struct line_header line_header_local;
11062 hashval_t line_header_local_hash;
527f3840
JK
11063 void **slot;
11064 int decode_mapping;
2ab95328 11065
f4dc4d17
DE
11066 gdb_assert (! cu->per_cu->is_debug_types);
11067
2ab95328 11068 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e 11069 if (attr == NULL || !attr->form_is_unsigned ())
527f3840
JK
11070 return;
11071
d4df075e 11072 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
527f3840
JK
11073
11074 /* The line header hash table is only created if needed (it exists to
11075 prevent redundant reading of the line table for partial_units).
11076 If we're given a partial_unit, we'll need it. If we're given a
11077 compile_unit, then use the line header hash table if it's already
11078 created, but don't create one just yet. */
11079
976ca316 11080 if (per_objfile->line_header_hash == NULL
527f3840 11081 && die->tag == DW_TAG_partial_unit)
2ab95328 11082 {
976ca316 11083 per_objfile->line_header_hash
d15acc42
TT
11084 .reset (htab_create_alloc (127, line_header_hash_voidp,
11085 line_header_eq_voidp,
11086 free_line_header_voidp,
11087 xcalloc, xfree));
527f3840 11088 }
2ab95328 11089
9c541725 11090 line_header_local.sect_off = line_offset;
527f3840
JK
11091 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11092 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 11093 if (per_objfile->line_header_hash != NULL)
527f3840 11094 {
976ca316 11095 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11096 &line_header_local,
11097 line_header_local_hash, NO_INSERT);
11098
11099 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11100 is not present in *SLOT (since if there is something in *SLOT then
11101 it will be for a partial_unit). */
11102 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11103 {
527f3840 11104 gdb_assert (*slot != NULL);
9a3c8263 11105 cu->line_header = (struct line_header *) *slot;
527f3840 11106 return;
dee91e82 11107 }
2ab95328 11108 }
527f3840
JK
11109
11110 /* dwarf_decode_line_header does not yet provide sufficient information.
11111 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11112 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11113 if (lh == NULL)
527f3840 11114 return;
4c8aa72d
PA
11115
11116 cu->line_header = lh.release ();
11117 cu->line_header_die_owner = die;
527f3840 11118
976ca316 11119 if (per_objfile->line_header_hash == NULL)
527f3840
JK
11120 slot = NULL;
11121 else
11122 {
976ca316 11123 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11124 &line_header_local,
11125 line_header_local_hash, INSERT);
11126 gdb_assert (slot != NULL);
11127 }
11128 if (slot != NULL && *slot == NULL)
11129 {
11130 /* This newly decoded line number information unit will be owned
11131 by line_header_hash hash table. */
11132 *slot = cu->line_header;
4c8aa72d 11133 cu->line_header_die_owner = NULL;
527f3840
JK
11134 }
11135 else
11136 {
11137 /* We cannot free any current entry in (*slot) as that struct line_header
dda83cd7 11138 may be already used by multiple CUs. Create only temporary decoded
527f3840
JK
11139 line_header for this CU - it may happen at most once for each line
11140 number information unit. And if we're not using line_header_hash
11141 then this is what we want as well. */
11142 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11143 }
11144 decode_mapping = (die->tag != DW_TAG_partial_unit);
11145 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11146 decode_mapping);
fff8551c 11147
2ab95328
TT
11148}
11149
95554aad 11150/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11151
c906108c 11152static void
e7c27a73 11153read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11154{
976ca316
SM
11155 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11156 struct objfile *objfile = per_objfile->objfile;
08feed99 11157 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 11158 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11159 CORE_ADDR highpc = ((CORE_ADDR) 0);
11160 struct attribute *attr;
c906108c 11161 struct die_info *child_die;
e142c38c 11162 CORE_ADDR baseaddr;
6e70227d 11163
380618d6 11164 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11165 baseaddr = objfile->text_section_offset ();
c906108c 11166
fae299cd 11167 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11168
11169 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11170 from finish_block. */
2acceee2 11171 if (lowpc == ((CORE_ADDR) -1))
c906108c 11172 lowpc = highpc;
3e29f34a 11173 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11174
d721ba37 11175 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11176
f4b8a18d
KW
11177 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11178 standardised yet. As a workaround for the language detection we fall
11179 back to the DW_AT_producer string. */
11180 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11181 cu->language = language_opencl;
11182
3019eac3
DE
11183 /* Similar hack for Go. */
11184 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11185 set_cu_language (DW_LANG_Go, cu);
11186
c24bdb02 11187 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11188
11189 /* Decode line number information if present. We do this before
11190 processing child DIEs, so that the line header table is available
11191 for DW_AT_decl_file. */
d721ba37 11192 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11193
11194 /* Process all dies in compilation unit. */
11195 if (die->child != NULL)
11196 {
11197 child_die = die->child;
11198 while (child_die && child_die->tag)
11199 {
11200 process_die (child_die, cu);
436c571c 11201 child_die = child_die->sibling;
3019eac3
DE
11202 }
11203 }
11204
11205 /* Decode macro information, if present. Dwarf 2 macro information
11206 refers to information in the line number info statement program
11207 header, so we can only read it if we've read the header
11208 successfully. */
0af92d60
JK
11209 attr = dwarf2_attr (die, DW_AT_macros, cu);
11210 if (attr == NULL)
11211 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
529908cb 11212 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3
DE
11213 {
11214 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11215 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11216
529908cb 11217 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
3019eac3
DE
11218 }
11219 else
11220 {
11221 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
529908cb 11222 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3 11223 {
529908cb 11224 unsigned int macro_offset = attr->as_unsigned ();
3019eac3 11225
43f3e411 11226 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11227 }
11228 }
3019eac3
DE
11229}
11230
c24bdb02
KS
11231void
11232dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11233{
f4dc4d17
DE
11234 struct type_unit_group *tu_group;
11235 int first_time;
3019eac3 11236 struct attribute *attr;
9c541725 11237 unsigned int i;
0186c6a7 11238 struct signatured_type *sig_type;
3019eac3 11239
f4dc4d17 11240 gdb_assert (per_cu->is_debug_types);
0186c6a7 11241 sig_type = (struct signatured_type *) per_cu;
3019eac3 11242
c24bdb02 11243 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11244
f4dc4d17 11245 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11246 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11247 if (sig_type->type_unit_group == NULL)
c24bdb02 11248 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11249 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11250
11251 /* If we've already processed this stmt_list there's no real need to
11252 do it again, we could fake it and just recreate the part we need
11253 (file name,index -> symtab mapping). If data shows this optimization
11254 is useful we can do it then. */
8adb8487
TT
11255 type_unit_group_unshareable *tug_unshare
11256 = per_objfile->get_type_unit_group_unshareable (tu_group);
11257 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11258
11259 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11260 debug info. */
fff8551c 11261 line_header_up lh;
d4df075e 11262 if (attr != NULL && attr->form_is_unsigned ())
3019eac3 11263 {
d4df075e 11264 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
c24bdb02 11265 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11266 }
11267 if (lh == NULL)
11268 {
11269 if (first_time)
c24bdb02 11270 start_symtab ("", NULL, 0);
f4dc4d17
DE
11271 else
11272 {
8adb8487 11273 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11274 gdb_assert (m_builder == nullptr);
8adb8487 11275 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11276 m_builder.reset (new struct buildsym_compunit
11277 (COMPUNIT_OBJFILE (cust), "",
11278 COMPUNIT_DIRNAME (cust),
11279 compunit_language (cust),
11280 0, cust));
770479f2 11281 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11282 }
f4dc4d17 11283 return;
3019eac3
DE
11284 }
11285
c24bdb02
KS
11286 line_header = lh.release ();
11287 line_header_die_owner = die;
3019eac3 11288
f4dc4d17
DE
11289 if (first_time)
11290 {
c24bdb02 11291 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11292
1fd60fc0
DE
11293 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11294 still initializing it, and our caller (a few levels up)
11295 process_full_type_unit still needs to know if this is the first
11296 time. */
11297
8adb8487 11298 tug_unshare->symtabs
4ac93832
TT
11299 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11300 struct symtab *, line_header->file_names_size ());
3019eac3 11301
7ba99d21
AT
11302 auto &file_names = line_header->file_names ();
11303 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11304 {
7ba99d21 11305 file_entry &fe = file_names[i];
c24bdb02
KS
11306 dwarf2_start_subfile (this, fe.name,
11307 fe.include_dir (line_header));
11308 buildsym_compunit *b = get_builder ();
11309 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11310 {
4c8aa72d
PA
11311 /* NOTE: start_subfile will recognize when it's been
11312 passed a file it has already seen. So we can't
11313 assume there's a simple mapping from
11314 cu->line_header->file_names to subfiles, plus
11315 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11316 b->get_current_subfile ()->symtab
11317 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11318 }
11319
c24bdb02 11320 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11321 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11322 }
11323 }
11324 else
3019eac3 11325 {
c24bdb02 11326 gdb_assert (m_builder == nullptr);
8adb8487 11327 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11328 m_builder.reset (new struct buildsym_compunit
11329 (COMPUNIT_OBJFILE (cust), "",
11330 COMPUNIT_DIRNAME (cust),
11331 compunit_language (cust),
11332 0, cust));
770479f2 11333 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11334
7ba99d21
AT
11335 auto &file_names = line_header->file_names ();
11336 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11337 {
7ba99d21 11338 file_entry &fe = file_names[i];
8adb8487 11339 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11340 }
3019eac3
DE
11341 }
11342
f4dc4d17
DE
11343 /* The main symtab is allocated last. Type units don't have DW_AT_name
11344 so they don't have a "real" (so to speak) symtab anyway.
11345 There is later code that will assign the main symtab to all symbols
11346 that don't have one. We need to handle the case of a symbol with a
11347 missing symtab (DW_AT_decl_file) anyway. */
11348}
3019eac3 11349
f4dc4d17
DE
11350/* Process DW_TAG_type_unit.
11351 For TUs we want to skip the first top level sibling if it's not the
11352 actual type being defined by this TU. In this case the first top
11353 level sibling is there to provide context only. */
3019eac3 11354
f4dc4d17
DE
11355static void
11356read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11357{
11358 struct die_info *child_die;
3019eac3 11359
f4dc4d17
DE
11360 prepare_one_comp_unit (cu, die, language_minimal);
11361
11362 /* Initialize (or reinitialize) the machinery for building symtabs.
11363 We do this before processing child DIEs, so that the line header table
11364 is available for DW_AT_decl_file. */
c24bdb02 11365 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11366
11367 if (die->child != NULL)
11368 {
11369 child_die = die->child;
11370 while (child_die && child_die->tag)
11371 {
11372 process_die (child_die, cu);
436c571c 11373 child_die = child_die->sibling;
f4dc4d17
DE
11374 }
11375 }
3019eac3
DE
11376}
11377\f
80626a55
DE
11378/* DWO/DWP files.
11379
11380 http://gcc.gnu.org/wiki/DebugFission
11381 http://gcc.gnu.org/wiki/DebugFissionDWP
11382
11383 To simplify handling of both DWO files ("object" files with the DWARF info)
11384 and DWP files (a file with the DWOs packaged up into one file), we treat
11385 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11386
11387static hashval_t
11388hash_dwo_file (const void *item)
11389{
9a3c8263 11390 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11391 hashval_t hash;
3019eac3 11392
a2ce51a0
DE
11393 hash = htab_hash_string (dwo_file->dwo_name);
11394 if (dwo_file->comp_dir != NULL)
11395 hash += htab_hash_string (dwo_file->comp_dir);
11396 return hash;
3019eac3
DE
11397}
11398
11399static int
11400eq_dwo_file (const void *item_lhs, const void *item_rhs)
11401{
9a3c8263
SM
11402 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11403 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11404
a2ce51a0
DE
11405 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11406 return 0;
11407 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11408 return lhs->comp_dir == rhs->comp_dir;
11409 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11410}
11411
11412/* Allocate a hash table for DWO files. */
11413
51ac9db5 11414static htab_up
298e9637 11415allocate_dwo_file_hash_table ()
3019eac3 11416{
51ac9db5
SM
11417 auto delete_dwo_file = [] (void *item)
11418 {
11419 struct dwo_file *dwo_file = (struct dwo_file *) item;
11420
11421 delete dwo_file;
11422 };
11423
bc68fb19
TT
11424 return htab_up (htab_create_alloc (41,
11425 hash_dwo_file,
11426 eq_dwo_file,
11427 delete_dwo_file,
11428 xcalloc, xfree));
3019eac3
DE
11429}
11430
80626a55
DE
11431/* Lookup DWO file DWO_NAME. */
11432
11433static void **
976ca316 11434lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11435 const char *dwo_name,
11436 const char *comp_dir)
80626a55
DE
11437{
11438 struct dwo_file find_entry;
11439 void **slot;
11440
976ca316
SM
11441 if (per_objfile->per_bfd->dwo_files == NULL)
11442 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11443
0ac5b59e
DE
11444 find_entry.dwo_name = dwo_name;
11445 find_entry.comp_dir = comp_dir;
976ca316 11446 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11447 INSERT);
80626a55
DE
11448
11449 return slot;
11450}
11451
3019eac3
DE
11452static hashval_t
11453hash_dwo_unit (const void *item)
11454{
9a3c8263 11455 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11456
11457 /* This drops the top 32 bits of the id, but is ok for a hash. */
11458 return dwo_unit->signature;
11459}
11460
11461static int
11462eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11463{
9a3c8263
SM
11464 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11465 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11466
11467 /* The signature is assumed to be unique within the DWO file.
11468 So while object file CU dwo_id's always have the value zero,
11469 that's OK, assuming each object file DWO file has only one CU,
11470 and that's the rule for now. */
11471 return lhs->signature == rhs->signature;
11472}
11473
11474/* Allocate a hash table for DWO CUs,TUs.
11475 There is one of these tables for each of CUs,TUs for each DWO file. */
11476
b0b6a987 11477static htab_up
298e9637 11478allocate_dwo_unit_table ()
3019eac3
DE
11479{
11480 /* Start out with a pretty small number.
11481 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11482 return htab_up (htab_create_alloc (3,
11483 hash_dwo_unit,
11484 eq_dwo_unit,
11485 NULL, xcalloc, xfree));
3019eac3
DE
11486}
11487
19c3d4c9 11488/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11489
11490static void
19c3d4c9
DE
11491create_dwo_cu_reader (const struct die_reader_specs *reader,
11492 const gdb_byte *info_ptr,
11493 struct die_info *comp_unit_die,
c0ab21c2
TT
11494 struct dwo_file *dwo_file,
11495 struct dwo_unit *dwo_unit)
3019eac3
DE
11496{
11497 struct dwarf2_cu *cu = reader->cu;
9c541725 11498 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11499 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11500
a084a2a6
AT
11501 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11502 if (!signature.has_value ())
3019eac3 11503 {
b98664d3 11504 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11505 " its dwo_id [in module %s]"),
9d8780f0 11506 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11507 return;
11508 }
11509
3019eac3 11510 dwo_unit->dwo_file = dwo_file;
a084a2a6 11511 dwo_unit->signature = *signature;
8a0459fd 11512 dwo_unit->section = section;
9c541725 11513 dwo_unit->sect_off = sect_off;
3019eac3
DE
11514 dwo_unit->length = cu->per_cu->length;
11515
6f738b01
SM
11516 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11517 sect_offset_str (sect_off),
11518 hex_string (dwo_unit->signature));
3019eac3
DE
11519}
11520
33c5cd75 11521/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11522 Note: This function processes DWO files only, not DWP files. */
3019eac3 11523
33c5cd75 11524static void
976ca316 11525create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11526 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11527 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11528{
976ca316
SM
11529 struct objfile *objfile = per_objfile->objfile;
11530 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11531 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11532
96b79293 11533 section.read (objfile);
33c5cd75 11534 info_ptr = section.buffer;
3019eac3
DE
11535
11536 if (info_ptr == NULL)
33c5cd75 11537 return;
3019eac3 11538
6f738b01
SM
11539 dwarf_read_debug_printf ("Reading %s for %s:",
11540 section.get_name (),
11541 section.get_file_name ());
3019eac3 11542
33c5cd75 11543 end_ptr = info_ptr + section.size;
3019eac3
DE
11544 while (info_ptr < end_ptr)
11545 {
11546 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11547 struct dwo_unit read_unit {};
33c5cd75
DB
11548 struct dwo_unit *dwo_unit;
11549 void **slot;
11550 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11551
11552 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11553 per_cu.per_bfd = per_bfd;
3019eac3 11554 per_cu.is_debug_types = 0;
33c5cd75
DB
11555 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11556 per_cu.section = &section;
11557
976ca316 11558 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11559 if (!reader.dummy_p)
11560 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11561 &dwo_file, &read_unit);
33c5cd75
DB
11562 info_ptr += per_cu.length;
11563
11564 // If the unit could not be parsed, skip it.
c0ab21c2 11565 if (read_unit.dwo_file == NULL)
33c5cd75 11566 continue;
3019eac3 11567
33c5cd75 11568 if (cus_htab == NULL)
298e9637 11569 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11570
1859c670 11571 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11572 struct dwo_unit);
c0ab21c2 11573 *dwo_unit = read_unit;
b0b6a987 11574 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11575 gdb_assert (slot != NULL);
11576 if (*slot != NULL)
19c3d4c9 11577 {
33c5cd75
DB
11578 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11579 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11580
b98664d3 11581 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11582 " the entry at offset %s, signature %s"),
11583 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11584 hex_string (dwo_unit->signature));
19c3d4c9 11585 }
33c5cd75 11586 *slot = (void *)dwo_unit;
3019eac3 11587 }
3019eac3
DE
11588}
11589
80626a55
DE
11590/* DWP file .debug_{cu,tu}_index section format:
11591 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
d2854d8d
CT
11592 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11593
11594 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11595 officially standard DWP format was published with DWARF v5 and is called
11596 Version 5. There are no versions 3 or 4.
80626a55 11597
d2415c6c
DE
11598 DWP Version 1:
11599
80626a55
DE
11600 Both index sections have the same format, and serve to map a 64-bit
11601 signature to a set of section numbers. Each section begins with a header,
11602 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11603 indexes, and a pool of 32-bit section numbers. The index sections will be
11604 aligned at 8-byte boundaries in the file.
11605
d2415c6c
DE
11606 The index section header consists of:
11607
11608 V, 32 bit version number
11609 -, 32 bits unused
11610 N, 32 bit number of compilation units or type units in the index
11611 M, 32 bit number of slots in the hash table
80626a55 11612
d2415c6c 11613 Numbers are recorded using the byte order of the application binary.
80626a55 11614
d2415c6c
DE
11615 The hash table begins at offset 16 in the section, and consists of an array
11616 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11617 order of the application binary). Unused slots in the hash table are 0.
11618 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11619
d2415c6c
DE
11620 The parallel table begins immediately after the hash table
11621 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11622 array of 32-bit indexes (using the byte order of the application binary),
11623 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11624 table contains a 32-bit index into the pool of section numbers. For unused
11625 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11626
73869dc2
DE
11627 The pool of section numbers begins immediately following the hash table
11628 (at offset 16 + 12 * M from the beginning of the section). The pool of
11629 section numbers consists of an array of 32-bit words (using the byte order
11630 of the application binary). Each item in the array is indexed starting
11631 from 0. The hash table entry provides the index of the first section
11632 number in the set. Additional section numbers in the set follow, and the
11633 set is terminated by a 0 entry (section number 0 is not used in ELF).
11634
11635 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11636 section must be the first entry in the set, and the .debug_abbrev.dwo must
11637 be the second entry. Other members of the set may follow in any order.
11638
11639 ---
11640
d2854d8d 11641 DWP Versions 2 and 5:
73869dc2 11642
d2854d8d 11643 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
73869dc2
DE
11644 and the entries in the index tables are now offsets into these sections.
11645 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11646 section.
11647
11648 Index Section Contents:
11649 Header
11650 Hash Table of Signatures dwp_hash_table.hash_table
11651 Parallel Table of Indices dwp_hash_table.unit_table
d2854d8d
CT
11652 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11653 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
73869dc2
DE
11654
11655 The index section header consists of:
11656
11657 V, 32 bit version number
11658 L, 32 bit number of columns in the table of section offsets
11659 N, 32 bit number of compilation units or type units in the index
11660 M, 32 bit number of slots in the hash table
11661
11662 Numbers are recorded using the byte order of the application binary.
11663
11664 The hash table has the same format as version 1.
11665 The parallel table of indices has the same format as version 1,
11666 except that the entries are origin-1 indices into the table of sections
11667 offsets and the table of section sizes.
11668
11669 The table of offsets begins immediately following the parallel table
11670 (at offset 16 + 12 * M from the beginning of the section). The table is
11671 a two-dimensional array of 32-bit words (using the byte order of the
11672 application binary), with L columns and N+1 rows, in row-major order.
11673 Each row in the array is indexed starting from 0. The first row provides
11674 a key to the remaining rows: each column in this row provides an identifier
11675 for a debug section, and the offsets in the same column of subsequent rows
d2854d8d 11676 refer to that section. The section identifiers for Version 2 are:
73869dc2
DE
11677
11678 DW_SECT_INFO 1 .debug_info.dwo
11679 DW_SECT_TYPES 2 .debug_types.dwo
11680 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11681 DW_SECT_LINE 4 .debug_line.dwo
11682 DW_SECT_LOC 5 .debug_loc.dwo
11683 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11684 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11685 DW_SECT_MACRO 8 .debug_macro.dwo
11686
d2854d8d
CT
11687 The section identifiers for Version 5 are:
11688
11689 DW_SECT_INFO_V5 1 .debug_info.dwo
11690 DW_SECT_RESERVED_V5 2 --
11691 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11692 DW_SECT_LINE_V5 4 .debug_line.dwo
11693 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11694 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11695 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11696 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11697
73869dc2
DE
11698 The offsets provided by the CU and TU index sections are the base offsets
11699 for the contributions made by each CU or TU to the corresponding section
11700 in the package file. Each CU and TU header contains an abbrev_offset
11701 field, used to find the abbreviations table for that CU or TU within the
11702 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11703 be interpreted as relative to the base offset given in the index section.
11704 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11705 should be interpreted as relative to the base offset for .debug_line.dwo,
11706 and offsets into other debug sections obtained from DWARF attributes should
11707 also be interpreted as relative to the corresponding base offset.
11708
11709 The table of sizes begins immediately following the table of offsets.
11710 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11711 with L columns and N rows, in row-major order. Each row in the array is
11712 indexed starting from 1 (row 0 is shared by the two tables).
11713
11714 ---
11715
11716 Hash table lookup is handled the same in version 1 and 2:
11717
11718 We assume that N and M will not exceed 2^32 - 1.
11719 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11720
d2415c6c
DE
11721 Given a 64-bit compilation unit signature or a type signature S, an entry
11722 in the hash table is located as follows:
80626a55 11723
d2415c6c
DE
11724 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11725 the low-order k bits all set to 1.
80626a55 11726
d2415c6c 11727 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11728
d2415c6c
DE
11729 3) If the hash table entry at index H matches the signature, use that
11730 entry. If the hash table entry at index H is unused (all zeroes),
11731 terminate the search: the signature is not present in the table.
80626a55 11732
d2415c6c 11733 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11734
d2415c6c 11735 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11736 to stop at an unused slot or find the match. */
80626a55
DE
11737
11738/* Create a hash table to map DWO IDs to their CU/TU entry in
11739 .debug_{info,types}.dwo in DWP_FILE.
11740 Returns NULL if there isn't one.
11741 Note: This function processes DWP files only, not DWO files. */
11742
11743static struct dwp_hash_table *
976ca316 11744create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11745 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11746{
976ca316 11747 struct objfile *objfile = per_objfile->objfile;
400174b1 11748 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11749 const gdb_byte *index_ptr, *index_end;
80626a55 11750 struct dwarf2_section_info *index;
73869dc2 11751 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11752 struct dwp_hash_table *htab;
11753
11754 if (is_debug_types)
11755 index = &dwp_file->sections.tu_index;
11756 else
11757 index = &dwp_file->sections.cu_index;
11758
96b79293 11759 if (index->empty ())
80626a55 11760 return NULL;
96b79293 11761 index->read (objfile);
80626a55
DE
11762
11763 index_ptr = index->buffer;
11764 index_end = index_ptr + index->size;
11765
d2854d8d
CT
11766 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11767 For now it's safe to just read 4 bytes (particularly as it's difficult to
11768 tell if you're dealing with Version 5 before you've read the version). */
80626a55 11769 version = read_4_bytes (dbfd, index_ptr);
73869dc2 11770 index_ptr += 4;
d2854d8d 11771 if (version == 2 || version == 5)
73869dc2
DE
11772 nr_columns = read_4_bytes (dbfd, index_ptr);
11773 else
11774 nr_columns = 0;
11775 index_ptr += 4;
80626a55
DE
11776 nr_units = read_4_bytes (dbfd, index_ptr);
11777 index_ptr += 4;
11778 nr_slots = read_4_bytes (dbfd, index_ptr);
11779 index_ptr += 4;
11780
d2854d8d 11781 if (version != 1 && version != 2 && version != 5)
80626a55 11782 {
21aa081e 11783 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11784 " [in module %s]"),
21aa081e 11785 pulongest (version), dwp_file->name);
80626a55
DE
11786 }
11787 if (nr_slots != (nr_slots & -nr_slots))
11788 {
21aa081e 11789 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11790 " is not power of 2 [in module %s]"),
21aa081e 11791 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11792 }
11793
976ca316 11794 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11795 htab->version = version;
11796 htab->nr_columns = nr_columns;
80626a55
DE
11797 htab->nr_units = nr_units;
11798 htab->nr_slots = nr_slots;
11799 htab->hash_table = index_ptr;
11800 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11801
11802 /* Exit early if the table is empty. */
11803 if (nr_slots == 0 || nr_units == 0
d2854d8d
CT
11804 || (version == 2 && nr_columns == 0)
11805 || (version == 5 && nr_columns == 0))
73869dc2
DE
11806 {
11807 /* All must be zero. */
11808 if (nr_slots != 0 || nr_units != 0
d2854d8d
CT
11809 || (version == 2 && nr_columns != 0)
11810 || (version == 5 && nr_columns != 0))
73869dc2 11811 {
b98664d3 11812 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11813 " all zero [in modules %s]"),
11814 dwp_file->name);
11815 }
11816 return htab;
11817 }
11818
11819 if (version == 1)
11820 {
11821 htab->section_pool.v1.indices =
11822 htab->unit_table + sizeof (uint32_t) * nr_slots;
11823 /* It's harder to decide whether the section is too small in v1.
11824 V1 is deprecated anyway so we punt. */
11825 }
d2854d8d 11826 else if (version == 2)
73869dc2
DE
11827 {
11828 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11829 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11830 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11831 /* Reverse map for error checking. */
11832 int ids_seen[DW_SECT_MAX + 1];
11833 int i;
11834
11835 if (nr_columns < 2)
11836 {
11837 error (_("Dwarf Error: bad DWP hash table, too few columns"
11838 " in section table [in module %s]"),
11839 dwp_file->name);
11840 }
11841 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11842 {
11843 error (_("Dwarf Error: bad DWP hash table, too many columns"
11844 " in section table [in module %s]"),
11845 dwp_file->name);
11846 }
04fd5eed
GB
11847 memset (ids, 255, sizeof_ids);
11848 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11849 for (i = 0; i < nr_columns; ++i)
11850 {
11851 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11852
11853 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11854 {
11855 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11856 " in section table [in module %s]"),
11857 id, dwp_file->name);
11858 }
11859 if (ids_seen[id] != -1)
11860 {
11861 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11862 " id %d in section table [in module %s]"),
11863 id, dwp_file->name);
11864 }
11865 ids_seen[id] = i;
11866 ids[i] = id;
11867 }
11868 /* Must have exactly one info or types section. */
11869 if (((ids_seen[DW_SECT_INFO] != -1)
11870 + (ids_seen[DW_SECT_TYPES] != -1))
11871 != 1)
11872 {
11873 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11874 " DWO info/types section [in module %s]"),
11875 dwp_file->name);
11876 }
11877 /* Must have an abbrev section. */
11878 if (ids_seen[DW_SECT_ABBREV] == -1)
11879 {
11880 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11881 " section [in module %s]"),
11882 dwp_file->name);
11883 }
11884 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11885 htab->section_pool.v2.sizes =
11886 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11887 * nr_units * nr_columns);
11888 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11889 * nr_units * nr_columns))
11890 > index_end)
11891 {
11892 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11893 " [in module %s]"),
11894 dwp_file->name);
11895 }
11896 }
d2854d8d
CT
11897 else /* version == 5 */
11898 {
11899 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11900 int *ids = htab->section_pool.v5.section_ids;
11901 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11902 /* Reverse map for error checking. */
11903 int ids_seen[DW_SECT_MAX_V5 + 1];
11904
11905 if (nr_columns < 2)
11906 {
11907 error (_("Dwarf Error: bad DWP hash table, too few columns"
11908 " in section table [in module %s]"),
11909 dwp_file->name);
11910 }
11911 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11912 {
11913 error (_("Dwarf Error: bad DWP hash table, too many columns"
11914 " in section table [in module %s]"),
11915 dwp_file->name);
11916 }
11917 memset (ids, 255, sizeof_ids);
11918 memset (ids_seen, 255, sizeof (ids_seen));
11919 for (int i = 0; i < nr_columns; ++i)
11920 {
11921 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11922
11923 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11924 {
11925 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11926 " in section table [in module %s]"),
11927 id, dwp_file->name);
11928 }
11929 if (ids_seen[id] != -1)
11930 {
11931 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11932 " id %d in section table [in module %s]"),
11933 id, dwp_file->name);
11934 }
11935 ids_seen[id] = i;
11936 ids[i] = id;
11937 }
11938 /* Must have seen an info section. */
11939 if (ids_seen[DW_SECT_INFO_V5] == -1)
11940 {
11941 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11942 " DWO info/types section [in module %s]"),
11943 dwp_file->name);
11944 }
11945 /* Must have an abbrev section. */
11946 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11947 {
11948 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11949 " section [in module %s]"),
11950 dwp_file->name);
11951 }
11952 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11953 htab->section_pool.v5.sizes
11954 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11955 * nr_units * nr_columns);
11956 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11957 * nr_units * nr_columns))
11958 > index_end)
11959 {
11960 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11961 " [in module %s]"),
11962 dwp_file->name);
11963 }
11964 }
80626a55
DE
11965
11966 return htab;
11967}
11968
11969/* Update SECTIONS with the data from SECTP.
11970
5bb6e9dd
TT
11971 This function is like the other "locate" section routines, but in
11972 this context the sections to read comes from the DWP V1 hash table,
11973 not the full ELF section table.
80626a55
DE
11974
11975 The result is non-zero for success, or zero if an error was found. */
11976
11977static int
73869dc2
DE
11978locate_v1_virtual_dwo_sections (asection *sectp,
11979 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11980{
11981 const struct dwop_section_names *names = &dwop_section_names;
11982
fbedd546 11983 if (names->abbrev_dwo.matches (sectp->name))
80626a55
DE
11984 {
11985 /* There can be only one. */
049412e3 11986 if (sections->abbrev.s.section != NULL)
80626a55 11987 return 0;
049412e3 11988 sections->abbrev.s.section = sectp;
fd361982 11989 sections->abbrev.size = bfd_section_size (sectp);
80626a55 11990 }
fbedd546
TT
11991 else if (names->info_dwo.matches (sectp->name)
11992 || names->types_dwo.matches (sectp->name))
80626a55
DE
11993 {
11994 /* There can be only one. */
049412e3 11995 if (sections->info_or_types.s.section != NULL)
80626a55 11996 return 0;
049412e3 11997 sections->info_or_types.s.section = sectp;
fd361982 11998 sections->info_or_types.size = bfd_section_size (sectp);
80626a55 11999 }
fbedd546 12000 else if (names->line_dwo.matches (sectp->name))
80626a55
DE
12001 {
12002 /* There can be only one. */
049412e3 12003 if (sections->line.s.section != NULL)
80626a55 12004 return 0;
049412e3 12005 sections->line.s.section = sectp;
fd361982 12006 sections->line.size = bfd_section_size (sectp);
80626a55 12007 }
fbedd546 12008 else if (names->loc_dwo.matches (sectp->name))
80626a55
DE
12009 {
12010 /* There can be only one. */
049412e3 12011 if (sections->loc.s.section != NULL)
80626a55 12012 return 0;
049412e3 12013 sections->loc.s.section = sectp;
fd361982 12014 sections->loc.size = bfd_section_size (sectp);
80626a55 12015 }
fbedd546 12016 else if (names->macinfo_dwo.matches (sectp->name))
80626a55
DE
12017 {
12018 /* There can be only one. */
049412e3 12019 if (sections->macinfo.s.section != NULL)
80626a55 12020 return 0;
049412e3 12021 sections->macinfo.s.section = sectp;
fd361982 12022 sections->macinfo.size = bfd_section_size (sectp);
80626a55 12023 }
fbedd546 12024 else if (names->macro_dwo.matches (sectp->name))
80626a55
DE
12025 {
12026 /* There can be only one. */
049412e3 12027 if (sections->macro.s.section != NULL)
80626a55 12028 return 0;
049412e3 12029 sections->macro.s.section = sectp;
fd361982 12030 sections->macro.size = bfd_section_size (sectp);
80626a55 12031 }
fbedd546 12032 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55
DE
12033 {
12034 /* There can be only one. */
049412e3 12035 if (sections->str_offsets.s.section != NULL)
80626a55 12036 return 0;
049412e3 12037 sections->str_offsets.s.section = sectp;
fd361982 12038 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12039 }
12040 else
12041 {
12042 /* No other kind of section is valid. */
12043 return 0;
12044 }
12045
12046 return 1;
12047}
12048
73869dc2
DE
12049/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12050 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12051 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12052 This is for DWP version 1 files. */
80626a55
DE
12053
12054static struct dwo_unit *
976ca316 12055create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 12056 struct dwp_file *dwp_file,
73869dc2
DE
12057 uint32_t unit_index,
12058 const char *comp_dir,
12059 ULONGEST signature, int is_debug_types)
80626a55 12060{
73869dc2
DE
12061 const struct dwp_hash_table *dwp_htab =
12062 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12063 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12064 const char *kind = is_debug_types ? "TU" : "CU";
12065 struct dwo_file *dwo_file;
12066 struct dwo_unit *dwo_unit;
73869dc2 12067 struct virtual_v1_dwo_sections sections;
80626a55 12068 void **dwo_file_slot;
80626a55
DE
12069 int i;
12070
73869dc2
DE
12071 gdb_assert (dwp_file->version == 1);
12072
6f738b01
SM
12073 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12074 kind, pulongest (unit_index), hex_string (signature),
12075 dwp_file->name);
80626a55 12076
19ac8c2e 12077 /* Fetch the sections of this DWO unit.
80626a55
DE
12078 Put a limit on the number of sections we look for so that bad data
12079 doesn't cause us to loop forever. */
12080
73869dc2 12081#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12082 (1 /* .debug_info or .debug_types */ \
12083 + 1 /* .debug_abbrev */ \
12084 + 1 /* .debug_line */ \
12085 + 1 /* .debug_loc */ \
12086 + 1 /* .debug_str_offsets */ \
19ac8c2e 12087 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12088 + 1 /* trailing zero */)
12089
12090 memset (&sections, 0, sizeof (sections));
80626a55 12091
73869dc2 12092 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12093 {
12094 asection *sectp;
12095 uint32_t section_nr =
12096 read_4_bytes (dbfd,
73869dc2
DE
12097 dwp_htab->section_pool.v1.indices
12098 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12099
12100 if (section_nr == 0)
12101 break;
12102 if (section_nr >= dwp_file->num_sections)
12103 {
12104 error (_("Dwarf Error: bad DWP hash table, section number too large"
12105 " [in module %s]"),
12106 dwp_file->name);
12107 }
12108
12109 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12110 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12111 {
12112 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12113 " [in module %s]"),
12114 dwp_file->name);
12115 }
12116 }
12117
12118 if (i < 2
96b79293
TT
12119 || sections.info_or_types.empty ()
12120 || sections.abbrev.empty ())
80626a55
DE
12121 {
12122 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12123 " [in module %s]"),
12124 dwp_file->name);
12125 }
73869dc2 12126 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12127 {
12128 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12129 " [in module %s]"),
12130 dwp_file->name);
12131 }
12132
12133 /* It's easier for the rest of the code if we fake a struct dwo_file and
12134 have dwo_unit "live" in that. At least for now.
12135
12136 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12137 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12138 file, we can combine them back into a virtual DWO file to save space
12139 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12140 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12141
791afaa2
TT
12142 std::string virtual_dwo_name =
12143 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
12144 sections.abbrev.get_id (),
12145 sections.line.get_id (),
12146 sections.loc.get_id (),
12147 sections.str_offsets.get_id ());
80626a55 12148 /* Can we use an existing virtual DWO file? */
976ca316 12149 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12150 comp_dir);
80626a55
DE
12151 /* Create one if necessary. */
12152 if (*dwo_file_slot == NULL)
12153 {
6f738b01
SM
12154 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12155 virtual_dwo_name.c_str ());
12156
51ac9db5 12157 dwo_file = new struct dwo_file;
976ca316 12158 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 12159 dwo_file->comp_dir = comp_dir;
80626a55
DE
12160 dwo_file->sections.abbrev = sections.abbrev;
12161 dwo_file->sections.line = sections.line;
12162 dwo_file->sections.loc = sections.loc;
12163 dwo_file->sections.macinfo = sections.macinfo;
12164 dwo_file->sections.macro = sections.macro;
12165 dwo_file->sections.str_offsets = sections.str_offsets;
12166 /* The "str" section is global to the entire DWP file. */
12167 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12168 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12169 there's no need to record it in dwo_file.
12170 Also, we can't simply record type sections in dwo_file because
12171 we record a pointer into the vector in dwo_unit. As we collect more
12172 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12173 for it, invalidating all copies of pointers into the previous
12174 contents. */
80626a55
DE
12175 *dwo_file_slot = dwo_file;
12176 }
12177 else
12178 {
6f738b01
SM
12179 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12180 virtual_dwo_name.c_str ());
12181
9a3c8263 12182 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12183 }
80626a55 12184
976ca316 12185 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
12186 dwo_unit->dwo_file = dwo_file;
12187 dwo_unit->signature = signature;
8d749320 12188 dwo_unit->section =
976ca316 12189 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 12190 *dwo_unit->section = sections.info_or_types;
57d63ce2 12191 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12192
12193 return dwo_unit;
12194}
12195
d2854d8d
CT
12196/* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12197 simplify them. Given a pointer to the containing section SECTION, and
12198 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12199 virtual section of just that piece. */
73869dc2
DE
12200
12201static struct dwarf2_section_info
d2854d8d
CT
12202create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12203 struct dwarf2_section_info *section,
12204 bfd_size_type offset, bfd_size_type size)
73869dc2
DE
12205{
12206 struct dwarf2_section_info result;
12207 asection *sectp;
12208
12209 gdb_assert (section != NULL);
12210 gdb_assert (!section->is_virtual);
12211
12212 memset (&result, 0, sizeof (result));
12213 result.s.containing_section = section;
dc4ccb6f 12214 result.is_virtual = true;
73869dc2
DE
12215
12216 if (size == 0)
12217 return result;
12218
96b79293 12219 sectp = section->get_bfd_section ();
73869dc2
DE
12220
12221 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12222 bounds of the real section. This is a pretty-rare event, so just
12223 flag an error (easier) instead of a warning and trying to cope. */
12224 if (sectp == NULL
fd361982 12225 || offset + size > bfd_section_size (sectp))
73869dc2 12226 {
d2854d8d 12227 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
73869dc2 12228 " in section %s [in module %s]"),
fd361982 12229 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 12230 objfile_name (per_objfile->objfile));
73869dc2
DE
12231 }
12232
12233 result.virtual_offset = offset;
12234 result.size = size;
12235 return result;
12236}
12237
12238/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12239 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12240 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12241 This is for DWP version 2 files. */
12242
12243static struct dwo_unit *
976ca316 12244create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 12245 struct dwp_file *dwp_file,
73869dc2
DE
12246 uint32_t unit_index,
12247 const char *comp_dir,
12248 ULONGEST signature, int is_debug_types)
12249{
73869dc2
DE
12250 const struct dwp_hash_table *dwp_htab =
12251 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12252 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12253 const char *kind = is_debug_types ? "TU" : "CU";
12254 struct dwo_file *dwo_file;
12255 struct dwo_unit *dwo_unit;
d2854d8d 12256 struct virtual_v2_or_v5_dwo_sections sections;
73869dc2 12257 void **dwo_file_slot;
73869dc2
DE
12258 int i;
12259
12260 gdb_assert (dwp_file->version == 2);
12261
6f738b01
SM
12262 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12263 kind, pulongest (unit_index), hex_string (signature),
12264 dwp_file->name);
73869dc2
DE
12265
12266 /* Fetch the section offsets of this DWO unit. */
12267
12268 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12269
12270 for (i = 0; i < dwp_htab->nr_columns; ++i)
12271 {
12272 uint32_t offset = read_4_bytes (dbfd,
12273 dwp_htab->section_pool.v2.offsets
12274 + (((unit_index - 1) * dwp_htab->nr_columns
12275 + i)
12276 * sizeof (uint32_t)));
12277 uint32_t size = read_4_bytes (dbfd,
12278 dwp_htab->section_pool.v2.sizes
12279 + (((unit_index - 1) * dwp_htab->nr_columns
12280 + i)
12281 * sizeof (uint32_t)));
12282
12283 switch (dwp_htab->section_pool.v2.section_ids[i])
12284 {
12285 case DW_SECT_INFO:
12286 case DW_SECT_TYPES:
12287 sections.info_or_types_offset = offset;
12288 sections.info_or_types_size = size;
12289 break;
12290 case DW_SECT_ABBREV:
12291 sections.abbrev_offset = offset;
12292 sections.abbrev_size = size;
12293 break;
12294 case DW_SECT_LINE:
12295 sections.line_offset = offset;
12296 sections.line_size = size;
12297 break;
12298 case DW_SECT_LOC:
12299 sections.loc_offset = offset;
12300 sections.loc_size = size;
12301 break;
12302 case DW_SECT_STR_OFFSETS:
12303 sections.str_offsets_offset = offset;
12304 sections.str_offsets_size = size;
12305 break;
12306 case DW_SECT_MACINFO:
12307 sections.macinfo_offset = offset;
12308 sections.macinfo_size = size;
12309 break;
12310 case DW_SECT_MACRO:
12311 sections.macro_offset = offset;
12312 sections.macro_size = size;
12313 break;
12314 }
12315 }
12316
12317 /* It's easier for the rest of the code if we fake a struct dwo_file and
12318 have dwo_unit "live" in that. At least for now.
12319
12320 The DWP file can be made up of a random collection of CUs and TUs.
12321 However, for each CU + set of TUs that came from the same original DWO
12322 file, we can combine them back into a virtual DWO file to save space
12323 (fewer struct dwo_file objects to allocate). Remember that for really
12324 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12325
791afaa2
TT
12326 std::string virtual_dwo_name =
12327 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12328 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12329 (long) (sections.line_size ? sections.line_offset : 0),
12330 (long) (sections.loc_size ? sections.loc_offset : 0),
12331 (long) (sections.str_offsets_size
12332 ? sections.str_offsets_offset : 0));
73869dc2 12333 /* Can we use an existing virtual DWO file? */
976ca316 12334 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12335 comp_dir);
73869dc2
DE
12336 /* Create one if necessary. */
12337 if (*dwo_file_slot == NULL)
12338 {
6f738b01
SM
12339 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12340 virtual_dwo_name.c_str ());
12341
51ac9db5 12342 dwo_file = new struct dwo_file;
976ca316 12343 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12344 dwo_file->comp_dir = comp_dir;
12345 dwo_file->sections.abbrev =
d2854d8d
CT
12346 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12347 sections.abbrev_offset,
12348 sections.abbrev_size);
73869dc2 12349 dwo_file->sections.line =
d2854d8d
CT
12350 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12351 sections.line_offset,
12352 sections.line_size);
73869dc2 12353 dwo_file->sections.loc =
d2854d8d
CT
12354 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12355 sections.loc_offset, sections.loc_size);
73869dc2 12356 dwo_file->sections.macinfo =
d2854d8d
CT
12357 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12358 sections.macinfo_offset,
12359 sections.macinfo_size);
73869dc2 12360 dwo_file->sections.macro =
d2854d8d
CT
12361 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12362 sections.macro_offset,
12363 sections.macro_size);
73869dc2 12364 dwo_file->sections.str_offsets =
d2854d8d
CT
12365 create_dwp_v2_or_v5_section (per_objfile,
12366 &dwp_file->sections.str_offsets,
12367 sections.str_offsets_offset,
12368 sections.str_offsets_size);
73869dc2
DE
12369 /* The "str" section is global to the entire DWP file. */
12370 dwo_file->sections.str = dwp_file->sections.str;
12371 /* The info or types section is assigned below to dwo_unit,
12372 there's no need to record it in dwo_file.
12373 Also, we can't simply record type sections in dwo_file because
12374 we record a pointer into the vector in dwo_unit. As we collect more
12375 types we'll grow the vector and eventually have to reallocate space
12376 for it, invalidating all copies of pointers into the previous
12377 contents. */
12378 *dwo_file_slot = dwo_file;
12379 }
12380 else
12381 {
6f738b01
SM
12382 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12383 virtual_dwo_name.c_str ());
12384
9a3c8263 12385 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12386 }
73869dc2 12387
976ca316 12388 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12389 dwo_unit->dwo_file = dwo_file;
12390 dwo_unit->signature = signature;
8d749320 12391 dwo_unit->section =
976ca316 12392 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
d2854d8d 12393 *dwo_unit->section = create_dwp_v2_or_v5_section
dda83cd7 12394 (per_objfile,
d2854d8d
CT
12395 is_debug_types
12396 ? &dwp_file->sections.types
12397 : &dwp_file->sections.info,
12398 sections.info_or_types_offset,
12399 sections.info_or_types_size);
12400 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12401
12402 return dwo_unit;
12403}
12404
12405/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12406 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12407 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12408 This is for DWP version 5 files. */
12409
12410static struct dwo_unit *
12411create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12412 struct dwp_file *dwp_file,
12413 uint32_t unit_index,
12414 const char *comp_dir,
12415 ULONGEST signature, int is_debug_types)
12416{
12417 const struct dwp_hash_table *dwp_htab
12418 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12419 bfd *dbfd = dwp_file->dbfd.get ();
12420 const char *kind = is_debug_types ? "TU" : "CU";
12421 struct dwo_file *dwo_file;
12422 struct dwo_unit *dwo_unit;
12423 struct virtual_v2_or_v5_dwo_sections sections {};
12424 void **dwo_file_slot;
12425
12426 gdb_assert (dwp_file->version == 5);
12427
6f738b01
SM
12428 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12429 kind, pulongest (unit_index), hex_string (signature),
12430 dwp_file->name);
d2854d8d
CT
12431
12432 /* Fetch the section offsets of this DWO unit. */
12433
12434 /* memset (&sections, 0, sizeof (sections)); */
12435
12436 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12437 {
12438 uint32_t offset = read_4_bytes (dbfd,
dda83cd7
SM
12439 dwp_htab->section_pool.v5.offsets
12440 + (((unit_index - 1)
12441 * dwp_htab->nr_columns
12442 + i)
12443 * sizeof (uint32_t)));
d2854d8d 12444 uint32_t size = read_4_bytes (dbfd,
dda83cd7
SM
12445 dwp_htab->section_pool.v5.sizes
12446 + (((unit_index - 1) * dwp_htab->nr_columns
12447 + i)
12448 * sizeof (uint32_t)));
d2854d8d
CT
12449
12450 switch (dwp_htab->section_pool.v5.section_ids[i])
dda83cd7
SM
12451 {
12452 case DW_SECT_ABBREV_V5:
12453 sections.abbrev_offset = offset;
12454 sections.abbrev_size = size;
12455 break;
12456 case DW_SECT_INFO_V5:
12457 sections.info_or_types_offset = offset;
12458 sections.info_or_types_size = size;
12459 break;
12460 case DW_SECT_LINE_V5:
12461 sections.line_offset = offset;
12462 sections.line_size = size;
12463 break;
12464 case DW_SECT_LOCLISTS_V5:
12465 sections.loclists_offset = offset;
12466 sections.loclists_size = size;
12467 break;
12468 case DW_SECT_MACRO_V5:
12469 sections.macro_offset = offset;
12470 sections.macro_size = size;
12471 break;
12472 case DW_SECT_RNGLISTS_V5:
12473 sections.rnglists_offset = offset;
12474 sections.rnglists_size = size;
12475 break;
12476 case DW_SECT_STR_OFFSETS_V5:
12477 sections.str_offsets_offset = offset;
12478 sections.str_offsets_size = size;
12479 break;
12480 case DW_SECT_RESERVED_V5:
12481 default:
12482 break;
12483 }
d2854d8d
CT
12484 }
12485
12486 /* It's easier for the rest of the code if we fake a struct dwo_file and
12487 have dwo_unit "live" in that. At least for now.
12488
12489 The DWP file can be made up of a random collection of CUs and TUs.
12490 However, for each CU + set of TUs that came from the same original DWO
12491 file, we can combine them back into a virtual DWO file to save space
12492 (fewer struct dwo_file objects to allocate). Remember that for really
12493 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12494
12495 std::string virtual_dwo_name =
12496 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
dda83cd7
SM
12497 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12498 (long) (sections.line_size ? sections.line_offset : 0),
12499 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12500 (long) (sections.str_offsets_size
12501 ? sections.str_offsets_offset : 0),
12502 (long) (sections.macro_size ? sections.macro_offset : 0),
12503 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
d2854d8d
CT
12504 /* Can we use an existing virtual DWO file? */
12505 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
dda83cd7
SM
12506 virtual_dwo_name.c_str (),
12507 comp_dir);
d2854d8d
CT
12508 /* Create one if necessary. */
12509 if (*dwo_file_slot == NULL)
12510 {
6f738b01
SM
12511 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12512 virtual_dwo_name.c_str ());
12513
d2854d8d
CT
12514 dwo_file = new struct dwo_file;
12515 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12516 dwo_file->comp_dir = comp_dir;
12517 dwo_file->sections.abbrev =
dda83cd7
SM
12518 create_dwp_v2_or_v5_section (per_objfile,
12519 &dwp_file->sections.abbrev,
12520 sections.abbrev_offset,
12521 sections.abbrev_size);
d2854d8d 12522 dwo_file->sections.line =
dda83cd7
SM
12523 create_dwp_v2_or_v5_section (per_objfile,
12524 &dwp_file->sections.line,
12525 sections.line_offset, sections.line_size);
d2854d8d 12526 dwo_file->sections.macro =
dda83cd7
SM
12527 create_dwp_v2_or_v5_section (per_objfile,
12528 &dwp_file->sections.macro,
12529 sections.macro_offset,
12530 sections.macro_size);
d2854d8d 12531 dwo_file->sections.loclists =
dda83cd7
SM
12532 create_dwp_v2_or_v5_section (per_objfile,
12533 &dwp_file->sections.loclists,
12534 sections.loclists_offset,
12535 sections.loclists_size);
d2854d8d 12536 dwo_file->sections.rnglists =
dda83cd7
SM
12537 create_dwp_v2_or_v5_section (per_objfile,
12538 &dwp_file->sections.rnglists,
12539 sections.rnglists_offset,
12540 sections.rnglists_size);
d2854d8d 12541 dwo_file->sections.str_offsets =
dda83cd7
SM
12542 create_dwp_v2_or_v5_section (per_objfile,
12543 &dwp_file->sections.str_offsets,
12544 sections.str_offsets_offset,
12545 sections.str_offsets_size);
d2854d8d
CT
12546 /* The "str" section is global to the entire DWP file. */
12547 dwo_file->sections.str = dwp_file->sections.str;
12548 /* The info or types section is assigned below to dwo_unit,
dda83cd7
SM
12549 there's no need to record it in dwo_file.
12550 Also, we can't simply record type sections in dwo_file because
12551 we record a pointer into the vector in dwo_unit. As we collect more
12552 types we'll grow the vector and eventually have to reallocate space
12553 for it, invalidating all copies of pointers into the previous
12554 contents. */
d2854d8d
CT
12555 *dwo_file_slot = dwo_file;
12556 }
12557 else
12558 {
6f738b01
SM
12559 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12560 virtual_dwo_name.c_str ());
12561
d2854d8d
CT
12562 dwo_file = (struct dwo_file *) *dwo_file_slot;
12563 }
12564
12565 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12566 dwo_unit->dwo_file = dwo_file;
12567 dwo_unit->signature = signature;
12568 dwo_unit->section
12569 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12570 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
dda83cd7
SM
12571 &dwp_file->sections.info,
12572 sections.info_or_types_offset,
12573 sections.info_or_types_size);
73869dc2
DE
12574 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12575
12576 return dwo_unit;
12577}
12578
57d63ce2
DE
12579/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12580 Returns NULL if the signature isn't found. */
80626a55
DE
12581
12582static struct dwo_unit *
976ca316 12583lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12584 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12585 ULONGEST signature, int is_debug_types)
80626a55 12586{
57d63ce2
DE
12587 const struct dwp_hash_table *dwp_htab =
12588 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12589 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12590 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12591 uint32_t hash = signature & mask;
12592 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12593 unsigned int i;
12594 void **slot;
870f88f7 12595 struct dwo_unit find_dwo_cu;
80626a55
DE
12596
12597 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12598 find_dwo_cu.signature = signature;
19ac8c2e 12599 slot = htab_find_slot (is_debug_types
48b490f2
TT
12600 ? dwp_file->loaded_tus.get ()
12601 : dwp_file->loaded_cus.get (),
19ac8c2e 12602 &find_dwo_cu, INSERT);
80626a55
DE
12603
12604 if (*slot != NULL)
9a3c8263 12605 return (struct dwo_unit *) *slot;
80626a55
DE
12606
12607 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12608 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12609 {
12610 ULONGEST signature_in_table;
12611
12612 signature_in_table =
57d63ce2 12613 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12614 if (signature_in_table == signature)
12615 {
57d63ce2
DE
12616 uint32_t unit_index =
12617 read_4_bytes (dbfd,
12618 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12619
73869dc2
DE
12620 if (dwp_file->version == 1)
12621 {
976ca316
SM
12622 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12623 unit_index, comp_dir,
12624 signature, is_debug_types);
73869dc2 12625 }
d2854d8d 12626 else if (dwp_file->version == 2)
73869dc2 12627 {
976ca316
SM
12628 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12629 unit_index, comp_dir,
12630 signature, is_debug_types);
73869dc2 12631 }
d2854d8d
CT
12632 else /* version == 5 */
12633 {
12634 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12635 unit_index, comp_dir,
12636 signature, is_debug_types);
12637 }
9a3c8263 12638 return (struct dwo_unit *) *slot;
80626a55
DE
12639 }
12640 if (signature_in_table == 0)
12641 return NULL;
12642 hash = (hash + hash2) & mask;
12643 }
12644
12645 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12646 " [in module %s]"),
12647 dwp_file->name);
12648}
12649
ab5088bf 12650/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12651 Open the file specified by FILE_NAME and hand it off to BFD for
12652 preliminary analysis. Return a newly initialized bfd *, which
12653 includes a canonicalized copy of FILE_NAME.
80626a55 12654 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12655 SEARCH_CWD is true if the current directory is to be searched.
12656 It will be searched before debug-file-directory.
13aaf454
DE
12657 If successful, the file is added to the bfd include table of the
12658 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12659 If unable to find/open the file, return NULL.
3019eac3
DE
12660 NOTE: This function is derived from symfile_bfd_open. */
12661
192b62ce 12662static gdb_bfd_ref_ptr
976ca316 12663try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12664 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12665{
24b9144d 12666 int desc;
9c02c129
DE
12667 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12668 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12669 to debug_file_directory. */
e0cc99a6 12670 const char *search_path;
9c02c129
DE
12671 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12672
e0cc99a6 12673 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12674 if (search_cwd)
12675 {
12676 if (*debug_file_directory != '\0')
e0cc99a6
TT
12677 {
12678 search_path_holder.reset (concat (".", dirname_separator_string,
12679 debug_file_directory,
12680 (char *) NULL));
12681 search_path = search_path_holder.get ();
12682 }
6ac97d4c 12683 else
e0cc99a6 12684 search_path = ".";
6ac97d4c 12685 }
9c02c129 12686 else
e0cc99a6 12687 search_path = debug_file_directory;
3019eac3 12688
24b9144d 12689 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12690 if (is_dwp)
12691 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12692
12693 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12694 desc = openp (search_path, flags, file_name,
3019eac3
DE
12695 O_RDONLY | O_BINARY, &absolute_name);
12696 if (desc < 0)
12697 return NULL;
12698
e0cc99a6
TT
12699 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12700 gnutarget, desc));
9c02c129
DE
12701 if (sym_bfd == NULL)
12702 return NULL;
192b62ce 12703 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12704
192b62ce
TT
12705 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12706 return NULL;
3019eac3 12707
13aaf454
DE
12708 /* Success. Record the bfd as having been included by the objfile's bfd.
12709 This is important because things like demangled_names_hash lives in the
12710 objfile's per_bfd space and may have references to things like symbol
12711 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12712 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12713
3019eac3
DE
12714 return sym_bfd;
12715}
12716
ab5088bf 12717/* Try to open DWO file FILE_NAME.
3019eac3
DE
12718 COMP_DIR is the DW_AT_comp_dir attribute.
12719 The result is the bfd handle of the file.
12720 If there is a problem finding or opening the file, return NULL.
12721 Upon success, the canonicalized path of the file is stored in the bfd,
12722 same as symfile_bfd_open. */
12723
192b62ce 12724static gdb_bfd_ref_ptr
976ca316 12725open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12726 const char *file_name, const char *comp_dir)
3019eac3 12727{
80626a55 12728 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12729 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12730 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12731
12732 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12733
12734 if (comp_dir != NULL)
12735 {
43816ebc
TT
12736 gdb::unique_xmalloc_ptr<char> path_to_try
12737 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12738
12739 /* NOTE: If comp_dir is a relative path, this will also try the
12740 search path, which seems useful. */
976ca316 12741 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12742 0 /*is_dwp*/,
192b62ce 12743 1 /*search_cwd*/));
3019eac3
DE
12744 if (abfd != NULL)
12745 return abfd;
12746 }
12747
12748 /* That didn't work, try debug-file-directory, which, despite its name,
12749 is a list of paths. */
12750
12751 if (*debug_file_directory == '\0')
12752 return NULL;
12753
976ca316 12754 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12755 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12756}
12757
80626a55
DE
12758/* This function is mapped across the sections and remembers the offset and
12759 size of each of the DWO debugging sections we are interested in. */
12760
12761static void
5bb6e9dd
TT
12762dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12763 dwo_sections *dwo_sections)
80626a55 12764{
80626a55
DE
12765 const struct dwop_section_names *names = &dwop_section_names;
12766
fbedd546 12767 if (names->abbrev_dwo.matches (sectp->name))
80626a55 12768 {
049412e3 12769 dwo_sections->abbrev.s.section = sectp;
fd361982 12770 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55 12771 }
fbedd546 12772 else if (names->info_dwo.matches (sectp->name))
80626a55 12773 {
049412e3 12774 dwo_sections->info.s.section = sectp;
fd361982 12775 dwo_sections->info.size = bfd_section_size (sectp);
80626a55 12776 }
fbedd546 12777 else if (names->line_dwo.matches (sectp->name))
80626a55 12778 {
049412e3 12779 dwo_sections->line.s.section = sectp;
fd361982 12780 dwo_sections->line.size = bfd_section_size (sectp);
80626a55 12781 }
fbedd546 12782 else if (names->loc_dwo.matches (sectp->name))
80626a55 12783 {
049412e3 12784 dwo_sections->loc.s.section = sectp;
fd361982 12785 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12786 }
fbedd546 12787 else if (names->loclists_dwo.matches (sectp->name))
41144253 12788 {
12789 dwo_sections->loclists.s.section = sectp;
12790 dwo_sections->loclists.size = bfd_section_size (sectp);
12791 }
fbedd546 12792 else if (names->macinfo_dwo.matches (sectp->name))
80626a55 12793 {
049412e3 12794 dwo_sections->macinfo.s.section = sectp;
fd361982 12795 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55 12796 }
fbedd546 12797 else if (names->macro_dwo.matches (sectp->name))
80626a55 12798 {
049412e3 12799 dwo_sections->macro.s.section = sectp;
fd361982 12800 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55 12801 }
fbedd546 12802 else if (names->rnglists_dwo.matches (sectp->name))
d0ce17d8
CT
12803 {
12804 dwo_sections->rnglists.s.section = sectp;
12805 dwo_sections->rnglists.size = bfd_section_size (sectp);
12806 }
fbedd546 12807 else if (names->str_dwo.matches (sectp->name))
80626a55 12808 {
049412e3 12809 dwo_sections->str.s.section = sectp;
fd361982 12810 dwo_sections->str.size = bfd_section_size (sectp);
80626a55 12811 }
fbedd546 12812 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55 12813 {
049412e3 12814 dwo_sections->str_offsets.s.section = sectp;
fd361982 12815 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55 12816 }
fbedd546 12817 else if (names->types_dwo.matches (sectp->name))
80626a55
DE
12818 {
12819 struct dwarf2_section_info type_section;
12820
12821 memset (&type_section, 0, sizeof (type_section));
049412e3 12822 type_section.s.section = sectp;
fd361982 12823 type_section.size = bfd_section_size (sectp);
fd5866f6 12824 dwo_sections->types.push_back (type_section);
80626a55
DE
12825 }
12826}
12827
ab5088bf 12828/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12829 by PER_CU. This is for the non-DWP case.
80626a55 12830 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12831
12832static struct dwo_file *
4ab09049
SM
12833open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12834 const char *comp_dir)
3019eac3 12835{
976ca316 12836 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12837
976ca316 12838 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12839 if (dbfd == NULL)
12840 {
6f738b01
SM
12841 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12842
80626a55
DE
12843 return NULL;
12844 }
263db9a1 12845
51ac9db5 12846 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12847 dwo_file->dwo_name = dwo_name;
12848 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12849 dwo_file->dbfd = std::move (dbfd);
3019eac3 12850
5bb6e9dd
TT
12851 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12852 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12853 &dwo_file->sections);
3019eac3 12854
976ca316
SM
12855 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12856 dwo_file->cus);
3019eac3 12857
d2854d8d
CT
12858 if (cu->per_cu->dwarf_version < 5)
12859 {
12860 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12861 dwo_file->sections.types, dwo_file->tus);
12862 }
12863 else
12864 {
12865 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12866 &dwo_file->sections.info, dwo_file->tus,
30c80d88 12867 rcuh_kind::COMPILE);
d2854d8d 12868 }
3019eac3 12869
6f738b01 12870 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
80626a55 12871
263db9a1 12872 return dwo_file.release ();
3019eac3
DE
12873}
12874
80626a55 12875/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12876 size of each of the DWP debugging sections common to version 1 and 2 that
12877 we are interested in. */
3019eac3 12878
80626a55 12879static void
73869dc2 12880dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
5bb6e9dd 12881 dwp_file *dwp_file)
3019eac3 12882{
80626a55
DE
12883 const struct dwop_section_names *names = &dwop_section_names;
12884 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12885
80626a55 12886 /* Record the ELF section number for later lookup: this is what the
73869dc2 12887 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12888 gdb_assert (elf_section_nr < dwp_file->num_sections);
12889 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12890
80626a55 12891 /* Look for specific sections that we need. */
fbedd546 12892 if (names->str_dwo.matches (sectp->name))
80626a55 12893 {
049412e3 12894 dwp_file->sections.str.s.section = sectp;
fd361982 12895 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55 12896 }
fbedd546 12897 else if (names->cu_index.matches (sectp->name))
80626a55 12898 {
049412e3 12899 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12900 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55 12901 }
fbedd546 12902 else if (names->tu_index.matches (sectp->name))
80626a55 12903 {
049412e3 12904 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12905 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12906 }
12907}
3019eac3 12908
73869dc2
DE
12909/* This function is mapped across the sections and remembers the offset and
12910 size of each of the DWP version 2 debugging sections that we are interested
12911 in. This is split into a separate function because we don't know if we
d2854d8d 12912 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
73869dc2
DE
12913
12914static void
12915dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12916{
9a3c8263 12917 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12918 const struct dwop_section_names *names = &dwop_section_names;
12919 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12920
12921 /* Record the ELF section number for later lookup: this is what the
12922 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12923 gdb_assert (elf_section_nr < dwp_file->num_sections);
12924 dwp_file->elf_sections[elf_section_nr] = sectp;
12925
12926 /* Look for specific sections that we need. */
fbedd546 12927 if (names->abbrev_dwo.matches (sectp->name))
73869dc2 12928 {
049412e3 12929 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12930 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2 12931 }
fbedd546 12932 else if (names->info_dwo.matches (sectp->name))
73869dc2 12933 {
049412e3 12934 dwp_file->sections.info.s.section = sectp;
fd361982 12935 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2 12936 }
fbedd546 12937 else if (names->line_dwo.matches (sectp->name))
73869dc2 12938 {
049412e3 12939 dwp_file->sections.line.s.section = sectp;
fd361982 12940 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2 12941 }
fbedd546 12942 else if (names->loc_dwo.matches (sectp->name))
73869dc2 12943 {
049412e3 12944 dwp_file->sections.loc.s.section = sectp;
fd361982 12945 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2 12946 }
fbedd546 12947 else if (names->macinfo_dwo.matches (sectp->name))
73869dc2 12948 {
049412e3 12949 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12950 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2 12951 }
fbedd546 12952 else if (names->macro_dwo.matches (sectp->name))
73869dc2 12953 {
049412e3 12954 dwp_file->sections.macro.s.section = sectp;
fd361982 12955 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2 12956 }
fbedd546 12957 else if (names->str_offsets_dwo.matches (sectp->name))
73869dc2 12958 {
049412e3 12959 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12960 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2 12961 }
fbedd546 12962 else if (names->types_dwo.matches (sectp->name))
73869dc2 12963 {
049412e3 12964 dwp_file->sections.types.s.section = sectp;
fd361982 12965 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12966 }
12967}
12968
d2854d8d
CT
12969/* This function is mapped across the sections and remembers the offset and
12970 size of each of the DWP version 5 debugging sections that we are interested
12971 in. This is split into a separate function because we don't know if we
12972 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12973
12974static void
12975dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12976{
12977 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12978 const struct dwop_section_names *names = &dwop_section_names;
12979 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12980
12981 /* Record the ELF section number for later lookup: this is what the
12982 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12983 gdb_assert (elf_section_nr < dwp_file->num_sections);
12984 dwp_file->elf_sections[elf_section_nr] = sectp;
12985
12986 /* Look for specific sections that we need. */
fbedd546 12987 if (names->abbrev_dwo.matches (sectp->name))
d2854d8d
CT
12988 {
12989 dwp_file->sections.abbrev.s.section = sectp;
12990 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12991 }
fbedd546 12992 else if (names->info_dwo.matches (sectp->name))
d2854d8d
CT
12993 {
12994 dwp_file->sections.info.s.section = sectp;
12995 dwp_file->sections.info.size = bfd_section_size (sectp);
12996 }
fbedd546 12997 else if (names->line_dwo.matches (sectp->name))
d2854d8d
CT
12998 {
12999 dwp_file->sections.line.s.section = sectp;
13000 dwp_file->sections.line.size = bfd_section_size (sectp);
13001 }
fbedd546 13002 else if (names->loclists_dwo.matches (sectp->name))
d2854d8d
CT
13003 {
13004 dwp_file->sections.loclists.s.section = sectp;
13005 dwp_file->sections.loclists.size = bfd_section_size (sectp);
13006 }
fbedd546 13007 else if (names->macro_dwo.matches (sectp->name))
d2854d8d
CT
13008 {
13009 dwp_file->sections.macro.s.section = sectp;
13010 dwp_file->sections.macro.size = bfd_section_size (sectp);
13011 }
fbedd546 13012 else if (names->rnglists_dwo.matches (sectp->name))
d2854d8d
CT
13013 {
13014 dwp_file->sections.rnglists.s.section = sectp;
13015 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
13016 }
fbedd546 13017 else if (names->str_offsets_dwo.matches (sectp->name))
d2854d8d
CT
13018 {
13019 dwp_file->sections.str_offsets.s.section = sectp;
13020 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13021 }
13022}
13023
80626a55 13024/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13025
80626a55
DE
13026static hashval_t
13027hash_dwp_loaded_cutus (const void *item)
13028{
9a3c8263 13029 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13030
80626a55
DE
13031 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13032 return dwo_unit->signature;
3019eac3
DE
13033}
13034
80626a55 13035/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13036
80626a55
DE
13037static int
13038eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13039{
9a3c8263
SM
13040 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13041 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13042
80626a55
DE
13043 return dua->signature == dub->signature;
13044}
3019eac3 13045
80626a55 13046/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13047
48b490f2 13048static htab_up
298e9637 13049allocate_dwp_loaded_cutus_table ()
80626a55 13050{
48b490f2
TT
13051 return htab_up (htab_create_alloc (3,
13052 hash_dwp_loaded_cutus,
13053 eq_dwp_loaded_cutus,
13054 NULL, xcalloc, xfree));
80626a55 13055}
3019eac3 13056
ab5088bf
DE
13057/* Try to open DWP file FILE_NAME.
13058 The result is the bfd handle of the file.
13059 If there is a problem finding or opening the file, return NULL.
13060 Upon success, the canonicalized path of the file is stored in the bfd,
13061 same as symfile_bfd_open. */
13062
192b62ce 13063static gdb_bfd_ref_ptr
976ca316 13064open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 13065{
976ca316 13066 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 13067 1 /*is_dwp*/,
192b62ce 13068 1 /*search_cwd*/));
6ac97d4c
DE
13069 if (abfd != NULL)
13070 return abfd;
13071
13072 /* Work around upstream bug 15652.
13073 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13074 [Whether that's a "bug" is debatable, but it is getting in our way.]
13075 We have no real idea where the dwp file is, because gdb's realpath-ing
13076 of the executable's path may have discarded the needed info.
13077 [IWBN if the dwp file name was recorded in the executable, akin to
13078 .gnu_debuglink, but that doesn't exist yet.]
13079 Strip the directory from FILE_NAME and search again. */
13080 if (*debug_file_directory != '\0')
13081 {
13082 /* Don't implicitly search the current directory here.
13083 If the user wants to search "." to handle this case,
13084 it must be added to debug-file-directory. */
976ca316
SM
13085 return try_open_dwop_file (per_objfile, lbasename (file_name),
13086 1 /*is_dwp*/,
6ac97d4c
DE
13087 0 /*search_cwd*/);
13088 }
13089
13090 return NULL;
ab5088bf
DE
13091}
13092
80626a55
DE
13093/* Initialize the use of the DWP file for the current objfile.
13094 By convention the name of the DWP file is ${objfile}.dwp.
13095 The result is NULL if it can't be found. */
a766d390 13096
400174b1 13097static std::unique_ptr<struct dwp_file>
976ca316 13098open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 13099{
976ca316 13100 struct objfile *objfile = per_objfile->objfile;
80626a55 13101
82bf32bc
JK
13102 /* Try to find first .dwp for the binary file before any symbolic links
13103 resolving. */
6c447423
DE
13104
13105 /* If the objfile is a debug file, find the name of the real binary
13106 file and get the name of dwp file from there. */
d721ba37 13107 std::string dwp_name;
6c447423
DE
13108 if (objfile->separate_debug_objfile_backlink != NULL)
13109 {
13110 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13111 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13112
d721ba37 13113 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13114 }
13115 else
d721ba37
PA
13116 dwp_name = objfile->original_name;
13117
13118 dwp_name += ".dwp";
80626a55 13119
976ca316 13120 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13121 if (dbfd == NULL
13122 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13123 {
13124 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13125 dwp_name = objfile_name (objfile);
13126 dwp_name += ".dwp";
976ca316 13127 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
13128 }
13129
80626a55
DE
13130 if (dbfd == NULL)
13131 {
6f738b01
SM
13132 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13133
400174b1 13134 return std::unique_ptr<dwp_file> ();
3019eac3 13135 }
400174b1
TT
13136
13137 const char *name = bfd_get_filename (dbfd.get ());
13138 std::unique_ptr<struct dwp_file> dwp_file
13139 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13140
0a0f4c01 13141 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 13142 dwp_file->elf_sections =
976ca316 13143 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
13144 dwp_file->num_sections, asection *);
13145
5bb6e9dd
TT
13146 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13147 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13148 dwp_file.get ());
80626a55 13149
976ca316 13150 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 13151
976ca316 13152 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 13153
73869dc2 13154 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13155 if (dwp_file->cus && dwp_file->tus
13156 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13157 {
13158 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13159 pretty bizarre. We use pulongest here because that's the established
4d65956b 13160 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13161 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13162 " TU version %s [in DWP file %s]"),
13163 pulongest (dwp_file->cus->version),
d721ba37 13164 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13165 }
08302ed2
DE
13166
13167 if (dwp_file->cus)
13168 dwp_file->version = dwp_file->cus->version;
13169 else if (dwp_file->tus)
13170 dwp_file->version = dwp_file->tus->version;
13171 else
13172 dwp_file->version = 2;
73869dc2 13173
5bb6e9dd
TT
13174 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13175 {
13176 if (dwp_file->version == 2)
13177 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13178 dwp_file.get ());
13179 else
13180 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13181 dwp_file.get ());
13182 }
73869dc2 13183
298e9637
SM
13184 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13185 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 13186
6f738b01
SM
13187 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13188 dwarf_read_debug_printf (" %s CUs, %s TUs",
13189 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13190 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13191
13192 return dwp_file;
3019eac3 13193}
c906108c 13194
ab5088bf
DE
13195/* Wrapper around open_and_init_dwp_file, only open it once. */
13196
13197static struct dwp_file *
976ca316 13198get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 13199{
976ca316 13200 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 13201 {
976ca316
SM
13202 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13203 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 13204 }
976ca316 13205 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
13206}
13207
80626a55
DE
13208/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13209 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13210 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13211 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13212 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13213
13214 This is called, for example, when wanting to read a variable with a
13215 complex location. Therefore we don't want to do file i/o for every call.
13216 Therefore we don't want to look for a DWO file on every call.
13217 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13218 then we check if we've already seen DWO_NAME, and only THEN do we check
13219 for a DWO file.
13220
1c658ad5 13221 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13222 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13223
3019eac3 13224static struct dwo_unit *
4ab09049 13225lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 13226 ULONGEST signature, int is_debug_types)
3019eac3 13227{
976ca316
SM
13228 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13229 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
13230 const char *kind = is_debug_types ? "TU" : "CU";
13231 void **dwo_file_slot;
3019eac3 13232 struct dwo_file *dwo_file;
80626a55 13233 struct dwp_file *dwp_file;
cb1df416 13234
6a506a2d
DE
13235 /* First see if there's a DWP file.
13236 If we have a DWP file but didn't find the DWO inside it, don't
13237 look for the original DWO file. It makes gdb behave differently
13238 depending on whether one is debugging in the build tree. */
cf2c3c16 13239
976ca316 13240 dwp_file = get_dwp_file (per_objfile);
80626a55 13241 if (dwp_file != NULL)
cf2c3c16 13242 {
80626a55
DE
13243 const struct dwp_hash_table *dwp_htab =
13244 is_debug_types ? dwp_file->tus : dwp_file->cus;
13245
13246 if (dwp_htab != NULL)
13247 {
13248 struct dwo_unit *dwo_cutu =
976ca316
SM
13249 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13250 is_debug_types);
80626a55
DE
13251
13252 if (dwo_cutu != NULL)
13253 {
6f738b01
SM
13254 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13255 kind, hex_string (signature),
13256 host_address_to_string (dwo_cutu));
13257
80626a55
DE
13258 return dwo_cutu;
13259 }
13260 }
13261 }
6a506a2d 13262 else
80626a55 13263 {
6a506a2d 13264 /* No DWP file, look for the DWO file. */
80626a55 13265
976ca316 13266 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 13267 if (*dwo_file_slot == NULL)
80626a55 13268 {
6a506a2d 13269 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 13270 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 13271 }
6a506a2d 13272 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13273 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13274
6a506a2d 13275 if (dwo_file != NULL)
19c3d4c9 13276 {
6a506a2d
DE
13277 struct dwo_unit *dwo_cutu = NULL;
13278
13279 if (is_debug_types && dwo_file->tus)
13280 {
13281 struct dwo_unit find_dwo_cutu;
13282
13283 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13284 find_dwo_cutu.signature = signature;
9a3c8263 13285 dwo_cutu
b0b6a987
TT
13286 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13287 &find_dwo_cutu);
6a506a2d 13288 }
33c5cd75 13289 else if (!is_debug_types && dwo_file->cus)
80626a55 13290 {
33c5cd75
DB
13291 struct dwo_unit find_dwo_cutu;
13292
13293 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13294 find_dwo_cutu.signature = signature;
b0b6a987 13295 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 13296 &find_dwo_cutu);
6a506a2d
DE
13297 }
13298
13299 if (dwo_cutu != NULL)
13300 {
6f738b01
SM
13301 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13302 kind, dwo_name, hex_string (signature),
13303 host_address_to_string (dwo_cutu));
13304
6a506a2d 13305 return dwo_cutu;
80626a55
DE
13306 }
13307 }
2e276125 13308 }
9cdd5dbd 13309
80626a55
DE
13310 /* We didn't find it. This could mean a dwo_id mismatch, or
13311 someone deleted the DWO/DWP file, or the search path isn't set up
13312 correctly to find the file. */
13313
6f738b01
SM
13314 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13315 kind, dwo_name, hex_string (signature));
3019eac3 13316
6656a72d
DE
13317 /* This is a warning and not a complaint because it can be caused by
13318 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13319 {
13320 /* Print the name of the DWP file if we looked there, helps the user
13321 better diagnose the problem. */
791afaa2 13322 std::string dwp_text;
43942612
DE
13323
13324 if (dwp_file != NULL)
791afaa2
TT
13325 dwp_text = string_printf (" [in DWP file %s]",
13326 lbasename (dwp_file->name));
43942612 13327
9d8780f0 13328 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 13329 " [in module %s]"),
4ab09049
SM
13330 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13331 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 13332 }
3019eac3 13333 return NULL;
5fb290d7
DJ
13334}
13335
80626a55
DE
13336/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13337 See lookup_dwo_cutu_unit for details. */
13338
13339static struct dwo_unit *
4ab09049 13340lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
13341 ULONGEST signature)
13342{
4ab09049
SM
13343 gdb_assert (!cu->per_cu->is_debug_types);
13344
13345 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
13346}
13347
13348/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13349 See lookup_dwo_cutu_unit for details. */
13350
13351static struct dwo_unit *
4ab09049 13352lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 13353{
4ab09049
SM
13354 gdb_assert (cu->per_cu->is_debug_types);
13355
13356 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13357
13358 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
13359}
13360
89e63ee4
DE
13361/* Traversal function for queue_and_load_all_dwo_tus. */
13362
13363static int
13364queue_and_load_dwo_tu (void **slot, void *info)
13365{
13366 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 13367 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 13368 ULONGEST signature = dwo_unit->signature;
d460f660 13369 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
13370
13371 if (sig_type != NULL)
13372 {
13373 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13374
13375 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13376 a real dependency of PER_CU on SIG_TYPE. That is detected later
13377 while processing PER_CU. */
120ce1b5 13378 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
13379 load_full_type_unit (sig_cu, cu->per_objfile);
13380 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13381 }
13382
13383 return 1;
13384}
13385
1b555f17 13386/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
13387 The DWO may have the only definition of the type, though it may not be
13388 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13389 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13390
13391static void
1b555f17 13392queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
13393{
13394 struct dwo_unit *dwo_unit;
13395 struct dwo_file *dwo_file;
13396
1b555f17
SM
13397 gdb_assert (cu != nullptr);
13398 gdb_assert (!cu->per_cu->is_debug_types);
13399 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 13400
1b555f17 13401 dwo_unit = cu->dwo_unit;
89e63ee4
DE
13402 gdb_assert (dwo_unit != NULL);
13403
13404 dwo_file = dwo_unit->dwo_file;
13405 if (dwo_file->tus != NULL)
1b555f17 13406 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
13407}
13408
3019eac3 13409/* Read in various DIEs. */
348e048f 13410
d389af10 13411/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13412 Inherit only the children of the DW_AT_abstract_origin DIE not being
13413 already referenced by DW_AT_abstract_origin from the children of the
13414 current DIE. */
d389af10
JK
13415
13416static void
13417inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13418{
13419 struct die_info *child_die;
791afaa2 13420 sect_offset *offsetp;
d389af10
JK
13421 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13422 struct die_info *origin_die;
13423 /* Iterator of the ORIGIN_DIE children. */
13424 struct die_info *origin_child_die;
d389af10 13425 struct attribute *attr;
cd02d79d
PA
13426 struct dwarf2_cu *origin_cu;
13427 struct pending **origin_previous_list_in_scope;
d389af10
JK
13428
13429 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13430 if (!attr)
13431 return;
13432
cd02d79d
PA
13433 /* Note that following die references may follow to a die in a
13434 different cu. */
13435
13436 origin_cu = cu;
13437 origin_die = follow_die_ref (die, attr, &origin_cu);
13438
13439 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13440 symbols in. */
13441 origin_previous_list_in_scope = origin_cu->list_in_scope;
13442 origin_cu->list_in_scope = cu->list_in_scope;
13443
edb3359d
DJ
13444 if (die->tag != origin_die->tag
13445 && !(die->tag == DW_TAG_inlined_subroutine
13446 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13447 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13448 sect_offset_str (die->sect_off),
13449 sect_offset_str (origin_die->sect_off));
d389af10 13450
791afaa2 13451 std::vector<sect_offset> offsets;
d389af10 13452
3ea89b92
PMR
13453 for (child_die = die->child;
13454 child_die && child_die->tag;
436c571c 13455 child_die = child_die->sibling)
3ea89b92
PMR
13456 {
13457 struct die_info *child_origin_die;
13458 struct dwarf2_cu *child_origin_cu;
13459
13460 /* We are trying to process concrete instance entries:
216f72a1 13461 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13462 it's not relevant to our analysis here. i.e. detecting DIEs that are
13463 present in the abstract instance but not referenced in the concrete
13464 one. */
216f72a1 13465 if (child_die->tag == DW_TAG_call_site
dda83cd7 13466 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13467 continue;
13468
c38f313d
DJ
13469 /* For each CHILD_DIE, find the corresponding child of
13470 ORIGIN_DIE. If there is more than one layer of
13471 DW_AT_abstract_origin, follow them all; there shouldn't be,
13472 but GCC versions at least through 4.4 generate this (GCC PR
13473 40573). */
3ea89b92
PMR
13474 child_origin_die = child_die;
13475 child_origin_cu = cu;
c38f313d
DJ
13476 while (1)
13477 {
cd02d79d
PA
13478 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13479 child_origin_cu);
c38f313d
DJ
13480 if (attr == NULL)
13481 break;
cd02d79d
PA
13482 child_origin_die = follow_die_ref (child_origin_die, attr,
13483 &child_origin_cu);
c38f313d
DJ
13484 }
13485
d389af10
JK
13486 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13487 counterpart may exist. */
c38f313d 13488 if (child_origin_die != child_die)
d389af10 13489 {
edb3359d
DJ
13490 if (child_die->tag != child_origin_die->tag
13491 && !(child_die->tag == DW_TAG_inlined_subroutine
13492 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13493 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13494 "different tags"),
9d8780f0
SM
13495 sect_offset_str (child_die->sect_off),
13496 sect_offset_str (child_origin_die->sect_off));
c38f313d 13497 if (child_origin_die->parent != origin_die)
b98664d3 13498 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13499 "different parents"),
9d8780f0
SM
13500 sect_offset_str (child_die->sect_off),
13501 sect_offset_str (child_origin_die->sect_off));
c38f313d 13502 else
791afaa2 13503 offsets.push_back (child_origin_die->sect_off);
d389af10 13504 }
d389af10 13505 }
791afaa2
TT
13506 std::sort (offsets.begin (), offsets.end ());
13507 sect_offset *offsets_end = offsets.data () + offsets.size ();
13508 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13509 if (offsetp[-1] == *offsetp)
b98664d3 13510 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13511 "to DIE %s as their abstract origin"),
13512 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13513
791afaa2 13514 offsetp = offsets.data ();
d389af10
JK
13515 origin_child_die = origin_die->child;
13516 while (origin_child_die && origin_child_die->tag)
13517 {
13518 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13519 while (offsetp < offsets_end
9c541725 13520 && *offsetp < origin_child_die->sect_off)
d389af10 13521 offsetp++;
b64f50a1 13522 if (offsetp >= offsets_end
9c541725 13523 || *offsetp > origin_child_die->sect_off)
d389af10 13524 {
adde2bff
DE
13525 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13526 Check whether we're already processing ORIGIN_CHILD_DIE.
13527 This can happen with mutually referenced abstract_origins.
13528 PR 16581. */
13529 if (!origin_child_die->in_process)
13530 process_die (origin_child_die, origin_cu);
d389af10 13531 }
436c571c 13532 origin_child_die = origin_child_die->sibling;
d389af10 13533 }
cd02d79d 13534 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13535
13536 if (cu != origin_cu)
13537 compute_delayed_physnames (origin_cu);
d389af10
JK
13538}
13539
c906108c 13540static void
e7c27a73 13541read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13542{
5e22e966 13543 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13544 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13545 struct context_stack *newobj;
c906108c
SS
13546 CORE_ADDR lowpc;
13547 CORE_ADDR highpc;
13548 struct die_info *child_die;
edb3359d 13549 struct attribute *attr, *call_line, *call_file;
15d034d0 13550 const char *name;
e142c38c 13551 CORE_ADDR baseaddr;
801e3a5b 13552 struct block *block;
edb3359d 13553 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13554 std::vector<struct symbol *> template_args;
34eaf542 13555 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13556
13557 if (inlined_func)
13558 {
13559 /* If we do not have call site information, we can't show the
13560 caller of this inlined function. That's too confusing, so
13561 only use the scope for local variables. */
13562 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13563 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13564 if (call_line == NULL || call_file == NULL)
13565 {
13566 read_lexical_block_scope (die, cu);
13567 return;
13568 }
13569 }
c906108c 13570
b3b3bada 13571 baseaddr = objfile->text_section_offset ();
e142c38c 13572
94af9270 13573 name = dwarf2_name (die, cu);
c906108c 13574
e8d05480
JB
13575 /* Ignore functions with missing or empty names. These are actually
13576 illegal according to the DWARF standard. */
13577 if (name == NULL)
13578 {
b98664d3 13579 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13580 sect_offset_str (die->sect_off));
e8d05480
JB
13581 return;
13582 }
13583
13584 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13585 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13586 <= PC_BOUNDS_INVALID)
e8d05480 13587 {
ae4d0c03 13588 attr = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 13589 if (attr == nullptr || !attr->as_boolean ())
b98664d3 13590 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13591 "for subprogram DIE at %s"),
13592 sect_offset_str (die->sect_off));
e8d05480
JB
13593 return;
13594 }
c906108c 13595
3e29f34a
MR
13596 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13597 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13598
34eaf542
TT
13599 /* If we have any template arguments, then we must allocate a
13600 different sort of symbol. */
436c571c 13601 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13602 {
13603 if (child_die->tag == DW_TAG_template_type_param
13604 || child_die->tag == DW_TAG_template_value_param)
13605 {
8c14c3a3 13606 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13607 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13608 break;
13609 }
13610 }
13611
c24bdb02 13612 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13613 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13614 (struct symbol *) templ_func);
4c2df51b 13615
81873cc8 13616 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13617 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13618 cu->language);
13619
4cecd739
DJ
13620 /* If there is a location expression for DW_AT_frame_base, record
13621 it. */
e142c38c 13622 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13623 if (attr != nullptr)
fe978cb0 13624 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13625
63e43d3a
PMR
13626 /* If there is a location for the static link, record it. */
13627 newobj->static_link = NULL;
13628 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13629 if (attr != nullptr)
63e43d3a 13630 {
224c3ddb
SM
13631 newobj->static_link
13632 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13633 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13634 cu->addr_type ());
63e43d3a
PMR
13635 }
13636
c24bdb02 13637 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13638
639d11d3 13639 if (die->child != NULL)
c906108c 13640 {
639d11d3 13641 child_die = die->child;
c906108c
SS
13642 while (child_die && child_die->tag)
13643 {
34eaf542
TT
13644 if (child_die->tag == DW_TAG_template_type_param
13645 || child_die->tag == DW_TAG_template_value_param)
13646 {
13647 struct symbol *arg = new_symbol (child_die, NULL, cu);
13648
f1078f66 13649 if (arg != NULL)
2f4732b0 13650 template_args.push_back (arg);
34eaf542
TT
13651 }
13652 else
13653 process_die (child_die, cu);
436c571c 13654 child_die = child_die->sibling;
c906108c
SS
13655 }
13656 }
13657
d389af10
JK
13658 inherit_abstract_dies (die, cu);
13659
4a811a97
UW
13660 /* If we have a DW_AT_specification, we might need to import using
13661 directives from the context of the specification DIE. See the
13662 comment in determine_prefix. */
13663 if (cu->language == language_cplus
13664 && dwarf2_attr (die, DW_AT_specification, cu))
13665 {
13666 struct dwarf2_cu *spec_cu = cu;
13667 struct die_info *spec_die = die_specification (die, &spec_cu);
13668
13669 while (spec_die)
13670 {
13671 child_die = spec_die->child;
13672 while (child_die && child_die->tag)
13673 {
13674 if (child_die->tag == DW_TAG_imported_module)
13675 process_die (child_die, spec_cu);
436c571c 13676 child_die = child_die->sibling;
4a811a97
UW
13677 }
13678
13679 /* In some cases, GCC generates specification DIEs that
13680 themselves contain DW_AT_specification attributes. */
13681 spec_die = die_specification (spec_die, &spec_cu);
13682 }
13683 }
13684
c24bdb02 13685 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13686 /* Make a block for the local symbols within. */
c24bdb02 13687 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13688 cstk.static_link, lowpc, highpc);
801e3a5b 13689
df8a16a1 13690 /* For C++, set the block's scope. */
45280282
IB
13691 if ((cu->language == language_cplus
13692 || cu->language == language_fortran
c44af4eb
TT
13693 || cu->language == language_d
13694 || cu->language == language_rust)
4d4ec4e5 13695 && cu->processing_has_namespace_info)
195a3f6c
TT
13696 block_set_scope (block, determine_prefix (die, cu),
13697 &objfile->objfile_obstack);
df8a16a1 13698
801e3a5b
JB
13699 /* If we have address ranges, record them. */
13700 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13701
a60f3166 13702 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13703
34eaf542 13704 /* Attach template arguments to function. */
2f4732b0 13705 if (!template_args.empty ())
34eaf542
TT
13706 {
13707 gdb_assert (templ_func != NULL);
13708
2f4732b0 13709 templ_func->n_template_arguments = template_args.size ();
34eaf542 13710 templ_func->template_arguments
dda83cd7 13711 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
8d749320 13712 templ_func->n_template_arguments);
34eaf542 13713 memcpy (templ_func->template_arguments,
2f4732b0 13714 template_args.data (),
34eaf542 13715 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13716
13717 /* Make sure that the symtab is set on the new symbols. Even
13718 though they don't appear in this symtab directly, other parts
13719 of gdb assume that symbols do, and this is reasonably
13720 true. */
8634679f 13721 for (symbol *sym : template_args)
3e1d3d8c 13722 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13723 }
13724
208d8187
JB
13725 /* In C++, we can have functions nested inside functions (e.g., when
13726 a function declares a class that has methods). This means that
13727 when we finish processing a function scope, we may need to go
13728 back to building a containing block's symbol lists. */
c24bdb02
KS
13729 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13730 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13731
921e78cf
JB
13732 /* If we've finished processing a top-level function, subsequent
13733 symbols go in the file symbol list. */
c24bdb02
KS
13734 if (cu->get_builder ()->outermost_context_p ())
13735 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13736}
13737
13738/* Process all the DIES contained within a lexical block scope. Start
13739 a new scope, process the dies, and then close the scope. */
13740
13741static void
e7c27a73 13742read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13743{
5e22e966 13744 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13745 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13746 CORE_ADDR lowpc, highpc;
13747 struct die_info *child_die;
e142c38c
DJ
13748 CORE_ADDR baseaddr;
13749
b3b3bada 13750 baseaddr = objfile->text_section_offset ();
c906108c
SS
13751
13752 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13753 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13754 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13755 be nasty. Might be easier to properly extend generic blocks to
af34e669 13756 describe ranges. */
e385593e
JK
13757 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13758 {
13759 case PC_BOUNDS_NOT_PRESENT:
13760 /* DW_TAG_lexical_block has no attributes, process its children as if
13761 there was no wrapping by that DW_TAG_lexical_block.
13762 GCC does no longer produces such DWARF since GCC r224161. */
13763 for (child_die = die->child;
13764 child_die != NULL && child_die->tag;
436c571c 13765 child_die = child_die->sibling)
4f7bc5ed
TT
13766 {
13767 /* We might already be processing this DIE. This can happen
13768 in an unusual circumstance -- where a subroutine A
13769 appears lexically in another subroutine B, but A actually
13770 inlines B. The recursion is broken here, rather than in
13771 inherit_abstract_dies, because it seems better to simply
13772 drop concrete children here. */
13773 if (!child_die->in_process)
13774 process_die (child_die, cu);
13775 }
e385593e
JK
13776 return;
13777 case PC_BOUNDS_INVALID:
13778 return;
13779 }
3e29f34a
MR
13780 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13781 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13782
c24bdb02 13783 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13784 if (die->child != NULL)
c906108c 13785 {
639d11d3 13786 child_die = die->child;
c906108c
SS
13787 while (child_die && child_die->tag)
13788 {
e7c27a73 13789 process_die (child_die, cu);
436c571c 13790 child_die = child_die->sibling;
c906108c
SS
13791 }
13792 }
3ea89b92 13793 inherit_abstract_dies (die, cu);
c24bdb02 13794 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13795
c24bdb02
KS
13796 if (*cu->get_builder ()->get_local_symbols () != NULL
13797 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13798 {
801e3a5b 13799 struct block *block
dda83cd7 13800 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13801 cstk.start_addr, highpc);
801e3a5b
JB
13802
13803 /* Note that recording ranges after traversing children, as we
dda83cd7
SM
13804 do here, means that recording a parent's ranges entails
13805 walking across all its children's ranges as they appear in
13806 the address map, which is quadratic behavior.
13807
13808 It would be nicer to record the parent's ranges before
13809 traversing its children, simply overriding whatever you find
13810 there. But since we don't even decide whether to create a
13811 block until after we've traversed its children, that's hard
13812 to do. */
801e3a5b 13813 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13814 }
c24bdb02
KS
13815 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13816 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13817}
13818
216f72a1 13819/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13820
13821static void
13822read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13823{
5e22e966 13824 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13825 struct objfile *objfile = per_objfile->objfile;
08feed99 13826 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13827 CORE_ADDR pc, baseaddr;
13828 struct attribute *attr;
13829 struct call_site *call_site, call_site_local;
13830 void **slot;
13831 int nparams;
13832 struct die_info *child_die;
13833
b3b3bada 13834 baseaddr = objfile->text_section_offset ();
96408a79 13835
216f72a1
JK
13836 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13837 if (attr == NULL)
13838 {
13839 /* This was a pre-DWARF-5 GNU extension alias
13840 for DW_AT_call_return_pc. */
13841 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13842 }
96408a79
SA
13843 if (!attr)
13844 {
b98664d3 13845 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13846 "DIE %s [in module %s]"),
13847 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13848 return;
13849 }
95f982e5 13850 pc = attr->as_address () + baseaddr;
3e29f34a 13851 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13852
13853 if (cu->call_site_htab == NULL)
13854 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13855 NULL, &objfile->objfile_obstack,
13856 hashtab_obstack_allocate, NULL);
13857 call_site_local.pc = pc;
13858 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13859 if (*slot != NULL)
13860 {
b98664d3 13861 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13862 "DIE %s [in module %s]"),
13863 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13864 objfile_name (objfile));
96408a79
SA
13865 return;
13866 }
13867
13868 /* Count parameters at the caller. */
13869
13870 nparams = 0;
13871 for (child_die = die->child; child_die && child_die->tag;
436c571c 13872 child_die = child_die->sibling)
96408a79 13873 {
216f72a1 13874 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13875 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13876 {
b98664d3 13877 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13878 "DW_TAG_call_site child DIE %s [in module %s]"),
13879 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13880 objfile_name (objfile));
96408a79
SA
13881 continue;
13882 }
13883
13884 nparams++;
13885 }
13886
224c3ddb
SM
13887 call_site
13888 = ((struct call_site *)
13889 obstack_alloc (&objfile->objfile_obstack,
13890 sizeof (*call_site)
13891 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13892 *slot = call_site;
13893 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13894 call_site->pc = pc;
13895
216f72a1
JK
13896 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13897 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13898 {
13899 struct die_info *func_die;
13900
13901 /* Skip also over DW_TAG_inlined_subroutine. */
13902 for (func_die = die->parent;
13903 func_die && func_die->tag != DW_TAG_subprogram
13904 && func_die->tag != DW_TAG_subroutine_type;
13905 func_die = func_die->parent);
13906
216f72a1
JK
13907 /* DW_AT_call_all_calls is a superset
13908 of DW_AT_call_all_tail_calls. */
96408a79 13909 if (func_die
dda83cd7
SM
13910 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13911 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13912 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13913 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13914 {
13915 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13916 not complete. But keep CALL_SITE for look ups via call_site_htab,
13917 both the initial caller containing the real return address PC and
13918 the final callee containing the current PC of a chain of tail
13919 calls do not need to have the tail call list complete. But any
13920 function candidate for a virtual tail call frame searched via
13921 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13922 determined unambiguously. */
13923 }
13924 else
13925 {
13926 struct type *func_type = NULL;
13927
13928 if (func_die)
13929 func_type = get_die_type (func_die, cu);
13930 if (func_type != NULL)
13931 {
78134374 13932 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13933
13934 /* Enlist this call site to the function. */
13935 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13936 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13937 }
13938 else
b98664d3 13939 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13940 "DIE %s [in module %s]"),
13941 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13942 }
13943 }
13944
216f72a1
JK
13945 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13946 if (attr == NULL)
13947 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13948 if (attr == NULL)
13949 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13950 if (attr == NULL)
216f72a1
JK
13951 {
13952 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13953 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13954 }
96408a79 13955 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9d2246fc 13956 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
96408a79 13957 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13958 else if (attr->form_is_block ())
96408a79
SA
13959 {
13960 struct dwarf2_locexpr_baton *dlbaton;
9d2246fc 13961 struct dwarf_block *block = attr->as_block ();
96408a79 13962
8d749320 13963 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9d2246fc
TT
13964 dlbaton->data = block->data;
13965 dlbaton->size = block->size;
a50264ba 13966 dlbaton->per_objfile = per_objfile;
96408a79
SA
13967 dlbaton->per_cu = cu->per_cu;
13968
13969 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13970 }
cd6c91b4 13971 else if (attr->form_is_ref ())
96408a79 13972 {
96408a79
SA
13973 struct dwarf2_cu *target_cu = cu;
13974 struct die_info *target_die;
13975
ac9ec31b 13976 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13977 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13978 if (die_is_declaration (target_die, target_cu))
13979 {
7d45c7c3 13980 const char *target_physname;
9112db09
JK
13981
13982 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13983 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13984 if (target_physname == NULL)
9112db09 13985 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13986 if (target_physname == NULL)
b98664d3 13987 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13988 "physname, for referencing DIE %s [in module %s]"),
9d8780f0 13989 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13990 else
7d455152 13991 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13992 }
13993 else
13994 {
13995 CORE_ADDR lowpc;
13996
13997 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13998 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13999 <= PC_BOUNDS_INVALID)
b98664d3 14000 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 14001 "low pc, for referencing DIE %s [in module %s]"),
9d8780f0 14002 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14003 else
3e29f34a
MR
14004 {
14005 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14006 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14007 }
96408a79
SA
14008 }
14009 }
14010 else
b98664d3 14011 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14012 "block nor reference, for DIE %s [in module %s]"),
14013 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14014
14015 call_site->per_cu = cu->per_cu;
9f47c707 14016 call_site->per_objfile = per_objfile;
96408a79
SA
14017
14018 for (child_die = die->child;
14019 child_die && child_die->tag;
436c571c 14020 child_die = child_die->sibling)
96408a79 14021 {
96408a79 14022 struct call_site_parameter *parameter;
1788b2d3 14023 struct attribute *loc, *origin;
96408a79 14024
216f72a1 14025 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 14026 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14027 {
14028 /* Already printed the complaint above. */
14029 continue;
14030 }
14031
14032 gdb_assert (call_site->parameter_count < nparams);
14033 parameter = &call_site->parameter[call_site->parameter_count];
14034
1788b2d3
JK
14035 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14036 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14037 register is contained in DW_AT_call_value. */
96408a79 14038
24c5c679 14039 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14040 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14041 if (origin == NULL)
14042 {
14043 /* This was a pre-DWARF-5 GNU extension alias
14044 for DW_AT_call_parameter. */
14045 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14046 }
cd6c91b4 14047 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 14048 {
1788b2d3 14049 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 14050
0826b30a 14051 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 14052 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
14053 {
14054 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14055 binding can be done only inside one CU. Such referenced DIE
14056 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14057 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14058 "DW_TAG_call_site child DIE %s [in module %s]"),
14059 sect_offset_str (child_die->sect_off),
9c541725 14060 objfile_name (objfile));
d76b7dbc
JK
14061 continue;
14062 }
9c541725
PA
14063 parameter->u.param_cu_off
14064 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 14065 }
4fc6c0d5 14066 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 14067 {
b98664d3 14068 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14069 "DW_TAG_call_site child DIE %s [in module %s]"),
14070 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14071 continue;
14072 }
24c5c679 14073 else
96408a79 14074 {
9d2246fc
TT
14075 struct dwarf_block *block = loc->as_block ();
14076
24c5c679 14077 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9d2246fc 14078 (block->data, &block->data[block->size]);
24c5c679
JK
14079 if (parameter->u.dwarf_reg != -1)
14080 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9d2246fc
TT
14081 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14082 &block->data[block->size],
24c5c679
JK
14083 &parameter->u.fb_offset))
14084 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14085 else
14086 {
b98664d3 14087 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14088 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14089 "DW_TAG_call_site child DIE %s "
24c5c679 14090 "[in module %s]"),
9d8780f0 14091 sect_offset_str (child_die->sect_off),
9c541725 14092 objfile_name (objfile));
24c5c679
JK
14093 continue;
14094 }
96408a79
SA
14095 }
14096
216f72a1
JK
14097 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14098 if (attr == NULL)
14099 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 14100 if (attr == NULL || !attr->form_is_block ())
96408a79 14101 {
b98664d3 14102 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14103 "DW_TAG_call_site child DIE %s [in module %s]"),
14104 sect_offset_str (child_die->sect_off),
9c541725 14105 objfile_name (objfile));
96408a79
SA
14106 continue;
14107 }
9d2246fc
TT
14108
14109 struct dwarf_block *block = attr->as_block ();
14110 parameter->value = block->data;
14111 parameter->value_size = block->size;
96408a79
SA
14112
14113 /* Parameters are not pre-cleared by memset above. */
14114 parameter->data_value = NULL;
14115 parameter->data_value_size = 0;
14116 call_site->parameter_count++;
14117
216f72a1
JK
14118 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14119 if (attr == NULL)
14120 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14121 if (attr != nullptr)
96408a79 14122 {
4fc6c0d5 14123 if (!attr->form_is_block ())
b98664d3 14124 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14125 "DW_TAG_call_site child DIE %s [in module %s]"),
14126 sect_offset_str (child_die->sect_off),
9c541725 14127 objfile_name (objfile));
96408a79
SA
14128 else
14129 {
9d2246fc
TT
14130 block = attr->as_block ();
14131 parameter->data_value = block->data;
14132 parameter->data_value_size = block->size;
96408a79
SA
14133 }
14134 }
14135 }
14136}
14137
71a3c369
TT
14138/* Helper function for read_variable. If DIE represents a virtual
14139 table, then return the type of the concrete object that is
14140 associated with the virtual table. Otherwise, return NULL. */
14141
14142static struct type *
14143rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14144{
14145 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14146 if (attr == NULL)
14147 return NULL;
14148
14149 /* Find the type DIE. */
14150 struct die_info *type_die = NULL;
14151 struct dwarf2_cu *type_cu = cu;
14152
cd6c91b4 14153 if (attr->form_is_ref ())
71a3c369
TT
14154 type_die = follow_die_ref (die, attr, &type_cu);
14155 if (type_die == NULL)
14156 return NULL;
14157
14158 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14159 return NULL;
14160 return die_containing_type (type_die, type_cu);
14161}
14162
14163/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14164
14165static void
14166read_variable (struct die_info *die, struct dwarf2_cu *cu)
14167{
14168 struct rust_vtable_symbol *storage = NULL;
14169
14170 if (cu->language == language_rust)
14171 {
14172 struct type *containing_type = rust_containing_type (die, cu);
14173
14174 if (containing_type != NULL)
14175 {
5e22e966 14176 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 14177
8c14c3a3 14178 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 14179 storage->concrete_type = containing_type;
cf724bc9 14180 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14181 }
14182 }
14183
e4a62c65
TV
14184 struct symbol *res = new_symbol (die, NULL, cu, storage);
14185 struct attribute *abstract_origin
14186 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14187 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14188 if (res == NULL && loc && abstract_origin)
14189 {
14190 /* We have a variable without a name, but with a location and an abstract
14191 origin. This may be a concrete instance of an abstract variable
14192 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14193 later. */
14194 struct dwarf2_cu *origin_cu = cu;
14195 struct die_info *origin_die
14196 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
14197 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14198 per_objfile->per_bfd->abstract_to_concrete
14199 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14200 }
71a3c369
TT
14201}
14202
43988095
JK
14203/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14204 reading .debug_rnglists.
14205 Callback's type should be:
14206 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14207 Return true if the attributes are present and valid, otherwise,
14208 return false. */
14209
14210template <typename Callback>
14211static bool
14212dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
d0ce17d8 14213 dwarf_tag tag, Callback &&callback)
43988095 14214{
976ca316
SM
14215 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14216 struct objfile *objfile = per_objfile->objfile;
43988095 14217 bfd *obfd = objfile->obfd;
43988095 14218 /* Base address selection entry. */
2b24b6e4 14219 gdb::optional<CORE_ADDR> base;
43988095 14220 const gdb_byte *buffer;
43988095
JK
14221 CORE_ADDR baseaddr;
14222 bool overflow = false;
d0ce17d8
CT
14223 ULONGEST addr_index;
14224 struct dwarf2_section_info *rnglists_section;
43988095 14225
43988095 14226 base = cu->base_address;
d0ce17d8
CT
14227 rnglists_section = cu_debug_rnglists_section (cu, tag);
14228 rnglists_section->read (objfile);
43988095 14229
d0ce17d8 14230 if (offset >= rnglists_section->size)
43988095 14231 {
b98664d3 14232 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14233 offset);
14234 return false;
14235 }
d0ce17d8 14236 buffer = rnglists_section->buffer + offset;
43988095 14237
b3b3bada 14238 baseaddr = objfile->text_section_offset ();
43988095
JK
14239
14240 while (1)
14241 {
7814882a
JK
14242 /* Initialize it due to a false compiler warning. */
14243 CORE_ADDR range_beginning = 0, range_end = 0;
d0ce17d8
CT
14244 const gdb_byte *buf_end = (rnglists_section->buffer
14245 + rnglists_section->size);
43988095
JK
14246 unsigned int bytes_read;
14247
14248 if (buffer == buf_end)
14249 {
14250 overflow = true;
14251 break;
14252 }
14253 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14254 switch (rlet)
14255 {
14256 case DW_RLE_end_of_list:
14257 break;
14258 case DW_RLE_base_address:
14259 if (buffer + cu->header.addr_size > buf_end)
14260 {
14261 overflow = true;
14262 break;
14263 }
c8a7a66f 14264 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14265 buffer += bytes_read;
14266 break;
dda83cd7
SM
14267 case DW_RLE_base_addressx:
14268 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14269 buffer += bytes_read;
14270 base = read_addr_index (cu, addr_index);
14271 break;
43988095
JK
14272 case DW_RLE_start_length:
14273 if (buffer + cu->header.addr_size > buf_end)
14274 {
14275 overflow = true;
14276 break;
14277 }
c8a7a66f
TT
14278 range_beginning = cu->header.read_address (obfd, buffer,
14279 &bytes_read);
43988095
JK
14280 buffer += bytes_read;
14281 range_end = (range_beginning
14282 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14283 buffer += bytes_read;
14284 if (buffer > buf_end)
14285 {
14286 overflow = true;
14287 break;
14288 }
14289 break;
d0ce17d8 14290 case DW_RLE_startx_length:
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 range_end = (range_beginning
14300 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14301 buffer += bytes_read;
14302 break;
43988095
JK
14303 case DW_RLE_offset_pair:
14304 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14305 buffer += bytes_read;
14306 if (buffer > buf_end)
14307 {
14308 overflow = true;
14309 break;
14310 }
14311 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14312 buffer += bytes_read;
14313 if (buffer > buf_end)
14314 {
14315 overflow = true;
14316 break;
14317 }
14318 break;
14319 case DW_RLE_start_end:
14320 if (buffer + 2 * cu->header.addr_size > buf_end)
14321 {
14322 overflow = true;
14323 break;
14324 }
c8a7a66f
TT
14325 range_beginning = cu->header.read_address (obfd, buffer,
14326 &bytes_read);
43988095 14327 buffer += bytes_read;
c8a7a66f 14328 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14329 buffer += bytes_read;
14330 break;
d0ce17d8 14331 case DW_RLE_startx_endx:
dda83cd7
SM
14332 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14333 buffer += bytes_read;
14334 range_beginning = read_addr_index (cu, addr_index);
14335 if (buffer > buf_end)
14336 {
14337 overflow = true;
14338 break;
14339 }
14340 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14341 buffer += bytes_read;
14342 range_end = read_addr_index (cu, addr_index);
14343 break;
43988095 14344 default:
b98664d3 14345 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14346 return false;
14347 }
14348 if (rlet == DW_RLE_end_of_list || overflow)
14349 break;
14350 if (rlet == DW_RLE_base_address)
14351 continue;
14352
43988095
JK
14353 if (range_beginning > range_end)
14354 {
14355 /* Inverted range entries are invalid. */
b98664d3 14356 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14357 return false;
14358 }
14359
14360 /* Empty range entries have no effect. */
14361 if (range_beginning == range_end)
14362 continue;
14363
d0ce17d8
CT
14364 /* Only DW_RLE_offset_pair needs the base address added. */
14365 if (rlet == DW_RLE_offset_pair)
14366 {
14367 if (!base.has_value ())
14368 {
14369 /* We have no valid base address for the DW_RLE_offset_pair. */
14370 complaint (_("Invalid .debug_rnglists data (no base address for "
14371 "DW_RLE_offset_pair)"));
14372 return false;
14373 }
14374
14375 range_beginning += *base;
14376 range_end += *base;
14377 }
43988095
JK
14378
14379 /* A not-uncommon case of bad debug info.
14380 Don't pollute the addrmap with bad data. */
14381 if (range_beginning + baseaddr == 0
976ca316 14382 && !per_objfile->per_bfd->has_section_at_zero)
43988095 14383 {
b98664d3 14384 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14385 " [in module %s]"), objfile_name (objfile));
14386 continue;
14387 }
14388
14389 callback (range_beginning, range_end);
14390 }
14391
14392 if (overflow)
14393 {
b98664d3 14394 complaint (_("Offset %d is not terminated "
43988095
JK
14395 "for DW_AT_ranges attribute"),
14396 offset);
14397 return false;
14398 }
14399
14400 return true;
14401}
14402
14403/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14404 Callback's type should be:
14405 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14406 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14407
43988095 14408template <typename Callback>
43039443 14409static int
d0ce17d8 14410dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
43988095 14411 Callback &&callback)
43039443 14412{
5e22e966
SM
14413 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14414 struct objfile *objfile = per_objfile->objfile;
43039443
JK
14415 struct comp_unit_head *cu_header = &cu->header;
14416 bfd *obfd = objfile->obfd;
14417 unsigned int addr_size = cu_header->addr_size;
14418 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14419 /* Base address selection entry. */
2b24b6e4 14420 gdb::optional<CORE_ADDR> base;
43039443 14421 unsigned int dummy;
d521ce57 14422 const gdb_byte *buffer;
ff013f42 14423 CORE_ADDR baseaddr;
43039443 14424
43988095 14425 if (cu_header->version >= 5)
d0ce17d8 14426 return dwarf2_rnglists_process (offset, cu, tag, callback);
43988095 14427
d00adf39 14428 base = cu->base_address;
43039443 14429
5e22e966
SM
14430 per_objfile->per_bfd->ranges.read (objfile);
14431 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 14432 {
b98664d3 14433 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14434 offset);
14435 return 0;
14436 }
5e22e966 14437 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 14438
b3b3bada 14439 baseaddr = objfile->text_section_offset ();
ff013f42 14440
43039443
JK
14441 while (1)
14442 {
14443 CORE_ADDR range_beginning, range_end;
14444
c8a7a66f 14445 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 14446 buffer += addr_size;
c8a7a66f 14447 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
14448 buffer += addr_size;
14449 offset += 2 * addr_size;
14450
14451 /* An end of list marker is a pair of zero addresses. */
14452 if (range_beginning == 0 && range_end == 0)
14453 /* Found the end of list entry. */
14454 break;
14455
14456 /* Each base address selection entry is a pair of 2 values.
14457 The first is the largest possible address, the second is
14458 the base address. Check for a base address here. */
14459 if ((range_beginning & mask) == mask)
14460 {
28d2bfb9
AB
14461 /* If we found the largest possible address, then we already
14462 have the base address in range_end. */
14463 base = range_end;
43039443
JK
14464 continue;
14465 }
14466
2b24b6e4 14467 if (!base.has_value ())
43039443
JK
14468 {
14469 /* We have no valid base address for the ranges
14470 data. */
b98664d3 14471 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14472 return 0;
14473 }
14474
9277c30c
UW
14475 if (range_beginning > range_end)
14476 {
14477 /* Inverted range entries are invalid. */
b98664d3 14478 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14479 return 0;
14480 }
14481
14482 /* Empty range entries have no effect. */
14483 if (range_beginning == range_end)
14484 continue;
14485
2b24b6e4
TT
14486 range_beginning += *base;
14487 range_end += *base;
43039443 14488
01093045
DE
14489 /* A not-uncommon case of bad debug info.
14490 Don't pollute the addrmap with bad data. */
14491 if (range_beginning + baseaddr == 0
5e22e966 14492 && !per_objfile->per_bfd->has_section_at_zero)
01093045 14493 {
b98664d3 14494 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14495 " [in module %s]"), objfile_name (objfile));
01093045
DE
14496 continue;
14497 }
14498
5f46c5a5
JK
14499 callback (range_beginning, range_end);
14500 }
14501
14502 return 1;
14503}
14504
14505/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14506 Return 1 if the attributes are present and valid, otherwise, return 0.
14507 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14508
14509static int
14510dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14511 CORE_ADDR *high_return, struct dwarf2_cu *cu,
d0ce17d8 14512 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
5f46c5a5 14513{
5e22e966 14514 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14515 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14516 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14517 int low_set = 0;
14518 CORE_ADDR low = 0;
14519 CORE_ADDR high = 0;
14520 int retval;
14521
d0ce17d8 14522 retval = dwarf2_ranges_process (offset, cu, tag,
5f46c5a5
JK
14523 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14524 {
9277c30c 14525 if (ranges_pst != NULL)
3e29f34a
MR
14526 {
14527 CORE_ADDR lowpc;
14528 CORE_ADDR highpc;
14529
79748972
TT
14530 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14531 range_beginning + baseaddr)
14532 - baseaddr);
14533 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14534 range_end + baseaddr)
14535 - baseaddr);
d320c2b5
TT
14536 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14537 lowpc, highpc - 1, ranges_pst);
3e29f34a 14538 }
ff013f42 14539
43039443
JK
14540 /* FIXME: This is recording everything as a low-high
14541 segment of consecutive addresses. We should have a
14542 data structure for discontiguous block ranges
14543 instead. */
14544 if (! low_set)
14545 {
14546 low = range_beginning;
14547 high = range_end;
14548 low_set = 1;
14549 }
14550 else
14551 {
14552 if (range_beginning < low)
14553 low = range_beginning;
14554 if (range_end > high)
14555 high = range_end;
14556 }
5f46c5a5
JK
14557 });
14558 if (!retval)
14559 return 0;
43039443
JK
14560
14561 if (! low_set)
14562 /* If the first entry is an end-of-list marker, the range
14563 describes an empty scope, i.e. no instructions. */
14564 return 0;
14565
14566 if (low_return)
14567 *low_return = low;
14568 if (high_return)
14569 *high_return = high;
14570 return 1;
14571}
14572
3a2b436a
JK
14573/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14574 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14575 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14576
3a2b436a 14577static enum pc_bounds_kind
af34e669 14578dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14579 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14580 dwarf2_psymtab *pst)
c906108c 14581{
976ca316 14582 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14583 struct attribute *attr;
91da1414 14584 struct attribute *attr_high;
af34e669
DJ
14585 CORE_ADDR low = 0;
14586 CORE_ADDR high = 0;
e385593e 14587 enum pc_bounds_kind ret;
c906108c 14588
91da1414
MW
14589 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14590 if (attr_high)
af34e669 14591 {
e142c38c 14592 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14593 if (attr != nullptr)
dda83cd7 14594 {
95f982e5
TT
14595 low = attr->as_address ();
14596 high = attr_high->as_address ();
cd6c91b4 14597 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14598 high += low;
91da1414 14599 }
af34e669
DJ
14600 else
14601 /* Found high w/o low attribute. */
e385593e 14602 return PC_BOUNDS_INVALID;
af34e669
DJ
14603
14604 /* Found consecutive range of addresses. */
3a2b436a 14605 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14606 }
c906108c 14607 else
af34e669 14608 {
e142c38c 14609 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14610 if (attr != nullptr && attr->form_is_unsigned ())
af34e669 14611 {
2b0c7f41
SM
14612 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14613 on DWARF version). */
14614 ULONGEST ranges_offset = attr->as_unsigned ();
14615
14616 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14617 this value. */
14618 if (die->tag != DW_TAG_compile_unit)
14619 ranges_offset += cu->gnu_ranges_base;
2e3cf129 14620
af34e669 14621 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14622 .debug_ranges section. */
d0ce17d8
CT
14623 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14624 die->tag))
e385593e 14625 return PC_BOUNDS_INVALID;
43039443 14626 /* Found discontinuous range of addresses. */
3a2b436a 14627 ret = PC_BOUNDS_RANGES;
af34e669 14628 }
e385593e
JK
14629 else
14630 return PC_BOUNDS_NOT_PRESENT;
af34e669 14631 }
c906108c 14632
48fbe735 14633 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14634 if (high <= low)
e385593e 14635 return PC_BOUNDS_INVALID;
c906108c
SS
14636
14637 /* When using the GNU linker, .gnu.linkonce. sections are used to
14638 eliminate duplicate copies of functions and vtables and such.
14639 The linker will arbitrarily choose one and discard the others.
14640 The AT_*_pc values for such functions refer to local labels in
14641 these sections. If the section from that file was discarded, the
14642 labels are not in the output, so the relocs get a value of 0.
14643 If this is a discarded function, mark the pc bounds as invalid,
14644 so that GDB will ignore it. */
976ca316 14645 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14646 return PC_BOUNDS_INVALID;
c906108c
SS
14647
14648 *lowpc = low;
96408a79
SA
14649 if (highpc)
14650 *highpc = high;
af34e669 14651 return ret;
c906108c
SS
14652}
14653
b084d499
JB
14654/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14655 its low and high PC addresses. Do nothing if these addresses could not
14656 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14657 and HIGHPC to the high address if greater than HIGHPC. */
14658
14659static void
14660dwarf2_get_subprogram_pc_bounds (struct die_info *die,
dda83cd7
SM
14661 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14662 struct dwarf2_cu *cu)
b084d499
JB
14663{
14664 CORE_ADDR low, high;
14665 struct die_info *child = die->child;
14666
e385593e 14667 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14668 {
325fac50
PA
14669 *lowpc = std::min (*lowpc, low);
14670 *highpc = std::max (*highpc, high);
b084d499
JB
14671 }
14672
14673 /* If the language does not allow nested subprograms (either inside
14674 subprograms or lexical blocks), we're done. */
14675 if (cu->language != language_ada)
14676 return;
6e70227d 14677
b084d499
JB
14678 /* Check all the children of the given DIE. If it contains nested
14679 subprograms, then check their pc bounds. Likewise, we need to
14680 check lexical blocks as well, as they may also contain subprogram
14681 definitions. */
14682 while (child && child->tag)
14683 {
14684 if (child->tag == DW_TAG_subprogram
dda83cd7
SM
14685 || child->tag == DW_TAG_lexical_block)
14686 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14687 child = child->sibling;
b084d499
JB
14688 }
14689}
14690
fae299cd
DC
14691/* Get the low and high pc's represented by the scope DIE, and store
14692 them in *LOWPC and *HIGHPC. If the correct values can't be
14693 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14694
14695static void
14696get_scope_pc_bounds (struct die_info *die,
14697 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14698 struct dwarf2_cu *cu)
14699{
14700 CORE_ADDR best_low = (CORE_ADDR) -1;
14701 CORE_ADDR best_high = (CORE_ADDR) 0;
14702 CORE_ADDR current_low, current_high;
14703
3a2b436a 14704 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14705 >= PC_BOUNDS_RANGES)
fae299cd
DC
14706 {
14707 best_low = current_low;
14708 best_high = current_high;
14709 }
14710 else
14711 {
14712 struct die_info *child = die->child;
14713
14714 while (child && child->tag)
14715 {
14716 switch (child->tag) {
14717 case DW_TAG_subprogram:
dda83cd7 14718 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14719 break;
14720 case DW_TAG_namespace:
f55ee35c 14721 case DW_TAG_module:
fae299cd
DC
14722 /* FIXME: carlton/2004-01-16: Should we do this for
14723 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14724 that current GCC's always emit the DIEs corresponding
14725 to definitions of methods of classes as children of a
14726 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14727 the DIEs giving the declarations, which could be
14728 anywhere). But I don't see any reason why the
14729 standards says that they have to be there. */
14730 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14731
14732 if (current_low != ((CORE_ADDR) -1))
14733 {
325fac50
PA
14734 best_low = std::min (best_low, current_low);
14735 best_high = std::max (best_high, current_high);
fae299cd
DC
14736 }
14737 break;
14738 default:
0963b4bd 14739 /* Ignore. */
fae299cd
DC
14740 break;
14741 }
14742
436c571c 14743 child = child->sibling;
fae299cd
DC
14744 }
14745 }
14746
14747 *lowpc = best_low;
14748 *highpc = best_high;
14749}
14750
801e3a5b
JB
14751/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14752 in DIE. */
380bca97 14753
801e3a5b
JB
14754static void
14755dwarf2_record_block_ranges (struct die_info *die, struct block *block,
dda83cd7 14756 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
801e3a5b 14757{
5e22e966 14758 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14759 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14760 struct attribute *attr;
91da1414 14761 struct attribute *attr_high;
801e3a5b 14762
91da1414
MW
14763 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14764 if (attr_high)
801e3a5b 14765 {
801e3a5b 14766 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14767 if (attr != nullptr)
dda83cd7 14768 {
95f982e5
TT
14769 CORE_ADDR low = attr->as_address ();
14770 CORE_ADDR high = attr_high->as_address ();
31aa7e4e 14771
cd6c91b4 14772 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14773 high += low;
9a619af0 14774
3e29f34a
MR
14775 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14776 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14777 cu->get_builder ()->record_block_range (block, low, high - 1);
dda83cd7 14778 }
801e3a5b
JB
14779 }
14780
14781 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14782 if (attr != nullptr && attr->form_is_unsigned ())
801e3a5b 14783 {
2b0c7f41
SM
14784 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14785 on DWARF version). */
14786 ULONGEST ranges_offset = attr->as_unsigned ();
801e3a5b 14787
2b0c7f41
SM
14788 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14789 this value. */
14790 if (die->tag != DW_TAG_compile_unit)
14791 ranges_offset += cu->gnu_ranges_base;
801e3a5b 14792
2d5f09ec 14793 std::vector<blockrange> blockvec;
2b0c7f41 14794 dwarf2_ranges_process (ranges_offset, cu, die->tag,
5f46c5a5
JK
14795 [&] (CORE_ADDR start, CORE_ADDR end)
14796 {
58fdfd2c
JK
14797 start += baseaddr;
14798 end += baseaddr;
5f46c5a5
JK
14799 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14800 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14801 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14802 blockvec.emplace_back (start, end);
5f46c5a5 14803 });
2d5f09ec
KB
14804
14805 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14806 }
14807}
14808
685b1105
JK
14809/* Check whether the producer field indicates either of GCC < 4.6, or the
14810 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14811
685b1105
JK
14812static void
14813check_producer (struct dwarf2_cu *cu)
60d5a603 14814{
38360086 14815 int major, minor;
60d5a603
JK
14816
14817 if (cu->producer == NULL)
14818 {
14819 /* For unknown compilers expect their behavior is DWARF version
14820 compliant.
14821
14822 GCC started to support .debug_types sections by -gdwarf-4 since
14823 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14824 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14825 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14826 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14827 }
b1ffba5a 14828 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14829 {
38360086
MW
14830 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14831 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14832 }
5230b05a 14833 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14834 {
14835 cu->producer_is_icc = true;
14836 cu->producer_is_icc_lt_14 = major < 14;
14837 }
c258c396
JD
14838 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14839 cu->producer_is_codewarrior = true;
685b1105
JK
14840 else
14841 {
14842 /* For other non-GCC compilers, expect their behavior is DWARF version
14843 compliant. */
60d5a603
JK
14844 }
14845
9068261f 14846 cu->checked_producer = true;
685b1105 14847}
ba919b58 14848
685b1105
JK
14849/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14850 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14851 during 4.6.0 experimental. */
14852
9068261f 14853static bool
685b1105
JK
14854producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14855{
14856 if (!cu->checked_producer)
14857 check_producer (cu);
14858
14859 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14860}
14861
c258c396
JD
14862
14863/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14864 with incorrect is_stmt attributes. */
14865
14866static bool
14867producer_is_codewarrior (struct dwarf2_cu *cu)
14868{
14869 if (!cu->checked_producer)
14870 check_producer (cu);
14871
14872 return cu->producer_is_codewarrior;
14873}
14874
bf23a268
TT
14875/* Return the accessibility of DIE, as given by DW_AT_accessibility.
14876 If that attribute is not available, return the appropriate
14877 default. */
60d5a603
JK
14878
14879static enum dwarf_access_attribute
bf23a268 14880dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
60d5a603 14881{
bf23a268
TT
14882 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14883 if (attr != nullptr)
14884 {
14885 LONGEST value = attr->constant_value (-1);
14886 if (value == DW_ACCESS_public
14887 || value == DW_ACCESS_protected
14888 || value == DW_ACCESS_private)
14889 return (dwarf_access_attribute) value;
14890 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14891 plongest (value));
14892 }
14893
60d5a603
JK
14894 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14895 {
14896 /* The default DWARF 2 accessibility for members is public, the default
14897 accessibility for inheritance is private. */
14898
14899 if (die->tag != DW_TAG_inheritance)
14900 return DW_ACCESS_public;
14901 else
14902 return DW_ACCESS_private;
14903 }
14904 else
14905 {
14906 /* DWARF 3+ defines the default accessibility a different way. The same
14907 rules apply now for DW_TAG_inheritance as for the members and it only
14908 depends on the container kind. */
14909
14910 if (die->parent->tag == DW_TAG_class_type)
14911 return DW_ACCESS_private;
14912 else
14913 return DW_ACCESS_public;
14914 }
14915}
14916
74ac6d43
TT
14917/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14918 offset. If the attribute was not found return 0, otherwise return
14919 1. If it was found but could not properly be handled, set *OFFSET
14920 to 0. */
14921
14922static int
14923handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14924 LONGEST *offset)
14925{
14926 struct attribute *attr;
14927
14928 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14929 if (attr != NULL)
14930 {
14931 *offset = 0;
14932
14933 /* Note that we do not check for a section offset first here.
14934 This is because DW_AT_data_member_location is new in DWARF 4,
14935 so if we see it, we can assume that a constant form is really
14936 a constant and not a section offset. */
cd6c91b4 14937 if (attr->form_is_constant ())
0826b30a 14938 *offset = attr->constant_value (0);
cd6c91b4 14939 else if (attr->form_is_section_offset ())
74ac6d43 14940 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14941 else if (attr->form_is_block ())
9d2246fc 14942 *offset = decode_locdesc (attr->as_block (), cu);
74ac6d43
TT
14943 else
14944 dwarf2_complex_location_expr_complaint ();
14945
14946 return 1;
14947 }
14948
14949 return 0;
14950}
14951
7d79de9a
TT
14952/* Look for DW_AT_data_member_location and store the results in FIELD. */
14953
14954static void
14955handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14956 struct field *field)
14957{
14958 struct attribute *attr;
14959
14960 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14961 if (attr != NULL)
14962 {
14963 if (attr->form_is_constant ())
14964 {
14965 LONGEST offset = attr->constant_value (0);
14966 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14967 }
14968 else if (attr->form_is_section_offset ())
14969 dwarf2_complex_location_expr_complaint ();
14970 else if (attr->form_is_block ())
14971 {
14972 bool handled;
9d2246fc 14973 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
7d79de9a
TT
14974 if (handled)
14975 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14976 else
14977 {
5e22e966
SM
14978 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14979 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14980 struct dwarf2_locexpr_baton *dlbaton
14981 = XOBNEW (&objfile->objfile_obstack,
14982 struct dwarf2_locexpr_baton);
9d2246fc
TT
14983 dlbaton->data = attr->as_block ()->data;
14984 dlbaton->size = attr->as_block ()->size;
7d79de9a
TT
14985 /* When using this baton, we want to compute the address
14986 of the field, not the value. This is why
14987 is_reference is set to false here. */
14988 dlbaton->is_reference = false;
5e22e966 14989 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14990 dlbaton->per_cu = cu->per_cu;
14991
14992 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14993 }
14994 }
14995 else
14996 dwarf2_complex_location_expr_complaint ();
14997 }
14998}
14999
c906108c
SS
15000/* Add an aggregate field to the field list. */
15001
15002static void
107d2387 15003dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15004 struct dwarf2_cu *cu)
6e70227d 15005{
5e22e966 15006 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 15007 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
15008 struct nextfield *new_field;
15009 struct attribute *attr;
15010 struct field *fp;
15d034d0 15011 const char *fieldname = "";
c906108c 15012
7d0ccb61
DJ
15013 if (die->tag == DW_TAG_inheritance)
15014 {
be2daae6
TT
15015 fip->baseclasses.emplace_back ();
15016 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15017 }
15018 else
15019 {
be2daae6
TT
15020 fip->fields.emplace_back ();
15021 new_field = &fip->fields.back ();
7d0ccb61 15022 }
be2daae6 15023
9c6a1327
TT
15024 new_field->offset = die->sect_off;
15025
bf23a268 15026 new_field->accessibility = dwarf2_access_attribute (die, cu);
c906108c 15027 if (new_field->accessibility != DW_ACCESS_public)
264fc0e2 15028 fip->non_public_fields = true;
60d5a603 15029
e142c38c 15030 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 15031 if (attr != nullptr)
23dca5c3 15032 new_field->virtuality = attr->as_virtuality ();
60d5a603
JK
15033 else
15034 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15035
15036 fp = &new_field->field;
a9a9bd0f 15037
e142c38c 15038 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15039 {
a9a9bd0f 15040 /* Data member other than a C++ static data member. */
6e70227d 15041
c906108c 15042 /* Get type of field. */
5d14b6e5 15043 fp->set_type (die_type (die, cu));
c906108c 15044
d6a843b5 15045 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15046
c906108c 15047 /* Get bit size of field (zero if none). */
e142c38c 15048 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 15049 if (attr != nullptr)
c906108c 15050 {
529908cb 15051 FIELD_BITSIZE (*fp) = attr->constant_value (0);
c906108c
SS
15052 }
15053 else
15054 {
15055 FIELD_BITSIZE (*fp) = 0;
15056 }
15057
15058 /* Get bit offset of field. */
7d79de9a 15059 handle_data_member_location (die, cu, fp);
e142c38c 15060 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7c184d33 15061 if (attr != nullptr && attr->form_is_constant ())
c906108c 15062 {
d5a22e77 15063 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
15064 {
15065 /* For big endian bits, the DW_AT_bit_offset gives the
dda83cd7
SM
15066 additional bit offset from the MSB of the containing
15067 anonymous object to the MSB of the field. We don't
15068 have to do anything special since we don't need to
15069 know the size of the anonymous object. */
529908cb 15070 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
7c184d33 15071 + attr->constant_value (0)));
c906108c
SS
15072 }
15073 else
15074 {
15075 /* For little endian bits, compute the bit offset to the
dda83cd7
SM
15076 MSB of the anonymous object, subtract off the number of
15077 bits from the MSB of the field to the MSB of the
15078 object, and then subtract off the number of bits of
15079 the field itself. The result is the bit offset of
15080 the LSB of the field. */
c906108c 15081 int anonymous_size;
7c184d33 15082 int bit_offset = attr->constant_value (0);
c906108c 15083
e142c38c 15084 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7c184d33 15085 if (attr != nullptr && attr->form_is_constant ())
c906108c
SS
15086 {
15087 /* The size of the anonymous object containing
15088 the bit field is explicit, so use the
15089 indicated size (in bytes). */
7c184d33 15090 anonymous_size = attr->constant_value (0);
c906108c
SS
15091 }
15092 else
15093 {
15094 /* The size of the anonymous object containing
15095 the bit field must be inferred from the type
15096 attribute of the data member containing the
15097 bit field. */
5d14b6e5 15098 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 15099 }
f41f5e61
PA
15100 SET_FIELD_BITPOS (*fp,
15101 (FIELD_BITPOS (*fp)
15102 + anonymous_size * bits_per_byte
15103 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15104 }
15105 }
da5b30da
AA
15106 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15107 if (attr != NULL)
15108 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 15109 + attr->constant_value (0)));
c906108c
SS
15110
15111 /* Get name of field. */
39cbfefa
DJ
15112 fieldname = dwarf2_name (die, cu);
15113 if (fieldname == NULL)
15114 fieldname = "";
d8151005
DJ
15115
15116 /* The name is already allocated along with this objfile, so we don't
15117 need to duplicate it for the type. */
15118 fp->name = fieldname;
c906108c
SS
15119
15120 /* Change accessibility for artificial fields (e.g. virtual table
dda83cd7 15121 pointer or virtual base class pointer) to private. */
e142c38c 15122 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15123 {
d48cc9dd 15124 FIELD_ARTIFICIAL (*fp) = 1;
c906108c 15125 new_field->accessibility = DW_ACCESS_private;
264fc0e2 15126 fip->non_public_fields = true;
c906108c
SS
15127 }
15128 }
a9a9bd0f 15129 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15130 {
a9a9bd0f
DC
15131 /* C++ static member. */
15132
15133 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15134 is a declaration, but all versions of G++ as of this writing
15135 (so through at least 3.2.1) incorrectly generate
15136 DW_TAG_variable tags. */
6e70227d 15137
ff355380 15138 const char *physname;
c906108c 15139
a9a9bd0f 15140 /* Get name of field. */
39cbfefa
DJ
15141 fieldname = dwarf2_name (die, cu);
15142 if (fieldname == NULL)
c906108c
SS
15143 return;
15144
254e6b9e 15145 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15146 if (attr
15147 /* Only create a symbol if this is an external value.
15148 new_symbol checks this and puts the value in the global symbol
15149 table, which we want. If it is not external, new_symbol
15150 will try to put the value in cu->list_in_scope which is wrong. */
15151 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15152 {
15153 /* A static const member, not much different than an enum as far as
15154 we're concerned, except that we can support more types. */
15155 new_symbol (die, NULL, cu);
15156 }
15157
2df3850c 15158 /* Get physical name. */
ff355380 15159 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15160
d8151005
DJ
15161 /* The name is already allocated along with this objfile, so we don't
15162 need to duplicate it for the type. */
15163 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 15164 fp->set_type (die_type (die, cu));
d8151005 15165 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15166 }
15167 else if (die->tag == DW_TAG_inheritance)
15168 {
74ac6d43 15169 /* C++ base class field. */
7d79de9a 15170 handle_data_member_location (die, cu, fp);
c906108c 15171 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
15172 fp->set_type (die_type (die, cu));
15173 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 15174 }
2ddeaf8a
TT
15175 else
15176 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15177}
15178
883fd55a
KS
15179/* Can the type given by DIE define another type? */
15180
15181static bool
15182type_can_define_types (const struct die_info *die)
15183{
15184 switch (die->tag)
15185 {
15186 case DW_TAG_typedef:
15187 case DW_TAG_class_type:
15188 case DW_TAG_structure_type:
15189 case DW_TAG_union_type:
15190 case DW_TAG_enumeration_type:
15191 return true;
15192
15193 default:
15194 return false;
15195 }
15196}
15197
15198/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15199
15200static void
883fd55a
KS
15201dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15202 struct dwarf2_cu *cu)
6e70227d 15203{
be2daae6
TT
15204 struct decl_field fp;
15205 memset (&fp, 0, sizeof (fp));
98751a41 15206
883fd55a 15207 gdb_assert (type_can_define_types (die));
98751a41 15208
883fd55a 15209 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15210 fp.name = dwarf2_name (die, cu);
15211 fp.type = read_type_die (die, cu);
98751a41 15212
c191a687 15213 /* Save accessibility. */
bf23a268 15214 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
c191a687
KS
15215 switch (accessibility)
15216 {
15217 case DW_ACCESS_public:
15218 /* The assumed value if neither private nor protected. */
15219 break;
15220 case DW_ACCESS_private:
be2daae6 15221 fp.is_private = 1;
c191a687
KS
15222 break;
15223 case DW_ACCESS_protected:
be2daae6 15224 fp.is_protected = 1;
c191a687 15225 break;
c191a687
KS
15226 }
15227
883fd55a 15228 if (die->tag == DW_TAG_typedef)
be2daae6 15229 fip->typedef_field_list.push_back (fp);
883fd55a 15230 else
be2daae6 15231 fip->nested_types_list.push_back (fp);
98751a41
JK
15232}
15233
9c6a1327
TT
15234/* A convenience typedef that's used when finding the discriminant
15235 field for a variant part. */
1b95cdb7
SM
15236typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15237 offset_map_type;
9c6a1327
TT
15238
15239/* Compute the discriminant range for a given variant. OBSTACK is
15240 where the results will be stored. VARIANT is the variant to
15241 process. IS_UNSIGNED indicates whether the discriminant is signed
15242 or unsigned. */
15243
15244static const gdb::array_view<discriminant_range>
15245convert_variant_range (struct obstack *obstack, const variant_field &variant,
15246 bool is_unsigned)
15247{
15248 std::vector<discriminant_range> ranges;
15249
15250 if (variant.default_branch)
15251 return {};
15252
15253 if (variant.discr_list_data == nullptr)
15254 {
15255 discriminant_range r
15256 = {variant.discriminant_value, variant.discriminant_value};
15257 ranges.push_back (r);
15258 }
15259 else
15260 {
15261 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15262 variant.discr_list_data->size);
15263 while (!data.empty ())
15264 {
15265 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15266 {
15267 complaint (_("invalid discriminant marker: %d"), data[0]);
15268 break;
15269 }
15270 bool is_range = data[0] == DW_DSC_range;
15271 data = data.slice (1);
15272
15273 ULONGEST low, high;
15274 unsigned int bytes_read;
15275
15276 if (data.empty ())
15277 {
15278 complaint (_("DW_AT_discr_list missing low value"));
15279 break;
15280 }
15281 if (is_unsigned)
15282 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15283 else
15284 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15285 &bytes_read);
15286 data = data.slice (bytes_read);
15287
15288 if (is_range)
15289 {
15290 if (data.empty ())
15291 {
15292 complaint (_("DW_AT_discr_list missing high value"));
15293 break;
15294 }
15295 if (is_unsigned)
15296 high = read_unsigned_leb128 (nullptr, data.data (),
15297 &bytes_read);
15298 else
15299 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15300 &bytes_read);
15301 data = data.slice (bytes_read);
15302 }
15303 else
15304 high = low;
15305
15306 ranges.push_back ({ low, high });
15307 }
15308 }
15309
15310 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15311 ranges.size ());
15312 std::copy (ranges.begin (), ranges.end (), result);
15313 return gdb::array_view<discriminant_range> (result, ranges.size ());
15314}
15315
15316static const gdb::array_view<variant_part> create_variant_parts
15317 (struct obstack *obstack,
15318 const offset_map_type &offset_map,
15319 struct field_info *fi,
15320 const std::vector<variant_part_builder> &variant_parts);
15321
15322/* Fill in a "struct variant" for a given variant field. RESULT is
15323 the variant to fill in. OBSTACK is where any needed allocations
15324 will be done. OFFSET_MAP holds the mapping from section offsets to
15325 fields for the type. FI describes the fields of the type we're
15326 processing. FIELD is the variant field we're converting. */
15327
15328static void
15329create_one_variant (variant &result, struct obstack *obstack,
15330 const offset_map_type &offset_map,
15331 struct field_info *fi, const variant_field &field)
15332{
15333 result.discriminants = convert_variant_range (obstack, field, false);
15334 result.first_field = field.first_field + fi->baseclasses.size ();
15335 result.last_field = field.last_field + fi->baseclasses.size ();
15336 result.parts = create_variant_parts (obstack, offset_map, fi,
15337 field.variant_parts);
15338}
15339
15340/* Fill in a "struct variant_part" for a given variant part. RESULT
15341 is the variant part to fill in. OBSTACK is where any needed
15342 allocations will be done. OFFSET_MAP holds the mapping from
15343 section offsets to fields for the type. FI describes the fields of
15344 the type we're processing. BUILDER is the variant part to be
15345 converted. */
15346
15347static void
15348create_one_variant_part (variant_part &result,
15349 struct obstack *obstack,
15350 const offset_map_type &offset_map,
15351 struct field_info *fi,
15352 const variant_part_builder &builder)
15353{
15354 auto iter = offset_map.find (builder.discriminant_offset);
15355 if (iter == offset_map.end ())
15356 {
15357 result.discriminant_index = -1;
15358 /* Doesn't matter. */
15359 result.is_unsigned = false;
15360 }
15361 else
15362 {
15363 result.discriminant_index = iter->second;
15364 result.is_unsigned
c6d940a9 15365 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
9c6a1327
TT
15366 }
15367
15368 size_t n = builder.variants.size ();
15369 variant *output = new (obstack) variant[n];
15370 for (size_t i = 0; i < n; ++i)
15371 create_one_variant (output[i], obstack, offset_map, fi,
15372 builder.variants[i]);
15373
15374 result.variants = gdb::array_view<variant> (output, n);
15375}
15376
15377/* Create a vector of variant parts that can be attached to a type.
15378 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15379 holds the mapping from section offsets to fields for the type. FI
15380 describes the fields of the type we're processing. VARIANT_PARTS
15381 is the vector to convert. */
15382
15383static const gdb::array_view<variant_part>
15384create_variant_parts (struct obstack *obstack,
15385 const offset_map_type &offset_map,
15386 struct field_info *fi,
15387 const std::vector<variant_part_builder> &variant_parts)
15388{
15389 if (variant_parts.empty ())
15390 return {};
15391
15392 size_t n = variant_parts.size ();
15393 variant_part *result = new (obstack) variant_part[n];
15394 for (size_t i = 0; i < n; ++i)
15395 create_one_variant_part (result[i], obstack, offset_map, fi,
15396 variant_parts[i]);
15397
15398 return gdb::array_view<variant_part> (result, n);
15399}
15400
15401/* Compute the variant part vector for FIP, attaching it to TYPE when
15402 done. */
15403
15404static void
15405add_variant_property (struct field_info *fip, struct type *type,
15406 struct dwarf2_cu *cu)
15407{
15408 /* Map section offsets of fields to their field index. Note the
15409 field index here does not take the number of baseclasses into
15410 account. */
15411 offset_map_type offset_map;
15412 for (int i = 0; i < fip->fields.size (); ++i)
15413 offset_map[fip->fields[i].offset] = i;
15414
5e22e966 15415 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
15416 gdb::array_view<variant_part> parts
15417 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15418 fip->variant_parts);
15419
15420 struct dynamic_prop prop;
8c2e4e06
SM
15421 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15422 obstack_copy (&objfile->objfile_obstack, &parts,
15423 sizeof (parts)));
9c6a1327 15424
5c54719c 15425 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
15426}
15427
c906108c
SS
15428/* Create the vector of fields, and attach it to the type. */
15429
15430static void
fba45db2 15431dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15432 struct dwarf2_cu *cu)
c906108c 15433{
317f7127 15434 int nfields = fip->nfields ();
c906108c
SS
15435
15436 /* Record the field count, allocate space for the array of fields,
15437 and create blank accessibility bitfields if necessary. */
5e33d5f4 15438 type->set_num_fields (nfields);
3cabb6b0
SM
15439 type->set_fields
15440 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 15441
b4ba55a1 15442 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15443 {
15444 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15445
15446 TYPE_FIELD_PRIVATE_BITS (type) =
15447 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15448 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15449
15450 TYPE_FIELD_PROTECTED_BITS (type) =
15451 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15452 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15453
774b6a14
TT
15454 TYPE_FIELD_IGNORE_BITS (type) =
15455 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15456 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15457 }
15458
15459 /* If the type has baseclasses, allocate and clear a bit vector for
15460 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15461 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15462 {
be2daae6 15463 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15464 unsigned char *pointer;
c906108c
SS
15465
15466 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15467 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15468 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15469 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15470 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15471 }
15472
9c6a1327
TT
15473 if (!fip->variant_parts.empty ())
15474 add_variant_property (fip, type, cu);
2ddeaf8a 15475
be2daae6
TT
15476 /* Copy the saved-up fields into the field vector. */
15477 for (int i = 0; i < nfields; ++i)
c906108c 15478 {
be2daae6
TT
15479 struct nextfield &field
15480 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15481 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15482
ceacbf6e 15483 type->field (i) = field.field;
be2daae6 15484 switch (field.accessibility)
c906108c 15485 {
c5aa993b 15486 case DW_ACCESS_private:
b4ba55a1 15487 if (cu->language != language_ada)
be2daae6 15488 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15489 break;
c906108c 15490
c5aa993b 15491 case DW_ACCESS_protected:
b4ba55a1 15492 if (cu->language != language_ada)
be2daae6 15493 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15494 break;
c906108c 15495
c5aa993b
JM
15496 case DW_ACCESS_public:
15497 break;
c906108c 15498
c5aa993b
JM
15499 default:
15500 /* Unknown accessibility. Complain and treat it as public. */
15501 {
b98664d3 15502 complaint (_("unsupported accessibility %d"),
be2daae6 15503 field.accessibility);
c5aa993b
JM
15504 }
15505 break;
c906108c 15506 }
be2daae6 15507 if (i < fip->baseclasses.size ())
c906108c 15508 {
be2daae6 15509 switch (field.virtuality)
c906108c 15510 {
c5aa993b
JM
15511 case DW_VIRTUALITY_virtual:
15512 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15513 if (cu->language == language_ada)
a73c6dcd 15514 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15515 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15516 break;
c906108c
SS
15517 }
15518 }
c906108c
SS
15519 }
15520}
15521
7d27a96d
TT
15522/* Return true if this member function is a constructor, false
15523 otherwise. */
15524
15525static int
15526dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15527{
15528 const char *fieldname;
fe978cb0 15529 const char *type_name;
7d27a96d
TT
15530 int len;
15531
15532 if (die->parent == NULL)
15533 return 0;
15534
15535 if (die->parent->tag != DW_TAG_structure_type
15536 && die->parent->tag != DW_TAG_union_type
15537 && die->parent->tag != DW_TAG_class_type)
15538 return 0;
15539
15540 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15541 type_name = dwarf2_name (die->parent, cu);
15542 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15543 return 0;
15544
15545 len = strlen (fieldname);
fe978cb0
PA
15546 return (strncmp (fieldname, type_name, len) == 0
15547 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15548}
15549
c906108c
SS
15550/* Add a member function to the proper fieldlist. */
15551
15552static void
107d2387 15553dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15554 struct type *type, struct dwarf2_cu *cu)
c906108c 15555{
5e22e966 15556 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15557 struct attribute *attr;
c906108c 15558 int i;
be2daae6 15559 struct fnfieldlist *flp = nullptr;
c906108c 15560 struct fn_field *fnp;
15d034d0 15561 const char *fieldname;
f792889a 15562 struct type *this_type;
c906108c 15563
b4ba55a1 15564 if (cu->language == language_ada)
a73c6dcd 15565 error (_("unexpected member function in Ada type"));
b4ba55a1 15566
2df3850c 15567 /* Get name of member function. */
39cbfefa
DJ
15568 fieldname = dwarf2_name (die, cu);
15569 if (fieldname == NULL)
2df3850c 15570 return;
c906108c 15571
c906108c 15572 /* Look up member function name in fieldlist. */
be2daae6 15573 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15574 {
27bfe10e 15575 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15576 {
15577 flp = &fip->fnfieldlists[i];
15578 break;
15579 }
c906108c
SS
15580 }
15581
be2daae6
TT
15582 /* Create a new fnfieldlist if necessary. */
15583 if (flp == nullptr)
c906108c 15584 {
be2daae6
TT
15585 fip->fnfieldlists.emplace_back ();
15586 flp = &fip->fnfieldlists.back ();
c906108c 15587 flp->name = fieldname;
be2daae6 15588 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15589 }
15590
be2daae6
TT
15591 /* Create a new member function field and add it to the vector of
15592 fnfieldlists. */
15593 flp->fnfields.emplace_back ();
15594 fnp = &flp->fnfields.back ();
3da10d80
KS
15595
15596 /* Delay processing of the physname until later. */
9c37b5ae 15597 if (cu->language == language_cplus)
be2daae6
TT
15598 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15599 die, cu);
3da10d80
KS
15600 else
15601 {
1d06ead6 15602 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15603 fnp->physname = physname ? physname : "";
15604 }
15605
c906108c 15606 fnp->type = alloc_type (objfile);
f792889a 15607 this_type = read_type_die (die, cu);
78134374 15608 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15609 {
1f704f76 15610 int nparams = this_type->num_fields ();
c906108c 15611
f792889a 15612 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15613 of the method itself (TYPE_CODE_METHOD). */
15614 smash_to_method_type (fnp->type, type,
f792889a 15615 TYPE_TARGET_TYPE (this_type),
80fc5e77 15616 this_type->fields (),
1f704f76 15617 this_type->num_fields (),
a409645d 15618 this_type->has_varargs ());
c906108c
SS
15619
15620 /* Handle static member functions.
dda83cd7
SM
15621 Dwarf2 has no clean way to discern C++ static and non-static
15622 member functions. G++ helps GDB by marking the first
15623 parameter for non-static member functions (which is the this
15624 pointer) as artificial. We obtain this information from
15625 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15626 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15627 fnp->voffset = VOFFSET_STATIC;
15628 }
15629 else
b98664d3 15630 complaint (_("member function type missing for '%s'"),
3da10d80 15631 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15632
15633 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15634 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15635 fnp->fcontext = die_containing_type (die, cu);
c906108c 15636
3e43a32a
MS
15637 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15638 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15639
15640 /* Get accessibility. */
bf23a268 15641 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
60d5a603 15642 switch (accessibility)
c906108c 15643 {
60d5a603
JK
15644 case DW_ACCESS_private:
15645 fnp->is_private = 1;
15646 break;
15647 case DW_ACCESS_protected:
15648 fnp->is_protected = 1;
15649 break;
c906108c
SS
15650 }
15651
b02dede2 15652 /* Check for artificial methods. */
e142c38c 15653 attr = dwarf2_attr (die, DW_AT_artificial, cu);
c45bc3f8 15654 if (attr && attr->as_boolean ())
b02dede2
DJ
15655 fnp->is_artificial = 1;
15656
e35000a7
TBA
15657 /* Check for defaulted methods. */
15658 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
e8e5c158
TT
15659 if (attr != nullptr)
15660 fnp->defaulted = attr->defaulted ();
e35000a7
TBA
15661
15662 /* Check for deleted methods. */
15663 attr = dwarf2_attr (die, DW_AT_deleted, cu);
c45bc3f8 15664 if (attr != nullptr && attr->as_boolean ())
e35000a7
TBA
15665 fnp->is_deleted = 1;
15666
7d27a96d
TT
15667 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15668
0d564a31 15669 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15670 function. For older versions of GCC, this is an offset in the
15671 appropriate virtual table, as specified by DW_AT_containing_type.
15672 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15673 to the object address. */
15674
e142c38c 15675 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15676 if (attr != nullptr)
8e19ed76 15677 {
9d2246fc 15678 if (attr->form_is_block () && attr->as_block ()->size > 0)
dda83cd7 15679 {
9d2246fc
TT
15680 struct dwarf_block *block = attr->as_block ();
15681
15682 if (block->data[0] == DW_OP_constu)
aec5aa8b
TT
15683 {
15684 /* Old-style GCC. */
9d2246fc 15685 fnp->voffset = decode_locdesc (block, cu) + 2;
aec5aa8b 15686 }
9d2246fc
TT
15687 else if (block->data[0] == DW_OP_deref
15688 || (block->size > 1
15689 && block->data[0] == DW_OP_deref_size
15690 && block->data[1] == cu->header.addr_size))
aec5aa8b 15691 {
9d2246fc 15692 fnp->voffset = decode_locdesc (block, cu);
aec5aa8b
TT
15693 if ((fnp->voffset % cu->header.addr_size) != 0)
15694 dwarf2_complex_location_expr_complaint ();
15695 else
15696 fnp->voffset /= cu->header.addr_size;
15697 fnp->voffset += 2;
15698 }
15699 else
15700 dwarf2_complex_location_expr_complaint ();
15701
15702 if (!fnp->fcontext)
7e993ebf
KS
15703 {
15704 /* If there is no `this' field and no DW_AT_containing_type,
15705 we cannot actually find a base class context for the
15706 vtable! */
1f704f76 15707 if (this_type->num_fields () == 0
7e993ebf
KS
15708 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15709 {
b98664d3 15710 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15711 "function \"%s\" (offset %s)"),
15712 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15713 }
15714 else
15715 {
15716 fnp->fcontext
940da03e 15717 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15718 }
15719 }
aec5aa8b 15720 }
cd6c91b4 15721 else if (attr->form_is_section_offset ())
dda83cd7 15722 {
4d3c2250 15723 dwarf2_complex_location_expr_complaint ();
dda83cd7 15724 }
8e19ed76 15725 else
dda83cd7 15726 {
4d3c2250
KB
15727 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15728 fieldname);
dda83cd7 15729 }
0d564a31 15730 }
d48cc9dd
DJ
15731 else
15732 {
15733 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
23dca5c3 15734 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
d48cc9dd
DJ
15735 {
15736 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15737 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15738 "but the vtable offset is not specified"),
9d8780f0 15739 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15740 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15741 TYPE_CPLUS_DYNAMIC (type) = 1;
15742 }
15743 }
c906108c
SS
15744}
15745
15746/* Create the vector of member function fields, and attach it to the type. */
15747
15748static void
fba45db2 15749dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15750 struct dwarf2_cu *cu)
c906108c 15751{
b4ba55a1 15752 if (cu->language == language_ada)
a73c6dcd 15753 error (_("unexpected member functions in Ada type"));
b4ba55a1 15754
c906108c
SS
15755 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15756 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15757 TYPE_ALLOC (type,
15758 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15759
be2daae6 15760 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15761 {
be2daae6 15762 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15763 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15764
be2daae6
TT
15765 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15766 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15767 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15768 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15769
15770 for (int k = 0; k < nf.fnfields.size (); ++k)
15771 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15772 }
15773
be2daae6 15774 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15775}
15776
1168df01
JB
15777/* Returns non-zero if NAME is the name of a vtable member in CU's
15778 language, zero otherwise. */
15779static int
15780is_vtable_name (const char *name, struct dwarf2_cu *cu)
15781{
15782 static const char vptr[] = "_vptr";
15783
9c37b5ae
TT
15784 /* Look for the C++ form of the vtable. */
15785 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15786 return 1;
15787
15788 return 0;
15789}
15790
c0dd20ea 15791/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15792 functions, with the ABI-specified layout. If TYPE describes
15793 such a structure, smash it into a member function type.
61049d3b
DJ
15794
15795 GCC shouldn't do this; it should just output pointer to member DIEs.
15796 This is GCC PR debug/28767. */
c0dd20ea 15797
0b92b5bb
TT
15798static void
15799quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15800{
09e2d7c7 15801 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15802
15803 /* Check for a structure with no name and two children. */
1f704f76 15804 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15805 return;
c0dd20ea
DJ
15806
15807 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15808 if (TYPE_FIELD_NAME (type, 0) == NULL
15809 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15810 || TYPE_FIELD_NAME (type, 1) == NULL
15811 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15812 return;
c0dd20ea
DJ
15813
15814 /* Find the type of the method. */
940da03e 15815 pfn_type = type->field (0).type ();
c0dd20ea 15816 if (pfn_type == NULL
78134374
SM
15817 || pfn_type->code () != TYPE_CODE_PTR
15818 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15819 return;
c0dd20ea
DJ
15820
15821 /* Look for the "this" argument. */
15822 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15823 if (pfn_type->num_fields () == 0
940da03e
SM
15824 /* || pfn_type->field (0).type () == NULL */
15825 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15826 return;
c0dd20ea 15827
940da03e 15828 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15829 new_type = alloc_type (objfile);
09e2d7c7 15830 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15831 pfn_type->fields (), pfn_type->num_fields (),
a409645d 15832 pfn_type->has_varargs ());
0b92b5bb 15833 smash_to_methodptr_type (type, new_type);
c0dd20ea 15834}
1168df01 15835
e26624c6
TT
15836/* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15837 requires rewriting, then copy it and return the updated copy.
15838 Otherwise return nullptr. */
15839
15840static struct type *
15841rewrite_array_type (struct type *type)
15842{
15843 if (type->code () != TYPE_CODE_ARRAY)
15844 return nullptr;
15845
15846 struct type *index_type = type->index_type ();
15847 range_bounds *current_bounds = index_type->bounds ();
15848
15849 /* Handle multi-dimensional arrays. */
15850 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15851 if (new_target == nullptr)
15852 {
15853 /* Maybe we don't need to rewrite this array. */
15854 if (current_bounds->low.kind () == PROP_CONST
15855 && current_bounds->high.kind () == PROP_CONST)
15856 return nullptr;
15857 }
15858
15859 /* Either the target type was rewritten, or the bounds have to be
15860 updated. Either way we want to copy the type and update
15861 everything. */
15862 struct type *copy = copy_type (type);
15863 int nfields = copy->num_fields ();
15864 field *new_fields
15865 = ((struct field *) TYPE_ZALLOC (copy,
15866 nfields * sizeof (struct field)));
15867 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15868 copy->set_fields (new_fields);
15869 if (new_target != nullptr)
15870 TYPE_TARGET_TYPE (copy) = new_target;
15871
15872 struct type *index_copy = copy_type (index_type);
15873 range_bounds *bounds
15874 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15875 sizeof (range_bounds));
15876 *bounds = *current_bounds;
15877 bounds->low.set_const_val (1);
15878 bounds->high.set_const_val (0);
15879 index_copy->set_bounds (bounds);
15880 copy->set_index_type (index_copy);
15881
15882 return copy;
15883}
15884
57567375
TT
15885/* While some versions of GCC will generate complicated DWARF for an
15886 array (see quirk_ada_thick_pointer), more recent versions were
15887 modified to emit an explicit thick pointer structure. However, in
15888 this case, the array still has DWARF expressions for its ranges,
15889 and these must be ignored. */
15890
15891static void
15892quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15893 struct type *type)
15894{
15895 gdb_assert (cu->language == language_ada);
15896
15897 /* Check for a structure with two children. */
15898 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15899 return;
15900
15901 /* Check for P_ARRAY and P_BOUNDS members. */
15902 if (TYPE_FIELD_NAME (type, 0) == NULL
15903 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15904 || TYPE_FIELD_NAME (type, 1) == NULL
15905 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15906 return;
15907
15908 /* Make sure we're looking at a pointer to an array. */
15909 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15910 return;
57567375 15911
e26624c6
TT
15912 /* The Ada code already knows how to handle these types, so all that
15913 we need to do is turn the bounds into static bounds. However, we
15914 don't want to rewrite existing array or index types in-place,
15915 because those may be referenced in other contexts where this
15916 rewriting is undesirable. */
15917 struct type *new_ary_type
15918 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15919 if (new_ary_type != nullptr)
15920 type->field (0).set_type (lookup_pointer_type (new_ary_type));
57567375
TT
15921}
15922
2b4424c3
TT
15923/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15924 appropriate error checking and issuing complaints if there is a
15925 problem. */
15926
15927static ULONGEST
15928get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15929{
15930 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15931
15932 if (attr == nullptr)
15933 return 0;
15934
cd6c91b4 15935 if (!attr->form_is_constant ())
2b4424c3 15936 {
b98664d3 15937 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15938 " - DIE at %s [in module %s]"),
15939 sect_offset_str (die->sect_off),
5e22e966 15940 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15941 return 0;
15942 }
15943
529908cb
TT
15944 LONGEST val = attr->constant_value (0);
15945 if (val < 0)
2b4424c3 15946 {
529908cb
TT
15947 complaint (_("DW_AT_alignment value must not be negative"
15948 " - DIE at %s [in module %s]"),
15949 sect_offset_str (die->sect_off),
15950 objfile_name (cu->per_objfile->objfile));
15951 return 0;
2b4424c3 15952 }
529908cb 15953 ULONGEST align = val;
2b4424c3
TT
15954
15955 if (align == 0)
15956 {
b98664d3 15957 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15958 " - DIE at %s [in module %s]"),
15959 sect_offset_str (die->sect_off),
5e22e966 15960 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15961 return 0;
15962 }
15963 if ((align & (align - 1)) != 0)
15964 {
b98664d3 15965 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15966 " - DIE at %s [in module %s]"),
15967 sect_offset_str (die->sect_off),
5e22e966 15968 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15969 return 0;
15970 }
15971
15972 return align;
15973}
15974
15975/* If the DIE has a DW_AT_alignment attribute, use its value to set
15976 the alignment for TYPE. */
15977
15978static void
15979maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15980 struct type *type)
15981{
15982 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15983 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15984 " - DIE at %s [in module %s]"),
15985 sect_offset_str (die->sect_off),
5e22e966 15986 objfile_name (cu->per_objfile->objfile));
2b4424c3 15987}
685b1105 15988
e35000a7
TBA
15989/* Check if the given VALUE is a valid enum dwarf_calling_convention
15990 constant for a type, according to DWARF5 spec, Table 5.5. */
15991
15992static bool
15993is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15994{
15995 switch (value)
15996 {
15997 case DW_CC_normal:
15998 case DW_CC_pass_by_reference:
15999 case DW_CC_pass_by_value:
16000 return true;
16001
16002 default:
16003 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 16004 "(%s) for a type"), pulongest (value));
e35000a7
TBA
16005 return false;
16006 }
16007}
16008
d0922fcf
TBA
16009/* Check if the given VALUE is a valid enum dwarf_calling_convention
16010 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
16011 also according to GNU-specific values (see include/dwarf2.h). */
16012
16013static bool
16014is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
16015{
16016 switch (value)
16017 {
16018 case DW_CC_normal:
16019 case DW_CC_program:
16020 case DW_CC_nocall:
16021 return true;
16022
16023 case DW_CC_GNU_renesas_sh:
16024 case DW_CC_GNU_borland_fastcall_i386:
16025 case DW_CC_GDB_IBM_OpenCL:
16026 return true;
16027
16028 default:
16029 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 16030 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
16031 return false;
16032 }
16033}
16034
c906108c 16035/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
16036 (definition) to create a type for the structure or union. Fill in
16037 the type's name and general properties; the members will not be
83655187
DE
16038 processed until process_structure_scope. A symbol table entry for
16039 the type will also not be done until process_structure_scope (assuming
16040 the type has a name).
c906108c 16041
c767944b
DJ
16042 NOTE: we need to call these functions regardless of whether or not the
16043 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 16044 structure or union. This gets the type entered into our set of
83655187 16045 user defined types. */
c906108c 16046
f792889a 16047static struct type *
134d01f1 16048read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16049{
5e22e966 16050 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
16051 struct type *type;
16052 struct attribute *attr;
15d034d0 16053 const char *name;
c906108c 16054
348e048f
DE
16055 /* If the definition of this type lives in .debug_types, read that type.
16056 Don't follow DW_AT_specification though, that will take us back up
16057 the chain and we want to go down. */
052c8bb8 16058 attr = die->attr (DW_AT_signature);
435d3d88 16059 if (attr != nullptr)
348e048f 16060 {
ac9ec31b 16061 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16062
ac9ec31b 16063 /* The type's CU may not be the same as CU.
02142a6c 16064 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16065 return set_die_type (die, type, cu);
16066 }
16067
c0dd20ea 16068 type = alloc_type (objfile);
c906108c 16069 INIT_CPLUS_SPECIFIC (type);
93311388 16070
39cbfefa
DJ
16071 name = dwarf2_name (die, cu);
16072 if (name != NULL)
c906108c 16073 {
987504bb 16074 if (cu->language == language_cplus
c44af4eb
TT
16075 || cu->language == language_d
16076 || cu->language == language_rust)
63d06c5c 16077 {
15d034d0 16078 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
16079
16080 /* dwarf2_full_name might have already finished building the DIE's
16081 type. If so, there is no need to continue. */
16082 if (get_die_type (die, cu) != NULL)
16083 return get_die_type (die, cu);
16084
d0e39ea2 16085 type->set_name (full_name);
63d06c5c
DC
16086 }
16087 else
16088 {
d8151005
DJ
16089 /* The name is already allocated along with this objfile, so
16090 we don't need to duplicate it for the type. */
d0e39ea2 16091 type->set_name (name);
63d06c5c 16092 }
c906108c
SS
16093 }
16094
16095 if (die->tag == DW_TAG_structure_type)
16096 {
67607e24 16097 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16098 }
16099 else if (die->tag == DW_TAG_union_type)
16100 {
67607e24 16101 type->set_code (TYPE_CODE_UNION);
c906108c
SS
16102 }
16103 else
16104 {
67607e24 16105 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16106 }
16107
0cc2414c
TT
16108 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16109 TYPE_DECLARED_CLASS (type) = 1;
16110
e35000a7
TBA
16111 /* Store the calling convention in the type if it's available in
16112 the die. Otherwise the calling convention remains set to
16113 the default value DW_CC_normal. */
16114 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16115 if (attr != nullptr
529908cb 16116 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
e35000a7
TBA
16117 {
16118 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16119 TYPE_CPLUS_CALLING_CONVENTION (type)
529908cb 16120 = (enum dwarf_calling_convention) (attr->constant_value (0));
e35000a7
TBA
16121 }
16122
e142c38c 16123 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16124 if (attr != nullptr)
c906108c 16125 {
cd6c91b4 16126 if (attr->form_is_constant ())
dda83cd7 16127 TYPE_LENGTH (type) = attr->constant_value (0);
155bfbd3
JB
16128 else
16129 {
f8e89861 16130 struct dynamic_prop prop;
293e7e51 16131 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 16132 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
dda83cd7 16133 TYPE_LENGTH (type) = 0;
155bfbd3 16134 }
c906108c
SS
16135 }
16136 else
16137 {
16138 TYPE_LENGTH (type) = 0;
16139 }
16140
2b4424c3
TT
16141 maybe_set_alignment (cu, die, type);
16142
5230b05a 16143 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 16144 {
5230b05a
WT
16145 /* ICC<14 does not output the required DW_AT_declaration on
16146 incomplete types, but gives them a size of zero. */
b4b73759 16147 type->set_is_stub (true);
685b1105
JK
16148 }
16149 else
9baccff6 16150 type->set_stub_is_supported (true);
685b1105 16151
dc718098 16152 if (die_is_declaration (die, cu))
b4b73759 16153 type->set_is_stub (true);
a6c727b2
DJ
16154 else if (attr == NULL && die->child == NULL
16155 && producer_is_realview (cu->producer))
16156 /* RealView does not output the required DW_AT_declaration
16157 on incomplete types. */
b4b73759 16158 type->set_is_stub (true);
dc718098 16159
c906108c
SS
16160 /* We need to add the type field to the die immediately so we don't
16161 infinitely recurse when dealing with pointers to the structure
0963b4bd 16162 type within the structure itself. */
1c379e20 16163 set_die_type (die, type, cu);
c906108c 16164
7e314c57
JK
16165 /* set_die_type should be already done. */
16166 set_descriptive_type (type, die, cu);
16167
c767944b
DJ
16168 return type;
16169}
16170
9c6a1327
TT
16171static void handle_struct_member_die
16172 (struct die_info *child_die,
16173 struct type *type,
16174 struct field_info *fi,
16175 std::vector<struct symbol *> *template_args,
16176 struct dwarf2_cu *cu);
16177
16178/* A helper for handle_struct_member_die that handles
16179 DW_TAG_variant_part. */
16180
16181static void
16182handle_variant_part (struct die_info *die, struct type *type,
16183 struct field_info *fi,
16184 std::vector<struct symbol *> *template_args,
16185 struct dwarf2_cu *cu)
16186{
16187 variant_part_builder *new_part;
16188 if (fi->current_variant_part == nullptr)
16189 {
16190 fi->variant_parts.emplace_back ();
16191 new_part = &fi->variant_parts.back ();
16192 }
16193 else if (!fi->current_variant_part->processing_variant)
16194 {
16195 complaint (_("nested DW_TAG_variant_part seen "
16196 "- DIE at %s [in module %s]"),
16197 sect_offset_str (die->sect_off),
5e22e966 16198 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16199 return;
16200 }
16201 else
16202 {
16203 variant_field &current = fi->current_variant_part->variants.back ();
16204 current.variant_parts.emplace_back ();
16205 new_part = &current.variant_parts.back ();
16206 }
16207
16208 /* When we recurse, we want callees to add to this new variant
16209 part. */
16210 scoped_restore save_current_variant_part
16211 = make_scoped_restore (&fi->current_variant_part, new_part);
16212
16213 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16214 if (discr == NULL)
16215 {
16216 /* It's a univariant form, an extension we support. */
16217 }
16218 else if (discr->form_is_ref ())
16219 {
16220 struct dwarf2_cu *target_cu = cu;
16221 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16222
16223 new_part->discriminant_offset = target_die->sect_off;
16224 }
16225 else
16226 {
16227 complaint (_("DW_AT_discr does not have DIE reference form"
16228 " - DIE at %s [in module %s]"),
16229 sect_offset_str (die->sect_off),
5e22e966 16230 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16231 }
16232
16233 for (die_info *child_die = die->child;
16234 child_die != NULL;
16235 child_die = child_die->sibling)
16236 handle_struct_member_die (child_die, type, fi, template_args, cu);
16237}
16238
16239/* A helper for handle_struct_member_die that handles
16240 DW_TAG_variant. */
16241
16242static void
16243handle_variant (struct die_info *die, struct type *type,
16244 struct field_info *fi,
16245 std::vector<struct symbol *> *template_args,
16246 struct dwarf2_cu *cu)
16247{
16248 if (fi->current_variant_part == nullptr)
16249 {
16250 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16251 "- DIE at %s [in module %s]"),
16252 sect_offset_str (die->sect_off),
5e22e966 16253 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16254 return;
16255 }
16256 if (fi->current_variant_part->processing_variant)
16257 {
16258 complaint (_("nested DW_TAG_variant seen "
16259 "- DIE at %s [in module %s]"),
16260 sect_offset_str (die->sect_off),
5e22e966 16261 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16262 return;
16263 }
16264
16265 scoped_restore save_processing_variant
16266 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16267 true);
16268
16269 fi->current_variant_part->variants.emplace_back ();
16270 variant_field &variant = fi->current_variant_part->variants.back ();
16271 variant.first_field = fi->fields.size ();
16272
16273 /* In a variant we want to get the discriminant and also add a
16274 field for our sole member child. */
16275 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
cae21f8e 16276 if (discr == nullptr || !discr->form_is_constant ())
9c6a1327
TT
16277 {
16278 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
9d2246fc 16279 if (discr == nullptr || discr->as_block ()->size == 0)
9c6a1327
TT
16280 variant.default_branch = true;
16281 else
9d2246fc 16282 variant.discr_list_data = discr->as_block ();
9c6a1327
TT
16283 }
16284 else
cae21f8e 16285 variant.discriminant_value = discr->constant_value (0);
9c6a1327
TT
16286
16287 for (die_info *variant_child = die->child;
16288 variant_child != NULL;
16289 variant_child = variant_child->sibling)
16290 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16291
16292 variant.last_field = fi->fields.size ();
16293}
16294
2ddeaf8a
TT
16295/* A helper for process_structure_scope that handles a single member
16296 DIE. */
16297
16298static void
16299handle_struct_member_die (struct die_info *child_die, struct type *type,
16300 struct field_info *fi,
16301 std::vector<struct symbol *> *template_args,
16302 struct dwarf2_cu *cu)
16303{
16304 if (child_die->tag == DW_TAG_member
9c6a1327 16305 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
16306 {
16307 /* NOTE: carlton/2002-11-05: A C++ static data member
16308 should be a DW_TAG_member that is a declaration, but
16309 all versions of G++ as of this writing (so through at
16310 least 3.2.1) incorrectly generate DW_TAG_variable
16311 tags for them instead. */
16312 dwarf2_add_field (fi, child_die, cu);
16313 }
16314 else if (child_die->tag == DW_TAG_subprogram)
16315 {
16316 /* Rust doesn't have member functions in the C++ sense.
16317 However, it does emit ordinary functions as children
16318 of a struct DIE. */
16319 if (cu->language == language_rust)
16320 read_func_scope (child_die, cu);
16321 else
16322 {
16323 /* C++ member function. */
16324 dwarf2_add_member_fn (fi, child_die, type, cu);
16325 }
16326 }
16327 else if (child_die->tag == DW_TAG_inheritance)
16328 {
16329 /* C++ base class field. */
16330 dwarf2_add_field (fi, child_die, cu);
16331 }
16332 else if (type_can_define_types (child_die))
16333 dwarf2_add_type_defn (fi, child_die, cu);
16334 else if (child_die->tag == DW_TAG_template_type_param
16335 || child_die->tag == DW_TAG_template_value_param)
16336 {
16337 struct symbol *arg = new_symbol (child_die, NULL, cu);
16338
16339 if (arg != NULL)
16340 template_args->push_back (arg);
16341 }
9c6a1327
TT
16342 else if (child_die->tag == DW_TAG_variant_part)
16343 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 16344 else if (child_die->tag == DW_TAG_variant)
9c6a1327 16345 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
16346}
16347
c767944b
DJ
16348/* Finish creating a structure or union type, including filling in
16349 its members and creating a symbol for it. */
16350
16351static void
16352process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16353{
5e22e966 16354 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 16355 struct die_info *child_die;
c767944b
DJ
16356 struct type *type;
16357
16358 type = get_die_type (die, cu);
16359 if (type == NULL)
16360 type = read_structure_type (die, cu);
16361
3e1d3d8c 16362 bool has_template_parameters = false;
e142c38c 16363 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16364 {
16365 struct field_info fi;
2f4732b0 16366 std::vector<struct symbol *> template_args;
c906108c 16367
639d11d3 16368 child_die = die->child;
c906108c
SS
16369
16370 while (child_die && child_die->tag)
16371 {
2ddeaf8a 16372 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 16373 child_die = child_die->sibling;
c906108c
SS
16374 }
16375
34eaf542 16376 /* Attach template arguments to type. */
2f4732b0 16377 if (!template_args.empty ())
34eaf542 16378 {
3e1d3d8c 16379 has_template_parameters = true;
34eaf542 16380 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16381 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16382 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16383 = XOBNEWVEC (&objfile->objfile_obstack,
16384 struct symbol *,
16385 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16386 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16387 template_args.data (),
34eaf542
TT
16388 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16389 * sizeof (struct symbol *)));
34eaf542
TT
16390 }
16391
c906108c 16392 /* Attach fields and member functions to the type. */
317f7127 16393 if (fi.nfields () > 0)
e7c27a73 16394 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16395 if (!fi.fnfieldlists.empty ())
c906108c 16396 {
e7c27a73 16397 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16398
c5aa993b 16399 /* Get the type which refers to the base class (possibly this
c906108c 16400 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16401 class from the DW_AT_containing_type attribute. This use of
16402 DW_AT_containing_type is a GNU extension. */
c906108c 16403
e142c38c 16404 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16405 {
e7c27a73 16406 struct type *t = die_containing_type (die, cu);
c906108c 16407
ae6ae975 16408 set_type_vptr_basetype (type, t);
c906108c
SS
16409 if (type == t)
16410 {
c906108c
SS
16411 int i;
16412
16413 /* Our own class provides vtbl ptr. */
1f704f76 16414 for (i = t->num_fields () - 1;
c906108c
SS
16415 i >= TYPE_N_BASECLASSES (t);
16416 --i)
16417 {
0d5cff50 16418 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16419
dda83cd7 16420 if (is_vtable_name (fieldname, cu))
c906108c 16421 {
ae6ae975 16422 set_type_vptr_fieldno (type, i);
c906108c
SS
16423 break;
16424 }
16425 }
16426
16427 /* Complain if virtual function table field not found. */
16428 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16429 complaint (_("virtual function table pointer "
3e43a32a 16430 "not found when defining class '%s'"),
7d93a1e0 16431 type->name () ? type->name () : "");
c906108c
SS
16432 }
16433 else
16434 {
ae6ae975 16435 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16436 }
16437 }
f6235d4c 16438 else if (cu->producer
61012eef 16439 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16440 {
16441 /* The IBM XLC compiler does not provide direct indication
dda83cd7
SM
16442 of the containing type, but the vtable pointer is
16443 always named __vfp. */
f6235d4c
EZ
16444
16445 int i;
16446
1f704f76 16447 for (i = type->num_fields () - 1;
f6235d4c
EZ
16448 i >= TYPE_N_BASECLASSES (type);
16449 --i)
16450 {
16451 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16452 {
ae6ae975
DE
16453 set_type_vptr_fieldno (type, i);
16454 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16455 break;
16456 }
16457 }
16458 }
c906108c 16459 }
98751a41
JK
16460
16461 /* Copy fi.typedef_field_list linked list elements content into the
16462 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16463 if (!fi.typedef_field_list.empty ())
98751a41 16464 {
be2daae6 16465 int count = fi.typedef_field_list.size ();
98751a41 16466
a0d7a4ff 16467 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16468 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16469 = ((struct decl_field *)
be2daae6
TT
16470 TYPE_ALLOC (type,
16471 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16472 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16473
be2daae6
TT
16474 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16475 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16476 }
c767944b 16477
883fd55a
KS
16478 /* Copy fi.nested_types_list linked list elements content into the
16479 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16480 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16481 {
be2daae6 16482 int count = fi.nested_types_list.size ();
883fd55a
KS
16483
16484 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16485 TYPE_NESTED_TYPES_ARRAY (type)
16486 = ((struct decl_field *)
be2daae6
TT
16487 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16488 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16489
be2daae6
TT
16490 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16491 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16492 }
c906108c 16493 }
63d06c5c 16494
bb5ed363 16495 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16496 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16497 cu->rust_unions.push_back (type);
57567375
TT
16498 else if (cu->language == language_ada)
16499 quirk_ada_thick_pointer_struct (die, cu, type);
0b92b5bb 16500
90aeadfc
DC
16501 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16502 snapshots) has been known to create a die giving a declaration
16503 for a class that has, as a child, a die giving a definition for a
16504 nested class. So we have to process our children even if the
16505 current die is a declaration. Normally, of course, a declaration
16506 won't have any children at all. */
134d01f1 16507
ca040673
DE
16508 child_die = die->child;
16509
90aeadfc
DC
16510 while (child_die != NULL && child_die->tag)
16511 {
16512 if (child_die->tag == DW_TAG_member
16513 || child_die->tag == DW_TAG_variable
34eaf542
TT
16514 || child_die->tag == DW_TAG_inheritance
16515 || child_die->tag == DW_TAG_template_value_param
16516 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16517 {
90aeadfc 16518 /* Do nothing. */
134d01f1 16519 }
90aeadfc
DC
16520 else
16521 process_die (child_die, cu);
134d01f1 16522
436c571c 16523 child_die = child_die->sibling;
134d01f1
DJ
16524 }
16525
fa4028e9
JB
16526 /* Do not consider external references. According to the DWARF standard,
16527 these DIEs are identified by the fact that they have no byte_size
16528 attribute, and a declaration attribute. */
16529 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
16530 || !die_is_declaration (die, cu)
16531 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
16532 {
16533 struct symbol *sym = new_symbol (die, type, cu);
16534
16535 if (has_template_parameters)
16536 {
a776957c
TT
16537 struct symtab *symtab;
16538 if (sym != nullptr)
16539 symtab = symbol_symtab (sym);
16540 else if (cu->line_header != nullptr)
16541 {
16542 /* Any related symtab will do. */
16543 symtab
7ba99d21 16544 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16545 }
16546 else
16547 {
16548 symtab = nullptr;
16549 complaint (_("could not find suitable "
16550 "symtab for template parameter"
16551 " - DIE at %s [in module %s]"),
16552 sect_offset_str (die->sect_off),
16553 objfile_name (objfile));
16554 }
16555
16556 if (symtab != nullptr)
16557 {
16558 /* Make sure that the symtab is set on the new symbols.
16559 Even though they don't appear in this symtab directly,
16560 other parts of gdb assume that symbols do, and this is
16561 reasonably true. */
16562 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16563 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16564 }
3e1d3d8c
TT
16565 }
16566 }
134d01f1
DJ
16567}
16568
ed6acedd
TT
16569/* Assuming DIE is an enumeration type, and TYPE is its associated
16570 type, update TYPE using some information only available in DIE's
16571 children. In particular, the fields are computed. */
55426c9d
JB
16572
16573static void
16574update_enumeration_type_from_children (struct die_info *die,
16575 struct type *type,
16576 struct dwarf2_cu *cu)
16577{
60f7655a 16578 struct die_info *child_die;
55426c9d
JB
16579 int unsigned_enum = 1;
16580 int flag_enum = 1;
55426c9d 16581
8268c778 16582 auto_obstack obstack;
ed6acedd 16583 std::vector<struct field> fields;
55426c9d 16584
60f7655a
DE
16585 for (child_die = die->child;
16586 child_die != NULL && child_die->tag;
436c571c 16587 child_die = child_die->sibling)
55426c9d
JB
16588 {
16589 struct attribute *attr;
16590 LONGEST value;
16591 const gdb_byte *bytes;
16592 struct dwarf2_locexpr_baton *baton;
16593 const char *name;
60f7655a 16594
55426c9d
JB
16595 if (child_die->tag != DW_TAG_enumerator)
16596 continue;
16597
16598 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16599 if (attr == NULL)
16600 continue;
16601
16602 name = dwarf2_name (child_die, cu);
16603 if (name == NULL)
16604 name = "<anonymous enumerator>";
16605
16606 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16607 &value, &bytes, &baton);
16608 if (value < 0)
16609 {
16610 unsigned_enum = 0;
16611 flag_enum = 0;
16612 }
55426c9d 16613 else
edd45eb0
SM
16614 {
16615 if (count_one_bits_ll (value) >= 2)
16616 flag_enum = 0;
edd45eb0 16617 }
55426c9d 16618
ed6acedd
TT
16619 fields.emplace_back ();
16620 struct field &field = fields.back ();
16621 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16622 SET_FIELD_ENUMVAL (field, value);
16623 }
16624
16625 if (!fields.empty ())
16626 {
5e33d5f4 16627 type->set_num_fields (fields.size ());
3cabb6b0
SM
16628 type->set_fields
16629 ((struct field *)
16630 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16631 memcpy (type->fields (), fields.data (),
ed6acedd 16632 sizeof (struct field) * fields.size ());
55426c9d
JB
16633 }
16634
16635 if (unsigned_enum)
653223d3
SM
16636 type->set_is_unsigned (true);
16637
55426c9d
JB
16638 if (flag_enum)
16639 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16640}
16641
134d01f1
DJ
16642/* Given a DW_AT_enumeration_type die, set its type. We do not
16643 complete the type's fields yet, or create any symbols. */
c906108c 16644
f792889a 16645static struct type *
134d01f1 16646read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16647{
5e22e966 16648 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16649 struct type *type;
c906108c 16650 struct attribute *attr;
0114d602 16651 const char *name;
134d01f1 16652
348e048f
DE
16653 /* If the definition of this type lives in .debug_types, read that type.
16654 Don't follow DW_AT_specification though, that will take us back up
16655 the chain and we want to go down. */
052c8bb8 16656 attr = die->attr (DW_AT_signature);
435d3d88 16657 if (attr != nullptr)
348e048f 16658 {
ac9ec31b 16659 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16660
ac9ec31b 16661 /* The type's CU may not be the same as CU.
02142a6c 16662 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16663 return set_die_type (die, type, cu);
16664 }
16665
c906108c
SS
16666 type = alloc_type (objfile);
16667
67607e24 16668 type->set_code (TYPE_CODE_ENUM);
94af9270 16669 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16670 if (name != NULL)
d0e39ea2 16671 type->set_name (name);
c906108c 16672
0626fc76
TT
16673 attr = dwarf2_attr (die, DW_AT_type, cu);
16674 if (attr != NULL)
16675 {
16676 struct type *underlying_type = die_type (die, cu);
16677
16678 TYPE_TARGET_TYPE (type) = underlying_type;
16679 }
16680
e142c38c 16681 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16682 if (attr != nullptr)
c906108c 16683 {
529908cb 16684 TYPE_LENGTH (type) = attr->constant_value (0);
c906108c
SS
16685 }
16686 else
16687 {
16688 TYPE_LENGTH (type) = 0;
16689 }
16690
2b4424c3
TT
16691 maybe_set_alignment (cu, die, type);
16692
137033e9
JB
16693 /* The enumeration DIE can be incomplete. In Ada, any type can be
16694 declared as private in the package spec, and then defined only
16695 inside the package body. Such types are known as Taft Amendment
16696 Types. When another package uses such a type, an incomplete DIE
16697 may be generated by the compiler. */
02eb380e 16698 if (die_is_declaration (die, cu))
b4b73759 16699 type->set_is_stub (true);
02eb380e 16700
0626fc76
TT
16701 /* If this type has an underlying type that is not a stub, then we
16702 may use its attributes. We always use the "unsigned" attribute
16703 in this situation, because ordinarily we guess whether the type
16704 is unsigned -- but the guess can be wrong and the underlying type
16705 can tell us the reality. However, we defer to a local size
16706 attribute if one exists, because this lets the compiler override
16707 the underlying type if needed. */
e46d3488 16708 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
0626fc76 16709 {
9e7c9a03
HD
16710 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16711 underlying_type = check_typedef (underlying_type);
653223d3
SM
16712
16713 type->set_is_unsigned (underlying_type->is_unsigned ());
16714
0626fc76 16715 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16716 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
653223d3 16717
2b4424c3 16718 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16719 && TYPE_RAW_ALIGN (underlying_type) != 0)
16720 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16721 }
16722
3d567982
TT
16723 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16724
ed6acedd
TT
16725 set_die_type (die, type, cu);
16726
16727 /* Finish the creation of this type by using the enum's children.
16728 Note that, as usual, this must come after set_die_type to avoid
16729 infinite recursion when trying to compute the names of the
16730 enumerators. */
16731 update_enumeration_type_from_children (die, type, cu);
16732
16733 return type;
134d01f1
DJ
16734}
16735
16736/* Given a pointer to a die which begins an enumeration, process all
16737 the dies that define the members of the enumeration, and create the
16738 symbol for the enumeration type.
16739
16740 NOTE: We reverse the order of the element list. */
16741
16742static void
16743process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16744{
f792889a 16745 struct type *this_type;
134d01f1 16746
f792889a
DJ
16747 this_type = get_die_type (die, cu);
16748 if (this_type == NULL)
16749 this_type = read_enumeration_type (die, cu);
9dc481d3 16750
639d11d3 16751 if (die->child != NULL)
c906108c 16752 {
9dc481d3 16753 struct die_info *child_die;
15d034d0 16754 const char *name;
9dc481d3 16755
639d11d3 16756 child_die = die->child;
c906108c
SS
16757 while (child_die && child_die->tag)
16758 {
16759 if (child_die->tag != DW_TAG_enumerator)
16760 {
e7c27a73 16761 process_die (child_die, cu);
c906108c
SS
16762 }
16763 else
16764 {
39cbfefa
DJ
16765 name = dwarf2_name (child_die, cu);
16766 if (name)
ed6acedd 16767 new_symbol (child_die, this_type, cu);
c906108c
SS
16768 }
16769
436c571c 16770 child_die = child_die->sibling;
c906108c 16771 }
c906108c 16772 }
134d01f1 16773
6c83ed52
TT
16774 /* If we are reading an enum from a .debug_types unit, and the enum
16775 is a declaration, and the enum is not the signatured type in the
16776 unit, then we do not want to add a symbol for it. Adding a
16777 symbol would in some cases obscure the true definition of the
16778 enum, giving users an incomplete type when the definition is
16779 actually available. Note that we do not want to do this for all
16780 enums which are just declarations, because C++0x allows forward
16781 enum declarations. */
3019eac3 16782 if (cu->per_cu->is_debug_types
6c83ed52
TT
16783 && die_is_declaration (die, cu))
16784 {
52dc124a 16785 struct signatured_type *sig_type;
6c83ed52 16786
c0f78cd4 16787 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16788 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16789 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16790 return;
16791 }
16792
f792889a 16793 new_symbol (die, this_type, cu);
c906108c
SS
16794}
16795
57567375
TT
16796/* Helper function for quirk_ada_thick_pointer that examines a bounds
16797 expression for an index type and finds the corresponding field
16798 offset in the hidden "P_BOUNDS" structure. Returns true on success
16799 and updates *FIELD, false if it fails to recognize an
16800 expression. */
16801
16802static bool
16803recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16804 int *bounds_offset, struct field *field,
16805 struct dwarf2_cu *cu)
16806{
16807 struct attribute *attr = dwarf2_attr (die, name, cu);
16808 if (attr == nullptr || !attr->form_is_block ())
16809 return false;
16810
16811 const struct dwarf_block *block = attr->as_block ();
16812 const gdb_byte *start = block->data;
16813 const gdb_byte *end = block->data + block->size;
16814
16815 /* The expression to recognize generally looks like:
16816
16817 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16818 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16819
16820 However, the second "plus_uconst" may be missing:
16821
16822 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16823 DW_OP_deref_size: 4)
16824
16825 This happens when the field is at the start of the structure.
16826
16827 Also, the final deref may not be sized:
16828
16829 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16830 DW_OP_deref)
16831
16832 This happens when the size of the index type happens to be the
16833 same as the architecture's word size. This can occur with or
16834 without the second plus_uconst. */
16835
16836 if (end - start < 2)
16837 return false;
16838 if (*start++ != DW_OP_push_object_address)
16839 return false;
16840 if (*start++ != DW_OP_plus_uconst)
16841 return false;
16842
16843 uint64_t this_bound_off;
16844 start = gdb_read_uleb128 (start, end, &this_bound_off);
16845 if (start == nullptr || (int) this_bound_off != this_bound_off)
16846 return false;
16847 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16848 is consistent among all bounds. */
16849 if (*bounds_offset == -1)
16850 *bounds_offset = this_bound_off;
16851 else if (*bounds_offset != this_bound_off)
16852 return false;
16853
16854 if (start == end || *start++ != DW_OP_deref)
16855 return false;
16856
16857 int offset = 0;
16858 if (start ==end)
16859 return false;
16860 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16861 {
16862 /* This means an offset of 0. */
16863 }
16864 else if (*start++ != DW_OP_plus_uconst)
16865 return false;
16866 else
16867 {
16868 /* The size is the parameter to DW_OP_plus_uconst. */
16869 uint64_t val;
16870 start = gdb_read_uleb128 (start, end, &val);
16871 if (start == nullptr)
16872 return false;
16873 if ((int) val != val)
16874 return false;
16875 offset = val;
16876 }
16877
16878 if (start == end)
16879 return false;
16880
16881 uint64_t size;
16882 if (*start == DW_OP_deref_size)
16883 {
16884 start = gdb_read_uleb128 (start + 1, end, &size);
16885 if (start == nullptr)
16886 return false;
16887 }
16888 else if (*start == DW_OP_deref)
16889 {
16890 size = cu->header.addr_size;
16891 ++start;
16892 }
16893 else
16894 return false;
16895
16896 SET_FIELD_BITPOS (*field, 8 * offset);
16897 if (size != TYPE_LENGTH (field->type ()))
16898 FIELD_BITSIZE (*field) = 8 * size;
16899
16900 return true;
16901}
16902
16903/* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16904 some kinds of Ada arrays:
16905
16906 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16907 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16908 <11e0> DW_AT_data_location: 2 byte block: 97 6
16909 (DW_OP_push_object_address; DW_OP_deref)
16910 <11e3> DW_AT_type : <0x1173>
16911 <11e7> DW_AT_sibling : <0x1201>
16912 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16913 <11ec> DW_AT_type : <0x1206>
16914 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16915 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16916 DW_OP_deref_size: 4)
16917 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16918 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16919 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16920
16921 This actually represents a "thick pointer", which is a structure
16922 with two elements: one that is a pointer to the array data, and one
16923 that is a pointer to another structure; this second structure holds
16924 the array bounds.
16925
16926 This returns a new type on success, or nullptr if this didn't
16927 recognize the type. */
16928
16929static struct type *
16930quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16931 struct type *type)
16932{
16933 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16934 /* So far we've only seen this with block form. */
16935 if (attr == nullptr || !attr->form_is_block ())
16936 return nullptr;
16937
16938 /* Note that this will fail if the structure layout is changed by
16939 the compiler. However, we have no good way to recognize some
16940 other layout, because we don't know what expression the compiler
16941 might choose to emit should this happen. */
16942 struct dwarf_block *blk = attr->as_block ();
16943 if (blk->size != 2
16944 || blk->data[0] != DW_OP_push_object_address
16945 || blk->data[1] != DW_OP_deref)
16946 return nullptr;
16947
16948 int bounds_offset = -1;
16949 int max_align = -1;
16950 std::vector<struct field> range_fields;
16951 for (struct die_info *child_die = die->child;
16952 child_die;
16953 child_die = child_die->sibling)
16954 {
16955 if (child_die->tag == DW_TAG_subrange_type)
16956 {
16957 struct type *underlying = read_subrange_index_type (child_die, cu);
16958
16959 int this_align = type_align (underlying);
16960 if (this_align > max_align)
16961 max_align = this_align;
16962
16963 range_fields.emplace_back ();
16964 range_fields.emplace_back ();
16965
16966 struct field &lower = range_fields[range_fields.size () - 2];
16967 struct field &upper = range_fields[range_fields.size () - 1];
16968
16969 lower.set_type (underlying);
16970 FIELD_ARTIFICIAL (lower) = 1;
16971
16972 upper.set_type (underlying);
16973 FIELD_ARTIFICIAL (upper) = 1;
16974
16975 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16976 &bounds_offset, &lower, cu)
16977 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16978 &bounds_offset, &upper, cu))
16979 return nullptr;
16980 }
16981 }
16982
16983 /* This shouldn't really happen, but double-check that we found
16984 where the bounds are stored. */
16985 if (bounds_offset == -1)
16986 return nullptr;
16987
16988 struct objfile *objfile = cu->per_objfile->objfile;
16989 for (int i = 0; i < range_fields.size (); i += 2)
16990 {
16991 char name[20];
16992
16993 /* Set the name of each field in the bounds. */
16994 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16995 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16996 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16997 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16998 }
16999
17000 struct type *bounds = alloc_type (objfile);
17001 bounds->set_code (TYPE_CODE_STRUCT);
17002
17003 bounds->set_num_fields (range_fields.size ());
17004 bounds->set_fields
17005 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
17006 * sizeof (struct field))));
17007 memcpy (bounds->fields (), range_fields.data (),
17008 bounds->num_fields () * sizeof (struct field));
17009
17010 int last_fieldno = range_fields.size () - 1;
17011 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
17012 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
17013 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
17014
17015 /* Rewrite the existing array type in place. Specifically, we
17016 remove any dynamic properties we might have read, and we replace
17017 the index types. */
17018 struct type *iter = type;
17019 for (int i = 0; i < range_fields.size (); i += 2)
17020 {
17021 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
17022 iter->main_type->dyn_prop_list = nullptr;
17023 iter->set_index_type
17024 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
17025 iter = TYPE_TARGET_TYPE (iter);
17026 }
17027
17028 struct type *result = alloc_type (objfile);
17029 result->set_code (TYPE_CODE_STRUCT);
17030
17031 result->set_num_fields (2);
17032 result->set_fields
17033 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
17034 * sizeof (struct field))));
17035
17036 /* The names are chosen to coincide with what the compiler does with
17037 -fgnat-encodings=all, which the Ada code in gdb already
17038 understands. */
17039 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
17040 result->field (0).set_type (lookup_pointer_type (type));
17041
17042 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
17043 result->field (1).set_type (lookup_pointer_type (bounds));
17044 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
17045
17046 result->set_name (type->name ());
17047 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17048 + TYPE_LENGTH (result->field (1).type ()));
17049
17050 return result;
17051}
17052
c906108c
SS
17053/* Extract all information from a DW_TAG_array_type DIE and put it in
17054 the DIE's type field. For now, this only handles one dimensional
17055 arrays. */
17056
f792889a 17057static struct type *
e7c27a73 17058read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17059{
5e22e966 17060 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 17061 struct die_info *child_die;
7e314c57 17062 struct type *type;
c906108c 17063 struct type *element_type, *range_type, *index_type;
c906108c 17064 struct attribute *attr;
15d034d0 17065 const char *name;
a405673c 17066 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 17067 unsigned int bit_stride = 0;
c906108c 17068
e7c27a73 17069 element_type = die_type (die, cu);
c906108c 17070
7e314c57
JK
17071 /* The die_type call above may have already set the type for this DIE. */
17072 type = get_die_type (die, cu);
17073 if (type)
17074 return type;
17075
dc53a7ad
JB
17076 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17077 if (attr != NULL)
a405673c
JB
17078 {
17079 int stride_ok;
293e7e51 17080 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
17081
17082 byte_stride_prop
17083 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
17084 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17085 prop_type);
a405673c
JB
17086 if (!stride_ok)
17087 {
b98664d3 17088 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
17089 " - DIE at %s [in module %s]"),
17090 sect_offset_str (die->sect_off),
5e22e966 17091 objfile_name (cu->per_objfile->objfile));
a405673c
JB
17092 /* Ignore this attribute. We will likely not be able to print
17093 arrays of this type correctly, but there is little we can do
17094 to help if we cannot read the attribute's value. */
17095 byte_stride_prop = NULL;
17096 }
17097 }
dc53a7ad
JB
17098
17099 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17100 if (attr != NULL)
529908cb 17101 bit_stride = attr->constant_value (0);
dc53a7ad 17102
c906108c
SS
17103 /* Irix 6.2 native cc creates array types without children for
17104 arrays with unspecified length. */
639d11d3 17105 if (die->child == NULL)
c906108c 17106 {
46bf5051 17107 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17108 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 17109 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 17110 byte_stride_prop, bit_stride);
f792889a 17111 return set_die_type (die, type, cu);
c906108c
SS
17112 }
17113
791afaa2 17114 std::vector<struct type *> range_types;
639d11d3 17115 child_die = die->child;
c906108c
SS
17116 while (child_die && child_die->tag)
17117 {
17118 if (child_die->tag == DW_TAG_subrange_type)
17119 {
f792889a 17120 struct type *child_type = read_type_die (child_die, cu);
9a619af0 17121
dda83cd7
SM
17122 if (child_type != NULL)
17123 {
0963b4bd 17124 /* The range type was succesfully read. Save it for the
dda83cd7 17125 array type creation. */
791afaa2 17126 range_types.push_back (child_type);
dda83cd7 17127 }
c906108c 17128 }
436c571c 17129 child_die = child_die->sibling;
c906108c
SS
17130 }
17131
cf2b2075
TV
17132 if (range_types.empty ())
17133 {
17134 complaint (_("unable to find array range - DIE at %s [in module %s]"),
17135 sect_offset_str (die->sect_off),
17136 objfile_name (cu->per_objfile->objfile));
17137 return NULL;
17138 }
17139
c906108c
SS
17140 /* Dwarf2 dimensions are output from left to right, create the
17141 necessary array types in backwards order. */
7ca2d3a3 17142
c906108c 17143 type = element_type;
7ca2d3a3
DL
17144
17145 if (read_array_order (die, cu) == DW_ORD_col_major)
17146 {
17147 int i = 0;
9a619af0 17148
791afaa2 17149 while (i < range_types.size ())
10f6a3ad
TT
17150 {
17151 type = create_array_type_with_stride (NULL, type, range_types[i++],
17152 byte_stride_prop, bit_stride);
17153 bit_stride = 0;
17154 byte_stride_prop = nullptr;
17155 }
7ca2d3a3
DL
17156 }
17157 else
17158 {
791afaa2 17159 size_t ndim = range_types.size ();
7ca2d3a3 17160 while (ndim-- > 0)
10f6a3ad
TT
17161 {
17162 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17163 byte_stride_prop, bit_stride);
17164 bit_stride = 0;
17165 byte_stride_prop = nullptr;
17166 }
7ca2d3a3 17167 }
c906108c 17168
cf2b2075
TV
17169 gdb_assert (type != element_type);
17170
f5f8a009
EZ
17171 /* Understand Dwarf2 support for vector types (like they occur on
17172 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17173 array type. This is not part of the Dwarf2/3 standard yet, but a
17174 custom vendor extension. The main difference between a regular
17175 array and the vector variant is that vectors are passed by value
17176 to functions. */
e142c38c 17177 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 17178 if (attr != nullptr)
ea37ba09 17179 make_vector_type (type);
f5f8a009 17180
dbc98a8b
KW
17181 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17182 implementation may choose to implement triple vectors using this
17183 attribute. */
17184 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb 17185 if (attr != nullptr && attr->form_is_unsigned ())
dbc98a8b 17186 {
529908cb
TT
17187 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17188 TYPE_LENGTH (type) = attr->as_unsigned ();
dbc98a8b 17189 else
b98664d3 17190 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 17191 "than the total size of elements"));
dbc98a8b
KW
17192 }
17193
39cbfefa
DJ
17194 name = dwarf2_name (die, cu);
17195 if (name)
d0e39ea2 17196 type->set_name (name);
6e70227d 17197
2b4424c3
TT
17198 maybe_set_alignment (cu, die, type);
17199
57567375
TT
17200 struct type *replacement_type = nullptr;
17201 if (cu->language == language_ada)
17202 {
17203 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17204 if (replacement_type != nullptr)
17205 type = replacement_type;
17206 }
17207
0963b4bd 17208 /* Install the type in the die. */
57567375 17209 set_die_type (die, type, cu, replacement_type != nullptr);
7e314c57
JK
17210
17211 /* set_die_type should be already done. */
b4ba55a1
JB
17212 set_descriptive_type (type, die, cu);
17213
7e314c57 17214 return type;
c906108c
SS
17215}
17216
7ca2d3a3 17217static enum dwarf_array_dim_ordering
6e70227d 17218read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
17219{
17220 struct attribute *attr;
17221
17222 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17223
435d3d88 17224 if (attr != nullptr)
1bc397c5
TT
17225 {
17226 LONGEST val = attr->constant_value (-1);
17227 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17228 return (enum dwarf_array_dim_ordering) val;
17229 }
7ca2d3a3 17230
0963b4bd
MS
17231 /* GNU F77 is a special case, as at 08/2004 array type info is the
17232 opposite order to the dwarf2 specification, but data is still
17233 laid out as per normal fortran.
7ca2d3a3 17234
0963b4bd
MS
17235 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17236 version checking. */
7ca2d3a3 17237
905e0470
PM
17238 if (cu->language == language_fortran
17239 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
17240 {
17241 return DW_ORD_row_major;
17242 }
17243
3a3440fb 17244 switch (cu->language_defn->array_ordering ())
7ca2d3a3
DL
17245 {
17246 case array_column_major:
17247 return DW_ORD_col_major;
17248 case array_row_major:
17249 default:
17250 return DW_ORD_row_major;
17251 };
17252}
17253
72019c9c 17254/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 17255 the DIE's type field. */
72019c9c 17256
f792889a 17257static struct type *
72019c9c
GM
17258read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17259{
7e314c57
JK
17260 struct type *domain_type, *set_type;
17261 struct attribute *attr;
f792889a 17262
7e314c57
JK
17263 domain_type = die_type (die, cu);
17264
17265 /* The die_type call above may have already set the type for this DIE. */
17266 set_type = get_die_type (die, cu);
17267 if (set_type)
17268 return set_type;
17269
17270 set_type = create_set_type (NULL, domain_type);
17271
17272 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb
TT
17273 if (attr != nullptr && attr->form_is_unsigned ())
17274 TYPE_LENGTH (set_type) = attr->as_unsigned ();
7e314c57 17275
2b4424c3
TT
17276 maybe_set_alignment (cu, die, set_type);
17277
f792889a 17278 return set_die_type (die, set_type, cu);
72019c9c 17279}
7ca2d3a3 17280
0971de02
TT
17281/* A helper for read_common_block that creates a locexpr baton.
17282 SYM is the symbol which we are marking as computed.
17283 COMMON_DIE is the DIE for the common block.
17284 COMMON_LOC is the location expression attribute for the common
17285 block itself.
17286 MEMBER_LOC is the location expression attribute for the particular
17287 member of the common block that we are processing.
17288 CU is the CU from which the above come. */
17289
17290static void
17291mark_common_block_symbol_computed (struct symbol *sym,
17292 struct die_info *common_die,
17293 struct attribute *common_loc,
17294 struct attribute *member_loc,
17295 struct dwarf2_cu *cu)
17296{
5e22e966 17297 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 17298 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
17299 struct dwarf2_locexpr_baton *baton;
17300 gdb_byte *ptr;
17301 unsigned int cu_off;
08feed99 17302 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
17303 LONGEST offset = 0;
17304
17305 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
17306 gdb_assert (common_loc->form_is_block ());
17307 gdb_assert (member_loc->form_is_block ()
cd6c91b4 17308 || member_loc->form_is_constant ());
0971de02 17309
8d749320 17310 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 17311 baton->per_objfile = per_objfile;
0971de02
TT
17312 baton->per_cu = cu->per_cu;
17313 gdb_assert (baton->per_cu);
17314
17315 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17316
cd6c91b4 17317 if (member_loc->form_is_constant ())
0971de02 17318 {
0826b30a 17319 offset = member_loc->constant_value (0);
0971de02
TT
17320 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17321 }
17322 else
9d2246fc 17323 baton->size += member_loc->as_block ()->size;
0971de02 17324
224c3ddb 17325 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
17326 baton->data = ptr;
17327
17328 *ptr++ = DW_OP_call4;
9c541725 17329 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
17330 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17331 ptr += 4;
17332
cd6c91b4 17333 if (member_loc->form_is_constant ())
0971de02
TT
17334 {
17335 *ptr++ = DW_OP_addr;
17336 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17337 ptr += cu->header.addr_size;
17338 }
17339 else
17340 {
17341 /* We have to copy the data here, because DW_OP_call4 will only
17342 use a DW_AT_location attribute. */
9d2246fc
TT
17343 struct dwarf_block *block = member_loc->as_block ();
17344 memcpy (ptr, block->data, block->size);
17345 ptr += block->size;
0971de02
TT
17346 }
17347
17348 *ptr++ = DW_OP_plus;
17349 gdb_assert (ptr - baton->data == baton->size);
17350
0971de02 17351 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 17352 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
17353}
17354
4357ac6c
TT
17355/* Create appropriate locally-scoped variables for all the
17356 DW_TAG_common_block entries. Also create a struct common_block
17357 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 17358 is used to separate the common blocks name namespace from regular
4357ac6c 17359 variable names. */
c906108c
SS
17360
17361static void
e7c27a73 17362read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17363{
0971de02
TT
17364 struct attribute *attr;
17365
17366 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 17367 if (attr != nullptr)
0971de02
TT
17368 {
17369 /* Support the .debug_loc offsets. */
4fc6c0d5 17370 if (attr->form_is_block ())
dda83cd7 17371 {
0971de02 17372 /* Ok. */
dda83cd7 17373 }
cd6c91b4 17374 else if (attr->form_is_section_offset ())
dda83cd7 17375 {
0971de02
TT
17376 dwarf2_complex_location_expr_complaint ();
17377 attr = NULL;
dda83cd7 17378 }
0971de02 17379 else
dda83cd7 17380 {
0971de02
TT
17381 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17382 "common block member");
17383 attr = NULL;
dda83cd7 17384 }
0971de02
TT
17385 }
17386
639d11d3 17387 if (die->child != NULL)
c906108c 17388 {
5e22e966 17389 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
17390 struct die_info *child_die;
17391 size_t n_entries = 0, size;
17392 struct common_block *common_block;
17393 struct symbol *sym;
74ac6d43 17394
4357ac6c
TT
17395 for (child_die = die->child;
17396 child_die && child_die->tag;
436c571c 17397 child_die = child_die->sibling)
4357ac6c
TT
17398 ++n_entries;
17399
17400 size = (sizeof (struct common_block)
17401 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
17402 common_block
17403 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17404 size);
4357ac6c
TT
17405 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17406 common_block->n_entries = 0;
17407
17408 for (child_die = die->child;
17409 child_die && child_die->tag;
436c571c 17410 child_die = child_die->sibling)
4357ac6c
TT
17411 {
17412 /* Create the symbol in the DW_TAG_common_block block in the current
17413 symbol scope. */
e7c27a73 17414 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
17415 if (sym != NULL)
17416 {
17417 struct attribute *member_loc;
17418
17419 common_block->contents[common_block->n_entries++] = sym;
17420
17421 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17422 cu);
17423 if (member_loc)
17424 {
17425 /* GDB has handled this for a long time, but it is
17426 not specified by DWARF. It seems to have been
17427 emitted by gfortran at least as recently as:
17428 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 17429 complaint (_("Variable in common block has "
0971de02 17430 "DW_AT_data_member_location "
9d8780f0
SM
17431 "- DIE at %s [in module %s]"),
17432 sect_offset_str (child_die->sect_off),
518817b3 17433 objfile_name (objfile));
0971de02 17434
cd6c91b4 17435 if (member_loc->form_is_section_offset ())
0971de02 17436 dwarf2_complex_location_expr_complaint ();
cd6c91b4 17437 else if (member_loc->form_is_constant ()
4fc6c0d5 17438 || member_loc->form_is_block ())
0971de02 17439 {
435d3d88 17440 if (attr != nullptr)
0971de02
TT
17441 mark_common_block_symbol_computed (sym, die, attr,
17442 member_loc, cu);
17443 }
17444 else
17445 dwarf2_complex_location_expr_complaint ();
17446 }
17447 }
c906108c 17448 }
4357ac6c
TT
17449
17450 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17451 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
17452 }
17453}
17454
0114d602 17455/* Create a type for a C++ namespace. */
d9fa45fe 17456
0114d602
DJ
17457static struct type *
17458read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 17459{
5e22e966 17460 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17461 const char *previous_prefix, *name;
9219021c 17462 int is_anonymous;
0114d602
DJ
17463 struct type *type;
17464
17465 /* For extensions, reuse the type of the original namespace. */
17466 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17467 {
17468 struct die_info *ext_die;
17469 struct dwarf2_cu *ext_cu = cu;
9a619af0 17470
0114d602
DJ
17471 ext_die = dwarf2_extension (die, &ext_cu);
17472 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
17473
17474 /* EXT_CU may not be the same as CU.
02142a6c 17475 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
17476 return set_die_type (die, type, cu);
17477 }
9219021c 17478
e142c38c 17479 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
17480
17481 /* Now build the name of the current namespace. */
17482
0114d602
DJ
17483 previous_prefix = determine_prefix (die, cu);
17484 if (previous_prefix[0] != '\0')
17485 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 17486 previous_prefix, name, 0, cu);
0114d602
DJ
17487
17488 /* Create the type. */
19f392bc 17489 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 17490
60531b24 17491 return set_die_type (die, type, cu);
0114d602
DJ
17492}
17493
22cee43f 17494/* Read a namespace scope. */
0114d602
DJ
17495
17496static void
17497read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17498{
5e22e966 17499 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17500 int is_anonymous;
9219021c 17501
5c4e30ca
DC
17502 /* Add a symbol associated to this if we haven't seen the namespace
17503 before. Also, add a using directive if it's an anonymous
17504 namespace. */
9219021c 17505
f2f0e013 17506 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17507 {
17508 struct type *type;
17509
0114d602 17510 type = read_type_die (die, cu);
e7c27a73 17511 new_symbol (die, type, cu);
5c4e30ca 17512
e8e80198 17513 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17514 if (is_anonymous)
0114d602
DJ
17515 {
17516 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17517
eb1e02fd 17518 std::vector<const char *> excludes;
804d2729 17519 add_using_directive (using_directives (cu),
7d93a1e0 17520 previous_prefix, type->name (), NULL,
eb1e02fd 17521 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17522 }
5c4e30ca 17523 }
9219021c 17524
639d11d3 17525 if (die->child != NULL)
d9fa45fe 17526 {
639d11d3 17527 struct die_info *child_die = die->child;
6e70227d 17528
d9fa45fe
DC
17529 while (child_die && child_die->tag)
17530 {
e7c27a73 17531 process_die (child_die, cu);
436c571c 17532 child_die = child_die->sibling;
d9fa45fe
DC
17533 }
17534 }
38d518c9
EZ
17535}
17536
f55ee35c
JK
17537/* Read a Fortran module as type. This DIE can be only a declaration used for
17538 imported module. Still we need that type as local Fortran "use ... only"
17539 declaration imports depend on the created type in determine_prefix. */
17540
17541static struct type *
17542read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17543{
5e22e966 17544 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 17545 const char *module_name;
f55ee35c
JK
17546 struct type *type;
17547
17548 module_name = dwarf2_name (die, cu);
19f392bc 17549 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17550
f55ee35c
JK
17551 return set_die_type (die, type, cu);
17552}
17553
5d7cb8df
JK
17554/* Read a Fortran module. */
17555
17556static void
17557read_module (struct die_info *die, struct dwarf2_cu *cu)
17558{
17559 struct die_info *child_die = die->child;
530e8392
KB
17560 struct type *type;
17561
17562 type = read_type_die (die, cu);
17563 new_symbol (die, type, cu);
5d7cb8df 17564
5d7cb8df
JK
17565 while (child_die && child_die->tag)
17566 {
17567 process_die (child_die, cu);
436c571c 17568 child_die = child_die->sibling;
5d7cb8df
JK
17569 }
17570}
17571
38d518c9
EZ
17572/* Return the name of the namespace represented by DIE. Set
17573 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17574 namespace. */
17575
17576static const char *
e142c38c 17577namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17578{
17579 struct die_info *current_die;
17580 const char *name = NULL;
17581
17582 /* Loop through the extensions until we find a name. */
17583
17584 for (current_die = die;
17585 current_die != NULL;
f2f0e013 17586 current_die = dwarf2_extension (die, &cu))
38d518c9 17587 {
96553a0c
DE
17588 /* We don't use dwarf2_name here so that we can detect the absence
17589 of a name -> anonymous namespace. */
7d45c7c3 17590 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17591
38d518c9
EZ
17592 if (name != NULL)
17593 break;
17594 }
17595
17596 /* Is it an anonymous namespace? */
17597
17598 *is_anonymous = (name == NULL);
17599 if (*is_anonymous)
2b1dbab0 17600 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17601
17602 return name;
d9fa45fe
DC
17603}
17604
c906108c
SS
17605/* Extract all information from a DW_TAG_pointer_type DIE and add to
17606 the user defined type vector. */
17607
f792889a 17608static struct type *
e7c27a73 17609read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17610{
5e22e966 17611 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 17612 struct comp_unit_head *cu_header = &cu->header;
c906108c 17613 struct type *type;
8b2dbe47
KB
17614 struct attribute *attr_byte_size;
17615 struct attribute *attr_address_class;
17616 int byte_size, addr_class;
7e314c57
JK
17617 struct type *target_type;
17618
17619 target_type = die_type (die, cu);
c906108c 17620
7e314c57
JK
17621 /* The die_type call above may have already set the type for this DIE. */
17622 type = get_die_type (die, cu);
17623 if (type)
17624 return type;
17625
17626 type = lookup_pointer_type (target_type);
8b2dbe47 17627
e142c38c 17628 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47 17629 if (attr_byte_size)
529908cb 17630 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
c906108c 17631 else
8b2dbe47
KB
17632 byte_size = cu_header->addr_size;
17633
e142c38c 17634 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47 17635 if (attr_address_class)
529908cb 17636 addr_class = attr_address_class->constant_value (DW_ADDR_none);
8b2dbe47
KB
17637 else
17638 addr_class = DW_ADDR_none;
17639
2b4424c3
TT
17640 ULONGEST alignment = get_alignment (cu, die);
17641
17642 /* If the pointer size, alignment, or address class is different
17643 than the default, create a type variant marked as such and set
17644 the length accordingly. */
17645 if (TYPE_LENGTH (type) != byte_size
17646 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17647 && alignment != TYPE_RAW_ALIGN (type))
17648 || addr_class != DW_ADDR_none)
c906108c 17649 {
5e2b427d 17650 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47 17651 {
314ad88d
PA
17652 type_instance_flags type_flags
17653 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17654 addr_class);
876cecd0
TT
17655 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17656 == 0);
8b2dbe47
KB
17657 type = make_type_with_address_space (type, type_flags);
17658 }
17659 else if (TYPE_LENGTH (type) != byte_size)
17660 {
b98664d3 17661 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17662 }
2b4424c3
TT
17663 else if (TYPE_RAW_ALIGN (type) != alignment)
17664 {
b98664d3 17665 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17666 " - DIE at %s [in module %s]"),
17667 sect_offset_str (die->sect_off),
5e22e966 17668 objfile_name (cu->per_objfile->objfile));
2b4424c3 17669 }
6e70227d 17670 else
9a619af0
MS
17671 {
17672 /* Should we also complain about unhandled address classes? */
17673 }
c906108c 17674 }
8b2dbe47
KB
17675
17676 TYPE_LENGTH (type) = byte_size;
2b4424c3 17677 set_type_align (type, alignment);
f792889a 17678 return set_die_type (die, type, cu);
c906108c
SS
17679}
17680
17681/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17682 the user defined type vector. */
17683
f792889a 17684static struct type *
e7c27a73 17685read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17686{
17687 struct type *type;
17688 struct type *to_type;
17689 struct type *domain;
17690
e7c27a73
DJ
17691 to_type = die_type (die, cu);
17692 domain = die_containing_type (die, cu);
0d5de010 17693
7e314c57
JK
17694 /* The calls above may have already set the type for this DIE. */
17695 type = get_die_type (die, cu);
17696 if (type)
17697 return type;
17698
78134374 17699 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 17700 type = lookup_methodptr_type (to_type);
78134374 17701 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 17702 {
5e22e966 17703 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
17704
17705 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 17706 to_type->fields (), to_type->num_fields (),
a409645d 17707 to_type->has_varargs ());
7078baeb
TT
17708 type = lookup_methodptr_type (new_type);
17709 }
0d5de010
DJ
17710 else
17711 type = lookup_memberptr_type (to_type, domain);
c906108c 17712
f792889a 17713 return set_die_type (die, type, cu);
c906108c
SS
17714}
17715
4297a3f0 17716/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17717 the user defined type vector. */
17718
f792889a 17719static struct type *
4297a3f0 17720read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
dda83cd7 17721 enum type_code refcode)
c906108c 17722{
e7c27a73 17723 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17724 struct type *type, *target_type;
c906108c
SS
17725 struct attribute *attr;
17726
4297a3f0
AV
17727 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17728
7e314c57
JK
17729 target_type = die_type (die, cu);
17730
17731 /* The die_type call above may have already set the type for this DIE. */
17732 type = get_die_type (die, cu);
17733 if (type)
17734 return type;
17735
4297a3f0 17736 type = lookup_reference_type (target_type, refcode);
e142c38c 17737 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17738 if (attr != nullptr)
c906108c 17739 {
529908cb 17740 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
c906108c
SS
17741 }
17742 else
17743 {
107d2387 17744 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17745 }
2b4424c3 17746 maybe_set_alignment (cu, die, type);
f792889a 17747 return set_die_type (die, type, cu);
c906108c
SS
17748}
17749
cf363f18
MW
17750/* Add the given cv-qualifiers to the element type of the array. GCC
17751 outputs DWARF type qualifiers that apply to an array, not the
17752 element type. But GDB relies on the array element type to carry
17753 the cv-qualifiers. This mimics section 6.7.3 of the C99
17754 specification. */
17755
17756static struct type *
17757add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17758 struct type *base_type, int cnst, int voltl)
17759{
17760 struct type *el_type, *inner_array;
17761
17762 base_type = copy_type (base_type);
17763 inner_array = base_type;
17764
78134374 17765 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17766 {
17767 TYPE_TARGET_TYPE (inner_array) =
17768 copy_type (TYPE_TARGET_TYPE (inner_array));
17769 inner_array = TYPE_TARGET_TYPE (inner_array);
17770 }
17771
17772 el_type = TYPE_TARGET_TYPE (inner_array);
17773 cnst |= TYPE_CONST (el_type);
17774 voltl |= TYPE_VOLATILE (el_type);
17775 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17776
17777 return set_die_type (die, base_type, cu);
17778}
17779
f792889a 17780static struct type *
e7c27a73 17781read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17782{
f792889a 17783 struct type *base_type, *cv_type;
c906108c 17784
e7c27a73 17785 base_type = die_type (die, cu);
7e314c57
JK
17786
17787 /* The die_type call above may have already set the type for this DIE. */
17788 cv_type = get_die_type (die, cu);
17789 if (cv_type)
17790 return cv_type;
17791
2f608a3a
KW
17792 /* In case the const qualifier is applied to an array type, the element type
17793 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 17794 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 17795 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17796
f792889a
DJ
17797 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17798 return set_die_type (die, cv_type, cu);
c906108c
SS
17799}
17800
f792889a 17801static struct type *
e7c27a73 17802read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17803{
f792889a 17804 struct type *base_type, *cv_type;
c906108c 17805
e7c27a73 17806 base_type = die_type (die, cu);
7e314c57
JK
17807
17808 /* The die_type call above may have already set the type for this DIE. */
17809 cv_type = get_die_type (die, cu);
17810 if (cv_type)
17811 return cv_type;
17812
cf363f18
MW
17813 /* In case the volatile qualifier is applied to an array type, the
17814 element type is so qualified, not the array type (section 6.7.3
17815 of C99). */
78134374 17816 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17817 return add_array_cv_type (die, cu, base_type, 0, 1);
17818
f792889a
DJ
17819 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17820 return set_die_type (die, cv_type, cu);
c906108c
SS
17821}
17822
06d66ee9
TT
17823/* Handle DW_TAG_restrict_type. */
17824
17825static struct type *
17826read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17827{
17828 struct type *base_type, *cv_type;
17829
17830 base_type = die_type (die, cu);
17831
17832 /* The die_type call above may have already set the type for this DIE. */
17833 cv_type = get_die_type (die, cu);
17834 if (cv_type)
17835 return cv_type;
17836
17837 cv_type = make_restrict_type (base_type);
17838 return set_die_type (die, cv_type, cu);
17839}
17840
a2c2acaf
MW
17841/* Handle DW_TAG_atomic_type. */
17842
17843static struct type *
17844read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17845{
17846 struct type *base_type, *cv_type;
17847
17848 base_type = die_type (die, cu);
17849
17850 /* The die_type call above may have already set the type for this DIE. */
17851 cv_type = get_die_type (die, cu);
17852 if (cv_type)
17853 return cv_type;
17854
17855 cv_type = make_atomic_type (base_type);
17856 return set_die_type (die, cv_type, cu);
17857}
17858
c906108c
SS
17859/* Extract all information from a DW_TAG_string_type DIE and add to
17860 the user defined type vector. It isn't really a user defined type,
17861 but it behaves like one, with other DIE's using an AT_user_def_type
17862 attribute to reference it. */
17863
f792889a 17864static struct type *
e7c27a73 17865read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17866{
5e22e966 17867 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17868 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17869 struct type *type, *range_type, *index_type, *char_type;
17870 struct attribute *attr;
216a7e6b
AB
17871 struct dynamic_prop prop;
17872 bool length_is_constant = true;
17873 LONGEST length;
17874
17875 /* There are a couple of places where bit sizes might be made use of
17876 when parsing a DW_TAG_string_type, however, no producer that we know
17877 of make use of these. Handling bit sizes that are a multiple of the
17878 byte size is easy enough, but what about other bit sizes? Lets deal
17879 with that problem when we have to. Warn about these attributes being
17880 unsupported, then parse the type and ignore them like we always
17881 have. */
17882 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17883 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17884 {
17885 static bool warning_printed = false;
17886 if (!warning_printed)
17887 {
17888 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17889 "currently supported on DW_TAG_string_type."));
17890 warning_printed = true;
17891 }
17892 }
c906108c 17893
e142c38c 17894 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17895 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17896 {
17897 /* The string length describes the location at which the length of
17898 the string can be found. The size of the length field can be
17899 specified with one of the attributes below. */
17900 struct type *prop_type;
17901 struct attribute *len
17902 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17903 if (len == nullptr)
17904 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17905 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17906 {
17907 /* Pass 0 as the default as we know this attribute is constant
17908 and the default value will not be returned. */
0826b30a 17909 LONGEST sz = len->constant_value (0);
293e7e51 17910 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17911 }
17912 else
17913 {
17914 /* If the size is not specified then we assume it is the size of
17915 an address on this target. */
293e7e51 17916 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17917 }
17918
17919 /* Convert the attribute into a dynamic property. */
17920 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17921 length = 1;
17922 else
17923 length_is_constant = false;
17924 }
17925 else if (attr != nullptr)
17926 {
17927 /* This DW_AT_string_length just contains the length with no
17928 indirection. There's no need to create a dynamic property in this
17929 case. Pass 0 for the default value as we know it will not be
17930 returned in this case. */
0826b30a 17931 length = attr->constant_value (0);
216a7e6b
AB
17932 }
17933 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17934 {
216a7e6b 17935 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17936 length = attr->constant_value (1);
c906108c
SS
17937 }
17938 else
17939 {
216a7e6b
AB
17940 /* Use 1 as a fallback length if we have nothing else. */
17941 length = 1;
c906108c 17942 }
6ccb9162 17943
46bf5051 17944 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17945 if (length_is_constant)
17946 range_type = create_static_range_type (NULL, index_type, 1, length);
17947 else
17948 {
17949 struct dynamic_prop low_bound;
17950
8c2e4e06 17951 low_bound.set_const_val (1);
216a7e6b
AB
17952 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17953 }
3b7538c0
UW
17954 char_type = language_string_char_type (cu->language_defn, gdbarch);
17955 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17956
f792889a 17957 return set_die_type (die, type, cu);
c906108c
SS
17958}
17959
4d804846
JB
17960/* Assuming that DIE corresponds to a function, returns nonzero
17961 if the function is prototyped. */
17962
17963static int
17964prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17965{
17966 struct attribute *attr;
17967
17968 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c45bc3f8 17969 if (attr && attr->as_boolean ())
4d804846
JB
17970 return 1;
17971
17972 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17973 is only meaningful for C, but the concept also extends to other
4d804846
JB
17974 languages that allow unprototyped functions (Eg: Objective C).
17975 For all other languages, assume that functions are always
17976 prototyped. */
17977 if (cu->language != language_c
17978 && cu->language != language_objc
17979 && cu->language != language_opencl)
17980 return 1;
17981
17982 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17983 prototyped and unprototyped functions; default to prototyped,
17984 since that is more common in modern code (and RealView warns
17985 about unprototyped functions). */
17986 if (producer_is_realview (cu->producer))
17987 return 1;
17988
17989 return 0;
17990}
17991
c906108c
SS
17992/* Handle DIES due to C code like:
17993
17994 struct foo
c5aa993b
JM
17995 {
17996 int (*funcp)(int a, long l);
17997 int b;
17998 };
c906108c 17999
0963b4bd 18000 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 18001
f792889a 18002static struct type *
e7c27a73 18003read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18004{
5e22e966 18005 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
18006 struct type *type; /* Type that this function returns. */
18007 struct type *ftype; /* Function that returns above type. */
c906108c
SS
18008 struct attribute *attr;
18009
e7c27a73 18010 type = die_type (die, cu);
7e314c57
JK
18011
18012 /* The die_type call above may have already set the type for this DIE. */
18013 ftype = get_die_type (die, cu);
18014 if (ftype)
18015 return ftype;
18016
0c8b41f1 18017 ftype = lookup_function_type (type);
c906108c 18018
4d804846 18019 if (prototyped_function_p (die, cu))
27e69b7a 18020 ftype->set_is_prototyped (true);
c906108c 18021
c055b101
CV
18022 /* Store the calling convention in the type if it's available in
18023 the subroutine die. Otherwise set the calling convention to
18024 the default value DW_CC_normal. */
18025 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf 18026 if (attr != nullptr
529908cb 18027 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
d0922fcf 18028 TYPE_CALLING_CONVENTION (ftype)
529908cb 18029 = (enum dwarf_calling_convention) attr->constant_value (0);
54fcddd0
UW
18030 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
18031 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
18032 else
18033 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 18034
743649fd
MW
18035 /* Record whether the function returns normally to its caller or not
18036 if the DWARF producer set that information. */
18037 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
c45bc3f8 18038 if (attr && attr->as_boolean ())
743649fd
MW
18039 TYPE_NO_RETURN (ftype) = 1;
18040
76c10ea2
GM
18041 /* We need to add the subroutine type to the die immediately so
18042 we don't infinitely recurse when dealing with parameters
0963b4bd 18043 declared as the same subroutine type. */
76c10ea2 18044 set_die_type (die, ftype, cu);
6e70227d 18045
639d11d3 18046 if (die->child != NULL)
c906108c 18047 {
bb5ed363 18048 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 18049 struct die_info *child_die;
8072405b 18050 int nparams, iparams;
c906108c
SS
18051
18052 /* Count the number of parameters.
dda83cd7
SM
18053 FIXME: GDB currently ignores vararg functions, but knows about
18054 vararg member functions. */
8072405b 18055 nparams = 0;
639d11d3 18056 child_die = die->child;
c906108c
SS
18057 while (child_die && child_die->tag)
18058 {
18059 if (child_die->tag == DW_TAG_formal_parameter)
18060 nparams++;
18061 else if (child_die->tag == DW_TAG_unspecified_parameters)
1d6286ed
SM
18062 ftype->set_has_varargs (true);
18063
436c571c 18064 child_die = child_die->sibling;
c906108c
SS
18065 }
18066
18067 /* Allocate storage for parameters and fill them in. */
5e33d5f4 18068 ftype->set_num_fields (nparams);
3cabb6b0
SM
18069 ftype->set_fields
18070 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 18071
8072405b
JK
18072 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18073 even if we error out during the parameters reading below. */
18074 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 18075 ftype->field (iparams).set_type (void_type);
8072405b
JK
18076
18077 iparams = 0;
639d11d3 18078 child_die = die->child;
c906108c
SS
18079 while (child_die && child_die->tag)
18080 {
18081 if (child_die->tag == DW_TAG_formal_parameter)
18082 {
3ce3b1ba
PA
18083 struct type *arg_type;
18084
18085 /* DWARF version 2 has no clean way to discern C++
18086 static and non-static member functions. G++ helps
18087 GDB by marking the first parameter for non-static
18088 member functions (which is the this pointer) as
18089 artificial. We pass this information to
18090 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18091
18092 DWARF version 3 added DW_AT_object_pointer, which GCC
18093 4.5 does not yet generate. */
e142c38c 18094 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 18095 if (attr != nullptr)
c45bc3f8 18096 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
c906108c 18097 else
9c37b5ae 18098 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
18099 arg_type = die_type (child_die, cu);
18100
18101 /* RealView does not mark THIS as const, which the testsuite
18102 expects. GCC marks THIS as const in method definitions,
18103 but not in the class specifications (GCC PR 43053). */
18104 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18105 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18106 {
18107 int is_this = 0;
18108 struct dwarf2_cu *arg_cu = cu;
18109 const char *name = dwarf2_name (child_die, cu);
18110
18111 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 18112 if (attr != nullptr)
3ce3b1ba
PA
18113 {
18114 /* If the compiler emits this, use it. */
18115 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18116 is_this = 1;
18117 }
18118 else if (name && strcmp (name, "this") == 0)
18119 /* Function definitions will have the argument names. */
18120 is_this = 1;
18121 else if (name == NULL && iparams == 0)
18122 /* Declarations may not have the names, so like
18123 elsewhere in GDB, assume an artificial first
18124 argument is "this". */
18125 is_this = 1;
18126
18127 if (is_this)
18128 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18129 arg_type, 0);
18130 }
18131
5d14b6e5 18132 ftype->field (iparams).set_type (arg_type);
c906108c
SS
18133 iparams++;
18134 }
436c571c 18135 child_die = child_die->sibling;
c906108c
SS
18136 }
18137 }
18138
76c10ea2 18139 return ftype;
c906108c
SS
18140}
18141
f792889a 18142static struct type *
e7c27a73 18143read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18144{
5e22e966 18145 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 18146 const char *name = NULL;
3c8e0968 18147 struct type *this_type, *target_type;
c906108c 18148
94af9270 18149 name = dwarf2_full_name (NULL, die, cu);
19f392bc 18150 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
8f53807e 18151 this_type->set_target_is_stub (true);
f792889a 18152 set_die_type (die, this_type, cu);
3c8e0968
DE
18153 target_type = die_type (die, cu);
18154 if (target_type != this_type)
18155 TYPE_TARGET_TYPE (this_type) = target_type;
18156 else
18157 {
18158 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18159 spec and cause infinite loops in GDB. */
b98664d3 18160 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
18161 "- DIE at %s [in module %s]"),
18162 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
18163 TYPE_TARGET_TYPE (this_type) = NULL;
18164 }
e4003a34
TV
18165 if (name == NULL)
18166 {
18167 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18168 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18169 Handle these by just returning the target type, rather than
18170 constructing an anonymous typedef type and trying to handle this
18171 elsewhere. */
18172 set_die_type (die, target_type, cu);
18173 return target_type;
18174 }
f792889a 18175 return this_type;
c906108c
SS
18176}
18177
a625a8c9
TT
18178/* Helper for get_dwarf2_rational_constant that computes the value of
18179 a given gmp_mpz given an attribute. */
18180
18181static void
18182get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
18183{
18184 /* GCC will sometimes emit a 16-byte constant value as a DWARF
18185 location expression that pushes an implicit value. */
18186 if (attr->form == DW_FORM_exprloc)
18187 {
18188 dwarf_block *blk = attr->as_block ();
18189 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18190 {
18191 uint64_t len;
18192 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18193 blk->data + blk->size,
18194 &len);
18195 if (ptr - blk->data + len <= blk->size)
18196 {
18197 mpz_import (value->val, len,
18198 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18199 1, 0, 0, ptr);
18200 return;
18201 }
18202 }
18203
18204 /* On failure set it to 1. */
18205 *value = gdb_mpz (1);
18206 }
18207 else if (attr->form_is_block ())
18208 {
18209 dwarf_block *blk = attr->as_block ();
18210 mpz_import (value->val, blk->size,
18211 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18212 1, 0, 0, blk->data);
18213 }
18214 else
18215 *value = gdb_mpz (attr->constant_value (1));
18216}
18217
09584414
JB
18218/* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18219 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18220
18221 If the numerator and/or numerator attribute is missing,
18222 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18223 untouched. */
18224
18225static void
18226get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
5cde1d82 18227 gdb_mpz *numerator, gdb_mpz *denominator)
09584414
JB
18228{
18229 struct attribute *num_attr, *denom_attr;
18230
18231 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18232 if (num_attr == nullptr)
18233 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18234 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18235
18236 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18237 if (denom_attr == nullptr)
18238 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18239 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18240
18241 if (num_attr == nullptr || denom_attr == nullptr)
18242 return;
18243
a625a8c9
TT
18244 get_mpz (cu, numerator, num_attr);
18245 get_mpz (cu, denominator, denom_attr);
09584414
JB
18246}
18247
18248/* Same as get_dwarf2_rational_constant, but extracting an unsigned
18249 rational constant, rather than a signed one.
18250
18251 If the rational constant has a negative value, a complaint
18252 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18253
18254static void
18255get_dwarf2_unsigned_rational_constant (struct die_info *die,
18256 struct dwarf2_cu *cu,
5cde1d82
TT
18257 gdb_mpz *numerator,
18258 gdb_mpz *denominator)
09584414 18259{
5cde1d82
TT
18260 gdb_mpz num (1);
18261 gdb_mpz denom (1);
09584414
JB
18262
18263 get_dwarf2_rational_constant (die, cu, &num, &denom);
5cde1d82 18264 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
09584414 18265 {
5cde1d82
TT
18266 mpz_neg (num.val, num.val);
18267 mpz_neg (denom.val, denom.val);
09584414 18268 }
5cde1d82 18269 else if (mpz_sgn (num.val) == -1)
09584414
JB
18270 {
18271 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18272 " in DIE at %s"),
18273 sect_offset_str (die->sect_off));
18274 return;
18275 }
5cde1d82 18276 else if (mpz_sgn (denom.val) == -1)
09584414
JB
18277 {
18278 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18279 " in DIE at %s"),
18280 sect_offset_str (die->sect_off));
18281 return;
18282 }
18283
5cde1d82
TT
18284 *numerator = std::move (num);
18285 *denominator = std::move (denom);
09584414
JB
18286}
18287
bbcdf9ab
TT
18288/* Assuming that ENCODING is a string whose contents starting at the
18289 K'th character is "_nn" where "nn" is a decimal number, scan that
18290 number and set RESULT to the value. K is updated to point to the
18291 character immediately following the number.
18292
18293 If the string does not conform to the format described above, false
18294 is returned, and K may or may not be changed. */
18295
18296static bool
18297ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
18298{
18299 /* The next character should be an underscore ('_') followed
18300 by a digit. */
18301 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
18302 return false;
18303
18304 /* Skip the underscore. */
18305 k++;
18306 int start = k;
18307
18308 /* Determine the number of digits for our number. */
18309 while (isdigit (encoding[k]))
18310 k++;
18311 if (k == start)
18312 return false;
18313
18314 std::string copy (&encoding[start], k - start);
18315 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
18316 return false;
18317
18318 return true;
18319}
18320
18321/* Scan two numbers from ENCODING at OFFSET, assuming the string is of
18322 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
18323 DENOM, update OFFSET, and return true on success. Return false on
18324 failure. */
18325
18326static bool
18327ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
18328 gdb_mpz *num, gdb_mpz *denom)
18329{
18330 if (!ada_get_gnat_encoded_number (encoding, offset, num))
18331 return false;
18332 return ada_get_gnat_encoded_number (encoding, offset, denom);
18333}
18334
09584414 18335/* Assuming DIE corresponds to a fixed point type, finish the creation
bbcdf9ab
TT
18336 of the corresponding TYPE by setting its type-specific data. CU is
18337 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
18338 encodings. It is nullptr if the GNAT encoding should be
18339 ignored. */
09584414
JB
18340
18341static void
bbcdf9ab
TT
18342finish_fixed_point_type (struct type *type, const char *suffix,
18343 struct die_info *die, struct dwarf2_cu *cu)
09584414 18344{
09584414
JB
18345 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18346 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18347
bbcdf9ab
TT
18348 /* If GNAT encodings are preferred, don't examine the
18349 attributes. */
18350 struct attribute *attr = nullptr;
18351 if (suffix == nullptr)
18352 {
18353 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18354 if (attr == nullptr)
18355 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18356 if (attr == nullptr)
18357 attr = dwarf2_attr (die, DW_AT_small, cu);
18358 }
09584414 18359
5cde1d82
TT
18360 /* Numerator and denominator of our fixed-point type's scaling factor.
18361 The default is a scaling factor of 1, which we use as a fallback
18362 when we are not able to decode it (problem with the debugging info,
18363 unsupported forms, bug in GDB, etc...). Using that as the default
18364 allows us to at least print the unscaled value, which might still
18365 be useful to a user. */
18366 gdb_mpz scale_num (1);
18367 gdb_mpz scale_denom (1);
18368
09584414
JB
18369 if (attr == nullptr)
18370 {
bbcdf9ab
TT
18371 int offset = 0;
18372 if (suffix != nullptr
18373 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18374 &scale_denom)
18375 /* The number might be encoded as _nn_dd_nn_dd, where the
18376 second ratio is the 'small value. In this situation, we
18377 want the second value. */
18378 && (suffix[offset] != '_'
18379 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18380 &scale_denom)))
18381 {
18382 /* Found it. */
18383 }
18384 else
18385 {
18386 /* Scaling factor not found. Assume a scaling factor of 1,
18387 and hope for the best. At least the user will be able to
18388 see the encoded value. */
18389 scale_num = 1;
18390 scale_denom = 1;
18391 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18392 sect_offset_str (die->sect_off));
18393 }
09584414
JB
18394 }
18395 else if (attr->name == DW_AT_binary_scale)
18396 {
18397 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18398 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18399
5cde1d82 18400 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
09584414
JB
18401 }
18402 else if (attr->name == DW_AT_decimal_scale)
18403 {
18404 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18405 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18406
5cde1d82 18407 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
09584414
JB
18408 }
18409 else if (attr->name == DW_AT_small)
18410 {
18411 struct die_info *scale_die;
18412 struct dwarf2_cu *scale_cu = cu;
18413
18414 scale_die = follow_die_ref (die, attr, &scale_cu);
18415 if (scale_die->tag == DW_TAG_constant)
18416 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18417 &scale_num, &scale_denom);
18418 else
18419 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18420 " (DIE at %s)"),
18421 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18422 }
18423 else
18424 {
18425 complaint (_("unsupported scale attribute %s for fixed-point type"
18426 " (DIE at %s)"),
18427 dwarf_attr_name (attr->name),
18428 sect_offset_str (die->sect_off));
18429 }
18430
2a12c336 18431 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
5cde1d82
TT
18432 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18433 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
09584414
JB
18434 mpq_canonicalize (scaling_factor.val);
18435}
18436
bbcdf9ab
TT
18437/* The gnat-encoding suffix for fixed point. */
18438
18439#define GNAT_FIXED_POINT_SUFFIX "___XF_"
18440
18441/* If NAME encodes an Ada fixed-point type, return a pointer to the
18442 "XF" suffix of the name. The text after this is what encodes the
18443 'small and 'delta information. Otherwise, return nullptr. */
18444
18445static const char *
18446gnat_encoded_fixed_point_type_info (const char *name)
18447{
18448 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18449}
18450
9b790ce7
UW
18451/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18452 (which may be different from NAME) to the architecture back-end to allow
18453 it to guess the correct format if necessary. */
18454
18455static struct type *
18456dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 18457 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 18458{
08feed99 18459 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
18460 const struct floatformat **format;
18461 struct type *type;
18462
18463 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18464 if (format)
103a685e 18465 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 18466 else
77b7c781 18467 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
18468
18469 return type;
18470}
18471
eb77c9df
AB
18472/* Allocate an integer type of size BITS and name NAME. */
18473
18474static struct type *
18475dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18476 int bits, int unsigned_p, const char *name)
18477{
18478 struct type *type;
18479
18480 /* Versions of Intel's C Compiler generate an integer type called "void"
18481 instead of using DW_TAG_unspecified_type. This has been seen on
18482 at least versions 14, 17, and 18. */
35ee2dc2
AB
18483 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18484 && strcmp (name, "void") == 0)
eb77c9df
AB
18485 type = objfile_type (objfile)->builtin_void;
18486 else
18487 type = init_integer_type (objfile, bits, unsigned_p, name);
18488
18489 return type;
18490}
18491
09584414
JB
18492/* Return true if DIE has a DW_AT_small attribute whose value is
18493 a constant rational, where both the numerator and denominator
18494 are equal to zero.
18495
18496 CU is the DIE's Compilation Unit. */
18497
18498static bool
18499has_zero_over_zero_small_attribute (struct die_info *die,
18500 struct dwarf2_cu *cu)
18501{
18502 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18503 if (attr == nullptr)
18504 return false;
18505
18506 struct dwarf2_cu *scale_cu = cu;
18507 struct die_info *scale_die
18508 = follow_die_ref (die, attr, &scale_cu);
18509
18510 if (scale_die->tag != DW_TAG_constant)
18511 return false;
18512
5cde1d82 18513 gdb_mpz num (1), denom (1);
09584414 18514 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
5cde1d82 18515 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
09584414
JB
18516}
18517
8bdc1658
AB
18518/* Initialise and return a floating point type of size BITS suitable for
18519 use as a component of a complex number. The NAME_HINT is passed through
18520 when initialising the floating point type and is the name of the complex
18521 type.
18522
18523 As DWARF doesn't currently provide an explicit name for the components
18524 of a complex number, but it can be helpful to have these components
18525 named, we try to select a suitable name based on the size of the
18526 component. */
18527static struct type *
18528dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18529 struct objfile *objfile,
103a685e
TT
18530 int bits, const char *name_hint,
18531 enum bfd_endian byte_order)
8bdc1658 18532{
08feed99 18533 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
18534 struct type *tt = nullptr;
18535
35add35e
AB
18536 /* Try to find a suitable floating point builtin type of size BITS.
18537 We're going to use the name of this type as the name for the complex
18538 target type that we are about to create. */
1db455a7 18539 switch (cu->language)
8bdc1658 18540 {
1db455a7
AB
18541 case language_fortran:
18542 switch (bits)
18543 {
18544 case 32:
18545 tt = builtin_f_type (gdbarch)->builtin_real;
18546 break;
18547 case 64:
18548 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18549 break;
18550 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18551 case 128:
18552 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18553 break;
18554 }
8bdc1658 18555 break;
1db455a7
AB
18556 default:
18557 switch (bits)
18558 {
18559 case 32:
18560 tt = builtin_type (gdbarch)->builtin_float;
18561 break;
18562 case 64:
18563 tt = builtin_type (gdbarch)->builtin_double;
18564 break;
18565 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18566 case 128:
18567 tt = builtin_type (gdbarch)->builtin_long_double;
18568 break;
18569 }
8bdc1658
AB
18570 break;
18571 }
18572
35add35e
AB
18573 /* If the type we found doesn't match the size we were looking for, then
18574 pretend we didn't find a type at all, the complex target type we
18575 create will then be nameless. */
a12e5744 18576 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
18577 tt = nullptr;
18578
7d93a1e0 18579 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 18580 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
18581}
18582
c906108c
SS
18583/* Find a representation of a given base type and install
18584 it in the TYPE field of the die. */
18585
f792889a 18586static struct type *
e7c27a73 18587read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18588{
5e22e966 18589 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
18590 struct type *type;
18591 struct attribute *attr;
19f392bc 18592 int encoding = 0, bits = 0;
15d034d0 18593 const char *name;
34877895 18594 gdbarch *arch;
c906108c 18595
e142c38c 18596 attr = dwarf2_attr (die, DW_AT_encoding, cu);
529908cb
TT
18597 if (attr != nullptr && attr->form_is_constant ())
18598 encoding = attr->constant_value (0);
e142c38c 18599 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18600 if (attr != nullptr)
529908cb 18601 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
39cbfefa 18602 name = dwarf2_name (die, cu);
6ccb9162 18603 if (!name)
34877895 18604 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 18605
08feed99 18606 arch = objfile->arch ();
103a685e
TT
18607 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18608
34877895 18609 attr = dwarf2_attr (die, DW_AT_endianity, cu);
529908cb 18610 if (attr != nullptr && attr->form_is_constant ())
103a685e 18611 {
529908cb 18612 int endianity = attr->constant_value (0);
103a685e
TT
18613
18614 switch (endianity)
18615 {
18616 case DW_END_big:
18617 byte_order = BFD_ENDIAN_BIG;
18618 break;
18619 case DW_END_little:
18620 byte_order = BFD_ENDIAN_LITTLE;
18621 break;
18622 default:
18623 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18624 break;
18625 }
18626 }
6ccb9162 18627
09584414
JB
18628 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18629 && cu->language == language_ada
18630 && has_zero_over_zero_small_attribute (die, cu))
18631 {
18632 /* brobecker/2018-02-24: This is a fixed point type for which
18633 the scaling factor is represented as fraction whose value
18634 does not make sense (zero divided by zero), so we should
18635 normally never see these. However, there is a small category
18636 of fixed point types for which GNAT is unable to provide
18637 the scaling factor via the standard DWARF mechanisms, and
18638 for which the info is provided via the GNAT encodings instead.
bbcdf9ab 18639 This is likely what this DIE is about. */
09584414
JB
18640 encoding = (encoding == DW_ATE_signed_fixed
18641 ? DW_ATE_signed
18642 : DW_ATE_unsigned);
18643 }
18644
bbcdf9ab
TT
18645 /* With GNAT encodings, fixed-point information will be encoded in
18646 the type name. Note that this can also occur with the above
18647 zero-over-zero case, which is why this is a separate "if" rather
18648 than an "else if". */
18649 const char *gnat_encoding_suffix = nullptr;
18650 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18651 && cu->language == language_ada
18652 && name != nullptr)
18653 {
18654 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18655 if (gnat_encoding_suffix != nullptr)
18656 {
18657 gdb_assert (startswith (gnat_encoding_suffix,
18658 GNAT_FIXED_POINT_SUFFIX));
18659 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18660 name, gnat_encoding_suffix - name);
18661 /* Use -1 here so that SUFFIX points at the "_" after the
18662 "XF". */
18663 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18664
18665 encoding = (encoding == DW_ATE_signed
18666 ? DW_ATE_signed_fixed
18667 : DW_ATE_unsigned_fixed);
18668 }
18669 }
18670
6ccb9162 18671 switch (encoding)
c906108c 18672 {
6ccb9162
UW
18673 case DW_ATE_address:
18674 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 18675 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 18676 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
18677 break;
18678 case DW_ATE_boolean:
19f392bc 18679 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
18680 break;
18681 case DW_ATE_complex_float:
103a685e
TT
18682 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18683 byte_order);
78134374 18684 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
18685 {
18686 if (name == nullptr)
18687 {
18688 struct obstack *obstack
5e22e966 18689 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 18690 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
18691 nullptr);
18692 }
18693 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18694 }
18695 else
18696 type = init_complex_type (name, type);
6ccb9162
UW
18697 break;
18698 case DW_ATE_decimal_float:
19f392bc 18699 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
18700 break;
18701 case DW_ATE_float:
103a685e 18702 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
18703 break;
18704 case DW_ATE_signed:
eb77c9df 18705 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18706 break;
18707 case DW_ATE_unsigned:
3b2b8fea
TT
18708 if (cu->language == language_fortran
18709 && name
61012eef 18710 && startswith (name, "character("))
19f392bc
UW
18711 type = init_character_type (objfile, bits, 1, name);
18712 else
eb77c9df 18713 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
18714 break;
18715 case DW_ATE_signed_char:
6e70227d 18716 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
18717 || cu->language == language_pascal
18718 || cu->language == language_fortran)
19f392bc
UW
18719 type = init_character_type (objfile, bits, 0, name);
18720 else
eb77c9df 18721 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18722 break;
18723 case DW_ATE_unsigned_char:
868a0084 18724 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 18725 || cu->language == language_pascal
c44af4eb
TT
18726 || cu->language == language_fortran
18727 || cu->language == language_rust)
19f392bc
UW
18728 type = init_character_type (objfile, bits, 1, name);
18729 else
eb77c9df 18730 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 18731 break;
75079b2b 18732 case DW_ATE_UTF:
53e710ac 18733 {
53e710ac
PA
18734 if (bits == 16)
18735 type = builtin_type (arch)->builtin_char16;
18736 else if (bits == 32)
18737 type = builtin_type (arch)->builtin_char32;
18738 else
18739 {
b98664d3 18740 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 18741 bits);
eb77c9df 18742 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
18743 }
18744 return set_die_type (die, type, cu);
18745 }
75079b2b 18746 break;
09584414
JB
18747 case DW_ATE_signed_fixed:
18748 type = init_fixed_point_type (objfile, bits, 0, name);
bbcdf9ab 18749 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414
JB
18750 break;
18751 case DW_ATE_unsigned_fixed:
18752 type = init_fixed_point_type (objfile, bits, 1, name);
bbcdf9ab 18753 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414 18754 break;
75079b2b 18755
6ccb9162 18756 default:
b98664d3 18757 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 18758 dwarf_type_encoding_name (encoding));
77b7c781 18759 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18760 break;
c906108c 18761 }
6ccb9162 18762
0114d602 18763 if (name && strcmp (name, "char") == 0)
15152a54 18764 type->set_has_no_signedness (true);
0114d602 18765
2b4424c3
TT
18766 maybe_set_alignment (cu, die, type);
18767
db558e34 18768 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
34877895 18769
20a5fcbd
TT
18770 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18771 {
18772 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
529908cb 18773 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
20a5fcbd 18774 {
529908cb 18775 unsigned real_bit_size = attr->as_unsigned ();
20a5fcbd
TT
18776 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18777 /* Only use the attributes if they make sense together. */
18778 if (attr == nullptr
529908cb
TT
18779 || (attr->as_unsigned () + real_bit_size
18780 <= 8 * TYPE_LENGTH (type)))
20a5fcbd
TT
18781 {
18782 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18783 = real_bit_size;
18784 if (attr != nullptr)
18785 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
529908cb 18786 = attr->as_unsigned ();
20a5fcbd
TT
18787 }
18788 }
18789 }
18790
f792889a 18791 return set_die_type (die, type, cu);
c906108c
SS
18792}
18793
80180f79
SA
18794/* Parse dwarf attribute if it's a block, reference or constant and put the
18795 resulting value of the attribute into struct bound_prop.
18796 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18797
18798static int
18799attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
18800 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18801 struct type *default_type)
80180f79
SA
18802{
18803 struct dwarf2_property_baton *baton;
5e22e966 18804 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
18805 struct objfile *objfile = per_objfile->objfile;
18806 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 18807
9a49df9d
AB
18808 gdb_assert (default_type != NULL);
18809
80180f79
SA
18810 if (attr == NULL || prop == NULL)
18811 return 0;
18812
4fc6c0d5 18813 if (attr->form_is_block ())
80180f79 18814 {
8d749320 18815 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18816 baton->property_type = default_type;
80180f79 18817 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18818 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18819
18820 struct dwarf_block *block = attr->as_block ();
18821 baton->locexpr.size = block->size;
18822 baton->locexpr.data = block->data;
216a7e6b
AB
18823 switch (attr->name)
18824 {
18825 case DW_AT_string_length:
18826 baton->locexpr.is_reference = true;
18827 break;
18828 default:
18829 baton->locexpr.is_reference = false;
18830 break;
18831 }
8c2e4e06
SM
18832
18833 prop->set_locexpr (baton);
18834 gdb_assert (prop->baton () != NULL);
80180f79 18835 }
cd6c91b4 18836 else if (attr->form_is_ref ())
80180f79
SA
18837 {
18838 struct dwarf2_cu *target_cu = cu;
18839 struct die_info *target_die;
18840 struct attribute *target_attr;
18841
18842 target_die = follow_die_ref (die, attr, &target_cu);
18843 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18844 if (target_attr == NULL)
18845 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18846 target_cu);
80180f79
SA
18847 if (target_attr == NULL)
18848 return 0;
18849
df25ebbd 18850 switch (target_attr->name)
80180f79 18851 {
df25ebbd 18852 case DW_AT_location:
cd6c91b4 18853 if (target_attr->form_is_section_offset ())
df25ebbd 18854 {
8d749320 18855 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18856 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18857 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
8c2e4e06
SM
18858 prop->set_loclist (baton);
18859 gdb_assert (prop->baton () != NULL);
df25ebbd 18860 }
4fc6c0d5 18861 else if (target_attr->form_is_block ())
df25ebbd 18862 {
8d749320 18863 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18864 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18865 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18866 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18867 struct dwarf_block *block = target_attr->as_block ();
18868 baton->locexpr.size = block->size;
18869 baton->locexpr.data = block->data;
9a49df9d 18870 baton->locexpr.is_reference = true;
8c2e4e06
SM
18871 prop->set_locexpr (baton);
18872 gdb_assert (prop->baton () != NULL);
df25ebbd
JB
18873 }
18874 else
18875 {
18876 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18877 "dynamic property");
18878 return 0;
18879 }
18880 break;
18881 case DW_AT_data_member_location:
18882 {
18883 LONGEST offset;
18884
18885 if (!handle_data_member_location (target_die, target_cu,
18886 &offset))
18887 return 0;
18888
8d749320 18889 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18890 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18891 target_cu);
df25ebbd
JB
18892 baton->offset_info.offset = offset;
18893 baton->offset_info.type = die_type (target_die, target_cu);
8c2e4e06 18894 prop->set_addr_offset (baton);
df25ebbd
JB
18895 break;
18896 }
80180f79
SA
18897 }
18898 }
cd6c91b4 18899 else if (attr->form_is_constant ())
8c2e4e06 18900 prop->set_const_val (attr->constant_value (0));
80180f79
SA
18901 else
18902 {
18903 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18904 dwarf2_name (die, cu));
18905 return 0;
18906 }
18907
18908 return 1;
18909}
18910
09ba997f 18911/* See read.h. */
9a49df9d 18912
09ba997f 18913struct type *
293e7e51 18914dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 18915{
9a49df9d
AB
18916 struct type *int_type;
18917
18918 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18919#define TRY_TYPE(F) \
18920 int_type = (unsigned_p \
18921 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18922 : objfile_type (objfile)->builtin_ ## F); \
18923 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18924 return int_type
18925
18926 TRY_TYPE (char);
18927 TRY_TYPE (short);
18928 TRY_TYPE (int);
18929 TRY_TYPE (long);
18930 TRY_TYPE (long_long);
18931
18932#undef TRY_TYPE
18933
18934 gdb_assert_not_reached ("unable to find suitable integer type");
18935}
18936
09ba997f 18937/* See read.h. */
11a8b164 18938
09ba997f 18939struct type *
293e7e51 18940dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 18941{
293e7e51
SM
18942 int addr_size = this->per_cu->addr_size ();
18943 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
18944}
18945
b86352cf
AB
18946/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18947 present (which is valid) then compute the default type based on the
18948 compilation units address size. */
18949
18950static struct type *
18951read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18952{
18953 struct type *index_type = die_type (die, cu);
18954
18955 /* Dwarf-2 specifications explicitly allows to create subrange types
18956 without specifying a base type.
18957 In that case, the base type must be set to the type of
18958 the lower bound, upper bound or count, in that order, if any of these
18959 three attributes references an object that has a type.
18960 If no base type is found, the Dwarf-2 specifications say that
18961 a signed integer type of size equal to the size of an address should
18962 be used.
18963 For the following C code: `extern char gdb_int [];'
18964 GCC produces an empty range DIE.
18965 FIXME: muller/2010-05-28: Possible references to object for low bound,
18966 high bound or count are not yet handled by this code. */
78134374 18967 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 18968 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
18969
18970 return index_type;
18971}
18972
a02abb62
JB
18973/* Read the given DW_AT_subrange DIE. */
18974
f792889a 18975static struct type *
a02abb62
JB
18976read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18977{
4c9ad8c2 18978 struct type *base_type, *orig_base_type;
a02abb62
JB
18979 struct type *range_type;
18980 struct attribute *attr;
729efb13 18981 struct dynamic_prop low, high;
4fae6e18 18982 int low_default_is_valid;
c451ebe5 18983 int high_bound_is_count = 0;
15d034d0 18984 const char *name;
d359392f 18985 ULONGEST negative_mask;
e77813c8 18986
b86352cf
AB
18987 orig_base_type = read_subrange_index_type (die, cu);
18988
4c9ad8c2
TT
18989 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18990 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18991 creating the range type, but we use the result of check_typedef
18992 when examining properties of the type. */
18993 base_type = check_typedef (orig_base_type);
a02abb62 18994
7e314c57
JK
18995 /* The die_type call above may have already set the type for this DIE. */
18996 range_type = get_die_type (die, cu);
18997 if (range_type)
18998 return range_type;
18999
8c2e4e06 19000 high.set_const_val (0);
729efb13 19001
4fae6e18
JK
19002 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
19003 omitting DW_AT_lower_bound. */
19004 switch (cu->language)
6e70227d 19005 {
4fae6e18
JK
19006 case language_c:
19007 case language_cplus:
8c2e4e06 19008 low.set_const_val (0);
4fae6e18
JK
19009 low_default_is_valid = 1;
19010 break;
19011 case language_fortran:
8c2e4e06 19012 low.set_const_val (1);
4fae6e18
JK
19013 low_default_is_valid = 1;
19014 break;
19015 case language_d:
4fae6e18 19016 case language_objc:
c44af4eb 19017 case language_rust:
8c2e4e06 19018 low.set_const_val (0);
4fae6e18
JK
19019 low_default_is_valid = (cu->header.version >= 4);
19020 break;
19021 case language_ada:
19022 case language_m2:
19023 case language_pascal:
8c2e4e06 19024 low.set_const_val (1);
4fae6e18
JK
19025 low_default_is_valid = (cu->header.version >= 4);
19026 break;
19027 default:
8c2e4e06 19028 low.set_const_val (0);
4fae6e18
JK
19029 low_default_is_valid = 0;
19030 break;
a02abb62
JB
19031 }
19032
e142c38c 19033 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 19034 if (attr != nullptr)
9a49df9d 19035 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 19036 else if (!low_default_is_valid)
b98664d3 19037 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
19038 "- DIE at %s [in module %s]"),
19039 sect_offset_str (die->sect_off),
5e22e966 19040 objfile_name (cu->per_objfile->objfile));
a02abb62 19041
506f5c41
TV
19042 struct attribute *attr_ub, *attr_count;
19043 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 19044 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 19045 {
506f5c41 19046 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 19047 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 19048 {
c451ebe5 19049 /* If bounds are constant do the final calculation here. */
8c2e4e06
SM
19050 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
19051 high.set_const_val (low.const_val () + high.const_val () - 1);
c451ebe5
SA
19052 else
19053 high_bound_is_count = 1;
c2ff108b 19054 }
506f5c41
TV
19055 else
19056 {
19057 if (attr_ub != NULL)
19058 complaint (_("Unresolved DW_AT_upper_bound "
19059 "- DIE at %s [in module %s]"),
19060 sect_offset_str (die->sect_off),
5e22e966 19061 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
19062 if (attr_count != NULL)
19063 complaint (_("Unresolved DW_AT_count "
19064 "- DIE at %s [in module %s]"),
19065 sect_offset_str (die->sect_off),
5e22e966 19066 objfile_name (cu->per_objfile->objfile));
506f5c41 19067 }
e77813c8 19068 }
a02abb62 19069
4e962e74
TT
19070 LONGEST bias = 0;
19071 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 19072 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 19073 bias = bias_attr->constant_value (0);
4e962e74 19074
dbb9c2b1
JB
19075 /* Normally, the DWARF producers are expected to use a signed
19076 constant form (Eg. DW_FORM_sdata) to express negative bounds.
19077 But this is unfortunately not always the case, as witnessed
19078 with GCC, for instance, where the ambiguous DW_FORM_dataN form
19079 is used instead. To work around that ambiguity, we treat
19080 the bounds as signed, and thus sign-extend their values, when
19081 the base type is signed. */
6e70227d 19082 negative_mask =
d359392f 19083 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
8c2e4e06 19084 if (low.kind () == PROP_CONST
c6d940a9 19085 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
8c2e4e06
SM
19086 low.set_const_val (low.const_val () | negative_mask);
19087 if (high.kind () == PROP_CONST
c6d940a9 19088 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
8c2e4e06 19089 high.set_const_val (high.const_val () | negative_mask);
43bbcdc2 19090
5bbd8269
AB
19091 /* Check for bit and byte strides. */
19092 struct dynamic_prop byte_stride_prop;
19093 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
19094 if (attr_byte_stride != nullptr)
19095 {
293e7e51 19096 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
19097 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
19098 prop_type);
19099 }
19100
19101 struct dynamic_prop bit_stride_prop;
19102 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
19103 if (attr_bit_stride != nullptr)
19104 {
19105 /* It only makes sense to have either a bit or byte stride. */
19106 if (attr_byte_stride != nullptr)
19107 {
19108 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19109 "- DIE at %s [in module %s]"),
19110 sect_offset_str (die->sect_off),
5e22e966 19111 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
19112 attr_bit_stride = nullptr;
19113 }
19114 else
19115 {
293e7e51 19116 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
19117 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19118 prop_type);
19119 }
19120 }
19121
19122 if (attr_byte_stride != nullptr
19123 || attr_bit_stride != nullptr)
19124 {
19125 bool byte_stride_p = (attr_byte_stride != nullptr);
19126 struct dynamic_prop *stride
19127 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19128
19129 range_type
19130 = create_range_type_with_stride (NULL, orig_base_type, &low,
19131 &high, bias, stride, byte_stride_p);
19132 }
19133 else
19134 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 19135
c451ebe5 19136 if (high_bound_is_count)
599088e3 19137 range_type->bounds ()->flag_upper_bound_is_count = 1;
c451ebe5 19138
c2ff108b
JK
19139 /* Ada expects an empty array on no boundary attributes. */
19140 if (attr == NULL && cu->language != language_ada)
8c2e4e06 19141 range_type->bounds ()->high.set_undefined ();
c2ff108b 19142
39cbfefa
DJ
19143 name = dwarf2_name (die, cu);
19144 if (name)
d0e39ea2 19145 range_type->set_name (name);
6e70227d 19146
e142c38c 19147 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 19148 if (attr != nullptr)
529908cb 19149 TYPE_LENGTH (range_type) = attr->constant_value (0);
a02abb62 19150
2b4424c3
TT
19151 maybe_set_alignment (cu, die, range_type);
19152
7e314c57
JK
19153 set_die_type (die, range_type, cu);
19154
19155 /* set_die_type should be already done. */
b4ba55a1
JB
19156 set_descriptive_type (range_type, die, cu);
19157
7e314c57 19158 return range_type;
a02abb62 19159}
6e70227d 19160
f792889a 19161static struct type *
81a17f79
JB
19162read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19163{
19164 struct type *type;
81a17f79 19165
5e22e966 19166 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 19167 type->set_name (dwarf2_name (die, cu));
81a17f79 19168
74a2f8ff 19169 /* In Ada, an unspecified type is typically used when the description
85102364 19170 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
19171 such a type, we treat it as a stub, and try to resolve it later on,
19172 when needed. */
19173 if (cu->language == language_ada)
b4b73759 19174 type->set_is_stub (true);
74a2f8ff 19175
f792889a 19176 return set_die_type (die, type, cu);
81a17f79 19177}
a02abb62 19178
639d11d3
DC
19179/* Read a single die and all its descendents. Set the die's sibling
19180 field to NULL; set other fields in the die correctly, and set all
19181 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19182 location of the info_ptr after reading all of those dies. PARENT
19183 is the parent of the die in question. */
19184
19185static struct die_info *
dee91e82 19186read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
19187 const gdb_byte *info_ptr,
19188 const gdb_byte **new_info_ptr,
dee91e82 19189 struct die_info *parent)
639d11d3
DC
19190{
19191 struct die_info *die;
d521ce57 19192 const gdb_byte *cur_ptr;
639d11d3 19193
3e225074 19194 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
19195 if (die == NULL)
19196 {
19197 *new_info_ptr = cur_ptr;
19198 return NULL;
19199 }
93311388 19200 store_in_ref_table (die, reader->cu);
639d11d3 19201
3e225074 19202 if (die->has_children)
bf6af496 19203 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
19204 else
19205 {
19206 die->child = NULL;
19207 *new_info_ptr = cur_ptr;
19208 }
19209
19210 die->sibling = NULL;
19211 die->parent = parent;
19212 return die;
19213}
19214
19215/* Read a die, all of its descendents, and all of its siblings; set
19216 all of the fields of all of the dies correctly. Arguments are as
19217 in read_die_and_children. */
19218
19219static struct die_info *
bf6af496 19220read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
19221 const gdb_byte *info_ptr,
19222 const gdb_byte **new_info_ptr,
bf6af496 19223 struct die_info *parent)
639d11d3
DC
19224{
19225 struct die_info *first_die, *last_sibling;
d521ce57 19226 const gdb_byte *cur_ptr;
639d11d3 19227
c906108c 19228 cur_ptr = info_ptr;
639d11d3
DC
19229 first_die = last_sibling = NULL;
19230
19231 while (1)
c906108c 19232 {
639d11d3 19233 struct die_info *die
dee91e82 19234 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 19235
1d325ec1 19236 if (die == NULL)
c906108c 19237 {
639d11d3
DC
19238 *new_info_ptr = cur_ptr;
19239 return first_die;
c906108c 19240 }
1d325ec1
DJ
19241
19242 if (!first_die)
19243 first_die = die;
c906108c 19244 else
1d325ec1
DJ
19245 last_sibling->sibling = die;
19246
19247 last_sibling = die;
c906108c 19248 }
c906108c
SS
19249}
19250
bf6af496
DE
19251/* Read a die, all of its descendents, and all of its siblings; set
19252 all of the fields of all of the dies correctly. Arguments are as
19253 in read_die_and_children.
19254 This the main entry point for reading a DIE and all its children. */
19255
19256static struct die_info *
19257read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
19258 const gdb_byte *info_ptr,
19259 const gdb_byte **new_info_ptr,
bf6af496
DE
19260 struct die_info *parent)
19261{
19262 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19263 new_info_ptr, parent);
19264
b4f54984 19265 if (dwarf_die_debug)
bf6af496
DE
19266 {
19267 fprintf_unfiltered (gdb_stdlog,
19268 "Read die from %s@0x%x of %s:\n",
96b79293 19269 reader->die_section->get_name (),
bf6af496
DE
19270 (unsigned) (info_ptr - reader->die_section->buffer),
19271 bfd_get_filename (reader->abfd));
b4f54984 19272 dump_die (die, dwarf_die_debug);
bf6af496
DE
19273 }
19274
19275 return die;
19276}
19277
3019eac3
DE
19278/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19279 attributes.
19280 The caller is responsible for filling in the extra attributes
19281 and updating (*DIEP)->num_attrs.
19282 Set DIEP to point to a newly allocated die with its information,
3e225074 19283 except for its child, sibling, and parent fields. */
93311388 19284
d521ce57 19285static const gdb_byte *
3019eac3 19286read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 19287 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 19288 int num_extra_attrs)
93311388 19289{
b64f50a1 19290 unsigned int abbrev_number, bytes_read, i;
7c32eebb 19291 const struct abbrev_info *abbrev;
93311388
DE
19292 struct die_info *die;
19293 struct dwarf2_cu *cu = reader->cu;
19294 bfd *abfd = reader->abfd;
19295
9c541725 19296 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
19297 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19298 info_ptr += bytes_read;
19299 if (!abbrev_number)
19300 {
19301 *diep = NULL;
93311388
DE
19302 return info_ptr;
19303 }
19304
685af9cd 19305 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 19306 if (!abbrev)
348e048f
DE
19307 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19308 abbrev_number,
19309 bfd_get_filename (abfd));
19310
3019eac3 19311 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 19312 die->sect_off = sect_off;
93311388
DE
19313 die->tag = abbrev->tag;
19314 die->abbrev = abbrev_number;
3e225074 19315 die->has_children = abbrev->has_children;
93311388 19316
3019eac3
DE
19317 /* Make the result usable.
19318 The caller needs to update num_attrs after adding the extra
19319 attributes. */
93311388
DE
19320 die->num_attrs = abbrev->num_attrs;
19321
7a5f294d 19322 bool any_need_reprocess = false;
93311388 19323 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e 19324 {
7a5f294d
TT
19325 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19326 info_ptr);
19327 if (die->attrs[i].requires_reprocessing_p ())
19328 any_need_reprocess = true;
18a8505e
AT
19329 }
19330
052c8bb8 19331 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
529908cb
TT
19332 if (attr != nullptr && attr->form_is_unsigned ())
19333 cu->str_offsets_base = attr->as_unsigned ();
93311388 19334
41144253 19335 attr = die->attr (DW_AT_loclists_base);
19336 if (attr != nullptr)
529908cb 19337 cu->loclist_base = attr->as_unsigned ();
41144253 19338
a39fdb41 19339 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
19340 if (maybe_addr_base.has_value ())
19341 cu->addr_base = *maybe_addr_base;
d0ce17d8
CT
19342
19343 attr = die->attr (DW_AT_rnglists_base);
19344 if (attr != nullptr)
2b0c7f41 19345 cu->rnglists_base = attr->as_unsigned ();
d0ce17d8 19346
7a5f294d
TT
19347 if (any_need_reprocess)
19348 {
19349 for (i = 0; i < abbrev->num_attrs; ++i)
19350 {
19351 if (die->attrs[i].requires_reprocessing_p ())
19352 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19353 }
19354 }
93311388 19355 *diep = die;
93311388
DE
19356 return info_ptr;
19357}
19358
3019eac3
DE
19359/* Read a die and all its attributes.
19360 Set DIEP to point to a newly allocated die with its information,
3e225074 19361 except for its child, sibling, and parent fields. */
3019eac3 19362
d521ce57 19363static const gdb_byte *
3019eac3 19364read_full_die (const struct die_reader_specs *reader,
3e225074 19365 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 19366{
d521ce57 19367 const gdb_byte *result;
bf6af496 19368
3e225074 19369 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 19370
b4f54984 19371 if (dwarf_die_debug)
bf6af496
DE
19372 {
19373 fprintf_unfiltered (gdb_stdlog,
19374 "Read die from %s@0x%x of %s:\n",
96b79293 19375 reader->die_section->get_name (),
bf6af496
DE
19376 (unsigned) (info_ptr - reader->die_section->buffer),
19377 bfd_get_filename (reader->abfd));
b4f54984 19378 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
19379 }
19380
19381 return result;
3019eac3 19382}
433df2d4 19383\f
c906108c 19384
72bf9492
DJ
19385/* Returns nonzero if TAG represents a type that we might generate a partial
19386 symbol for. */
19387
19388static int
d8f62e84 19389is_type_tag_for_partial (int tag, enum language lang)
72bf9492
DJ
19390{
19391 switch (tag)
19392 {
19393#if 0
19394 /* Some types that would be reasonable to generate partial symbols for,
d8f62e84
TT
19395 that we don't at present. Note that normally this does not
19396 matter, mainly because C compilers don't give names to these
19397 types, but instead emit DW_TAG_typedef. */
72bf9492
DJ
19398 case DW_TAG_file_type:
19399 case DW_TAG_ptr_to_member_type:
19400 case DW_TAG_set_type:
19401 case DW_TAG_string_type:
19402 case DW_TAG_subroutine_type:
19403#endif
d8f62e84
TT
19404
19405 /* GNAT may emit an array with a name, but no typedef, so we
19406 need to make a symbol in this case. */
19407 case DW_TAG_array_type:
19408 return lang == language_ada;
19409
72bf9492
DJ
19410 case DW_TAG_base_type:
19411 case DW_TAG_class_type:
680b30c7 19412 case DW_TAG_interface_type:
72bf9492
DJ
19413 case DW_TAG_enumeration_type:
19414 case DW_TAG_structure_type:
19415 case DW_TAG_subrange_type:
19416 case DW_TAG_typedef:
19417 case DW_TAG_union_type:
19418 return 1;
19419 default:
19420 return 0;
19421 }
19422}
19423
19424/* Load all DIEs that are interesting for partial symbols into memory. */
19425
19426static struct partial_die_info *
dee91e82 19427load_partial_dies (const struct die_reader_specs *reader,
d521ce57 19428 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 19429{
dee91e82 19430 struct dwarf2_cu *cu = reader->cu;
5e22e966 19431 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 19432 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 19433 unsigned int bytes_read;
5afb4e99 19434 unsigned int load_all = 0;
72bf9492
DJ
19435 int nesting_level = 1;
19436
19437 parent_die = NULL;
19438 last_die = NULL;
19439
7adf1e79
DE
19440 gdb_assert (cu->per_cu != NULL);
19441 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
19442 load_all = 1;
19443
72bf9492
DJ
19444 cu->partial_dies
19445 = htab_create_alloc_ex (cu->header.length / 12,
19446 partial_die_hash,
19447 partial_die_eq,
19448 NULL,
19449 &cu->comp_unit_obstack,
19450 hashtab_obstack_allocate,
19451 dummy_obstack_deallocate);
19452
72bf9492
DJ
19453 while (1)
19454 {
7c32eebb
TT
19455 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19456 &bytes_read);
72bf9492
DJ
19457
19458 /* A NULL abbrev means the end of a series of children. */
19459 if (abbrev == NULL)
19460 {
19461 if (--nesting_level == 0)
cd9983dd
YQ
19462 return first_die;
19463
72bf9492
DJ
19464 info_ptr += bytes_read;
19465 last_die = parent_die;
19466 parent_die = parent_die->die_parent;
19467 continue;
19468 }
19469
98bfdba5
PA
19470 /* Check for template arguments. We never save these; if
19471 they're seen, we just mark the parent, and go on our way. */
19472 if (parent_die != NULL
19473 && cu->language == language_cplus
19474 && (abbrev->tag == DW_TAG_template_type_param
19475 || abbrev->tag == DW_TAG_template_value_param))
19476 {
19477 parent_die->has_template_arguments = 1;
19478
19479 if (!load_all)
19480 {
19481 /* We don't need a partial DIE for the template argument. */
dee91e82 19482 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19483 continue;
19484 }
19485 }
19486
0d99eb77 19487 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
19488 Skip their other children. */
19489 if (!load_all
19490 && cu->language == language_cplus
19491 && parent_die != NULL
f9b5d5ea
TV
19492 && parent_die->tag == DW_TAG_subprogram
19493 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 19494 {
dee91e82 19495 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19496 continue;
19497 }
19498
5afb4e99
DJ
19499 /* Check whether this DIE is interesting enough to save. Normally
19500 we would not be interested in members here, but there may be
19501 later variables referencing them via DW_AT_specification (for
19502 static members). */
19503 if (!load_all
d8f62e84 19504 && !is_type_tag_for_partial (abbrev->tag, cu->language)
72929c62 19505 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
19506 && abbrev->tag != DW_TAG_enumerator
19507 && abbrev->tag != DW_TAG_subprogram
b1dc1806 19508 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 19509 && abbrev->tag != DW_TAG_lexical_block
72bf9492 19510 && abbrev->tag != DW_TAG_variable
5afb4e99 19511 && abbrev->tag != DW_TAG_namespace
f55ee35c 19512 && abbrev->tag != DW_TAG_module
95554aad 19513 && abbrev->tag != DW_TAG_member
74921315
KS
19514 && abbrev->tag != DW_TAG_imported_unit
19515 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
19516 {
19517 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19518 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
19519 continue;
19520 }
19521
6f06d47b
YQ
19522 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19523 abbrev);
cd9983dd 19524
48fbe735 19525 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
19526
19527 /* This two-pass algorithm for processing partial symbols has a
19528 high cost in cache pressure. Thus, handle some simple cases
19529 here which cover the majority of C partial symbols. DIEs
19530 which neither have specification tags in them, nor could have
19531 specification tags elsewhere pointing at them, can simply be
19532 processed and discarded.
19533
19534 This segment is also optional; scan_partial_symbols and
19535 add_partial_symbol will handle these DIEs if we chain
19536 them in normally. When compilers which do not emit large
19537 quantities of duplicate debug information are more common,
19538 this code can probably be removed. */
19539
19540 /* Any complete simple types at the top level (pretty much all
19541 of them, for a language without namespaces), can be processed
19542 directly. */
19543 if (parent_die == NULL
cd9983dd
YQ
19544 && pdi.has_specification == 0
19545 && pdi.is_declaration == 0
19546 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19547 || pdi.tag == DW_TAG_base_type
d8f62e84 19548 || pdi.tag == DW_TAG_array_type
cd9983dd 19549 || pdi.tag == DW_TAG_subrange_type))
72bf9492 19550 {
7d00ffec 19551 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
19552 add_partial_symbol (&pdi, cu);
19553
cd9983dd 19554 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19555 continue;
19556 }
19557
d8228535
JK
19558 /* The exception for DW_TAG_typedef with has_children above is
19559 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 19560 type_name_or_error will error on such types later.
d8228535
JK
19561
19562 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19563 it could not find the child DIEs referenced later, this is checked
19564 above. In correct DWARF DW_TAG_typedef should have no children. */
19565
cd9983dd 19566 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 19567 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 19568 "- DIE at %s [in module %s]"),
cd9983dd 19569 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 19570
72bf9492
DJ
19571 /* If we're at the second level, and we're an enumerator, and
19572 our parent has no specification (meaning possibly lives in a
19573 namespace elsewhere), then we can add the partial symbol now
19574 instead of queueing it. */
cd9983dd 19575 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
19576 && parent_die != NULL
19577 && parent_die->die_parent == NULL
19578 && parent_die->tag == DW_TAG_enumeration_type
19579 && parent_die->has_specification == 0)
19580 {
7d00ffec 19581 if (pdi.raw_name == NULL)
b98664d3 19582 complaint (_("malformed enumerator DIE ignored"));
72bf9492 19583 else if (building_psymtab)
f0fbb768 19584 add_partial_symbol (&pdi, cu);
72bf9492 19585
cd9983dd 19586 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19587 continue;
19588 }
19589
cd9983dd 19590 struct partial_die_info *part_die
6f06d47b 19591 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 19592
72bf9492
DJ
19593 /* We'll save this DIE so link it in. */
19594 part_die->die_parent = parent_die;
19595 part_die->die_sibling = NULL;
19596 part_die->die_child = NULL;
19597
19598 if (last_die && last_die == parent_die)
19599 last_die->die_child = part_die;
19600 else if (last_die)
19601 last_die->die_sibling = part_die;
19602
19603 last_die = part_die;
19604
19605 if (first_die == NULL)
19606 first_die = part_die;
19607
19608 /* Maybe add the DIE to the hash table. Not all DIEs that we
19609 find interesting need to be in the hash table, because we
19610 also have the parent/sibling/child chains; only those that we
19611 might refer to by offset later during partial symbol reading.
19612
19613 For now this means things that might have be the target of a
19614 DW_AT_specification, DW_AT_abstract_origin, or
19615 DW_AT_extension. DW_AT_extension will refer only to
19616 namespaces; DW_AT_abstract_origin refers to functions (and
19617 many things under the function DIE, but we do not recurse
19618 into function DIEs during partial symbol reading) and
19619 possibly variables as well; DW_AT_specification refers to
19620 declarations. Declarations ought to have the DW_AT_declaration
19621 flag. It happens that GCC forgets to put it in sometimes, but
19622 only for functions, not for types.
19623
19624 Adding more things than necessary to the hash table is harmless
19625 except for the performance cost. Adding too few will result in
5afb4e99
DJ
19626 wasted time in find_partial_die, when we reread the compilation
19627 unit with load_all_dies set. */
72bf9492 19628
5afb4e99 19629 if (load_all
72929c62 19630 || abbrev->tag == DW_TAG_constant
5afb4e99 19631 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
19632 || abbrev->tag == DW_TAG_variable
19633 || abbrev->tag == DW_TAG_namespace
19634 || part_die->is_declaration)
19635 {
19636 void **slot;
19637
19638 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
19639 to_underlying (part_die->sect_off),
19640 INSERT);
72bf9492
DJ
19641 *slot = part_die;
19642 }
19643
72bf9492 19644 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 19645 we have no reason to follow the children of structures; for other
98bfdba5
PA
19646 languages we have to, so that we can get at method physnames
19647 to infer fully qualified class names, for DW_AT_specification,
19648 and for C++ template arguments. For C++, we also look one level
19649 inside functions to find template arguments (if the name of the
19650 function does not already contain the template arguments).
bc30ff58 19651
0a4b0913
AB
19652 For Ada and Fortran, we need to scan the children of subprograms
19653 and lexical blocks as well because these languages allow the
19654 definition of nested entities that could be interesting for the
19655 debugger, such as nested subprograms for instance. */
72bf9492 19656 if (last_die->has_children
5afb4e99
DJ
19657 && (load_all
19658 || last_die->tag == DW_TAG_namespace
f55ee35c 19659 || last_die->tag == DW_TAG_module
72bf9492 19660 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
19661 || (cu->language == language_cplus
19662 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
19663 && (last_die->raw_name == NULL
19664 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
19665 || (cu->language != language_c
19666 && (last_die->tag == DW_TAG_class_type
680b30c7 19667 || last_die->tag == DW_TAG_interface_type
72bf9492 19668 || last_die->tag == DW_TAG_structure_type
bc30ff58 19669 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
19670 || ((cu->language == language_ada
19671 || cu->language == language_fortran)
bc30ff58
JB
19672 && (last_die->tag == DW_TAG_subprogram
19673 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
19674 {
19675 nesting_level++;
19676 parent_die = last_die;
19677 continue;
19678 }
19679
19680 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19681 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
19682
19683 /* Back to the top, do it again. */
19684 }
19685}
19686
6f06d47b 19687partial_die_info::partial_die_info (sect_offset sect_off_,
7c32eebb 19688 const struct abbrev_info *abbrev)
6f06d47b
YQ
19689 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19690{
19691}
19692
7d00ffec
TT
19693/* See class definition. */
19694
19695const char *
19696partial_die_info::name (dwarf2_cu *cu)
19697{
19698 if (!canonical_name && raw_name != nullptr)
19699 {
19700 struct objfile *objfile = cu->per_objfile->objfile;
19701 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19702 canonical_name = 1;
19703 }
19704
19705 return raw_name;
19706}
19707
35cc7ed7
YQ
19708/* Read a minimal amount of information into the minimal die structure.
19709 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 19710
48fbe735
YQ
19711const gdb_byte *
19712partial_die_info::read (const struct die_reader_specs *reader,
19713 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 19714{
dee91e82 19715 struct dwarf2_cu *cu = reader->cu;
976ca316 19716 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 19717 unsigned int i;
c5aa993b 19718 int has_low_pc_attr = 0;
c906108c 19719 int has_high_pc_attr = 0;
91da1414 19720 int high_pc_relative = 0;
c906108c 19721
fd0a254f 19722 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 19723 {
e7da7f8f 19724 attribute attr;
7a5f294d 19725 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18a8505e 19726 /* String and address offsets that need to do the reprocessing have
dda83cd7 19727 already been read at this point, so there is no need to wait until
18a8505e 19728 the loop terminates to do the reprocessing. */
7a5f294d 19729 if (attr.requires_reprocessing_p ())
d0ce17d8 19730 read_attribute_reprocess (reader, &attr, tag);
c906108c 19731 /* Store the data if it is of an attribute we want to keep in a
dda83cd7 19732 partial symbol table. */
c906108c
SS
19733 switch (attr.name)
19734 {
19735 case DW_AT_name:
48fbe735 19736 switch (tag)
71c25dea
TT
19737 {
19738 case DW_TAG_compile_unit:
95554aad 19739 case DW_TAG_partial_unit:
348e048f 19740 case DW_TAG_type_unit:
71c25dea
TT
19741 /* Compilation units have a DW_AT_name that is a filename, not
19742 a source language identifier. */
19743 case DW_TAG_enumeration_type:
19744 case DW_TAG_enumerator:
19745 /* These tags always have simple identifiers already; no need
19746 to canonicalize them. */
7d00ffec 19747 canonical_name = 1;
2c830f54 19748 raw_name = attr.as_string ();
71c25dea
TT
19749 break;
19750 default:
7d00ffec 19751 canonical_name = 0;
2c830f54 19752 raw_name = attr.as_string ();
71c25dea
TT
19753 break;
19754 }
c906108c 19755 break;
31ef98ae 19756 case DW_AT_linkage_name:
c906108c 19757 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19758 /* Note that both forms of linkage name might appear. We
19759 assume they will be the same, and we only store the last
19760 one we see. */
95f982e5 19761 linkage_name = attr.as_string ();
c906108c
SS
19762 break;
19763 case DW_AT_low_pc:
19764 has_low_pc_attr = 1;
95f982e5 19765 lowpc = attr.as_address ();
c906108c
SS
19766 break;
19767 case DW_AT_high_pc:
19768 has_high_pc_attr = 1;
95f982e5 19769 highpc = attr.as_address ();
cd6c91b4 19770 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 19771 high_pc_relative = 1;
c906108c
SS
19772 break;
19773 case DW_AT_location:
dda83cd7
SM
19774 /* Support the .debug_loc offsets. */
19775 if (attr.form_is_block ())
19776 {
9d2246fc 19777 d.locdesc = attr.as_block ();
dda83cd7
SM
19778 }
19779 else if (attr.form_is_section_offset ())
19780 {
4d3c2250 19781 dwarf2_complex_location_expr_complaint ();
dda83cd7
SM
19782 }
19783 else
19784 {
4d3c2250
KB
19785 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19786 "partial symbol information");
dda83cd7 19787 }
c906108c 19788 break;
c906108c 19789 case DW_AT_external:
c45bc3f8 19790 is_external = attr.as_boolean ();
c906108c
SS
19791 break;
19792 case DW_AT_declaration:
c45bc3f8 19793 is_declaration = attr.as_boolean ();
c906108c
SS
19794 break;
19795 case DW_AT_type:
48fbe735 19796 has_type = 1;
c906108c
SS
19797 break;
19798 case DW_AT_abstract_origin:
19799 case DW_AT_specification:
72bf9492 19800 case DW_AT_extension:
48fbe735 19801 has_specification = 1;
0826b30a 19802 spec_offset = attr.get_ref_die_offset ();
48fbe735 19803 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19804 || cu->per_cu->is_dwz);
c906108c
SS
19805 break;
19806 case DW_AT_sibling:
19807 /* Ignore absolute siblings, they might point outside of
19808 the current compile unit. */
19809 if (attr.form == DW_FORM_ref_addr)
b98664d3 19810 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19811 else
b9502d3f 19812 {
48fbe735 19813 const gdb_byte *buffer = reader->buffer;
0826b30a 19814 sect_offset off = attr.get_ref_die_offset ();
9c541725 19815 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19816
19817 if (sibling_ptr < info_ptr)
b98664d3 19818 complaint (_("DW_AT_sibling points backwards"));
22869d73 19819 else if (sibling_ptr > reader->buffer_end)
a0194fa8 19820 reader->die_section->overflow_complaint ();
b9502d3f 19821 else
48fbe735 19822 sibling = sibling_ptr;
b9502d3f 19823 }
c906108c 19824 break;
dda83cd7
SM
19825 case DW_AT_byte_size:
19826 has_byte_size = 1;
19827 break;
19828 case DW_AT_const_value:
19829 has_const_value = 1;
19830 break;
68511cec
CES
19831 case DW_AT_calling_convention:
19832 /* DWARF doesn't provide a way to identify a program's source-level
19833 entry point. DW_AT_calling_convention attributes are only meant
19834 to describe functions' calling conventions.
19835
19836 However, because it's a necessary piece of information in
0c1b455e
TT
19837 Fortran, and before DWARF 4 DW_CC_program was the only
19838 piece of debugging information whose definition refers to
19839 a 'main program' at all, several compilers marked Fortran
19840 main programs with DW_CC_program --- even when those
19841 functions use the standard calling conventions.
19842
19843 Although DWARF now specifies a way to provide this
19844 information, we support this practice for backward
19845 compatibility. */
529908cb 19846 if (attr.constant_value (0) == DW_CC_program
0c1b455e 19847 && cu->language == language_fortran)
48fbe735 19848 main_subprogram = 1;
68511cec 19849 break;
481860b3 19850 case DW_AT_inline:
529908cb
TT
19851 {
19852 LONGEST value = attr.constant_value (-1);
19853 if (value == DW_INL_inlined
19854 || value == DW_INL_declared_inlined)
19855 may_be_inlined = 1;
19856 }
481860b3 19857 break;
95554aad
TT
19858
19859 case DW_AT_import:
48fbe735 19860 if (tag == DW_TAG_imported_unit)
36586728 19861 {
0826b30a 19862 d.sect_off = attr.get_ref_die_offset ();
48fbe735 19863 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19864 || cu->per_cu->is_dwz);
19865 }
95554aad
TT
19866 break;
19867
0c1b455e 19868 case DW_AT_main_subprogram:
c45bc3f8 19869 main_subprogram = attr.as_boolean ();
0c1b455e
TT
19870 break;
19871
05caa1d2
TT
19872 case DW_AT_ranges:
19873 {
2b0c7f41
SM
19874 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19875 on DWARF version). */
19876 ULONGEST ranges_offset = attr.as_unsigned ();
19877
19878 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19879 this value. */
19880 if (tag != DW_TAG_compile_unit)
19881 ranges_offset += cu->gnu_ranges_base;
19882
05caa1d2 19883 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
d0ce17d8 19884 nullptr, tag))
05caa1d2
TT
19885 has_pc_info = 1;
19886 }
19887 break;
19888
c906108c
SS
19889 default:
19890 break;
19891 }
19892 }
19893
10d06d82
TT
19894 /* For Ada, if both the name and the linkage name appear, we prefer
19895 the latter. This lets "catch exception" work better, regardless
19896 of the order in which the name and linkage name were emitted.
19897 Really, though, this is just a workaround for the fact that gdb
19898 doesn't store both the name and the linkage name. */
19899 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 19900 raw_name = linkage_name;
10d06d82 19901
91da1414 19902 if (high_pc_relative)
48fbe735 19903 highpc += lowpc;
91da1414 19904
9373cf26
JK
19905 if (has_low_pc_attr && has_high_pc_attr)
19906 {
19907 /* When using the GNU linker, .gnu.linkonce. sections are used to
19908 eliminate duplicate copies of functions and vtables and such.
19909 The linker will arbitrarily choose one and discard the others.
19910 The AT_*_pc values for such functions refer to local labels in
19911 these sections. If the section from that file was discarded, the
19912 labels are not in the output, so the relocs get a value of 0.
19913 If this is a discarded function, mark the pc bounds as invalid,
19914 so that GDB will ignore it. */
976ca316 19915 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 19916 {
976ca316 19917 struct objfile *objfile = per_objfile->objfile;
08feed99 19918 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19919
b98664d3 19920 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19921 "for DIE at %s [in module %s]"),
48fbe735
YQ
19922 paddress (gdbarch, lowpc),
19923 sect_offset_str (sect_off),
9d8780f0 19924 objfile_name (objfile));
9373cf26
JK
19925 }
19926 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19927 else if (lowpc >= highpc)
9373cf26 19928 {
976ca316 19929 struct objfile *objfile = per_objfile->objfile;
08feed99 19930 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19931
b98664d3 19932 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19933 "for DIE at %s [in module %s]"),
48fbe735
YQ
19934 paddress (gdbarch, lowpc),
19935 paddress (gdbarch, highpc),
19936 sect_offset_str (sect_off),
9c541725 19937 objfile_name (objfile));
9373cf26
JK
19938 }
19939 else
48fbe735 19940 has_pc_info = 1;
9373cf26 19941 }
85cbf3d3 19942
c906108c
SS
19943 return info_ptr;
19944}
19945
72bf9492
DJ
19946/* Find a cached partial DIE at OFFSET in CU. */
19947
d590ff25
YQ
19948struct partial_die_info *
19949dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19950{
19951 struct partial_die_info *lookup_die = NULL;
6f06d47b 19952 struct partial_die_info part_die (sect_off);
72bf9492 19953
9a3c8263 19954 lookup_die = ((struct partial_die_info *)
d590ff25 19955 htab_find_with_hash (partial_dies, &part_die,
9c541725 19956 to_underlying (sect_off)));
72bf9492 19957
72bf9492
DJ
19958 return lookup_die;
19959}
19960
348e048f
DE
19961/* Find a partial DIE at OFFSET, which may or may not be in CU,
19962 except in the case of .debug_types DIEs which do not reference
19963 outside their CU (they do however referencing other types via
55f1336d 19964 DW_FORM_ref_sig8). */
72bf9492 19965
122cf0f2 19966static const struct cu_partial_die_info
9c541725 19967find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19968{
976ca316
SM
19969 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19970 struct objfile *objfile = per_objfile->objfile;
5afb4e99 19971 struct partial_die_info *pd = NULL;
72bf9492 19972
36586728 19973 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 19974 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 19975 {
d590ff25 19976 pd = cu->find_partial_die (sect_off);
5afb4e99 19977 if (pd != NULL)
fb816e8b 19978 return { cu, pd };
0d99eb77
DE
19979 /* We missed recording what we needed.
19980 Load all dies and try again. */
5afb4e99 19981 }
0d99eb77
DE
19982 else
19983 {
19984 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19985 if (cu->per_cu->is_debug_types)
0d99eb77 19986 {
9d8780f0
SM
19987 error (_("Dwarf Error: Type Unit at offset %s contains"
19988 " external reference to offset %s [in module %s].\n"),
19989 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19990 bfd_get_filename (objfile->obfd));
19991 }
7188ed02
SM
19992 dwarf2_per_cu_data *per_cu
19993 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 19994 per_objfile);
72bf9492 19995
976ca316 19996 cu = per_objfile->get_cu (per_cu);
7188ed02 19997 if (cu == NULL || cu->partial_dies == NULL)
976ca316 19998 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 19999
976ca316 20000 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
20001
20002 cu->last_used = 0;
20003 pd = cu->find_partial_die (sect_off);
0d99eb77 20004 }
5afb4e99 20005
dee91e82
DE
20006 /* If we didn't find it, and not all dies have been loaded,
20007 load them all and try again. */
20008
7188ed02 20009 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 20010 {
7188ed02 20011 cu->per_cu->load_all_dies = 1;
fd820528
DE
20012
20013 /* This is nasty. When we reread the DIEs, somewhere up the call chain
20014 THIS_CU->cu may already be in use. So we can't just free it and
20015 replace its DIEs with the ones we read in. Instead, we leave those
20016 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
20017 and clobber THIS_CU->cu->partial_dies with the hash table for the new
20018 set. */
976ca316 20019 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 20020
7188ed02 20021 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
20022 }
20023
20024 if (pd == NULL)
521894aa 20025 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
9d8780f0 20026 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 20027 return { cu, pd };
72bf9492
DJ
20028}
20029
abc72ce4
DE
20030/* See if we can figure out if the class lives in a namespace. We do
20031 this by looking for a member function; its demangled name will
20032 contain namespace info, if there is any. */
20033
20034static void
20035guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
20036 struct dwarf2_cu *cu)
20037{
20038 /* NOTE: carlton/2003-10-07: Getting the info this way changes
20039 what template types look like, because the demangler
20040 frequently doesn't give the same name as the debug info. We
20041 could fix this by only using the demangled name to get the
20042 prefix (but see comment in read_structure_type). */
20043
20044 struct partial_die_info *real_pdi;
20045 struct partial_die_info *child_pdi;
20046
20047 /* If this DIE (this DIE's specification, if any) has a parent, then
20048 we should not do this. We'll prepend the parent's fully qualified
20049 name when we create the partial symbol. */
20050
20051 real_pdi = struct_pdi;
20052 while (real_pdi->has_specification)
fb816e8b 20053 {
122cf0f2
AB
20054 auto res = find_partial_die (real_pdi->spec_offset,
20055 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
20056 real_pdi = res.pdi;
20057 cu = res.cu;
20058 }
abc72ce4
DE
20059
20060 if (real_pdi->die_parent != NULL)
20061 return;
20062
20063 for (child_pdi = struct_pdi->die_child;
20064 child_pdi != NULL;
20065 child_pdi = child_pdi->die_sibling)
20066 {
20067 if (child_pdi->tag == DW_TAG_subprogram
20068 && child_pdi->linkage_name != NULL)
20069 {
43816ebc 20070 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
20071 (cu->language_defn->class_name_from_physname
20072 (child_pdi->linkage_name));
abc72ce4
DE
20073 if (actual_class_name != NULL)
20074 {
5e22e966 20075 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
20076 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
20077 struct_pdi->canonical_name = 1;
abc72ce4
DE
20078 }
20079 break;
20080 }
20081 }
20082}
20083
25c11aca
TV
20084/* Return true if a DIE with TAG may have the DW_AT_const_value
20085 attribute. */
20086
20087static bool
20088can_have_DW_AT_const_value_p (enum dwarf_tag tag)
20089{
20090 switch (tag)
20091 {
20092 case DW_TAG_constant:
20093 case DW_TAG_enumerator:
20094 case DW_TAG_formal_parameter:
20095 case DW_TAG_template_value_param:
20096 case DW_TAG_variable:
20097 return true;
20098 }
20099
20100 return false;
20101}
20102
52356b79
YQ
20103void
20104partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 20105{
abc72ce4
DE
20106 /* Once we've fixed up a die, there's no point in doing so again.
20107 This also avoids a memory leak if we were to call
20108 guess_partial_die_structure_name multiple times. */
52356b79 20109 if (fixup_called)
abc72ce4
DE
20110 return;
20111
72bf9492
DJ
20112 /* If we found a reference attribute and the DIE has no name, try
20113 to find a name in the referred to DIE. */
20114
7d00ffec 20115 if (raw_name == NULL && has_specification)
72bf9492
DJ
20116 {
20117 struct partial_die_info *spec_die;
72bf9492 20118
122cf0f2 20119 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
20120 spec_die = res.pdi;
20121 cu = res.cu;
72bf9492 20122
52356b79 20123 spec_die->fixup (cu);
72bf9492 20124
7d00ffec 20125 if (spec_die->raw_name)
72bf9492 20126 {
7d00ffec
TT
20127 raw_name = spec_die->raw_name;
20128 canonical_name = spec_die->canonical_name;
72bf9492
DJ
20129
20130 /* Copy DW_AT_external attribute if it is set. */
20131 if (spec_die->is_external)
52356b79 20132 is_external = spec_die->is_external;
72bf9492
DJ
20133 }
20134 }
20135
25c11aca
TV
20136 if (!has_const_value && has_specification
20137 && can_have_DW_AT_const_value_p (tag))
20138 {
20139 struct partial_die_info *spec_die;
20140
20141 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20142 spec_die = res.pdi;
20143 cu = res.cu;
20144
20145 spec_die->fixup (cu);
20146
20147 if (spec_die->has_const_value)
20148 {
20149 /* Copy DW_AT_const_value attribute if it is set. */
20150 has_const_value = spec_die->has_const_value;
20151 }
20152 }
20153
72bf9492 20154 /* Set default names for some unnamed DIEs. */
72bf9492 20155
7d00ffec
TT
20156 if (raw_name == NULL && tag == DW_TAG_namespace)
20157 {
20158 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20159 canonical_name = 1;
20160 }
72bf9492 20161
abc72ce4
DE
20162 /* If there is no parent die to provide a namespace, and there are
20163 children, see if we can determine the namespace from their linkage
122d1940 20164 name. */
abc72ce4 20165 if (cu->language == language_cplus
5e22e966 20166 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
20167 && die_parent == NULL
20168 && has_children
20169 && (tag == DW_TAG_class_type
20170 || tag == DW_TAG_structure_type
20171 || tag == DW_TAG_union_type))
20172 guess_partial_die_structure_name (this, cu);
abc72ce4 20173
53832f31
TT
20174 /* GCC might emit a nameless struct or union that has a linkage
20175 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 20176 if (raw_name == NULL
52356b79
YQ
20177 && (tag == DW_TAG_class_type
20178 || tag == DW_TAG_interface_type
20179 || tag == DW_TAG_structure_type
20180 || tag == DW_TAG_union_type)
20181 && linkage_name != NULL)
53832f31 20182 {
43816ebc
TT
20183 gdb::unique_xmalloc_ptr<char> demangled
20184 (gdb_demangle (linkage_name, DMGL_TYPES));
20185 if (demangled != nullptr)
53832f31 20186 {
96408a79
SA
20187 const char *base;
20188
20189 /* Strip any leading namespaces/classes, keep only the base name.
20190 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
20191 base = strrchr (demangled.get (), ':');
20192 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
20193 base++;
20194 else
43816ebc 20195 base = demangled.get ();
96408a79 20196
5e22e966 20197 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
20198 raw_name = objfile->intern (base);
20199 canonical_name = 1;
53832f31
TT
20200 }
20201 }
20202
52356b79 20203 fixup_called = 1;
72bf9492
DJ
20204}
20205
d0ce17d8 20206/* Read the .debug_loclists or .debug_rnglists header (they are the same format)
a1c40103
SM
20207 contents from the given SECTION in the HEADER.
20208
20209 HEADER_OFFSET is the offset of the header in the section. */
41144253 20210static void
d0ce17d8 20211read_loclists_rnglists_header (struct loclists_rnglists_header *header,
a1c40103
SM
20212 struct dwarf2_section_info *section,
20213 sect_offset header_offset)
41144253 20214{
20215 unsigned int bytes_read;
20216 bfd *abfd = section->get_bfd_owner ();
a1c40103
SM
20217 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20218
41144253 20219 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20220 info_ptr += bytes_read;
a1c40103 20221
41144253 20222 header->version = read_2_bytes (abfd, info_ptr);
20223 info_ptr += 2;
a1c40103 20224
41144253 20225 header->addr_size = read_1_byte (abfd, info_ptr);
20226 info_ptr += 1;
a1c40103 20227
41144253 20228 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20229 info_ptr += 1;
a1c40103 20230
41144253 20231 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20232}
20233
20234/* Return the DW_AT_loclists_base value for the CU. */
20235static ULONGEST
20236lookup_loclist_base (struct dwarf2_cu *cu)
20237{
20238 /* For the .dwo unit, the loclist_base points to the first offset following
20239 the header. The header consists of the following entities-
20240 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20241 bit format)
20242 2. version (2 bytes)
20243 3. address size (1 byte)
20244 4. segment selector size (1 byte)
20245 5. offset entry count (4 bytes)
20246 These sizes are derived as per the DWARFv5 standard. */
20247 if (cu->dwo_unit != nullptr)
20248 {
20249 if (cu->header.initial_length_size == 4)
20250 return LOCLIST_HEADER_SIZE32;
20251 return LOCLIST_HEADER_SIZE64;
20252 }
20253 return cu->loclist_base;
20254}
20255
20256/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20257 array of offsets in the .debug_loclists section. */
e57933dc
SM
20258
20259static sect_offset
41144253 20260read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20261{
976ca316
SM
20262 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20263 struct objfile *objfile = per_objfile->objfile;
41144253 20264 bfd *abfd = objfile->obfd;
a1c40103
SM
20265 ULONGEST loclist_header_size =
20266 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20267 : LOCLIST_HEADER_SIZE64);
41144253 20268 ULONGEST loclist_base = lookup_loclist_base (cu);
05787bad
SM
20269
20270 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20271 ULONGEST start_offset =
20272 loclist_base + loclist_index * cu->header.offset_size;
20273
a1c40103 20274 /* Get loclists section. */
41144253 20275 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20276
a1c40103 20277 /* Read the loclists section content. */
41144253 20278 section->read (objfile);
20279 if (section->buffer == NULL)
a0c1eeba
SM
20280 error (_("DW_FORM_loclistx used without .debug_loclists "
20281 "section [in module %s]"), objfile_name (objfile));
20282
a1c40103
SM
20283 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20284 so if loclist_base is smaller than the header size, we have a problem. */
20285 if (loclist_base < loclist_header_size)
20286 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20287 objfile_name (objfile));
20288
20289 /* Read the header of the loclists contribution. */
d0ce17d8 20290 struct loclists_rnglists_header header;
a1c40103
SM
20291 read_loclists_rnglists_header (&header, section,
20292 (sect_offset) (loclist_base - loclist_header_size));
20293
20294 /* Verify the loclist index is valid. */
41144253 20295 if (loclist_index >= header.offset_entry_count)
a0c1eeba
SM
20296 error (_("DW_FORM_loclistx pointing outside of "
20297 ".debug_loclists offset array [in module %s]"),
20298 objfile_name (objfile));
20299
05787bad
SM
20300 /* Validate that reading won't go beyond the end of the section. */
20301 if (start_offset + cu->header.offset_size > section->size)
20302 error (_("Reading DW_FORM_loclistx index beyond end of"
20303 ".debug_loclists section [in module %s]"),
20304 objfile_name (objfile));
20305
20306 const gdb_byte *info_ptr = section->buffer + start_offset;
41144253 20307
20308 if (cu->header.offset_size == 4)
e57933dc 20309 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
41144253 20310 else
e57933dc 20311 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
41144253 20312}
20313
d0ce17d8
CT
20314/* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20315 array of offsets in the .debug_rnglists section. */
e57933dc
SM
20316
20317static sect_offset
d0ce17d8
CT
20318read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20319 dwarf_tag tag)
20320{
20321 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20322 struct objfile *objfile = dwarf2_per_objfile->objfile;
20323 bfd *abfd = objfile->obfd;
20324 ULONGEST rnglist_header_size =
20325 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20326 : RNGLIST_HEADER_SIZE64);
2b0c7f41
SM
20327
20328 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20329 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20330 doesn't apply. */
d0ce17d8 20331 ULONGEST rnglist_base =
2b0c7f41 20332 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
5e4d9bbc
SM
20333
20334 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
d0ce17d8
CT
20335 ULONGEST start_offset =
20336 rnglist_base + rnglist_index * cu->header.offset_size;
20337
20338 /* Get rnglists section. */
20339 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20340
20341 /* Read the rnglists section content. */
20342 section->read (objfile);
20343 if (section->buffer == nullptr)
20344 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20345 "[in module %s]"),
20346 objfile_name (objfile));
20347
a1c40103
SM
20348 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20349 so if rnglist_base is smaller than the header size, we have a problem. */
20350 if (rnglist_base < rnglist_header_size)
20351 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20352 objfile_name (objfile));
20353
20354 /* Read the header of the rnglists contribution. */
d0ce17d8 20355 struct loclists_rnglists_header header;
a1c40103
SM
20356 read_loclists_rnglists_header (&header, section,
20357 (sect_offset) (rnglist_base - rnglist_header_size));
20358
20359 /* Verify the rnglist index is valid. */
d0ce17d8
CT
20360 if (rnglist_index >= header.offset_entry_count)
20361 error (_("DW_FORM_rnglistx index pointing outside of "
20362 ".debug_rnglists offset array [in module %s]"),
20363 objfile_name (objfile));
20364
d0ce17d8 20365 /* Validate that reading won't go beyond the end of the section. */
5e4d9bbc 20366 if (start_offset + cu->header.offset_size > section->size)
d0ce17d8
CT
20367 error (_("Reading DW_FORM_rnglistx index beyond end of"
20368 ".debug_rnglists section [in module %s]"),
20369 objfile_name (objfile));
20370
20371 const gdb_byte *info_ptr = section->buffer + start_offset;
20372
20373 if (cu->header.offset_size == 4)
e57933dc 20374 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8 20375 else
e57933dc 20376 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8
CT
20377}
20378
18a8505e
AT
20379/* Process the attributes that had to be skipped in the first round. These
20380 attributes are the ones that need str_offsets_base or addr_base attributes.
20381 They could not have been processed in the first round, because at the time
20382 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
20383static void
20384read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 20385 struct attribute *attr, dwarf_tag tag)
18a8505e
AT
20386{
20387 struct dwarf2_cu *cu = reader->cu;
20388 switch (attr->form)
20389 {
20390 case DW_FORM_addrx:
20391 case DW_FORM_GNU_addr_index:
36d378cf
TT
20392 attr->set_address (read_addr_index (cu,
20393 attr->as_unsigned_reprocess ()));
dda83cd7 20394 break;
41144253 20395 case DW_FORM_loclistx:
b1829e1b 20396 {
e57933dc 20397 sect_offset loclists_sect_off
b1829e1b
SM
20398 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20399
e57933dc 20400 attr->set_unsigned (to_underlying (loclists_sect_off));
b1829e1b
SM
20401 }
20402 break;
d0ce17d8 20403 case DW_FORM_rnglistx:
b1829e1b 20404 {
e57933dc 20405 sect_offset rnglists_sect_off
b1829e1b
SM
20406 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20407
e57933dc 20408 attr->set_unsigned (to_underlying (rnglists_sect_off));
b1829e1b 20409 }
dda83cd7 20410 break;
18a8505e
AT
20411 case DW_FORM_strx:
20412 case DW_FORM_strx1:
20413 case DW_FORM_strx2:
20414 case DW_FORM_strx3:
20415 case DW_FORM_strx4:
20416 case DW_FORM_GNU_str_index:
20417 {
fe56917a 20418 unsigned int str_index = attr->as_unsigned_reprocess ();
c6481205 20419 gdb_assert (!attr->canonical_string_p ());
18a8505e 20420 if (reader->dwo_file != NULL)
c6481205
TT
20421 attr->set_string_noncanonical (read_dwo_str_index (reader,
20422 str_index));
18a8505e 20423 else
c6481205
TT
20424 attr->set_string_noncanonical (read_stub_str_index (cu,
20425 str_index));
18a8505e
AT
20426 break;
20427 }
20428 default:
20429 gdb_assert_not_reached (_("Unexpected DWARF form."));
20430 }
20431}
20432
a8329558 20433/* Read an attribute value described by an attribute form. */
c906108c 20434
d521ce57 20435static const gdb_byte *
dee91e82
DE
20436read_attribute_value (const struct die_reader_specs *reader,
20437 struct attribute *attr, unsigned form,
7a5f294d 20438 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 20439{
dee91e82 20440 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
20441 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20442 struct objfile *objfile = per_objfile->objfile;
dee91e82 20443 bfd *abfd = reader->abfd;
e7c27a73 20444 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
20445 unsigned int bytes_read;
20446 struct dwarf_block *blk;
20447
aead7601 20448 attr->form = (enum dwarf_form) form;
a8329558 20449 switch (form)
c906108c 20450 {
c906108c 20451 case DW_FORM_ref_addr:
7c290a04
TT
20452 if (cu_header->version == 2)
20453 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
414ad644 20454 &bytes_read));
ae411497 20455 else
7c290a04 20456 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20457 &bytes_read));
ae411497
TT
20458 info_ptr += bytes_read;
20459 break;
36586728 20460 case DW_FORM_GNU_ref_alt:
7c290a04 20461 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20462 &bytes_read));
36586728
TT
20463 info_ptr += bytes_read;
20464 break;
ae411497 20465 case DW_FORM_addr:
08feed99
TT
20466 {
20467 struct gdbarch *gdbarch = objfile->arch ();
7c290a04 20468 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
36d378cf
TT
20469 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20470 attr->set_address (addr);
08feed99
TT
20471 info_ptr += bytes_read;
20472 }
c906108c
SS
20473 break;
20474 case DW_FORM_block2:
7b5a2f43 20475 blk = dwarf_alloc_block (cu);
c906108c
SS
20476 blk->size = read_2_bytes (abfd, info_ptr);
20477 info_ptr += 2;
20478 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20479 info_ptr += blk->size;
9d2246fc 20480 attr->set_block (blk);
c906108c
SS
20481 break;
20482 case DW_FORM_block4:
7b5a2f43 20483 blk = dwarf_alloc_block (cu);
c906108c
SS
20484 blk->size = read_4_bytes (abfd, info_ptr);
20485 info_ptr += 4;
20486 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20487 info_ptr += blk->size;
9d2246fc 20488 attr->set_block (blk);
c906108c
SS
20489 break;
20490 case DW_FORM_data2:
414ad644 20491 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
c906108c
SS
20492 info_ptr += 2;
20493 break;
20494 case DW_FORM_data4:
414ad644 20495 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
c906108c
SS
20496 info_ptr += 4;
20497 break;
20498 case DW_FORM_data8:
414ad644 20499 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
c906108c
SS
20500 info_ptr += 8;
20501 break;
0224619f
JK
20502 case DW_FORM_data16:
20503 blk = dwarf_alloc_block (cu);
20504 blk->size = 16;
20505 blk->data = read_n_bytes (abfd, info_ptr, 16);
20506 info_ptr += 16;
9d2246fc 20507 attr->set_block (blk);
0224619f 20508 break;
2dc7f7b3 20509 case DW_FORM_sec_offset:
7c290a04 20510 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20511 &bytes_read));
2dc7f7b3
TT
20512 info_ptr += bytes_read;
20513 break;
41144253 20514 case DW_FORM_loclistx:
20515 {
7a5f294d
TT
20516 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20517 &bytes_read));
20518 info_ptr += bytes_read;
41144253 20519 }
20520 break;
c906108c 20521 case DW_FORM_string:
c6481205
TT
20522 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20523 &bytes_read));
c906108c
SS
20524 info_ptr += bytes_read;
20525 break;
4bdf3d34 20526 case DW_FORM_strp:
36586728
TT
20527 if (!cu->per_cu->is_dwz)
20528 {
c6481205
TT
20529 attr->set_string_noncanonical
20530 (read_indirect_string (per_objfile,
20531 abfd, info_ptr, cu_header,
20532 &bytes_read));
36586728
TT
20533 info_ptr += bytes_read;
20534 break;
20535 }
20536 /* FALLTHROUGH */
43988095
JK
20537 case DW_FORM_line_strp:
20538 if (!cu->per_cu->is_dwz)
20539 {
c6481205
TT
20540 attr->set_string_noncanonical
20541 (per_objfile->read_line_string (info_ptr, cu_header,
20542 &bytes_read));
43988095
JK
20543 info_ptr += bytes_read;
20544 break;
20545 }
20546 /* FALLTHROUGH */
36586728
TT
20547 case DW_FORM_GNU_strp_alt:
20548 {
a7308ce0 20549 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
8266302d
TT
20550 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20551 &bytes_read);
36586728 20552
c6481205
TT
20553 attr->set_string_noncanonical
20554 (dwz->read_string (objfile, str_offset));
36586728
TT
20555 info_ptr += bytes_read;
20556 }
4bdf3d34 20557 break;
2dc7f7b3 20558 case DW_FORM_exprloc:
c906108c 20559 case DW_FORM_block:
7b5a2f43 20560 blk = dwarf_alloc_block (cu);
c906108c
SS
20561 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20562 info_ptr += bytes_read;
20563 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20564 info_ptr += blk->size;
9d2246fc 20565 attr->set_block (blk);
c906108c
SS
20566 break;
20567 case DW_FORM_block1:
7b5a2f43 20568 blk = dwarf_alloc_block (cu);
c906108c
SS
20569 blk->size = read_1_byte (abfd, info_ptr);
20570 info_ptr += 1;
20571 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20572 info_ptr += blk->size;
9d2246fc 20573 attr->set_block (blk);
c906108c
SS
20574 break;
20575 case DW_FORM_data1:
c906108c 20576 case DW_FORM_flag:
414ad644 20577 attr->set_unsigned (read_1_byte (abfd, info_ptr));
c906108c
SS
20578 info_ptr += 1;
20579 break;
2dc7f7b3 20580 case DW_FORM_flag_present:
414ad644 20581 attr->set_unsigned (1);
2dc7f7b3 20582 break;
c906108c 20583 case DW_FORM_sdata:
1bc397c5 20584 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20585 info_ptr += bytes_read;
20586 break;
18a8505e 20587 case DW_FORM_rnglistx:
7a5f294d
TT
20588 {
20589 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20590 &bytes_read));
20591 info_ptr += bytes_read;
20592 }
20593 break;
d0ce17d8 20594 case DW_FORM_udata:
414ad644 20595 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20596 info_ptr += bytes_read;
20597 break;
20598 case DW_FORM_ref1:
7c290a04 20599 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20600 + read_1_byte (abfd, info_ptr)));
c906108c
SS
20601 info_ptr += 1;
20602 break;
20603 case DW_FORM_ref2:
7c290a04 20604 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20605 + read_2_bytes (abfd, info_ptr)));
c906108c
SS
20606 info_ptr += 2;
20607 break;
20608 case DW_FORM_ref4:
7c290a04 20609 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20610 + read_4_bytes (abfd, info_ptr)));
c906108c
SS
20611 info_ptr += 4;
20612 break;
613e1657 20613 case DW_FORM_ref8:
7c290a04 20614 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20615 + read_8_bytes (abfd, info_ptr)));
613e1657
KB
20616 info_ptr += 8;
20617 break;
55f1336d 20618 case DW_FORM_ref_sig8:
630ed6b9 20619 attr->set_signature (read_8_bytes (abfd, info_ptr));
348e048f
DE
20620 info_ptr += 8;
20621 break;
c906108c 20622 case DW_FORM_ref_udata:
7c290a04 20623 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644
TT
20624 + read_unsigned_leb128 (abfd, info_ptr,
20625 &bytes_read)));
c906108c
SS
20626 info_ptr += bytes_read;
20627 break;
c906108c 20628 case DW_FORM_indirect:
a8329558
KW
20629 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20630 info_ptr += bytes_read;
43988095
JK
20631 if (form == DW_FORM_implicit_const)
20632 {
20633 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20634 info_ptr += bytes_read;
20635 }
20636 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
7a5f294d 20637 info_ptr);
43988095
JK
20638 break;
20639 case DW_FORM_implicit_const:
1bc397c5 20640 attr->set_signed (implicit_const);
a8329558 20641 break;
336d760d 20642 case DW_FORM_addrx:
3019eac3 20643 case DW_FORM_GNU_addr_index:
fe56917a
TT
20644 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20645 &bytes_read));
3019eac3
DE
20646 info_ptr += bytes_read;
20647 break;
cf532bd1 20648 case DW_FORM_strx:
15f18d14
AT
20649 case DW_FORM_strx1:
20650 case DW_FORM_strx2:
20651 case DW_FORM_strx3:
20652 case DW_FORM_strx4:
3019eac3 20653 case DW_FORM_GNU_str_index:
3019eac3 20654 {
15f18d14
AT
20655 ULONGEST str_index;
20656 if (form == DW_FORM_strx1)
20657 {
20658 str_index = read_1_byte (abfd, info_ptr);
20659 info_ptr += 1;
20660 }
20661 else if (form == DW_FORM_strx2)
20662 {
20663 str_index = read_2_bytes (abfd, info_ptr);
20664 info_ptr += 2;
20665 }
20666 else if (form == DW_FORM_strx3)
20667 {
20668 str_index = read_3_bytes (abfd, info_ptr);
20669 info_ptr += 3;
20670 }
20671 else if (form == DW_FORM_strx4)
20672 {
20673 str_index = read_4_bytes (abfd, info_ptr);
20674 info_ptr += 4;
20675 }
20676 else
20677 {
20678 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20679 info_ptr += bytes_read;
20680 }
fe56917a 20681 attr->set_unsigned_reprocess (str_index);
7a5f294d 20682 }
3019eac3 20683 break;
c906108c 20684 default:
8a3fe4f8 20685 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
20686 dwarf_form_name (form),
20687 bfd_get_filename (abfd));
c906108c 20688 }
28e94949 20689
36586728 20690 /* Super hack. */
cd6c91b4 20691 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
20692 attr->form = DW_FORM_GNU_ref_alt;
20693
28e94949
JB
20694 /* We have seen instances where the compiler tried to emit a byte
20695 size attribute of -1 which ended up being encoded as an unsigned
20696 0xffffffff. Although 0xffffffff is technically a valid size value,
20697 an object of this size seems pretty unlikely so we can relatively
20698 safely treat these cases as if the size attribute was invalid and
20699 treat them as zero by default. */
20700 if (attr->name == DW_AT_byte_size
20701 && form == DW_FORM_data4
529908cb 20702 && attr->as_unsigned () >= 0xffffffff)
01c66ae6
JB
20703 {
20704 complaint
dda83cd7
SM
20705 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20706 hex_string (attr->as_unsigned ()));
414ad644 20707 attr->set_unsigned (0);
01c66ae6 20708 }
28e94949 20709
c906108c
SS
20710 return info_ptr;
20711}
20712
a8329558
KW
20713/* Read an attribute described by an abbreviated attribute. */
20714
d521ce57 20715static const gdb_byte *
dee91e82 20716read_attribute (const struct die_reader_specs *reader,
4444f407 20717 struct attribute *attr, const struct attr_abbrev *abbrev,
7a5f294d 20718 const gdb_byte *info_ptr)
a8329558
KW
20719{
20720 attr->name = abbrev->name;
c6481205 20721 attr->string_is_canonical = 0;
fe56917a 20722 attr->requires_reprocessing = 0;
43988095 20723 return read_attribute_value (reader, attr, abbrev->form,
7a5f294d 20724 abbrev->implicit_const, info_ptr);
a8329558
KW
20725}
20726
43988095
JK
20727/* Return pointer to string at .debug_str offset STR_OFFSET. */
20728
20729static const char *
976ca316 20730read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 20731 LONGEST str_offset)
43988095 20732{
976ca316
SM
20733 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20734 str_offset, "DW_FORM_strp");
c906108c
SS
20735}
20736
43988095
JK
20737/* Return pointer to string at .debug_str offset as read from BUF.
20738 BUF is assumed to be in a compilation unit described by CU_HEADER.
20739 Return *BYTES_READ_PTR count of bytes read from BUF. */
20740
d521ce57 20741static const char *
976ca316 20742read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 20743 const gdb_byte *buf,
cf2c3c16
TT
20744 const struct comp_unit_head *cu_header,
20745 unsigned int *bytes_read_ptr)
20746{
8266302d 20747 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 20748
976ca316 20749 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
20750}
20751
86c0bb4c 20752/* See read.h. */
43988095 20753
86c0bb4c
TT
20754const char *
20755dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
20756 const struct comp_unit_head *cu_header,
20757 unsigned int *bytes_read_ptr)
43988095 20758{
86c0bb4c 20759 bfd *abfd = objfile->obfd;
8266302d 20760 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 20761
5989a64e 20762 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
20763}
20764
3019eac3 20765/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20766 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20767 ADDR_SIZE is the size of addresses from the CU header. */
20768
20769static CORE_ADDR
976ca316
SM
20770read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20771 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 20772{
976ca316 20773 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
20774 bfd *abfd = objfile->obfd;
20775 const gdb_byte *info_ptr;
18a8505e 20776 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 20777
976ca316
SM
20778 per_objfile->per_bfd->addr.read (objfile);
20779 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 20780 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20781 objfile_name (objfile));
18a8505e 20782 if (addr_base_or_zero + addr_index * addr_size
976ca316 20783 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
20784 error (_("DW_FORM_addr_index pointing outside of "
20785 ".debug_addr section [in module %s]"),
4262abfb 20786 objfile_name (objfile));
976ca316
SM
20787 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20788 + addr_index * addr_size);
3019eac3
DE
20789 if (addr_size == 4)
20790 return bfd_get_32 (abfd, info_ptr);
20791 else
20792 return bfd_get_64 (abfd, info_ptr);
20793}
20794
20795/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20796
20797static CORE_ADDR
20798read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20799{
5e22e966 20800 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 20801 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20802}
20803
20804/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20805
20806static CORE_ADDR
d521ce57 20807read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20808 unsigned int *bytes_read)
20809{
5e22e966 20810 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
20811 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20812
20813 return read_addr_index (cu, addr_index);
20814}
20815
450a1bfc 20816/* See read.h. */
3019eac3
DE
20817
20818CORE_ADDR
82ca3f51 20819dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 20820 dwarf2_per_objfile *per_objfile,
82ca3f51 20821 unsigned int addr_index)
3019eac3 20822{
976ca316 20823 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 20824 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20825 int addr_size;
20826
3019eac3
DE
20827 /* We need addr_base and addr_size.
20828 If we don't have PER_CU->cu, we have to get it.
20829 Nasty, but the alternative is storing the needed info in PER_CU,
20830 which at this point doesn't seem justified: it's not clear how frequently
20831 it would get used and it would increase the size of every PER_CU.
20832 Entry points like dwarf2_per_cu_addr_size do a similar thing
20833 so we're not in uncharted territory here.
20834 Alas we need to be a bit more complicated as addr_base is contained
20835 in the DIE.
20836
20837 We don't need to read the entire CU(/TU).
20838 We just need the header and top level die.
a1b64ce1 20839
3019eac3 20840 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20841 For now we skip this optimization. */
3019eac3
DE
20842
20843 if (cu != NULL)
20844 {
20845 addr_base = cu->addr_base;
20846 addr_size = cu->header.addr_size;
20847 }
20848 else
20849 {
976ca316 20850 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
20851 addr_base = reader.cu->addr_base;
20852 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20853 }
20854
976ca316 20855 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
20856}
20857
18a8505e
AT
20858/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20859 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20860 DWO file. */
3019eac3 20861
d521ce57 20862static const char *
18a8505e
AT
20863read_str_index (struct dwarf2_cu *cu,
20864 struct dwarf2_section_info *str_section,
20865 struct dwarf2_section_info *str_offsets_section,
20866 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20867{
976ca316
SM
20868 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20869 struct objfile *objfile = per_objfile->objfile;
c5164cbc 20870 const char *objf_name = objfile_name (objfile);
3019eac3 20871 bfd *abfd = objfile->obfd;
d521ce57 20872 const gdb_byte *info_ptr;
3019eac3 20873 ULONGEST str_offset;
cf532bd1 20874 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20875
96b79293
TT
20876 str_section->read (objfile);
20877 str_offsets_section->read (objfile);
73869dc2 20878 if (str_section->buffer == NULL)
18a8505e 20879 error (_("%s used without %s section"
9d8780f0 20880 " in CU at offset %s [in module %s]"),
96b79293 20881 form_name, str_section->get_name (),
dda83cd7 20882 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20883 if (str_offsets_section->buffer == NULL)
18a8505e 20884 error (_("%s used without %s section"
9d8780f0 20885 " in CU at offset %s [in module %s]"),
96b79293 20886 form_name, str_section->get_name (),
dda83cd7 20887 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20888 info_ptr = (str_offsets_section->buffer
18a8505e 20889 + str_offsets_base
3019eac3
DE
20890 + str_index * cu->header.offset_size);
20891 if (cu->header.offset_size == 4)
20892 str_offset = bfd_get_32 (abfd, info_ptr);
20893 else
20894 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20895 if (str_offset >= str_section->size)
57d63ce2 20896 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20897 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20898 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20899 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20900}
20901
18a8505e
AT
20902/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20903
20904static const char *
20905read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20906{
20907 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20908 ? reader->cu->header.addr_size : 0;
20909 return read_str_index (reader->cu,
20910 &reader->dwo_file->sections.str,
20911 &reader->dwo_file->sections.str_offsets,
20912 str_offsets_base, str_index);
20913}
20914
20915/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20916
20917static const char *
20918read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20919{
5e22e966 20920 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
20921 const char *objf_name = objfile_name (objfile);
20922 static const char form_name[] = "DW_FORM_GNU_str_index";
20923 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20924
20925 if (!cu->str_offsets_base.has_value ())
20926 error (_("%s used in Fission stub without %s"
20927 " in CU at offset 0x%lx [in module %s]"),
20928 form_name, str_offsets_attr_name,
20929 (long) cu->header.offset_size, objf_name);
20930
20931 return read_str_index (cu,
5e22e966
SM
20932 &cu->per_objfile->per_bfd->str,
20933 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
20934 *cu->str_offsets_base, str_index);
20935}
20936
3019eac3
DE
20937/* Return the length of an LEB128 number in BUF. */
20938
20939static int
20940leb128_size (const gdb_byte *buf)
20941{
20942 const gdb_byte *begin = buf;
20943 gdb_byte byte;
20944
20945 while (1)
20946 {
20947 byte = *buf++;
20948 if ((byte & 128) == 0)
20949 return buf - begin;
20950 }
20951}
20952
c906108c 20953static void
e142c38c 20954set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20955{
20956 switch (lang)
20957 {
20958 case DW_LANG_C89:
76bee0cc 20959 case DW_LANG_C99:
0cfd832f 20960 case DW_LANG_C11:
c906108c 20961 case DW_LANG_C:
d1be3247 20962 case DW_LANG_UPC:
e142c38c 20963 cu->language = language_c;
c906108c 20964 break;
9c37b5ae 20965 case DW_LANG_Java:
c906108c 20966 case DW_LANG_C_plus_plus:
0cfd832f
MW
20967 case DW_LANG_C_plus_plus_11:
20968 case DW_LANG_C_plus_plus_14:
e142c38c 20969 cu->language = language_cplus;
c906108c 20970 break;
6aecb9c2
JB
20971 case DW_LANG_D:
20972 cu->language = language_d;
20973 break;
c906108c
SS
20974 case DW_LANG_Fortran77:
20975 case DW_LANG_Fortran90:
b21b22e0 20976 case DW_LANG_Fortran95:
f7de9aab
MW
20977 case DW_LANG_Fortran03:
20978 case DW_LANG_Fortran08:
e142c38c 20979 cu->language = language_fortran;
c906108c 20980 break;
a766d390
DE
20981 case DW_LANG_Go:
20982 cu->language = language_go;
20983 break;
c906108c 20984 case DW_LANG_Mips_Assembler:
e142c38c 20985 cu->language = language_asm;
c906108c
SS
20986 break;
20987 case DW_LANG_Ada83:
8aaf0b47 20988 case DW_LANG_Ada95:
bc5f45f8
JB
20989 cu->language = language_ada;
20990 break;
72019c9c
GM
20991 case DW_LANG_Modula2:
20992 cu->language = language_m2;
20993 break;
fe8e67fd
PM
20994 case DW_LANG_Pascal83:
20995 cu->language = language_pascal;
20996 break;
22566fbd
DJ
20997 case DW_LANG_ObjC:
20998 cu->language = language_objc;
20999 break;
c44af4eb
TT
21000 case DW_LANG_Rust:
21001 case DW_LANG_Rust_old:
21002 cu->language = language_rust;
21003 break;
c906108c
SS
21004 case DW_LANG_Cobol74:
21005 case DW_LANG_Cobol85:
c906108c 21006 default:
e142c38c 21007 cu->language = language_minimal;
c906108c
SS
21008 break;
21009 }
e142c38c 21010 cu->language_defn = language_def (cu->language);
c906108c
SS
21011}
21012
21013/* Return the named attribute or NULL if not there. */
21014
21015static struct attribute *
e142c38c 21016dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 21017{
a48e046c 21018 for (;;)
c906108c 21019 {
a48e046c
TT
21020 unsigned int i;
21021 struct attribute *spec = NULL;
21022
21023 for (i = 0; i < die->num_attrs; ++i)
21024 {
21025 if (die->attrs[i].name == name)
21026 return &die->attrs[i];
21027 if (die->attrs[i].name == DW_AT_specification
21028 || die->attrs[i].name == DW_AT_abstract_origin)
21029 spec = &die->attrs[i];
21030 }
21031
21032 if (!spec)
21033 break;
c906108c 21034
f2f0e013 21035 die = follow_die_ref (die, spec, &cu);
f2f0e013 21036 }
c5aa993b 21037
c906108c
SS
21038 return NULL;
21039}
21040
7d45c7c3
KB
21041/* Return the string associated with a string-typed attribute, or NULL if it
21042 is either not found or is of an incorrect type. */
21043
21044static const char *
21045dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21046{
21047 struct attribute *attr;
21048 const char *str = NULL;
21049
21050 attr = dwarf2_attr (die, name, cu);
21051
21052 if (attr != NULL)
21053 {
95f982e5 21054 str = attr->as_string ();
e61108c9 21055 if (str == nullptr)
dda83cd7 21056 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
21057 "DIE at %s in module %s"),
21058 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 21059 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
21060 }
21061
21062 return str;
21063}
21064
a084a2a6 21065/* Return the dwo name or NULL if not present. If present, it is in either
85102364 21066 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
21067static const char *
21068dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
21069{
21070 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
21071 if (dwo_name == nullptr)
21072 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
21073 return dwo_name;
21074}
21075
05cf31d1
JB
21076/* Return non-zero iff the attribute NAME is defined for the given DIE,
21077 and holds a non-zero value. This function should only be used for
2dc7f7b3 21078 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
21079
21080static int
21081dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
21082{
21083 struct attribute *attr = dwarf2_attr (die, name, cu);
21084
c45bc3f8 21085 return attr != nullptr && attr->as_boolean ();
05cf31d1
JB
21086}
21087
3ca72b44 21088static int
e142c38c 21089die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 21090{
05cf31d1
JB
21091 /* A DIE is a declaration if it has a DW_AT_declaration attribute
21092 which value is non-zero. However, we have to be careful with
21093 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
21094 (via dwarf2_flag_true_p) follows this attribute. So we may
21095 end up accidently finding a declaration attribute that belongs
21096 to a different DIE referenced by the specification attribute,
21097 even though the given DIE does not have a declaration attribute. */
21098 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
21099 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
21100}
21101
63d06c5c 21102/* Return the die giving the specification for DIE, if there is
f2f0e013 21103 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
21104 containing the return value on output. If there is no
21105 specification, but there is an abstract origin, that is
21106 returned. */
63d06c5c
DC
21107
21108static struct die_info *
f2f0e013 21109die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 21110{
f2f0e013
DJ
21111 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
21112 *spec_cu);
63d06c5c 21113
edb3359d
DJ
21114 if (spec_attr == NULL)
21115 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
21116
63d06c5c
DC
21117 if (spec_attr == NULL)
21118 return NULL;
21119 else
f2f0e013 21120 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 21121}
c906108c 21122
527f3840
JK
21123/* Stub for free_line_header to match void * callback types. */
21124
21125static void
21126free_line_header_voidp (void *arg)
21127{
9a3c8263 21128 struct line_header *lh = (struct line_header *) arg;
527f3840 21129
fff8551c 21130 delete lh;
527f3840
JK
21131}
21132
83769d0b 21133/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
21134
21135static struct dwarf2_section_info *
21136get_debug_line_section (struct dwarf2_cu *cu)
21137{
21138 struct dwarf2_section_info *section;
976ca316 21139 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
21140
21141 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
21142 DWO file. */
21143 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21144 section = &cu->dwo_unit->dwo_file->sections.line;
21145 else if (cu->per_cu->is_dwz)
21146 {
a7308ce0 21147 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
36586728
TT
21148
21149 section = &dwz->line;
21150 }
21151 else
976ca316 21152 section = &per_objfile->per_bfd->line;
36586728
TT
21153
21154 return section;
21155}
21156
debd256d 21157/* Read the statement program header starting at OFFSET in
3019eac3 21158 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 21159 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
21160 Returns NULL if there is a problem reading the header, e.g., if it
21161 has a version we don't understand.
debd256d
JB
21162
21163 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
21164 the returned object point into the dwarf line section buffer,
21165 and must not be freed. */
ae2de4f8 21166
fff8551c 21167static line_header_up
9c541725 21168dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 21169{
3019eac3 21170 struct dwarf2_section_info *section;
976ca316 21171 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 21172
36586728 21173 section = get_debug_line_section (cu);
976ca316 21174 section->read (per_objfile->objfile);
3019eac3 21175 if (section->buffer == NULL)
debd256d 21176 {
3019eac3 21177 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 21178 complaint (_("missing .debug_line.dwo section"));
3019eac3 21179 else
b98664d3 21180 complaint (_("missing .debug_line section"));
debd256d
JB
21181 return 0;
21182 }
21183
0df7ad3a 21184 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 21185 per_objfile, section, &cu->header);
debd256d 21186}
c906108c 21187
c6da4cef 21188/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 21189 Return the file name of the psymtab for the given file_entry.
c6da4cef 21190 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
21191 If space for the result is malloc'd, *NAME_HOLDER will be set.
21192 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 21193
d521ce57 21194static const char *
7ba99d21 21195psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 21196 const dwarf2_psymtab *pst,
c89b44cd
TT
21197 const char *comp_dir,
21198 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 21199{
d521ce57
TT
21200 const char *include_name = fe.name;
21201 const char *include_name_to_compare = include_name;
72b9f47f 21202 const char *pst_filename;
c6da4cef
DE
21203 int file_is_pst;
21204
8c43009f 21205 const char *dir_name = fe.include_dir (lh);
c6da4cef 21206
c89b44cd 21207 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
21208 if (!IS_ABSOLUTE_PATH (include_name)
21209 && (dir_name != NULL || comp_dir != NULL))
21210 {
21211 /* Avoid creating a duplicate psymtab for PST.
21212 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21213 Before we do the comparison, however, we need to account
21214 for DIR_NAME and COMP_DIR.
21215 First prepend dir_name (if non-NULL). If we still don't
21216 have an absolute path prepend comp_dir (if non-NULL).
21217 However, the directory we record in the include-file's
21218 psymtab does not contain COMP_DIR (to match the
21219 corresponding symtab(s)).
21220
21221 Example:
21222
21223 bash$ cd /tmp
21224 bash$ gcc -g ./hello.c
21225 include_name = "hello.c"
21226 dir_name = "."
21227 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
21228 DW_AT_name = "./hello.c"
21229
21230 */
c6da4cef
DE
21231
21232 if (dir_name != NULL)
21233 {
c89b44cd
TT
21234 name_holder->reset (concat (dir_name, SLASH_STRING,
21235 include_name, (char *) NULL));
21236 include_name = name_holder->get ();
c6da4cef 21237 include_name_to_compare = include_name;
c6da4cef
DE
21238 }
21239 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21240 {
c89b44cd
TT
21241 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21242 include_name, (char *) NULL));
21243 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
21244 }
21245 }
21246
21247 pst_filename = pst->filename;
c89b44cd 21248 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
21249 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21250 {
c89b44cd
TT
21251 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21252 pst_filename, (char *) NULL));
21253 pst_filename = copied_name.get ();
c6da4cef
DE
21254 }
21255
1e3fad37 21256 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 21257
c6da4cef
DE
21258 if (file_is_pst)
21259 return NULL;
21260 return include_name;
21261}
21262
d9b3de22
DE
21263/* State machine to track the state of the line number program. */
21264
6f77053d 21265class lnp_state_machine
d9b3de22 21266{
6f77053d
PA
21267public:
21268 /* Initialize a machine state for the start of a line number
21269 program. */
804d2729
TT
21270 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21271 bool record_lines_p);
6f77053d 21272
8c43009f
PA
21273 file_entry *current_file ()
21274 {
21275 /* lh->file_names is 0-based, but the file name numbers in the
21276 statement program are 1-based. */
6f77053d
PA
21277 return m_line_header->file_name_at (m_file);
21278 }
21279
21280 /* Record the line in the state machine. END_SEQUENCE is true if
21281 we're processing the end of a sequence. */
21282 void record_line (bool end_sequence);
21283
a8caed5d 21284 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
7ab6656f 21285 nop-out rest of the lines in this sequence. */
6f77053d
PA
21286 void check_line_address (struct dwarf2_cu *cu,
21287 const gdb_byte *line_ptr,
7ab6656f 21288 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
21289
21290 void handle_set_discriminator (unsigned int discriminator)
21291 {
21292 m_discriminator = discriminator;
21293 m_line_has_non_zero_discriminator |= discriminator != 0;
21294 }
21295
21296 /* Handle DW_LNE_set_address. */
21297 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21298 {
21299 m_op_index = 0;
21300 address += baseaddr;
21301 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21302 }
21303
21304 /* Handle DW_LNS_advance_pc. */
21305 void handle_advance_pc (CORE_ADDR adjust);
21306
21307 /* Handle a special opcode. */
21308 void handle_special_opcode (unsigned char op_code);
21309
21310 /* Handle DW_LNS_advance_line. */
21311 void handle_advance_line (int line_delta)
21312 {
21313 advance_line (line_delta);
21314 }
21315
21316 /* Handle DW_LNS_set_file. */
21317 void handle_set_file (file_name_index file);
21318
21319 /* Handle DW_LNS_negate_stmt. */
21320 void handle_negate_stmt ()
21321 {
21322 m_is_stmt = !m_is_stmt;
21323 }
21324
21325 /* Handle DW_LNS_const_add_pc. */
21326 void handle_const_add_pc ();
21327
21328 /* Handle DW_LNS_fixed_advance_pc. */
21329 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21330 {
21331 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21332 m_op_index = 0;
21333 }
21334
21335 /* Handle DW_LNS_copy. */
21336 void handle_copy ()
21337 {
21338 record_line (false);
21339 m_discriminator = 0;
21340 }
21341
21342 /* Handle DW_LNE_end_sequence. */
21343 void handle_end_sequence ()
21344 {
804d2729 21345 m_currently_recording_lines = true;
6f77053d
PA
21346 }
21347
21348private:
21349 /* Advance the line by LINE_DELTA. */
21350 void advance_line (int line_delta)
21351 {
21352 m_line += line_delta;
21353
21354 if (line_delta != 0)
21355 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21356 }
21357
804d2729
TT
21358 struct dwarf2_cu *m_cu;
21359
6f77053d
PA
21360 gdbarch *m_gdbarch;
21361
21362 /* True if we're recording lines.
21363 Otherwise we're building partial symtabs and are just interested in
21364 finding include files mentioned by the line number program. */
21365 bool m_record_lines_p;
21366
8c43009f 21367 /* The line number header. */
6f77053d 21368 line_header *m_line_header;
8c43009f 21369
6f77053d
PA
21370 /* These are part of the standard DWARF line number state machine,
21371 and initialized according to the DWARF spec. */
d9b3de22 21372
6f77053d 21373 unsigned char m_op_index = 0;
7ba99d21
AT
21374 /* The line table index of the current file. */
21375 file_name_index m_file = 1;
6f77053d
PA
21376 unsigned int m_line = 1;
21377
21378 /* These are initialized in the constructor. */
21379
21380 CORE_ADDR m_address;
21381 bool m_is_stmt;
21382 unsigned int m_discriminator;
d9b3de22
DE
21383
21384 /* Additional bits of state we need to track. */
21385
21386 /* The last file that we called dwarf2_start_subfile for.
21387 This is only used for TLLs. */
6f77053d 21388 unsigned int m_last_file = 0;
d9b3de22 21389 /* The last file a line number was recorded for. */
6f77053d 21390 struct subfile *m_last_subfile = NULL;
d9b3de22 21391
1313c56e
AB
21392 /* The address of the last line entry. */
21393 CORE_ADDR m_last_address;
21394
21395 /* Set to true when a previous line at the same address (using
21396 m_last_address) had m_is_stmt true. This is reset to false when a
21397 line entry at a new address (m_address different to m_last_address) is
21398 processed. */
21399 bool m_stmt_at_address = false;
21400
804d2729
TT
21401 /* When true, record the lines we decode. */
21402 bool m_currently_recording_lines = false;
d9b3de22
DE
21403
21404 /* The last line number that was recorded, used to coalesce
21405 consecutive entries for the same line. This can happen, for
21406 example, when discriminators are present. PR 17276. */
6f77053d
PA
21407 unsigned int m_last_line = 0;
21408 bool m_line_has_non_zero_discriminator = false;
8c43009f 21409};
d9b3de22 21410
6f77053d
PA
21411void
21412lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21413{
21414 CORE_ADDR addr_adj = (((m_op_index + adjust)
21415 / m_line_header->maximum_ops_per_instruction)
21416 * m_line_header->minimum_instruction_length);
21417 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21418 m_op_index = ((m_op_index + adjust)
21419 % m_line_header->maximum_ops_per_instruction);
21420}
d9b3de22 21421
6f77053d
PA
21422void
21423lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21424{
6f77053d 21425 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
21426 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21427 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21428 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
21429 / m_line_header->maximum_ops_per_instruction)
21430 * m_line_header->minimum_instruction_length);
21431 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 21432 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 21433 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21434
258bf0ee 21435 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
21436 advance_line (line_delta);
21437 record_line (false);
21438 m_discriminator = 0;
21439}
d9b3de22 21440
6f77053d
PA
21441void
21442lnp_state_machine::handle_set_file (file_name_index file)
21443{
21444 m_file = file;
21445
21446 const file_entry *fe = current_file ();
21447 if (fe == NULL)
21448 dwarf2_debug_line_missing_file_complaint ();
21449 else if (m_record_lines_p)
21450 {
21451 const char *dir = fe->include_dir (m_line_header);
21452
c24bdb02 21453 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21454 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21455 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21456 }
21457}
21458
21459void
21460lnp_state_machine::handle_const_add_pc ()
21461{
21462 CORE_ADDR adjust
21463 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21464
21465 CORE_ADDR addr_adj
21466 = (((m_op_index + adjust)
21467 / m_line_header->maximum_ops_per_instruction)
21468 * m_line_header->minimum_instruction_length);
21469
21470 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21471 m_op_index = ((m_op_index + adjust)
21472 % m_line_header->maximum_ops_per_instruction);
21473}
d9b3de22 21474
a05a36a5
DE
21475/* Return non-zero if we should add LINE to the line number table.
21476 LINE is the line to add, LAST_LINE is the last line that was added,
21477 LAST_SUBFILE is the subfile for LAST_LINE.
21478 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21479 had a non-zero discriminator.
21480
21481 We have to be careful in the presence of discriminators.
21482 E.g., for this line:
21483
21484 for (i = 0; i < 100000; i++);
21485
21486 clang can emit four line number entries for that one line,
21487 each with a different discriminator.
21488 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21489
21490 However, we want gdb to coalesce all four entries into one.
21491 Otherwise the user could stepi into the middle of the line and
21492 gdb would get confused about whether the pc really was in the
21493 middle of the line.
21494
21495 Things are further complicated by the fact that two consecutive
21496 line number entries for the same line is a heuristic used by gcc
21497 to denote the end of the prologue. So we can't just discard duplicate
21498 entries, we have to be selective about it. The heuristic we use is
21499 that we only collapse consecutive entries for the same line if at least
21500 one of those entries has a non-zero discriminator. PR 17276.
21501
21502 Note: Addresses in the line number state machine can never go backwards
21503 within one sequence, thus this coalescing is ok. */
21504
21505static int
804d2729
TT
21506dwarf_record_line_p (struct dwarf2_cu *cu,
21507 unsigned int line, unsigned int last_line,
a05a36a5
DE
21508 int line_has_non_zero_discriminator,
21509 struct subfile *last_subfile)
21510{
c24bdb02 21511 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21512 return 1;
21513 if (line != last_line)
21514 return 1;
21515 /* Same line for the same file that we've seen already.
21516 As a last check, for pr 17276, only record the line if the line
21517 has never had a non-zero discriminator. */
21518 if (!line_has_non_zero_discriminator)
21519 return 1;
21520 return 0;
21521}
21522
804d2729
TT
21523/* Use the CU's builder to record line number LINE beginning at
21524 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21525
21526static void
d9b3de22 21527dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 21528 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 21529 struct dwarf2_cu *cu)
252a6764
DE
21530{
21531 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21532
27e0867f
DE
21533 if (dwarf_line_debug)
21534 {
21535 fprintf_unfiltered (gdb_stdlog,
21536 "Recording line %u, file %s, address %s\n",
21537 line, lbasename (subfile->name),
21538 paddress (gdbarch, address));
21539 }
21540
804d2729 21541 if (cu != nullptr)
8c95582d 21542 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
21543}
21544
21545/* Subroutine of dwarf_decode_lines_1 to simplify it.
21546 Mark the end of a set of line number records.
d9b3de22 21547 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21548 If SUBFILE is NULL the request is ignored. */
21549
21550static void
21551dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21552 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21553{
27e0867f
DE
21554 if (subfile == NULL)
21555 return;
21556
21557 if (dwarf_line_debug)
21558 {
21559 fprintf_unfiltered (gdb_stdlog,
21560 "Finishing current line, file %s, address %s\n",
21561 lbasename (subfile->name),
21562 paddress (gdbarch, address));
21563 }
21564
8c95582d 21565 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
21566}
21567
6f77053d
PA
21568void
21569lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21570{
d9b3de22
DE
21571 if (dwarf_line_debug)
21572 {
21573 fprintf_unfiltered (gdb_stdlog,
21574 "Processing actual line %u: file %u,"
94a72be7 21575 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 21576 m_line, m_file,
6f77053d 21577 paddress (m_gdbarch, m_address),
94a72be7
AB
21578 m_is_stmt, m_discriminator,
21579 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
21580 }
21581
6f77053d 21582 file_entry *fe = current_file ();
8c43009f
PA
21583
21584 if (fe == NULL)
d9b3de22
DE
21585 dwarf2_debug_line_missing_file_complaint ();
21586 /* For now we ignore lines not starting on an instruction boundary.
21587 But not when processing end_sequence for compatibility with the
21588 previous version of the code. */
6f77053d 21589 else if (m_op_index == 0 || end_sequence)
d9b3de22 21590 {
8c43009f 21591 fe->included_p = 1;
8c95582d 21592 if (m_record_lines_p)
d9b3de22 21593 {
1313c56e
AB
21594 /* When we switch files we insert an end maker in the first file,
21595 switch to the second file and add a new line entry. The
21596 problem is that the end marker inserted in the first file will
21597 discard any previous line entries at the same address. If the
21598 line entries in the first file are marked as is-stmt, while
21599 the new line in the second file is non-stmt, then this means
21600 the end marker will discard is-stmt lines so we can have a
21601 non-stmt line. This means that there are less addresses at
21602 which the user can insert a breakpoint.
21603
21604 To improve this we track the last address in m_last_address,
21605 and whether we have seen an is-stmt at this address. Then
21606 when switching files, if we have seen a stmt at the current
21607 address, and we are switching to create a non-stmt line, then
21608 discard the new line. */
21609 bool file_changed
21610 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21611 bool ignore_this_line
dda83cd7
SM
21612 = ((file_changed && !end_sequence && m_last_address == m_address
21613 && !m_is_stmt && m_stmt_at_address)
21614 || (!end_sequence && m_line == 0));
1313c56e
AB
21615
21616 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 21617 {
804d2729
TT
21618 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21619 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21620 }
21621
1313c56e 21622 if (!end_sequence && !ignore_this_line)
d9b3de22 21623 {
8c95582d
AB
21624 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21625
804d2729 21626 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21627 m_line_has_non_zero_discriminator,
21628 m_last_subfile))
d9b3de22 21629 {
c24bdb02 21630 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21631 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21632 builder->get_current_subfile (),
8c95582d 21633 m_line, m_address, is_stmt,
804d2729 21634 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21635 }
c24bdb02 21636 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21637 m_last_line = m_line;
d9b3de22
DE
21638 }
21639 }
21640 }
1313c56e
AB
21641
21642 /* Track whether we have seen any m_is_stmt true at m_address in case we
21643 have multiple line table entries all at m_address. */
21644 if (m_last_address != m_address)
21645 {
21646 m_stmt_at_address = false;
21647 m_last_address = m_address;
21648 }
21649 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
21650}
21651
804d2729
TT
21652lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21653 line_header *lh, bool record_lines_p)
d9b3de22 21654{
804d2729 21655 m_cu = cu;
6f77053d
PA
21656 m_gdbarch = arch;
21657 m_record_lines_p = record_lines_p;
21658 m_line_header = lh;
d9b3de22 21659
804d2729 21660 m_currently_recording_lines = true;
d9b3de22 21661
d9b3de22
DE
21662 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21663 was a line entry for it so that the backend has a chance to adjust it
21664 and also record it in case it needs it. This is currently used by MIPS
21665 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21666 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21667 m_is_stmt = lh->default_is_stmt;
21668 m_discriminator = 0;
1313c56e
AB
21669
21670 m_last_address = m_address;
21671 m_stmt_at_address = false;
252a6764
DE
21672}
21673
6f77053d
PA
21674void
21675lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21676 const gdb_byte *line_ptr,
7ab6656f 21677 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21678{
a8caed5d
FS
21679 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21680 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21681 located at 0x0. In this case, additionally check that if
21682 ADDRESS < UNRELOCATED_LOWPC. */
924c2928 21683
a8caed5d
FS
21684 if ((address == 0 && address < unrelocated_lowpc)
21685 || address == (CORE_ADDR) -1)
924c2928
DE
21686 {
21687 /* This line table is for a function which has been
21688 GCd by the linker. Ignore it. PR gdb/12528 */
21689
5e22e966 21690 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
21691 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21692
b98664d3 21693 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21694 line_offset, objfile_name (objfile));
804d2729
TT
21695 m_currently_recording_lines = false;
21696 /* Note: m_currently_recording_lines is left as false until we see
21697 DW_LNE_end_sequence. */
924c2928
DE
21698 }
21699}
21700
f3f5162e 21701/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21702 Process the line number information in LH.
21703 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21704 program in order to set included_p for every referenced header. */
debd256d 21705
c906108c 21706static void
43f3e411
DE
21707dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21708 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21709{
d521ce57
TT
21710 const gdb_byte *line_ptr, *extended_end;
21711 const gdb_byte *line_end;
a8c50c1f 21712 unsigned int bytes_read, extended_len;
699ca60a 21713 unsigned char op_code, extended_op;
e142c38c 21714 CORE_ADDR baseaddr;
5e22e966 21715 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21716 bfd *abfd = objfile->obfd;
08feed99 21717 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
21718 /* True if we're recording line info (as opposed to building partial
21719 symtabs and just interested in finding include files mentioned by
21720 the line number program). */
21721 bool record_lines_p = !decode_for_pst_p;
e142c38c 21722
b3b3bada 21723 baseaddr = objfile->text_section_offset ();
c906108c 21724
debd256d
JB
21725 line_ptr = lh->statement_program_start;
21726 line_end = lh->statement_program_end;
c906108c
SS
21727
21728 /* Read the statement sequences until there's nothing left. */
21729 while (line_ptr < line_end)
21730 {
6f77053d
PA
21731 /* The DWARF line number program state machine. Reset the state
21732 machine at the start of each sequence. */
804d2729 21733 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21734 bool end_sequence = false;
d9b3de22 21735
8c43009f 21736 if (record_lines_p)
c906108c 21737 {
8c43009f
PA
21738 /* Start a subfile for the current file of the state
21739 machine. */
21740 const file_entry *fe = state_machine.current_file ();
21741
21742 if (fe != NULL)
804d2729 21743 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21744 }
21745
a738430d 21746 /* Decode the table. */
d9b3de22 21747 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21748 {
21749 op_code = read_1_byte (abfd, line_ptr);
21750 line_ptr += 1;
9aa1fe7e 21751
debd256d 21752 if (op_code >= lh->opcode_base)
6e70227d 21753 {
8e07a239 21754 /* Special opcode. */
6f77053d 21755 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21756 }
21757 else switch (op_code)
c906108c
SS
21758 {
21759 case DW_LNS_extended_op:
3e43a32a
MS
21760 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21761 &bytes_read);
473b7be6 21762 line_ptr += bytes_read;
a8c50c1f 21763 extended_end = line_ptr + extended_len;
c906108c
SS
21764 extended_op = read_1_byte (abfd, line_ptr);
21765 line_ptr += 1;
8f34b746
TV
21766 if (DW_LNE_lo_user <= extended_op
21767 && extended_op <= DW_LNE_hi_user)
21768 {
21769 /* Vendor extension, ignore. */
21770 line_ptr = extended_end;
21771 break;
21772 }
c906108c
SS
21773 switch (extended_op)
21774 {
21775 case DW_LNE_end_sequence:
6f77053d
PA
21776 state_machine.handle_end_sequence ();
21777 end_sequence = true;
c906108c
SS
21778 break;
21779 case DW_LNE_set_address:
d9b3de22
DE
21780 {
21781 CORE_ADDR address
c8a7a66f 21782 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 21783 line_ptr += bytes_read;
6f77053d
PA
21784
21785 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21786 lowpc - baseaddr, address);
6f77053d 21787 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21788 }
c906108c
SS
21789 break;
21790 case DW_LNE_define_file:
dda83cd7
SM
21791 {
21792 const char *cur_file;
ecfb656c
PA
21793 unsigned int mod_time, length;
21794 dir_index dindex;
6e70227d 21795
dda83cd7 21796 cur_file = read_direct_string (abfd, line_ptr,
3e43a32a 21797 &bytes_read);
dda83cd7
SM
21798 line_ptr += bytes_read;
21799 dindex = (dir_index)
21800 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21801 line_ptr += bytes_read;
21802 mod_time =
21803 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21804 line_ptr += bytes_read;
21805 length =
21806 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21807 line_ptr += bytes_read;
21808 lh->add_file_name (cur_file, dindex, mod_time, length);
21809 }
c906108c 21810 break;
d0c6ba3d 21811 case DW_LNE_set_discriminator:
6f77053d
PA
21812 {
21813 /* The discriminator is not interesting to the
21814 debugger; just ignore it. We still need to
21815 check its value though:
21816 if there are consecutive entries for the same
21817 (non-prologue) line we want to coalesce them.
21818 PR 17276. */
21819 unsigned int discr
21820 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21821 line_ptr += bytes_read;
21822
21823 state_machine.handle_set_discriminator (discr);
21824 }
d0c6ba3d 21825 break;
c906108c 21826 default:
b98664d3 21827 complaint (_("mangled .debug_line section"));
debd256d 21828 return;
c906108c 21829 }
a8c50c1f
DJ
21830 /* Make sure that we parsed the extended op correctly. If e.g.
21831 we expected a different address size than the producer used,
21832 we may have read the wrong number of bytes. */
21833 if (line_ptr != extended_end)
21834 {
b98664d3 21835 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21836 return;
21837 }
c906108c
SS
21838 break;
21839 case DW_LNS_copy:
6f77053d 21840 state_machine.handle_copy ();
c906108c
SS
21841 break;
21842 case DW_LNS_advance_pc:
2dc7f7b3
TT
21843 {
21844 CORE_ADDR adjust
21845 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21846 line_ptr += bytes_read;
6f77053d
PA
21847
21848 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21849 }
c906108c
SS
21850 break;
21851 case DW_LNS_advance_line:
a05a36a5
DE
21852 {
21853 int line_delta
21854 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21855 line_ptr += bytes_read;
6f77053d
PA
21856
21857 state_machine.handle_advance_line (line_delta);
a05a36a5 21858 }
c906108c
SS
21859 break;
21860 case DW_LNS_set_file:
d9b3de22 21861 {
6f77053d 21862 file_name_index file
ecfb656c
PA
21863 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21864 &bytes_read);
d9b3de22 21865 line_ptr += bytes_read;
8c43009f 21866
6f77053d 21867 state_machine.handle_set_file (file);
d9b3de22 21868 }
c906108c
SS
21869 break;
21870 case DW_LNS_set_column:
0ad93d4f 21871 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21872 line_ptr += bytes_read;
21873 break;
21874 case DW_LNS_negate_stmt:
6f77053d 21875 state_machine.handle_negate_stmt ();
c906108c
SS
21876 break;
21877 case DW_LNS_set_basic_block:
c906108c 21878 break;
c2c6d25f
JM
21879 /* Add to the address register of the state machine the
21880 address increment value corresponding to special opcode
a738430d
MK
21881 255. I.e., this value is scaled by the minimum
21882 instruction length since special opcode 255 would have
b021a221 21883 scaled the increment. */
c906108c 21884 case DW_LNS_const_add_pc:
6f77053d 21885 state_machine.handle_const_add_pc ();
c906108c
SS
21886 break;
21887 case DW_LNS_fixed_advance_pc:
3e29f34a 21888 {
6f77053d 21889 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21890 line_ptr += 2;
6f77053d
PA
21891
21892 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21893 }
c906108c 21894 break;
9aa1fe7e 21895 default:
a738430d
MK
21896 {
21897 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21898 int i;
a738430d 21899
debd256d 21900 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21901 {
21902 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21903 line_ptr += bytes_read;
21904 }
21905 }
c906108c
SS
21906 }
21907 }
d9b3de22
DE
21908
21909 if (!end_sequence)
21910 dwarf2_debug_line_missing_end_sequence_complaint ();
21911
21912 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21913 in which case we still finish recording the last line). */
6f77053d 21914 state_machine.record_line (true);
c906108c 21915 }
f3f5162e
DE
21916}
21917
21918/* Decode the Line Number Program (LNP) for the given line_header
21919 structure and CU. The actual information extracted and the type
21920 of structures created from the LNP depends on the value of PST.
21921
21922 1. If PST is NULL, then this procedure uses the data from the program
21923 to create all necessary symbol tables, and their linetables.
21924
21925 2. If PST is not NULL, this procedure reads the program to determine
21926 the list of files included by the unit represented by PST, and
21927 builds all the associated partial symbol tables.
21928
21929 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21930 It is used for relative paths in the line table.
21931 NOTE: When processing partial symtabs (pst != NULL),
21932 comp_dir == pst->dirname.
21933
21934 NOTE: It is important that psymtabs have the same file name (via strcmp)
21935 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21936 symtab we don't use it in the name of the psymtabs we create.
21937 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21938 A good testcase for this is mb-inline.exp.
21939
527f3840
JK
21940 LOWPC is the lowest address in CU (or 0 if not known).
21941
21942 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21943 for its PC<->lines mapping information. Otherwise only the filename
21944 table is read in. */
f3f5162e
DE
21945
21946static void
21947dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21948 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21949 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21950{
5e22e966 21951 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21952 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21953
527f3840
JK
21954 if (decode_mapping)
21955 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21956
21957 if (decode_for_pst_p)
21958 {
aaa75496 21959 /* Now that we're done scanning the Line Header Program, we can
dda83cd7 21960 create the psymtab of each included file. */
7ba99d21 21961 for (auto &file_entry : lh->file_names ())
dda83cd7
SM
21962 if (file_entry.included_p == 1)
21963 {
c89b44cd 21964 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21965 const char *include_name =
7ba99d21
AT
21966 psymtab_include_file_name (lh, file_entry, pst,
21967 comp_dir, &name_holder);
c6da4cef 21968 if (include_name != NULL)
dda83cd7
SM
21969 dwarf2_create_include_psymtab (include_name, pst, objfile);
21970 }
aaa75496 21971 }
cb1df416
DJ
21972 else
21973 {
21974 /* Make sure a symtab is created for every file, even files
21975 which contain only variables (i.e. no code with associated
21976 line numbers). */
c24bdb02
KS
21977 buildsym_compunit *builder = cu->get_builder ();
21978 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21979
7ba99d21 21980 for (auto &fe : lh->file_names ())
cb1df416 21981 {
804d2729 21982 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21983 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21984 {
c24bdb02 21985 builder->get_current_subfile ()->symtab
804d2729 21986 = allocate_symtab (cust,
c24bdb02 21987 builder->get_current_subfile ()->name);
43f3e411 21988 }
c24bdb02 21989 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21990 }
21991 }
c906108c
SS
21992}
21993
21994/* Start a subfile for DWARF. FILENAME is the name of the file and
21995 DIRNAME the name of the source directory which contains FILENAME
4d663531 21996 or NULL if not known.
c906108c
SS
21997 This routine tries to keep line numbers from identical absolute and
21998 relative file names in a common subfile.
21999
22000 Using the `list' example from the GDB testsuite, which resides in
22001 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
22002 of /srcdir/list0.c yields the following debugging information for list0.c:
22003
c5aa993b 22004 DW_AT_name: /srcdir/list0.c
4d663531 22005 DW_AT_comp_dir: /compdir
357e46e7 22006 files.files[0].name: list0.h
c5aa993b 22007 files.files[0].dir: /srcdir
357e46e7 22008 files.files[1].name: list0.c
c5aa993b 22009 files.files[1].dir: /srcdir
c906108c
SS
22010
22011 The line number information for list0.c has to end up in a single
4f1520fb
FR
22012 subfile, so that `break /srcdir/list0.c:1' works as expected.
22013 start_subfile will ensure that this happens provided that we pass the
22014 concatenation of files.files[1].dir and files.files[1].name as the
22015 subfile's name. */
c906108c
SS
22016
22017static void
804d2729
TT
22018dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
22019 const char *dirname)
c906108c 22020{
43816ebc 22021 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 22022
4d663531 22023 /* In order not to lose the line information directory,
4f1520fb
FR
22024 we concatenate it to the filename when it makes sense.
22025 Note that the Dwarf3 standard says (speaking of filenames in line
22026 information): ``The directory index is ignored for file names
22027 that represent full path names''. Thus ignoring dirname in the
22028 `else' branch below isn't an issue. */
c906108c 22029
d5166ae1 22030 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 22031 {
43816ebc
TT
22032 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
22033 filename = copy.get ();
d521ce57 22034 }
c906108c 22035
c24bdb02 22036 cu->get_builder ()->start_subfile (filename);
c906108c
SS
22037}
22038
804d2729
TT
22039/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
22040 buildsym_compunit constructor. */
f4dc4d17 22041
c24bdb02
KS
22042struct compunit_symtab *
22043dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
22044 CORE_ADDR low_pc)
f4dc4d17 22045{
c24bdb02 22046 gdb_assert (m_builder == nullptr);
43f3e411 22047
c24bdb02 22048 m_builder.reset (new struct buildsym_compunit
f6e649dd 22049 (this->per_objfile->objfile,
c24bdb02 22050 name, comp_dir, language, low_pc));
93b8bea4 22051
c24bdb02 22052 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 22053
c24bdb02
KS
22054 get_builder ()->record_debugformat ("DWARF 2");
22055 get_builder ()->record_producer (producer);
f4dc4d17 22056
c24bdb02 22057 processing_has_namespace_info = false;
43f3e411 22058
c24bdb02 22059 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
22060}
22061
4c2df51b
DJ
22062static void
22063var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 22064 struct dwarf2_cu *cu)
4c2df51b 22065{
5e22e966 22066 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
22067 struct comp_unit_head *cu_header = &cu->header;
22068
4c2df51b
DJ
22069 /* NOTE drow/2003-01-30: There used to be a comment and some special
22070 code here to turn a symbol with DW_AT_external and a
22071 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
22072 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
22073 with some versions of binutils) where shared libraries could have
22074 relocations against symbols in their debug information - the
22075 minimal symbol would have the right address, but the debug info
22076 would not. It's no longer necessary, because we will explicitly
22077 apply relocations when we read in the debug information now. */
22078
22079 /* A DW_AT_location attribute with no contents indicates that a
22080 variable has been optimized away. */
9d2246fc 22081 if (attr->form_is_block () && attr->as_block ()->size == 0)
4c2df51b 22082 {
f1e6e072 22083 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
22084 return;
22085 }
22086
22087 /* Handle one degenerate form of location expression specially, to
22088 preserve GDB's previous behavior when section offsets are
336d760d
AT
22089 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
22090 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 22091
9d2246fc
TT
22092 if (attr->form_is_block ())
22093 {
22094 struct dwarf_block *block = attr->as_block ();
22095
22096 if ((block->data[0] == DW_OP_addr
22097 && block->size == 1 + cu_header->addr_size)
22098 || ((block->data[0] == DW_OP_GNU_addr_index
dda83cd7 22099 || block->data[0] == DW_OP_addrx)
9d2246fc
TT
22100 && (block->size
22101 == 1 + leb128_size (&block->data[1]))))
22102 {
22103 unsigned int dummy;
22104
22105 if (block->data[0] == DW_OP_addr)
22106 SET_SYMBOL_VALUE_ADDRESS
22107 (sym, cu->header.read_address (objfile->obfd,
22108 block->data + 1,
38583298 22109 &dummy));
9d2246fc
TT
22110 else
22111 SET_SYMBOL_VALUE_ADDRESS
22112 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
22113 &dummy));
22114 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
22115 fixup_symbol_section (sym, objfile);
22116 SET_SYMBOL_VALUE_ADDRESS
22117 (sym,
22118 SYMBOL_VALUE_ADDRESS (sym)
a52d653e 22119 + objfile->section_offsets[sym->section_index ()]);
9d2246fc
TT
22120 return;
22121 }
4c2df51b
DJ
22122 }
22123
22124 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
22125 expression evaluator, and use LOC_COMPUTED only when necessary
22126 (i.e. when the value of a register or memory location is
22127 referenced, or a thread-local block, etc.). Then again, it might
22128 not be worthwhile. I'm assuming that it isn't unless performance
22129 or memory numbers show me otherwise. */
22130
f1e6e072 22131 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 22132
f1e6e072 22133 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 22134 cu->has_loclist = true;
4c2df51b
DJ
22135}
22136
c906108c
SS
22137/* Given a pointer to a DWARF information entry, figure out if we need
22138 to make a symbol table entry for it, and if so, create a new entry
22139 and return a pointer to it.
22140 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
22141 used the passed type.
22142 If SPACE is not NULL, use it to hold the new symbol. If it is
22143 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
22144
22145static struct symbol *
5e2db402
TT
22146new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
22147 struct symbol *space)
c906108c 22148{
976ca316
SM
22149 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22150 struct objfile *objfile = per_objfile->objfile;
08feed99 22151 struct gdbarch *gdbarch = objfile->arch ();
c906108c 22152 struct symbol *sym = NULL;
15d034d0 22153 const char *name;
c906108c
SS
22154 struct attribute *attr = NULL;
22155 struct attribute *attr2 = NULL;
e142c38c 22156 CORE_ADDR baseaddr;
e37fd15a
SW
22157 struct pending **list_to_add = NULL;
22158
edb3359d 22159 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 22160
b3b3bada 22161 baseaddr = objfile->text_section_offset ();
c906108c 22162
94af9270 22163 name = dwarf2_name (die, cu);
c906108c
SS
22164 if (name)
22165 {
34eaf542 22166 int suppress_add = 0;
94af9270 22167
34eaf542
TT
22168 if (space)
22169 sym = space;
22170 else
8c14c3a3 22171 sym = new (&objfile->objfile_obstack) symbol;
c906108c 22172 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
22173
22174 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 22175 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
22176 /* Fortran does not have mangling standard and the mangling does differ
22177 between gfortran, iFort etc. */
bcfe6157
TT
22178 const char *physname
22179 = (cu->language == language_fortran
22180 ? dwarf2_full_name (name, die, cu)
22181 : dwarf2_physname (name, die, cu));
22182 const char *linkagename = dw2_linkage_name (die, cu);
22183
22184 if (linkagename == nullptr || cu->language == language_ada)
22185 sym->set_linkage_name (physname);
22186 else
22187 {
22188 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22189 sym->set_linkage_name (linkagename);
22190 }
f55ee35c 22191
c906108c 22192 /* Default assumptions.
dda83cd7 22193 Use the passed type or decode it from the die. */
176620f1 22194 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 22195 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
22196 if (type != NULL)
22197 SYMBOL_TYPE (sym) = type;
22198 else
e7c27a73 22199 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
22200 attr = dwarf2_attr (die,
22201 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22202 cu);
435d3d88 22203 if (attr != nullptr)
529908cb 22204 SYMBOL_LINE (sym) = attr->constant_value (0);
cb1df416 22205
edb3359d
DJ
22206 attr = dwarf2_attr (die,
22207 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22208 cu);
aa659cfa 22209 if (attr != nullptr && attr->is_nonnegative ())
cb1df416 22210 {
529908cb 22211 file_name_index file_index
aa659cfa 22212 = (file_name_index) attr->as_nonnegative ();
8c43009f 22213 struct file_entry *fe;
9a619af0 22214
ecfb656c
PA
22215 if (cu->line_header != NULL)
22216 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
22217 else
22218 fe = NULL;
22219
22220 if (fe == NULL)
b98664d3 22221 complaint (_("file index out of range"));
8c43009f
PA
22222 else
22223 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
22224 }
22225
c906108c
SS
22226 switch (die->tag)
22227 {
22228 case DW_TAG_label:
e142c38c 22229 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 22230 if (attr != nullptr)
3e29f34a
MR
22231 {
22232 CORE_ADDR addr;
22233
95f982e5 22234 addr = attr->as_address ();
3e29f34a 22235 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 22236 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
8f5c6526 22237 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
3e29f34a 22238 }
8f5c6526
TV
22239 else
22240 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
0f5238ed
TT
22241 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22242 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
d3cb6808 22243 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
22244 break;
22245 case DW_TAG_subprogram:
22246 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22247 finish_block. */
f1e6e072 22248 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 22249 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 22250 if ((attr2 != nullptr && attr2->as_boolean ())
0a4b0913
AB
22251 || cu->language == language_ada
22252 || cu->language == language_fortran)
c906108c 22253 {
dda83cd7
SM
22254 /* Subprograms marked external are stored as a global symbol.
22255 Ada and Fortran subprograms, whether marked external or
22256 not, are always stored as a global symbol, because we want
22257 to be able to access them globally. For instance, we want
22258 to be able to break on a nested subprogram without having
22259 to specify the context. */
c24bdb02 22260 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
22261 }
22262 else
22263 {
e37fd15a 22264 list_to_add = cu->list_in_scope;
c906108c
SS
22265 }
22266 break;
edb3359d
DJ
22267 case DW_TAG_inlined_subroutine:
22268 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22269 finish_block. */
f1e6e072 22270 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 22271 SYMBOL_INLINED (sym) = 1;
481860b3 22272 list_to_add = cu->list_in_scope;
edb3359d 22273 break;
34eaf542
TT
22274 case DW_TAG_template_value_param:
22275 suppress_add = 1;
22276 /* Fall through. */
72929c62 22277 case DW_TAG_constant:
c906108c 22278 case DW_TAG_variable:
254e6b9e 22279 case DW_TAG_member:
0963b4bd
MS
22280 /* Compilation with minimal debug info may result in
22281 variables with missing type entries. Change the
22282 misleading `void' type to something sensible. */
78134374 22283 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 22284 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 22285
e142c38c 22286 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
22287 /* In the case of DW_TAG_member, we should only be called for
22288 static const members. */
22289 if (die->tag == DW_TAG_member)
22290 {
3863f96c
DE
22291 /* dwarf2_add_field uses die_is_declaration,
22292 so we do the same. */
254e6b9e
DE
22293 gdb_assert (die_is_declaration (die, cu));
22294 gdb_assert (attr);
22295 }
435d3d88 22296 if (attr != nullptr)
c906108c 22297 {
e7c27a73 22298 dwarf2_const_value (attr, sym, cu);
e142c38c 22299 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 22300 if (!suppress_add)
34eaf542 22301 {
c45bc3f8 22302 if (attr2 != nullptr && attr2->as_boolean ())
c24bdb02 22303 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 22304 else
e37fd15a 22305 list_to_add = cu->list_in_scope;
34eaf542 22306 }
c906108c
SS
22307 break;
22308 }
e142c38c 22309 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22310 if (attr != nullptr)
c906108c 22311 {
e7c27a73 22312 var_decode_location (attr, sym, cu);
e142c38c 22313 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
22314
22315 /* Fortran explicitly imports any global symbols to the local
22316 scope by DW_TAG_common_block. */
22317 if (cu->language == language_fortran && die->parent
22318 && die->parent->tag == DW_TAG_common_block)
22319 attr2 = NULL;
22320
caac4577
JG
22321 if (SYMBOL_CLASS (sym) == LOC_STATIC
22322 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 22323 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
22324 {
22325 /* When a static variable is eliminated by the linker,
22326 the corresponding debug information is not stripped
22327 out, but the variable address is set to null;
22328 do not add such variables into symbol table. */
22329 }
c45bc3f8 22330 else if (attr2 != nullptr && attr2->as_boolean ())
1c809c68 22331 {
4b610737
TT
22332 if (SYMBOL_CLASS (sym) == LOC_STATIC
22333 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 22334 && per_objfile->per_bfd->can_copy)
4b610737
TT
22335 {
22336 /* A global static variable might be subject to
22337 copy relocation. We first check for a local
22338 minsym, though, because maybe the symbol was
22339 marked hidden, in which case this would not
22340 apply. */
22341 bound_minimal_symbol found
22342 = (lookup_minimal_symbol_linkage
987012b8 22343 (sym->linkage_name (), objfile));
4b610737
TT
22344 if (found.minsym != nullptr)
22345 sym->maybe_copied = 1;
22346 }
f55ee35c 22347
1c809c68
TT
22348 /* A variable with DW_AT_external is never static,
22349 but it may be block-scoped. */
804d2729 22350 list_to_add
c24bdb02
KS
22351 = ((cu->list_in_scope
22352 == cu->get_builder ()->get_file_symbols ())
22353 ? cu->get_builder ()->get_global_symbols ()
804d2729 22354 : cu->list_in_scope);
1c809c68 22355 }
c906108c 22356 else
e37fd15a 22357 list_to_add = cu->list_in_scope;
c906108c
SS
22358 }
22359 else
22360 {
22361 /* We do not know the address of this symbol.
dda83cd7
SM
22362 If it is an external symbol and we have type information
22363 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22364 The address of the variable will then be determined from
22365 the minimal symbol table whenever the variable is
22366 referenced. */
e142c38c 22367 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
22368
22369 /* Fortran explicitly imports any global symbols to the local
22370 scope by DW_TAG_common_block. */
22371 if (cu->language == language_fortran && die->parent
22372 && die->parent->tag == DW_TAG_common_block)
22373 {
22374 /* SYMBOL_CLASS doesn't matter here because
22375 read_common_block is going to reset it. */
22376 if (!suppress_add)
22377 list_to_add = cu->list_in_scope;
22378 }
c45bc3f8 22379 else if (attr2 != nullptr && attr2->as_boolean ()
0971de02 22380 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 22381 {
0fe7935b
DJ
22382 /* A variable with DW_AT_external is never static, but it
22383 may be block-scoped. */
804d2729 22384 list_to_add
c24bdb02
KS
22385 = ((cu->list_in_scope
22386 == cu->get_builder ()->get_file_symbols ())
22387 ? cu->get_builder ()->get_global_symbols ()
804d2729 22388 : cu->list_in_scope);
0fe7935b 22389
f1e6e072 22390 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 22391 }
442ddf59
JK
22392 else if (!die_is_declaration (die, cu))
22393 {
22394 /* Use the default LOC_OPTIMIZED_OUT class. */
22395 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22396 if (!suppress_add)
22397 list_to_add = cu->list_in_scope;
442ddf59 22398 }
c906108c
SS
22399 }
22400 break;
22401 case DW_TAG_formal_parameter:
a60f3166
TT
22402 {
22403 /* If we are inside a function, mark this as an argument. If
22404 not, we might be looking at an argument to an inlined function
22405 when we do not have enough information to show inlined frames;
22406 pretend it's a local variable in that case so that the user can
22407 still see it. */
804d2729 22408 struct context_stack *curr
c24bdb02 22409 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
22410 if (curr != nullptr && curr->name != nullptr)
22411 SYMBOL_IS_ARGUMENT (sym) = 1;
22412 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22413 if (attr != nullptr)
a60f3166
TT
22414 {
22415 var_decode_location (attr, sym, cu);
22416 }
22417 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22418 if (attr != nullptr)
a60f3166
TT
22419 {
22420 dwarf2_const_value (attr, sym, cu);
22421 }
f346a30d 22422
a60f3166
TT
22423 list_to_add = cu->list_in_scope;
22424 }
c906108c
SS
22425 break;
22426 case DW_TAG_unspecified_parameters:
22427 /* From varargs functions; gdb doesn't seem to have any
22428 interest in this information, so just ignore it for now.
22429 (FIXME?) */
22430 break;
34eaf542
TT
22431 case DW_TAG_template_type_param:
22432 suppress_add = 1;
22433 /* Fall through. */
c906108c 22434 case DW_TAG_class_type:
680b30c7 22435 case DW_TAG_interface_type:
c906108c
SS
22436 case DW_TAG_structure_type:
22437 case DW_TAG_union_type:
72019c9c 22438 case DW_TAG_set_type:
c906108c 22439 case DW_TAG_enumeration_type:
f1e6e072 22440 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22441 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22442
63d06c5c 22443 {
9c37b5ae 22444 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22445 really ever be static objects: otherwise, if you try
22446 to, say, break of a class's method and you're in a file
22447 which doesn't mention that class, it won't work unless
22448 the check for all static symbols in lookup_symbol_aux
22449 saves you. See the OtherFileClass tests in
22450 gdb.c++/namespace.exp. */
22451
e37fd15a 22452 if (!suppress_add)
34eaf542 22453 {
c24bdb02 22454 buildsym_compunit *builder = cu->get_builder ();
804d2729 22455 list_to_add
c24bdb02 22456 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22457 && cu->language == language_cplus
c24bdb02 22458 ? builder->get_global_symbols ()
804d2729 22459 : cu->list_in_scope);
63d06c5c 22460
64382290 22461 /* The semantics of C++ state that "struct foo {
9c37b5ae 22462 ... }" also defines a typedef for "foo". */
64382290 22463 if (cu->language == language_cplus
45280282 22464 || cu->language == language_ada
c44af4eb
TT
22465 || cu->language == language_d
22466 || cu->language == language_rust)
64382290
TT
22467 {
22468 /* The symbol's name is already allocated along
22469 with this objfile, so we don't need to
22470 duplicate it for the type. */
7d93a1e0 22471 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 22472 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 22473 }
63d06c5c
DC
22474 }
22475 }
c906108c
SS
22476 break;
22477 case DW_TAG_typedef:
f1e6e072 22478 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22479 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22480 list_to_add = cu->list_in_scope;
63d06c5c 22481 break;
d8f62e84 22482 case DW_TAG_array_type:
c906108c 22483 case DW_TAG_base_type:
dda83cd7 22484 case DW_TAG_subrange_type:
f1e6e072 22485 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22486 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22487 list_to_add = cu->list_in_scope;
c906108c
SS
22488 break;
22489 case DW_TAG_enumerator:
e142c38c 22490 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22491 if (attr != nullptr)
c906108c 22492 {
e7c27a73 22493 dwarf2_const_value (attr, sym, cu);
c906108c 22494 }
63d06c5c
DC
22495 {
22496 /* NOTE: carlton/2003-11-10: See comment above in the
22497 DW_TAG_class_type, etc. block. */
22498
804d2729 22499 list_to_add
c24bdb02 22500 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22501 && cu->language == language_cplus
c24bdb02 22502 ? cu->get_builder ()->get_global_symbols ()
804d2729 22503 : cu->list_in_scope);
63d06c5c 22504 }
c906108c 22505 break;
74921315 22506 case DW_TAG_imported_declaration:
5c4e30ca 22507 case DW_TAG_namespace:
f1e6e072 22508 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22509 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22510 break;
530e8392
KB
22511 case DW_TAG_module:
22512 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22513 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22514 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22515 break;
4357ac6c 22516 case DW_TAG_common_block:
f1e6e072 22517 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22518 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22519 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22520 break;
c906108c
SS
22521 default:
22522 /* Not a tag we recognize. Hopefully we aren't processing
22523 trash data, but since we must specifically ignore things
22524 we don't recognize, there is nothing else we should do at
0963b4bd 22525 this point. */
b98664d3 22526 complaint (_("unsupported tag: '%s'"),
4d3c2250 22527 dwarf_tag_name (die->tag));
c906108c
SS
22528 break;
22529 }
df8a16a1 22530
e37fd15a
SW
22531 if (suppress_add)
22532 {
22533 sym->hash_next = objfile->template_symbols;
22534 objfile->template_symbols = sym;
22535 list_to_add = NULL;
22536 }
22537
22538 if (list_to_add != NULL)
d3cb6808 22539 add_symbol_to_list (sym, list_to_add);
e37fd15a 22540
df8a16a1
DJ
22541 /* For the benefit of old versions of GCC, check for anonymous
22542 namespaces based on the demangled name. */
4d4ec4e5 22543 if (!cu->processing_has_namespace_info
94af9270 22544 && cu->language == language_cplus)
c24bdb02 22545 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22546 }
22547 return (sym);
22548}
22549
98bfdba5
PA
22550/* Given an attr with a DW_FORM_dataN value in host byte order,
22551 zero-extend it as appropriate for the symbol's type. The DWARF
22552 standard (v4) is not entirely clear about the meaning of using
22553 DW_FORM_dataN for a constant with a signed type, where the type is
22554 wider than the data. The conclusion of a discussion on the DWARF
22555 list was that this is unspecified. We choose to always zero-extend
22556 because that is the interpretation long in use by GCC. */
c906108c 22557
98bfdba5 22558static gdb_byte *
ff39bb5e 22559dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22560 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22561{
5e22e966 22562 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
22563 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22564 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
529908cb 22565 LONGEST l = attr->constant_value (0);
98bfdba5
PA
22566
22567 if (bits < sizeof (*value) * 8)
22568 {
22569 l &= ((LONGEST) 1 << bits) - 1;
22570 *value = l;
22571 }
22572 else if (bits == sizeof (*value) * 8)
22573 *value = l;
22574 else
22575 {
224c3ddb 22576 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22577 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22578 return bytes;
22579 }
22580
22581 return NULL;
22582}
22583
22584/* Read a constant value from an attribute. Either set *VALUE, or if
22585 the value does not fit in *VALUE, set *BYTES - either already
22586 allocated on the objfile obstack, or newly allocated on OBSTACK,
22587 or, set *BATON, if we translated the constant to a location
22588 expression. */
22589
22590static void
ff39bb5e 22591dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22592 const char *name, struct obstack *obstack,
22593 struct dwarf2_cu *cu,
d521ce57 22594 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22595 struct dwarf2_locexpr_baton **baton)
22596{
5e22e966 22597 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 22598 struct objfile *objfile = per_objfile->objfile;
98bfdba5 22599 struct comp_unit_head *cu_header = &cu->header;
c906108c 22600 struct dwarf_block *blk;
98bfdba5
PA
22601 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22602 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22603
22604 *value = 0;
22605 *bytes = NULL;
22606 *baton = NULL;
c906108c
SS
22607
22608 switch (attr->form)
22609 {
22610 case DW_FORM_addr:
336d760d 22611 case DW_FORM_addrx:
3019eac3 22612 case DW_FORM_GNU_addr_index:
ac56253d 22613 {
ac56253d
TT
22614 gdb_byte *data;
22615
98bfdba5
PA
22616 if (TYPE_LENGTH (type) != cu_header->addr_size)
22617 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22618 cu_header->addr_size,
98bfdba5 22619 TYPE_LENGTH (type));
ac56253d
TT
22620 /* Symbols of this form are reasonably rare, so we just
22621 piggyback on the existing location code rather than writing
22622 a new implementation of symbol_computed_ops. */
8d749320 22623 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 22624 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
22625 (*baton)->per_cu = cu->per_cu;
22626 gdb_assert ((*baton)->per_cu);
ac56253d 22627
98bfdba5 22628 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22629 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22630 (*baton)->data = data;
ac56253d
TT
22631
22632 data[0] = DW_OP_addr;
22633 store_unsigned_integer (&data[1], cu_header->addr_size,
36d378cf 22634 byte_order, attr->as_address ());
ac56253d 22635 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22636 }
c906108c 22637 break;
4ac36638 22638 case DW_FORM_string:
93b5768b 22639 case DW_FORM_strp:
cf532bd1 22640 case DW_FORM_strx:
3019eac3 22641 case DW_FORM_GNU_str_index:
36586728 22642 case DW_FORM_GNU_strp_alt:
c6481205 22643 /* The string is already allocated on the objfile obstack, point
98bfdba5 22644 directly to it. */
2c830f54 22645 *bytes = (const gdb_byte *) attr->as_string ();
93b5768b 22646 break;
c906108c
SS
22647 case DW_FORM_block1:
22648 case DW_FORM_block2:
22649 case DW_FORM_block4:
22650 case DW_FORM_block:
2dc7f7b3 22651 case DW_FORM_exprloc:
0224619f 22652 case DW_FORM_data16:
9d2246fc 22653 blk = attr->as_block ();
98bfdba5
PA
22654 if (TYPE_LENGTH (type) != blk->size)
22655 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22656 TYPE_LENGTH (type));
22657 *bytes = blk->data;
c906108c 22658 break;
2df3850c
JM
22659
22660 /* The DW_AT_const_value attributes are supposed to carry the
22661 symbol's value "represented as it would be on the target
22662 architecture." By the time we get here, it's already been
22663 converted to host endianness, so we just need to sign- or
22664 zero-extend it as appropriate. */
22665 case DW_FORM_data1:
3aef2284 22666 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22667 break;
c906108c 22668 case DW_FORM_data2:
3aef2284 22669 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22670 break;
c906108c 22671 case DW_FORM_data4:
3aef2284 22672 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22673 break;
c906108c 22674 case DW_FORM_data8:
3aef2284 22675 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22676 break;
22677
c906108c 22678 case DW_FORM_sdata:
663c44ac 22679 case DW_FORM_implicit_const:
1bc397c5 22680 *value = attr->as_signed ();
2df3850c
JM
22681 break;
22682
c906108c 22683 case DW_FORM_udata:
529908cb 22684 *value = attr->as_unsigned ();
c906108c 22685 break;
2df3850c 22686
c906108c 22687 default:
b98664d3 22688 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22689 dwarf_form_name (attr->form));
98bfdba5 22690 *value = 0;
c906108c
SS
22691 break;
22692 }
22693}
22694
2df3850c 22695
98bfdba5
PA
22696/* Copy constant value from an attribute to a symbol. */
22697
2df3850c 22698static void
ff39bb5e 22699dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22700 struct dwarf2_cu *cu)
2df3850c 22701{
5e22e966 22702 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 22703 LONGEST value;
d521ce57 22704 const gdb_byte *bytes;
98bfdba5 22705 struct dwarf2_locexpr_baton *baton;
2df3850c 22706
98bfdba5 22707 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22708 sym->print_name (),
98bfdba5
PA
22709 &objfile->objfile_obstack, cu,
22710 &value, &bytes, &baton);
2df3850c 22711
98bfdba5
PA
22712 if (baton != NULL)
22713 {
98bfdba5 22714 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22715 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22716 }
22717 else if (bytes != NULL)
22718 {
22719 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22720 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22721 }
22722 else
22723 {
22724 SYMBOL_VALUE (sym) = value;
f1e6e072 22725 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22726 }
2df3850c
JM
22727}
22728
c906108c
SS
22729/* Return the type of the die in question using its DW_AT_type attribute. */
22730
22731static struct type *
e7c27a73 22732die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22733{
c906108c 22734 struct attribute *type_attr;
c906108c 22735
e142c38c 22736 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22737 if (!type_attr)
22738 {
5e22e966 22739 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22740 /* A missing DW_AT_type represents a void type. */
518817b3 22741 return objfile_type (objfile)->builtin_void;
c906108c 22742 }
348e048f 22743
673bfd45 22744 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22745}
22746
b4ba55a1
JB
22747/* True iff CU's producer generates GNAT Ada auxiliary information
22748 that allows to find parallel types through that information instead
22749 of having to do expensive parallel lookups by type name. */
22750
22751static int
22752need_gnat_info (struct dwarf2_cu *cu)
22753{
de4cb04a
JB
22754 /* Assume that the Ada compiler was GNAT, which always produces
22755 the auxiliary information. */
22756 return (cu->language == language_ada);
b4ba55a1
JB
22757}
22758
b4ba55a1
JB
22759/* Return the auxiliary type of the die in question using its
22760 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22761 attribute is not present. */
22762
22763static struct type *
22764die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22765{
b4ba55a1 22766 struct attribute *type_attr;
b4ba55a1
JB
22767
22768 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22769 if (!type_attr)
22770 return NULL;
22771
673bfd45 22772 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22773}
22774
22775/* If DIE has a descriptive_type attribute, then set the TYPE's
22776 descriptive type accordingly. */
22777
22778static void
22779set_descriptive_type (struct type *type, struct die_info *die,
22780 struct dwarf2_cu *cu)
22781{
22782 struct type *descriptive_type = die_descriptive_type (die, cu);
22783
22784 if (descriptive_type)
22785 {
22786 ALLOCATE_GNAT_AUX_TYPE (type);
22787 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22788 }
22789}
22790
c906108c
SS
22791/* Return the containing type of the die in question using its
22792 DW_AT_containing_type attribute. */
22793
22794static struct type *
e7c27a73 22795die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22796{
c906108c 22797 struct attribute *type_attr;
5e22e966 22798 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22799
e142c38c 22800 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22801 if (!type_attr)
22802 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22803 "[in module %s]"), objfile_name (objfile));
33ac96f0 22804
673bfd45 22805 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22806}
22807
ac9ec31b
DE
22808/* Return an error marker type to use for the ill formed type in DIE/CU. */
22809
22810static struct type *
22811build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22812{
976ca316
SM
22813 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22814 struct objfile *objfile = per_objfile->objfile;
528e1572 22815 char *saved;
ac9ec31b 22816
528e1572
SM
22817 std::string message
22818 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22819 objfile_name (objfile),
22820 sect_offset_str (cu->header.sect_off),
22821 sect_offset_str (die->sect_off));
efba19b0 22822 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22823
19f392bc 22824 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22825}
22826
673bfd45 22827/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22828 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22829 DW_AT_containing_type.
673bfd45
DE
22830 If there is no type substitute an error marker. */
22831
c906108c 22832static struct type *
ff39bb5e 22833lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22834 struct dwarf2_cu *cu)
c906108c 22835{
976ca316
SM
22836 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22837 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
22838 struct type *this_type;
22839
ac9ec31b
DE
22840 gdb_assert (attr->name == DW_AT_type
22841 || attr->name == DW_AT_GNAT_descriptive_type
22842 || attr->name == DW_AT_containing_type);
22843
673bfd45
DE
22844 /* First see if we have it cached. */
22845
36586728
TT
22846 if (attr->form == DW_FORM_GNU_ref_alt)
22847 {
22848 struct dwarf2_per_cu_data *per_cu;
0826b30a 22849 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 22850
976ca316
SM
22851 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22852 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 22853 }
cd6c91b4 22854 else if (attr->form_is_ref ())
673bfd45 22855 {
0826b30a 22856 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 22857
976ca316 22858 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 22859 }
55f1336d 22860 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22861 {
630ed6b9 22862 ULONGEST signature = attr->as_signature ();
673bfd45 22863
ac9ec31b 22864 return get_signatured_type (die, signature, cu);
673bfd45
DE
22865 }
22866 else
22867 {
b98664d3 22868 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22869 " at %s [in module %s]"),
22870 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22871 objfile_name (objfile));
ac9ec31b 22872 return build_error_marker_type (cu, die);
673bfd45
DE
22873 }
22874
22875 /* If not cached we need to read it in. */
22876
22877 if (this_type == NULL)
22878 {
ac9ec31b 22879 struct die_info *type_die = NULL;
673bfd45
DE
22880 struct dwarf2_cu *type_cu = cu;
22881
cd6c91b4 22882 if (attr->form_is_ref ())
ac9ec31b
DE
22883 type_die = follow_die_ref (die, attr, &type_cu);
22884 if (type_die == NULL)
22885 return build_error_marker_type (cu, die);
22886 /* If we find the type now, it's probably because the type came
3019eac3
DE
22887 from an inter-CU reference and the type's CU got expanded before
22888 ours. */
ac9ec31b 22889 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22890 }
22891
22892 /* If we still don't have a type use an error marker. */
22893
22894 if (this_type == NULL)
ac9ec31b 22895 return build_error_marker_type (cu, die);
673bfd45 22896
f792889a 22897 return this_type;
c906108c
SS
22898}
22899
673bfd45
DE
22900/* Return the type in DIE, CU.
22901 Returns NULL for invalid types.
22902
02142a6c 22903 This first does a lookup in die_type_hash,
673bfd45
DE
22904 and only reads the die in if necessary.
22905
22906 NOTE: This can be called when reading in partial or full symbols. */
22907
f792889a 22908static struct type *
e7c27a73 22909read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22910{
f792889a
DJ
22911 struct type *this_type;
22912
22913 this_type = get_die_type (die, cu);
22914 if (this_type)
22915 return this_type;
22916
673bfd45
DE
22917 return read_type_die_1 (die, cu);
22918}
22919
22920/* Read the type in DIE, CU.
22921 Returns NULL for invalid types. */
22922
22923static struct type *
22924read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22925{
22926 struct type *this_type = NULL;
22927
c906108c
SS
22928 switch (die->tag)
22929 {
22930 case DW_TAG_class_type:
680b30c7 22931 case DW_TAG_interface_type:
c906108c
SS
22932 case DW_TAG_structure_type:
22933 case DW_TAG_union_type:
f792889a 22934 this_type = read_structure_type (die, cu);
c906108c
SS
22935 break;
22936 case DW_TAG_enumeration_type:
f792889a 22937 this_type = read_enumeration_type (die, cu);
c906108c
SS
22938 break;
22939 case DW_TAG_subprogram:
22940 case DW_TAG_subroutine_type:
edb3359d 22941 case DW_TAG_inlined_subroutine:
f792889a 22942 this_type = read_subroutine_type (die, cu);
c906108c
SS
22943 break;
22944 case DW_TAG_array_type:
f792889a 22945 this_type = read_array_type (die, cu);
c906108c 22946 break;
72019c9c 22947 case DW_TAG_set_type:
f792889a 22948 this_type = read_set_type (die, cu);
72019c9c 22949 break;
c906108c 22950 case DW_TAG_pointer_type:
f792889a 22951 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22952 break;
22953 case DW_TAG_ptr_to_member_type:
f792889a 22954 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22955 break;
22956 case DW_TAG_reference_type:
4297a3f0
AV
22957 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22958 break;
22959 case DW_TAG_rvalue_reference_type:
22960 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22961 break;
22962 case DW_TAG_const_type:
f792889a 22963 this_type = read_tag_const_type (die, cu);
c906108c
SS
22964 break;
22965 case DW_TAG_volatile_type:
f792889a 22966 this_type = read_tag_volatile_type (die, cu);
c906108c 22967 break;
06d66ee9
TT
22968 case DW_TAG_restrict_type:
22969 this_type = read_tag_restrict_type (die, cu);
22970 break;
c906108c 22971 case DW_TAG_string_type:
f792889a 22972 this_type = read_tag_string_type (die, cu);
c906108c
SS
22973 break;
22974 case DW_TAG_typedef:
f792889a 22975 this_type = read_typedef (die, cu);
c906108c 22976 break;
a02abb62 22977 case DW_TAG_subrange_type:
f792889a 22978 this_type = read_subrange_type (die, cu);
a02abb62 22979 break;
c906108c 22980 case DW_TAG_base_type:
f792889a 22981 this_type = read_base_type (die, cu);
c906108c 22982 break;
81a17f79 22983 case DW_TAG_unspecified_type:
f792889a 22984 this_type = read_unspecified_type (die, cu);
81a17f79 22985 break;
0114d602
DJ
22986 case DW_TAG_namespace:
22987 this_type = read_namespace_type (die, cu);
22988 break;
f55ee35c
JK
22989 case DW_TAG_module:
22990 this_type = read_module_type (die, cu);
22991 break;
a2c2acaf
MW
22992 case DW_TAG_atomic_type:
22993 this_type = read_tag_atomic_type (die, cu);
22994 break;
c906108c 22995 default:
b98664d3 22996 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22997 dwarf_tag_name (die->tag));
c906108c
SS
22998 break;
22999 }
63d06c5c 23000
f792889a 23001 return this_type;
63d06c5c
DC
23002}
23003
abc72ce4
DE
23004/* See if we can figure out if the class lives in a namespace. We do
23005 this by looking for a member function; its demangled name will
23006 contain namespace info, if there is any.
23007 Return the computed name or NULL.
23008 Space for the result is allocated on the objfile's obstack.
23009 This is the full-die version of guess_partial_die_structure_name.
23010 In this case we know DIE has no useful parent. */
23011
43816ebc 23012static const char *
abc72ce4
DE
23013guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
23014{
23015 struct die_info *spec_die;
23016 struct dwarf2_cu *spec_cu;
23017 struct die_info *child;
5e22e966 23018 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
23019
23020 spec_cu = cu;
23021 spec_die = die_specification (die, &spec_cu);
23022 if (spec_die != NULL)
23023 {
23024 die = spec_die;
23025 cu = spec_cu;
23026 }
23027
23028 for (child = die->child;
23029 child != NULL;
23030 child = child->sibling)
23031 {
23032 if (child->tag == DW_TAG_subprogram)
23033 {
73b9be8b 23034 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 23035
7d45c7c3 23036 if (linkage_name != NULL)
abc72ce4 23037 {
43816ebc 23038 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 23039 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 23040 const char *name = NULL;
abc72ce4
DE
23041
23042 if (actual_name != NULL)
23043 {
15d034d0 23044 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
23045
23046 if (die_name != NULL
43816ebc 23047 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
23048 {
23049 /* Strip off the class name from the full name.
23050 We want the prefix. */
23051 int die_name_len = strlen (die_name);
43816ebc
TT
23052 int actual_name_len = strlen (actual_name.get ());
23053 const char *ptr = actual_name.get ();
abc72ce4
DE
23054
23055 /* Test for '::' as a sanity check. */
23056 if (actual_name_len > die_name_len + 2
43816ebc 23057 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 23058 name = obstack_strndup (
e3b94546 23059 &objfile->per_bfd->storage_obstack,
43816ebc 23060 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
23061 }
23062 }
abc72ce4
DE
23063 return name;
23064 }
23065 }
23066 }
23067
23068 return NULL;
23069}
23070
96408a79
SA
23071/* GCC might emit a nameless typedef that has a linkage name. Determine the
23072 prefix part in such case. See
23073 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23074
a121b7c1 23075static const char *
96408a79
SA
23076anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
23077{
23078 struct attribute *attr;
e6a959d6 23079 const char *base;
96408a79
SA
23080
23081 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
23082 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
23083 return NULL;
23084
7d45c7c3 23085 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
23086 return NULL;
23087
73b9be8b 23088 attr = dw2_linkage_name_attr (die, cu);
2c830f54
TT
23089 const char *attr_name = attr->as_string ();
23090 if (attr == NULL || attr_name == NULL)
96408a79
SA
23091 return NULL;
23092
23093 /* dwarf2_name had to be already called. */
3b64bf15 23094 gdb_assert (attr->canonical_string_p ());
96408a79
SA
23095
23096 /* Strip the base name, keep any leading namespaces/classes. */
2c830f54
TT
23097 base = strrchr (attr_name, ':');
23098 if (base == NULL || base == attr_name || base[-1] != ':')
96408a79
SA
23099 return "";
23100
5e22e966 23101 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9 23102 return obstack_strndup (&objfile->per_bfd->storage_obstack,
2c830f54
TT
23103 attr_name,
23104 &base[-1] - attr_name);
96408a79
SA
23105}
23106
fdde2d81 23107/* Return the name of the namespace/class that DIE is defined within,
0114d602 23108 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 23109
0114d602
DJ
23110 For example, if we're within the method foo() in the following
23111 code:
23112
23113 namespace N {
23114 class C {
23115 void foo () {
23116 }
23117 };
23118 }
23119
23120 then determine_prefix on foo's die will return "N::C". */
fdde2d81 23121
0d5cff50 23122static const char *
e142c38c 23123determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 23124{
976ca316 23125 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
23126 struct die_info *parent, *spec_die;
23127 struct dwarf2_cu *spec_cu;
23128 struct type *parent_type;
a121b7c1 23129 const char *retval;
63d06c5c 23130
9c37b5ae 23131 if (cu->language != language_cplus
c44af4eb
TT
23132 && cu->language != language_fortran && cu->language != language_d
23133 && cu->language != language_rust)
0114d602
DJ
23134 return "";
23135
96408a79
SA
23136 retval = anonymous_struct_prefix (die, cu);
23137 if (retval)
23138 return retval;
23139
0114d602
DJ
23140 /* We have to be careful in the presence of DW_AT_specification.
23141 For example, with GCC 3.4, given the code
23142
23143 namespace N {
23144 void foo() {
23145 // Definition of N::foo.
23146 }
23147 }
23148
23149 then we'll have a tree of DIEs like this:
23150
23151 1: DW_TAG_compile_unit
23152 2: DW_TAG_namespace // N
23153 3: DW_TAG_subprogram // declaration of N::foo
23154 4: DW_TAG_subprogram // definition of N::foo
23155 DW_AT_specification // refers to die #3
23156
23157 Thus, when processing die #4, we have to pretend that we're in
23158 the context of its DW_AT_specification, namely the contex of die
23159 #3. */
23160 spec_cu = cu;
23161 spec_die = die_specification (die, &spec_cu);
23162 if (spec_die == NULL)
23163 parent = die->parent;
23164 else
63d06c5c 23165 {
0114d602
DJ
23166 parent = spec_die->parent;
23167 cu = spec_cu;
63d06c5c 23168 }
0114d602
DJ
23169
23170 if (parent == NULL)
23171 return "";
98bfdba5
PA
23172 else if (parent->building_fullname)
23173 {
23174 const char *name;
23175 const char *parent_name;
23176
23177 /* It has been seen on RealView 2.2 built binaries,
23178 DW_TAG_template_type_param types actually _defined_ as
23179 children of the parent class:
23180
23181 enum E {};
23182 template class <class Enum> Class{};
23183 Class<enum E> class_e;
23184
dda83cd7
SM
23185 1: DW_TAG_class_type (Class)
23186 2: DW_TAG_enumeration_type (E)
23187 3: DW_TAG_enumerator (enum1:0)
23188 3: DW_TAG_enumerator (enum2:1)
23189 ...
23190 2: DW_TAG_template_type_param
23191 DW_AT_type DW_FORM_ref_udata (E)
98bfdba5
PA
23192
23193 Besides being broken debug info, it can put GDB into an
23194 infinite loop. Consider:
23195
23196 When we're building the full name for Class<E>, we'll start
23197 at Class, and go look over its template type parameters,
23198 finding E. We'll then try to build the full name of E, and
23199 reach here. We're now trying to build the full name of E,
23200 and look over the parent DIE for containing scope. In the
23201 broken case, if we followed the parent DIE of E, we'd again
23202 find Class, and once again go look at its template type
23203 arguments, etc., etc. Simply don't consider such parent die
23204 as source-level parent of this die (it can't be, the language
23205 doesn't allow it), and break the loop here. */
23206 name = dwarf2_name (die, cu);
23207 parent_name = dwarf2_name (parent, cu);
b98664d3 23208 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
23209 name ? name : "<unknown>",
23210 parent_name ? parent_name : "<unknown>");
23211 return "";
23212 }
63d06c5c 23213 else
0114d602
DJ
23214 switch (parent->tag)
23215 {
63d06c5c 23216 case DW_TAG_namespace:
0114d602 23217 parent_type = read_type_die (parent, cu);
acebe513
UW
23218 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23219 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23220 Work around this problem here. */
23221 if (cu->language == language_cplus
7d93a1e0 23222 && strcmp (parent_type->name (), "::") == 0)
acebe513 23223 return "";
0114d602 23224 /* We give a name to even anonymous namespaces. */
7d93a1e0 23225 return parent_type->name ();
63d06c5c 23226 case DW_TAG_class_type:
680b30c7 23227 case DW_TAG_interface_type:
63d06c5c 23228 case DW_TAG_structure_type:
0114d602 23229 case DW_TAG_union_type:
f55ee35c 23230 case DW_TAG_module:
0114d602 23231 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
23232 if (parent_type->name () != NULL)
23233 return parent_type->name ();
0114d602
DJ
23234 else
23235 /* An anonymous structure is only allowed non-static data
23236 members; no typedefs, no member functions, et cetera.
23237 So it does not need a prefix. */
23238 return "";
abc72ce4 23239 case DW_TAG_compile_unit:
95554aad 23240 case DW_TAG_partial_unit:
abc72ce4
DE
23241 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23242 if (cu->language == language_cplus
976ca316 23243 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
23244 && die->child != NULL
23245 && (die->tag == DW_TAG_class_type
23246 || die->tag == DW_TAG_structure_type
23247 || die->tag == DW_TAG_union_type))
23248 {
43816ebc 23249 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
23250 if (name != NULL)
23251 return name;
23252 }
23253 return "";
0a4b0913
AB
23254 case DW_TAG_subprogram:
23255 /* Nested subroutines in Fortran get a prefix with the name
23256 of the parent's subroutine. */
23257 if (cu->language == language_fortran)
23258 {
23259 if ((die->tag == DW_TAG_subprogram)
23260 && (dwarf2_name (parent, cu) != NULL))
23261 return dwarf2_name (parent, cu);
23262 }
23263 return determine_prefix (parent, cu);
3d567982
TT
23264 case DW_TAG_enumeration_type:
23265 parent_type = read_type_die (parent, cu);
23266 if (TYPE_DECLARED_CLASS (parent_type))
23267 {
7d93a1e0
SM
23268 if (parent_type->name () != NULL)
23269 return parent_type->name ();
3d567982
TT
23270 return "";
23271 }
23272 /* Fall through. */
63d06c5c 23273 default:
8176b9b8 23274 return determine_prefix (parent, cu);
63d06c5c 23275 }
63d06c5c
DC
23276}
23277
3e43a32a
MS
23278/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23279 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23280 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23281 an obconcat, otherwise allocate storage for the result. The CU argument is
23282 used to determine the language and hence, the appropriate separator. */
987504bb 23283
f55ee35c 23284#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
23285
23286static char *
f55ee35c 23287typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
dda83cd7 23288 int physname, struct dwarf2_cu *cu)
63d06c5c 23289{
f55ee35c 23290 const char *lead = "";
5c315b68 23291 const char *sep;
63d06c5c 23292
3e43a32a
MS
23293 if (suffix == NULL || suffix[0] == '\0'
23294 || prefix == NULL || prefix[0] == '\0')
987504bb 23295 sep = "";
45280282
IB
23296 else if (cu->language == language_d)
23297 {
23298 /* For D, the 'main' function could be defined in any module, but it
23299 should never be prefixed. */
23300 if (strcmp (suffix, "D main") == 0)
23301 {
23302 prefix = "";
23303 sep = "";
23304 }
23305 else
23306 sep = ".";
23307 }
f55ee35c
JK
23308 else if (cu->language == language_fortran && physname)
23309 {
23310 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23311 DW_AT_MIPS_linkage_name is preferred and used instead. */
23312
23313 lead = "__";
23314 sep = "_MOD_";
23315 }
987504bb
JJ
23316 else
23317 sep = "::";
63d06c5c 23318
6dd47d34
DE
23319 if (prefix == NULL)
23320 prefix = "";
23321 if (suffix == NULL)
23322 suffix = "";
23323
987504bb
JJ
23324 if (obs == NULL)
23325 {
3e43a32a 23326 char *retval
224c3ddb
SM
23327 = ((char *)
23328 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 23329
f55ee35c
JK
23330 strcpy (retval, lead);
23331 strcat (retval, prefix);
6dd47d34
DE
23332 strcat (retval, sep);
23333 strcat (retval, suffix);
63d06c5c
DC
23334 return retval;
23335 }
987504bb
JJ
23336 else
23337 {
23338 /* We have an obstack. */
f55ee35c 23339 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 23340 }
63d06c5c
DC
23341}
23342
71c25dea
TT
23343/* Get name of a die, return NULL if not found. */
23344
15d034d0
TT
23345static const char *
23346dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 23347 struct objfile *objfile)
71c25dea
TT
23348{
23349 if (name && cu->language == language_cplus)
23350 {
596dc4ad
TT
23351 gdb::unique_xmalloc_ptr<char> canon_name
23352 = cp_canonicalize_string (name);
71c25dea 23353
596dc4ad
TT
23354 if (canon_name != nullptr)
23355 name = objfile->intern (canon_name.get ());
71c25dea
TT
23356 }
23357
23358 return name;
c906108c
SS
23359}
23360
96553a0c
DE
23361/* Get name of a die, return NULL if not found.
23362 Anonymous namespaces are converted to their magic string. */
9219021c 23363
15d034d0 23364static const char *
e142c38c 23365dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
23366{
23367 struct attribute *attr;
5e22e966 23368 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 23369
e142c38c 23370 attr = dwarf2_attr (die, DW_AT_name, cu);
2c830f54
TT
23371 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23372 if (attr_name == nullptr
96553a0c 23373 && die->tag != DW_TAG_namespace
53832f31
TT
23374 && die->tag != DW_TAG_class_type
23375 && die->tag != DW_TAG_interface_type
23376 && die->tag != DW_TAG_structure_type
23377 && die->tag != DW_TAG_union_type)
71c25dea
TT
23378 return NULL;
23379
23380 switch (die->tag)
23381 {
23382 case DW_TAG_compile_unit:
95554aad 23383 case DW_TAG_partial_unit:
71c25dea
TT
23384 /* Compilation units have a DW_AT_name that is a filename, not
23385 a source language identifier. */
23386 case DW_TAG_enumeration_type:
23387 case DW_TAG_enumerator:
23388 /* These tags always have simple identifiers already; no need
23389 to canonicalize them. */
2c830f54 23390 return attr_name;
907af001 23391
96553a0c 23392 case DW_TAG_namespace:
2c830f54
TT
23393 if (attr_name != nullptr)
23394 return attr_name;
96553a0c
DE
23395 return CP_ANONYMOUS_NAMESPACE_STR;
23396
907af001
UW
23397 case DW_TAG_class_type:
23398 case DW_TAG_interface_type:
23399 case DW_TAG_structure_type:
23400 case DW_TAG_union_type:
23401 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23402 structures or unions. These were of the form "._%d" in GCC 4.1,
23403 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23404 and GCC 4.4. We work around this problem by ignoring these. */
2c830f54
TT
23405 if (attr_name != nullptr
23406 && (startswith (attr_name, "._")
23407 || startswith (attr_name, "<anonymous")))
907af001 23408 return NULL;
53832f31
TT
23409
23410 /* GCC might emit a nameless typedef that has a linkage name. See
23411 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
2c830f54 23412 if (!attr || attr_name == NULL)
53832f31 23413 {
73b9be8b 23414 attr = dw2_linkage_name_attr (die, cu);
95eb9e54 23415 attr_name = attr == nullptr ? nullptr : attr->as_string ();
2c830f54 23416 if (attr == NULL || attr_name == NULL)
53832f31
TT
23417 return NULL;
23418
2c830f54 23419 /* Avoid demangling attr_name the second time on a second
df5c6c50 23420 call for the same DIE. */
3b64bf15 23421 if (!attr->canonical_string_p ())
53832f31 23422 {
43816ebc 23423 gdb::unique_xmalloc_ptr<char> demangled
2c830f54 23424 (gdb_demangle (attr_name, DMGL_TYPES));
4f180d53
AT
23425 if (demangled == nullptr)
23426 return nullptr;
43816ebc 23427
c6481205 23428 attr->set_string_canonical (objfile->intern (demangled.get ()));
95eb9e54 23429 attr_name = attr->as_string ();
53832f31 23430 }
67430cd0 23431
2c830f54
TT
23432 /* Strip any leading namespaces/classes, keep only the
23433 base name. DW_AT_name for named DIEs does not
23434 contain the prefixes. */
23435 const char *base = strrchr (attr_name, ':');
23436 if (base && base > attr_name && base[-1] == ':')
67430cd0
TT
23437 return &base[1];
23438 else
2c830f54 23439 return attr_name;
53832f31 23440 }
907af001
UW
23441 break;
23442
71c25dea 23443 default:
907af001
UW
23444 break;
23445 }
23446
3b64bf15 23447 if (!attr->canonical_string_p ())
c6481205
TT
23448 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23449 objfile));
2c830f54 23450 return attr->as_string ();
9219021c
DC
23451}
23452
23453/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23454 is none. *EXT_CU is the CU containing DIE on input, and the CU
23455 containing the return value on output. */
9219021c
DC
23456
23457static struct die_info *
f2f0e013 23458dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23459{
23460 struct attribute *attr;
9219021c 23461
f2f0e013 23462 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23463 if (attr == NULL)
23464 return NULL;
23465
f2f0e013 23466 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23467}
23468
f9aca02d 23469static void
d97bc12b 23470dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23471{
23472 unsigned int i;
23473
d97bc12b 23474 print_spaces (indent, f);
9d8780f0 23475 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23476 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23477 sect_offset_str (die->sect_off));
d97bc12b
DE
23478
23479 if (die->parent != NULL)
23480 {
23481 print_spaces (indent, f);
9d8780f0
SM
23482 fprintf_unfiltered (f, " parent at offset: %s\n",
23483 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23484 }
23485
23486 print_spaces (indent, f);
23487 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23488 dwarf_bool_name (die->child != NULL));
c906108c 23489
d97bc12b
DE
23490 print_spaces (indent, f);
23491 fprintf_unfiltered (f, " attributes:\n");
23492
c906108c
SS
23493 for (i = 0; i < die->num_attrs; ++i)
23494 {
d97bc12b
DE
23495 print_spaces (indent, f);
23496 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23497 dwarf_attr_name (die->attrs[i].name),
23498 dwarf_form_name (die->attrs[i].form));
d97bc12b 23499
c906108c
SS
23500 switch (die->attrs[i].form)
23501 {
c906108c 23502 case DW_FORM_addr:
336d760d 23503 case DW_FORM_addrx:
3019eac3 23504 case DW_FORM_GNU_addr_index:
d97bc12b 23505 fprintf_unfiltered (f, "address: ");
36d378cf 23506 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
c906108c
SS
23507 break;
23508 case DW_FORM_block2:
23509 case DW_FORM_block4:
23510 case DW_FORM_block:
23511 case DW_FORM_block1:
56eb65bd 23512 fprintf_unfiltered (f, "block: size %s",
9d2246fc 23513 pulongest (die->attrs[i].as_block ()->size));
c906108c 23514 break;
2dc7f7b3 23515 case DW_FORM_exprloc:
56eb65bd 23516 fprintf_unfiltered (f, "expression: size %s",
9d2246fc 23517 pulongest (die->attrs[i].as_block ()->size));
2dc7f7b3 23518 break;
0224619f
JK
23519 case DW_FORM_data16:
23520 fprintf_unfiltered (f, "constant of 16 bytes");
23521 break;
4568ecf9
DE
23522 case DW_FORM_ref_addr:
23523 fprintf_unfiltered (f, "ref address: ");
529908cb 23524 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
4568ecf9 23525 break;
36586728
TT
23526 case DW_FORM_GNU_ref_alt:
23527 fprintf_unfiltered (f, "alt ref address: ");
529908cb 23528 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
36586728 23529 break;
10b3939b
DJ
23530 case DW_FORM_ref1:
23531 case DW_FORM_ref2:
23532 case DW_FORM_ref4:
4568ecf9
DE
23533 case DW_FORM_ref8:
23534 case DW_FORM_ref_udata:
d97bc12b 23535 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
529908cb 23536 (long) (die->attrs[i].as_unsigned ()));
10b3939b 23537 break;
c906108c
SS
23538 case DW_FORM_data1:
23539 case DW_FORM_data2:
23540 case DW_FORM_data4:
ce5d95e1 23541 case DW_FORM_data8:
c906108c 23542 case DW_FORM_udata:
43bbcdc2 23543 fprintf_unfiltered (f, "constant: %s",
529908cb 23544 pulongest (die->attrs[i].as_unsigned ()));
c906108c 23545 break;
2dc7f7b3
TT
23546 case DW_FORM_sec_offset:
23547 fprintf_unfiltered (f, "section offset: %s",
529908cb 23548 pulongest (die->attrs[i].as_unsigned ()));
2dc7f7b3 23549 break;
55f1336d 23550 case DW_FORM_ref_sig8:
ac9ec31b 23551 fprintf_unfiltered (f, "signature: %s",
630ed6b9 23552 hex_string (die->attrs[i].as_signature ()));
348e048f 23553 break;
c906108c 23554 case DW_FORM_string:
4bdf3d34 23555 case DW_FORM_strp:
43988095 23556 case DW_FORM_line_strp:
cf532bd1 23557 case DW_FORM_strx:
3019eac3 23558 case DW_FORM_GNU_str_index:
36586728 23559 case DW_FORM_GNU_strp_alt:
8285870a 23560 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c6481205
TT
23561 die->attrs[i].as_string ()
23562 ? die->attrs[i].as_string () : "",
23563 die->attrs[i].canonical_string_p () ? "is" : "not");
c906108c
SS
23564 break;
23565 case DW_FORM_flag:
c45bc3f8 23566 if (die->attrs[i].as_boolean ())
d97bc12b 23567 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23568 else
d97bc12b 23569 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23570 break;
2dc7f7b3
TT
23571 case DW_FORM_flag_present:
23572 fprintf_unfiltered (f, "flag: TRUE");
23573 break;
a8329558 23574 case DW_FORM_indirect:
0963b4bd
MS
23575 /* The reader will have reduced the indirect form to
23576 the "base form" so this form should not occur. */
5f48f8f3 23577 fprintf_unfiltered (f,
3e43a32a 23578 "unexpected attribute form: DW_FORM_indirect");
a8329558 23579 break;
1bc397c5 23580 case DW_FORM_sdata:
663c44ac
JK
23581 case DW_FORM_implicit_const:
23582 fprintf_unfiltered (f, "constant: %s",
1bc397c5 23583 plongest (die->attrs[i].as_signed ()));
663c44ac 23584 break;
c906108c 23585 default:
d97bc12b 23586 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23587 die->attrs[i].form);
d97bc12b 23588 break;
c906108c 23589 }
d97bc12b 23590 fprintf_unfiltered (f, "\n");
c906108c
SS
23591 }
23592}
23593
f9aca02d 23594static void
d97bc12b 23595dump_die_for_error (struct die_info *die)
c906108c 23596{
d97bc12b
DE
23597 dump_die_shallow (gdb_stderr, 0, die);
23598}
23599
23600static void
23601dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23602{
23603 int indent = level * 4;
23604
23605 gdb_assert (die != NULL);
23606
23607 if (level >= max_level)
23608 return;
23609
23610 dump_die_shallow (f, indent, die);
23611
23612 if (die->child != NULL)
c906108c 23613 {
d97bc12b
DE
23614 print_spaces (indent, f);
23615 fprintf_unfiltered (f, " Children:");
23616 if (level + 1 < max_level)
23617 {
23618 fprintf_unfiltered (f, "\n");
23619 dump_die_1 (f, level + 1, max_level, die->child);
23620 }
23621 else
23622 {
3e43a32a
MS
23623 fprintf_unfiltered (f,
23624 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23625 }
23626 }
23627
23628 if (die->sibling != NULL && level > 0)
23629 {
23630 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23631 }
23632}
23633
d97bc12b
DE
23634/* This is called from the pdie macro in gdbinit.in.
23635 It's not static so gcc will keep a copy callable from gdb. */
23636
23637void
23638dump_die (struct die_info *die, int max_level)
23639{
23640 dump_die_1 (gdb_stdlog, 0, max_level, die);
23641}
23642
f9aca02d 23643static void
51545339 23644store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23645{
51545339 23646 void **slot;
c906108c 23647
9c541725
PA
23648 slot = htab_find_slot_with_hash (cu->die_hash, die,
23649 to_underlying (die->sect_off),
b64f50a1 23650 INSERT);
51545339
DJ
23651
23652 *slot = die;
c906108c
SS
23653}
23654
348e048f
DE
23655/* Follow reference or signature attribute ATTR of SRC_DIE.
23656 On entry *REF_CU is the CU of SRC_DIE.
23657 On exit *REF_CU is the CU of the result. */
23658
23659static struct die_info *
ff39bb5e 23660follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23661 struct dwarf2_cu **ref_cu)
23662{
23663 struct die_info *die;
23664
cd6c91b4 23665 if (attr->form_is_ref ())
348e048f 23666 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23667 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23668 die = follow_die_sig (src_die, attr, ref_cu);
23669 else
23670 {
23671 dump_die_for_error (src_die);
23672 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 23673 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
23674 }
23675
23676 return die;
03dd20cc
DJ
23677}
23678
5c631832 23679/* Follow reference OFFSET.
673bfd45
DE
23680 On entry *REF_CU is the CU of the source die referencing OFFSET.
23681 On exit *REF_CU is the CU of the result.
23682 Returns NULL if OFFSET is invalid. */
f504f079 23683
f9aca02d 23684static struct die_info *
9c541725 23685follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23686 struct dwarf2_cu **ref_cu)
c906108c 23687{
10b3939b 23688 struct die_info temp_die;
f2f0e013 23689 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 23690 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 23691
348e048f
DE
23692 gdb_assert (cu->per_cu != NULL);
23693
98bfdba5
PA
23694 target_cu = cu;
23695
17e593e9
SM
23696 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23697 "source CU contains target offset: %d",
23698 sect_offset_str (cu->per_cu->sect_off),
23699 sect_offset_str (sect_off),
23700 cu->header.offset_in_cu_p (sect_off));
23701
3019eac3 23702 if (cu->per_cu->is_debug_types)
348e048f
DE
23703 {
23704 /* .debug_types CUs cannot reference anything outside their CU.
23705 If they need to, they have to reference a signatured type via
55f1336d 23706 DW_FORM_ref_sig8. */
4057dfde 23707 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 23708 return NULL;
348e048f 23709 }
36586728 23710 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 23711 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
23712 {
23713 struct dwarf2_per_cu_data *per_cu;
9a619af0 23714
9c541725 23715 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 23716 per_objfile);
03dd20cc 23717
17e593e9
SM
23718 dwarf_read_debug_printf_v ("target CU offset: %s, "
23719 "target CU DIEs loaded: %d",
23720 sect_offset_str (per_cu->sect_off),
23721 per_objfile->get_cu (per_cu) != nullptr);
23722
616c069a
SM
23723 /* If necessary, add it to the queue and load its DIEs.
23724
23725 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23726 it doesn't mean they are currently loaded. Since we require them
23727 to be loaded, we must check for ourselves. */
23728 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23729 || per_objfile->get_cu (per_cu) == nullptr)
4a636814
SM
23730 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23731 false, cu->language);
03dd20cc 23732
976ca316 23733 target_cu = per_objfile->get_cu (per_cu);
616c069a 23734 gdb_assert (target_cu != nullptr);
10b3939b 23735 }
98bfdba5
PA
23736 else if (cu->dies == NULL)
23737 {
23738 /* We're loading full DIEs during partial symbol reading. */
976ca316 23739 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
4a636814
SM
23740 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23741 language_minimal);
98bfdba5 23742 }
c906108c 23743
f2f0e013 23744 *ref_cu = target_cu;
9c541725 23745 temp_die.sect_off = sect_off;
c24bdb02
KS
23746
23747 if (target_cu != cu)
23748 target_cu->ancestor = cu;
23749
9a3c8263 23750 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23751 &temp_die,
23752 to_underlying (sect_off));
5c631832 23753}
10b3939b 23754
5c631832
JK
23755/* Follow reference attribute ATTR of SRC_DIE.
23756 On entry *REF_CU is the CU of SRC_DIE.
23757 On exit *REF_CU is the CU of the result. */
23758
23759static struct die_info *
ff39bb5e 23760follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23761 struct dwarf2_cu **ref_cu)
23762{
0826b30a 23763 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
23764 struct dwarf2_cu *cu = *ref_cu;
23765 struct die_info *die;
23766
9c541725 23767 die = follow_die_offset (sect_off,
36586728
TT
23768 (attr->form == DW_FORM_GNU_ref_alt
23769 || cu->per_cu->is_dwz),
23770 ref_cu);
5c631832 23771 if (!die)
9d8780f0
SM
23772 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23773 "at %s [in module %s]"),
23774 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 23775 objfile_name (cu->per_objfile->objfile));
348e048f 23776
5c631832
JK
23777 return die;
23778}
23779
d4c9a4f8 23780/* See read.h. */
5c631832
JK
23781
23782struct dwarf2_locexpr_baton
9c541725 23783dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 23784 dwarf2_per_cu_data *per_cu,
976ca316 23785 dwarf2_per_objfile *per_objfile,
041d9819
SM
23786 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23787 bool resolve_abstract_p)
5c631832 23788{
5c631832
JK
23789 struct die_info *die;
23790 struct attribute *attr;
23791 struct dwarf2_locexpr_baton retval;
976ca316 23792 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 23793
976ca316 23794 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 23795 if (cu == nullptr)
976ca316 23796 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
23797
23798 if (cu == nullptr)
cc12ce38
DE
23799 {
23800 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23801 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23802 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23803 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23804 }
918dd910 23805
9c541725 23806 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23807 if (!die)
9d8780f0
SM
23808 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23809 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23810
23811 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23812 if (!attr && resolve_abstract_p
976ca316
SM
23813 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23814 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65 23815 {
041d9819 23816 CORE_ADDR pc = get_frame_pc ();
b3b3bada 23817 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 23818 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 23819
3360b6e7 23820 for (const auto &cand_off
976ca316 23821 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 23822 {
3360b6e7
TV
23823 struct dwarf2_cu *cand_cu = cu;
23824 struct die_info *cand
23825 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23826 if (!cand
23827 || !cand->parent
e4a62c65
TV
23828 || cand->parent->tag != DW_TAG_subprogram)
23829 continue;
23830
23831 CORE_ADDR pc_low, pc_high;
23832 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23833 if (pc_low == ((CORE_ADDR) -1))
23834 continue;
23835 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23836 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23837 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23838 continue;
23839
23840 die = cand;
23841 attr = dwarf2_attr (die, DW_AT_location, cu);
23842 break;
23843 }
23844 }
23845
5c631832
JK
23846 if (!attr)
23847 {
e103e986
JK
23848 /* DWARF: "If there is no such attribute, then there is no effect.".
23849 DATA is ignored if SIZE is 0. */
5c631832 23850
e103e986 23851 retval.data = NULL;
5c631832
JK
23852 retval.size = 0;
23853 }
cd6c91b4 23854 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23855 {
23856 struct dwarf2_loclist_baton loclist_baton;
041d9819 23857 CORE_ADDR pc = get_frame_pc ();
8cf6f0b1
TT
23858 size_t size;
23859
23860 fill_in_loclist_baton (cu, &loclist_baton, attr);
23861
23862 retval.data = dwarf2_find_location_expression (&loclist_baton,
23863 &size, pc);
23864 retval.size = size;
23865 }
5c631832
JK
23866 else
23867 {
4fc6c0d5 23868 if (!attr->form_is_block ())
9d8780f0 23869 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23870 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23871 sect_offset_str (sect_off), objfile_name (objfile));
5c631832 23872
9d2246fc
TT
23873 struct dwarf_block *block = attr->as_block ();
23874 retval.data = block->data;
23875 retval.size = block->size;
5c631832 23876 }
976ca316 23877 retval.per_objfile = per_objfile;
5c631832 23878 retval.per_cu = cu->per_cu;
918dd910 23879
976ca316 23880 per_objfile->age_comp_units ();
918dd910 23881
5c631832 23882 return retval;
348e048f
DE
23883}
23884
d4c9a4f8 23885/* See read.h. */
8b9737bf
TT
23886
23887struct dwarf2_locexpr_baton
23888dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 23889 dwarf2_per_cu_data *per_cu,
14095eb3 23890 dwarf2_per_objfile *per_objfile,
041d9819 23891 gdb::function_view<CORE_ADDR ()> get_frame_pc)
8b9737bf 23892{
9c541725 23893 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23894
14095eb3 23895 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
041d9819 23896 get_frame_pc);
8b9737bf
TT
23897}
23898
b6807d98
TT
23899/* Write a constant of a given type as target-ordered bytes into
23900 OBSTACK. */
23901
23902static const gdb_byte *
23903write_constant_as_bytes (struct obstack *obstack,
23904 enum bfd_endian byte_order,
23905 struct type *type,
23906 ULONGEST value,
23907 LONGEST *len)
23908{
23909 gdb_byte *result;
23910
23911 *len = TYPE_LENGTH (type);
224c3ddb 23912 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23913 store_unsigned_integer (result, *len, byte_order, value);
23914
23915 return result;
23916}
23917
d4c9a4f8 23918/* See read.h. */
b6807d98
TT
23919
23920const gdb_byte *
9c541725 23921dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 23922 dwarf2_per_cu_data *per_cu,
14095eb3 23923 dwarf2_per_objfile *per_objfile,
d4c9a4f8 23924 obstack *obstack,
b6807d98
TT
23925 LONGEST *len)
23926{
b6807d98
TT
23927 struct die_info *die;
23928 struct attribute *attr;
23929 const gdb_byte *result = NULL;
23930 struct type *type;
23931 LONGEST value;
23932 enum bfd_endian byte_order;
14095eb3 23933 struct objfile *objfile = per_objfile->objfile;
b6807d98 23934
7188ed02 23935 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23936 if (cu == nullptr)
23937 cu = load_cu (per_cu, per_objfile, false);
23938
23939 if (cu == nullptr)
cc12ce38
DE
23940 {
23941 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23942 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23943 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23944 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23945 }
b6807d98 23946
9c541725 23947 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23948 if (!die)
9d8780f0
SM
23949 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23950 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23951
23952 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23953 if (attr == NULL)
23954 return NULL;
23955
e3b94546 23956 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23957 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23958
23959 switch (attr->form)
23960 {
23961 case DW_FORM_addr:
336d760d 23962 case DW_FORM_addrx:
b6807d98
TT
23963 case DW_FORM_GNU_addr_index:
23964 {
23965 gdb_byte *tem;
23966
23967 *len = cu->header.addr_size;
224c3ddb 23968 tem = (gdb_byte *) obstack_alloc (obstack, *len);
36d378cf 23969 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
b6807d98
TT
23970 result = tem;
23971 }
23972 break;
23973 case DW_FORM_string:
23974 case DW_FORM_strp:
cf532bd1 23975 case DW_FORM_strx:
b6807d98
TT
23976 case DW_FORM_GNU_str_index:
23977 case DW_FORM_GNU_strp_alt:
c6481205 23978 /* The string is already allocated on the objfile obstack, point
b6807d98 23979 directly to it. */
2c830f54
TT
23980 {
23981 const char *attr_name = attr->as_string ();
23982 result = (const gdb_byte *) attr_name;
23983 *len = strlen (attr_name);
23984 }
b6807d98
TT
23985 break;
23986 case DW_FORM_block1:
23987 case DW_FORM_block2:
23988 case DW_FORM_block4:
23989 case DW_FORM_block:
23990 case DW_FORM_exprloc:
0224619f 23991 case DW_FORM_data16:
9d2246fc
TT
23992 {
23993 struct dwarf_block *block = attr->as_block ();
23994 result = block->data;
23995 *len = block->size;
23996 }
b6807d98
TT
23997 break;
23998
23999 /* The DW_AT_const_value attributes are supposed to carry the
24000 symbol's value "represented as it would be on the target
24001 architecture." By the time we get here, it's already been
24002 converted to host endianness, so we just need to sign- or
24003 zero-extend it as appropriate. */
24004 case DW_FORM_data1:
24005 type = die_type (die, cu);
24006 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
24007 if (result == NULL)
24008 result = write_constant_as_bytes (obstack, byte_order,
24009 type, value, len);
24010 break;
24011 case DW_FORM_data2:
24012 type = die_type (die, cu);
24013 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
24014 if (result == NULL)
24015 result = write_constant_as_bytes (obstack, byte_order,
24016 type, value, len);
24017 break;
24018 case DW_FORM_data4:
24019 type = die_type (die, cu);
24020 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
24021 if (result == NULL)
24022 result = write_constant_as_bytes (obstack, byte_order,
24023 type, value, len);
24024 break;
24025 case DW_FORM_data8:
24026 type = die_type (die, cu);
24027 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
24028 if (result == NULL)
24029 result = write_constant_as_bytes (obstack, byte_order,
24030 type, value, len);
24031 break;
24032
24033 case DW_FORM_sdata:
663c44ac 24034 case DW_FORM_implicit_const:
b6807d98
TT
24035 type = die_type (die, cu);
24036 result = write_constant_as_bytes (obstack, byte_order,
1bc397c5 24037 type, attr->as_signed (), len);
b6807d98
TT
24038 break;
24039
24040 case DW_FORM_udata:
24041 type = die_type (die, cu);
24042 result = write_constant_as_bytes (obstack, byte_order,
529908cb 24043 type, attr->as_unsigned (), len);
b6807d98
TT
24044 break;
24045
24046 default:
b98664d3 24047 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
24048 dwarf_form_name (attr->form));
24049 break;
24050 }
24051
24052 return result;
24053}
24054
d4c9a4f8 24055/* See read.h. */
7942e96e
AA
24056
24057struct type *
9c541725 24058dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
24059 dwarf2_per_cu_data *per_cu,
24060 dwarf2_per_objfile *per_objfile)
7942e96e 24061{
7942e96e
AA
24062 struct die_info *die;
24063
7188ed02 24064 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
24065 if (cu == nullptr)
24066 cu = load_cu (per_cu, per_objfile, false);
24067
24068 if (cu == nullptr)
24069 return nullptr;
7942e96e 24070
9c541725 24071 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
24072 if (!die)
24073 return NULL;
24074
24075 return die_type (die, cu);
24076}
24077
8cb5117c 24078/* See read.h. */
8a9b8146
TT
24079
24080struct type *
b64f50a1 24081dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
24082 dwarf2_per_cu_data *per_cu,
24083 dwarf2_per_objfile *per_objfile)
8a9b8146 24084{
9c541725 24085 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 24086 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
24087}
24088
ac9ec31b 24089/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 24090 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
24091 On exit *REF_CU is the CU of the result.
24092 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
24093
24094static struct die_info *
ac9ec31b
DE
24095follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
24096 struct dwarf2_cu **ref_cu)
348e048f 24097{
348e048f 24098 struct die_info temp_die;
c24bdb02 24099 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 24100 struct die_info *die;
976ca316 24101 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 24102
348e048f 24103
ac9ec31b
DE
24104 /* While it might be nice to assert sig_type->type == NULL here,
24105 we can get here for DW_AT_imported_declaration where we need
24106 the DIE not the type. */
348e048f 24107
616c069a 24108 /* If necessary, add it to the queue and load its DIEs.
348e048f 24109
616c069a
SM
24110 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
24111 it doesn't mean they are currently loaded. Since we require them
24112 to be loaded, we must check for ourselves. */
976ca316 24113 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
616c069a
SM
24114 language_minimal)
24115 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
976ca316 24116 read_signatured_type (sig_type, per_objfile);
348e048f 24117
976ca316 24118 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
69d751e3 24119 gdb_assert (sig_cu != NULL);
9c541725
PA
24120 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
24121 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 24122 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 24123 to_underlying (temp_die.sect_off));
348e048f
DE
24124 if (die)
24125 {
796a7ff8
DE
24126 /* For .gdb_index version 7 keep track of included TUs.
24127 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
24128 if (per_objfile->per_bfd->index_table != NULL
24129 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 24130 {
ae640021 24131 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
24132 }
24133
348e048f 24134 *ref_cu = sig_cu;
c24bdb02
KS
24135 if (sig_cu != cu)
24136 sig_cu->ancestor = cu;
24137
348e048f
DE
24138 return die;
24139 }
24140
ac9ec31b
DE
24141 return NULL;
24142}
24143
24144/* Follow signatured type referenced by ATTR in SRC_DIE.
24145 On entry *REF_CU is the CU of SRC_DIE.
24146 On exit *REF_CU is the CU of the result.
24147 The result is the DIE of the type.
24148 If the referenced type cannot be found an error is thrown. */
24149
24150static struct die_info *
ff39bb5e 24151follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
24152 struct dwarf2_cu **ref_cu)
24153{
630ed6b9 24154 ULONGEST signature = attr->as_signature ();
ac9ec31b
DE
24155 struct signatured_type *sig_type;
24156 struct die_info *die;
24157
24158 gdb_assert (attr->form == DW_FORM_ref_sig8);
24159
a2ce51a0 24160 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
24161 /* sig_type will be NULL if the signatured type is missing from
24162 the debug info. */
24163 if (sig_type == NULL)
24164 {
24165 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
dda83cd7
SM
24166 " from DIE at %s [in module %s]"),
24167 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 24168 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
24169 }
24170
24171 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24172 if (die == NULL)
24173 {
24174 dump_die_for_error (src_die);
24175 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24176 " from DIE at %s [in module %s]"),
24177 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 24178 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
24179 }
24180
24181 return die;
24182}
24183
24184/* Get the type specified by SIGNATURE referenced in DIE/CU,
24185 reading in and processing the type unit if necessary. */
24186
24187static struct type *
24188get_signatured_type (struct die_info *die, ULONGEST signature,
24189 struct dwarf2_cu *cu)
24190{
976ca316 24191 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
24192 struct signatured_type *sig_type;
24193 struct dwarf2_cu *type_cu;
24194 struct die_info *type_die;
24195 struct type *type;
24196
a2ce51a0 24197 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
24198 /* sig_type will be NULL if the signatured type is missing from
24199 the debug info. */
24200 if (sig_type == NULL)
24201 {
b98664d3 24202 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
24203 " from DIE at %s [in module %s]"),
24204 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24205 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24206 return build_error_marker_type (cu, die);
24207 }
24208
24209 /* If we already know the type we're done. */
976ca316 24210 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
24211 if (type != nullptr)
24212 return type;
ac9ec31b
DE
24213
24214 type_cu = cu;
24215 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24216 if (type_die != NULL)
24217 {
24218 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24219 is created. This is important, for example, because for c++ classes
24220 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24221 type = read_type_die (type_die, type_cu);
24222 if (type == NULL)
24223 {
b98664d3 24224 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
24225 " referenced from DIE at %s [in module %s]"),
24226 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24227 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24228 type = build_error_marker_type (cu, die);
24229 }
24230 }
24231 else
24232 {
b98664d3 24233 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24234 " from DIE at %s [in module %s]"),
24235 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24236 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24237 type = build_error_marker_type (cu, die);
24238 }
e286671b 24239
976ca316 24240 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
24241
24242 return type;
24243}
24244
24245/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24246 reading in and processing the type unit if necessary. */
24247
24248static struct type *
ff39bb5e 24249get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 24250 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
24251{
24252 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 24253 if (attr->form_is_ref ())
ac9ec31b
DE
24254 {
24255 struct dwarf2_cu *type_cu = cu;
24256 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24257
24258 return read_type_die (type_die, type_cu);
24259 }
24260 else if (attr->form == DW_FORM_ref_sig8)
24261 {
630ed6b9 24262 return get_signatured_type (die, attr->as_signature (), cu);
ac9ec31b
DE
24263 }
24264 else
24265 {
976ca316 24266 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24267
b98664d3 24268 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
24269 " at %s [in module %s]"),
24270 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 24271 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24272 return build_error_marker_type (cu, die);
24273 }
348e048f
DE
24274}
24275
e5fe5e75 24276/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
24277
24278static void
ab432490
SM
24279load_full_type_unit (dwarf2_per_cu_data *per_cu,
24280 dwarf2_per_objfile *per_objfile)
348e048f 24281{
52dc124a 24282 struct signatured_type *sig_type;
348e048f 24283
f4dc4d17 24284 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 24285 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 24286
6721b2ec
DE
24287 /* We have the per_cu, but we need the signatured_type.
24288 Fortunately this is an easy translation. */
24289 gdb_assert (per_cu->is_debug_types);
24290 sig_type = (struct signatured_type *) per_cu;
348e048f 24291
7188ed02 24292 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24293
ab432490 24294 read_signatured_type (sig_type, per_objfile);
348e048f 24295
7188ed02 24296 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
24297}
24298
3019eac3
DE
24299/* Read in a signatured type and build its CU and DIEs.
24300 If the type is a stub for the real type in a DWO file,
24301 read in the real type from the DWO file as well. */
dee91e82
DE
24302
24303static void
ab432490
SM
24304read_signatured_type (signatured_type *sig_type,
24305 dwarf2_per_objfile *per_objfile)
dee91e82
DE
24306{
24307 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24308
3019eac3 24309 gdb_assert (per_cu->is_debug_types);
7188ed02 24310 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24311
2e671100 24312 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
24313
24314 if (!reader.dummy_p)
24315 {
24316 struct dwarf2_cu *cu = reader.cu;
24317 const gdb_byte *info_ptr = reader.info_ptr;
24318
24319 gdb_assert (cu->die_hash == NULL);
24320 cu->die_hash =
24321 htab_create_alloc_ex (cu->header.length / 12,
24322 die_hash,
24323 die_eq,
24324 NULL,
24325 &cu->comp_unit_obstack,
24326 hashtab_obstack_allocate,
24327 dummy_obstack_deallocate);
24328
3e225074 24329 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
24330 reader.comp_unit_die->child
24331 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24332 reader.comp_unit_die);
24333 cu->dies = reader.comp_unit_die;
24334 /* comp_unit_die is not stored in die_hash, no need. */
24335
24336 /* We try not to read any attributes in this function, because
24337 not all CUs needed for references have been loaded yet, and
24338 symbol table processing isn't initialized. But we have to
24339 set the CU language, or we won't be able to build types
24340 correctly. Similarly, if we do not read the producer, we can
24341 not apply producer-specific interpretation. */
24342 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
24343
24344 reader.keep ();
c0ab21c2
TT
24345 }
24346
7ee85ab1 24347 sig_type->per_cu.tu_read = 1;
c906108c
SS
24348}
24349
c906108c
SS
24350/* Decode simple location descriptions.
24351 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
24352 the location and return the value. If COMPUTED is non-null, it is
24353 set to true to indicate that decoding was successful, and false
24354 otherwise. If COMPUTED is null, then this function may emit a
24355 complaint. */
c906108c
SS
24356
24357static CORE_ADDR
7d79de9a 24358decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 24359{
5e22e966 24360 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
24361 size_t i;
24362 size_t size = blk->size;
d521ce57 24363 const gdb_byte *data = blk->data;
21ae7a4d
JK
24364 CORE_ADDR stack[64];
24365 int stacki;
24366 unsigned int bytes_read, unsnd;
24367 gdb_byte op;
c906108c 24368
7d79de9a
TT
24369 if (computed != nullptr)
24370 *computed = false;
24371
21ae7a4d
JK
24372 i = 0;
24373 stacki = 0;
24374 stack[stacki] = 0;
24375 stack[++stacki] = 0;
24376
24377 while (i < size)
24378 {
24379 op = data[i++];
24380 switch (op)
24381 {
24382 case DW_OP_lit0:
24383 case DW_OP_lit1:
24384 case DW_OP_lit2:
24385 case DW_OP_lit3:
24386 case DW_OP_lit4:
24387 case DW_OP_lit5:
24388 case DW_OP_lit6:
24389 case DW_OP_lit7:
24390 case DW_OP_lit8:
24391 case DW_OP_lit9:
24392 case DW_OP_lit10:
24393 case DW_OP_lit11:
24394 case DW_OP_lit12:
24395 case DW_OP_lit13:
24396 case DW_OP_lit14:
24397 case DW_OP_lit15:
24398 case DW_OP_lit16:
24399 case DW_OP_lit17:
24400 case DW_OP_lit18:
24401 case DW_OP_lit19:
24402 case DW_OP_lit20:
24403 case DW_OP_lit21:
24404 case DW_OP_lit22:
24405 case DW_OP_lit23:
24406 case DW_OP_lit24:
24407 case DW_OP_lit25:
24408 case DW_OP_lit26:
24409 case DW_OP_lit27:
24410 case DW_OP_lit28:
24411 case DW_OP_lit29:
24412 case DW_OP_lit30:
24413 case DW_OP_lit31:
24414 stack[++stacki] = op - DW_OP_lit0;
24415 break;
f1bea926 24416
21ae7a4d
JK
24417 case DW_OP_reg0:
24418 case DW_OP_reg1:
24419 case DW_OP_reg2:
24420 case DW_OP_reg3:
24421 case DW_OP_reg4:
24422 case DW_OP_reg5:
24423 case DW_OP_reg6:
24424 case DW_OP_reg7:
24425 case DW_OP_reg8:
24426 case DW_OP_reg9:
24427 case DW_OP_reg10:
24428 case DW_OP_reg11:
24429 case DW_OP_reg12:
24430 case DW_OP_reg13:
24431 case DW_OP_reg14:
24432 case DW_OP_reg15:
24433 case DW_OP_reg16:
24434 case DW_OP_reg17:
24435 case DW_OP_reg18:
24436 case DW_OP_reg19:
24437 case DW_OP_reg20:
24438 case DW_OP_reg21:
24439 case DW_OP_reg22:
24440 case DW_OP_reg23:
24441 case DW_OP_reg24:
24442 case DW_OP_reg25:
24443 case DW_OP_reg26:
24444 case DW_OP_reg27:
24445 case DW_OP_reg28:
24446 case DW_OP_reg29:
24447 case DW_OP_reg30:
24448 case DW_OP_reg31:
24449 stack[++stacki] = op - DW_OP_reg0;
24450 if (i < size)
7d79de9a
TT
24451 {
24452 if (computed == nullptr)
24453 dwarf2_complex_location_expr_complaint ();
24454 else
24455 return 0;
24456 }
21ae7a4d 24457 break;
c906108c 24458
21ae7a4d
JK
24459 case DW_OP_regx:
24460 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24461 i += bytes_read;
24462 stack[++stacki] = unsnd;
24463 if (i < size)
7d79de9a
TT
24464 {
24465 if (computed == nullptr)
24466 dwarf2_complex_location_expr_complaint ();
24467 else
24468 return 0;
24469 }
21ae7a4d 24470 break;
c906108c 24471
21ae7a4d 24472 case DW_OP_addr:
c8a7a66f
TT
24473 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24474 &bytes_read);
21ae7a4d
JK
24475 i += bytes_read;
24476 break;
d53d4ac5 24477
21ae7a4d
JK
24478 case DW_OP_const1u:
24479 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24480 i += 1;
24481 break;
24482
24483 case DW_OP_const1s:
24484 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24485 i += 1;
24486 break;
24487
24488 case DW_OP_const2u:
24489 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24490 i += 2;
24491 break;
24492
24493 case DW_OP_const2s:
24494 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24495 i += 2;
24496 break;
d53d4ac5 24497
21ae7a4d
JK
24498 case DW_OP_const4u:
24499 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24500 i += 4;
24501 break;
24502
24503 case DW_OP_const4s:
24504 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24505 i += 4;
24506 break;
24507
585861ea
JK
24508 case DW_OP_const8u:
24509 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24510 i += 8;
24511 break;
24512
21ae7a4d
JK
24513 case DW_OP_constu:
24514 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24515 &bytes_read);
24516 i += bytes_read;
24517 break;
24518
24519 case DW_OP_consts:
24520 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24521 i += bytes_read;
24522 break;
24523
24524 case DW_OP_dup:
24525 stack[stacki + 1] = stack[stacki];
24526 stacki++;
24527 break;
24528
24529 case DW_OP_plus:
24530 stack[stacki - 1] += stack[stacki];
24531 stacki--;
24532 break;
24533
24534 case DW_OP_plus_uconst:
24535 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24536 &bytes_read);
24537 i += bytes_read;
24538 break;
24539
24540 case DW_OP_minus:
24541 stack[stacki - 1] -= stack[stacki];
24542 stacki--;
24543 break;
24544
24545 case DW_OP_deref:
24546 /* If we're not the last op, then we definitely can't encode
24547 this using GDB's address_class enum. This is valid for partial
24548 global symbols, although the variable's address will be bogus
24549 in the psymtab. */
24550 if (i < size)
7d79de9a
TT
24551 {
24552 if (computed == nullptr)
24553 dwarf2_complex_location_expr_complaint ();
24554 else
24555 return 0;
24556 }
21ae7a4d
JK
24557 break;
24558
dda83cd7 24559 case DW_OP_GNU_push_tls_address:
4aa4e28b 24560 case DW_OP_form_tls_address:
21ae7a4d
JK
24561 /* The top of the stack has the offset from the beginning
24562 of the thread control block at which the variable is located. */
24563 /* Nothing should follow this operator, so the top of stack would
24564 be returned. */
24565 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24566 address will be bogus in the psymtab. Make it always at least
24567 non-zero to not look as a variable garbage collected by linker
24568 which have DW_OP_addr 0. */
21ae7a4d 24569 if (i < size)
7d79de9a
TT
24570 {
24571 if (computed == nullptr)
24572 dwarf2_complex_location_expr_complaint ();
24573 else
24574 return 0;
24575 }
585861ea 24576 stack[stacki]++;
dda83cd7 24577 break;
21ae7a4d
JK
24578
24579 case DW_OP_GNU_uninit:
7d79de9a
TT
24580 if (computed != nullptr)
24581 return 0;
21ae7a4d
JK
24582 break;
24583
336d760d 24584 case DW_OP_addrx:
3019eac3 24585 case DW_OP_GNU_addr_index:
49f6c839 24586 case DW_OP_GNU_const_index:
3019eac3
DE
24587 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24588 &bytes_read);
24589 i += bytes_read;
24590 break;
24591
21ae7a4d 24592 default:
7d79de9a
TT
24593 if (computed == nullptr)
24594 {
24595 const char *name = get_DW_OP_name (op);
21ae7a4d 24596
7d79de9a
TT
24597 if (name)
24598 complaint (_("unsupported stack op: '%s'"),
24599 name);
24600 else
24601 complaint (_("unsupported stack op: '%02x'"),
24602 op);
24603 }
21ae7a4d
JK
24604
24605 return (stack[stacki]);
d53d4ac5 24606 }
3c6e0cb3 24607
21ae7a4d 24608 /* Enforce maximum stack depth of SIZE-1 to avoid writing
dda83cd7 24609 outside of the allocated space. Also enforce minimum>0. */
21ae7a4d
JK
24610 if (stacki >= ARRAY_SIZE (stack) - 1)
24611 {
7d79de9a
TT
24612 if (computed == nullptr)
24613 complaint (_("location description stack overflow"));
21ae7a4d
JK
24614 return 0;
24615 }
24616
24617 if (stacki <= 0)
24618 {
7d79de9a
TT
24619 if (computed == nullptr)
24620 complaint (_("location description stack underflow"));
21ae7a4d
JK
24621 return 0;
24622 }
24623 }
7d79de9a
TT
24624
24625 if (computed != nullptr)
24626 *computed = true;
21ae7a4d 24627 return (stack[stacki]);
c906108c
SS
24628}
24629
24630/* memory allocation interface */
24631
c906108c 24632static struct dwarf_block *
7b5a2f43 24633dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24634{
8d749320 24635 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24636}
24637
c906108c 24638static struct die_info *
b60c80d6 24639dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24640{
24641 struct die_info *die;
b60c80d6
DJ
24642 size_t size = sizeof (struct die_info);
24643
24644 if (num_attrs > 1)
24645 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24646
b60c80d6 24647 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24648 memset (die, 0, sizeof (struct die_info));
24649 return (die);
24650}
2e276125
JB
24651
24652\f
a036ba48 24653
c90ec28a 24654/* Macro support. */
cf2c3c16 24655
9eac9650
TT
24656/* An overload of dwarf_decode_macros that finds the correct section
24657 and ensures it is read in before calling the other overload. */
24658
24659static void
24660dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24661 int section_is_gnu)
24662{
976ca316
SM
24663 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24664 struct objfile *objfile = per_objfile->objfile;
5a0e026f 24665 const struct line_header *lh = cu->line_header;
9eac9650
TT
24666 unsigned int offset_size = cu->header.offset_size;
24667 struct dwarf2_section_info *section;
24668 const char *section_name;
24669
24670 if (cu->dwo_unit != nullptr)
24671 {
24672 if (section_is_gnu)
24673 {
24674 section = &cu->dwo_unit->dwo_file->sections.macro;
24675 section_name = ".debug_macro.dwo";
24676 }
24677 else
24678 {
24679 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24680 section_name = ".debug_macinfo.dwo";
24681 }
24682 }
24683 else
24684 {
24685 if (section_is_gnu)
24686 {
976ca316 24687 section = &per_objfile->per_bfd->macro;
9eac9650
TT
24688 section_name = ".debug_macro";
24689 }
24690 else
24691 {
976ca316 24692 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
24693 section_name = ".debug_macinfo";
24694 }
24695 }
24696
24697 section->read (objfile);
24698 if (section->buffer == nullptr)
24699 {
24700 complaint (_("missing %s section"), section_name);
24701 return;
24702 }
24703
24704 buildsym_compunit *builder = cu->get_builder ();
24705
048fde1e 24706 struct dwarf2_section_info *str_offsets_section;
24707 struct dwarf2_section_info *str_section;
24708 ULONGEST str_offsets_base;
24709
24710 if (cu->dwo_unit != nullptr)
24711 {
24712 str_offsets_section = &cu->dwo_unit->dwo_file
24713 ->sections.str_offsets;
24714 str_section = &cu->dwo_unit->dwo_file->sections.str;
24715 str_offsets_base = cu->header.addr_size;
24716 }
24717 else
24718 {
24719 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24720 str_section = &per_objfile->per_bfd->str;
24721 str_offsets_base = *cu->str_offsets_base;
24722 }
24723
976ca316 24724 dwarf_decode_macros (per_objfile, builder, section, lh,
048fde1e 24725 offset_size, offset, str_section, str_offsets_section,
24726 str_offsets_base, section_is_gnu);
9eac9650
TT
24727}
24728
3019eac3
DE
24729/* Return the .debug_loc section to use for CU.
24730 For DWO files use .debug_loc.dwo. */
24731
24732static struct dwarf2_section_info *
24733cu_debug_loc_section (struct dwarf2_cu *cu)
24734{
976ca316 24735 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24736
3019eac3 24737 if (cu->dwo_unit)
43988095
JK
24738 {
24739 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24740
43988095
JK
24741 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24742 }
976ca316
SM
24743 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24744 : &per_objfile->per_bfd->loc);
3019eac3
DE
24745}
24746
d0ce17d8
CT
24747/* Return the .debug_rnglists section to use for CU. */
24748static struct dwarf2_section_info *
24749cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24750{
24751 if (cu->header.version < 5)
24752 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24753 cu->header.version);
24754 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24755
24756 /* Make sure we read the .debug_rnglists section from the file that
24757 contains the DW_AT_ranges attribute we are reading. Normally that
24758 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24759 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24760 program. */
24761 if (cu->dwo_unit != nullptr
24762 && tag != DW_TAG_compile_unit
24763 && tag != DW_TAG_skeleton_unit)
24764 {
24765 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24766
24767 if (sections->rnglists.size > 0)
24768 return &sections->rnglists;
24769 else
24770 error (_(".debug_rnglists section is missing from .dwo file."));
24771 }
24772 return &dwarf2_per_objfile->per_bfd->rnglists;
24773}
24774
8cf6f0b1
TT
24775/* A helper function that fills in a dwarf2_loclist_baton. */
24776
24777static void
24778fill_in_loclist_baton (struct dwarf2_cu *cu,
24779 struct dwarf2_loclist_baton *baton,
ff39bb5e 24780 const struct attribute *attr)
8cf6f0b1 24781{
976ca316 24782 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
24783 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24784
976ca316 24785 section->read (per_objfile->objfile);
8cf6f0b1 24786
976ca316 24787 baton->per_objfile = per_objfile;
8cf6f0b1
TT
24788 baton->per_cu = cu->per_cu;
24789 gdb_assert (baton->per_cu);
24790 /* We don't know how long the location list is, but make sure we
24791 don't run off the edge of the section. */
d4df075e
TT
24792 baton->size = section->size - attr->as_unsigned ();
24793 baton->data = section->buffer + attr->as_unsigned ();
2b24b6e4
TT
24794 if (cu->base_address.has_value ())
24795 baton->base_address = *cu->base_address;
24796 else
24797 baton->base_address = 0;
f664829e 24798 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24799}
24800
4c2df51b 24801static void
ff39bb5e 24802dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24803 struct dwarf2_cu *cu, int is_block)
4c2df51b 24804{
976ca316
SM
24805 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24806 struct objfile *objfile = per_objfile->objfile;
3019eac3 24807 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24808
cd6c91b4 24809 if (attr->form_is_section_offset ()
3019eac3 24810 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24811 the section. If so, fall through to the complaint in the
24812 other branch. */
d4df075e 24813 && attr->as_unsigned () < section->get_size (objfile))
4c2df51b 24814 {
0d53c4c4 24815 struct dwarf2_loclist_baton *baton;
4c2df51b 24816
8d749320 24817 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24818
8cf6f0b1 24819 fill_in_loclist_baton (cu, baton, attr);
be391dca 24820
2b24b6e4 24821 if (!cu->base_address.has_value ())
b98664d3 24822 complaint (_("Location list used without "
3e43a32a 24823 "specifying the CU base address."));
4c2df51b 24824
f1e6e072
TT
24825 SYMBOL_ACLASS_INDEX (sym) = (is_block
24826 ? dwarf2_loclist_block_index
24827 : dwarf2_loclist_index);
0d53c4c4
DJ
24828 SYMBOL_LOCATION_BATON (sym) = baton;
24829 }
24830 else
24831 {
24832 struct dwarf2_locexpr_baton *baton;
24833
8d749320 24834 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 24835 baton->per_objfile = per_objfile;
ae0d2f24
UW
24836 baton->per_cu = cu->per_cu;
24837 gdb_assert (baton->per_cu);
0d53c4c4 24838
4fc6c0d5 24839 if (attr->form_is_block ())
0d53c4c4
DJ
24840 {
24841 /* Note that we're just copying the block's data pointer
24842 here, not the actual data. We're still pointing into the
6502dd73
DJ
24843 info_buffer for SYM's objfile; right now we never release
24844 that buffer, but when we do clean up properly this may
24845 need to change. */
9d2246fc
TT
24846 struct dwarf_block *block = attr->as_block ();
24847 baton->size = block->size;
24848 baton->data = block->data;
0d53c4c4
DJ
24849 }
24850 else
24851 {
24852 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24853 sym->natural_name ());
0d53c4c4 24854 baton->size = 0;
0d53c4c4 24855 }
6e70227d 24856
f1e6e072
TT
24857 SYMBOL_ACLASS_INDEX (sym) = (is_block
24858 ? dwarf2_locexpr_block_index
24859 : dwarf2_locexpr_index);
0d53c4c4
DJ
24860 SYMBOL_LOCATION_BATON (sym) = baton;
24861 }
4c2df51b 24862}
6502dd73 24863
2e6a9f79 24864/* See read.h. */
96408a79 24865
2e6a9f79
SM
24866const comp_unit_head *
24867dwarf2_per_cu_data::get_header () const
96408a79 24868{
2e6a9f79
SM
24869 if (!m_header_read_in)
24870 {
24871 const gdb_byte *info_ptr
24872 = this->section->buffer + to_underlying (this->sect_off);
96408a79 24873
2e6a9f79 24874 memset (&m_header, 0, sizeof (m_header));
96408a79 24875
2e6a9f79
SM
24876 read_comp_unit_head (&m_header, info_ptr, this->section,
24877 rcuh_kind::COMPILE);
a9f172c6
TT
24878
24879 m_header_read_in = true;
2e6a9f79 24880 }
96408a79 24881
2e6a9f79 24882 return &m_header;
96408a79
SA
24883}
24884
09ba997f 24885/* See read.h. */
ae0d2f24 24886
98714339 24887int
09ba997f 24888dwarf2_per_cu_data::addr_size () const
ae0d2f24 24889{
2e6a9f79 24890 return this->get_header ()->addr_size;
ae0d2f24
UW
24891}
24892
09ba997f 24893/* See read.h. */
9eae7c52
TT
24894
24895int
09ba997f 24896dwarf2_per_cu_data::offset_size () const
9eae7c52 24897{
2e6a9f79 24898 return this->get_header ()->offset_size;
96408a79
SA
24899}
24900
09ba997f 24901/* See read.h. */
96408a79
SA
24902
24903int
09ba997f 24904dwarf2_per_cu_data::ref_addr_size () const
96408a79 24905{
2e6a9f79 24906 const comp_unit_head *header = this->get_header ();
96408a79 24907
2e6a9f79
SM
24908 if (header->version == 2)
24909 return header->addr_size;
96408a79 24910 else
2e6a9f79 24911 return header->offset_size;
181cebd4
JK
24912}
24913
09ba997f 24914/* See read.h. */
9aa1f1e3 24915
09ba997f 24916struct type *
293e7e51 24917dwarf2_cu::addr_type () const
9a49df9d 24918{
293e7e51 24919 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
24920 struct type *void_type = objfile_type (objfile)->builtin_void;
24921 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 24922 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
24923
24924 if (TYPE_LENGTH (addr_type) == addr_size)
24925 return addr_type;
24926
c6d940a9 24927 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
9a49df9d
AB
24928 return addr_type;
24929}
24930
22b6cd70
TT
24931/* A helper function for dwarf2_find_containing_comp_unit that returns
24932 the index of the result, and that searches a vector. It will
24933 return a result even if the offset in question does not actually
24934 occur in any CU. This is separate so that it can be unit
24935 tested. */
ae038cb0 24936
22b6cd70
TT
24937static int
24938dwarf2_find_containing_comp_unit
24939 (sect_offset sect_off,
24940 unsigned int offset_in_dwz,
24941 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 24942{
ae038cb0
DJ
24943 int low, high;
24944
ae038cb0 24945 low = 0;
22b6cd70 24946 high = all_comp_units.size () - 1;
ae038cb0
DJ
24947 while (high > low)
24948 {
36586728 24949 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24950 int mid = low + (high - low) / 2;
9a619af0 24951
22b6cd70 24952 mid_cu = all_comp_units[mid];
36586728 24953 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24954 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24955 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24956 high = mid;
24957 else
24958 low = mid + 1;
24959 }
24960 gdb_assert (low == high);
22b6cd70
TT
24961 return low;
24962}
24963
24964/* Locate the .debug_info compilation unit from CU's objfile which contains
24965 the DIE at OFFSET. Raises an error on failure. */
24966
24967static struct dwarf2_per_cu_data *
24968dwarf2_find_containing_comp_unit (sect_offset sect_off,
24969 unsigned int offset_in_dwz,
976ca316 24970 dwarf2_per_objfile *per_objfile)
22b6cd70 24971{
976ca316
SM
24972 int low = dwarf2_find_containing_comp_unit
24973 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24974 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
22b6cd70 24975
45b8ae0c 24976 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24977 {
36586728 24978 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24979 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24980 "offset %s [in module %s]"),
24981 sect_offset_str (sect_off),
976ca316 24982 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 24983
976ca316 24984 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 24985 <= sect_off);
976ca316 24986 return per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
24987 }
24988 else
24989 {
976ca316 24990 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 24991 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24992 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24993 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24994 return this_cu;
24995 }
24996}
24997
22b6cd70
TT
24998#if GDB_SELF_TEST
24999
25000namespace selftests {
25001namespace find_containing_comp_unit {
25002
25003static void
25004run_test ()
25005{
25006 struct dwarf2_per_cu_data one {};
25007 struct dwarf2_per_cu_data two {};
25008 struct dwarf2_per_cu_data three {};
25009 struct dwarf2_per_cu_data four {};
25010
25011 one.length = 5;
25012 two.sect_off = sect_offset (one.length);
25013 two.length = 7;
25014
25015 three.length = 5;
25016 three.is_dwz = 1;
25017 four.sect_off = sect_offset (three.length);
25018 four.length = 7;
25019 four.is_dwz = 1;
25020
25021 std::vector<dwarf2_per_cu_data *> units;
25022 units.push_back (&one);
25023 units.push_back (&two);
25024 units.push_back (&three);
25025 units.push_back (&four);
25026
25027 int result;
25028
25029 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
25030 SELF_CHECK (units[result] == &one);
25031 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
25032 SELF_CHECK (units[result] == &one);
25033 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
25034 SELF_CHECK (units[result] == &two);
25035
25036 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
25037 SELF_CHECK (units[result] == &three);
25038 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
25039 SELF_CHECK (units[result] == &three);
25040 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
25041 SELF_CHECK (units[result] == &four);
25042}
25043
25044}
25045}
25046
25047#endif /* GDB_SELF_TEST */
25048
9e021579 25049/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 25050
9e021579
SM
25051dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
25052 dwarf2_per_objfile *per_objfile)
25053 : per_cu (per_cu),
25054 per_objfile (per_objfile),
9068261f
AB
25055 mark (false),
25056 has_loclist (false),
25057 checked_producer (false),
25058 producer_is_gxx_lt_4_6 (false),
25059 producer_is_gcc_lt_4_3 (false),
eb77c9df 25060 producer_is_icc (false),
9068261f 25061 producer_is_icc_lt_14 (false),
c258c396 25062 producer_is_codewarrior (false),
9068261f 25063 processing_has_namespace_info (false)
93311388 25064{
9816fde3
JK
25065}
25066
25067/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25068
25069static void
95554aad
TT
25070prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25071 enum language pretend_language)
9816fde3
JK
25072{
25073 struct attribute *attr;
25074
25075 /* Set the language we're debugging. */
25076 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 25077 if (attr != nullptr)
529908cb 25078 set_cu_language (attr->constant_value (0), cu);
9816fde3 25079 else
9cded63f 25080 {
95554aad 25081 cu->language = pretend_language;
9cded63f
TT
25082 cu->language_defn = language_def (cu->language);
25083 }
dee91e82 25084
7d45c7c3 25085 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25086}
25087
7188ed02 25088/* See read.h. */
ae038cb0 25089
7188ed02
SM
25090dwarf2_cu *
25091dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 25092{
7188ed02
SM
25093 auto it = m_dwarf2_cus.find (per_cu);
25094 if (it == m_dwarf2_cus.end ())
25095 return nullptr;
ae038cb0 25096
7188ed02
SM
25097 return it->second;
25098}
25099
25100/* See read.h. */
25101
25102void
25103dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
25104{
25105 gdb_assert (this->get_cu (per_cu) == nullptr);
25106
25107 m_dwarf2_cus[per_cu] = cu;
25108}
25109
25110/* See read.h. */
25111
25112void
25113dwarf2_per_objfile::age_comp_units ()
25114{
17e593e9
SM
25115 dwarf_read_debug_printf_v ("running");
25116
08ac5771
SM
25117 /* This is not expected to be called in the middle of CU expansion. There is
25118 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
25119 loaded in memory. Calling age_comp_units while the queue is in use could
25120 make us free the DIEs for a CU that is in the queue and therefore break
25121 that invariant. */
25122 gdb_assert (!this->per_bfd->queue.has_value ());
25123
7188ed02
SM
25124 /* Start by clearing all marks. */
25125 for (auto pair : m_dwarf2_cus)
25126 pair.second->mark = false;
25127
25128 /* Traverse all CUs, mark them and their dependencies if used recently
25129 enough. */
25130 for (auto pair : m_dwarf2_cus)
ae038cb0 25131 {
7188ed02
SM
25132 dwarf2_cu *cu = pair.second;
25133
25134 cu->last_used++;
25135 if (cu->last_used <= dwarf_max_cache_age)
25136 dwarf2_mark (cu);
ae038cb0
DJ
25137 }
25138
7188ed02
SM
25139 /* Delete all CUs still not marked. */
25140 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 25141 {
7188ed02 25142 dwarf2_cu *cu = it->second;
ae038cb0 25143
7188ed02 25144 if (!cu->mark)
ae038cb0 25145 {
17e593e9
SM
25146 dwarf_read_debug_printf_v ("deleting old CU %s",
25147 sect_offset_str (cu->per_cu->sect_off));
7188ed02
SM
25148 delete cu;
25149 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
25150 }
25151 else
7188ed02 25152 it++;
ae038cb0
DJ
25153 }
25154}
25155
7188ed02 25156/* See read.h. */
ae038cb0 25157
7188ed02
SM
25158void
25159dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 25160{
7188ed02
SM
25161 auto it = m_dwarf2_cus.find (per_cu);
25162 if (it == m_dwarf2_cus.end ())
25163 return;
ae038cb0 25164
7188ed02 25165 delete it->second;
ae038cb0 25166
7188ed02
SM
25167 m_dwarf2_cus.erase (it);
25168}
ae038cb0 25169
7188ed02
SM
25170dwarf2_per_objfile::~dwarf2_per_objfile ()
25171{
25172 remove_all_cus ();
ae038cb0
DJ
25173}
25174
dee91e82
DE
25175/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25176 We store these in a hash table separate from the DIEs, and preserve them
25177 when the DIEs are flushed out of cache.
25178
25179 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25180 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25181 or the type may come from a DWO file. Furthermore, while it's more logical
25182 to use per_cu->section+offset, with Fission the section with the data is in
25183 the DWO file but we don't know that section at the point we need it.
25184 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25185 because we can enter the lookup routine, get_die_type_at_offset, from
25186 outside this file, and thus won't necessarily have PER_CU->cu.
25187 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25188
dee91e82 25189struct dwarf2_per_cu_offset_and_type
1c379e20 25190{
dee91e82 25191 const struct dwarf2_per_cu_data *per_cu;
9c541725 25192 sect_offset sect_off;
1c379e20
DJ
25193 struct type *type;
25194};
25195
dee91e82 25196/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25197
25198static hashval_t
dee91e82 25199per_cu_offset_and_type_hash (const void *item)
1c379e20 25200{
9a3c8263
SM
25201 const struct dwarf2_per_cu_offset_and_type *ofs
25202 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25203
9c541725 25204 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25205}
25206
dee91e82 25207/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25208
25209static int
dee91e82 25210per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25211{
9a3c8263
SM
25212 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25213 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25214 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25215 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25216
dee91e82 25217 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25218 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25219}
25220
25221/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25222 table if necessary. For convenience, return TYPE.
25223
25224 The DIEs reading must have careful ordering to:
85102364 25225 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25226 reading current DIE.
25227 * Not trying to dereference contents of still incompletely read in types
25228 while reading in other DIEs.
25229 * Enable referencing still incompletely read in types just by a pointer to
25230 the type without accessing its fields.
25231
25232 Therefore caller should follow these rules:
25233 * Try to fetch any prerequisite types we may need to build this DIE type
25234 before building the type and calling set_die_type.
e71ec853 25235 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25236 possible before fetching more types to complete the current type.
25237 * Make the type as complete as possible before fetching more types. */
1c379e20 25238
f792889a 25239static struct type *
57567375
TT
25240set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25241 bool skip_data_location)
1c379e20 25242{
976ca316 25243 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 25244 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 25245 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
25246 struct attribute *attr;
25247 struct dynamic_prop prop;
1c379e20 25248
b4ba55a1
JB
25249 /* For Ada types, make sure that the gnat-specific data is always
25250 initialized (if not already set). There are a few types where
25251 we should not be doing so, because the type-specific area is
25252 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25253 where the type-specific area is used to store the floatformat).
25254 But this is not a problem, because the gnat-specific information
25255 is actually not needed for these types. */
25256 if (need_gnat_info (cu)
78134374
SM
25257 && type->code () != TYPE_CODE_FUNC
25258 && type->code () != TYPE_CODE_FLT
25259 && type->code () != TYPE_CODE_METHODPTR
25260 && type->code () != TYPE_CODE_MEMBERPTR
25261 && type->code () != TYPE_CODE_METHOD
09584414 25262 && type->code () != TYPE_CODE_FIXED_POINT
b4ba55a1
JB
25263 && !HAVE_GNAT_AUX_INFO (type))
25264 INIT_GNAT_SPECIFIC (type);
25265
3f2f83dd
KB
25266 /* Read DW_AT_allocated and set in type. */
25267 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 25268 if (attr != NULL)
3f2f83dd 25269 {
293e7e51 25270 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25271 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25272 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 25273 }
3f2f83dd
KB
25274
25275 /* Read DW_AT_associated and set in type. */
25276 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 25277 if (attr != NULL)
3f2f83dd 25278 {
293e7e51 25279 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25280 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25281 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 25282 }
3f2f83dd 25283
3cdcd0ce 25284 /* Read DW_AT_data_location and set in type. */
57567375
TT
25285 if (!skip_data_location)
25286 {
25287 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25288 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25289 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25290 }
3cdcd0ce 25291
976ca316
SM
25292 if (per_objfile->die_type_hash == NULL)
25293 per_objfile->die_type_hash
0335378b
TT
25294 = htab_up (htab_create_alloc (127,
25295 per_cu_offset_and_type_hash,
25296 per_cu_offset_and_type_eq,
25297 NULL, xcalloc, xfree));
1c379e20 25298
dee91e82 25299 ofs.per_cu = cu->per_cu;
9c541725 25300 ofs.sect_off = die->sect_off;
1c379e20 25301 ofs.type = type;
dee91e82 25302 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 25303 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 25304 if (*slot)
b98664d3 25305 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25306 sect_offset_str (die->sect_off));
8d749320
SM
25307 *slot = XOBNEW (&objfile->objfile_obstack,
25308 struct dwarf2_per_cu_offset_and_type);
1c379e20 25309 **slot = ofs;
f792889a 25310 return type;
1c379e20
DJ
25311}
25312
9c541725 25313/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25314 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25315
25316static struct type *
9c541725 25317get_die_type_at_offset (sect_offset sect_off,
aa66c379 25318 dwarf2_per_cu_data *per_cu,
976ca316 25319 dwarf2_per_objfile *per_objfile)
1c379e20 25320{
dee91e82 25321 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 25322
976ca316 25323 if (per_objfile->die_type_hash == NULL)
f792889a 25324 return NULL;
1c379e20 25325
dee91e82 25326 ofs.per_cu = per_cu;
9c541725 25327 ofs.sect_off = sect_off;
9a3c8263 25328 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 25329 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
25330 if (slot)
25331 return slot->type;
25332 else
25333 return NULL;
25334}
25335
02142a6c 25336/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25337 or return NULL if DIE does not have a saved type. */
25338
25339static struct type *
25340get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25341{
aa66c379 25342 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
25343}
25344
10b3939b
DJ
25345/* Add a dependence relationship from CU to REF_PER_CU. */
25346
25347static void
25348dwarf2_add_dependence (struct dwarf2_cu *cu,
25349 struct dwarf2_per_cu_data *ref_per_cu)
25350{
25351 void **slot;
25352
25353 if (cu->dependencies == NULL)
25354 cu->dependencies
25355 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25356 NULL, &cu->comp_unit_obstack,
25357 hashtab_obstack_allocate,
25358 dummy_obstack_deallocate);
25359
25360 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25361 if (*slot == NULL)
25362 *slot = ref_per_cu;
25363}
1c379e20 25364
f504f079
DE
25365/* Subroutine of dwarf2_mark to pass to htab_traverse.
25366 Set the mark field in every compilation unit in the
7188ed02
SM
25367 cache that we must keep because we are keeping CU.
25368
25369 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 25370
10b3939b
DJ
25371static int
25372dwarf2_mark_helper (void **slot, void *data)
25373{
7188ed02
SM
25374 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25375 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25376 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
25377
25378 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25379 reading of the chain. As such dependencies remain valid it is not much
25380 useful to track and undo them during QUIT cleanups. */
7188ed02 25381 if (cu == nullptr)
d07ed419
JK
25382 return 1;
25383
7188ed02 25384 if (cu->mark)
10b3939b 25385 return 1;
10b3939b 25386
7188ed02
SM
25387 cu->mark = true;
25388
25389 if (cu->dependencies != nullptr)
25390 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
25391
25392 return 1;
25393}
25394
f504f079
DE
25395/* Set the mark field in CU and in every other compilation unit in the
25396 cache that we must keep because we are keeping CU. */
25397
ae038cb0
DJ
25398static void
25399dwarf2_mark (struct dwarf2_cu *cu)
25400{
25401 if (cu->mark)
25402 return;
7188ed02 25403
9068261f 25404 cu->mark = true;
ae038cb0 25405
7188ed02
SM
25406 if (cu->dependencies != nullptr)
25407 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
25408}
25409
72bf9492
DJ
25410/* Trivial hash function for partial_die_info: the hash value of a DIE
25411 is its offset in .debug_info for this objfile. */
25412
25413static hashval_t
25414partial_die_hash (const void *item)
25415{
9a3c8263
SM
25416 const struct partial_die_info *part_die
25417 = (const struct partial_die_info *) item;
9a619af0 25418
9c541725 25419 return to_underlying (part_die->sect_off);
72bf9492
DJ
25420}
25421
25422/* Trivial comparison function for partial_die_info structures: two DIEs
25423 are equal if they have the same offset. */
25424
25425static int
25426partial_die_eq (const void *item_lhs, const void *item_rhs)
25427{
9a3c8263
SM
25428 const struct partial_die_info *part_die_lhs
25429 = (const struct partial_die_info *) item_lhs;
25430 const struct partial_die_info *part_die_rhs
25431 = (const struct partial_die_info *) item_rhs;
9a619af0 25432
9c541725 25433 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25434}
25435
3c3bb058
AB
25436struct cmd_list_element *set_dwarf_cmdlist;
25437struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 25438
9291a0cd 25439static void
cd4fb1b2
SM
25440show_check_physname (struct ui_file *file, int from_tty,
25441 struct cmd_list_element *c, const char *value)
9291a0cd 25442{
cd4fb1b2
SM
25443 fprintf_filtered (file,
25444 _("Whether to check \"physname\" is %s.\n"),
25445 value);
9291a0cd
TT
25446}
25447
6c265988 25448void _initialize_dwarf2_read ();
cd4fb1b2 25449void
6c265988 25450_initialize_dwarf2_read ()
9291a0cd 25451{
0743fc83 25452 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 25453Set DWARF specific variables.\n\
590042fc 25454Configure DWARF variables such as the cache size."),
0743fc83
TT
25455 &set_dwarf_cmdlist, "maintenance set dwarf ",
25456 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25457
0743fc83 25458 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
25459Show DWARF specific variables.\n\
25460Show DWARF variables such as the cache size."),
0743fc83
TT
25461 &show_dwarf_cmdlist, "maintenance show dwarf ",
25462 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25463
cd4fb1b2
SM
25464 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25465 &dwarf_max_cache_age, _("\
25466Set the upper bound on the age of cached DWARF compilation units."), _("\
25467Show the upper bound on the age of cached DWARF compilation units."), _("\
25468A higher limit means that cached compilation units will be stored\n\
25469in memory longer, and more total memory will be used. Zero disables\n\
25470caching, which can slow down startup."),
25471 NULL,
25472 show_dwarf_max_cache_age,
25473 &set_dwarf_cmdlist,
25474 &show_dwarf_cmdlist);
156942c7 25475
cd4fb1b2
SM
25476 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25477Set debugging of the DWARF reader."), _("\
25478Show debugging of the DWARF reader."), _("\
25479When enabled (non-zero), debugging messages are printed during DWARF\n\
25480reading and symtab expansion. A value of 1 (one) provides basic\n\
25481information. A value greater than 1 provides more verbose information."),
25482 NULL,
25483 NULL,
25484 &setdebuglist, &showdebuglist);
9291a0cd 25485
cd4fb1b2
SM
25486 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25487Set debugging of the DWARF DIE reader."), _("\
25488Show debugging of the DWARF DIE reader."), _("\
25489When enabled (non-zero), DIEs are dumped after they are read in.\n\
25490The value is the maximum depth to print."),
25491 NULL,
25492 NULL,
25493 &setdebuglist, &showdebuglist);
9291a0cd 25494
cd4fb1b2
SM
25495 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25496Set debugging of the dwarf line reader."), _("\
25497Show debugging of the dwarf line reader."), _("\
25498When enabled (non-zero), line number entries are dumped as they are read in.\n\
25499A value of 1 (one) provides basic information.\n\
25500A value greater than 1 provides more verbose information."),
25501 NULL,
25502 NULL,
25503 &setdebuglist, &showdebuglist);
437afbb8 25504
cd4fb1b2
SM
25505 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25506Set cross-checking of \"physname\" code against demangler."), _("\
25507Show cross-checking of \"physname\" code against demangler."), _("\
25508When enabled, GDB's internal \"physname\" code is checked against\n\
25509the demangler."),
25510 NULL, show_check_physname,
25511 &setdebuglist, &showdebuglist);
900e11f9 25512
e615022a
DE
25513 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25514 no_class, &use_deprecated_index_sections, _("\
25515Set whether to use deprecated gdb_index sections."), _("\
25516Show whether to use deprecated gdb_index sections."), _("\
25517When enabled, deprecated .gdb_index sections are used anyway.\n\
25518Normally they are ignored either because of a missing feature or\n\
25519performance issue.\n\
25520Warning: This option must be enabled before gdb reads the file."),
25521 NULL,
25522 NULL,
25523 &setlist, &showlist);
25524
f1e6e072
TT
25525 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25526 &dwarf2_locexpr_funcs);
25527 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25528 &dwarf2_loclist_funcs);
25529
25530 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25531 &dwarf2_block_frame_base_locexpr_funcs);
25532 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25533 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25534
25535#if GDB_SELF_TEST
25536 selftests::register_test ("dw2_expand_symtabs_matching",
25537 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
25538 selftests::register_test ("dwarf2_find_containing_comp_unit",
25539 selftests::find_containing_comp_unit::run_test);
c62446b1 25540#endif
6502dd73 25541}
This page took 6.237065 seconds and 4 git commands to generate.