Simplify DWARF reader initialization
[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
eb36a3eb
TT
1955 doesn't require relocations. */
1956 if (!gdb_bfd_requires_relocations (objfile->obfd))
17ee85fc
TT
1957 {
1958 /* See if one has been created for this BFD yet. */
1959 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1960
1961 if (per_bfd == nullptr)
1962 {
1963 /* No, create it now. */
1964 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1965 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1966 }
1967 }
1968 else
1969 {
1970 /* No sharing possible, create one specifically for this objfile. */
1971 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1972 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1973 }
5989a64e 1974
976ca316 1975 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 1976 }
5bfd760d 1977
976ca316
SM
1978 return (!per_objfile->per_bfd->info.is_virtual
1979 && per_objfile->per_bfd->info.s.section != NULL
1980 && !per_objfile->per_bfd->abbrev.is_virtual
1981 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1982}
1983
330cdd98 1984/* See declaration. */
c906108c 1985
330cdd98 1986void
5989a64e
SM
1987dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1988 const dwarf2_debug_sections &names)
c906108c 1989{
fd361982 1990 flagword aflag = bfd_section_flags (sectp);
251d32d9 1991
dc7650b8
JK
1992 if ((aflag & SEC_HAS_CONTENTS) == 0)
1993 {
1994 }
950b7495
KS
1995 else if (elf_section_data (sectp)->this_hdr.sh_size
1996 > bfd_get_file_size (abfd))
1997 {
1998 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1999 warning (_("Discarding section %s which has a section size (%s"
2000 ") larger than the file size [in module %s]"),
2001 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2002 bfd_get_filename (abfd));
2003 }
fbedd546 2004 else if (names.info.matches (sectp->name))
c906108c 2005 {
330cdd98 2006 this->info.s.section = sectp;
fd361982 2007 this->info.size = bfd_section_size (sectp);
c906108c 2008 }
fbedd546 2009 else if (names.abbrev.matches (sectp->name))
c906108c 2010 {
330cdd98 2011 this->abbrev.s.section = sectp;
fd361982 2012 this->abbrev.size = bfd_section_size (sectp);
c906108c 2013 }
fbedd546 2014 else if (names.line.matches (sectp->name))
c906108c 2015 {
330cdd98 2016 this->line.s.section = sectp;
fd361982 2017 this->line.size = bfd_section_size (sectp);
c906108c 2018 }
fbedd546 2019 else if (names.loc.matches (sectp->name))
c906108c 2020 {
330cdd98 2021 this->loc.s.section = sectp;
fd361982 2022 this->loc.size = bfd_section_size (sectp);
c906108c 2023 }
fbedd546 2024 else if (names.loclists.matches (sectp->name))
43988095 2025 {
330cdd98 2026 this->loclists.s.section = sectp;
fd361982 2027 this->loclists.size = bfd_section_size (sectp);
43988095 2028 }
fbedd546 2029 else if (names.macinfo.matches (sectp->name))
c906108c 2030 {
330cdd98 2031 this->macinfo.s.section = sectp;
fd361982 2032 this->macinfo.size = bfd_section_size (sectp);
c906108c 2033 }
fbedd546 2034 else if (names.macro.matches (sectp->name))
cf2c3c16 2035 {
330cdd98 2036 this->macro.s.section = sectp;
fd361982 2037 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2038 }
fbedd546 2039 else if (names.str.matches (sectp->name))
c906108c 2040 {
330cdd98 2041 this->str.s.section = sectp;
fd361982 2042 this->str.size = bfd_section_size (sectp);
c906108c 2043 }
fbedd546 2044 else if (names.str_offsets.matches (sectp->name))
18a8505e
AT
2045 {
2046 this->str_offsets.s.section = sectp;
2047 this->str_offsets.size = bfd_section_size (sectp);
2048 }
fbedd546 2049 else if (names.line_str.matches (sectp->name))
43988095 2050 {
330cdd98 2051 this->line_str.s.section = sectp;
fd361982 2052 this->line_str.size = bfd_section_size (sectp);
43988095 2053 }
fbedd546 2054 else if (names.addr.matches (sectp->name))
3019eac3 2055 {
330cdd98 2056 this->addr.s.section = sectp;
fd361982 2057 this->addr.size = bfd_section_size (sectp);
3019eac3 2058 }
fbedd546 2059 else if (names.frame.matches (sectp->name))
b6af0555 2060 {
330cdd98 2061 this->frame.s.section = sectp;
fd361982 2062 this->frame.size = bfd_section_size (sectp);
b6af0555 2063 }
fbedd546 2064 else if (names.eh_frame.matches (sectp->name))
b6af0555 2065 {
330cdd98 2066 this->eh_frame.s.section = sectp;
fd361982 2067 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2068 }
fbedd546 2069 else if (names.ranges.matches (sectp->name))
af34e669 2070 {
330cdd98 2071 this->ranges.s.section = sectp;
fd361982 2072 this->ranges.size = bfd_section_size (sectp);
af34e669 2073 }
fbedd546 2074 else if (names.rnglists.matches (sectp->name))
43988095 2075 {
330cdd98 2076 this->rnglists.s.section = sectp;
fd361982 2077 this->rnglists.size = bfd_section_size (sectp);
43988095 2078 }
fbedd546 2079 else if (names.types.matches (sectp->name))
348e048f 2080 {
8b70b953
TT
2081 struct dwarf2_section_info type_section;
2082
2083 memset (&type_section, 0, sizeof (type_section));
049412e3 2084 type_section.s.section = sectp;
fd361982 2085 type_section.size = bfd_section_size (sectp);
8b70b953 2086
fd5866f6 2087 this->types.push_back (type_section);
348e048f 2088 }
fbedd546 2089 else if (names.gdb_index.matches (sectp->name))
9291a0cd 2090 {
330cdd98 2091 this->gdb_index.s.section = sectp;
fd361982 2092 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2093 }
fbedd546 2094 else if (names.debug_names.matches (sectp->name))
927aa2e7
JK
2095 {
2096 this->debug_names.s.section = sectp;
fd361982 2097 this->debug_names.size = bfd_section_size (sectp);
927aa2e7 2098 }
fbedd546 2099 else if (names.debug_aranges.matches (sectp->name))
927aa2e7
JK
2100 {
2101 this->debug_aranges.s.section = sectp;
fd361982 2102 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2103 }
dce234bc 2104
fd361982
AM
2105 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2106 && bfd_section_vma (sectp) == 0)
330cdd98 2107 this->has_section_at_zero = true;
c906108c
SS
2108}
2109
dce234bc 2110/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2111 SECTION_NAME. */
af34e669 2112
dce234bc 2113void
3017a003 2114dwarf2_get_section_info (struct objfile *objfile,
dda83cd7
SM
2115 enum dwarf2_section_enum sect,
2116 asection **sectp, const gdb_byte **bufp,
2117 bfd_size_type *sizep)
dce234bc 2118{
976ca316 2119 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 2120 struct dwarf2_section_info *info;
a3b2a86b
TT
2121
2122 /* We may see an objfile without any DWARF, in which case we just
2123 return nothing. */
976ca316 2124 if (per_objfile == NULL)
a3b2a86b
TT
2125 {
2126 *sectp = NULL;
2127 *bufp = NULL;
2128 *sizep = 0;
2129 return;
2130 }
3017a003
TG
2131 switch (sect)
2132 {
2133 case DWARF2_DEBUG_FRAME:
976ca316 2134 info = &per_objfile->per_bfd->frame;
3017a003
TG
2135 break;
2136 case DWARF2_EH_FRAME:
976ca316 2137 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
2138 break;
2139 default:
2140 gdb_assert_not_reached ("unexpected section");
2141 }
dce234bc 2142
96b79293 2143 info->read (objfile);
dce234bc 2144
96b79293 2145 *sectp = info->get_bfd_section ();
dce234bc
PP
2146 *bufp = info->buffer;
2147 *sizep = info->size;
2148}
2149
9291a0cd 2150\f
39298a5d 2151/* DWARF quick_symbol_functions support. */
7b9f3c50
DE
2152
2153/* TUs can share .debug_line entries, and there can be a lot more TUs than
2154 unique line tables, so we maintain a separate table of all .debug_line
2155 derived entries to support the sharing.
2156 All the quick functions need is the list of file names. We discard the
2157 line_header when we're done and don't need to record it here. */
2158struct quick_file_names
2159{
094b34ac
DE
2160 /* The data used to construct the hash key. */
2161 struct stmt_list_hash hash;
7b9f3c50
DE
2162
2163 /* The number of entries in file_names, real_names. */
2164 unsigned int num_file_names;
2165
2166 /* The file names from the line table, after being run through
2167 file_full_name. */
2168 const char **file_names;
2169
2170 /* The file names from the line table after being run through
2171 gdb_realpath. These are computed lazily. */
2172 const char **real_names;
2173};
2174
2175/* When using the index (and thus not using psymtabs), each CU has an
2176 object of this type. This is used to hold information needed by
2177 the various "quick" methods. */
2178struct dwarf2_per_cu_quick_data
2179{
2180 /* The file table. This can be NULL if there was no file table
2181 or it's currently not read in.
5989a64e 2182 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2183 struct quick_file_names *file_names;
2184
7b9f3c50
DE
2185 /* A temporary mark bit used when iterating over all CUs in
2186 expand_symtabs_matching. */
2187 unsigned int mark : 1;
2188
2189 /* True if we've tried to read the file table and found there isn't one.
2190 There will be no point in trying to read it again next time. */
2191 unsigned int no_file_data : 1;
2192};
2193
edc02ceb
TT
2194/* A subclass of psymbol_functions that arranges to read the DWARF
2195 partial symbols when needed. */
2196struct lazy_dwarf_reader : public psymbol_functions
2197{
2198 using psymbol_functions::psymbol_functions;
2199
2200 bool can_lazily_read_symbols () override
2201 {
2202 return true;
2203 }
2204
2205 void read_partial_symbols (struct objfile *objfile) override
2206 {
2207 if (dwarf2_has_info (objfile, nullptr))
2208 dwarf2_build_psymtabs (objfile, this);
2209 }
2210};
2211
2212static quick_symbol_functions_up
2213make_lazy_dwarf_reader ()
2214{
2215 return quick_symbol_functions_up (new lazy_dwarf_reader);
2216}
2217
39298a5d
TT
2218struct dwarf2_base_index_functions : public quick_symbol_functions
2219{
2220 bool has_symbols (struct objfile *objfile) override;
2221
2222 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2223
2224 void forget_cached_source_info (struct objfile *objfile) override;
2225
2226 bool map_symtabs_matching_filename
2227 (struct objfile *objfile, const char *name, const char *real_path,
2228 gdb::function_view<bool (symtab *)> callback) override;
2229
2230 enum language lookup_global_symbol_language (struct objfile *objfile,
2231 const char *name,
2232 domain_enum domain,
2233 bool *symbol_found_p) override
2234 {
2235 *symbol_found_p = false;
2236 return language_unknown;
2237 }
2238
4829711b 2239 void print_stats (struct objfile *objfile, bool print_bcache) override;
39298a5d
TT
2240
2241 void expand_all_symtabs (struct objfile *objfile) override;
2242
2243 void expand_symtabs_with_fullname (struct objfile *objfile,
2244 const char *fullname) override;
2245
2246 struct compunit_symtab *find_pc_sect_compunit_symtab
2247 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2248 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2249
2250 struct compunit_symtab *find_compunit_symtab_by_address
2251 (struct objfile *objfile, CORE_ADDR address) override
2252 {
2253 return nullptr;
2254 }
2255
2256 void map_symbol_filenames (struct objfile *objfile,
f4655dee
TT
2257 gdb::function_view<symbol_filename_ftype> fun,
2258 bool need_fullname) override;
39298a5d
TT
2259};
2260
2261struct dwarf2_gdb_index : public dwarf2_base_index_functions
2262{
2263 struct compunit_symtab *lookup_symbol (struct objfile *objfile,
2264 block_enum block_index,
2265 const char *name,
2266 domain_enum domain) override;
2267
2268 void dump (struct objfile *objfile) override;
2269
2270 void expand_symtabs_for_function (struct objfile *objfile,
2271 const char *func_name) override;
2272
2273 void map_matching_symbols
2274 (struct objfile *,
2275 const lookup_name_info &lookup_name,
2276 domain_enum domain,
2277 int global,
2278 gdb::function_view<symbol_found_callback_ftype> callback,
2279 symbol_compare_ftype *ordered_compare) override;
2280
2281 void expand_symtabs_matching
2282 (struct objfile *objfile,
2283 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2284 const lookup_name_info *lookup_name,
2285 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2286 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2287 enum search_domain kind) override;
2288};
2289
2290struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2291{
2292 struct compunit_symtab *lookup_symbol (struct objfile *objfile,
2293 block_enum block_index,
2294 const char *name,
2295 domain_enum domain) override;
2296
2297 void dump (struct objfile *objfile) override;
2298
2299 void expand_symtabs_for_function (struct objfile *objfile,
2300 const char *func_name) override;
2301
2302 void map_matching_symbols
2303 (struct objfile *,
2304 const lookup_name_info &lookup_name,
2305 domain_enum domain,
2306 int global,
2307 gdb::function_view<symbol_found_callback_ftype> callback,
2308 symbol_compare_ftype *ordered_compare) override;
2309
2310 void expand_symtabs_matching
2311 (struct objfile *objfile,
2312 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2313 const lookup_name_info *lookup_name,
2314 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2315 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2316 enum search_domain kind) override;
2317};
2318
edc02ceb 2319static quick_symbol_functions_up
39298a5d
TT
2320make_dwarf_gdb_index ()
2321{
2322 return quick_symbol_functions_up (new dwarf2_gdb_index);
2323}
2324
edc02ceb 2325static quick_symbol_functions_up
39298a5d
TT
2326make_dwarf_debug_names ()
2327{
2328 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2329}
2330
094b34ac
DE
2331/* Utility hash function for a stmt_list_hash. */
2332
2333static hashval_t
2334hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2335{
2336 hashval_t v = 0;
2337
2338 if (stmt_list_hash->dwo_unit != NULL)
2339 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2340 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2341 return v;
2342}
2343
2344/* Utility equality function for a stmt_list_hash. */
2345
2346static int
2347eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2348 const struct stmt_list_hash *rhs)
2349{
2350 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2351 return 0;
2352 if (lhs->dwo_unit != NULL
2353 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2354 return 0;
2355
9c541725 2356 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2357}
2358
7b9f3c50
DE
2359/* Hash function for a quick_file_names. */
2360
2361static hashval_t
2362hash_file_name_entry (const void *e)
2363{
9a3c8263
SM
2364 const struct quick_file_names *file_data
2365 = (const struct quick_file_names *) e;
7b9f3c50 2366
094b34ac 2367 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2368}
2369
2370/* Equality function for a quick_file_names. */
2371
2372static int
2373eq_file_name_entry (const void *a, const void *b)
2374{
9a3c8263
SM
2375 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2376 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2377
094b34ac 2378 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2379}
2380
2381/* Delete function for a quick_file_names. */
2382
2383static void
2384delete_file_name_entry (void *e)
2385{
9a3c8263 2386 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2387 int i;
2388
2389 for (i = 0; i < file_data->num_file_names; ++i)
2390 {
2391 xfree ((void*) file_data->file_names[i]);
2392 if (file_data->real_names)
2393 xfree ((void*) file_data->real_names[i]);
2394 }
2395
45940949
TT
2396 /* The space for the struct itself lives on the obstack, so we don't
2397 free it here. */
7b9f3c50
DE
2398}
2399
2400/* Create a quick_file_names hash table. */
2401
5895093f 2402static htab_up
7b9f3c50
DE
2403create_quick_file_names_table (unsigned int nr_initial_entries)
2404{
5895093f
TT
2405 return htab_up (htab_create_alloc (nr_initial_entries,
2406 hash_file_name_entry, eq_file_name_entry,
2407 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2408}
9291a0cd 2409
ab432490
SM
2410/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2411 function is unrelated to symtabs, symtab would have to be created afterwards.
2412 You should call age_cached_comp_units after processing the CU. */
918dd910 2413
1b555f17 2414static dwarf2_cu *
ab432490
SM
2415load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2416 bool skip_partial)
918dd910 2417{
3019eac3 2418 if (per_cu->is_debug_types)
ab432490 2419 load_full_type_unit (per_cu, per_objfile);
918dd910 2420 else
4a636814
SM
2421 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2422 skip_partial, language_minimal);
918dd910 2423
7188ed02
SM
2424 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2425 if (cu == nullptr)
1b555f17 2426 return nullptr; /* Dummy CU. */
2dc860c0 2427
7188ed02 2428 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2429
7188ed02 2430 return cu;
918dd910
JK
2431}
2432
1350c3b4 2433/* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2fdf6df6 2434
9291a0cd 2435static void
97a1449a 2436dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2437 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2438{
f4dc4d17
DE
2439 /* Skip type_unit_groups, reading the type units they contain
2440 is handled elsewhere. */
197400e8 2441 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2442 return;
2443
08ac5771
SM
2444 {
2445 /* The destructor of dwarf2_queue_guard frees any entries left on
2446 the queue. After this point we're guaranteed to leave this function
2447 with the dwarf queue empty. */
2448 dwarf2_queue_guard q_guard (per_objfile);
89e63ee4 2449
08ac5771
SM
2450 if (!per_objfile->symtab_set_p (per_cu))
2451 {
2452 queue_comp_unit (per_cu, per_objfile, language_minimal);
2453 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2454
2455 /* If we just loaded a CU from a DWO, and we're working with an index
2456 that may badly handle TUs, load all the TUs in that DWO as well.
2457 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2458 if (!per_cu->is_debug_types
2459 && cu != NULL
2460 && cu->dwo_unit != NULL
2461 && per_objfile->per_bfd->index_table != NULL
2462 && per_objfile->per_bfd->index_table->version <= 7
2463 /* DWP files aren't supported yet. */
2464 && get_dwp_file (per_objfile) == NULL)
2465 queue_and_load_all_dwo_tus (cu);
2466 }
9291a0cd 2467
08ac5771
SM
2468 process_queue (per_objfile);
2469 }
9291a0cd
TT
2470
2471 /* Age the cache, releasing compilation units that have not
2472 been used recently. */
976ca316 2473 per_objfile->age_comp_units ();
9291a0cd
TT
2474}
2475
97a1449a
SM
2476/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2477 the per-objfile for which this symtab is instantiated.
2478
2479 Returns the resulting symbol table. */
2fdf6df6 2480
43f3e411 2481static struct compunit_symtab *
97a1449a 2482dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2483 dwarf2_per_objfile *per_objfile,
97a1449a 2484 bool skip_partial)
9291a0cd 2485{
976ca316 2486 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2487
976ca316 2488 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2489 {
976ca316 2490 free_cached_comp_units freer (per_objfile);
c83dd867 2491 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2492 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2493 process_cu_includes (per_objfile);
9291a0cd 2494 }
f194fefb 2495
976ca316 2496 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2497}
2498
ff4c9fec 2499/* See declaration. */
f4dc4d17 2500
ff4c9fec 2501dwarf2_per_cu_data *
5989a64e 2502dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2503{
b76e467d 2504 if (index >= this->all_comp_units.size ())
ff4c9fec 2505 {
b76e467d 2506 index -= this->all_comp_units.size ();
b2bdb8cf 2507 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2508 return &this->all_type_units[index]->per_cu;
2509 }
f4dc4d17 2510
ff4c9fec
SM
2511 return this->all_comp_units[index];
2512}
f4dc4d17 2513
ff4c9fec 2514/* See declaration. */
2fdf6df6 2515
ff4c9fec 2516dwarf2_per_cu_data *
5989a64e 2517dwarf2_per_bfd::get_cu (int index)
1fd400ff 2518{
b76e467d 2519 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2520
ff4c9fec 2521 return this->all_comp_units[index];
f4dc4d17
DE
2522}
2523
ff4c9fec 2524/* See declaration. */
f4dc4d17 2525
ff4c9fec 2526signatured_type *
5989a64e 2527dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2528{
b2bdb8cf 2529 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2530
ff4c9fec 2531 return this->all_type_units[index];
1fd400ff
TT
2532}
2533
d3473f0c
TT
2534/* See read.h. */
2535
2536dwarf2_per_cu_data *
5989a64e 2537dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2538{
2539 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2540 result->per_bfd = this;
d3473f0c
TT
2541 result->index = m_num_psymtabs++;
2542 return result;
2543}
2544
2545/* See read.h. */
2546
2547signatured_type *
5989a64e 2548dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2549{
2550 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2551 result->per_cu.per_bfd = this;
d3473f0c
TT
2552 result->per_cu.index = m_num_psymtabs++;
2553 return result;
2554}
2555
168c9250 2556/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2557 obstack, and constructed with the specified field values. */
4b514bc8
JK
2558
2559static dwarf2_per_cu_data *
168c9250
SM
2560create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2561 struct dwarf2_section_info *section,
2562 int is_dwz,
2563 sect_offset sect_off, ULONGEST length)
4b514bc8 2564{
168c9250 2565 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2566 the_cu->sect_off = sect_off;
2567 the_cu->length = length;
4b514bc8 2568 the_cu->section = section;
168c9250 2569 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2570 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2571 the_cu->is_dwz = is_dwz;
2572 return the_cu;
2573}
2574
2ec9a5e0
TT
2575/* A helper for create_cus_from_index that handles a given list of
2576 CUs. */
2fdf6df6 2577
74a0d9f6 2578static void
168c9250 2579create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2580 const gdb_byte *cu_list, offset_type n_elements,
2581 struct dwarf2_section_info *section,
b76e467d 2582 int is_dwz)
9291a0cd 2583{
12359b5e 2584 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2585 {
74a0d9f6 2586 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2587
2588 sect_offset sect_off
2589 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2590 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2591 cu_list += 2 * 8;
2592
b76e467d 2593 dwarf2_per_cu_data *per_cu
168c9250
SM
2594 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2595 length);
2596 per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2597 }
9291a0cd
TT
2598}
2599
2ec9a5e0 2600/* Read the CU list from the mapped index, and use it to create all
168c9250 2601 the CU objects for PER_BFD. */
2ec9a5e0 2602
74a0d9f6 2603static void
168c9250 2604create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2605 const gdb_byte *cu_list, offset_type cu_list_elements,
2606 const gdb_byte *dwz_list, offset_type dwz_elements)
2607{
168c9250
SM
2608 gdb_assert (per_bfd->all_comp_units.empty ());
2609 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2610
168c9250
SM
2611 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2612 &per_bfd->info, 0);
2ec9a5e0
TT
2613
2614 if (dwz_elements == 0)
74a0d9f6 2615 return;
2ec9a5e0 2616
168c9250
SM
2617 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2618 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2619 &dwz->info, 1);
2ec9a5e0
TT
2620}
2621
1fd400ff 2622/* Create the signatured type hash table from the index. */
673bfd45 2623
74a0d9f6 2624static void
12359b5e 2625create_signatured_type_table_from_index
168c9250
SM
2626 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2627 const gdb_byte *bytes, offset_type elements)
1fd400ff 2628{
168c9250
SM
2629 gdb_assert (per_bfd->all_type_units.empty ());
2630 per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2631
298e9637 2632 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2633
12359b5e 2634 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2635 {
52dc124a 2636 struct signatured_type *sig_type;
9c541725 2637 ULONGEST signature;
1fd400ff 2638 void **slot;
9c541725 2639 cu_offset type_offset_in_tu;
1fd400ff 2640
74a0d9f6 2641 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2642 sect_offset sect_off
2643 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2644 type_offset_in_tu
2645 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2646 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2647 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2648 bytes += 3 * 8;
2649
168c9250 2650 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2651 sig_type->signature = signature;
9c541725 2652 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2653 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2654 sig_type->per_cu.section = section;
9c541725 2655 sig_type->per_cu.sect_off = sect_off;
52dc124a 2656 sig_type->per_cu.v.quick
168c9250 2657 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2658 struct dwarf2_per_cu_quick_data);
2659
b0b6a987 2660 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2661 *slot = sig_type;
1fd400ff 2662
168c9250 2663 per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2664 }
2665
168c9250 2666 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2667}
2668
927aa2e7
JK
2669/* Create the signatured type hash table from .debug_names. */
2670
2671static void
2672create_signatured_type_table_from_debug_names
976ca316 2673 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2674 const mapped_debug_names &map,
2675 struct dwarf2_section_info *section,
2676 struct dwarf2_section_info *abbrev_section)
2677{
976ca316 2678 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2679
96b79293
TT
2680 section->read (objfile);
2681 abbrev_section->read (objfile);
927aa2e7 2682
976ca316
SM
2683 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2684 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2685
298e9637 2686 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2687
2688 for (uint32_t i = 0; i < map.tu_count; ++i)
2689 {
2690 struct signatured_type *sig_type;
927aa2e7 2691 void **slot;
927aa2e7
JK
2692
2693 sect_offset sect_off
2694 = (sect_offset) (extract_unsigned_integer
2695 (map.tu_table_reordered + i * map.offset_size,
2696 map.offset_size,
2697 map.dwarf5_byte_order));
2698
2699 comp_unit_head cu_header;
976ca316 2700 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2701 abbrev_section,
927aa2e7
JK
2702 section->buffer + to_underlying (sect_off),
2703 rcuh_kind::TYPE);
2704
976ca316 2705 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2706 sig_type->signature = cu_header.signature;
2707 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2708 sig_type->per_cu.is_debug_types = 1;
2709 sig_type->per_cu.section = section;
2710 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2711 sig_type->per_cu.v.quick
976ca316 2712 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2713 struct dwarf2_per_cu_quick_data);
2714
b0b6a987 2715 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2716 *slot = sig_type;
2717
976ca316 2718 per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2719 }
2720
976ca316 2721 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2722}
2723
9291a0cd 2724/* Read the address map data from the mapped index, and use it to
efd7398e 2725 populate the psymtabs_addrmap. */
2fdf6df6 2726
9291a0cd 2727static void
976ca316 2728create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2729 struct mapped_index *index)
9291a0cd 2730{
976ca316 2731 struct objfile *objfile = per_objfile->objfile;
efd7398e 2732 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
08feed99 2733 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2734 const gdb_byte *iter, *end;
9291a0cd 2735 struct addrmap *mutable_map;
9291a0cd
TT
2736 CORE_ADDR baseaddr;
2737
8268c778
PA
2738 auto_obstack temp_obstack;
2739
9291a0cd
TT
2740 mutable_map = addrmap_create_mutable (&temp_obstack);
2741
f00a2de2
PA
2742 iter = index->address_table.data ();
2743 end = iter + index->address_table.size ();
9291a0cd 2744
b3b3bada 2745 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2746
2747 while (iter < end)
2748 {
2749 ULONGEST hi, lo, cu_index;
2750 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2751 iter += 8;
2752 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2753 iter += 8;
2754 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2755 iter += 4;
f652bce2 2756
24a55014 2757 if (lo > hi)
f652bce2 2758 {
b98664d3 2759 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2760 hex_string (lo), hex_string (hi));
24a55014 2761 continue;
f652bce2 2762 }
24a55014 2763
efd7398e 2764 if (cu_index >= per_bfd->all_comp_units.size ())
f652bce2 2765 {
b98664d3 2766 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2767 (unsigned) cu_index);
24a55014 2768 continue;
f652bce2 2769 }
24a55014 2770
79748972
TT
2771 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2772 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2773 addrmap_set_empty (mutable_map, lo, hi - 1,
efd7398e 2774 per_bfd->get_cu (cu_index));
9291a0cd
TT
2775 }
2776
efd7398e
TT
2777 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2778 &per_bfd->obstack);
9291a0cd
TT
2779}
2780
927aa2e7 2781/* Read the address map data from DWARF-5 .debug_aranges, and use it to
efd7398e 2782 populate the psymtabs_addrmap. */
927aa2e7
JK
2783
2784static void
976ca316 2785create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2786 struct dwarf2_section_info *section)
2787{
976ca316 2788 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2789 bfd *abfd = objfile->obfd;
08feed99 2790 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2791 const CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e 2792 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
2793
2794 auto_obstack temp_obstack;
2795 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2796
2797 std::unordered_map<sect_offset,
2798 dwarf2_per_cu_data *,
2799 gdb::hash_enum<sect_offset>>
2800 debug_info_offset_to_per_cu;
bab287cd 2801 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
927aa2e7 2802 {
927aa2e7
JK
2803 const auto insertpair
2804 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2805 if (!insertpair.second)
2806 {
2807 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2808 "debug_info_offset %s, ignoring .debug_aranges."),
2809 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2810 return;
2811 }
2812 }
2813
96b79293 2814 section->read (objfile);
927aa2e7
JK
2815
2816 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2817
2818 const gdb_byte *addr = section->buffer;
2819
2820 while (addr < section->buffer + section->size)
2821 {
2822 const gdb_byte *const entry_addr = addr;
2823 unsigned int bytes_read;
2824
2825 const LONGEST entry_length = read_initial_length (abfd, addr,
2826 &bytes_read);
2827 addr += bytes_read;
2828
2829 const gdb_byte *const entry_end = addr + entry_length;
2830 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2831 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2832 if (addr + entry_length > section->buffer + section->size)
2833 {
47e3f474 2834 warning (_("Section .debug_aranges in %s entry at offset %s "
dda83cd7 2835 "length %s exceeds section length %s, "
927aa2e7 2836 "ignoring .debug_aranges."),
47e3f474
TV
2837 objfile_name (objfile),
2838 plongest (entry_addr - section->buffer),
927aa2e7
JK
2839 plongest (bytes_read + entry_length),
2840 pulongest (section->size));
2841 return;
2842 }
2843
2844 /* The version number. */
2845 const uint16_t version = read_2_bytes (abfd, addr);
2846 addr += 2;
2847 if (version != 2)
2848 {
47e3f474 2849 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2850 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2851 objfile_name (objfile),
2852 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2853 return;
2854 }
2855
2856 const uint64_t debug_info_offset
2857 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2858 addr += offset_size;
2859 const auto per_cu_it
2860 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2861 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2862 {
47e3f474 2863 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2864 "debug_info_offset %s does not exists, "
2865 "ignoring .debug_aranges."),
47e3f474
TV
2866 objfile_name (objfile),
2867 plongest (entry_addr - section->buffer),
927aa2e7
JK
2868 pulongest (debug_info_offset));
2869 return;
2870 }
2871 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2872
2873 const uint8_t address_size = *addr++;
2874 if (address_size < 1 || address_size > 8)
2875 {
47e3f474 2876 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2877 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2878 objfile_name (objfile),
2879 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2880 return;
2881 }
2882
2883 const uint8_t segment_selector_size = *addr++;
2884 if (segment_selector_size != 0)
2885 {
47e3f474 2886 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2887 "segment_selector_size %u is not supported, "
2888 "ignoring .debug_aranges."),
47e3f474
TV
2889 objfile_name (objfile),
2890 plongest (entry_addr - section->buffer),
927aa2e7
JK
2891 segment_selector_size);
2892 return;
2893 }
2894
2895 /* Must pad to an alignment boundary that is twice the address
dda83cd7
SM
2896 size. It is undocumented by the DWARF standard but GCC does
2897 use it. */
927aa2e7
JK
2898 for (size_t padding = ((-(addr - section->buffer))
2899 & (2 * address_size - 1));
dda83cd7 2900 padding > 0; padding--)
927aa2e7
JK
2901 if (*addr++ != 0)
2902 {
47e3f474 2903 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2904 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2905 objfile_name (objfile),
2906 plongest (entry_addr - section->buffer));
927aa2e7
JK
2907 return;
2908 }
2909
2910 for (;;)
2911 {
2912 if (addr + 2 * address_size > entry_end)
2913 {
47e3f474 2914 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2915 "address list is not properly terminated, "
2916 "ignoring .debug_aranges."),
47e3f474
TV
2917 objfile_name (objfile),
2918 plongest (entry_addr - section->buffer));
927aa2e7
JK
2919 return;
2920 }
2921 ULONGEST start = extract_unsigned_integer (addr, address_size,
2922 dwarf5_byte_order);
2923 addr += address_size;
2924 ULONGEST length = extract_unsigned_integer (addr, address_size,
2925 dwarf5_byte_order);
2926 addr += address_size;
2927 if (start == 0 && length == 0)
2928 break;
efd7398e 2929 if (start == 0 && !per_bfd->has_section_at_zero)
927aa2e7
JK
2930 {
2931 /* Symbol was eliminated due to a COMDAT group. */
2932 continue;
2933 }
2934 ULONGEST end = start + length;
79748972
TT
2935 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2936 - baseaddr);
2937 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2938 - baseaddr);
927aa2e7
JK
2939 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2940 }
2941 }
2942
efd7398e
TT
2943 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2944 &per_bfd->obstack);
927aa2e7
JK
2945}
2946
9291a0cd
TT
2947/* Find a slot in the mapped index INDEX for the object named NAME.
2948 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2949 constant pool and return true. If NAME cannot be found, return
2950 false. */
2fdf6df6 2951
109483d9 2952static bool
9291a0cd
TT
2953find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2954 offset_type **vec_out)
2955{
0cf03b49 2956 offset_type hash;
9291a0cd 2957 offset_type slot, step;
559a7a62 2958 int (*cmp) (const char *, const char *);
9291a0cd 2959
791afaa2 2960 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2961 if (current_language->la_language == language_cplus
45280282
IB
2962 || current_language->la_language == language_fortran
2963 || current_language->la_language == language_d)
0cf03b49
JK
2964 {
2965 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2966 not contain any. */
a8719064 2967
72998fb3 2968 if (strchr (name, '(') != NULL)
0cf03b49 2969 {
109483d9 2970 without_params = cp_remove_params (name);
0cf03b49 2971
72998fb3 2972 if (without_params != NULL)
791afaa2 2973 name = without_params.get ();
0cf03b49
JK
2974 }
2975 }
2976
559a7a62 2977 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2978 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2979 simulate our NAME being searched is also lowercased. */
2980 hash = mapped_index_string_hash ((index->version == 4
dda83cd7 2981 && case_sensitivity == case_sensitive_off
559a7a62
JK
2982 ? 5 : index->version),
2983 name);
2984
f00a2de2
PA
2985 slot = hash & (index->symbol_table.size () - 1);
2986 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2987 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2988
2989 for (;;)
2990 {
9291a0cd 2991 const char *str;
f00a2de2
PA
2992
2993 const auto &bucket = index->symbol_table[slot];
2994 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2995 return false;
9291a0cd 2996
f00a2de2 2997 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2998 if (!cmp (name, str))
9291a0cd
TT
2999 {
3000 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3001 + MAYBE_SWAP (bucket.vec));
109483d9 3002 return true;
9291a0cd
TT
3003 }
3004
f00a2de2 3005 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3006 }
3007}
3008
4485a1c1
SM
3009/* A helper function that reads the .gdb_index from BUFFER and fills
3010 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3011 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3012 ok to use deprecated sections.
3013
3014 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3015 out parameters that are filled in with information about the CU and
3016 TU lists in the section.
3017
4485a1c1 3018 Returns true if all went well, false otherwise. */
2fdf6df6 3019
d33bc52e 3020static bool
3810f182 3021read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
3022 bool deprecated_ok,
3023 gdb::array_view<const gdb_byte> buffer,
3024 struct mapped_index *map,
3025 const gdb_byte **cu_list,
3026 offset_type *cu_list_elements,
3027 const gdb_byte **types_list,
3028 offset_type *types_list_elements)
3029{
3030 const gdb_byte *addr = &buffer[0];
82430852 3031
9291a0cd 3032 /* Version check. */
4485a1c1 3033 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3034 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3035 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3036 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3037 indices. */
831adc1f 3038 if (version < 4)
481860b3
GB
3039 {
3040 static int warning_printed = 0;
3041 if (!warning_printed)
3042 {
3043 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3044 filename);
481860b3
GB
3045 warning_printed = 1;
3046 }
3047 return 0;
3048 }
3049 /* Index version 4 uses a different hash function than index version
3050 5 and later.
3051
3052 Versions earlier than 6 did not emit psymbols for inlined
3053 functions. Using these files will cause GDB not to be able to
3054 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3055 indices unless the user has done
3056 "set use-deprecated-index-sections on". */
2ec9a5e0 3057 if (version < 6 && !deprecated_ok)
481860b3
GB
3058 {
3059 static int warning_printed = 0;
3060 if (!warning_printed)
3061 {
e615022a
DE
3062 warning (_("\
3063Skipping deprecated .gdb_index section in %s.\n\
3064Do \"set use-deprecated-index-sections on\" before the file is read\n\
3065to use the section anyway."),
2ec9a5e0 3066 filename);
481860b3
GB
3067 warning_printed = 1;
3068 }
3069 return 0;
3070 }
796a7ff8 3071 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3072 of the TU (for symbols coming from TUs),
3073 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3074 Plus gold-generated indices can have duplicate entries for global symbols,
3075 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3076 These are just performance bugs, and we can't distinguish gdb-generated
3077 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3078
481860b3 3079 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3080 longer backward compatible. */
796a7ff8 3081 if (version > 8)
594e8718 3082 return 0;
9291a0cd 3083
559a7a62 3084 map->version = version;
9291a0cd 3085
4485a1c1 3086 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3087
4485a1c1 3088 int i = 0;
2ec9a5e0
TT
3089 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3090 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3091 / 8);
1fd400ff
TT
3092 ++i;
3093
2ec9a5e0
TT
3094 *types_list = addr + MAYBE_SWAP (metadata[i]);
3095 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3096 - MAYBE_SWAP (metadata[i]))
3097 / 8);
987d643c 3098 ++i;
1fd400ff 3099
f00a2de2
PA
3100 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3101 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3102 map->address_table
3103 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3104 ++i;
3105
f00a2de2
PA
3106 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3107 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3108 map->symbol_table
3109 = gdb::array_view<mapped_index::symbol_table_slot>
3110 ((mapped_index::symbol_table_slot *) symbol_table,
3111 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3112
f00a2de2 3113 ++i;
f9d83a0b 3114 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3115
2ec9a5e0
TT
3116 return 1;
3117}
3118
4485a1c1
SM
3119/* Callback types for dwarf2_read_gdb_index. */
3120
3121typedef gdb::function_view
5989a64e 3122 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3123 get_gdb_index_contents_ftype;
3124typedef gdb::function_view
3125 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3126 get_gdb_index_contents_dwz_ftype;
3127
927aa2e7 3128/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3129 elements of all the CUs and return 1. Otherwise, return 0. */
3130
3131static int
4485a1c1 3132dwarf2_read_gdb_index
976ca316 3133 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
3134 get_gdb_index_contents_ftype get_gdb_index_contents,
3135 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3136{
2ec9a5e0
TT
3137 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3138 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3139 struct dwz_file *dwz;
976ca316 3140 struct objfile *objfile = per_objfile->objfile;
f8c41851 3141 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 3142
4485a1c1 3143 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 3144 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
3145
3146 if (main_index_contents.empty ())
3147 return 0;
3148
3063847f 3149 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3150 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3151 use_deprecated_index_sections,
3152 main_index_contents, map.get (), &cu_list,
3153 &cu_list_elements, &types_list,
3154 &types_list_elements))
2ec9a5e0
TT
3155 return 0;
3156
0fefef59 3157 /* Don't use the index if it's empty. */
3063847f 3158 if (map->symbol_table.empty ())
0fefef59
DE
3159 return 0;
3160
2ec9a5e0
TT
3161 /* If there is a .dwz file, read it so we can get its CU list as
3162 well. */
f8c41851 3163 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 3164 if (dwz != NULL)
2ec9a5e0 3165 {
2ec9a5e0
TT
3166 struct mapped_index dwz_map;
3167 const gdb_byte *dwz_types_ignore;
3168 offset_type dwz_types_elements_ignore;
3169
4485a1c1
SM
3170 gdb::array_view<const gdb_byte> dwz_index_content
3171 = get_gdb_index_contents_dwz (objfile, dwz);
3172
3173 if (dwz_index_content.empty ())
3174 return 0;
3175
3810f182 3176 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3177 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3178 &dwz_list, &dwz_list_elements,
3179 &dwz_types_ignore,
3180 &dwz_types_elements_ignore))
2ec9a5e0
TT
3181 {
3182 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3183 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3184 return 0;
3185 }
3186 }
3187
f8c41851
SM
3188 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3189 dwz_list_elements);
1fd400ff 3190
8b70b953
TT
3191 if (types_list_elements)
3192 {
8b70b953
TT
3193 /* We can only handle a single .debug_types when we have an
3194 index. */
f8c41851 3195 if (per_bfd->types.size () != 1)
8b70b953
TT
3196 return 0;
3197
f8c41851 3198 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 3199
f8c41851 3200 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 3201 types_list_elements);
8b70b953 3202 }
9291a0cd 3203
976ca316 3204 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 3205
f8c41851
SM
3206 per_bfd->index_table = std::move (map);
3207 per_bfd->using_index = 1;
3208 per_bfd->quick_file_names_table =
3209 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3210
9291a0cd
TT
3211 return 1;
3212}
3213
dee91e82 3214/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3215
dee91e82
DE
3216static void
3217dw2_get_file_names_reader (const struct die_reader_specs *reader,
3e225074 3218 struct die_info *comp_unit_die)
9291a0cd 3219{
dee91e82 3220 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3221 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 3222 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 3223 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3224 struct attribute *attr;
7b9f3c50
DE
3225 void **slot;
3226 struct quick_file_names *qfn;
9291a0cd 3227
0186c6a7
DE
3228 gdb_assert (! this_cu->is_debug_types);
3229
07261596
TT
3230 /* Our callers never want to match partial units -- instead they
3231 will match the enclosing full CU. */
3232 if (comp_unit_die->tag == DW_TAG_partial_unit)
3233 {
3234 this_cu->v.quick->no_file_data = 1;
3235 return;
3236 }
3237
0186c6a7 3238 lh_cu = this_cu;
7b9f3c50 3239 slot = NULL;
dee91e82 3240
fff8551c 3241 line_header_up lh;
9c541725 3242 sect_offset line_offset {};
fff8551c 3243
dee91e82 3244 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
d4df075e 3245 if (attr != nullptr && attr->form_is_unsigned ())
9291a0cd 3246 {
7b9f3c50
DE
3247 struct quick_file_names find_entry;
3248
d4df075e 3249 line_offset = (sect_offset) attr->as_unsigned ();
7b9f3c50
DE
3250
3251 /* We may have already read in this line header (TU line header sharing).
3252 If we have we're done. */
094b34ac 3253 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3254 find_entry.hash.line_sect_off = line_offset;
976ca316 3255 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3256 &find_entry, INSERT);
3257 if (*slot != NULL)
3258 {
9a3c8263 3259 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3260 return;
7b9f3c50
DE
3261 }
3262
3019eac3 3263 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3264 }
3265 if (lh == NULL)
3266 {
094b34ac 3267 lh_cu->v.quick->no_file_data = 1;
dee91e82 3268 return;
9291a0cd
TT
3269 }
3270
976ca316 3271 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3272 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3273 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3274 gdb_assert (slot != NULL);
3275 *slot = qfn;
9291a0cd 3276
d721ba37 3277 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3278
aa391654
TT
3279 int offset = 0;
3280 if (strcmp (fnd.name, "<unknown>") != 0)
3281 ++offset;
3282
7ba99d21 3283 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3284 qfn->file_names =
976ca316 3285 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 3286 qfn->num_file_names);
aa391654
TT
3287 if (offset != 0)
3288 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3289 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3290 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3291 fnd.comp_dir).release ();
7b9f3c50 3292 qfn->real_names = NULL;
9291a0cd 3293
094b34ac 3294 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3295}
3296
3297/* A helper for the "quick" functions which attempts to read the line
3298 table for THIS_CU. */
3299
3300static struct quick_file_names *
ab432490
SM
3301dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3302 dwarf2_per_objfile *per_objfile)
dee91e82 3303{
0186c6a7
DE
3304 /* This should never be called for TUs. */
3305 gdb_assert (! this_cu->is_debug_types);
3306 /* Nor type unit groups. */
197400e8 3307 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3308
dee91e82
DE
3309 if (this_cu->v.quick->file_names != NULL)
3310 return this_cu->v.quick->file_names;
3311 /* If we know there is no line data, no point in looking again. */
3312 if (this_cu->v.quick->no_file_data)
3313 return NULL;
3314
ab432490 3315 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3316 if (!reader.dummy_p)
eb5dd737 3317 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
dee91e82
DE
3318
3319 if (this_cu->v.quick->no_file_data)
3320 return NULL;
3321 return this_cu->v.quick->file_names;
9291a0cd
TT
3322}
3323
3324/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3325 real path for a given file name from the line table. */
2fdf6df6 3326
9291a0cd 3327static const char *
976ca316 3328dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3329 struct quick_file_names *qfn, int index)
9291a0cd 3330{
7b9f3c50 3331 if (qfn->real_names == NULL)
976ca316 3332 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3333 qfn->num_file_names, const char *);
9291a0cd 3334
7b9f3c50 3335 if (qfn->real_names[index] == NULL)
14278e1f 3336 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3337
7b9f3c50 3338 return qfn->real_names[index];
9291a0cd
TT
3339}
3340
39298a5d
TT
3341struct symtab *
3342dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
9291a0cd 3343{
976ca316
SM
3344 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3345 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3346 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3347
43f3e411
DE
3348 if (cust == NULL)
3349 return NULL;
ed2dc618 3350
43f3e411 3351 return compunit_primary_filetab (cust);
9291a0cd
TT
3352}
3353
7b9f3c50
DE
3354/* Traversal function for dw2_forget_cached_source_info. */
3355
3356static int
3357dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3358{
7b9f3c50 3359 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3360
7b9f3c50 3361 if (file_data->real_names)
9291a0cd 3362 {
7b9f3c50 3363 int i;
9291a0cd 3364
7b9f3c50 3365 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3366 {
7b9f3c50
DE
3367 xfree ((void*) file_data->real_names[i]);
3368 file_data->real_names[i] = NULL;
9291a0cd
TT
3369 }
3370 }
7b9f3c50
DE
3371
3372 return 1;
3373}
3374
39298a5d
TT
3375void
3376dwarf2_base_index_functions::forget_cached_source_info
3377 (struct objfile *objfile)
7b9f3c50 3378{
976ca316 3379 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3380
976ca316 3381 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3382 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3383}
3384
f8eba3c6
TT
3385/* Helper function for dw2_map_symtabs_matching_filename that expands
3386 the symtabs and calls the iterator. */
3387
3388static int
3389dw2_map_expand_apply (struct objfile *objfile,
3390 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3391 const char *name, const char *real_path,
14bc53a8 3392 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3393{
43f3e411 3394 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3395
3396 /* Don't visit already-expanded CUs. */
af758d11
SM
3397 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3398 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3399 return 0;
3400
3401 /* This may expand more than one symtab, and we want to iterate over
3402 all of them. */
97a1449a 3403 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3404
14bc53a8
PA
3405 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3406 last_made, callback);
f8eba3c6
TT
3407}
3408
3409/* Implementation of the map_symtabs_matching_filename method. */
3410
39298a5d
TT
3411bool
3412dwarf2_base_index_functions::map_symtabs_matching_filename
14bc53a8
PA
3413 (struct objfile *objfile, const char *name, const char *real_path,
3414 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3415{
c011a4f4 3416 const char *name_basename = lbasename (name);
976ca316 3417 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 3418
848e3e78
DE
3419 /* The rule is CUs specify all the files, including those used by
3420 any TU, so there's no need to scan TUs here. */
f4dc4d17 3421
976ca316 3422 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3423 {
3d7bb9d9 3424 /* We only need to look at symtabs not already expanded. */
976ca316 3425 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3426 continue;
3427
976ca316 3428 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3429 if (file_data == NULL)
9291a0cd
TT
3430 continue;
3431
b76e467d 3432 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3433 {
7b9f3c50 3434 const char *this_name = file_data->file_names[j];
da235a7c 3435 const char *this_real_name;
9291a0cd 3436
af529f8f 3437 if (compare_filenames_for_search (this_name, name))
9291a0cd 3438 {
f5b95b50 3439 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3440 callback))
3441 return true;
288e77a7 3442 continue;
4aac40c8 3443 }
9291a0cd 3444
c011a4f4
DE
3445 /* Before we invoke realpath, which can get expensive when many
3446 files are involved, do a quick comparison of the basenames. */
3447 if (! basenames_may_differ
3448 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3449 continue;
3450
976ca316 3451 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
da235a7c 3452 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3453 {
da235a7c 3454 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3455 callback))
3456 return true;
288e77a7 3457 continue;
da235a7c 3458 }
9291a0cd 3459
da235a7c
JK
3460 if (real_path != NULL)
3461 {
af529f8f
JK
3462 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3463 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3464 if (this_real_name != NULL
af529f8f 3465 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3466 {
f5b95b50 3467 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3468 callback))
3469 return true;
288e77a7 3470 continue;
9291a0cd
TT
3471 }
3472 }
3473 }
3474 }
3475
14bc53a8 3476 return false;
9291a0cd
TT
3477}
3478
da51c347
DE
3479/* Struct used to manage iterating over all CUs looking for a symbol. */
3480
3481struct dw2_symtab_iterator
9291a0cd 3482{
ed2dc618 3483 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3484 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3485 /* If set, only look for symbols that match that block. Valid values are
3486 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3487 gdb::optional<block_enum> block_index;
da51c347
DE
3488 /* The kind of symbol we're looking for. */
3489 domain_enum domain;
3490 /* The list of CUs from the index entry of the symbol,
3491 or NULL if not found. */
3492 offset_type *vec;
3493 /* The next element in VEC to look at. */
3494 int next;
3495 /* The number of elements in VEC, or zero if there is no match. */
3496 int length;
8943b874
DE
3497 /* Have we seen a global version of the symbol?
3498 If so we can ignore all further global instances.
3499 This is to work around gold/15646, inefficient gold-generated
3500 indices. */
3501 int global_seen;
da51c347 3502};
9291a0cd 3503
e5f3ece2 3504/* Initialize the index symtab iterator ITER, common part. */
2fdf6df6 3505
9291a0cd 3506static void
e5f3ece2
TV
3507dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3508 dwarf2_per_objfile *per_objfile,
3509 gdb::optional<block_enum> block_index,
3510 domain_enum domain)
da51c347 3511{
976ca316 3512 iter->per_objfile = per_objfile;
da51c347
DE
3513 iter->block_index = block_index;
3514 iter->domain = domain;
3515 iter->next = 0;
8943b874 3516 iter->global_seen = 0;
e5f3ece2
TV
3517 iter->vec = NULL;
3518 iter->length = 0;
3519}
da51c347 3520
e5f3ece2
TV
3521/* Initialize the index symtab iterator ITER, const char *NAME variant. */
3522
3523static void
3524dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3525 dwarf2_per_objfile *per_objfile,
3526 gdb::optional<block_enum> block_index,
3527 domain_enum domain,
3528 const char *name)
3529{
3530 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
ed2dc618 3531
e5f3ece2 3532 mapped_index *index = per_objfile->per_bfd->index_table.get ();
ed2dc618 3533 /* index is NULL if OBJF_READNOW. */
e5f3ece2
TV
3534 if (index == NULL)
3535 return;
3536
3537 if (find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347 3538 iter->length = MAYBE_SWAP (*iter->vec);
e5f3ece2
TV
3539}
3540
3541/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3542
3543static void
3544dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3545 dwarf2_per_objfile *per_objfile,
3546 gdb::optional<block_enum> block_index,
3547 domain_enum domain, offset_type namei)
3548{
3549 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3550
3551 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3552 /* index is NULL if OBJF_READNOW. */
3553 if (index == NULL)
3554 return;
3555
3556 gdb_assert (!index->symbol_name_slot_invalid (namei));
3557 const auto &bucket = index->symbol_table[namei];
3558
3559 iter->vec = (offset_type *) (index->constant_pool
3560 + MAYBE_SWAP (bucket.vec));
3561 iter->length = MAYBE_SWAP (*iter->vec);
da51c347
DE
3562}
3563
3564/* Return the next matching CU or NULL if there are no more. */
3565
3566static struct dwarf2_per_cu_data *
3567dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3568{
976ca316 3569 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3570
da51c347
DE
3571 for ( ; iter->next < iter->length; ++iter->next)
3572 {
3573 offset_type cu_index_and_attrs =
3574 MAYBE_SWAP (iter->vec[iter->next + 1]);
3575 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3576 gdb_index_symbol_kind symbol_kind =
3577 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3578 /* Only check the symbol attributes if they're present.
3579 Indices prior to version 7 don't record them,
3580 and indices >= 7 may elide them for certain symbols
3581 (gold does this). */
3582 int attrs_valid =
976ca316 3583 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3584 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3585
3190f0c6 3586 /* Don't crash on bad data. */
976ca316
SM
3587 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3588 + per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3589 {
b98664d3 3590 complaint (_(".gdb_index entry has bad CU index"
976ca316 3591 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3592 continue;
3593 }
3594
976ca316 3595 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3596
da51c347 3597 /* Skip if already read in. */
976ca316 3598 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3599 continue;
3600
8943b874
DE
3601 /* Check static vs global. */
3602 if (attrs_valid)
3603 {
2b79f376
SM
3604 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3605
3606 if (iter->block_index.has_value ())
3607 {
3608 bool want_static = *iter->block_index == STATIC_BLOCK;
3609
3610 if (is_static != want_static)
3611 continue;
3612 }
3613
8943b874 3614 /* Work around gold/15646. */
f030440d
TV
3615 if (!is_static
3616 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3617 {
3618 if (iter->global_seen)
3619 continue;
3620
3621 iter->global_seen = 1;
3622 }
8943b874 3623 }
da51c347
DE
3624
3625 /* Only check the symbol's kind if it has one. */
3626 if (attrs_valid)
3627 {
3628 switch (iter->domain)
3629 {
3630 case VAR_DOMAIN:
3631 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3632 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3633 /* Some types are also in VAR_DOMAIN. */
3634 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3635 continue;
3636 break;
3637 case STRUCT_DOMAIN:
3638 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3639 continue;
3640 break;
3641 case LABEL_DOMAIN:
3642 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3643 continue;
3644 break;
59c35742
AB
3645 case MODULE_DOMAIN:
3646 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3647 continue;
3648 break;
da51c347
DE
3649 default:
3650 break;
3651 }
3652 }
3653
3654 ++iter->next;
3655 return per_cu;
3656 }
3657
3658 return NULL;
3659}
3660
39298a5d
TT
3661struct compunit_symtab *
3662dwarf2_gdb_index::lookup_symbol (struct objfile *objfile,
3663 block_enum block_index,
3664 const char *name, domain_enum domain)
9291a0cd 3665{
43f3e411 3666 struct compunit_symtab *stab_best = NULL;
976ca316 3667 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 3668
b5ec771e
PA
3669 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3670
ed2dc618
SM
3671 struct dw2_symtab_iterator iter;
3672 struct dwarf2_per_cu_data *per_cu;
da51c347 3673
976ca316 3674 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
9291a0cd 3675
ed2dc618
SM
3676 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3677 {
3678 struct symbol *sym, *with_opaque = NULL;
97a1449a 3679 struct compunit_symtab *stab
976ca316 3680 = dw2_instantiate_symtab (per_cu, per_objfile, false);
ed2dc618 3681 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3682 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3683
ed2dc618
SM
3684 sym = block_find_symbol (block, name, domain,
3685 block_find_non_opaque_type_preferred,
3686 &with_opaque);
b2e2f908 3687
ed2dc618
SM
3688 /* Some caution must be observed with overloaded functions
3689 and methods, since the index will not contain any overload
3690 information (but NAME might contain it). */
da51c347 3691
ed2dc618
SM
3692 if (sym != NULL
3693 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3694 return stab;
3695 if (with_opaque != NULL
3696 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3697 stab_best = stab;
da51c347 3698
ed2dc618 3699 /* Keep looking through other CUs. */
9291a0cd 3700 }
9291a0cd 3701
da51c347 3702 return stab_best;
9291a0cd
TT
3703}
3704
39298a5d 3705void
4829711b
TT
3706dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3707 bool print_bcache)
9291a0cd 3708{
4829711b
TT
3709 if (print_bcache)
3710 return;
3711
976ca316
SM
3712 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3713 int total = (per_objfile->per_bfd->all_comp_units.size ()
3714 + per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3715 int count = 0;
9291a0cd 3716
ed2dc618 3717 for (int i = 0; i < total; ++i)
9291a0cd 3718 {
976ca316 3719 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3720
976ca316 3721 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3722 ++count;
3723 }
e4a48d9d 3724 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3725 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3726}
3727
779bd270
DE
3728/* This dumps minimal information about the index.
3729 It is called via "mt print objfiles".
3730 One use is to verify .gdb_index has been loaded by the
3731 gdb.dwarf2/gdb-index.exp testcase. */
3732
39298a5d
TT
3733void
3734dwarf2_gdb_index::dump (struct objfile *objfile)
9291a0cd 3735{
976ca316 3736 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3737
976ca316 3738 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3739 printf_filtered (".gdb_index:");
976ca316 3740 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3741 {
3742 printf_filtered (" version %d\n",
976ca316 3743 per_objfile->per_bfd->index_table->version);
779bd270
DE
3744 }
3745 else
3746 printf_filtered (" faked for \"readnow\"\n");
3747 printf_filtered ("\n");
9291a0cd
TT
3748}
3749
39298a5d
TT
3750void
3751dwarf2_gdb_index::expand_symtabs_for_function (struct objfile *objfile,
3752 const char *func_name)
9291a0cd 3753{
976ca316 3754 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
da51c347 3755
ed2dc618
SM
3756 struct dw2_symtab_iterator iter;
3757 struct dwarf2_per_cu_data *per_cu;
da51c347 3758
976ca316 3759 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3760
ed2dc618 3761 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3762 dw2_instantiate_symtab (per_cu, per_objfile, false);
da51c347 3763
9291a0cd
TT
3764}
3765
39298a5d
TT
3766void
3767dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
9291a0cd 3768{
976ca316
SM
3769 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3770 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3771 + per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3772
ed2dc618 3773 for (int i = 0; i < total_units; ++i)
9291a0cd 3774 {
976ca316 3775 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3776
58f0c718
TT
3777 /* We don't want to directly expand a partial CU, because if we
3778 read it with the wrong language, then assertion failures can
3779 be triggered later on. See PR symtab/23010. So, tell
3780 dw2_instantiate_symtab to skip partial CUs -- any important
3781 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3782 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3783 }
3784}
3785
39298a5d
TT
3786void
3787dwarf2_base_index_functions::expand_symtabs_with_fullname
3788 (struct objfile *objfile, const char *fullname)
9291a0cd 3789{
976ca316 3790 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3791
3792 /* We don't need to consider type units here.
3793 This is only called for examining code, e.g. expand_line_sal.
3794 There can be an order of magnitude (or more) more type units
3795 than comp units, and we avoid them if we can. */
3796
976ca316 3797 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3798 {
3d7bb9d9 3799 /* We only need to look at symtabs not already expanded. */
976ca316 3800 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3801 continue;
3802
976ca316 3803 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3804 if (file_data == NULL)
9291a0cd
TT
3805 continue;
3806
b76e467d 3807 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3808 {
652a8996
JK
3809 const char *this_fullname = file_data->file_names[j];
3810
3811 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3812 {
976ca316 3813 dw2_instantiate_symtab (per_cu, per_objfile, false);
9291a0cd
TT
3814 break;
3815 }
3816 }
3817 }
3818}
3819
9a0bacfb
TV
3820static void
3821dw2_expand_symtabs_matching_symbol
3822 (mapped_index_base &index,
3823 const lookup_name_info &lookup_name_in,
3824 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
3825 gdb::function_view<bool (offset_type)> match_callback,
3826 dwarf2_per_objfile *per_objfile);
9a0bacfb
TV
3827
3828static void
3829dw2_expand_symtabs_matching_one
97a1449a
SM
3830 (dwarf2_per_cu_data *per_cu,
3831 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3832 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3833 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3834
9291a0cd 3835static void
199b4314
TT
3836dw2_map_matching_symbols
3837 (struct objfile *objfile,
b054970d 3838 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3839 int global,
3840 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3841 symbol_compare_ftype *ordered_compare)
9291a0cd 3842{
1aa98955 3843 /* Used for Ada. */
976ca316 3844 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3845
9a0bacfb
TV
3846 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3847
976ca316 3848 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3849 {
976ca316 3850 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3851
9a0bacfb
TV
3852 const char *match_name = name.ada ().lookup_name ().c_str ();
3853 auto matcher = [&] (const char *symname)
3854 {
3855 if (ordered_compare == nullptr)
3856 return true;
3857 return ordered_compare (symname, match_name) == 0;
3858 };
3859
b64f703b 3860 dw2_expand_symtabs_matching_symbol (index, name, matcher,
9a0bacfb
TV
3861 [&] (offset_type namei)
3862 {
3863 struct dw2_symtab_iterator iter;
3864 struct dwarf2_per_cu_data *per_cu;
3865
976ca316 3866 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3867 namei);
9a0bacfb 3868 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3869 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3870 nullptr);
9a0bacfb 3871 return true;
976ca316 3872 }, per_objfile);
9a0bacfb
TV
3873 }
3874 else
3875 {
3876 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3877 proceed assuming all symtabs have been read in. */
3878 }
1aa98955
TV
3879
3880 for (compunit_symtab *cust : objfile->compunits ())
3881 {
3882 const struct block *block;
3883
3884 if (cust == NULL)
3885 continue;
3886 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3887 if (!iterate_over_symbols_terminated (block, name,
3888 domain, callback))
3889 return;
3890 }
9291a0cd
TT
3891}
3892
39298a5d
TT
3893void
3894dwarf2_gdb_index::map_matching_symbols
3895 (struct objfile *objfile,
3896 const lookup_name_info &name, domain_enum domain,
3897 int global,
3898 gdb::function_view<symbol_found_callback_ftype> callback,
3899 symbol_compare_ftype *ordered_compare)
3900{
3901 dw2_map_matching_symbols (objfile, name, domain, global, callback,
3902 ordered_compare);
3903}
3904
e1ef7d7a
PA
3905/* Starting from a search name, return the string that finds the upper
3906 bound of all strings that start with SEARCH_NAME in a sorted name
3907 list. Returns the empty string to indicate that the upper bound is
3908 the end of the list. */
3909
3910static std::string
3911make_sort_after_prefix_name (const char *search_name)
3912{
3913 /* When looking to complete "func", we find the upper bound of all
3914 symbols that start with "func" by looking for where we'd insert
3915 the closest string that would follow "func" in lexicographical
3916 order. Usually, that's "func"-with-last-character-incremented,
3917 i.e. "fund". Mind non-ASCII characters, though. Usually those
3918 will be UTF-8 multi-byte sequences, but we can't be certain.
3919 Especially mind the 0xff character, which is a valid character in
3920 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3921 rule out compilers allowing it in identifiers. Note that
3922 conveniently, strcmp/strcasecmp are specified to compare
3923 characters interpreted as unsigned char. So what we do is treat
3924 the whole string as a base 256 number composed of a sequence of
3925 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3926 to 0, and carries 1 to the following more-significant position.
3927 If the very first character in SEARCH_NAME ends up incremented
3928 and carries/overflows, then the upper bound is the end of the
3929 list. The string after the empty string is also the empty
3930 string.
3931
3932 Some examples of this operation:
3933
3934 SEARCH_NAME => "+1" RESULT
3935
3936 "abc" => "abd"
3937 "ab\xff" => "ac"
3938 "\xff" "a" "\xff" => "\xff" "b"
3939 "\xff" => ""
3940 "\xff\xff" => ""
3941 "" => ""
3942
3943 Then, with these symbols for example:
3944
3945 func
3946 func1
3947 fund
3948
3949 completing "func" looks for symbols between "func" and
3950 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3951 which finds "func" and "func1", but not "fund".
3952
3953 And with:
3954
3955 funcÿ (Latin1 'ÿ' [0xff])
3956 funcÿ1
3957 fund
3958
3959 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3960 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3961
3962 And with:
3963
3964 ÿÿ (Latin1 'ÿ' [0xff])
3965 ÿÿ1
3966
3967 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3968 the end of the list.
3969 */
3970 std::string after = search_name;
3971 while (!after.empty () && (unsigned char) after.back () == 0xff)
3972 after.pop_back ();
3973 if (!after.empty ())
3974 after.back () = (unsigned char) after.back () + 1;
3975 return after;
3976}
3977
5c58de74 3978/* See declaration. */
61d96d7e 3979
5c58de74
PA
3980std::pair<std::vector<name_component>::const_iterator,
3981 std::vector<name_component>::const_iterator>
44ed8f3e 3982mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3983 (const lookup_name_info &lookup_name_without_params, language lang,
3984 dwarf2_per_objfile *per_objfile) const
3f563c84 3985{
5c58de74
PA
3986 auto *name_cmp
3987 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3988
3b00ef10 3989 const char *lang_name
e0802d59 3990 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3991
3f563c84
PA
3992 /* Comparison function object for lower_bound that matches against a
3993 given symbol name. */
3994 auto lookup_compare_lower = [&] (const name_component &elem,
3995 const char *name)
3996 {
fcf23d5b 3997 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3998 const char *elem_name = elem_qualified + elem.name_offset;
3999 return name_cmp (elem_name, name) < 0;
4000 };
4001
4002 /* Comparison function object for upper_bound that matches against a
4003 given symbol name. */
4004 auto lookup_compare_upper = [&] (const char *name,
4005 const name_component &elem)
4006 {
fcf23d5b 4007 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
4008 const char *elem_name = elem_qualified + elem.name_offset;
4009 return name_cmp (name, elem_name) < 0;
4010 };
4011
5c58de74
PA
4012 auto begin = this->name_components.begin ();
4013 auto end = this->name_components.end ();
3f563c84
PA
4014
4015 /* Find the lower bound. */
4016 auto lower = [&] ()
4017 {
3b00ef10 4018 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4019 return begin;
4020 else
3b00ef10 4021 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4022 } ();
4023
4024 /* Find the upper bound. */
4025 auto upper = [&] ()
4026 {
5c58de74 4027 if (lookup_name_without_params.completion_mode ())
3f563c84 4028 {
e1ef7d7a
PA
4029 /* In completion mode, we want UPPER to point past all
4030 symbols names that have the same prefix. I.e., with
4031 these symbols, and completing "func":
4032
4033 function << lower bound
4034 function1
4035 other_function << upper bound
4036
4037 We find the upper bound by looking for the insertion
4038 point of "func"-with-last-character-incremented,
4039 i.e. "fund". */
3b00ef10 4040 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4041 if (after.empty ())
3f563c84 4042 return end;
e6b2f5ef
PA
4043 return std::lower_bound (lower, end, after.c_str (),
4044 lookup_compare_lower);
3f563c84
PA
4045 }
4046 else
3b00ef10 4047 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4048 } ();
4049
5c58de74
PA
4050 return {lower, upper};
4051}
4052
4053/* See declaration. */
4054
4055void
fcf23d5b 4056mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
4057{
4058 if (!this->name_components.empty ())
4059 return;
4060
4061 this->name_components_casing = case_sensitivity;
4062 auto *name_cmp
4063 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4064
4065 /* The code below only knows how to break apart components of C++
4066 symbol names (and other languages that use '::' as
3b00ef10 4067 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4068 auto count = this->symbol_name_count ();
4069 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4070 {
44ed8f3e 4071 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4072 continue;
4073
fcf23d5b 4074 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
4075
4076 /* Add each name component to the name component table. */
4077 unsigned int previous_len = 0;
3b00ef10
TT
4078
4079 if (strstr (name, "::") != nullptr)
4080 {
4081 for (unsigned int current_len = cp_find_first_component (name);
4082 name[current_len] != '\0';
4083 current_len += cp_find_first_component (name + current_len))
4084 {
4085 gdb_assert (name[current_len] == ':');
4086 this->name_components.push_back ({previous_len, idx});
4087 /* Skip the '::'. */
4088 current_len += 2;
4089 previous_len = current_len;
4090 }
4091 }
4092 else
5c58de74 4093 {
3b00ef10
TT
4094 /* Handle the Ada encoded (aka mangled) form here. */
4095 for (const char *iter = strstr (name, "__");
4096 iter != nullptr;
4097 iter = strstr (iter, "__"))
4098 {
4099 this->name_components.push_back ({previous_len, idx});
4100 iter += 2;
4101 previous_len = iter - name;
4102 }
5c58de74 4103 }
3b00ef10 4104
5c58de74
PA
4105 this->name_components.push_back ({previous_len, idx});
4106 }
4107
4108 /* Sort name_components elements by name. */
4109 auto name_comp_compare = [&] (const name_component &left,
4110 const name_component &right)
4111 {
fcf23d5b
SM
4112 const char *left_qualified
4113 = this->symbol_name_at (left.idx, per_objfile);
4114 const char *right_qualified
4115 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
4116
4117 const char *left_name = left_qualified + left.name_offset;
4118 const char *right_name = right_qualified + right.name_offset;
4119
4120 return name_cmp (left_name, right_name) < 0;
4121 };
4122
4123 std::sort (this->name_components.begin (),
4124 this->name_components.end (),
4125 name_comp_compare);
4126}
4127
4128/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4129 mapped_index_base instead of the containing objfile. This is split
4130 to a separate function in order to be able to unit test the
4131 name_components matching using a mock mapped_index_base. For each
5c58de74 4132 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4133 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4134
4135static void
4136dw2_expand_symtabs_matching_symbol
44ed8f3e 4137 (mapped_index_base &index,
5c58de74
PA
4138 const lookup_name_info &lookup_name_in,
4139 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
4140 gdb::function_view<bool (offset_type)> match_callback,
4141 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4142{
4143 lookup_name_info lookup_name_without_params
4144 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4145
4146 /* Build the symbol name component sorted vector, if we haven't
4147 yet. */
fcf23d5b 4148 index.build_name_components (per_objfile);
5c58de74 4149
3f563c84
PA
4150 /* The same symbol may appear more than once in the range though.
4151 E.g., if we're looking for symbols that complete "w", and we have
4152 a symbol named "w1::w2", we'll find the two name components for
4153 that same symbol in the range. To be sure we only call the
4154 callback once per symbol, we first collect the symbol name
4155 indexes that matched in a temporary vector and ignore
4156 duplicates. */
4157 std::vector<offset_type> matches;
3f563c84 4158
3b00ef10
TT
4159 struct name_and_matcher
4160 {
4161 symbol_name_matcher_ftype *matcher;
ecc6c606 4162 const char *name;
3b00ef10
TT
4163
4164 bool operator== (const name_and_matcher &other) const
3f563c84 4165 {
ecc6c606 4166 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4167 }
4168 };
4169
4170 /* A vector holding all the different symbol name matchers, for all
4171 languages. */
4172 std::vector<name_and_matcher> matchers;
4173
4174 for (int i = 0; i < nr_languages; i++)
4175 {
4176 enum language lang_e = (enum language) i;
4177
4178 const language_defn *lang = language_def (lang_e);
4179 symbol_name_matcher_ftype *name_matcher
c9debfb9 4180 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 4181
3b00ef10 4182 name_and_matcher key {
dda83cd7 4183 name_matcher,
3b00ef10
TT
4184 lookup_name_without_params.language_lookup_name (lang_e)
4185 };
4186
4187 /* Don't insert the same comparison routine more than once.
4188 Note that we do this linear walk. This is not a problem in
4189 practice because the number of supported languages is
4190 low. */
4191 if (std::find (matchers.begin (), matchers.end (), key)
4192 != matchers.end ())
9291a0cd 4193 continue;
3b00ef10
TT
4194 matchers.push_back (std::move (key));
4195
4196 auto bounds
4197 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 4198 lang_e, per_objfile);
3b00ef10
TT
4199
4200 /* Now for each symbol name in range, check to see if we have a name
4201 match, and if so, call the MATCH_CALLBACK callback. */
4202
4203 for (; bounds.first != bounds.second; ++bounds.first)
4204 {
fcf23d5b
SM
4205 const char *qualified
4206 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
4207
4208 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4209 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4210 continue;
9291a0cd 4211
3b00ef10
TT
4212 matches.push_back (bounds.first->idx);
4213 }
3f563c84
PA
4214 }
4215
4216 std::sort (matches.begin (), matches.end ());
4217
4218 /* Finally call the callback, once per match. */
4219 ULONGEST prev = -1;
4220 for (offset_type idx : matches)
4221 {
4222 if (prev != idx)
4223 {
3b00ef10
TT
4224 if (!match_callback (idx))
4225 break;
3f563c84
PA
4226 prev = idx;
4227 }
4228 }
4229
4230 /* Above we use a type wider than idx's for 'prev', since 0 and
4231 (offset_type)-1 are both possible values. */
4232 static_assert (sizeof (prev) > sizeof (offset_type), "");
4233}
4234
c62446b1
PA
4235#if GDB_SELF_TEST
4236
4237namespace selftests { namespace dw2_expand_symtabs_matching {
4238
a3c5fafd
PA
4239/* A mock .gdb_index/.debug_names-like name index table, enough to
4240 exercise dw2_expand_symtabs_matching_symbol, which works with the
4241 mapped_index_base interface. Builds an index from the symbol list
4242 passed as parameter to the constructor. */
4243class mock_mapped_index : public mapped_index_base
c62446b1
PA
4244{
4245public:
a3c5fafd
PA
4246 mock_mapped_index (gdb::array_view<const char *> symbols)
4247 : m_symbol_table (symbols)
c62446b1
PA
4248 {}
4249
a3c5fafd 4250 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4251
a3c5fafd 4252 /* Return the number of names in the symbol table. */
632e107b 4253 size_t symbol_name_count () const override
c62446b1 4254 {
a3c5fafd 4255 return m_symbol_table.size ();
c62446b1
PA
4256 }
4257
a3c5fafd 4258 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
4259 const char *symbol_name_at
4260 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
4261 {
4262 return m_symbol_table[idx];
4263 }
c62446b1 4264
a3c5fafd
PA
4265private:
4266 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4267};
4268
4269/* Convenience function that converts a NULL pointer to a "<null>"
4270 string, to pass to print routines. */
4271
4272static const char *
4273string_or_null (const char *str)
4274{
4275 return str != NULL ? str : "<null>";
4276}
4277
4278/* Check if a lookup_name_info built from
4279 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4280 index. EXPECTED_LIST is the list of expected matches, in expected
4281 matching order. If no match expected, then an empty list is
4282 specified. Returns true on success. On failure prints a warning
4283 indicating the file:line that failed, and returns false. */
4284
4285static bool
4286check_match (const char *file, int line,
4287 mock_mapped_index &mock_index,
4288 const char *name, symbol_name_match_type match_type,
4289 bool completion_mode,
fcf23d5b
SM
4290 std::initializer_list<const char *> expected_list,
4291 dwarf2_per_objfile *per_objfile)
c62446b1
PA
4292{
4293 lookup_name_info lookup_name (name, match_type, completion_mode);
4294
4295 bool matched = true;
4296
4297 auto mismatch = [&] (const char *expected_str,
4298 const char *got)
4299 {
4300 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4301 "expected=\"%s\", got=\"%s\"\n"),
4302 file, line,
4303 (match_type == symbol_name_match_type::FULL
4304 ? "FULL" : "WILD"),
4305 name, string_or_null (expected_str), string_or_null (got));
4306 matched = false;
4307 };
4308
4309 auto expected_it = expected_list.begin ();
4310 auto expected_end = expected_list.end ();
4311
a3c5fafd 4312 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
b64f703b 4313 nullptr,
c62446b1
PA
4314 [&] (offset_type idx)
4315 {
fcf23d5b 4316 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
4317 const char *expected_str
4318 = expected_it == expected_end ? NULL : *expected_it++;
4319
4320 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4321 mismatch (expected_str, matched_name);
3b00ef10 4322 return true;
fcf23d5b 4323 }, per_objfile);
c62446b1
PA
4324
4325 const char *expected_str
4326 = expected_it == expected_end ? NULL : *expected_it++;
4327 if (expected_str != NULL)
4328 mismatch (expected_str, NULL);
4329
4330 return matched;
4331}
4332
4333/* The symbols added to the mock mapped_index for testing (in
4334 canonical form). */
4335static const char *test_symbols[] = {
4336 "function",
4337 "std::bar",
4338 "std::zfunction",
4339 "std::zfunction2",
4340 "w1::w2",
4341 "ns::foo<char*>",
4342 "ns::foo<int>",
4343 "ns::foo<long>",
a20714ff
PA
4344 "ns2::tmpl<int>::foo2",
4345 "(anonymous namespace)::A::B::C",
c62446b1 4346
e1ef7d7a
PA
4347 /* These are used to check that the increment-last-char in the
4348 matching algorithm for completion doesn't match "t1_fund" when
4349 completing "t1_func". */
4350 "t1_func",
4351 "t1_func1",
4352 "t1_fund",
4353 "t1_fund1",
4354
4355 /* A UTF-8 name with multi-byte sequences to make sure that
4356 cp-name-parser understands this as a single identifier ("função"
4357 is "function" in PT). */
4358 u8"u8função",
4359
4360 /* \377 (0xff) is Latin1 'ÿ'. */
4361 "yfunc\377",
4362
4363 /* \377 (0xff) is Latin1 'ÿ'. */
4364 "\377",
4365 "\377\377123",
4366
c62446b1
PA
4367 /* A name with all sorts of complications. Starts with "z" to make
4368 it easier for the completion tests below. */
4369#define Z_SYM_NAME \
4370 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4371 "::tuple<(anonymous namespace)::ui*, " \
4372 "std::default_delete<(anonymous namespace)::ui>, void>"
4373
4374 Z_SYM_NAME
4375};
4376
a3c5fafd
PA
4377/* Returns true if the mapped_index_base::find_name_component_bounds
4378 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4379 in completion mode. */
5c58de74
PA
4380
4381static bool
a3c5fafd 4382check_find_bounds_finds (mapped_index_base &index,
5c58de74 4383 const char *search_name,
fcf23d5b
SM
4384 gdb::array_view<const char *> expected_syms,
4385 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4386{
4387 lookup_name_info lookup_name (search_name,
4388 symbol_name_match_type::FULL, true);
4389
3b00ef10 4390 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4391 language_cplus,
4392 per_objfile);
5c58de74
PA
4393
4394 size_t distance = std::distance (bounds.first, bounds.second);
4395 if (distance != expected_syms.size ())
4396 return false;
4397
4398 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4399 {
4400 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4401 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4402 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4403 return false;
4404 }
4405
4406 return true;
4407}
4408
4409/* Test the lower-level mapped_index::find_name_component_bounds
4410 method. */
4411
c62446b1 4412static void
5c58de74
PA
4413test_mapped_index_find_name_component_bounds ()
4414{
4415 mock_mapped_index mock_index (test_symbols);
4416
fcf23d5b 4417 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4418
4419 /* Test the lower-level mapped_index::find_name_component_bounds
4420 method in completion mode. */
4421 {
4422 static const char *expected_syms[] = {
4423 "t1_func",
4424 "t1_func1",
5c58de74
PA
4425 };
4426
fcf23d5b
SM
4427 SELF_CHECK (check_find_bounds_finds
4428 (mock_index, "t1_func", expected_syms,
4429 NULL /* per_objfile */));
5c58de74
PA
4430 }
4431
4432 /* Check that the increment-last-char in the name matching algorithm
4433 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4434 {
4435 static const char *expected_syms1[] = {
4436 "\377",
4437 "\377\377123",
4438 };
fcf23d5b
SM
4439 SELF_CHECK (check_find_bounds_finds
4440 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4441
4442 static const char *expected_syms2[] = {
4443 "\377\377123",
4444 };
fcf23d5b
SM
4445 SELF_CHECK (check_find_bounds_finds
4446 (mock_index, "\377\377", expected_syms2,
4447 NULL /* per_objfile */));
5c58de74
PA
4448 }
4449}
4450
4451/* Test dw2_expand_symtabs_matching_symbol. */
4452
4453static void
4454test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4455{
4456 mock_mapped_index mock_index (test_symbols);
4457
4458 /* We let all tests run until the end even if some fails, for debug
4459 convenience. */
4460 bool any_mismatch = false;
4461
4462 /* Create the expected symbols list (an initializer_list). Needed
4463 because lists have commas, and we need to pass them to CHECK,
4464 which is a macro. */
4465#define EXPECT(...) { __VA_ARGS__ }
4466
4467 /* Wrapper for check_match that passes down the current
4468 __FILE__/__LINE__. */
4469#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4470 any_mismatch |= !check_match (__FILE__, __LINE__, \
4471 mock_index, \
4472 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4473 EXPECTED_LIST, NULL)
c62446b1
PA
4474
4475 /* Identity checks. */
4476 for (const char *sym : test_symbols)
4477 {
4478 /* Should be able to match all existing symbols. */
4479 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4480 EXPECT (sym));
4481
4482 /* Should be able to match all existing symbols with
4483 parameters. */
4484 std::string with_params = std::string (sym) + "(int)";
4485 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4486 EXPECT (sym));
4487
4488 /* Should be able to match all existing symbols with
4489 parameters and qualifiers. */
4490 with_params = std::string (sym) + " ( int ) const";
4491 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4492 EXPECT (sym));
4493
4494 /* This should really find sym, but cp-name-parser.y doesn't
4495 know about lvalue/rvalue qualifiers yet. */
4496 with_params = std::string (sym) + " ( int ) &&";
4497 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4498 {});
4499 }
4500
e1ef7d7a
PA
4501 /* Check that the name matching algorithm for completion doesn't get
4502 confused with Latin1 'ÿ' / 0xff. */
4503 {
4504 static const char str[] = "\377";
4505 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4506 EXPECT ("\377", "\377\377123"));
4507 }
4508
4509 /* Check that the increment-last-char in the matching algorithm for
4510 completion doesn't match "t1_fund" when completing "t1_func". */
4511 {
4512 static const char str[] = "t1_func";
4513 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4514 EXPECT ("t1_func", "t1_func1"));
4515 }
4516
c62446b1
PA
4517 /* Check that completion mode works at each prefix of the expected
4518 symbol name. */
4519 {
4520 static const char str[] = "function(int)";
4521 size_t len = strlen (str);
4522 std::string lookup;
4523
4524 for (size_t i = 1; i < len; i++)
4525 {
4526 lookup.assign (str, i);
4527 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4528 EXPECT ("function"));
4529 }
4530 }
4531
4532 /* While "w" is a prefix of both components, the match function
4533 should still only be called once. */
4534 {
4535 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4536 EXPECT ("w1::w2"));
a20714ff
PA
4537 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4538 EXPECT ("w1::w2"));
c62446b1
PA
4539 }
4540
4541 /* Same, with a "complicated" symbol. */
4542 {
4543 static const char str[] = Z_SYM_NAME;
4544 size_t len = strlen (str);
4545 std::string lookup;
4546
4547 for (size_t i = 1; i < len; i++)
4548 {
4549 lookup.assign (str, i);
4550 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4551 EXPECT (Z_SYM_NAME));
4552 }
4553 }
4554
4555 /* In FULL mode, an incomplete symbol doesn't match. */
4556 {
4557 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4558 {});
4559 }
4560
4561 /* A complete symbol with parameters matches any overload, since the
4562 index has no overload info. */
4563 {
4564 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4565 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4566 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4567 EXPECT ("std::zfunction", "std::zfunction2"));
4568 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4569 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4570 }
4571
4572 /* Check that whitespace is ignored appropriately. A symbol with a
4573 template argument list. */
4574 {
4575 static const char expected[] = "ns::foo<int>";
4576 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4577 EXPECT (expected));
a20714ff
PA
4578 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4579 EXPECT (expected));
c62446b1
PA
4580 }
4581
4582 /* Check that whitespace is ignored appropriately. A symbol with a
4583 template argument list that includes a pointer. */
4584 {
4585 static const char expected[] = "ns::foo<char*>";
4586 /* Try both completion and non-completion modes. */
4587 static const bool completion_mode[2] = {false, true};
4588 for (size_t i = 0; i < 2; i++)
4589 {
4590 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4591 completion_mode[i], EXPECT (expected));
a20714ff
PA
4592 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4593 completion_mode[i], EXPECT (expected));
c62446b1
PA
4594
4595 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4596 completion_mode[i], EXPECT (expected));
a20714ff
PA
4597 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4598 completion_mode[i], EXPECT (expected));
c62446b1
PA
4599 }
4600 }
4601
4602 {
4603 /* Check method qualifiers are ignored. */
4604 static const char expected[] = "ns::foo<char*>";
4605 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4606 symbol_name_match_type::FULL, true, EXPECT (expected));
4607 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4608 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4609 CHECK_MATCH ("foo < char * > ( int ) const",
4610 symbol_name_match_type::WILD, true, EXPECT (expected));
4611 CHECK_MATCH ("foo < char * > ( int ) &&",
4612 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4613 }
4614
4615 /* Test lookup names that don't match anything. */
4616 {
a20714ff
PA
4617 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4618 {});
4619
c62446b1
PA
4620 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4621 {});
4622 }
4623
a20714ff
PA
4624 /* Some wild matching tests, exercising "(anonymous namespace)",
4625 which should not be confused with a parameter list. */
4626 {
4627 static const char *syms[] = {
4628 "A::B::C",
4629 "B::C",
4630 "C",
4631 "A :: B :: C ( int )",
4632 "B :: C ( int )",
4633 "C ( int )",
4634 };
4635
4636 for (const char *s : syms)
4637 {
4638 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4639 EXPECT ("(anonymous namespace)::A::B::C"));
4640 }
4641 }
4642
4643 {
4644 static const char expected[] = "ns2::tmpl<int>::foo2";
4645 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4646 EXPECT (expected));
4647 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4648 EXPECT (expected));
4649 }
4650
c62446b1
PA
4651 SELF_CHECK (!any_mismatch);
4652
4653#undef EXPECT
4654#undef CHECK_MATCH
4655}
4656
5c58de74
PA
4657static void
4658run_test ()
4659{
4660 test_mapped_index_find_name_component_bounds ();
4661 test_dw2_expand_symtabs_matching_symbol ();
4662}
4663
c62446b1
PA
4664}} // namespace selftests::dw2_expand_symtabs_matching
4665
4666#endif /* GDB_SELF_TEST */
4667
4b514bc8
JK
4668/* If FILE_MATCHER is NULL or if PER_CU has
4669 dwarf2_per_cu_quick_data::MARK set (see
4670 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4671 EXPANSION_NOTIFY on it. */
4672
4673static void
4674dw2_expand_symtabs_matching_one
97a1449a
SM
4675 (dwarf2_per_cu_data *per_cu,
4676 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4677 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4678 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4679{
4680 if (file_matcher == NULL || per_cu->v.quick->mark)
4681 {
af758d11 4682 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4683
97a1449a
SM
4684 compunit_symtab *symtab
4685 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4686 gdb_assert (symtab != nullptr);
4b514bc8 4687
af758d11
SM
4688 if (expansion_notify != NULL && symtab_was_null)
4689 expansion_notify (symtab);
4b514bc8
JK
4690 }
4691}
4692
3f563c84
PA
4693/* Helper for dw2_expand_matching symtabs. Called on each symbol
4694 matched, to expand corresponding CUs that were marked. IDX is the
4695 index of the symbol name that matched. */
4696
4697static void
4698dw2_expand_marked_cus
976ca316 4699 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4700 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4701 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4702 search_domain kind)
4703{
3f563c84
PA
4704 offset_type *vec, vec_len, vec_idx;
4705 bool global_seen = false;
976ca316 4706 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4707
61920122 4708 vec = (offset_type *) (index.constant_pool
f00a2de2 4709 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4710 vec_len = MAYBE_SWAP (vec[0]);
4711 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4712 {
61920122
PA
4713 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4714 /* This value is only valid for index versions >= 7. */
4715 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4716 gdb_index_symbol_kind symbol_kind =
4717 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4718 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4719 /* Only check the symbol attributes if they're present.
4720 Indices prior to version 7 don't record them,
4721 and indices >= 7 may elide them for certain symbols
4722 (gold does this). */
4723 int attrs_valid =
4724 (index.version >= 7
4725 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4726
4727 /* Work around gold/15646. */
f030440d
TV
4728 if (attrs_valid
4729 && !is_static
4730 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4731 {
f030440d 4732 if (global_seen)
61920122 4733 continue;
f030440d
TV
4734
4735 global_seen = true;
61920122 4736 }
3190f0c6 4737
61920122
PA
4738 /* Only check the symbol's kind if it has one. */
4739 if (attrs_valid)
4740 {
4741 switch (kind)
8943b874 4742 {
61920122
PA
4743 case VARIABLES_DOMAIN:
4744 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4745 continue;
4746 break;
4747 case FUNCTIONS_DOMAIN:
4748 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4749 continue;
61920122
PA
4750 break;
4751 case TYPES_DOMAIN:
4752 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4753 continue;
4754 break;
59c35742
AB
4755 case MODULES_DOMAIN:
4756 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4757 continue;
4758 break;
61920122
PA
4759 default:
4760 break;
8943b874 4761 }
61920122 4762 }
8943b874 4763
61920122 4764 /* Don't crash on bad data. */
976ca316
SM
4765 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4766 + per_objfile->per_bfd->all_type_units.size ()))
61920122 4767 {
b98664d3 4768 complaint (_(".gdb_index entry has bad CU index"
976ca316 4769 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4770 continue;
4771 }
4772
976ca316
SM
4773 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4774 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4b514bc8 4775 expansion_notify);
61920122
PA
4776 }
4777}
4778
4b514bc8
JK
4779/* If FILE_MATCHER is non-NULL, set all the
4780 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4781 that match FILE_MATCHER. */
4782
61920122 4783static void
4b514bc8 4784dw_expand_symtabs_matching_file_matcher
976ca316 4785 (dwarf2_per_objfile *per_objfile,
ed2dc618 4786 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4787{
4b514bc8 4788 if (file_matcher == NULL)
61920122
PA
4789 return;
4790
4b514bc8
JK
4791 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4792 htab_eq_pointer,
4793 NULL, xcalloc, xfree));
4794 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4795 htab_eq_pointer,
4796 NULL, xcalloc, xfree));
61920122 4797
4b514bc8
JK
4798 /* The rule is CUs specify all the files, including those used by
4799 any TU, so there's no need to scan TUs here. */
61920122 4800
976ca316 4801 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4802 {
927aa2e7
JK
4803 QUIT;
4804
4805 per_cu->v.quick->mark = 0;
4806
4807 /* We only need to look at symtabs not already expanded. */
976ca316 4808 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4809 continue;
4810
976ca316 4811 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4812 if (file_data == NULL)
4813 continue;
4814
4815 if (htab_find (visited_not_found.get (), file_data) != NULL)
4816 continue;
4817 else if (htab_find (visited_found.get (), file_data) != NULL)
4818 {
4819 per_cu->v.quick->mark = 1;
4820 continue;
4821 }
4822
b76e467d 4823 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4824 {
4825 const char *this_real_name;
4826
4827 if (file_matcher (file_data->file_names[j], false))
4828 {
4829 per_cu->v.quick->mark = 1;
4830 break;
4831 }
4832
4833 /* Before we invoke realpath, which can get expensive when many
4834 files are involved, do a quick comparison of the basenames. */
4835 if (!basenames_may_differ
4836 && !file_matcher (lbasename (file_data->file_names[j]),
4837 true))
4838 continue;
4839
976ca316 4840 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4841 if (file_matcher (this_real_name, false))
4842 {
4843 per_cu->v.quick->mark = 1;
4844 break;
4845 }
4846 }
4847
b76e467d
SM
4848 void **slot = htab_find_slot (per_cu->v.quick->mark
4849 ? visited_found.get ()
4850 : visited_not_found.get (),
4851 file_data, INSERT);
927aa2e7
JK
4852 *slot = file_data;
4853 }
4854}
4855
4856static void
4857dw2_expand_symtabs_matching
4858 (struct objfile *objfile,
4859 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4860 const lookup_name_info *lookup_name,
927aa2e7
JK
4861 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4862 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4863 enum search_domain kind)
4864{
976ca316 4865 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4866
4867 /* index_table is NULL if OBJF_READNOW. */
976ca316 4868 if (!per_objfile->per_bfd->index_table)
927aa2e7
JK
4869 return;
4870
976ca316 4871 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4872
c1a66c06
TV
4873 if (symbol_matcher == NULL && lookup_name == NULL)
4874 {
976ca316 4875 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4876 {
4877 QUIT;
4878
976ca316 4879 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
97a1449a 4880 file_matcher, expansion_notify);
c1a66c06
TV
4881 }
4882 return;
4883 }
4884
976ca316 4885 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4886
c1a66c06 4887 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7 4888 symbol_matcher,
b64f703b 4889 [&] (offset_type idx)
927aa2e7 4890 {
976ca316
SM
4891 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4892 kind);
3b00ef10 4893 return true;
976ca316 4894 }, per_objfile);
927aa2e7
JK
4895}
4896
39298a5d
TT
4897void
4898dwarf2_gdb_index::expand_symtabs_matching
4899 (struct objfile *objfile,
4900 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4901 const lookup_name_info *lookup_name,
4902 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4903 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4904 enum search_domain kind)
4905{
4906 dw2_expand_symtabs_matching (objfile, file_matcher, lookup_name,
4907 symbol_matcher, expansion_notify, kind);
4908}
4909
927aa2e7
JK
4910/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4911 symtab. */
4912
4913static struct compunit_symtab *
4914recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4915 CORE_ADDR pc)
4916{
4917 int i;
4918
4919 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4920 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4921 return cust;
4922
4923 if (cust->includes == NULL)
4924 return NULL;
4925
4926 for (i = 0; cust->includes[i]; ++i)
4927 {
4928 struct compunit_symtab *s = cust->includes[i];
4929
4930 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4931 if (s != NULL)
4932 return s;
4933 }
4934
4935 return NULL;
4936}
4937
39298a5d
TT
4938struct compunit_symtab *
4939dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4940 (struct objfile *objfile,
4941 struct bound_minimal_symbol msymbol,
4942 CORE_ADDR pc,
4943 struct obj_section *section,
4944 int warn_if_readin)
927aa2e7
JK
4945{
4946 struct dwarf2_per_cu_data *data;
4947 struct compunit_symtab *result;
4948
efd7398e
TT
4949 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4950 if (per_objfile->per_bfd->index_addrmap == nullptr)
927aa2e7
JK
4951 return NULL;
4952
b3b3bada 4953 CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e
TT
4954 data = ((struct dwarf2_per_cu_data *)
4955 addrmap_find (per_objfile->per_bfd->index_addrmap,
4956 pc - baseaddr));
927aa2e7
JK
4957 if (!data)
4958 return NULL;
4959
af758d11 4960 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4961 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4962 paddress (objfile->arch (), pc));
927aa2e7 4963
97a1449a
SM
4964 result = recursively_find_pc_sect_compunit_symtab
4965 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4966
927aa2e7
JK
4967 gdb_assert (result != NULL);
4968 return result;
4969}
4970
39298a5d 4971void
f4655dee
TT
4972dwarf2_base_index_functions::map_symbol_filenames
4973 (struct objfile *objfile,
4974 gdb::function_view<symbol_filename_ftype> fun,
4975 bool need_fullname)
927aa2e7 4976{
976ca316 4977 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4978
976ca316 4979 if (!per_objfile->per_bfd->filenames_cache)
927aa2e7 4980 {
976ca316 4981 per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4982
4983 htab_up visited (htab_create_alloc (10,
4984 htab_hash_pointer, htab_eq_pointer,
4985 NULL, xcalloc, xfree));
4986
4987 /* The rule is CUs specify all the files, including those used
4988 by any TU, so there's no need to scan TUs here. We can
4989 ignore file names coming from already-expanded CUs. */
4990
976ca316 4991 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4992 {
976ca316 4993 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4994 {
4995 void **slot = htab_find_slot (visited.get (),
4996 per_cu->v.quick->file_names,
4997 INSERT);
4998
4999 *slot = per_cu->v.quick->file_names;
5000 }
5001 }
5002
976ca316 5003 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 5004 {
927aa2e7 5005 /* We only need to look at symtabs not already expanded. */
976ca316 5006 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5007 continue;
5008
ab432490 5009 quick_file_names *file_data
976ca316 5010 = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
5011 if (file_data == NULL)
5012 continue;
5013
b76e467d 5014 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5015 if (*slot)
5016 {
5017 /* Already visited. */
5018 continue;
5019 }
5020 *slot = file_data;
5021
5022 for (int j = 0; j < file_data->num_file_names; ++j)
5023 {
5024 const char *filename = file_data->file_names[j];
976ca316 5025 per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
5026 }
5027 }
5028 }
5029
976ca316 5030 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
5031 {
5032 gdb::unique_xmalloc_ptr<char> this_real_name;
5033
5034 if (need_fullname)
5035 this_real_name = gdb_realpath (filename);
f4655dee 5036 fun (filename, this_real_name.get ());
927aa2e7
JK
5037 });
5038}
5039
39298a5d
TT
5040bool
5041dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
927aa2e7 5042{
fae2120b 5043 return true;
927aa2e7
JK
5044}
5045
927aa2e7
JK
5046/* DWARF-5 debug_names reader. */
5047
5048/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5049static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5050
5051/* A helper function that reads the .debug_names section in SECTION
5052 and fills in MAP. FILENAME is the name of the file containing the
5053 section; it is used for error reporting.
5054
5055 Returns true if all went well, false otherwise. */
5056
5057static bool
5058read_debug_names_from_section (struct objfile *objfile,
5059 const char *filename,
5060 struct dwarf2_section_info *section,
5061 mapped_debug_names &map)
5062{
96b79293 5063 if (section->empty ())
927aa2e7
JK
5064 return false;
5065
5066 /* Older elfutils strip versions could keep the section in the main
5067 executable while splitting it for the separate debug info file. */
96b79293 5068 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
5069 return false;
5070
96b79293 5071 section->read (objfile);
927aa2e7 5072
08feed99 5073 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
5074
5075 const gdb_byte *addr = section->buffer;
5076
96b79293 5077 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
5078
5079 unsigned int bytes_read;
5080 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5081 addr += bytes_read;
5082
5083 map.dwarf5_is_dwarf64 = bytes_read != 4;
5084 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5085 if (bytes_read + length != section->size)
5086 {
5087 /* There may be multiple per-CU indices. */
5088 warning (_("Section .debug_names in %s length %s does not match "
5089 "section length %s, ignoring .debug_names."),
5090 filename, plongest (bytes_read + length),
5091 pulongest (section->size));
5092 return false;
5093 }
5094
5095 /* The version number. */
5096 uint16_t version = read_2_bytes (abfd, addr);
5097 addr += 2;
5098 if (version != 5)
5099 {
5100 warning (_("Section .debug_names in %s has unsupported version %d, "
5101 "ignoring .debug_names."),
5102 filename, version);
5103 return false;
5104 }
5105
5106 /* Padding. */
5107 uint16_t padding = read_2_bytes (abfd, addr);
5108 addr += 2;
5109 if (padding != 0)
5110 {
5111 warning (_("Section .debug_names in %s has unsupported padding %d, "
5112 "ignoring .debug_names."),
5113 filename, padding);
5114 return false;
5115 }
5116
5117 /* comp_unit_count - The number of CUs in the CU list. */
5118 map.cu_count = read_4_bytes (abfd, addr);
5119 addr += 4;
5120
5121 /* local_type_unit_count - The number of TUs in the local TU
5122 list. */
5123 map.tu_count = read_4_bytes (abfd, addr);
5124 addr += 4;
5125
5126 /* foreign_type_unit_count - The number of TUs in the foreign TU
5127 list. */
5128 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5129 addr += 4;
5130 if (foreign_tu_count != 0)
5131 {
5132 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5133 "ignoring .debug_names."),
5134 filename, static_cast<unsigned long> (foreign_tu_count));
5135 return false;
5136 }
5137
5138 /* bucket_count - The number of hash buckets in the hash lookup
5139 table. */
5140 map.bucket_count = read_4_bytes (abfd, addr);
5141 addr += 4;
5142
5143 /* name_count - The number of unique names in the index. */
5144 map.name_count = read_4_bytes (abfd, addr);
5145 addr += 4;
5146
5147 /* abbrev_table_size - The size in bytes of the abbreviations
5148 table. */
5149 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5150 addr += 4;
5151
5152 /* augmentation_string_size - The size in bytes of the augmentation
5153 string. This value is rounded up to a multiple of 4. */
5154 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5155 addr += 4;
5156 map.augmentation_is_gdb = ((augmentation_string_size
5157 == sizeof (dwarf5_augmentation))
5158 && memcmp (addr, dwarf5_augmentation,
5159 sizeof (dwarf5_augmentation)) == 0);
5160 augmentation_string_size += (-augmentation_string_size) & 3;
5161 addr += augmentation_string_size;
5162
5163 /* List of CUs */
5164 map.cu_table_reordered = addr;
5165 addr += map.cu_count * map.offset_size;
5166
5167 /* List of Local TUs */
5168 map.tu_table_reordered = addr;
5169 addr += map.tu_count * map.offset_size;
5170
5171 /* Hash Lookup Table */
5172 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5173 addr += map.bucket_count * 4;
5174 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5175 addr += map.name_count * 4;
5176
5177 /* Name Table */
5178 map.name_table_string_offs_reordered = addr;
5179 addr += map.name_count * map.offset_size;
5180 map.name_table_entry_offs_reordered = addr;
5181 addr += map.name_count * map.offset_size;
5182
5183 const gdb_byte *abbrev_table_start = addr;
5184 for (;;)
5185 {
927aa2e7
JK
5186 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5187 addr += bytes_read;
5188 if (index_num == 0)
5189 break;
5190
5191 const auto insertpair
5192 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5193 if (!insertpair.second)
5194 {
5195 warning (_("Section .debug_names in %s has duplicate index %s, "
5196 "ignoring .debug_names."),
5197 filename, pulongest (index_num));
5198 return false;
5199 }
5200 mapped_debug_names::index_val &indexval = insertpair.first->second;
5201 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5202 addr += bytes_read;
5203
5204 for (;;)
5205 {
5206 mapped_debug_names::index_val::attr attr;
5207 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5208 addr += bytes_read;
5209 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5210 addr += bytes_read;
5211 if (attr.form == DW_FORM_implicit_const)
5212 {
5213 attr.implicit_const = read_signed_leb128 (abfd, addr,
5214 &bytes_read);
5215 addr += bytes_read;
5216 }
5217 if (attr.dw_idx == 0 && attr.form == 0)
5218 break;
5219 indexval.attr_vec.push_back (std::move (attr));
5220 }
5221 }
5222 if (addr != abbrev_table_start + abbrev_table_size)
5223 {
5224 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5225 "of size %s vs. written as %u, ignoring .debug_names."),
5226 filename, plongest (addr - abbrev_table_start),
5227 abbrev_table_size);
927aa2e7
JK
5228 return false;
5229 }
5230 map.entry_pool = addr;
5231
5232 return true;
5233}
5234
5235/* A helper for create_cus_from_debug_names that handles the MAP's CU
5236 list. */
5237
5238static void
168c9250 5239create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5240 const mapped_debug_names &map,
5241 dwarf2_section_info &section,
b76e467d 5242 bool is_dwz)
927aa2e7 5243{
3ee6bb11
TV
5244 if (!map.augmentation_is_gdb)
5245 {
fc9a13fb
TV
5246 for (uint32_t i = 0; i < map.cu_count; ++i)
5247 {
5248 sect_offset sect_off
5249 = (sect_offset) (extract_unsigned_integer
5250 (map.cu_table_reordered + i * map.offset_size,
5251 map.offset_size,
5252 map.dwarf5_byte_order));
5253 /* We don't know the length of the CU, because the CU list in a
5254 .debug_names index can be incomplete, so we can't use the start
5255 of the next CU as end of this CU. We create the CUs here with
5256 length 0, and in cutu_reader::cutu_reader we'll fill in the
5257 actual length. */
5258 dwarf2_per_cu_data *per_cu
5259 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5260 sect_off, 0);
5261 per_bfd->all_comp_units.push_back (per_cu);
5262 }
d3b54e63 5263 return;
3ee6bb11
TV
5264 }
5265
927aa2e7
JK
5266 sect_offset sect_off_prev;
5267 for (uint32_t i = 0; i <= map.cu_count; ++i)
5268 {
5269 sect_offset sect_off_next;
5270 if (i < map.cu_count)
5271 {
5272 sect_off_next
5273 = (sect_offset) (extract_unsigned_integer
5274 (map.cu_table_reordered + i * map.offset_size,
5275 map.offset_size,
5276 map.dwarf5_byte_order));
5277 }
5278 else
5279 sect_off_next = (sect_offset) section.size;
5280 if (i >= 1)
5281 {
5282 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5283 dwarf2_per_cu_data *per_cu
168c9250 5284 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5285 sect_off_prev, length);
168c9250 5286 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5287 }
5288 sect_off_prev = sect_off_next;
5289 }
5290}
5291
5292/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5293 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5294
5295static void
168c9250 5296create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5297 const mapped_debug_names &map,
5298 const mapped_debug_names &dwz_map)
5299{
168c9250
SM
5300 gdb_assert (per_bfd->all_comp_units.empty ());
5301 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5302
168c9250 5303 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5304 false /* is_dwz */);
927aa2e7
JK
5305
5306 if (dwz_map.cu_count == 0)
5307 return;
5308
168c9250
SM
5309 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5310 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5311 true /* is_dwz */);
927aa2e7
JK
5312}
5313
5314/* Read .debug_names. If everything went ok, initialize the "quick"
5315 elements of all the CUs and return true. Otherwise, return false. */
5316
5317static bool
976ca316 5318dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 5319{
fcf23d5b
SM
5320 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5321 mapped_debug_names dwz_map;
976ca316 5322 struct objfile *objfile = per_objfile->objfile;
f8c41851 5323 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
5324
5325 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
bab287cd 5326 &per_bfd->debug_names, *map))
927aa2e7
JK
5327 return false;
5328
5329 /* Don't use the index if it's empty. */
22ca247e 5330 if (map->name_count == 0)
927aa2e7
JK
5331 return false;
5332
5333 /* If there is a .dwz file, read it so we can get its CU list as
5334 well. */
f8c41851 5335 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
5336 if (dwz != NULL)
5337 {
5338 if (!read_debug_names_from_section (objfile,
00f93c44 5339 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5340 &dwz->debug_names, dwz_map))
5341 {
5342 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5343 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5344 return false;
5345 }
5346 }
5347
f8c41851 5348 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 5349
22ca247e 5350 if (map->tu_count != 0)
927aa2e7
JK
5351 {
5352 /* We can only handle a single .debug_types when we have an
5353 index. */
f8c41851 5354 if (per_bfd->types.size () != 1)
927aa2e7
JK
5355 return false;
5356
f8c41851 5357 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
5358
5359 create_signatured_type_table_from_debug_names
f8c41851 5360 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
5361 }
5362
f8c41851 5363 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 5364
f8c41851
SM
5365 per_bfd->debug_names_table = std::move (map);
5366 per_bfd->using_index = 1;
5367 per_bfd->quick_file_names_table =
bab287cd 5368 create_quick_file_names_table (per_bfd->all_comp_units.size ());
927aa2e7
JK
5369
5370 return true;
5371}
5372
927aa2e7
JK
5373/* Type used to manage iterating over all CUs looking for a symbol for
5374 .debug_names. */
5375
5376class dw2_debug_names_iterator
5377{
5378public:
927aa2e7 5379 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5380 gdb::optional<block_enum> block_index,
5381 domain_enum domain,
fcf23d5b 5382 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5383 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5384 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5385 m_per_objfile (per_objfile)
927aa2e7
JK
5386 {}
5387
5388 dw2_debug_names_iterator (const mapped_debug_names &map,
fcf23d5b 5389 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5390 : m_map (map),
5391 m_search (search),
fcf23d5b
SM
5392 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5393 m_per_objfile (per_objfile)
927aa2e7
JK
5394 {}
5395
3b00ef10
TT
5396 dw2_debug_names_iterator (const mapped_debug_names &map,
5397 block_enum block_index, domain_enum domain,
fcf23d5b 5398 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5399 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5400 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5401 m_per_objfile (per_objfile)
3b00ef10
TT
5402 {}
5403
927aa2e7
JK
5404 /* Return the next matching CU or NULL if there are no more. */
5405 dwarf2_per_cu_data *next ();
5406
5407private:
5408 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5409 const char *name,
5410 dwarf2_per_objfile *per_objfile);
927aa2e7 5411 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5412 uint32_t namei,
5413 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5414
5415 /* The internalized form of .debug_names. */
5416 const mapped_debug_names &m_map;
5417
2b79f376
SM
5418 /* If set, only look for symbols that match that block. Valid values are
5419 GLOBAL_BLOCK and STATIC_BLOCK. */
5420 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5421
5422 /* The kind of symbol we're looking for. */
5423 const domain_enum m_domain = UNDEF_DOMAIN;
5424 const search_domain m_search = ALL_DOMAIN;
5425
5426 /* The list of CUs from the index entry of the symbol, or NULL if
5427 not found. */
5428 const gdb_byte *m_addr;
fcf23d5b
SM
5429
5430 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5431};
5432
5433const char *
fcf23d5b 5434mapped_debug_names::namei_to_name
976ca316 5435 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5436{
5437 const ULONGEST namei_string_offs
5438 = extract_unsigned_integer ((name_table_string_offs_reordered
5439 + namei * offset_size),
5440 offset_size,
5441 dwarf5_byte_order);
976ca316 5442 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5443}
5444
5445/* Find a slot in .debug_names for the object named NAME. If NAME is
5446 found, return pointer to its pool data. If NAME cannot be found,
5447 return NULL. */
5448
5449const gdb_byte *
5450dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5451 (const mapped_debug_names &map, const char *name,
5452 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5453{
5454 int (*cmp) (const char *, const char *);
5455
54ee4252 5456 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5457 if (current_language->la_language == language_cplus
5458 || current_language->la_language == language_fortran
5459 || current_language->la_language == language_d)
5460 {
5461 /* NAME is already canonical. Drop any qualifiers as
5462 .debug_names does not contain any. */
5463
5464 if (strchr (name, '(') != NULL)
5465 {
54ee4252 5466 without_params = cp_remove_params (name);
927aa2e7 5467 if (without_params != NULL)
54ee4252 5468 name = without_params.get ();
927aa2e7
JK
5469 }
5470 }
5471
5472 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5473
5474 const uint32_t full_hash = dwarf5_djb_hash (name);
5475 uint32_t namei
5476 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5477 (map.bucket_table_reordered
5478 + (full_hash % map.bucket_count)), 4,
5479 map.dwarf5_byte_order);
5480 if (namei == 0)
5481 return NULL;
5482 --namei;
5483 if (namei >= map.name_count)
5484 {
b98664d3 5485 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5486 "[in module %s]"),
5487 namei, map.name_count,
fcf23d5b 5488 objfile_name (per_objfile->objfile));
927aa2e7
JK
5489 return NULL;
5490 }
5491
5492 for (;;)
5493 {
5494 const uint32_t namei_full_hash
5495 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5496 (map.hash_table_reordered + namei), 4,
5497 map.dwarf5_byte_order);
5498 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5499 return NULL;
5500
5501 if (full_hash == namei_full_hash)
5502 {
fcf23d5b 5503 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5504
5505#if 0 /* An expensive sanity check. */
5506 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5507 {
b98664d3 5508 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5509 "[in module %s]"),
5510 namei, objfile_name (dwarf2_per_objfile->objfile));
5511 return NULL;
5512 }
5513#endif
5514
5515 if (cmp (namei_string, name) == 0)
5516 {
5517 const ULONGEST namei_entry_offs
5518 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5519 + namei * map.offset_size),
5520 map.offset_size, map.dwarf5_byte_order);
5521 return map.entry_pool + namei_entry_offs;
5522 }
5523 }
5524
5525 ++namei;
5526 if (namei >= map.name_count)
5527 return NULL;
5528 }
5529}
5530
5531const gdb_byte *
5532dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5533 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5534{
5535 if (namei >= map.name_count)
5536 {
b98664d3 5537 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5538 "[in module %s]"),
5539 namei, map.name_count,
fcf23d5b 5540 objfile_name (per_objfile->objfile));
927aa2e7
JK
5541 return NULL;
5542 }
5543
5544 const ULONGEST namei_entry_offs
5545 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5546 + namei * map.offset_size),
5547 map.offset_size, map.dwarf5_byte_order);
5548 return map.entry_pool + namei_entry_offs;
5549}
5550
5551/* See dw2_debug_names_iterator. */
5552
5553dwarf2_per_cu_data *
5554dw2_debug_names_iterator::next ()
5555{
5556 if (m_addr == NULL)
5557 return NULL;
5558
fcf23d5b
SM
5559 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5560 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5561 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5562
5563 again:
5564
5565 unsigned int bytes_read;
5566 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5567 m_addr += bytes_read;
5568 if (abbrev == 0)
5569 return NULL;
5570
5571 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5572 if (indexval_it == m_map.abbrev_map.cend ())
5573 {
b98664d3 5574 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5575 "[in module %s]"),
ed2dc618 5576 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5577 return NULL;
5578 }
5579 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5580 enum class symbol_linkage {
5581 unknown,
5582 static_,
5583 extern_,
23c13d42 5584 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5585 dwarf2_per_cu_data *per_cu = NULL;
5586 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5587 {
5588 ULONGEST ull;
5589 switch (attr.form)
5590 {
5591 case DW_FORM_implicit_const:
5592 ull = attr.implicit_const;
5593 break;
5594 case DW_FORM_flag_present:
5595 ull = 1;
5596 break;
5597 case DW_FORM_udata:
5598 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5599 m_addr += bytes_read;
5600 break;
6dc55ce9 5601 case DW_FORM_ref4:
5602 ull = read_4_bytes (abfd, m_addr);
5603 m_addr += 4;
5604 break;
5605 case DW_FORM_ref8:
5606 ull = read_8_bytes (abfd, m_addr);
5607 m_addr += 8;
5608 break;
5609 case DW_FORM_ref_sig8:
5610 ull = read_8_bytes (abfd, m_addr);
5611 m_addr += 8;
5612 break;
927aa2e7 5613 default:
b98664d3 5614 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5615 dwarf_form_name (attr.form),
ed2dc618 5616 objfile_name (objfile));
927aa2e7
JK
5617 return NULL;
5618 }
5619 switch (attr.dw_idx)
5620 {
5621 case DW_IDX_compile_unit:
5622 /* Don't crash on bad data. */
bab287cd 5623 if (ull >= per_bfd->all_comp_units.size ())
927aa2e7 5624 {
b98664d3 5625 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5626 " [in module %s]"),
5627 pulongest (ull),
fcf23d5b 5628 objfile_name (objfile));
927aa2e7
JK
5629 continue;
5630 }
fcf23d5b 5631 per_cu = per_bfd->get_cutu (ull);
927aa2e7 5632 break;
8af5c486
JK
5633 case DW_IDX_type_unit:
5634 /* Don't crash on bad data. */
fcf23d5b 5635 if (ull >= per_bfd->all_type_units.size ())
8af5c486 5636 {
b98664d3 5637 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5638 " [in module %s]"),
5639 pulongest (ull),
fcf23d5b 5640 objfile_name (objfile));
8af5c486
JK
5641 continue;
5642 }
fcf23d5b 5643 per_cu = &per_bfd->get_tu (ull)->per_cu;
8af5c486 5644 break;
6dc55ce9 5645 case DW_IDX_die_offset:
5646 /* In a per-CU index (as opposed to a per-module index), index
5647 entries without CU attribute implicitly refer to the single CU. */
5648 if (per_cu == NULL)
fcf23d5b 5649 per_cu = per_bfd->get_cu (0);
6dc55ce9 5650 break;
927aa2e7
JK
5651 case DW_IDX_GNU_internal:
5652 if (!m_map.augmentation_is_gdb)
5653 break;
23c13d42 5654 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5655 break;
5656 case DW_IDX_GNU_external:
5657 if (!m_map.augmentation_is_gdb)
5658 break;
23c13d42 5659 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5660 break;
5661 }
5662 }
5663
5664 /* Skip if already read in. */
fcf23d5b 5665 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5666 goto again;
5667
5668 /* Check static vs global. */
23c13d42 5669 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5670 {
2b79f376 5671 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5672 const bool symbol_is_static =
5673 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5674 if (want_static != symbol_is_static)
2b79f376 5675 goto again;
927aa2e7
JK
5676 }
5677
5678 /* Match dw2_symtab_iter_next, symbol_kind
5679 and debug_names::psymbol_tag. */
5680 switch (m_domain)
5681 {
5682 case VAR_DOMAIN:
5683 switch (indexval.dwarf_tag)
5684 {
5685 case DW_TAG_variable:
5686 case DW_TAG_subprogram:
5687 /* Some types are also in VAR_DOMAIN. */
5688 case DW_TAG_typedef:
5689 case DW_TAG_structure_type:
5690 break;
5691 default:
5692 goto again;
5693 }
5694 break;
5695 case STRUCT_DOMAIN:
5696 switch (indexval.dwarf_tag)
5697 {
5698 case DW_TAG_typedef:
5699 case DW_TAG_structure_type:
5700 break;
5701 default:
5702 goto again;
5703 }
5704 break;
5705 case LABEL_DOMAIN:
5706 switch (indexval.dwarf_tag)
5707 {
5708 case 0:
5709 case DW_TAG_variable:
5710 break;
5711 default:
5712 goto again;
5713 }
5714 break;
59c35742
AB
5715 case MODULE_DOMAIN:
5716 switch (indexval.dwarf_tag)
5717 {
5718 case DW_TAG_module:
5719 break;
5720 default:
5721 goto again;
5722 }
5723 break;
927aa2e7
JK
5724 default:
5725 break;
5726 }
5727
5728 /* Match dw2_expand_symtabs_matching, symbol_kind and
5729 debug_names::psymbol_tag. */
5730 switch (m_search)
4b514bc8 5731 {
927aa2e7
JK
5732 case VARIABLES_DOMAIN:
5733 switch (indexval.dwarf_tag)
4b514bc8 5734 {
927aa2e7
JK
5735 case DW_TAG_variable:
5736 break;
5737 default:
5738 goto again;
4b514bc8 5739 }
927aa2e7
JK
5740 break;
5741 case FUNCTIONS_DOMAIN:
5742 switch (indexval.dwarf_tag)
4b514bc8 5743 {
927aa2e7
JK
5744 case DW_TAG_subprogram:
5745 break;
5746 default:
5747 goto again;
4b514bc8 5748 }
927aa2e7
JK
5749 break;
5750 case TYPES_DOMAIN:
5751 switch (indexval.dwarf_tag)
5752 {
5753 case DW_TAG_typedef:
5754 case DW_TAG_structure_type:
5755 break;
5756 default:
5757 goto again;
5758 }
5759 break;
59c35742
AB
5760 case MODULES_DOMAIN:
5761 switch (indexval.dwarf_tag)
5762 {
5763 case DW_TAG_module:
5764 break;
5765 default:
5766 goto again;
5767 }
927aa2e7
JK
5768 default:
5769 break;
4b514bc8 5770 }
927aa2e7
JK
5771
5772 return per_cu;
4b514bc8 5773}
61920122 5774
39298a5d
TT
5775struct compunit_symtab *
5776dwarf2_debug_names_index::lookup_symbol
5777 (struct objfile *objfile, block_enum block_index,
5778 const char *name, domain_enum domain)
4b514bc8 5779{
976ca316 5780 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
61920122 5781
976ca316 5782 const auto &mapp = per_objfile->per_bfd->debug_names_table;
927aa2e7 5783 if (!mapp)
61920122 5784 {
927aa2e7
JK
5785 /* index is NULL if OBJF_READNOW. */
5786 return NULL;
5787 }
5788 const auto &map = *mapp;
9291a0cd 5789
976ca316 5790 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
9703b513 5791
927aa2e7
JK
5792 struct compunit_symtab *stab_best = NULL;
5793 struct dwarf2_per_cu_data *per_cu;
5794 while ((per_cu = iter.next ()) != NULL)
5795 {
5796 struct symbol *sym, *with_opaque = NULL;
97a1449a 5797 compunit_symtab *stab
976ca316 5798 = dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7 5799 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5800 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5801
927aa2e7
JK
5802 sym = block_find_symbol (block, name, domain,
5803 block_find_non_opaque_type_preferred,
5804 &with_opaque);
9703b513 5805
927aa2e7
JK
5806 /* Some caution must be observed with overloaded functions and
5807 methods, since the index will not contain any overload
5808 information (but NAME might contain it). */
a3ec0bb1 5809
927aa2e7 5810 if (sym != NULL
987012b8 5811 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5812 return stab;
5813 if (with_opaque != NULL
987012b8 5814 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5815 stab_best = stab;
9703b513 5816
927aa2e7 5817 /* Keep looking through other CUs. */
9703b513
TT
5818 }
5819
927aa2e7 5820 return stab_best;
9703b513
TT
5821}
5822
927aa2e7
JK
5823/* This dumps minimal information about .debug_names. It is called
5824 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5825 uses this to verify that .debug_names has been loaded. */
9291a0cd 5826
39298a5d
TT
5827void
5828dwarf2_debug_names_index::dump (struct objfile *objfile)
927aa2e7 5829{
976ca316 5830 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5831
976ca316 5832 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5833 printf_filtered (".debug_names:");
976ca316 5834 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5835 printf_filtered (" exists\n");
5836 else
5837 printf_filtered (" faked for \"readnow\"\n");
5838 printf_filtered ("\n");
9291a0cd
TT
5839}
5840
39298a5d
TT
5841void
5842dwarf2_debug_names_index::expand_symtabs_for_function
5843 (struct objfile *objfile, const char *func_name)
9291a0cd 5844{
976ca316 5845 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 5846
976ca316
SM
5847 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5848 if (per_objfile->per_bfd->debug_names_table)
24c79950 5849 {
976ca316 5850 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
24c79950 5851
fcf23d5b 5852 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
976ca316 5853 per_objfile);
24c79950 5854
927aa2e7
JK
5855 struct dwarf2_per_cu_data *per_cu;
5856 while ((per_cu = iter.next ()) != NULL)
976ca316 5857 dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7
JK
5858 }
5859}
24c79950 5860
39298a5d
TT
5861void
5862dwarf2_debug_names_index::map_matching_symbols
3b00ef10
TT
5863 (struct objfile *objfile,
5864 const lookup_name_info &name, domain_enum domain,
5865 int global,
5866 gdb::function_view<symbol_found_callback_ftype> callback,
5867 symbol_compare_ftype *ordered_compare)
5868{
976ca316 5869 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5870
5871 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5872 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5873 return;
5874
976ca316 5875 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5876 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5877
5878 const char *match_name = name.ada ().lookup_name ().c_str ();
5879 auto matcher = [&] (const char *symname)
5880 {
5881 if (ordered_compare == nullptr)
5882 return true;
5883 return ordered_compare (symname, match_name) == 0;
5884 };
5885
b64f703b 5886 dw2_expand_symtabs_matching_symbol (map, name, matcher,
3b00ef10
TT
5887 [&] (offset_type namei)
5888 {
5889 /* The name was matched, now expand corresponding CUs that were
5890 marked. */
fcf23d5b 5891 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
976ca316 5892 per_objfile);
3b00ef10
TT
5893
5894 struct dwarf2_per_cu_data *per_cu;
5895 while ((per_cu = iter.next ()) != NULL)
976ca316 5896 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5897 nullptr);
3b00ef10 5898 return true;
976ca316 5899 }, per_objfile);
3b00ef10
TT
5900
5901 /* It's a shame we couldn't do this inside the
5902 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5903 that have already been expanded. Instead, this loop matches what
5904 the psymtab code does. */
976ca316 5905 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3b00ef10 5906 {
976ca316 5907 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
af758d11 5908 if (symtab != nullptr)
3b00ef10
TT
5909 {
5910 const struct block *block
af758d11 5911 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5912 if (!iterate_over_symbols_terminated (block, name,
5913 domain, callback))
5914 break;
5915 }
5916 }
5917}
5918
39298a5d
TT
5919void
5920dwarf2_debug_names_index::expand_symtabs_matching
927aa2e7
JK
5921 (struct objfile *objfile,
5922 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5923 const lookup_name_info *lookup_name,
927aa2e7
JK
5924 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5925 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5926 enum search_domain kind)
5927{
976ca316 5928 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5929
927aa2e7 5930 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5931 if (!per_objfile->per_bfd->debug_names_table)
927aa2e7 5932 return;
9291a0cd 5933
976ca316 5934 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5935
c1a66c06
TV
5936 if (symbol_matcher == NULL && lookup_name == NULL)
5937 {
976ca316 5938 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5939 {
5940 QUIT;
5941
976ca316
SM
5942 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5943 expansion_notify);
c1a66c06
TV
5944 }
5945 return;
5946 }
5947
976ca316 5948 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5949
c1a66c06 5950 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e 5951 symbol_matcher,
b64f703b 5952 [&] (offset_type namei)
927aa2e7 5953 {
927aa2e7
JK
5954 /* The name was matched, now expand corresponding CUs that were
5955 marked. */
976ca316 5956 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
bbf2f4df 5957
927aa2e7
JK
5958 struct dwarf2_per_cu_data *per_cu;
5959 while ((per_cu = iter.next ()) != NULL)
976ca316
SM
5960 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5961 expansion_notify);
3b00ef10 5962 return true;
976ca316 5963 }, per_objfile);
9291a0cd
TT
5964}
5965
4485a1c1 5966/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5967 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5968
5969template <typename T>
5970static gdb::array_view<const gdb_byte>
5971get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5972{
5973 dwarf2_section_info *section = &section_owner->gdb_index;
5974
96b79293 5975 if (section->empty ())
4485a1c1
SM
5976 return {};
5977
5978 /* Older elfutils strip versions could keep the section in the main
5979 executable while splitting it for the separate debug info file. */
96b79293 5980 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5981 return {};
5982
96b79293 5983 section->read (obj);
4485a1c1 5984
8bebfcda
PA
5985 /* dwarf2_section_info::size is a bfd_size_type, while
5986 gdb::array_view works with size_t. On 32-bit hosts, with
5987 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5988 is 32-bit. So we need an explicit narrowing conversion here.
5989 This is fine, because it's impossible to allocate or mmap an
5990 array/buffer larger than what size_t can represent. */
5991 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5992}
5993
87d6a7aa
SM
5994/* Lookup the index cache for the contents of the index associated to
5995 DWARF2_OBJ. */
5996
5997static gdb::array_view<const gdb_byte>
5989a64e 5998get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5999{
6000 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6001 if (build_id == nullptr)
6002 return {};
6003
6004 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 6005 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
6006}
6007
6008/* Same as the above, but for DWZ. */
6009
6010static gdb::array_view<const gdb_byte>
6011get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6012{
6013 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6014 if (build_id == nullptr)
6015 return {};
6016
6017 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6018}
6019
edc02ceb 6020/* See dwarf2/public.h. */
9291a0cd 6021
edc02ceb
TT
6022void
6023dwarf2_initialize_objfile (struct objfile *objfile)
9291a0cd 6024{
976ca316
SM
6025 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6026 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 6027
850ed749
SM
6028 dwarf_read_debug_printf ("called");
6029
9291a0cd
TT
6030 /* If we're about to read full symbols, don't bother with the
6031 indices. In this case we also don't care if some other debug
6032 format is making psymtabs, because they are all about to be
6033 expanded anyway. */
6034 if ((objfile->flags & OBJF_READNOW))
6035 {
850ed749
SM
6036 dwarf_read_debug_printf ("readnow requested");
6037
17ee85fc
TT
6038 /* When using READNOW, the using_index flag (set below) indicates that
6039 PER_BFD was already initialized, when we loaded some other objfile. */
6040 if (per_bfd->using_index)
6041 {
850ed749 6042 dwarf_read_debug_printf ("using_index already set");
976ca316 6043 per_objfile->resize_symtabs ();
edc02ceb
TT
6044 objfile->qf.push_front (make_dwarf_gdb_index ());
6045 return;
17ee85fc
TT
6046 }
6047
6048 per_bfd->using_index = 1;
976ca316
SM
6049 create_all_comp_units (per_objfile);
6050 create_all_type_units (per_objfile);
17ee85fc
TT
6051 per_bfd->quick_file_names_table
6052 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 6053 per_objfile->resize_symtabs ();
9291a0cd 6054
17ee85fc
TT
6055 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6056 + per_bfd->all_type_units.size ()); ++i)
9291a0cd 6057 {
17ee85fc 6058 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
9291a0cd 6059
17ee85fc 6060 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 6061 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6062 }
6063
edc02ceb
TT
6064 /* Arrange for gdb to see the "quick" functions. However, these
6065 functions will be no-ops because we will have expanded all
6066 symtabs. */
6067 objfile->qf.push_front (make_dwarf_gdb_index ());
6068 return;
9291a0cd
TT
6069 }
6070
17ee85fc
TT
6071 /* Was a debug names index already read when we processed an objfile sharing
6072 PER_BFD? */
6073 if (per_bfd->debug_names_table != nullptr)
6074 {
850ed749 6075 dwarf_read_debug_printf ("re-using shared debug names table");
976ca316 6076 per_objfile->resize_symtabs ();
edc02ceb
TT
6077 objfile->qf.push_front (make_dwarf_debug_names ());
6078 return;
17ee85fc
TT
6079 }
6080
6081 /* Was a GDB index already read when we processed an objfile sharing
6082 PER_BFD? */
6083 if (per_bfd->index_table != nullptr)
6084 {
850ed749 6085 dwarf_read_debug_printf ("re-using shared index table");
976ca316 6086 per_objfile->resize_symtabs ();
edc02ceb
TT
6087 objfile->qf.push_front (make_dwarf_gdb_index ());
6088 return;
17ee85fc
TT
6089 }
6090
efb763a5
SM
6091 /* There might already be partial symtabs built for this BFD. This happens
6092 when loading the same binary twice with the index-cache enabled. If so,
6093 don't try to read an index. The objfile / per_objfile initialization will
6094 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6095 code path. */
6096 if (per_bfd->partial_symtabs != nullptr)
850ed749
SM
6097 {
6098 dwarf_read_debug_printf ("re-using shared partial symtabs");
edc02ceb
TT
6099 objfile->qf.push_front (make_lazy_dwarf_reader ());
6100 return;
850ed749 6101 }
efb763a5 6102
976ca316 6103 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a 6104 {
850ed749 6105 dwarf_read_debug_printf ("found debug names");
976ca316 6106 per_objfile->resize_symtabs ();
edc02ceb
TT
6107 objfile->qf.push_front (make_dwarf_debug_names ());
6108 return;
3c0aa29a 6109 }
927aa2e7 6110
976ca316 6111 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 6112 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 6113 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a 6114 {
850ed749 6115 dwarf_read_debug_printf ("found gdb index from file");
976ca316 6116 per_objfile->resize_symtabs ();
edc02ceb
TT
6117 objfile->qf.push_front (make_dwarf_gdb_index ());
6118 return;
3c0aa29a 6119 }
9291a0cd 6120
87d6a7aa 6121 /* ... otherwise, try to find the index in the index cache. */
976ca316 6122 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
6123 get_gdb_index_contents_from_cache,
6124 get_gdb_index_contents_from_cache_dwz))
6125 {
850ed749 6126 dwarf_read_debug_printf ("found gdb index from cache");
87d6a7aa 6127 global_index_cache.hit ();
976ca316 6128 per_objfile->resize_symtabs ();
edc02ceb
TT
6129 objfile->qf.push_front (make_dwarf_gdb_index ());
6130 return;
87d6a7aa
SM
6131 }
6132
6133 global_index_cache.miss ();
edc02ceb 6134 objfile->qf.push_front (make_lazy_dwarf_reader ());
9291a0cd
TT
6135}
6136
6137\f
6138
dce234bc
PP
6139/* Build a partial symbol table. */
6140
6141void
eb36a3eb 6142dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
dce234bc 6143{
976ca316
SM
6144 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6145 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
6146
6147 if (per_bfd->partial_symtabs != nullptr)
6148 {
6149 /* Partial symbols were already read, so now we can simply
6150 attach them. */
eb36a3eb
TT
6151 if (psf == nullptr)
6152 {
6153 psf = new psymbol_functions (per_bfd->partial_symtabs);
6154 objfile->qf.emplace_front (psf);
6155 }
6156 else
6157 psf->set_partial_symtabs (per_bfd->partial_symtabs);
976ca316 6158 per_objfile->resize_symtabs ();
17ee85fc
TT
6159 return;
6160 }
c9bf0622 6161
eb36a3eb
TT
6162 if (psf == nullptr)
6163 {
6164 psf = new psymbol_functions;
6165 objfile->qf.emplace_front (psf);
6166 }
6167 const std::shared_ptr<psymtab_storage> &partial_symtabs
6168 = psf->get_partial_symtabs ();
6169
51962708
TT
6170 /* Set the local reference to partial symtabs, so that we don't try
6171 to read them again if reading another objfile with the same BFD.
6172 If we can't in fact share, this won't make a difference anyway as
6173 the dwarf2_per_bfd object won't be shared. */
eb36a3eb 6174 per_bfd->partial_symtabs = partial_symtabs;
51962708 6175
a70b8144 6176 try
c9bf0622
TT
6177 {
6178 /* This isn't really ideal: all the data we allocate on the
6179 objfile's obstack is still uselessly kept around. However,
6180 freeing it seems unsafe. */
eb36a3eb 6181 psymtab_discarder psymtabs (partial_symtabs.get ());
976ca316 6182 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 6183 psymtabs.keep ();
87d6a7aa 6184
976ca316 6185 per_objfile->resize_symtabs ();
af758d11 6186
87d6a7aa 6187 /* (maybe) store an index in the cache. */
976ca316 6188 global_index_cache.store (per_objfile);
c9bf0622 6189 }
230d2906 6190 catch (const gdb_exception_error &except)
492d29ea
PA
6191 {
6192 exception_print (gdb_stderr, except);
6193 }
c906108c 6194}
c906108c 6195
3b80fe9b
DE
6196/* Find the base address of the compilation unit for range lists and
6197 location lists. It will normally be specified by DW_AT_low_pc.
6198 In DWARF-3 draft 4, the base address could be overridden by
6199 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6200 compilation units with discontinuous ranges. */
6201
6202static void
6203dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6204{
6205 struct attribute *attr;
6206
2b24b6e4 6207 cu->base_address.reset ();
3b80fe9b
DE
6208
6209 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6210 if (attr != nullptr)
95f982e5 6211 cu->base_address = attr->as_address ();
3b80fe9b
DE
6212 else
6213 {
6214 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6215 if (attr != nullptr)
95f982e5 6216 cu->base_address = attr->as_address ();
3b80fe9b
DE
6217 }
6218}
6219
36586728
TT
6220/* Helper function that returns the proper abbrev section for
6221 THIS_CU. */
6222
6223static struct dwarf2_section_info *
6224get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6225{
6226 struct dwarf2_section_info *abbrev;
c3699833 6227 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6228
6229 if (this_cu->is_dwz)
a7308ce0 6230 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
36586728 6231 else
c3699833 6232 abbrev = &per_bfd->abbrev;
36586728
TT
6233
6234 return abbrev;
6235}
6236
f4dc4d17
DE
6237/* Fetch the abbreviation table offset from a comp or type unit header. */
6238
6239static sect_offset
976ca316 6240read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 6241 struct dwarf2_section_info *section,
9c541725 6242 sect_offset sect_off)
f4dc4d17 6243{
96b79293 6244 bfd *abfd = section->get_bfd_owner ();
d521ce57 6245 const gdb_byte *info_ptr;
ac298888 6246 unsigned int initial_length_size, offset_size;
43988095 6247 uint16_t version;
f4dc4d17 6248
976ca316 6249 section->read (per_objfile->objfile);
9c541725 6250 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6251 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6252 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6253 info_ptr += initial_length_size;
6254
6255 version = read_2_bytes (abfd, info_ptr);
6256 info_ptr += 2;
6257 if (version >= 5)
6258 {
6259 /* Skip unit type and address size. */
6260 info_ptr += 2;
6261 }
6262
24aa364d 6263 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6264}
6265
b83470bf
TT
6266/* A partial symtab that is used only for include files. */
6267struct dwarf2_include_psymtab : public partial_symtab
6268{
7b249e47
TT
6269 dwarf2_include_psymtab (const char *filename,
6270 psymtab_storage *partial_symtabs,
6271 struct objfile *objfile)
6272 : partial_symtab (filename, partial_symtabs, objfile)
b83470bf
TT
6273 {
6274 }
6275
6276 void read_symtab (struct objfile *objfile) override
6277 {
194d088f
TV
6278 /* It's an include file, no symbols to read for it.
6279 Everything is in the includer symtab. */
6280
6281 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6282 expansion of the includer psymtab. We use the dependencies[0] field to
6283 model the includer. But if we go the regular route of calling
6284 expand_psymtab here, and having expand_psymtab call expand_dependencies
6285 to expand the includer, we'll only use expand_psymtab on the includer
6286 (making it a non-toplevel psymtab), while if we expand the includer via
6287 another path, we'll use read_symtab (making it a toplevel psymtab).
6288 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6289 psymtab, and trigger read_symtab on the includer here directly. */
6290 includer ()->read_symtab (objfile);
b83470bf
TT
6291 }
6292
6293 void expand_psymtab (struct objfile *objfile) override
6294 {
194d088f
TV
6295 /* This is not called by read_symtab, and should not be called by any
6296 expand_dependencies. */
6297 gdb_assert (false);
b83470bf
TT
6298 }
6299
5717c425 6300 bool readin_p (struct objfile *objfile) const override
b83470bf 6301 {
5717c425 6302 return includer ()->readin_p (objfile);
b83470bf
TT
6303 }
6304
5717c425 6305 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6306 {
6307 return nullptr;
6308 }
6309
6310private:
194d088f
TV
6311 partial_symtab *includer () const
6312 {
6313 /* An include psymtab has exactly one dependency: the psymtab that
6314 includes it. */
6315 gdb_assert (this->number_of_dependencies == 1);
6316 return this->dependencies[0];
6317 }
b83470bf
TT
6318};
6319
aaa75496
JB
6320/* Allocate a new partial symtab for file named NAME and mark this new
6321 partial symtab as being an include of PST. */
6322
6323static void
84685904 6324dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
7b249e47
TT
6325 const char *name,
6326 dwarf2_psymtab *pst,
6327 psymtab_storage *partial_symtabs,
dda83cd7 6328 struct objfile *objfile)
aaa75496 6329{
7b249e47
TT
6330 dwarf2_include_psymtab *subpst
6331 = new dwarf2_include_psymtab (name, partial_symtabs, objfile);
aaa75496 6332
fbd9ab74 6333 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6334 subpst->dirname = pst->dirname;
fbd9ab74 6335
84685904 6336 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6337 subpst->dependencies[0] = pst;
6338 subpst->number_of_dependencies = 1;
aaa75496
JB
6339}
6340
6341/* Read the Line Number Program data and extract the list of files
6342 included by the source file represented by PST. Build an include
d85a05f0 6343 partial symtab for each of these included files. */
aaa75496
JB
6344
6345static void
6346dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6347 struct die_info *die,
891813be 6348 dwarf2_psymtab *pst)
aaa75496 6349{
fff8551c 6350 line_header_up lh;
d85a05f0 6351 struct attribute *attr;
aaa75496 6352
d85a05f0 6353 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e
TT
6354 if (attr != nullptr && attr->form_is_unsigned ())
6355 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
aaa75496
JB
6356 if (lh == NULL)
6357 return; /* No linetable, so no includes. */
6358
79748972
TT
6359 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6360 that we pass in the raw text_low here; that is ok because we're
6361 only decoding the line table to make include partial symtabs, and
6362 so the addresses aren't really used. */
4ae976d1 6363 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6364 pst->raw_text_low (), 1);
aaa75496
JB
6365}
6366
348e048f 6367static hashval_t
52dc124a 6368hash_signatured_type (const void *item)
348e048f 6369{
9a3c8263
SM
6370 const struct signatured_type *sig_type
6371 = (const struct signatured_type *) item;
9a619af0 6372
348e048f 6373 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6374 return sig_type->signature;
348e048f
DE
6375}
6376
6377static int
52dc124a 6378eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6379{
9a3c8263
SM
6380 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6381 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6382
348e048f
DE
6383 return lhs->signature == rhs->signature;
6384}
6385
1fd400ff
TT
6386/* Allocate a hash table for signatured types. */
6387
b0b6a987 6388static htab_up
298e9637 6389allocate_signatured_type_table ()
1fd400ff 6390{
b0b6a987
TT
6391 return htab_up (htab_create_alloc (41,
6392 hash_signatured_type,
6393 eq_signatured_type,
6394 NULL, xcalloc, xfree));
1fd400ff
TT
6395}
6396
d467dd73 6397/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6398
6399static int
d467dd73 6400add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6401{
9a3c8263 6402 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6403 std::vector<signatured_type *> *all_type_units
6404 = (std::vector<signatured_type *> *) datum;
1fd400ff 6405
b2bdb8cf 6406 all_type_units->push_back (sigt);
1fd400ff
TT
6407
6408 return 1;
6409}
6410
78d4d2c5 6411/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6412 and fill them into TYPES_HTAB. It will process only type units,
6413 therefore DW_UT_type. */
c88ee1f0 6414
78d4d2c5 6415static void
976ca316 6416create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6417 struct dwo_file *dwo_file,
b0b6a987 6418 dwarf2_section_info *section, htab_up &types_htab,
43988095 6419 rcuh_kind section_kind)
348e048f 6420{
976ca316 6421 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6422 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6423 bfd *abfd;
6424 const gdb_byte *info_ptr, *end_ptr;
348e048f 6425
4bdcc0c1
DE
6426 abbrev_section = (dwo_file != NULL
6427 ? &dwo_file->sections.abbrev
976ca316 6428 : &per_objfile->per_bfd->abbrev);
4bdcc0c1 6429
4800761a 6430 dwarf_read_debug_printf ("Reading %s for %s",
6f738b01
SM
6431 section->get_name (),
6432 abbrev_section->get_file_name ());
09406207 6433
96b79293 6434 section->read (objfile);
78d4d2c5 6435 info_ptr = section->buffer;
348e048f 6436
78d4d2c5
JK
6437 if (info_ptr == NULL)
6438 return;
348e048f 6439
78d4d2c5
JK
6440 /* We can't set abfd until now because the section may be empty or
6441 not present, in which case the bfd is unknown. */
96b79293 6442 abfd = section->get_bfd_owner ();
348e048f 6443
c0ab21c2
TT
6444 /* We don't use cutu_reader here because we don't need to read
6445 any dies: the signature is in the header. */
3019eac3 6446
78d4d2c5
JK
6447 end_ptr = info_ptr + section->size;
6448 while (info_ptr < end_ptr)
6449 {
78d4d2c5
JK
6450 struct signatured_type *sig_type;
6451 struct dwo_unit *dwo_tu;
6452 void **slot;
6453 const gdb_byte *ptr = info_ptr;
6454 struct comp_unit_head header;
6455 unsigned int length;
8b70b953 6456
9c541725 6457 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6458
a49dd8dd
JK
6459 /* Initialize it due to a false compiler warning. */
6460 header.signature = -1;
9c541725 6461 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6462
78d4d2c5
JK
6463 /* We need to read the type's signature in order to build the hash
6464 table, but we don't need anything else just yet. */
348e048f 6465
976ca316 6466 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6467 abbrev_section, ptr, section_kind);
348e048f 6468
4057dfde 6469 length = header.get_length ();
6caca83c 6470
78d4d2c5
JK
6471 /* Skip dummy type units. */
6472 if (ptr >= info_ptr + length
43988095 6473 || peek_abbrev_code (abfd, ptr) == 0
d2854d8d
CT
6474 || (header.unit_type != DW_UT_type
6475 && header.unit_type != DW_UT_split_type))
78d4d2c5
JK
6476 {
6477 info_ptr += length;
6478 continue;
6479 }
dee91e82 6480
78d4d2c5
JK
6481 if (types_htab == NULL)
6482 {
6483 if (dwo_file)
298e9637 6484 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6485 else
298e9637 6486 types_htab = allocate_signatured_type_table ();
78d4d2c5 6487 }
8b70b953 6488
78d4d2c5
JK
6489 if (dwo_file)
6490 {
6491 sig_type = NULL;
976ca316 6492 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
78d4d2c5 6493 dwo_tu->dwo_file = dwo_file;
43988095 6494 dwo_tu->signature = header.signature;
9c541725 6495 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6496 dwo_tu->section = section;
9c541725 6497 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6498 dwo_tu->length = length;
6499 }
6500 else
6501 {
6502 /* N.B.: type_offset is not usable if this type uses a DWO file.
6503 The real type_offset is in the DWO file. */
6504 dwo_tu = NULL;
976ca316 6505 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
43988095 6506 sig_type->signature = header.signature;
9c541725 6507 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6508 sig_type->per_cu.is_debug_types = 1;
6509 sig_type->per_cu.section = section;
9c541725 6510 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6511 sig_type->per_cu.length = length;
6512 }
6513
b0b6a987 6514 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6515 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6516 INSERT);
6517 gdb_assert (slot != NULL);
6518 if (*slot != NULL)
6519 {
9c541725 6520 sect_offset dup_sect_off;
0349ea22 6521
3019eac3
DE
6522 if (dwo_file)
6523 {
78d4d2c5
JK
6524 const struct dwo_unit *dup_tu
6525 = (const struct dwo_unit *) *slot;
6526
9c541725 6527 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6528 }
6529 else
6530 {
78d4d2c5
JK
6531 const struct signatured_type *dup_tu
6532 = (const struct signatured_type *) *slot;
6533
9c541725 6534 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6535 }
8b70b953 6536
b98664d3 6537 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6538 " the entry at offset %s, signature %s"),
6539 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6540 hex_string (header.signature));
78d4d2c5
JK
6541 }
6542 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6543
6f738b01
SM
6544 dwarf_read_debug_printf_v (" offset %s, signature %s",
6545 sect_offset_str (sect_off),
6546 hex_string (header.signature));
3019eac3 6547
78d4d2c5
JK
6548 info_ptr += length;
6549 }
6550}
3019eac3 6551
78d4d2c5
JK
6552/* Create the hash table of all entries in the .debug_types
6553 (or .debug_types.dwo) section(s).
6554 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6555 otherwise it is NULL.
b3c8eb43 6556
78d4d2c5 6557 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6558
78d4d2c5 6559 Note: This function processes DWO files only, not DWP files. */
348e048f 6560
78d4d2c5 6561static void
976ca316 6562create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6563 struct dwo_file *dwo_file,
fd5866f6 6564 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6565 htab_up &types_htab)
78d4d2c5 6566{
fd5866f6 6567 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6568 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6569 rcuh_kind::TYPE);
3019eac3
DE
6570}
6571
6572/* Create the hash table of all entries in the .debug_types section,
6573 and initialize all_type_units.
6574 The result is zero if there is an error (e.g. missing .debug_types section),
6575 otherwise non-zero. */
6576
6577static int
976ca316 6578create_all_type_units (dwarf2_per_objfile *per_objfile)
3019eac3 6579{
b0b6a987 6580 htab_up types_htab;
3019eac3 6581
976ca316
SM
6582 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6583 types_htab, rcuh_kind::COMPILE);
6584 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6585 types_htab);
3019eac3
DE
6586 if (types_htab == NULL)
6587 {
976ca316 6588 per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6589 return 0;
6590 }
6591
976ca316 6592 per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6593
976ca316
SM
6594 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6595 per_objfile->per_bfd->all_type_units.reserve
6596 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6597
976ca316 6598 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6599 add_signatured_type_cu_to_table,
976ca316 6600 &per_objfile->per_bfd->all_type_units);
1fd400ff 6601
348e048f
DE
6602 return 1;
6603}
6604
5989a64e 6605/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6606 If SLOT is non-NULL, it is the entry to use in the hash table.
6607 Otherwise we find one. */
6608
6609static struct signatured_type *
976ca316 6610add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6611{
976ca316
SM
6612 if (per_objfile->per_bfd->all_type_units.size ()
6613 == per_objfile->per_bfd->all_type_units.capacity ())
6614 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6615
976ca316 6616 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6617
976ca316 6618 per_objfile->resize_symtabs ();
af758d11 6619
976ca316 6620 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6621 sig_type->signature = sig;
6622 sig_type->per_cu.is_debug_types = 1;
976ca316 6623 if (per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6624 {
6625 sig_type->per_cu.v.quick =
976ca316 6626 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6627 struct dwarf2_per_cu_quick_data);
6628 }
6629
6630 if (slot == NULL)
6631 {
976ca316 6632 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6633 sig_type, INSERT);
6634 }
6635 gdb_assert (*slot == NULL);
6636 *slot = sig_type;
6637 /* The rest of sig_type must be filled in by the caller. */
6638 return sig_type;
6639}
6640
a2ce51a0
DE
6641/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6642 Fill in SIG_ENTRY with DWO_ENTRY. */
6643
6644static void
976ca316 6645fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6646 struct signatured_type *sig_entry,
6647 struct dwo_unit *dwo_entry)
6648{
976ca316 6649 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6650
7ee85ab1 6651 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6652 gdb_assert (! sig_entry->per_cu.queued);
976ca316 6653 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6654 if (per_bfd->using_index)
6aa5f3a6
DE
6655 {
6656 gdb_assert (sig_entry->per_cu.v.quick != NULL);
976ca316 6657 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6658 }
6659 else
6660 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6661 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6662 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6663 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6664 gdb_assert (sig_entry->dwo_unit == NULL);
6665
6666 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6667 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6668 sig_entry->per_cu.length = dwo_entry->length;
6669 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6670 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6671 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6672 sig_entry->dwo_unit = dwo_entry;
6673}
6674
6675/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6676 If we haven't read the TU yet, create the signatured_type data structure
6677 for a TU to be read in directly from a DWO file, bypassing the stub.
6678 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6679 using .gdb_index, then when reading a CU we want to stay in the DWO file
6680 containing that CU. Otherwise we could end up reading several other DWO
6681 files (due to comdat folding) to process the transitive closure of all the
6682 mentioned TUs, and that can be slow. The current DWO file will have every
6683 type signature that it needs.
a2ce51a0
DE
6684 We only do this for .gdb_index because in the psymtab case we already have
6685 to read all the DWOs to build the type unit groups. */
6686
6687static struct signatured_type *
6688lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6689{
976ca316 6690 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6691 struct dwo_file *dwo_file;
6692 struct dwo_unit find_dwo_entry, *dwo_entry;
6693 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6694 void **slot;
a2ce51a0 6695
976ca316 6696 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6697
6aa5f3a6
DE
6698 /* If TU skeletons have been removed then we may not have read in any
6699 TUs yet. */
976ca316
SM
6700 if (per_objfile->per_bfd->signatured_types == NULL)
6701 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6702
6703 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6704 Use the global signatured_types array to do our own comdat-folding
6705 of types. If this is the first time we're reading this TU, and
6706 the TU has an entry in .gdb_index, replace the recorded data from
6707 .gdb_index with this TU. */
a2ce51a0 6708
a2ce51a0 6709 find_sig_entry.signature = sig;
976ca316 6710 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6711 &find_sig_entry, INSERT);
9a3c8263 6712 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6713
6714 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6715 read. Don't reassign the global entry to point to this DWO if that's
6716 the case. Also note that if the TU is already being read, it may not
6717 have come from a DWO, the program may be a mix of Fission-compiled
6718 code and non-Fission-compiled code. */
6719
6720 /* Have we already tried to read this TU?
6721 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6722 needn't exist in the global table yet). */
6723 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6724 return sig_entry;
6725
6aa5f3a6
DE
6726 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6727 dwo_unit of the TU itself. */
6728 dwo_file = cu->dwo_unit->dwo_file;
6729
a2ce51a0
DE
6730 /* Ok, this is the first time we're reading this TU. */
6731 if (dwo_file->tus == NULL)
6732 return NULL;
6733 find_dwo_entry.signature = sig;
b0b6a987
TT
6734 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6735 &find_dwo_entry);
a2ce51a0
DE
6736 if (dwo_entry == NULL)
6737 return NULL;
6738
6aa5f3a6
DE
6739 /* If the global table doesn't have an entry for this TU, add one. */
6740 if (sig_entry == NULL)
976ca316 6741 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6742
976ca316 6743 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
89e63ee4 6744 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6745 return sig_entry;
6746}
6747
a2ce51a0
DE
6748/* Subroutine of lookup_signatured_type.
6749 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6750 then try the DWP file. If the TU stub (skeleton) has been removed then
6751 it won't be in .gdb_index. */
a2ce51a0
DE
6752
6753static struct signatured_type *
6754lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6755{
976ca316
SM
6756 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6757 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6758 struct dwo_unit *dwo_entry;
6759 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6760 void **slot;
a2ce51a0 6761
976ca316 6762 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6763 gdb_assert (dwp_file != NULL);
6764
6aa5f3a6
DE
6765 /* If TU skeletons have been removed then we may not have read in any
6766 TUs yet. */
976ca316
SM
6767 if (per_objfile->per_bfd->signatured_types == NULL)
6768 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6769
6aa5f3a6 6770 find_sig_entry.signature = sig;
976ca316 6771 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6772 &find_sig_entry, INSERT);
9a3c8263 6773 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6774
6775 /* Have we already tried to read this TU?
6776 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6777 needn't exist in the global table yet). */
6778 if (sig_entry != NULL)
6779 return sig_entry;
6780
a2ce51a0
DE
6781 if (dwp_file->tus == NULL)
6782 return NULL;
976ca316
SM
6783 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6784 1 /* is_debug_types */);
a2ce51a0
DE
6785 if (dwo_entry == NULL)
6786 return NULL;
6787
976ca316
SM
6788 sig_entry = add_type_unit (per_objfile, sig, slot);
6789 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6790
a2ce51a0
DE
6791 return sig_entry;
6792}
6793
380bca97 6794/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6795 Returns NULL if signature SIG is not present in the table.
6796 It is up to the caller to complain about this. */
348e048f
DE
6797
6798static struct signatured_type *
a2ce51a0 6799lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6800{
976ca316 6801 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6802
976ca316 6803 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6804 {
6805 /* We're in a DWO/DWP file, and we're using .gdb_index.
6806 These cases require special processing. */
976ca316 6807 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6808 return lookup_dwo_signatured_type (cu, sig);
6809 else
6810 return lookup_dwp_signatured_type (cu, sig);
6811 }
6812 else
6813 {
6814 struct signatured_type find_entry, *entry;
348e048f 6815
976ca316 6816 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6817 return NULL;
6818 find_entry.signature = sig;
9a3c8263 6819 entry = ((struct signatured_type *)
976ca316 6820 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6821 &find_entry));
a2ce51a0
DE
6822 return entry;
6823 }
348e048f 6824}
18a8505e 6825
42e7ad6c 6826/* Low level DIE reading support. */
348e048f 6827
d85a05f0
DJ
6828/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6829
6830static void
6831init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6832 struct dwarf2_cu *cu,
3019eac3 6833 struct dwarf2_section_info *section,
685af9cd
TT
6834 struct dwo_file *dwo_file,
6835 struct abbrev_table *abbrev_table)
d85a05f0 6836{
fceca515 6837 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6838 reader->abfd = section->get_bfd_owner ();
d85a05f0 6839 reader->cu = cu;
3019eac3 6840 reader->dwo_file = dwo_file;
dee91e82
DE
6841 reader->die_section = section;
6842 reader->buffer = section->buffer;
f664829e 6843 reader->buffer_end = section->buffer + section->size;
685af9cd 6844 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6845}
6846
c0ab21c2 6847/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6848 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6849 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6850 already.
6851
6852 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6853 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6854 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6855 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6856 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6857 STUB_COMP_DIR may be non-NULL.
3e225074 6858 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6859 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6860 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6861 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6862 kept around for at least as long as *RESULT_READER.
6863
b0c7bfa9
DE
6864 The result is non-zero if a valid (non-dummy) DIE was found. */
6865
6866static int
4ab09049 6867read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6868 struct dwo_unit *dwo_unit,
b0c7bfa9 6869 struct die_info *stub_comp_unit_die,
a2ce51a0 6870 const char *stub_comp_dir,
b0c7bfa9 6871 struct die_reader_specs *result_reader,
d521ce57 6872 const gdb_byte **result_info_ptr,
b0c7bfa9 6873 struct die_info **result_comp_unit_die,
685af9cd 6874 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6875{
976ca316 6876 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6877 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6878 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6879 bfd *abfd;
d521ce57 6880 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6881 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6882 int i,num_extra_attrs;
6883 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6884 struct die_info *comp_unit_die;
6885
b0aeadb3
DE
6886 /* At most one of these may be provided. */
6887 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6888
b0c7bfa9
DE
6889 /* These attributes aren't processed until later:
6890 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6891 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6892 referenced later. However, these attributes are found in the stub
6893 which we won't have later. In order to not impose this complication
6894 on the rest of the code, we read them here and copy them to the
6895 DWO CU/TU die. */
b0c7bfa9
DE
6896
6897 stmt_list = NULL;
6898 low_pc = NULL;
6899 high_pc = NULL;
6900 ranges = NULL;
6901 comp_dir = NULL;
6902
6903 if (stub_comp_unit_die != NULL)
6904 {
6905 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6906 DWO file. */
4ab09049 6907 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6908 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6909 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6910 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6911 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6912 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6913
a39fdb41 6914 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6915
2b0c7f41
SM
6916 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6917 We need the value before we can process DW_AT_ranges values from the
6918 DWO. */
6919 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6920
6921 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6922 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6923 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6924 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6925 section. */
6926 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
b0c7bfa9 6927 }
a2ce51a0
DE
6928 else if (stub_comp_dir != NULL)
6929 {
6930 /* Reconstruct the comp_dir attribute to simplify the code below. */
fe56917a 6931 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6932 comp_dir->name = DW_AT_comp_dir;
6933 comp_dir->form = DW_FORM_string;
c6481205 6934 comp_dir->set_string_noncanonical (stub_comp_dir);
a2ce51a0 6935 }
b0c7bfa9
DE
6936
6937 /* Set up for reading the DWO CU/TU. */
6938 cu->dwo_unit = dwo_unit;
685af9cd 6939 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6940 section->read (objfile);
6941 abfd = section->get_bfd_owner ();
9c541725
PA
6942 begin_info_ptr = info_ptr = (section->buffer
6943 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6944 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6945
4ab09049 6946 if (per_cu->is_debug_types)
b0c7bfa9 6947 {
4ab09049 6948 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6949
976ca316
SM
6950 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6951 section, dwo_abbrev_section,
43988095 6952 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6953 /* This is not an assert because it can be caused by bad debug info. */
43988095 6954 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6955 {
6956 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6957 " TU at offset %s [in module %s]"),
a2ce51a0 6958 hex_string (sig_type->signature),
43988095 6959 hex_string (cu->header.signature),
9d8780f0 6960 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6961 bfd_get_filename (abfd));
6962 }
9c541725 6963 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6964 /* For DWOs coming from DWP files, we don't know the CU length
6965 nor the type's offset in the TU until now. */
4057dfde 6966 dwo_unit->length = cu->header.get_length ();
9c541725 6967 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6968
6969 /* Establish the type offset that can be used to lookup the type.
6970 For DWO files, we don't know it until now. */
9c541725
PA
6971 sig_type->type_offset_in_section
6972 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6973 }
6974 else
6975 {
976ca316
SM
6976 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6977 section, dwo_abbrev_section,
43988095 6978 info_ptr, rcuh_kind::COMPILE);
9c541725 6979 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6980 /* For DWOs coming from DWP files, we don't know the CU length
6981 until now. */
4057dfde 6982 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6983 }
6984
606decb2 6985 dwo_abbrev_section->read (objfile);
685af9cd 6986 *result_dwo_abbrev_table
606decb2 6987 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
685af9cd
TT
6988 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6989 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6990
6991 /* Read in the die, but leave space to copy over the attributes
6992 from the stub. This has the benefit of simplifying the rest of
6993 the code - all the work to maintain the illusion of a single
6994 DW_TAG_{compile,type}_unit DIE is done here. */
6995 num_extra_attrs = ((stmt_list != NULL)
6996 + (low_pc != NULL)
6997 + (high_pc != NULL)
6998 + (ranges != NULL)
6999 + (comp_dir != NULL));
7000 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 7001 num_extra_attrs);
b0c7bfa9
DE
7002
7003 /* Copy over the attributes from the stub to the DIE we just read in. */
7004 comp_unit_die = *result_comp_unit_die;
7005 i = comp_unit_die->num_attrs;
7006 if (stmt_list != NULL)
7007 comp_unit_die->attrs[i++] = *stmt_list;
7008 if (low_pc != NULL)
7009 comp_unit_die->attrs[i++] = *low_pc;
7010 if (high_pc != NULL)
7011 comp_unit_die->attrs[i++] = *high_pc;
7012 if (ranges != NULL)
7013 comp_unit_die->attrs[i++] = *ranges;
7014 if (comp_dir != NULL)
7015 comp_unit_die->attrs[i++] = *comp_dir;
7016 comp_unit_die->num_attrs += num_extra_attrs;
7017
b4f54984 7018 if (dwarf_die_debug)
bf6af496
DE
7019 {
7020 fprintf_unfiltered (gdb_stdlog,
7021 "Read die from %s@0x%x of %s:\n",
96b79293 7022 section->get_name (),
bf6af496
DE
7023 (unsigned) (begin_info_ptr - section->buffer),
7024 bfd_get_filename (abfd));
b4f54984 7025 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7026 }
7027
b0c7bfa9
DE
7028 /* Skip dummy compilation units. */
7029 if (info_ptr >= begin_info_ptr + dwo_unit->length
7030 || peek_abbrev_code (abfd, info_ptr) == 0)
7031 return 0;
7032
7033 *result_info_ptr = info_ptr;
7034 return 1;
7035}
7036
a084a2a6
AT
7037/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7038 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7039 signature is part of the header. */
7040static gdb::optional<ULONGEST>
7041lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7042{
7043 if (cu->header.version >= 5)
7044 return cu->header.signature;
7045 struct attribute *attr;
7046 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
529908cb 7047 if (attr == nullptr || !attr->form_is_unsigned ())
a084a2a6 7048 return gdb::optional<ULONGEST> ();
529908cb 7049 return attr->as_unsigned ();
a084a2a6
AT
7050}
7051
c0ab21c2 7052/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 7053 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7054 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7055
7056static struct dwo_unit *
4ab09049 7057lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 7058{
4ab09049 7059 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 7060 struct dwo_unit *dwo_unit;
c0ab21c2 7061 const char *comp_dir;
b0c7bfa9 7062
a2ce51a0
DE
7063 gdb_assert (cu != NULL);
7064
b0c7bfa9 7065 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7066 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7067 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 7068
4ab09049
SM
7069 if (per_cu->is_debug_types)
7070 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
7071 else
7072 {
a084a2a6 7073 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 7074
a084a2a6 7075 if (!signature.has_value ())
b0c7bfa9
DE
7076 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7077 " [in module %s]"),
4ab09049
SM
7078 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7079
7080 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
7081 }
7082
b0c7bfa9
DE
7083 return dwo_unit;
7084}
7085
c0ab21c2 7086/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 7087 See it for a description of the parameters.
fcd3b13d 7088 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 7089
c0ab21c2 7090void
9e021579
SM
7091cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7092 dwarf2_per_objfile *per_objfile,
2e671100 7093 dwarf2_cu *existing_cu)
a2ce51a0 7094{
a2ce51a0 7095 struct signatured_type *sig_type;
a2ce51a0
DE
7096
7097 /* Verify we can do the following downcast, and that we have the
7098 data we need. */
7099 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7100 sig_type = (struct signatured_type *) this_cu;
7101 gdb_assert (sig_type->dwo_unit != NULL);
7102
2e671100
SM
7103 dwarf2_cu *cu;
7104
7105 if (existing_cu != nullptr)
6aa5f3a6 7106 {
2e671100
SM
7107 cu = existing_cu;
7108 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 7109 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 7110 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
7111 }
7112 else
7113 {
7188ed02 7114 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 7115 in per_objfile yet. */
7188ed02 7116 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 7117 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 7118 cu = m_new_cu.get ();
6aa5f3a6
DE
7119 }
7120
7121 /* A future optimization, if needed, would be to use an existing
7122 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7123 could share abbrev tables. */
a2ce51a0 7124
2e671100 7125 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
7126 NULL /* stub_comp_unit_die */,
7127 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 7128 this, &info_ptr,
3e225074 7129 &comp_unit_die,
c0ab21c2 7130 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7131 {
7132 /* Dummy die. */
c0ab21c2 7133 dummy_p = true;
a2ce51a0 7134 }
a2ce51a0
DE
7135}
7136
fd820528 7137/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7138 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7139
f4dc4d17
DE
7140 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7141 Otherwise the table specified in the comp unit header is read in and used.
7142 This is an optimization for when we already have the abbrev table.
7143
2e671100
SM
7144 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7145 allocated. */
aaa75496 7146
ab432490 7147cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7148 dwarf2_per_objfile *per_objfile,
c0ab21c2 7149 struct abbrev_table *abbrev_table,
2e671100 7150 dwarf2_cu *existing_cu,
c0ab21c2
TT
7151 bool skip_partial)
7152 : die_reader_specs {},
6751ebae 7153 m_this_cu (this_cu)
c906108c 7154{
976ca316 7155 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7156 struct dwarf2_section_info *section = this_cu->section;
96b79293 7157 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 7158 const gdb_byte *begin_info_ptr;
dee91e82 7159 struct signatured_type *sig_type = NULL;
4bdcc0c1 7160 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7161 /* Non-zero if CU currently points to a DWO file and we need to
7162 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7163 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7164 int rereading_dwo_cu = 0;
c906108c 7165
b4f54984 7166 if (dwarf_die_debug)
9d8780f0 7167 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7168 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7169 sect_offset_str (this_cu->sect_off));
09406207 7170
a2ce51a0
DE
7171 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7172 file (instead of going through the stub), short-circuit all of this. */
7173 if (this_cu->reading_dwo_directly)
7174 {
7175 /* Narrow down the scope of possibilities to have to understand. */
7176 gdb_assert (this_cu->is_debug_types);
7177 gdb_assert (abbrev_table == NULL);
976ca316 7178 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
7179 return;
7180 }
7181
dee91e82 7182 /* This is cheap if the section is already read in. */
96b79293 7183 section->read (objfile);
dee91e82 7184
9c541725 7185 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7186
7187 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7188
2e671100
SM
7189 dwarf2_cu *cu;
7190
7191 if (existing_cu != nullptr)
dee91e82 7192 {
2e671100 7193 cu = existing_cu;
42e7ad6c
DE
7194 /* If this CU is from a DWO file we need to start over, we need to
7195 refetch the attributes from the skeleton CU.
7196 This could be optimized by retrieving those attributes from when we
7197 were here the first time: the previous comp_unit_die was stored in
7198 comp_unit_obstack. But there's no data yet that we need this
7199 optimization. */
7200 if (cu->dwo_unit != NULL)
7201 rereading_dwo_cu = 1;
dee91e82
DE
7202 }
7203 else
7204 {
7188ed02 7205 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 7206 in per_objfile yet. */
976ca316
SM
7207 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7208 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 7209 cu = m_new_cu.get ();
42e7ad6c 7210 }
dee91e82 7211
b0c7bfa9 7212 /* Get the header. */
9c541725 7213 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7214 {
7215 /* We already have the header, there's no need to read it in again. */
9c541725 7216 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7217 }
7218 else
7219 {
3019eac3 7220 if (this_cu->is_debug_types)
dee91e82 7221 {
976ca316
SM
7222 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7223 section, abbrev_section,
7224 info_ptr, rcuh_kind::TYPE);
dee91e82 7225
42e7ad6c
DE
7226 /* Since per_cu is the first member of struct signatured_type,
7227 we can go from a pointer to one to a pointer to the other. */
7228 sig_type = (struct signatured_type *) this_cu;
43988095 7229 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7230 gdb_assert (sig_type->type_offset_in_tu
7231 == cu->header.type_cu_offset_in_tu);
7232 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7233
42e7ad6c
DE
7234 /* LENGTH has not been set yet for type units if we're
7235 using .gdb_index. */
4057dfde 7236 this_cu->length = cu->header.get_length ();
3019eac3
DE
7237
7238 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7239 sig_type->type_offset_in_section =
7240 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7241
7242 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7243 }
7244 else
7245 {
976ca316
SM
7246 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7247 section, abbrev_section,
43988095
JK
7248 info_ptr,
7249 rcuh_kind::COMPILE);
dee91e82 7250
9c541725 7251 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7252 if (this_cu->length == 0)
7253 this_cu->length = cu->header.get_length ();
7254 else
7255 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7256 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7257 }
7258 }
10b3939b 7259
6caca83c 7260 /* Skip dummy compilation units. */
dee91e82 7261 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7262 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7263 {
7264 dummy_p = true;
7265 return;
7266 }
6caca83c 7267
433df2d4
DE
7268 /* If we don't have them yet, read the abbrevs for this compilation unit.
7269 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7270 done. */
f4dc4d17 7271 if (abbrev_table != NULL)
685af9cd
TT
7272 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7273 else
f4dc4d17 7274 {
606decb2 7275 abbrev_section->read (objfile);
c0ab21c2 7276 m_abbrev_table_holder
606decb2 7277 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
c0ab21c2 7278 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7279 }
af703f96 7280
dee91e82 7281 /* Read the top level CU/TU die. */
c0ab21c2 7282 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7283 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7284
58f0c718 7285 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7286 {
7287 dummy_p = true;
7288 return;
7289 }
58f0c718 7290
b0c7bfa9 7291 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7292 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7293 table from the DWO file and pass the ownership over to us. It will be
7294 referenced from READER, so we must make sure to free it after we're done
7295 with READER.
7296
b0c7bfa9
DE
7297 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7298 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7299 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7300 if (dwo_name != nullptr)
3019eac3 7301 {
3019eac3 7302 struct dwo_unit *dwo_unit;
b0c7bfa9 7303 struct die_info *dwo_comp_unit_die;
3019eac3 7304
3e225074 7305 if (comp_unit_die->has_children)
6a506a2d 7306 {
b98664d3 7307 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7308 " has children (offset %s) [in module %s]"),
7309 sect_offset_str (this_cu->sect_off),
7310 bfd_get_filename (abfd));
6a506a2d 7311 }
4ab09049 7312 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7313 if (dwo_unit != NULL)
3019eac3 7314 {
4ab09049 7315 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7316 comp_unit_die, NULL,
c0ab21c2 7317 this, &info_ptr,
3e225074 7318 &dwo_comp_unit_die,
c0ab21c2 7319 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7320 {
7321 /* Dummy die. */
c0ab21c2 7322 dummy_p = true;
6a506a2d
DE
7323 return;
7324 }
7325 comp_unit_die = dwo_comp_unit_die;
7326 }
7327 else
7328 {
7329 /* Yikes, we couldn't find the rest of the DIE, we only have
7330 the stub. A complaint has already been logged. There's
7331 not much more we can do except pass on the stub DIE to
7332 die_reader_func. We don't want to throw an error on bad
7333 debug info. */
3019eac3
DE
7334 }
7335 }
c0ab21c2 7336}
3019eac3 7337
6751ebae
TT
7338void
7339cutu_reader::keep ()
c0ab21c2 7340{
b0c7bfa9 7341 /* Done, clean up. */
6751ebae
TT
7342 gdb_assert (!dummy_p);
7343 if (m_new_cu != NULL)
348e048f 7344 {
7188ed02 7345 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
dda83cd7 7346 now. */
7188ed02
SM
7347 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7348 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 7349 }
dee91e82
DE
7350}
7351
18a8505e
AT
7352/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7353 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7354 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7355
7356 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7357 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7358
7359 We fill in THIS_CU->length.
7360
dee91e82 7361 THIS_CU->cu is always freed when done.
3019eac3 7362 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7363 to care whether it refers to the "main" CU or the DWO CU.
7364
7365 When parent_cu is passed, it is used to provide a default value for
7366 str_offsets_base and addr_base from the parent. */
dee91e82 7367
ab432490 7368cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7369 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
7370 struct dwarf2_cu *parent_cu,
7371 struct dwo_file *dwo_file)
7372 : die_reader_specs {},
7373 m_this_cu (this_cu)
dee91e82 7374{
976ca316 7375 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7376 struct dwarf2_section_info *section = this_cu->section;
96b79293 7377 bfd *abfd = section->get_bfd_owner ();
33e80786 7378 struct dwarf2_section_info *abbrev_section;
d521ce57 7379 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7380
b4f54984 7381 if (dwarf_die_debug)
9d8780f0 7382 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7383 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7384 sect_offset_str (this_cu->sect_off));
09406207 7385
976ca316 7386 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7387
33e80786
DE
7388 abbrev_section = (dwo_file != NULL
7389 ? &dwo_file->sections.abbrev
7390 : get_abbrev_section_for_cu (this_cu));
7391
dee91e82 7392 /* This is cheap if the section is already read in. */
96b79293 7393 section->read (objfile);
dee91e82 7394
976ca316 7395 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 7396
9c541725 7397 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
7398 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7399 section, abbrev_section, info_ptr,
43988095
JK
7400 (this_cu->is_debug_types
7401 ? rcuh_kind::TYPE
7402 : rcuh_kind::COMPILE));
dee91e82 7403
18a8505e
AT
7404 if (parent_cu != nullptr)
7405 {
c0ab21c2
TT
7406 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7407 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7408 }
4057dfde 7409 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7410
7411 /* Skip dummy compilation units. */
7412 if (info_ptr >= begin_info_ptr + this_cu->length
7413 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7414 {
7415 dummy_p = true;
7416 return;
7417 }
72bf9492 7418
606decb2 7419 abbrev_section->read (objfile);
c0ab21c2 7420 m_abbrev_table_holder
606decb2 7421 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
dee91e82 7422
c0ab21c2
TT
7423 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7424 m_abbrev_table_holder.get ());
3e225074 7425 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7426}
7427
0018ea6f
DE
7428\f
7429/* Type Unit Groups.
dee91e82 7430
0018ea6f
DE
7431 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7432 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7433 so that all types coming from the same compilation (.o file) are grouped
7434 together. A future step could be to put the types in the same symtab as
7435 the CU the types ultimately came from. */
ff013f42 7436
f4dc4d17
DE
7437static hashval_t
7438hash_type_unit_group (const void *item)
7439{
9a3c8263
SM
7440 const struct type_unit_group *tu_group
7441 = (const struct type_unit_group *) item;
f4dc4d17 7442
094b34ac 7443 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7444}
348e048f
DE
7445
7446static int
f4dc4d17 7447eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7448{
9a3c8263
SM
7449 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7450 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7451
094b34ac 7452 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7453}
348e048f 7454
f4dc4d17
DE
7455/* Allocate a hash table for type unit groups. */
7456
eaa5fa8b 7457static htab_up
298e9637 7458allocate_type_unit_groups_table ()
f4dc4d17 7459{
eaa5fa8b
TT
7460 return htab_up (htab_create_alloc (3,
7461 hash_type_unit_group,
7462 eq_type_unit_group,
7463 NULL, xcalloc, xfree));
f4dc4d17 7464}
dee91e82 7465
f4dc4d17
DE
7466/* Type units that don't have DW_AT_stmt_list are grouped into their own
7467 partial symtabs. We combine several TUs per psymtab to not let the size
7468 of any one psymtab grow too big. */
7469#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7470#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7471
094b34ac 7472/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7473 Create the type_unit_group object used to hold one or more TUs. */
7474
7475static struct type_unit_group *
094b34ac 7476create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7477{
976ca316
SM
7478 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7479 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
094b34ac 7480 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7481 struct type_unit_group *tu_group;
f4dc4d17 7482
bab287cd 7483 tu_group = OBSTACK_ZALLOC (&per_bfd->obstack, type_unit_group);
094b34ac 7484 per_cu = &tu_group->per_cu;
1859c670 7485 per_cu->per_bfd = per_bfd;
f4dc4d17 7486
1859c670 7487 if (per_bfd->using_index)
094b34ac 7488 {
1859c670 7489 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7490 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7491 }
7492 else
7493 {
9c541725 7494 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7495 dwarf2_psymtab *pst;
528e1572 7496 std::string name;
094b34ac
DE
7497
7498 /* Give the symtab a useful name for debug purposes. */
7499 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7500 name = string_printf ("<type_units_%d>",
7501 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7502 else
528e1572 7503 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7504
976ca316 7505 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
6d94535f 7506 pst->anonymous = true;
094b34ac 7507 }
f4dc4d17 7508
094b34ac 7509 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7510 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7511
7512 return tu_group;
7513}
7514
094b34ac
DE
7515/* Look up the type_unit_group for type unit CU, and create it if necessary.
7516 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7517
7518static struct type_unit_group *
ff39bb5e 7519get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7520{
976ca316
SM
7521 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7522 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7523 struct type_unit_group *tu_group;
7524 void **slot;
7525 unsigned int line_offset;
7526 struct type_unit_group type_unit_group_for_lookup;
7527
976ca316
SM
7528 if (per_objfile->per_bfd->type_unit_groups == NULL)
7529 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7530
7531 /* Do we need to create a new group, or can we use an existing one? */
7532
529908cb 7533 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
f4dc4d17 7534 {
529908cb 7535 line_offset = stmt_list->as_unsigned ();
f4dc4d17
DE
7536 ++tu_stats->nr_symtab_sharers;
7537 }
7538 else
7539 {
7540 /* Ugh, no stmt_list. Rare, but we have to handle it.
7541 We can do various things here like create one group per TU or
7542 spread them over multiple groups to split up the expansion work.
7543 To avoid worst case scenarios (too many groups or too large groups)
7544 we, umm, group them in bunches. */
7545 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7546 | (tu_stats->nr_stmt_less_type_units
7547 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7548 ++tu_stats->nr_stmt_less_type_units;
7549 }
7550
094b34ac 7551 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7552 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7553 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7554 &type_unit_group_for_lookup, INSERT);
7555 if (*slot != NULL)
7556 {
9a3c8263 7557 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7558 gdb_assert (tu_group != NULL);
7559 }
7560 else
7561 {
9c541725 7562 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7563 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7564 *slot = tu_group;
7565 ++tu_stats->nr_symtabs;
7566 }
7567
7568 return tu_group;
7569}
0018ea6f
DE
7570\f
7571/* Partial symbol tables. */
7572
7573/* Create a psymtab named NAME and assign it to PER_CU.
7574
7575 The caller must fill in the following details:
7576 dirname, textlow, texthigh. */
7577
891813be 7578static dwarf2_psymtab *
7aa104c4
SM
7579create_partial_symtab (dwarf2_per_cu_data *per_cu,
7580 dwarf2_per_objfile *per_objfile,
7581 const char *name)
0018ea6f 7582{
7aa104c4 7583 struct objfile *objfile = per_objfile->objfile;
891813be 7584 dwarf2_psymtab *pst;
0018ea6f 7585
7b249e47
TT
7586 pst = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
7587 objfile, per_cu);
0018ea6f 7588
6d94535f 7589 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7590
7591 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7592 per_cu->v.psymtab = pst;
7593
7594 return pst;
7595}
7596
c0ab21c2 7597/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7598
7599static void
7600process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7601 const gdb_byte *info_ptr,
0018ea6f 7602 struct die_info *comp_unit_die,
c0ab21c2 7603 enum language pretend_language)
0018ea6f
DE
7604{
7605 struct dwarf2_cu *cu = reader->cu;
7aa104c4 7606 dwarf2_per_objfile *per_objfile = cu->per_objfile;
84685904 7607 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7aa104c4 7608 struct objfile *objfile = per_objfile->objfile;
08feed99 7609 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7610 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7611 CORE_ADDR baseaddr;
7612 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7613 dwarf2_psymtab *pst;
3a2b436a 7614 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7615 const char *filename;
0018ea6f 7616
0018ea6f
DE
7617 gdb_assert (! per_cu->is_debug_types);
7618
c0ab21c2 7619 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7620
0018ea6f 7621 /* Allocate a new partial symbol table structure. */
2e927613
TV
7622 gdb::unique_xmalloc_ptr<char> debug_filename;
7623 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7624 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7625 if (filename == NULL)
0018ea6f 7626 filename = "";
2e927613
TV
7627 else if (strcmp (filename, artificial) == 0)
7628 {
7629 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7630 sect_offset_str (per_cu->sect_off),
7631 (char *) NULL));
2e927613
TV
7632 filename = debug_filename.get ();
7633 }
0018ea6f 7634
7aa104c4 7635 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7636
7637 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7638 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7639
b3b3bada 7640 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7641
7642 dwarf2_find_base_address (comp_unit_die, cu);
7643
7644 /* Possibly set the default values of LOWPC and HIGHPC from
7645 `DW_AT_ranges'. */
3a2b436a
JK
7646 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7647 &best_highpc, cu, pst);
7648 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7649 {
7650 CORE_ADDR low
7651 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7652 - baseaddr);
7653 CORE_ADDR high
7654 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7655 - baseaddr - 1);
7656 /* Store the contiguous range if it is not empty; it can be
7657 empty for CUs with no code. */
84685904 7658 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 7659 low, high, pst);
79748972 7660 }
0018ea6f
DE
7661
7662 /* Check if comp unit has_children.
7663 If so, read the rest of the partial symbols from this comp unit.
7664 If not, there's no more debug_info for this comp unit. */
3e225074 7665 if (comp_unit_die->has_children)
0018ea6f
DE
7666 {
7667 struct partial_die_info *first_die;
7668 CORE_ADDR lowpc, highpc;
7669
7670 lowpc = ((CORE_ADDR) -1);
7671 highpc = ((CORE_ADDR) 0);
7672
7673 first_die = load_partial_dies (reader, info_ptr, 1);
7674
7675 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7676 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7677
7678 /* If we didn't find a lowpc, set it to highpc to avoid
7679 complaints from `maint check'. */
7680 if (lowpc == ((CORE_ADDR) -1))
7681 lowpc = highpc;
7682
7683 /* If the compilation unit didn't have an explicit address range,
7684 then use the information extracted from its child dies. */
e385593e 7685 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7686 {
7687 best_lowpc = lowpc;
7688 best_highpc = highpc;
7689 }
7690 }
4ae976d1 7691 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7692 best_lowpc + baseaddr)
7693 - baseaddr);
4ae976d1 7694 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7695 best_highpc + baseaddr)
7696 - baseaddr);
0018ea6f 7697
ae7754b2 7698 pst->end ();
0018ea6f 7699
ae640021 7700 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7701 {
7702 int i;
ae640021 7703 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7704
7705 /* Fill in 'dependencies' here; we fill in 'users' in a
7706 post-pass. */
7707 pst->number_of_dependencies = len;
a9342b62 7708 pst->dependencies
84685904 7709 = per_bfd->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7710 for (i = 0; i < len; ++i)
7711 {
7712 pst->dependencies[i]
7713 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7714 }
0018ea6f 7715
ae640021 7716 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7717 }
7718
7719 /* Get the list of files included in the current compilation unit,
7720 and build a psymtab for each of them. */
7721 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7722
6f738b01
SM
7723 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7724 ", %d global, %d static syms",
7725 per_cu->is_debug_types ? "type" : "comp",
7726 sect_offset_str (per_cu->sect_off),
7727 paddress (gdbarch, pst->text_low (objfile)),
7728 paddress (gdbarch, pst->text_high (objfile)),
7729 (int) pst->global_psymbols.size (),
7730 (int) pst->static_psymbols.size ());
0018ea6f
DE
7731}
7732
7733/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7734 Process compilation unit THIS_CU for a psymtab. */
7735
7736static void
ab432490
SM
7737process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7738 dwarf2_per_objfile *per_objfile,
135f5437 7739 bool want_partial_unit,
b93601f3 7740 enum language pretend_language)
0018ea6f
DE
7741{
7742 /* If this compilation unit was already read in, free the
7743 cached copy in order to read it in again. This is
7744 necessary because we skipped some symbols when we first
7745 read in the compilation unit (see load_partial_dies).
7746 This problem could be avoided, but the benefit is unclear. */
7188ed02 7747 per_objfile->remove_cu (this_cu);
0018ea6f 7748
2e671100 7749 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7750
58990295
TV
7751 switch (reader.comp_unit_die->tag)
7752 {
7753 case DW_TAG_compile_unit:
7754 this_cu->unit_type = DW_UT_compile;
7755 break;
7756 case DW_TAG_partial_unit:
7757 this_cu->unit_type = DW_UT_partial;
7758 break;
e77b0004
TV
7759 case DW_TAG_type_unit:
7760 this_cu->unit_type = DW_UT_type;
7761 break;
58990295 7762 default:
702cf3f5
AB
7763 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7764 dwarf_tag_name (reader.comp_unit_die->tag),
7765 sect_offset_str (reader.cu->per_cu->sect_off),
7766 objfile_name (per_objfile->objfile));
58990295
TV
7767 }
7768
c0ab21c2 7769 if (reader.dummy_p)
f1902523 7770 {
c0ab21c2 7771 /* Nothing. */
f1902523 7772 }
c0ab21c2 7773 else if (this_cu->is_debug_types)
3e225074
TT
7774 build_type_psymtabs_reader (&reader, reader.info_ptr,
7775 reader.comp_unit_die);
135f5437
TT
7776 else if (want_partial_unit
7777 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7778 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7779 reader.comp_unit_die,
c0ab21c2 7780 pretend_language);
0018ea6f 7781
7188ed02 7782 this_cu->lang = reader.cu->language;
58990295 7783
0018ea6f 7784 /* Age out any secondary CUs. */
7188ed02 7785 per_objfile->age_comp_units ();
0018ea6f 7786}
f4dc4d17
DE
7787
7788/* Reader function for build_type_psymtabs. */
7789
7790static void
7791build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7792 const gdb_byte *info_ptr,
3e225074 7793 struct die_info *type_unit_die)
f4dc4d17 7794{
976ca316 7795 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7796 struct dwarf2_cu *cu = reader->cu;
7797 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7798 struct signatured_type *sig_type;
f4dc4d17
DE
7799 struct type_unit_group *tu_group;
7800 struct attribute *attr;
7801 struct partial_die_info *first_die;
7802 CORE_ADDR lowpc, highpc;
891813be 7803 dwarf2_psymtab *pst;
f4dc4d17 7804
0186c6a7
DE
7805 gdb_assert (per_cu->is_debug_types);
7806 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7807
3e225074 7808 if (! type_unit_die->has_children)
f4dc4d17
DE
7809 return;
7810
052c8bb8 7811 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7812 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7813
df07e2c7 7814 if (tu_group->tus == nullptr)
a8b3b8e9 7815 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7816 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7817
7818 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7819 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7820 pst->anonymous = true;
f4dc4d17
DE
7821
7822 first_die = load_partial_dies (reader, info_ptr, 1);
7823
7824 lowpc = (CORE_ADDR) -1;
7825 highpc = (CORE_ADDR) 0;
7826 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7827
ae7754b2 7828 pst->end ();
f4dc4d17
DE
7829}
7830
73051182
DE
7831/* Struct used to sort TUs by their abbreviation table offset. */
7832
7833struct tu_abbrev_offset
7834{
b2bdb8cf
SM
7835 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7836 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7837 {}
7838
7839 signatured_type *sig_type;
73051182
DE
7840 sect_offset abbrev_offset;
7841};
7842
484cf504 7843/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7844
484cf504
TT
7845static bool
7846sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7847 const struct tu_abbrev_offset &b)
73051182 7848{
484cf504 7849 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7850}
7851
7852/* Efficiently read all the type units.
7853 This does the bulk of the work for build_type_psymtabs.
7854
7855 The efficiency is because we sort TUs by the abbrev table they use and
7856 only read each abbrev table once. In one program there are 200K TUs
7857 sharing 8K abbrev tables.
7858
7859 The main purpose of this function is to support building the
5989a64e 7860 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7861 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7862 can collapse the search space by grouping them by stmt_list.
7863 The savings can be significant, in the same program from above the 200K TUs
7864 share 8K stmt_list tables.
7865
7866 FUNC is expected to call get_type_unit_group, which will create the
7867 struct type_unit_group if necessary and add it to
5989a64e 7868 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7869
7870static void
976ca316 7871build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
73051182 7872{
976ca316 7873 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7874 abbrev_table_up abbrev_table;
73051182 7875 sect_offset abbrev_offset;
73051182
DE
7876
7877 /* It's up to the caller to not call us multiple times. */
976ca316 7878 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7879
976ca316 7880 if (per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7881 return;
7882
7883 /* TUs typically share abbrev tables, and there can be way more TUs than
7884 abbrev tables. Sort by abbrev table to reduce the number of times we
7885 read each abbrev table in.
7886 Alternatives are to punt or to maintain a cache of abbrev tables.
7887 This is simpler and efficient enough for now.
7888
7889 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7890 symtab to use). Typically TUs with the same abbrev offset have the same
7891 stmt_list value too so in practice this should work well.
7892
7893 The basic algorithm here is:
7894
7895 sort TUs by abbrev table
7896 for each TU with same abbrev table:
7897 read abbrev table if first user
7898 read TU top level DIE
7899 [IWBN if DWO skeletons had DW_AT_stmt_list]
7900 call FUNC */
7901
6f738b01 7902 dwarf_read_debug_printf ("Building type unit groups ...");
73051182
DE
7903
7904 /* Sort in a separate table to maintain the order of all_type_units
7905 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7906 std::vector<tu_abbrev_offset> sorted_by_abbrev;
976ca316 7907 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7908
976ca316 7909 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
b2bdb8cf 7910 sorted_by_abbrev.emplace_back
976ca316 7911 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
b2bdb8cf 7912 sig_type->per_cu.sect_off));
73051182 7913
484cf504
TT
7914 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7915 sort_tu_by_abbrev_offset);
73051182 7916
9c541725 7917 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7918
b2bdb8cf 7919 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7920 {
73051182
DE
7921 /* Switch to the next abbrev table if necessary. */
7922 if (abbrev_table == NULL
b2bdb8cf 7923 || tu.abbrev_offset != abbrev_offset)
73051182 7924 {
b2bdb8cf 7925 abbrev_offset = tu.abbrev_offset;
606decb2 7926 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
73051182 7927 abbrev_table =
606decb2 7928 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7929 ++tu_stats->nr_uniq_abbrev_tables;
7930 }
7931
976ca316 7932 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
2e671100 7933 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7934 if (!reader.dummy_p)
7935 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7936 reader.comp_unit_die);
73051182 7937 }
6aa5f3a6 7938}
73051182 7939
6aa5f3a6
DE
7940/* Print collected type unit statistics. */
7941
7942static void
976ca316 7943print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7944{
976ca316 7945 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6 7946
6f738b01
SM
7947 dwarf_read_debug_printf ("Type unit statistics:");
7948 dwarf_read_debug_printf (" %zu TUs",
7949 per_objfile->per_bfd->all_type_units.size ());
7950 dwarf_read_debug_printf (" %d uniq abbrev tables",
7951 tu_stats->nr_uniq_abbrev_tables);
7952 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7953 tu_stats->nr_symtabs);
7954 dwarf_read_debug_printf (" %d symtab sharers",
7955 tu_stats->nr_symtab_sharers);
7956 dwarf_read_debug_printf (" %d type units without a stmt_list",
7957 tu_stats->nr_stmt_less_type_units);
7958 dwarf_read_debug_printf (" %d all_type_units reallocs",
7959 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7960}
7961
f4dc4d17
DE
7962/* Traversal function for build_type_psymtabs. */
7963
7964static int
7965build_type_psymtab_dependencies (void **slot, void *info)
7966{
976ca316 7967 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
84685904 7968 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
f4dc4d17 7969 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7970 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7971 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7972 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7973 int i;
7974
7975 gdb_assert (len > 0);
197400e8 7976 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7977
7978 pst->number_of_dependencies = len;
84685904 7979 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
df07e2c7 7980 for (i = 0; i < len; ++i)
f4dc4d17 7981 {
df07e2c7 7982 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7983 gdb_assert (iter->per_cu.is_debug_types);
7984 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7985 iter->type_unit_group = tu_group;
f4dc4d17
DE
7986 }
7987
df07e2c7
AB
7988 delete tu_group->tus;
7989 tu_group->tus = nullptr;
348e048f
DE
7990
7991 return 1;
7992}
7993
7994/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7995 Build partial symbol tables for the .debug_types comp-units. */
7996
7997static void
976ca316 7998build_type_psymtabs (dwarf2_per_objfile *per_objfile)
348e048f 7999{
976ca316 8000 if (! create_all_type_units (per_objfile))
348e048f
DE
8001 return;
8002
976ca316 8003 build_type_psymtabs_1 (per_objfile);
6aa5f3a6 8004}
f4dc4d17 8005
6aa5f3a6
DE
8006/* Traversal function for process_skeletonless_type_unit.
8007 Read a TU in a DWO file and build partial symbols for it. */
8008
8009static int
8010process_skeletonless_type_unit (void **slot, void *info)
8011{
8012 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 8013 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
8014 struct signatured_type find_entry, *entry;
8015
8016 /* If this TU doesn't exist in the global table, add it and read it in. */
8017
976ca316
SM
8018 if (per_objfile->per_bfd->signatured_types == NULL)
8019 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
8020
8021 find_entry.signature = dwo_unit->signature;
976ca316 8022 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 8023 &find_entry, INSERT);
6aa5f3a6
DE
8024 /* If we've already seen this type there's nothing to do. What's happening
8025 is we're doing our own version of comdat-folding here. */
8026 if (*slot != NULL)
8027 return 1;
8028
8029 /* This does the job that create_all_type_units would have done for
8030 this TU. */
976ca316
SM
8031 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
8032 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8033 *slot = entry;
8034
8035 /* This does the job that build_type_psymtabs_1 would have done. */
976ca316 8036 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
8037 if (!reader.dummy_p)
8038 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 8039 reader.comp_unit_die);
6aa5f3a6
DE
8040
8041 return 1;
8042}
8043
8044/* Traversal function for process_skeletonless_type_units. */
8045
8046static int
8047process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8048{
8049 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8050
8051 if (dwo_file->tus != NULL)
b0b6a987
TT
8052 htab_traverse_noresize (dwo_file->tus.get (),
8053 process_skeletonless_type_unit, info);
6aa5f3a6
DE
8054
8055 return 1;
8056}
8057
8058/* Scan all TUs of DWO files, verifying we've processed them.
8059 This is needed in case a TU was emitted without its skeleton.
8060 Note: This can't be done until we know what all the DWO files are. */
8061
8062static void
976ca316 8063process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
8064{
8065 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
8066 if (get_dwp_file (per_objfile) == NULL
8067 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 8068 {
976ca316 8069 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 8070 process_dwo_file_for_skeletonless_type_units,
976ca316 8071 per_objfile);
6aa5f3a6 8072 }
348e048f
DE
8073}
8074
ed2dc618 8075/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8076
8077static void
976ca316 8078set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 8079{
976ca316 8080 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 8081 {
891813be 8082 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 8083
36586728
TT
8084 if (pst == NULL)
8085 continue;
8086
b76e467d 8087 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8088 {
8089 /* Set the 'user' field only if it is not already set. */
8090 if (pst->dependencies[j]->user == NULL)
8091 pst->dependencies[j]->user = pst;
8092 }
8093 }
8094}
8095
93311388
DE
8096/* Build the partial symbol table by doing a quick pass through the
8097 .debug_info and .debug_abbrev sections. */
72bf9492 8098
93311388 8099static void
976ca316 8100dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 8101{
976ca316 8102 struct objfile *objfile = per_objfile->objfile;
84685904 8103 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
93311388 8104
6f738b01
SM
8105 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8106 objfile_name (objfile));
45cfd468 8107
76935768 8108 scoped_restore restore_reading_psyms
bab287cd 8109 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
98bfdba5 8110
84685904 8111 per_bfd->info.read (objfile);
91c24f0a 8112
93311388
DE
8113 /* Any cached compilation units will be linked by the per-objfile
8114 read_in_chain. Make sure to free them when we're done. */
976ca316 8115 free_cached_comp_units freer (per_objfile);
72bf9492 8116
976ca316 8117 build_type_psymtabs (per_objfile);
348e048f 8118
976ca316 8119 create_all_comp_units (per_objfile);
c906108c 8120
60606b2c
TT
8121 /* Create a temporary address map on a temporary obstack. We later
8122 copy this to the final obstack. */
8268c778 8123 auto_obstack temp_obstack;
791afaa2
TT
8124
8125 scoped_restore save_psymtabs_addrmap
84685904 8126 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
791afaa2 8127 addrmap_create_mutable (&temp_obstack));
72bf9492 8128
84685904 8129 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
3d5afab3
TV
8130 {
8131 if (per_cu->v.psymtab != NULL)
8132 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8133 continue;
976ca316 8134 process_psymtab_comp_unit (per_cu, per_objfile, false,
ab432490 8135 language_minimal);
3d5afab3 8136 }
ff013f42 8137
6aa5f3a6 8138 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 8139 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
8140
8141 /* Now that all TUs have been processed we can fill in the dependencies. */
84685904 8142 if (per_bfd->type_unit_groups != NULL)
6aa5f3a6 8143 {
84685904 8144 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
976ca316 8145 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
8146 }
8147
6f738b01 8148 if (dwarf_read_debug > 0)
976ca316 8149 print_tu_stats (per_objfile);
6aa5f3a6 8150
976ca316 8151 set_partial_user (per_objfile);
95554aad 8152
84685904
TT
8153 per_bfd->partial_symtabs->psymtabs_addrmap
8154 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
8155 per_bfd->partial_symtabs->obstack ());
791afaa2
TT
8156 /* At this point we want to keep the address map. */
8157 save_psymtabs_addrmap.release ();
ff013f42 8158
6f738b01
SM
8159 dwarf_read_debug_printf ("Done building psymtabs of %s",
8160 objfile_name (objfile));
ae038cb0
DJ
8161}
8162
dee91e82
DE
8163/* Load the partial DIEs for a secondary CU into memory.
8164 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8165
dee91e82 8166static void
ab432490 8167load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
8168 dwarf2_per_objfile *per_objfile,
8169 dwarf2_cu *existing_cu)
dee91e82 8170{
2e671100 8171 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
8172
8173 if (!reader.dummy_p)
8174 {
8175 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8176 language_minimal);
8177
8178 /* Check if comp unit has_children.
8179 If so, read the rest of the partial symbols from this comp unit.
8180 If not, there's no more debug_info for this comp unit. */
3e225074 8181 if (reader.comp_unit_die->has_children)
c0ab21c2 8182 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8183
8184 reader.keep ();
c0ab21c2 8185 }
ae038cb0
DJ
8186}
8187
ae038cb0 8188static void
976ca316 8189read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 8190 struct dwarf2_section_info *section,
f1902523 8191 struct dwarf2_section_info *abbrev_section,
b76e467d 8192 unsigned int is_dwz)
ae038cb0 8193{
d521ce57 8194 const gdb_byte *info_ptr;
976ca316 8195 struct objfile *objfile = per_objfile->objfile;
be391dca 8196
6f738b01
SM
8197 dwarf_read_debug_printf ("Reading %s for %s",
8198 section->get_name (),
8199 section->get_file_name ());
bf6af496 8200
96b79293 8201 section->read (objfile);
ae038cb0 8202
36586728 8203 info_ptr = section->buffer;
6e70227d 8204
36586728 8205 while (info_ptr < section->buffer + section->size)
ae038cb0 8206 {
ae038cb0 8207 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8208
9c541725 8209 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8210
f1902523 8211 comp_unit_head cu_header;
976ca316 8212 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618
SM
8213 abbrev_section, info_ptr,
8214 rcuh_kind::COMPILE);
ae038cb0
DJ
8215
8216 /* Save the compilation unit for later lookup. */
f1902523 8217 if (cu_header.unit_type != DW_UT_type)
976ca316 8218 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8219 else
8220 {
976ca316 8221 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8222 sig_type->signature = cu_header.signature;
8223 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8224 this_cu = &sig_type->per_cu;
8225 }
8226 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8227 this_cu->sect_off = sect_off;
f1902523 8228 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8229 this_cu->is_dwz = is_dwz;
8a0459fd 8230 this_cu->section = section;
ae038cb0 8231
976ca316 8232 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8233
8234 info_ptr = info_ptr + this_cu->length;
8235 }
36586728
TT
8236}
8237
8238/* Create a list of all compilation units in OBJFILE.
8239 This is only done for -readnow and building partial symtabs. */
8240
8241static void
976ca316 8242create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 8243{
976ca316
SM
8244 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8245 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8246 &per_objfile->per_bfd->abbrev, 0);
36586728 8247
976ca316 8248 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 8249 if (dwz != NULL)
976ca316 8250 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
c906108c
SS
8251}
8252
5734ee8b 8253/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8254 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8255 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8256 DW_AT_ranges). See the comments of add_partial_subprogram on how
8257 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8258
72bf9492
DJ
8259static void
8260scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8261 CORE_ADDR *highpc, int set_addrmap,
8262 struct dwarf2_cu *cu)
c906108c 8263{
72bf9492 8264 struct partial_die_info *pdi;
c906108c 8265
91c24f0a
DC
8266 /* Now, march along the PDI's, descending into ones which have
8267 interesting children but skipping the children of the other ones,
8268 until we reach the end of the compilation unit. */
c906108c 8269
72bf9492 8270 pdi = first_die;
91c24f0a 8271
72bf9492
DJ
8272 while (pdi != NULL)
8273 {
52356b79 8274 pdi->fixup (cu);
c906108c 8275
f55ee35c 8276 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8277 children, so we need to look at them. Ditto for anonymous
8278 enums. */
933c6fe4 8279
7d00ffec 8280 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8281 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8282 || pdi->tag == DW_TAG_imported_unit
8283 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8284 {
72bf9492 8285 switch (pdi->tag)
c906108c
SS
8286 {
8287 case DW_TAG_subprogram:
b1dc1806 8288 case DW_TAG_inlined_subroutine:
cdc07690 8289 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
8290 if (cu->language == language_cplus)
8291 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8292 set_addrmap, cu);
c906108c 8293 break;
72929c62 8294 case DW_TAG_constant:
c906108c
SS
8295 case DW_TAG_variable:
8296 case DW_TAG_typedef:
91c24f0a 8297 case DW_TAG_union_type:
317d2668
TV
8298 if (!pdi->is_declaration
8299 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8300 {
72bf9492 8301 add_partial_symbol (pdi, cu);
63d06c5c
DC
8302 }
8303 break;
c906108c 8304 case DW_TAG_class_type:
680b30c7 8305 case DW_TAG_interface_type:
c906108c 8306 case DW_TAG_structure_type:
72bf9492 8307 if (!pdi->is_declaration)
c906108c 8308 {
72bf9492 8309 add_partial_symbol (pdi, cu);
c906108c 8310 }
b7fee5a3
KS
8311 if ((cu->language == language_rust
8312 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8313 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8314 set_addrmap, cu);
c906108c 8315 break;
91c24f0a 8316 case DW_TAG_enumeration_type:
72bf9492
DJ
8317 if (!pdi->is_declaration)
8318 add_partial_enumeration (pdi, cu);
c906108c
SS
8319 break;
8320 case DW_TAG_base_type:
dda83cd7 8321 case DW_TAG_subrange_type:
c906108c 8322 /* File scope base type definitions are added to the partial
dda83cd7 8323 symbol table. */
72bf9492 8324 add_partial_symbol (pdi, cu);
c906108c 8325 break;
d9fa45fe 8326 case DW_TAG_namespace:
cdc07690 8327 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8328 break;
5d7cb8df 8329 case DW_TAG_module:
59c35742
AB
8330 if (!pdi->is_declaration)
8331 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8332 break;
95554aad
TT
8333 case DW_TAG_imported_unit:
8334 {
8335 struct dwarf2_per_cu_data *per_cu;
8336
f4dc4d17
DE
8337 /* For now we don't handle imported units in type units. */
8338 if (cu->per_cu->is_debug_types)
8339 {
8340 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8341 " supported in type units [in module %s]"),
5e22e966 8342 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8343 }
8344
e3b94546 8345 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8346 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8347
8348 /* Go read the partial unit, if needed. */
8349 if (per_cu->v.psymtab == NULL)
ab432490
SM
8350 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8351 cu->language);
95554aad 8352
ae640021 8353 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8354 }
8355 break;
74921315
KS
8356 case DW_TAG_imported_declaration:
8357 add_partial_symbol (pdi, cu);
8358 break;
c906108c
SS
8359 default:
8360 break;
8361 }
8362 }
8363
72bf9492
DJ
8364 /* If the die has a sibling, skip to the sibling. */
8365
8366 pdi = pdi->die_sibling;
8367 }
8368}
8369
8370/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8371
72bf9492 8372 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8373 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8374 Enumerators are an exception; they use the scope of their parent
8375 enumeration type, i.e. the name of the enumeration type is not
8376 prepended to the enumerator.
91c24f0a 8377
72bf9492
DJ
8378 There are two complexities. One is DW_AT_specification; in this
8379 case "parent" means the parent of the target of the specification,
8380 instead of the direct parent of the DIE. The other is compilers
8381 which do not emit DW_TAG_namespace; in this case we try to guess
8382 the fully qualified name of structure types from their members'
8383 linkage names. This must be done using the DIE's children rather
8384 than the children of any DW_AT_specification target. We only need
8385 to do this for structures at the top level, i.e. if the target of
8386 any DW_AT_specification (if any; otherwise the DIE itself) does not
8387 have a parent. */
8388
8389/* Compute the scope prefix associated with PDI's parent, in
8390 compilation unit CU. The result will be allocated on CU's
8391 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8392 field. NULL is returned if no prefix is necessary. */
15d034d0 8393static const char *
72bf9492
DJ
8394partial_die_parent_scope (struct partial_die_info *pdi,
8395 struct dwarf2_cu *cu)
8396{
15d034d0 8397 const char *grandparent_scope;
72bf9492 8398 struct partial_die_info *parent, *real_pdi;
91c24f0a 8399
72bf9492
DJ
8400 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8401 then this means the parent of the specification DIE. */
8402
8403 real_pdi = pdi;
72bf9492 8404 while (real_pdi->has_specification)
fb816e8b 8405 {
122cf0f2
AB
8406 auto res = find_partial_die (real_pdi->spec_offset,
8407 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8408 real_pdi = res.pdi;
8409 cu = res.cu;
8410 }
72bf9492
DJ
8411
8412 parent = real_pdi->die_parent;
8413 if (parent == NULL)
8414 return NULL;
8415
8416 if (parent->scope_set)
8417 return parent->scope;
8418
52356b79 8419 parent->fixup (cu);
72bf9492 8420
10b3939b 8421 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8422
acebe513
UW
8423 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8424 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8425 Work around this problem here. */
8426 if (cu->language == language_cplus
6e70227d 8427 && parent->tag == DW_TAG_namespace
7d00ffec 8428 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
8429 && grandparent_scope == NULL)
8430 {
8431 parent->scope = NULL;
8432 parent->scope_set = 1;
8433 return NULL;
8434 }
8435
0a4b0913 8436 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8437 if (pdi->tag == DW_TAG_enumerator)
8438 /* Enumerators should not get the name of the enumeration as a prefix. */
8439 parent->scope = grandparent_scope;
8440 else if (parent->tag == DW_TAG_namespace
f55ee35c 8441 || parent->tag == DW_TAG_module
72bf9492
DJ
8442 || parent->tag == DW_TAG_structure_type
8443 || parent->tag == DW_TAG_class_type
680b30c7 8444 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8445 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8446 || parent->tag == DW_TAG_enumeration_type
8447 || (cu->language == language_fortran
8448 && parent->tag == DW_TAG_subprogram
8449 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8450 {
8451 if (grandparent_scope == NULL)
7d00ffec 8452 parent->scope = parent->name (cu);
72bf9492 8453 else
3e43a32a
MS
8454 parent->scope = typename_concat (&cu->comp_unit_obstack,
8455 grandparent_scope,
7d00ffec 8456 parent->name (cu), 0, cu);
72bf9492 8457 }
72bf9492
DJ
8458 else
8459 {
8460 /* FIXME drow/2004-04-01: What should we be doing with
8461 function-local names? For partial symbols, we should probably be
8462 ignoring them. */
fa9c3fa0
TT
8463 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8464 dwarf_tag_name (parent->tag),
8465 sect_offset_str (pdi->sect_off));
72bf9492 8466 parent->scope = grandparent_scope;
c906108c
SS
8467 }
8468
72bf9492
DJ
8469 parent->scope_set = 1;
8470 return parent->scope;
8471}
8472
8473/* Return the fully scoped name associated with PDI, from compilation unit
8474 CU. The result will be allocated with malloc. */
4568ecf9 8475
43816ebc 8476static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8477partial_die_full_name (struct partial_die_info *pdi,
8478 struct dwarf2_cu *cu)
8479{
15d034d0 8480 const char *parent_scope;
72bf9492 8481
98bfdba5
PA
8482 /* If this is a template instantiation, we can not work out the
8483 template arguments from partial DIEs. So, unfortunately, we have
8484 to go through the full DIEs. At least any work we do building
8485 types here will be reused if full symbols are loaded later. */
8486 if (pdi->has_template_arguments)
8487 {
52356b79 8488 pdi->fixup (cu);
98bfdba5 8489
7d00ffec 8490 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8491 {
8492 struct die_info *die;
8493 struct attribute attr;
8494 struct dwarf2_cu *ref_cu = cu;
8495
b64f50a1 8496 /* DW_FORM_ref_addr is using section offset. */
b4069958 8497 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8498 attr.form = DW_FORM_ref_addr;
9c541725 8499 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8500 die = follow_die_ref (NULL, &attr, &ref_cu);
8501
43816ebc 8502 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8503 }
8504 }
8505
72bf9492
DJ
8506 parent_scope = partial_die_parent_scope (pdi, cu);
8507 if (parent_scope == NULL)
8508 return NULL;
8509 else
43816ebc 8510 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8511 pdi->name (cu),
8512 0, cu));
c906108c
SS
8513}
8514
8515static void
72bf9492 8516add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8517{
976ca316
SM
8518 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8519 struct objfile *objfile = per_objfile->objfile;
08feed99 8520 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8521 CORE_ADDR addr = 0;
15d034d0 8522 const char *actual_name = NULL;
e142c38c
DJ
8523 CORE_ADDR baseaddr;
8524
b3b3bada 8525 baseaddr = objfile->text_section_offset ();
c906108c 8526
43816ebc
TT
8527 gdb::unique_xmalloc_ptr<char> built_actual_name
8528 = partial_die_full_name (pdi, cu);
15d034d0 8529 if (built_actual_name != NULL)
43816ebc 8530 actual_name = built_actual_name.get ();
63d06c5c 8531
72bf9492 8532 if (actual_name == NULL)
7d00ffec 8533 actual_name = pdi->name (cu);
72bf9492 8534
76e288d1
TT
8535 partial_symbol psymbol;
8536 memset (&psymbol, 0, sizeof (psymbol));
8537 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
a52d653e 8538 psymbol.ginfo.set_section_index (-1);
76e288d1
TT
8539
8540 /* The code below indicates that the psymbol should be installed by
8541 setting this. */
8542 gdb::optional<psymbol_placement> where;
8543
c906108c
SS
8544 switch (pdi->tag)
8545 {
b1dc1806 8546 case DW_TAG_inlined_subroutine:
c906108c 8547 case DW_TAG_subprogram:
79748972
TT
8548 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8549 - baseaddr);
0a4b0913
AB
8550 if (pdi->is_external
8551 || cu->language == language_ada
8552 || (cu->language == language_fortran
8553 && pdi->die_parent != NULL
8554 && pdi->die_parent->tag == DW_TAG_subprogram))
8555 {
dda83cd7
SM
8556 /* Normally, only "external" DIEs are part of the global scope.
8557 But in Ada and Fortran, we want to be able to access nested
8558 procedures globally. So all Ada and Fortran subprograms are
8559 stored in the global scope. */
76e288d1 8560 where = psymbol_placement::GLOBAL;
c906108c
SS
8561 }
8562 else
76e288d1
TT
8563 where = psymbol_placement::STATIC;
8564
8565 psymbol.domain = VAR_DOMAIN;
8566 psymbol.aclass = LOC_BLOCK;
a52d653e 8567 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1 8568 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8569
8570 if (pdi->main_subprogram && actual_name != NULL)
8571 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8572 break;
72929c62 8573 case DW_TAG_constant:
76e288d1
TT
8574 psymbol.domain = VAR_DOMAIN;
8575 psymbol.aclass = LOC_STATIC;
8576 where = (pdi->is_external
8577 ? psymbol_placement::GLOBAL
8578 : psymbol_placement::STATIC);
72929c62 8579 break;
c906108c 8580 case DW_TAG_variable:
95554aad
TT
8581 if (pdi->d.locdesc)
8582 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8583
95554aad 8584 if (pdi->d.locdesc
caac4577 8585 && addr == 0
976ca316 8586 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8587 {
8588 /* A global or static variable may also have been stripped
8589 out by the linker if unused, in which case its address
8590 will be nullified; do not add such variables into partial
8591 symbol table then. */
8592 }
8593 else if (pdi->is_external)
c906108c
SS
8594 {
8595 /* Global Variable.
8596 Don't enter into the minimal symbol tables as there is
8597 a minimal symbol table entry from the ELF symbols already.
8598 Enter into partial symbol table if it has a location
8599 descriptor or a type.
8600 If the location descriptor is missing, new_symbol will create
8601 a LOC_UNRESOLVED symbol, the address of the variable will then
8602 be determined from the minimal symbol table whenever the variable
8603 is referenced.
8604 The address for the partial symbol table entry is not
8605 used by GDB, but it comes in handy for debugging partial symbol
8606 table building. */
8607
95554aad 8608 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8609 {
8610 psymbol.domain = VAR_DOMAIN;
8611 psymbol.aclass = LOC_STATIC;
a52d653e 8612 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8613 psymbol.ginfo.value.address = addr;
8614 where = psymbol_placement::GLOBAL;
8615 }
c906108c
SS
8616 }
8617 else
8618 {
ff908ebf
AW
8619 int has_loc = pdi->d.locdesc != NULL;
8620
8621 /* Static Variable. Skip symbols whose value we cannot know (those
8622 without location descriptors or constant values). */
8623 if (!has_loc && !pdi->has_const_value)
43816ebc 8624 return;
ff908ebf 8625
76e288d1
TT
8626 psymbol.domain = VAR_DOMAIN;
8627 psymbol.aclass = LOC_STATIC;
a52d653e 8628 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8629 if (has_loc)
8630 psymbol.ginfo.value.address = addr;
8631 where = psymbol_placement::STATIC;
c906108c
SS
8632 }
8633 break;
d8f62e84 8634 case DW_TAG_array_type:
c906108c
SS
8635 case DW_TAG_typedef:
8636 case DW_TAG_base_type:
a02abb62 8637 case DW_TAG_subrange_type:
76e288d1
TT
8638 psymbol.domain = VAR_DOMAIN;
8639 psymbol.aclass = LOC_TYPEDEF;
8640 where = psymbol_placement::STATIC;
c906108c 8641 break;
74921315 8642 case DW_TAG_imported_declaration:
72bf9492 8643 case DW_TAG_namespace:
76e288d1
TT
8644 psymbol.domain = VAR_DOMAIN;
8645 psymbol.aclass = LOC_TYPEDEF;
8646 where = psymbol_placement::GLOBAL;
72bf9492 8647 break;
530e8392 8648 case DW_TAG_module:
a5fd13a9 8649 /* With Fortran 77 there might be a "BLOCK DATA" module
dda83cd7
SM
8650 available without any name. If so, we skip the module as it
8651 doesn't bring any value. */
a5fd13a9 8652 if (actual_name != nullptr)
76e288d1
TT
8653 {
8654 psymbol.domain = MODULE_DOMAIN;
8655 psymbol.aclass = LOC_TYPEDEF;
8656 where = psymbol_placement::GLOBAL;
8657 }
530e8392 8658 break;
c906108c 8659 case DW_TAG_class_type:
680b30c7 8660 case DW_TAG_interface_type:
c906108c
SS
8661 case DW_TAG_structure_type:
8662 case DW_TAG_union_type:
8663 case DW_TAG_enumeration_type:
fa4028e9 8664 /* Skip external references. The DWARF standard says in the section
dda83cd7
SM
8665 about "Structure, Union, and Class Type Entries": "An incomplete
8666 structure, union or class type is represented by a structure,
8667 union or class entry that does not have a byte size attribute
8668 and that has a DW_AT_declaration attribute." */
fa4028e9 8669 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8670 return;
fa4028e9 8671
63d06c5c
DC
8672 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8673 static vs. global. */
76e288d1
TT
8674 psymbol.domain = STRUCT_DOMAIN;
8675 psymbol.aclass = LOC_TYPEDEF;
8676 where = (cu->language == language_cplus
8677 ? psymbol_placement::GLOBAL
8678 : psymbol_placement::STATIC);
c906108c
SS
8679 break;
8680 case DW_TAG_enumerator:
76e288d1
TT
8681 psymbol.domain = VAR_DOMAIN;
8682 psymbol.aclass = LOC_CONST;
8683 where = (cu->language == language_cplus
8684 ? psymbol_placement::GLOBAL
8685 : psymbol_placement::STATIC);
c906108c
SS
8686 break;
8687 default:
8688 break;
8689 }
76e288d1
TT
8690
8691 if (where.has_value ())
8692 {
f049a313
TT
8693 if (built_actual_name != nullptr)
8694 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8695 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8696 psymbol.ginfo.set_linkage_name (actual_name);
8697 else
8698 {
8699 psymbol.ginfo.set_demangled_name (actual_name,
8700 &objfile->objfile_obstack);
8701 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8702 }
7b249e47
TT
8703 cu->per_cu->v.psymtab->add_psymbol
8704 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8705 objfile);
76e288d1 8706 }
c906108c
SS
8707}
8708
5c4e30ca
DC
8709/* Read a partial die corresponding to a namespace; also, add a symbol
8710 corresponding to that namespace to the symbol table. NAMESPACE is
8711 the name of the enclosing namespace. */
91c24f0a 8712
72bf9492
DJ
8713static void
8714add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8715 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8716 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8717{
72bf9492 8718 /* Add a symbol for the namespace. */
e7c27a73 8719
72bf9492 8720 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8721
8722 /* Now scan partial symbols in that namespace. */
8723
91c24f0a 8724 if (pdi->has_children)
cdc07690 8725 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8726}
8727
5d7cb8df
JK
8728/* Read a partial die corresponding to a Fortran module. */
8729
8730static void
8731add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8732 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8733{
530e8392
KB
8734 /* Add a symbol for the namespace. */
8735
8736 add_partial_symbol (pdi, cu);
8737
f55ee35c 8738 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8739
8740 if (pdi->has_children)
cdc07690 8741 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8742}
8743
b1dc1806
XR
8744/* Read a partial die corresponding to a subprogram or an inlined
8745 subprogram and create a partial symbol for that subprogram.
8746 When the CU language allows it, this routine also defines a partial
8747 symbol for each nested subprogram that this subprogram contains.
8748 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8749 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8750
cdc07690
YQ
8751 PDI may also be a lexical block, in which case we simply search
8752 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8753 Again, this is only performed when the CU language allows this
8754 type of definitions. */
8755
8756static void
8757add_partial_subprogram (struct partial_die_info *pdi,
8758 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8759 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8760{
b1dc1806 8761 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8762 {
8763 if (pdi->has_pc_info)
dda83cd7
SM
8764 {
8765 if (pdi->lowpc < *lowpc)
8766 *lowpc = pdi->lowpc;
8767 if (pdi->highpc > *highpc)
8768 *highpc = pdi->highpc;
cdc07690 8769 if (set_addrmap)
5734ee8b 8770 {
5e22e966 8771 struct objfile *objfile = cu->per_objfile->objfile;
84685904 8772 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 8773 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8774 CORE_ADDR baseaddr;
b926417a
TT
8775 CORE_ADDR this_highpc;
8776 CORE_ADDR this_lowpc;
5734ee8b 8777
b3b3bada 8778 baseaddr = objfile->text_section_offset ();
b926417a
TT
8779 this_lowpc
8780 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8781 pdi->lowpc + baseaddr)
8782 - baseaddr);
8783 this_highpc
8784 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8785 pdi->highpc + baseaddr)
8786 - baseaddr);
84685904 8787 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
b926417a 8788 this_lowpc, this_highpc - 1,
9291a0cd 8789 cu->per_cu->v.psymtab);
5734ee8b 8790 }
dda83cd7 8791 }
481860b3
GB
8792
8793 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8794 {
dda83cd7 8795 if (!pdi->is_declaration)
e8d05480
JB
8796 /* Ignore subprogram DIEs that do not have a name, they are
8797 illegal. Do not emit a complaint at this point, we will
8798 do so when we convert this psymtab into a symtab. */
7d00ffec 8799 if (pdi->name (cu))
e8d05480 8800 add_partial_symbol (pdi, cu);
dda83cd7 8801 }
bc30ff58 8802 }
6e70227d 8803
bc30ff58
JB
8804 if (! pdi->has_children)
8805 return;
8806
0a4b0913 8807 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8808 {
8809 pdi = pdi->die_child;
8810 while (pdi != NULL)
8811 {
52356b79 8812 pdi->fixup (cu);
bc30ff58 8813 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8814 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8815 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8816 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8817 pdi = pdi->die_sibling;
8818 }
8819 }
8820}
8821
91c24f0a
DC
8822/* Read a partial die corresponding to an enumeration type. */
8823
72bf9492
DJ
8824static void
8825add_partial_enumeration (struct partial_die_info *enum_pdi,
8826 struct dwarf2_cu *cu)
91c24f0a 8827{
72bf9492 8828 struct partial_die_info *pdi;
91c24f0a 8829
7d00ffec 8830 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8831 add_partial_symbol (enum_pdi, cu);
8832
8833 pdi = enum_pdi->die_child;
8834 while (pdi)
91c24f0a 8835 {
7d00ffec 8836 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8837 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8838 else
72bf9492
DJ
8839 add_partial_symbol (pdi, cu);
8840 pdi = pdi->die_sibling;
91c24f0a 8841 }
91c24f0a
DC
8842}
8843
6caca83c
CC
8844/* Return the initial uleb128 in the die at INFO_PTR. */
8845
8846static unsigned int
d521ce57 8847peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8848{
8849 unsigned int bytes_read;
8850
8851 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8852}
8853
685af9cd
TT
8854/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8855 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8856
4bb7a0a7
DJ
8857 Return the corresponding abbrev, or NULL if the number is zero (indicating
8858 an empty DIE). In either case *BYTES_READ will be set to the length of
8859 the initial number. */
8860
7c32eebb 8861static const struct abbrev_info *
685af9cd
TT
8862peek_die_abbrev (const die_reader_specs &reader,
8863 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8864{
685af9cd 8865 dwarf2_cu *cu = reader.cu;
0280fdcc 8866 bfd *abfd = reader.abfd;
685af9cd
TT
8867 unsigned int abbrev_number
8868 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8869
8870 if (abbrev_number == 0)
8871 return NULL;
8872
7c32eebb
TT
8873 const abbrev_info *abbrev
8874 = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8875 if (!abbrev)
8876 {
422b9917 8877 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8878 " at offset %s [in module %s]"),
422b9917 8879 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8880 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8881 }
8882
8883 return abbrev;
8884}
8885
93311388
DE
8886/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8887 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8888 DIE. Any children of the skipped DIEs will also be skipped. */
8889
d521ce57
TT
8890static const gdb_byte *
8891skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8892{
4bb7a0a7
DJ
8893 while (1)
8894 {
685af9cd 8895 unsigned int bytes_read;
7c32eebb
TT
8896 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8897 &bytes_read);
685af9cd 8898
4bb7a0a7
DJ
8899 if (abbrev == NULL)
8900 return info_ptr + bytes_read;
8901 else
dee91e82 8902 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8903 }
8904}
8905
93311388
DE
8906/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8907 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8908 abbrev corresponding to that skipped uleb128 should be passed in
8909 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8910 children. */
8911
d521ce57
TT
8912static const gdb_byte *
8913skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7c32eebb 8914 const struct abbrev_info *abbrev)
4bb7a0a7
DJ
8915{
8916 unsigned int bytes_read;
8917 struct attribute attr;
dee91e82
DE
8918 bfd *abfd = reader->abfd;
8919 struct dwarf2_cu *cu = reader->cu;
d521ce57 8920 const gdb_byte *buffer = reader->buffer;
f664829e 8921 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8922 unsigned int form, i;
8923
8924 for (i = 0; i < abbrev->num_attrs; i++)
8925 {
8926 /* The only abbrev we care about is DW_AT_sibling. */
8927 if (abbrev->attrs[i].name == DW_AT_sibling)
8928 {
7a5f294d 8929 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8930 if (attr.form == DW_FORM_ref_addr)
b98664d3 8931 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8932 else
b9502d3f 8933 {
0826b30a 8934 sect_offset off = attr.get_ref_die_offset ();
9c541725 8935 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8936
8937 if (sibling_ptr < info_ptr)
b98664d3 8938 complaint (_("DW_AT_sibling points backwards"));
22869d73 8939 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8940 reader->die_section->overflow_complaint ();
b9502d3f
WN
8941 else
8942 return sibling_ptr;
8943 }
4bb7a0a7
DJ
8944 }
8945
8946 /* If it isn't DW_AT_sibling, skip this attribute. */
8947 form = abbrev->attrs[i].form;
8948 skip_attribute:
8949 switch (form)
8950 {
4bb7a0a7 8951 case DW_FORM_ref_addr:
ae411497
TT
8952 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8953 and later it is offset sized. */
8954 if (cu->header.version == 2)
8955 info_ptr += cu->header.addr_size;
8956 else
8957 info_ptr += cu->header.offset_size;
8958 break;
36586728
TT
8959 case DW_FORM_GNU_ref_alt:
8960 info_ptr += cu->header.offset_size;
8961 break;
ae411497 8962 case DW_FORM_addr:
4bb7a0a7
DJ
8963 info_ptr += cu->header.addr_size;
8964 break;
8965 case DW_FORM_data1:
8966 case DW_FORM_ref1:
8967 case DW_FORM_flag:
8fe0f950 8968 case DW_FORM_strx1:
4bb7a0a7
DJ
8969 info_ptr += 1;
8970 break;
2dc7f7b3 8971 case DW_FORM_flag_present:
43988095 8972 case DW_FORM_implicit_const:
2dc7f7b3 8973 break;
4bb7a0a7
DJ
8974 case DW_FORM_data2:
8975 case DW_FORM_ref2:
8fe0f950 8976 case DW_FORM_strx2:
4bb7a0a7
DJ
8977 info_ptr += 2;
8978 break;
8fe0f950
AT
8979 case DW_FORM_strx3:
8980 info_ptr += 3;
8981 break;
4bb7a0a7
DJ
8982 case DW_FORM_data4:
8983 case DW_FORM_ref4:
8fe0f950 8984 case DW_FORM_strx4:
4bb7a0a7
DJ
8985 info_ptr += 4;
8986 break;
8987 case DW_FORM_data8:
8988 case DW_FORM_ref8:
55f1336d 8989 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8990 info_ptr += 8;
8991 break;
0224619f
JK
8992 case DW_FORM_data16:
8993 info_ptr += 16;
8994 break;
4bb7a0a7 8995 case DW_FORM_string:
9b1c24c8 8996 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8997 info_ptr += bytes_read;
8998 break;
2dc7f7b3 8999 case DW_FORM_sec_offset:
4bb7a0a7 9000 case DW_FORM_strp:
36586728 9001 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9002 info_ptr += cu->header.offset_size;
9003 break;
2dc7f7b3 9004 case DW_FORM_exprloc:
4bb7a0a7
DJ
9005 case DW_FORM_block:
9006 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9007 info_ptr += bytes_read;
9008 break;
9009 case DW_FORM_block1:
9010 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9011 break;
9012 case DW_FORM_block2:
9013 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9014 break;
9015 case DW_FORM_block4:
9016 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9017 break;
336d760d 9018 case DW_FORM_addrx:
cf532bd1 9019 case DW_FORM_strx:
4bb7a0a7
DJ
9020 case DW_FORM_sdata:
9021 case DW_FORM_udata:
9022 case DW_FORM_ref_udata:
3019eac3
DE
9023 case DW_FORM_GNU_addr_index:
9024 case DW_FORM_GNU_str_index:
18a8505e 9025 case DW_FORM_rnglistx:
41144253 9026 case DW_FORM_loclistx:
d521ce57 9027 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9028 break;
9029 case DW_FORM_indirect:
9030 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9031 info_ptr += bytes_read;
9032 /* We need to continue parsing from here, so just go back to
9033 the top. */
9034 goto skip_attribute;
9035
9036 default:
3e43a32a
MS
9037 error (_("Dwarf Error: Cannot handle %s "
9038 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9039 dwarf_form_name (form),
9040 bfd_get_filename (abfd));
9041 }
9042 }
9043
9044 if (abbrev->has_children)
dee91e82 9045 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9046 else
9047 return info_ptr;
9048}
9049
93311388 9050/* Locate ORIG_PDI's sibling.
dee91e82 9051 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9052
d521ce57 9053static const gdb_byte *
dee91e82
DE
9054locate_pdi_sibling (const struct die_reader_specs *reader,
9055 struct partial_die_info *orig_pdi,
d521ce57 9056 const gdb_byte *info_ptr)
91c24f0a
DC
9057{
9058 /* Do we know the sibling already? */
72bf9492 9059
91c24f0a
DC
9060 if (orig_pdi->sibling)
9061 return orig_pdi->sibling;
9062
9063 /* Are there any children to deal with? */
9064
9065 if (!orig_pdi->has_children)
9066 return info_ptr;
9067
4bb7a0a7 9068 /* Skip the children the long way. */
91c24f0a 9069
dee91e82 9070 return skip_children (reader, info_ptr);
91c24f0a
DC
9071}
9072
257e7a09 9073/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9074 not NULL. */
c906108c 9075
891813be
TT
9076void
9077dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 9078{
976ca316 9079 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 9080
976ca316 9081 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 9082
077cbab2
TT
9083 /* If this psymtab is constructed from a debug-only objfile, the
9084 has_section_at_zero flag will not necessarily be correct. We
9085 can get the correct value for this flag by looking at the data
9086 associated with the (presumably stripped) associated objfile. */
9087 if (objfile->separate_debug_objfile_backlink)
c906108c 9088 {
976ca316 9089 dwarf2_per_objfile *per_objfile_backlink
077cbab2 9090 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 9091
976ca316
SM
9092 per_objfile->per_bfd->has_section_at_zero
9093 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 9094 }
98bfdba5 9095
8566b89b 9096 expand_psymtab (objfile);
95554aad 9097
976ca316 9098 process_cu_includes (per_objfile);
c906108c 9099}
9cdd5dbd
DE
9100\f
9101/* Reading in full CUs. */
c906108c 9102
10b3939b
DJ
9103/* Add PER_CU to the queue. */
9104
9105static void
120ce1b5
SM
9106queue_comp_unit (dwarf2_per_cu_data *per_cu,
9107 dwarf2_per_objfile *per_objfile,
95554aad 9108 enum language pretend_language)
10b3939b 9109{
10b3939b 9110 per_cu->queued = 1;
08ac5771
SM
9111
9112 gdb_assert (per_objfile->per_bfd->queue.has_value ());
9113 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
9114}
9115
616c069a
SM
9116/* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
9117
89e63ee4
DE
9118 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9119 dependency.
69d751e3 9120
616c069a
SM
9121 Return true if maybe_queue_comp_unit requires the caller to load the CU's
9122 DIEs, false otherwise.
9123
9124 Explanation: there is an invariant that if a CU is queued for expansion
9125 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
9126 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
9127 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
9128 are not yet loaded, the the caller must load the CU's DIEs to ensure the
9129 invariant is respected.
9130
9131 The caller is therefore not required to load the CU's DIEs (we return false)
9132 if:
9133
9134 - the CU is already expanded, and therefore does not get enqueued
9135 - the CU gets enqueued for expansion, but its DIEs are already loaded
9136
9137 Note that the caller should not use this function's return value as an
9138 indicator of whether the CU's DIEs are loaded right now, it should check
9139 that by calling `dwarf2_per_objfile::get_cu` instead. */
0907af0c
DE
9140
9141static int
89e63ee4 9142maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
9143 dwarf2_per_cu_data *per_cu,
9144 dwarf2_per_objfile *per_objfile,
0907af0c
DE
9145 enum language pretend_language)
9146{
9147 /* We may arrive here during partial symbol reading, if we need full
9148 DIEs to process an unusual case (e.g. template arguments). Do
9149 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 9150 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 9151 {
7188ed02
SM
9152 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9153
9154 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
9155 return 1;
9156 return 0;
9157 }
9158
9159 /* Mark the dependence relation so that we don't flush PER_CU
9160 too early. */
89e63ee4
DE
9161 if (dependent_cu != NULL)
9162 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9163
9164 /* If it's already on the queue, we have nothing to do. */
9165 if (per_cu->queued)
de53369b
SM
9166 {
9167 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
9168 loaded. */
9169 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
616c069a
SM
9170
9171 /* If the CU is queued for expansion, it should not already be
9172 expanded. */
9173 gdb_assert (!per_objfile->symtab_set_p (per_cu));
9174
9175 /* The DIEs are already loaded, the caller doesn't need to do it. */
de53369b
SM
9176 return 0;
9177 }
0907af0c 9178
616c069a
SM
9179 bool queued = false;
9180 if (!per_objfile->symtab_set_p (per_cu))
9181 {
9182 /* Add it to the queue. */
9183 queue_comp_unit (per_cu, per_objfile, pretend_language);
9184 queued = true;
9185 }
9186
0907af0c
DE
9187 /* If the compilation unit is already loaded, just mark it as
9188 used. */
7188ed02
SM
9189 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9190 if (cu != nullptr)
616c069a 9191 cu->last_used = 0;
0907af0c 9192
616c069a
SM
9193 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
9194 and the DIEs are not already loaded. */
9195 return queued && cu == nullptr;
0907af0c
DE
9196}
9197
10b3939b
DJ
9198/* Process the queue. */
9199
9200static void
976ca316 9201process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 9202{
6f738b01
SM
9203 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9204 objfile_name (per_objfile->objfile));
45cfd468 9205
03dd20cc
DJ
9206 /* The queue starts out with one item, but following a DIE reference
9207 may load a new CU, adding it to the end of the queue. */
08ac5771 9208 while (!per_objfile->per_bfd->queue->empty ())
10b3939b 9209 {
08ac5771 9210 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
7188ed02 9211 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 9212
976ca316 9213 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 9214 {
976ca316 9215 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 9216
7188ed02
SM
9217 /* Skip dummy CUs. */
9218 if (cu != nullptr)
73be47f5 9219 {
7188ed02
SM
9220 unsigned int debug_print_threshold;
9221 char buf[100];
9222
9223 if (per_cu->is_debug_types)
9224 {
9225 struct signatured_type *sig_type =
9226 (struct signatured_type *) per_cu;
9227
9228 sprintf (buf, "TU %s at offset %s",
9229 hex_string (sig_type->signature),
9230 sect_offset_str (per_cu->sect_off));
9231 /* There can be 100s of TUs.
9232 Only print them in verbose mode. */
9233 debug_print_threshold = 2;
9234 }
9235 else
9236 {
9237 sprintf (buf, "CU at offset %s",
9238 sect_offset_str (per_cu->sect_off));
9239 debug_print_threshold = 1;
9240 }
247f5c4f 9241
7188ed02 9242 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 9243 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
f4dc4d17 9244
7188ed02
SM
9245 if (per_cu->is_debug_types)
9246 process_full_type_unit (cu, item.pretend_language);
9247 else
9248 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 9249
7188ed02 9250 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 9251 dwarf_read_debug_printf ("Done expanding %s", buf);
7188ed02 9252 }
f4dc4d17 9253 }
10b3939b 9254
7188ed02 9255 per_cu->queued = 0;
08ac5771 9256 per_objfile->per_bfd->queue->pop ();
10b3939b
DJ
9257 }
9258
6f738b01
SM
9259 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9260 objfile_name (per_objfile->objfile));
10b3939b
DJ
9261}
9262
10b3939b
DJ
9263/* Read in full symbols for PST, and anything it depends on. */
9264
8566b89b
TT
9265void
9266dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9267{
af758d11 9268 gdb_assert (!readin_p (objfile));
95554aad 9269
17ee85fc
TT
9270 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9271 free_cached_comp_units freer (per_objfile);
48993951 9272 expand_dependencies (objfile);
aaa75496 9273
97a1449a 9274 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9275 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9276}
9277
af758d11
SM
9278/* See psympriv.h. */
9279
9280bool
9281dwarf2_psymtab::readin_p (struct objfile *objfile) const
9282{
9283 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9284 return per_objfile->symtab_set_p (per_cu_data);
9285}
9286
9287/* See psympriv.h. */
9288
9289compunit_symtab *
9290dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9291{
9292 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9293 return per_objfile->get_symtab (per_cu_data);
9294}
9295
dee91e82
DE
9296/* Trivial hash function for die_info: the hash value of a DIE
9297 is its offset in .debug_info for this objfile. */
10b3939b 9298
dee91e82
DE
9299static hashval_t
9300die_hash (const void *item)
10b3939b 9301{
9a3c8263 9302 const struct die_info *die = (const struct die_info *) item;
6502dd73 9303
9c541725 9304 return to_underlying (die->sect_off);
dee91e82 9305}
63d06c5c 9306
dee91e82
DE
9307/* Trivial comparison function for die_info structures: two DIEs
9308 are equal if they have the same offset. */
98bfdba5 9309
dee91e82
DE
9310static int
9311die_eq (const void *item_lhs, const void *item_rhs)
9312{
9a3c8263
SM
9313 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9314 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9315
9c541725 9316 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9317}
c906108c 9318
4a636814
SM
9319/* Load the DIEs associated with PER_CU into memory.
9320
9321 In some cases, the caller, while reading partial symbols, will need to load
9322 the full symbols for the CU for some reason. It will already have a
9323 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9324 rather than creating a new one. */
c906108c 9325
dee91e82 9326static void
ab432490
SM
9327load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9328 dwarf2_per_objfile *per_objfile,
4a636814 9329 dwarf2_cu *existing_cu,
c0ab21c2
TT
9330 bool skip_partial,
9331 enum language pretend_language)
dee91e82 9332{
c0ab21c2
TT
9333 gdb_assert (! this_cu->is_debug_types);
9334
7188ed02 9335 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
9336 if (reader.dummy_p)
9337 return;
9338
9339 struct dwarf2_cu *cu = reader.cu;
9340 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9341
dee91e82
DE
9342 gdb_assert (cu->die_hash == NULL);
9343 cu->die_hash =
9344 htab_create_alloc_ex (cu->header.length / 12,
9345 die_hash,
9346 die_eq,
9347 NULL,
9348 &cu->comp_unit_obstack,
9349 hashtab_obstack_allocate,
9350 dummy_obstack_deallocate);
e142c38c 9351
3e225074 9352 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9353 reader.comp_unit_die->child
9354 = read_die_and_siblings (&reader, reader.info_ptr,
9355 &info_ptr, reader.comp_unit_die);
9356 cu->dies = reader.comp_unit_die;
dee91e82 9357 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9358
9359 /* We try not to read any attributes in this function, because not
9cdd5dbd 9360 all CUs needed for references have been loaded yet, and symbol
10b3939b 9361 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9362 or we won't be able to build types correctly.
9363 Similarly, if we do not read the producer, we can not apply
9364 producer-specific interpretation. */
c0ab21c2 9365 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9366
9367 reader.keep ();
10b3939b
DJ
9368}
9369
3da10d80
KS
9370/* Add a DIE to the delayed physname list. */
9371
9372static void
9373add_to_method_list (struct type *type, int fnfield_index, int index,
9374 const char *name, struct die_info *die,
9375 struct dwarf2_cu *cu)
9376{
9377 struct delayed_method_info mi;
9378 mi.type = type;
9379 mi.fnfield_index = fnfield_index;
9380 mi.index = index;
9381 mi.name = name;
9382 mi.die = die;
c89b44cd 9383 cu->method_list.push_back (mi);
3da10d80
KS
9384}
9385
3693fdb3
PA
9386/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9387 "const" / "volatile". If so, decrements LEN by the length of the
9388 modifier and return true. Otherwise return false. */
9389
9390template<size_t N>
9391static bool
9392check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9393{
9394 size_t mod_len = sizeof (mod) - 1;
9395 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9396 {
9397 len -= mod_len;
9398 return true;
9399 }
9400 return false;
9401}
9402
3da10d80
KS
9403/* Compute the physnames of any methods on the CU's method list.
9404
9405 The computation of method physnames is delayed in order to avoid the
9406 (bad) condition that one of the method's formal parameters is of an as yet
9407 incomplete type. */
9408
9409static void
9410compute_delayed_physnames (struct dwarf2_cu *cu)
9411{
3693fdb3 9412 /* Only C++ delays computing physnames. */
c89b44cd 9413 if (cu->method_list.empty ())
3693fdb3
PA
9414 return;
9415 gdb_assert (cu->language == language_cplus);
9416
52941706 9417 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9418 {
1d06ead6 9419 const char *physname;
3da10d80 9420 struct fn_fieldlist *fn_flp
c89b44cd
TT
9421 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9422 physname = dwarf2_physname (mi.name, mi.die, cu);
9423 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9424 = physname ? physname : "";
3693fdb3
PA
9425
9426 /* Since there's no tag to indicate whether a method is a
9427 const/volatile overload, extract that information out of the
9428 demangled name. */
9429 if (physname != NULL)
9430 {
9431 size_t len = strlen (physname);
9432
9433 while (1)
9434 {
9435 if (physname[len] == ')') /* shortcut */
9436 break;
9437 else if (check_modifier (physname, len, " const"))
c89b44cd 9438 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9439 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9440 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9441 else
9442 break;
9443 }
9444 }
3da10d80 9445 }
c89b44cd
TT
9446
9447 /* The list is no longer needed. */
9448 cu->method_list.clear ();
3da10d80
KS
9449}
9450
a766d390
DE
9451/* Go objects should be embedded in a DW_TAG_module DIE,
9452 and it's not clear if/how imported objects will appear.
9453 To keep Go support simple until that's worked out,
9454 go back through what we've read and create something usable.
9455 We could do this while processing each DIE, and feels kinda cleaner,
9456 but that way is more invasive.
9457 This is to, for example, allow the user to type "p var" or "b main"
9458 without having to specify the package name, and allow lookups
9459 of module.object to work in contexts that use the expression
9460 parser. */
9461
9462static void
9463fixup_go_packaging (struct dwarf2_cu *cu)
9464{
421d1616 9465 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9466 struct pending *list;
9467 int i;
9468
c24bdb02 9469 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9470 list != NULL;
9471 list = list->next)
a766d390
DE
9472 {
9473 for (i = 0; i < list->nsyms; ++i)
9474 {
9475 struct symbol *sym = list->symbol[i];
9476
c1b5c1eb 9477 if (sym->language () == language_go
a766d390
DE
9478 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9479 {
421d1616
TT
9480 gdb::unique_xmalloc_ptr<char> this_package_name
9481 (go_symbol_package_name (sym));
a766d390
DE
9482
9483 if (this_package_name == NULL)
9484 continue;
9485 if (package_name == NULL)
421d1616 9486 package_name = std::move (this_package_name);
a766d390
DE
9487 else
9488 {
5e22e966 9489 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9490 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9491 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9492 (symbol_symtab (sym) != NULL
9493 ? symtab_to_filename_for_display
9494 (symbol_symtab (sym))
e3b94546 9495 : objfile_name (objfile)),
421d1616 9496 this_package_name.get (), package_name.get ());
a766d390
DE
9497 }
9498 }
9499 }
9500 }
9501
9502 if (package_name != NULL)
9503 {
5e22e966 9504 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9505 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9506 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9507 saved_package_name);
a766d390
DE
9508 struct symbol *sym;
9509
8c14c3a3 9510 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9511 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9512 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9513 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9514 e.g., "main" finds the "main" module and not C's main(). */
9515 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9516 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9517 SYMBOL_TYPE (sym) = type;
9518
c24bdb02 9519 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9520 }
9521}
9522
c9317f21
TT
9523/* Allocate a fully-qualified name consisting of the two parts on the
9524 obstack. */
9525
9526static const char *
9527rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9528{
9529 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9530}
9531
9c6a1327
TT
9532/* A helper that allocates a variant part to attach to a Rust enum
9533 type. OBSTACK is where the results should be allocated. TYPE is
9534 the type we're processing. DISCRIMINANT_INDEX is the index of the
57d02173
TT
9535 discriminant. It must be the index of one of the fields of TYPE,
9536 or -1 to mean there is no discriminant (univariant enum).
9c6a1327
TT
9537 DEFAULT_INDEX is the index of the default field; or -1 if there is
9538 no default. RANGES is indexed by "effective" field number (the
9539 field index, but omitting the discriminant and default fields) and
9540 must hold the discriminant values used by the variants. Note that
9541 RANGES must have a lifetime at least as long as OBSTACK -- either
9542 already allocated on it, or static. */
c9317f21 9543
9c6a1327
TT
9544static void
9545alloc_rust_variant (struct obstack *obstack, struct type *type,
9546 int discriminant_index, int default_index,
9547 gdb::array_view<discriminant_range> ranges)
9548{
57d02173
TT
9549 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9550 gdb_assert (discriminant_index == -1
9551 || (discriminant_index >= 0
9552 && discriminant_index < type->num_fields ()));
c9317f21 9553 gdb_assert (default_index == -1
1f704f76 9554 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9555
9c6a1327 9556 /* We have one variant for each non-discriminant field. */
57d02173
TT
9557 int n_variants = type->num_fields ();
9558 if (discriminant_index != -1)
9559 --n_variants;
c9317f21 9560
9c6a1327
TT
9561 variant *variants = new (obstack) variant[n_variants];
9562 int var_idx = 0;
9563 int range_idx = 0;
1f704f76 9564 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9565 {
9566 if (i == discriminant_index)
9567 continue;
c9317f21 9568
9c6a1327
TT
9569 variants[var_idx].first_field = i;
9570 variants[var_idx].last_field = i + 1;
9571
9572 /* The default field does not need a range, but other fields do.
9573 We skipped the discriminant above. */
9574 if (i != default_index)
9575 {
9576 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9577 ++range_idx;
9578 }
c9317f21 9579
9c6a1327
TT
9580 ++var_idx;
9581 }
9582
9583 gdb_assert (range_idx == ranges.size ());
9584 gdb_assert (var_idx == n_variants);
9585
9586 variant_part *part = new (obstack) variant_part;
9587 part->discriminant_index = discriminant_index;
57d02173
TT
9588 /* If there is no discriminant, then whether it is signed is of no
9589 consequence. */
9590 part->is_unsigned
9591 = (discriminant_index == -1
9592 ? false
c6d940a9 9593 : type->field (discriminant_index).type ()->is_unsigned ());
9c6a1327
TT
9594 part->variants = gdb::array_view<variant> (variants, n_variants);
9595
9596 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9597 gdb::array_view<variant_part> *prop_value
9598 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9599
9c6a1327 9600 struct dynamic_prop prop;
8c2e4e06 9601 prop.set_variant_parts (prop_value);
9c6a1327 9602
5c54719c 9603 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9604}
9605
9606/* Some versions of rustc emitted enums in an unusual way.
9607
9608 Ordinary enums were emitted as unions. The first element of each
9609 structure in the union was named "RUST$ENUM$DISR". This element
9610 held the discriminant.
9611
9612 These versions of Rust also implemented the "non-zero"
9613 optimization. When the enum had two values, and one is empty and
9614 the other holds a pointer that cannot be zero, the pointer is used
9615 as the discriminant, with a zero value meaning the empty variant.
9616 Here, the union's first member is of the form
9617 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9618 where the fieldnos are the indices of the fields that should be
9619 traversed in order to find the field (which may be several fields deep)
9620 and the variantname is the name of the variant of the case when the
9621 field is zero.
9622
9623 This function recognizes whether TYPE is of one of these forms,
9624 and, if so, smashes it to be a variant type. */
9625
9626static void
9627quirk_rust_enum (struct type *type, struct objfile *objfile)
9628{
78134374 9629 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9630
9631 /* We don't need to deal with empty enums. */
1f704f76 9632 if (type->num_fields () == 0)
c9317f21
TT
9633 return;
9634
9635#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9636 if (type->num_fields () == 1
c9317f21
TT
9637 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9638 {
9639 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9640
9641 /* Decode the field name to find the offset of the
9642 discriminant. */
9643 ULONGEST bit_offset = 0;
940da03e 9644 struct type *field_type = type->field (0).type ();
c9317f21
TT
9645 while (name[0] >= '0' && name[0] <= '9')
9646 {
9647 char *tail;
9648 unsigned long index = strtoul (name, &tail, 10);
9649 name = tail;
9650 if (*name != '$'
1f704f76 9651 || index >= field_type->num_fields ()
c9317f21
TT
9652 || (TYPE_FIELD_LOC_KIND (field_type, index)
9653 != FIELD_LOC_KIND_BITPOS))
9654 {
b98664d3 9655 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9656 "[in module %s]"),
9657 TYPE_FIELD_NAME (type, 0),
9658 objfile_name (objfile));
9659 return;
9660 }
9661 ++name;
9662
9663 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 9664 field_type = field_type->field (index).type ();
c9317f21
TT
9665 }
9666
9c6a1327
TT
9667 /* Smash this type to be a structure type. We have to do this
9668 because the type has already been recorded. */
67607e24 9669 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9670 type->set_num_fields (3);
9c6a1327 9671 /* Save the field we care about. */
ceacbf6e 9672 struct field saved_field = type->field (0);
3cabb6b0
SM
9673 type->set_fields
9674 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9675
9c6a1327 9676 /* Put the discriminant at index 0. */
5d14b6e5 9677 type->field (0).set_type (field_type);
9c6a1327
TT
9678 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9679 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9680 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9681
9682 /* The order of fields doesn't really matter, so put the real
9683 field at index 1 and the data-less field at index 2. */
ceacbf6e 9684 type->field (1) = saved_field;
9c6a1327 9685 TYPE_FIELD_NAME (type, 1)
940da03e
SM
9686 = rust_last_path_segment (type->field (1).type ()->name ());
9687 type->field (1).type ()->set_name
7d93a1e0 9688 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9689 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9690
9691 const char *dataless_name
7d93a1e0 9692 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9693 name);
9694 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9695 dataless_name);
5d14b6e5 9696 type->field (2).set_type (dataless_type);
c9317f21
TT
9697 /* NAME points into the original discriminant name, which
9698 already has the correct lifetime. */
9c6a1327 9699 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9700 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9701
9c6a1327
TT
9702 /* Indicate that this is a variant type. */
9703 static discriminant_range ranges[1] = { { 0, 0 } };
9704 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9705 }
77c2dba3
TT
9706 /* A union with a single anonymous field is probably an old-style
9707 univariant enum. */
1f704f76 9708 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9709 {
c9317f21
TT
9710 /* Smash this type to be a structure type. We have to do this
9711 because the type has already been recorded. */
67607e24 9712 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9713
940da03e 9714 struct type *field_type = type->field (0).type ();
c9317f21 9715 const char *variant_name
7d93a1e0 9716 = rust_last_path_segment (field_type->name ());
9c6a1327 9717 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9718 field_type->set_name
9719 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9720 type->name (), variant_name));
57d02173
TT
9721
9722 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
c9317f21
TT
9723 }
9724 else
9725 {
9726 struct type *disr_type = nullptr;
1f704f76 9727 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9728 {
940da03e 9729 disr_type = type->field (i).type ();
c9317f21 9730
78134374 9731 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9732 {
9733 /* All fields of a true enum will be structs. */
9734 return;
9735 }
1f704f76 9736 else if (disr_type->num_fields () == 0)
c9317f21
TT
9737 {
9738 /* Could be data-less variant, so keep going. */
a037790e 9739 disr_type = nullptr;
c9317f21
TT
9740 }
9741 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9742 "RUST$ENUM$DISR") != 0)
9743 {
9744 /* Not a Rust enum. */
9745 return;
9746 }
9747 else
9748 {
9749 /* Found one. */
9750 break;
9751 }
9752 }
9753
9754 /* If we got here without a discriminant, then it's probably
9755 just a union. */
9756 if (disr_type == nullptr)
9757 return;
9758
9759 /* Smash this type to be a structure type. We have to do this
9760 because the type has already been recorded. */
67607e24 9761 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9762
9c6a1327 9763 /* Make space for the discriminant field. */
ceacbf6e 9764 struct field *disr_field = &disr_type->field (0);
9c6a1327 9765 field *new_fields
1f704f76 9766 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9767 * sizeof (struct field)));
80fc5e77 9768 memcpy (new_fields + 1, type->fields (),
1f704f76 9769 type->num_fields () * sizeof (struct field));
3cabb6b0 9770 type->set_fields (new_fields);
1f704f76 9771 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9772
9773 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9774 type->field (0) = *disr_field;
9c6a1327
TT
9775 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9776 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9777
9778 /* We need a way to find the correct discriminant given a
9779 variant name. For convenience we build a map here. */
b6cdac4b 9780 struct type *enum_type = disr_field->type ();
c9317f21 9781 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9782 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9783 {
9784 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9785 {
9786 const char *name
9787 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9788 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9789 }
9790 }
9791
1f704f76 9792 int n_fields = type->num_fields ();
9c6a1327
TT
9793 /* We don't need a range entry for the discriminant, but we do
9794 need one for every other field, as there is no default
9795 variant. */
9796 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9797 discriminant_range,
9798 n_fields - 1);
c9317f21
TT
9799 /* Skip the discriminant here. */
9800 for (int i = 1; i < n_fields; ++i)
9801 {
9802 /* Find the final word in the name of this variant's type.
9803 That name can be used to look up the correct
9804 discriminant. */
9805 const char *variant_name
940da03e 9806 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9807
9808 auto iter = discriminant_map.find (variant_name);
9809 if (iter != discriminant_map.end ())
9c6a1327 9810 {
57d02173
TT
9811 ranges[i - 1].low = iter->second;
9812 ranges[i - 1].high = iter->second;
9c6a1327 9813 }
c9317f21 9814
57d02173
TT
9815 /* In Rust, each element should have the size of the
9816 enclosing enum. */
9817 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9818
bedda9ac 9819 /* Remove the discriminant field, if it exists. */
940da03e 9820 struct type *sub_type = type->field (i).type ();
1f704f76 9821 if (sub_type->num_fields () > 0)
bedda9ac 9822 {
5e33d5f4 9823 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9824 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9825 }
9c6a1327 9826 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9827 sub_type->set_name
9828 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9829 type->name (), variant_name));
c9317f21 9830 }
9c6a1327
TT
9831
9832 /* Indicate that this is a variant type. */
a1520ad8 9833 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9834 gdb::array_view<discriminant_range> (ranges,
9835 n_fields - 1));
c9317f21
TT
9836 }
9837}
9838
9839/* Rewrite some Rust unions to be structures with variants parts. */
9840
9841static void
9842rust_union_quirks (struct dwarf2_cu *cu)
9843{
9844 gdb_assert (cu->language == language_rust);
52941706 9845 for (type *type_ : cu->rust_unions)
5e22e966 9846 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9847 /* We don't need this any more. */
9848 cu->rust_unions.clear ();
c9317f21
TT
9849}
9850
8adb8487
TT
9851/* See read.h. */
9852
9853type_unit_group_unshareable *
9854dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9855{
9856 auto iter = this->m_type_units.find (tu_group);
9857 if (iter != this->m_type_units.end ())
9858 return iter->second.get ();
9859
9860 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9861 type_unit_group_unshareable *result = uniq.get ();
9862 this->m_type_units[tu_group] = std::move (uniq);
9863 return result;
9864}
9865
e286671b
TT
9866struct type *
9867dwarf2_per_objfile::get_type_for_signatured_type
9868 (signatured_type *sig_type) const
9869{
9870 auto iter = this->m_type_map.find (sig_type);
9871 if (iter == this->m_type_map.end ())
9872 return nullptr;
9873
9874 return iter->second;
9875}
9876
9877void dwarf2_per_objfile::set_type_for_signatured_type
9878 (signatured_type *sig_type, struct type *type)
9879{
9880 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9881
9882 this->m_type_map[sig_type] = type;
9883}
9884
95554aad
TT
9885/* A helper function for computing the list of all symbol tables
9886 included by PER_CU. */
9887
9888static void
4c39bc03 9889recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9890 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9891 dwarf2_per_cu_data *per_cu,
9892 dwarf2_per_objfile *per_objfile,
43f3e411 9893 struct compunit_symtab *immediate_parent)
95554aad 9894{
af758d11 9895 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9896 if (*slot != NULL)
9897 {
9898 /* This inclusion and its children have been processed. */
9899 return;
9900 }
9901
9902 *slot = per_cu;
af758d11 9903
95554aad 9904 /* Only add a CU if it has a symbol table. */
43182c09 9905 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9906 if (cust != NULL)
ec94af83
DE
9907 {
9908 /* If this is a type unit only add its symbol table if we haven't
9909 seen it yet (type unit per_cu's can share symtabs). */
9910 if (per_cu->is_debug_types)
9911 {
43f3e411 9912 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9913 if (*slot == NULL)
9914 {
43f3e411 9915 *slot = cust;
4c39bc03 9916 result->push_back (cust);
43f3e411
DE
9917 if (cust->user == NULL)
9918 cust->user = immediate_parent;
ec94af83
DE
9919 }
9920 }
9921 else
f9125b6c 9922 {
4c39bc03 9923 result->push_back (cust);
43f3e411
DE
9924 if (cust->user == NULL)
9925 cust->user = immediate_parent;
f9125b6c 9926 }
ec94af83 9927 }
95554aad 9928
ae640021
AB
9929 if (!per_cu->imported_symtabs_empty ())
9930 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9931 {
9932 recursively_compute_inclusions (result, all_children,
43182c09
SM
9933 all_type_symtabs, ptr, per_objfile,
9934 cust);
ae640021 9935 }
95554aad
TT
9936}
9937
43f3e411 9938/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9939 PER_CU. */
9940
9941static void
43182c09
SM
9942compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9943 dwarf2_per_objfile *per_objfile)
95554aad 9944{
f4dc4d17
DE
9945 gdb_assert (! per_cu->is_debug_types);
9946
ae640021 9947 if (!per_cu->imported_symtabs_empty ())
95554aad 9948 {
ae640021 9949 int len;
4c39bc03 9950 std::vector<compunit_symtab *> result_symtabs;
43182c09 9951 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9952
9953 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9954 if (cust == NULL)
95554aad
TT
9955 return;
9956
280a9412
TT
9957 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9958 htab_eq_pointer,
9959 NULL, xcalloc, xfree));
9960 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9961 htab_eq_pointer,
9962 NULL, xcalloc, xfree));
95554aad 9963
ae640021 9964 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83 9965 {
280a9412
TT
9966 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9967 all_type_symtabs.get (), ptr,
9968 per_objfile, cust);
ec94af83 9969 }
95554aad 9970
ec94af83 9971 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9972 len = result_symtabs.size ();
43f3e411 9973 cust->includes
f6e649dd 9974 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9975 struct compunit_symtab *, len + 1);
4c39bc03
TT
9976 memcpy (cust->includes, result_symtabs.data (),
9977 len * sizeof (compunit_symtab *));
43f3e411 9978 cust->includes[len] = NULL;
95554aad
TT
9979 }
9980}
9981
9982/* Compute the 'includes' field for the symtabs of all the CUs we just
9983 read. */
9984
9985static void
976ca316 9986process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9987{
976ca316 9988 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9989 {
9990 if (! iter->is_debug_types)
976ca316 9991 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9992 }
95554aad 9993
976ca316 9994 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9995}
9996
8fc0b21d 9997/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9998 already been loaded into memory. */
9999
10000static void
8fc0b21d 10001process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 10002{
976ca316
SM
10003 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10004 struct objfile *objfile = per_objfile->objfile;
08feed99 10005 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 10006 CORE_ADDR lowpc, highpc;
43f3e411 10007 struct compunit_symtab *cust;
10b3939b 10008 CORE_ADDR baseaddr;
4359dff1 10009 struct block *static_block;
3e29f34a 10010 CORE_ADDR addr;
10b3939b 10011
b3b3bada 10012 baseaddr = objfile->text_section_offset ();
10b3939b 10013
c89b44cd
TT
10014 /* Clear the list here in case something was left over. */
10015 cu->method_list.clear ();
10b3939b 10016
95554aad
TT
10017 cu->language = pretend_language;
10018 cu->language_defn = language_def (cu->language);
10019
1c47ec3e
TV
10020 dwarf2_find_base_address (cu->dies, cu);
10021
702cf3f5
AB
10022 /* Before we start reading the top-level DIE, ensure it has a valid tag
10023 type. */
10024 switch (cu->dies->tag)
10025 {
10026 case DW_TAG_compile_unit:
10027 case DW_TAG_partial_unit:
10028 case DW_TAG_type_unit:
10029 break;
10030 default:
10031 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
10032 dwarf_tag_name (cu->dies->tag),
10033 sect_offset_str (cu->per_cu->sect_off),
10034 objfile_name (per_objfile->objfile));
10035 }
10036
c906108c 10037 /* Do line number decoding in read_file_scope () */
10b3939b 10038 process_die (cu->dies, cu);
c906108c 10039
a766d390
DE
10040 /* For now fudge the Go package. */
10041 if (cu->language == language_go)
10042 fixup_go_packaging (cu);
10043
5f48f8f3 10044 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10045 should be complete, and it should now be safe to compute all of the
10046 physnames. */
10047 compute_delayed_physnames (cu);
3da10d80 10048
c9317f21
TT
10049 if (cu->language == language_rust)
10050 rust_union_quirks (cu);
10051
fae299cd
DC
10052 /* Some compilers don't define a DW_AT_high_pc attribute for the
10053 compilation unit. If the DW_AT_high_pc is missing, synthesize
10054 it, by scanning the DIE's below the compilation unit. */
10b3939b 10055 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10056
3e29f34a 10057 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10058 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10059
10060 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10061 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10062 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10063 addrmap to help ensure it has an accurate map of pc values belonging to
10064 this comp unit. */
10065 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10066
c24bdb02 10067 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10068 SECT_OFF_TEXT (objfile),
10069 0);
c906108c 10070
43f3e411 10071 if (cust != NULL)
c906108c 10072 {
df15bd07 10073 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10074
8be455d7
JK
10075 /* Set symtab language to language from DW_AT_language. If the
10076 compilation is from a C file generated by language preprocessors, do
10077 not set the language if it was already deduced by start_subfile. */
43f3e411 10078 if (!(cu->language == language_c
40e3ad0e 10079 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10080 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10081
10082 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10083 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10084 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10085 there were bugs in prologue debug info, fixed later in GCC-4.5
10086 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10087
10088 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10089 needed, it would be wrong due to missing DW_AT_producer there.
10090
10091 Still one can confuse GDB by using non-standard GCC compilation
10092 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10093 */
ab260dad 10094 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10095 cust->locations_valid = 1;
e0d00bc7
JK
10096
10097 if (gcc_4_minor >= 5)
43f3e411 10098 cust->epilogue_unwind_valid = 1;
96408a79 10099
43f3e411 10100 cust->call_site_htab = cu->call_site_htab;
c906108c 10101 }
9291a0cd 10102
976ca316 10103 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 10104
95554aad 10105 /* Push it for inclusion processing later. */
976ca316 10106 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
10107
10108 /* Not needed any more. */
c24bdb02 10109 cu->reset_builder ();
f4dc4d17 10110}
45cfd468 10111
8fc0b21d 10112/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
10113 already been loaded into memory. */
10114
10115static void
8fc0b21d 10116process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
10117 enum language pretend_language)
10118{
976ca316
SM
10119 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10120 struct objfile *objfile = per_objfile->objfile;
43f3e411 10121 struct compunit_symtab *cust;
0186c6a7
DE
10122 struct signatured_type *sig_type;
10123
8fc0b21d
SM
10124 gdb_assert (cu->per_cu->is_debug_types);
10125 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 10126
c89b44cd
TT
10127 /* Clear the list here in case something was left over. */
10128 cu->method_list.clear ();
f4dc4d17 10129
f4dc4d17
DE
10130 cu->language = pretend_language;
10131 cu->language_defn = language_def (cu->language);
10132
10133 /* The symbol tables are set up in read_type_unit_scope. */
10134 process_die (cu->dies, cu);
10135
10136 /* For now fudge the Go package. */
10137 if (cu->language == language_go)
10138 fixup_go_packaging (cu);
10139
5f48f8f3 10140 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10141 should be complete, and it should now be safe to compute all of the
10142 physnames. */
10143 compute_delayed_physnames (cu);
f4dc4d17 10144
c9317f21
TT
10145 if (cu->language == language_rust)
10146 rust_union_quirks (cu);
10147
f4dc4d17
DE
10148 /* TUs share symbol tables.
10149 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10150 of it with end_expandable_symtab. Otherwise, complete the addition of
10151 this TU's symbols to the existing symtab. */
8adb8487 10152 type_unit_group_unshareable *tug_unshare =
976ca316 10153 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 10154 if (tug_unshare->compunit_symtab == NULL)
45cfd468 10155 {
c24bdb02
KS
10156 buildsym_compunit *builder = cu->get_builder ();
10157 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 10158 tug_unshare->compunit_symtab = cust;
f4dc4d17 10159
43f3e411 10160 if (cust != NULL)
f4dc4d17
DE
10161 {
10162 /* Set symtab language to language from DW_AT_language. If the
10163 compilation is from a C file generated by language preprocessors,
10164 do not set the language if it was already deduced by
10165 start_subfile. */
43f3e411
DE
10166 if (!(cu->language == language_c
10167 && COMPUNIT_FILETABS (cust)->language != language_c))
10168 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10169 }
10170 }
10171 else
10172 {
c24bdb02 10173 cu->get_builder ()->augment_type_symtab ();
8adb8487 10174 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
10175 }
10176
976ca316 10177 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
10178
10179 /* Not needed any more. */
c24bdb02 10180 cu->reset_builder ();
c906108c
SS
10181}
10182
95554aad
TT
10183/* Process an imported unit DIE. */
10184
10185static void
10186process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10187{
10188 struct attribute *attr;
10189
f4dc4d17
DE
10190 /* For now we don't handle imported units in type units. */
10191 if (cu->per_cu->is_debug_types)
10192 {
10193 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10194 " supported in type units [in module %s]"),
5e22e966 10195 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
10196 }
10197
95554aad
TT
10198 attr = dwarf2_attr (die, DW_AT_import, cu);
10199 if (attr != NULL)
10200 {
0826b30a 10201 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 10202 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 10203 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 10204 dwarf2_per_cu_data *per_cu
ab432490 10205 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 10206
58990295
TV
10207 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10208 into another compilation unit, at root level. Regard this as a hint,
10209 and ignore it. */
10210 if (die->parent && die->parent->parent == NULL
10211 && per_cu->unit_type == DW_UT_compile
10212 && per_cu->lang == language_cplus)
10213 return;
10214
69d751e3 10215 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 10216 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
10217 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10218 false, cu->language);
95554aad 10219
ae640021 10220 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10221 }
10222}
10223
4c8aa72d
PA
10224/* RAII object that represents a process_die scope: i.e.,
10225 starts/finishes processing a DIE. */
10226class process_die_scope
adde2bff 10227{
4c8aa72d
PA
10228public:
10229 process_die_scope (die_info *die, dwarf2_cu *cu)
10230 : m_die (die), m_cu (cu)
10231 {
10232 /* We should only be processing DIEs not already in process. */
10233 gdb_assert (!m_die->in_process);
10234 m_die->in_process = true;
10235 }
8c3cb9fa 10236
4c8aa72d
PA
10237 ~process_die_scope ()
10238 {
10239 m_die->in_process = false;
10240
10241 /* If we're done processing the DIE for the CU that owns the line
10242 header, we don't need the line header anymore. */
10243 if (m_cu->line_header_die_owner == m_die)
10244 {
10245 delete m_cu->line_header;
10246 m_cu->line_header = NULL;
10247 m_cu->line_header_die_owner = NULL;
10248 }
10249 }
10250
10251private:
10252 die_info *m_die;
10253 dwarf2_cu *m_cu;
10254};
adde2bff 10255
c906108c
SS
10256/* Process a die and its children. */
10257
10258static void
e7c27a73 10259process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10260{
4c8aa72d 10261 process_die_scope scope (die, cu);
adde2bff 10262
c906108c
SS
10263 switch (die->tag)
10264 {
10265 case DW_TAG_padding:
10266 break;
10267 case DW_TAG_compile_unit:
95554aad 10268 case DW_TAG_partial_unit:
e7c27a73 10269 read_file_scope (die, cu);
c906108c 10270 break;
348e048f
DE
10271 case DW_TAG_type_unit:
10272 read_type_unit_scope (die, cu);
10273 break;
c906108c 10274 case DW_TAG_subprogram:
0a4b0913
AB
10275 /* Nested subprograms in Fortran get a prefix. */
10276 if (cu->language == language_fortran
10277 && die->parent != NULL
10278 && die->parent->tag == DW_TAG_subprogram)
10279 cu->processing_has_namespace_info = true;
10280 /* Fall through. */
c906108c 10281 case DW_TAG_inlined_subroutine:
edb3359d 10282 read_func_scope (die, cu);
c906108c
SS
10283 break;
10284 case DW_TAG_lexical_block:
14898363
L
10285 case DW_TAG_try_block:
10286 case DW_TAG_catch_block:
e7c27a73 10287 read_lexical_block_scope (die, cu);
c906108c 10288 break;
216f72a1 10289 case DW_TAG_call_site:
96408a79
SA
10290 case DW_TAG_GNU_call_site:
10291 read_call_site_scope (die, cu);
10292 break;
c906108c 10293 case DW_TAG_class_type:
680b30c7 10294 case DW_TAG_interface_type:
c906108c
SS
10295 case DW_TAG_structure_type:
10296 case DW_TAG_union_type:
134d01f1 10297 process_structure_scope (die, cu);
c906108c
SS
10298 break;
10299 case DW_TAG_enumeration_type:
134d01f1 10300 process_enumeration_scope (die, cu);
c906108c 10301 break;
134d01f1 10302
f792889a
DJ
10303 /* These dies have a type, but processing them does not create
10304 a symbol or recurse to process the children. Therefore we can
10305 read them on-demand through read_type_die. */
c906108c 10306 case DW_TAG_subroutine_type:
72019c9c 10307 case DW_TAG_set_type:
c906108c 10308 case DW_TAG_pointer_type:
c906108c 10309 case DW_TAG_ptr_to_member_type:
c906108c 10310 case DW_TAG_reference_type:
4297a3f0 10311 case DW_TAG_rvalue_reference_type:
c906108c 10312 case DW_TAG_string_type:
c906108c 10313 break;
134d01f1 10314
d8f62e84
TT
10315 case DW_TAG_array_type:
10316 /* We only need to handle this case for Ada -- in other
10317 languages, it's normal for the compiler to emit a typedef
10318 instead. */
10319 if (cu->language != language_ada)
10320 break;
10321 /* FALLTHROUGH */
c906108c 10322 case DW_TAG_base_type:
a02abb62 10323 case DW_TAG_subrange_type:
cb249c71 10324 case DW_TAG_typedef:
134d01f1 10325 /* Add a typedef symbol for the type definition, if it has a
dda83cd7 10326 DW_AT_name. */
f792889a 10327 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10328 break;
c906108c 10329 case DW_TAG_common_block:
e7c27a73 10330 read_common_block (die, cu);
c906108c
SS
10331 break;
10332 case DW_TAG_common_inclusion:
10333 break;
d9fa45fe 10334 case DW_TAG_namespace:
9068261f 10335 cu->processing_has_namespace_info = true;
e7c27a73 10336 read_namespace (die, cu);
d9fa45fe 10337 break;
5d7cb8df 10338 case DW_TAG_module:
9068261f 10339 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10340 read_module (die, cu);
10341 break;
d9fa45fe 10342 case DW_TAG_imported_declaration:
9068261f 10343 cu->processing_has_namespace_info = true;
74921315
KS
10344 if (read_namespace_alias (die, cu))
10345 break;
86a73007
TT
10346 /* The declaration is not a global namespace alias. */
10347 /* Fall through. */
d9fa45fe 10348 case DW_TAG_imported_module:
9068261f 10349 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10350 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10351 || cu->language != language_fortran))
b98664d3 10352 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10353 dwarf_tag_name (die->tag));
10354 read_import_statement (die, cu);
d9fa45fe 10355 break;
95554aad
TT
10356
10357 case DW_TAG_imported_unit:
10358 process_imported_unit_die (die, cu);
10359 break;
10360
71a3c369
TT
10361 case DW_TAG_variable:
10362 read_variable (die, cu);
10363 break;
10364
c906108c 10365 default:
e7c27a73 10366 new_symbol (die, NULL, cu);
c906108c
SS
10367 break;
10368 }
10369}
ca69b9e6
DE
10370\f
10371/* DWARF name computation. */
c906108c 10372
94af9270
KS
10373/* A helper function for dwarf2_compute_name which determines whether DIE
10374 needs to have the name of the scope prepended to the name listed in the
10375 die. */
10376
10377static int
10378die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10379{
1c809c68
TT
10380 struct attribute *attr;
10381
94af9270
KS
10382 switch (die->tag)
10383 {
10384 case DW_TAG_namespace:
10385 case DW_TAG_typedef:
10386 case DW_TAG_class_type:
10387 case DW_TAG_interface_type:
10388 case DW_TAG_structure_type:
10389 case DW_TAG_union_type:
10390 case DW_TAG_enumeration_type:
10391 case DW_TAG_enumerator:
10392 case DW_TAG_subprogram:
08a76f8a 10393 case DW_TAG_inlined_subroutine:
94af9270 10394 case DW_TAG_member:
74921315 10395 case DW_TAG_imported_declaration:
94af9270
KS
10396 return 1;
10397
10398 case DW_TAG_variable:
c2b0a229 10399 case DW_TAG_constant:
94af9270
KS
10400 /* We only need to prefix "globally" visible variables. These include
10401 any variable marked with DW_AT_external or any variable that
10402 lives in a namespace. [Variables in anonymous namespaces
10403 require prefixing, but they are not DW_AT_external.] */
10404
10405 if (dwarf2_attr (die, DW_AT_specification, cu))
10406 {
10407 struct dwarf2_cu *spec_cu = cu;
9a619af0 10408
94af9270
KS
10409 return die_needs_namespace (die_specification (die, &spec_cu),
10410 spec_cu);
10411 }
10412
1c809c68 10413 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10414 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10415 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10416 return 0;
10417 /* A variable in a lexical block of some kind does not need a
10418 namespace, even though in C++ such variables may be external
10419 and have a mangled name. */
10420 if (die->parent->tag == DW_TAG_lexical_block
10421 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10422 || die->parent->tag == DW_TAG_catch_block
10423 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10424 return 0;
10425 return 1;
94af9270
KS
10426
10427 default:
10428 return 0;
10429 }
10430}
10431
73b9be8b
KS
10432/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10433 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10434 defined for the given DIE. */
10435
10436static struct attribute *
10437dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10438{
10439 struct attribute *attr;
10440
10441 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10442 if (attr == NULL)
10443 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10444
10445 return attr;
10446}
10447
10448/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10449 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10450 defined for the given DIE. */
10451
10452static const char *
10453dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10454{
10455 const char *linkage_name;
10456
10457 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10458 if (linkage_name == NULL)
10459 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10460
787de330
TT
10461 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10462 See https://github.com/rust-lang/rust/issues/32925. */
10463 if (cu->language == language_rust && linkage_name != NULL
10464 && strchr (linkage_name, '{') != NULL)
10465 linkage_name = NULL;
10466
73b9be8b
KS
10467 return linkage_name;
10468}
10469
94af9270 10470/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10471 compute the physname for the object, which include a method's:
9c37b5ae 10472 - formal parameters (C++),
a766d390 10473 - receiver type (Go),
a766d390
DE
10474
10475 The term "physname" is a bit confusing.
10476 For C++, for example, it is the demangled name.
10477 For Go, for example, it's the mangled name.
94af9270 10478
af6b7be1
JB
10479 For Ada, return the DIE's linkage name rather than the fully qualified
10480 name. PHYSNAME is ignored..
10481
5989a64e 10482 The result is allocated on the objfile->per_bfd's obstack and
45940949 10483 canonicalized. */
94af9270
KS
10484
10485static const char *
15d034d0
TT
10486dwarf2_compute_name (const char *name,
10487 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10488 int physname)
10489{
5e22e966 10490 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10491
94af9270
KS
10492 if (name == NULL)
10493 name = dwarf2_name (die, cu);
10494
2ee7123e
DE
10495 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10496 but otherwise compute it by typename_concat inside GDB.
10497 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10498 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10499 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10500 will set the demangled name to the result of dwarf2_full_name, and it is
10501 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10502 if (cu->language == language_ada
10503 || (cu->language == language_fortran && physname))
10504 {
10505 /* For Ada unit, we prefer the linkage name over the name, as
10506 the former contains the exported name, which the user expects
10507 to be able to reference. Ideally, we want the user to be able
10508 to reference this entity using either natural or linkage name,
10509 but we haven't started looking at this enhancement yet. */
73b9be8b 10510 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10511
2ee7123e
DE
10512 if (linkage_name != NULL)
10513 return linkage_name;
f55ee35c
JK
10514 }
10515
94af9270
KS
10516 /* These are the only languages we know how to qualify names in. */
10517 if (name != NULL
9c37b5ae 10518 && (cu->language == language_cplus
c44af4eb
TT
10519 || cu->language == language_fortran || cu->language == language_d
10520 || cu->language == language_rust))
94af9270
KS
10521 {
10522 if (die_needs_namespace (die, cu))
10523 {
0d5cff50 10524 const char *prefix;
34a68019 10525 const char *canonical_name = NULL;
94af9270 10526
d7e74731
PA
10527 string_file buf;
10528
94af9270 10529 prefix = determine_prefix (die, cu);
94af9270
KS
10530 if (*prefix != '\0')
10531 {
43816ebc
TT
10532 gdb::unique_xmalloc_ptr<char> prefixed_name
10533 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10534
43816ebc 10535 buf.puts (prefixed_name.get ());
94af9270
KS
10536 }
10537 else
d7e74731 10538 buf.puts (name);
94af9270 10539
98bfdba5
PA
10540 /* Template parameters may be specified in the DIE's DW_AT_name, or
10541 as children with DW_TAG_template_type_param or
10542 DW_TAG_value_type_param. If the latter, add them to the name
10543 here. If the name already has template parameters, then
10544 skip this step; some versions of GCC emit both, and
10545 it is more efficient to use the pre-computed name.
10546
10547 Something to keep in mind about this process: it is very
10548 unlikely, or in some cases downright impossible, to produce
10549 something that will match the mangled name of a function.
10550 If the definition of the function has the same debug info,
10551 we should be able to match up with it anyway. But fallbacks
10552 using the minimal symbol, for instance to find a method
10553 implemented in a stripped copy of libstdc++, will not work.
10554 If we do not have debug info for the definition, we will have to
10555 match them up some other way.
10556
10557 When we do name matching there is a related problem with function
10558 templates; two instantiated function templates are allowed to
10559 differ only by their return types, which we do not add here. */
10560
10561 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10562 {
10563 struct attribute *attr;
10564 struct die_info *child;
10565 int first = 1;
2c75ccb2 10566 const language_defn *cplus_lang = language_def (cu->language);
98bfdba5
PA
10567
10568 die->building_fullname = 1;
10569
10570 for (child = die->child; child != NULL; child = child->sibling)
10571 {
10572 struct type *type;
12df843f 10573 LONGEST value;
d521ce57 10574 const gdb_byte *bytes;
98bfdba5
PA
10575 struct dwarf2_locexpr_baton *baton;
10576 struct value *v;
10577
10578 if (child->tag != DW_TAG_template_type_param
10579 && child->tag != DW_TAG_template_value_param)
10580 continue;
10581
10582 if (first)
10583 {
d7e74731 10584 buf.puts ("<");
98bfdba5
PA
10585 first = 0;
10586 }
10587 else
d7e74731 10588 buf.puts (", ");
98bfdba5
PA
10589
10590 attr = dwarf2_attr (child, DW_AT_type, cu);
10591 if (attr == NULL)
10592 {
b98664d3 10593 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10594 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10595 continue;
10596 }
10597 type = die_type (child, cu);
10598
10599 if (child->tag == DW_TAG_template_type_param)
10600 {
2c75ccb2
AB
10601 cplus_lang->print_type (type, "", &buf, -1, 0,
10602 &type_print_raw_options);
98bfdba5
PA
10603 continue;
10604 }
10605
10606 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10607 if (attr == NULL)
10608 {
b98664d3 10609 complaint (_("template parameter missing "
3e43a32a 10610 "DW_AT_const_value"));
d7e74731 10611 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10612 continue;
10613 }
10614
10615 dwarf2_const_value_attr (attr, type, name,
10616 &cu->comp_unit_obstack, cu,
10617 &value, &bytes, &baton);
10618
20ce4123 10619 if (type->has_no_signedness ())
98bfdba5
PA
10620 /* GDB prints characters as NUMBER 'CHAR'. If that's
10621 changed, this can use value_print instead. */
2c75ccb2 10622 cplus_lang->printchar (value, type, &buf);
98bfdba5
PA
10623 else
10624 {
10625 struct value_print_options opts;
10626
10627 if (baton != NULL)
10628 v = dwarf2_evaluate_loc_desc (type, NULL,
10629 baton->data,
10630 baton->size,
9f47c707
SM
10631 baton->per_cu,
10632 baton->per_objfile);
98bfdba5
PA
10633 else if (bytes != NULL)
10634 {
10635 v = allocate_value (type);
10636 memcpy (value_contents_writeable (v), bytes,
10637 TYPE_LENGTH (type));
10638 }
10639 else
10640 v = value_from_longest (type, value);
10641
3e43a32a
MS
10642 /* Specify decimal so that we do not depend on
10643 the radix. */
98bfdba5
PA
10644 get_formatted_print_options (&opts, 'd');
10645 opts.raw = 1;
d7e74731 10646 value_print (v, &buf, &opts);
98bfdba5 10647 release_value (v);
98bfdba5
PA
10648 }
10649 }
10650
10651 die->building_fullname = 0;
10652
10653 if (!first)
10654 {
10655 /* Close the argument list, with a space if necessary
10656 (nested templates). */
d7e74731
PA
10657 if (!buf.empty () && buf.string ().back () == '>')
10658 buf.puts (" >");
98bfdba5 10659 else
d7e74731 10660 buf.puts (">");
98bfdba5
PA
10661 }
10662 }
10663
9c37b5ae 10664 /* For C++ methods, append formal parameter type
94af9270 10665 information, if PHYSNAME. */
6e70227d 10666
94af9270 10667 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10668 && cu->language == language_cplus)
94af9270
KS
10669 {
10670 struct type *type = read_type_die (die, cu);
10671
d7e74731 10672 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10673 &type_print_raw_options);
94af9270 10674
9c37b5ae 10675 if (cu->language == language_cplus)
94af9270 10676 {
60430eff
DJ
10677 /* Assume that an artificial first parameter is
10678 "this", but do not crash if it is not. RealView
10679 marks unnamed (and thus unused) parameters as
10680 artificial; there is no way to differentiate
10681 the two cases. */
1f704f76 10682 if (type->num_fields () > 0
94af9270 10683 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
10684 && type->field (0).type ()->code () == TYPE_CODE_PTR
10685 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 10686 buf.puts (" const");
94af9270
KS
10687 }
10688 }
10689
d7e74731 10690 const std::string &intermediate_name = buf.string ();
94af9270
KS
10691
10692 if (cu->language == language_cplus)
34a68019 10693 canonical_name
322a8516 10694 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10695 objfile);
34a68019
TT
10696
10697 /* If we only computed INTERMEDIATE_NAME, or if
10698 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10699 intern it. */
322a8516 10700 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10701 name = objfile->intern (intermediate_name);
34a68019
TT
10702 else
10703 name = canonical_name;
94af9270
KS
10704 }
10705 }
10706
10707 return name;
10708}
10709
0114d602
DJ
10710/* Return the fully qualified name of DIE, based on its DW_AT_name.
10711 If scope qualifiers are appropriate they will be added. The result
34a68019 10712 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10713 not have a name. NAME may either be from a previous call to
10714 dwarf2_name or NULL.
10715
9c37b5ae 10716 The output string will be canonicalized (if C++). */
0114d602
DJ
10717
10718static const char *
15d034d0 10719dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10720{
94af9270
KS
10721 return dwarf2_compute_name (name, die, cu, 0);
10722}
0114d602 10723
94af9270
KS
10724/* Construct a physname for the given DIE in CU. NAME may either be
10725 from a previous call to dwarf2_name or NULL. The result will be
10726 allocated on the objfile_objstack or NULL if the DIE does not have a
10727 name.
0114d602 10728
9c37b5ae 10729 The output string will be canonicalized (if C++). */
0114d602 10730
94af9270 10731static const char *
15d034d0 10732dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10733{
5e22e966 10734 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10735 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10736 int need_copy = 1;
10737
10738 /* In this case dwarf2_compute_name is just a shortcut not building anything
10739 on its own. */
10740 if (!die_needs_namespace (die, cu))
10741 return dwarf2_compute_name (name, die, cu, 1);
10742
906bb4c5
TT
10743 if (cu->language != language_rust)
10744 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10745
10746 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10747 has computed. */
791afaa2 10748 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10749 if (mangled != NULL)
900e11f9 10750 {
900e11f9 10751
d3355e4d 10752 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
59cc4834
JB
10753 {
10754 /* Do nothing (do not demangle the symbol name). */
10755 }
a766d390
DE
10756 else
10757 {
0eb876f5
JB
10758 /* Use DMGL_RET_DROP for C++ template functions to suppress
10759 their return type. It is easier for GDB users to search
10760 for such functions as `name(params)' than `long name(params)'.
10761 In such case the minimal symbol names do not match the full
10762 symbol names but for template functions there is never a need
10763 to look up their definition from their declaration so
10764 the only disadvantage remains the minimal symbol variant
10765 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10766 demangled.reset (gdb_demangle (mangled,
10767 (DMGL_PARAMS | DMGL_ANSI
10768 | DMGL_RET_DROP)));
a766d390 10769 }
900e11f9 10770 if (demangled)
791afaa2 10771 canon = demangled.get ();
900e11f9
JK
10772 else
10773 {
10774 canon = mangled;
10775 need_copy = 0;
10776 }
10777 }
10778
10779 if (canon == NULL || check_physname)
10780 {
10781 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10782
10783 if (canon != NULL && strcmp (physname, canon) != 0)
10784 {
10785 /* It may not mean a bug in GDB. The compiler could also
10786 compute DW_AT_linkage_name incorrectly. But in such case
10787 GDB would need to be bug-to-bug compatible. */
10788
b98664d3 10789 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10790 "(from linkage <%s>) - DIE at %s [in module %s]"),
10791 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10792 objfile_name (objfile));
900e11f9
JK
10793
10794 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10795 is available here - over computed PHYSNAME. It is safer
10796 against both buggy GDB and buggy compilers. */
10797
10798 retval = canon;
10799 }
10800 else
10801 {
10802 retval = physname;
10803 need_copy = 0;
10804 }
10805 }
10806 else
10807 retval = canon;
10808
10809 if (need_copy)
be1e3d3e 10810 retval = objfile->intern (retval);
900e11f9 10811
900e11f9 10812 return retval;
0114d602
DJ
10813}
10814
74921315
KS
10815/* Inspect DIE in CU for a namespace alias. If one exists, record
10816 a new symbol for it.
10817
10818 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10819
10820static int
10821read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10822{
10823 struct attribute *attr;
10824
10825 /* If the die does not have a name, this is not a namespace
10826 alias. */
10827 attr = dwarf2_attr (die, DW_AT_name, cu);
10828 if (attr != NULL)
10829 {
10830 int num;
10831 struct die_info *d = die;
10832 struct dwarf2_cu *imported_cu = cu;
10833
10834 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10835 keep inspecting DIEs until we hit the underlying import. */
10836#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10837 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10838 {
10839 attr = dwarf2_attr (d, DW_AT_import, cu);
10840 if (attr == NULL)
10841 break;
10842
10843 d = follow_die_ref (d, attr, &imported_cu);
10844 if (d->tag != DW_TAG_imported_declaration)
10845 break;
10846 }
10847
10848 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10849 {
b98664d3 10850 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10851 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10852 return 0;
10853 }
10854
10855 if (attr != NULL)
10856 {
10857 struct type *type;
0826b30a 10858 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10859
aa66c379 10860 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10861 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10862 {
10863 /* This declaration is a global namespace alias. Add
10864 a symbol for it whose type is the aliased namespace. */
10865 new_symbol (die, type, cu);
10866 return 1;
10867 }
10868 }
10869 }
10870
10871 return 0;
10872}
10873
22cee43f 10874/* Return the using directives repository (global or local?) to use in the
804d2729 10875 current context for CU.
22cee43f
PMR
10876
10877 For Ada, imported declarations can materialize renamings, which *may* be
10878 global. However it is impossible (for now?) in DWARF to distinguish
10879 "external" imported declarations and "static" ones. As all imported
10880 declarations seem to be static in all other languages, make them all CU-wide
10881 global only in Ada. */
10882
10883static struct using_direct **
804d2729 10884using_directives (struct dwarf2_cu *cu)
22cee43f 10885{
c24bdb02
KS
10886 if (cu->language == language_ada
10887 && cu->get_builder ()->outermost_context_p ())
10888 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10889 else
c24bdb02 10890 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10891}
10892
27aa8d6a
SW
10893/* Read the import statement specified by the given die and record it. */
10894
10895static void
10896read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10897{
5e22e966 10898 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10899 struct attribute *import_attr;
32019081 10900 struct die_info *imported_die, *child_die;
de4affc9 10901 struct dwarf2_cu *imported_cu;
27aa8d6a 10902 const char *imported_name;
794684b6 10903 const char *imported_name_prefix;
13387711
SW
10904 const char *canonical_name;
10905 const char *import_alias;
10906 const char *imported_declaration = NULL;
794684b6 10907 const char *import_prefix;
eb1e02fd 10908 std::vector<const char *> excludes;
13387711 10909
27aa8d6a
SW
10910 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10911 if (import_attr == NULL)
10912 {
b98664d3 10913 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10914 dwarf_tag_name (die->tag));
10915 return;
10916 }
10917
de4affc9
CC
10918 imported_cu = cu;
10919 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10920 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10921 if (imported_name == NULL)
10922 {
10923 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10924
dda83cd7
SM
10925 The import in the following code:
10926 namespace A
10927 {
10928 typedef int B;
10929 }
10930
10931 int main ()
10932 {
10933 using A::B;
10934 B b;
10935 return b;
10936 }
10937
10938 ...
10939 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10940 <52> DW_AT_decl_file : 1
10941 <53> DW_AT_decl_line : 6
10942 <54> DW_AT_import : <0x75>
10943 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10944 <59> DW_AT_name : B
10945 <5b> DW_AT_decl_file : 1
10946 <5c> DW_AT_decl_line : 2
10947 <5d> DW_AT_type : <0x6e>
10948 ...
10949 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10950 <76> DW_AT_byte_size : 4
10951 <77> DW_AT_encoding : 5 (signed)
10952
10953 imports the wrong die ( 0x75 instead of 0x58 ).
10954 This case will be ignored until the gcc bug is fixed. */
27aa8d6a
SW
10955 return;
10956 }
10957
82856980
SW
10958 /* Figure out the local name after import. */
10959 import_alias = dwarf2_name (die, cu);
27aa8d6a 10960
794684b6
SW
10961 /* Figure out where the statement is being imported to. */
10962 import_prefix = determine_prefix (die, cu);
10963
10964 /* Figure out what the scope of the imported die is and prepend it
10965 to the name of the imported die. */
de4affc9 10966 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10967
f55ee35c
JK
10968 if (imported_die->tag != DW_TAG_namespace
10969 && imported_die->tag != DW_TAG_module)
794684b6 10970 {
13387711
SW
10971 imported_declaration = imported_name;
10972 canonical_name = imported_name_prefix;
794684b6 10973 }
13387711 10974 else if (strlen (imported_name_prefix) > 0)
12aaed36 10975 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10976 imported_name_prefix,
10977 (cu->language == language_d ? "." : "::"),
10978 imported_name, (char *) NULL);
13387711
SW
10979 else
10980 canonical_name = imported_name;
794684b6 10981
32019081
JK
10982 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10983 for (child_die = die->child; child_die && child_die->tag;
436c571c 10984 child_die = child_die->sibling)
32019081
JK
10985 {
10986 /* DWARF-4: A Fortran use statement with a “rename list” may be
10987 represented by an imported module entry with an import attribute
10988 referring to the module and owned entries corresponding to those
10989 entities that are renamed as part of being imported. */
10990
10991 if (child_die->tag != DW_TAG_imported_declaration)
10992 {
b98664d3 10993 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10994 "- DIE at %s [in module %s]"),
10995 sect_offset_str (child_die->sect_off),
10996 objfile_name (objfile));
32019081
JK
10997 continue;
10998 }
10999
11000 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11001 if (import_attr == NULL)
11002 {
b98664d3 11003 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11004 dwarf_tag_name (child_die->tag));
11005 continue;
11006 }
11007
11008 imported_cu = cu;
11009 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11010 &imported_cu);
11011 imported_name = dwarf2_name (imported_die, imported_cu);
11012 if (imported_name == NULL)
11013 {
b98664d3 11014 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11015 "imported name - DIE at %s [in module %s]"),
11016 sect_offset_str (child_die->sect_off),
11017 objfile_name (objfile));
32019081
JK
11018 continue;
11019 }
11020
eb1e02fd 11021 excludes.push_back (imported_name);
32019081
JK
11022
11023 process_die (child_die, cu);
11024 }
11025
804d2729 11026 add_using_directive (using_directives (cu),
22cee43f
PMR
11027 import_prefix,
11028 canonical_name,
11029 import_alias,
11030 imported_declaration,
11031 excludes,
11032 0,
11033 &objfile->objfile_obstack);
27aa8d6a
SW
11034}
11035
5230b05a
WT
11036/* ICC<14 does not output the required DW_AT_declaration on incomplete
11037 types, but gives them a size of zero. Starting with version 14,
11038 ICC is compatible with GCC. */
11039
9068261f 11040static bool
5230b05a
WT
11041producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11042{
11043 if (!cu->checked_producer)
11044 check_producer (cu);
11045
11046 return cu->producer_is_icc_lt_14;
11047}
11048
eb77c9df
AB
11049/* ICC generates a DW_AT_type for C void functions. This was observed on
11050 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11051 which says that void functions should not have a DW_AT_type. */
11052
11053static bool
11054producer_is_icc (struct dwarf2_cu *cu)
11055{
11056 if (!cu->checked_producer)
11057 check_producer (cu);
11058
11059 return cu->producer_is_icc;
11060}
11061
1b80a9fa
JK
11062/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11063 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11064 this, it was first present in GCC release 4.3.0. */
11065
9068261f 11066static bool
1b80a9fa
JK
11067producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11068{
11069 if (!cu->checked_producer)
11070 check_producer (cu);
11071
11072 return cu->producer_is_gcc_lt_4_3;
11073}
11074
d721ba37
PA
11075static file_and_directory
11076find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11077{
d721ba37
PA
11078 file_and_directory res;
11079
9291a0cd
TT
11080 /* Find the filename. Do not use dwarf2_name here, since the filename
11081 is not a source language identifier. */
d721ba37
PA
11082 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11083 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11084
d721ba37
PA
11085 if (res.comp_dir == NULL
11086 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11087 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11088 {
d721ba37
PA
11089 res.comp_dir_storage = ldirname (res.name);
11090 if (!res.comp_dir_storage.empty ())
11091 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11092 }
d721ba37 11093 if (res.comp_dir != NULL)
9291a0cd
TT
11094 {
11095 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11096 directory, get rid of it. */
d721ba37 11097 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11098
d721ba37
PA
11099 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11100 res.comp_dir = cp + 1;
9291a0cd
TT
11101 }
11102
d721ba37
PA
11103 if (res.name == NULL)
11104 res.name = "<unknown>";
11105
11106 return res;
9291a0cd
TT
11107}
11108
f4dc4d17
DE
11109/* Handle DW_AT_stmt_list for a compilation unit.
11110 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11111 COMP_DIR is the compilation directory. LOWPC is passed to
11112 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11113
11114static void
11115handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11116 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11117{
976ca316 11118 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 11119 struct attribute *attr;
527f3840
JK
11120 struct line_header line_header_local;
11121 hashval_t line_header_local_hash;
527f3840
JK
11122 void **slot;
11123 int decode_mapping;
2ab95328 11124
f4dc4d17
DE
11125 gdb_assert (! cu->per_cu->is_debug_types);
11126
2ab95328 11127 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e 11128 if (attr == NULL || !attr->form_is_unsigned ())
527f3840
JK
11129 return;
11130
d4df075e 11131 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
527f3840
JK
11132
11133 /* The line header hash table is only created if needed (it exists to
11134 prevent redundant reading of the line table for partial_units).
11135 If we're given a partial_unit, we'll need it. If we're given a
11136 compile_unit, then use the line header hash table if it's already
11137 created, but don't create one just yet. */
11138
976ca316 11139 if (per_objfile->line_header_hash == NULL
527f3840 11140 && die->tag == DW_TAG_partial_unit)
2ab95328 11141 {
976ca316 11142 per_objfile->line_header_hash
d15acc42
TT
11143 .reset (htab_create_alloc (127, line_header_hash_voidp,
11144 line_header_eq_voidp,
11145 free_line_header_voidp,
11146 xcalloc, xfree));
527f3840 11147 }
2ab95328 11148
9c541725 11149 line_header_local.sect_off = line_offset;
527f3840
JK
11150 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11151 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 11152 if (per_objfile->line_header_hash != NULL)
527f3840 11153 {
976ca316 11154 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11155 &line_header_local,
11156 line_header_local_hash, NO_INSERT);
11157
11158 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11159 is not present in *SLOT (since if there is something in *SLOT then
11160 it will be for a partial_unit). */
11161 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11162 {
527f3840 11163 gdb_assert (*slot != NULL);
9a3c8263 11164 cu->line_header = (struct line_header *) *slot;
527f3840 11165 return;
dee91e82 11166 }
2ab95328 11167 }
527f3840
JK
11168
11169 /* dwarf_decode_line_header does not yet provide sufficient information.
11170 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11171 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11172 if (lh == NULL)
527f3840 11173 return;
4c8aa72d
PA
11174
11175 cu->line_header = lh.release ();
11176 cu->line_header_die_owner = die;
527f3840 11177
976ca316 11178 if (per_objfile->line_header_hash == NULL)
527f3840
JK
11179 slot = NULL;
11180 else
11181 {
976ca316 11182 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11183 &line_header_local,
11184 line_header_local_hash, INSERT);
11185 gdb_assert (slot != NULL);
11186 }
11187 if (slot != NULL && *slot == NULL)
11188 {
11189 /* This newly decoded line number information unit will be owned
11190 by line_header_hash hash table. */
11191 *slot = cu->line_header;
4c8aa72d 11192 cu->line_header_die_owner = NULL;
527f3840
JK
11193 }
11194 else
11195 {
11196 /* We cannot free any current entry in (*slot) as that struct line_header
dda83cd7 11197 may be already used by multiple CUs. Create only temporary decoded
527f3840
JK
11198 line_header for this CU - it may happen at most once for each line
11199 number information unit. And if we're not using line_header_hash
11200 then this is what we want as well. */
11201 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11202 }
11203 decode_mapping = (die->tag != DW_TAG_partial_unit);
11204 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11205 decode_mapping);
fff8551c 11206
2ab95328
TT
11207}
11208
95554aad 11209/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11210
c906108c 11211static void
e7c27a73 11212read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11213{
976ca316
SM
11214 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11215 struct objfile *objfile = per_objfile->objfile;
08feed99 11216 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 11217 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11218 CORE_ADDR highpc = ((CORE_ADDR) 0);
11219 struct attribute *attr;
c906108c 11220 struct die_info *child_die;
e142c38c 11221 CORE_ADDR baseaddr;
6e70227d 11222
380618d6 11223 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11224 baseaddr = objfile->text_section_offset ();
c906108c 11225
fae299cd 11226 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11227
11228 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11229 from finish_block. */
2acceee2 11230 if (lowpc == ((CORE_ADDR) -1))
c906108c 11231 lowpc = highpc;
3e29f34a 11232 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11233
d721ba37 11234 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11235
f4b8a18d
KW
11236 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11237 standardised yet. As a workaround for the language detection we fall
11238 back to the DW_AT_producer string. */
11239 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11240 cu->language = language_opencl;
11241
3019eac3
DE
11242 /* Similar hack for Go. */
11243 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11244 set_cu_language (DW_LANG_Go, cu);
11245
c24bdb02 11246 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11247
11248 /* Decode line number information if present. We do this before
11249 processing child DIEs, so that the line header table is available
11250 for DW_AT_decl_file. */
d721ba37 11251 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11252
11253 /* Process all dies in compilation unit. */
11254 if (die->child != NULL)
11255 {
11256 child_die = die->child;
11257 while (child_die && child_die->tag)
11258 {
11259 process_die (child_die, cu);
436c571c 11260 child_die = child_die->sibling;
3019eac3
DE
11261 }
11262 }
11263
11264 /* Decode macro information, if present. Dwarf 2 macro information
11265 refers to information in the line number info statement program
11266 header, so we can only read it if we've read the header
11267 successfully. */
0af92d60
JK
11268 attr = dwarf2_attr (die, DW_AT_macros, cu);
11269 if (attr == NULL)
11270 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
529908cb 11271 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3
DE
11272 {
11273 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11274 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11275
529908cb 11276 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
3019eac3
DE
11277 }
11278 else
11279 {
11280 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
529908cb 11281 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3 11282 {
529908cb 11283 unsigned int macro_offset = attr->as_unsigned ();
3019eac3 11284
43f3e411 11285 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11286 }
11287 }
3019eac3
DE
11288}
11289
c24bdb02
KS
11290void
11291dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11292{
f4dc4d17
DE
11293 struct type_unit_group *tu_group;
11294 int first_time;
3019eac3 11295 struct attribute *attr;
9c541725 11296 unsigned int i;
0186c6a7 11297 struct signatured_type *sig_type;
3019eac3 11298
f4dc4d17 11299 gdb_assert (per_cu->is_debug_types);
0186c6a7 11300 sig_type = (struct signatured_type *) per_cu;
3019eac3 11301
c24bdb02 11302 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11303
f4dc4d17 11304 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11305 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11306 if (sig_type->type_unit_group == NULL)
c24bdb02 11307 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11308 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11309
11310 /* If we've already processed this stmt_list there's no real need to
11311 do it again, we could fake it and just recreate the part we need
11312 (file name,index -> symtab mapping). If data shows this optimization
11313 is useful we can do it then. */
8adb8487
TT
11314 type_unit_group_unshareable *tug_unshare
11315 = per_objfile->get_type_unit_group_unshareable (tu_group);
11316 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11317
11318 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11319 debug info. */
fff8551c 11320 line_header_up lh;
d4df075e 11321 if (attr != NULL && attr->form_is_unsigned ())
3019eac3 11322 {
d4df075e 11323 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
c24bdb02 11324 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11325 }
11326 if (lh == NULL)
11327 {
11328 if (first_time)
c24bdb02 11329 start_symtab ("", NULL, 0);
f4dc4d17
DE
11330 else
11331 {
8adb8487 11332 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11333 gdb_assert (m_builder == nullptr);
8adb8487 11334 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11335 m_builder.reset (new struct buildsym_compunit
11336 (COMPUNIT_OBJFILE (cust), "",
11337 COMPUNIT_DIRNAME (cust),
11338 compunit_language (cust),
11339 0, cust));
770479f2 11340 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11341 }
f4dc4d17 11342 return;
3019eac3
DE
11343 }
11344
c24bdb02
KS
11345 line_header = lh.release ();
11346 line_header_die_owner = die;
3019eac3 11347
f4dc4d17
DE
11348 if (first_time)
11349 {
c24bdb02 11350 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11351
1fd60fc0
DE
11352 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11353 still initializing it, and our caller (a few levels up)
11354 process_full_type_unit still needs to know if this is the first
11355 time. */
11356
8adb8487 11357 tug_unshare->symtabs
4ac93832
TT
11358 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11359 struct symtab *, line_header->file_names_size ());
3019eac3 11360
7ba99d21
AT
11361 auto &file_names = line_header->file_names ();
11362 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11363 {
7ba99d21 11364 file_entry &fe = file_names[i];
c24bdb02
KS
11365 dwarf2_start_subfile (this, fe.name,
11366 fe.include_dir (line_header));
11367 buildsym_compunit *b = get_builder ();
11368 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11369 {
4c8aa72d
PA
11370 /* NOTE: start_subfile will recognize when it's been
11371 passed a file it has already seen. So we can't
11372 assume there's a simple mapping from
11373 cu->line_header->file_names to subfiles, plus
11374 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11375 b->get_current_subfile ()->symtab
11376 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11377 }
11378
c24bdb02 11379 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11380 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11381 }
11382 }
11383 else
3019eac3 11384 {
c24bdb02 11385 gdb_assert (m_builder == nullptr);
8adb8487 11386 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11387 m_builder.reset (new struct buildsym_compunit
11388 (COMPUNIT_OBJFILE (cust), "",
11389 COMPUNIT_DIRNAME (cust),
11390 compunit_language (cust),
11391 0, cust));
770479f2 11392 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11393
7ba99d21
AT
11394 auto &file_names = line_header->file_names ();
11395 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11396 {
7ba99d21 11397 file_entry &fe = file_names[i];
8adb8487 11398 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11399 }
3019eac3
DE
11400 }
11401
f4dc4d17
DE
11402 /* The main symtab is allocated last. Type units don't have DW_AT_name
11403 so they don't have a "real" (so to speak) symtab anyway.
11404 There is later code that will assign the main symtab to all symbols
11405 that don't have one. We need to handle the case of a symbol with a
11406 missing symtab (DW_AT_decl_file) anyway. */
11407}
3019eac3 11408
f4dc4d17
DE
11409/* Process DW_TAG_type_unit.
11410 For TUs we want to skip the first top level sibling if it's not the
11411 actual type being defined by this TU. In this case the first top
11412 level sibling is there to provide context only. */
3019eac3 11413
f4dc4d17
DE
11414static void
11415read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11416{
11417 struct die_info *child_die;
3019eac3 11418
f4dc4d17
DE
11419 prepare_one_comp_unit (cu, die, language_minimal);
11420
11421 /* Initialize (or reinitialize) the machinery for building symtabs.
11422 We do this before processing child DIEs, so that the line header table
11423 is available for DW_AT_decl_file. */
c24bdb02 11424 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11425
11426 if (die->child != NULL)
11427 {
11428 child_die = die->child;
11429 while (child_die && child_die->tag)
11430 {
11431 process_die (child_die, cu);
436c571c 11432 child_die = child_die->sibling;
f4dc4d17
DE
11433 }
11434 }
3019eac3
DE
11435}
11436\f
80626a55
DE
11437/* DWO/DWP files.
11438
11439 http://gcc.gnu.org/wiki/DebugFission
11440 http://gcc.gnu.org/wiki/DebugFissionDWP
11441
11442 To simplify handling of both DWO files ("object" files with the DWARF info)
11443 and DWP files (a file with the DWOs packaged up into one file), we treat
11444 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11445
11446static hashval_t
11447hash_dwo_file (const void *item)
11448{
9a3c8263 11449 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11450 hashval_t hash;
3019eac3 11451
a2ce51a0
DE
11452 hash = htab_hash_string (dwo_file->dwo_name);
11453 if (dwo_file->comp_dir != NULL)
11454 hash += htab_hash_string (dwo_file->comp_dir);
11455 return hash;
3019eac3
DE
11456}
11457
11458static int
11459eq_dwo_file (const void *item_lhs, const void *item_rhs)
11460{
9a3c8263
SM
11461 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11462 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11463
a2ce51a0
DE
11464 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11465 return 0;
11466 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11467 return lhs->comp_dir == rhs->comp_dir;
11468 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11469}
11470
11471/* Allocate a hash table for DWO files. */
11472
51ac9db5 11473static htab_up
298e9637 11474allocate_dwo_file_hash_table ()
3019eac3 11475{
51ac9db5
SM
11476 auto delete_dwo_file = [] (void *item)
11477 {
11478 struct dwo_file *dwo_file = (struct dwo_file *) item;
11479
11480 delete dwo_file;
11481 };
11482
bc68fb19
TT
11483 return htab_up (htab_create_alloc (41,
11484 hash_dwo_file,
11485 eq_dwo_file,
11486 delete_dwo_file,
11487 xcalloc, xfree));
3019eac3
DE
11488}
11489
80626a55
DE
11490/* Lookup DWO file DWO_NAME. */
11491
11492static void **
976ca316 11493lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11494 const char *dwo_name,
11495 const char *comp_dir)
80626a55
DE
11496{
11497 struct dwo_file find_entry;
11498 void **slot;
11499
976ca316
SM
11500 if (per_objfile->per_bfd->dwo_files == NULL)
11501 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11502
0ac5b59e
DE
11503 find_entry.dwo_name = dwo_name;
11504 find_entry.comp_dir = comp_dir;
976ca316 11505 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11506 INSERT);
80626a55
DE
11507
11508 return slot;
11509}
11510
3019eac3
DE
11511static hashval_t
11512hash_dwo_unit (const void *item)
11513{
9a3c8263 11514 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11515
11516 /* This drops the top 32 bits of the id, but is ok for a hash. */
11517 return dwo_unit->signature;
11518}
11519
11520static int
11521eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11522{
9a3c8263
SM
11523 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11524 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11525
11526 /* The signature is assumed to be unique within the DWO file.
11527 So while object file CU dwo_id's always have the value zero,
11528 that's OK, assuming each object file DWO file has only one CU,
11529 and that's the rule for now. */
11530 return lhs->signature == rhs->signature;
11531}
11532
11533/* Allocate a hash table for DWO CUs,TUs.
11534 There is one of these tables for each of CUs,TUs for each DWO file. */
11535
b0b6a987 11536static htab_up
298e9637 11537allocate_dwo_unit_table ()
3019eac3
DE
11538{
11539 /* Start out with a pretty small number.
11540 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11541 return htab_up (htab_create_alloc (3,
11542 hash_dwo_unit,
11543 eq_dwo_unit,
11544 NULL, xcalloc, xfree));
3019eac3
DE
11545}
11546
19c3d4c9 11547/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11548
11549static void
19c3d4c9
DE
11550create_dwo_cu_reader (const struct die_reader_specs *reader,
11551 const gdb_byte *info_ptr,
11552 struct die_info *comp_unit_die,
c0ab21c2
TT
11553 struct dwo_file *dwo_file,
11554 struct dwo_unit *dwo_unit)
3019eac3
DE
11555{
11556 struct dwarf2_cu *cu = reader->cu;
9c541725 11557 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11558 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11559
a084a2a6
AT
11560 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11561 if (!signature.has_value ())
3019eac3 11562 {
b98664d3 11563 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11564 " its dwo_id [in module %s]"),
9d8780f0 11565 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11566 return;
11567 }
11568
3019eac3 11569 dwo_unit->dwo_file = dwo_file;
a084a2a6 11570 dwo_unit->signature = *signature;
8a0459fd 11571 dwo_unit->section = section;
9c541725 11572 dwo_unit->sect_off = sect_off;
3019eac3
DE
11573 dwo_unit->length = cu->per_cu->length;
11574
6f738b01
SM
11575 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11576 sect_offset_str (sect_off),
11577 hex_string (dwo_unit->signature));
3019eac3
DE
11578}
11579
33c5cd75 11580/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11581 Note: This function processes DWO files only, not DWP files. */
3019eac3 11582
33c5cd75 11583static void
976ca316 11584create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11585 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11586 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11587{
976ca316
SM
11588 struct objfile *objfile = per_objfile->objfile;
11589 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11590 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11591
96b79293 11592 section.read (objfile);
33c5cd75 11593 info_ptr = section.buffer;
3019eac3
DE
11594
11595 if (info_ptr == NULL)
33c5cd75 11596 return;
3019eac3 11597
6f738b01
SM
11598 dwarf_read_debug_printf ("Reading %s for %s:",
11599 section.get_name (),
11600 section.get_file_name ());
3019eac3 11601
33c5cd75 11602 end_ptr = info_ptr + section.size;
3019eac3
DE
11603 while (info_ptr < end_ptr)
11604 {
11605 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11606 struct dwo_unit read_unit {};
33c5cd75
DB
11607 struct dwo_unit *dwo_unit;
11608 void **slot;
11609 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11610
11611 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11612 per_cu.per_bfd = per_bfd;
3019eac3 11613 per_cu.is_debug_types = 0;
33c5cd75
DB
11614 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11615 per_cu.section = &section;
11616
976ca316 11617 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11618 if (!reader.dummy_p)
11619 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11620 &dwo_file, &read_unit);
33c5cd75
DB
11621 info_ptr += per_cu.length;
11622
11623 // If the unit could not be parsed, skip it.
c0ab21c2 11624 if (read_unit.dwo_file == NULL)
33c5cd75 11625 continue;
3019eac3 11626
33c5cd75 11627 if (cus_htab == NULL)
298e9637 11628 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11629
1859c670 11630 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11631 struct dwo_unit);
c0ab21c2 11632 *dwo_unit = read_unit;
b0b6a987 11633 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11634 gdb_assert (slot != NULL);
11635 if (*slot != NULL)
19c3d4c9 11636 {
33c5cd75
DB
11637 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11638 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11639
b98664d3 11640 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11641 " the entry at offset %s, signature %s"),
11642 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11643 hex_string (dwo_unit->signature));
19c3d4c9 11644 }
33c5cd75 11645 *slot = (void *)dwo_unit;
3019eac3 11646 }
3019eac3
DE
11647}
11648
80626a55
DE
11649/* DWP file .debug_{cu,tu}_index section format:
11650 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
d2854d8d
CT
11651 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11652
11653 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11654 officially standard DWP format was published with DWARF v5 and is called
11655 Version 5. There are no versions 3 or 4.
80626a55 11656
d2415c6c
DE
11657 DWP Version 1:
11658
80626a55
DE
11659 Both index sections have the same format, and serve to map a 64-bit
11660 signature to a set of section numbers. Each section begins with a header,
11661 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11662 indexes, and a pool of 32-bit section numbers. The index sections will be
11663 aligned at 8-byte boundaries in the file.
11664
d2415c6c
DE
11665 The index section header consists of:
11666
11667 V, 32 bit version number
11668 -, 32 bits unused
11669 N, 32 bit number of compilation units or type units in the index
11670 M, 32 bit number of slots in the hash table
80626a55 11671
d2415c6c 11672 Numbers are recorded using the byte order of the application binary.
80626a55 11673
d2415c6c
DE
11674 The hash table begins at offset 16 in the section, and consists of an array
11675 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11676 order of the application binary). Unused slots in the hash table are 0.
11677 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11678
d2415c6c
DE
11679 The parallel table begins immediately after the hash table
11680 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11681 array of 32-bit indexes (using the byte order of the application binary),
11682 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11683 table contains a 32-bit index into the pool of section numbers. For unused
11684 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11685
73869dc2
DE
11686 The pool of section numbers begins immediately following the hash table
11687 (at offset 16 + 12 * M from the beginning of the section). The pool of
11688 section numbers consists of an array of 32-bit words (using the byte order
11689 of the application binary). Each item in the array is indexed starting
11690 from 0. The hash table entry provides the index of the first section
11691 number in the set. Additional section numbers in the set follow, and the
11692 set is terminated by a 0 entry (section number 0 is not used in ELF).
11693
11694 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11695 section must be the first entry in the set, and the .debug_abbrev.dwo must
11696 be the second entry. Other members of the set may follow in any order.
11697
11698 ---
11699
d2854d8d 11700 DWP Versions 2 and 5:
73869dc2 11701
d2854d8d 11702 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
73869dc2
DE
11703 and the entries in the index tables are now offsets into these sections.
11704 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11705 section.
11706
11707 Index Section Contents:
11708 Header
11709 Hash Table of Signatures dwp_hash_table.hash_table
11710 Parallel Table of Indices dwp_hash_table.unit_table
d2854d8d
CT
11711 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11712 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
73869dc2
DE
11713
11714 The index section header consists of:
11715
11716 V, 32 bit version number
11717 L, 32 bit number of columns in the table of section offsets
11718 N, 32 bit number of compilation units or type units in the index
11719 M, 32 bit number of slots in the hash table
11720
11721 Numbers are recorded using the byte order of the application binary.
11722
11723 The hash table has the same format as version 1.
11724 The parallel table of indices has the same format as version 1,
11725 except that the entries are origin-1 indices into the table of sections
11726 offsets and the table of section sizes.
11727
11728 The table of offsets begins immediately following the parallel table
11729 (at offset 16 + 12 * M from the beginning of the section). The table is
11730 a two-dimensional array of 32-bit words (using the byte order of the
11731 application binary), with L columns and N+1 rows, in row-major order.
11732 Each row in the array is indexed starting from 0. The first row provides
11733 a key to the remaining rows: each column in this row provides an identifier
11734 for a debug section, and the offsets in the same column of subsequent rows
d2854d8d 11735 refer to that section. The section identifiers for Version 2 are:
73869dc2
DE
11736
11737 DW_SECT_INFO 1 .debug_info.dwo
11738 DW_SECT_TYPES 2 .debug_types.dwo
11739 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11740 DW_SECT_LINE 4 .debug_line.dwo
11741 DW_SECT_LOC 5 .debug_loc.dwo
11742 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11743 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11744 DW_SECT_MACRO 8 .debug_macro.dwo
11745
d2854d8d
CT
11746 The section identifiers for Version 5 are:
11747
11748 DW_SECT_INFO_V5 1 .debug_info.dwo
11749 DW_SECT_RESERVED_V5 2 --
11750 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11751 DW_SECT_LINE_V5 4 .debug_line.dwo
11752 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11753 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11754 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11755 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11756
73869dc2
DE
11757 The offsets provided by the CU and TU index sections are the base offsets
11758 for the contributions made by each CU or TU to the corresponding section
11759 in the package file. Each CU and TU header contains an abbrev_offset
11760 field, used to find the abbreviations table for that CU or TU within the
11761 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11762 be interpreted as relative to the base offset given in the index section.
11763 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11764 should be interpreted as relative to the base offset for .debug_line.dwo,
11765 and offsets into other debug sections obtained from DWARF attributes should
11766 also be interpreted as relative to the corresponding base offset.
11767
11768 The table of sizes begins immediately following the table of offsets.
11769 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11770 with L columns and N rows, in row-major order. Each row in the array is
11771 indexed starting from 1 (row 0 is shared by the two tables).
11772
11773 ---
11774
11775 Hash table lookup is handled the same in version 1 and 2:
11776
11777 We assume that N and M will not exceed 2^32 - 1.
11778 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11779
d2415c6c
DE
11780 Given a 64-bit compilation unit signature or a type signature S, an entry
11781 in the hash table is located as follows:
80626a55 11782
d2415c6c
DE
11783 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11784 the low-order k bits all set to 1.
80626a55 11785
d2415c6c 11786 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11787
d2415c6c
DE
11788 3) If the hash table entry at index H matches the signature, use that
11789 entry. If the hash table entry at index H is unused (all zeroes),
11790 terminate the search: the signature is not present in the table.
80626a55 11791
d2415c6c 11792 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11793
d2415c6c 11794 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11795 to stop at an unused slot or find the match. */
80626a55
DE
11796
11797/* Create a hash table to map DWO IDs to their CU/TU entry in
11798 .debug_{info,types}.dwo in DWP_FILE.
11799 Returns NULL if there isn't one.
11800 Note: This function processes DWP files only, not DWO files. */
11801
11802static struct dwp_hash_table *
976ca316 11803create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11804 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11805{
976ca316 11806 struct objfile *objfile = per_objfile->objfile;
400174b1 11807 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11808 const gdb_byte *index_ptr, *index_end;
80626a55 11809 struct dwarf2_section_info *index;
73869dc2 11810 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11811 struct dwp_hash_table *htab;
11812
11813 if (is_debug_types)
11814 index = &dwp_file->sections.tu_index;
11815 else
11816 index = &dwp_file->sections.cu_index;
11817
96b79293 11818 if (index->empty ())
80626a55 11819 return NULL;
96b79293 11820 index->read (objfile);
80626a55
DE
11821
11822 index_ptr = index->buffer;
11823 index_end = index_ptr + index->size;
11824
d2854d8d
CT
11825 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11826 For now it's safe to just read 4 bytes (particularly as it's difficult to
11827 tell if you're dealing with Version 5 before you've read the version). */
80626a55 11828 version = read_4_bytes (dbfd, index_ptr);
73869dc2 11829 index_ptr += 4;
d2854d8d 11830 if (version == 2 || version == 5)
73869dc2
DE
11831 nr_columns = read_4_bytes (dbfd, index_ptr);
11832 else
11833 nr_columns = 0;
11834 index_ptr += 4;
80626a55
DE
11835 nr_units = read_4_bytes (dbfd, index_ptr);
11836 index_ptr += 4;
11837 nr_slots = read_4_bytes (dbfd, index_ptr);
11838 index_ptr += 4;
11839
d2854d8d 11840 if (version != 1 && version != 2 && version != 5)
80626a55 11841 {
21aa081e 11842 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11843 " [in module %s]"),
21aa081e 11844 pulongest (version), dwp_file->name);
80626a55
DE
11845 }
11846 if (nr_slots != (nr_slots & -nr_slots))
11847 {
21aa081e 11848 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11849 " is not power of 2 [in module %s]"),
21aa081e 11850 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11851 }
11852
976ca316 11853 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11854 htab->version = version;
11855 htab->nr_columns = nr_columns;
80626a55
DE
11856 htab->nr_units = nr_units;
11857 htab->nr_slots = nr_slots;
11858 htab->hash_table = index_ptr;
11859 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11860
11861 /* Exit early if the table is empty. */
11862 if (nr_slots == 0 || nr_units == 0
d2854d8d
CT
11863 || (version == 2 && nr_columns == 0)
11864 || (version == 5 && nr_columns == 0))
73869dc2
DE
11865 {
11866 /* All must be zero. */
11867 if (nr_slots != 0 || nr_units != 0
d2854d8d
CT
11868 || (version == 2 && nr_columns != 0)
11869 || (version == 5 && nr_columns != 0))
73869dc2 11870 {
b98664d3 11871 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11872 " all zero [in modules %s]"),
11873 dwp_file->name);
11874 }
11875 return htab;
11876 }
11877
11878 if (version == 1)
11879 {
11880 htab->section_pool.v1.indices =
11881 htab->unit_table + sizeof (uint32_t) * nr_slots;
11882 /* It's harder to decide whether the section is too small in v1.
11883 V1 is deprecated anyway so we punt. */
11884 }
d2854d8d 11885 else if (version == 2)
73869dc2
DE
11886 {
11887 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11888 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11889 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11890 /* Reverse map for error checking. */
11891 int ids_seen[DW_SECT_MAX + 1];
11892 int i;
11893
11894 if (nr_columns < 2)
11895 {
11896 error (_("Dwarf Error: bad DWP hash table, too few columns"
11897 " in section table [in module %s]"),
11898 dwp_file->name);
11899 }
11900 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11901 {
11902 error (_("Dwarf Error: bad DWP hash table, too many columns"
11903 " in section table [in module %s]"),
11904 dwp_file->name);
11905 }
04fd5eed
GB
11906 memset (ids, 255, sizeof_ids);
11907 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11908 for (i = 0; i < nr_columns; ++i)
11909 {
11910 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11911
11912 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11913 {
11914 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11915 " in section table [in module %s]"),
11916 id, dwp_file->name);
11917 }
11918 if (ids_seen[id] != -1)
11919 {
11920 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11921 " id %d in section table [in module %s]"),
11922 id, dwp_file->name);
11923 }
11924 ids_seen[id] = i;
11925 ids[i] = id;
11926 }
11927 /* Must have exactly one info or types section. */
11928 if (((ids_seen[DW_SECT_INFO] != -1)
11929 + (ids_seen[DW_SECT_TYPES] != -1))
11930 != 1)
11931 {
11932 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11933 " DWO info/types section [in module %s]"),
11934 dwp_file->name);
11935 }
11936 /* Must have an abbrev section. */
11937 if (ids_seen[DW_SECT_ABBREV] == -1)
11938 {
11939 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11940 " section [in module %s]"),
11941 dwp_file->name);
11942 }
11943 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11944 htab->section_pool.v2.sizes =
11945 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11946 * nr_units * nr_columns);
11947 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11948 * nr_units * nr_columns))
11949 > index_end)
11950 {
11951 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11952 " [in module %s]"),
11953 dwp_file->name);
11954 }
11955 }
d2854d8d
CT
11956 else /* version == 5 */
11957 {
11958 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11959 int *ids = htab->section_pool.v5.section_ids;
11960 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11961 /* Reverse map for error checking. */
11962 int ids_seen[DW_SECT_MAX_V5 + 1];
11963
11964 if (nr_columns < 2)
11965 {
11966 error (_("Dwarf Error: bad DWP hash table, too few columns"
11967 " in section table [in module %s]"),
11968 dwp_file->name);
11969 }
11970 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11971 {
11972 error (_("Dwarf Error: bad DWP hash table, too many columns"
11973 " in section table [in module %s]"),
11974 dwp_file->name);
11975 }
11976 memset (ids, 255, sizeof_ids);
11977 memset (ids_seen, 255, sizeof (ids_seen));
11978 for (int i = 0; i < nr_columns; ++i)
11979 {
11980 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11981
11982 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11983 {
11984 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11985 " in section table [in module %s]"),
11986 id, dwp_file->name);
11987 }
11988 if (ids_seen[id] != -1)
11989 {
11990 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11991 " id %d in section table [in module %s]"),
11992 id, dwp_file->name);
11993 }
11994 ids_seen[id] = i;
11995 ids[i] = id;
11996 }
11997 /* Must have seen an info section. */
11998 if (ids_seen[DW_SECT_INFO_V5] == -1)
11999 {
12000 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12001 " DWO info/types section [in module %s]"),
12002 dwp_file->name);
12003 }
12004 /* Must have an abbrev section. */
12005 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
12006 {
12007 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12008 " section [in module %s]"),
12009 dwp_file->name);
12010 }
12011 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12012 htab->section_pool.v5.sizes
12013 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
12014 * nr_units * nr_columns);
12015 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
12016 * nr_units * nr_columns))
12017 > index_end)
12018 {
12019 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12020 " [in module %s]"),
12021 dwp_file->name);
12022 }
12023 }
80626a55
DE
12024
12025 return htab;
12026}
12027
12028/* Update SECTIONS with the data from SECTP.
12029
5bb6e9dd
TT
12030 This function is like the other "locate" section routines, but in
12031 this context the sections to read comes from the DWP V1 hash table,
12032 not the full ELF section table.
80626a55
DE
12033
12034 The result is non-zero for success, or zero if an error was found. */
12035
12036static int
73869dc2
DE
12037locate_v1_virtual_dwo_sections (asection *sectp,
12038 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12039{
12040 const struct dwop_section_names *names = &dwop_section_names;
12041
fbedd546 12042 if (names->abbrev_dwo.matches (sectp->name))
80626a55
DE
12043 {
12044 /* There can be only one. */
049412e3 12045 if (sections->abbrev.s.section != NULL)
80626a55 12046 return 0;
049412e3 12047 sections->abbrev.s.section = sectp;
fd361982 12048 sections->abbrev.size = bfd_section_size (sectp);
80626a55 12049 }
fbedd546
TT
12050 else if (names->info_dwo.matches (sectp->name)
12051 || names->types_dwo.matches (sectp->name))
80626a55
DE
12052 {
12053 /* There can be only one. */
049412e3 12054 if (sections->info_or_types.s.section != NULL)
80626a55 12055 return 0;
049412e3 12056 sections->info_or_types.s.section = sectp;
fd361982 12057 sections->info_or_types.size = bfd_section_size (sectp);
80626a55 12058 }
fbedd546 12059 else if (names->line_dwo.matches (sectp->name))
80626a55
DE
12060 {
12061 /* There can be only one. */
049412e3 12062 if (sections->line.s.section != NULL)
80626a55 12063 return 0;
049412e3 12064 sections->line.s.section = sectp;
fd361982 12065 sections->line.size = bfd_section_size (sectp);
80626a55 12066 }
fbedd546 12067 else if (names->loc_dwo.matches (sectp->name))
80626a55
DE
12068 {
12069 /* There can be only one. */
049412e3 12070 if (sections->loc.s.section != NULL)
80626a55 12071 return 0;
049412e3 12072 sections->loc.s.section = sectp;
fd361982 12073 sections->loc.size = bfd_section_size (sectp);
80626a55 12074 }
fbedd546 12075 else if (names->macinfo_dwo.matches (sectp->name))
80626a55
DE
12076 {
12077 /* There can be only one. */
049412e3 12078 if (sections->macinfo.s.section != NULL)
80626a55 12079 return 0;
049412e3 12080 sections->macinfo.s.section = sectp;
fd361982 12081 sections->macinfo.size = bfd_section_size (sectp);
80626a55 12082 }
fbedd546 12083 else if (names->macro_dwo.matches (sectp->name))
80626a55
DE
12084 {
12085 /* There can be only one. */
049412e3 12086 if (sections->macro.s.section != NULL)
80626a55 12087 return 0;
049412e3 12088 sections->macro.s.section = sectp;
fd361982 12089 sections->macro.size = bfd_section_size (sectp);
80626a55 12090 }
fbedd546 12091 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55
DE
12092 {
12093 /* There can be only one. */
049412e3 12094 if (sections->str_offsets.s.section != NULL)
80626a55 12095 return 0;
049412e3 12096 sections->str_offsets.s.section = sectp;
fd361982 12097 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12098 }
12099 else
12100 {
12101 /* No other kind of section is valid. */
12102 return 0;
12103 }
12104
12105 return 1;
12106}
12107
73869dc2
DE
12108/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12109 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12110 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12111 This is for DWP version 1 files. */
80626a55
DE
12112
12113static struct dwo_unit *
976ca316 12114create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 12115 struct dwp_file *dwp_file,
73869dc2
DE
12116 uint32_t unit_index,
12117 const char *comp_dir,
12118 ULONGEST signature, int is_debug_types)
80626a55 12119{
73869dc2
DE
12120 const struct dwp_hash_table *dwp_htab =
12121 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12122 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12123 const char *kind = is_debug_types ? "TU" : "CU";
12124 struct dwo_file *dwo_file;
12125 struct dwo_unit *dwo_unit;
73869dc2 12126 struct virtual_v1_dwo_sections sections;
80626a55 12127 void **dwo_file_slot;
80626a55
DE
12128 int i;
12129
73869dc2
DE
12130 gdb_assert (dwp_file->version == 1);
12131
6f738b01
SM
12132 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12133 kind, pulongest (unit_index), hex_string (signature),
12134 dwp_file->name);
80626a55 12135
19ac8c2e 12136 /* Fetch the sections of this DWO unit.
80626a55
DE
12137 Put a limit on the number of sections we look for so that bad data
12138 doesn't cause us to loop forever. */
12139
73869dc2 12140#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12141 (1 /* .debug_info or .debug_types */ \
12142 + 1 /* .debug_abbrev */ \
12143 + 1 /* .debug_line */ \
12144 + 1 /* .debug_loc */ \
12145 + 1 /* .debug_str_offsets */ \
19ac8c2e 12146 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12147 + 1 /* trailing zero */)
12148
12149 memset (&sections, 0, sizeof (sections));
80626a55 12150
73869dc2 12151 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12152 {
12153 asection *sectp;
12154 uint32_t section_nr =
12155 read_4_bytes (dbfd,
73869dc2
DE
12156 dwp_htab->section_pool.v1.indices
12157 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12158
12159 if (section_nr == 0)
12160 break;
12161 if (section_nr >= dwp_file->num_sections)
12162 {
12163 error (_("Dwarf Error: bad DWP hash table, section number too large"
12164 " [in module %s]"),
12165 dwp_file->name);
12166 }
12167
12168 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12169 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12170 {
12171 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12172 " [in module %s]"),
12173 dwp_file->name);
12174 }
12175 }
12176
12177 if (i < 2
96b79293
TT
12178 || sections.info_or_types.empty ()
12179 || sections.abbrev.empty ())
80626a55
DE
12180 {
12181 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12182 " [in module %s]"),
12183 dwp_file->name);
12184 }
73869dc2 12185 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12186 {
12187 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12188 " [in module %s]"),
12189 dwp_file->name);
12190 }
12191
12192 /* It's easier for the rest of the code if we fake a struct dwo_file and
12193 have dwo_unit "live" in that. At least for now.
12194
12195 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12196 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12197 file, we can combine them back into a virtual DWO file to save space
12198 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12199 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12200
791afaa2
TT
12201 std::string virtual_dwo_name =
12202 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
12203 sections.abbrev.get_id (),
12204 sections.line.get_id (),
12205 sections.loc.get_id (),
12206 sections.str_offsets.get_id ());
80626a55 12207 /* Can we use an existing virtual DWO file? */
976ca316 12208 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12209 comp_dir);
80626a55
DE
12210 /* Create one if necessary. */
12211 if (*dwo_file_slot == NULL)
12212 {
6f738b01
SM
12213 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12214 virtual_dwo_name.c_str ());
12215
51ac9db5 12216 dwo_file = new struct dwo_file;
976ca316 12217 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 12218 dwo_file->comp_dir = comp_dir;
80626a55
DE
12219 dwo_file->sections.abbrev = sections.abbrev;
12220 dwo_file->sections.line = sections.line;
12221 dwo_file->sections.loc = sections.loc;
12222 dwo_file->sections.macinfo = sections.macinfo;
12223 dwo_file->sections.macro = sections.macro;
12224 dwo_file->sections.str_offsets = sections.str_offsets;
12225 /* The "str" section is global to the entire DWP file. */
12226 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12227 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12228 there's no need to record it in dwo_file.
12229 Also, we can't simply record type sections in dwo_file because
12230 we record a pointer into the vector in dwo_unit. As we collect more
12231 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12232 for it, invalidating all copies of pointers into the previous
12233 contents. */
80626a55
DE
12234 *dwo_file_slot = dwo_file;
12235 }
12236 else
12237 {
6f738b01
SM
12238 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12239 virtual_dwo_name.c_str ());
12240
9a3c8263 12241 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12242 }
80626a55 12243
976ca316 12244 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
12245 dwo_unit->dwo_file = dwo_file;
12246 dwo_unit->signature = signature;
8d749320 12247 dwo_unit->section =
976ca316 12248 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 12249 *dwo_unit->section = sections.info_or_types;
57d63ce2 12250 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12251
12252 return dwo_unit;
12253}
12254
d2854d8d
CT
12255/* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12256 simplify them. Given a pointer to the containing section SECTION, and
12257 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12258 virtual section of just that piece. */
73869dc2
DE
12259
12260static struct dwarf2_section_info
d2854d8d
CT
12261create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12262 struct dwarf2_section_info *section,
12263 bfd_size_type offset, bfd_size_type size)
73869dc2
DE
12264{
12265 struct dwarf2_section_info result;
12266 asection *sectp;
12267
12268 gdb_assert (section != NULL);
12269 gdb_assert (!section->is_virtual);
12270
12271 memset (&result, 0, sizeof (result));
12272 result.s.containing_section = section;
dc4ccb6f 12273 result.is_virtual = true;
73869dc2
DE
12274
12275 if (size == 0)
12276 return result;
12277
96b79293 12278 sectp = section->get_bfd_section ();
73869dc2
DE
12279
12280 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12281 bounds of the real section. This is a pretty-rare event, so just
12282 flag an error (easier) instead of a warning and trying to cope. */
12283 if (sectp == NULL
fd361982 12284 || offset + size > bfd_section_size (sectp))
73869dc2 12285 {
d2854d8d 12286 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
73869dc2 12287 " in section %s [in module %s]"),
fd361982 12288 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 12289 objfile_name (per_objfile->objfile));
73869dc2
DE
12290 }
12291
12292 result.virtual_offset = offset;
12293 result.size = size;
12294 return result;
12295}
12296
12297/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12298 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12299 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12300 This is for DWP version 2 files. */
12301
12302static struct dwo_unit *
976ca316 12303create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 12304 struct dwp_file *dwp_file,
73869dc2
DE
12305 uint32_t unit_index,
12306 const char *comp_dir,
12307 ULONGEST signature, int is_debug_types)
12308{
73869dc2
DE
12309 const struct dwp_hash_table *dwp_htab =
12310 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12311 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12312 const char *kind = is_debug_types ? "TU" : "CU";
12313 struct dwo_file *dwo_file;
12314 struct dwo_unit *dwo_unit;
d2854d8d 12315 struct virtual_v2_or_v5_dwo_sections sections;
73869dc2 12316 void **dwo_file_slot;
73869dc2
DE
12317 int i;
12318
12319 gdb_assert (dwp_file->version == 2);
12320
6f738b01
SM
12321 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12322 kind, pulongest (unit_index), hex_string (signature),
12323 dwp_file->name);
73869dc2
DE
12324
12325 /* Fetch the section offsets of this DWO unit. */
12326
12327 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12328
12329 for (i = 0; i < dwp_htab->nr_columns; ++i)
12330 {
12331 uint32_t offset = read_4_bytes (dbfd,
12332 dwp_htab->section_pool.v2.offsets
12333 + (((unit_index - 1) * dwp_htab->nr_columns
12334 + i)
12335 * sizeof (uint32_t)));
12336 uint32_t size = read_4_bytes (dbfd,
12337 dwp_htab->section_pool.v2.sizes
12338 + (((unit_index - 1) * dwp_htab->nr_columns
12339 + i)
12340 * sizeof (uint32_t)));
12341
12342 switch (dwp_htab->section_pool.v2.section_ids[i])
12343 {
12344 case DW_SECT_INFO:
12345 case DW_SECT_TYPES:
12346 sections.info_or_types_offset = offset;
12347 sections.info_or_types_size = size;
12348 break;
12349 case DW_SECT_ABBREV:
12350 sections.abbrev_offset = offset;
12351 sections.abbrev_size = size;
12352 break;
12353 case DW_SECT_LINE:
12354 sections.line_offset = offset;
12355 sections.line_size = size;
12356 break;
12357 case DW_SECT_LOC:
12358 sections.loc_offset = offset;
12359 sections.loc_size = size;
12360 break;
12361 case DW_SECT_STR_OFFSETS:
12362 sections.str_offsets_offset = offset;
12363 sections.str_offsets_size = size;
12364 break;
12365 case DW_SECT_MACINFO:
12366 sections.macinfo_offset = offset;
12367 sections.macinfo_size = size;
12368 break;
12369 case DW_SECT_MACRO:
12370 sections.macro_offset = offset;
12371 sections.macro_size = size;
12372 break;
12373 }
12374 }
12375
12376 /* It's easier for the rest of the code if we fake a struct dwo_file and
12377 have dwo_unit "live" in that. At least for now.
12378
12379 The DWP file can be made up of a random collection of CUs and TUs.
12380 However, for each CU + set of TUs that came from the same original DWO
12381 file, we can combine them back into a virtual DWO file to save space
12382 (fewer struct dwo_file objects to allocate). Remember that for really
12383 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12384
791afaa2
TT
12385 std::string virtual_dwo_name =
12386 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12387 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12388 (long) (sections.line_size ? sections.line_offset : 0),
12389 (long) (sections.loc_size ? sections.loc_offset : 0),
12390 (long) (sections.str_offsets_size
12391 ? sections.str_offsets_offset : 0));
73869dc2 12392 /* Can we use an existing virtual DWO file? */
976ca316 12393 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12394 comp_dir);
73869dc2
DE
12395 /* Create one if necessary. */
12396 if (*dwo_file_slot == NULL)
12397 {
6f738b01
SM
12398 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12399 virtual_dwo_name.c_str ());
12400
51ac9db5 12401 dwo_file = new struct dwo_file;
976ca316 12402 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12403 dwo_file->comp_dir = comp_dir;
12404 dwo_file->sections.abbrev =
d2854d8d
CT
12405 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12406 sections.abbrev_offset,
12407 sections.abbrev_size);
73869dc2 12408 dwo_file->sections.line =
d2854d8d
CT
12409 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12410 sections.line_offset,
12411 sections.line_size);
73869dc2 12412 dwo_file->sections.loc =
d2854d8d
CT
12413 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12414 sections.loc_offset, sections.loc_size);
73869dc2 12415 dwo_file->sections.macinfo =
d2854d8d
CT
12416 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12417 sections.macinfo_offset,
12418 sections.macinfo_size);
73869dc2 12419 dwo_file->sections.macro =
d2854d8d
CT
12420 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12421 sections.macro_offset,
12422 sections.macro_size);
73869dc2 12423 dwo_file->sections.str_offsets =
d2854d8d
CT
12424 create_dwp_v2_or_v5_section (per_objfile,
12425 &dwp_file->sections.str_offsets,
12426 sections.str_offsets_offset,
12427 sections.str_offsets_size);
73869dc2
DE
12428 /* The "str" section is global to the entire DWP file. */
12429 dwo_file->sections.str = dwp_file->sections.str;
12430 /* The info or types section is assigned below to dwo_unit,
12431 there's no need to record it in dwo_file.
12432 Also, we can't simply record type sections in dwo_file because
12433 we record a pointer into the vector in dwo_unit. As we collect more
12434 types we'll grow the vector and eventually have to reallocate space
12435 for it, invalidating all copies of pointers into the previous
12436 contents. */
12437 *dwo_file_slot = dwo_file;
12438 }
12439 else
12440 {
6f738b01
SM
12441 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12442 virtual_dwo_name.c_str ());
12443
9a3c8263 12444 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12445 }
73869dc2 12446
976ca316 12447 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12448 dwo_unit->dwo_file = dwo_file;
12449 dwo_unit->signature = signature;
8d749320 12450 dwo_unit->section =
976ca316 12451 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
d2854d8d 12452 *dwo_unit->section = create_dwp_v2_or_v5_section
dda83cd7 12453 (per_objfile,
d2854d8d
CT
12454 is_debug_types
12455 ? &dwp_file->sections.types
12456 : &dwp_file->sections.info,
12457 sections.info_or_types_offset,
12458 sections.info_or_types_size);
12459 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12460
12461 return dwo_unit;
12462}
12463
12464/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12465 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12466 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12467 This is for DWP version 5 files. */
12468
12469static struct dwo_unit *
12470create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12471 struct dwp_file *dwp_file,
12472 uint32_t unit_index,
12473 const char *comp_dir,
12474 ULONGEST signature, int is_debug_types)
12475{
12476 const struct dwp_hash_table *dwp_htab
12477 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12478 bfd *dbfd = dwp_file->dbfd.get ();
12479 const char *kind = is_debug_types ? "TU" : "CU";
12480 struct dwo_file *dwo_file;
12481 struct dwo_unit *dwo_unit;
12482 struct virtual_v2_or_v5_dwo_sections sections {};
12483 void **dwo_file_slot;
12484
12485 gdb_assert (dwp_file->version == 5);
12486
6f738b01
SM
12487 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12488 kind, pulongest (unit_index), hex_string (signature),
12489 dwp_file->name);
d2854d8d
CT
12490
12491 /* Fetch the section offsets of this DWO unit. */
12492
12493 /* memset (&sections, 0, sizeof (sections)); */
12494
12495 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12496 {
12497 uint32_t offset = read_4_bytes (dbfd,
dda83cd7
SM
12498 dwp_htab->section_pool.v5.offsets
12499 + (((unit_index - 1)
12500 * dwp_htab->nr_columns
12501 + i)
12502 * sizeof (uint32_t)));
d2854d8d 12503 uint32_t size = read_4_bytes (dbfd,
dda83cd7
SM
12504 dwp_htab->section_pool.v5.sizes
12505 + (((unit_index - 1) * dwp_htab->nr_columns
12506 + i)
12507 * sizeof (uint32_t)));
d2854d8d
CT
12508
12509 switch (dwp_htab->section_pool.v5.section_ids[i])
dda83cd7
SM
12510 {
12511 case DW_SECT_ABBREV_V5:
12512 sections.abbrev_offset = offset;
12513 sections.abbrev_size = size;
12514 break;
12515 case DW_SECT_INFO_V5:
12516 sections.info_or_types_offset = offset;
12517 sections.info_or_types_size = size;
12518 break;
12519 case DW_SECT_LINE_V5:
12520 sections.line_offset = offset;
12521 sections.line_size = size;
12522 break;
12523 case DW_SECT_LOCLISTS_V5:
12524 sections.loclists_offset = offset;
12525 sections.loclists_size = size;
12526 break;
12527 case DW_SECT_MACRO_V5:
12528 sections.macro_offset = offset;
12529 sections.macro_size = size;
12530 break;
12531 case DW_SECT_RNGLISTS_V5:
12532 sections.rnglists_offset = offset;
12533 sections.rnglists_size = size;
12534 break;
12535 case DW_SECT_STR_OFFSETS_V5:
12536 sections.str_offsets_offset = offset;
12537 sections.str_offsets_size = size;
12538 break;
12539 case DW_SECT_RESERVED_V5:
12540 default:
12541 break;
12542 }
d2854d8d
CT
12543 }
12544
12545 /* It's easier for the rest of the code if we fake a struct dwo_file and
12546 have dwo_unit "live" in that. At least for now.
12547
12548 The DWP file can be made up of a random collection of CUs and TUs.
12549 However, for each CU + set of TUs that came from the same original DWO
12550 file, we can combine them back into a virtual DWO file to save space
12551 (fewer struct dwo_file objects to allocate). Remember that for really
12552 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12553
12554 std::string virtual_dwo_name =
12555 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
dda83cd7
SM
12556 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12557 (long) (sections.line_size ? sections.line_offset : 0),
12558 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12559 (long) (sections.str_offsets_size
12560 ? sections.str_offsets_offset : 0),
12561 (long) (sections.macro_size ? sections.macro_offset : 0),
12562 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
d2854d8d
CT
12563 /* Can we use an existing virtual DWO file? */
12564 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
dda83cd7
SM
12565 virtual_dwo_name.c_str (),
12566 comp_dir);
d2854d8d
CT
12567 /* Create one if necessary. */
12568 if (*dwo_file_slot == NULL)
12569 {
6f738b01
SM
12570 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12571 virtual_dwo_name.c_str ());
12572
d2854d8d
CT
12573 dwo_file = new struct dwo_file;
12574 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12575 dwo_file->comp_dir = comp_dir;
12576 dwo_file->sections.abbrev =
dda83cd7
SM
12577 create_dwp_v2_or_v5_section (per_objfile,
12578 &dwp_file->sections.abbrev,
12579 sections.abbrev_offset,
12580 sections.abbrev_size);
d2854d8d 12581 dwo_file->sections.line =
dda83cd7
SM
12582 create_dwp_v2_or_v5_section (per_objfile,
12583 &dwp_file->sections.line,
12584 sections.line_offset, sections.line_size);
d2854d8d 12585 dwo_file->sections.macro =
dda83cd7
SM
12586 create_dwp_v2_or_v5_section (per_objfile,
12587 &dwp_file->sections.macro,
12588 sections.macro_offset,
12589 sections.macro_size);
d2854d8d 12590 dwo_file->sections.loclists =
dda83cd7
SM
12591 create_dwp_v2_or_v5_section (per_objfile,
12592 &dwp_file->sections.loclists,
12593 sections.loclists_offset,
12594 sections.loclists_size);
d2854d8d 12595 dwo_file->sections.rnglists =
dda83cd7
SM
12596 create_dwp_v2_or_v5_section (per_objfile,
12597 &dwp_file->sections.rnglists,
12598 sections.rnglists_offset,
12599 sections.rnglists_size);
d2854d8d 12600 dwo_file->sections.str_offsets =
dda83cd7
SM
12601 create_dwp_v2_or_v5_section (per_objfile,
12602 &dwp_file->sections.str_offsets,
12603 sections.str_offsets_offset,
12604 sections.str_offsets_size);
d2854d8d
CT
12605 /* The "str" section is global to the entire DWP file. */
12606 dwo_file->sections.str = dwp_file->sections.str;
12607 /* The info or types section is assigned below to dwo_unit,
dda83cd7
SM
12608 there's no need to record it in dwo_file.
12609 Also, we can't simply record type sections in dwo_file because
12610 we record a pointer into the vector in dwo_unit. As we collect more
12611 types we'll grow the vector and eventually have to reallocate space
12612 for it, invalidating all copies of pointers into the previous
12613 contents. */
d2854d8d
CT
12614 *dwo_file_slot = dwo_file;
12615 }
12616 else
12617 {
6f738b01
SM
12618 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12619 virtual_dwo_name.c_str ());
12620
d2854d8d
CT
12621 dwo_file = (struct dwo_file *) *dwo_file_slot;
12622 }
12623
12624 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12625 dwo_unit->dwo_file = dwo_file;
12626 dwo_unit->signature = signature;
12627 dwo_unit->section
12628 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12629 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
dda83cd7
SM
12630 &dwp_file->sections.info,
12631 sections.info_or_types_offset,
12632 sections.info_or_types_size);
73869dc2
DE
12633 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12634
12635 return dwo_unit;
12636}
12637
57d63ce2
DE
12638/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12639 Returns NULL if the signature isn't found. */
80626a55
DE
12640
12641static struct dwo_unit *
976ca316 12642lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12643 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12644 ULONGEST signature, int is_debug_types)
80626a55 12645{
57d63ce2
DE
12646 const struct dwp_hash_table *dwp_htab =
12647 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12648 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12649 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12650 uint32_t hash = signature & mask;
12651 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12652 unsigned int i;
12653 void **slot;
870f88f7 12654 struct dwo_unit find_dwo_cu;
80626a55
DE
12655
12656 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12657 find_dwo_cu.signature = signature;
19ac8c2e 12658 slot = htab_find_slot (is_debug_types
48b490f2
TT
12659 ? dwp_file->loaded_tus.get ()
12660 : dwp_file->loaded_cus.get (),
19ac8c2e 12661 &find_dwo_cu, INSERT);
80626a55
DE
12662
12663 if (*slot != NULL)
9a3c8263 12664 return (struct dwo_unit *) *slot;
80626a55
DE
12665
12666 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12667 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12668 {
12669 ULONGEST signature_in_table;
12670
12671 signature_in_table =
57d63ce2 12672 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12673 if (signature_in_table == signature)
12674 {
57d63ce2
DE
12675 uint32_t unit_index =
12676 read_4_bytes (dbfd,
12677 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12678
73869dc2
DE
12679 if (dwp_file->version == 1)
12680 {
976ca316
SM
12681 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12682 unit_index, comp_dir,
12683 signature, is_debug_types);
73869dc2 12684 }
d2854d8d 12685 else if (dwp_file->version == 2)
73869dc2 12686 {
976ca316
SM
12687 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12688 unit_index, comp_dir,
12689 signature, is_debug_types);
73869dc2 12690 }
d2854d8d
CT
12691 else /* version == 5 */
12692 {
12693 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12694 unit_index, comp_dir,
12695 signature, is_debug_types);
12696 }
9a3c8263 12697 return (struct dwo_unit *) *slot;
80626a55
DE
12698 }
12699 if (signature_in_table == 0)
12700 return NULL;
12701 hash = (hash + hash2) & mask;
12702 }
12703
12704 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12705 " [in module %s]"),
12706 dwp_file->name);
12707}
12708
ab5088bf 12709/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12710 Open the file specified by FILE_NAME and hand it off to BFD for
12711 preliminary analysis. Return a newly initialized bfd *, which
12712 includes a canonicalized copy of FILE_NAME.
80626a55 12713 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12714 SEARCH_CWD is true if the current directory is to be searched.
12715 It will be searched before debug-file-directory.
13aaf454
DE
12716 If successful, the file is added to the bfd include table of the
12717 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12718 If unable to find/open the file, return NULL.
3019eac3
DE
12719 NOTE: This function is derived from symfile_bfd_open. */
12720
192b62ce 12721static gdb_bfd_ref_ptr
976ca316 12722try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12723 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12724{
24b9144d 12725 int desc;
9c02c129
DE
12726 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12727 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12728 to debug_file_directory. */
e0cc99a6 12729 const char *search_path;
9c02c129
DE
12730 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12731
e0cc99a6 12732 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12733 if (search_cwd)
12734 {
12735 if (*debug_file_directory != '\0')
e0cc99a6
TT
12736 {
12737 search_path_holder.reset (concat (".", dirname_separator_string,
12738 debug_file_directory,
12739 (char *) NULL));
12740 search_path = search_path_holder.get ();
12741 }
6ac97d4c 12742 else
e0cc99a6 12743 search_path = ".";
6ac97d4c 12744 }
9c02c129 12745 else
e0cc99a6 12746 search_path = debug_file_directory;
3019eac3 12747
24b9144d 12748 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12749 if (is_dwp)
12750 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12751
12752 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12753 desc = openp (search_path, flags, file_name,
3019eac3
DE
12754 O_RDONLY | O_BINARY, &absolute_name);
12755 if (desc < 0)
12756 return NULL;
12757
e0cc99a6
TT
12758 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12759 gnutarget, desc));
9c02c129
DE
12760 if (sym_bfd == NULL)
12761 return NULL;
192b62ce 12762 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12763
192b62ce
TT
12764 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12765 return NULL;
3019eac3 12766
13aaf454
DE
12767 /* Success. Record the bfd as having been included by the objfile's bfd.
12768 This is important because things like demangled_names_hash lives in the
12769 objfile's per_bfd space and may have references to things like symbol
12770 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12771 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12772
3019eac3
DE
12773 return sym_bfd;
12774}
12775
ab5088bf 12776/* Try to open DWO file FILE_NAME.
3019eac3
DE
12777 COMP_DIR is the DW_AT_comp_dir attribute.
12778 The result is the bfd handle of the file.
12779 If there is a problem finding or opening the file, return NULL.
12780 Upon success, the canonicalized path of the file is stored in the bfd,
12781 same as symfile_bfd_open. */
12782
192b62ce 12783static gdb_bfd_ref_ptr
976ca316 12784open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12785 const char *file_name, const char *comp_dir)
3019eac3 12786{
80626a55 12787 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12788 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12789 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12790
12791 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12792
12793 if (comp_dir != NULL)
12794 {
43816ebc
TT
12795 gdb::unique_xmalloc_ptr<char> path_to_try
12796 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12797
12798 /* NOTE: If comp_dir is a relative path, this will also try the
12799 search path, which seems useful. */
976ca316 12800 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12801 0 /*is_dwp*/,
192b62ce 12802 1 /*search_cwd*/));
3019eac3
DE
12803 if (abfd != NULL)
12804 return abfd;
12805 }
12806
12807 /* That didn't work, try debug-file-directory, which, despite its name,
12808 is a list of paths. */
12809
12810 if (*debug_file_directory == '\0')
12811 return NULL;
12812
976ca316 12813 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12814 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12815}
12816
80626a55
DE
12817/* This function is mapped across the sections and remembers the offset and
12818 size of each of the DWO debugging sections we are interested in. */
12819
12820static void
5bb6e9dd
TT
12821dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12822 dwo_sections *dwo_sections)
80626a55 12823{
80626a55
DE
12824 const struct dwop_section_names *names = &dwop_section_names;
12825
fbedd546 12826 if (names->abbrev_dwo.matches (sectp->name))
80626a55 12827 {
049412e3 12828 dwo_sections->abbrev.s.section = sectp;
fd361982 12829 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55 12830 }
fbedd546 12831 else if (names->info_dwo.matches (sectp->name))
80626a55 12832 {
049412e3 12833 dwo_sections->info.s.section = sectp;
fd361982 12834 dwo_sections->info.size = bfd_section_size (sectp);
80626a55 12835 }
fbedd546 12836 else if (names->line_dwo.matches (sectp->name))
80626a55 12837 {
049412e3 12838 dwo_sections->line.s.section = sectp;
fd361982 12839 dwo_sections->line.size = bfd_section_size (sectp);
80626a55 12840 }
fbedd546 12841 else if (names->loc_dwo.matches (sectp->name))
80626a55 12842 {
049412e3 12843 dwo_sections->loc.s.section = sectp;
fd361982 12844 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12845 }
fbedd546 12846 else if (names->loclists_dwo.matches (sectp->name))
41144253 12847 {
12848 dwo_sections->loclists.s.section = sectp;
12849 dwo_sections->loclists.size = bfd_section_size (sectp);
12850 }
fbedd546 12851 else if (names->macinfo_dwo.matches (sectp->name))
80626a55 12852 {
049412e3 12853 dwo_sections->macinfo.s.section = sectp;
fd361982 12854 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55 12855 }
fbedd546 12856 else if (names->macro_dwo.matches (sectp->name))
80626a55 12857 {
049412e3 12858 dwo_sections->macro.s.section = sectp;
fd361982 12859 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55 12860 }
fbedd546 12861 else if (names->rnglists_dwo.matches (sectp->name))
d0ce17d8
CT
12862 {
12863 dwo_sections->rnglists.s.section = sectp;
12864 dwo_sections->rnglists.size = bfd_section_size (sectp);
12865 }
fbedd546 12866 else if (names->str_dwo.matches (sectp->name))
80626a55 12867 {
049412e3 12868 dwo_sections->str.s.section = sectp;
fd361982 12869 dwo_sections->str.size = bfd_section_size (sectp);
80626a55 12870 }
fbedd546 12871 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55 12872 {
049412e3 12873 dwo_sections->str_offsets.s.section = sectp;
fd361982 12874 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55 12875 }
fbedd546 12876 else if (names->types_dwo.matches (sectp->name))
80626a55
DE
12877 {
12878 struct dwarf2_section_info type_section;
12879
12880 memset (&type_section, 0, sizeof (type_section));
049412e3 12881 type_section.s.section = sectp;
fd361982 12882 type_section.size = bfd_section_size (sectp);
fd5866f6 12883 dwo_sections->types.push_back (type_section);
80626a55
DE
12884 }
12885}
12886
ab5088bf 12887/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12888 by PER_CU. This is for the non-DWP case.
80626a55 12889 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12890
12891static struct dwo_file *
4ab09049
SM
12892open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12893 const char *comp_dir)
3019eac3 12894{
976ca316 12895 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12896
976ca316 12897 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12898 if (dbfd == NULL)
12899 {
6f738b01
SM
12900 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12901
80626a55
DE
12902 return NULL;
12903 }
263db9a1 12904
51ac9db5 12905 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12906 dwo_file->dwo_name = dwo_name;
12907 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12908 dwo_file->dbfd = std::move (dbfd);
3019eac3 12909
5bb6e9dd
TT
12910 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12911 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12912 &dwo_file->sections);
3019eac3 12913
976ca316
SM
12914 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12915 dwo_file->cus);
3019eac3 12916
d2854d8d
CT
12917 if (cu->per_cu->dwarf_version < 5)
12918 {
12919 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12920 dwo_file->sections.types, dwo_file->tus);
12921 }
12922 else
12923 {
12924 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12925 &dwo_file->sections.info, dwo_file->tus,
30c80d88 12926 rcuh_kind::COMPILE);
d2854d8d 12927 }
3019eac3 12928
6f738b01 12929 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
80626a55 12930
263db9a1 12931 return dwo_file.release ();
3019eac3
DE
12932}
12933
80626a55 12934/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12935 size of each of the DWP debugging sections common to version 1 and 2 that
12936 we are interested in. */
3019eac3 12937
80626a55 12938static void
73869dc2 12939dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
5bb6e9dd 12940 dwp_file *dwp_file)
3019eac3 12941{
80626a55
DE
12942 const struct dwop_section_names *names = &dwop_section_names;
12943 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12944
80626a55 12945 /* Record the ELF section number for later lookup: this is what the
73869dc2 12946 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12947 gdb_assert (elf_section_nr < dwp_file->num_sections);
12948 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12949
80626a55 12950 /* Look for specific sections that we need. */
fbedd546 12951 if (names->str_dwo.matches (sectp->name))
80626a55 12952 {
049412e3 12953 dwp_file->sections.str.s.section = sectp;
fd361982 12954 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55 12955 }
fbedd546 12956 else if (names->cu_index.matches (sectp->name))
80626a55 12957 {
049412e3 12958 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12959 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55 12960 }
fbedd546 12961 else if (names->tu_index.matches (sectp->name))
80626a55 12962 {
049412e3 12963 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12964 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12965 }
12966}
3019eac3 12967
73869dc2
DE
12968/* This function is mapped across the sections and remembers the offset and
12969 size of each of the DWP version 2 debugging sections that we are interested
12970 in. This is split into a separate function because we don't know if we
d2854d8d 12971 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
73869dc2
DE
12972
12973static void
12974dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12975{
9a3c8263 12976 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12977 const struct dwop_section_names *names = &dwop_section_names;
12978 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12979
12980 /* Record the ELF section number for later lookup: this is what the
12981 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12982 gdb_assert (elf_section_nr < dwp_file->num_sections);
12983 dwp_file->elf_sections[elf_section_nr] = sectp;
12984
12985 /* Look for specific sections that we need. */
fbedd546 12986 if (names->abbrev_dwo.matches (sectp->name))
73869dc2 12987 {
049412e3 12988 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12989 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2 12990 }
fbedd546 12991 else if (names->info_dwo.matches (sectp->name))
73869dc2 12992 {
049412e3 12993 dwp_file->sections.info.s.section = sectp;
fd361982 12994 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2 12995 }
fbedd546 12996 else if (names->line_dwo.matches (sectp->name))
73869dc2 12997 {
049412e3 12998 dwp_file->sections.line.s.section = sectp;
fd361982 12999 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2 13000 }
fbedd546 13001 else if (names->loc_dwo.matches (sectp->name))
73869dc2 13002 {
049412e3 13003 dwp_file->sections.loc.s.section = sectp;
fd361982 13004 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2 13005 }
fbedd546 13006 else if (names->macinfo_dwo.matches (sectp->name))
73869dc2 13007 {
049412e3 13008 dwp_file->sections.macinfo.s.section = sectp;
fd361982 13009 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2 13010 }
fbedd546 13011 else if (names->macro_dwo.matches (sectp->name))
73869dc2 13012 {
049412e3 13013 dwp_file->sections.macro.s.section = sectp;
fd361982 13014 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2 13015 }
fbedd546 13016 else if (names->str_offsets_dwo.matches (sectp->name))
73869dc2 13017 {
049412e3 13018 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 13019 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2 13020 }
fbedd546 13021 else if (names->types_dwo.matches (sectp->name))
73869dc2 13022 {
049412e3 13023 dwp_file->sections.types.s.section = sectp;
fd361982 13024 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
13025 }
13026}
13027
d2854d8d
CT
13028/* This function is mapped across the sections and remembers the offset and
13029 size of each of the DWP version 5 debugging sections that we are interested
13030 in. This is split into a separate function because we don't know if we
13031 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
13032
13033static void
13034dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13035{
13036 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13037 const struct dwop_section_names *names = &dwop_section_names;
13038 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13039
13040 /* Record the ELF section number for later lookup: this is what the
13041 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13042 gdb_assert (elf_section_nr < dwp_file->num_sections);
13043 dwp_file->elf_sections[elf_section_nr] = sectp;
13044
13045 /* Look for specific sections that we need. */
fbedd546 13046 if (names->abbrev_dwo.matches (sectp->name))
d2854d8d
CT
13047 {
13048 dwp_file->sections.abbrev.s.section = sectp;
13049 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13050 }
fbedd546 13051 else if (names->info_dwo.matches (sectp->name))
d2854d8d
CT
13052 {
13053 dwp_file->sections.info.s.section = sectp;
13054 dwp_file->sections.info.size = bfd_section_size (sectp);
13055 }
fbedd546 13056 else if (names->line_dwo.matches (sectp->name))
d2854d8d
CT
13057 {
13058 dwp_file->sections.line.s.section = sectp;
13059 dwp_file->sections.line.size = bfd_section_size (sectp);
13060 }
fbedd546 13061 else if (names->loclists_dwo.matches (sectp->name))
d2854d8d
CT
13062 {
13063 dwp_file->sections.loclists.s.section = sectp;
13064 dwp_file->sections.loclists.size = bfd_section_size (sectp);
13065 }
fbedd546 13066 else if (names->macro_dwo.matches (sectp->name))
d2854d8d
CT
13067 {
13068 dwp_file->sections.macro.s.section = sectp;
13069 dwp_file->sections.macro.size = bfd_section_size (sectp);
13070 }
fbedd546 13071 else if (names->rnglists_dwo.matches (sectp->name))
d2854d8d
CT
13072 {
13073 dwp_file->sections.rnglists.s.section = sectp;
13074 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
13075 }
fbedd546 13076 else if (names->str_offsets_dwo.matches (sectp->name))
d2854d8d
CT
13077 {
13078 dwp_file->sections.str_offsets.s.section = sectp;
13079 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13080 }
13081}
13082
80626a55 13083/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13084
80626a55
DE
13085static hashval_t
13086hash_dwp_loaded_cutus (const void *item)
13087{
9a3c8263 13088 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13089
80626a55
DE
13090 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13091 return dwo_unit->signature;
3019eac3
DE
13092}
13093
80626a55 13094/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13095
80626a55
DE
13096static int
13097eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13098{
9a3c8263
SM
13099 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13100 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13101
80626a55
DE
13102 return dua->signature == dub->signature;
13103}
3019eac3 13104
80626a55 13105/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13106
48b490f2 13107static htab_up
298e9637 13108allocate_dwp_loaded_cutus_table ()
80626a55 13109{
48b490f2
TT
13110 return htab_up (htab_create_alloc (3,
13111 hash_dwp_loaded_cutus,
13112 eq_dwp_loaded_cutus,
13113 NULL, xcalloc, xfree));
80626a55 13114}
3019eac3 13115
ab5088bf
DE
13116/* Try to open DWP file FILE_NAME.
13117 The result is the bfd handle of the file.
13118 If there is a problem finding or opening the file, return NULL.
13119 Upon success, the canonicalized path of the file is stored in the bfd,
13120 same as symfile_bfd_open. */
13121
192b62ce 13122static gdb_bfd_ref_ptr
976ca316 13123open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 13124{
976ca316 13125 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 13126 1 /*is_dwp*/,
192b62ce 13127 1 /*search_cwd*/));
6ac97d4c
DE
13128 if (abfd != NULL)
13129 return abfd;
13130
13131 /* Work around upstream bug 15652.
13132 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13133 [Whether that's a "bug" is debatable, but it is getting in our way.]
13134 We have no real idea where the dwp file is, because gdb's realpath-ing
13135 of the executable's path may have discarded the needed info.
13136 [IWBN if the dwp file name was recorded in the executable, akin to
13137 .gnu_debuglink, but that doesn't exist yet.]
13138 Strip the directory from FILE_NAME and search again. */
13139 if (*debug_file_directory != '\0')
13140 {
13141 /* Don't implicitly search the current directory here.
13142 If the user wants to search "." to handle this case,
13143 it must be added to debug-file-directory. */
976ca316
SM
13144 return try_open_dwop_file (per_objfile, lbasename (file_name),
13145 1 /*is_dwp*/,
6ac97d4c
DE
13146 0 /*search_cwd*/);
13147 }
13148
13149 return NULL;
ab5088bf
DE
13150}
13151
80626a55
DE
13152/* Initialize the use of the DWP file for the current objfile.
13153 By convention the name of the DWP file is ${objfile}.dwp.
13154 The result is NULL if it can't be found. */
a766d390 13155
400174b1 13156static std::unique_ptr<struct dwp_file>
976ca316 13157open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 13158{
976ca316 13159 struct objfile *objfile = per_objfile->objfile;
80626a55 13160
82bf32bc
JK
13161 /* Try to find first .dwp for the binary file before any symbolic links
13162 resolving. */
6c447423
DE
13163
13164 /* If the objfile is a debug file, find the name of the real binary
13165 file and get the name of dwp file from there. */
d721ba37 13166 std::string dwp_name;
6c447423
DE
13167 if (objfile->separate_debug_objfile_backlink != NULL)
13168 {
13169 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13170 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13171
d721ba37 13172 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13173 }
13174 else
d721ba37
PA
13175 dwp_name = objfile->original_name;
13176
13177 dwp_name += ".dwp";
80626a55 13178
976ca316 13179 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13180 if (dbfd == NULL
13181 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13182 {
13183 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13184 dwp_name = objfile_name (objfile);
13185 dwp_name += ".dwp";
976ca316 13186 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
13187 }
13188
80626a55
DE
13189 if (dbfd == NULL)
13190 {
6f738b01
SM
13191 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13192
400174b1 13193 return std::unique_ptr<dwp_file> ();
3019eac3 13194 }
400174b1
TT
13195
13196 const char *name = bfd_get_filename (dbfd.get ());
13197 std::unique_ptr<struct dwp_file> dwp_file
13198 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13199
0a0f4c01 13200 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 13201 dwp_file->elf_sections =
976ca316 13202 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
13203 dwp_file->num_sections, asection *);
13204
5bb6e9dd
TT
13205 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13206 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13207 dwp_file.get ());
80626a55 13208
976ca316 13209 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 13210
976ca316 13211 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 13212
73869dc2 13213 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13214 if (dwp_file->cus && dwp_file->tus
13215 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13216 {
13217 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13218 pretty bizarre. We use pulongest here because that's the established
4d65956b 13219 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13220 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13221 " TU version %s [in DWP file %s]"),
13222 pulongest (dwp_file->cus->version),
d721ba37 13223 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13224 }
08302ed2
DE
13225
13226 if (dwp_file->cus)
13227 dwp_file->version = dwp_file->cus->version;
13228 else if (dwp_file->tus)
13229 dwp_file->version = dwp_file->tus->version;
13230 else
13231 dwp_file->version = 2;
73869dc2 13232
5bb6e9dd
TT
13233 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13234 {
13235 if (dwp_file->version == 2)
13236 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13237 dwp_file.get ());
13238 else
13239 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13240 dwp_file.get ());
13241 }
73869dc2 13242
298e9637
SM
13243 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13244 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 13245
6f738b01
SM
13246 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13247 dwarf_read_debug_printf (" %s CUs, %s TUs",
13248 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13249 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13250
13251 return dwp_file;
3019eac3 13252}
c906108c 13253
ab5088bf
DE
13254/* Wrapper around open_and_init_dwp_file, only open it once. */
13255
13256static struct dwp_file *
976ca316 13257get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 13258{
976ca316 13259 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 13260 {
976ca316
SM
13261 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13262 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 13263 }
976ca316 13264 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
13265}
13266
80626a55
DE
13267/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13268 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13269 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13270 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13271 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13272
13273 This is called, for example, when wanting to read a variable with a
13274 complex location. Therefore we don't want to do file i/o for every call.
13275 Therefore we don't want to look for a DWO file on every call.
13276 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13277 then we check if we've already seen DWO_NAME, and only THEN do we check
13278 for a DWO file.
13279
1c658ad5 13280 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13281 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13282
3019eac3 13283static struct dwo_unit *
4ab09049 13284lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 13285 ULONGEST signature, int is_debug_types)
3019eac3 13286{
976ca316
SM
13287 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13288 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
13289 const char *kind = is_debug_types ? "TU" : "CU";
13290 void **dwo_file_slot;
3019eac3 13291 struct dwo_file *dwo_file;
80626a55 13292 struct dwp_file *dwp_file;
cb1df416 13293
6a506a2d
DE
13294 /* First see if there's a DWP file.
13295 If we have a DWP file but didn't find the DWO inside it, don't
13296 look for the original DWO file. It makes gdb behave differently
13297 depending on whether one is debugging in the build tree. */
cf2c3c16 13298
976ca316 13299 dwp_file = get_dwp_file (per_objfile);
80626a55 13300 if (dwp_file != NULL)
cf2c3c16 13301 {
80626a55
DE
13302 const struct dwp_hash_table *dwp_htab =
13303 is_debug_types ? dwp_file->tus : dwp_file->cus;
13304
13305 if (dwp_htab != NULL)
13306 {
13307 struct dwo_unit *dwo_cutu =
976ca316
SM
13308 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13309 is_debug_types);
80626a55
DE
13310
13311 if (dwo_cutu != NULL)
13312 {
6f738b01
SM
13313 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13314 kind, hex_string (signature),
13315 host_address_to_string (dwo_cutu));
13316
80626a55
DE
13317 return dwo_cutu;
13318 }
13319 }
13320 }
6a506a2d 13321 else
80626a55 13322 {
6a506a2d 13323 /* No DWP file, look for the DWO file. */
80626a55 13324
976ca316 13325 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 13326 if (*dwo_file_slot == NULL)
80626a55 13327 {
6a506a2d 13328 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 13329 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 13330 }
6a506a2d 13331 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13332 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13333
6a506a2d 13334 if (dwo_file != NULL)
19c3d4c9 13335 {
6a506a2d
DE
13336 struct dwo_unit *dwo_cutu = NULL;
13337
13338 if (is_debug_types && dwo_file->tus)
13339 {
13340 struct dwo_unit find_dwo_cutu;
13341
13342 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13343 find_dwo_cutu.signature = signature;
9a3c8263 13344 dwo_cutu
b0b6a987
TT
13345 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13346 &find_dwo_cutu);
6a506a2d 13347 }
33c5cd75 13348 else if (!is_debug_types && dwo_file->cus)
80626a55 13349 {
33c5cd75
DB
13350 struct dwo_unit find_dwo_cutu;
13351
13352 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13353 find_dwo_cutu.signature = signature;
b0b6a987 13354 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 13355 &find_dwo_cutu);
6a506a2d
DE
13356 }
13357
13358 if (dwo_cutu != NULL)
13359 {
6f738b01
SM
13360 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13361 kind, dwo_name, hex_string (signature),
13362 host_address_to_string (dwo_cutu));
13363
6a506a2d 13364 return dwo_cutu;
80626a55
DE
13365 }
13366 }
2e276125 13367 }
9cdd5dbd 13368
80626a55
DE
13369 /* We didn't find it. This could mean a dwo_id mismatch, or
13370 someone deleted the DWO/DWP file, or the search path isn't set up
13371 correctly to find the file. */
13372
6f738b01
SM
13373 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13374 kind, dwo_name, hex_string (signature));
3019eac3 13375
6656a72d
DE
13376 /* This is a warning and not a complaint because it can be caused by
13377 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13378 {
13379 /* Print the name of the DWP file if we looked there, helps the user
13380 better diagnose the problem. */
791afaa2 13381 std::string dwp_text;
43942612
DE
13382
13383 if (dwp_file != NULL)
791afaa2
TT
13384 dwp_text = string_printf (" [in DWP file %s]",
13385 lbasename (dwp_file->name));
43942612 13386
9d8780f0 13387 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 13388 " [in module %s]"),
4ab09049
SM
13389 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13390 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 13391 }
3019eac3 13392 return NULL;
5fb290d7
DJ
13393}
13394
80626a55
DE
13395/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13396 See lookup_dwo_cutu_unit for details. */
13397
13398static struct dwo_unit *
4ab09049 13399lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
13400 ULONGEST signature)
13401{
4ab09049
SM
13402 gdb_assert (!cu->per_cu->is_debug_types);
13403
13404 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
13405}
13406
13407/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13408 See lookup_dwo_cutu_unit for details. */
13409
13410static struct dwo_unit *
4ab09049 13411lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 13412{
4ab09049
SM
13413 gdb_assert (cu->per_cu->is_debug_types);
13414
13415 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13416
13417 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
13418}
13419
89e63ee4
DE
13420/* Traversal function for queue_and_load_all_dwo_tus. */
13421
13422static int
13423queue_and_load_dwo_tu (void **slot, void *info)
13424{
13425 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 13426 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 13427 ULONGEST signature = dwo_unit->signature;
d460f660 13428 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
13429
13430 if (sig_type != NULL)
13431 {
13432 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13433
13434 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13435 a real dependency of PER_CU on SIG_TYPE. That is detected later
13436 while processing PER_CU. */
120ce1b5 13437 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
13438 load_full_type_unit (sig_cu, cu->per_objfile);
13439 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13440 }
13441
13442 return 1;
13443}
13444
1b555f17 13445/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
13446 The DWO may have the only definition of the type, though it may not be
13447 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13448 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13449
13450static void
1b555f17 13451queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
13452{
13453 struct dwo_unit *dwo_unit;
13454 struct dwo_file *dwo_file;
13455
1b555f17
SM
13456 gdb_assert (cu != nullptr);
13457 gdb_assert (!cu->per_cu->is_debug_types);
13458 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 13459
1b555f17 13460 dwo_unit = cu->dwo_unit;
89e63ee4
DE
13461 gdb_assert (dwo_unit != NULL);
13462
13463 dwo_file = dwo_unit->dwo_file;
13464 if (dwo_file->tus != NULL)
1b555f17 13465 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
13466}
13467
3019eac3 13468/* Read in various DIEs. */
348e048f 13469
d389af10 13470/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13471 Inherit only the children of the DW_AT_abstract_origin DIE not being
13472 already referenced by DW_AT_abstract_origin from the children of the
13473 current DIE. */
d389af10
JK
13474
13475static void
13476inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13477{
13478 struct die_info *child_die;
791afaa2 13479 sect_offset *offsetp;
d389af10
JK
13480 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13481 struct die_info *origin_die;
13482 /* Iterator of the ORIGIN_DIE children. */
13483 struct die_info *origin_child_die;
d389af10 13484 struct attribute *attr;
cd02d79d
PA
13485 struct dwarf2_cu *origin_cu;
13486 struct pending **origin_previous_list_in_scope;
d389af10
JK
13487
13488 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13489 if (!attr)
13490 return;
13491
cd02d79d
PA
13492 /* Note that following die references may follow to a die in a
13493 different cu. */
13494
13495 origin_cu = cu;
13496 origin_die = follow_die_ref (die, attr, &origin_cu);
13497
13498 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13499 symbols in. */
13500 origin_previous_list_in_scope = origin_cu->list_in_scope;
13501 origin_cu->list_in_scope = cu->list_in_scope;
13502
edb3359d
DJ
13503 if (die->tag != origin_die->tag
13504 && !(die->tag == DW_TAG_inlined_subroutine
13505 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13506 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13507 sect_offset_str (die->sect_off),
13508 sect_offset_str (origin_die->sect_off));
d389af10 13509
791afaa2 13510 std::vector<sect_offset> offsets;
d389af10 13511
3ea89b92
PMR
13512 for (child_die = die->child;
13513 child_die && child_die->tag;
436c571c 13514 child_die = child_die->sibling)
3ea89b92
PMR
13515 {
13516 struct die_info *child_origin_die;
13517 struct dwarf2_cu *child_origin_cu;
13518
13519 /* We are trying to process concrete instance entries:
216f72a1 13520 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13521 it's not relevant to our analysis here. i.e. detecting DIEs that are
13522 present in the abstract instance but not referenced in the concrete
13523 one. */
216f72a1 13524 if (child_die->tag == DW_TAG_call_site
dda83cd7 13525 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13526 continue;
13527
c38f313d
DJ
13528 /* For each CHILD_DIE, find the corresponding child of
13529 ORIGIN_DIE. If there is more than one layer of
13530 DW_AT_abstract_origin, follow them all; there shouldn't be,
13531 but GCC versions at least through 4.4 generate this (GCC PR
13532 40573). */
3ea89b92
PMR
13533 child_origin_die = child_die;
13534 child_origin_cu = cu;
c38f313d
DJ
13535 while (1)
13536 {
cd02d79d
PA
13537 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13538 child_origin_cu);
c38f313d
DJ
13539 if (attr == NULL)
13540 break;
cd02d79d
PA
13541 child_origin_die = follow_die_ref (child_origin_die, attr,
13542 &child_origin_cu);
c38f313d
DJ
13543 }
13544
d389af10
JK
13545 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13546 counterpart may exist. */
c38f313d 13547 if (child_origin_die != child_die)
d389af10 13548 {
edb3359d
DJ
13549 if (child_die->tag != child_origin_die->tag
13550 && !(child_die->tag == DW_TAG_inlined_subroutine
13551 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13552 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13553 "different tags"),
9d8780f0
SM
13554 sect_offset_str (child_die->sect_off),
13555 sect_offset_str (child_origin_die->sect_off));
c38f313d 13556 if (child_origin_die->parent != origin_die)
b98664d3 13557 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13558 "different parents"),
9d8780f0
SM
13559 sect_offset_str (child_die->sect_off),
13560 sect_offset_str (child_origin_die->sect_off));
c38f313d 13561 else
791afaa2 13562 offsets.push_back (child_origin_die->sect_off);
d389af10 13563 }
d389af10 13564 }
791afaa2
TT
13565 std::sort (offsets.begin (), offsets.end ());
13566 sect_offset *offsets_end = offsets.data () + offsets.size ();
13567 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13568 if (offsetp[-1] == *offsetp)
b98664d3 13569 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13570 "to DIE %s as their abstract origin"),
13571 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13572
791afaa2 13573 offsetp = offsets.data ();
d389af10
JK
13574 origin_child_die = origin_die->child;
13575 while (origin_child_die && origin_child_die->tag)
13576 {
13577 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13578 while (offsetp < offsets_end
9c541725 13579 && *offsetp < origin_child_die->sect_off)
d389af10 13580 offsetp++;
b64f50a1 13581 if (offsetp >= offsets_end
9c541725 13582 || *offsetp > origin_child_die->sect_off)
d389af10 13583 {
adde2bff
DE
13584 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13585 Check whether we're already processing ORIGIN_CHILD_DIE.
13586 This can happen with mutually referenced abstract_origins.
13587 PR 16581. */
13588 if (!origin_child_die->in_process)
13589 process_die (origin_child_die, origin_cu);
d389af10 13590 }
436c571c 13591 origin_child_die = origin_child_die->sibling;
d389af10 13592 }
cd02d79d 13593 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13594
13595 if (cu != origin_cu)
13596 compute_delayed_physnames (origin_cu);
d389af10
JK
13597}
13598
c906108c 13599static void
e7c27a73 13600read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13601{
5e22e966 13602 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13603 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13604 struct context_stack *newobj;
c906108c
SS
13605 CORE_ADDR lowpc;
13606 CORE_ADDR highpc;
13607 struct die_info *child_die;
edb3359d 13608 struct attribute *attr, *call_line, *call_file;
15d034d0 13609 const char *name;
e142c38c 13610 CORE_ADDR baseaddr;
801e3a5b 13611 struct block *block;
edb3359d 13612 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13613 std::vector<struct symbol *> template_args;
34eaf542 13614 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13615
13616 if (inlined_func)
13617 {
13618 /* If we do not have call site information, we can't show the
13619 caller of this inlined function. That's too confusing, so
13620 only use the scope for local variables. */
13621 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13622 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13623 if (call_line == NULL || call_file == NULL)
13624 {
13625 read_lexical_block_scope (die, cu);
13626 return;
13627 }
13628 }
c906108c 13629
b3b3bada 13630 baseaddr = objfile->text_section_offset ();
e142c38c 13631
94af9270 13632 name = dwarf2_name (die, cu);
c906108c 13633
e8d05480
JB
13634 /* Ignore functions with missing or empty names. These are actually
13635 illegal according to the DWARF standard. */
13636 if (name == NULL)
13637 {
b98664d3 13638 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13639 sect_offset_str (die->sect_off));
e8d05480
JB
13640 return;
13641 }
13642
13643 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13644 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13645 <= PC_BOUNDS_INVALID)
e8d05480 13646 {
ae4d0c03 13647 attr = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 13648 if (attr == nullptr || !attr->as_boolean ())
b98664d3 13649 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13650 "for subprogram DIE at %s"),
13651 sect_offset_str (die->sect_off));
e8d05480
JB
13652 return;
13653 }
c906108c 13654
3e29f34a
MR
13655 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13656 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13657
34eaf542
TT
13658 /* If we have any template arguments, then we must allocate a
13659 different sort of symbol. */
436c571c 13660 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13661 {
13662 if (child_die->tag == DW_TAG_template_type_param
13663 || child_die->tag == DW_TAG_template_value_param)
13664 {
8c14c3a3 13665 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13666 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13667 break;
13668 }
13669 }
13670
702cf3f5 13671 gdb_assert (cu->get_builder () != nullptr);
c24bdb02 13672 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13673 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13674 (struct symbol *) templ_func);
4c2df51b 13675
81873cc8 13676 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13677 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13678 cu->language);
13679
4cecd739
DJ
13680 /* If there is a location expression for DW_AT_frame_base, record
13681 it. */
e142c38c 13682 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13683 if (attr != nullptr)
fe978cb0 13684 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13685
63e43d3a
PMR
13686 /* If there is a location for the static link, record it. */
13687 newobj->static_link = NULL;
13688 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13689 if (attr != nullptr)
63e43d3a 13690 {
224c3ddb
SM
13691 newobj->static_link
13692 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13693 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13694 cu->addr_type ());
63e43d3a
PMR
13695 }
13696
c24bdb02 13697 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13698
639d11d3 13699 if (die->child != NULL)
c906108c 13700 {
639d11d3 13701 child_die = die->child;
c906108c
SS
13702 while (child_die && child_die->tag)
13703 {
34eaf542
TT
13704 if (child_die->tag == DW_TAG_template_type_param
13705 || child_die->tag == DW_TAG_template_value_param)
13706 {
13707 struct symbol *arg = new_symbol (child_die, NULL, cu);
13708
f1078f66 13709 if (arg != NULL)
2f4732b0 13710 template_args.push_back (arg);
34eaf542
TT
13711 }
13712 else
13713 process_die (child_die, cu);
436c571c 13714 child_die = child_die->sibling;
c906108c
SS
13715 }
13716 }
13717
d389af10
JK
13718 inherit_abstract_dies (die, cu);
13719
4a811a97
UW
13720 /* If we have a DW_AT_specification, we might need to import using
13721 directives from the context of the specification DIE. See the
13722 comment in determine_prefix. */
13723 if (cu->language == language_cplus
13724 && dwarf2_attr (die, DW_AT_specification, cu))
13725 {
13726 struct dwarf2_cu *spec_cu = cu;
13727 struct die_info *spec_die = die_specification (die, &spec_cu);
13728
13729 while (spec_die)
13730 {
13731 child_die = spec_die->child;
13732 while (child_die && child_die->tag)
13733 {
13734 if (child_die->tag == DW_TAG_imported_module)
13735 process_die (child_die, spec_cu);
436c571c 13736 child_die = child_die->sibling;
4a811a97
UW
13737 }
13738
13739 /* In some cases, GCC generates specification DIEs that
13740 themselves contain DW_AT_specification attributes. */
13741 spec_die = die_specification (spec_die, &spec_cu);
13742 }
13743 }
13744
c24bdb02 13745 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13746 /* Make a block for the local symbols within. */
c24bdb02 13747 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13748 cstk.static_link, lowpc, highpc);
801e3a5b 13749
df8a16a1 13750 /* For C++, set the block's scope. */
45280282
IB
13751 if ((cu->language == language_cplus
13752 || cu->language == language_fortran
c44af4eb
TT
13753 || cu->language == language_d
13754 || cu->language == language_rust)
4d4ec4e5 13755 && cu->processing_has_namespace_info)
195a3f6c
TT
13756 block_set_scope (block, determine_prefix (die, cu),
13757 &objfile->objfile_obstack);
df8a16a1 13758
801e3a5b
JB
13759 /* If we have address ranges, record them. */
13760 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13761
a60f3166 13762 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13763
34eaf542 13764 /* Attach template arguments to function. */
2f4732b0 13765 if (!template_args.empty ())
34eaf542
TT
13766 {
13767 gdb_assert (templ_func != NULL);
13768
2f4732b0 13769 templ_func->n_template_arguments = template_args.size ();
34eaf542 13770 templ_func->template_arguments
dda83cd7 13771 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
8d749320 13772 templ_func->n_template_arguments);
34eaf542 13773 memcpy (templ_func->template_arguments,
2f4732b0 13774 template_args.data (),
34eaf542 13775 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13776
13777 /* Make sure that the symtab is set on the new symbols. Even
13778 though they don't appear in this symtab directly, other parts
13779 of gdb assume that symbols do, and this is reasonably
13780 true. */
8634679f 13781 for (symbol *sym : template_args)
3e1d3d8c 13782 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13783 }
13784
208d8187
JB
13785 /* In C++, we can have functions nested inside functions (e.g., when
13786 a function declares a class that has methods). This means that
13787 when we finish processing a function scope, we may need to go
13788 back to building a containing block's symbol lists. */
c24bdb02
KS
13789 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13790 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13791
921e78cf
JB
13792 /* If we've finished processing a top-level function, subsequent
13793 symbols go in the file symbol list. */
c24bdb02
KS
13794 if (cu->get_builder ()->outermost_context_p ())
13795 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13796}
13797
13798/* Process all the DIES contained within a lexical block scope. Start
13799 a new scope, process the dies, and then close the scope. */
13800
13801static void
e7c27a73 13802read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13803{
5e22e966 13804 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13805 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13806 CORE_ADDR lowpc, highpc;
13807 struct die_info *child_die;
e142c38c
DJ
13808 CORE_ADDR baseaddr;
13809
b3b3bada 13810 baseaddr = objfile->text_section_offset ();
c906108c
SS
13811
13812 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13813 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13814 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13815 be nasty. Might be easier to properly extend generic blocks to
af34e669 13816 describe ranges. */
e385593e
JK
13817 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13818 {
13819 case PC_BOUNDS_NOT_PRESENT:
13820 /* DW_TAG_lexical_block has no attributes, process its children as if
13821 there was no wrapping by that DW_TAG_lexical_block.
13822 GCC does no longer produces such DWARF since GCC r224161. */
13823 for (child_die = die->child;
13824 child_die != NULL && child_die->tag;
436c571c 13825 child_die = child_die->sibling)
4f7bc5ed
TT
13826 {
13827 /* We might already be processing this DIE. This can happen
13828 in an unusual circumstance -- where a subroutine A
13829 appears lexically in another subroutine B, but A actually
13830 inlines B. The recursion is broken here, rather than in
13831 inherit_abstract_dies, because it seems better to simply
13832 drop concrete children here. */
13833 if (!child_die->in_process)
13834 process_die (child_die, cu);
13835 }
e385593e
JK
13836 return;
13837 case PC_BOUNDS_INVALID:
13838 return;
13839 }
3e29f34a
MR
13840 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13841 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13842
c24bdb02 13843 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13844 if (die->child != NULL)
c906108c 13845 {
639d11d3 13846 child_die = die->child;
c906108c
SS
13847 while (child_die && child_die->tag)
13848 {
e7c27a73 13849 process_die (child_die, cu);
436c571c 13850 child_die = child_die->sibling;
c906108c
SS
13851 }
13852 }
3ea89b92 13853 inherit_abstract_dies (die, cu);
c24bdb02 13854 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13855
c24bdb02
KS
13856 if (*cu->get_builder ()->get_local_symbols () != NULL
13857 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13858 {
801e3a5b 13859 struct block *block
dda83cd7 13860 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13861 cstk.start_addr, highpc);
801e3a5b
JB
13862
13863 /* Note that recording ranges after traversing children, as we
dda83cd7
SM
13864 do here, means that recording a parent's ranges entails
13865 walking across all its children's ranges as they appear in
13866 the address map, which is quadratic behavior.
13867
13868 It would be nicer to record the parent's ranges before
13869 traversing its children, simply overriding whatever you find
13870 there. But since we don't even decide whether to create a
13871 block until after we've traversed its children, that's hard
13872 to do. */
801e3a5b 13873 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13874 }
c24bdb02
KS
13875 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13876 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13877}
13878
216f72a1 13879/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13880
13881static void
13882read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13883{
5e22e966 13884 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13885 struct objfile *objfile = per_objfile->objfile;
08feed99 13886 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13887 CORE_ADDR pc, baseaddr;
13888 struct attribute *attr;
13889 struct call_site *call_site, call_site_local;
13890 void **slot;
13891 int nparams;
13892 struct die_info *child_die;
13893
b3b3bada 13894 baseaddr = objfile->text_section_offset ();
96408a79 13895
216f72a1
JK
13896 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13897 if (attr == NULL)
13898 {
13899 /* This was a pre-DWARF-5 GNU extension alias
13900 for DW_AT_call_return_pc. */
13901 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13902 }
96408a79
SA
13903 if (!attr)
13904 {
b98664d3 13905 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13906 "DIE %s [in module %s]"),
13907 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13908 return;
13909 }
95f982e5 13910 pc = attr->as_address () + baseaddr;
3e29f34a 13911 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13912
13913 if (cu->call_site_htab == NULL)
13914 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13915 NULL, &objfile->objfile_obstack,
13916 hashtab_obstack_allocate, NULL);
13917 call_site_local.pc = pc;
13918 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13919 if (*slot != NULL)
13920 {
b98664d3 13921 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13922 "DIE %s [in module %s]"),
13923 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13924 objfile_name (objfile));
96408a79
SA
13925 return;
13926 }
13927
13928 /* Count parameters at the caller. */
13929
13930 nparams = 0;
13931 for (child_die = die->child; child_die && child_die->tag;
436c571c 13932 child_die = child_die->sibling)
96408a79 13933 {
216f72a1 13934 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13935 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13936 {
b98664d3 13937 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13938 "DW_TAG_call_site child DIE %s [in module %s]"),
13939 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13940 objfile_name (objfile));
96408a79
SA
13941 continue;
13942 }
13943
13944 nparams++;
13945 }
13946
224c3ddb
SM
13947 call_site
13948 = ((struct call_site *)
13949 obstack_alloc (&objfile->objfile_obstack,
13950 sizeof (*call_site)
13951 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13952 *slot = call_site;
13953 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13954 call_site->pc = pc;
13955
216f72a1
JK
13956 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13957 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13958 {
13959 struct die_info *func_die;
13960
13961 /* Skip also over DW_TAG_inlined_subroutine. */
13962 for (func_die = die->parent;
13963 func_die && func_die->tag != DW_TAG_subprogram
13964 && func_die->tag != DW_TAG_subroutine_type;
13965 func_die = func_die->parent);
13966
216f72a1
JK
13967 /* DW_AT_call_all_calls is a superset
13968 of DW_AT_call_all_tail_calls. */
96408a79 13969 if (func_die
dda83cd7
SM
13970 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13971 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13972 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13973 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13974 {
13975 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13976 not complete. But keep CALL_SITE for look ups via call_site_htab,
13977 both the initial caller containing the real return address PC and
13978 the final callee containing the current PC of a chain of tail
13979 calls do not need to have the tail call list complete. But any
13980 function candidate for a virtual tail call frame searched via
13981 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13982 determined unambiguously. */
13983 }
13984 else
13985 {
13986 struct type *func_type = NULL;
13987
13988 if (func_die)
13989 func_type = get_die_type (func_die, cu);
13990 if (func_type != NULL)
13991 {
78134374 13992 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13993
13994 /* Enlist this call site to the function. */
13995 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13996 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13997 }
13998 else
b98664d3 13999 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14000 "DIE %s [in module %s]"),
14001 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14002 }
14003 }
14004
216f72a1
JK
14005 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14006 if (attr == NULL)
14007 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14008 if (attr == NULL)
14009 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14010 if (attr == NULL)
216f72a1
JK
14011 {
14012 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14013 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14014 }
96408a79 14015 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9d2246fc 14016 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
96408a79 14017 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 14018 else if (attr->form_is_block ())
96408a79
SA
14019 {
14020 struct dwarf2_locexpr_baton *dlbaton;
9d2246fc 14021 struct dwarf_block *block = attr->as_block ();
96408a79 14022
8d749320 14023 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9d2246fc
TT
14024 dlbaton->data = block->data;
14025 dlbaton->size = block->size;
a50264ba 14026 dlbaton->per_objfile = per_objfile;
96408a79
SA
14027 dlbaton->per_cu = cu->per_cu;
14028
14029 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14030 }
cd6c91b4 14031 else if (attr->form_is_ref ())
96408a79 14032 {
96408a79
SA
14033 struct dwarf2_cu *target_cu = cu;
14034 struct die_info *target_die;
14035
ac9ec31b 14036 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 14037 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
14038 if (die_is_declaration (target_die, target_cu))
14039 {
7d45c7c3 14040 const char *target_physname;
9112db09
JK
14041
14042 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14043 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14044 if (target_physname == NULL)
9112db09 14045 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14046 if (target_physname == NULL)
b98664d3 14047 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 14048 "physname, for referencing DIE %s [in module %s]"),
9d8780f0 14049 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14050 else
7d455152 14051 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14052 }
14053 else
14054 {
14055 CORE_ADDR lowpc;
14056
14057 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14058 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14059 <= PC_BOUNDS_INVALID)
b98664d3 14060 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 14061 "low pc, for referencing DIE %s [in module %s]"),
9d8780f0 14062 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14063 else
3e29f34a
MR
14064 {
14065 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14066 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14067 }
96408a79
SA
14068 }
14069 }
14070 else
b98664d3 14071 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14072 "block nor reference, for DIE %s [in module %s]"),
14073 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14074
14075 call_site->per_cu = cu->per_cu;
9f47c707 14076 call_site->per_objfile = per_objfile;
96408a79
SA
14077
14078 for (child_die = die->child;
14079 child_die && child_die->tag;
436c571c 14080 child_die = child_die->sibling)
96408a79 14081 {
96408a79 14082 struct call_site_parameter *parameter;
1788b2d3 14083 struct attribute *loc, *origin;
96408a79 14084
216f72a1 14085 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 14086 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14087 {
14088 /* Already printed the complaint above. */
14089 continue;
14090 }
14091
14092 gdb_assert (call_site->parameter_count < nparams);
14093 parameter = &call_site->parameter[call_site->parameter_count];
14094
1788b2d3
JK
14095 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14096 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14097 register is contained in DW_AT_call_value. */
96408a79 14098
24c5c679 14099 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14100 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14101 if (origin == NULL)
14102 {
14103 /* This was a pre-DWARF-5 GNU extension alias
14104 for DW_AT_call_parameter. */
14105 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14106 }
cd6c91b4 14107 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 14108 {
1788b2d3 14109 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 14110
0826b30a 14111 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 14112 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
14113 {
14114 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14115 binding can be done only inside one CU. Such referenced DIE
14116 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14117 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14118 "DW_TAG_call_site child DIE %s [in module %s]"),
14119 sect_offset_str (child_die->sect_off),
9c541725 14120 objfile_name (objfile));
d76b7dbc
JK
14121 continue;
14122 }
9c541725
PA
14123 parameter->u.param_cu_off
14124 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 14125 }
4fc6c0d5 14126 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 14127 {
b98664d3 14128 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14129 "DW_TAG_call_site child DIE %s [in module %s]"),
14130 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14131 continue;
14132 }
24c5c679 14133 else
96408a79 14134 {
9d2246fc
TT
14135 struct dwarf_block *block = loc->as_block ();
14136
24c5c679 14137 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9d2246fc 14138 (block->data, &block->data[block->size]);
24c5c679
JK
14139 if (parameter->u.dwarf_reg != -1)
14140 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9d2246fc
TT
14141 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14142 &block->data[block->size],
24c5c679
JK
14143 &parameter->u.fb_offset))
14144 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14145 else
14146 {
b98664d3 14147 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14148 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14149 "DW_TAG_call_site child DIE %s "
24c5c679 14150 "[in module %s]"),
9d8780f0 14151 sect_offset_str (child_die->sect_off),
9c541725 14152 objfile_name (objfile));
24c5c679
JK
14153 continue;
14154 }
96408a79
SA
14155 }
14156
216f72a1
JK
14157 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14158 if (attr == NULL)
14159 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 14160 if (attr == NULL || !attr->form_is_block ())
96408a79 14161 {
b98664d3 14162 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14163 "DW_TAG_call_site child DIE %s [in module %s]"),
14164 sect_offset_str (child_die->sect_off),
9c541725 14165 objfile_name (objfile));
96408a79
SA
14166 continue;
14167 }
9d2246fc
TT
14168
14169 struct dwarf_block *block = attr->as_block ();
14170 parameter->value = block->data;
14171 parameter->value_size = block->size;
96408a79
SA
14172
14173 /* Parameters are not pre-cleared by memset above. */
14174 parameter->data_value = NULL;
14175 parameter->data_value_size = 0;
14176 call_site->parameter_count++;
14177
216f72a1
JK
14178 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14179 if (attr == NULL)
14180 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14181 if (attr != nullptr)
96408a79 14182 {
4fc6c0d5 14183 if (!attr->form_is_block ())
b98664d3 14184 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14185 "DW_TAG_call_site child DIE %s [in module %s]"),
14186 sect_offset_str (child_die->sect_off),
9c541725 14187 objfile_name (objfile));
96408a79
SA
14188 else
14189 {
9d2246fc
TT
14190 block = attr->as_block ();
14191 parameter->data_value = block->data;
14192 parameter->data_value_size = block->size;
96408a79
SA
14193 }
14194 }
14195 }
14196}
14197
71a3c369
TT
14198/* Helper function for read_variable. If DIE represents a virtual
14199 table, then return the type of the concrete object that is
14200 associated with the virtual table. Otherwise, return NULL. */
14201
14202static struct type *
14203rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14204{
14205 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14206 if (attr == NULL)
14207 return NULL;
14208
14209 /* Find the type DIE. */
14210 struct die_info *type_die = NULL;
14211 struct dwarf2_cu *type_cu = cu;
14212
cd6c91b4 14213 if (attr->form_is_ref ())
71a3c369
TT
14214 type_die = follow_die_ref (die, attr, &type_cu);
14215 if (type_die == NULL)
14216 return NULL;
14217
14218 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14219 return NULL;
14220 return die_containing_type (type_die, type_cu);
14221}
14222
14223/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14224
14225static void
14226read_variable (struct die_info *die, struct dwarf2_cu *cu)
14227{
14228 struct rust_vtable_symbol *storage = NULL;
14229
14230 if (cu->language == language_rust)
14231 {
14232 struct type *containing_type = rust_containing_type (die, cu);
14233
14234 if (containing_type != NULL)
14235 {
5e22e966 14236 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 14237
8c14c3a3 14238 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 14239 storage->concrete_type = containing_type;
cf724bc9 14240 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14241 }
14242 }
14243
e4a62c65
TV
14244 struct symbol *res = new_symbol (die, NULL, cu, storage);
14245 struct attribute *abstract_origin
14246 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14247 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14248 if (res == NULL && loc && abstract_origin)
14249 {
14250 /* We have a variable without a name, but with a location and an abstract
14251 origin. This may be a concrete instance of an abstract variable
14252 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14253 later. */
14254 struct dwarf2_cu *origin_cu = cu;
14255 struct die_info *origin_die
14256 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
14257 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14258 per_objfile->per_bfd->abstract_to_concrete
14259 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14260 }
71a3c369
TT
14261}
14262
43988095
JK
14263/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14264 reading .debug_rnglists.
14265 Callback's type should be:
14266 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14267 Return true if the attributes are present and valid, otherwise,
14268 return false. */
14269
14270template <typename Callback>
14271static bool
14272dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
d0ce17d8 14273 dwarf_tag tag, Callback &&callback)
43988095 14274{
976ca316
SM
14275 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14276 struct objfile *objfile = per_objfile->objfile;
43988095 14277 bfd *obfd = objfile->obfd;
43988095 14278 /* Base address selection entry. */
2b24b6e4 14279 gdb::optional<CORE_ADDR> base;
43988095 14280 const gdb_byte *buffer;
43988095
JK
14281 CORE_ADDR baseaddr;
14282 bool overflow = false;
d0ce17d8
CT
14283 ULONGEST addr_index;
14284 struct dwarf2_section_info *rnglists_section;
43988095 14285
43988095 14286 base = cu->base_address;
d0ce17d8
CT
14287 rnglists_section = cu_debug_rnglists_section (cu, tag);
14288 rnglists_section->read (objfile);
43988095 14289
d0ce17d8 14290 if (offset >= rnglists_section->size)
43988095 14291 {
b98664d3 14292 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14293 offset);
14294 return false;
14295 }
d0ce17d8 14296 buffer = rnglists_section->buffer + offset;
43988095 14297
b3b3bada 14298 baseaddr = objfile->text_section_offset ();
43988095
JK
14299
14300 while (1)
14301 {
7814882a
JK
14302 /* Initialize it due to a false compiler warning. */
14303 CORE_ADDR range_beginning = 0, range_end = 0;
d0ce17d8
CT
14304 const gdb_byte *buf_end = (rnglists_section->buffer
14305 + rnglists_section->size);
43988095
JK
14306 unsigned int bytes_read;
14307
14308 if (buffer == buf_end)
14309 {
14310 overflow = true;
14311 break;
14312 }
14313 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14314 switch (rlet)
14315 {
14316 case DW_RLE_end_of_list:
14317 break;
14318 case DW_RLE_base_address:
14319 if (buffer + cu->header.addr_size > buf_end)
14320 {
14321 overflow = true;
14322 break;
14323 }
c8a7a66f 14324 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14325 buffer += bytes_read;
14326 break;
dda83cd7
SM
14327 case DW_RLE_base_addressx:
14328 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14329 buffer += bytes_read;
14330 base = read_addr_index (cu, addr_index);
14331 break;
43988095
JK
14332 case DW_RLE_start_length:
14333 if (buffer + cu->header.addr_size > buf_end)
14334 {
14335 overflow = true;
14336 break;
14337 }
c8a7a66f
TT
14338 range_beginning = cu->header.read_address (obfd, buffer,
14339 &bytes_read);
43988095
JK
14340 buffer += bytes_read;
14341 range_end = (range_beginning
14342 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14343 buffer += bytes_read;
14344 if (buffer > buf_end)
14345 {
14346 overflow = true;
14347 break;
14348 }
14349 break;
d0ce17d8 14350 case DW_RLE_startx_length:
dda83cd7
SM
14351 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14352 buffer += bytes_read;
14353 range_beginning = read_addr_index (cu, addr_index);
14354 if (buffer > buf_end)
14355 {
14356 overflow = true;
14357 break;
14358 }
14359 range_end = (range_beginning
14360 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14361 buffer += bytes_read;
14362 break;
43988095
JK
14363 case DW_RLE_offset_pair:
14364 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14365 buffer += bytes_read;
14366 if (buffer > buf_end)
14367 {
14368 overflow = true;
14369 break;
14370 }
14371 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14372 buffer += bytes_read;
14373 if (buffer > buf_end)
14374 {
14375 overflow = true;
14376 break;
14377 }
14378 break;
14379 case DW_RLE_start_end:
14380 if (buffer + 2 * cu->header.addr_size > buf_end)
14381 {
14382 overflow = true;
14383 break;
14384 }
c8a7a66f
TT
14385 range_beginning = cu->header.read_address (obfd, buffer,
14386 &bytes_read);
43988095 14387 buffer += bytes_read;
c8a7a66f 14388 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14389 buffer += bytes_read;
14390 break;
d0ce17d8 14391 case DW_RLE_startx_endx:
dda83cd7
SM
14392 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14393 buffer += bytes_read;
14394 range_beginning = read_addr_index (cu, addr_index);
14395 if (buffer > buf_end)
14396 {
14397 overflow = true;
14398 break;
14399 }
14400 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14401 buffer += bytes_read;
14402 range_end = read_addr_index (cu, addr_index);
14403 break;
43988095 14404 default:
b98664d3 14405 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14406 return false;
14407 }
14408 if (rlet == DW_RLE_end_of_list || overflow)
14409 break;
14410 if (rlet == DW_RLE_base_address)
14411 continue;
14412
43988095
JK
14413 if (range_beginning > range_end)
14414 {
14415 /* Inverted range entries are invalid. */
b98664d3 14416 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14417 return false;
14418 }
14419
14420 /* Empty range entries have no effect. */
14421 if (range_beginning == range_end)
14422 continue;
14423
d0ce17d8
CT
14424 /* Only DW_RLE_offset_pair needs the base address added. */
14425 if (rlet == DW_RLE_offset_pair)
14426 {
14427 if (!base.has_value ())
14428 {
14429 /* We have no valid base address for the DW_RLE_offset_pair. */
14430 complaint (_("Invalid .debug_rnglists data (no base address for "
14431 "DW_RLE_offset_pair)"));
14432 return false;
14433 }
14434
14435 range_beginning += *base;
14436 range_end += *base;
14437 }
43988095
JK
14438
14439 /* A not-uncommon case of bad debug info.
14440 Don't pollute the addrmap with bad data. */
14441 if (range_beginning + baseaddr == 0
976ca316 14442 && !per_objfile->per_bfd->has_section_at_zero)
43988095 14443 {
b98664d3 14444 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14445 " [in module %s]"), objfile_name (objfile));
14446 continue;
14447 }
14448
14449 callback (range_beginning, range_end);
14450 }
14451
14452 if (overflow)
14453 {
b98664d3 14454 complaint (_("Offset %d is not terminated "
43988095
JK
14455 "for DW_AT_ranges attribute"),
14456 offset);
14457 return false;
14458 }
14459
14460 return true;
14461}
14462
14463/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14464 Callback's type should be:
14465 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14466 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14467
43988095 14468template <typename Callback>
43039443 14469static int
d0ce17d8 14470dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
43988095 14471 Callback &&callback)
43039443 14472{
5e22e966
SM
14473 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14474 struct objfile *objfile = per_objfile->objfile;
43039443
JK
14475 struct comp_unit_head *cu_header = &cu->header;
14476 bfd *obfd = objfile->obfd;
14477 unsigned int addr_size = cu_header->addr_size;
14478 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14479 /* Base address selection entry. */
2b24b6e4 14480 gdb::optional<CORE_ADDR> base;
43039443 14481 unsigned int dummy;
d521ce57 14482 const gdb_byte *buffer;
ff013f42 14483 CORE_ADDR baseaddr;
43039443 14484
43988095 14485 if (cu_header->version >= 5)
d0ce17d8 14486 return dwarf2_rnglists_process (offset, cu, tag, callback);
43988095 14487
d00adf39 14488 base = cu->base_address;
43039443 14489
5e22e966
SM
14490 per_objfile->per_bfd->ranges.read (objfile);
14491 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 14492 {
b98664d3 14493 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14494 offset);
14495 return 0;
14496 }
5e22e966 14497 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 14498
b3b3bada 14499 baseaddr = objfile->text_section_offset ();
ff013f42 14500
43039443
JK
14501 while (1)
14502 {
14503 CORE_ADDR range_beginning, range_end;
14504
c8a7a66f 14505 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 14506 buffer += addr_size;
c8a7a66f 14507 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
14508 buffer += addr_size;
14509 offset += 2 * addr_size;
14510
14511 /* An end of list marker is a pair of zero addresses. */
14512 if (range_beginning == 0 && range_end == 0)
14513 /* Found the end of list entry. */
14514 break;
14515
14516 /* Each base address selection entry is a pair of 2 values.
14517 The first is the largest possible address, the second is
14518 the base address. Check for a base address here. */
14519 if ((range_beginning & mask) == mask)
14520 {
28d2bfb9
AB
14521 /* If we found the largest possible address, then we already
14522 have the base address in range_end. */
14523 base = range_end;
43039443
JK
14524 continue;
14525 }
14526
2b24b6e4 14527 if (!base.has_value ())
43039443
JK
14528 {
14529 /* We have no valid base address for the ranges
14530 data. */
b98664d3 14531 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14532 return 0;
14533 }
14534
9277c30c
UW
14535 if (range_beginning > range_end)
14536 {
14537 /* Inverted range entries are invalid. */
b98664d3 14538 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14539 return 0;
14540 }
14541
14542 /* Empty range entries have no effect. */
14543 if (range_beginning == range_end)
14544 continue;
14545
2b24b6e4
TT
14546 range_beginning += *base;
14547 range_end += *base;
43039443 14548
01093045
DE
14549 /* A not-uncommon case of bad debug info.
14550 Don't pollute the addrmap with bad data. */
14551 if (range_beginning + baseaddr == 0
5e22e966 14552 && !per_objfile->per_bfd->has_section_at_zero)
01093045 14553 {
b98664d3 14554 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14555 " [in module %s]"), objfile_name (objfile));
01093045
DE
14556 continue;
14557 }
14558
5f46c5a5
JK
14559 callback (range_beginning, range_end);
14560 }
14561
14562 return 1;
14563}
14564
14565/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14566 Return 1 if the attributes are present and valid, otherwise, return 0.
efd7398e 14567 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
5f46c5a5
JK
14568
14569static int
14570dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14571 CORE_ADDR *high_return, struct dwarf2_cu *cu,
d0ce17d8 14572 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
5f46c5a5 14573{
5e22e966 14574 struct objfile *objfile = cu->per_objfile->objfile;
84685904 14575 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 14576 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14577 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14578 int low_set = 0;
14579 CORE_ADDR low = 0;
14580 CORE_ADDR high = 0;
14581 int retval;
14582
d0ce17d8 14583 retval = dwarf2_ranges_process (offset, cu, tag,
5f46c5a5
JK
14584 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14585 {
9277c30c 14586 if (ranges_pst != NULL)
3e29f34a
MR
14587 {
14588 CORE_ADDR lowpc;
14589 CORE_ADDR highpc;
14590
79748972
TT
14591 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14592 range_beginning + baseaddr)
14593 - baseaddr);
14594 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14595 range_end + baseaddr)
14596 - baseaddr);
84685904 14597 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 14598 lowpc, highpc - 1, ranges_pst);
3e29f34a 14599 }
ff013f42 14600
43039443
JK
14601 /* FIXME: This is recording everything as a low-high
14602 segment of consecutive addresses. We should have a
14603 data structure for discontiguous block ranges
14604 instead. */
14605 if (! low_set)
14606 {
14607 low = range_beginning;
14608 high = range_end;
14609 low_set = 1;
14610 }
14611 else
14612 {
14613 if (range_beginning < low)
14614 low = range_beginning;
14615 if (range_end > high)
14616 high = range_end;
14617 }
5f46c5a5
JK
14618 });
14619 if (!retval)
14620 return 0;
43039443
JK
14621
14622 if (! low_set)
14623 /* If the first entry is an end-of-list marker, the range
14624 describes an empty scope, i.e. no instructions. */
14625 return 0;
14626
14627 if (low_return)
14628 *low_return = low;
14629 if (high_return)
14630 *high_return = high;
14631 return 1;
14632}
14633
3a2b436a
JK
14634/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14635 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14636 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14637
3a2b436a 14638static enum pc_bounds_kind
af34e669 14639dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14640 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14641 dwarf2_psymtab *pst)
c906108c 14642{
976ca316 14643 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14644 struct attribute *attr;
91da1414 14645 struct attribute *attr_high;
af34e669
DJ
14646 CORE_ADDR low = 0;
14647 CORE_ADDR high = 0;
e385593e 14648 enum pc_bounds_kind ret;
c906108c 14649
91da1414
MW
14650 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14651 if (attr_high)
af34e669 14652 {
e142c38c 14653 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14654 if (attr != nullptr)
dda83cd7 14655 {
95f982e5
TT
14656 low = attr->as_address ();
14657 high = attr_high->as_address ();
cd6c91b4 14658 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14659 high += low;
91da1414 14660 }
af34e669
DJ
14661 else
14662 /* Found high w/o low attribute. */
e385593e 14663 return PC_BOUNDS_INVALID;
af34e669
DJ
14664
14665 /* Found consecutive range of addresses. */
3a2b436a 14666 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14667 }
c906108c 14668 else
af34e669 14669 {
e142c38c 14670 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14671 if (attr != nullptr && attr->form_is_unsigned ())
af34e669 14672 {
2b0c7f41
SM
14673 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14674 on DWARF version). */
14675 ULONGEST ranges_offset = attr->as_unsigned ();
14676
14677 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14678 this value. */
14679 if (die->tag != DW_TAG_compile_unit)
14680 ranges_offset += cu->gnu_ranges_base;
2e3cf129 14681
af34e669 14682 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14683 .debug_ranges section. */
d0ce17d8
CT
14684 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14685 die->tag))
e385593e 14686 return PC_BOUNDS_INVALID;
43039443 14687 /* Found discontinuous range of addresses. */
3a2b436a 14688 ret = PC_BOUNDS_RANGES;
af34e669 14689 }
e385593e
JK
14690 else
14691 return PC_BOUNDS_NOT_PRESENT;
af34e669 14692 }
c906108c 14693
48fbe735 14694 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14695 if (high <= low)
e385593e 14696 return PC_BOUNDS_INVALID;
c906108c
SS
14697
14698 /* When using the GNU linker, .gnu.linkonce. sections are used to
14699 eliminate duplicate copies of functions and vtables and such.
14700 The linker will arbitrarily choose one and discard the others.
14701 The AT_*_pc values for such functions refer to local labels in
14702 these sections. If the section from that file was discarded, the
14703 labels are not in the output, so the relocs get a value of 0.
14704 If this is a discarded function, mark the pc bounds as invalid,
14705 so that GDB will ignore it. */
976ca316 14706 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14707 return PC_BOUNDS_INVALID;
c906108c
SS
14708
14709 *lowpc = low;
96408a79
SA
14710 if (highpc)
14711 *highpc = high;
af34e669 14712 return ret;
c906108c
SS
14713}
14714
b084d499
JB
14715/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14716 its low and high PC addresses. Do nothing if these addresses could not
14717 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14718 and HIGHPC to the high address if greater than HIGHPC. */
14719
14720static void
14721dwarf2_get_subprogram_pc_bounds (struct die_info *die,
dda83cd7
SM
14722 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14723 struct dwarf2_cu *cu)
b084d499
JB
14724{
14725 CORE_ADDR low, high;
14726 struct die_info *child = die->child;
14727
e385593e 14728 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14729 {
325fac50
PA
14730 *lowpc = std::min (*lowpc, low);
14731 *highpc = std::max (*highpc, high);
b084d499
JB
14732 }
14733
14734 /* If the language does not allow nested subprograms (either inside
14735 subprograms or lexical blocks), we're done. */
14736 if (cu->language != language_ada)
14737 return;
6e70227d 14738
b084d499
JB
14739 /* Check all the children of the given DIE. If it contains nested
14740 subprograms, then check their pc bounds. Likewise, we need to
14741 check lexical blocks as well, as they may also contain subprogram
14742 definitions. */
14743 while (child && child->tag)
14744 {
14745 if (child->tag == DW_TAG_subprogram
dda83cd7
SM
14746 || child->tag == DW_TAG_lexical_block)
14747 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14748 child = child->sibling;
b084d499
JB
14749 }
14750}
14751
fae299cd
DC
14752/* Get the low and high pc's represented by the scope DIE, and store
14753 them in *LOWPC and *HIGHPC. If the correct values can't be
14754 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14755
14756static void
14757get_scope_pc_bounds (struct die_info *die,
14758 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14759 struct dwarf2_cu *cu)
14760{
14761 CORE_ADDR best_low = (CORE_ADDR) -1;
14762 CORE_ADDR best_high = (CORE_ADDR) 0;
14763 CORE_ADDR current_low, current_high;
14764
3a2b436a 14765 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14766 >= PC_BOUNDS_RANGES)
fae299cd
DC
14767 {
14768 best_low = current_low;
14769 best_high = current_high;
14770 }
14771 else
14772 {
14773 struct die_info *child = die->child;
14774
14775 while (child && child->tag)
14776 {
14777 switch (child->tag) {
14778 case DW_TAG_subprogram:
dda83cd7 14779 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14780 break;
14781 case DW_TAG_namespace:
f55ee35c 14782 case DW_TAG_module:
fae299cd
DC
14783 /* FIXME: carlton/2004-01-16: Should we do this for
14784 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14785 that current GCC's always emit the DIEs corresponding
14786 to definitions of methods of classes as children of a
14787 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14788 the DIEs giving the declarations, which could be
14789 anywhere). But I don't see any reason why the
14790 standards says that they have to be there. */
14791 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14792
14793 if (current_low != ((CORE_ADDR) -1))
14794 {
325fac50
PA
14795 best_low = std::min (best_low, current_low);
14796 best_high = std::max (best_high, current_high);
fae299cd
DC
14797 }
14798 break;
14799 default:
0963b4bd 14800 /* Ignore. */
fae299cd
DC
14801 break;
14802 }
14803
436c571c 14804 child = child->sibling;
fae299cd
DC
14805 }
14806 }
14807
14808 *lowpc = best_low;
14809 *highpc = best_high;
14810}
14811
801e3a5b
JB
14812/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14813 in DIE. */
380bca97 14814
801e3a5b
JB
14815static void
14816dwarf2_record_block_ranges (struct die_info *die, struct block *block,
dda83cd7 14817 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
801e3a5b 14818{
5e22e966 14819 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14820 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14821 struct attribute *attr;
91da1414 14822 struct attribute *attr_high;
801e3a5b 14823
91da1414
MW
14824 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14825 if (attr_high)
801e3a5b 14826 {
801e3a5b 14827 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14828 if (attr != nullptr)
dda83cd7 14829 {
95f982e5
TT
14830 CORE_ADDR low = attr->as_address ();
14831 CORE_ADDR high = attr_high->as_address ();
31aa7e4e 14832
cd6c91b4 14833 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14834 high += low;
9a619af0 14835
3e29f34a
MR
14836 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14837 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14838 cu->get_builder ()->record_block_range (block, low, high - 1);
dda83cd7 14839 }
801e3a5b
JB
14840 }
14841
14842 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14843 if (attr != nullptr && attr->form_is_unsigned ())
801e3a5b 14844 {
2b0c7f41
SM
14845 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14846 on DWARF version). */
14847 ULONGEST ranges_offset = attr->as_unsigned ();
801e3a5b 14848
2b0c7f41
SM
14849 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14850 this value. */
14851 if (die->tag != DW_TAG_compile_unit)
14852 ranges_offset += cu->gnu_ranges_base;
801e3a5b 14853
2d5f09ec 14854 std::vector<blockrange> blockvec;
2b0c7f41 14855 dwarf2_ranges_process (ranges_offset, cu, die->tag,
5f46c5a5
JK
14856 [&] (CORE_ADDR start, CORE_ADDR end)
14857 {
58fdfd2c
JK
14858 start += baseaddr;
14859 end += baseaddr;
5f46c5a5
JK
14860 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14861 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14862 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14863 blockvec.emplace_back (start, end);
5f46c5a5 14864 });
2d5f09ec
KB
14865
14866 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14867 }
14868}
14869
685b1105
JK
14870/* Check whether the producer field indicates either of GCC < 4.6, or the
14871 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14872
685b1105
JK
14873static void
14874check_producer (struct dwarf2_cu *cu)
60d5a603 14875{
38360086 14876 int major, minor;
60d5a603
JK
14877
14878 if (cu->producer == NULL)
14879 {
14880 /* For unknown compilers expect their behavior is DWARF version
14881 compliant.
14882
14883 GCC started to support .debug_types sections by -gdwarf-4 since
14884 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14885 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14886 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14887 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14888 }
b1ffba5a 14889 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14890 {
38360086
MW
14891 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14892 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14893 }
5230b05a 14894 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14895 {
14896 cu->producer_is_icc = true;
14897 cu->producer_is_icc_lt_14 = major < 14;
14898 }
c258c396
JD
14899 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14900 cu->producer_is_codewarrior = true;
685b1105
JK
14901 else
14902 {
14903 /* For other non-GCC compilers, expect their behavior is DWARF version
14904 compliant. */
60d5a603
JK
14905 }
14906
9068261f 14907 cu->checked_producer = true;
685b1105 14908}
ba919b58 14909
685b1105
JK
14910/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14911 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14912 during 4.6.0 experimental. */
14913
9068261f 14914static bool
685b1105
JK
14915producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14916{
14917 if (!cu->checked_producer)
14918 check_producer (cu);
14919
14920 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14921}
14922
c258c396
JD
14923
14924/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14925 with incorrect is_stmt attributes. */
14926
14927static bool
14928producer_is_codewarrior (struct dwarf2_cu *cu)
14929{
14930 if (!cu->checked_producer)
14931 check_producer (cu);
14932
14933 return cu->producer_is_codewarrior;
14934}
14935
bf23a268
TT
14936/* Return the accessibility of DIE, as given by DW_AT_accessibility.
14937 If that attribute is not available, return the appropriate
14938 default. */
60d5a603
JK
14939
14940static enum dwarf_access_attribute
bf23a268 14941dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
60d5a603 14942{
bf23a268
TT
14943 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14944 if (attr != nullptr)
14945 {
14946 LONGEST value = attr->constant_value (-1);
14947 if (value == DW_ACCESS_public
14948 || value == DW_ACCESS_protected
14949 || value == DW_ACCESS_private)
14950 return (dwarf_access_attribute) value;
14951 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14952 plongest (value));
14953 }
14954
60d5a603
JK
14955 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14956 {
14957 /* The default DWARF 2 accessibility for members is public, the default
14958 accessibility for inheritance is private. */
14959
14960 if (die->tag != DW_TAG_inheritance)
14961 return DW_ACCESS_public;
14962 else
14963 return DW_ACCESS_private;
14964 }
14965 else
14966 {
14967 /* DWARF 3+ defines the default accessibility a different way. The same
14968 rules apply now for DW_TAG_inheritance as for the members and it only
14969 depends on the container kind. */
14970
14971 if (die->parent->tag == DW_TAG_class_type)
14972 return DW_ACCESS_private;
14973 else
14974 return DW_ACCESS_public;
14975 }
14976}
14977
74ac6d43
TT
14978/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14979 offset. If the attribute was not found return 0, otherwise return
14980 1. If it was found but could not properly be handled, set *OFFSET
14981 to 0. */
14982
14983static int
14984handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14985 LONGEST *offset)
14986{
14987 struct attribute *attr;
14988
14989 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14990 if (attr != NULL)
14991 {
14992 *offset = 0;
14993
14994 /* Note that we do not check for a section offset first here.
14995 This is because DW_AT_data_member_location is new in DWARF 4,
14996 so if we see it, we can assume that a constant form is really
14997 a constant and not a section offset. */
cd6c91b4 14998 if (attr->form_is_constant ())
0826b30a 14999 *offset = attr->constant_value (0);
cd6c91b4 15000 else if (attr->form_is_section_offset ())
74ac6d43 15001 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 15002 else if (attr->form_is_block ())
9d2246fc 15003 *offset = decode_locdesc (attr->as_block (), cu);
74ac6d43
TT
15004 else
15005 dwarf2_complex_location_expr_complaint ();
15006
15007 return 1;
15008 }
15009
15010 return 0;
15011}
15012
7d79de9a
TT
15013/* Look for DW_AT_data_member_location and store the results in FIELD. */
15014
15015static void
15016handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15017 struct field *field)
15018{
15019 struct attribute *attr;
15020
15021 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15022 if (attr != NULL)
15023 {
15024 if (attr->form_is_constant ())
15025 {
15026 LONGEST offset = attr->constant_value (0);
15027 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15028 }
15029 else if (attr->form_is_section_offset ())
15030 dwarf2_complex_location_expr_complaint ();
15031 else if (attr->form_is_block ())
15032 {
15033 bool handled;
9d2246fc 15034 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
7d79de9a
TT
15035 if (handled)
15036 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15037 else
15038 {
5e22e966
SM
15039 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15040 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
15041 struct dwarf2_locexpr_baton *dlbaton
15042 = XOBNEW (&objfile->objfile_obstack,
15043 struct dwarf2_locexpr_baton);
9d2246fc
TT
15044 dlbaton->data = attr->as_block ()->data;
15045 dlbaton->size = attr->as_block ()->size;
7d79de9a
TT
15046 /* When using this baton, we want to compute the address
15047 of the field, not the value. This is why
15048 is_reference is set to false here. */
15049 dlbaton->is_reference = false;
5e22e966 15050 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
15051 dlbaton->per_cu = cu->per_cu;
15052
15053 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
15054 }
15055 }
15056 else
15057 dwarf2_complex_location_expr_complaint ();
15058 }
15059}
15060
c906108c
SS
15061/* Add an aggregate field to the field list. */
15062
15063static void
107d2387 15064dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15065 struct dwarf2_cu *cu)
6e70227d 15066{
5e22e966 15067 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 15068 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
15069 struct nextfield *new_field;
15070 struct attribute *attr;
15071 struct field *fp;
15d034d0 15072 const char *fieldname = "";
c906108c 15073
7d0ccb61
DJ
15074 if (die->tag == DW_TAG_inheritance)
15075 {
be2daae6
TT
15076 fip->baseclasses.emplace_back ();
15077 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15078 }
15079 else
15080 {
be2daae6
TT
15081 fip->fields.emplace_back ();
15082 new_field = &fip->fields.back ();
7d0ccb61 15083 }
be2daae6 15084
9c6a1327
TT
15085 new_field->offset = die->sect_off;
15086
bf23a268 15087 new_field->accessibility = dwarf2_access_attribute (die, cu);
c906108c 15088 if (new_field->accessibility != DW_ACCESS_public)
264fc0e2 15089 fip->non_public_fields = true;
60d5a603 15090
e142c38c 15091 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 15092 if (attr != nullptr)
23dca5c3 15093 new_field->virtuality = attr->as_virtuality ();
60d5a603
JK
15094 else
15095 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15096
15097 fp = &new_field->field;
a9a9bd0f 15098
e142c38c 15099 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15100 {
a9a9bd0f 15101 /* Data member other than a C++ static data member. */
6e70227d 15102
c906108c 15103 /* Get type of field. */
5d14b6e5 15104 fp->set_type (die_type (die, cu));
c906108c 15105
d6a843b5 15106 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15107
c906108c 15108 /* Get bit size of field (zero if none). */
e142c38c 15109 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 15110 if (attr != nullptr)
c906108c 15111 {
529908cb 15112 FIELD_BITSIZE (*fp) = attr->constant_value (0);
c906108c
SS
15113 }
15114 else
15115 {
15116 FIELD_BITSIZE (*fp) = 0;
15117 }
15118
15119 /* Get bit offset of field. */
7d79de9a 15120 handle_data_member_location (die, cu, fp);
e142c38c 15121 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7c184d33 15122 if (attr != nullptr && attr->form_is_constant ())
c906108c 15123 {
d5a22e77 15124 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
15125 {
15126 /* For big endian bits, the DW_AT_bit_offset gives the
dda83cd7
SM
15127 additional bit offset from the MSB of the containing
15128 anonymous object to the MSB of the field. We don't
15129 have to do anything special since we don't need to
15130 know the size of the anonymous object. */
529908cb 15131 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
7c184d33 15132 + attr->constant_value (0)));
c906108c
SS
15133 }
15134 else
15135 {
15136 /* For little endian bits, compute the bit offset to the
dda83cd7
SM
15137 MSB of the anonymous object, subtract off the number of
15138 bits from the MSB of the field to the MSB of the
15139 object, and then subtract off the number of bits of
15140 the field itself. The result is the bit offset of
15141 the LSB of the field. */
c906108c 15142 int anonymous_size;
7c184d33 15143 int bit_offset = attr->constant_value (0);
c906108c 15144
e142c38c 15145 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7c184d33 15146 if (attr != nullptr && attr->form_is_constant ())
c906108c
SS
15147 {
15148 /* The size of the anonymous object containing
15149 the bit field is explicit, so use the
15150 indicated size (in bytes). */
7c184d33 15151 anonymous_size = attr->constant_value (0);
c906108c
SS
15152 }
15153 else
15154 {
15155 /* The size of the anonymous object containing
15156 the bit field must be inferred from the type
15157 attribute of the data member containing the
15158 bit field. */
5d14b6e5 15159 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 15160 }
f41f5e61
PA
15161 SET_FIELD_BITPOS (*fp,
15162 (FIELD_BITPOS (*fp)
15163 + anonymous_size * bits_per_byte
15164 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15165 }
15166 }
da5b30da
AA
15167 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15168 if (attr != NULL)
15169 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 15170 + attr->constant_value (0)));
c906108c
SS
15171
15172 /* Get name of field. */
39cbfefa
DJ
15173 fieldname = dwarf2_name (die, cu);
15174 if (fieldname == NULL)
15175 fieldname = "";
d8151005
DJ
15176
15177 /* The name is already allocated along with this objfile, so we don't
15178 need to duplicate it for the type. */
15179 fp->name = fieldname;
c906108c
SS
15180
15181 /* Change accessibility for artificial fields (e.g. virtual table
dda83cd7 15182 pointer or virtual base class pointer) to private. */
e142c38c 15183 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15184 {
d48cc9dd 15185 FIELD_ARTIFICIAL (*fp) = 1;
c906108c 15186 new_field->accessibility = DW_ACCESS_private;
264fc0e2 15187 fip->non_public_fields = true;
c906108c
SS
15188 }
15189 }
a9a9bd0f 15190 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15191 {
a9a9bd0f
DC
15192 /* C++ static member. */
15193
15194 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15195 is a declaration, but all versions of G++ as of this writing
15196 (so through at least 3.2.1) incorrectly generate
15197 DW_TAG_variable tags. */
6e70227d 15198
ff355380 15199 const char *physname;
c906108c 15200
a9a9bd0f 15201 /* Get name of field. */
39cbfefa
DJ
15202 fieldname = dwarf2_name (die, cu);
15203 if (fieldname == NULL)
c906108c
SS
15204 return;
15205
254e6b9e 15206 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15207 if (attr
15208 /* Only create a symbol if this is an external value.
15209 new_symbol checks this and puts the value in the global symbol
15210 table, which we want. If it is not external, new_symbol
15211 will try to put the value in cu->list_in_scope which is wrong. */
15212 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15213 {
15214 /* A static const member, not much different than an enum as far as
15215 we're concerned, except that we can support more types. */
15216 new_symbol (die, NULL, cu);
15217 }
15218
2df3850c 15219 /* Get physical name. */
ff355380 15220 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15221
d8151005
DJ
15222 /* The name is already allocated along with this objfile, so we don't
15223 need to duplicate it for the type. */
15224 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 15225 fp->set_type (die_type (die, cu));
d8151005 15226 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15227 }
15228 else if (die->tag == DW_TAG_inheritance)
15229 {
74ac6d43 15230 /* C++ base class field. */
7d79de9a 15231 handle_data_member_location (die, cu, fp);
c906108c 15232 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
15233 fp->set_type (die_type (die, cu));
15234 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 15235 }
2ddeaf8a
TT
15236 else
15237 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15238}
15239
883fd55a
KS
15240/* Can the type given by DIE define another type? */
15241
15242static bool
15243type_can_define_types (const struct die_info *die)
15244{
15245 switch (die->tag)
15246 {
15247 case DW_TAG_typedef:
15248 case DW_TAG_class_type:
15249 case DW_TAG_structure_type:
15250 case DW_TAG_union_type:
15251 case DW_TAG_enumeration_type:
15252 return true;
15253
15254 default:
15255 return false;
15256 }
15257}
15258
15259/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15260
15261static void
883fd55a
KS
15262dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15263 struct dwarf2_cu *cu)
6e70227d 15264{
be2daae6
TT
15265 struct decl_field fp;
15266 memset (&fp, 0, sizeof (fp));
98751a41 15267
883fd55a 15268 gdb_assert (type_can_define_types (die));
98751a41 15269
883fd55a 15270 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15271 fp.name = dwarf2_name (die, cu);
15272 fp.type = read_type_die (die, cu);
98751a41 15273
c191a687 15274 /* Save accessibility. */
bf23a268 15275 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
c191a687
KS
15276 switch (accessibility)
15277 {
15278 case DW_ACCESS_public:
15279 /* The assumed value if neither private nor protected. */
15280 break;
15281 case DW_ACCESS_private:
be2daae6 15282 fp.is_private = 1;
c191a687
KS
15283 break;
15284 case DW_ACCESS_protected:
be2daae6 15285 fp.is_protected = 1;
c191a687 15286 break;
c191a687
KS
15287 }
15288
883fd55a 15289 if (die->tag == DW_TAG_typedef)
be2daae6 15290 fip->typedef_field_list.push_back (fp);
883fd55a 15291 else
be2daae6 15292 fip->nested_types_list.push_back (fp);
98751a41
JK
15293}
15294
9c6a1327
TT
15295/* A convenience typedef that's used when finding the discriminant
15296 field for a variant part. */
1b95cdb7
SM
15297typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15298 offset_map_type;
9c6a1327
TT
15299
15300/* Compute the discriminant range for a given variant. OBSTACK is
15301 where the results will be stored. VARIANT is the variant to
15302 process. IS_UNSIGNED indicates whether the discriminant is signed
15303 or unsigned. */
15304
15305static const gdb::array_view<discriminant_range>
15306convert_variant_range (struct obstack *obstack, const variant_field &variant,
15307 bool is_unsigned)
15308{
15309 std::vector<discriminant_range> ranges;
15310
15311 if (variant.default_branch)
15312 return {};
15313
15314 if (variant.discr_list_data == nullptr)
15315 {
15316 discriminant_range r
15317 = {variant.discriminant_value, variant.discriminant_value};
15318 ranges.push_back (r);
15319 }
15320 else
15321 {
15322 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15323 variant.discr_list_data->size);
15324 while (!data.empty ())
15325 {
15326 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15327 {
15328 complaint (_("invalid discriminant marker: %d"), data[0]);
15329 break;
15330 }
15331 bool is_range = data[0] == DW_DSC_range;
15332 data = data.slice (1);
15333
15334 ULONGEST low, high;
15335 unsigned int bytes_read;
15336
15337 if (data.empty ())
15338 {
15339 complaint (_("DW_AT_discr_list missing low value"));
15340 break;
15341 }
15342 if (is_unsigned)
15343 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15344 else
15345 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15346 &bytes_read);
15347 data = data.slice (bytes_read);
15348
15349 if (is_range)
15350 {
15351 if (data.empty ())
15352 {
15353 complaint (_("DW_AT_discr_list missing high value"));
15354 break;
15355 }
15356 if (is_unsigned)
15357 high = read_unsigned_leb128 (nullptr, data.data (),
15358 &bytes_read);
15359 else
15360 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15361 &bytes_read);
15362 data = data.slice (bytes_read);
15363 }
15364 else
15365 high = low;
15366
15367 ranges.push_back ({ low, high });
15368 }
15369 }
15370
15371 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15372 ranges.size ());
15373 std::copy (ranges.begin (), ranges.end (), result);
15374 return gdb::array_view<discriminant_range> (result, ranges.size ());
15375}
15376
15377static const gdb::array_view<variant_part> create_variant_parts
15378 (struct obstack *obstack,
15379 const offset_map_type &offset_map,
15380 struct field_info *fi,
15381 const std::vector<variant_part_builder> &variant_parts);
15382
15383/* Fill in a "struct variant" for a given variant field. RESULT is
15384 the variant to fill in. OBSTACK is where any needed allocations
15385 will be done. OFFSET_MAP holds the mapping from section offsets to
15386 fields for the type. FI describes the fields of the type we're
15387 processing. FIELD is the variant field we're converting. */
15388
15389static void
15390create_one_variant (variant &result, struct obstack *obstack,
15391 const offset_map_type &offset_map,
15392 struct field_info *fi, const variant_field &field)
15393{
15394 result.discriminants = convert_variant_range (obstack, field, false);
15395 result.first_field = field.first_field + fi->baseclasses.size ();
15396 result.last_field = field.last_field + fi->baseclasses.size ();
15397 result.parts = create_variant_parts (obstack, offset_map, fi,
15398 field.variant_parts);
15399}
15400
15401/* Fill in a "struct variant_part" for a given variant part. RESULT
15402 is the variant part to fill in. OBSTACK is where any needed
15403 allocations will be done. OFFSET_MAP holds the mapping from
15404 section offsets to fields for the type. FI describes the fields of
15405 the type we're processing. BUILDER is the variant part to be
15406 converted. */
15407
15408static void
15409create_one_variant_part (variant_part &result,
15410 struct obstack *obstack,
15411 const offset_map_type &offset_map,
15412 struct field_info *fi,
15413 const variant_part_builder &builder)
15414{
15415 auto iter = offset_map.find (builder.discriminant_offset);
15416 if (iter == offset_map.end ())
15417 {
15418 result.discriminant_index = -1;
15419 /* Doesn't matter. */
15420 result.is_unsigned = false;
15421 }
15422 else
15423 {
15424 result.discriminant_index = iter->second;
15425 result.is_unsigned
c6d940a9 15426 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
9c6a1327
TT
15427 }
15428
15429 size_t n = builder.variants.size ();
15430 variant *output = new (obstack) variant[n];
15431 for (size_t i = 0; i < n; ++i)
15432 create_one_variant (output[i], obstack, offset_map, fi,
15433 builder.variants[i]);
15434
15435 result.variants = gdb::array_view<variant> (output, n);
15436}
15437
15438/* Create a vector of variant parts that can be attached to a type.
15439 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15440 holds the mapping from section offsets to fields for the type. FI
15441 describes the fields of the type we're processing. VARIANT_PARTS
15442 is the vector to convert. */
15443
15444static const gdb::array_view<variant_part>
15445create_variant_parts (struct obstack *obstack,
15446 const offset_map_type &offset_map,
15447 struct field_info *fi,
15448 const std::vector<variant_part_builder> &variant_parts)
15449{
15450 if (variant_parts.empty ())
15451 return {};
15452
15453 size_t n = variant_parts.size ();
15454 variant_part *result = new (obstack) variant_part[n];
15455 for (size_t i = 0; i < n; ++i)
15456 create_one_variant_part (result[i], obstack, offset_map, fi,
15457 variant_parts[i]);
15458
15459 return gdb::array_view<variant_part> (result, n);
15460}
15461
15462/* Compute the variant part vector for FIP, attaching it to TYPE when
15463 done. */
15464
15465static void
15466add_variant_property (struct field_info *fip, struct type *type,
15467 struct dwarf2_cu *cu)
15468{
15469 /* Map section offsets of fields to their field index. Note the
15470 field index here does not take the number of baseclasses into
15471 account. */
15472 offset_map_type offset_map;
15473 for (int i = 0; i < fip->fields.size (); ++i)
15474 offset_map[fip->fields[i].offset] = i;
15475
5e22e966 15476 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
15477 gdb::array_view<variant_part> parts
15478 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15479 fip->variant_parts);
15480
15481 struct dynamic_prop prop;
8c2e4e06
SM
15482 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15483 obstack_copy (&objfile->objfile_obstack, &parts,
15484 sizeof (parts)));
9c6a1327 15485
5c54719c 15486 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
15487}
15488
c906108c
SS
15489/* Create the vector of fields, and attach it to the type. */
15490
15491static void
fba45db2 15492dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15493 struct dwarf2_cu *cu)
c906108c 15494{
317f7127 15495 int nfields = fip->nfields ();
c906108c
SS
15496
15497 /* Record the field count, allocate space for the array of fields,
15498 and create blank accessibility bitfields if necessary. */
5e33d5f4 15499 type->set_num_fields (nfields);
3cabb6b0
SM
15500 type->set_fields
15501 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 15502
b4ba55a1 15503 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15504 {
15505 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15506
15507 TYPE_FIELD_PRIVATE_BITS (type) =
15508 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15509 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15510
15511 TYPE_FIELD_PROTECTED_BITS (type) =
15512 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15513 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15514
774b6a14
TT
15515 TYPE_FIELD_IGNORE_BITS (type) =
15516 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15517 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15518 }
15519
15520 /* If the type has baseclasses, allocate and clear a bit vector for
15521 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15522 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15523 {
be2daae6 15524 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15525 unsigned char *pointer;
c906108c
SS
15526
15527 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15528 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15529 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15530 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15531 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15532 }
15533
9c6a1327
TT
15534 if (!fip->variant_parts.empty ())
15535 add_variant_property (fip, type, cu);
2ddeaf8a 15536
be2daae6
TT
15537 /* Copy the saved-up fields into the field vector. */
15538 for (int i = 0; i < nfields; ++i)
c906108c 15539 {
be2daae6
TT
15540 struct nextfield &field
15541 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15542 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15543
ceacbf6e 15544 type->field (i) = field.field;
be2daae6 15545 switch (field.accessibility)
c906108c 15546 {
c5aa993b 15547 case DW_ACCESS_private:
b4ba55a1 15548 if (cu->language != language_ada)
be2daae6 15549 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15550 break;
c906108c 15551
c5aa993b 15552 case DW_ACCESS_protected:
b4ba55a1 15553 if (cu->language != language_ada)
be2daae6 15554 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15555 break;
c906108c 15556
c5aa993b
JM
15557 case DW_ACCESS_public:
15558 break;
c906108c 15559
c5aa993b
JM
15560 default:
15561 /* Unknown accessibility. Complain and treat it as public. */
15562 {
b98664d3 15563 complaint (_("unsupported accessibility %d"),
be2daae6 15564 field.accessibility);
c5aa993b
JM
15565 }
15566 break;
c906108c 15567 }
be2daae6 15568 if (i < fip->baseclasses.size ())
c906108c 15569 {
be2daae6 15570 switch (field.virtuality)
c906108c 15571 {
c5aa993b
JM
15572 case DW_VIRTUALITY_virtual:
15573 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15574 if (cu->language == language_ada)
a73c6dcd 15575 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15576 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15577 break;
c906108c
SS
15578 }
15579 }
c906108c
SS
15580 }
15581}
15582
7d27a96d
TT
15583/* Return true if this member function is a constructor, false
15584 otherwise. */
15585
15586static int
15587dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15588{
15589 const char *fieldname;
fe978cb0 15590 const char *type_name;
7d27a96d
TT
15591 int len;
15592
15593 if (die->parent == NULL)
15594 return 0;
15595
15596 if (die->parent->tag != DW_TAG_structure_type
15597 && die->parent->tag != DW_TAG_union_type
15598 && die->parent->tag != DW_TAG_class_type)
15599 return 0;
15600
15601 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15602 type_name = dwarf2_name (die->parent, cu);
15603 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15604 return 0;
15605
15606 len = strlen (fieldname);
fe978cb0
PA
15607 return (strncmp (fieldname, type_name, len) == 0
15608 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15609}
15610
c906108c
SS
15611/* Add a member function to the proper fieldlist. */
15612
15613static void
107d2387 15614dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15615 struct type *type, struct dwarf2_cu *cu)
c906108c 15616{
5e22e966 15617 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15618 struct attribute *attr;
c906108c 15619 int i;
be2daae6 15620 struct fnfieldlist *flp = nullptr;
c906108c 15621 struct fn_field *fnp;
15d034d0 15622 const char *fieldname;
f792889a 15623 struct type *this_type;
c906108c 15624
b4ba55a1 15625 if (cu->language == language_ada)
a73c6dcd 15626 error (_("unexpected member function in Ada type"));
b4ba55a1 15627
2df3850c 15628 /* Get name of member function. */
39cbfefa
DJ
15629 fieldname = dwarf2_name (die, cu);
15630 if (fieldname == NULL)
2df3850c 15631 return;
c906108c 15632
c906108c 15633 /* Look up member function name in fieldlist. */
be2daae6 15634 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15635 {
27bfe10e 15636 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15637 {
15638 flp = &fip->fnfieldlists[i];
15639 break;
15640 }
c906108c
SS
15641 }
15642
be2daae6
TT
15643 /* Create a new fnfieldlist if necessary. */
15644 if (flp == nullptr)
c906108c 15645 {
be2daae6
TT
15646 fip->fnfieldlists.emplace_back ();
15647 flp = &fip->fnfieldlists.back ();
c906108c 15648 flp->name = fieldname;
be2daae6 15649 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15650 }
15651
be2daae6
TT
15652 /* Create a new member function field and add it to the vector of
15653 fnfieldlists. */
15654 flp->fnfields.emplace_back ();
15655 fnp = &flp->fnfields.back ();
3da10d80
KS
15656
15657 /* Delay processing of the physname until later. */
9c37b5ae 15658 if (cu->language == language_cplus)
be2daae6
TT
15659 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15660 die, cu);
3da10d80
KS
15661 else
15662 {
1d06ead6 15663 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15664 fnp->physname = physname ? physname : "";
15665 }
15666
c906108c 15667 fnp->type = alloc_type (objfile);
f792889a 15668 this_type = read_type_die (die, cu);
78134374 15669 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15670 {
1f704f76 15671 int nparams = this_type->num_fields ();
c906108c 15672
f792889a 15673 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15674 of the method itself (TYPE_CODE_METHOD). */
15675 smash_to_method_type (fnp->type, type,
f792889a 15676 TYPE_TARGET_TYPE (this_type),
80fc5e77 15677 this_type->fields (),
1f704f76 15678 this_type->num_fields (),
a409645d 15679 this_type->has_varargs ());
c906108c
SS
15680
15681 /* Handle static member functions.
dda83cd7
SM
15682 Dwarf2 has no clean way to discern C++ static and non-static
15683 member functions. G++ helps GDB by marking the first
15684 parameter for non-static member functions (which is the this
15685 pointer) as artificial. We obtain this information from
15686 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15687 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15688 fnp->voffset = VOFFSET_STATIC;
15689 }
15690 else
b98664d3 15691 complaint (_("member function type missing for '%s'"),
3da10d80 15692 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15693
15694 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15695 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15696 fnp->fcontext = die_containing_type (die, cu);
c906108c 15697
3e43a32a
MS
15698 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15699 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15700
15701 /* Get accessibility. */
bf23a268 15702 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
60d5a603 15703 switch (accessibility)
c906108c 15704 {
60d5a603
JK
15705 case DW_ACCESS_private:
15706 fnp->is_private = 1;
15707 break;
15708 case DW_ACCESS_protected:
15709 fnp->is_protected = 1;
15710 break;
c906108c
SS
15711 }
15712
b02dede2 15713 /* Check for artificial methods. */
e142c38c 15714 attr = dwarf2_attr (die, DW_AT_artificial, cu);
c45bc3f8 15715 if (attr && attr->as_boolean ())
b02dede2
DJ
15716 fnp->is_artificial = 1;
15717
e35000a7
TBA
15718 /* Check for defaulted methods. */
15719 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
e8e5c158
TT
15720 if (attr != nullptr)
15721 fnp->defaulted = attr->defaulted ();
e35000a7
TBA
15722
15723 /* Check for deleted methods. */
15724 attr = dwarf2_attr (die, DW_AT_deleted, cu);
c45bc3f8 15725 if (attr != nullptr && attr->as_boolean ())
e35000a7
TBA
15726 fnp->is_deleted = 1;
15727
7d27a96d
TT
15728 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15729
0d564a31 15730 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15731 function. For older versions of GCC, this is an offset in the
15732 appropriate virtual table, as specified by DW_AT_containing_type.
15733 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15734 to the object address. */
15735
e142c38c 15736 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15737 if (attr != nullptr)
8e19ed76 15738 {
9d2246fc 15739 if (attr->form_is_block () && attr->as_block ()->size > 0)
dda83cd7 15740 {
9d2246fc
TT
15741 struct dwarf_block *block = attr->as_block ();
15742
15743 if (block->data[0] == DW_OP_constu)
aec5aa8b
TT
15744 {
15745 /* Old-style GCC. */
9d2246fc 15746 fnp->voffset = decode_locdesc (block, cu) + 2;
aec5aa8b 15747 }
9d2246fc
TT
15748 else if (block->data[0] == DW_OP_deref
15749 || (block->size > 1
15750 && block->data[0] == DW_OP_deref_size
15751 && block->data[1] == cu->header.addr_size))
aec5aa8b 15752 {
9d2246fc 15753 fnp->voffset = decode_locdesc (block, cu);
aec5aa8b
TT
15754 if ((fnp->voffset % cu->header.addr_size) != 0)
15755 dwarf2_complex_location_expr_complaint ();
15756 else
15757 fnp->voffset /= cu->header.addr_size;
15758 fnp->voffset += 2;
15759 }
15760 else
15761 dwarf2_complex_location_expr_complaint ();
15762
15763 if (!fnp->fcontext)
7e993ebf
KS
15764 {
15765 /* If there is no `this' field and no DW_AT_containing_type,
15766 we cannot actually find a base class context for the
15767 vtable! */
1f704f76 15768 if (this_type->num_fields () == 0
7e993ebf
KS
15769 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15770 {
b98664d3 15771 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15772 "function \"%s\" (offset %s)"),
15773 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15774 }
15775 else
15776 {
15777 fnp->fcontext
940da03e 15778 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15779 }
15780 }
aec5aa8b 15781 }
cd6c91b4 15782 else if (attr->form_is_section_offset ())
dda83cd7 15783 {
4d3c2250 15784 dwarf2_complex_location_expr_complaint ();
dda83cd7 15785 }
8e19ed76 15786 else
dda83cd7 15787 {
4d3c2250
KB
15788 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15789 fieldname);
dda83cd7 15790 }
0d564a31 15791 }
d48cc9dd
DJ
15792 else
15793 {
15794 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
23dca5c3 15795 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
d48cc9dd
DJ
15796 {
15797 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15798 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15799 "but the vtable offset is not specified"),
9d8780f0 15800 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15801 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15802 TYPE_CPLUS_DYNAMIC (type) = 1;
15803 }
15804 }
c906108c
SS
15805}
15806
15807/* Create the vector of member function fields, and attach it to the type. */
15808
15809static void
fba45db2 15810dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15811 struct dwarf2_cu *cu)
c906108c 15812{
b4ba55a1 15813 if (cu->language == language_ada)
a73c6dcd 15814 error (_("unexpected member functions in Ada type"));
b4ba55a1 15815
c906108c
SS
15816 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15817 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15818 TYPE_ALLOC (type,
15819 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15820
be2daae6 15821 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15822 {
be2daae6 15823 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15824 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15825
be2daae6
TT
15826 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15827 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15828 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15829 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15830
15831 for (int k = 0; k < nf.fnfields.size (); ++k)
15832 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15833 }
15834
be2daae6 15835 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15836}
15837
1168df01
JB
15838/* Returns non-zero if NAME is the name of a vtable member in CU's
15839 language, zero otherwise. */
15840static int
15841is_vtable_name (const char *name, struct dwarf2_cu *cu)
15842{
15843 static const char vptr[] = "_vptr";
15844
9c37b5ae
TT
15845 /* Look for the C++ form of the vtable. */
15846 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15847 return 1;
15848
15849 return 0;
15850}
15851
c0dd20ea 15852/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15853 functions, with the ABI-specified layout. If TYPE describes
15854 such a structure, smash it into a member function type.
61049d3b
DJ
15855
15856 GCC shouldn't do this; it should just output pointer to member DIEs.
15857 This is GCC PR debug/28767. */
c0dd20ea 15858
0b92b5bb
TT
15859static void
15860quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15861{
09e2d7c7 15862 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15863
15864 /* Check for a structure with no name and two children. */
1f704f76 15865 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15866 return;
c0dd20ea
DJ
15867
15868 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15869 if (TYPE_FIELD_NAME (type, 0) == NULL
15870 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15871 || TYPE_FIELD_NAME (type, 1) == NULL
15872 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15873 return;
c0dd20ea
DJ
15874
15875 /* Find the type of the method. */
940da03e 15876 pfn_type = type->field (0).type ();
c0dd20ea 15877 if (pfn_type == NULL
78134374
SM
15878 || pfn_type->code () != TYPE_CODE_PTR
15879 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15880 return;
c0dd20ea
DJ
15881
15882 /* Look for the "this" argument. */
15883 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15884 if (pfn_type->num_fields () == 0
940da03e
SM
15885 /* || pfn_type->field (0).type () == NULL */
15886 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15887 return;
c0dd20ea 15888
940da03e 15889 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15890 new_type = alloc_type (objfile);
09e2d7c7 15891 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15892 pfn_type->fields (), pfn_type->num_fields (),
a409645d 15893 pfn_type->has_varargs ());
0b92b5bb 15894 smash_to_methodptr_type (type, new_type);
c0dd20ea 15895}
1168df01 15896
e26624c6
TT
15897/* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15898 requires rewriting, then copy it and return the updated copy.
15899 Otherwise return nullptr. */
15900
15901static struct type *
15902rewrite_array_type (struct type *type)
15903{
15904 if (type->code () != TYPE_CODE_ARRAY)
15905 return nullptr;
15906
15907 struct type *index_type = type->index_type ();
15908 range_bounds *current_bounds = index_type->bounds ();
15909
15910 /* Handle multi-dimensional arrays. */
15911 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15912 if (new_target == nullptr)
15913 {
15914 /* Maybe we don't need to rewrite this array. */
15915 if (current_bounds->low.kind () == PROP_CONST
15916 && current_bounds->high.kind () == PROP_CONST)
15917 return nullptr;
15918 }
15919
15920 /* Either the target type was rewritten, or the bounds have to be
15921 updated. Either way we want to copy the type and update
15922 everything. */
15923 struct type *copy = copy_type (type);
15924 int nfields = copy->num_fields ();
15925 field *new_fields
15926 = ((struct field *) TYPE_ZALLOC (copy,
15927 nfields * sizeof (struct field)));
15928 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15929 copy->set_fields (new_fields);
15930 if (new_target != nullptr)
15931 TYPE_TARGET_TYPE (copy) = new_target;
15932
15933 struct type *index_copy = copy_type (index_type);
15934 range_bounds *bounds
15935 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15936 sizeof (range_bounds));
15937 *bounds = *current_bounds;
15938 bounds->low.set_const_val (1);
15939 bounds->high.set_const_val (0);
15940 index_copy->set_bounds (bounds);
15941 copy->set_index_type (index_copy);
15942
15943 return copy;
15944}
15945
57567375
TT
15946/* While some versions of GCC will generate complicated DWARF for an
15947 array (see quirk_ada_thick_pointer), more recent versions were
15948 modified to emit an explicit thick pointer structure. However, in
15949 this case, the array still has DWARF expressions for its ranges,
15950 and these must be ignored. */
15951
15952static void
15953quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15954 struct type *type)
15955{
15956 gdb_assert (cu->language == language_ada);
15957
15958 /* Check for a structure with two children. */
15959 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15960 return;
15961
15962 /* Check for P_ARRAY and P_BOUNDS members. */
15963 if (TYPE_FIELD_NAME (type, 0) == NULL
15964 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15965 || TYPE_FIELD_NAME (type, 1) == NULL
15966 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15967 return;
15968
15969 /* Make sure we're looking at a pointer to an array. */
15970 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15971 return;
57567375 15972
e26624c6
TT
15973 /* The Ada code already knows how to handle these types, so all that
15974 we need to do is turn the bounds into static bounds. However, we
15975 don't want to rewrite existing array or index types in-place,
15976 because those may be referenced in other contexts where this
15977 rewriting is undesirable. */
15978 struct type *new_ary_type
15979 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15980 if (new_ary_type != nullptr)
15981 type->field (0).set_type (lookup_pointer_type (new_ary_type));
57567375
TT
15982}
15983
2b4424c3
TT
15984/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15985 appropriate error checking and issuing complaints if there is a
15986 problem. */
15987
15988static ULONGEST
15989get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15990{
15991 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15992
15993 if (attr == nullptr)
15994 return 0;
15995
cd6c91b4 15996 if (!attr->form_is_constant ())
2b4424c3 15997 {
b98664d3 15998 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15999 " - DIE at %s [in module %s]"),
16000 sect_offset_str (die->sect_off),
5e22e966 16001 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
16002 return 0;
16003 }
16004
529908cb
TT
16005 LONGEST val = attr->constant_value (0);
16006 if (val < 0)
2b4424c3 16007 {
529908cb
TT
16008 complaint (_("DW_AT_alignment value must not be negative"
16009 " - DIE at %s [in module %s]"),
16010 sect_offset_str (die->sect_off),
16011 objfile_name (cu->per_objfile->objfile));
16012 return 0;
2b4424c3 16013 }
529908cb 16014 ULONGEST align = val;
2b4424c3
TT
16015
16016 if (align == 0)
16017 {
b98664d3 16018 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
16019 " - DIE at %s [in module %s]"),
16020 sect_offset_str (die->sect_off),
5e22e966 16021 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
16022 return 0;
16023 }
16024 if ((align & (align - 1)) != 0)
16025 {
b98664d3 16026 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
16027 " - DIE at %s [in module %s]"),
16028 sect_offset_str (die->sect_off),
5e22e966 16029 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
16030 return 0;
16031 }
16032
16033 return align;
16034}
16035
16036/* If the DIE has a DW_AT_alignment attribute, use its value to set
16037 the alignment for TYPE. */
16038
16039static void
16040maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
16041 struct type *type)
16042{
16043 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 16044 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
16045 " - DIE at %s [in module %s]"),
16046 sect_offset_str (die->sect_off),
5e22e966 16047 objfile_name (cu->per_objfile->objfile));
2b4424c3 16048}
685b1105 16049
e35000a7
TBA
16050/* Check if the given VALUE is a valid enum dwarf_calling_convention
16051 constant for a type, according to DWARF5 spec, Table 5.5. */
16052
16053static bool
16054is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
16055{
16056 switch (value)
16057 {
16058 case DW_CC_normal:
16059 case DW_CC_pass_by_reference:
16060 case DW_CC_pass_by_value:
16061 return true;
16062
16063 default:
16064 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 16065 "(%s) for a type"), pulongest (value));
e35000a7
TBA
16066 return false;
16067 }
16068}
16069
d0922fcf
TBA
16070/* Check if the given VALUE is a valid enum dwarf_calling_convention
16071 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
16072 also according to GNU-specific values (see include/dwarf2.h). */
16073
16074static bool
16075is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
16076{
16077 switch (value)
16078 {
16079 case DW_CC_normal:
16080 case DW_CC_program:
16081 case DW_CC_nocall:
16082 return true;
16083
16084 case DW_CC_GNU_renesas_sh:
16085 case DW_CC_GNU_borland_fastcall_i386:
16086 case DW_CC_GDB_IBM_OpenCL:
16087 return true;
16088
16089 default:
16090 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 16091 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
16092 return false;
16093 }
16094}
16095
c906108c 16096/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
16097 (definition) to create a type for the structure or union. Fill in
16098 the type's name and general properties; the members will not be
83655187
DE
16099 processed until process_structure_scope. A symbol table entry for
16100 the type will also not be done until process_structure_scope (assuming
16101 the type has a name).
c906108c 16102
c767944b
DJ
16103 NOTE: we need to call these functions regardless of whether or not the
16104 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 16105 structure or union. This gets the type entered into our set of
83655187 16106 user defined types. */
c906108c 16107
f792889a 16108static struct type *
134d01f1 16109read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16110{
5e22e966 16111 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
16112 struct type *type;
16113 struct attribute *attr;
15d034d0 16114 const char *name;
c906108c 16115
348e048f
DE
16116 /* If the definition of this type lives in .debug_types, read that type.
16117 Don't follow DW_AT_specification though, that will take us back up
16118 the chain and we want to go down. */
052c8bb8 16119 attr = die->attr (DW_AT_signature);
435d3d88 16120 if (attr != nullptr)
348e048f 16121 {
ac9ec31b 16122 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16123
ac9ec31b 16124 /* The type's CU may not be the same as CU.
02142a6c 16125 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16126 return set_die_type (die, type, cu);
16127 }
16128
c0dd20ea 16129 type = alloc_type (objfile);
c906108c 16130 INIT_CPLUS_SPECIFIC (type);
93311388 16131
39cbfefa
DJ
16132 name = dwarf2_name (die, cu);
16133 if (name != NULL)
c906108c 16134 {
987504bb 16135 if (cu->language == language_cplus
c44af4eb
TT
16136 || cu->language == language_d
16137 || cu->language == language_rust)
63d06c5c 16138 {
15d034d0 16139 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
16140
16141 /* dwarf2_full_name might have already finished building the DIE's
16142 type. If so, there is no need to continue. */
16143 if (get_die_type (die, cu) != NULL)
16144 return get_die_type (die, cu);
16145
d0e39ea2 16146 type->set_name (full_name);
63d06c5c
DC
16147 }
16148 else
16149 {
d8151005
DJ
16150 /* The name is already allocated along with this objfile, so
16151 we don't need to duplicate it for the type. */
d0e39ea2 16152 type->set_name (name);
63d06c5c 16153 }
c906108c
SS
16154 }
16155
16156 if (die->tag == DW_TAG_structure_type)
16157 {
67607e24 16158 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16159 }
16160 else if (die->tag == DW_TAG_union_type)
16161 {
67607e24 16162 type->set_code (TYPE_CODE_UNION);
c906108c
SS
16163 }
16164 else
16165 {
67607e24 16166 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16167 }
16168
0cc2414c
TT
16169 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16170 TYPE_DECLARED_CLASS (type) = 1;
16171
e35000a7
TBA
16172 /* Store the calling convention in the type if it's available in
16173 the die. Otherwise the calling convention remains set to
16174 the default value DW_CC_normal. */
16175 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16176 if (attr != nullptr
529908cb 16177 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
e35000a7
TBA
16178 {
16179 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16180 TYPE_CPLUS_CALLING_CONVENTION (type)
529908cb 16181 = (enum dwarf_calling_convention) (attr->constant_value (0));
e35000a7
TBA
16182 }
16183
e142c38c 16184 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16185 if (attr != nullptr)
c906108c 16186 {
cd6c91b4 16187 if (attr->form_is_constant ())
dda83cd7 16188 TYPE_LENGTH (type) = attr->constant_value (0);
155bfbd3
JB
16189 else
16190 {
f8e89861 16191 struct dynamic_prop prop;
293e7e51 16192 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 16193 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
dda83cd7 16194 TYPE_LENGTH (type) = 0;
155bfbd3 16195 }
c906108c
SS
16196 }
16197 else
16198 {
16199 TYPE_LENGTH (type) = 0;
16200 }
16201
2b4424c3
TT
16202 maybe_set_alignment (cu, die, type);
16203
5230b05a 16204 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 16205 {
5230b05a
WT
16206 /* ICC<14 does not output the required DW_AT_declaration on
16207 incomplete types, but gives them a size of zero. */
b4b73759 16208 type->set_is_stub (true);
685b1105
JK
16209 }
16210 else
9baccff6 16211 type->set_stub_is_supported (true);
685b1105 16212
dc718098 16213 if (die_is_declaration (die, cu))
b4b73759 16214 type->set_is_stub (true);
a6c727b2
DJ
16215 else if (attr == NULL && die->child == NULL
16216 && producer_is_realview (cu->producer))
16217 /* RealView does not output the required DW_AT_declaration
16218 on incomplete types. */
b4b73759 16219 type->set_is_stub (true);
dc718098 16220
c906108c
SS
16221 /* We need to add the type field to the die immediately so we don't
16222 infinitely recurse when dealing with pointers to the structure
0963b4bd 16223 type within the structure itself. */
1c379e20 16224 set_die_type (die, type, cu);
c906108c 16225
7e314c57
JK
16226 /* set_die_type should be already done. */
16227 set_descriptive_type (type, die, cu);
16228
c767944b
DJ
16229 return type;
16230}
16231
9c6a1327
TT
16232static void handle_struct_member_die
16233 (struct die_info *child_die,
16234 struct type *type,
16235 struct field_info *fi,
16236 std::vector<struct symbol *> *template_args,
16237 struct dwarf2_cu *cu);
16238
16239/* A helper for handle_struct_member_die that handles
16240 DW_TAG_variant_part. */
16241
16242static void
16243handle_variant_part (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 variant_part_builder *new_part;
16249 if (fi->current_variant_part == nullptr)
16250 {
16251 fi->variant_parts.emplace_back ();
16252 new_part = &fi->variant_parts.back ();
16253 }
16254 else if (!fi->current_variant_part->processing_variant)
16255 {
16256 complaint (_("nested DW_TAG_variant_part seen "
16257 "- DIE at %s [in module %s]"),
16258 sect_offset_str (die->sect_off),
5e22e966 16259 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16260 return;
16261 }
16262 else
16263 {
16264 variant_field &current = fi->current_variant_part->variants.back ();
16265 current.variant_parts.emplace_back ();
16266 new_part = &current.variant_parts.back ();
16267 }
16268
16269 /* When we recurse, we want callees to add to this new variant
16270 part. */
16271 scoped_restore save_current_variant_part
16272 = make_scoped_restore (&fi->current_variant_part, new_part);
16273
16274 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16275 if (discr == NULL)
16276 {
16277 /* It's a univariant form, an extension we support. */
16278 }
16279 else if (discr->form_is_ref ())
16280 {
16281 struct dwarf2_cu *target_cu = cu;
16282 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16283
16284 new_part->discriminant_offset = target_die->sect_off;
16285 }
16286 else
16287 {
16288 complaint (_("DW_AT_discr does not have DIE reference form"
16289 " - DIE at %s [in module %s]"),
16290 sect_offset_str (die->sect_off),
5e22e966 16291 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16292 }
16293
16294 for (die_info *child_die = die->child;
16295 child_die != NULL;
16296 child_die = child_die->sibling)
16297 handle_struct_member_die (child_die, type, fi, template_args, cu);
16298}
16299
16300/* A helper for handle_struct_member_die that handles
16301 DW_TAG_variant. */
16302
16303static void
16304handle_variant (struct die_info *die, struct type *type,
16305 struct field_info *fi,
16306 std::vector<struct symbol *> *template_args,
16307 struct dwarf2_cu *cu)
16308{
16309 if (fi->current_variant_part == nullptr)
16310 {
16311 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16312 "- DIE at %s [in module %s]"),
16313 sect_offset_str (die->sect_off),
5e22e966 16314 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16315 return;
16316 }
16317 if (fi->current_variant_part->processing_variant)
16318 {
16319 complaint (_("nested DW_TAG_variant seen "
16320 "- DIE at %s [in module %s]"),
16321 sect_offset_str (die->sect_off),
5e22e966 16322 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16323 return;
16324 }
16325
16326 scoped_restore save_processing_variant
16327 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16328 true);
16329
16330 fi->current_variant_part->variants.emplace_back ();
16331 variant_field &variant = fi->current_variant_part->variants.back ();
16332 variant.first_field = fi->fields.size ();
16333
16334 /* In a variant we want to get the discriminant and also add a
16335 field for our sole member child. */
16336 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
cae21f8e 16337 if (discr == nullptr || !discr->form_is_constant ())
9c6a1327
TT
16338 {
16339 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
9d2246fc 16340 if (discr == nullptr || discr->as_block ()->size == 0)
9c6a1327
TT
16341 variant.default_branch = true;
16342 else
9d2246fc 16343 variant.discr_list_data = discr->as_block ();
9c6a1327
TT
16344 }
16345 else
cae21f8e 16346 variant.discriminant_value = discr->constant_value (0);
9c6a1327
TT
16347
16348 for (die_info *variant_child = die->child;
16349 variant_child != NULL;
16350 variant_child = variant_child->sibling)
16351 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16352
16353 variant.last_field = fi->fields.size ();
16354}
16355
2ddeaf8a
TT
16356/* A helper for process_structure_scope that handles a single member
16357 DIE. */
16358
16359static void
16360handle_struct_member_die (struct die_info *child_die, struct type *type,
16361 struct field_info *fi,
16362 std::vector<struct symbol *> *template_args,
16363 struct dwarf2_cu *cu)
16364{
16365 if (child_die->tag == DW_TAG_member
9c6a1327 16366 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
16367 {
16368 /* NOTE: carlton/2002-11-05: A C++ static data member
16369 should be a DW_TAG_member that is a declaration, but
16370 all versions of G++ as of this writing (so through at
16371 least 3.2.1) incorrectly generate DW_TAG_variable
16372 tags for them instead. */
16373 dwarf2_add_field (fi, child_die, cu);
16374 }
16375 else if (child_die->tag == DW_TAG_subprogram)
16376 {
16377 /* Rust doesn't have member functions in the C++ sense.
16378 However, it does emit ordinary functions as children
16379 of a struct DIE. */
16380 if (cu->language == language_rust)
16381 read_func_scope (child_die, cu);
16382 else
16383 {
16384 /* C++ member function. */
16385 dwarf2_add_member_fn (fi, child_die, type, cu);
16386 }
16387 }
16388 else if (child_die->tag == DW_TAG_inheritance)
16389 {
16390 /* C++ base class field. */
16391 dwarf2_add_field (fi, child_die, cu);
16392 }
16393 else if (type_can_define_types (child_die))
16394 dwarf2_add_type_defn (fi, child_die, cu);
16395 else if (child_die->tag == DW_TAG_template_type_param
16396 || child_die->tag == DW_TAG_template_value_param)
16397 {
16398 struct symbol *arg = new_symbol (child_die, NULL, cu);
16399
16400 if (arg != NULL)
16401 template_args->push_back (arg);
16402 }
9c6a1327
TT
16403 else if (child_die->tag == DW_TAG_variant_part)
16404 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 16405 else if (child_die->tag == DW_TAG_variant)
9c6a1327 16406 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
16407}
16408
c767944b
DJ
16409/* Finish creating a structure or union type, including filling in
16410 its members and creating a symbol for it. */
16411
16412static void
16413process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16414{
5e22e966 16415 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 16416 struct die_info *child_die;
c767944b
DJ
16417 struct type *type;
16418
16419 type = get_die_type (die, cu);
16420 if (type == NULL)
16421 type = read_structure_type (die, cu);
16422
3e1d3d8c 16423 bool has_template_parameters = false;
e142c38c 16424 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16425 {
16426 struct field_info fi;
2f4732b0 16427 std::vector<struct symbol *> template_args;
c906108c 16428
639d11d3 16429 child_die = die->child;
c906108c
SS
16430
16431 while (child_die && child_die->tag)
16432 {
2ddeaf8a 16433 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 16434 child_die = child_die->sibling;
c906108c
SS
16435 }
16436
34eaf542 16437 /* Attach template arguments to type. */
2f4732b0 16438 if (!template_args.empty ())
34eaf542 16439 {
3e1d3d8c 16440 has_template_parameters = true;
34eaf542 16441 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16442 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16443 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16444 = XOBNEWVEC (&objfile->objfile_obstack,
16445 struct symbol *,
16446 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16447 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16448 template_args.data (),
34eaf542
TT
16449 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16450 * sizeof (struct symbol *)));
34eaf542
TT
16451 }
16452
c906108c 16453 /* Attach fields and member functions to the type. */
317f7127 16454 if (fi.nfields () > 0)
e7c27a73 16455 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16456 if (!fi.fnfieldlists.empty ())
c906108c 16457 {
e7c27a73 16458 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16459
c5aa993b 16460 /* Get the type which refers to the base class (possibly this
c906108c 16461 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16462 class from the DW_AT_containing_type attribute. This use of
16463 DW_AT_containing_type is a GNU extension. */
c906108c 16464
e142c38c 16465 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16466 {
e7c27a73 16467 struct type *t = die_containing_type (die, cu);
c906108c 16468
ae6ae975 16469 set_type_vptr_basetype (type, t);
c906108c
SS
16470 if (type == t)
16471 {
c906108c
SS
16472 int i;
16473
16474 /* Our own class provides vtbl ptr. */
1f704f76 16475 for (i = t->num_fields () - 1;
c906108c
SS
16476 i >= TYPE_N_BASECLASSES (t);
16477 --i)
16478 {
0d5cff50 16479 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16480
dda83cd7 16481 if (is_vtable_name (fieldname, cu))
c906108c 16482 {
ae6ae975 16483 set_type_vptr_fieldno (type, i);
c906108c
SS
16484 break;
16485 }
16486 }
16487
16488 /* Complain if virtual function table field not found. */
16489 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16490 complaint (_("virtual function table pointer "
3e43a32a 16491 "not found when defining class '%s'"),
7d93a1e0 16492 type->name () ? type->name () : "");
c906108c
SS
16493 }
16494 else
16495 {
ae6ae975 16496 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16497 }
16498 }
f6235d4c 16499 else if (cu->producer
61012eef 16500 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16501 {
16502 /* The IBM XLC compiler does not provide direct indication
dda83cd7
SM
16503 of the containing type, but the vtable pointer is
16504 always named __vfp. */
f6235d4c
EZ
16505
16506 int i;
16507
1f704f76 16508 for (i = type->num_fields () - 1;
f6235d4c
EZ
16509 i >= TYPE_N_BASECLASSES (type);
16510 --i)
16511 {
16512 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16513 {
ae6ae975
DE
16514 set_type_vptr_fieldno (type, i);
16515 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16516 break;
16517 }
16518 }
16519 }
c906108c 16520 }
98751a41
JK
16521
16522 /* Copy fi.typedef_field_list linked list elements content into the
16523 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16524 if (!fi.typedef_field_list.empty ())
98751a41 16525 {
be2daae6 16526 int count = fi.typedef_field_list.size ();
98751a41 16527
a0d7a4ff 16528 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16529 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16530 = ((struct decl_field *)
be2daae6
TT
16531 TYPE_ALLOC (type,
16532 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16533 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16534
be2daae6
TT
16535 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16536 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16537 }
c767944b 16538
883fd55a
KS
16539 /* Copy fi.nested_types_list linked list elements content into the
16540 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16541 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16542 {
be2daae6 16543 int count = fi.nested_types_list.size ();
883fd55a
KS
16544
16545 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16546 TYPE_NESTED_TYPES_ARRAY (type)
16547 = ((struct decl_field *)
be2daae6
TT
16548 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16549 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16550
be2daae6
TT
16551 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16552 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16553 }
c906108c 16554 }
63d06c5c 16555
bb5ed363 16556 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16557 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16558 cu->rust_unions.push_back (type);
57567375
TT
16559 else if (cu->language == language_ada)
16560 quirk_ada_thick_pointer_struct (die, cu, type);
0b92b5bb 16561
90aeadfc
DC
16562 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16563 snapshots) has been known to create a die giving a declaration
16564 for a class that has, as a child, a die giving a definition for a
16565 nested class. So we have to process our children even if the
16566 current die is a declaration. Normally, of course, a declaration
16567 won't have any children at all. */
134d01f1 16568
ca040673
DE
16569 child_die = die->child;
16570
90aeadfc
DC
16571 while (child_die != NULL && child_die->tag)
16572 {
16573 if (child_die->tag == DW_TAG_member
16574 || child_die->tag == DW_TAG_variable
34eaf542
TT
16575 || child_die->tag == DW_TAG_inheritance
16576 || child_die->tag == DW_TAG_template_value_param
16577 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16578 {
90aeadfc 16579 /* Do nothing. */
134d01f1 16580 }
90aeadfc
DC
16581 else
16582 process_die (child_die, cu);
134d01f1 16583
436c571c 16584 child_die = child_die->sibling;
134d01f1
DJ
16585 }
16586
fa4028e9
JB
16587 /* Do not consider external references. According to the DWARF standard,
16588 these DIEs are identified by the fact that they have no byte_size
16589 attribute, and a declaration attribute. */
16590 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
16591 || !die_is_declaration (die, cu)
16592 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
16593 {
16594 struct symbol *sym = new_symbol (die, type, cu);
16595
16596 if (has_template_parameters)
16597 {
a776957c
TT
16598 struct symtab *symtab;
16599 if (sym != nullptr)
16600 symtab = symbol_symtab (sym);
16601 else if (cu->line_header != nullptr)
16602 {
16603 /* Any related symtab will do. */
16604 symtab
7ba99d21 16605 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16606 }
16607 else
16608 {
16609 symtab = nullptr;
16610 complaint (_("could not find suitable "
16611 "symtab for template parameter"
16612 " - DIE at %s [in module %s]"),
16613 sect_offset_str (die->sect_off),
16614 objfile_name (objfile));
16615 }
16616
16617 if (symtab != nullptr)
16618 {
16619 /* Make sure that the symtab is set on the new symbols.
16620 Even though they don't appear in this symtab directly,
16621 other parts of gdb assume that symbols do, and this is
16622 reasonably true. */
16623 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16624 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16625 }
3e1d3d8c
TT
16626 }
16627 }
134d01f1
DJ
16628}
16629
ed6acedd
TT
16630/* Assuming DIE is an enumeration type, and TYPE is its associated
16631 type, update TYPE using some information only available in DIE's
16632 children. In particular, the fields are computed. */
55426c9d
JB
16633
16634static void
16635update_enumeration_type_from_children (struct die_info *die,
16636 struct type *type,
16637 struct dwarf2_cu *cu)
16638{
60f7655a 16639 struct die_info *child_die;
55426c9d
JB
16640 int unsigned_enum = 1;
16641 int flag_enum = 1;
55426c9d 16642
8268c778 16643 auto_obstack obstack;
ed6acedd 16644 std::vector<struct field> fields;
55426c9d 16645
60f7655a
DE
16646 for (child_die = die->child;
16647 child_die != NULL && child_die->tag;
436c571c 16648 child_die = child_die->sibling)
55426c9d
JB
16649 {
16650 struct attribute *attr;
16651 LONGEST value;
16652 const gdb_byte *bytes;
16653 struct dwarf2_locexpr_baton *baton;
16654 const char *name;
60f7655a 16655
55426c9d
JB
16656 if (child_die->tag != DW_TAG_enumerator)
16657 continue;
16658
16659 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16660 if (attr == NULL)
16661 continue;
16662
16663 name = dwarf2_name (child_die, cu);
16664 if (name == NULL)
16665 name = "<anonymous enumerator>";
16666
16667 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16668 &value, &bytes, &baton);
16669 if (value < 0)
16670 {
16671 unsigned_enum = 0;
16672 flag_enum = 0;
16673 }
55426c9d 16674 else
edd45eb0
SM
16675 {
16676 if (count_one_bits_ll (value) >= 2)
16677 flag_enum = 0;
edd45eb0 16678 }
55426c9d 16679
ed6acedd
TT
16680 fields.emplace_back ();
16681 struct field &field = fields.back ();
16682 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16683 SET_FIELD_ENUMVAL (field, value);
16684 }
16685
16686 if (!fields.empty ())
16687 {
5e33d5f4 16688 type->set_num_fields (fields.size ());
3cabb6b0
SM
16689 type->set_fields
16690 ((struct field *)
16691 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16692 memcpy (type->fields (), fields.data (),
ed6acedd 16693 sizeof (struct field) * fields.size ());
55426c9d
JB
16694 }
16695
16696 if (unsigned_enum)
653223d3
SM
16697 type->set_is_unsigned (true);
16698
55426c9d
JB
16699 if (flag_enum)
16700 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16701}
16702
134d01f1
DJ
16703/* Given a DW_AT_enumeration_type die, set its type. We do not
16704 complete the type's fields yet, or create any symbols. */
c906108c 16705
f792889a 16706static struct type *
134d01f1 16707read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16708{
5e22e966 16709 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16710 struct type *type;
c906108c 16711 struct attribute *attr;
0114d602 16712 const char *name;
134d01f1 16713
348e048f
DE
16714 /* If the definition of this type lives in .debug_types, read that type.
16715 Don't follow DW_AT_specification though, that will take us back up
16716 the chain and we want to go down. */
052c8bb8 16717 attr = die->attr (DW_AT_signature);
435d3d88 16718 if (attr != nullptr)
348e048f 16719 {
ac9ec31b 16720 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16721
ac9ec31b 16722 /* The type's CU may not be the same as CU.
02142a6c 16723 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16724 return set_die_type (die, type, cu);
16725 }
16726
c906108c
SS
16727 type = alloc_type (objfile);
16728
67607e24 16729 type->set_code (TYPE_CODE_ENUM);
94af9270 16730 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16731 if (name != NULL)
d0e39ea2 16732 type->set_name (name);
c906108c 16733
0626fc76
TT
16734 attr = dwarf2_attr (die, DW_AT_type, cu);
16735 if (attr != NULL)
16736 {
16737 struct type *underlying_type = die_type (die, cu);
16738
16739 TYPE_TARGET_TYPE (type) = underlying_type;
16740 }
16741
e142c38c 16742 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16743 if (attr != nullptr)
c906108c 16744 {
529908cb 16745 TYPE_LENGTH (type) = attr->constant_value (0);
c906108c
SS
16746 }
16747 else
16748 {
16749 TYPE_LENGTH (type) = 0;
16750 }
16751
2b4424c3
TT
16752 maybe_set_alignment (cu, die, type);
16753
137033e9
JB
16754 /* The enumeration DIE can be incomplete. In Ada, any type can be
16755 declared as private in the package spec, and then defined only
16756 inside the package body. Such types are known as Taft Amendment
16757 Types. When another package uses such a type, an incomplete DIE
16758 may be generated by the compiler. */
02eb380e 16759 if (die_is_declaration (die, cu))
b4b73759 16760 type->set_is_stub (true);
02eb380e 16761
0626fc76
TT
16762 /* If this type has an underlying type that is not a stub, then we
16763 may use its attributes. We always use the "unsigned" attribute
16764 in this situation, because ordinarily we guess whether the type
16765 is unsigned -- but the guess can be wrong and the underlying type
16766 can tell us the reality. However, we defer to a local size
16767 attribute if one exists, because this lets the compiler override
16768 the underlying type if needed. */
e46d3488 16769 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
0626fc76 16770 {
9e7c9a03
HD
16771 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16772 underlying_type = check_typedef (underlying_type);
653223d3
SM
16773
16774 type->set_is_unsigned (underlying_type->is_unsigned ());
16775
0626fc76 16776 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16777 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
653223d3 16778
2b4424c3 16779 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16780 && TYPE_RAW_ALIGN (underlying_type) != 0)
16781 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16782 }
16783
3d567982
TT
16784 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16785
ed6acedd
TT
16786 set_die_type (die, type, cu);
16787
16788 /* Finish the creation of this type by using the enum's children.
16789 Note that, as usual, this must come after set_die_type to avoid
16790 infinite recursion when trying to compute the names of the
16791 enumerators. */
16792 update_enumeration_type_from_children (die, type, cu);
16793
16794 return type;
134d01f1
DJ
16795}
16796
16797/* Given a pointer to a die which begins an enumeration, process all
16798 the dies that define the members of the enumeration, and create the
16799 symbol for the enumeration type.
16800
16801 NOTE: We reverse the order of the element list. */
16802
16803static void
16804process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16805{
f792889a 16806 struct type *this_type;
134d01f1 16807
f792889a
DJ
16808 this_type = get_die_type (die, cu);
16809 if (this_type == NULL)
16810 this_type = read_enumeration_type (die, cu);
9dc481d3 16811
639d11d3 16812 if (die->child != NULL)
c906108c 16813 {
9dc481d3 16814 struct die_info *child_die;
15d034d0 16815 const char *name;
9dc481d3 16816
639d11d3 16817 child_die = die->child;
c906108c
SS
16818 while (child_die && child_die->tag)
16819 {
16820 if (child_die->tag != DW_TAG_enumerator)
16821 {
e7c27a73 16822 process_die (child_die, cu);
c906108c
SS
16823 }
16824 else
16825 {
39cbfefa
DJ
16826 name = dwarf2_name (child_die, cu);
16827 if (name)
ed6acedd 16828 new_symbol (child_die, this_type, cu);
c906108c
SS
16829 }
16830
436c571c 16831 child_die = child_die->sibling;
c906108c 16832 }
c906108c 16833 }
134d01f1 16834
6c83ed52
TT
16835 /* If we are reading an enum from a .debug_types unit, and the enum
16836 is a declaration, and the enum is not the signatured type in the
16837 unit, then we do not want to add a symbol for it. Adding a
16838 symbol would in some cases obscure the true definition of the
16839 enum, giving users an incomplete type when the definition is
16840 actually available. Note that we do not want to do this for all
16841 enums which are just declarations, because C++0x allows forward
16842 enum declarations. */
3019eac3 16843 if (cu->per_cu->is_debug_types
6c83ed52
TT
16844 && die_is_declaration (die, cu))
16845 {
52dc124a 16846 struct signatured_type *sig_type;
6c83ed52 16847
c0f78cd4 16848 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16849 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16850 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16851 return;
16852 }
16853
f792889a 16854 new_symbol (die, this_type, cu);
c906108c
SS
16855}
16856
57567375
TT
16857/* Helper function for quirk_ada_thick_pointer that examines a bounds
16858 expression for an index type and finds the corresponding field
16859 offset in the hidden "P_BOUNDS" structure. Returns true on success
16860 and updates *FIELD, false if it fails to recognize an
16861 expression. */
16862
16863static bool
16864recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16865 int *bounds_offset, struct field *field,
16866 struct dwarf2_cu *cu)
16867{
16868 struct attribute *attr = dwarf2_attr (die, name, cu);
16869 if (attr == nullptr || !attr->form_is_block ())
16870 return false;
16871
16872 const struct dwarf_block *block = attr->as_block ();
16873 const gdb_byte *start = block->data;
16874 const gdb_byte *end = block->data + block->size;
16875
16876 /* The expression to recognize generally looks like:
16877
16878 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16879 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16880
16881 However, the second "plus_uconst" may be missing:
16882
16883 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16884 DW_OP_deref_size: 4)
16885
16886 This happens when the field is at the start of the structure.
16887
16888 Also, the final deref may not be sized:
16889
16890 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16891 DW_OP_deref)
16892
16893 This happens when the size of the index type happens to be the
16894 same as the architecture's word size. This can occur with or
16895 without the second plus_uconst. */
16896
16897 if (end - start < 2)
16898 return false;
16899 if (*start++ != DW_OP_push_object_address)
16900 return false;
16901 if (*start++ != DW_OP_plus_uconst)
16902 return false;
16903
16904 uint64_t this_bound_off;
16905 start = gdb_read_uleb128 (start, end, &this_bound_off);
16906 if (start == nullptr || (int) this_bound_off != this_bound_off)
16907 return false;
16908 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16909 is consistent among all bounds. */
16910 if (*bounds_offset == -1)
16911 *bounds_offset = this_bound_off;
16912 else if (*bounds_offset != this_bound_off)
16913 return false;
16914
16915 if (start == end || *start++ != DW_OP_deref)
16916 return false;
16917
16918 int offset = 0;
16919 if (start ==end)
16920 return false;
16921 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16922 {
16923 /* This means an offset of 0. */
16924 }
16925 else if (*start++ != DW_OP_plus_uconst)
16926 return false;
16927 else
16928 {
16929 /* The size is the parameter to DW_OP_plus_uconst. */
16930 uint64_t val;
16931 start = gdb_read_uleb128 (start, end, &val);
16932 if (start == nullptr)
16933 return false;
16934 if ((int) val != val)
16935 return false;
16936 offset = val;
16937 }
16938
16939 if (start == end)
16940 return false;
16941
16942 uint64_t size;
16943 if (*start == DW_OP_deref_size)
16944 {
16945 start = gdb_read_uleb128 (start + 1, end, &size);
16946 if (start == nullptr)
16947 return false;
16948 }
16949 else if (*start == DW_OP_deref)
16950 {
16951 size = cu->header.addr_size;
16952 ++start;
16953 }
16954 else
16955 return false;
16956
16957 SET_FIELD_BITPOS (*field, 8 * offset);
16958 if (size != TYPE_LENGTH (field->type ()))
16959 FIELD_BITSIZE (*field) = 8 * size;
16960
16961 return true;
16962}
16963
16964/* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16965 some kinds of Ada arrays:
16966
16967 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16968 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16969 <11e0> DW_AT_data_location: 2 byte block: 97 6
16970 (DW_OP_push_object_address; DW_OP_deref)
16971 <11e3> DW_AT_type : <0x1173>
16972 <11e7> DW_AT_sibling : <0x1201>
16973 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16974 <11ec> DW_AT_type : <0x1206>
16975 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16976 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16977 DW_OP_deref_size: 4)
16978 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16979 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16980 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16981
16982 This actually represents a "thick pointer", which is a structure
16983 with two elements: one that is a pointer to the array data, and one
16984 that is a pointer to another structure; this second structure holds
16985 the array bounds.
16986
16987 This returns a new type on success, or nullptr if this didn't
16988 recognize the type. */
16989
16990static struct type *
16991quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16992 struct type *type)
16993{
16994 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16995 /* So far we've only seen this with block form. */
16996 if (attr == nullptr || !attr->form_is_block ())
16997 return nullptr;
16998
16999 /* Note that this will fail if the structure layout is changed by
17000 the compiler. However, we have no good way to recognize some
17001 other layout, because we don't know what expression the compiler
17002 might choose to emit should this happen. */
17003 struct dwarf_block *blk = attr->as_block ();
17004 if (blk->size != 2
17005 || blk->data[0] != DW_OP_push_object_address
17006 || blk->data[1] != DW_OP_deref)
17007 return nullptr;
17008
17009 int bounds_offset = -1;
17010 int max_align = -1;
17011 std::vector<struct field> range_fields;
17012 for (struct die_info *child_die = die->child;
17013 child_die;
17014 child_die = child_die->sibling)
17015 {
17016 if (child_die->tag == DW_TAG_subrange_type)
17017 {
17018 struct type *underlying = read_subrange_index_type (child_die, cu);
17019
17020 int this_align = type_align (underlying);
17021 if (this_align > max_align)
17022 max_align = this_align;
17023
17024 range_fields.emplace_back ();
17025 range_fields.emplace_back ();
17026
17027 struct field &lower = range_fields[range_fields.size () - 2];
17028 struct field &upper = range_fields[range_fields.size () - 1];
17029
17030 lower.set_type (underlying);
17031 FIELD_ARTIFICIAL (lower) = 1;
17032
17033 upper.set_type (underlying);
17034 FIELD_ARTIFICIAL (upper) = 1;
17035
17036 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
17037 &bounds_offset, &lower, cu)
17038 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
17039 &bounds_offset, &upper, cu))
17040 return nullptr;
17041 }
17042 }
17043
17044 /* This shouldn't really happen, but double-check that we found
17045 where the bounds are stored. */
17046 if (bounds_offset == -1)
17047 return nullptr;
17048
17049 struct objfile *objfile = cu->per_objfile->objfile;
17050 for (int i = 0; i < range_fields.size (); i += 2)
17051 {
17052 char name[20];
17053
17054 /* Set the name of each field in the bounds. */
17055 xsnprintf (name, sizeof (name), "LB%d", i / 2);
17056 FIELD_NAME (range_fields[i]) = objfile->intern (name);
17057 xsnprintf (name, sizeof (name), "UB%d", i / 2);
17058 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
17059 }
17060
17061 struct type *bounds = alloc_type (objfile);
17062 bounds->set_code (TYPE_CODE_STRUCT);
17063
17064 bounds->set_num_fields (range_fields.size ());
17065 bounds->set_fields
17066 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
17067 * sizeof (struct field))));
17068 memcpy (bounds->fields (), range_fields.data (),
17069 bounds->num_fields () * sizeof (struct field));
17070
17071 int last_fieldno = range_fields.size () - 1;
17072 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
17073 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
17074 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
17075
17076 /* Rewrite the existing array type in place. Specifically, we
17077 remove any dynamic properties we might have read, and we replace
17078 the index types. */
17079 struct type *iter = type;
17080 for (int i = 0; i < range_fields.size (); i += 2)
17081 {
17082 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
17083 iter->main_type->dyn_prop_list = nullptr;
17084 iter->set_index_type
17085 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
17086 iter = TYPE_TARGET_TYPE (iter);
17087 }
17088
17089 struct type *result = alloc_type (objfile);
17090 result->set_code (TYPE_CODE_STRUCT);
17091
17092 result->set_num_fields (2);
17093 result->set_fields
17094 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
17095 * sizeof (struct field))));
17096
17097 /* The names are chosen to coincide with what the compiler does with
17098 -fgnat-encodings=all, which the Ada code in gdb already
17099 understands. */
17100 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
17101 result->field (0).set_type (lookup_pointer_type (type));
17102
17103 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
17104 result->field (1).set_type (lookup_pointer_type (bounds));
17105 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
17106
17107 result->set_name (type->name ());
17108 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17109 + TYPE_LENGTH (result->field (1).type ()));
17110
17111 return result;
17112}
17113
c906108c
SS
17114/* Extract all information from a DW_TAG_array_type DIE and put it in
17115 the DIE's type field. For now, this only handles one dimensional
17116 arrays. */
17117
f792889a 17118static struct type *
e7c27a73 17119read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17120{
5e22e966 17121 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 17122 struct die_info *child_die;
7e314c57 17123 struct type *type;
c906108c 17124 struct type *element_type, *range_type, *index_type;
c906108c 17125 struct attribute *attr;
15d034d0 17126 const char *name;
a405673c 17127 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 17128 unsigned int bit_stride = 0;
c906108c 17129
e7c27a73 17130 element_type = die_type (die, cu);
c906108c 17131
7e314c57
JK
17132 /* The die_type call above may have already set the type for this DIE. */
17133 type = get_die_type (die, cu);
17134 if (type)
17135 return type;
17136
dc53a7ad
JB
17137 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17138 if (attr != NULL)
a405673c
JB
17139 {
17140 int stride_ok;
293e7e51 17141 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
17142
17143 byte_stride_prop
17144 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
17145 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17146 prop_type);
a405673c
JB
17147 if (!stride_ok)
17148 {
b98664d3 17149 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
17150 " - DIE at %s [in module %s]"),
17151 sect_offset_str (die->sect_off),
5e22e966 17152 objfile_name (cu->per_objfile->objfile));
a405673c
JB
17153 /* Ignore this attribute. We will likely not be able to print
17154 arrays of this type correctly, but there is little we can do
17155 to help if we cannot read the attribute's value. */
17156 byte_stride_prop = NULL;
17157 }
17158 }
dc53a7ad
JB
17159
17160 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17161 if (attr != NULL)
529908cb 17162 bit_stride = attr->constant_value (0);
dc53a7ad 17163
c906108c
SS
17164 /* Irix 6.2 native cc creates array types without children for
17165 arrays with unspecified length. */
639d11d3 17166 if (die->child == NULL)
c906108c 17167 {
46bf5051 17168 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17169 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 17170 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 17171 byte_stride_prop, bit_stride);
f792889a 17172 return set_die_type (die, type, cu);
c906108c
SS
17173 }
17174
791afaa2 17175 std::vector<struct type *> range_types;
639d11d3 17176 child_die = die->child;
c906108c
SS
17177 while (child_die && child_die->tag)
17178 {
17179 if (child_die->tag == DW_TAG_subrange_type)
17180 {
f792889a 17181 struct type *child_type = read_type_die (child_die, cu);
9a619af0 17182
dda83cd7
SM
17183 if (child_type != NULL)
17184 {
0963b4bd 17185 /* The range type was succesfully read. Save it for the
dda83cd7 17186 array type creation. */
791afaa2 17187 range_types.push_back (child_type);
dda83cd7 17188 }
c906108c 17189 }
436c571c 17190 child_die = child_die->sibling;
c906108c
SS
17191 }
17192
cf2b2075
TV
17193 if (range_types.empty ())
17194 {
17195 complaint (_("unable to find array range - DIE at %s [in module %s]"),
17196 sect_offset_str (die->sect_off),
17197 objfile_name (cu->per_objfile->objfile));
17198 return NULL;
17199 }
17200
c906108c
SS
17201 /* Dwarf2 dimensions are output from left to right, create the
17202 necessary array types in backwards order. */
7ca2d3a3 17203
c906108c 17204 type = element_type;
7ca2d3a3
DL
17205
17206 if (read_array_order (die, cu) == DW_ORD_col_major)
17207 {
17208 int i = 0;
9a619af0 17209
791afaa2 17210 while (i < range_types.size ())
10f6a3ad
TT
17211 {
17212 type = create_array_type_with_stride (NULL, type, range_types[i++],
17213 byte_stride_prop, bit_stride);
17214 bit_stride = 0;
17215 byte_stride_prop = nullptr;
17216 }
7ca2d3a3
DL
17217 }
17218 else
17219 {
791afaa2 17220 size_t ndim = range_types.size ();
7ca2d3a3 17221 while (ndim-- > 0)
10f6a3ad
TT
17222 {
17223 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17224 byte_stride_prop, bit_stride);
17225 bit_stride = 0;
17226 byte_stride_prop = nullptr;
17227 }
7ca2d3a3 17228 }
c906108c 17229
cf2b2075
TV
17230 gdb_assert (type != element_type);
17231
f5f8a009
EZ
17232 /* Understand Dwarf2 support for vector types (like they occur on
17233 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17234 array type. This is not part of the Dwarf2/3 standard yet, but a
17235 custom vendor extension. The main difference between a regular
17236 array and the vector variant is that vectors are passed by value
17237 to functions. */
e142c38c 17238 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 17239 if (attr != nullptr)
ea37ba09 17240 make_vector_type (type);
f5f8a009 17241
dbc98a8b
KW
17242 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17243 implementation may choose to implement triple vectors using this
17244 attribute. */
17245 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb 17246 if (attr != nullptr && attr->form_is_unsigned ())
dbc98a8b 17247 {
529908cb
TT
17248 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17249 TYPE_LENGTH (type) = attr->as_unsigned ();
dbc98a8b 17250 else
b98664d3 17251 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 17252 "than the total size of elements"));
dbc98a8b
KW
17253 }
17254
39cbfefa
DJ
17255 name = dwarf2_name (die, cu);
17256 if (name)
d0e39ea2 17257 type->set_name (name);
6e70227d 17258
2b4424c3
TT
17259 maybe_set_alignment (cu, die, type);
17260
57567375
TT
17261 struct type *replacement_type = nullptr;
17262 if (cu->language == language_ada)
17263 {
17264 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17265 if (replacement_type != nullptr)
17266 type = replacement_type;
17267 }
17268
0963b4bd 17269 /* Install the type in the die. */
57567375 17270 set_die_type (die, type, cu, replacement_type != nullptr);
7e314c57
JK
17271
17272 /* set_die_type should be already done. */
b4ba55a1
JB
17273 set_descriptive_type (type, die, cu);
17274
7e314c57 17275 return type;
c906108c
SS
17276}
17277
7ca2d3a3 17278static enum dwarf_array_dim_ordering
6e70227d 17279read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
17280{
17281 struct attribute *attr;
17282
17283 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17284
435d3d88 17285 if (attr != nullptr)
1bc397c5
TT
17286 {
17287 LONGEST val = attr->constant_value (-1);
17288 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17289 return (enum dwarf_array_dim_ordering) val;
17290 }
7ca2d3a3 17291
0963b4bd
MS
17292 /* GNU F77 is a special case, as at 08/2004 array type info is the
17293 opposite order to the dwarf2 specification, but data is still
17294 laid out as per normal fortran.
7ca2d3a3 17295
0963b4bd
MS
17296 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17297 version checking. */
7ca2d3a3 17298
905e0470
PM
17299 if (cu->language == language_fortran
17300 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
17301 {
17302 return DW_ORD_row_major;
17303 }
17304
3a3440fb 17305 switch (cu->language_defn->array_ordering ())
7ca2d3a3
DL
17306 {
17307 case array_column_major:
17308 return DW_ORD_col_major;
17309 case array_row_major:
17310 default:
17311 return DW_ORD_row_major;
17312 };
17313}
17314
72019c9c 17315/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 17316 the DIE's type field. */
72019c9c 17317
f792889a 17318static struct type *
72019c9c
GM
17319read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17320{
7e314c57
JK
17321 struct type *domain_type, *set_type;
17322 struct attribute *attr;
f792889a 17323
7e314c57
JK
17324 domain_type = die_type (die, cu);
17325
17326 /* The die_type call above may have already set the type for this DIE. */
17327 set_type = get_die_type (die, cu);
17328 if (set_type)
17329 return set_type;
17330
17331 set_type = create_set_type (NULL, domain_type);
17332
17333 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb
TT
17334 if (attr != nullptr && attr->form_is_unsigned ())
17335 TYPE_LENGTH (set_type) = attr->as_unsigned ();
7e314c57 17336
2b4424c3
TT
17337 maybe_set_alignment (cu, die, set_type);
17338
f792889a 17339 return set_die_type (die, set_type, cu);
72019c9c 17340}
7ca2d3a3 17341
0971de02
TT
17342/* A helper for read_common_block that creates a locexpr baton.
17343 SYM is the symbol which we are marking as computed.
17344 COMMON_DIE is the DIE for the common block.
17345 COMMON_LOC is the location expression attribute for the common
17346 block itself.
17347 MEMBER_LOC is the location expression attribute for the particular
17348 member of the common block that we are processing.
17349 CU is the CU from which the above come. */
17350
17351static void
17352mark_common_block_symbol_computed (struct symbol *sym,
17353 struct die_info *common_die,
17354 struct attribute *common_loc,
17355 struct attribute *member_loc,
17356 struct dwarf2_cu *cu)
17357{
5e22e966 17358 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 17359 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
17360 struct dwarf2_locexpr_baton *baton;
17361 gdb_byte *ptr;
17362 unsigned int cu_off;
08feed99 17363 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
17364 LONGEST offset = 0;
17365
17366 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
17367 gdb_assert (common_loc->form_is_block ());
17368 gdb_assert (member_loc->form_is_block ()
cd6c91b4 17369 || member_loc->form_is_constant ());
0971de02 17370
8d749320 17371 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 17372 baton->per_objfile = per_objfile;
0971de02
TT
17373 baton->per_cu = cu->per_cu;
17374 gdb_assert (baton->per_cu);
17375
17376 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17377
cd6c91b4 17378 if (member_loc->form_is_constant ())
0971de02 17379 {
0826b30a 17380 offset = member_loc->constant_value (0);
0971de02
TT
17381 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17382 }
17383 else
9d2246fc 17384 baton->size += member_loc->as_block ()->size;
0971de02 17385
224c3ddb 17386 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
17387 baton->data = ptr;
17388
17389 *ptr++ = DW_OP_call4;
9c541725 17390 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
17391 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17392 ptr += 4;
17393
cd6c91b4 17394 if (member_loc->form_is_constant ())
0971de02
TT
17395 {
17396 *ptr++ = DW_OP_addr;
17397 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17398 ptr += cu->header.addr_size;
17399 }
17400 else
17401 {
17402 /* We have to copy the data here, because DW_OP_call4 will only
17403 use a DW_AT_location attribute. */
9d2246fc
TT
17404 struct dwarf_block *block = member_loc->as_block ();
17405 memcpy (ptr, block->data, block->size);
17406 ptr += block->size;
0971de02
TT
17407 }
17408
17409 *ptr++ = DW_OP_plus;
17410 gdb_assert (ptr - baton->data == baton->size);
17411
0971de02 17412 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 17413 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
17414}
17415
4357ac6c
TT
17416/* Create appropriate locally-scoped variables for all the
17417 DW_TAG_common_block entries. Also create a struct common_block
17418 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 17419 is used to separate the common blocks name namespace from regular
4357ac6c 17420 variable names. */
c906108c
SS
17421
17422static void
e7c27a73 17423read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17424{
0971de02
TT
17425 struct attribute *attr;
17426
17427 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 17428 if (attr != nullptr)
0971de02
TT
17429 {
17430 /* Support the .debug_loc offsets. */
4fc6c0d5 17431 if (attr->form_is_block ())
dda83cd7 17432 {
0971de02 17433 /* Ok. */
dda83cd7 17434 }
cd6c91b4 17435 else if (attr->form_is_section_offset ())
dda83cd7 17436 {
0971de02
TT
17437 dwarf2_complex_location_expr_complaint ();
17438 attr = NULL;
dda83cd7 17439 }
0971de02 17440 else
dda83cd7 17441 {
0971de02
TT
17442 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17443 "common block member");
17444 attr = NULL;
dda83cd7 17445 }
0971de02
TT
17446 }
17447
639d11d3 17448 if (die->child != NULL)
c906108c 17449 {
5e22e966 17450 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
17451 struct die_info *child_die;
17452 size_t n_entries = 0, size;
17453 struct common_block *common_block;
17454 struct symbol *sym;
74ac6d43 17455
4357ac6c
TT
17456 for (child_die = die->child;
17457 child_die && child_die->tag;
436c571c 17458 child_die = child_die->sibling)
4357ac6c
TT
17459 ++n_entries;
17460
17461 size = (sizeof (struct common_block)
17462 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
17463 common_block
17464 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17465 size);
4357ac6c
TT
17466 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17467 common_block->n_entries = 0;
17468
17469 for (child_die = die->child;
17470 child_die && child_die->tag;
436c571c 17471 child_die = child_die->sibling)
4357ac6c
TT
17472 {
17473 /* Create the symbol in the DW_TAG_common_block block in the current
17474 symbol scope. */
e7c27a73 17475 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
17476 if (sym != NULL)
17477 {
17478 struct attribute *member_loc;
17479
17480 common_block->contents[common_block->n_entries++] = sym;
17481
17482 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17483 cu);
17484 if (member_loc)
17485 {
17486 /* GDB has handled this for a long time, but it is
17487 not specified by DWARF. It seems to have been
17488 emitted by gfortran at least as recently as:
17489 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 17490 complaint (_("Variable in common block has "
0971de02 17491 "DW_AT_data_member_location "
9d8780f0
SM
17492 "- DIE at %s [in module %s]"),
17493 sect_offset_str (child_die->sect_off),
518817b3 17494 objfile_name (objfile));
0971de02 17495
cd6c91b4 17496 if (member_loc->form_is_section_offset ())
0971de02 17497 dwarf2_complex_location_expr_complaint ();
cd6c91b4 17498 else if (member_loc->form_is_constant ()
4fc6c0d5 17499 || member_loc->form_is_block ())
0971de02 17500 {
435d3d88 17501 if (attr != nullptr)
0971de02
TT
17502 mark_common_block_symbol_computed (sym, die, attr,
17503 member_loc, cu);
17504 }
17505 else
17506 dwarf2_complex_location_expr_complaint ();
17507 }
17508 }
c906108c 17509 }
4357ac6c
TT
17510
17511 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17512 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
17513 }
17514}
17515
0114d602 17516/* Create a type for a C++ namespace. */
d9fa45fe 17517
0114d602
DJ
17518static struct type *
17519read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 17520{
5e22e966 17521 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17522 const char *previous_prefix, *name;
9219021c 17523 int is_anonymous;
0114d602
DJ
17524 struct type *type;
17525
17526 /* For extensions, reuse the type of the original namespace. */
17527 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17528 {
17529 struct die_info *ext_die;
17530 struct dwarf2_cu *ext_cu = cu;
9a619af0 17531
0114d602
DJ
17532 ext_die = dwarf2_extension (die, &ext_cu);
17533 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
17534
17535 /* EXT_CU may not be the same as CU.
02142a6c 17536 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
17537 return set_die_type (die, type, cu);
17538 }
9219021c 17539
e142c38c 17540 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
17541
17542 /* Now build the name of the current namespace. */
17543
0114d602
DJ
17544 previous_prefix = determine_prefix (die, cu);
17545 if (previous_prefix[0] != '\0')
17546 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 17547 previous_prefix, name, 0, cu);
0114d602
DJ
17548
17549 /* Create the type. */
19f392bc 17550 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 17551
60531b24 17552 return set_die_type (die, type, cu);
0114d602
DJ
17553}
17554
22cee43f 17555/* Read a namespace scope. */
0114d602
DJ
17556
17557static void
17558read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17559{
5e22e966 17560 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17561 int is_anonymous;
9219021c 17562
5c4e30ca
DC
17563 /* Add a symbol associated to this if we haven't seen the namespace
17564 before. Also, add a using directive if it's an anonymous
17565 namespace. */
9219021c 17566
f2f0e013 17567 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17568 {
17569 struct type *type;
17570
0114d602 17571 type = read_type_die (die, cu);
e7c27a73 17572 new_symbol (die, type, cu);
5c4e30ca 17573
e8e80198 17574 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17575 if (is_anonymous)
0114d602
DJ
17576 {
17577 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17578
eb1e02fd 17579 std::vector<const char *> excludes;
804d2729 17580 add_using_directive (using_directives (cu),
7d93a1e0 17581 previous_prefix, type->name (), NULL,
eb1e02fd 17582 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17583 }
5c4e30ca 17584 }
9219021c 17585
639d11d3 17586 if (die->child != NULL)
d9fa45fe 17587 {
639d11d3 17588 struct die_info *child_die = die->child;
6e70227d 17589
d9fa45fe
DC
17590 while (child_die && child_die->tag)
17591 {
e7c27a73 17592 process_die (child_die, cu);
436c571c 17593 child_die = child_die->sibling;
d9fa45fe
DC
17594 }
17595 }
38d518c9
EZ
17596}
17597
f55ee35c
JK
17598/* Read a Fortran module as type. This DIE can be only a declaration used for
17599 imported module. Still we need that type as local Fortran "use ... only"
17600 declaration imports depend on the created type in determine_prefix. */
17601
17602static struct type *
17603read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17604{
5e22e966 17605 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 17606 const char *module_name;
f55ee35c
JK
17607 struct type *type;
17608
17609 module_name = dwarf2_name (die, cu);
19f392bc 17610 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17611
f55ee35c
JK
17612 return set_die_type (die, type, cu);
17613}
17614
5d7cb8df
JK
17615/* Read a Fortran module. */
17616
17617static void
17618read_module (struct die_info *die, struct dwarf2_cu *cu)
17619{
17620 struct die_info *child_die = die->child;
530e8392
KB
17621 struct type *type;
17622
17623 type = read_type_die (die, cu);
17624 new_symbol (die, type, cu);
5d7cb8df 17625
5d7cb8df
JK
17626 while (child_die && child_die->tag)
17627 {
17628 process_die (child_die, cu);
436c571c 17629 child_die = child_die->sibling;
5d7cb8df
JK
17630 }
17631}
17632
38d518c9
EZ
17633/* Return the name of the namespace represented by DIE. Set
17634 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17635 namespace. */
17636
17637static const char *
e142c38c 17638namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17639{
17640 struct die_info *current_die;
17641 const char *name = NULL;
17642
17643 /* Loop through the extensions until we find a name. */
17644
17645 for (current_die = die;
17646 current_die != NULL;
f2f0e013 17647 current_die = dwarf2_extension (die, &cu))
38d518c9 17648 {
96553a0c
DE
17649 /* We don't use dwarf2_name here so that we can detect the absence
17650 of a name -> anonymous namespace. */
7d45c7c3 17651 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17652
38d518c9
EZ
17653 if (name != NULL)
17654 break;
17655 }
17656
17657 /* Is it an anonymous namespace? */
17658
17659 *is_anonymous = (name == NULL);
17660 if (*is_anonymous)
2b1dbab0 17661 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17662
17663 return name;
d9fa45fe
DC
17664}
17665
c906108c
SS
17666/* Extract all information from a DW_TAG_pointer_type DIE and add to
17667 the user defined type vector. */
17668
f792889a 17669static struct type *
e7c27a73 17670read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17671{
5e22e966 17672 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 17673 struct comp_unit_head *cu_header = &cu->header;
c906108c 17674 struct type *type;
8b2dbe47
KB
17675 struct attribute *attr_byte_size;
17676 struct attribute *attr_address_class;
17677 int byte_size, addr_class;
7e314c57
JK
17678 struct type *target_type;
17679
17680 target_type = die_type (die, cu);
c906108c 17681
7e314c57
JK
17682 /* The die_type call above may have already set the type for this DIE. */
17683 type = get_die_type (die, cu);
17684 if (type)
17685 return type;
17686
17687 type = lookup_pointer_type (target_type);
8b2dbe47 17688
e142c38c 17689 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47 17690 if (attr_byte_size)
529908cb 17691 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
c906108c 17692 else
8b2dbe47
KB
17693 byte_size = cu_header->addr_size;
17694
e142c38c 17695 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47 17696 if (attr_address_class)
529908cb 17697 addr_class = attr_address_class->constant_value (DW_ADDR_none);
8b2dbe47
KB
17698 else
17699 addr_class = DW_ADDR_none;
17700
2b4424c3
TT
17701 ULONGEST alignment = get_alignment (cu, die);
17702
17703 /* If the pointer size, alignment, or address class is different
17704 than the default, create a type variant marked as such and set
17705 the length accordingly. */
17706 if (TYPE_LENGTH (type) != byte_size
17707 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17708 && alignment != TYPE_RAW_ALIGN (type))
17709 || addr_class != DW_ADDR_none)
c906108c 17710 {
5e2b427d 17711 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47 17712 {
314ad88d
PA
17713 type_instance_flags type_flags
17714 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17715 addr_class);
876cecd0
TT
17716 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17717 == 0);
8b2dbe47
KB
17718 type = make_type_with_address_space (type, type_flags);
17719 }
17720 else if (TYPE_LENGTH (type) != byte_size)
17721 {
b98664d3 17722 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17723 }
2b4424c3
TT
17724 else if (TYPE_RAW_ALIGN (type) != alignment)
17725 {
b98664d3 17726 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17727 " - DIE at %s [in module %s]"),
17728 sect_offset_str (die->sect_off),
5e22e966 17729 objfile_name (cu->per_objfile->objfile));
2b4424c3 17730 }
6e70227d 17731 else
9a619af0
MS
17732 {
17733 /* Should we also complain about unhandled address classes? */
17734 }
c906108c 17735 }
8b2dbe47
KB
17736
17737 TYPE_LENGTH (type) = byte_size;
2b4424c3 17738 set_type_align (type, alignment);
f792889a 17739 return set_die_type (die, type, cu);
c906108c
SS
17740}
17741
17742/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17743 the user defined type vector. */
17744
f792889a 17745static struct type *
e7c27a73 17746read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17747{
17748 struct type *type;
17749 struct type *to_type;
17750 struct type *domain;
17751
e7c27a73
DJ
17752 to_type = die_type (die, cu);
17753 domain = die_containing_type (die, cu);
0d5de010 17754
7e314c57
JK
17755 /* The calls above may have already set the type for this DIE. */
17756 type = get_die_type (die, cu);
17757 if (type)
17758 return type;
17759
78134374 17760 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 17761 type = lookup_methodptr_type (to_type);
78134374 17762 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 17763 {
5e22e966 17764 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
17765
17766 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 17767 to_type->fields (), to_type->num_fields (),
a409645d 17768 to_type->has_varargs ());
7078baeb
TT
17769 type = lookup_methodptr_type (new_type);
17770 }
0d5de010
DJ
17771 else
17772 type = lookup_memberptr_type (to_type, domain);
c906108c 17773
f792889a 17774 return set_die_type (die, type, cu);
c906108c
SS
17775}
17776
4297a3f0 17777/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17778 the user defined type vector. */
17779
f792889a 17780static struct type *
4297a3f0 17781read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
dda83cd7 17782 enum type_code refcode)
c906108c 17783{
e7c27a73 17784 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17785 struct type *type, *target_type;
c906108c
SS
17786 struct attribute *attr;
17787
4297a3f0
AV
17788 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17789
7e314c57
JK
17790 target_type = die_type (die, cu);
17791
17792 /* The die_type call above may have already set the type for this DIE. */
17793 type = get_die_type (die, cu);
17794 if (type)
17795 return type;
17796
4297a3f0 17797 type = lookup_reference_type (target_type, refcode);
e142c38c 17798 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17799 if (attr != nullptr)
c906108c 17800 {
529908cb 17801 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
c906108c
SS
17802 }
17803 else
17804 {
107d2387 17805 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17806 }
2b4424c3 17807 maybe_set_alignment (cu, die, type);
f792889a 17808 return set_die_type (die, type, cu);
c906108c
SS
17809}
17810
cf363f18
MW
17811/* Add the given cv-qualifiers to the element type of the array. GCC
17812 outputs DWARF type qualifiers that apply to an array, not the
17813 element type. But GDB relies on the array element type to carry
17814 the cv-qualifiers. This mimics section 6.7.3 of the C99
17815 specification. */
17816
17817static struct type *
17818add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17819 struct type *base_type, int cnst, int voltl)
17820{
17821 struct type *el_type, *inner_array;
17822
17823 base_type = copy_type (base_type);
17824 inner_array = base_type;
17825
78134374 17826 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17827 {
17828 TYPE_TARGET_TYPE (inner_array) =
17829 copy_type (TYPE_TARGET_TYPE (inner_array));
17830 inner_array = TYPE_TARGET_TYPE (inner_array);
17831 }
17832
17833 el_type = TYPE_TARGET_TYPE (inner_array);
17834 cnst |= TYPE_CONST (el_type);
17835 voltl |= TYPE_VOLATILE (el_type);
17836 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17837
17838 return set_die_type (die, base_type, cu);
17839}
17840
f792889a 17841static struct type *
e7c27a73 17842read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17843{
f792889a 17844 struct type *base_type, *cv_type;
c906108c 17845
e7c27a73 17846 base_type = die_type (die, cu);
7e314c57
JK
17847
17848 /* The die_type call above may have already set the type for this DIE. */
17849 cv_type = get_die_type (die, cu);
17850 if (cv_type)
17851 return cv_type;
17852
2f608a3a
KW
17853 /* In case the const qualifier is applied to an array type, the element type
17854 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 17855 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 17856 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17857
f792889a
DJ
17858 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17859 return set_die_type (die, cv_type, cu);
c906108c
SS
17860}
17861
f792889a 17862static struct type *
e7c27a73 17863read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17864{
f792889a 17865 struct type *base_type, *cv_type;
c906108c 17866
e7c27a73 17867 base_type = die_type (die, cu);
7e314c57
JK
17868
17869 /* The die_type call above may have already set the type for this DIE. */
17870 cv_type = get_die_type (die, cu);
17871 if (cv_type)
17872 return cv_type;
17873
cf363f18
MW
17874 /* In case the volatile qualifier is applied to an array type, the
17875 element type is so qualified, not the array type (section 6.7.3
17876 of C99). */
78134374 17877 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17878 return add_array_cv_type (die, cu, base_type, 0, 1);
17879
f792889a
DJ
17880 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17881 return set_die_type (die, cv_type, cu);
c906108c
SS
17882}
17883
06d66ee9
TT
17884/* Handle DW_TAG_restrict_type. */
17885
17886static struct type *
17887read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17888{
17889 struct type *base_type, *cv_type;
17890
17891 base_type = die_type (die, cu);
17892
17893 /* The die_type call above may have already set the type for this DIE. */
17894 cv_type = get_die_type (die, cu);
17895 if (cv_type)
17896 return cv_type;
17897
17898 cv_type = make_restrict_type (base_type);
17899 return set_die_type (die, cv_type, cu);
17900}
17901
a2c2acaf
MW
17902/* Handle DW_TAG_atomic_type. */
17903
17904static struct type *
17905read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17906{
17907 struct type *base_type, *cv_type;
17908
17909 base_type = die_type (die, cu);
17910
17911 /* The die_type call above may have already set the type for this DIE. */
17912 cv_type = get_die_type (die, cu);
17913 if (cv_type)
17914 return cv_type;
17915
17916 cv_type = make_atomic_type (base_type);
17917 return set_die_type (die, cv_type, cu);
17918}
17919
c906108c
SS
17920/* Extract all information from a DW_TAG_string_type DIE and add to
17921 the user defined type vector. It isn't really a user defined type,
17922 but it behaves like one, with other DIE's using an AT_user_def_type
17923 attribute to reference it. */
17924
f792889a 17925static struct type *
e7c27a73 17926read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17927{
5e22e966 17928 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17929 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17930 struct type *type, *range_type, *index_type, *char_type;
17931 struct attribute *attr;
216a7e6b
AB
17932 struct dynamic_prop prop;
17933 bool length_is_constant = true;
17934 LONGEST length;
17935
17936 /* There are a couple of places where bit sizes might be made use of
17937 when parsing a DW_TAG_string_type, however, no producer that we know
17938 of make use of these. Handling bit sizes that are a multiple of the
17939 byte size is easy enough, but what about other bit sizes? Lets deal
17940 with that problem when we have to. Warn about these attributes being
17941 unsupported, then parse the type and ignore them like we always
17942 have. */
17943 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17944 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17945 {
17946 static bool warning_printed = false;
17947 if (!warning_printed)
17948 {
17949 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17950 "currently supported on DW_TAG_string_type."));
17951 warning_printed = true;
17952 }
17953 }
c906108c 17954
e142c38c 17955 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17956 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17957 {
17958 /* The string length describes the location at which the length of
17959 the string can be found. The size of the length field can be
17960 specified with one of the attributes below. */
17961 struct type *prop_type;
17962 struct attribute *len
17963 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17964 if (len == nullptr)
17965 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17966 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17967 {
17968 /* Pass 0 as the default as we know this attribute is constant
17969 and the default value will not be returned. */
0826b30a 17970 LONGEST sz = len->constant_value (0);
293e7e51 17971 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17972 }
17973 else
17974 {
17975 /* If the size is not specified then we assume it is the size of
17976 an address on this target. */
293e7e51 17977 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17978 }
17979
17980 /* Convert the attribute into a dynamic property. */
17981 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17982 length = 1;
17983 else
17984 length_is_constant = false;
17985 }
17986 else if (attr != nullptr)
17987 {
17988 /* This DW_AT_string_length just contains the length with no
17989 indirection. There's no need to create a dynamic property in this
17990 case. Pass 0 for the default value as we know it will not be
17991 returned in this case. */
0826b30a 17992 length = attr->constant_value (0);
216a7e6b
AB
17993 }
17994 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17995 {
216a7e6b 17996 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17997 length = attr->constant_value (1);
c906108c
SS
17998 }
17999 else
18000 {
216a7e6b
AB
18001 /* Use 1 as a fallback length if we have nothing else. */
18002 length = 1;
c906108c 18003 }
6ccb9162 18004
46bf5051 18005 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
18006 if (length_is_constant)
18007 range_type = create_static_range_type (NULL, index_type, 1, length);
18008 else
18009 {
18010 struct dynamic_prop low_bound;
18011
8c2e4e06 18012 low_bound.set_const_val (1);
216a7e6b
AB
18013 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
18014 }
3b7538c0
UW
18015 char_type = language_string_char_type (cu->language_defn, gdbarch);
18016 type = create_string_type (NULL, char_type, range_type);
6ccb9162 18017
f792889a 18018 return set_die_type (die, type, cu);
c906108c
SS
18019}
18020
4d804846
JB
18021/* Assuming that DIE corresponds to a function, returns nonzero
18022 if the function is prototyped. */
18023
18024static int
18025prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
18026{
18027 struct attribute *attr;
18028
18029 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c45bc3f8 18030 if (attr && attr->as_boolean ())
4d804846
JB
18031 return 1;
18032
18033 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 18034 is only meaningful for C, but the concept also extends to other
4d804846
JB
18035 languages that allow unprototyped functions (Eg: Objective C).
18036 For all other languages, assume that functions are always
18037 prototyped. */
18038 if (cu->language != language_c
18039 && cu->language != language_objc
18040 && cu->language != language_opencl)
18041 return 1;
18042
18043 /* RealView does not emit DW_AT_prototyped. We can not distinguish
18044 prototyped and unprototyped functions; default to prototyped,
18045 since that is more common in modern code (and RealView warns
18046 about unprototyped functions). */
18047 if (producer_is_realview (cu->producer))
18048 return 1;
18049
18050 return 0;
18051}
18052
c906108c
SS
18053/* Handle DIES due to C code like:
18054
18055 struct foo
c5aa993b
JM
18056 {
18057 int (*funcp)(int a, long l);
18058 int b;
18059 };
c906108c 18060
0963b4bd 18061 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 18062
f792889a 18063static struct type *
e7c27a73 18064read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18065{
5e22e966 18066 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
18067 struct type *type; /* Type that this function returns. */
18068 struct type *ftype; /* Function that returns above type. */
c906108c
SS
18069 struct attribute *attr;
18070
e7c27a73 18071 type = die_type (die, cu);
7e314c57
JK
18072
18073 /* The die_type call above may have already set the type for this DIE. */
18074 ftype = get_die_type (die, cu);
18075 if (ftype)
18076 return ftype;
18077
0c8b41f1 18078 ftype = lookup_function_type (type);
c906108c 18079
4d804846 18080 if (prototyped_function_p (die, cu))
27e69b7a 18081 ftype->set_is_prototyped (true);
c906108c 18082
c055b101
CV
18083 /* Store the calling convention in the type if it's available in
18084 the subroutine die. Otherwise set the calling convention to
18085 the default value DW_CC_normal. */
18086 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf 18087 if (attr != nullptr
529908cb 18088 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
d0922fcf 18089 TYPE_CALLING_CONVENTION (ftype)
529908cb 18090 = (enum dwarf_calling_convention) attr->constant_value (0);
54fcddd0
UW
18091 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
18092 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
18093 else
18094 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 18095
743649fd
MW
18096 /* Record whether the function returns normally to its caller or not
18097 if the DWARF producer set that information. */
18098 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
c45bc3f8 18099 if (attr && attr->as_boolean ())
743649fd
MW
18100 TYPE_NO_RETURN (ftype) = 1;
18101
76c10ea2
GM
18102 /* We need to add the subroutine type to the die immediately so
18103 we don't infinitely recurse when dealing with parameters
0963b4bd 18104 declared as the same subroutine type. */
76c10ea2 18105 set_die_type (die, ftype, cu);
6e70227d 18106
639d11d3 18107 if (die->child != NULL)
c906108c 18108 {
bb5ed363 18109 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 18110 struct die_info *child_die;
8072405b 18111 int nparams, iparams;
c906108c
SS
18112
18113 /* Count the number of parameters.
dda83cd7
SM
18114 FIXME: GDB currently ignores vararg functions, but knows about
18115 vararg member functions. */
8072405b 18116 nparams = 0;
639d11d3 18117 child_die = die->child;
c906108c
SS
18118 while (child_die && child_die->tag)
18119 {
18120 if (child_die->tag == DW_TAG_formal_parameter)
18121 nparams++;
18122 else if (child_die->tag == DW_TAG_unspecified_parameters)
1d6286ed
SM
18123 ftype->set_has_varargs (true);
18124
436c571c 18125 child_die = child_die->sibling;
c906108c
SS
18126 }
18127
18128 /* Allocate storage for parameters and fill them in. */
5e33d5f4 18129 ftype->set_num_fields (nparams);
3cabb6b0
SM
18130 ftype->set_fields
18131 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 18132
8072405b
JK
18133 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18134 even if we error out during the parameters reading below. */
18135 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 18136 ftype->field (iparams).set_type (void_type);
8072405b
JK
18137
18138 iparams = 0;
639d11d3 18139 child_die = die->child;
c906108c
SS
18140 while (child_die && child_die->tag)
18141 {
18142 if (child_die->tag == DW_TAG_formal_parameter)
18143 {
3ce3b1ba
PA
18144 struct type *arg_type;
18145
18146 /* DWARF version 2 has no clean way to discern C++
18147 static and non-static member functions. G++ helps
18148 GDB by marking the first parameter for non-static
18149 member functions (which is the this pointer) as
18150 artificial. We pass this information to
18151 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18152
18153 DWARF version 3 added DW_AT_object_pointer, which GCC
18154 4.5 does not yet generate. */
e142c38c 18155 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 18156 if (attr != nullptr)
c45bc3f8 18157 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
c906108c 18158 else
9c37b5ae 18159 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
18160 arg_type = die_type (child_die, cu);
18161
18162 /* RealView does not mark THIS as const, which the testsuite
18163 expects. GCC marks THIS as const in method definitions,
18164 but not in the class specifications (GCC PR 43053). */
18165 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18166 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18167 {
18168 int is_this = 0;
18169 struct dwarf2_cu *arg_cu = cu;
18170 const char *name = dwarf2_name (child_die, cu);
18171
18172 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 18173 if (attr != nullptr)
3ce3b1ba
PA
18174 {
18175 /* If the compiler emits this, use it. */
18176 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18177 is_this = 1;
18178 }
18179 else if (name && strcmp (name, "this") == 0)
18180 /* Function definitions will have the argument names. */
18181 is_this = 1;
18182 else if (name == NULL && iparams == 0)
18183 /* Declarations may not have the names, so like
18184 elsewhere in GDB, assume an artificial first
18185 argument is "this". */
18186 is_this = 1;
18187
18188 if (is_this)
18189 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18190 arg_type, 0);
18191 }
18192
5d14b6e5 18193 ftype->field (iparams).set_type (arg_type);
c906108c
SS
18194 iparams++;
18195 }
436c571c 18196 child_die = child_die->sibling;
c906108c
SS
18197 }
18198 }
18199
76c10ea2 18200 return ftype;
c906108c
SS
18201}
18202
f792889a 18203static struct type *
e7c27a73 18204read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18205{
5e22e966 18206 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 18207 const char *name = NULL;
3c8e0968 18208 struct type *this_type, *target_type;
c906108c 18209
94af9270 18210 name = dwarf2_full_name (NULL, die, cu);
19f392bc 18211 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
8f53807e 18212 this_type->set_target_is_stub (true);
f792889a 18213 set_die_type (die, this_type, cu);
3c8e0968
DE
18214 target_type = die_type (die, cu);
18215 if (target_type != this_type)
18216 TYPE_TARGET_TYPE (this_type) = target_type;
18217 else
18218 {
18219 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18220 spec and cause infinite loops in GDB. */
b98664d3 18221 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
18222 "- DIE at %s [in module %s]"),
18223 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
18224 TYPE_TARGET_TYPE (this_type) = NULL;
18225 }
e4003a34
TV
18226 if (name == NULL)
18227 {
18228 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18229 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18230 Handle these by just returning the target type, rather than
18231 constructing an anonymous typedef type and trying to handle this
18232 elsewhere. */
18233 set_die_type (die, target_type, cu);
18234 return target_type;
18235 }
f792889a 18236 return this_type;
c906108c
SS
18237}
18238
a625a8c9
TT
18239/* Helper for get_dwarf2_rational_constant that computes the value of
18240 a given gmp_mpz given an attribute. */
18241
18242static void
18243get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
18244{
18245 /* GCC will sometimes emit a 16-byte constant value as a DWARF
18246 location expression that pushes an implicit value. */
18247 if (attr->form == DW_FORM_exprloc)
18248 {
18249 dwarf_block *blk = attr->as_block ();
18250 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18251 {
18252 uint64_t len;
18253 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18254 blk->data + blk->size,
18255 &len);
18256 if (ptr - blk->data + len <= blk->size)
18257 {
18258 mpz_import (value->val, len,
18259 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18260 1, 0, 0, ptr);
18261 return;
18262 }
18263 }
18264
18265 /* On failure set it to 1. */
18266 *value = gdb_mpz (1);
18267 }
18268 else if (attr->form_is_block ())
18269 {
18270 dwarf_block *blk = attr->as_block ();
18271 mpz_import (value->val, blk->size,
18272 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18273 1, 0, 0, blk->data);
18274 }
18275 else
18276 *value = gdb_mpz (attr->constant_value (1));
18277}
18278
09584414
JB
18279/* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18280 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18281
18282 If the numerator and/or numerator attribute is missing,
18283 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18284 untouched. */
18285
18286static void
18287get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
5cde1d82 18288 gdb_mpz *numerator, gdb_mpz *denominator)
09584414
JB
18289{
18290 struct attribute *num_attr, *denom_attr;
18291
18292 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18293 if (num_attr == nullptr)
18294 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18295 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18296
18297 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18298 if (denom_attr == nullptr)
18299 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18300 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18301
18302 if (num_attr == nullptr || denom_attr == nullptr)
18303 return;
18304
a625a8c9
TT
18305 get_mpz (cu, numerator, num_attr);
18306 get_mpz (cu, denominator, denom_attr);
09584414
JB
18307}
18308
18309/* Same as get_dwarf2_rational_constant, but extracting an unsigned
18310 rational constant, rather than a signed one.
18311
18312 If the rational constant has a negative value, a complaint
18313 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18314
18315static void
18316get_dwarf2_unsigned_rational_constant (struct die_info *die,
18317 struct dwarf2_cu *cu,
5cde1d82
TT
18318 gdb_mpz *numerator,
18319 gdb_mpz *denominator)
09584414 18320{
5cde1d82
TT
18321 gdb_mpz num (1);
18322 gdb_mpz denom (1);
09584414
JB
18323
18324 get_dwarf2_rational_constant (die, cu, &num, &denom);
5cde1d82 18325 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
09584414 18326 {
5cde1d82
TT
18327 mpz_neg (num.val, num.val);
18328 mpz_neg (denom.val, denom.val);
09584414 18329 }
5cde1d82 18330 else if (mpz_sgn (num.val) == -1)
09584414
JB
18331 {
18332 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18333 " in DIE at %s"),
18334 sect_offset_str (die->sect_off));
18335 return;
18336 }
5cde1d82 18337 else if (mpz_sgn (denom.val) == -1)
09584414
JB
18338 {
18339 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18340 " in DIE at %s"),
18341 sect_offset_str (die->sect_off));
18342 return;
18343 }
18344
5cde1d82
TT
18345 *numerator = std::move (num);
18346 *denominator = std::move (denom);
09584414
JB
18347}
18348
bbcdf9ab
TT
18349/* Assuming that ENCODING is a string whose contents starting at the
18350 K'th character is "_nn" where "nn" is a decimal number, scan that
18351 number and set RESULT to the value. K is updated to point to the
18352 character immediately following the number.
18353
18354 If the string does not conform to the format described above, false
18355 is returned, and K may or may not be changed. */
18356
18357static bool
18358ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
18359{
18360 /* The next character should be an underscore ('_') followed
18361 by a digit. */
18362 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
18363 return false;
18364
18365 /* Skip the underscore. */
18366 k++;
18367 int start = k;
18368
18369 /* Determine the number of digits for our number. */
18370 while (isdigit (encoding[k]))
18371 k++;
18372 if (k == start)
18373 return false;
18374
18375 std::string copy (&encoding[start], k - start);
18376 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
18377 return false;
18378
18379 return true;
18380}
18381
18382/* Scan two numbers from ENCODING at OFFSET, assuming the string is of
18383 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
18384 DENOM, update OFFSET, and return true on success. Return false on
18385 failure. */
18386
18387static bool
18388ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
18389 gdb_mpz *num, gdb_mpz *denom)
18390{
18391 if (!ada_get_gnat_encoded_number (encoding, offset, num))
18392 return false;
18393 return ada_get_gnat_encoded_number (encoding, offset, denom);
18394}
18395
09584414 18396/* Assuming DIE corresponds to a fixed point type, finish the creation
bbcdf9ab
TT
18397 of the corresponding TYPE by setting its type-specific data. CU is
18398 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
18399 encodings. It is nullptr if the GNAT encoding should be
18400 ignored. */
09584414
JB
18401
18402static void
bbcdf9ab
TT
18403finish_fixed_point_type (struct type *type, const char *suffix,
18404 struct die_info *die, struct dwarf2_cu *cu)
09584414 18405{
09584414
JB
18406 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18407 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18408
bbcdf9ab
TT
18409 /* If GNAT encodings are preferred, don't examine the
18410 attributes. */
18411 struct attribute *attr = nullptr;
18412 if (suffix == nullptr)
18413 {
18414 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18415 if (attr == nullptr)
18416 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18417 if (attr == nullptr)
18418 attr = dwarf2_attr (die, DW_AT_small, cu);
18419 }
09584414 18420
5cde1d82
TT
18421 /* Numerator and denominator of our fixed-point type's scaling factor.
18422 The default is a scaling factor of 1, which we use as a fallback
18423 when we are not able to decode it (problem with the debugging info,
18424 unsupported forms, bug in GDB, etc...). Using that as the default
18425 allows us to at least print the unscaled value, which might still
18426 be useful to a user. */
18427 gdb_mpz scale_num (1);
18428 gdb_mpz scale_denom (1);
18429
09584414
JB
18430 if (attr == nullptr)
18431 {
bbcdf9ab
TT
18432 int offset = 0;
18433 if (suffix != nullptr
18434 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18435 &scale_denom)
18436 /* The number might be encoded as _nn_dd_nn_dd, where the
18437 second ratio is the 'small value. In this situation, we
18438 want the second value. */
18439 && (suffix[offset] != '_'
18440 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18441 &scale_denom)))
18442 {
18443 /* Found it. */
18444 }
18445 else
18446 {
18447 /* Scaling factor not found. Assume a scaling factor of 1,
18448 and hope for the best. At least the user will be able to
18449 see the encoded value. */
18450 scale_num = 1;
18451 scale_denom = 1;
18452 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18453 sect_offset_str (die->sect_off));
18454 }
09584414
JB
18455 }
18456 else if (attr->name == DW_AT_binary_scale)
18457 {
18458 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18459 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18460
5cde1d82 18461 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
09584414
JB
18462 }
18463 else if (attr->name == DW_AT_decimal_scale)
18464 {
18465 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18466 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18467
5cde1d82 18468 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
09584414
JB
18469 }
18470 else if (attr->name == DW_AT_small)
18471 {
18472 struct die_info *scale_die;
18473 struct dwarf2_cu *scale_cu = cu;
18474
18475 scale_die = follow_die_ref (die, attr, &scale_cu);
18476 if (scale_die->tag == DW_TAG_constant)
18477 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18478 &scale_num, &scale_denom);
18479 else
18480 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18481 " (DIE at %s)"),
18482 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18483 }
18484 else
18485 {
18486 complaint (_("unsupported scale attribute %s for fixed-point type"
18487 " (DIE at %s)"),
18488 dwarf_attr_name (attr->name),
18489 sect_offset_str (die->sect_off));
18490 }
18491
2a12c336 18492 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
5cde1d82
TT
18493 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18494 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
09584414
JB
18495 mpq_canonicalize (scaling_factor.val);
18496}
18497
bbcdf9ab
TT
18498/* The gnat-encoding suffix for fixed point. */
18499
18500#define GNAT_FIXED_POINT_SUFFIX "___XF_"
18501
18502/* If NAME encodes an Ada fixed-point type, return a pointer to the
18503 "XF" suffix of the name. The text after this is what encodes the
18504 'small and 'delta information. Otherwise, return nullptr. */
18505
18506static const char *
18507gnat_encoded_fixed_point_type_info (const char *name)
18508{
18509 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18510}
18511
9b790ce7
UW
18512/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18513 (which may be different from NAME) to the architecture back-end to allow
18514 it to guess the correct format if necessary. */
18515
18516static struct type *
18517dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 18518 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 18519{
08feed99 18520 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
18521 const struct floatformat **format;
18522 struct type *type;
18523
18524 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18525 if (format)
103a685e 18526 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 18527 else
77b7c781 18528 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
18529
18530 return type;
18531}
18532
eb77c9df
AB
18533/* Allocate an integer type of size BITS and name NAME. */
18534
18535static struct type *
18536dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18537 int bits, int unsigned_p, const char *name)
18538{
18539 struct type *type;
18540
18541 /* Versions of Intel's C Compiler generate an integer type called "void"
18542 instead of using DW_TAG_unspecified_type. This has been seen on
18543 at least versions 14, 17, and 18. */
35ee2dc2
AB
18544 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18545 && strcmp (name, "void") == 0)
eb77c9df
AB
18546 type = objfile_type (objfile)->builtin_void;
18547 else
18548 type = init_integer_type (objfile, bits, unsigned_p, name);
18549
18550 return type;
18551}
18552
09584414
JB
18553/* Return true if DIE has a DW_AT_small attribute whose value is
18554 a constant rational, where both the numerator and denominator
18555 are equal to zero.
18556
18557 CU is the DIE's Compilation Unit. */
18558
18559static bool
18560has_zero_over_zero_small_attribute (struct die_info *die,
18561 struct dwarf2_cu *cu)
18562{
18563 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18564 if (attr == nullptr)
18565 return false;
18566
18567 struct dwarf2_cu *scale_cu = cu;
18568 struct die_info *scale_die
18569 = follow_die_ref (die, attr, &scale_cu);
18570
18571 if (scale_die->tag != DW_TAG_constant)
18572 return false;
18573
5cde1d82 18574 gdb_mpz num (1), denom (1);
09584414 18575 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
5cde1d82 18576 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
09584414
JB
18577}
18578
8bdc1658
AB
18579/* Initialise and return a floating point type of size BITS suitable for
18580 use as a component of a complex number. The NAME_HINT is passed through
18581 when initialising the floating point type and is the name of the complex
18582 type.
18583
18584 As DWARF doesn't currently provide an explicit name for the components
18585 of a complex number, but it can be helpful to have these components
18586 named, we try to select a suitable name based on the size of the
18587 component. */
18588static struct type *
18589dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18590 struct objfile *objfile,
103a685e
TT
18591 int bits, const char *name_hint,
18592 enum bfd_endian byte_order)
8bdc1658 18593{
08feed99 18594 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
18595 struct type *tt = nullptr;
18596
35add35e
AB
18597 /* Try to find a suitable floating point builtin type of size BITS.
18598 We're going to use the name of this type as the name for the complex
18599 target type that we are about to create. */
1db455a7 18600 switch (cu->language)
8bdc1658 18601 {
1db455a7
AB
18602 case language_fortran:
18603 switch (bits)
18604 {
18605 case 32:
18606 tt = builtin_f_type (gdbarch)->builtin_real;
18607 break;
18608 case 64:
18609 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18610 break;
18611 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18612 case 128:
18613 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18614 break;
18615 }
8bdc1658 18616 break;
1db455a7
AB
18617 default:
18618 switch (bits)
18619 {
18620 case 32:
18621 tt = builtin_type (gdbarch)->builtin_float;
18622 break;
18623 case 64:
18624 tt = builtin_type (gdbarch)->builtin_double;
18625 break;
18626 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18627 case 128:
18628 tt = builtin_type (gdbarch)->builtin_long_double;
18629 break;
18630 }
8bdc1658
AB
18631 break;
18632 }
18633
35add35e
AB
18634 /* If the type we found doesn't match the size we were looking for, then
18635 pretend we didn't find a type at all, the complex target type we
18636 create will then be nameless. */
a12e5744 18637 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
18638 tt = nullptr;
18639
7d93a1e0 18640 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 18641 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
18642}
18643
c906108c
SS
18644/* Find a representation of a given base type and install
18645 it in the TYPE field of the die. */
18646
f792889a 18647static struct type *
e7c27a73 18648read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18649{
5e22e966 18650 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
18651 struct type *type;
18652 struct attribute *attr;
19f392bc 18653 int encoding = 0, bits = 0;
15d034d0 18654 const char *name;
34877895 18655 gdbarch *arch;
c906108c 18656
e142c38c 18657 attr = dwarf2_attr (die, DW_AT_encoding, cu);
529908cb
TT
18658 if (attr != nullptr && attr->form_is_constant ())
18659 encoding = attr->constant_value (0);
e142c38c 18660 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18661 if (attr != nullptr)
529908cb 18662 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
39cbfefa 18663 name = dwarf2_name (die, cu);
6ccb9162 18664 if (!name)
34877895 18665 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 18666
08feed99 18667 arch = objfile->arch ();
103a685e
TT
18668 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18669
34877895 18670 attr = dwarf2_attr (die, DW_AT_endianity, cu);
529908cb 18671 if (attr != nullptr && attr->form_is_constant ())
103a685e 18672 {
529908cb 18673 int endianity = attr->constant_value (0);
103a685e
TT
18674
18675 switch (endianity)
18676 {
18677 case DW_END_big:
18678 byte_order = BFD_ENDIAN_BIG;
18679 break;
18680 case DW_END_little:
18681 byte_order = BFD_ENDIAN_LITTLE;
18682 break;
18683 default:
18684 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18685 break;
18686 }
18687 }
6ccb9162 18688
09584414
JB
18689 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18690 && cu->language == language_ada
18691 && has_zero_over_zero_small_attribute (die, cu))
18692 {
18693 /* brobecker/2018-02-24: This is a fixed point type for which
18694 the scaling factor is represented as fraction whose value
18695 does not make sense (zero divided by zero), so we should
18696 normally never see these. However, there is a small category
18697 of fixed point types for which GNAT is unable to provide
18698 the scaling factor via the standard DWARF mechanisms, and
18699 for which the info is provided via the GNAT encodings instead.
bbcdf9ab 18700 This is likely what this DIE is about. */
09584414
JB
18701 encoding = (encoding == DW_ATE_signed_fixed
18702 ? DW_ATE_signed
18703 : DW_ATE_unsigned);
18704 }
18705
bbcdf9ab
TT
18706 /* With GNAT encodings, fixed-point information will be encoded in
18707 the type name. Note that this can also occur with the above
18708 zero-over-zero case, which is why this is a separate "if" rather
18709 than an "else if". */
18710 const char *gnat_encoding_suffix = nullptr;
18711 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18712 && cu->language == language_ada
18713 && name != nullptr)
18714 {
18715 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18716 if (gnat_encoding_suffix != nullptr)
18717 {
18718 gdb_assert (startswith (gnat_encoding_suffix,
18719 GNAT_FIXED_POINT_SUFFIX));
18720 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18721 name, gnat_encoding_suffix - name);
18722 /* Use -1 here so that SUFFIX points at the "_" after the
18723 "XF". */
18724 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18725
18726 encoding = (encoding == DW_ATE_signed
18727 ? DW_ATE_signed_fixed
18728 : DW_ATE_unsigned_fixed);
18729 }
18730 }
18731
6ccb9162 18732 switch (encoding)
c906108c 18733 {
6ccb9162
UW
18734 case DW_ATE_address:
18735 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 18736 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 18737 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
18738 break;
18739 case DW_ATE_boolean:
19f392bc 18740 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
18741 break;
18742 case DW_ATE_complex_float:
103a685e
TT
18743 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18744 byte_order);
78134374 18745 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
18746 {
18747 if (name == nullptr)
18748 {
18749 struct obstack *obstack
5e22e966 18750 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 18751 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
18752 nullptr);
18753 }
18754 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18755 }
18756 else
18757 type = init_complex_type (name, type);
6ccb9162
UW
18758 break;
18759 case DW_ATE_decimal_float:
19f392bc 18760 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
18761 break;
18762 case DW_ATE_float:
103a685e 18763 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
18764 break;
18765 case DW_ATE_signed:
eb77c9df 18766 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18767 break;
18768 case DW_ATE_unsigned:
3b2b8fea
TT
18769 if (cu->language == language_fortran
18770 && name
61012eef 18771 && startswith (name, "character("))
19f392bc
UW
18772 type = init_character_type (objfile, bits, 1, name);
18773 else
eb77c9df 18774 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
18775 break;
18776 case DW_ATE_signed_char:
6e70227d 18777 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
18778 || cu->language == language_pascal
18779 || cu->language == language_fortran)
19f392bc
UW
18780 type = init_character_type (objfile, bits, 0, name);
18781 else
eb77c9df 18782 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18783 break;
18784 case DW_ATE_unsigned_char:
868a0084 18785 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 18786 || cu->language == language_pascal
c44af4eb
TT
18787 || cu->language == language_fortran
18788 || cu->language == language_rust)
19f392bc
UW
18789 type = init_character_type (objfile, bits, 1, name);
18790 else
eb77c9df 18791 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 18792 break;
75079b2b 18793 case DW_ATE_UTF:
53e710ac 18794 {
53e710ac
PA
18795 if (bits == 16)
18796 type = builtin_type (arch)->builtin_char16;
18797 else if (bits == 32)
18798 type = builtin_type (arch)->builtin_char32;
18799 else
18800 {
b98664d3 18801 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 18802 bits);
eb77c9df 18803 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
18804 }
18805 return set_die_type (die, type, cu);
18806 }
75079b2b 18807 break;
09584414
JB
18808 case DW_ATE_signed_fixed:
18809 type = init_fixed_point_type (objfile, bits, 0, name);
bbcdf9ab 18810 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414
JB
18811 break;
18812 case DW_ATE_unsigned_fixed:
18813 type = init_fixed_point_type (objfile, bits, 1, name);
bbcdf9ab 18814 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414 18815 break;
75079b2b 18816
6ccb9162 18817 default:
b98664d3 18818 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 18819 dwarf_type_encoding_name (encoding));
77b7c781 18820 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18821 break;
c906108c 18822 }
6ccb9162 18823
0114d602 18824 if (name && strcmp (name, "char") == 0)
15152a54 18825 type->set_has_no_signedness (true);
0114d602 18826
2b4424c3
TT
18827 maybe_set_alignment (cu, die, type);
18828
db558e34 18829 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
34877895 18830
20a5fcbd
TT
18831 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18832 {
18833 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
529908cb 18834 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
20a5fcbd 18835 {
529908cb 18836 unsigned real_bit_size = attr->as_unsigned ();
20a5fcbd
TT
18837 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18838 /* Only use the attributes if they make sense together. */
18839 if (attr == nullptr
529908cb
TT
18840 || (attr->as_unsigned () + real_bit_size
18841 <= 8 * TYPE_LENGTH (type)))
20a5fcbd
TT
18842 {
18843 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18844 = real_bit_size;
18845 if (attr != nullptr)
18846 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
529908cb 18847 = attr->as_unsigned ();
20a5fcbd
TT
18848 }
18849 }
18850 }
18851
f792889a 18852 return set_die_type (die, type, cu);
c906108c
SS
18853}
18854
80180f79
SA
18855/* Parse dwarf attribute if it's a block, reference or constant and put the
18856 resulting value of the attribute into struct bound_prop.
18857 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18858
18859static int
18860attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
18861 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18862 struct type *default_type)
80180f79
SA
18863{
18864 struct dwarf2_property_baton *baton;
5e22e966 18865 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
18866 struct objfile *objfile = per_objfile->objfile;
18867 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 18868
9a49df9d
AB
18869 gdb_assert (default_type != NULL);
18870
80180f79
SA
18871 if (attr == NULL || prop == NULL)
18872 return 0;
18873
4fc6c0d5 18874 if (attr->form_is_block ())
80180f79 18875 {
8d749320 18876 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18877 baton->property_type = default_type;
80180f79 18878 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18879 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18880
18881 struct dwarf_block *block = attr->as_block ();
18882 baton->locexpr.size = block->size;
18883 baton->locexpr.data = block->data;
216a7e6b
AB
18884 switch (attr->name)
18885 {
18886 case DW_AT_string_length:
18887 baton->locexpr.is_reference = true;
18888 break;
18889 default:
18890 baton->locexpr.is_reference = false;
18891 break;
18892 }
8c2e4e06
SM
18893
18894 prop->set_locexpr (baton);
18895 gdb_assert (prop->baton () != NULL);
80180f79 18896 }
cd6c91b4 18897 else if (attr->form_is_ref ())
80180f79
SA
18898 {
18899 struct dwarf2_cu *target_cu = cu;
18900 struct die_info *target_die;
18901 struct attribute *target_attr;
18902
18903 target_die = follow_die_ref (die, attr, &target_cu);
18904 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18905 if (target_attr == NULL)
18906 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18907 target_cu);
80180f79
SA
18908 if (target_attr == NULL)
18909 return 0;
18910
df25ebbd 18911 switch (target_attr->name)
80180f79 18912 {
df25ebbd 18913 case DW_AT_location:
cd6c91b4 18914 if (target_attr->form_is_section_offset ())
df25ebbd 18915 {
8d749320 18916 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18917 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18918 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
8c2e4e06
SM
18919 prop->set_loclist (baton);
18920 gdb_assert (prop->baton () != NULL);
df25ebbd 18921 }
4fc6c0d5 18922 else if (target_attr->form_is_block ())
df25ebbd 18923 {
8d749320 18924 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18925 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18926 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18927 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18928 struct dwarf_block *block = target_attr->as_block ();
18929 baton->locexpr.size = block->size;
18930 baton->locexpr.data = block->data;
9a49df9d 18931 baton->locexpr.is_reference = true;
8c2e4e06
SM
18932 prop->set_locexpr (baton);
18933 gdb_assert (prop->baton () != NULL);
df25ebbd
JB
18934 }
18935 else
18936 {
18937 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18938 "dynamic property");
18939 return 0;
18940 }
18941 break;
18942 case DW_AT_data_member_location:
18943 {
18944 LONGEST offset;
18945
18946 if (!handle_data_member_location (target_die, target_cu,
18947 &offset))
18948 return 0;
18949
8d749320 18950 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18951 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18952 target_cu);
df25ebbd
JB
18953 baton->offset_info.offset = offset;
18954 baton->offset_info.type = die_type (target_die, target_cu);
8c2e4e06 18955 prop->set_addr_offset (baton);
df25ebbd
JB
18956 break;
18957 }
80180f79
SA
18958 }
18959 }
cd6c91b4 18960 else if (attr->form_is_constant ())
8c2e4e06 18961 prop->set_const_val (attr->constant_value (0));
80180f79
SA
18962 else
18963 {
18964 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18965 dwarf2_name (die, cu));
18966 return 0;
18967 }
18968
18969 return 1;
18970}
18971
09ba997f 18972/* See read.h. */
9a49df9d 18973
09ba997f 18974struct type *
293e7e51 18975dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 18976{
9a49df9d
AB
18977 struct type *int_type;
18978
18979 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18980#define TRY_TYPE(F) \
18981 int_type = (unsigned_p \
18982 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18983 : objfile_type (objfile)->builtin_ ## F); \
18984 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18985 return int_type
18986
18987 TRY_TYPE (char);
18988 TRY_TYPE (short);
18989 TRY_TYPE (int);
18990 TRY_TYPE (long);
18991 TRY_TYPE (long_long);
18992
18993#undef TRY_TYPE
18994
18995 gdb_assert_not_reached ("unable to find suitable integer type");
18996}
18997
09ba997f 18998/* See read.h. */
11a8b164 18999
09ba997f 19000struct type *
293e7e51 19001dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 19002{
293e7e51
SM
19003 int addr_size = this->per_cu->addr_size ();
19004 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
19005}
19006
b86352cf
AB
19007/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
19008 present (which is valid) then compute the default type based on the
19009 compilation units address size. */
19010
19011static struct type *
19012read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
19013{
19014 struct type *index_type = die_type (die, cu);
19015
19016 /* Dwarf-2 specifications explicitly allows to create subrange types
19017 without specifying a base type.
19018 In that case, the base type must be set to the type of
19019 the lower bound, upper bound or count, in that order, if any of these
19020 three attributes references an object that has a type.
19021 If no base type is found, the Dwarf-2 specifications say that
19022 a signed integer type of size equal to the size of an address should
19023 be used.
19024 For the following C code: `extern char gdb_int [];'
19025 GCC produces an empty range DIE.
19026 FIXME: muller/2010-05-28: Possible references to object for low bound,
19027 high bound or count are not yet handled by this code. */
78134374 19028 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 19029 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
19030
19031 return index_type;
19032}
19033
a02abb62
JB
19034/* Read the given DW_AT_subrange DIE. */
19035
f792889a 19036static struct type *
a02abb62
JB
19037read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
19038{
4c9ad8c2 19039 struct type *base_type, *orig_base_type;
a02abb62
JB
19040 struct type *range_type;
19041 struct attribute *attr;
729efb13 19042 struct dynamic_prop low, high;
4fae6e18 19043 int low_default_is_valid;
c451ebe5 19044 int high_bound_is_count = 0;
15d034d0 19045 const char *name;
d359392f 19046 ULONGEST negative_mask;
e77813c8 19047
b86352cf
AB
19048 orig_base_type = read_subrange_index_type (die, cu);
19049
4c9ad8c2
TT
19050 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
19051 whereas the real type might be. So, we use ORIG_BASE_TYPE when
19052 creating the range type, but we use the result of check_typedef
19053 when examining properties of the type. */
19054 base_type = check_typedef (orig_base_type);
a02abb62 19055
7e314c57
JK
19056 /* The die_type call above may have already set the type for this DIE. */
19057 range_type = get_die_type (die, cu);
19058 if (range_type)
19059 return range_type;
19060
8c2e4e06 19061 high.set_const_val (0);
729efb13 19062
4fae6e18
JK
19063 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
19064 omitting DW_AT_lower_bound. */
19065 switch (cu->language)
6e70227d 19066 {
4fae6e18
JK
19067 case language_c:
19068 case language_cplus:
8c2e4e06 19069 low.set_const_val (0);
4fae6e18
JK
19070 low_default_is_valid = 1;
19071 break;
19072 case language_fortran:
8c2e4e06 19073 low.set_const_val (1);
4fae6e18
JK
19074 low_default_is_valid = 1;
19075 break;
19076 case language_d:
4fae6e18 19077 case language_objc:
c44af4eb 19078 case language_rust:
8c2e4e06 19079 low.set_const_val (0);
4fae6e18
JK
19080 low_default_is_valid = (cu->header.version >= 4);
19081 break;
19082 case language_ada:
19083 case language_m2:
19084 case language_pascal:
8c2e4e06 19085 low.set_const_val (1);
4fae6e18
JK
19086 low_default_is_valid = (cu->header.version >= 4);
19087 break;
19088 default:
8c2e4e06 19089 low.set_const_val (0);
4fae6e18
JK
19090 low_default_is_valid = 0;
19091 break;
a02abb62
JB
19092 }
19093
e142c38c 19094 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 19095 if (attr != nullptr)
9a49df9d 19096 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 19097 else if (!low_default_is_valid)
b98664d3 19098 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
19099 "- DIE at %s [in module %s]"),
19100 sect_offset_str (die->sect_off),
5e22e966 19101 objfile_name (cu->per_objfile->objfile));
a02abb62 19102
506f5c41
TV
19103 struct attribute *attr_ub, *attr_count;
19104 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 19105 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 19106 {
506f5c41 19107 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 19108 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 19109 {
c451ebe5 19110 /* If bounds are constant do the final calculation here. */
8c2e4e06
SM
19111 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
19112 high.set_const_val (low.const_val () + high.const_val () - 1);
c451ebe5
SA
19113 else
19114 high_bound_is_count = 1;
c2ff108b 19115 }
506f5c41
TV
19116 else
19117 {
19118 if (attr_ub != NULL)
19119 complaint (_("Unresolved DW_AT_upper_bound "
19120 "- DIE at %s [in module %s]"),
19121 sect_offset_str (die->sect_off),
5e22e966 19122 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
19123 if (attr_count != NULL)
19124 complaint (_("Unresolved DW_AT_count "
19125 "- DIE at %s [in module %s]"),
19126 sect_offset_str (die->sect_off),
5e22e966 19127 objfile_name (cu->per_objfile->objfile));
506f5c41 19128 }
e77813c8 19129 }
a02abb62 19130
4e962e74
TT
19131 LONGEST bias = 0;
19132 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 19133 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 19134 bias = bias_attr->constant_value (0);
4e962e74 19135
dbb9c2b1
JB
19136 /* Normally, the DWARF producers are expected to use a signed
19137 constant form (Eg. DW_FORM_sdata) to express negative bounds.
19138 But this is unfortunately not always the case, as witnessed
19139 with GCC, for instance, where the ambiguous DW_FORM_dataN form
19140 is used instead. To work around that ambiguity, we treat
19141 the bounds as signed, and thus sign-extend their values, when
19142 the base type is signed. */
6e70227d 19143 negative_mask =
d359392f 19144 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
8c2e4e06 19145 if (low.kind () == PROP_CONST
c6d940a9 19146 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
8c2e4e06
SM
19147 low.set_const_val (low.const_val () | negative_mask);
19148 if (high.kind () == PROP_CONST
c6d940a9 19149 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
8c2e4e06 19150 high.set_const_val (high.const_val () | negative_mask);
43bbcdc2 19151
5bbd8269
AB
19152 /* Check for bit and byte strides. */
19153 struct dynamic_prop byte_stride_prop;
19154 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
19155 if (attr_byte_stride != nullptr)
19156 {
293e7e51 19157 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
19158 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
19159 prop_type);
19160 }
19161
19162 struct dynamic_prop bit_stride_prop;
19163 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
19164 if (attr_bit_stride != nullptr)
19165 {
19166 /* It only makes sense to have either a bit or byte stride. */
19167 if (attr_byte_stride != nullptr)
19168 {
19169 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19170 "- DIE at %s [in module %s]"),
19171 sect_offset_str (die->sect_off),
5e22e966 19172 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
19173 attr_bit_stride = nullptr;
19174 }
19175 else
19176 {
293e7e51 19177 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
19178 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19179 prop_type);
19180 }
19181 }
19182
19183 if (attr_byte_stride != nullptr
19184 || attr_bit_stride != nullptr)
19185 {
19186 bool byte_stride_p = (attr_byte_stride != nullptr);
19187 struct dynamic_prop *stride
19188 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19189
19190 range_type
19191 = create_range_type_with_stride (NULL, orig_base_type, &low,
19192 &high, bias, stride, byte_stride_p);
19193 }
19194 else
19195 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 19196
c451ebe5 19197 if (high_bound_is_count)
599088e3 19198 range_type->bounds ()->flag_upper_bound_is_count = 1;
c451ebe5 19199
c2ff108b
JK
19200 /* Ada expects an empty array on no boundary attributes. */
19201 if (attr == NULL && cu->language != language_ada)
8c2e4e06 19202 range_type->bounds ()->high.set_undefined ();
c2ff108b 19203
39cbfefa
DJ
19204 name = dwarf2_name (die, cu);
19205 if (name)
d0e39ea2 19206 range_type->set_name (name);
6e70227d 19207
e142c38c 19208 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 19209 if (attr != nullptr)
529908cb 19210 TYPE_LENGTH (range_type) = attr->constant_value (0);
a02abb62 19211
2b4424c3
TT
19212 maybe_set_alignment (cu, die, range_type);
19213
7e314c57
JK
19214 set_die_type (die, range_type, cu);
19215
19216 /* set_die_type should be already done. */
b4ba55a1
JB
19217 set_descriptive_type (range_type, die, cu);
19218
7e314c57 19219 return range_type;
a02abb62 19220}
6e70227d 19221
f792889a 19222static struct type *
81a17f79
JB
19223read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19224{
19225 struct type *type;
81a17f79 19226
5e22e966 19227 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 19228 type->set_name (dwarf2_name (die, cu));
81a17f79 19229
74a2f8ff 19230 /* In Ada, an unspecified type is typically used when the description
85102364 19231 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
19232 such a type, we treat it as a stub, and try to resolve it later on,
19233 when needed. */
19234 if (cu->language == language_ada)
b4b73759 19235 type->set_is_stub (true);
74a2f8ff 19236
f792889a 19237 return set_die_type (die, type, cu);
81a17f79 19238}
a02abb62 19239
639d11d3
DC
19240/* Read a single die and all its descendents. Set the die's sibling
19241 field to NULL; set other fields in the die correctly, and set all
19242 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19243 location of the info_ptr after reading all of those dies. PARENT
19244 is the parent of the die in question. */
19245
19246static struct die_info *
dee91e82 19247read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
19248 const gdb_byte *info_ptr,
19249 const gdb_byte **new_info_ptr,
dee91e82 19250 struct die_info *parent)
639d11d3
DC
19251{
19252 struct die_info *die;
d521ce57 19253 const gdb_byte *cur_ptr;
639d11d3 19254
3e225074 19255 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
19256 if (die == NULL)
19257 {
19258 *new_info_ptr = cur_ptr;
19259 return NULL;
19260 }
93311388 19261 store_in_ref_table (die, reader->cu);
639d11d3 19262
3e225074 19263 if (die->has_children)
bf6af496 19264 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
19265 else
19266 {
19267 die->child = NULL;
19268 *new_info_ptr = cur_ptr;
19269 }
19270
19271 die->sibling = NULL;
19272 die->parent = parent;
19273 return die;
19274}
19275
19276/* Read a die, all of its descendents, and all of its siblings; set
19277 all of the fields of all of the dies correctly. Arguments are as
19278 in read_die_and_children. */
19279
19280static struct die_info *
bf6af496 19281read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
19282 const gdb_byte *info_ptr,
19283 const gdb_byte **new_info_ptr,
bf6af496 19284 struct die_info *parent)
639d11d3
DC
19285{
19286 struct die_info *first_die, *last_sibling;
d521ce57 19287 const gdb_byte *cur_ptr;
639d11d3 19288
c906108c 19289 cur_ptr = info_ptr;
639d11d3
DC
19290 first_die = last_sibling = NULL;
19291
19292 while (1)
c906108c 19293 {
639d11d3 19294 struct die_info *die
dee91e82 19295 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 19296
1d325ec1 19297 if (die == NULL)
c906108c 19298 {
639d11d3
DC
19299 *new_info_ptr = cur_ptr;
19300 return first_die;
c906108c 19301 }
1d325ec1
DJ
19302
19303 if (!first_die)
19304 first_die = die;
c906108c 19305 else
1d325ec1
DJ
19306 last_sibling->sibling = die;
19307
19308 last_sibling = die;
c906108c 19309 }
c906108c
SS
19310}
19311
bf6af496
DE
19312/* Read a die, all of its descendents, and all of its siblings; set
19313 all of the fields of all of the dies correctly. Arguments are as
19314 in read_die_and_children.
19315 This the main entry point for reading a DIE and all its children. */
19316
19317static struct die_info *
19318read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
19319 const gdb_byte *info_ptr,
19320 const gdb_byte **new_info_ptr,
bf6af496
DE
19321 struct die_info *parent)
19322{
19323 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19324 new_info_ptr, parent);
19325
b4f54984 19326 if (dwarf_die_debug)
bf6af496
DE
19327 {
19328 fprintf_unfiltered (gdb_stdlog,
19329 "Read die from %s@0x%x of %s:\n",
96b79293 19330 reader->die_section->get_name (),
bf6af496
DE
19331 (unsigned) (info_ptr - reader->die_section->buffer),
19332 bfd_get_filename (reader->abfd));
b4f54984 19333 dump_die (die, dwarf_die_debug);
bf6af496
DE
19334 }
19335
19336 return die;
19337}
19338
3019eac3
DE
19339/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19340 attributes.
19341 The caller is responsible for filling in the extra attributes
19342 and updating (*DIEP)->num_attrs.
19343 Set DIEP to point to a newly allocated die with its information,
3e225074 19344 except for its child, sibling, and parent fields. */
93311388 19345
d521ce57 19346static const gdb_byte *
3019eac3 19347read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 19348 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 19349 int num_extra_attrs)
93311388 19350{
b64f50a1 19351 unsigned int abbrev_number, bytes_read, i;
7c32eebb 19352 const struct abbrev_info *abbrev;
93311388
DE
19353 struct die_info *die;
19354 struct dwarf2_cu *cu = reader->cu;
19355 bfd *abfd = reader->abfd;
19356
9c541725 19357 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
19358 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19359 info_ptr += bytes_read;
19360 if (!abbrev_number)
19361 {
19362 *diep = NULL;
93311388
DE
19363 return info_ptr;
19364 }
19365
685af9cd 19366 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 19367 if (!abbrev)
348e048f
DE
19368 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19369 abbrev_number,
19370 bfd_get_filename (abfd));
19371
3019eac3 19372 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 19373 die->sect_off = sect_off;
93311388
DE
19374 die->tag = abbrev->tag;
19375 die->abbrev = abbrev_number;
3e225074 19376 die->has_children = abbrev->has_children;
93311388 19377
3019eac3
DE
19378 /* Make the result usable.
19379 The caller needs to update num_attrs after adding the extra
19380 attributes. */
93311388
DE
19381 die->num_attrs = abbrev->num_attrs;
19382
7a5f294d 19383 bool any_need_reprocess = false;
93311388 19384 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e 19385 {
7a5f294d
TT
19386 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19387 info_ptr);
19388 if (die->attrs[i].requires_reprocessing_p ())
19389 any_need_reprocess = true;
18a8505e
AT
19390 }
19391
052c8bb8 19392 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
529908cb
TT
19393 if (attr != nullptr && attr->form_is_unsigned ())
19394 cu->str_offsets_base = attr->as_unsigned ();
93311388 19395
41144253 19396 attr = die->attr (DW_AT_loclists_base);
19397 if (attr != nullptr)
529908cb 19398 cu->loclist_base = attr->as_unsigned ();
41144253 19399
a39fdb41 19400 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
19401 if (maybe_addr_base.has_value ())
19402 cu->addr_base = *maybe_addr_base;
d0ce17d8
CT
19403
19404 attr = die->attr (DW_AT_rnglists_base);
19405 if (attr != nullptr)
2b0c7f41 19406 cu->rnglists_base = attr->as_unsigned ();
d0ce17d8 19407
7a5f294d
TT
19408 if (any_need_reprocess)
19409 {
19410 for (i = 0; i < abbrev->num_attrs; ++i)
19411 {
19412 if (die->attrs[i].requires_reprocessing_p ())
19413 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19414 }
19415 }
93311388 19416 *diep = die;
93311388
DE
19417 return info_ptr;
19418}
19419
3019eac3
DE
19420/* Read a die and all its attributes.
19421 Set DIEP to point to a newly allocated die with its information,
3e225074 19422 except for its child, sibling, and parent fields. */
3019eac3 19423
d521ce57 19424static const gdb_byte *
3019eac3 19425read_full_die (const struct die_reader_specs *reader,
3e225074 19426 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 19427{
d521ce57 19428 const gdb_byte *result;
bf6af496 19429
3e225074 19430 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 19431
b4f54984 19432 if (dwarf_die_debug)
bf6af496
DE
19433 {
19434 fprintf_unfiltered (gdb_stdlog,
19435 "Read die from %s@0x%x of %s:\n",
96b79293 19436 reader->die_section->get_name (),
bf6af496
DE
19437 (unsigned) (info_ptr - reader->die_section->buffer),
19438 bfd_get_filename (reader->abfd));
b4f54984 19439 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
19440 }
19441
19442 return result;
3019eac3 19443}
433df2d4 19444\f
c906108c 19445
72bf9492
DJ
19446/* Returns nonzero if TAG represents a type that we might generate a partial
19447 symbol for. */
19448
19449static int
d8f62e84 19450is_type_tag_for_partial (int tag, enum language lang)
72bf9492
DJ
19451{
19452 switch (tag)
19453 {
19454#if 0
19455 /* Some types that would be reasonable to generate partial symbols for,
d8f62e84
TT
19456 that we don't at present. Note that normally this does not
19457 matter, mainly because C compilers don't give names to these
19458 types, but instead emit DW_TAG_typedef. */
72bf9492
DJ
19459 case DW_TAG_file_type:
19460 case DW_TAG_ptr_to_member_type:
19461 case DW_TAG_set_type:
19462 case DW_TAG_string_type:
19463 case DW_TAG_subroutine_type:
19464#endif
d8f62e84
TT
19465
19466 /* GNAT may emit an array with a name, but no typedef, so we
19467 need to make a symbol in this case. */
19468 case DW_TAG_array_type:
19469 return lang == language_ada;
19470
72bf9492
DJ
19471 case DW_TAG_base_type:
19472 case DW_TAG_class_type:
680b30c7 19473 case DW_TAG_interface_type:
72bf9492
DJ
19474 case DW_TAG_enumeration_type:
19475 case DW_TAG_structure_type:
19476 case DW_TAG_subrange_type:
19477 case DW_TAG_typedef:
19478 case DW_TAG_union_type:
19479 return 1;
19480 default:
19481 return 0;
19482 }
19483}
19484
19485/* Load all DIEs that are interesting for partial symbols into memory. */
19486
19487static struct partial_die_info *
dee91e82 19488load_partial_dies (const struct die_reader_specs *reader,
d521ce57 19489 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 19490{
dee91e82 19491 struct dwarf2_cu *cu = reader->cu;
5e22e966 19492 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 19493 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 19494 unsigned int bytes_read;
5afb4e99 19495 unsigned int load_all = 0;
72bf9492
DJ
19496 int nesting_level = 1;
19497
19498 parent_die = NULL;
19499 last_die = NULL;
19500
7adf1e79
DE
19501 gdb_assert (cu->per_cu != NULL);
19502 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
19503 load_all = 1;
19504
72bf9492
DJ
19505 cu->partial_dies
19506 = htab_create_alloc_ex (cu->header.length / 12,
19507 partial_die_hash,
19508 partial_die_eq,
19509 NULL,
19510 &cu->comp_unit_obstack,
19511 hashtab_obstack_allocate,
19512 dummy_obstack_deallocate);
19513
72bf9492
DJ
19514 while (1)
19515 {
7c32eebb
TT
19516 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19517 &bytes_read);
72bf9492
DJ
19518
19519 /* A NULL abbrev means the end of a series of children. */
19520 if (abbrev == NULL)
19521 {
19522 if (--nesting_level == 0)
cd9983dd
YQ
19523 return first_die;
19524
72bf9492
DJ
19525 info_ptr += bytes_read;
19526 last_die = parent_die;
19527 parent_die = parent_die->die_parent;
19528 continue;
19529 }
19530
98bfdba5
PA
19531 /* Check for template arguments. We never save these; if
19532 they're seen, we just mark the parent, and go on our way. */
19533 if (parent_die != NULL
19534 && cu->language == language_cplus
19535 && (abbrev->tag == DW_TAG_template_type_param
19536 || abbrev->tag == DW_TAG_template_value_param))
19537 {
19538 parent_die->has_template_arguments = 1;
19539
19540 if (!load_all)
19541 {
19542 /* We don't need a partial DIE for the template argument. */
dee91e82 19543 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19544 continue;
19545 }
19546 }
19547
0d99eb77 19548 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
19549 Skip their other children. */
19550 if (!load_all
19551 && cu->language == language_cplus
19552 && parent_die != NULL
f9b5d5ea
TV
19553 && parent_die->tag == DW_TAG_subprogram
19554 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 19555 {
dee91e82 19556 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19557 continue;
19558 }
19559
5afb4e99
DJ
19560 /* Check whether this DIE is interesting enough to save. Normally
19561 we would not be interested in members here, but there may be
19562 later variables referencing them via DW_AT_specification (for
19563 static members). */
19564 if (!load_all
d8f62e84 19565 && !is_type_tag_for_partial (abbrev->tag, cu->language)
72929c62 19566 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
19567 && abbrev->tag != DW_TAG_enumerator
19568 && abbrev->tag != DW_TAG_subprogram
b1dc1806 19569 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 19570 && abbrev->tag != DW_TAG_lexical_block
72bf9492 19571 && abbrev->tag != DW_TAG_variable
5afb4e99 19572 && abbrev->tag != DW_TAG_namespace
f55ee35c 19573 && abbrev->tag != DW_TAG_module
95554aad 19574 && abbrev->tag != DW_TAG_member
74921315
KS
19575 && abbrev->tag != DW_TAG_imported_unit
19576 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
19577 {
19578 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19579 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
19580 continue;
19581 }
19582
6f06d47b
YQ
19583 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19584 abbrev);
cd9983dd 19585
48fbe735 19586 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
19587
19588 /* This two-pass algorithm for processing partial symbols has a
19589 high cost in cache pressure. Thus, handle some simple cases
19590 here which cover the majority of C partial symbols. DIEs
19591 which neither have specification tags in them, nor could have
19592 specification tags elsewhere pointing at them, can simply be
19593 processed and discarded.
19594
19595 This segment is also optional; scan_partial_symbols and
19596 add_partial_symbol will handle these DIEs if we chain
19597 them in normally. When compilers which do not emit large
19598 quantities of duplicate debug information are more common,
19599 this code can probably be removed. */
19600
19601 /* Any complete simple types at the top level (pretty much all
19602 of them, for a language without namespaces), can be processed
19603 directly. */
19604 if (parent_die == NULL
cd9983dd
YQ
19605 && pdi.has_specification == 0
19606 && pdi.is_declaration == 0
19607 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19608 || pdi.tag == DW_TAG_base_type
d8f62e84 19609 || pdi.tag == DW_TAG_array_type
cd9983dd 19610 || pdi.tag == DW_TAG_subrange_type))
72bf9492 19611 {
7d00ffec 19612 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
19613 add_partial_symbol (&pdi, cu);
19614
cd9983dd 19615 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19616 continue;
19617 }
19618
d8228535
JK
19619 /* The exception for DW_TAG_typedef with has_children above is
19620 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 19621 type_name_or_error will error on such types later.
d8228535
JK
19622
19623 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19624 it could not find the child DIEs referenced later, this is checked
19625 above. In correct DWARF DW_TAG_typedef should have no children. */
19626
cd9983dd 19627 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 19628 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 19629 "- DIE at %s [in module %s]"),
cd9983dd 19630 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 19631
72bf9492
DJ
19632 /* If we're at the second level, and we're an enumerator, and
19633 our parent has no specification (meaning possibly lives in a
19634 namespace elsewhere), then we can add the partial symbol now
19635 instead of queueing it. */
cd9983dd 19636 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
19637 && parent_die != NULL
19638 && parent_die->die_parent == NULL
19639 && parent_die->tag == DW_TAG_enumeration_type
19640 && parent_die->has_specification == 0)
19641 {
7d00ffec 19642 if (pdi.raw_name == NULL)
b98664d3 19643 complaint (_("malformed enumerator DIE ignored"));
72bf9492 19644 else if (building_psymtab)
f0fbb768 19645 add_partial_symbol (&pdi, cu);
72bf9492 19646
cd9983dd 19647 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19648 continue;
19649 }
19650
cd9983dd 19651 struct partial_die_info *part_die
6f06d47b 19652 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 19653
72bf9492
DJ
19654 /* We'll save this DIE so link it in. */
19655 part_die->die_parent = parent_die;
19656 part_die->die_sibling = NULL;
19657 part_die->die_child = NULL;
19658
19659 if (last_die && last_die == parent_die)
19660 last_die->die_child = part_die;
19661 else if (last_die)
19662 last_die->die_sibling = part_die;
19663
19664 last_die = part_die;
19665
19666 if (first_die == NULL)
19667 first_die = part_die;
19668
19669 /* Maybe add the DIE to the hash table. Not all DIEs that we
19670 find interesting need to be in the hash table, because we
19671 also have the parent/sibling/child chains; only those that we
19672 might refer to by offset later during partial symbol reading.
19673
19674 For now this means things that might have be the target of a
19675 DW_AT_specification, DW_AT_abstract_origin, or
19676 DW_AT_extension. DW_AT_extension will refer only to
19677 namespaces; DW_AT_abstract_origin refers to functions (and
19678 many things under the function DIE, but we do not recurse
19679 into function DIEs during partial symbol reading) and
19680 possibly variables as well; DW_AT_specification refers to
19681 declarations. Declarations ought to have the DW_AT_declaration
19682 flag. It happens that GCC forgets to put it in sometimes, but
19683 only for functions, not for types.
19684
19685 Adding more things than necessary to the hash table is harmless
19686 except for the performance cost. Adding too few will result in
5afb4e99
DJ
19687 wasted time in find_partial_die, when we reread the compilation
19688 unit with load_all_dies set. */
72bf9492 19689
5afb4e99 19690 if (load_all
72929c62 19691 || abbrev->tag == DW_TAG_constant
5afb4e99 19692 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
19693 || abbrev->tag == DW_TAG_variable
19694 || abbrev->tag == DW_TAG_namespace
19695 || part_die->is_declaration)
19696 {
19697 void **slot;
19698
19699 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
19700 to_underlying (part_die->sect_off),
19701 INSERT);
72bf9492
DJ
19702 *slot = part_die;
19703 }
19704
72bf9492 19705 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 19706 we have no reason to follow the children of structures; for other
98bfdba5
PA
19707 languages we have to, so that we can get at method physnames
19708 to infer fully qualified class names, for DW_AT_specification,
19709 and for C++ template arguments. For C++, we also look one level
19710 inside functions to find template arguments (if the name of the
19711 function does not already contain the template arguments).
bc30ff58 19712
0a4b0913
AB
19713 For Ada and Fortran, we need to scan the children of subprograms
19714 and lexical blocks as well because these languages allow the
19715 definition of nested entities that could be interesting for the
19716 debugger, such as nested subprograms for instance. */
72bf9492 19717 if (last_die->has_children
5afb4e99
DJ
19718 && (load_all
19719 || last_die->tag == DW_TAG_namespace
f55ee35c 19720 || last_die->tag == DW_TAG_module
72bf9492 19721 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
19722 || (cu->language == language_cplus
19723 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
19724 && (last_die->raw_name == NULL
19725 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
19726 || (cu->language != language_c
19727 && (last_die->tag == DW_TAG_class_type
680b30c7 19728 || last_die->tag == DW_TAG_interface_type
72bf9492 19729 || last_die->tag == DW_TAG_structure_type
bc30ff58 19730 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
19731 || ((cu->language == language_ada
19732 || cu->language == language_fortran)
bc30ff58
JB
19733 && (last_die->tag == DW_TAG_subprogram
19734 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
19735 {
19736 nesting_level++;
19737 parent_die = last_die;
19738 continue;
19739 }
19740
19741 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19742 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
19743
19744 /* Back to the top, do it again. */
19745 }
19746}
19747
6f06d47b 19748partial_die_info::partial_die_info (sect_offset sect_off_,
7c32eebb 19749 const struct abbrev_info *abbrev)
6f06d47b
YQ
19750 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19751{
19752}
19753
7d00ffec
TT
19754/* See class definition. */
19755
19756const char *
19757partial_die_info::name (dwarf2_cu *cu)
19758{
19759 if (!canonical_name && raw_name != nullptr)
19760 {
19761 struct objfile *objfile = cu->per_objfile->objfile;
19762 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19763 canonical_name = 1;
19764 }
19765
19766 return raw_name;
19767}
19768
35cc7ed7
YQ
19769/* Read a minimal amount of information into the minimal die structure.
19770 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 19771
48fbe735
YQ
19772const gdb_byte *
19773partial_die_info::read (const struct die_reader_specs *reader,
19774 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 19775{
dee91e82 19776 struct dwarf2_cu *cu = reader->cu;
976ca316 19777 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 19778 unsigned int i;
c5aa993b 19779 int has_low_pc_attr = 0;
c906108c 19780 int has_high_pc_attr = 0;
91da1414 19781 int high_pc_relative = 0;
c906108c 19782
fd0a254f 19783 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 19784 {
e7da7f8f 19785 attribute attr;
7a5f294d 19786 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18a8505e 19787 /* String and address offsets that need to do the reprocessing have
dda83cd7 19788 already been read at this point, so there is no need to wait until
18a8505e 19789 the loop terminates to do the reprocessing. */
7a5f294d 19790 if (attr.requires_reprocessing_p ())
d0ce17d8 19791 read_attribute_reprocess (reader, &attr, tag);
c906108c 19792 /* Store the data if it is of an attribute we want to keep in a
dda83cd7 19793 partial symbol table. */
c906108c
SS
19794 switch (attr.name)
19795 {
19796 case DW_AT_name:
48fbe735 19797 switch (tag)
71c25dea
TT
19798 {
19799 case DW_TAG_compile_unit:
95554aad 19800 case DW_TAG_partial_unit:
348e048f 19801 case DW_TAG_type_unit:
71c25dea
TT
19802 /* Compilation units have a DW_AT_name that is a filename, not
19803 a source language identifier. */
19804 case DW_TAG_enumeration_type:
19805 case DW_TAG_enumerator:
19806 /* These tags always have simple identifiers already; no need
19807 to canonicalize them. */
7d00ffec 19808 canonical_name = 1;
2c830f54 19809 raw_name = attr.as_string ();
71c25dea
TT
19810 break;
19811 default:
7d00ffec 19812 canonical_name = 0;
2c830f54 19813 raw_name = attr.as_string ();
71c25dea
TT
19814 break;
19815 }
c906108c 19816 break;
31ef98ae 19817 case DW_AT_linkage_name:
c906108c 19818 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19819 /* Note that both forms of linkage name might appear. We
19820 assume they will be the same, and we only store the last
19821 one we see. */
95f982e5 19822 linkage_name = attr.as_string ();
c906108c
SS
19823 break;
19824 case DW_AT_low_pc:
19825 has_low_pc_attr = 1;
95f982e5 19826 lowpc = attr.as_address ();
c906108c
SS
19827 break;
19828 case DW_AT_high_pc:
19829 has_high_pc_attr = 1;
95f982e5 19830 highpc = attr.as_address ();
cd6c91b4 19831 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 19832 high_pc_relative = 1;
c906108c
SS
19833 break;
19834 case DW_AT_location:
dda83cd7
SM
19835 /* Support the .debug_loc offsets. */
19836 if (attr.form_is_block ())
19837 {
9d2246fc 19838 d.locdesc = attr.as_block ();
dda83cd7
SM
19839 }
19840 else if (attr.form_is_section_offset ())
19841 {
4d3c2250 19842 dwarf2_complex_location_expr_complaint ();
dda83cd7
SM
19843 }
19844 else
19845 {
4d3c2250
KB
19846 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19847 "partial symbol information");
dda83cd7 19848 }
c906108c 19849 break;
c906108c 19850 case DW_AT_external:
c45bc3f8 19851 is_external = attr.as_boolean ();
c906108c
SS
19852 break;
19853 case DW_AT_declaration:
c45bc3f8 19854 is_declaration = attr.as_boolean ();
c906108c
SS
19855 break;
19856 case DW_AT_type:
48fbe735 19857 has_type = 1;
c906108c
SS
19858 break;
19859 case DW_AT_abstract_origin:
19860 case DW_AT_specification:
72bf9492 19861 case DW_AT_extension:
48fbe735 19862 has_specification = 1;
0826b30a 19863 spec_offset = attr.get_ref_die_offset ();
48fbe735 19864 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19865 || cu->per_cu->is_dwz);
c906108c
SS
19866 break;
19867 case DW_AT_sibling:
19868 /* Ignore absolute siblings, they might point outside of
19869 the current compile unit. */
19870 if (attr.form == DW_FORM_ref_addr)
b98664d3 19871 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19872 else
b9502d3f 19873 {
48fbe735 19874 const gdb_byte *buffer = reader->buffer;
0826b30a 19875 sect_offset off = attr.get_ref_die_offset ();
9c541725 19876 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19877
19878 if (sibling_ptr < info_ptr)
b98664d3 19879 complaint (_("DW_AT_sibling points backwards"));
22869d73 19880 else if (sibling_ptr > reader->buffer_end)
a0194fa8 19881 reader->die_section->overflow_complaint ();
b9502d3f 19882 else
48fbe735 19883 sibling = sibling_ptr;
b9502d3f 19884 }
c906108c 19885 break;
dda83cd7
SM
19886 case DW_AT_byte_size:
19887 has_byte_size = 1;
19888 break;
19889 case DW_AT_const_value:
19890 has_const_value = 1;
19891 break;
68511cec
CES
19892 case DW_AT_calling_convention:
19893 /* DWARF doesn't provide a way to identify a program's source-level
19894 entry point. DW_AT_calling_convention attributes are only meant
19895 to describe functions' calling conventions.
19896
19897 However, because it's a necessary piece of information in
0c1b455e
TT
19898 Fortran, and before DWARF 4 DW_CC_program was the only
19899 piece of debugging information whose definition refers to
19900 a 'main program' at all, several compilers marked Fortran
19901 main programs with DW_CC_program --- even when those
19902 functions use the standard calling conventions.
19903
19904 Although DWARF now specifies a way to provide this
19905 information, we support this practice for backward
19906 compatibility. */
529908cb 19907 if (attr.constant_value (0) == DW_CC_program
0c1b455e 19908 && cu->language == language_fortran)
48fbe735 19909 main_subprogram = 1;
68511cec 19910 break;
481860b3 19911 case DW_AT_inline:
529908cb
TT
19912 {
19913 LONGEST value = attr.constant_value (-1);
19914 if (value == DW_INL_inlined
19915 || value == DW_INL_declared_inlined)
19916 may_be_inlined = 1;
19917 }
481860b3 19918 break;
95554aad
TT
19919
19920 case DW_AT_import:
48fbe735 19921 if (tag == DW_TAG_imported_unit)
36586728 19922 {
0826b30a 19923 d.sect_off = attr.get_ref_die_offset ();
48fbe735 19924 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19925 || cu->per_cu->is_dwz);
19926 }
95554aad
TT
19927 break;
19928
0c1b455e 19929 case DW_AT_main_subprogram:
c45bc3f8 19930 main_subprogram = attr.as_boolean ();
0c1b455e
TT
19931 break;
19932
05caa1d2
TT
19933 case DW_AT_ranges:
19934 {
2b0c7f41
SM
19935 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19936 on DWARF version). */
19937 ULONGEST ranges_offset = attr.as_unsigned ();
19938
19939 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19940 this value. */
19941 if (tag != DW_TAG_compile_unit)
19942 ranges_offset += cu->gnu_ranges_base;
19943
05caa1d2 19944 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
d0ce17d8 19945 nullptr, tag))
05caa1d2
TT
19946 has_pc_info = 1;
19947 }
19948 break;
19949
c906108c
SS
19950 default:
19951 break;
19952 }
19953 }
19954
10d06d82
TT
19955 /* For Ada, if both the name and the linkage name appear, we prefer
19956 the latter. This lets "catch exception" work better, regardless
19957 of the order in which the name and linkage name were emitted.
19958 Really, though, this is just a workaround for the fact that gdb
19959 doesn't store both the name and the linkage name. */
19960 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 19961 raw_name = linkage_name;
10d06d82 19962
91da1414 19963 if (high_pc_relative)
48fbe735 19964 highpc += lowpc;
91da1414 19965
9373cf26
JK
19966 if (has_low_pc_attr && has_high_pc_attr)
19967 {
19968 /* When using the GNU linker, .gnu.linkonce. sections are used to
19969 eliminate duplicate copies of functions and vtables and such.
19970 The linker will arbitrarily choose one and discard the others.
19971 The AT_*_pc values for such functions refer to local labels in
19972 these sections. If the section from that file was discarded, the
19973 labels are not in the output, so the relocs get a value of 0.
19974 If this is a discarded function, mark the pc bounds as invalid,
19975 so that GDB will ignore it. */
976ca316 19976 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 19977 {
976ca316 19978 struct objfile *objfile = per_objfile->objfile;
08feed99 19979 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19980
b98664d3 19981 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19982 "for DIE at %s [in module %s]"),
48fbe735
YQ
19983 paddress (gdbarch, lowpc),
19984 sect_offset_str (sect_off),
9d8780f0 19985 objfile_name (objfile));
9373cf26
JK
19986 }
19987 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19988 else if (lowpc >= highpc)
9373cf26 19989 {
976ca316 19990 struct objfile *objfile = per_objfile->objfile;
08feed99 19991 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19992
b98664d3 19993 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19994 "for DIE at %s [in module %s]"),
48fbe735
YQ
19995 paddress (gdbarch, lowpc),
19996 paddress (gdbarch, highpc),
19997 sect_offset_str (sect_off),
9c541725 19998 objfile_name (objfile));
9373cf26
JK
19999 }
20000 else
48fbe735 20001 has_pc_info = 1;
9373cf26 20002 }
85cbf3d3 20003
c906108c
SS
20004 return info_ptr;
20005}
20006
72bf9492
DJ
20007/* Find a cached partial DIE at OFFSET in CU. */
20008
d590ff25
YQ
20009struct partial_die_info *
20010dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
20011{
20012 struct partial_die_info *lookup_die = NULL;
6f06d47b 20013 struct partial_die_info part_die (sect_off);
72bf9492 20014
9a3c8263 20015 lookup_die = ((struct partial_die_info *)
d590ff25 20016 htab_find_with_hash (partial_dies, &part_die,
9c541725 20017 to_underlying (sect_off)));
72bf9492 20018
72bf9492
DJ
20019 return lookup_die;
20020}
20021
348e048f
DE
20022/* Find a partial DIE at OFFSET, which may or may not be in CU,
20023 except in the case of .debug_types DIEs which do not reference
20024 outside their CU (they do however referencing other types via
55f1336d 20025 DW_FORM_ref_sig8). */
72bf9492 20026
122cf0f2 20027static const struct cu_partial_die_info
9c541725 20028find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 20029{
976ca316
SM
20030 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20031 struct objfile *objfile = per_objfile->objfile;
5afb4e99 20032 struct partial_die_info *pd = NULL;
72bf9492 20033
36586728 20034 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 20035 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 20036 {
d590ff25 20037 pd = cu->find_partial_die (sect_off);
5afb4e99 20038 if (pd != NULL)
fb816e8b 20039 return { cu, pd };
0d99eb77
DE
20040 /* We missed recording what we needed.
20041 Load all dies and try again. */
5afb4e99 20042 }
0d99eb77
DE
20043 else
20044 {
20045 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 20046 if (cu->per_cu->is_debug_types)
0d99eb77 20047 {
9d8780f0
SM
20048 error (_("Dwarf Error: Type Unit at offset %s contains"
20049 " external reference to offset %s [in module %s].\n"),
20050 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
20051 bfd_get_filename (objfile->obfd));
20052 }
7188ed02
SM
20053 dwarf2_per_cu_data *per_cu
20054 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 20055 per_objfile);
72bf9492 20056
976ca316 20057 cu = per_objfile->get_cu (per_cu);
7188ed02 20058 if (cu == NULL || cu->partial_dies == NULL)
976ca316 20059 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 20060
976ca316 20061 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
20062
20063 cu->last_used = 0;
20064 pd = cu->find_partial_die (sect_off);
0d99eb77 20065 }
5afb4e99 20066
dee91e82
DE
20067 /* If we didn't find it, and not all dies have been loaded,
20068 load them all and try again. */
20069
7188ed02 20070 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 20071 {
7188ed02 20072 cu->per_cu->load_all_dies = 1;
fd820528
DE
20073
20074 /* This is nasty. When we reread the DIEs, somewhere up the call chain
20075 THIS_CU->cu may already be in use. So we can't just free it and
20076 replace its DIEs with the ones we read in. Instead, we leave those
20077 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
20078 and clobber THIS_CU->cu->partial_dies with the hash table for the new
20079 set. */
976ca316 20080 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 20081
7188ed02 20082 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
20083 }
20084
20085 if (pd == NULL)
521894aa 20086 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
9d8780f0 20087 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 20088 return { cu, pd };
72bf9492
DJ
20089}
20090
abc72ce4
DE
20091/* See if we can figure out if the class lives in a namespace. We do
20092 this by looking for a member function; its demangled name will
20093 contain namespace info, if there is any. */
20094
20095static void
20096guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
20097 struct dwarf2_cu *cu)
20098{
20099 /* NOTE: carlton/2003-10-07: Getting the info this way changes
20100 what template types look like, because the demangler
20101 frequently doesn't give the same name as the debug info. We
20102 could fix this by only using the demangled name to get the
20103 prefix (but see comment in read_structure_type). */
20104
20105 struct partial_die_info *real_pdi;
20106 struct partial_die_info *child_pdi;
20107
20108 /* If this DIE (this DIE's specification, if any) has a parent, then
20109 we should not do this. We'll prepend the parent's fully qualified
20110 name when we create the partial symbol. */
20111
20112 real_pdi = struct_pdi;
20113 while (real_pdi->has_specification)
fb816e8b 20114 {
122cf0f2
AB
20115 auto res = find_partial_die (real_pdi->spec_offset,
20116 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
20117 real_pdi = res.pdi;
20118 cu = res.cu;
20119 }
abc72ce4
DE
20120
20121 if (real_pdi->die_parent != NULL)
20122 return;
20123
20124 for (child_pdi = struct_pdi->die_child;
20125 child_pdi != NULL;
20126 child_pdi = child_pdi->die_sibling)
20127 {
20128 if (child_pdi->tag == DW_TAG_subprogram
20129 && child_pdi->linkage_name != NULL)
20130 {
43816ebc 20131 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
20132 (cu->language_defn->class_name_from_physname
20133 (child_pdi->linkage_name));
abc72ce4
DE
20134 if (actual_class_name != NULL)
20135 {
5e22e966 20136 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
20137 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
20138 struct_pdi->canonical_name = 1;
abc72ce4
DE
20139 }
20140 break;
20141 }
20142 }
20143}
20144
25c11aca
TV
20145/* Return true if a DIE with TAG may have the DW_AT_const_value
20146 attribute. */
20147
20148static bool
20149can_have_DW_AT_const_value_p (enum dwarf_tag tag)
20150{
20151 switch (tag)
20152 {
20153 case DW_TAG_constant:
20154 case DW_TAG_enumerator:
20155 case DW_TAG_formal_parameter:
20156 case DW_TAG_template_value_param:
20157 case DW_TAG_variable:
20158 return true;
20159 }
20160
20161 return false;
20162}
20163
52356b79
YQ
20164void
20165partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 20166{
abc72ce4
DE
20167 /* Once we've fixed up a die, there's no point in doing so again.
20168 This also avoids a memory leak if we were to call
20169 guess_partial_die_structure_name multiple times. */
52356b79 20170 if (fixup_called)
abc72ce4
DE
20171 return;
20172
72bf9492
DJ
20173 /* If we found a reference attribute and the DIE has no name, try
20174 to find a name in the referred to DIE. */
20175
7d00ffec 20176 if (raw_name == NULL && has_specification)
72bf9492
DJ
20177 {
20178 struct partial_die_info *spec_die;
72bf9492 20179
122cf0f2 20180 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
20181 spec_die = res.pdi;
20182 cu = res.cu;
72bf9492 20183
52356b79 20184 spec_die->fixup (cu);
72bf9492 20185
7d00ffec 20186 if (spec_die->raw_name)
72bf9492 20187 {
7d00ffec
TT
20188 raw_name = spec_die->raw_name;
20189 canonical_name = spec_die->canonical_name;
72bf9492
DJ
20190
20191 /* Copy DW_AT_external attribute if it is set. */
20192 if (spec_die->is_external)
52356b79 20193 is_external = spec_die->is_external;
72bf9492
DJ
20194 }
20195 }
20196
25c11aca
TV
20197 if (!has_const_value && has_specification
20198 && can_have_DW_AT_const_value_p (tag))
20199 {
20200 struct partial_die_info *spec_die;
20201
20202 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20203 spec_die = res.pdi;
20204 cu = res.cu;
20205
20206 spec_die->fixup (cu);
20207
20208 if (spec_die->has_const_value)
20209 {
20210 /* Copy DW_AT_const_value attribute if it is set. */
20211 has_const_value = spec_die->has_const_value;
20212 }
20213 }
20214
72bf9492 20215 /* Set default names for some unnamed DIEs. */
72bf9492 20216
7d00ffec
TT
20217 if (raw_name == NULL && tag == DW_TAG_namespace)
20218 {
20219 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20220 canonical_name = 1;
20221 }
72bf9492 20222
abc72ce4
DE
20223 /* If there is no parent die to provide a namespace, and there are
20224 children, see if we can determine the namespace from their linkage
122d1940 20225 name. */
abc72ce4 20226 if (cu->language == language_cplus
5e22e966 20227 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
20228 && die_parent == NULL
20229 && has_children
20230 && (tag == DW_TAG_class_type
20231 || tag == DW_TAG_structure_type
20232 || tag == DW_TAG_union_type))
20233 guess_partial_die_structure_name (this, cu);
abc72ce4 20234
53832f31
TT
20235 /* GCC might emit a nameless struct or union that has a linkage
20236 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 20237 if (raw_name == NULL
52356b79
YQ
20238 && (tag == DW_TAG_class_type
20239 || tag == DW_TAG_interface_type
20240 || tag == DW_TAG_structure_type
20241 || tag == DW_TAG_union_type)
20242 && linkage_name != NULL)
53832f31 20243 {
43816ebc
TT
20244 gdb::unique_xmalloc_ptr<char> demangled
20245 (gdb_demangle (linkage_name, DMGL_TYPES));
20246 if (demangled != nullptr)
53832f31 20247 {
96408a79
SA
20248 const char *base;
20249
20250 /* Strip any leading namespaces/classes, keep only the base name.
20251 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
20252 base = strrchr (demangled.get (), ':');
20253 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
20254 base++;
20255 else
43816ebc 20256 base = demangled.get ();
96408a79 20257
5e22e966 20258 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
20259 raw_name = objfile->intern (base);
20260 canonical_name = 1;
53832f31
TT
20261 }
20262 }
20263
52356b79 20264 fixup_called = 1;
72bf9492
DJ
20265}
20266
d0ce17d8 20267/* Read the .debug_loclists or .debug_rnglists header (they are the same format)
a1c40103
SM
20268 contents from the given SECTION in the HEADER.
20269
20270 HEADER_OFFSET is the offset of the header in the section. */
41144253 20271static void
d0ce17d8 20272read_loclists_rnglists_header (struct loclists_rnglists_header *header,
a1c40103
SM
20273 struct dwarf2_section_info *section,
20274 sect_offset header_offset)
41144253 20275{
20276 unsigned int bytes_read;
20277 bfd *abfd = section->get_bfd_owner ();
a1c40103
SM
20278 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20279
41144253 20280 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20281 info_ptr += bytes_read;
a1c40103 20282
41144253 20283 header->version = read_2_bytes (abfd, info_ptr);
20284 info_ptr += 2;
a1c40103 20285
41144253 20286 header->addr_size = read_1_byte (abfd, info_ptr);
20287 info_ptr += 1;
a1c40103 20288
41144253 20289 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20290 info_ptr += 1;
a1c40103 20291
41144253 20292 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20293}
20294
20295/* Return the DW_AT_loclists_base value for the CU. */
20296static ULONGEST
20297lookup_loclist_base (struct dwarf2_cu *cu)
20298{
20299 /* For the .dwo unit, the loclist_base points to the first offset following
20300 the header. The header consists of the following entities-
20301 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20302 bit format)
20303 2. version (2 bytes)
20304 3. address size (1 byte)
20305 4. segment selector size (1 byte)
20306 5. offset entry count (4 bytes)
20307 These sizes are derived as per the DWARFv5 standard. */
20308 if (cu->dwo_unit != nullptr)
20309 {
20310 if (cu->header.initial_length_size == 4)
20311 return LOCLIST_HEADER_SIZE32;
20312 return LOCLIST_HEADER_SIZE64;
20313 }
20314 return cu->loclist_base;
20315}
20316
20317/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20318 array of offsets in the .debug_loclists section. */
e57933dc
SM
20319
20320static sect_offset
41144253 20321read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20322{
976ca316
SM
20323 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20324 struct objfile *objfile = per_objfile->objfile;
41144253 20325 bfd *abfd = objfile->obfd;
a1c40103
SM
20326 ULONGEST loclist_header_size =
20327 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20328 : LOCLIST_HEADER_SIZE64);
41144253 20329 ULONGEST loclist_base = lookup_loclist_base (cu);
05787bad
SM
20330
20331 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20332 ULONGEST start_offset =
20333 loclist_base + loclist_index * cu->header.offset_size;
20334
a1c40103 20335 /* Get loclists section. */
41144253 20336 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20337
a1c40103 20338 /* Read the loclists section content. */
41144253 20339 section->read (objfile);
20340 if (section->buffer == NULL)
a0c1eeba
SM
20341 error (_("DW_FORM_loclistx used without .debug_loclists "
20342 "section [in module %s]"), objfile_name (objfile));
20343
a1c40103
SM
20344 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20345 so if loclist_base is smaller than the header size, we have a problem. */
20346 if (loclist_base < loclist_header_size)
20347 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20348 objfile_name (objfile));
20349
20350 /* Read the header of the loclists contribution. */
d0ce17d8 20351 struct loclists_rnglists_header header;
a1c40103
SM
20352 read_loclists_rnglists_header (&header, section,
20353 (sect_offset) (loclist_base - loclist_header_size));
20354
20355 /* Verify the loclist index is valid. */
41144253 20356 if (loclist_index >= header.offset_entry_count)
a0c1eeba
SM
20357 error (_("DW_FORM_loclistx pointing outside of "
20358 ".debug_loclists offset array [in module %s]"),
20359 objfile_name (objfile));
20360
05787bad
SM
20361 /* Validate that reading won't go beyond the end of the section. */
20362 if (start_offset + cu->header.offset_size > section->size)
20363 error (_("Reading DW_FORM_loclistx index beyond end of"
20364 ".debug_loclists section [in module %s]"),
20365 objfile_name (objfile));
20366
20367 const gdb_byte *info_ptr = section->buffer + start_offset;
41144253 20368
20369 if (cu->header.offset_size == 4)
e57933dc 20370 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
41144253 20371 else
e57933dc 20372 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
41144253 20373}
20374
d0ce17d8
CT
20375/* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20376 array of offsets in the .debug_rnglists section. */
e57933dc
SM
20377
20378static sect_offset
d0ce17d8
CT
20379read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20380 dwarf_tag tag)
20381{
20382 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20383 struct objfile *objfile = dwarf2_per_objfile->objfile;
20384 bfd *abfd = objfile->obfd;
20385 ULONGEST rnglist_header_size =
20386 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20387 : RNGLIST_HEADER_SIZE64);
2b0c7f41
SM
20388
20389 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20390 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20391 doesn't apply. */
d0ce17d8 20392 ULONGEST rnglist_base =
2b0c7f41 20393 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
5e4d9bbc
SM
20394
20395 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
d0ce17d8
CT
20396 ULONGEST start_offset =
20397 rnglist_base + rnglist_index * cu->header.offset_size;
20398
20399 /* Get rnglists section. */
20400 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20401
20402 /* Read the rnglists section content. */
20403 section->read (objfile);
20404 if (section->buffer == nullptr)
20405 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20406 "[in module %s]"),
20407 objfile_name (objfile));
20408
a1c40103
SM
20409 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20410 so if rnglist_base is smaller than the header size, we have a problem. */
20411 if (rnglist_base < rnglist_header_size)
20412 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20413 objfile_name (objfile));
20414
20415 /* Read the header of the rnglists contribution. */
d0ce17d8 20416 struct loclists_rnglists_header header;
a1c40103
SM
20417 read_loclists_rnglists_header (&header, section,
20418 (sect_offset) (rnglist_base - rnglist_header_size));
20419
20420 /* Verify the rnglist index is valid. */
d0ce17d8
CT
20421 if (rnglist_index >= header.offset_entry_count)
20422 error (_("DW_FORM_rnglistx index pointing outside of "
20423 ".debug_rnglists offset array [in module %s]"),
20424 objfile_name (objfile));
20425
d0ce17d8 20426 /* Validate that reading won't go beyond the end of the section. */
5e4d9bbc 20427 if (start_offset + cu->header.offset_size > section->size)
d0ce17d8
CT
20428 error (_("Reading DW_FORM_rnglistx index beyond end of"
20429 ".debug_rnglists section [in module %s]"),
20430 objfile_name (objfile));
20431
20432 const gdb_byte *info_ptr = section->buffer + start_offset;
20433
20434 if (cu->header.offset_size == 4)
e57933dc 20435 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8 20436 else
e57933dc 20437 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8
CT
20438}
20439
18a8505e
AT
20440/* Process the attributes that had to be skipped in the first round. These
20441 attributes are the ones that need str_offsets_base or addr_base attributes.
20442 They could not have been processed in the first round, because at the time
20443 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
20444static void
20445read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 20446 struct attribute *attr, dwarf_tag tag)
18a8505e
AT
20447{
20448 struct dwarf2_cu *cu = reader->cu;
20449 switch (attr->form)
20450 {
20451 case DW_FORM_addrx:
20452 case DW_FORM_GNU_addr_index:
36d378cf
TT
20453 attr->set_address (read_addr_index (cu,
20454 attr->as_unsigned_reprocess ()));
dda83cd7 20455 break;
41144253 20456 case DW_FORM_loclistx:
b1829e1b 20457 {
e57933dc 20458 sect_offset loclists_sect_off
b1829e1b
SM
20459 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20460
e57933dc 20461 attr->set_unsigned (to_underlying (loclists_sect_off));
b1829e1b
SM
20462 }
20463 break;
d0ce17d8 20464 case DW_FORM_rnglistx:
b1829e1b 20465 {
e57933dc 20466 sect_offset rnglists_sect_off
b1829e1b
SM
20467 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20468
e57933dc 20469 attr->set_unsigned (to_underlying (rnglists_sect_off));
b1829e1b 20470 }
dda83cd7 20471 break;
18a8505e
AT
20472 case DW_FORM_strx:
20473 case DW_FORM_strx1:
20474 case DW_FORM_strx2:
20475 case DW_FORM_strx3:
20476 case DW_FORM_strx4:
20477 case DW_FORM_GNU_str_index:
20478 {
fe56917a 20479 unsigned int str_index = attr->as_unsigned_reprocess ();
c6481205 20480 gdb_assert (!attr->canonical_string_p ());
18a8505e 20481 if (reader->dwo_file != NULL)
c6481205
TT
20482 attr->set_string_noncanonical (read_dwo_str_index (reader,
20483 str_index));
18a8505e 20484 else
c6481205
TT
20485 attr->set_string_noncanonical (read_stub_str_index (cu,
20486 str_index));
18a8505e
AT
20487 break;
20488 }
20489 default:
20490 gdb_assert_not_reached (_("Unexpected DWARF form."));
20491 }
20492}
20493
a8329558 20494/* Read an attribute value described by an attribute form. */
c906108c 20495
d521ce57 20496static const gdb_byte *
dee91e82
DE
20497read_attribute_value (const struct die_reader_specs *reader,
20498 struct attribute *attr, unsigned form,
7a5f294d 20499 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 20500{
dee91e82 20501 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
20502 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20503 struct objfile *objfile = per_objfile->objfile;
dee91e82 20504 bfd *abfd = reader->abfd;
e7c27a73 20505 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
20506 unsigned int bytes_read;
20507 struct dwarf_block *blk;
20508
aead7601 20509 attr->form = (enum dwarf_form) form;
a8329558 20510 switch (form)
c906108c 20511 {
c906108c 20512 case DW_FORM_ref_addr:
7c290a04
TT
20513 if (cu_header->version == 2)
20514 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
414ad644 20515 &bytes_read));
ae411497 20516 else
7c290a04 20517 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20518 &bytes_read));
ae411497
TT
20519 info_ptr += bytes_read;
20520 break;
36586728 20521 case DW_FORM_GNU_ref_alt:
7c290a04 20522 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20523 &bytes_read));
36586728
TT
20524 info_ptr += bytes_read;
20525 break;
ae411497 20526 case DW_FORM_addr:
08feed99
TT
20527 {
20528 struct gdbarch *gdbarch = objfile->arch ();
7c290a04 20529 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
36d378cf
TT
20530 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20531 attr->set_address (addr);
08feed99
TT
20532 info_ptr += bytes_read;
20533 }
c906108c
SS
20534 break;
20535 case DW_FORM_block2:
7b5a2f43 20536 blk = dwarf_alloc_block (cu);
c906108c
SS
20537 blk->size = read_2_bytes (abfd, info_ptr);
20538 info_ptr += 2;
20539 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20540 info_ptr += blk->size;
9d2246fc 20541 attr->set_block (blk);
c906108c
SS
20542 break;
20543 case DW_FORM_block4:
7b5a2f43 20544 blk = dwarf_alloc_block (cu);
c906108c
SS
20545 blk->size = read_4_bytes (abfd, info_ptr);
20546 info_ptr += 4;
20547 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20548 info_ptr += blk->size;
9d2246fc 20549 attr->set_block (blk);
c906108c
SS
20550 break;
20551 case DW_FORM_data2:
414ad644 20552 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
c906108c
SS
20553 info_ptr += 2;
20554 break;
20555 case DW_FORM_data4:
414ad644 20556 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
c906108c
SS
20557 info_ptr += 4;
20558 break;
20559 case DW_FORM_data8:
414ad644 20560 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
c906108c
SS
20561 info_ptr += 8;
20562 break;
0224619f
JK
20563 case DW_FORM_data16:
20564 blk = dwarf_alloc_block (cu);
20565 blk->size = 16;
20566 blk->data = read_n_bytes (abfd, info_ptr, 16);
20567 info_ptr += 16;
9d2246fc 20568 attr->set_block (blk);
0224619f 20569 break;
2dc7f7b3 20570 case DW_FORM_sec_offset:
7c290a04 20571 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20572 &bytes_read));
2dc7f7b3
TT
20573 info_ptr += bytes_read;
20574 break;
41144253 20575 case DW_FORM_loclistx:
20576 {
7a5f294d
TT
20577 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20578 &bytes_read));
20579 info_ptr += bytes_read;
41144253 20580 }
20581 break;
c906108c 20582 case DW_FORM_string:
c6481205
TT
20583 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20584 &bytes_read));
c906108c
SS
20585 info_ptr += bytes_read;
20586 break;
4bdf3d34 20587 case DW_FORM_strp:
36586728
TT
20588 if (!cu->per_cu->is_dwz)
20589 {
c6481205
TT
20590 attr->set_string_noncanonical
20591 (read_indirect_string (per_objfile,
20592 abfd, info_ptr, cu_header,
20593 &bytes_read));
36586728
TT
20594 info_ptr += bytes_read;
20595 break;
20596 }
20597 /* FALLTHROUGH */
43988095
JK
20598 case DW_FORM_line_strp:
20599 if (!cu->per_cu->is_dwz)
20600 {
c6481205
TT
20601 attr->set_string_noncanonical
20602 (per_objfile->read_line_string (info_ptr, cu_header,
20603 &bytes_read));
43988095
JK
20604 info_ptr += bytes_read;
20605 break;
20606 }
20607 /* FALLTHROUGH */
36586728
TT
20608 case DW_FORM_GNU_strp_alt:
20609 {
a7308ce0 20610 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
8266302d
TT
20611 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20612 &bytes_read);
36586728 20613
c6481205
TT
20614 attr->set_string_noncanonical
20615 (dwz->read_string (objfile, str_offset));
36586728
TT
20616 info_ptr += bytes_read;
20617 }
4bdf3d34 20618 break;
2dc7f7b3 20619 case DW_FORM_exprloc:
c906108c 20620 case DW_FORM_block:
7b5a2f43 20621 blk = dwarf_alloc_block (cu);
c906108c
SS
20622 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20623 info_ptr += bytes_read;
20624 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20625 info_ptr += blk->size;
9d2246fc 20626 attr->set_block (blk);
c906108c
SS
20627 break;
20628 case DW_FORM_block1:
7b5a2f43 20629 blk = dwarf_alloc_block (cu);
c906108c
SS
20630 blk->size = read_1_byte (abfd, info_ptr);
20631 info_ptr += 1;
20632 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20633 info_ptr += blk->size;
9d2246fc 20634 attr->set_block (blk);
c906108c
SS
20635 break;
20636 case DW_FORM_data1:
c906108c 20637 case DW_FORM_flag:
414ad644 20638 attr->set_unsigned (read_1_byte (abfd, info_ptr));
c906108c
SS
20639 info_ptr += 1;
20640 break;
2dc7f7b3 20641 case DW_FORM_flag_present:
414ad644 20642 attr->set_unsigned (1);
2dc7f7b3 20643 break;
c906108c 20644 case DW_FORM_sdata:
1bc397c5 20645 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20646 info_ptr += bytes_read;
20647 break;
18a8505e 20648 case DW_FORM_rnglistx:
7a5f294d
TT
20649 {
20650 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20651 &bytes_read));
20652 info_ptr += bytes_read;
20653 }
20654 break;
d0ce17d8 20655 case DW_FORM_udata:
414ad644 20656 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20657 info_ptr += bytes_read;
20658 break;
20659 case DW_FORM_ref1:
7c290a04 20660 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20661 + read_1_byte (abfd, info_ptr)));
c906108c
SS
20662 info_ptr += 1;
20663 break;
20664 case DW_FORM_ref2:
7c290a04 20665 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20666 + read_2_bytes (abfd, info_ptr)));
c906108c
SS
20667 info_ptr += 2;
20668 break;
20669 case DW_FORM_ref4:
7c290a04 20670 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20671 + read_4_bytes (abfd, info_ptr)));
c906108c
SS
20672 info_ptr += 4;
20673 break;
613e1657 20674 case DW_FORM_ref8:
7c290a04 20675 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20676 + read_8_bytes (abfd, info_ptr)));
613e1657
KB
20677 info_ptr += 8;
20678 break;
55f1336d 20679 case DW_FORM_ref_sig8:
630ed6b9 20680 attr->set_signature (read_8_bytes (abfd, info_ptr));
348e048f
DE
20681 info_ptr += 8;
20682 break;
c906108c 20683 case DW_FORM_ref_udata:
7c290a04 20684 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644
TT
20685 + read_unsigned_leb128 (abfd, info_ptr,
20686 &bytes_read)));
c906108c
SS
20687 info_ptr += bytes_read;
20688 break;
c906108c 20689 case DW_FORM_indirect:
a8329558
KW
20690 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20691 info_ptr += bytes_read;
43988095
JK
20692 if (form == DW_FORM_implicit_const)
20693 {
20694 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20695 info_ptr += bytes_read;
20696 }
20697 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
7a5f294d 20698 info_ptr);
43988095
JK
20699 break;
20700 case DW_FORM_implicit_const:
1bc397c5 20701 attr->set_signed (implicit_const);
a8329558 20702 break;
336d760d 20703 case DW_FORM_addrx:
3019eac3 20704 case DW_FORM_GNU_addr_index:
fe56917a
TT
20705 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20706 &bytes_read));
3019eac3
DE
20707 info_ptr += bytes_read;
20708 break;
cf532bd1 20709 case DW_FORM_strx:
15f18d14
AT
20710 case DW_FORM_strx1:
20711 case DW_FORM_strx2:
20712 case DW_FORM_strx3:
20713 case DW_FORM_strx4:
3019eac3 20714 case DW_FORM_GNU_str_index:
3019eac3 20715 {
15f18d14
AT
20716 ULONGEST str_index;
20717 if (form == DW_FORM_strx1)
20718 {
20719 str_index = read_1_byte (abfd, info_ptr);
20720 info_ptr += 1;
20721 }
20722 else if (form == DW_FORM_strx2)
20723 {
20724 str_index = read_2_bytes (abfd, info_ptr);
20725 info_ptr += 2;
20726 }
20727 else if (form == DW_FORM_strx3)
20728 {
20729 str_index = read_3_bytes (abfd, info_ptr);
20730 info_ptr += 3;
20731 }
20732 else if (form == DW_FORM_strx4)
20733 {
20734 str_index = read_4_bytes (abfd, info_ptr);
20735 info_ptr += 4;
20736 }
20737 else
20738 {
20739 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20740 info_ptr += bytes_read;
20741 }
fe56917a 20742 attr->set_unsigned_reprocess (str_index);
7a5f294d 20743 }
3019eac3 20744 break;
c906108c 20745 default:
8a3fe4f8 20746 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
20747 dwarf_form_name (form),
20748 bfd_get_filename (abfd));
c906108c 20749 }
28e94949 20750
36586728 20751 /* Super hack. */
cd6c91b4 20752 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
20753 attr->form = DW_FORM_GNU_ref_alt;
20754
28e94949
JB
20755 /* We have seen instances where the compiler tried to emit a byte
20756 size attribute of -1 which ended up being encoded as an unsigned
20757 0xffffffff. Although 0xffffffff is technically a valid size value,
20758 an object of this size seems pretty unlikely so we can relatively
20759 safely treat these cases as if the size attribute was invalid and
20760 treat them as zero by default. */
20761 if (attr->name == DW_AT_byte_size
20762 && form == DW_FORM_data4
529908cb 20763 && attr->as_unsigned () >= 0xffffffff)
01c66ae6
JB
20764 {
20765 complaint
dda83cd7
SM
20766 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20767 hex_string (attr->as_unsigned ()));
414ad644 20768 attr->set_unsigned (0);
01c66ae6 20769 }
28e94949 20770
c906108c
SS
20771 return info_ptr;
20772}
20773
a8329558
KW
20774/* Read an attribute described by an abbreviated attribute. */
20775
d521ce57 20776static const gdb_byte *
dee91e82 20777read_attribute (const struct die_reader_specs *reader,
4444f407 20778 struct attribute *attr, const struct attr_abbrev *abbrev,
7a5f294d 20779 const gdb_byte *info_ptr)
a8329558
KW
20780{
20781 attr->name = abbrev->name;
c6481205 20782 attr->string_is_canonical = 0;
fe56917a 20783 attr->requires_reprocessing = 0;
43988095 20784 return read_attribute_value (reader, attr, abbrev->form,
7a5f294d 20785 abbrev->implicit_const, info_ptr);
a8329558
KW
20786}
20787
43988095
JK
20788/* Return pointer to string at .debug_str offset STR_OFFSET. */
20789
20790static const char *
976ca316 20791read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 20792 LONGEST str_offset)
43988095 20793{
976ca316
SM
20794 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20795 str_offset, "DW_FORM_strp");
c906108c
SS
20796}
20797
43988095
JK
20798/* Return pointer to string at .debug_str offset as read from BUF.
20799 BUF is assumed to be in a compilation unit described by CU_HEADER.
20800 Return *BYTES_READ_PTR count of bytes read from BUF. */
20801
d521ce57 20802static const char *
976ca316 20803read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 20804 const gdb_byte *buf,
cf2c3c16
TT
20805 const struct comp_unit_head *cu_header,
20806 unsigned int *bytes_read_ptr)
20807{
8266302d 20808 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 20809
976ca316 20810 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
20811}
20812
86c0bb4c 20813/* See read.h. */
43988095 20814
86c0bb4c
TT
20815const char *
20816dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
20817 const struct comp_unit_head *cu_header,
20818 unsigned int *bytes_read_ptr)
43988095 20819{
86c0bb4c 20820 bfd *abfd = objfile->obfd;
8266302d 20821 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 20822
5989a64e 20823 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
20824}
20825
3019eac3 20826/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20827 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20828 ADDR_SIZE is the size of addresses from the CU header. */
20829
20830static CORE_ADDR
976ca316
SM
20831read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20832 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 20833{
976ca316 20834 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
20835 bfd *abfd = objfile->obfd;
20836 const gdb_byte *info_ptr;
18a8505e 20837 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 20838
976ca316
SM
20839 per_objfile->per_bfd->addr.read (objfile);
20840 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 20841 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20842 objfile_name (objfile));
18a8505e 20843 if (addr_base_or_zero + addr_index * addr_size
976ca316 20844 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
20845 error (_("DW_FORM_addr_index pointing outside of "
20846 ".debug_addr section [in module %s]"),
4262abfb 20847 objfile_name (objfile));
976ca316
SM
20848 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20849 + addr_index * addr_size);
3019eac3
DE
20850 if (addr_size == 4)
20851 return bfd_get_32 (abfd, info_ptr);
20852 else
20853 return bfd_get_64 (abfd, info_ptr);
20854}
20855
20856/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20857
20858static CORE_ADDR
20859read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20860{
5e22e966 20861 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 20862 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20863}
20864
20865/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20866
20867static CORE_ADDR
d521ce57 20868read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20869 unsigned int *bytes_read)
20870{
5e22e966 20871 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
20872 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20873
20874 return read_addr_index (cu, addr_index);
20875}
20876
450a1bfc 20877/* See read.h. */
3019eac3
DE
20878
20879CORE_ADDR
82ca3f51 20880dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 20881 dwarf2_per_objfile *per_objfile,
82ca3f51 20882 unsigned int addr_index)
3019eac3 20883{
976ca316 20884 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 20885 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20886 int addr_size;
20887
3019eac3
DE
20888 /* We need addr_base and addr_size.
20889 If we don't have PER_CU->cu, we have to get it.
20890 Nasty, but the alternative is storing the needed info in PER_CU,
20891 which at this point doesn't seem justified: it's not clear how frequently
20892 it would get used and it would increase the size of every PER_CU.
20893 Entry points like dwarf2_per_cu_addr_size do a similar thing
20894 so we're not in uncharted territory here.
20895 Alas we need to be a bit more complicated as addr_base is contained
20896 in the DIE.
20897
20898 We don't need to read the entire CU(/TU).
20899 We just need the header and top level die.
a1b64ce1 20900
3019eac3 20901 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20902 For now we skip this optimization. */
3019eac3
DE
20903
20904 if (cu != NULL)
20905 {
20906 addr_base = cu->addr_base;
20907 addr_size = cu->header.addr_size;
20908 }
20909 else
20910 {
976ca316 20911 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
20912 addr_base = reader.cu->addr_base;
20913 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20914 }
20915
976ca316 20916 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
20917}
20918
18a8505e
AT
20919/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20920 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20921 DWO file. */
3019eac3 20922
d521ce57 20923static const char *
18a8505e
AT
20924read_str_index (struct dwarf2_cu *cu,
20925 struct dwarf2_section_info *str_section,
20926 struct dwarf2_section_info *str_offsets_section,
20927 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20928{
976ca316
SM
20929 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20930 struct objfile *objfile = per_objfile->objfile;
c5164cbc 20931 const char *objf_name = objfile_name (objfile);
3019eac3 20932 bfd *abfd = objfile->obfd;
d521ce57 20933 const gdb_byte *info_ptr;
3019eac3 20934 ULONGEST str_offset;
cf532bd1 20935 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20936
96b79293
TT
20937 str_section->read (objfile);
20938 str_offsets_section->read (objfile);
73869dc2 20939 if (str_section->buffer == NULL)
18a8505e 20940 error (_("%s used without %s section"
9d8780f0 20941 " in CU at offset %s [in module %s]"),
96b79293 20942 form_name, str_section->get_name (),
dda83cd7 20943 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20944 if (str_offsets_section->buffer == NULL)
18a8505e 20945 error (_("%s used without %s section"
9d8780f0 20946 " in CU at offset %s [in module %s]"),
96b79293 20947 form_name, str_section->get_name (),
dda83cd7 20948 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20949 info_ptr = (str_offsets_section->buffer
18a8505e 20950 + str_offsets_base
3019eac3
DE
20951 + str_index * cu->header.offset_size);
20952 if (cu->header.offset_size == 4)
20953 str_offset = bfd_get_32 (abfd, info_ptr);
20954 else
20955 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20956 if (str_offset >= str_section->size)
57d63ce2 20957 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20958 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20959 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20960 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20961}
20962
18a8505e
AT
20963/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20964
20965static const char *
20966read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20967{
20968 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20969 ? reader->cu->header.addr_size : 0;
20970 return read_str_index (reader->cu,
20971 &reader->dwo_file->sections.str,
20972 &reader->dwo_file->sections.str_offsets,
20973 str_offsets_base, str_index);
20974}
20975
20976/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20977
20978static const char *
20979read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20980{
5e22e966 20981 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
20982 const char *objf_name = objfile_name (objfile);
20983 static const char form_name[] = "DW_FORM_GNU_str_index";
20984 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20985
20986 if (!cu->str_offsets_base.has_value ())
20987 error (_("%s used in Fission stub without %s"
20988 " in CU at offset 0x%lx [in module %s]"),
20989 form_name, str_offsets_attr_name,
20990 (long) cu->header.offset_size, objf_name);
20991
20992 return read_str_index (cu,
5e22e966
SM
20993 &cu->per_objfile->per_bfd->str,
20994 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
20995 *cu->str_offsets_base, str_index);
20996}
20997
3019eac3
DE
20998/* Return the length of an LEB128 number in BUF. */
20999
21000static int
21001leb128_size (const gdb_byte *buf)
21002{
21003 const gdb_byte *begin = buf;
21004 gdb_byte byte;
21005
21006 while (1)
21007 {
21008 byte = *buf++;
21009 if ((byte & 128) == 0)
21010 return buf - begin;
21011 }
21012}
21013
c906108c 21014static void
e142c38c 21015set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
21016{
21017 switch (lang)
21018 {
21019 case DW_LANG_C89:
76bee0cc 21020 case DW_LANG_C99:
0cfd832f 21021 case DW_LANG_C11:
c906108c 21022 case DW_LANG_C:
d1be3247 21023 case DW_LANG_UPC:
e142c38c 21024 cu->language = language_c;
c906108c 21025 break;
9c37b5ae 21026 case DW_LANG_Java:
c906108c 21027 case DW_LANG_C_plus_plus:
0cfd832f
MW
21028 case DW_LANG_C_plus_plus_11:
21029 case DW_LANG_C_plus_plus_14:
e142c38c 21030 cu->language = language_cplus;
c906108c 21031 break;
6aecb9c2
JB
21032 case DW_LANG_D:
21033 cu->language = language_d;
21034 break;
c906108c
SS
21035 case DW_LANG_Fortran77:
21036 case DW_LANG_Fortran90:
b21b22e0 21037 case DW_LANG_Fortran95:
f7de9aab
MW
21038 case DW_LANG_Fortran03:
21039 case DW_LANG_Fortran08:
e142c38c 21040 cu->language = language_fortran;
c906108c 21041 break;
a766d390
DE
21042 case DW_LANG_Go:
21043 cu->language = language_go;
21044 break;
c906108c 21045 case DW_LANG_Mips_Assembler:
e142c38c 21046 cu->language = language_asm;
c906108c
SS
21047 break;
21048 case DW_LANG_Ada83:
8aaf0b47 21049 case DW_LANG_Ada95:
bc5f45f8
JB
21050 cu->language = language_ada;
21051 break;
72019c9c
GM
21052 case DW_LANG_Modula2:
21053 cu->language = language_m2;
21054 break;
fe8e67fd
PM
21055 case DW_LANG_Pascal83:
21056 cu->language = language_pascal;
21057 break;
22566fbd
DJ
21058 case DW_LANG_ObjC:
21059 cu->language = language_objc;
21060 break;
c44af4eb
TT
21061 case DW_LANG_Rust:
21062 case DW_LANG_Rust_old:
21063 cu->language = language_rust;
21064 break;
c906108c
SS
21065 case DW_LANG_Cobol74:
21066 case DW_LANG_Cobol85:
c906108c 21067 default:
e142c38c 21068 cu->language = language_minimal;
c906108c
SS
21069 break;
21070 }
e142c38c 21071 cu->language_defn = language_def (cu->language);
c906108c
SS
21072}
21073
21074/* Return the named attribute or NULL if not there. */
21075
21076static struct attribute *
e142c38c 21077dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 21078{
a48e046c 21079 for (;;)
c906108c 21080 {
a48e046c
TT
21081 unsigned int i;
21082 struct attribute *spec = NULL;
21083
21084 for (i = 0; i < die->num_attrs; ++i)
21085 {
21086 if (die->attrs[i].name == name)
21087 return &die->attrs[i];
21088 if (die->attrs[i].name == DW_AT_specification
21089 || die->attrs[i].name == DW_AT_abstract_origin)
21090 spec = &die->attrs[i];
21091 }
21092
21093 if (!spec)
21094 break;
c906108c 21095
f2f0e013 21096 die = follow_die_ref (die, spec, &cu);
f2f0e013 21097 }
c5aa993b 21098
c906108c
SS
21099 return NULL;
21100}
21101
7d45c7c3
KB
21102/* Return the string associated with a string-typed attribute, or NULL if it
21103 is either not found or is of an incorrect type. */
21104
21105static const char *
21106dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21107{
21108 struct attribute *attr;
21109 const char *str = NULL;
21110
21111 attr = dwarf2_attr (die, name, cu);
21112
21113 if (attr != NULL)
21114 {
95f982e5 21115 str = attr->as_string ();
e61108c9 21116 if (str == nullptr)
dda83cd7 21117 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
21118 "DIE at %s in module %s"),
21119 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 21120 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
21121 }
21122
21123 return str;
21124}
21125
a084a2a6 21126/* Return the dwo name or NULL if not present. If present, it is in either
85102364 21127 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
21128static const char *
21129dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
21130{
21131 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
21132 if (dwo_name == nullptr)
21133 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
21134 return dwo_name;
21135}
21136
05cf31d1
JB
21137/* Return non-zero iff the attribute NAME is defined for the given DIE,
21138 and holds a non-zero value. This function should only be used for
2dc7f7b3 21139 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
21140
21141static int
21142dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
21143{
21144 struct attribute *attr = dwarf2_attr (die, name, cu);
21145
c45bc3f8 21146 return attr != nullptr && attr->as_boolean ();
05cf31d1
JB
21147}
21148
3ca72b44 21149static int
e142c38c 21150die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 21151{
05cf31d1
JB
21152 /* A DIE is a declaration if it has a DW_AT_declaration attribute
21153 which value is non-zero. However, we have to be careful with
21154 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
21155 (via dwarf2_flag_true_p) follows this attribute. So we may
21156 end up accidently finding a declaration attribute that belongs
21157 to a different DIE referenced by the specification attribute,
21158 even though the given DIE does not have a declaration attribute. */
21159 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
21160 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
21161}
21162
63d06c5c 21163/* Return the die giving the specification for DIE, if there is
f2f0e013 21164 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
21165 containing the return value on output. If there is no
21166 specification, but there is an abstract origin, that is
21167 returned. */
63d06c5c
DC
21168
21169static struct die_info *
f2f0e013 21170die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 21171{
f2f0e013
DJ
21172 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
21173 *spec_cu);
63d06c5c 21174
edb3359d
DJ
21175 if (spec_attr == NULL)
21176 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
21177
63d06c5c
DC
21178 if (spec_attr == NULL)
21179 return NULL;
21180 else
f2f0e013 21181 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 21182}
c906108c 21183
527f3840
JK
21184/* Stub for free_line_header to match void * callback types. */
21185
21186static void
21187free_line_header_voidp (void *arg)
21188{
9a3c8263 21189 struct line_header *lh = (struct line_header *) arg;
527f3840 21190
fff8551c 21191 delete lh;
527f3840
JK
21192}
21193
83769d0b 21194/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
21195
21196static struct dwarf2_section_info *
21197get_debug_line_section (struct dwarf2_cu *cu)
21198{
21199 struct dwarf2_section_info *section;
976ca316 21200 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
21201
21202 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
21203 DWO file. */
21204 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21205 section = &cu->dwo_unit->dwo_file->sections.line;
21206 else if (cu->per_cu->is_dwz)
21207 {
a7308ce0 21208 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
36586728
TT
21209
21210 section = &dwz->line;
21211 }
21212 else
976ca316 21213 section = &per_objfile->per_bfd->line;
36586728
TT
21214
21215 return section;
21216}
21217
debd256d 21218/* Read the statement program header starting at OFFSET in
3019eac3 21219 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 21220 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
21221 Returns NULL if there is a problem reading the header, e.g., if it
21222 has a version we don't understand.
debd256d
JB
21223
21224 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
21225 the returned object point into the dwarf line section buffer,
21226 and must not be freed. */
ae2de4f8 21227
fff8551c 21228static line_header_up
9c541725 21229dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 21230{
3019eac3 21231 struct dwarf2_section_info *section;
976ca316 21232 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 21233
36586728 21234 section = get_debug_line_section (cu);
976ca316 21235 section->read (per_objfile->objfile);
3019eac3 21236 if (section->buffer == NULL)
debd256d 21237 {
3019eac3 21238 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 21239 complaint (_("missing .debug_line.dwo section"));
3019eac3 21240 else
b98664d3 21241 complaint (_("missing .debug_line section"));
debd256d
JB
21242 return 0;
21243 }
21244
0df7ad3a 21245 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 21246 per_objfile, section, &cu->header);
debd256d 21247}
c906108c 21248
c6da4cef 21249/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 21250 Return the file name of the psymtab for the given file_entry.
c6da4cef 21251 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
21252 If space for the result is malloc'd, *NAME_HOLDER will be set.
21253 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 21254
d521ce57 21255static const char *
7ba99d21 21256psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 21257 const dwarf2_psymtab *pst,
c89b44cd
TT
21258 const char *comp_dir,
21259 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 21260{
d521ce57
TT
21261 const char *include_name = fe.name;
21262 const char *include_name_to_compare = include_name;
72b9f47f 21263 const char *pst_filename;
c6da4cef
DE
21264 int file_is_pst;
21265
8c43009f 21266 const char *dir_name = fe.include_dir (lh);
c6da4cef 21267
c89b44cd 21268 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
21269 if (!IS_ABSOLUTE_PATH (include_name)
21270 && (dir_name != NULL || comp_dir != NULL))
21271 {
21272 /* Avoid creating a duplicate psymtab for PST.
21273 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21274 Before we do the comparison, however, we need to account
21275 for DIR_NAME and COMP_DIR.
21276 First prepend dir_name (if non-NULL). If we still don't
21277 have an absolute path prepend comp_dir (if non-NULL).
21278 However, the directory we record in the include-file's
21279 psymtab does not contain COMP_DIR (to match the
21280 corresponding symtab(s)).
21281
21282 Example:
21283
21284 bash$ cd /tmp
21285 bash$ gcc -g ./hello.c
21286 include_name = "hello.c"
21287 dir_name = "."
21288 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
21289 DW_AT_name = "./hello.c"
21290
21291 */
c6da4cef
DE
21292
21293 if (dir_name != NULL)
21294 {
c89b44cd
TT
21295 name_holder->reset (concat (dir_name, SLASH_STRING,
21296 include_name, (char *) NULL));
21297 include_name = name_holder->get ();
c6da4cef 21298 include_name_to_compare = include_name;
c6da4cef
DE
21299 }
21300 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21301 {
c89b44cd
TT
21302 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21303 include_name, (char *) NULL));
21304 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
21305 }
21306 }
21307
21308 pst_filename = pst->filename;
c89b44cd 21309 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
21310 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21311 {
c89b44cd
TT
21312 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21313 pst_filename, (char *) NULL));
21314 pst_filename = copied_name.get ();
c6da4cef
DE
21315 }
21316
1e3fad37 21317 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 21318
c6da4cef
DE
21319 if (file_is_pst)
21320 return NULL;
21321 return include_name;
21322}
21323
d9b3de22
DE
21324/* State machine to track the state of the line number program. */
21325
6f77053d 21326class lnp_state_machine
d9b3de22 21327{
6f77053d
PA
21328public:
21329 /* Initialize a machine state for the start of a line number
21330 program. */
804d2729
TT
21331 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21332 bool record_lines_p);
6f77053d 21333
8c43009f
PA
21334 file_entry *current_file ()
21335 {
21336 /* lh->file_names is 0-based, but the file name numbers in the
21337 statement program are 1-based. */
6f77053d
PA
21338 return m_line_header->file_name_at (m_file);
21339 }
21340
21341 /* Record the line in the state machine. END_SEQUENCE is true if
21342 we're processing the end of a sequence. */
21343 void record_line (bool end_sequence);
21344
a8caed5d 21345 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
7ab6656f 21346 nop-out rest of the lines in this sequence. */
6f77053d
PA
21347 void check_line_address (struct dwarf2_cu *cu,
21348 const gdb_byte *line_ptr,
7ab6656f 21349 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
21350
21351 void handle_set_discriminator (unsigned int discriminator)
21352 {
21353 m_discriminator = discriminator;
21354 m_line_has_non_zero_discriminator |= discriminator != 0;
21355 }
21356
21357 /* Handle DW_LNE_set_address. */
21358 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21359 {
21360 m_op_index = 0;
21361 address += baseaddr;
21362 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21363 }
21364
21365 /* Handle DW_LNS_advance_pc. */
21366 void handle_advance_pc (CORE_ADDR adjust);
21367
21368 /* Handle a special opcode. */
21369 void handle_special_opcode (unsigned char op_code);
21370
21371 /* Handle DW_LNS_advance_line. */
21372 void handle_advance_line (int line_delta)
21373 {
21374 advance_line (line_delta);
21375 }
21376
21377 /* Handle DW_LNS_set_file. */
21378 void handle_set_file (file_name_index file);
21379
21380 /* Handle DW_LNS_negate_stmt. */
21381 void handle_negate_stmt ()
21382 {
21383 m_is_stmt = !m_is_stmt;
21384 }
21385
21386 /* Handle DW_LNS_const_add_pc. */
21387 void handle_const_add_pc ();
21388
21389 /* Handle DW_LNS_fixed_advance_pc. */
21390 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21391 {
21392 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21393 m_op_index = 0;
21394 }
21395
21396 /* Handle DW_LNS_copy. */
21397 void handle_copy ()
21398 {
21399 record_line (false);
21400 m_discriminator = 0;
21401 }
21402
21403 /* Handle DW_LNE_end_sequence. */
21404 void handle_end_sequence ()
21405 {
804d2729 21406 m_currently_recording_lines = true;
6f77053d
PA
21407 }
21408
21409private:
21410 /* Advance the line by LINE_DELTA. */
21411 void advance_line (int line_delta)
21412 {
21413 m_line += line_delta;
21414
21415 if (line_delta != 0)
21416 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21417 }
21418
804d2729
TT
21419 struct dwarf2_cu *m_cu;
21420
6f77053d
PA
21421 gdbarch *m_gdbarch;
21422
21423 /* True if we're recording lines.
21424 Otherwise we're building partial symtabs and are just interested in
21425 finding include files mentioned by the line number program. */
21426 bool m_record_lines_p;
21427
8c43009f 21428 /* The line number header. */
6f77053d 21429 line_header *m_line_header;
8c43009f 21430
6f77053d
PA
21431 /* These are part of the standard DWARF line number state machine,
21432 and initialized according to the DWARF spec. */
d9b3de22 21433
6f77053d 21434 unsigned char m_op_index = 0;
7ba99d21
AT
21435 /* The line table index of the current file. */
21436 file_name_index m_file = 1;
6f77053d
PA
21437 unsigned int m_line = 1;
21438
21439 /* These are initialized in the constructor. */
21440
21441 CORE_ADDR m_address;
21442 bool m_is_stmt;
21443 unsigned int m_discriminator;
d9b3de22
DE
21444
21445 /* Additional bits of state we need to track. */
21446
21447 /* The last file that we called dwarf2_start_subfile for.
21448 This is only used for TLLs. */
6f77053d 21449 unsigned int m_last_file = 0;
d9b3de22 21450 /* The last file a line number was recorded for. */
6f77053d 21451 struct subfile *m_last_subfile = NULL;
d9b3de22 21452
1313c56e
AB
21453 /* The address of the last line entry. */
21454 CORE_ADDR m_last_address;
21455
21456 /* Set to true when a previous line at the same address (using
21457 m_last_address) had m_is_stmt true. This is reset to false when a
21458 line entry at a new address (m_address different to m_last_address) is
21459 processed. */
21460 bool m_stmt_at_address = false;
21461
804d2729
TT
21462 /* When true, record the lines we decode. */
21463 bool m_currently_recording_lines = false;
d9b3de22
DE
21464
21465 /* The last line number that was recorded, used to coalesce
21466 consecutive entries for the same line. This can happen, for
21467 example, when discriminators are present. PR 17276. */
6f77053d
PA
21468 unsigned int m_last_line = 0;
21469 bool m_line_has_non_zero_discriminator = false;
8c43009f 21470};
d9b3de22 21471
6f77053d
PA
21472void
21473lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21474{
21475 CORE_ADDR addr_adj = (((m_op_index + adjust)
21476 / m_line_header->maximum_ops_per_instruction)
21477 * m_line_header->minimum_instruction_length);
21478 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21479 m_op_index = ((m_op_index + adjust)
21480 % m_line_header->maximum_ops_per_instruction);
21481}
d9b3de22 21482
6f77053d
PA
21483void
21484lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21485{
6f77053d 21486 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
21487 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21488 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21489 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
21490 / m_line_header->maximum_ops_per_instruction)
21491 * m_line_header->minimum_instruction_length);
21492 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 21493 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 21494 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21495
258bf0ee 21496 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
21497 advance_line (line_delta);
21498 record_line (false);
21499 m_discriminator = 0;
21500}
d9b3de22 21501
6f77053d
PA
21502void
21503lnp_state_machine::handle_set_file (file_name_index file)
21504{
21505 m_file = file;
21506
21507 const file_entry *fe = current_file ();
21508 if (fe == NULL)
21509 dwarf2_debug_line_missing_file_complaint ();
21510 else if (m_record_lines_p)
21511 {
21512 const char *dir = fe->include_dir (m_line_header);
21513
c24bdb02 21514 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21515 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21516 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21517 }
21518}
21519
21520void
21521lnp_state_machine::handle_const_add_pc ()
21522{
21523 CORE_ADDR adjust
21524 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21525
21526 CORE_ADDR addr_adj
21527 = (((m_op_index + adjust)
21528 / m_line_header->maximum_ops_per_instruction)
21529 * m_line_header->minimum_instruction_length);
21530
21531 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21532 m_op_index = ((m_op_index + adjust)
21533 % m_line_header->maximum_ops_per_instruction);
21534}
d9b3de22 21535
a05a36a5
DE
21536/* Return non-zero if we should add LINE to the line number table.
21537 LINE is the line to add, LAST_LINE is the last line that was added,
21538 LAST_SUBFILE is the subfile for LAST_LINE.
21539 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21540 had a non-zero discriminator.
21541
21542 We have to be careful in the presence of discriminators.
21543 E.g., for this line:
21544
21545 for (i = 0; i < 100000; i++);
21546
21547 clang can emit four line number entries for that one line,
21548 each with a different discriminator.
21549 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21550
21551 However, we want gdb to coalesce all four entries into one.
21552 Otherwise the user could stepi into the middle of the line and
21553 gdb would get confused about whether the pc really was in the
21554 middle of the line.
21555
21556 Things are further complicated by the fact that two consecutive
21557 line number entries for the same line is a heuristic used by gcc
21558 to denote the end of the prologue. So we can't just discard duplicate
21559 entries, we have to be selective about it. The heuristic we use is
21560 that we only collapse consecutive entries for the same line if at least
21561 one of those entries has a non-zero discriminator. PR 17276.
21562
21563 Note: Addresses in the line number state machine can never go backwards
21564 within one sequence, thus this coalescing is ok. */
21565
21566static int
804d2729
TT
21567dwarf_record_line_p (struct dwarf2_cu *cu,
21568 unsigned int line, unsigned int last_line,
a05a36a5
DE
21569 int line_has_non_zero_discriminator,
21570 struct subfile *last_subfile)
21571{
c24bdb02 21572 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21573 return 1;
21574 if (line != last_line)
21575 return 1;
21576 /* Same line for the same file that we've seen already.
21577 As a last check, for pr 17276, only record the line if the line
21578 has never had a non-zero discriminator. */
21579 if (!line_has_non_zero_discriminator)
21580 return 1;
21581 return 0;
21582}
21583
804d2729
TT
21584/* Use the CU's builder to record line number LINE beginning at
21585 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21586
21587static void
d9b3de22 21588dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 21589 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 21590 struct dwarf2_cu *cu)
252a6764
DE
21591{
21592 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21593
27e0867f
DE
21594 if (dwarf_line_debug)
21595 {
21596 fprintf_unfiltered (gdb_stdlog,
21597 "Recording line %u, file %s, address %s\n",
21598 line, lbasename (subfile->name),
21599 paddress (gdbarch, address));
21600 }
21601
804d2729 21602 if (cu != nullptr)
8c95582d 21603 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
21604}
21605
21606/* Subroutine of dwarf_decode_lines_1 to simplify it.
21607 Mark the end of a set of line number records.
d9b3de22 21608 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21609 If SUBFILE is NULL the request is ignored. */
21610
21611static void
21612dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21613 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21614{
27e0867f
DE
21615 if (subfile == NULL)
21616 return;
21617
21618 if (dwarf_line_debug)
21619 {
21620 fprintf_unfiltered (gdb_stdlog,
21621 "Finishing current line, file %s, address %s\n",
21622 lbasename (subfile->name),
21623 paddress (gdbarch, address));
21624 }
21625
8c95582d 21626 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
21627}
21628
6f77053d
PA
21629void
21630lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21631{
d9b3de22
DE
21632 if (dwarf_line_debug)
21633 {
21634 fprintf_unfiltered (gdb_stdlog,
21635 "Processing actual line %u: file %u,"
94a72be7 21636 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 21637 m_line, m_file,
6f77053d 21638 paddress (m_gdbarch, m_address),
94a72be7
AB
21639 m_is_stmt, m_discriminator,
21640 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
21641 }
21642
6f77053d 21643 file_entry *fe = current_file ();
8c43009f
PA
21644
21645 if (fe == NULL)
d9b3de22
DE
21646 dwarf2_debug_line_missing_file_complaint ();
21647 /* For now we ignore lines not starting on an instruction boundary.
21648 But not when processing end_sequence for compatibility with the
21649 previous version of the code. */
6f77053d 21650 else if (m_op_index == 0 || end_sequence)
d9b3de22 21651 {
8c43009f 21652 fe->included_p = 1;
8c95582d 21653 if (m_record_lines_p)
d9b3de22 21654 {
1313c56e
AB
21655 /* When we switch files we insert an end maker in the first file,
21656 switch to the second file and add a new line entry. The
21657 problem is that the end marker inserted in the first file will
21658 discard any previous line entries at the same address. If the
21659 line entries in the first file are marked as is-stmt, while
21660 the new line in the second file is non-stmt, then this means
21661 the end marker will discard is-stmt lines so we can have a
21662 non-stmt line. This means that there are less addresses at
21663 which the user can insert a breakpoint.
21664
21665 To improve this we track the last address in m_last_address,
21666 and whether we have seen an is-stmt at this address. Then
21667 when switching files, if we have seen a stmt at the current
21668 address, and we are switching to create a non-stmt line, then
21669 discard the new line. */
21670 bool file_changed
21671 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21672 bool ignore_this_line
dda83cd7
SM
21673 = ((file_changed && !end_sequence && m_last_address == m_address
21674 && !m_is_stmt && m_stmt_at_address)
21675 || (!end_sequence && m_line == 0));
1313c56e
AB
21676
21677 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 21678 {
804d2729
TT
21679 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21680 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21681 }
21682
1313c56e 21683 if (!end_sequence && !ignore_this_line)
d9b3de22 21684 {
8c95582d
AB
21685 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21686
804d2729 21687 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21688 m_line_has_non_zero_discriminator,
21689 m_last_subfile))
d9b3de22 21690 {
c24bdb02 21691 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21692 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21693 builder->get_current_subfile (),
8c95582d 21694 m_line, m_address, is_stmt,
804d2729 21695 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21696 }
c24bdb02 21697 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21698 m_last_line = m_line;
d9b3de22
DE
21699 }
21700 }
21701 }
1313c56e
AB
21702
21703 /* Track whether we have seen any m_is_stmt true at m_address in case we
21704 have multiple line table entries all at m_address. */
21705 if (m_last_address != m_address)
21706 {
21707 m_stmt_at_address = false;
21708 m_last_address = m_address;
21709 }
21710 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
21711}
21712
804d2729
TT
21713lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21714 line_header *lh, bool record_lines_p)
d9b3de22 21715{
804d2729 21716 m_cu = cu;
6f77053d
PA
21717 m_gdbarch = arch;
21718 m_record_lines_p = record_lines_p;
21719 m_line_header = lh;
d9b3de22 21720
804d2729 21721 m_currently_recording_lines = true;
d9b3de22 21722
d9b3de22
DE
21723 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21724 was a line entry for it so that the backend has a chance to adjust it
21725 and also record it in case it needs it. This is currently used by MIPS
21726 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21727 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21728 m_is_stmt = lh->default_is_stmt;
21729 m_discriminator = 0;
1313c56e
AB
21730
21731 m_last_address = m_address;
21732 m_stmt_at_address = false;
252a6764
DE
21733}
21734
6f77053d
PA
21735void
21736lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21737 const gdb_byte *line_ptr,
7ab6656f 21738 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21739{
a8caed5d
FS
21740 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21741 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21742 located at 0x0. In this case, additionally check that if
21743 ADDRESS < UNRELOCATED_LOWPC. */
924c2928 21744
a8caed5d
FS
21745 if ((address == 0 && address < unrelocated_lowpc)
21746 || address == (CORE_ADDR) -1)
924c2928
DE
21747 {
21748 /* This line table is for a function which has been
21749 GCd by the linker. Ignore it. PR gdb/12528 */
21750
5e22e966 21751 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
21752 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21753
b98664d3 21754 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21755 line_offset, objfile_name (objfile));
804d2729
TT
21756 m_currently_recording_lines = false;
21757 /* Note: m_currently_recording_lines is left as false until we see
21758 DW_LNE_end_sequence. */
924c2928
DE
21759 }
21760}
21761
f3f5162e 21762/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21763 Process the line number information in LH.
21764 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21765 program in order to set included_p for every referenced header. */
debd256d 21766
c906108c 21767static void
43f3e411
DE
21768dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21769 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21770{
d521ce57
TT
21771 const gdb_byte *line_ptr, *extended_end;
21772 const gdb_byte *line_end;
a8c50c1f 21773 unsigned int bytes_read, extended_len;
699ca60a 21774 unsigned char op_code, extended_op;
e142c38c 21775 CORE_ADDR baseaddr;
5e22e966 21776 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21777 bfd *abfd = objfile->obfd;
08feed99 21778 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
21779 /* True if we're recording line info (as opposed to building partial
21780 symtabs and just interested in finding include files mentioned by
21781 the line number program). */
21782 bool record_lines_p = !decode_for_pst_p;
e142c38c 21783
b3b3bada 21784 baseaddr = objfile->text_section_offset ();
c906108c 21785
debd256d
JB
21786 line_ptr = lh->statement_program_start;
21787 line_end = lh->statement_program_end;
c906108c
SS
21788
21789 /* Read the statement sequences until there's nothing left. */
21790 while (line_ptr < line_end)
21791 {
6f77053d
PA
21792 /* The DWARF line number program state machine. Reset the state
21793 machine at the start of each sequence. */
804d2729 21794 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21795 bool end_sequence = false;
d9b3de22 21796
8c43009f 21797 if (record_lines_p)
c906108c 21798 {
8c43009f
PA
21799 /* Start a subfile for the current file of the state
21800 machine. */
21801 const file_entry *fe = state_machine.current_file ();
21802
21803 if (fe != NULL)
804d2729 21804 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21805 }
21806
a738430d 21807 /* Decode the table. */
d9b3de22 21808 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21809 {
21810 op_code = read_1_byte (abfd, line_ptr);
21811 line_ptr += 1;
9aa1fe7e 21812
debd256d 21813 if (op_code >= lh->opcode_base)
6e70227d 21814 {
8e07a239 21815 /* Special opcode. */
6f77053d 21816 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21817 }
21818 else switch (op_code)
c906108c
SS
21819 {
21820 case DW_LNS_extended_op:
3e43a32a
MS
21821 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21822 &bytes_read);
473b7be6 21823 line_ptr += bytes_read;
a8c50c1f 21824 extended_end = line_ptr + extended_len;
c906108c
SS
21825 extended_op = read_1_byte (abfd, line_ptr);
21826 line_ptr += 1;
8f34b746
TV
21827 if (DW_LNE_lo_user <= extended_op
21828 && extended_op <= DW_LNE_hi_user)
21829 {
21830 /* Vendor extension, ignore. */
21831 line_ptr = extended_end;
21832 break;
21833 }
c906108c
SS
21834 switch (extended_op)
21835 {
21836 case DW_LNE_end_sequence:
6f77053d
PA
21837 state_machine.handle_end_sequence ();
21838 end_sequence = true;
c906108c
SS
21839 break;
21840 case DW_LNE_set_address:
d9b3de22
DE
21841 {
21842 CORE_ADDR address
c8a7a66f 21843 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 21844 line_ptr += bytes_read;
6f77053d
PA
21845
21846 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21847 lowpc - baseaddr, address);
6f77053d 21848 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21849 }
c906108c
SS
21850 break;
21851 case DW_LNE_define_file:
dda83cd7
SM
21852 {
21853 const char *cur_file;
ecfb656c
PA
21854 unsigned int mod_time, length;
21855 dir_index dindex;
6e70227d 21856
dda83cd7 21857 cur_file = read_direct_string (abfd, line_ptr,
3e43a32a 21858 &bytes_read);
dda83cd7
SM
21859 line_ptr += bytes_read;
21860 dindex = (dir_index)
21861 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21862 line_ptr += bytes_read;
21863 mod_time =
21864 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21865 line_ptr += bytes_read;
21866 length =
21867 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21868 line_ptr += bytes_read;
21869 lh->add_file_name (cur_file, dindex, mod_time, length);
21870 }
c906108c 21871 break;
d0c6ba3d 21872 case DW_LNE_set_discriminator:
6f77053d
PA
21873 {
21874 /* The discriminator is not interesting to the
21875 debugger; just ignore it. We still need to
21876 check its value though:
21877 if there are consecutive entries for the same
21878 (non-prologue) line we want to coalesce them.
21879 PR 17276. */
21880 unsigned int discr
21881 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21882 line_ptr += bytes_read;
21883
21884 state_machine.handle_set_discriminator (discr);
21885 }
d0c6ba3d 21886 break;
c906108c 21887 default:
b98664d3 21888 complaint (_("mangled .debug_line section"));
debd256d 21889 return;
c906108c 21890 }
a8c50c1f
DJ
21891 /* Make sure that we parsed the extended op correctly. If e.g.
21892 we expected a different address size than the producer used,
21893 we may have read the wrong number of bytes. */
21894 if (line_ptr != extended_end)
21895 {
b98664d3 21896 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21897 return;
21898 }
c906108c
SS
21899 break;
21900 case DW_LNS_copy:
6f77053d 21901 state_machine.handle_copy ();
c906108c
SS
21902 break;
21903 case DW_LNS_advance_pc:
2dc7f7b3
TT
21904 {
21905 CORE_ADDR adjust
21906 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21907 line_ptr += bytes_read;
6f77053d
PA
21908
21909 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21910 }
c906108c
SS
21911 break;
21912 case DW_LNS_advance_line:
a05a36a5
DE
21913 {
21914 int line_delta
21915 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21916 line_ptr += bytes_read;
6f77053d
PA
21917
21918 state_machine.handle_advance_line (line_delta);
a05a36a5 21919 }
c906108c
SS
21920 break;
21921 case DW_LNS_set_file:
d9b3de22 21922 {
6f77053d 21923 file_name_index file
ecfb656c
PA
21924 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21925 &bytes_read);
d9b3de22 21926 line_ptr += bytes_read;
8c43009f 21927
6f77053d 21928 state_machine.handle_set_file (file);
d9b3de22 21929 }
c906108c
SS
21930 break;
21931 case DW_LNS_set_column:
0ad93d4f 21932 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21933 line_ptr += bytes_read;
21934 break;
21935 case DW_LNS_negate_stmt:
6f77053d 21936 state_machine.handle_negate_stmt ();
c906108c
SS
21937 break;
21938 case DW_LNS_set_basic_block:
c906108c 21939 break;
c2c6d25f
JM
21940 /* Add to the address register of the state machine the
21941 address increment value corresponding to special opcode
a738430d
MK
21942 255. I.e., this value is scaled by the minimum
21943 instruction length since special opcode 255 would have
b021a221 21944 scaled the increment. */
c906108c 21945 case DW_LNS_const_add_pc:
6f77053d 21946 state_machine.handle_const_add_pc ();
c906108c
SS
21947 break;
21948 case DW_LNS_fixed_advance_pc:
3e29f34a 21949 {
6f77053d 21950 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21951 line_ptr += 2;
6f77053d
PA
21952
21953 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21954 }
c906108c 21955 break;
9aa1fe7e 21956 default:
a738430d
MK
21957 {
21958 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21959 int i;
a738430d 21960
debd256d 21961 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21962 {
21963 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21964 line_ptr += bytes_read;
21965 }
21966 }
c906108c
SS
21967 }
21968 }
d9b3de22
DE
21969
21970 if (!end_sequence)
21971 dwarf2_debug_line_missing_end_sequence_complaint ();
21972
21973 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21974 in which case we still finish recording the last line). */
6f77053d 21975 state_machine.record_line (true);
c906108c 21976 }
f3f5162e
DE
21977}
21978
21979/* Decode the Line Number Program (LNP) for the given line_header
21980 structure and CU. The actual information extracted and the type
21981 of structures created from the LNP depends on the value of PST.
21982
21983 1. If PST is NULL, then this procedure uses the data from the program
21984 to create all necessary symbol tables, and their linetables.
21985
21986 2. If PST is not NULL, this procedure reads the program to determine
21987 the list of files included by the unit represented by PST, and
21988 builds all the associated partial symbol tables.
21989
21990 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21991 It is used for relative paths in the line table.
21992 NOTE: When processing partial symtabs (pst != NULL),
21993 comp_dir == pst->dirname.
21994
21995 NOTE: It is important that psymtabs have the same file name (via strcmp)
21996 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21997 symtab we don't use it in the name of the psymtabs we create.
21998 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21999 A good testcase for this is mb-inline.exp.
22000
527f3840
JK
22001 LOWPC is the lowest address in CU (or 0 if not known).
22002
22003 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
22004 for its PC<->lines mapping information. Otherwise only the filename
22005 table is read in. */
f3f5162e
DE
22006
22007static void
22008dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 22009 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 22010 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 22011{
5e22e966 22012 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 22013 const int decode_for_pst_p = (pst != NULL);
f3f5162e 22014
527f3840
JK
22015 if (decode_mapping)
22016 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
22017
22018 if (decode_for_pst_p)
22019 {
aaa75496 22020 /* Now that we're done scanning the Line Header Program, we can
dda83cd7 22021 create the psymtab of each included file. */
7ba99d21 22022 for (auto &file_entry : lh->file_names ())
dda83cd7
SM
22023 if (file_entry.included_p == 1)
22024 {
c89b44cd 22025 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 22026 const char *include_name =
7ba99d21
AT
22027 psymtab_include_file_name (lh, file_entry, pst,
22028 comp_dir, &name_holder);
c6da4cef 22029 if (include_name != NULL)
7b249e47
TT
22030 dwarf2_create_include_psymtab
22031 (cu->per_objfile->per_bfd, include_name, pst,
22032 cu->per_objfile->per_bfd->partial_symtabs.get (),
22033 objfile);
dda83cd7 22034 }
aaa75496 22035 }
cb1df416
DJ
22036 else
22037 {
22038 /* Make sure a symtab is created for every file, even files
22039 which contain only variables (i.e. no code with associated
22040 line numbers). */
c24bdb02
KS
22041 buildsym_compunit *builder = cu->get_builder ();
22042 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 22043
7ba99d21 22044 for (auto &fe : lh->file_names ())
cb1df416 22045 {
804d2729 22046 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 22047 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 22048 {
c24bdb02 22049 builder->get_current_subfile ()->symtab
804d2729 22050 = allocate_symtab (cust,
c24bdb02 22051 builder->get_current_subfile ()->name);
43f3e411 22052 }
c24bdb02 22053 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
22054 }
22055 }
c906108c
SS
22056}
22057
22058/* Start a subfile for DWARF. FILENAME is the name of the file and
22059 DIRNAME the name of the source directory which contains FILENAME
4d663531 22060 or NULL if not known.
c906108c
SS
22061 This routine tries to keep line numbers from identical absolute and
22062 relative file names in a common subfile.
22063
22064 Using the `list' example from the GDB testsuite, which resides in
22065 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
22066 of /srcdir/list0.c yields the following debugging information for list0.c:
22067
c5aa993b 22068 DW_AT_name: /srcdir/list0.c
4d663531 22069 DW_AT_comp_dir: /compdir
357e46e7 22070 files.files[0].name: list0.h
c5aa993b 22071 files.files[0].dir: /srcdir
357e46e7 22072 files.files[1].name: list0.c
c5aa993b 22073 files.files[1].dir: /srcdir
c906108c
SS
22074
22075 The line number information for list0.c has to end up in a single
4f1520fb
FR
22076 subfile, so that `break /srcdir/list0.c:1' works as expected.
22077 start_subfile will ensure that this happens provided that we pass the
22078 concatenation of files.files[1].dir and files.files[1].name as the
22079 subfile's name. */
c906108c
SS
22080
22081static void
804d2729
TT
22082dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
22083 const char *dirname)
c906108c 22084{
43816ebc 22085 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 22086
4d663531 22087 /* In order not to lose the line information directory,
4f1520fb
FR
22088 we concatenate it to the filename when it makes sense.
22089 Note that the Dwarf3 standard says (speaking of filenames in line
22090 information): ``The directory index is ignored for file names
22091 that represent full path names''. Thus ignoring dirname in the
22092 `else' branch below isn't an issue. */
c906108c 22093
d5166ae1 22094 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 22095 {
43816ebc
TT
22096 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
22097 filename = copy.get ();
d521ce57 22098 }
c906108c 22099
c24bdb02 22100 cu->get_builder ()->start_subfile (filename);
c906108c
SS
22101}
22102
804d2729
TT
22103/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
22104 buildsym_compunit constructor. */
f4dc4d17 22105
c24bdb02
KS
22106struct compunit_symtab *
22107dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
22108 CORE_ADDR low_pc)
f4dc4d17 22109{
c24bdb02 22110 gdb_assert (m_builder == nullptr);
43f3e411 22111
c24bdb02 22112 m_builder.reset (new struct buildsym_compunit
f6e649dd 22113 (this->per_objfile->objfile,
c24bdb02 22114 name, comp_dir, language, low_pc));
93b8bea4 22115
c24bdb02 22116 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 22117
c24bdb02
KS
22118 get_builder ()->record_debugformat ("DWARF 2");
22119 get_builder ()->record_producer (producer);
f4dc4d17 22120
c24bdb02 22121 processing_has_namespace_info = false;
43f3e411 22122
c24bdb02 22123 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
22124}
22125
4c2df51b
DJ
22126static void
22127var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 22128 struct dwarf2_cu *cu)
4c2df51b 22129{
5e22e966 22130 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
22131 struct comp_unit_head *cu_header = &cu->header;
22132
4c2df51b
DJ
22133 /* NOTE drow/2003-01-30: There used to be a comment and some special
22134 code here to turn a symbol with DW_AT_external and a
22135 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
22136 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
22137 with some versions of binutils) where shared libraries could have
22138 relocations against symbols in their debug information - the
22139 minimal symbol would have the right address, but the debug info
22140 would not. It's no longer necessary, because we will explicitly
22141 apply relocations when we read in the debug information now. */
22142
22143 /* A DW_AT_location attribute with no contents indicates that a
22144 variable has been optimized away. */
9d2246fc 22145 if (attr->form_is_block () && attr->as_block ()->size == 0)
4c2df51b 22146 {
f1e6e072 22147 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
22148 return;
22149 }
22150
22151 /* Handle one degenerate form of location expression specially, to
22152 preserve GDB's previous behavior when section offsets are
336d760d
AT
22153 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
22154 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 22155
9d2246fc
TT
22156 if (attr->form_is_block ())
22157 {
22158 struct dwarf_block *block = attr->as_block ();
22159
22160 if ((block->data[0] == DW_OP_addr
22161 && block->size == 1 + cu_header->addr_size)
22162 || ((block->data[0] == DW_OP_GNU_addr_index
dda83cd7 22163 || block->data[0] == DW_OP_addrx)
9d2246fc
TT
22164 && (block->size
22165 == 1 + leb128_size (&block->data[1]))))
22166 {
22167 unsigned int dummy;
22168
22169 if (block->data[0] == DW_OP_addr)
22170 SET_SYMBOL_VALUE_ADDRESS
22171 (sym, cu->header.read_address (objfile->obfd,
22172 block->data + 1,
38583298 22173 &dummy));
9d2246fc
TT
22174 else
22175 SET_SYMBOL_VALUE_ADDRESS
22176 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
22177 &dummy));
22178 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
22179 fixup_symbol_section (sym, objfile);
22180 SET_SYMBOL_VALUE_ADDRESS
22181 (sym,
22182 SYMBOL_VALUE_ADDRESS (sym)
a52d653e 22183 + objfile->section_offsets[sym->section_index ()]);
9d2246fc
TT
22184 return;
22185 }
4c2df51b
DJ
22186 }
22187
22188 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
22189 expression evaluator, and use LOC_COMPUTED only when necessary
22190 (i.e. when the value of a register or memory location is
22191 referenced, or a thread-local block, etc.). Then again, it might
22192 not be worthwhile. I'm assuming that it isn't unless performance
22193 or memory numbers show me otherwise. */
22194
f1e6e072 22195 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 22196
f1e6e072 22197 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 22198 cu->has_loclist = true;
4c2df51b
DJ
22199}
22200
c906108c
SS
22201/* Given a pointer to a DWARF information entry, figure out if we need
22202 to make a symbol table entry for it, and if so, create a new entry
22203 and return a pointer to it.
22204 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
22205 used the passed type.
22206 If SPACE is not NULL, use it to hold the new symbol. If it is
22207 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
22208
22209static struct symbol *
5e2db402
TT
22210new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
22211 struct symbol *space)
c906108c 22212{
976ca316
SM
22213 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22214 struct objfile *objfile = per_objfile->objfile;
08feed99 22215 struct gdbarch *gdbarch = objfile->arch ();
c906108c 22216 struct symbol *sym = NULL;
15d034d0 22217 const char *name;
c906108c
SS
22218 struct attribute *attr = NULL;
22219 struct attribute *attr2 = NULL;
e142c38c 22220 CORE_ADDR baseaddr;
e37fd15a
SW
22221 struct pending **list_to_add = NULL;
22222
edb3359d 22223 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 22224
b3b3bada 22225 baseaddr = objfile->text_section_offset ();
c906108c 22226
94af9270 22227 name = dwarf2_name (die, cu);
c906108c
SS
22228 if (name)
22229 {
34eaf542 22230 int suppress_add = 0;
94af9270 22231
34eaf542
TT
22232 if (space)
22233 sym = space;
22234 else
8c14c3a3 22235 sym = new (&objfile->objfile_obstack) symbol;
c906108c 22236 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
22237
22238 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 22239 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
22240 /* Fortran does not have mangling standard and the mangling does differ
22241 between gfortran, iFort etc. */
bcfe6157
TT
22242 const char *physname
22243 = (cu->language == language_fortran
22244 ? dwarf2_full_name (name, die, cu)
22245 : dwarf2_physname (name, die, cu));
22246 const char *linkagename = dw2_linkage_name (die, cu);
22247
22248 if (linkagename == nullptr || cu->language == language_ada)
22249 sym->set_linkage_name (physname);
22250 else
22251 {
22252 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22253 sym->set_linkage_name (linkagename);
22254 }
f55ee35c 22255
c906108c 22256 /* Default assumptions.
dda83cd7 22257 Use the passed type or decode it from the die. */
176620f1 22258 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 22259 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
22260 if (type != NULL)
22261 SYMBOL_TYPE (sym) = type;
22262 else
e7c27a73 22263 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
22264 attr = dwarf2_attr (die,
22265 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22266 cu);
435d3d88 22267 if (attr != nullptr)
529908cb 22268 SYMBOL_LINE (sym) = attr->constant_value (0);
cb1df416 22269
edb3359d
DJ
22270 attr = dwarf2_attr (die,
22271 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22272 cu);
aa659cfa 22273 if (attr != nullptr && attr->is_nonnegative ())
cb1df416 22274 {
529908cb 22275 file_name_index file_index
aa659cfa 22276 = (file_name_index) attr->as_nonnegative ();
8c43009f 22277 struct file_entry *fe;
9a619af0 22278
ecfb656c
PA
22279 if (cu->line_header != NULL)
22280 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
22281 else
22282 fe = NULL;
22283
22284 if (fe == NULL)
b98664d3 22285 complaint (_("file index out of range"));
8c43009f
PA
22286 else
22287 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
22288 }
22289
c906108c
SS
22290 switch (die->tag)
22291 {
22292 case DW_TAG_label:
e142c38c 22293 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 22294 if (attr != nullptr)
3e29f34a
MR
22295 {
22296 CORE_ADDR addr;
22297
95f982e5 22298 addr = attr->as_address ();
3e29f34a 22299 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 22300 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
8f5c6526 22301 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
3e29f34a 22302 }
8f5c6526
TV
22303 else
22304 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
0f5238ed
TT
22305 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22306 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
d3cb6808 22307 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
22308 break;
22309 case DW_TAG_subprogram:
22310 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22311 finish_block. */
f1e6e072 22312 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 22313 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 22314 if ((attr2 != nullptr && attr2->as_boolean ())
0a4b0913
AB
22315 || cu->language == language_ada
22316 || cu->language == language_fortran)
c906108c 22317 {
dda83cd7
SM
22318 /* Subprograms marked external are stored as a global symbol.
22319 Ada and Fortran subprograms, whether marked external or
22320 not, are always stored as a global symbol, because we want
22321 to be able to access them globally. For instance, we want
22322 to be able to break on a nested subprogram without having
22323 to specify the context. */
c24bdb02 22324 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
22325 }
22326 else
22327 {
e37fd15a 22328 list_to_add = cu->list_in_scope;
c906108c
SS
22329 }
22330 break;
edb3359d
DJ
22331 case DW_TAG_inlined_subroutine:
22332 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22333 finish_block. */
f1e6e072 22334 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 22335 SYMBOL_INLINED (sym) = 1;
481860b3 22336 list_to_add = cu->list_in_scope;
edb3359d 22337 break;
34eaf542
TT
22338 case DW_TAG_template_value_param:
22339 suppress_add = 1;
22340 /* Fall through. */
72929c62 22341 case DW_TAG_constant:
c906108c 22342 case DW_TAG_variable:
254e6b9e 22343 case DW_TAG_member:
0963b4bd
MS
22344 /* Compilation with minimal debug info may result in
22345 variables with missing type entries. Change the
22346 misleading `void' type to something sensible. */
78134374 22347 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 22348 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 22349
e142c38c 22350 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
22351 /* In the case of DW_TAG_member, we should only be called for
22352 static const members. */
22353 if (die->tag == DW_TAG_member)
22354 {
3863f96c
DE
22355 /* dwarf2_add_field uses die_is_declaration,
22356 so we do the same. */
254e6b9e
DE
22357 gdb_assert (die_is_declaration (die, cu));
22358 gdb_assert (attr);
22359 }
435d3d88 22360 if (attr != nullptr)
c906108c 22361 {
e7c27a73 22362 dwarf2_const_value (attr, sym, cu);
e142c38c 22363 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 22364 if (!suppress_add)
34eaf542 22365 {
c45bc3f8 22366 if (attr2 != nullptr && attr2->as_boolean ())
c24bdb02 22367 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 22368 else
e37fd15a 22369 list_to_add = cu->list_in_scope;
34eaf542 22370 }
c906108c
SS
22371 break;
22372 }
e142c38c 22373 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22374 if (attr != nullptr)
c906108c 22375 {
e7c27a73 22376 var_decode_location (attr, sym, cu);
e142c38c 22377 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
22378
22379 /* Fortran explicitly imports any global symbols to the local
22380 scope by DW_TAG_common_block. */
22381 if (cu->language == language_fortran && die->parent
22382 && die->parent->tag == DW_TAG_common_block)
22383 attr2 = NULL;
22384
caac4577
JG
22385 if (SYMBOL_CLASS (sym) == LOC_STATIC
22386 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 22387 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
22388 {
22389 /* When a static variable is eliminated by the linker,
22390 the corresponding debug information is not stripped
22391 out, but the variable address is set to null;
22392 do not add such variables into symbol table. */
22393 }
c45bc3f8 22394 else if (attr2 != nullptr && attr2->as_boolean ())
1c809c68 22395 {
4b610737
TT
22396 if (SYMBOL_CLASS (sym) == LOC_STATIC
22397 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 22398 && per_objfile->per_bfd->can_copy)
4b610737
TT
22399 {
22400 /* A global static variable might be subject to
22401 copy relocation. We first check for a local
22402 minsym, though, because maybe the symbol was
22403 marked hidden, in which case this would not
22404 apply. */
22405 bound_minimal_symbol found
22406 = (lookup_minimal_symbol_linkage
987012b8 22407 (sym->linkage_name (), objfile));
4b610737
TT
22408 if (found.minsym != nullptr)
22409 sym->maybe_copied = 1;
22410 }
f55ee35c 22411
1c809c68
TT
22412 /* A variable with DW_AT_external is never static,
22413 but it may be block-scoped. */
804d2729 22414 list_to_add
c24bdb02
KS
22415 = ((cu->list_in_scope
22416 == cu->get_builder ()->get_file_symbols ())
22417 ? cu->get_builder ()->get_global_symbols ()
804d2729 22418 : cu->list_in_scope);
1c809c68 22419 }
c906108c 22420 else
e37fd15a 22421 list_to_add = cu->list_in_scope;
c906108c
SS
22422 }
22423 else
22424 {
22425 /* We do not know the address of this symbol.
dda83cd7
SM
22426 If it is an external symbol and we have type information
22427 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22428 The address of the variable will then be determined from
22429 the minimal symbol table whenever the variable is
22430 referenced. */
e142c38c 22431 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
22432
22433 /* Fortran explicitly imports any global symbols to the local
22434 scope by DW_TAG_common_block. */
22435 if (cu->language == language_fortran && die->parent
22436 && die->parent->tag == DW_TAG_common_block)
22437 {
22438 /* SYMBOL_CLASS doesn't matter here because
22439 read_common_block is going to reset it. */
22440 if (!suppress_add)
22441 list_to_add = cu->list_in_scope;
22442 }
c45bc3f8 22443 else if (attr2 != nullptr && attr2->as_boolean ()
0971de02 22444 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 22445 {
0fe7935b
DJ
22446 /* A variable with DW_AT_external is never static, but it
22447 may be block-scoped. */
804d2729 22448 list_to_add
c24bdb02
KS
22449 = ((cu->list_in_scope
22450 == cu->get_builder ()->get_file_symbols ())
22451 ? cu->get_builder ()->get_global_symbols ()
804d2729 22452 : cu->list_in_scope);
0fe7935b 22453
f1e6e072 22454 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 22455 }
442ddf59
JK
22456 else if (!die_is_declaration (die, cu))
22457 {
22458 /* Use the default LOC_OPTIMIZED_OUT class. */
22459 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22460 if (!suppress_add)
22461 list_to_add = cu->list_in_scope;
442ddf59 22462 }
c906108c
SS
22463 }
22464 break;
22465 case DW_TAG_formal_parameter:
a60f3166
TT
22466 {
22467 /* If we are inside a function, mark this as an argument. If
22468 not, we might be looking at an argument to an inlined function
22469 when we do not have enough information to show inlined frames;
22470 pretend it's a local variable in that case so that the user can
22471 still see it. */
804d2729 22472 struct context_stack *curr
c24bdb02 22473 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
22474 if (curr != nullptr && curr->name != nullptr)
22475 SYMBOL_IS_ARGUMENT (sym) = 1;
22476 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22477 if (attr != nullptr)
a60f3166
TT
22478 {
22479 var_decode_location (attr, sym, cu);
22480 }
22481 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22482 if (attr != nullptr)
a60f3166
TT
22483 {
22484 dwarf2_const_value (attr, sym, cu);
22485 }
f346a30d 22486
a60f3166
TT
22487 list_to_add = cu->list_in_scope;
22488 }
c906108c
SS
22489 break;
22490 case DW_TAG_unspecified_parameters:
22491 /* From varargs functions; gdb doesn't seem to have any
22492 interest in this information, so just ignore it for now.
22493 (FIXME?) */
22494 break;
34eaf542
TT
22495 case DW_TAG_template_type_param:
22496 suppress_add = 1;
22497 /* Fall through. */
c906108c 22498 case DW_TAG_class_type:
680b30c7 22499 case DW_TAG_interface_type:
c906108c
SS
22500 case DW_TAG_structure_type:
22501 case DW_TAG_union_type:
72019c9c 22502 case DW_TAG_set_type:
c906108c 22503 case DW_TAG_enumeration_type:
f1e6e072 22504 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22505 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22506
63d06c5c 22507 {
9c37b5ae 22508 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22509 really ever be static objects: otherwise, if you try
22510 to, say, break of a class's method and you're in a file
22511 which doesn't mention that class, it won't work unless
22512 the check for all static symbols in lookup_symbol_aux
22513 saves you. See the OtherFileClass tests in
22514 gdb.c++/namespace.exp. */
22515
e37fd15a 22516 if (!suppress_add)
34eaf542 22517 {
c24bdb02 22518 buildsym_compunit *builder = cu->get_builder ();
804d2729 22519 list_to_add
c24bdb02 22520 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22521 && cu->language == language_cplus
c24bdb02 22522 ? builder->get_global_symbols ()
804d2729 22523 : cu->list_in_scope);
63d06c5c 22524
64382290 22525 /* The semantics of C++ state that "struct foo {
9c37b5ae 22526 ... }" also defines a typedef for "foo". */
64382290 22527 if (cu->language == language_cplus
45280282 22528 || cu->language == language_ada
c44af4eb
TT
22529 || cu->language == language_d
22530 || cu->language == language_rust)
64382290
TT
22531 {
22532 /* The symbol's name is already allocated along
22533 with this objfile, so we don't need to
22534 duplicate it for the type. */
7d93a1e0 22535 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 22536 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 22537 }
63d06c5c
DC
22538 }
22539 }
c906108c
SS
22540 break;
22541 case DW_TAG_typedef:
f1e6e072 22542 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22543 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22544 list_to_add = cu->list_in_scope;
63d06c5c 22545 break;
d8f62e84 22546 case DW_TAG_array_type:
c906108c 22547 case DW_TAG_base_type:
dda83cd7 22548 case DW_TAG_subrange_type:
f1e6e072 22549 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22550 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22551 list_to_add = cu->list_in_scope;
c906108c
SS
22552 break;
22553 case DW_TAG_enumerator:
e142c38c 22554 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22555 if (attr != nullptr)
c906108c 22556 {
e7c27a73 22557 dwarf2_const_value (attr, sym, cu);
c906108c 22558 }
63d06c5c
DC
22559 {
22560 /* NOTE: carlton/2003-11-10: See comment above in the
22561 DW_TAG_class_type, etc. block. */
22562
804d2729 22563 list_to_add
c24bdb02 22564 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22565 && cu->language == language_cplus
c24bdb02 22566 ? cu->get_builder ()->get_global_symbols ()
804d2729 22567 : cu->list_in_scope);
63d06c5c 22568 }
c906108c 22569 break;
74921315 22570 case DW_TAG_imported_declaration:
5c4e30ca 22571 case DW_TAG_namespace:
f1e6e072 22572 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22573 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22574 break;
530e8392
KB
22575 case DW_TAG_module:
22576 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22577 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22578 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22579 break;
4357ac6c 22580 case DW_TAG_common_block:
f1e6e072 22581 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22582 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22583 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22584 break;
c906108c
SS
22585 default:
22586 /* Not a tag we recognize. Hopefully we aren't processing
22587 trash data, but since we must specifically ignore things
22588 we don't recognize, there is nothing else we should do at
0963b4bd 22589 this point. */
b98664d3 22590 complaint (_("unsupported tag: '%s'"),
4d3c2250 22591 dwarf_tag_name (die->tag));
c906108c
SS
22592 break;
22593 }
df8a16a1 22594
e37fd15a
SW
22595 if (suppress_add)
22596 {
22597 sym->hash_next = objfile->template_symbols;
22598 objfile->template_symbols = sym;
22599 list_to_add = NULL;
22600 }
22601
22602 if (list_to_add != NULL)
d3cb6808 22603 add_symbol_to_list (sym, list_to_add);
e37fd15a 22604
df8a16a1
DJ
22605 /* For the benefit of old versions of GCC, check for anonymous
22606 namespaces based on the demangled name. */
4d4ec4e5 22607 if (!cu->processing_has_namespace_info
94af9270 22608 && cu->language == language_cplus)
c24bdb02 22609 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22610 }
22611 return (sym);
22612}
22613
98bfdba5
PA
22614/* Given an attr with a DW_FORM_dataN value in host byte order,
22615 zero-extend it as appropriate for the symbol's type. The DWARF
22616 standard (v4) is not entirely clear about the meaning of using
22617 DW_FORM_dataN for a constant with a signed type, where the type is
22618 wider than the data. The conclusion of a discussion on the DWARF
22619 list was that this is unspecified. We choose to always zero-extend
22620 because that is the interpretation long in use by GCC. */
c906108c 22621
98bfdba5 22622static gdb_byte *
ff39bb5e 22623dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22624 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22625{
5e22e966 22626 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
22627 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22628 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
529908cb 22629 LONGEST l = attr->constant_value (0);
98bfdba5
PA
22630
22631 if (bits < sizeof (*value) * 8)
22632 {
22633 l &= ((LONGEST) 1 << bits) - 1;
22634 *value = l;
22635 }
22636 else if (bits == sizeof (*value) * 8)
22637 *value = l;
22638 else
22639 {
224c3ddb 22640 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22641 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22642 return bytes;
22643 }
22644
22645 return NULL;
22646}
22647
22648/* Read a constant value from an attribute. Either set *VALUE, or if
22649 the value does not fit in *VALUE, set *BYTES - either already
22650 allocated on the objfile obstack, or newly allocated on OBSTACK,
22651 or, set *BATON, if we translated the constant to a location
22652 expression. */
22653
22654static void
ff39bb5e 22655dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22656 const char *name, struct obstack *obstack,
22657 struct dwarf2_cu *cu,
d521ce57 22658 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22659 struct dwarf2_locexpr_baton **baton)
22660{
5e22e966 22661 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 22662 struct objfile *objfile = per_objfile->objfile;
98bfdba5 22663 struct comp_unit_head *cu_header = &cu->header;
c906108c 22664 struct dwarf_block *blk;
98bfdba5
PA
22665 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22666 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22667
22668 *value = 0;
22669 *bytes = NULL;
22670 *baton = NULL;
c906108c
SS
22671
22672 switch (attr->form)
22673 {
22674 case DW_FORM_addr:
336d760d 22675 case DW_FORM_addrx:
3019eac3 22676 case DW_FORM_GNU_addr_index:
ac56253d 22677 {
ac56253d
TT
22678 gdb_byte *data;
22679
98bfdba5
PA
22680 if (TYPE_LENGTH (type) != cu_header->addr_size)
22681 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22682 cu_header->addr_size,
98bfdba5 22683 TYPE_LENGTH (type));
ac56253d
TT
22684 /* Symbols of this form are reasonably rare, so we just
22685 piggyback on the existing location code rather than writing
22686 a new implementation of symbol_computed_ops. */
8d749320 22687 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 22688 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
22689 (*baton)->per_cu = cu->per_cu;
22690 gdb_assert ((*baton)->per_cu);
ac56253d 22691
98bfdba5 22692 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22693 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22694 (*baton)->data = data;
ac56253d
TT
22695
22696 data[0] = DW_OP_addr;
22697 store_unsigned_integer (&data[1], cu_header->addr_size,
36d378cf 22698 byte_order, attr->as_address ());
ac56253d 22699 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22700 }
c906108c 22701 break;
4ac36638 22702 case DW_FORM_string:
93b5768b 22703 case DW_FORM_strp:
cf532bd1 22704 case DW_FORM_strx:
3019eac3 22705 case DW_FORM_GNU_str_index:
36586728 22706 case DW_FORM_GNU_strp_alt:
c6481205 22707 /* The string is already allocated on the objfile obstack, point
98bfdba5 22708 directly to it. */
2c830f54 22709 *bytes = (const gdb_byte *) attr->as_string ();
93b5768b 22710 break;
c906108c
SS
22711 case DW_FORM_block1:
22712 case DW_FORM_block2:
22713 case DW_FORM_block4:
22714 case DW_FORM_block:
2dc7f7b3 22715 case DW_FORM_exprloc:
0224619f 22716 case DW_FORM_data16:
9d2246fc 22717 blk = attr->as_block ();
98bfdba5
PA
22718 if (TYPE_LENGTH (type) != blk->size)
22719 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22720 TYPE_LENGTH (type));
22721 *bytes = blk->data;
c906108c 22722 break;
2df3850c
JM
22723
22724 /* The DW_AT_const_value attributes are supposed to carry the
22725 symbol's value "represented as it would be on the target
22726 architecture." By the time we get here, it's already been
22727 converted to host endianness, so we just need to sign- or
22728 zero-extend it as appropriate. */
22729 case DW_FORM_data1:
3aef2284 22730 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22731 break;
c906108c 22732 case DW_FORM_data2:
3aef2284 22733 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22734 break;
c906108c 22735 case DW_FORM_data4:
3aef2284 22736 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22737 break;
c906108c 22738 case DW_FORM_data8:
3aef2284 22739 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22740 break;
22741
c906108c 22742 case DW_FORM_sdata:
663c44ac 22743 case DW_FORM_implicit_const:
1bc397c5 22744 *value = attr->as_signed ();
2df3850c
JM
22745 break;
22746
c906108c 22747 case DW_FORM_udata:
529908cb 22748 *value = attr->as_unsigned ();
c906108c 22749 break;
2df3850c 22750
c906108c 22751 default:
b98664d3 22752 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22753 dwarf_form_name (attr->form));
98bfdba5 22754 *value = 0;
c906108c
SS
22755 break;
22756 }
22757}
22758
2df3850c 22759
98bfdba5
PA
22760/* Copy constant value from an attribute to a symbol. */
22761
2df3850c 22762static void
ff39bb5e 22763dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22764 struct dwarf2_cu *cu)
2df3850c 22765{
5e22e966 22766 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 22767 LONGEST value;
d521ce57 22768 const gdb_byte *bytes;
98bfdba5 22769 struct dwarf2_locexpr_baton *baton;
2df3850c 22770
98bfdba5 22771 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22772 sym->print_name (),
98bfdba5
PA
22773 &objfile->objfile_obstack, cu,
22774 &value, &bytes, &baton);
2df3850c 22775
98bfdba5
PA
22776 if (baton != NULL)
22777 {
98bfdba5 22778 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22779 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22780 }
22781 else if (bytes != NULL)
22782 {
22783 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22784 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22785 }
22786 else
22787 {
22788 SYMBOL_VALUE (sym) = value;
f1e6e072 22789 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22790 }
2df3850c
JM
22791}
22792
c906108c
SS
22793/* Return the type of the die in question using its DW_AT_type attribute. */
22794
22795static struct type *
e7c27a73 22796die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22797{
c906108c 22798 struct attribute *type_attr;
c906108c 22799
e142c38c 22800 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22801 if (!type_attr)
22802 {
5e22e966 22803 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22804 /* A missing DW_AT_type represents a void type. */
518817b3 22805 return objfile_type (objfile)->builtin_void;
c906108c 22806 }
348e048f 22807
673bfd45 22808 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22809}
22810
b4ba55a1
JB
22811/* True iff CU's producer generates GNAT Ada auxiliary information
22812 that allows to find parallel types through that information instead
22813 of having to do expensive parallel lookups by type name. */
22814
22815static int
22816need_gnat_info (struct dwarf2_cu *cu)
22817{
de4cb04a
JB
22818 /* Assume that the Ada compiler was GNAT, which always produces
22819 the auxiliary information. */
22820 return (cu->language == language_ada);
b4ba55a1
JB
22821}
22822
b4ba55a1
JB
22823/* Return the auxiliary type of the die in question using its
22824 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22825 attribute is not present. */
22826
22827static struct type *
22828die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22829{
b4ba55a1 22830 struct attribute *type_attr;
b4ba55a1
JB
22831
22832 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22833 if (!type_attr)
22834 return NULL;
22835
673bfd45 22836 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22837}
22838
22839/* If DIE has a descriptive_type attribute, then set the TYPE's
22840 descriptive type accordingly. */
22841
22842static void
22843set_descriptive_type (struct type *type, struct die_info *die,
22844 struct dwarf2_cu *cu)
22845{
22846 struct type *descriptive_type = die_descriptive_type (die, cu);
22847
22848 if (descriptive_type)
22849 {
22850 ALLOCATE_GNAT_AUX_TYPE (type);
22851 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22852 }
22853}
22854
c906108c
SS
22855/* Return the containing type of the die in question using its
22856 DW_AT_containing_type attribute. */
22857
22858static struct type *
e7c27a73 22859die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22860{
c906108c 22861 struct attribute *type_attr;
5e22e966 22862 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22863
e142c38c 22864 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22865 if (!type_attr)
22866 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22867 "[in module %s]"), objfile_name (objfile));
33ac96f0 22868
673bfd45 22869 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22870}
22871
ac9ec31b
DE
22872/* Return an error marker type to use for the ill formed type in DIE/CU. */
22873
22874static struct type *
22875build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22876{
976ca316
SM
22877 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22878 struct objfile *objfile = per_objfile->objfile;
528e1572 22879 char *saved;
ac9ec31b 22880
528e1572
SM
22881 std::string message
22882 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22883 objfile_name (objfile),
22884 sect_offset_str (cu->header.sect_off),
22885 sect_offset_str (die->sect_off));
efba19b0 22886 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22887
19f392bc 22888 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22889}
22890
673bfd45 22891/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22892 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22893 DW_AT_containing_type.
673bfd45
DE
22894 If there is no type substitute an error marker. */
22895
c906108c 22896static struct type *
ff39bb5e 22897lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22898 struct dwarf2_cu *cu)
c906108c 22899{
976ca316
SM
22900 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22901 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
22902 struct type *this_type;
22903
ac9ec31b
DE
22904 gdb_assert (attr->name == DW_AT_type
22905 || attr->name == DW_AT_GNAT_descriptive_type
22906 || attr->name == DW_AT_containing_type);
22907
673bfd45
DE
22908 /* First see if we have it cached. */
22909
36586728
TT
22910 if (attr->form == DW_FORM_GNU_ref_alt)
22911 {
22912 struct dwarf2_per_cu_data *per_cu;
0826b30a 22913 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 22914
976ca316
SM
22915 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22916 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 22917 }
cd6c91b4 22918 else if (attr->form_is_ref ())
673bfd45 22919 {
0826b30a 22920 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 22921
976ca316 22922 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 22923 }
55f1336d 22924 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22925 {
630ed6b9 22926 ULONGEST signature = attr->as_signature ();
673bfd45 22927
ac9ec31b 22928 return get_signatured_type (die, signature, cu);
673bfd45
DE
22929 }
22930 else
22931 {
b98664d3 22932 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22933 " at %s [in module %s]"),
22934 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22935 objfile_name (objfile));
ac9ec31b 22936 return build_error_marker_type (cu, die);
673bfd45
DE
22937 }
22938
22939 /* If not cached we need to read it in. */
22940
22941 if (this_type == NULL)
22942 {
ac9ec31b 22943 struct die_info *type_die = NULL;
673bfd45
DE
22944 struct dwarf2_cu *type_cu = cu;
22945
cd6c91b4 22946 if (attr->form_is_ref ())
ac9ec31b
DE
22947 type_die = follow_die_ref (die, attr, &type_cu);
22948 if (type_die == NULL)
22949 return build_error_marker_type (cu, die);
22950 /* If we find the type now, it's probably because the type came
3019eac3
DE
22951 from an inter-CU reference and the type's CU got expanded before
22952 ours. */
ac9ec31b 22953 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22954 }
22955
22956 /* If we still don't have a type use an error marker. */
22957
22958 if (this_type == NULL)
ac9ec31b 22959 return build_error_marker_type (cu, die);
673bfd45 22960
f792889a 22961 return this_type;
c906108c
SS
22962}
22963
673bfd45
DE
22964/* Return the type in DIE, CU.
22965 Returns NULL for invalid types.
22966
02142a6c 22967 This first does a lookup in die_type_hash,
673bfd45
DE
22968 and only reads the die in if necessary.
22969
22970 NOTE: This can be called when reading in partial or full symbols. */
22971
f792889a 22972static struct type *
e7c27a73 22973read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22974{
f792889a
DJ
22975 struct type *this_type;
22976
22977 this_type = get_die_type (die, cu);
22978 if (this_type)
22979 return this_type;
22980
673bfd45
DE
22981 return read_type_die_1 (die, cu);
22982}
22983
22984/* Read the type in DIE, CU.
22985 Returns NULL for invalid types. */
22986
22987static struct type *
22988read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22989{
22990 struct type *this_type = NULL;
22991
c906108c
SS
22992 switch (die->tag)
22993 {
22994 case DW_TAG_class_type:
680b30c7 22995 case DW_TAG_interface_type:
c906108c
SS
22996 case DW_TAG_structure_type:
22997 case DW_TAG_union_type:
f792889a 22998 this_type = read_structure_type (die, cu);
c906108c
SS
22999 break;
23000 case DW_TAG_enumeration_type:
f792889a 23001 this_type = read_enumeration_type (die, cu);
c906108c
SS
23002 break;
23003 case DW_TAG_subprogram:
23004 case DW_TAG_subroutine_type:
edb3359d 23005 case DW_TAG_inlined_subroutine:
f792889a 23006 this_type = read_subroutine_type (die, cu);
c906108c
SS
23007 break;
23008 case DW_TAG_array_type:
f792889a 23009 this_type = read_array_type (die, cu);
c906108c 23010 break;
72019c9c 23011 case DW_TAG_set_type:
f792889a 23012 this_type = read_set_type (die, cu);
72019c9c 23013 break;
c906108c 23014 case DW_TAG_pointer_type:
f792889a 23015 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
23016 break;
23017 case DW_TAG_ptr_to_member_type:
f792889a 23018 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
23019 break;
23020 case DW_TAG_reference_type:
4297a3f0
AV
23021 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
23022 break;
23023 case DW_TAG_rvalue_reference_type:
23024 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
23025 break;
23026 case DW_TAG_const_type:
f792889a 23027 this_type = read_tag_const_type (die, cu);
c906108c
SS
23028 break;
23029 case DW_TAG_volatile_type:
f792889a 23030 this_type = read_tag_volatile_type (die, cu);
c906108c 23031 break;
06d66ee9
TT
23032 case DW_TAG_restrict_type:
23033 this_type = read_tag_restrict_type (die, cu);
23034 break;
c906108c 23035 case DW_TAG_string_type:
f792889a 23036 this_type = read_tag_string_type (die, cu);
c906108c
SS
23037 break;
23038 case DW_TAG_typedef:
f792889a 23039 this_type = read_typedef (die, cu);
c906108c 23040 break;
a02abb62 23041 case DW_TAG_subrange_type:
f792889a 23042 this_type = read_subrange_type (die, cu);
a02abb62 23043 break;
c906108c 23044 case DW_TAG_base_type:
f792889a 23045 this_type = read_base_type (die, cu);
c906108c 23046 break;
81a17f79 23047 case DW_TAG_unspecified_type:
f792889a 23048 this_type = read_unspecified_type (die, cu);
81a17f79 23049 break;
0114d602
DJ
23050 case DW_TAG_namespace:
23051 this_type = read_namespace_type (die, cu);
23052 break;
f55ee35c
JK
23053 case DW_TAG_module:
23054 this_type = read_module_type (die, cu);
23055 break;
a2c2acaf
MW
23056 case DW_TAG_atomic_type:
23057 this_type = read_tag_atomic_type (die, cu);
23058 break;
c906108c 23059 default:
b98664d3 23060 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 23061 dwarf_tag_name (die->tag));
c906108c
SS
23062 break;
23063 }
63d06c5c 23064
f792889a 23065 return this_type;
63d06c5c
DC
23066}
23067
abc72ce4
DE
23068/* See if we can figure out if the class lives in a namespace. We do
23069 this by looking for a member function; its demangled name will
23070 contain namespace info, if there is any.
23071 Return the computed name or NULL.
23072 Space for the result is allocated on the objfile's obstack.
23073 This is the full-die version of guess_partial_die_structure_name.
23074 In this case we know DIE has no useful parent. */
23075
43816ebc 23076static const char *
abc72ce4
DE
23077guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
23078{
23079 struct die_info *spec_die;
23080 struct dwarf2_cu *spec_cu;
23081 struct die_info *child;
5e22e966 23082 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
23083
23084 spec_cu = cu;
23085 spec_die = die_specification (die, &spec_cu);
23086 if (spec_die != NULL)
23087 {
23088 die = spec_die;
23089 cu = spec_cu;
23090 }
23091
23092 for (child = die->child;
23093 child != NULL;
23094 child = child->sibling)
23095 {
23096 if (child->tag == DW_TAG_subprogram)
23097 {
73b9be8b 23098 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 23099
7d45c7c3 23100 if (linkage_name != NULL)
abc72ce4 23101 {
43816ebc 23102 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 23103 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 23104 const char *name = NULL;
abc72ce4
DE
23105
23106 if (actual_name != NULL)
23107 {
15d034d0 23108 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
23109
23110 if (die_name != NULL
43816ebc 23111 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
23112 {
23113 /* Strip off the class name from the full name.
23114 We want the prefix. */
23115 int die_name_len = strlen (die_name);
43816ebc
TT
23116 int actual_name_len = strlen (actual_name.get ());
23117 const char *ptr = actual_name.get ();
abc72ce4
DE
23118
23119 /* Test for '::' as a sanity check. */
23120 if (actual_name_len > die_name_len + 2
43816ebc 23121 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 23122 name = obstack_strndup (
e3b94546 23123 &objfile->per_bfd->storage_obstack,
43816ebc 23124 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
23125 }
23126 }
abc72ce4
DE
23127 return name;
23128 }
23129 }
23130 }
23131
23132 return NULL;
23133}
23134
96408a79
SA
23135/* GCC might emit a nameless typedef that has a linkage name. Determine the
23136 prefix part in such case. See
23137 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23138
a121b7c1 23139static const char *
96408a79
SA
23140anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
23141{
23142 struct attribute *attr;
e6a959d6 23143 const char *base;
96408a79
SA
23144
23145 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
23146 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
23147 return NULL;
23148
7d45c7c3 23149 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
23150 return NULL;
23151
73b9be8b 23152 attr = dw2_linkage_name_attr (die, cu);
2c830f54
TT
23153 const char *attr_name = attr->as_string ();
23154 if (attr == NULL || attr_name == NULL)
96408a79
SA
23155 return NULL;
23156
23157 /* dwarf2_name had to be already called. */
3b64bf15 23158 gdb_assert (attr->canonical_string_p ());
96408a79
SA
23159
23160 /* Strip the base name, keep any leading namespaces/classes. */
2c830f54
TT
23161 base = strrchr (attr_name, ':');
23162 if (base == NULL || base == attr_name || base[-1] != ':')
96408a79
SA
23163 return "";
23164
5e22e966 23165 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9 23166 return obstack_strndup (&objfile->per_bfd->storage_obstack,
2c830f54
TT
23167 attr_name,
23168 &base[-1] - attr_name);
96408a79
SA
23169}
23170
fdde2d81 23171/* Return the name of the namespace/class that DIE is defined within,
0114d602 23172 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 23173
0114d602
DJ
23174 For example, if we're within the method foo() in the following
23175 code:
23176
23177 namespace N {
23178 class C {
23179 void foo () {
23180 }
23181 };
23182 }
23183
23184 then determine_prefix on foo's die will return "N::C". */
fdde2d81 23185
0d5cff50 23186static const char *
e142c38c 23187determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 23188{
976ca316 23189 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
23190 struct die_info *parent, *spec_die;
23191 struct dwarf2_cu *spec_cu;
23192 struct type *parent_type;
a121b7c1 23193 const char *retval;
63d06c5c 23194
9c37b5ae 23195 if (cu->language != language_cplus
c44af4eb
TT
23196 && cu->language != language_fortran && cu->language != language_d
23197 && cu->language != language_rust)
0114d602
DJ
23198 return "";
23199
96408a79
SA
23200 retval = anonymous_struct_prefix (die, cu);
23201 if (retval)
23202 return retval;
23203
0114d602
DJ
23204 /* We have to be careful in the presence of DW_AT_specification.
23205 For example, with GCC 3.4, given the code
23206
23207 namespace N {
23208 void foo() {
23209 // Definition of N::foo.
23210 }
23211 }
23212
23213 then we'll have a tree of DIEs like this:
23214
23215 1: DW_TAG_compile_unit
23216 2: DW_TAG_namespace // N
23217 3: DW_TAG_subprogram // declaration of N::foo
23218 4: DW_TAG_subprogram // definition of N::foo
23219 DW_AT_specification // refers to die #3
23220
23221 Thus, when processing die #4, we have to pretend that we're in
23222 the context of its DW_AT_specification, namely the contex of die
23223 #3. */
23224 spec_cu = cu;
23225 spec_die = die_specification (die, &spec_cu);
23226 if (spec_die == NULL)
23227 parent = die->parent;
23228 else
63d06c5c 23229 {
0114d602
DJ
23230 parent = spec_die->parent;
23231 cu = spec_cu;
63d06c5c 23232 }
0114d602
DJ
23233
23234 if (parent == NULL)
23235 return "";
98bfdba5
PA
23236 else if (parent->building_fullname)
23237 {
23238 const char *name;
23239 const char *parent_name;
23240
23241 /* It has been seen on RealView 2.2 built binaries,
23242 DW_TAG_template_type_param types actually _defined_ as
23243 children of the parent class:
23244
23245 enum E {};
23246 template class <class Enum> Class{};
23247 Class<enum E> class_e;
23248
dda83cd7
SM
23249 1: DW_TAG_class_type (Class)
23250 2: DW_TAG_enumeration_type (E)
23251 3: DW_TAG_enumerator (enum1:0)
23252 3: DW_TAG_enumerator (enum2:1)
23253 ...
23254 2: DW_TAG_template_type_param
23255 DW_AT_type DW_FORM_ref_udata (E)
98bfdba5
PA
23256
23257 Besides being broken debug info, it can put GDB into an
23258 infinite loop. Consider:
23259
23260 When we're building the full name for Class<E>, we'll start
23261 at Class, and go look over its template type parameters,
23262 finding E. We'll then try to build the full name of E, and
23263 reach here. We're now trying to build the full name of E,
23264 and look over the parent DIE for containing scope. In the
23265 broken case, if we followed the parent DIE of E, we'd again
23266 find Class, and once again go look at its template type
23267 arguments, etc., etc. Simply don't consider such parent die
23268 as source-level parent of this die (it can't be, the language
23269 doesn't allow it), and break the loop here. */
23270 name = dwarf2_name (die, cu);
23271 parent_name = dwarf2_name (parent, cu);
b98664d3 23272 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
23273 name ? name : "<unknown>",
23274 parent_name ? parent_name : "<unknown>");
23275 return "";
23276 }
63d06c5c 23277 else
0114d602
DJ
23278 switch (parent->tag)
23279 {
63d06c5c 23280 case DW_TAG_namespace:
0114d602 23281 parent_type = read_type_die (parent, cu);
acebe513
UW
23282 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23283 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23284 Work around this problem here. */
23285 if (cu->language == language_cplus
7d93a1e0 23286 && strcmp (parent_type->name (), "::") == 0)
acebe513 23287 return "";
0114d602 23288 /* We give a name to even anonymous namespaces. */
7d93a1e0 23289 return parent_type->name ();
63d06c5c 23290 case DW_TAG_class_type:
680b30c7 23291 case DW_TAG_interface_type:
63d06c5c 23292 case DW_TAG_structure_type:
0114d602 23293 case DW_TAG_union_type:
f55ee35c 23294 case DW_TAG_module:
0114d602 23295 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
23296 if (parent_type->name () != NULL)
23297 return parent_type->name ();
0114d602
DJ
23298 else
23299 /* An anonymous structure is only allowed non-static data
23300 members; no typedefs, no member functions, et cetera.
23301 So it does not need a prefix. */
23302 return "";
abc72ce4 23303 case DW_TAG_compile_unit:
95554aad 23304 case DW_TAG_partial_unit:
abc72ce4
DE
23305 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23306 if (cu->language == language_cplus
976ca316 23307 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
23308 && die->child != NULL
23309 && (die->tag == DW_TAG_class_type
23310 || die->tag == DW_TAG_structure_type
23311 || die->tag == DW_TAG_union_type))
23312 {
43816ebc 23313 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
23314 if (name != NULL)
23315 return name;
23316 }
23317 return "";
0a4b0913
AB
23318 case DW_TAG_subprogram:
23319 /* Nested subroutines in Fortran get a prefix with the name
23320 of the parent's subroutine. */
23321 if (cu->language == language_fortran)
23322 {
23323 if ((die->tag == DW_TAG_subprogram)
23324 && (dwarf2_name (parent, cu) != NULL))
23325 return dwarf2_name (parent, cu);
23326 }
23327 return determine_prefix (parent, cu);
3d567982
TT
23328 case DW_TAG_enumeration_type:
23329 parent_type = read_type_die (parent, cu);
23330 if (TYPE_DECLARED_CLASS (parent_type))
23331 {
7d93a1e0
SM
23332 if (parent_type->name () != NULL)
23333 return parent_type->name ();
3d567982
TT
23334 return "";
23335 }
23336 /* Fall through. */
63d06c5c 23337 default:
8176b9b8 23338 return determine_prefix (parent, cu);
63d06c5c 23339 }
63d06c5c
DC
23340}
23341
3e43a32a
MS
23342/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23343 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23344 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23345 an obconcat, otherwise allocate storage for the result. The CU argument is
23346 used to determine the language and hence, the appropriate separator. */
987504bb 23347
f55ee35c 23348#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
23349
23350static char *
f55ee35c 23351typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
dda83cd7 23352 int physname, struct dwarf2_cu *cu)
63d06c5c 23353{
f55ee35c 23354 const char *lead = "";
5c315b68 23355 const char *sep;
63d06c5c 23356
3e43a32a
MS
23357 if (suffix == NULL || suffix[0] == '\0'
23358 || prefix == NULL || prefix[0] == '\0')
987504bb 23359 sep = "";
45280282
IB
23360 else if (cu->language == language_d)
23361 {
23362 /* For D, the 'main' function could be defined in any module, but it
23363 should never be prefixed. */
23364 if (strcmp (suffix, "D main") == 0)
23365 {
23366 prefix = "";
23367 sep = "";
23368 }
23369 else
23370 sep = ".";
23371 }
f55ee35c
JK
23372 else if (cu->language == language_fortran && physname)
23373 {
23374 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23375 DW_AT_MIPS_linkage_name is preferred and used instead. */
23376
23377 lead = "__";
23378 sep = "_MOD_";
23379 }
987504bb
JJ
23380 else
23381 sep = "::";
63d06c5c 23382
6dd47d34
DE
23383 if (prefix == NULL)
23384 prefix = "";
23385 if (suffix == NULL)
23386 suffix = "";
23387
987504bb
JJ
23388 if (obs == NULL)
23389 {
3e43a32a 23390 char *retval
224c3ddb
SM
23391 = ((char *)
23392 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 23393
f55ee35c
JK
23394 strcpy (retval, lead);
23395 strcat (retval, prefix);
6dd47d34
DE
23396 strcat (retval, sep);
23397 strcat (retval, suffix);
63d06c5c
DC
23398 return retval;
23399 }
987504bb
JJ
23400 else
23401 {
23402 /* We have an obstack. */
f55ee35c 23403 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 23404 }
63d06c5c
DC
23405}
23406
71c25dea
TT
23407/* Get name of a die, return NULL if not found. */
23408
15d034d0
TT
23409static const char *
23410dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 23411 struct objfile *objfile)
71c25dea
TT
23412{
23413 if (name && cu->language == language_cplus)
23414 {
596dc4ad
TT
23415 gdb::unique_xmalloc_ptr<char> canon_name
23416 = cp_canonicalize_string (name);
71c25dea 23417
596dc4ad
TT
23418 if (canon_name != nullptr)
23419 name = objfile->intern (canon_name.get ());
71c25dea
TT
23420 }
23421
23422 return name;
c906108c
SS
23423}
23424
96553a0c
DE
23425/* Get name of a die, return NULL if not found.
23426 Anonymous namespaces are converted to their magic string. */
9219021c 23427
15d034d0 23428static const char *
e142c38c 23429dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
23430{
23431 struct attribute *attr;
5e22e966 23432 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 23433
e142c38c 23434 attr = dwarf2_attr (die, DW_AT_name, cu);
2c830f54
TT
23435 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23436 if (attr_name == nullptr
96553a0c 23437 && die->tag != DW_TAG_namespace
53832f31
TT
23438 && die->tag != DW_TAG_class_type
23439 && die->tag != DW_TAG_interface_type
23440 && die->tag != DW_TAG_structure_type
23441 && die->tag != DW_TAG_union_type)
71c25dea
TT
23442 return NULL;
23443
23444 switch (die->tag)
23445 {
23446 case DW_TAG_compile_unit:
95554aad 23447 case DW_TAG_partial_unit:
71c25dea
TT
23448 /* Compilation units have a DW_AT_name that is a filename, not
23449 a source language identifier. */
23450 case DW_TAG_enumeration_type:
23451 case DW_TAG_enumerator:
23452 /* These tags always have simple identifiers already; no need
23453 to canonicalize them. */
2c830f54 23454 return attr_name;
907af001 23455
96553a0c 23456 case DW_TAG_namespace:
2c830f54
TT
23457 if (attr_name != nullptr)
23458 return attr_name;
96553a0c
DE
23459 return CP_ANONYMOUS_NAMESPACE_STR;
23460
907af001
UW
23461 case DW_TAG_class_type:
23462 case DW_TAG_interface_type:
23463 case DW_TAG_structure_type:
23464 case DW_TAG_union_type:
23465 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23466 structures or unions. These were of the form "._%d" in GCC 4.1,
23467 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23468 and GCC 4.4. We work around this problem by ignoring these. */
2c830f54
TT
23469 if (attr_name != nullptr
23470 && (startswith (attr_name, "._")
23471 || startswith (attr_name, "<anonymous")))
907af001 23472 return NULL;
53832f31
TT
23473
23474 /* GCC might emit a nameless typedef that has a linkage name. See
23475 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
2c830f54 23476 if (!attr || attr_name == NULL)
53832f31 23477 {
73b9be8b 23478 attr = dw2_linkage_name_attr (die, cu);
95eb9e54 23479 attr_name = attr == nullptr ? nullptr : attr->as_string ();
2c830f54 23480 if (attr == NULL || attr_name == NULL)
53832f31
TT
23481 return NULL;
23482
2c830f54 23483 /* Avoid demangling attr_name the second time on a second
df5c6c50 23484 call for the same DIE. */
3b64bf15 23485 if (!attr->canonical_string_p ())
53832f31 23486 {
43816ebc 23487 gdb::unique_xmalloc_ptr<char> demangled
2c830f54 23488 (gdb_demangle (attr_name, DMGL_TYPES));
4f180d53
AT
23489 if (demangled == nullptr)
23490 return nullptr;
43816ebc 23491
c6481205 23492 attr->set_string_canonical (objfile->intern (demangled.get ()));
95eb9e54 23493 attr_name = attr->as_string ();
53832f31 23494 }
67430cd0 23495
2c830f54
TT
23496 /* Strip any leading namespaces/classes, keep only the
23497 base name. DW_AT_name for named DIEs does not
23498 contain the prefixes. */
23499 const char *base = strrchr (attr_name, ':');
23500 if (base && base > attr_name && base[-1] == ':')
67430cd0
TT
23501 return &base[1];
23502 else
2c830f54 23503 return attr_name;
53832f31 23504 }
907af001
UW
23505 break;
23506
71c25dea 23507 default:
907af001
UW
23508 break;
23509 }
23510
3b64bf15 23511 if (!attr->canonical_string_p ())
c6481205
TT
23512 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23513 objfile));
2c830f54 23514 return attr->as_string ();
9219021c
DC
23515}
23516
23517/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23518 is none. *EXT_CU is the CU containing DIE on input, and the CU
23519 containing the return value on output. */
9219021c
DC
23520
23521static struct die_info *
f2f0e013 23522dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23523{
23524 struct attribute *attr;
9219021c 23525
f2f0e013 23526 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23527 if (attr == NULL)
23528 return NULL;
23529
f2f0e013 23530 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23531}
23532
f9aca02d 23533static void
d97bc12b 23534dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23535{
23536 unsigned int i;
23537
d97bc12b 23538 print_spaces (indent, f);
9d8780f0 23539 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23540 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23541 sect_offset_str (die->sect_off));
d97bc12b
DE
23542
23543 if (die->parent != NULL)
23544 {
23545 print_spaces (indent, f);
9d8780f0
SM
23546 fprintf_unfiltered (f, " parent at offset: %s\n",
23547 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23548 }
23549
23550 print_spaces (indent, f);
23551 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23552 dwarf_bool_name (die->child != NULL));
c906108c 23553
d97bc12b
DE
23554 print_spaces (indent, f);
23555 fprintf_unfiltered (f, " attributes:\n");
23556
c906108c
SS
23557 for (i = 0; i < die->num_attrs; ++i)
23558 {
d97bc12b
DE
23559 print_spaces (indent, f);
23560 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23561 dwarf_attr_name (die->attrs[i].name),
23562 dwarf_form_name (die->attrs[i].form));
d97bc12b 23563
c906108c
SS
23564 switch (die->attrs[i].form)
23565 {
c906108c 23566 case DW_FORM_addr:
336d760d 23567 case DW_FORM_addrx:
3019eac3 23568 case DW_FORM_GNU_addr_index:
d97bc12b 23569 fprintf_unfiltered (f, "address: ");
36d378cf 23570 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
c906108c
SS
23571 break;
23572 case DW_FORM_block2:
23573 case DW_FORM_block4:
23574 case DW_FORM_block:
23575 case DW_FORM_block1:
56eb65bd 23576 fprintf_unfiltered (f, "block: size %s",
9d2246fc 23577 pulongest (die->attrs[i].as_block ()->size));
c906108c 23578 break;
2dc7f7b3 23579 case DW_FORM_exprloc:
56eb65bd 23580 fprintf_unfiltered (f, "expression: size %s",
9d2246fc 23581 pulongest (die->attrs[i].as_block ()->size));
2dc7f7b3 23582 break;
0224619f
JK
23583 case DW_FORM_data16:
23584 fprintf_unfiltered (f, "constant of 16 bytes");
23585 break;
4568ecf9
DE
23586 case DW_FORM_ref_addr:
23587 fprintf_unfiltered (f, "ref address: ");
529908cb 23588 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
4568ecf9 23589 break;
36586728
TT
23590 case DW_FORM_GNU_ref_alt:
23591 fprintf_unfiltered (f, "alt ref address: ");
529908cb 23592 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
36586728 23593 break;
10b3939b
DJ
23594 case DW_FORM_ref1:
23595 case DW_FORM_ref2:
23596 case DW_FORM_ref4:
4568ecf9
DE
23597 case DW_FORM_ref8:
23598 case DW_FORM_ref_udata:
d97bc12b 23599 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
529908cb 23600 (long) (die->attrs[i].as_unsigned ()));
10b3939b 23601 break;
c906108c
SS
23602 case DW_FORM_data1:
23603 case DW_FORM_data2:
23604 case DW_FORM_data4:
ce5d95e1 23605 case DW_FORM_data8:
c906108c 23606 case DW_FORM_udata:
43bbcdc2 23607 fprintf_unfiltered (f, "constant: %s",
529908cb 23608 pulongest (die->attrs[i].as_unsigned ()));
c906108c 23609 break;
2dc7f7b3
TT
23610 case DW_FORM_sec_offset:
23611 fprintf_unfiltered (f, "section offset: %s",
529908cb 23612 pulongest (die->attrs[i].as_unsigned ()));
2dc7f7b3 23613 break;
55f1336d 23614 case DW_FORM_ref_sig8:
ac9ec31b 23615 fprintf_unfiltered (f, "signature: %s",
630ed6b9 23616 hex_string (die->attrs[i].as_signature ()));
348e048f 23617 break;
c906108c 23618 case DW_FORM_string:
4bdf3d34 23619 case DW_FORM_strp:
43988095 23620 case DW_FORM_line_strp:
cf532bd1 23621 case DW_FORM_strx:
3019eac3 23622 case DW_FORM_GNU_str_index:
36586728 23623 case DW_FORM_GNU_strp_alt:
8285870a 23624 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c6481205
TT
23625 die->attrs[i].as_string ()
23626 ? die->attrs[i].as_string () : "",
23627 die->attrs[i].canonical_string_p () ? "is" : "not");
c906108c
SS
23628 break;
23629 case DW_FORM_flag:
c45bc3f8 23630 if (die->attrs[i].as_boolean ())
d97bc12b 23631 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23632 else
d97bc12b 23633 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23634 break;
2dc7f7b3
TT
23635 case DW_FORM_flag_present:
23636 fprintf_unfiltered (f, "flag: TRUE");
23637 break;
a8329558 23638 case DW_FORM_indirect:
0963b4bd
MS
23639 /* The reader will have reduced the indirect form to
23640 the "base form" so this form should not occur. */
5f48f8f3 23641 fprintf_unfiltered (f,
3e43a32a 23642 "unexpected attribute form: DW_FORM_indirect");
a8329558 23643 break;
1bc397c5 23644 case DW_FORM_sdata:
663c44ac
JK
23645 case DW_FORM_implicit_const:
23646 fprintf_unfiltered (f, "constant: %s",
1bc397c5 23647 plongest (die->attrs[i].as_signed ()));
663c44ac 23648 break;
c906108c 23649 default:
d97bc12b 23650 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23651 die->attrs[i].form);
d97bc12b 23652 break;
c906108c 23653 }
d97bc12b 23654 fprintf_unfiltered (f, "\n");
c906108c
SS
23655 }
23656}
23657
f9aca02d 23658static void
d97bc12b 23659dump_die_for_error (struct die_info *die)
c906108c 23660{
d97bc12b
DE
23661 dump_die_shallow (gdb_stderr, 0, die);
23662}
23663
23664static void
23665dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23666{
23667 int indent = level * 4;
23668
23669 gdb_assert (die != NULL);
23670
23671 if (level >= max_level)
23672 return;
23673
23674 dump_die_shallow (f, indent, die);
23675
23676 if (die->child != NULL)
c906108c 23677 {
d97bc12b
DE
23678 print_spaces (indent, f);
23679 fprintf_unfiltered (f, " Children:");
23680 if (level + 1 < max_level)
23681 {
23682 fprintf_unfiltered (f, "\n");
23683 dump_die_1 (f, level + 1, max_level, die->child);
23684 }
23685 else
23686 {
3e43a32a
MS
23687 fprintf_unfiltered (f,
23688 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23689 }
23690 }
23691
23692 if (die->sibling != NULL && level > 0)
23693 {
23694 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23695 }
23696}
23697
d97bc12b
DE
23698/* This is called from the pdie macro in gdbinit.in.
23699 It's not static so gcc will keep a copy callable from gdb. */
23700
23701void
23702dump_die (struct die_info *die, int max_level)
23703{
23704 dump_die_1 (gdb_stdlog, 0, max_level, die);
23705}
23706
f9aca02d 23707static void
51545339 23708store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23709{
51545339 23710 void **slot;
c906108c 23711
9c541725
PA
23712 slot = htab_find_slot_with_hash (cu->die_hash, die,
23713 to_underlying (die->sect_off),
b64f50a1 23714 INSERT);
51545339
DJ
23715
23716 *slot = die;
c906108c
SS
23717}
23718
348e048f
DE
23719/* Follow reference or signature attribute ATTR of SRC_DIE.
23720 On entry *REF_CU is the CU of SRC_DIE.
23721 On exit *REF_CU is the CU of the result. */
23722
23723static struct die_info *
ff39bb5e 23724follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23725 struct dwarf2_cu **ref_cu)
23726{
23727 struct die_info *die;
23728
cd6c91b4 23729 if (attr->form_is_ref ())
348e048f 23730 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23731 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23732 die = follow_die_sig (src_die, attr, ref_cu);
23733 else
23734 {
23735 dump_die_for_error (src_die);
23736 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 23737 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
23738 }
23739
23740 return die;
03dd20cc
DJ
23741}
23742
5c631832 23743/* Follow reference OFFSET.
673bfd45
DE
23744 On entry *REF_CU is the CU of the source die referencing OFFSET.
23745 On exit *REF_CU is the CU of the result.
23746 Returns NULL if OFFSET is invalid. */
f504f079 23747
f9aca02d 23748static struct die_info *
9c541725 23749follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23750 struct dwarf2_cu **ref_cu)
c906108c 23751{
10b3939b 23752 struct die_info temp_die;
f2f0e013 23753 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 23754 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 23755
348e048f
DE
23756 gdb_assert (cu->per_cu != NULL);
23757
98bfdba5
PA
23758 target_cu = cu;
23759
17e593e9
SM
23760 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23761 "source CU contains target offset: %d",
23762 sect_offset_str (cu->per_cu->sect_off),
23763 sect_offset_str (sect_off),
23764 cu->header.offset_in_cu_p (sect_off));
23765
3019eac3 23766 if (cu->per_cu->is_debug_types)
348e048f
DE
23767 {
23768 /* .debug_types CUs cannot reference anything outside their CU.
23769 If they need to, they have to reference a signatured type via
55f1336d 23770 DW_FORM_ref_sig8. */
4057dfde 23771 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 23772 return NULL;
348e048f 23773 }
36586728 23774 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 23775 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
23776 {
23777 struct dwarf2_per_cu_data *per_cu;
9a619af0 23778
9c541725 23779 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 23780 per_objfile);
03dd20cc 23781
17e593e9
SM
23782 dwarf_read_debug_printf_v ("target CU offset: %s, "
23783 "target CU DIEs loaded: %d",
23784 sect_offset_str (per_cu->sect_off),
23785 per_objfile->get_cu (per_cu) != nullptr);
23786
616c069a
SM
23787 /* If necessary, add it to the queue and load its DIEs.
23788
23789 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23790 it doesn't mean they are currently loaded. Since we require them
23791 to be loaded, we must check for ourselves. */
23792 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23793 || per_objfile->get_cu (per_cu) == nullptr)
4a636814
SM
23794 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23795 false, cu->language);
03dd20cc 23796
976ca316 23797 target_cu = per_objfile->get_cu (per_cu);
616c069a 23798 gdb_assert (target_cu != nullptr);
10b3939b 23799 }
98bfdba5
PA
23800 else if (cu->dies == NULL)
23801 {
23802 /* We're loading full DIEs during partial symbol reading. */
976ca316 23803 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
4a636814
SM
23804 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23805 language_minimal);
98bfdba5 23806 }
c906108c 23807
f2f0e013 23808 *ref_cu = target_cu;
9c541725 23809 temp_die.sect_off = sect_off;
c24bdb02
KS
23810
23811 if (target_cu != cu)
23812 target_cu->ancestor = cu;
23813
9a3c8263 23814 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23815 &temp_die,
23816 to_underlying (sect_off));
5c631832 23817}
10b3939b 23818
5c631832
JK
23819/* Follow reference attribute ATTR of SRC_DIE.
23820 On entry *REF_CU is the CU of SRC_DIE.
23821 On exit *REF_CU is the CU of the result. */
23822
23823static struct die_info *
ff39bb5e 23824follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23825 struct dwarf2_cu **ref_cu)
23826{
0826b30a 23827 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
23828 struct dwarf2_cu *cu = *ref_cu;
23829 struct die_info *die;
23830
9c541725 23831 die = follow_die_offset (sect_off,
36586728
TT
23832 (attr->form == DW_FORM_GNU_ref_alt
23833 || cu->per_cu->is_dwz),
23834 ref_cu);
5c631832 23835 if (!die)
9d8780f0
SM
23836 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23837 "at %s [in module %s]"),
23838 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 23839 objfile_name (cu->per_objfile->objfile));
348e048f 23840
5c631832
JK
23841 return die;
23842}
23843
d4c9a4f8 23844/* See read.h. */
5c631832
JK
23845
23846struct dwarf2_locexpr_baton
9c541725 23847dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 23848 dwarf2_per_cu_data *per_cu,
976ca316 23849 dwarf2_per_objfile *per_objfile,
041d9819
SM
23850 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23851 bool resolve_abstract_p)
5c631832 23852{
5c631832
JK
23853 struct die_info *die;
23854 struct attribute *attr;
23855 struct dwarf2_locexpr_baton retval;
976ca316 23856 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 23857
976ca316 23858 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 23859 if (cu == nullptr)
976ca316 23860 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
23861
23862 if (cu == nullptr)
cc12ce38
DE
23863 {
23864 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23865 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23866 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23867 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23868 }
918dd910 23869
9c541725 23870 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23871 if (!die)
9d8780f0
SM
23872 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23873 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23874
23875 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23876 if (!attr && resolve_abstract_p
976ca316
SM
23877 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23878 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65 23879 {
041d9819 23880 CORE_ADDR pc = get_frame_pc ();
b3b3bada 23881 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 23882 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 23883
3360b6e7 23884 for (const auto &cand_off
976ca316 23885 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 23886 {
3360b6e7
TV
23887 struct dwarf2_cu *cand_cu = cu;
23888 struct die_info *cand
23889 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23890 if (!cand
23891 || !cand->parent
e4a62c65
TV
23892 || cand->parent->tag != DW_TAG_subprogram)
23893 continue;
23894
23895 CORE_ADDR pc_low, pc_high;
23896 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23897 if (pc_low == ((CORE_ADDR) -1))
23898 continue;
23899 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23900 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23901 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23902 continue;
23903
23904 die = cand;
23905 attr = dwarf2_attr (die, DW_AT_location, cu);
23906 break;
23907 }
23908 }
23909
5c631832
JK
23910 if (!attr)
23911 {
e103e986
JK
23912 /* DWARF: "If there is no such attribute, then there is no effect.".
23913 DATA is ignored if SIZE is 0. */
5c631832 23914
e103e986 23915 retval.data = NULL;
5c631832
JK
23916 retval.size = 0;
23917 }
cd6c91b4 23918 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23919 {
23920 struct dwarf2_loclist_baton loclist_baton;
041d9819 23921 CORE_ADDR pc = get_frame_pc ();
8cf6f0b1
TT
23922 size_t size;
23923
23924 fill_in_loclist_baton (cu, &loclist_baton, attr);
23925
23926 retval.data = dwarf2_find_location_expression (&loclist_baton,
23927 &size, pc);
23928 retval.size = size;
23929 }
5c631832
JK
23930 else
23931 {
4fc6c0d5 23932 if (!attr->form_is_block ())
9d8780f0 23933 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23934 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23935 sect_offset_str (sect_off), objfile_name (objfile));
5c631832 23936
9d2246fc
TT
23937 struct dwarf_block *block = attr->as_block ();
23938 retval.data = block->data;
23939 retval.size = block->size;
5c631832 23940 }
976ca316 23941 retval.per_objfile = per_objfile;
5c631832 23942 retval.per_cu = cu->per_cu;
918dd910 23943
976ca316 23944 per_objfile->age_comp_units ();
918dd910 23945
5c631832 23946 return retval;
348e048f
DE
23947}
23948
d4c9a4f8 23949/* See read.h. */
8b9737bf
TT
23950
23951struct dwarf2_locexpr_baton
23952dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 23953 dwarf2_per_cu_data *per_cu,
14095eb3 23954 dwarf2_per_objfile *per_objfile,
041d9819 23955 gdb::function_view<CORE_ADDR ()> get_frame_pc)
8b9737bf 23956{
9c541725 23957 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23958
14095eb3 23959 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
041d9819 23960 get_frame_pc);
8b9737bf
TT
23961}
23962
b6807d98
TT
23963/* Write a constant of a given type as target-ordered bytes into
23964 OBSTACK. */
23965
23966static const gdb_byte *
23967write_constant_as_bytes (struct obstack *obstack,
23968 enum bfd_endian byte_order,
23969 struct type *type,
23970 ULONGEST value,
23971 LONGEST *len)
23972{
23973 gdb_byte *result;
23974
23975 *len = TYPE_LENGTH (type);
224c3ddb 23976 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23977 store_unsigned_integer (result, *len, byte_order, value);
23978
23979 return result;
23980}
23981
d4c9a4f8 23982/* See read.h. */
b6807d98
TT
23983
23984const gdb_byte *
9c541725 23985dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 23986 dwarf2_per_cu_data *per_cu,
14095eb3 23987 dwarf2_per_objfile *per_objfile,
d4c9a4f8 23988 obstack *obstack,
b6807d98
TT
23989 LONGEST *len)
23990{
b6807d98
TT
23991 struct die_info *die;
23992 struct attribute *attr;
23993 const gdb_byte *result = NULL;
23994 struct type *type;
23995 LONGEST value;
23996 enum bfd_endian byte_order;
14095eb3 23997 struct objfile *objfile = per_objfile->objfile;
b6807d98 23998
7188ed02 23999 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
24000 if (cu == nullptr)
24001 cu = load_cu (per_cu, per_objfile, false);
24002
24003 if (cu == nullptr)
cc12ce38
DE
24004 {
24005 /* We shouldn't get here for a dummy CU, but don't crash on the user.
24006 Instead just throw an error, not much else we can do. */
9d8780f0
SM
24007 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
24008 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 24009 }
b6807d98 24010
9c541725 24011 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 24012 if (!die)
9d8780f0
SM
24013 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
24014 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
24015
24016 attr = dwarf2_attr (die, DW_AT_const_value, cu);
24017 if (attr == NULL)
24018 return NULL;
24019
e3b94546 24020 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
24021 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
24022
24023 switch (attr->form)
24024 {
24025 case DW_FORM_addr:
336d760d 24026 case DW_FORM_addrx:
b6807d98
TT
24027 case DW_FORM_GNU_addr_index:
24028 {
24029 gdb_byte *tem;
24030
24031 *len = cu->header.addr_size;
224c3ddb 24032 tem = (gdb_byte *) obstack_alloc (obstack, *len);
36d378cf 24033 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
b6807d98
TT
24034 result = tem;
24035 }
24036 break;
24037 case DW_FORM_string:
24038 case DW_FORM_strp:
cf532bd1 24039 case DW_FORM_strx:
b6807d98
TT
24040 case DW_FORM_GNU_str_index:
24041 case DW_FORM_GNU_strp_alt:
c6481205 24042 /* The string is already allocated on the objfile obstack, point
b6807d98 24043 directly to it. */
2c830f54
TT
24044 {
24045 const char *attr_name = attr->as_string ();
24046 result = (const gdb_byte *) attr_name;
24047 *len = strlen (attr_name);
24048 }
b6807d98
TT
24049 break;
24050 case DW_FORM_block1:
24051 case DW_FORM_block2:
24052 case DW_FORM_block4:
24053 case DW_FORM_block:
24054 case DW_FORM_exprloc:
0224619f 24055 case DW_FORM_data16:
9d2246fc
TT
24056 {
24057 struct dwarf_block *block = attr->as_block ();
24058 result = block->data;
24059 *len = block->size;
24060 }
b6807d98
TT
24061 break;
24062
24063 /* The DW_AT_const_value attributes are supposed to carry the
24064 symbol's value "represented as it would be on the target
24065 architecture." By the time we get here, it's already been
24066 converted to host endianness, so we just need to sign- or
24067 zero-extend it as appropriate. */
24068 case DW_FORM_data1:
24069 type = die_type (die, cu);
24070 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
24071 if (result == NULL)
24072 result = write_constant_as_bytes (obstack, byte_order,
24073 type, value, len);
24074 break;
24075 case DW_FORM_data2:
24076 type = die_type (die, cu);
24077 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
24078 if (result == NULL)
24079 result = write_constant_as_bytes (obstack, byte_order,
24080 type, value, len);
24081 break;
24082 case DW_FORM_data4:
24083 type = die_type (die, cu);
24084 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
24085 if (result == NULL)
24086 result = write_constant_as_bytes (obstack, byte_order,
24087 type, value, len);
24088 break;
24089 case DW_FORM_data8:
24090 type = die_type (die, cu);
24091 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
24092 if (result == NULL)
24093 result = write_constant_as_bytes (obstack, byte_order,
24094 type, value, len);
24095 break;
24096
24097 case DW_FORM_sdata:
663c44ac 24098 case DW_FORM_implicit_const:
b6807d98
TT
24099 type = die_type (die, cu);
24100 result = write_constant_as_bytes (obstack, byte_order,
1bc397c5 24101 type, attr->as_signed (), len);
b6807d98
TT
24102 break;
24103
24104 case DW_FORM_udata:
24105 type = die_type (die, cu);
24106 result = write_constant_as_bytes (obstack, byte_order,
529908cb 24107 type, attr->as_unsigned (), len);
b6807d98
TT
24108 break;
24109
24110 default:
b98664d3 24111 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
24112 dwarf_form_name (attr->form));
24113 break;
24114 }
24115
24116 return result;
24117}
24118
d4c9a4f8 24119/* See read.h. */
7942e96e
AA
24120
24121struct type *
9c541725 24122dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
24123 dwarf2_per_cu_data *per_cu,
24124 dwarf2_per_objfile *per_objfile)
7942e96e 24125{
7942e96e
AA
24126 struct die_info *die;
24127
7188ed02 24128 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
24129 if (cu == nullptr)
24130 cu = load_cu (per_cu, per_objfile, false);
24131
24132 if (cu == nullptr)
24133 return nullptr;
7942e96e 24134
9c541725 24135 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
24136 if (!die)
24137 return NULL;
24138
24139 return die_type (die, cu);
24140}
24141
8cb5117c 24142/* See read.h. */
8a9b8146
TT
24143
24144struct type *
b64f50a1 24145dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
24146 dwarf2_per_cu_data *per_cu,
24147 dwarf2_per_objfile *per_objfile)
8a9b8146 24148{
9c541725 24149 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 24150 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
24151}
24152
ac9ec31b 24153/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 24154 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
24155 On exit *REF_CU is the CU of the result.
24156 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
24157
24158static struct die_info *
ac9ec31b
DE
24159follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
24160 struct dwarf2_cu **ref_cu)
348e048f 24161{
348e048f 24162 struct die_info temp_die;
c24bdb02 24163 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 24164 struct die_info *die;
976ca316 24165 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 24166
348e048f 24167
ac9ec31b
DE
24168 /* While it might be nice to assert sig_type->type == NULL here,
24169 we can get here for DW_AT_imported_declaration where we need
24170 the DIE not the type. */
348e048f 24171
616c069a 24172 /* If necessary, add it to the queue and load its DIEs.
348e048f 24173
616c069a
SM
24174 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
24175 it doesn't mean they are currently loaded. Since we require them
24176 to be loaded, we must check for ourselves. */
976ca316 24177 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
616c069a
SM
24178 language_minimal)
24179 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
976ca316 24180 read_signatured_type (sig_type, per_objfile);
348e048f 24181
976ca316 24182 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
69d751e3 24183 gdb_assert (sig_cu != NULL);
9c541725
PA
24184 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
24185 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 24186 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 24187 to_underlying (temp_die.sect_off));
348e048f
DE
24188 if (die)
24189 {
796a7ff8
DE
24190 /* For .gdb_index version 7 keep track of included TUs.
24191 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
24192 if (per_objfile->per_bfd->index_table != NULL
24193 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 24194 {
ae640021 24195 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
24196 }
24197
348e048f 24198 *ref_cu = sig_cu;
c24bdb02
KS
24199 if (sig_cu != cu)
24200 sig_cu->ancestor = cu;
24201
348e048f
DE
24202 return die;
24203 }
24204
ac9ec31b
DE
24205 return NULL;
24206}
24207
24208/* Follow signatured type referenced by ATTR in SRC_DIE.
24209 On entry *REF_CU is the CU of SRC_DIE.
24210 On exit *REF_CU is the CU of the result.
24211 The result is the DIE of the type.
24212 If the referenced type cannot be found an error is thrown. */
24213
24214static struct die_info *
ff39bb5e 24215follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
24216 struct dwarf2_cu **ref_cu)
24217{
630ed6b9 24218 ULONGEST signature = attr->as_signature ();
ac9ec31b
DE
24219 struct signatured_type *sig_type;
24220 struct die_info *die;
24221
24222 gdb_assert (attr->form == DW_FORM_ref_sig8);
24223
a2ce51a0 24224 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
24225 /* sig_type will be NULL if the signatured type is missing from
24226 the debug info. */
24227 if (sig_type == NULL)
24228 {
24229 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
dda83cd7
SM
24230 " from DIE at %s [in module %s]"),
24231 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 24232 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
24233 }
24234
24235 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24236 if (die == NULL)
24237 {
24238 dump_die_for_error (src_die);
24239 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24240 " from DIE at %s [in module %s]"),
24241 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 24242 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
24243 }
24244
24245 return die;
24246}
24247
24248/* Get the type specified by SIGNATURE referenced in DIE/CU,
24249 reading in and processing the type unit if necessary. */
24250
24251static struct type *
24252get_signatured_type (struct die_info *die, ULONGEST signature,
24253 struct dwarf2_cu *cu)
24254{
976ca316 24255 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
24256 struct signatured_type *sig_type;
24257 struct dwarf2_cu *type_cu;
24258 struct die_info *type_die;
24259 struct type *type;
24260
a2ce51a0 24261 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
24262 /* sig_type will be NULL if the signatured type is missing from
24263 the debug info. */
24264 if (sig_type == NULL)
24265 {
b98664d3 24266 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
24267 " from DIE at %s [in module %s]"),
24268 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24269 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24270 return build_error_marker_type (cu, die);
24271 }
24272
24273 /* If we already know the type we're done. */
976ca316 24274 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
24275 if (type != nullptr)
24276 return type;
ac9ec31b
DE
24277
24278 type_cu = cu;
24279 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24280 if (type_die != NULL)
24281 {
24282 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24283 is created. This is important, for example, because for c++ classes
24284 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24285 type = read_type_die (type_die, type_cu);
24286 if (type == NULL)
24287 {
b98664d3 24288 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
24289 " referenced from DIE at %s [in module %s]"),
24290 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24291 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24292 type = build_error_marker_type (cu, die);
24293 }
24294 }
24295 else
24296 {
b98664d3 24297 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24298 " from DIE at %s [in module %s]"),
24299 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24300 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24301 type = build_error_marker_type (cu, die);
24302 }
e286671b 24303
976ca316 24304 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
24305
24306 return type;
24307}
24308
24309/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24310 reading in and processing the type unit if necessary. */
24311
24312static struct type *
ff39bb5e 24313get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 24314 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
24315{
24316 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 24317 if (attr->form_is_ref ())
ac9ec31b
DE
24318 {
24319 struct dwarf2_cu *type_cu = cu;
24320 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24321
24322 return read_type_die (type_die, type_cu);
24323 }
24324 else if (attr->form == DW_FORM_ref_sig8)
24325 {
630ed6b9 24326 return get_signatured_type (die, attr->as_signature (), cu);
ac9ec31b
DE
24327 }
24328 else
24329 {
976ca316 24330 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24331
b98664d3 24332 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
24333 " at %s [in module %s]"),
24334 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 24335 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24336 return build_error_marker_type (cu, die);
24337 }
348e048f
DE
24338}
24339
e5fe5e75 24340/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
24341
24342static void
ab432490
SM
24343load_full_type_unit (dwarf2_per_cu_data *per_cu,
24344 dwarf2_per_objfile *per_objfile)
348e048f 24345{
52dc124a 24346 struct signatured_type *sig_type;
348e048f 24347
f4dc4d17 24348 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 24349 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 24350
6721b2ec
DE
24351 /* We have the per_cu, but we need the signatured_type.
24352 Fortunately this is an easy translation. */
24353 gdb_assert (per_cu->is_debug_types);
24354 sig_type = (struct signatured_type *) per_cu;
348e048f 24355
7188ed02 24356 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24357
ab432490 24358 read_signatured_type (sig_type, per_objfile);
348e048f 24359
7188ed02 24360 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
24361}
24362
3019eac3
DE
24363/* Read in a signatured type and build its CU and DIEs.
24364 If the type is a stub for the real type in a DWO file,
24365 read in the real type from the DWO file as well. */
dee91e82
DE
24366
24367static void
ab432490
SM
24368read_signatured_type (signatured_type *sig_type,
24369 dwarf2_per_objfile *per_objfile)
dee91e82
DE
24370{
24371 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24372
3019eac3 24373 gdb_assert (per_cu->is_debug_types);
7188ed02 24374 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24375
2e671100 24376 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
24377
24378 if (!reader.dummy_p)
24379 {
24380 struct dwarf2_cu *cu = reader.cu;
24381 const gdb_byte *info_ptr = reader.info_ptr;
24382
24383 gdb_assert (cu->die_hash == NULL);
24384 cu->die_hash =
24385 htab_create_alloc_ex (cu->header.length / 12,
24386 die_hash,
24387 die_eq,
24388 NULL,
24389 &cu->comp_unit_obstack,
24390 hashtab_obstack_allocate,
24391 dummy_obstack_deallocate);
24392
3e225074 24393 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
24394 reader.comp_unit_die->child
24395 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24396 reader.comp_unit_die);
24397 cu->dies = reader.comp_unit_die;
24398 /* comp_unit_die is not stored in die_hash, no need. */
24399
24400 /* We try not to read any attributes in this function, because
24401 not all CUs needed for references have been loaded yet, and
24402 symbol table processing isn't initialized. But we have to
24403 set the CU language, or we won't be able to build types
24404 correctly. Similarly, if we do not read the producer, we can
24405 not apply producer-specific interpretation. */
24406 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
24407
24408 reader.keep ();
c0ab21c2
TT
24409 }
24410
7ee85ab1 24411 sig_type->per_cu.tu_read = 1;
c906108c
SS
24412}
24413
c906108c
SS
24414/* Decode simple location descriptions.
24415 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
24416 the location and return the value. If COMPUTED is non-null, it is
24417 set to true to indicate that decoding was successful, and false
24418 otherwise. If COMPUTED is null, then this function may emit a
24419 complaint. */
c906108c
SS
24420
24421static CORE_ADDR
7d79de9a 24422decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 24423{
5e22e966 24424 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
24425 size_t i;
24426 size_t size = blk->size;
d521ce57 24427 const gdb_byte *data = blk->data;
21ae7a4d
JK
24428 CORE_ADDR stack[64];
24429 int stacki;
24430 unsigned int bytes_read, unsnd;
24431 gdb_byte op;
c906108c 24432
7d79de9a
TT
24433 if (computed != nullptr)
24434 *computed = false;
24435
21ae7a4d
JK
24436 i = 0;
24437 stacki = 0;
24438 stack[stacki] = 0;
24439 stack[++stacki] = 0;
24440
24441 while (i < size)
24442 {
24443 op = data[i++];
24444 switch (op)
24445 {
24446 case DW_OP_lit0:
24447 case DW_OP_lit1:
24448 case DW_OP_lit2:
24449 case DW_OP_lit3:
24450 case DW_OP_lit4:
24451 case DW_OP_lit5:
24452 case DW_OP_lit6:
24453 case DW_OP_lit7:
24454 case DW_OP_lit8:
24455 case DW_OP_lit9:
24456 case DW_OP_lit10:
24457 case DW_OP_lit11:
24458 case DW_OP_lit12:
24459 case DW_OP_lit13:
24460 case DW_OP_lit14:
24461 case DW_OP_lit15:
24462 case DW_OP_lit16:
24463 case DW_OP_lit17:
24464 case DW_OP_lit18:
24465 case DW_OP_lit19:
24466 case DW_OP_lit20:
24467 case DW_OP_lit21:
24468 case DW_OP_lit22:
24469 case DW_OP_lit23:
24470 case DW_OP_lit24:
24471 case DW_OP_lit25:
24472 case DW_OP_lit26:
24473 case DW_OP_lit27:
24474 case DW_OP_lit28:
24475 case DW_OP_lit29:
24476 case DW_OP_lit30:
24477 case DW_OP_lit31:
24478 stack[++stacki] = op - DW_OP_lit0;
24479 break;
f1bea926 24480
21ae7a4d
JK
24481 case DW_OP_reg0:
24482 case DW_OP_reg1:
24483 case DW_OP_reg2:
24484 case DW_OP_reg3:
24485 case DW_OP_reg4:
24486 case DW_OP_reg5:
24487 case DW_OP_reg6:
24488 case DW_OP_reg7:
24489 case DW_OP_reg8:
24490 case DW_OP_reg9:
24491 case DW_OP_reg10:
24492 case DW_OP_reg11:
24493 case DW_OP_reg12:
24494 case DW_OP_reg13:
24495 case DW_OP_reg14:
24496 case DW_OP_reg15:
24497 case DW_OP_reg16:
24498 case DW_OP_reg17:
24499 case DW_OP_reg18:
24500 case DW_OP_reg19:
24501 case DW_OP_reg20:
24502 case DW_OP_reg21:
24503 case DW_OP_reg22:
24504 case DW_OP_reg23:
24505 case DW_OP_reg24:
24506 case DW_OP_reg25:
24507 case DW_OP_reg26:
24508 case DW_OP_reg27:
24509 case DW_OP_reg28:
24510 case DW_OP_reg29:
24511 case DW_OP_reg30:
24512 case DW_OP_reg31:
24513 stack[++stacki] = op - DW_OP_reg0;
24514 if (i < size)
7d79de9a
TT
24515 {
24516 if (computed == nullptr)
24517 dwarf2_complex_location_expr_complaint ();
24518 else
24519 return 0;
24520 }
21ae7a4d 24521 break;
c906108c 24522
21ae7a4d
JK
24523 case DW_OP_regx:
24524 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24525 i += bytes_read;
24526 stack[++stacki] = unsnd;
24527 if (i < size)
7d79de9a
TT
24528 {
24529 if (computed == nullptr)
24530 dwarf2_complex_location_expr_complaint ();
24531 else
24532 return 0;
24533 }
21ae7a4d 24534 break;
c906108c 24535
21ae7a4d 24536 case DW_OP_addr:
c8a7a66f
TT
24537 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24538 &bytes_read);
21ae7a4d
JK
24539 i += bytes_read;
24540 break;
d53d4ac5 24541
21ae7a4d
JK
24542 case DW_OP_const1u:
24543 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24544 i += 1;
24545 break;
24546
24547 case DW_OP_const1s:
24548 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24549 i += 1;
24550 break;
24551
24552 case DW_OP_const2u:
24553 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24554 i += 2;
24555 break;
24556
24557 case DW_OP_const2s:
24558 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24559 i += 2;
24560 break;
d53d4ac5 24561
21ae7a4d
JK
24562 case DW_OP_const4u:
24563 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24564 i += 4;
24565 break;
24566
24567 case DW_OP_const4s:
24568 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24569 i += 4;
24570 break;
24571
585861ea
JK
24572 case DW_OP_const8u:
24573 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24574 i += 8;
24575 break;
24576
21ae7a4d
JK
24577 case DW_OP_constu:
24578 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24579 &bytes_read);
24580 i += bytes_read;
24581 break;
24582
24583 case DW_OP_consts:
24584 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24585 i += bytes_read;
24586 break;
24587
24588 case DW_OP_dup:
24589 stack[stacki + 1] = stack[stacki];
24590 stacki++;
24591 break;
24592
24593 case DW_OP_plus:
24594 stack[stacki - 1] += stack[stacki];
24595 stacki--;
24596 break;
24597
24598 case DW_OP_plus_uconst:
24599 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24600 &bytes_read);
24601 i += bytes_read;
24602 break;
24603
24604 case DW_OP_minus:
24605 stack[stacki - 1] -= stack[stacki];
24606 stacki--;
24607 break;
24608
24609 case DW_OP_deref:
24610 /* If we're not the last op, then we definitely can't encode
24611 this using GDB's address_class enum. This is valid for partial
24612 global symbols, although the variable's address will be bogus
24613 in the psymtab. */
24614 if (i < size)
7d79de9a
TT
24615 {
24616 if (computed == nullptr)
24617 dwarf2_complex_location_expr_complaint ();
24618 else
24619 return 0;
24620 }
21ae7a4d
JK
24621 break;
24622
dda83cd7 24623 case DW_OP_GNU_push_tls_address:
4aa4e28b 24624 case DW_OP_form_tls_address:
21ae7a4d
JK
24625 /* The top of the stack has the offset from the beginning
24626 of the thread control block at which the variable is located. */
24627 /* Nothing should follow this operator, so the top of stack would
24628 be returned. */
24629 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24630 address will be bogus in the psymtab. Make it always at least
24631 non-zero to not look as a variable garbage collected by linker
24632 which have DW_OP_addr 0. */
21ae7a4d 24633 if (i < size)
7d79de9a
TT
24634 {
24635 if (computed == nullptr)
24636 dwarf2_complex_location_expr_complaint ();
24637 else
24638 return 0;
24639 }
585861ea 24640 stack[stacki]++;
dda83cd7 24641 break;
21ae7a4d
JK
24642
24643 case DW_OP_GNU_uninit:
7d79de9a
TT
24644 if (computed != nullptr)
24645 return 0;
21ae7a4d
JK
24646 break;
24647
336d760d 24648 case DW_OP_addrx:
3019eac3 24649 case DW_OP_GNU_addr_index:
49f6c839 24650 case DW_OP_GNU_const_index:
3019eac3
DE
24651 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24652 &bytes_read);
24653 i += bytes_read;
24654 break;
24655
21ae7a4d 24656 default:
7d79de9a
TT
24657 if (computed == nullptr)
24658 {
24659 const char *name = get_DW_OP_name (op);
21ae7a4d 24660
7d79de9a
TT
24661 if (name)
24662 complaint (_("unsupported stack op: '%s'"),
24663 name);
24664 else
24665 complaint (_("unsupported stack op: '%02x'"),
24666 op);
24667 }
21ae7a4d
JK
24668
24669 return (stack[stacki]);
d53d4ac5 24670 }
3c6e0cb3 24671
21ae7a4d 24672 /* Enforce maximum stack depth of SIZE-1 to avoid writing
dda83cd7 24673 outside of the allocated space. Also enforce minimum>0. */
21ae7a4d
JK
24674 if (stacki >= ARRAY_SIZE (stack) - 1)
24675 {
7d79de9a
TT
24676 if (computed == nullptr)
24677 complaint (_("location description stack overflow"));
21ae7a4d
JK
24678 return 0;
24679 }
24680
24681 if (stacki <= 0)
24682 {
7d79de9a
TT
24683 if (computed == nullptr)
24684 complaint (_("location description stack underflow"));
21ae7a4d
JK
24685 return 0;
24686 }
24687 }
7d79de9a
TT
24688
24689 if (computed != nullptr)
24690 *computed = true;
21ae7a4d 24691 return (stack[stacki]);
c906108c
SS
24692}
24693
24694/* memory allocation interface */
24695
c906108c 24696static struct dwarf_block *
7b5a2f43 24697dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24698{
8d749320 24699 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24700}
24701
c906108c 24702static struct die_info *
b60c80d6 24703dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24704{
24705 struct die_info *die;
b60c80d6
DJ
24706 size_t size = sizeof (struct die_info);
24707
24708 if (num_attrs > 1)
24709 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24710
b60c80d6 24711 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24712 memset (die, 0, sizeof (struct die_info));
24713 return (die);
24714}
2e276125
JB
24715
24716\f
a036ba48 24717
c90ec28a 24718/* Macro support. */
cf2c3c16 24719
9eac9650
TT
24720/* An overload of dwarf_decode_macros that finds the correct section
24721 and ensures it is read in before calling the other overload. */
24722
24723static void
24724dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24725 int section_is_gnu)
24726{
976ca316
SM
24727 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24728 struct objfile *objfile = per_objfile->objfile;
5a0e026f 24729 const struct line_header *lh = cu->line_header;
9eac9650
TT
24730 unsigned int offset_size = cu->header.offset_size;
24731 struct dwarf2_section_info *section;
24732 const char *section_name;
24733
24734 if (cu->dwo_unit != nullptr)
24735 {
24736 if (section_is_gnu)
24737 {
24738 section = &cu->dwo_unit->dwo_file->sections.macro;
24739 section_name = ".debug_macro.dwo";
24740 }
24741 else
24742 {
24743 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24744 section_name = ".debug_macinfo.dwo";
24745 }
24746 }
24747 else
24748 {
24749 if (section_is_gnu)
24750 {
976ca316 24751 section = &per_objfile->per_bfd->macro;
9eac9650
TT
24752 section_name = ".debug_macro";
24753 }
24754 else
24755 {
976ca316 24756 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
24757 section_name = ".debug_macinfo";
24758 }
24759 }
24760
24761 section->read (objfile);
24762 if (section->buffer == nullptr)
24763 {
24764 complaint (_("missing %s section"), section_name);
24765 return;
24766 }
24767
24768 buildsym_compunit *builder = cu->get_builder ();
24769
048fde1e 24770 struct dwarf2_section_info *str_offsets_section;
24771 struct dwarf2_section_info *str_section;
24772 ULONGEST str_offsets_base;
24773
24774 if (cu->dwo_unit != nullptr)
24775 {
24776 str_offsets_section = &cu->dwo_unit->dwo_file
24777 ->sections.str_offsets;
24778 str_section = &cu->dwo_unit->dwo_file->sections.str;
24779 str_offsets_base = cu->header.addr_size;
24780 }
24781 else
24782 {
24783 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24784 str_section = &per_objfile->per_bfd->str;
24785 str_offsets_base = *cu->str_offsets_base;
24786 }
24787
976ca316 24788 dwarf_decode_macros (per_objfile, builder, section, lh,
048fde1e 24789 offset_size, offset, str_section, str_offsets_section,
24790 str_offsets_base, section_is_gnu);
9eac9650
TT
24791}
24792
3019eac3
DE
24793/* Return the .debug_loc section to use for CU.
24794 For DWO files use .debug_loc.dwo. */
24795
24796static struct dwarf2_section_info *
24797cu_debug_loc_section (struct dwarf2_cu *cu)
24798{
976ca316 24799 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24800
3019eac3 24801 if (cu->dwo_unit)
43988095
JK
24802 {
24803 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24804
43988095
JK
24805 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24806 }
976ca316
SM
24807 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24808 : &per_objfile->per_bfd->loc);
3019eac3
DE
24809}
24810
d0ce17d8
CT
24811/* Return the .debug_rnglists section to use for CU. */
24812static struct dwarf2_section_info *
24813cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24814{
24815 if (cu->header.version < 5)
24816 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24817 cu->header.version);
24818 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24819
24820 /* Make sure we read the .debug_rnglists section from the file that
24821 contains the DW_AT_ranges attribute we are reading. Normally that
24822 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24823 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24824 program. */
24825 if (cu->dwo_unit != nullptr
24826 && tag != DW_TAG_compile_unit
24827 && tag != DW_TAG_skeleton_unit)
24828 {
24829 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24830
24831 if (sections->rnglists.size > 0)
24832 return &sections->rnglists;
24833 else
24834 error (_(".debug_rnglists section is missing from .dwo file."));
24835 }
24836 return &dwarf2_per_objfile->per_bfd->rnglists;
24837}
24838
8cf6f0b1
TT
24839/* A helper function that fills in a dwarf2_loclist_baton. */
24840
24841static void
24842fill_in_loclist_baton (struct dwarf2_cu *cu,
24843 struct dwarf2_loclist_baton *baton,
ff39bb5e 24844 const struct attribute *attr)
8cf6f0b1 24845{
976ca316 24846 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
24847 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24848
976ca316 24849 section->read (per_objfile->objfile);
8cf6f0b1 24850
976ca316 24851 baton->per_objfile = per_objfile;
8cf6f0b1
TT
24852 baton->per_cu = cu->per_cu;
24853 gdb_assert (baton->per_cu);
24854 /* We don't know how long the location list is, but make sure we
24855 don't run off the edge of the section. */
d4df075e
TT
24856 baton->size = section->size - attr->as_unsigned ();
24857 baton->data = section->buffer + attr->as_unsigned ();
2b24b6e4
TT
24858 if (cu->base_address.has_value ())
24859 baton->base_address = *cu->base_address;
24860 else
24861 baton->base_address = 0;
f664829e 24862 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24863}
24864
4c2df51b 24865static void
ff39bb5e 24866dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24867 struct dwarf2_cu *cu, int is_block)
4c2df51b 24868{
976ca316
SM
24869 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24870 struct objfile *objfile = per_objfile->objfile;
3019eac3 24871 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24872
cd6c91b4 24873 if (attr->form_is_section_offset ()
3019eac3 24874 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24875 the section. If so, fall through to the complaint in the
24876 other branch. */
d4df075e 24877 && attr->as_unsigned () < section->get_size (objfile))
4c2df51b 24878 {
0d53c4c4 24879 struct dwarf2_loclist_baton *baton;
4c2df51b 24880
8d749320 24881 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24882
8cf6f0b1 24883 fill_in_loclist_baton (cu, baton, attr);
be391dca 24884
2b24b6e4 24885 if (!cu->base_address.has_value ())
b98664d3 24886 complaint (_("Location list used without "
3e43a32a 24887 "specifying the CU base address."));
4c2df51b 24888
f1e6e072
TT
24889 SYMBOL_ACLASS_INDEX (sym) = (is_block
24890 ? dwarf2_loclist_block_index
24891 : dwarf2_loclist_index);
0d53c4c4
DJ
24892 SYMBOL_LOCATION_BATON (sym) = baton;
24893 }
24894 else
24895 {
24896 struct dwarf2_locexpr_baton *baton;
24897
8d749320 24898 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 24899 baton->per_objfile = per_objfile;
ae0d2f24
UW
24900 baton->per_cu = cu->per_cu;
24901 gdb_assert (baton->per_cu);
0d53c4c4 24902
4fc6c0d5 24903 if (attr->form_is_block ())
0d53c4c4
DJ
24904 {
24905 /* Note that we're just copying the block's data pointer
24906 here, not the actual data. We're still pointing into the
6502dd73
DJ
24907 info_buffer for SYM's objfile; right now we never release
24908 that buffer, but when we do clean up properly this may
24909 need to change. */
9d2246fc
TT
24910 struct dwarf_block *block = attr->as_block ();
24911 baton->size = block->size;
24912 baton->data = block->data;
0d53c4c4
DJ
24913 }
24914 else
24915 {
24916 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24917 sym->natural_name ());
0d53c4c4 24918 baton->size = 0;
0d53c4c4 24919 }
6e70227d 24920
f1e6e072
TT
24921 SYMBOL_ACLASS_INDEX (sym) = (is_block
24922 ? dwarf2_locexpr_block_index
24923 : dwarf2_locexpr_index);
0d53c4c4
DJ
24924 SYMBOL_LOCATION_BATON (sym) = baton;
24925 }
4c2df51b 24926}
6502dd73 24927
2e6a9f79 24928/* See read.h. */
96408a79 24929
2e6a9f79
SM
24930const comp_unit_head *
24931dwarf2_per_cu_data::get_header () const
96408a79 24932{
2e6a9f79
SM
24933 if (!m_header_read_in)
24934 {
24935 const gdb_byte *info_ptr
24936 = this->section->buffer + to_underlying (this->sect_off);
96408a79 24937
2e6a9f79 24938 memset (&m_header, 0, sizeof (m_header));
96408a79 24939
2e6a9f79
SM
24940 read_comp_unit_head (&m_header, info_ptr, this->section,
24941 rcuh_kind::COMPILE);
a9f172c6
TT
24942
24943 m_header_read_in = true;
2e6a9f79 24944 }
96408a79 24945
2e6a9f79 24946 return &m_header;
96408a79
SA
24947}
24948
09ba997f 24949/* See read.h. */
ae0d2f24 24950
98714339 24951int
09ba997f 24952dwarf2_per_cu_data::addr_size () const
ae0d2f24 24953{
2e6a9f79 24954 return this->get_header ()->addr_size;
ae0d2f24
UW
24955}
24956
09ba997f 24957/* See read.h. */
9eae7c52
TT
24958
24959int
09ba997f 24960dwarf2_per_cu_data::offset_size () const
9eae7c52 24961{
2e6a9f79 24962 return this->get_header ()->offset_size;
96408a79
SA
24963}
24964
09ba997f 24965/* See read.h. */
96408a79
SA
24966
24967int
09ba997f 24968dwarf2_per_cu_data::ref_addr_size () const
96408a79 24969{
2e6a9f79 24970 const comp_unit_head *header = this->get_header ();
96408a79 24971
2e6a9f79
SM
24972 if (header->version == 2)
24973 return header->addr_size;
96408a79 24974 else
2e6a9f79 24975 return header->offset_size;
181cebd4
JK
24976}
24977
09ba997f 24978/* See read.h. */
9aa1f1e3 24979
09ba997f 24980struct type *
293e7e51 24981dwarf2_cu::addr_type () const
9a49df9d 24982{
293e7e51 24983 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
24984 struct type *void_type = objfile_type (objfile)->builtin_void;
24985 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 24986 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
24987
24988 if (TYPE_LENGTH (addr_type) == addr_size)
24989 return addr_type;
24990
c6d940a9 24991 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
9a49df9d
AB
24992 return addr_type;
24993}
24994
22b6cd70
TT
24995/* A helper function for dwarf2_find_containing_comp_unit that returns
24996 the index of the result, and that searches a vector. It will
24997 return a result even if the offset in question does not actually
24998 occur in any CU. This is separate so that it can be unit
24999 tested. */
ae038cb0 25000
22b6cd70
TT
25001static int
25002dwarf2_find_containing_comp_unit
25003 (sect_offset sect_off,
25004 unsigned int offset_in_dwz,
25005 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 25006{
ae038cb0
DJ
25007 int low, high;
25008
ae038cb0 25009 low = 0;
22b6cd70 25010 high = all_comp_units.size () - 1;
ae038cb0
DJ
25011 while (high > low)
25012 {
36586728 25013 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25014 int mid = low + (high - low) / 2;
9a619af0 25015
22b6cd70 25016 mid_cu = all_comp_units[mid];
36586728 25017 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 25018 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 25019 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
25020 high = mid;
25021 else
25022 low = mid + 1;
25023 }
25024 gdb_assert (low == high);
22b6cd70
TT
25025 return low;
25026}
25027
25028/* Locate the .debug_info compilation unit from CU's objfile which contains
25029 the DIE at OFFSET. Raises an error on failure. */
25030
25031static struct dwarf2_per_cu_data *
25032dwarf2_find_containing_comp_unit (sect_offset sect_off,
25033 unsigned int offset_in_dwz,
976ca316 25034 dwarf2_per_objfile *per_objfile)
22b6cd70 25035{
976ca316
SM
25036 int low = dwarf2_find_containing_comp_unit
25037 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
25038 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
22b6cd70 25039
45b8ae0c 25040 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 25041 {
36586728 25042 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25043 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25044 "offset %s [in module %s]"),
25045 sect_offset_str (sect_off),
976ca316 25046 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 25047
976ca316 25048 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 25049 <= sect_off);
976ca316 25050 return per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
25051 }
25052 else
25053 {
976ca316 25054 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 25055 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25056 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25057 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25058 return this_cu;
25059 }
25060}
25061
22b6cd70
TT
25062#if GDB_SELF_TEST
25063
25064namespace selftests {
25065namespace find_containing_comp_unit {
25066
25067static void
25068run_test ()
25069{
25070 struct dwarf2_per_cu_data one {};
25071 struct dwarf2_per_cu_data two {};
25072 struct dwarf2_per_cu_data three {};
25073 struct dwarf2_per_cu_data four {};
25074
25075 one.length = 5;
25076 two.sect_off = sect_offset (one.length);
25077 two.length = 7;
25078
25079 three.length = 5;
25080 three.is_dwz = 1;
25081 four.sect_off = sect_offset (three.length);
25082 four.length = 7;
25083 four.is_dwz = 1;
25084
25085 std::vector<dwarf2_per_cu_data *> units;
25086 units.push_back (&one);
25087 units.push_back (&two);
25088 units.push_back (&three);
25089 units.push_back (&four);
25090
25091 int result;
25092
25093 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
25094 SELF_CHECK (units[result] == &one);
25095 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
25096 SELF_CHECK (units[result] == &one);
25097 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
25098 SELF_CHECK (units[result] == &two);
25099
25100 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
25101 SELF_CHECK (units[result] == &three);
25102 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
25103 SELF_CHECK (units[result] == &three);
25104 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
25105 SELF_CHECK (units[result] == &four);
25106}
25107
25108}
25109}
25110
25111#endif /* GDB_SELF_TEST */
25112
9e021579 25113/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 25114
9e021579
SM
25115dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
25116 dwarf2_per_objfile *per_objfile)
25117 : per_cu (per_cu),
25118 per_objfile (per_objfile),
9068261f
AB
25119 mark (false),
25120 has_loclist (false),
25121 checked_producer (false),
25122 producer_is_gxx_lt_4_6 (false),
25123 producer_is_gcc_lt_4_3 (false),
eb77c9df 25124 producer_is_icc (false),
9068261f 25125 producer_is_icc_lt_14 (false),
c258c396 25126 producer_is_codewarrior (false),
9068261f 25127 processing_has_namespace_info (false)
93311388 25128{
9816fde3
JK
25129}
25130
25131/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25132
25133static void
95554aad
TT
25134prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25135 enum language pretend_language)
9816fde3
JK
25136{
25137 struct attribute *attr;
25138
25139 /* Set the language we're debugging. */
25140 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 25141 if (attr != nullptr)
529908cb 25142 set_cu_language (attr->constant_value (0), cu);
9816fde3 25143 else
9cded63f 25144 {
95554aad 25145 cu->language = pretend_language;
9cded63f
TT
25146 cu->language_defn = language_def (cu->language);
25147 }
dee91e82 25148
7d45c7c3 25149 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25150}
25151
7188ed02 25152/* See read.h. */
ae038cb0 25153
7188ed02
SM
25154dwarf2_cu *
25155dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 25156{
7188ed02
SM
25157 auto it = m_dwarf2_cus.find (per_cu);
25158 if (it == m_dwarf2_cus.end ())
25159 return nullptr;
ae038cb0 25160
7188ed02
SM
25161 return it->second;
25162}
25163
25164/* See read.h. */
25165
25166void
25167dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
25168{
25169 gdb_assert (this->get_cu (per_cu) == nullptr);
25170
25171 m_dwarf2_cus[per_cu] = cu;
25172}
25173
25174/* See read.h. */
25175
25176void
25177dwarf2_per_objfile::age_comp_units ()
25178{
17e593e9
SM
25179 dwarf_read_debug_printf_v ("running");
25180
08ac5771
SM
25181 /* This is not expected to be called in the middle of CU expansion. There is
25182 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
25183 loaded in memory. Calling age_comp_units while the queue is in use could
25184 make us free the DIEs for a CU that is in the queue and therefore break
25185 that invariant. */
25186 gdb_assert (!this->per_bfd->queue.has_value ());
25187
7188ed02
SM
25188 /* Start by clearing all marks. */
25189 for (auto pair : m_dwarf2_cus)
25190 pair.second->mark = false;
25191
25192 /* Traverse all CUs, mark them and their dependencies if used recently
25193 enough. */
25194 for (auto pair : m_dwarf2_cus)
ae038cb0 25195 {
7188ed02
SM
25196 dwarf2_cu *cu = pair.second;
25197
25198 cu->last_used++;
25199 if (cu->last_used <= dwarf_max_cache_age)
25200 dwarf2_mark (cu);
ae038cb0
DJ
25201 }
25202
7188ed02
SM
25203 /* Delete all CUs still not marked. */
25204 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 25205 {
7188ed02 25206 dwarf2_cu *cu = it->second;
ae038cb0 25207
7188ed02 25208 if (!cu->mark)
ae038cb0 25209 {
17e593e9
SM
25210 dwarf_read_debug_printf_v ("deleting old CU %s",
25211 sect_offset_str (cu->per_cu->sect_off));
7188ed02
SM
25212 delete cu;
25213 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
25214 }
25215 else
7188ed02 25216 it++;
ae038cb0
DJ
25217 }
25218}
25219
7188ed02 25220/* See read.h. */
ae038cb0 25221
7188ed02
SM
25222void
25223dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 25224{
7188ed02
SM
25225 auto it = m_dwarf2_cus.find (per_cu);
25226 if (it == m_dwarf2_cus.end ())
25227 return;
ae038cb0 25228
7188ed02 25229 delete it->second;
ae038cb0 25230
7188ed02
SM
25231 m_dwarf2_cus.erase (it);
25232}
ae038cb0 25233
7188ed02
SM
25234dwarf2_per_objfile::~dwarf2_per_objfile ()
25235{
25236 remove_all_cus ();
ae038cb0
DJ
25237}
25238
dee91e82
DE
25239/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25240 We store these in a hash table separate from the DIEs, and preserve them
25241 when the DIEs are flushed out of cache.
25242
25243 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25244 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25245 or the type may come from a DWO file. Furthermore, while it's more logical
25246 to use per_cu->section+offset, with Fission the section with the data is in
25247 the DWO file but we don't know that section at the point we need it.
25248 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25249 because we can enter the lookup routine, get_die_type_at_offset, from
25250 outside this file, and thus won't necessarily have PER_CU->cu.
25251 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25252
dee91e82 25253struct dwarf2_per_cu_offset_and_type
1c379e20 25254{
dee91e82 25255 const struct dwarf2_per_cu_data *per_cu;
9c541725 25256 sect_offset sect_off;
1c379e20
DJ
25257 struct type *type;
25258};
25259
dee91e82 25260/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25261
25262static hashval_t
dee91e82 25263per_cu_offset_and_type_hash (const void *item)
1c379e20 25264{
9a3c8263
SM
25265 const struct dwarf2_per_cu_offset_and_type *ofs
25266 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25267
9c541725 25268 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25269}
25270
dee91e82 25271/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25272
25273static int
dee91e82 25274per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25275{
9a3c8263
SM
25276 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25277 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25278 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25279 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25280
dee91e82 25281 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25282 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25283}
25284
25285/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25286 table if necessary. For convenience, return TYPE.
25287
25288 The DIEs reading must have careful ordering to:
85102364 25289 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25290 reading current DIE.
25291 * Not trying to dereference contents of still incompletely read in types
25292 while reading in other DIEs.
25293 * Enable referencing still incompletely read in types just by a pointer to
25294 the type without accessing its fields.
25295
25296 Therefore caller should follow these rules:
25297 * Try to fetch any prerequisite types we may need to build this DIE type
25298 before building the type and calling set_die_type.
e71ec853 25299 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25300 possible before fetching more types to complete the current type.
25301 * Make the type as complete as possible before fetching more types. */
1c379e20 25302
f792889a 25303static struct type *
57567375
TT
25304set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25305 bool skip_data_location)
1c379e20 25306{
976ca316 25307 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 25308 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 25309 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
25310 struct attribute *attr;
25311 struct dynamic_prop prop;
1c379e20 25312
b4ba55a1
JB
25313 /* For Ada types, make sure that the gnat-specific data is always
25314 initialized (if not already set). There are a few types where
25315 we should not be doing so, because the type-specific area is
25316 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25317 where the type-specific area is used to store the floatformat).
25318 But this is not a problem, because the gnat-specific information
25319 is actually not needed for these types. */
25320 if (need_gnat_info (cu)
78134374
SM
25321 && type->code () != TYPE_CODE_FUNC
25322 && type->code () != TYPE_CODE_FLT
25323 && type->code () != TYPE_CODE_METHODPTR
25324 && type->code () != TYPE_CODE_MEMBERPTR
25325 && type->code () != TYPE_CODE_METHOD
09584414 25326 && type->code () != TYPE_CODE_FIXED_POINT
b4ba55a1
JB
25327 && !HAVE_GNAT_AUX_INFO (type))
25328 INIT_GNAT_SPECIFIC (type);
25329
3f2f83dd
KB
25330 /* Read DW_AT_allocated and set in type. */
25331 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 25332 if (attr != NULL)
3f2f83dd 25333 {
293e7e51 25334 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25335 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25336 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 25337 }
3f2f83dd
KB
25338
25339 /* Read DW_AT_associated and set in type. */
25340 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 25341 if (attr != NULL)
3f2f83dd 25342 {
293e7e51 25343 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25344 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25345 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 25346 }
3f2f83dd 25347
3cdcd0ce 25348 /* Read DW_AT_data_location and set in type. */
57567375
TT
25349 if (!skip_data_location)
25350 {
25351 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25352 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25353 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25354 }
3cdcd0ce 25355
976ca316
SM
25356 if (per_objfile->die_type_hash == NULL)
25357 per_objfile->die_type_hash
0335378b
TT
25358 = htab_up (htab_create_alloc (127,
25359 per_cu_offset_and_type_hash,
25360 per_cu_offset_and_type_eq,
25361 NULL, xcalloc, xfree));
1c379e20 25362
dee91e82 25363 ofs.per_cu = cu->per_cu;
9c541725 25364 ofs.sect_off = die->sect_off;
1c379e20 25365 ofs.type = type;
dee91e82 25366 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 25367 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 25368 if (*slot)
b98664d3 25369 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25370 sect_offset_str (die->sect_off));
8d749320
SM
25371 *slot = XOBNEW (&objfile->objfile_obstack,
25372 struct dwarf2_per_cu_offset_and_type);
1c379e20 25373 **slot = ofs;
f792889a 25374 return type;
1c379e20
DJ
25375}
25376
9c541725 25377/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25378 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25379
25380static struct type *
9c541725 25381get_die_type_at_offset (sect_offset sect_off,
aa66c379 25382 dwarf2_per_cu_data *per_cu,
976ca316 25383 dwarf2_per_objfile *per_objfile)
1c379e20 25384{
dee91e82 25385 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 25386
976ca316 25387 if (per_objfile->die_type_hash == NULL)
f792889a 25388 return NULL;
1c379e20 25389
dee91e82 25390 ofs.per_cu = per_cu;
9c541725 25391 ofs.sect_off = sect_off;
9a3c8263 25392 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 25393 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
25394 if (slot)
25395 return slot->type;
25396 else
25397 return NULL;
25398}
25399
02142a6c 25400/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25401 or return NULL if DIE does not have a saved type. */
25402
25403static struct type *
25404get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25405{
aa66c379 25406 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
25407}
25408
10b3939b
DJ
25409/* Add a dependence relationship from CU to REF_PER_CU. */
25410
25411static void
25412dwarf2_add_dependence (struct dwarf2_cu *cu,
25413 struct dwarf2_per_cu_data *ref_per_cu)
25414{
25415 void **slot;
25416
25417 if (cu->dependencies == NULL)
25418 cu->dependencies
25419 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25420 NULL, &cu->comp_unit_obstack,
25421 hashtab_obstack_allocate,
25422 dummy_obstack_deallocate);
25423
25424 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25425 if (*slot == NULL)
25426 *slot = ref_per_cu;
25427}
1c379e20 25428
f504f079
DE
25429/* Subroutine of dwarf2_mark to pass to htab_traverse.
25430 Set the mark field in every compilation unit in the
7188ed02
SM
25431 cache that we must keep because we are keeping CU.
25432
25433 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 25434
10b3939b
DJ
25435static int
25436dwarf2_mark_helper (void **slot, void *data)
25437{
7188ed02
SM
25438 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25439 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25440 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
25441
25442 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25443 reading of the chain. As such dependencies remain valid it is not much
25444 useful to track and undo them during QUIT cleanups. */
7188ed02 25445 if (cu == nullptr)
d07ed419
JK
25446 return 1;
25447
7188ed02 25448 if (cu->mark)
10b3939b 25449 return 1;
10b3939b 25450
7188ed02
SM
25451 cu->mark = true;
25452
25453 if (cu->dependencies != nullptr)
25454 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
25455
25456 return 1;
25457}
25458
f504f079
DE
25459/* Set the mark field in CU and in every other compilation unit in the
25460 cache that we must keep because we are keeping CU. */
25461
ae038cb0
DJ
25462static void
25463dwarf2_mark (struct dwarf2_cu *cu)
25464{
25465 if (cu->mark)
25466 return;
7188ed02 25467
9068261f 25468 cu->mark = true;
ae038cb0 25469
7188ed02
SM
25470 if (cu->dependencies != nullptr)
25471 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
25472}
25473
72bf9492
DJ
25474/* Trivial hash function for partial_die_info: the hash value of a DIE
25475 is its offset in .debug_info for this objfile. */
25476
25477static hashval_t
25478partial_die_hash (const void *item)
25479{
9a3c8263
SM
25480 const struct partial_die_info *part_die
25481 = (const struct partial_die_info *) item;
9a619af0 25482
9c541725 25483 return to_underlying (part_die->sect_off);
72bf9492
DJ
25484}
25485
25486/* Trivial comparison function for partial_die_info structures: two DIEs
25487 are equal if they have the same offset. */
25488
25489static int
25490partial_die_eq (const void *item_lhs, const void *item_rhs)
25491{
9a3c8263
SM
25492 const struct partial_die_info *part_die_lhs
25493 = (const struct partial_die_info *) item_lhs;
25494 const struct partial_die_info *part_die_rhs
25495 = (const struct partial_die_info *) item_rhs;
9a619af0 25496
9c541725 25497 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25498}
25499
3c3bb058
AB
25500struct cmd_list_element *set_dwarf_cmdlist;
25501struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 25502
9291a0cd 25503static void
cd4fb1b2
SM
25504show_check_physname (struct ui_file *file, int from_tty,
25505 struct cmd_list_element *c, const char *value)
9291a0cd 25506{
cd4fb1b2
SM
25507 fprintf_filtered (file,
25508 _("Whether to check \"physname\" is %s.\n"),
25509 value);
9291a0cd
TT
25510}
25511
6c265988 25512void _initialize_dwarf2_read ();
cd4fb1b2 25513void
6c265988 25514_initialize_dwarf2_read ()
9291a0cd 25515{
0743fc83 25516 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 25517Set DWARF specific variables.\n\
590042fc 25518Configure DWARF variables such as the cache size."),
0743fc83
TT
25519 &set_dwarf_cmdlist, "maintenance set dwarf ",
25520 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25521
0743fc83 25522 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
25523Show DWARF specific variables.\n\
25524Show DWARF variables such as the cache size."),
0743fc83
TT
25525 &show_dwarf_cmdlist, "maintenance show dwarf ",
25526 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25527
cd4fb1b2
SM
25528 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25529 &dwarf_max_cache_age, _("\
25530Set the upper bound on the age of cached DWARF compilation units."), _("\
25531Show the upper bound on the age of cached DWARF compilation units."), _("\
25532A higher limit means that cached compilation units will be stored\n\
25533in memory longer, and more total memory will be used. Zero disables\n\
25534caching, which can slow down startup."),
25535 NULL,
25536 show_dwarf_max_cache_age,
25537 &set_dwarf_cmdlist,
25538 &show_dwarf_cmdlist);
156942c7 25539
cd4fb1b2
SM
25540 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25541Set debugging of the DWARF reader."), _("\
25542Show debugging of the DWARF reader."), _("\
25543When enabled (non-zero), debugging messages are printed during DWARF\n\
25544reading and symtab expansion. A value of 1 (one) provides basic\n\
25545information. A value greater than 1 provides more verbose information."),
25546 NULL,
25547 NULL,
25548 &setdebuglist, &showdebuglist);
9291a0cd 25549
cd4fb1b2
SM
25550 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25551Set debugging of the DWARF DIE reader."), _("\
25552Show debugging of the DWARF DIE reader."), _("\
25553When enabled (non-zero), DIEs are dumped after they are read in.\n\
25554The value is the maximum depth to print."),
25555 NULL,
25556 NULL,
25557 &setdebuglist, &showdebuglist);
9291a0cd 25558
cd4fb1b2
SM
25559 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25560Set debugging of the dwarf line reader."), _("\
25561Show debugging of the dwarf line reader."), _("\
25562When enabled (non-zero), line number entries are dumped as they are read in.\n\
25563A value of 1 (one) provides basic information.\n\
25564A value greater than 1 provides more verbose information."),
25565 NULL,
25566 NULL,
25567 &setdebuglist, &showdebuglist);
437afbb8 25568
cd4fb1b2
SM
25569 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25570Set cross-checking of \"physname\" code against demangler."), _("\
25571Show cross-checking of \"physname\" code against demangler."), _("\
25572When enabled, GDB's internal \"physname\" code is checked against\n\
25573the demangler."),
25574 NULL, show_check_physname,
25575 &setdebuglist, &showdebuglist);
900e11f9 25576
e615022a
DE
25577 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25578 no_class, &use_deprecated_index_sections, _("\
25579Set whether to use deprecated gdb_index sections."), _("\
25580Show whether to use deprecated gdb_index sections."), _("\
25581When enabled, deprecated .gdb_index sections are used anyway.\n\
25582Normally they are ignored either because of a missing feature or\n\
25583performance issue.\n\
25584Warning: This option must be enabled before gdb reads the file."),
25585 NULL,
25586 NULL,
25587 &setlist, &showlist);
25588
f1e6e072
TT
25589 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25590 &dwarf2_locexpr_funcs);
25591 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25592 &dwarf2_loclist_funcs);
25593
25594 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25595 &dwarf2_block_frame_base_locexpr_funcs);
25596 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25597 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25598
25599#if GDB_SELF_TEST
25600 selftests::register_test ("dw2_expand_symtabs_matching",
25601 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
25602 selftests::register_test ("dwarf2_find_containing_comp_unit",
25603 selftests::find_containing_comp_unit::run_test);
c62446b1 25604#endif
6502dd73 25605}
This page took 5.92039 seconds and 4 git commands to generate.