gdb: handle relative paths to DWO files
[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
8a91fbdf
SM
1954 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1955 BFD doesn't require relocations.
1956
1957 We don't share with objfiles for which -readnow was requested,
1958 because it would complicate things when loading the same BFD with
1959 -readnow and then without -readnow. */
1960 if (!gdb_bfd_requires_relocations (objfile->obfd)
1961 && (objfile->flags & OBJF_READNOW) == 0)
17ee85fc
TT
1962 {
1963 /* See if one has been created for this BFD yet. */
1964 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1965
1966 if (per_bfd == nullptr)
1967 {
1968 /* No, create it now. */
1969 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1970 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1971 }
1972 }
1973 else
1974 {
1975 /* No sharing possible, create one specifically for this objfile. */
1976 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1977 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1978 }
5989a64e 1979
976ca316 1980 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 1981 }
5bfd760d 1982
976ca316
SM
1983 return (!per_objfile->per_bfd->info.is_virtual
1984 && per_objfile->per_bfd->info.s.section != NULL
1985 && !per_objfile->per_bfd->abbrev.is_virtual
1986 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1987}
1988
330cdd98 1989/* See declaration. */
c906108c 1990
330cdd98 1991void
5989a64e
SM
1992dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1993 const dwarf2_debug_sections &names)
c906108c 1994{
fd361982 1995 flagword aflag = bfd_section_flags (sectp);
251d32d9 1996
dc7650b8
JK
1997 if ((aflag & SEC_HAS_CONTENTS) == 0)
1998 {
1999 }
950b7495
KS
2000 else if (elf_section_data (sectp)->this_hdr.sh_size
2001 > bfd_get_file_size (abfd))
2002 {
2003 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2004 warning (_("Discarding section %s which has a section size (%s"
2005 ") larger than the file size [in module %s]"),
2006 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2007 bfd_get_filename (abfd));
2008 }
fbedd546 2009 else if (names.info.matches (sectp->name))
c906108c 2010 {
330cdd98 2011 this->info.s.section = sectp;
fd361982 2012 this->info.size = bfd_section_size (sectp);
c906108c 2013 }
fbedd546 2014 else if (names.abbrev.matches (sectp->name))
c906108c 2015 {
330cdd98 2016 this->abbrev.s.section = sectp;
fd361982 2017 this->abbrev.size = bfd_section_size (sectp);
c906108c 2018 }
fbedd546 2019 else if (names.line.matches (sectp->name))
c906108c 2020 {
330cdd98 2021 this->line.s.section = sectp;
fd361982 2022 this->line.size = bfd_section_size (sectp);
c906108c 2023 }
fbedd546 2024 else if (names.loc.matches (sectp->name))
c906108c 2025 {
330cdd98 2026 this->loc.s.section = sectp;
fd361982 2027 this->loc.size = bfd_section_size (sectp);
c906108c 2028 }
fbedd546 2029 else if (names.loclists.matches (sectp->name))
43988095 2030 {
330cdd98 2031 this->loclists.s.section = sectp;
fd361982 2032 this->loclists.size = bfd_section_size (sectp);
43988095 2033 }
fbedd546 2034 else if (names.macinfo.matches (sectp->name))
c906108c 2035 {
330cdd98 2036 this->macinfo.s.section = sectp;
fd361982 2037 this->macinfo.size = bfd_section_size (sectp);
c906108c 2038 }
fbedd546 2039 else if (names.macro.matches (sectp->name))
cf2c3c16 2040 {
330cdd98 2041 this->macro.s.section = sectp;
fd361982 2042 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2043 }
fbedd546 2044 else if (names.str.matches (sectp->name))
c906108c 2045 {
330cdd98 2046 this->str.s.section = sectp;
fd361982 2047 this->str.size = bfd_section_size (sectp);
c906108c 2048 }
fbedd546 2049 else if (names.str_offsets.matches (sectp->name))
18a8505e
AT
2050 {
2051 this->str_offsets.s.section = sectp;
2052 this->str_offsets.size = bfd_section_size (sectp);
2053 }
fbedd546 2054 else if (names.line_str.matches (sectp->name))
43988095 2055 {
330cdd98 2056 this->line_str.s.section = sectp;
fd361982 2057 this->line_str.size = bfd_section_size (sectp);
43988095 2058 }
fbedd546 2059 else if (names.addr.matches (sectp->name))
3019eac3 2060 {
330cdd98 2061 this->addr.s.section = sectp;
fd361982 2062 this->addr.size = bfd_section_size (sectp);
3019eac3 2063 }
fbedd546 2064 else if (names.frame.matches (sectp->name))
b6af0555 2065 {
330cdd98 2066 this->frame.s.section = sectp;
fd361982 2067 this->frame.size = bfd_section_size (sectp);
b6af0555 2068 }
fbedd546 2069 else if (names.eh_frame.matches (sectp->name))
b6af0555 2070 {
330cdd98 2071 this->eh_frame.s.section = sectp;
fd361982 2072 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2073 }
fbedd546 2074 else if (names.ranges.matches (sectp->name))
af34e669 2075 {
330cdd98 2076 this->ranges.s.section = sectp;
fd361982 2077 this->ranges.size = bfd_section_size (sectp);
af34e669 2078 }
fbedd546 2079 else if (names.rnglists.matches (sectp->name))
43988095 2080 {
330cdd98 2081 this->rnglists.s.section = sectp;
fd361982 2082 this->rnglists.size = bfd_section_size (sectp);
43988095 2083 }
fbedd546 2084 else if (names.types.matches (sectp->name))
348e048f 2085 {
8b70b953
TT
2086 struct dwarf2_section_info type_section;
2087
2088 memset (&type_section, 0, sizeof (type_section));
049412e3 2089 type_section.s.section = sectp;
fd361982 2090 type_section.size = bfd_section_size (sectp);
8b70b953 2091
fd5866f6 2092 this->types.push_back (type_section);
348e048f 2093 }
fbedd546 2094 else if (names.gdb_index.matches (sectp->name))
9291a0cd 2095 {
330cdd98 2096 this->gdb_index.s.section = sectp;
fd361982 2097 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2098 }
fbedd546 2099 else if (names.debug_names.matches (sectp->name))
927aa2e7
JK
2100 {
2101 this->debug_names.s.section = sectp;
fd361982 2102 this->debug_names.size = bfd_section_size (sectp);
927aa2e7 2103 }
fbedd546 2104 else if (names.debug_aranges.matches (sectp->name))
927aa2e7
JK
2105 {
2106 this->debug_aranges.s.section = sectp;
fd361982 2107 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2108 }
dce234bc 2109
fd361982
AM
2110 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2111 && bfd_section_vma (sectp) == 0)
330cdd98 2112 this->has_section_at_zero = true;
c906108c
SS
2113}
2114
dce234bc 2115/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2116 SECTION_NAME. */
af34e669 2117
dce234bc 2118void
3017a003 2119dwarf2_get_section_info (struct objfile *objfile,
dda83cd7
SM
2120 enum dwarf2_section_enum sect,
2121 asection **sectp, const gdb_byte **bufp,
2122 bfd_size_type *sizep)
dce234bc 2123{
976ca316 2124 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 2125 struct dwarf2_section_info *info;
a3b2a86b
TT
2126
2127 /* We may see an objfile without any DWARF, in which case we just
2128 return nothing. */
976ca316 2129 if (per_objfile == NULL)
a3b2a86b
TT
2130 {
2131 *sectp = NULL;
2132 *bufp = NULL;
2133 *sizep = 0;
2134 return;
2135 }
3017a003
TG
2136 switch (sect)
2137 {
2138 case DWARF2_DEBUG_FRAME:
976ca316 2139 info = &per_objfile->per_bfd->frame;
3017a003
TG
2140 break;
2141 case DWARF2_EH_FRAME:
976ca316 2142 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
2143 break;
2144 default:
2145 gdb_assert_not_reached ("unexpected section");
2146 }
dce234bc 2147
96b79293 2148 info->read (objfile);
dce234bc 2149
96b79293 2150 *sectp = info->get_bfd_section ();
dce234bc
PP
2151 *bufp = info->buffer;
2152 *sizep = info->size;
2153}
2154
9291a0cd 2155\f
39298a5d 2156/* DWARF quick_symbol_functions support. */
7b9f3c50
DE
2157
2158/* TUs can share .debug_line entries, and there can be a lot more TUs than
2159 unique line tables, so we maintain a separate table of all .debug_line
2160 derived entries to support the sharing.
2161 All the quick functions need is the list of file names. We discard the
2162 line_header when we're done and don't need to record it here. */
2163struct quick_file_names
2164{
094b34ac
DE
2165 /* The data used to construct the hash key. */
2166 struct stmt_list_hash hash;
7b9f3c50
DE
2167
2168 /* The number of entries in file_names, real_names. */
2169 unsigned int num_file_names;
2170
2171 /* The file names from the line table, after being run through
2172 file_full_name. */
2173 const char **file_names;
2174
2175 /* The file names from the line table after being run through
2176 gdb_realpath. These are computed lazily. */
2177 const char **real_names;
2178};
2179
2180/* When using the index (and thus not using psymtabs), each CU has an
2181 object of this type. This is used to hold information needed by
2182 the various "quick" methods. */
2183struct dwarf2_per_cu_quick_data
2184{
2185 /* The file table. This can be NULL if there was no file table
2186 or it's currently not read in.
5989a64e 2187 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2188 struct quick_file_names *file_names;
2189
7b9f3c50
DE
2190 /* A temporary mark bit used when iterating over all CUs in
2191 expand_symtabs_matching. */
2192 unsigned int mark : 1;
2193
2194 /* True if we've tried to read the file table and found there isn't one.
2195 There will be no point in trying to read it again next time. */
2196 unsigned int no_file_data : 1;
2197};
2198
edc02ceb
TT
2199/* A subclass of psymbol_functions that arranges to read the DWARF
2200 partial symbols when needed. */
2201struct lazy_dwarf_reader : public psymbol_functions
2202{
2203 using psymbol_functions::psymbol_functions;
2204
2205 bool can_lazily_read_symbols () override
2206 {
2207 return true;
2208 }
2209
2210 void read_partial_symbols (struct objfile *objfile) override
2211 {
2212 if (dwarf2_has_info (objfile, nullptr))
2213 dwarf2_build_psymtabs (objfile, this);
2214 }
2215};
2216
2217static quick_symbol_functions_up
2218make_lazy_dwarf_reader ()
2219{
2220 return quick_symbol_functions_up (new lazy_dwarf_reader);
2221}
2222
39298a5d
TT
2223struct dwarf2_base_index_functions : public quick_symbol_functions
2224{
2225 bool has_symbols (struct objfile *objfile) override;
2226
2227 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2228
2229 void forget_cached_source_info (struct objfile *objfile) override;
2230
2231 bool map_symtabs_matching_filename
2232 (struct objfile *objfile, const char *name, const char *real_path,
2233 gdb::function_view<bool (symtab *)> callback) override;
2234
2235 enum language lookup_global_symbol_language (struct objfile *objfile,
2236 const char *name,
2237 domain_enum domain,
2238 bool *symbol_found_p) override
2239 {
2240 *symbol_found_p = false;
2241 return language_unknown;
2242 }
2243
4829711b 2244 void print_stats (struct objfile *objfile, bool print_bcache) override;
39298a5d
TT
2245
2246 void expand_all_symtabs (struct objfile *objfile) override;
2247
2248 void expand_symtabs_with_fullname (struct objfile *objfile,
2249 const char *fullname) override;
2250
2251 struct compunit_symtab *find_pc_sect_compunit_symtab
2252 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2253 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2254
2255 struct compunit_symtab *find_compunit_symtab_by_address
2256 (struct objfile *objfile, CORE_ADDR address) override
2257 {
2258 return nullptr;
2259 }
2260
2261 void map_symbol_filenames (struct objfile *objfile,
f4655dee
TT
2262 gdb::function_view<symbol_filename_ftype> fun,
2263 bool need_fullname) override;
39298a5d
TT
2264};
2265
2266struct dwarf2_gdb_index : public dwarf2_base_index_functions
2267{
2268 struct compunit_symtab *lookup_symbol (struct objfile *objfile,
2269 block_enum block_index,
2270 const char *name,
2271 domain_enum domain) override;
2272
2273 void dump (struct objfile *objfile) override;
2274
2275 void expand_symtabs_for_function (struct objfile *objfile,
2276 const char *func_name) override;
2277
2278 void map_matching_symbols
2279 (struct objfile *,
2280 const lookup_name_info &lookup_name,
2281 domain_enum domain,
2282 int global,
2283 gdb::function_view<symbol_found_callback_ftype> callback,
2284 symbol_compare_ftype *ordered_compare) override;
2285
2286 void expand_symtabs_matching
2287 (struct objfile *objfile,
2288 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2289 const lookup_name_info *lookup_name,
2290 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2291 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2292 enum search_domain kind) override;
2293};
2294
2295struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2296{
2297 struct compunit_symtab *lookup_symbol (struct objfile *objfile,
2298 block_enum block_index,
2299 const char *name,
2300 domain_enum domain) override;
2301
2302 void dump (struct objfile *objfile) override;
2303
2304 void expand_symtabs_for_function (struct objfile *objfile,
2305 const char *func_name) override;
2306
2307 void map_matching_symbols
2308 (struct objfile *,
2309 const lookup_name_info &lookup_name,
2310 domain_enum domain,
2311 int global,
2312 gdb::function_view<symbol_found_callback_ftype> callback,
2313 symbol_compare_ftype *ordered_compare) override;
2314
2315 void expand_symtabs_matching
2316 (struct objfile *objfile,
2317 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2318 const lookup_name_info *lookup_name,
2319 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2320 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2321 enum search_domain kind) override;
2322};
2323
edc02ceb 2324static quick_symbol_functions_up
39298a5d
TT
2325make_dwarf_gdb_index ()
2326{
2327 return quick_symbol_functions_up (new dwarf2_gdb_index);
2328}
2329
edc02ceb 2330static quick_symbol_functions_up
39298a5d
TT
2331make_dwarf_debug_names ()
2332{
2333 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2334}
2335
094b34ac
DE
2336/* Utility hash function for a stmt_list_hash. */
2337
2338static hashval_t
2339hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2340{
2341 hashval_t v = 0;
2342
2343 if (stmt_list_hash->dwo_unit != NULL)
2344 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2345 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2346 return v;
2347}
2348
2349/* Utility equality function for a stmt_list_hash. */
2350
2351static int
2352eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2353 const struct stmt_list_hash *rhs)
2354{
2355 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2356 return 0;
2357 if (lhs->dwo_unit != NULL
2358 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2359 return 0;
2360
9c541725 2361 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2362}
2363
7b9f3c50
DE
2364/* Hash function for a quick_file_names. */
2365
2366static hashval_t
2367hash_file_name_entry (const void *e)
2368{
9a3c8263
SM
2369 const struct quick_file_names *file_data
2370 = (const struct quick_file_names *) e;
7b9f3c50 2371
094b34ac 2372 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2373}
2374
2375/* Equality function for a quick_file_names. */
2376
2377static int
2378eq_file_name_entry (const void *a, const void *b)
2379{
9a3c8263
SM
2380 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2381 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2382
094b34ac 2383 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2384}
2385
2386/* Delete function for a quick_file_names. */
2387
2388static void
2389delete_file_name_entry (void *e)
2390{
9a3c8263 2391 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2392 int i;
2393
2394 for (i = 0; i < file_data->num_file_names; ++i)
2395 {
2396 xfree ((void*) file_data->file_names[i]);
2397 if (file_data->real_names)
2398 xfree ((void*) file_data->real_names[i]);
2399 }
2400
45940949
TT
2401 /* The space for the struct itself lives on the obstack, so we don't
2402 free it here. */
7b9f3c50
DE
2403}
2404
2405/* Create a quick_file_names hash table. */
2406
5895093f 2407static htab_up
7b9f3c50
DE
2408create_quick_file_names_table (unsigned int nr_initial_entries)
2409{
5895093f
TT
2410 return htab_up (htab_create_alloc (nr_initial_entries,
2411 hash_file_name_entry, eq_file_name_entry,
2412 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2413}
9291a0cd 2414
ab432490
SM
2415/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2416 function is unrelated to symtabs, symtab would have to be created afterwards.
2417 You should call age_cached_comp_units after processing the CU. */
918dd910 2418
1b555f17 2419static dwarf2_cu *
ab432490
SM
2420load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2421 bool skip_partial)
918dd910 2422{
3019eac3 2423 if (per_cu->is_debug_types)
ab432490 2424 load_full_type_unit (per_cu, per_objfile);
918dd910 2425 else
4a636814
SM
2426 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2427 skip_partial, language_minimal);
918dd910 2428
7188ed02
SM
2429 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2430 if (cu == nullptr)
1b555f17 2431 return nullptr; /* Dummy CU. */
2dc860c0 2432
7188ed02 2433 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2434
7188ed02 2435 return cu;
918dd910
JK
2436}
2437
1350c3b4 2438/* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2fdf6df6 2439
9291a0cd 2440static void
97a1449a 2441dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2442 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2443{
f4dc4d17
DE
2444 /* Skip type_unit_groups, reading the type units they contain
2445 is handled elsewhere. */
197400e8 2446 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2447 return;
2448
08ac5771
SM
2449 {
2450 /* The destructor of dwarf2_queue_guard frees any entries left on
2451 the queue. After this point we're guaranteed to leave this function
2452 with the dwarf queue empty. */
2453 dwarf2_queue_guard q_guard (per_objfile);
89e63ee4 2454
08ac5771
SM
2455 if (!per_objfile->symtab_set_p (per_cu))
2456 {
2457 queue_comp_unit (per_cu, per_objfile, language_minimal);
2458 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2459
2460 /* If we just loaded a CU from a DWO, and we're working with an index
2461 that may badly handle TUs, load all the TUs in that DWO as well.
2462 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2463 if (!per_cu->is_debug_types
2464 && cu != NULL
2465 && cu->dwo_unit != NULL
2466 && per_objfile->per_bfd->index_table != NULL
2467 && per_objfile->per_bfd->index_table->version <= 7
2468 /* DWP files aren't supported yet. */
2469 && get_dwp_file (per_objfile) == NULL)
2470 queue_and_load_all_dwo_tus (cu);
2471 }
9291a0cd 2472
08ac5771
SM
2473 process_queue (per_objfile);
2474 }
9291a0cd
TT
2475
2476 /* Age the cache, releasing compilation units that have not
2477 been used recently. */
976ca316 2478 per_objfile->age_comp_units ();
9291a0cd
TT
2479}
2480
97a1449a
SM
2481/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2482 the per-objfile for which this symtab is instantiated.
2483
2484 Returns the resulting symbol table. */
2fdf6df6 2485
43f3e411 2486static struct compunit_symtab *
97a1449a 2487dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2488 dwarf2_per_objfile *per_objfile,
97a1449a 2489 bool skip_partial)
9291a0cd 2490{
976ca316 2491 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2492
976ca316 2493 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2494 {
976ca316 2495 free_cached_comp_units freer (per_objfile);
c83dd867 2496 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2497 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2498 process_cu_includes (per_objfile);
9291a0cd 2499 }
f194fefb 2500
976ca316 2501 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2502}
2503
ff4c9fec 2504/* See declaration. */
f4dc4d17 2505
ff4c9fec 2506dwarf2_per_cu_data *
5989a64e 2507dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2508{
b76e467d 2509 if (index >= this->all_comp_units.size ())
ff4c9fec 2510 {
b76e467d 2511 index -= this->all_comp_units.size ();
b2bdb8cf 2512 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2513 return &this->all_type_units[index]->per_cu;
2514 }
f4dc4d17 2515
ff4c9fec
SM
2516 return this->all_comp_units[index];
2517}
f4dc4d17 2518
ff4c9fec 2519/* See declaration. */
2fdf6df6 2520
ff4c9fec 2521dwarf2_per_cu_data *
5989a64e 2522dwarf2_per_bfd::get_cu (int index)
1fd400ff 2523{
b76e467d 2524 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2525
ff4c9fec 2526 return this->all_comp_units[index];
f4dc4d17
DE
2527}
2528
ff4c9fec 2529/* See declaration. */
f4dc4d17 2530
ff4c9fec 2531signatured_type *
5989a64e 2532dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2533{
b2bdb8cf 2534 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2535
ff4c9fec 2536 return this->all_type_units[index];
1fd400ff
TT
2537}
2538
d3473f0c
TT
2539/* See read.h. */
2540
2541dwarf2_per_cu_data *
5989a64e 2542dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2543{
2544 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2545 result->per_bfd = this;
d3473f0c
TT
2546 result->index = m_num_psymtabs++;
2547 return result;
2548}
2549
2550/* See read.h. */
2551
2552signatured_type *
5989a64e 2553dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2554{
2555 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2556 result->per_cu.per_bfd = this;
d3473f0c
TT
2557 result->per_cu.index = m_num_psymtabs++;
2558 return result;
2559}
2560
168c9250 2561/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2562 obstack, and constructed with the specified field values. */
4b514bc8
JK
2563
2564static dwarf2_per_cu_data *
168c9250
SM
2565create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2566 struct dwarf2_section_info *section,
2567 int is_dwz,
2568 sect_offset sect_off, ULONGEST length)
4b514bc8 2569{
168c9250 2570 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2571 the_cu->sect_off = sect_off;
2572 the_cu->length = length;
4b514bc8 2573 the_cu->section = section;
168c9250 2574 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2575 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2576 the_cu->is_dwz = is_dwz;
2577 return the_cu;
2578}
2579
2ec9a5e0
TT
2580/* A helper for create_cus_from_index that handles a given list of
2581 CUs. */
2fdf6df6 2582
74a0d9f6 2583static void
168c9250 2584create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2585 const gdb_byte *cu_list, offset_type n_elements,
2586 struct dwarf2_section_info *section,
b76e467d 2587 int is_dwz)
9291a0cd 2588{
12359b5e 2589 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2590 {
74a0d9f6 2591 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2592
2593 sect_offset sect_off
2594 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2595 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2596 cu_list += 2 * 8;
2597
b76e467d 2598 dwarf2_per_cu_data *per_cu
168c9250
SM
2599 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2600 length);
2601 per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2602 }
9291a0cd
TT
2603}
2604
2ec9a5e0 2605/* Read the CU list from the mapped index, and use it to create all
168c9250 2606 the CU objects for PER_BFD. */
2ec9a5e0 2607
74a0d9f6 2608static void
168c9250 2609create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2610 const gdb_byte *cu_list, offset_type cu_list_elements,
2611 const gdb_byte *dwz_list, offset_type dwz_elements)
2612{
168c9250
SM
2613 gdb_assert (per_bfd->all_comp_units.empty ());
2614 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2615
168c9250
SM
2616 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2617 &per_bfd->info, 0);
2ec9a5e0
TT
2618
2619 if (dwz_elements == 0)
74a0d9f6 2620 return;
2ec9a5e0 2621
168c9250
SM
2622 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2623 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2624 &dwz->info, 1);
2ec9a5e0
TT
2625}
2626
1fd400ff 2627/* Create the signatured type hash table from the index. */
673bfd45 2628
74a0d9f6 2629static void
12359b5e 2630create_signatured_type_table_from_index
168c9250
SM
2631 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2632 const gdb_byte *bytes, offset_type elements)
1fd400ff 2633{
168c9250
SM
2634 gdb_assert (per_bfd->all_type_units.empty ());
2635 per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2636
298e9637 2637 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2638
12359b5e 2639 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2640 {
52dc124a 2641 struct signatured_type *sig_type;
9c541725 2642 ULONGEST signature;
1fd400ff 2643 void **slot;
9c541725 2644 cu_offset type_offset_in_tu;
1fd400ff 2645
74a0d9f6 2646 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2647 sect_offset sect_off
2648 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2649 type_offset_in_tu
2650 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2651 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2652 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2653 bytes += 3 * 8;
2654
168c9250 2655 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2656 sig_type->signature = signature;
9c541725 2657 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2658 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2659 sig_type->per_cu.section = section;
9c541725 2660 sig_type->per_cu.sect_off = sect_off;
52dc124a 2661 sig_type->per_cu.v.quick
168c9250 2662 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2663 struct dwarf2_per_cu_quick_data);
2664
b0b6a987 2665 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2666 *slot = sig_type;
1fd400ff 2667
168c9250 2668 per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2669 }
2670
168c9250 2671 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2672}
2673
927aa2e7
JK
2674/* Create the signatured type hash table from .debug_names. */
2675
2676static void
2677create_signatured_type_table_from_debug_names
976ca316 2678 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2679 const mapped_debug_names &map,
2680 struct dwarf2_section_info *section,
2681 struct dwarf2_section_info *abbrev_section)
2682{
976ca316 2683 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2684
96b79293
TT
2685 section->read (objfile);
2686 abbrev_section->read (objfile);
927aa2e7 2687
976ca316
SM
2688 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2689 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2690
298e9637 2691 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2692
2693 for (uint32_t i = 0; i < map.tu_count; ++i)
2694 {
2695 struct signatured_type *sig_type;
927aa2e7 2696 void **slot;
927aa2e7
JK
2697
2698 sect_offset sect_off
2699 = (sect_offset) (extract_unsigned_integer
2700 (map.tu_table_reordered + i * map.offset_size,
2701 map.offset_size,
2702 map.dwarf5_byte_order));
2703
2704 comp_unit_head cu_header;
976ca316 2705 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2706 abbrev_section,
927aa2e7
JK
2707 section->buffer + to_underlying (sect_off),
2708 rcuh_kind::TYPE);
2709
976ca316 2710 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2711 sig_type->signature = cu_header.signature;
2712 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2713 sig_type->per_cu.is_debug_types = 1;
2714 sig_type->per_cu.section = section;
2715 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2716 sig_type->per_cu.v.quick
976ca316 2717 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2718 struct dwarf2_per_cu_quick_data);
2719
b0b6a987 2720 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2721 *slot = sig_type;
2722
976ca316 2723 per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2724 }
2725
976ca316 2726 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2727}
2728
9291a0cd 2729/* Read the address map data from the mapped index, and use it to
efd7398e 2730 populate the psymtabs_addrmap. */
2fdf6df6 2731
9291a0cd 2732static void
976ca316 2733create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2734 struct mapped_index *index)
9291a0cd 2735{
976ca316 2736 struct objfile *objfile = per_objfile->objfile;
efd7398e 2737 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
08feed99 2738 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2739 const gdb_byte *iter, *end;
9291a0cd 2740 struct addrmap *mutable_map;
9291a0cd
TT
2741 CORE_ADDR baseaddr;
2742
8268c778
PA
2743 auto_obstack temp_obstack;
2744
9291a0cd
TT
2745 mutable_map = addrmap_create_mutable (&temp_obstack);
2746
f00a2de2
PA
2747 iter = index->address_table.data ();
2748 end = iter + index->address_table.size ();
9291a0cd 2749
b3b3bada 2750 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2751
2752 while (iter < end)
2753 {
2754 ULONGEST hi, lo, cu_index;
2755 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2756 iter += 8;
2757 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2758 iter += 8;
2759 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2760 iter += 4;
f652bce2 2761
24a55014 2762 if (lo > hi)
f652bce2 2763 {
b98664d3 2764 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2765 hex_string (lo), hex_string (hi));
24a55014 2766 continue;
f652bce2 2767 }
24a55014 2768
efd7398e 2769 if (cu_index >= per_bfd->all_comp_units.size ())
f652bce2 2770 {
b98664d3 2771 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2772 (unsigned) cu_index);
24a55014 2773 continue;
f652bce2 2774 }
24a55014 2775
79748972
TT
2776 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2777 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2778 addrmap_set_empty (mutable_map, lo, hi - 1,
efd7398e 2779 per_bfd->get_cu (cu_index));
9291a0cd
TT
2780 }
2781
efd7398e
TT
2782 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2783 &per_bfd->obstack);
9291a0cd
TT
2784}
2785
927aa2e7 2786/* Read the address map data from DWARF-5 .debug_aranges, and use it to
efd7398e 2787 populate the psymtabs_addrmap. */
927aa2e7
JK
2788
2789static void
976ca316 2790create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2791 struct dwarf2_section_info *section)
2792{
976ca316 2793 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2794 bfd *abfd = objfile->obfd;
08feed99 2795 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2796 const CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e 2797 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
2798
2799 auto_obstack temp_obstack;
2800 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2801
2802 std::unordered_map<sect_offset,
2803 dwarf2_per_cu_data *,
2804 gdb::hash_enum<sect_offset>>
2805 debug_info_offset_to_per_cu;
bab287cd 2806 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
927aa2e7 2807 {
927aa2e7
JK
2808 const auto insertpair
2809 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2810 if (!insertpair.second)
2811 {
2812 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2813 "debug_info_offset %s, ignoring .debug_aranges."),
2814 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2815 return;
2816 }
2817 }
2818
96b79293 2819 section->read (objfile);
927aa2e7
JK
2820
2821 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2822
2823 const gdb_byte *addr = section->buffer;
2824
2825 while (addr < section->buffer + section->size)
2826 {
2827 const gdb_byte *const entry_addr = addr;
2828 unsigned int bytes_read;
2829
2830 const LONGEST entry_length = read_initial_length (abfd, addr,
2831 &bytes_read);
2832 addr += bytes_read;
2833
2834 const gdb_byte *const entry_end = addr + entry_length;
2835 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2836 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2837 if (addr + entry_length > section->buffer + section->size)
2838 {
47e3f474 2839 warning (_("Section .debug_aranges in %s entry at offset %s "
dda83cd7 2840 "length %s exceeds section length %s, "
927aa2e7 2841 "ignoring .debug_aranges."),
47e3f474
TV
2842 objfile_name (objfile),
2843 plongest (entry_addr - section->buffer),
927aa2e7
JK
2844 plongest (bytes_read + entry_length),
2845 pulongest (section->size));
2846 return;
2847 }
2848
2849 /* The version number. */
2850 const uint16_t version = read_2_bytes (abfd, addr);
2851 addr += 2;
2852 if (version != 2)
2853 {
47e3f474 2854 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2855 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2856 objfile_name (objfile),
2857 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2858 return;
2859 }
2860
2861 const uint64_t debug_info_offset
2862 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2863 addr += offset_size;
2864 const auto per_cu_it
2865 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2866 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2867 {
47e3f474 2868 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2869 "debug_info_offset %s does not exists, "
2870 "ignoring .debug_aranges."),
47e3f474
TV
2871 objfile_name (objfile),
2872 plongest (entry_addr - section->buffer),
927aa2e7
JK
2873 pulongest (debug_info_offset));
2874 return;
2875 }
2876 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2877
2878 const uint8_t address_size = *addr++;
2879 if (address_size < 1 || address_size > 8)
2880 {
47e3f474 2881 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2882 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2883 objfile_name (objfile),
2884 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2885 return;
2886 }
2887
2888 const uint8_t segment_selector_size = *addr++;
2889 if (segment_selector_size != 0)
2890 {
47e3f474 2891 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2892 "segment_selector_size %u is not supported, "
2893 "ignoring .debug_aranges."),
47e3f474
TV
2894 objfile_name (objfile),
2895 plongest (entry_addr - section->buffer),
927aa2e7
JK
2896 segment_selector_size);
2897 return;
2898 }
2899
2900 /* Must pad to an alignment boundary that is twice the address
dda83cd7
SM
2901 size. It is undocumented by the DWARF standard but GCC does
2902 use it. */
927aa2e7
JK
2903 for (size_t padding = ((-(addr - section->buffer))
2904 & (2 * address_size - 1));
dda83cd7 2905 padding > 0; padding--)
927aa2e7
JK
2906 if (*addr++ != 0)
2907 {
47e3f474 2908 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2909 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2910 objfile_name (objfile),
2911 plongest (entry_addr - section->buffer));
927aa2e7
JK
2912 return;
2913 }
2914
2915 for (;;)
2916 {
2917 if (addr + 2 * address_size > entry_end)
2918 {
47e3f474 2919 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2920 "address list is not properly terminated, "
2921 "ignoring .debug_aranges."),
47e3f474
TV
2922 objfile_name (objfile),
2923 plongest (entry_addr - section->buffer));
927aa2e7
JK
2924 return;
2925 }
2926 ULONGEST start = extract_unsigned_integer (addr, address_size,
2927 dwarf5_byte_order);
2928 addr += address_size;
2929 ULONGEST length = extract_unsigned_integer (addr, address_size,
2930 dwarf5_byte_order);
2931 addr += address_size;
2932 if (start == 0 && length == 0)
2933 break;
efd7398e 2934 if (start == 0 && !per_bfd->has_section_at_zero)
927aa2e7
JK
2935 {
2936 /* Symbol was eliminated due to a COMDAT group. */
2937 continue;
2938 }
2939 ULONGEST end = start + length;
79748972
TT
2940 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2941 - baseaddr);
2942 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2943 - baseaddr);
927aa2e7
JK
2944 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2945 }
2946 }
2947
efd7398e
TT
2948 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2949 &per_bfd->obstack);
927aa2e7
JK
2950}
2951
9291a0cd
TT
2952/* Find a slot in the mapped index INDEX for the object named NAME.
2953 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2954 constant pool and return true. If NAME cannot be found, return
2955 false. */
2fdf6df6 2956
109483d9 2957static bool
9291a0cd
TT
2958find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2959 offset_type **vec_out)
2960{
0cf03b49 2961 offset_type hash;
9291a0cd 2962 offset_type slot, step;
559a7a62 2963 int (*cmp) (const char *, const char *);
9291a0cd 2964
791afaa2 2965 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2966 if (current_language->la_language == language_cplus
45280282
IB
2967 || current_language->la_language == language_fortran
2968 || current_language->la_language == language_d)
0cf03b49
JK
2969 {
2970 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2971 not contain any. */
a8719064 2972
72998fb3 2973 if (strchr (name, '(') != NULL)
0cf03b49 2974 {
109483d9 2975 without_params = cp_remove_params (name);
0cf03b49 2976
72998fb3 2977 if (without_params != NULL)
791afaa2 2978 name = without_params.get ();
0cf03b49
JK
2979 }
2980 }
2981
559a7a62 2982 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2983 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2984 simulate our NAME being searched is also lowercased. */
2985 hash = mapped_index_string_hash ((index->version == 4
dda83cd7 2986 && case_sensitivity == case_sensitive_off
559a7a62
JK
2987 ? 5 : index->version),
2988 name);
2989
f00a2de2
PA
2990 slot = hash & (index->symbol_table.size () - 1);
2991 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2992 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2993
2994 for (;;)
2995 {
9291a0cd 2996 const char *str;
f00a2de2
PA
2997
2998 const auto &bucket = index->symbol_table[slot];
2999 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3000 return false;
9291a0cd 3001
f00a2de2 3002 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3003 if (!cmp (name, str))
9291a0cd
TT
3004 {
3005 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3006 + MAYBE_SWAP (bucket.vec));
109483d9 3007 return true;
9291a0cd
TT
3008 }
3009
f00a2de2 3010 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3011 }
3012}
3013
4485a1c1
SM
3014/* A helper function that reads the .gdb_index from BUFFER and fills
3015 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3016 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3017 ok to use deprecated sections.
3018
3019 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3020 out parameters that are filled in with information about the CU and
3021 TU lists in the section.
3022
4485a1c1 3023 Returns true if all went well, false otherwise. */
2fdf6df6 3024
d33bc52e 3025static bool
3810f182 3026read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
3027 bool deprecated_ok,
3028 gdb::array_view<const gdb_byte> buffer,
3029 struct mapped_index *map,
3030 const gdb_byte **cu_list,
3031 offset_type *cu_list_elements,
3032 const gdb_byte **types_list,
3033 offset_type *types_list_elements)
3034{
3035 const gdb_byte *addr = &buffer[0];
82430852 3036
9291a0cd 3037 /* Version check. */
4485a1c1 3038 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3039 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3040 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3041 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3042 indices. */
831adc1f 3043 if (version < 4)
481860b3
GB
3044 {
3045 static int warning_printed = 0;
3046 if (!warning_printed)
3047 {
3048 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3049 filename);
481860b3
GB
3050 warning_printed = 1;
3051 }
3052 return 0;
3053 }
3054 /* Index version 4 uses a different hash function than index version
3055 5 and later.
3056
3057 Versions earlier than 6 did not emit psymbols for inlined
3058 functions. Using these files will cause GDB not to be able to
3059 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3060 indices unless the user has done
3061 "set use-deprecated-index-sections on". */
2ec9a5e0 3062 if (version < 6 && !deprecated_ok)
481860b3
GB
3063 {
3064 static int warning_printed = 0;
3065 if (!warning_printed)
3066 {
e615022a
DE
3067 warning (_("\
3068Skipping deprecated .gdb_index section in %s.\n\
3069Do \"set use-deprecated-index-sections on\" before the file is read\n\
3070to use the section anyway."),
2ec9a5e0 3071 filename);
481860b3
GB
3072 warning_printed = 1;
3073 }
3074 return 0;
3075 }
796a7ff8 3076 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3077 of the TU (for symbols coming from TUs),
3078 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3079 Plus gold-generated indices can have duplicate entries for global symbols,
3080 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3081 These are just performance bugs, and we can't distinguish gdb-generated
3082 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3083
481860b3 3084 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3085 longer backward compatible. */
796a7ff8 3086 if (version > 8)
594e8718 3087 return 0;
9291a0cd 3088
559a7a62 3089 map->version = version;
9291a0cd 3090
4485a1c1 3091 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3092
4485a1c1 3093 int i = 0;
2ec9a5e0
TT
3094 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3095 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3096 / 8);
1fd400ff
TT
3097 ++i;
3098
2ec9a5e0
TT
3099 *types_list = addr + MAYBE_SWAP (metadata[i]);
3100 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3101 - MAYBE_SWAP (metadata[i]))
3102 / 8);
987d643c 3103 ++i;
1fd400ff 3104
f00a2de2
PA
3105 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3106 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3107 map->address_table
3108 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3109 ++i;
3110
f00a2de2
PA
3111 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3112 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3113 map->symbol_table
3114 = gdb::array_view<mapped_index::symbol_table_slot>
3115 ((mapped_index::symbol_table_slot *) symbol_table,
3116 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3117
f00a2de2 3118 ++i;
f9d83a0b 3119 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3120
2ec9a5e0
TT
3121 return 1;
3122}
3123
4485a1c1
SM
3124/* Callback types for dwarf2_read_gdb_index. */
3125
3126typedef gdb::function_view
5989a64e 3127 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3128 get_gdb_index_contents_ftype;
3129typedef gdb::function_view
3130 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3131 get_gdb_index_contents_dwz_ftype;
3132
927aa2e7 3133/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3134 elements of all the CUs and return 1. Otherwise, return 0. */
3135
3136static int
4485a1c1 3137dwarf2_read_gdb_index
976ca316 3138 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
3139 get_gdb_index_contents_ftype get_gdb_index_contents,
3140 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3141{
2ec9a5e0
TT
3142 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3143 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3144 struct dwz_file *dwz;
976ca316 3145 struct objfile *objfile = per_objfile->objfile;
f8c41851 3146 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 3147
4485a1c1 3148 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 3149 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
3150
3151 if (main_index_contents.empty ())
3152 return 0;
3153
3063847f 3154 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3155 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3156 use_deprecated_index_sections,
3157 main_index_contents, map.get (), &cu_list,
3158 &cu_list_elements, &types_list,
3159 &types_list_elements))
2ec9a5e0
TT
3160 return 0;
3161
0fefef59 3162 /* Don't use the index if it's empty. */
3063847f 3163 if (map->symbol_table.empty ())
0fefef59
DE
3164 return 0;
3165
2ec9a5e0
TT
3166 /* If there is a .dwz file, read it so we can get its CU list as
3167 well. */
f8c41851 3168 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 3169 if (dwz != NULL)
2ec9a5e0 3170 {
2ec9a5e0
TT
3171 struct mapped_index dwz_map;
3172 const gdb_byte *dwz_types_ignore;
3173 offset_type dwz_types_elements_ignore;
3174
4485a1c1
SM
3175 gdb::array_view<const gdb_byte> dwz_index_content
3176 = get_gdb_index_contents_dwz (objfile, dwz);
3177
3178 if (dwz_index_content.empty ())
3179 return 0;
3180
3810f182 3181 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3182 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3183 &dwz_list, &dwz_list_elements,
3184 &dwz_types_ignore,
3185 &dwz_types_elements_ignore))
2ec9a5e0
TT
3186 {
3187 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3188 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3189 return 0;
3190 }
3191 }
3192
f8c41851
SM
3193 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3194 dwz_list_elements);
1fd400ff 3195
8b70b953
TT
3196 if (types_list_elements)
3197 {
8b70b953
TT
3198 /* We can only handle a single .debug_types when we have an
3199 index. */
f8c41851 3200 if (per_bfd->types.size () != 1)
8b70b953
TT
3201 return 0;
3202
f8c41851 3203 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 3204
f8c41851 3205 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 3206 types_list_elements);
8b70b953 3207 }
9291a0cd 3208
976ca316 3209 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 3210
f8c41851
SM
3211 per_bfd->index_table = std::move (map);
3212 per_bfd->using_index = 1;
3213 per_bfd->quick_file_names_table =
3214 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3215
9291a0cd
TT
3216 return 1;
3217}
3218
dee91e82 3219/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3220
dee91e82
DE
3221static void
3222dw2_get_file_names_reader (const struct die_reader_specs *reader,
3e225074 3223 struct die_info *comp_unit_die)
9291a0cd 3224{
dee91e82 3225 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3226 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 3227 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 3228 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3229 struct attribute *attr;
7b9f3c50
DE
3230 void **slot;
3231 struct quick_file_names *qfn;
9291a0cd 3232
0186c6a7
DE
3233 gdb_assert (! this_cu->is_debug_types);
3234
07261596
TT
3235 /* Our callers never want to match partial units -- instead they
3236 will match the enclosing full CU. */
3237 if (comp_unit_die->tag == DW_TAG_partial_unit)
3238 {
3239 this_cu->v.quick->no_file_data = 1;
3240 return;
3241 }
3242
0186c6a7 3243 lh_cu = this_cu;
7b9f3c50 3244 slot = NULL;
dee91e82 3245
fff8551c 3246 line_header_up lh;
9c541725 3247 sect_offset line_offset {};
fff8551c 3248
dee91e82 3249 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
d4df075e 3250 if (attr != nullptr && attr->form_is_unsigned ())
9291a0cd 3251 {
7b9f3c50
DE
3252 struct quick_file_names find_entry;
3253
d4df075e 3254 line_offset = (sect_offset) attr->as_unsigned ();
7b9f3c50
DE
3255
3256 /* We may have already read in this line header (TU line header sharing).
3257 If we have we're done. */
094b34ac 3258 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3259 find_entry.hash.line_sect_off = line_offset;
976ca316 3260 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3261 &find_entry, INSERT);
3262 if (*slot != NULL)
3263 {
9a3c8263 3264 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3265 return;
7b9f3c50
DE
3266 }
3267
3019eac3 3268 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3269 }
3270 if (lh == NULL)
3271 {
094b34ac 3272 lh_cu->v.quick->no_file_data = 1;
dee91e82 3273 return;
9291a0cd
TT
3274 }
3275
976ca316 3276 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3277 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3278 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3279 gdb_assert (slot != NULL);
3280 *slot = qfn;
9291a0cd 3281
d721ba37 3282 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3283
aa391654
TT
3284 int offset = 0;
3285 if (strcmp (fnd.name, "<unknown>") != 0)
3286 ++offset;
3287
7ba99d21 3288 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3289 qfn->file_names =
976ca316 3290 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 3291 qfn->num_file_names);
aa391654
TT
3292 if (offset != 0)
3293 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3294 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3295 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3296 fnd.comp_dir).release ();
7b9f3c50 3297 qfn->real_names = NULL;
9291a0cd 3298
094b34ac 3299 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3300}
3301
3302/* A helper for the "quick" functions which attempts to read the line
3303 table for THIS_CU. */
3304
3305static struct quick_file_names *
ab432490
SM
3306dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3307 dwarf2_per_objfile *per_objfile)
dee91e82 3308{
0186c6a7
DE
3309 /* This should never be called for TUs. */
3310 gdb_assert (! this_cu->is_debug_types);
3311 /* Nor type unit groups. */
197400e8 3312 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3313
dee91e82
DE
3314 if (this_cu->v.quick->file_names != NULL)
3315 return this_cu->v.quick->file_names;
3316 /* If we know there is no line data, no point in looking again. */
3317 if (this_cu->v.quick->no_file_data)
3318 return NULL;
3319
ab432490 3320 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3321 if (!reader.dummy_p)
eb5dd737 3322 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
dee91e82
DE
3323
3324 if (this_cu->v.quick->no_file_data)
3325 return NULL;
3326 return this_cu->v.quick->file_names;
9291a0cd
TT
3327}
3328
3329/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3330 real path for a given file name from the line table. */
2fdf6df6 3331
9291a0cd 3332static const char *
976ca316 3333dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3334 struct quick_file_names *qfn, int index)
9291a0cd 3335{
7b9f3c50 3336 if (qfn->real_names == NULL)
976ca316 3337 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3338 qfn->num_file_names, const char *);
9291a0cd 3339
7b9f3c50 3340 if (qfn->real_names[index] == NULL)
14278e1f 3341 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3342
7b9f3c50 3343 return qfn->real_names[index];
9291a0cd
TT
3344}
3345
39298a5d
TT
3346struct symtab *
3347dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
9291a0cd 3348{
976ca316
SM
3349 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3350 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3351 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3352
43f3e411
DE
3353 if (cust == NULL)
3354 return NULL;
ed2dc618 3355
43f3e411 3356 return compunit_primary_filetab (cust);
9291a0cd
TT
3357}
3358
7b9f3c50
DE
3359/* Traversal function for dw2_forget_cached_source_info. */
3360
3361static int
3362dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3363{
7b9f3c50 3364 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3365
7b9f3c50 3366 if (file_data->real_names)
9291a0cd 3367 {
7b9f3c50 3368 int i;
9291a0cd 3369
7b9f3c50 3370 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3371 {
7b9f3c50
DE
3372 xfree ((void*) file_data->real_names[i]);
3373 file_data->real_names[i] = NULL;
9291a0cd
TT
3374 }
3375 }
7b9f3c50
DE
3376
3377 return 1;
3378}
3379
39298a5d
TT
3380void
3381dwarf2_base_index_functions::forget_cached_source_info
3382 (struct objfile *objfile)
7b9f3c50 3383{
976ca316 3384 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3385
976ca316 3386 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3387 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3388}
3389
f8eba3c6
TT
3390/* Helper function for dw2_map_symtabs_matching_filename that expands
3391 the symtabs and calls the iterator. */
3392
3393static int
3394dw2_map_expand_apply (struct objfile *objfile,
3395 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3396 const char *name, const char *real_path,
14bc53a8 3397 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3398{
43f3e411 3399 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3400
3401 /* Don't visit already-expanded CUs. */
af758d11
SM
3402 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3403 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3404 return 0;
3405
3406 /* This may expand more than one symtab, and we want to iterate over
3407 all of them. */
97a1449a 3408 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3409
14bc53a8
PA
3410 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3411 last_made, callback);
f8eba3c6
TT
3412}
3413
3414/* Implementation of the map_symtabs_matching_filename method. */
3415
39298a5d
TT
3416bool
3417dwarf2_base_index_functions::map_symtabs_matching_filename
14bc53a8
PA
3418 (struct objfile *objfile, const char *name, const char *real_path,
3419 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3420{
c011a4f4 3421 const char *name_basename = lbasename (name);
976ca316 3422 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 3423
848e3e78
DE
3424 /* The rule is CUs specify all the files, including those used by
3425 any TU, so there's no need to scan TUs here. */
f4dc4d17 3426
976ca316 3427 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3428 {
3d7bb9d9 3429 /* We only need to look at symtabs not already expanded. */
976ca316 3430 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3431 continue;
3432
976ca316 3433 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3434 if (file_data == NULL)
9291a0cd
TT
3435 continue;
3436
b76e467d 3437 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3438 {
7b9f3c50 3439 const char *this_name = file_data->file_names[j];
da235a7c 3440 const char *this_real_name;
9291a0cd 3441
af529f8f 3442 if (compare_filenames_for_search (this_name, name))
9291a0cd 3443 {
f5b95b50 3444 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3445 callback))
3446 return true;
288e77a7 3447 continue;
4aac40c8 3448 }
9291a0cd 3449
c011a4f4
DE
3450 /* Before we invoke realpath, which can get expensive when many
3451 files are involved, do a quick comparison of the basenames. */
3452 if (! basenames_may_differ
3453 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3454 continue;
3455
976ca316 3456 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
da235a7c 3457 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3458 {
da235a7c 3459 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3460 callback))
3461 return true;
288e77a7 3462 continue;
da235a7c 3463 }
9291a0cd 3464
da235a7c
JK
3465 if (real_path != NULL)
3466 {
af529f8f
JK
3467 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3468 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3469 if (this_real_name != NULL
af529f8f 3470 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3471 {
f5b95b50 3472 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3473 callback))
3474 return true;
288e77a7 3475 continue;
9291a0cd
TT
3476 }
3477 }
3478 }
3479 }
3480
14bc53a8 3481 return false;
9291a0cd
TT
3482}
3483
da51c347
DE
3484/* Struct used to manage iterating over all CUs looking for a symbol. */
3485
3486struct dw2_symtab_iterator
9291a0cd 3487{
ed2dc618 3488 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3489 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3490 /* If set, only look for symbols that match that block. Valid values are
3491 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3492 gdb::optional<block_enum> block_index;
da51c347
DE
3493 /* The kind of symbol we're looking for. */
3494 domain_enum domain;
3495 /* The list of CUs from the index entry of the symbol,
3496 or NULL if not found. */
3497 offset_type *vec;
3498 /* The next element in VEC to look at. */
3499 int next;
3500 /* The number of elements in VEC, or zero if there is no match. */
3501 int length;
8943b874
DE
3502 /* Have we seen a global version of the symbol?
3503 If so we can ignore all further global instances.
3504 This is to work around gold/15646, inefficient gold-generated
3505 indices. */
3506 int global_seen;
da51c347 3507};
9291a0cd 3508
e5f3ece2 3509/* Initialize the index symtab iterator ITER, common part. */
2fdf6df6 3510
9291a0cd 3511static void
e5f3ece2
TV
3512dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3513 dwarf2_per_objfile *per_objfile,
3514 gdb::optional<block_enum> block_index,
3515 domain_enum domain)
da51c347 3516{
976ca316 3517 iter->per_objfile = per_objfile;
da51c347
DE
3518 iter->block_index = block_index;
3519 iter->domain = domain;
3520 iter->next = 0;
8943b874 3521 iter->global_seen = 0;
e5f3ece2
TV
3522 iter->vec = NULL;
3523 iter->length = 0;
3524}
da51c347 3525
e5f3ece2
TV
3526/* Initialize the index symtab iterator ITER, const char *NAME variant. */
3527
3528static void
3529dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3530 dwarf2_per_objfile *per_objfile,
3531 gdb::optional<block_enum> block_index,
3532 domain_enum domain,
3533 const char *name)
3534{
3535 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
ed2dc618 3536
e5f3ece2 3537 mapped_index *index = per_objfile->per_bfd->index_table.get ();
ed2dc618 3538 /* index is NULL if OBJF_READNOW. */
e5f3ece2
TV
3539 if (index == NULL)
3540 return;
3541
3542 if (find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347 3543 iter->length = MAYBE_SWAP (*iter->vec);
e5f3ece2
TV
3544}
3545
3546/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3547
3548static void
3549dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3550 dwarf2_per_objfile *per_objfile,
3551 gdb::optional<block_enum> block_index,
3552 domain_enum domain, offset_type namei)
3553{
3554 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3555
3556 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3557 /* index is NULL if OBJF_READNOW. */
3558 if (index == NULL)
3559 return;
3560
3561 gdb_assert (!index->symbol_name_slot_invalid (namei));
3562 const auto &bucket = index->symbol_table[namei];
3563
3564 iter->vec = (offset_type *) (index->constant_pool
3565 + MAYBE_SWAP (bucket.vec));
3566 iter->length = MAYBE_SWAP (*iter->vec);
da51c347
DE
3567}
3568
3569/* Return the next matching CU or NULL if there are no more. */
3570
3571static struct dwarf2_per_cu_data *
3572dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3573{
976ca316 3574 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3575
da51c347
DE
3576 for ( ; iter->next < iter->length; ++iter->next)
3577 {
3578 offset_type cu_index_and_attrs =
3579 MAYBE_SWAP (iter->vec[iter->next + 1]);
3580 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3581 gdb_index_symbol_kind symbol_kind =
3582 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3583 /* Only check the symbol attributes if they're present.
3584 Indices prior to version 7 don't record them,
3585 and indices >= 7 may elide them for certain symbols
3586 (gold does this). */
3587 int attrs_valid =
976ca316 3588 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3589 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3590
3190f0c6 3591 /* Don't crash on bad data. */
976ca316
SM
3592 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3593 + per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3594 {
b98664d3 3595 complaint (_(".gdb_index entry has bad CU index"
976ca316 3596 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3597 continue;
3598 }
3599
976ca316 3600 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3601
da51c347 3602 /* Skip if already read in. */
976ca316 3603 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3604 continue;
3605
8943b874
DE
3606 /* Check static vs global. */
3607 if (attrs_valid)
3608 {
2b79f376
SM
3609 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3610
3611 if (iter->block_index.has_value ())
3612 {
3613 bool want_static = *iter->block_index == STATIC_BLOCK;
3614
3615 if (is_static != want_static)
3616 continue;
3617 }
3618
8943b874 3619 /* Work around gold/15646. */
f030440d
TV
3620 if (!is_static
3621 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3622 {
3623 if (iter->global_seen)
3624 continue;
3625
3626 iter->global_seen = 1;
3627 }
8943b874 3628 }
da51c347
DE
3629
3630 /* Only check the symbol's kind if it has one. */
3631 if (attrs_valid)
3632 {
3633 switch (iter->domain)
3634 {
3635 case VAR_DOMAIN:
3636 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3637 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3638 /* Some types are also in VAR_DOMAIN. */
3639 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3640 continue;
3641 break;
3642 case STRUCT_DOMAIN:
3643 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3644 continue;
3645 break;
3646 case LABEL_DOMAIN:
3647 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3648 continue;
3649 break;
59c35742
AB
3650 case MODULE_DOMAIN:
3651 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3652 continue;
3653 break;
da51c347
DE
3654 default:
3655 break;
3656 }
3657 }
3658
3659 ++iter->next;
3660 return per_cu;
3661 }
3662
3663 return NULL;
3664}
3665
39298a5d
TT
3666struct compunit_symtab *
3667dwarf2_gdb_index::lookup_symbol (struct objfile *objfile,
3668 block_enum block_index,
3669 const char *name, domain_enum domain)
9291a0cd 3670{
43f3e411 3671 struct compunit_symtab *stab_best = NULL;
976ca316 3672 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 3673
b5ec771e
PA
3674 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3675
ed2dc618
SM
3676 struct dw2_symtab_iterator iter;
3677 struct dwarf2_per_cu_data *per_cu;
da51c347 3678
976ca316 3679 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
9291a0cd 3680
ed2dc618
SM
3681 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3682 {
3683 struct symbol *sym, *with_opaque = NULL;
97a1449a 3684 struct compunit_symtab *stab
976ca316 3685 = dw2_instantiate_symtab (per_cu, per_objfile, false);
ed2dc618 3686 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3687 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3688
ed2dc618
SM
3689 sym = block_find_symbol (block, name, domain,
3690 block_find_non_opaque_type_preferred,
3691 &with_opaque);
b2e2f908 3692
ed2dc618
SM
3693 /* Some caution must be observed with overloaded functions
3694 and methods, since the index will not contain any overload
3695 information (but NAME might contain it). */
da51c347 3696
ed2dc618
SM
3697 if (sym != NULL
3698 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3699 return stab;
3700 if (with_opaque != NULL
3701 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3702 stab_best = stab;
da51c347 3703
ed2dc618 3704 /* Keep looking through other CUs. */
9291a0cd 3705 }
9291a0cd 3706
da51c347 3707 return stab_best;
9291a0cd
TT
3708}
3709
39298a5d 3710void
4829711b
TT
3711dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3712 bool print_bcache)
9291a0cd 3713{
4829711b
TT
3714 if (print_bcache)
3715 return;
3716
976ca316
SM
3717 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3718 int total = (per_objfile->per_bfd->all_comp_units.size ()
3719 + per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3720 int count = 0;
9291a0cd 3721
ed2dc618 3722 for (int i = 0; i < total; ++i)
9291a0cd 3723 {
976ca316 3724 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3725
976ca316 3726 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3727 ++count;
3728 }
e4a48d9d 3729 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3730 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3731}
3732
779bd270
DE
3733/* This dumps minimal information about the index.
3734 It is called via "mt print objfiles".
3735 One use is to verify .gdb_index has been loaded by the
3736 gdb.dwarf2/gdb-index.exp testcase. */
3737
39298a5d
TT
3738void
3739dwarf2_gdb_index::dump (struct objfile *objfile)
9291a0cd 3740{
976ca316 3741 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3742
976ca316 3743 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3744 printf_filtered (".gdb_index:");
976ca316 3745 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3746 {
3747 printf_filtered (" version %d\n",
976ca316 3748 per_objfile->per_bfd->index_table->version);
779bd270
DE
3749 }
3750 else
3751 printf_filtered (" faked for \"readnow\"\n");
3752 printf_filtered ("\n");
9291a0cd
TT
3753}
3754
39298a5d
TT
3755void
3756dwarf2_gdb_index::expand_symtabs_for_function (struct objfile *objfile,
3757 const char *func_name)
9291a0cd 3758{
976ca316 3759 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
da51c347 3760
ed2dc618
SM
3761 struct dw2_symtab_iterator iter;
3762 struct dwarf2_per_cu_data *per_cu;
da51c347 3763
976ca316 3764 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3765
ed2dc618 3766 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3767 dw2_instantiate_symtab (per_cu, per_objfile, false);
da51c347 3768
9291a0cd
TT
3769}
3770
39298a5d
TT
3771void
3772dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
9291a0cd 3773{
976ca316
SM
3774 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3775 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3776 + per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3777
ed2dc618 3778 for (int i = 0; i < total_units; ++i)
9291a0cd 3779 {
976ca316 3780 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3781
58f0c718
TT
3782 /* We don't want to directly expand a partial CU, because if we
3783 read it with the wrong language, then assertion failures can
3784 be triggered later on. See PR symtab/23010. So, tell
3785 dw2_instantiate_symtab to skip partial CUs -- any important
3786 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3787 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3788 }
3789}
3790
39298a5d
TT
3791void
3792dwarf2_base_index_functions::expand_symtabs_with_fullname
3793 (struct objfile *objfile, const char *fullname)
9291a0cd 3794{
976ca316 3795 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3796
3797 /* We don't need to consider type units here.
3798 This is only called for examining code, e.g. expand_line_sal.
3799 There can be an order of magnitude (or more) more type units
3800 than comp units, and we avoid them if we can. */
3801
976ca316 3802 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3803 {
3d7bb9d9 3804 /* We only need to look at symtabs not already expanded. */
976ca316 3805 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3806 continue;
3807
976ca316 3808 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3809 if (file_data == NULL)
9291a0cd
TT
3810 continue;
3811
b76e467d 3812 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3813 {
652a8996
JK
3814 const char *this_fullname = file_data->file_names[j];
3815
3816 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3817 {
976ca316 3818 dw2_instantiate_symtab (per_cu, per_objfile, false);
9291a0cd
TT
3819 break;
3820 }
3821 }
3822 }
3823}
3824
9a0bacfb
TV
3825static void
3826dw2_expand_symtabs_matching_symbol
3827 (mapped_index_base &index,
3828 const lookup_name_info &lookup_name_in,
3829 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
3830 gdb::function_view<bool (offset_type)> match_callback,
3831 dwarf2_per_objfile *per_objfile);
9a0bacfb
TV
3832
3833static void
3834dw2_expand_symtabs_matching_one
97a1449a
SM
3835 (dwarf2_per_cu_data *per_cu,
3836 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3837 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3838 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3839
af82f89d
TT
3840void
3841dwarf2_gdb_index::map_matching_symbols
199b4314 3842 (struct objfile *objfile,
b054970d 3843 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3844 int global,
3845 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3846 symbol_compare_ftype *ordered_compare)
9291a0cd 3847{
1aa98955 3848 /* Used for Ada. */
976ca316 3849 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3850
9a0bacfb
TV
3851 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3852
976ca316 3853 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3854 {
976ca316 3855 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3856
9a0bacfb
TV
3857 const char *match_name = name.ada ().lookup_name ().c_str ();
3858 auto matcher = [&] (const char *symname)
3859 {
3860 if (ordered_compare == nullptr)
3861 return true;
3862 return ordered_compare (symname, match_name) == 0;
3863 };
3864
b64f703b 3865 dw2_expand_symtabs_matching_symbol (index, name, matcher,
9a0bacfb
TV
3866 [&] (offset_type namei)
3867 {
3868 struct dw2_symtab_iterator iter;
3869 struct dwarf2_per_cu_data *per_cu;
3870
976ca316 3871 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3872 namei);
9a0bacfb 3873 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3874 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3875 nullptr);
9a0bacfb 3876 return true;
976ca316 3877 }, per_objfile);
9a0bacfb
TV
3878 }
3879 else
3880 {
3881 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3882 proceed assuming all symtabs have been read in. */
3883 }
1aa98955
TV
3884
3885 for (compunit_symtab *cust : objfile->compunits ())
3886 {
3887 const struct block *block;
3888
3889 if (cust == NULL)
3890 continue;
3891 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3892 if (!iterate_over_symbols_terminated (block, name,
3893 domain, callback))
3894 return;
3895 }
9291a0cd
TT
3896}
3897
e1ef7d7a
PA
3898/* Starting from a search name, return the string that finds the upper
3899 bound of all strings that start with SEARCH_NAME in a sorted name
3900 list. Returns the empty string to indicate that the upper bound is
3901 the end of the list. */
3902
3903static std::string
3904make_sort_after_prefix_name (const char *search_name)
3905{
3906 /* When looking to complete "func", we find the upper bound of all
3907 symbols that start with "func" by looking for where we'd insert
3908 the closest string that would follow "func" in lexicographical
3909 order. Usually, that's "func"-with-last-character-incremented,
3910 i.e. "fund". Mind non-ASCII characters, though. Usually those
3911 will be UTF-8 multi-byte sequences, but we can't be certain.
3912 Especially mind the 0xff character, which is a valid character in
3913 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3914 rule out compilers allowing it in identifiers. Note that
3915 conveniently, strcmp/strcasecmp are specified to compare
3916 characters interpreted as unsigned char. So what we do is treat
3917 the whole string as a base 256 number composed of a sequence of
3918 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3919 to 0, and carries 1 to the following more-significant position.
3920 If the very first character in SEARCH_NAME ends up incremented
3921 and carries/overflows, then the upper bound is the end of the
3922 list. The string after the empty string is also the empty
3923 string.
3924
3925 Some examples of this operation:
3926
3927 SEARCH_NAME => "+1" RESULT
3928
3929 "abc" => "abd"
3930 "ab\xff" => "ac"
3931 "\xff" "a" "\xff" => "\xff" "b"
3932 "\xff" => ""
3933 "\xff\xff" => ""
3934 "" => ""
3935
3936 Then, with these symbols for example:
3937
3938 func
3939 func1
3940 fund
3941
3942 completing "func" looks for symbols between "func" and
3943 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3944 which finds "func" and "func1", but not "fund".
3945
3946 And with:
3947
3948 funcÿ (Latin1 'ÿ' [0xff])
3949 funcÿ1
3950 fund
3951
3952 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3953 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3954
3955 And with:
3956
3957 ÿÿ (Latin1 'ÿ' [0xff])
3958 ÿÿ1
3959
3960 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3961 the end of the list.
3962 */
3963 std::string after = search_name;
3964 while (!after.empty () && (unsigned char) after.back () == 0xff)
3965 after.pop_back ();
3966 if (!after.empty ())
3967 after.back () = (unsigned char) after.back () + 1;
3968 return after;
3969}
3970
5c58de74 3971/* See declaration. */
61d96d7e 3972
5c58de74
PA
3973std::pair<std::vector<name_component>::const_iterator,
3974 std::vector<name_component>::const_iterator>
44ed8f3e 3975mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3976 (const lookup_name_info &lookup_name_without_params, language lang,
3977 dwarf2_per_objfile *per_objfile) const
3f563c84 3978{
5c58de74
PA
3979 auto *name_cmp
3980 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3981
3b00ef10 3982 const char *lang_name
e0802d59 3983 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3984
3f563c84
PA
3985 /* Comparison function object for lower_bound that matches against a
3986 given symbol name. */
3987 auto lookup_compare_lower = [&] (const name_component &elem,
3988 const char *name)
3989 {
fcf23d5b 3990 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3991 const char *elem_name = elem_qualified + elem.name_offset;
3992 return name_cmp (elem_name, name) < 0;
3993 };
3994
3995 /* Comparison function object for upper_bound that matches against a
3996 given symbol name. */
3997 auto lookup_compare_upper = [&] (const char *name,
3998 const name_component &elem)
3999 {
fcf23d5b 4000 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
4001 const char *elem_name = elem_qualified + elem.name_offset;
4002 return name_cmp (name, elem_name) < 0;
4003 };
4004
5c58de74
PA
4005 auto begin = this->name_components.begin ();
4006 auto end = this->name_components.end ();
3f563c84
PA
4007
4008 /* Find the lower bound. */
4009 auto lower = [&] ()
4010 {
3b00ef10 4011 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4012 return begin;
4013 else
3b00ef10 4014 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4015 } ();
4016
4017 /* Find the upper bound. */
4018 auto upper = [&] ()
4019 {
5c58de74 4020 if (lookup_name_without_params.completion_mode ())
3f563c84 4021 {
e1ef7d7a
PA
4022 /* In completion mode, we want UPPER to point past all
4023 symbols names that have the same prefix. I.e., with
4024 these symbols, and completing "func":
4025
4026 function << lower bound
4027 function1
4028 other_function << upper bound
4029
4030 We find the upper bound by looking for the insertion
4031 point of "func"-with-last-character-incremented,
4032 i.e. "fund". */
3b00ef10 4033 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4034 if (after.empty ())
3f563c84 4035 return end;
e6b2f5ef
PA
4036 return std::lower_bound (lower, end, after.c_str (),
4037 lookup_compare_lower);
3f563c84
PA
4038 }
4039 else
3b00ef10 4040 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4041 } ();
4042
5c58de74
PA
4043 return {lower, upper};
4044}
4045
4046/* See declaration. */
4047
4048void
fcf23d5b 4049mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
4050{
4051 if (!this->name_components.empty ())
4052 return;
4053
4054 this->name_components_casing = case_sensitivity;
4055 auto *name_cmp
4056 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4057
4058 /* The code below only knows how to break apart components of C++
4059 symbol names (and other languages that use '::' as
3b00ef10 4060 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4061 auto count = this->symbol_name_count ();
4062 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4063 {
44ed8f3e 4064 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4065 continue;
4066
fcf23d5b 4067 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
4068
4069 /* Add each name component to the name component table. */
4070 unsigned int previous_len = 0;
3b00ef10
TT
4071
4072 if (strstr (name, "::") != nullptr)
4073 {
4074 for (unsigned int current_len = cp_find_first_component (name);
4075 name[current_len] != '\0';
4076 current_len += cp_find_first_component (name + current_len))
4077 {
4078 gdb_assert (name[current_len] == ':');
4079 this->name_components.push_back ({previous_len, idx});
4080 /* Skip the '::'. */
4081 current_len += 2;
4082 previous_len = current_len;
4083 }
4084 }
4085 else
5c58de74 4086 {
3b00ef10
TT
4087 /* Handle the Ada encoded (aka mangled) form here. */
4088 for (const char *iter = strstr (name, "__");
4089 iter != nullptr;
4090 iter = strstr (iter, "__"))
4091 {
4092 this->name_components.push_back ({previous_len, idx});
4093 iter += 2;
4094 previous_len = iter - name;
4095 }
5c58de74 4096 }
3b00ef10 4097
5c58de74
PA
4098 this->name_components.push_back ({previous_len, idx});
4099 }
4100
4101 /* Sort name_components elements by name. */
4102 auto name_comp_compare = [&] (const name_component &left,
4103 const name_component &right)
4104 {
fcf23d5b
SM
4105 const char *left_qualified
4106 = this->symbol_name_at (left.idx, per_objfile);
4107 const char *right_qualified
4108 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
4109
4110 const char *left_name = left_qualified + left.name_offset;
4111 const char *right_name = right_qualified + right.name_offset;
4112
4113 return name_cmp (left_name, right_name) < 0;
4114 };
4115
4116 std::sort (this->name_components.begin (),
4117 this->name_components.end (),
4118 name_comp_compare);
4119}
4120
4121/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4122 mapped_index_base instead of the containing objfile. This is split
4123 to a separate function in order to be able to unit test the
4124 name_components matching using a mock mapped_index_base. For each
5c58de74 4125 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4126 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4127
4128static void
4129dw2_expand_symtabs_matching_symbol
44ed8f3e 4130 (mapped_index_base &index,
5c58de74
PA
4131 const lookup_name_info &lookup_name_in,
4132 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
4133 gdb::function_view<bool (offset_type)> match_callback,
4134 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4135{
4136 lookup_name_info lookup_name_without_params
4137 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4138
4139 /* Build the symbol name component sorted vector, if we haven't
4140 yet. */
fcf23d5b 4141 index.build_name_components (per_objfile);
5c58de74 4142
3f563c84
PA
4143 /* The same symbol may appear more than once in the range though.
4144 E.g., if we're looking for symbols that complete "w", and we have
4145 a symbol named "w1::w2", we'll find the two name components for
4146 that same symbol in the range. To be sure we only call the
4147 callback once per symbol, we first collect the symbol name
4148 indexes that matched in a temporary vector and ignore
4149 duplicates. */
4150 std::vector<offset_type> matches;
3f563c84 4151
3b00ef10
TT
4152 struct name_and_matcher
4153 {
4154 symbol_name_matcher_ftype *matcher;
ecc6c606 4155 const char *name;
3b00ef10
TT
4156
4157 bool operator== (const name_and_matcher &other) const
3f563c84 4158 {
ecc6c606 4159 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4160 }
4161 };
4162
4163 /* A vector holding all the different symbol name matchers, for all
4164 languages. */
4165 std::vector<name_and_matcher> matchers;
4166
4167 for (int i = 0; i < nr_languages; i++)
4168 {
4169 enum language lang_e = (enum language) i;
4170
4171 const language_defn *lang = language_def (lang_e);
4172 symbol_name_matcher_ftype *name_matcher
c9debfb9 4173 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 4174
3b00ef10 4175 name_and_matcher key {
dda83cd7 4176 name_matcher,
3b00ef10
TT
4177 lookup_name_without_params.language_lookup_name (lang_e)
4178 };
4179
4180 /* Don't insert the same comparison routine more than once.
4181 Note that we do this linear walk. This is not a problem in
4182 practice because the number of supported languages is
4183 low. */
4184 if (std::find (matchers.begin (), matchers.end (), key)
4185 != matchers.end ())
9291a0cd 4186 continue;
3b00ef10
TT
4187 matchers.push_back (std::move (key));
4188
4189 auto bounds
4190 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 4191 lang_e, per_objfile);
3b00ef10
TT
4192
4193 /* Now for each symbol name in range, check to see if we have a name
4194 match, and if so, call the MATCH_CALLBACK callback. */
4195
4196 for (; bounds.first != bounds.second; ++bounds.first)
4197 {
fcf23d5b
SM
4198 const char *qualified
4199 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
4200
4201 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4202 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4203 continue;
9291a0cd 4204
3b00ef10
TT
4205 matches.push_back (bounds.first->idx);
4206 }
3f563c84
PA
4207 }
4208
4209 std::sort (matches.begin (), matches.end ());
4210
4211 /* Finally call the callback, once per match. */
4212 ULONGEST prev = -1;
4213 for (offset_type idx : matches)
4214 {
4215 if (prev != idx)
4216 {
3b00ef10
TT
4217 if (!match_callback (idx))
4218 break;
3f563c84
PA
4219 prev = idx;
4220 }
4221 }
4222
4223 /* Above we use a type wider than idx's for 'prev', since 0 and
4224 (offset_type)-1 are both possible values. */
4225 static_assert (sizeof (prev) > sizeof (offset_type), "");
4226}
4227
c62446b1
PA
4228#if GDB_SELF_TEST
4229
4230namespace selftests { namespace dw2_expand_symtabs_matching {
4231
a3c5fafd
PA
4232/* A mock .gdb_index/.debug_names-like name index table, enough to
4233 exercise dw2_expand_symtabs_matching_symbol, which works with the
4234 mapped_index_base interface. Builds an index from the symbol list
4235 passed as parameter to the constructor. */
4236class mock_mapped_index : public mapped_index_base
c62446b1
PA
4237{
4238public:
a3c5fafd
PA
4239 mock_mapped_index (gdb::array_view<const char *> symbols)
4240 : m_symbol_table (symbols)
c62446b1
PA
4241 {}
4242
a3c5fafd 4243 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4244
a3c5fafd 4245 /* Return the number of names in the symbol table. */
632e107b 4246 size_t symbol_name_count () const override
c62446b1 4247 {
a3c5fafd 4248 return m_symbol_table.size ();
c62446b1
PA
4249 }
4250
a3c5fafd 4251 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
4252 const char *symbol_name_at
4253 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
4254 {
4255 return m_symbol_table[idx];
4256 }
c62446b1 4257
a3c5fafd
PA
4258private:
4259 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4260};
4261
4262/* Convenience function that converts a NULL pointer to a "<null>"
4263 string, to pass to print routines. */
4264
4265static const char *
4266string_or_null (const char *str)
4267{
4268 return str != NULL ? str : "<null>";
4269}
4270
4271/* Check if a lookup_name_info built from
4272 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4273 index. EXPECTED_LIST is the list of expected matches, in expected
4274 matching order. If no match expected, then an empty list is
4275 specified. Returns true on success. On failure prints a warning
4276 indicating the file:line that failed, and returns false. */
4277
4278static bool
4279check_match (const char *file, int line,
4280 mock_mapped_index &mock_index,
4281 const char *name, symbol_name_match_type match_type,
4282 bool completion_mode,
fcf23d5b
SM
4283 std::initializer_list<const char *> expected_list,
4284 dwarf2_per_objfile *per_objfile)
c62446b1
PA
4285{
4286 lookup_name_info lookup_name (name, match_type, completion_mode);
4287
4288 bool matched = true;
4289
4290 auto mismatch = [&] (const char *expected_str,
4291 const char *got)
4292 {
4293 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4294 "expected=\"%s\", got=\"%s\"\n"),
4295 file, line,
4296 (match_type == symbol_name_match_type::FULL
4297 ? "FULL" : "WILD"),
4298 name, string_or_null (expected_str), string_or_null (got));
4299 matched = false;
4300 };
4301
4302 auto expected_it = expected_list.begin ();
4303 auto expected_end = expected_list.end ();
4304
a3c5fafd 4305 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
b64f703b 4306 nullptr,
c62446b1
PA
4307 [&] (offset_type idx)
4308 {
fcf23d5b 4309 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
4310 const char *expected_str
4311 = expected_it == expected_end ? NULL : *expected_it++;
4312
4313 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4314 mismatch (expected_str, matched_name);
3b00ef10 4315 return true;
fcf23d5b 4316 }, per_objfile);
c62446b1
PA
4317
4318 const char *expected_str
4319 = expected_it == expected_end ? NULL : *expected_it++;
4320 if (expected_str != NULL)
4321 mismatch (expected_str, NULL);
4322
4323 return matched;
4324}
4325
4326/* The symbols added to the mock mapped_index for testing (in
4327 canonical form). */
4328static const char *test_symbols[] = {
4329 "function",
4330 "std::bar",
4331 "std::zfunction",
4332 "std::zfunction2",
4333 "w1::w2",
4334 "ns::foo<char*>",
4335 "ns::foo<int>",
4336 "ns::foo<long>",
a20714ff
PA
4337 "ns2::tmpl<int>::foo2",
4338 "(anonymous namespace)::A::B::C",
c62446b1 4339
e1ef7d7a
PA
4340 /* These are used to check that the increment-last-char in the
4341 matching algorithm for completion doesn't match "t1_fund" when
4342 completing "t1_func". */
4343 "t1_func",
4344 "t1_func1",
4345 "t1_fund",
4346 "t1_fund1",
4347
4348 /* A UTF-8 name with multi-byte sequences to make sure that
4349 cp-name-parser understands this as a single identifier ("função"
4350 is "function" in PT). */
4351 u8"u8função",
4352
4353 /* \377 (0xff) is Latin1 'ÿ'. */
4354 "yfunc\377",
4355
4356 /* \377 (0xff) is Latin1 'ÿ'. */
4357 "\377",
4358 "\377\377123",
4359
c62446b1
PA
4360 /* A name with all sorts of complications. Starts with "z" to make
4361 it easier for the completion tests below. */
4362#define Z_SYM_NAME \
4363 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4364 "::tuple<(anonymous namespace)::ui*, " \
4365 "std::default_delete<(anonymous namespace)::ui>, void>"
4366
4367 Z_SYM_NAME
4368};
4369
a3c5fafd
PA
4370/* Returns true if the mapped_index_base::find_name_component_bounds
4371 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4372 in completion mode. */
5c58de74
PA
4373
4374static bool
a3c5fafd 4375check_find_bounds_finds (mapped_index_base &index,
5c58de74 4376 const char *search_name,
fcf23d5b
SM
4377 gdb::array_view<const char *> expected_syms,
4378 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4379{
4380 lookup_name_info lookup_name (search_name,
4381 symbol_name_match_type::FULL, true);
4382
3b00ef10 4383 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4384 language_cplus,
4385 per_objfile);
5c58de74
PA
4386
4387 size_t distance = std::distance (bounds.first, bounds.second);
4388 if (distance != expected_syms.size ())
4389 return false;
4390
4391 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4392 {
4393 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4394 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4395 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4396 return false;
4397 }
4398
4399 return true;
4400}
4401
4402/* Test the lower-level mapped_index::find_name_component_bounds
4403 method. */
4404
c62446b1 4405static void
5c58de74
PA
4406test_mapped_index_find_name_component_bounds ()
4407{
4408 mock_mapped_index mock_index (test_symbols);
4409
fcf23d5b 4410 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4411
4412 /* Test the lower-level mapped_index::find_name_component_bounds
4413 method in completion mode. */
4414 {
4415 static const char *expected_syms[] = {
4416 "t1_func",
4417 "t1_func1",
5c58de74
PA
4418 };
4419
fcf23d5b
SM
4420 SELF_CHECK (check_find_bounds_finds
4421 (mock_index, "t1_func", expected_syms,
4422 NULL /* per_objfile */));
5c58de74
PA
4423 }
4424
4425 /* Check that the increment-last-char in the name matching algorithm
4426 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4427 {
4428 static const char *expected_syms1[] = {
4429 "\377",
4430 "\377\377123",
4431 };
fcf23d5b
SM
4432 SELF_CHECK (check_find_bounds_finds
4433 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4434
4435 static const char *expected_syms2[] = {
4436 "\377\377123",
4437 };
fcf23d5b
SM
4438 SELF_CHECK (check_find_bounds_finds
4439 (mock_index, "\377\377", expected_syms2,
4440 NULL /* per_objfile */));
5c58de74
PA
4441 }
4442}
4443
4444/* Test dw2_expand_symtabs_matching_symbol. */
4445
4446static void
4447test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4448{
4449 mock_mapped_index mock_index (test_symbols);
4450
4451 /* We let all tests run until the end even if some fails, for debug
4452 convenience. */
4453 bool any_mismatch = false;
4454
4455 /* Create the expected symbols list (an initializer_list). Needed
4456 because lists have commas, and we need to pass them to CHECK,
4457 which is a macro. */
4458#define EXPECT(...) { __VA_ARGS__ }
4459
4460 /* Wrapper for check_match that passes down the current
4461 __FILE__/__LINE__. */
4462#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4463 any_mismatch |= !check_match (__FILE__, __LINE__, \
4464 mock_index, \
4465 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4466 EXPECTED_LIST, NULL)
c62446b1
PA
4467
4468 /* Identity checks. */
4469 for (const char *sym : test_symbols)
4470 {
4471 /* Should be able to match all existing symbols. */
4472 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4473 EXPECT (sym));
4474
4475 /* Should be able to match all existing symbols with
4476 parameters. */
4477 std::string with_params = std::string (sym) + "(int)";
4478 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4479 EXPECT (sym));
4480
4481 /* Should be able to match all existing symbols with
4482 parameters and qualifiers. */
4483 with_params = std::string (sym) + " ( int ) const";
4484 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4485 EXPECT (sym));
4486
4487 /* This should really find sym, but cp-name-parser.y doesn't
4488 know about lvalue/rvalue qualifiers yet. */
4489 with_params = std::string (sym) + " ( int ) &&";
4490 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4491 {});
4492 }
4493
e1ef7d7a
PA
4494 /* Check that the name matching algorithm for completion doesn't get
4495 confused with Latin1 'ÿ' / 0xff. */
4496 {
4497 static const char str[] = "\377";
4498 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4499 EXPECT ("\377", "\377\377123"));
4500 }
4501
4502 /* Check that the increment-last-char in the matching algorithm for
4503 completion doesn't match "t1_fund" when completing "t1_func". */
4504 {
4505 static const char str[] = "t1_func";
4506 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4507 EXPECT ("t1_func", "t1_func1"));
4508 }
4509
c62446b1
PA
4510 /* Check that completion mode works at each prefix of the expected
4511 symbol name. */
4512 {
4513 static const char str[] = "function(int)";
4514 size_t len = strlen (str);
4515 std::string lookup;
4516
4517 for (size_t i = 1; i < len; i++)
4518 {
4519 lookup.assign (str, i);
4520 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4521 EXPECT ("function"));
4522 }
4523 }
4524
4525 /* While "w" is a prefix of both components, the match function
4526 should still only be called once. */
4527 {
4528 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4529 EXPECT ("w1::w2"));
a20714ff
PA
4530 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4531 EXPECT ("w1::w2"));
c62446b1
PA
4532 }
4533
4534 /* Same, with a "complicated" symbol. */
4535 {
4536 static const char str[] = Z_SYM_NAME;
4537 size_t len = strlen (str);
4538 std::string lookup;
4539
4540 for (size_t i = 1; i < len; i++)
4541 {
4542 lookup.assign (str, i);
4543 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4544 EXPECT (Z_SYM_NAME));
4545 }
4546 }
4547
4548 /* In FULL mode, an incomplete symbol doesn't match. */
4549 {
4550 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4551 {});
4552 }
4553
4554 /* A complete symbol with parameters matches any overload, since the
4555 index has no overload info. */
4556 {
4557 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4558 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4559 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4560 EXPECT ("std::zfunction", "std::zfunction2"));
4561 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4562 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4563 }
4564
4565 /* Check that whitespace is ignored appropriately. A symbol with a
4566 template argument list. */
4567 {
4568 static const char expected[] = "ns::foo<int>";
4569 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4570 EXPECT (expected));
a20714ff
PA
4571 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4572 EXPECT (expected));
c62446b1
PA
4573 }
4574
4575 /* Check that whitespace is ignored appropriately. A symbol with a
4576 template argument list that includes a pointer. */
4577 {
4578 static const char expected[] = "ns::foo<char*>";
4579 /* Try both completion and non-completion modes. */
4580 static const bool completion_mode[2] = {false, true};
4581 for (size_t i = 0; i < 2; i++)
4582 {
4583 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4584 completion_mode[i], EXPECT (expected));
a20714ff
PA
4585 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4586 completion_mode[i], EXPECT (expected));
c62446b1
PA
4587
4588 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4589 completion_mode[i], EXPECT (expected));
a20714ff
PA
4590 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4591 completion_mode[i], EXPECT (expected));
c62446b1
PA
4592 }
4593 }
4594
4595 {
4596 /* Check method qualifiers are ignored. */
4597 static const char expected[] = "ns::foo<char*>";
4598 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4599 symbol_name_match_type::FULL, true, EXPECT (expected));
4600 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4601 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4602 CHECK_MATCH ("foo < char * > ( int ) const",
4603 symbol_name_match_type::WILD, true, EXPECT (expected));
4604 CHECK_MATCH ("foo < char * > ( int ) &&",
4605 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4606 }
4607
4608 /* Test lookup names that don't match anything. */
4609 {
a20714ff
PA
4610 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4611 {});
4612
c62446b1
PA
4613 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4614 {});
4615 }
4616
a20714ff
PA
4617 /* Some wild matching tests, exercising "(anonymous namespace)",
4618 which should not be confused with a parameter list. */
4619 {
4620 static const char *syms[] = {
4621 "A::B::C",
4622 "B::C",
4623 "C",
4624 "A :: B :: C ( int )",
4625 "B :: C ( int )",
4626 "C ( int )",
4627 };
4628
4629 for (const char *s : syms)
4630 {
4631 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4632 EXPECT ("(anonymous namespace)::A::B::C"));
4633 }
4634 }
4635
4636 {
4637 static const char expected[] = "ns2::tmpl<int>::foo2";
4638 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4639 EXPECT (expected));
4640 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4641 EXPECT (expected));
4642 }
4643
c62446b1
PA
4644 SELF_CHECK (!any_mismatch);
4645
4646#undef EXPECT
4647#undef CHECK_MATCH
4648}
4649
5c58de74
PA
4650static void
4651run_test ()
4652{
4653 test_mapped_index_find_name_component_bounds ();
4654 test_dw2_expand_symtabs_matching_symbol ();
4655}
4656
c62446b1
PA
4657}} // namespace selftests::dw2_expand_symtabs_matching
4658
4659#endif /* GDB_SELF_TEST */
4660
4b514bc8
JK
4661/* If FILE_MATCHER is NULL or if PER_CU has
4662 dwarf2_per_cu_quick_data::MARK set (see
4663 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4664 EXPANSION_NOTIFY on it. */
4665
4666static void
4667dw2_expand_symtabs_matching_one
97a1449a
SM
4668 (dwarf2_per_cu_data *per_cu,
4669 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4670 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4671 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4672{
4673 if (file_matcher == NULL || per_cu->v.quick->mark)
4674 {
af758d11 4675 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4676
97a1449a
SM
4677 compunit_symtab *symtab
4678 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4679 gdb_assert (symtab != nullptr);
4b514bc8 4680
af758d11
SM
4681 if (expansion_notify != NULL && symtab_was_null)
4682 expansion_notify (symtab);
4b514bc8
JK
4683 }
4684}
4685
3f563c84
PA
4686/* Helper for dw2_expand_matching symtabs. Called on each symbol
4687 matched, to expand corresponding CUs that were marked. IDX is the
4688 index of the symbol name that matched. */
4689
4690static void
4691dw2_expand_marked_cus
976ca316 4692 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4693 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4694 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4695 search_domain kind)
4696{
3f563c84
PA
4697 offset_type *vec, vec_len, vec_idx;
4698 bool global_seen = false;
976ca316 4699 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4700
61920122 4701 vec = (offset_type *) (index.constant_pool
f00a2de2 4702 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4703 vec_len = MAYBE_SWAP (vec[0]);
4704 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4705 {
61920122
PA
4706 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4707 /* This value is only valid for index versions >= 7. */
4708 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4709 gdb_index_symbol_kind symbol_kind =
4710 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4711 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4712 /* Only check the symbol attributes if they're present.
4713 Indices prior to version 7 don't record them,
4714 and indices >= 7 may elide them for certain symbols
4715 (gold does this). */
4716 int attrs_valid =
4717 (index.version >= 7
4718 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4719
4720 /* Work around gold/15646. */
f030440d
TV
4721 if (attrs_valid
4722 && !is_static
4723 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4724 {
f030440d 4725 if (global_seen)
61920122 4726 continue;
f030440d
TV
4727
4728 global_seen = true;
61920122 4729 }
3190f0c6 4730
61920122
PA
4731 /* Only check the symbol's kind if it has one. */
4732 if (attrs_valid)
4733 {
4734 switch (kind)
8943b874 4735 {
61920122
PA
4736 case VARIABLES_DOMAIN:
4737 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4738 continue;
4739 break;
4740 case FUNCTIONS_DOMAIN:
4741 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4742 continue;
61920122
PA
4743 break;
4744 case TYPES_DOMAIN:
4745 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4746 continue;
4747 break;
59c35742
AB
4748 case MODULES_DOMAIN:
4749 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4750 continue;
4751 break;
61920122
PA
4752 default:
4753 break;
8943b874 4754 }
61920122 4755 }
8943b874 4756
61920122 4757 /* Don't crash on bad data. */
976ca316
SM
4758 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4759 + per_objfile->per_bfd->all_type_units.size ()))
61920122 4760 {
b98664d3 4761 complaint (_(".gdb_index entry has bad CU index"
976ca316 4762 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4763 continue;
4764 }
4765
976ca316
SM
4766 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4767 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4b514bc8 4768 expansion_notify);
61920122
PA
4769 }
4770}
4771
4b514bc8
JK
4772/* If FILE_MATCHER is non-NULL, set all the
4773 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4774 that match FILE_MATCHER. */
4775
61920122 4776static void
4b514bc8 4777dw_expand_symtabs_matching_file_matcher
976ca316 4778 (dwarf2_per_objfile *per_objfile,
ed2dc618 4779 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4780{
4b514bc8 4781 if (file_matcher == NULL)
61920122
PA
4782 return;
4783
4b514bc8
JK
4784 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4785 htab_eq_pointer,
4786 NULL, xcalloc, xfree));
4787 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4788 htab_eq_pointer,
4789 NULL, xcalloc, xfree));
61920122 4790
4b514bc8
JK
4791 /* The rule is CUs specify all the files, including those used by
4792 any TU, so there's no need to scan TUs here. */
61920122 4793
976ca316 4794 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4795 {
927aa2e7
JK
4796 QUIT;
4797
4798 per_cu->v.quick->mark = 0;
4799
4800 /* We only need to look at symtabs not already expanded. */
976ca316 4801 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4802 continue;
4803
976ca316 4804 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4805 if (file_data == NULL)
4806 continue;
4807
4808 if (htab_find (visited_not_found.get (), file_data) != NULL)
4809 continue;
4810 else if (htab_find (visited_found.get (), file_data) != NULL)
4811 {
4812 per_cu->v.quick->mark = 1;
4813 continue;
4814 }
4815
b76e467d 4816 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4817 {
4818 const char *this_real_name;
4819
4820 if (file_matcher (file_data->file_names[j], false))
4821 {
4822 per_cu->v.quick->mark = 1;
4823 break;
4824 }
4825
4826 /* Before we invoke realpath, which can get expensive when many
4827 files are involved, do a quick comparison of the basenames. */
4828 if (!basenames_may_differ
4829 && !file_matcher (lbasename (file_data->file_names[j]),
4830 true))
4831 continue;
4832
976ca316 4833 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4834 if (file_matcher (this_real_name, false))
4835 {
4836 per_cu->v.quick->mark = 1;
4837 break;
4838 }
4839 }
4840
b76e467d
SM
4841 void **slot = htab_find_slot (per_cu->v.quick->mark
4842 ? visited_found.get ()
4843 : visited_not_found.get (),
4844 file_data, INSERT);
927aa2e7
JK
4845 *slot = file_data;
4846 }
4847}
4848
af82f89d
TT
4849void
4850dwarf2_gdb_index::expand_symtabs_matching
4851 (struct objfile *objfile,
4852 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4853 const lookup_name_info *lookup_name,
4854 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4855 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4856 enum search_domain kind)
927aa2e7 4857{
976ca316 4858 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4859
4860 /* index_table is NULL if OBJF_READNOW. */
976ca316 4861 if (!per_objfile->per_bfd->index_table)
927aa2e7
JK
4862 return;
4863
976ca316 4864 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4865
c1a66c06
TV
4866 if (symbol_matcher == NULL && lookup_name == NULL)
4867 {
976ca316 4868 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4869 {
4870 QUIT;
4871
976ca316 4872 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
97a1449a 4873 file_matcher, expansion_notify);
c1a66c06
TV
4874 }
4875 return;
4876 }
4877
976ca316 4878 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4879
c1a66c06 4880 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7 4881 symbol_matcher,
b64f703b 4882 [&] (offset_type idx)
927aa2e7 4883 {
976ca316
SM
4884 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4885 kind);
3b00ef10 4886 return true;
976ca316 4887 }, per_objfile);
927aa2e7
JK
4888}
4889
4890/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4891 symtab. */
4892
4893static struct compunit_symtab *
4894recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4895 CORE_ADDR pc)
4896{
4897 int i;
4898
4899 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4900 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4901 return cust;
4902
4903 if (cust->includes == NULL)
4904 return NULL;
4905
4906 for (i = 0; cust->includes[i]; ++i)
4907 {
4908 struct compunit_symtab *s = cust->includes[i];
4909
4910 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4911 if (s != NULL)
4912 return s;
4913 }
4914
4915 return NULL;
4916}
4917
39298a5d
TT
4918struct compunit_symtab *
4919dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4920 (struct objfile *objfile,
4921 struct bound_minimal_symbol msymbol,
4922 CORE_ADDR pc,
4923 struct obj_section *section,
4924 int warn_if_readin)
927aa2e7
JK
4925{
4926 struct dwarf2_per_cu_data *data;
4927 struct compunit_symtab *result;
4928
efd7398e
TT
4929 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4930 if (per_objfile->per_bfd->index_addrmap == nullptr)
927aa2e7
JK
4931 return NULL;
4932
b3b3bada 4933 CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e
TT
4934 data = ((struct dwarf2_per_cu_data *)
4935 addrmap_find (per_objfile->per_bfd->index_addrmap,
4936 pc - baseaddr));
927aa2e7
JK
4937 if (!data)
4938 return NULL;
4939
af758d11 4940 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4941 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4942 paddress (objfile->arch (), pc));
927aa2e7 4943
97a1449a
SM
4944 result = recursively_find_pc_sect_compunit_symtab
4945 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4946
927aa2e7
JK
4947 gdb_assert (result != NULL);
4948 return result;
4949}
4950
39298a5d 4951void
f4655dee
TT
4952dwarf2_base_index_functions::map_symbol_filenames
4953 (struct objfile *objfile,
4954 gdb::function_view<symbol_filename_ftype> fun,
4955 bool need_fullname)
927aa2e7 4956{
976ca316 4957 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4958
976ca316 4959 if (!per_objfile->per_bfd->filenames_cache)
927aa2e7 4960 {
976ca316 4961 per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4962
4963 htab_up visited (htab_create_alloc (10,
4964 htab_hash_pointer, htab_eq_pointer,
4965 NULL, xcalloc, xfree));
4966
4967 /* The rule is CUs specify all the files, including those used
4968 by any TU, so there's no need to scan TUs here. We can
4969 ignore file names coming from already-expanded CUs. */
4970
976ca316 4971 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4972 {
976ca316 4973 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4974 {
4975 void **slot = htab_find_slot (visited.get (),
4976 per_cu->v.quick->file_names,
4977 INSERT);
4978
4979 *slot = per_cu->v.quick->file_names;
4980 }
4981 }
4982
976ca316 4983 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4984 {
927aa2e7 4985 /* We only need to look at symtabs not already expanded. */
976ca316 4986 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4987 continue;
4988
ab432490 4989 quick_file_names *file_data
976ca316 4990 = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4991 if (file_data == NULL)
4992 continue;
4993
b76e467d 4994 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4995 if (*slot)
4996 {
4997 /* Already visited. */
4998 continue;
4999 }
5000 *slot = file_data;
5001
5002 for (int j = 0; j < file_data->num_file_names; ++j)
5003 {
5004 const char *filename = file_data->file_names[j];
976ca316 5005 per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
5006 }
5007 }
5008 }
5009
976ca316 5010 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
5011 {
5012 gdb::unique_xmalloc_ptr<char> this_real_name;
5013
5014 if (need_fullname)
5015 this_real_name = gdb_realpath (filename);
f4655dee 5016 fun (filename, this_real_name.get ());
927aa2e7
JK
5017 });
5018}
5019
39298a5d
TT
5020bool
5021dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
927aa2e7 5022{
fae2120b 5023 return true;
927aa2e7
JK
5024}
5025
927aa2e7
JK
5026/* DWARF-5 debug_names reader. */
5027
5028/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5029static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5030
5031/* A helper function that reads the .debug_names section in SECTION
5032 and fills in MAP. FILENAME is the name of the file containing the
5033 section; it is used for error reporting.
5034
5035 Returns true if all went well, false otherwise. */
5036
5037static bool
5038read_debug_names_from_section (struct objfile *objfile,
5039 const char *filename,
5040 struct dwarf2_section_info *section,
5041 mapped_debug_names &map)
5042{
96b79293 5043 if (section->empty ())
927aa2e7
JK
5044 return false;
5045
5046 /* Older elfutils strip versions could keep the section in the main
5047 executable while splitting it for the separate debug info file. */
96b79293 5048 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
5049 return false;
5050
96b79293 5051 section->read (objfile);
927aa2e7 5052
08feed99 5053 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
5054
5055 const gdb_byte *addr = section->buffer;
5056
96b79293 5057 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
5058
5059 unsigned int bytes_read;
5060 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5061 addr += bytes_read;
5062
5063 map.dwarf5_is_dwarf64 = bytes_read != 4;
5064 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5065 if (bytes_read + length != section->size)
5066 {
5067 /* There may be multiple per-CU indices. */
5068 warning (_("Section .debug_names in %s length %s does not match "
5069 "section length %s, ignoring .debug_names."),
5070 filename, plongest (bytes_read + length),
5071 pulongest (section->size));
5072 return false;
5073 }
5074
5075 /* The version number. */
5076 uint16_t version = read_2_bytes (abfd, addr);
5077 addr += 2;
5078 if (version != 5)
5079 {
5080 warning (_("Section .debug_names in %s has unsupported version %d, "
5081 "ignoring .debug_names."),
5082 filename, version);
5083 return false;
5084 }
5085
5086 /* Padding. */
5087 uint16_t padding = read_2_bytes (abfd, addr);
5088 addr += 2;
5089 if (padding != 0)
5090 {
5091 warning (_("Section .debug_names in %s has unsupported padding %d, "
5092 "ignoring .debug_names."),
5093 filename, padding);
5094 return false;
5095 }
5096
5097 /* comp_unit_count - The number of CUs in the CU list. */
5098 map.cu_count = read_4_bytes (abfd, addr);
5099 addr += 4;
5100
5101 /* local_type_unit_count - The number of TUs in the local TU
5102 list. */
5103 map.tu_count = read_4_bytes (abfd, addr);
5104 addr += 4;
5105
5106 /* foreign_type_unit_count - The number of TUs in the foreign TU
5107 list. */
5108 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5109 addr += 4;
5110 if (foreign_tu_count != 0)
5111 {
5112 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5113 "ignoring .debug_names."),
5114 filename, static_cast<unsigned long> (foreign_tu_count));
5115 return false;
5116 }
5117
5118 /* bucket_count - The number of hash buckets in the hash lookup
5119 table. */
5120 map.bucket_count = read_4_bytes (abfd, addr);
5121 addr += 4;
5122
5123 /* name_count - The number of unique names in the index. */
5124 map.name_count = read_4_bytes (abfd, addr);
5125 addr += 4;
5126
5127 /* abbrev_table_size - The size in bytes of the abbreviations
5128 table. */
5129 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5130 addr += 4;
5131
5132 /* augmentation_string_size - The size in bytes of the augmentation
5133 string. This value is rounded up to a multiple of 4. */
5134 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5135 addr += 4;
5136 map.augmentation_is_gdb = ((augmentation_string_size
5137 == sizeof (dwarf5_augmentation))
5138 && memcmp (addr, dwarf5_augmentation,
5139 sizeof (dwarf5_augmentation)) == 0);
5140 augmentation_string_size += (-augmentation_string_size) & 3;
5141 addr += augmentation_string_size;
5142
5143 /* List of CUs */
5144 map.cu_table_reordered = addr;
5145 addr += map.cu_count * map.offset_size;
5146
5147 /* List of Local TUs */
5148 map.tu_table_reordered = addr;
5149 addr += map.tu_count * map.offset_size;
5150
5151 /* Hash Lookup Table */
5152 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5153 addr += map.bucket_count * 4;
5154 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5155 addr += map.name_count * 4;
5156
5157 /* Name Table */
5158 map.name_table_string_offs_reordered = addr;
5159 addr += map.name_count * map.offset_size;
5160 map.name_table_entry_offs_reordered = addr;
5161 addr += map.name_count * map.offset_size;
5162
5163 const gdb_byte *abbrev_table_start = addr;
5164 for (;;)
5165 {
927aa2e7
JK
5166 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5167 addr += bytes_read;
5168 if (index_num == 0)
5169 break;
5170
5171 const auto insertpair
5172 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5173 if (!insertpair.second)
5174 {
5175 warning (_("Section .debug_names in %s has duplicate index %s, "
5176 "ignoring .debug_names."),
5177 filename, pulongest (index_num));
5178 return false;
5179 }
5180 mapped_debug_names::index_val &indexval = insertpair.first->second;
5181 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5182 addr += bytes_read;
5183
5184 for (;;)
5185 {
5186 mapped_debug_names::index_val::attr attr;
5187 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5188 addr += bytes_read;
5189 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5190 addr += bytes_read;
5191 if (attr.form == DW_FORM_implicit_const)
5192 {
5193 attr.implicit_const = read_signed_leb128 (abfd, addr,
5194 &bytes_read);
5195 addr += bytes_read;
5196 }
5197 if (attr.dw_idx == 0 && attr.form == 0)
5198 break;
5199 indexval.attr_vec.push_back (std::move (attr));
5200 }
5201 }
5202 if (addr != abbrev_table_start + abbrev_table_size)
5203 {
5204 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5205 "of size %s vs. written as %u, ignoring .debug_names."),
5206 filename, plongest (addr - abbrev_table_start),
5207 abbrev_table_size);
927aa2e7
JK
5208 return false;
5209 }
5210 map.entry_pool = addr;
5211
5212 return true;
5213}
5214
5215/* A helper for create_cus_from_debug_names that handles the MAP's CU
5216 list. */
5217
5218static void
168c9250 5219create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5220 const mapped_debug_names &map,
5221 dwarf2_section_info &section,
b76e467d 5222 bool is_dwz)
927aa2e7 5223{
3ee6bb11
TV
5224 if (!map.augmentation_is_gdb)
5225 {
fc9a13fb
TV
5226 for (uint32_t i = 0; i < map.cu_count; ++i)
5227 {
5228 sect_offset sect_off
5229 = (sect_offset) (extract_unsigned_integer
5230 (map.cu_table_reordered + i * map.offset_size,
5231 map.offset_size,
5232 map.dwarf5_byte_order));
5233 /* We don't know the length of the CU, because the CU list in a
5234 .debug_names index can be incomplete, so we can't use the start
5235 of the next CU as end of this CU. We create the CUs here with
5236 length 0, and in cutu_reader::cutu_reader we'll fill in the
5237 actual length. */
5238 dwarf2_per_cu_data *per_cu
5239 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5240 sect_off, 0);
5241 per_bfd->all_comp_units.push_back (per_cu);
5242 }
d3b54e63 5243 return;
3ee6bb11
TV
5244 }
5245
927aa2e7
JK
5246 sect_offset sect_off_prev;
5247 for (uint32_t i = 0; i <= map.cu_count; ++i)
5248 {
5249 sect_offset sect_off_next;
5250 if (i < map.cu_count)
5251 {
5252 sect_off_next
5253 = (sect_offset) (extract_unsigned_integer
5254 (map.cu_table_reordered + i * map.offset_size,
5255 map.offset_size,
5256 map.dwarf5_byte_order));
5257 }
5258 else
5259 sect_off_next = (sect_offset) section.size;
5260 if (i >= 1)
5261 {
5262 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5263 dwarf2_per_cu_data *per_cu
168c9250 5264 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5265 sect_off_prev, length);
168c9250 5266 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5267 }
5268 sect_off_prev = sect_off_next;
5269 }
5270}
5271
5272/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5273 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5274
5275static void
168c9250 5276create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5277 const mapped_debug_names &map,
5278 const mapped_debug_names &dwz_map)
5279{
168c9250
SM
5280 gdb_assert (per_bfd->all_comp_units.empty ());
5281 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5282
168c9250 5283 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5284 false /* is_dwz */);
927aa2e7
JK
5285
5286 if (dwz_map.cu_count == 0)
5287 return;
5288
168c9250
SM
5289 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5290 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5291 true /* is_dwz */);
927aa2e7
JK
5292}
5293
5294/* Read .debug_names. If everything went ok, initialize the "quick"
5295 elements of all the CUs and return true. Otherwise, return false. */
5296
5297static bool
976ca316 5298dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 5299{
fcf23d5b
SM
5300 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5301 mapped_debug_names dwz_map;
976ca316 5302 struct objfile *objfile = per_objfile->objfile;
f8c41851 5303 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
5304
5305 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
bab287cd 5306 &per_bfd->debug_names, *map))
927aa2e7
JK
5307 return false;
5308
5309 /* Don't use the index if it's empty. */
22ca247e 5310 if (map->name_count == 0)
927aa2e7
JK
5311 return false;
5312
5313 /* If there is a .dwz file, read it so we can get its CU list as
5314 well. */
f8c41851 5315 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
5316 if (dwz != NULL)
5317 {
5318 if (!read_debug_names_from_section (objfile,
00f93c44 5319 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5320 &dwz->debug_names, dwz_map))
5321 {
5322 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5323 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5324 return false;
5325 }
5326 }
5327
f8c41851 5328 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 5329
22ca247e 5330 if (map->tu_count != 0)
927aa2e7
JK
5331 {
5332 /* We can only handle a single .debug_types when we have an
5333 index. */
f8c41851 5334 if (per_bfd->types.size () != 1)
927aa2e7
JK
5335 return false;
5336
f8c41851 5337 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
5338
5339 create_signatured_type_table_from_debug_names
f8c41851 5340 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
5341 }
5342
f8c41851 5343 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 5344
f8c41851
SM
5345 per_bfd->debug_names_table = std::move (map);
5346 per_bfd->using_index = 1;
5347 per_bfd->quick_file_names_table =
bab287cd 5348 create_quick_file_names_table (per_bfd->all_comp_units.size ());
927aa2e7
JK
5349
5350 return true;
5351}
5352
927aa2e7
JK
5353/* Type used to manage iterating over all CUs looking for a symbol for
5354 .debug_names. */
5355
5356class dw2_debug_names_iterator
5357{
5358public:
927aa2e7 5359 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5360 gdb::optional<block_enum> block_index,
5361 domain_enum domain,
fcf23d5b 5362 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5363 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5364 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5365 m_per_objfile (per_objfile)
927aa2e7
JK
5366 {}
5367
5368 dw2_debug_names_iterator (const mapped_debug_names &map,
fcf23d5b 5369 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5370 : m_map (map),
5371 m_search (search),
fcf23d5b
SM
5372 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5373 m_per_objfile (per_objfile)
927aa2e7
JK
5374 {}
5375
3b00ef10
TT
5376 dw2_debug_names_iterator (const mapped_debug_names &map,
5377 block_enum block_index, domain_enum domain,
fcf23d5b 5378 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5379 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5380 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5381 m_per_objfile (per_objfile)
3b00ef10
TT
5382 {}
5383
927aa2e7
JK
5384 /* Return the next matching CU or NULL if there are no more. */
5385 dwarf2_per_cu_data *next ();
5386
5387private:
5388 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5389 const char *name,
5390 dwarf2_per_objfile *per_objfile);
927aa2e7 5391 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5392 uint32_t namei,
5393 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5394
5395 /* The internalized form of .debug_names. */
5396 const mapped_debug_names &m_map;
5397
2b79f376
SM
5398 /* If set, only look for symbols that match that block. Valid values are
5399 GLOBAL_BLOCK and STATIC_BLOCK. */
5400 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5401
5402 /* The kind of symbol we're looking for. */
5403 const domain_enum m_domain = UNDEF_DOMAIN;
5404 const search_domain m_search = ALL_DOMAIN;
5405
5406 /* The list of CUs from the index entry of the symbol, or NULL if
5407 not found. */
5408 const gdb_byte *m_addr;
fcf23d5b
SM
5409
5410 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5411};
5412
5413const char *
fcf23d5b 5414mapped_debug_names::namei_to_name
976ca316 5415 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5416{
5417 const ULONGEST namei_string_offs
5418 = extract_unsigned_integer ((name_table_string_offs_reordered
5419 + namei * offset_size),
5420 offset_size,
5421 dwarf5_byte_order);
976ca316 5422 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5423}
5424
5425/* Find a slot in .debug_names for the object named NAME. If NAME is
5426 found, return pointer to its pool data. If NAME cannot be found,
5427 return NULL. */
5428
5429const gdb_byte *
5430dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5431 (const mapped_debug_names &map, const char *name,
5432 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5433{
5434 int (*cmp) (const char *, const char *);
5435
54ee4252 5436 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5437 if (current_language->la_language == language_cplus
5438 || current_language->la_language == language_fortran
5439 || current_language->la_language == language_d)
5440 {
5441 /* NAME is already canonical. Drop any qualifiers as
5442 .debug_names does not contain any. */
5443
5444 if (strchr (name, '(') != NULL)
5445 {
54ee4252 5446 without_params = cp_remove_params (name);
927aa2e7 5447 if (without_params != NULL)
54ee4252 5448 name = without_params.get ();
927aa2e7
JK
5449 }
5450 }
5451
5452 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5453
5454 const uint32_t full_hash = dwarf5_djb_hash (name);
5455 uint32_t namei
5456 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5457 (map.bucket_table_reordered
5458 + (full_hash % map.bucket_count)), 4,
5459 map.dwarf5_byte_order);
5460 if (namei == 0)
5461 return NULL;
5462 --namei;
5463 if (namei >= map.name_count)
5464 {
b98664d3 5465 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5466 "[in module %s]"),
5467 namei, map.name_count,
fcf23d5b 5468 objfile_name (per_objfile->objfile));
927aa2e7
JK
5469 return NULL;
5470 }
5471
5472 for (;;)
5473 {
5474 const uint32_t namei_full_hash
5475 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5476 (map.hash_table_reordered + namei), 4,
5477 map.dwarf5_byte_order);
5478 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5479 return NULL;
5480
5481 if (full_hash == namei_full_hash)
5482 {
fcf23d5b 5483 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5484
5485#if 0 /* An expensive sanity check. */
5486 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5487 {
b98664d3 5488 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5489 "[in module %s]"),
5490 namei, objfile_name (dwarf2_per_objfile->objfile));
5491 return NULL;
5492 }
5493#endif
5494
5495 if (cmp (namei_string, name) == 0)
5496 {
5497 const ULONGEST namei_entry_offs
5498 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5499 + namei * map.offset_size),
5500 map.offset_size, map.dwarf5_byte_order);
5501 return map.entry_pool + namei_entry_offs;
5502 }
5503 }
5504
5505 ++namei;
5506 if (namei >= map.name_count)
5507 return NULL;
5508 }
5509}
5510
5511const gdb_byte *
5512dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5513 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5514{
5515 if (namei >= map.name_count)
5516 {
b98664d3 5517 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5518 "[in module %s]"),
5519 namei, map.name_count,
fcf23d5b 5520 objfile_name (per_objfile->objfile));
927aa2e7
JK
5521 return NULL;
5522 }
5523
5524 const ULONGEST namei_entry_offs
5525 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5526 + namei * map.offset_size),
5527 map.offset_size, map.dwarf5_byte_order);
5528 return map.entry_pool + namei_entry_offs;
5529}
5530
5531/* See dw2_debug_names_iterator. */
5532
5533dwarf2_per_cu_data *
5534dw2_debug_names_iterator::next ()
5535{
5536 if (m_addr == NULL)
5537 return NULL;
5538
fcf23d5b
SM
5539 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5540 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5541 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5542
5543 again:
5544
5545 unsigned int bytes_read;
5546 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5547 m_addr += bytes_read;
5548 if (abbrev == 0)
5549 return NULL;
5550
5551 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5552 if (indexval_it == m_map.abbrev_map.cend ())
5553 {
b98664d3 5554 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5555 "[in module %s]"),
ed2dc618 5556 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5557 return NULL;
5558 }
5559 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5560 enum class symbol_linkage {
5561 unknown,
5562 static_,
5563 extern_,
23c13d42 5564 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5565 dwarf2_per_cu_data *per_cu = NULL;
5566 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5567 {
5568 ULONGEST ull;
5569 switch (attr.form)
5570 {
5571 case DW_FORM_implicit_const:
5572 ull = attr.implicit_const;
5573 break;
5574 case DW_FORM_flag_present:
5575 ull = 1;
5576 break;
5577 case DW_FORM_udata:
5578 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5579 m_addr += bytes_read;
5580 break;
6dc55ce9 5581 case DW_FORM_ref4:
5582 ull = read_4_bytes (abfd, m_addr);
5583 m_addr += 4;
5584 break;
5585 case DW_FORM_ref8:
5586 ull = read_8_bytes (abfd, m_addr);
5587 m_addr += 8;
5588 break;
5589 case DW_FORM_ref_sig8:
5590 ull = read_8_bytes (abfd, m_addr);
5591 m_addr += 8;
5592 break;
927aa2e7 5593 default:
b98664d3 5594 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5595 dwarf_form_name (attr.form),
ed2dc618 5596 objfile_name (objfile));
927aa2e7
JK
5597 return NULL;
5598 }
5599 switch (attr.dw_idx)
5600 {
5601 case DW_IDX_compile_unit:
5602 /* Don't crash on bad data. */
bab287cd 5603 if (ull >= per_bfd->all_comp_units.size ())
927aa2e7 5604 {
b98664d3 5605 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5606 " [in module %s]"),
5607 pulongest (ull),
fcf23d5b 5608 objfile_name (objfile));
927aa2e7
JK
5609 continue;
5610 }
fcf23d5b 5611 per_cu = per_bfd->get_cutu (ull);
927aa2e7 5612 break;
8af5c486
JK
5613 case DW_IDX_type_unit:
5614 /* Don't crash on bad data. */
fcf23d5b 5615 if (ull >= per_bfd->all_type_units.size ())
8af5c486 5616 {
b98664d3 5617 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5618 " [in module %s]"),
5619 pulongest (ull),
fcf23d5b 5620 objfile_name (objfile));
8af5c486
JK
5621 continue;
5622 }
fcf23d5b 5623 per_cu = &per_bfd->get_tu (ull)->per_cu;
8af5c486 5624 break;
6dc55ce9 5625 case DW_IDX_die_offset:
5626 /* In a per-CU index (as opposed to a per-module index), index
5627 entries without CU attribute implicitly refer to the single CU. */
5628 if (per_cu == NULL)
fcf23d5b 5629 per_cu = per_bfd->get_cu (0);
6dc55ce9 5630 break;
927aa2e7
JK
5631 case DW_IDX_GNU_internal:
5632 if (!m_map.augmentation_is_gdb)
5633 break;
23c13d42 5634 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5635 break;
5636 case DW_IDX_GNU_external:
5637 if (!m_map.augmentation_is_gdb)
5638 break;
23c13d42 5639 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5640 break;
5641 }
5642 }
5643
5644 /* Skip if already read in. */
fcf23d5b 5645 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5646 goto again;
5647
5648 /* Check static vs global. */
23c13d42 5649 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5650 {
2b79f376 5651 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5652 const bool symbol_is_static =
5653 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5654 if (want_static != symbol_is_static)
2b79f376 5655 goto again;
927aa2e7
JK
5656 }
5657
5658 /* Match dw2_symtab_iter_next, symbol_kind
5659 and debug_names::psymbol_tag. */
5660 switch (m_domain)
5661 {
5662 case VAR_DOMAIN:
5663 switch (indexval.dwarf_tag)
5664 {
5665 case DW_TAG_variable:
5666 case DW_TAG_subprogram:
5667 /* Some types are also in VAR_DOMAIN. */
5668 case DW_TAG_typedef:
5669 case DW_TAG_structure_type:
5670 break;
5671 default:
5672 goto again;
5673 }
5674 break;
5675 case STRUCT_DOMAIN:
5676 switch (indexval.dwarf_tag)
5677 {
5678 case DW_TAG_typedef:
5679 case DW_TAG_structure_type:
5680 break;
5681 default:
5682 goto again;
5683 }
5684 break;
5685 case LABEL_DOMAIN:
5686 switch (indexval.dwarf_tag)
5687 {
5688 case 0:
5689 case DW_TAG_variable:
5690 break;
5691 default:
5692 goto again;
5693 }
5694 break;
59c35742
AB
5695 case MODULE_DOMAIN:
5696 switch (indexval.dwarf_tag)
5697 {
5698 case DW_TAG_module:
5699 break;
5700 default:
5701 goto again;
5702 }
5703 break;
927aa2e7
JK
5704 default:
5705 break;
5706 }
5707
5708 /* Match dw2_expand_symtabs_matching, symbol_kind and
5709 debug_names::psymbol_tag. */
5710 switch (m_search)
4b514bc8 5711 {
927aa2e7
JK
5712 case VARIABLES_DOMAIN:
5713 switch (indexval.dwarf_tag)
4b514bc8 5714 {
927aa2e7
JK
5715 case DW_TAG_variable:
5716 break;
5717 default:
5718 goto again;
4b514bc8 5719 }
927aa2e7
JK
5720 break;
5721 case FUNCTIONS_DOMAIN:
5722 switch (indexval.dwarf_tag)
4b514bc8 5723 {
927aa2e7
JK
5724 case DW_TAG_subprogram:
5725 break;
5726 default:
5727 goto again;
4b514bc8 5728 }
927aa2e7
JK
5729 break;
5730 case TYPES_DOMAIN:
5731 switch (indexval.dwarf_tag)
5732 {
5733 case DW_TAG_typedef:
5734 case DW_TAG_structure_type:
5735 break;
5736 default:
5737 goto again;
5738 }
5739 break;
59c35742
AB
5740 case MODULES_DOMAIN:
5741 switch (indexval.dwarf_tag)
5742 {
5743 case DW_TAG_module:
5744 break;
5745 default:
5746 goto again;
5747 }
927aa2e7
JK
5748 default:
5749 break;
4b514bc8 5750 }
927aa2e7
JK
5751
5752 return per_cu;
4b514bc8 5753}
61920122 5754
39298a5d
TT
5755struct compunit_symtab *
5756dwarf2_debug_names_index::lookup_symbol
5757 (struct objfile *objfile, block_enum block_index,
5758 const char *name, domain_enum domain)
4b514bc8 5759{
976ca316 5760 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
61920122 5761
976ca316 5762 const auto &mapp = per_objfile->per_bfd->debug_names_table;
927aa2e7 5763 if (!mapp)
61920122 5764 {
927aa2e7
JK
5765 /* index is NULL if OBJF_READNOW. */
5766 return NULL;
5767 }
5768 const auto &map = *mapp;
9291a0cd 5769
976ca316 5770 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
9703b513 5771
927aa2e7
JK
5772 struct compunit_symtab *stab_best = NULL;
5773 struct dwarf2_per_cu_data *per_cu;
5774 while ((per_cu = iter.next ()) != NULL)
5775 {
5776 struct symbol *sym, *with_opaque = NULL;
97a1449a 5777 compunit_symtab *stab
976ca316 5778 = dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7 5779 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5780 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5781
927aa2e7
JK
5782 sym = block_find_symbol (block, name, domain,
5783 block_find_non_opaque_type_preferred,
5784 &with_opaque);
9703b513 5785
927aa2e7
JK
5786 /* Some caution must be observed with overloaded functions and
5787 methods, since the index will not contain any overload
5788 information (but NAME might contain it). */
a3ec0bb1 5789
927aa2e7 5790 if (sym != NULL
987012b8 5791 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5792 return stab;
5793 if (with_opaque != NULL
987012b8 5794 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5795 stab_best = stab;
9703b513 5796
927aa2e7 5797 /* Keep looking through other CUs. */
9703b513
TT
5798 }
5799
927aa2e7 5800 return stab_best;
9703b513
TT
5801}
5802
927aa2e7
JK
5803/* This dumps minimal information about .debug_names. It is called
5804 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5805 uses this to verify that .debug_names has been loaded. */
9291a0cd 5806
39298a5d
TT
5807void
5808dwarf2_debug_names_index::dump (struct objfile *objfile)
927aa2e7 5809{
976ca316 5810 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5811
976ca316 5812 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5813 printf_filtered (".debug_names:");
976ca316 5814 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5815 printf_filtered (" exists\n");
5816 else
5817 printf_filtered (" faked for \"readnow\"\n");
5818 printf_filtered ("\n");
9291a0cd
TT
5819}
5820
39298a5d
TT
5821void
5822dwarf2_debug_names_index::expand_symtabs_for_function
5823 (struct objfile *objfile, const char *func_name)
9291a0cd 5824{
976ca316 5825 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 5826
976ca316
SM
5827 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5828 if (per_objfile->per_bfd->debug_names_table)
24c79950 5829 {
976ca316 5830 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
24c79950 5831
fcf23d5b 5832 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
976ca316 5833 per_objfile);
24c79950 5834
927aa2e7
JK
5835 struct dwarf2_per_cu_data *per_cu;
5836 while ((per_cu = iter.next ()) != NULL)
976ca316 5837 dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7
JK
5838 }
5839}
24c79950 5840
39298a5d
TT
5841void
5842dwarf2_debug_names_index::map_matching_symbols
3b00ef10
TT
5843 (struct objfile *objfile,
5844 const lookup_name_info &name, domain_enum domain,
5845 int global,
5846 gdb::function_view<symbol_found_callback_ftype> callback,
5847 symbol_compare_ftype *ordered_compare)
5848{
976ca316 5849 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5850
5851 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5852 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5853 return;
5854
976ca316 5855 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5856 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5857
5858 const char *match_name = name.ada ().lookup_name ().c_str ();
5859 auto matcher = [&] (const char *symname)
5860 {
5861 if (ordered_compare == nullptr)
5862 return true;
5863 return ordered_compare (symname, match_name) == 0;
5864 };
5865
b64f703b 5866 dw2_expand_symtabs_matching_symbol (map, name, matcher,
3b00ef10
TT
5867 [&] (offset_type namei)
5868 {
5869 /* The name was matched, now expand corresponding CUs that were
5870 marked. */
fcf23d5b 5871 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
976ca316 5872 per_objfile);
3b00ef10
TT
5873
5874 struct dwarf2_per_cu_data *per_cu;
5875 while ((per_cu = iter.next ()) != NULL)
976ca316 5876 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5877 nullptr);
3b00ef10 5878 return true;
976ca316 5879 }, per_objfile);
3b00ef10
TT
5880
5881 /* It's a shame we couldn't do this inside the
5882 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5883 that have already been expanded. Instead, this loop matches what
5884 the psymtab code does. */
976ca316 5885 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3b00ef10 5886 {
976ca316 5887 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
af758d11 5888 if (symtab != nullptr)
3b00ef10
TT
5889 {
5890 const struct block *block
af758d11 5891 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5892 if (!iterate_over_symbols_terminated (block, name,
5893 domain, callback))
5894 break;
5895 }
5896 }
5897}
5898
39298a5d
TT
5899void
5900dwarf2_debug_names_index::expand_symtabs_matching
927aa2e7
JK
5901 (struct objfile *objfile,
5902 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5903 const lookup_name_info *lookup_name,
927aa2e7
JK
5904 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5905 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5906 enum search_domain kind)
5907{
976ca316 5908 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5909
927aa2e7 5910 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5911 if (!per_objfile->per_bfd->debug_names_table)
927aa2e7 5912 return;
9291a0cd 5913
976ca316 5914 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5915
c1a66c06
TV
5916 if (symbol_matcher == NULL && lookup_name == NULL)
5917 {
976ca316 5918 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5919 {
5920 QUIT;
5921
976ca316
SM
5922 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5923 expansion_notify);
c1a66c06
TV
5924 }
5925 return;
5926 }
5927
976ca316 5928 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5929
c1a66c06 5930 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e 5931 symbol_matcher,
b64f703b 5932 [&] (offset_type namei)
927aa2e7 5933 {
927aa2e7
JK
5934 /* The name was matched, now expand corresponding CUs that were
5935 marked. */
976ca316 5936 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
bbf2f4df 5937
927aa2e7
JK
5938 struct dwarf2_per_cu_data *per_cu;
5939 while ((per_cu = iter.next ()) != NULL)
976ca316
SM
5940 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5941 expansion_notify);
3b00ef10 5942 return true;
976ca316 5943 }, per_objfile);
9291a0cd
TT
5944}
5945
4485a1c1 5946/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5947 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5948
5949template <typename T>
5950static gdb::array_view<const gdb_byte>
5951get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5952{
5953 dwarf2_section_info *section = &section_owner->gdb_index;
5954
96b79293 5955 if (section->empty ())
4485a1c1
SM
5956 return {};
5957
5958 /* Older elfutils strip versions could keep the section in the main
5959 executable while splitting it for the separate debug info file. */
96b79293 5960 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5961 return {};
5962
96b79293 5963 section->read (obj);
4485a1c1 5964
8bebfcda
PA
5965 /* dwarf2_section_info::size is a bfd_size_type, while
5966 gdb::array_view works with size_t. On 32-bit hosts, with
5967 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5968 is 32-bit. So we need an explicit narrowing conversion here.
5969 This is fine, because it's impossible to allocate or mmap an
5970 array/buffer larger than what size_t can represent. */
5971 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5972}
5973
87d6a7aa
SM
5974/* Lookup the index cache for the contents of the index associated to
5975 DWARF2_OBJ. */
5976
5977static gdb::array_view<const gdb_byte>
5989a64e 5978get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5979{
5980 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5981 if (build_id == nullptr)
5982 return {};
5983
5984 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5985 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5986}
5987
5988/* Same as the above, but for DWZ. */
5989
5990static gdb::array_view<const gdb_byte>
5991get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5992{
5993 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5994 if (build_id == nullptr)
5995 return {};
5996
5997 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5998}
5999
edc02ceb 6000/* See dwarf2/public.h. */
9291a0cd 6001
edc02ceb
TT
6002void
6003dwarf2_initialize_objfile (struct objfile *objfile)
9291a0cd 6004{
976ca316
SM
6005 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6006 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 6007
850ed749
SM
6008 dwarf_read_debug_printf ("called");
6009
9291a0cd
TT
6010 /* If we're about to read full symbols, don't bother with the
6011 indices. In this case we also don't care if some other debug
6012 format is making psymtabs, because they are all about to be
6013 expanded anyway. */
6014 if ((objfile->flags & OBJF_READNOW))
6015 {
850ed749
SM
6016 dwarf_read_debug_printf ("readnow requested");
6017
17ee85fc
TT
6018 /* When using READNOW, the using_index flag (set below) indicates that
6019 PER_BFD was already initialized, when we loaded some other objfile. */
6020 if (per_bfd->using_index)
6021 {
850ed749 6022 dwarf_read_debug_printf ("using_index already set");
976ca316 6023 per_objfile->resize_symtabs ();
edc02ceb
TT
6024 objfile->qf.push_front (make_dwarf_gdb_index ());
6025 return;
17ee85fc
TT
6026 }
6027
6028 per_bfd->using_index = 1;
976ca316
SM
6029 create_all_comp_units (per_objfile);
6030 create_all_type_units (per_objfile);
17ee85fc
TT
6031 per_bfd->quick_file_names_table
6032 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 6033 per_objfile->resize_symtabs ();
9291a0cd 6034
17ee85fc
TT
6035 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6036 + per_bfd->all_type_units.size ()); ++i)
9291a0cd 6037 {
17ee85fc 6038 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
9291a0cd 6039
17ee85fc 6040 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 6041 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6042 }
6043
edc02ceb
TT
6044 /* Arrange for gdb to see the "quick" functions. However, these
6045 functions will be no-ops because we will have expanded all
6046 symtabs. */
6047 objfile->qf.push_front (make_dwarf_gdb_index ());
6048 return;
9291a0cd
TT
6049 }
6050
17ee85fc
TT
6051 /* Was a debug names index already read when we processed an objfile sharing
6052 PER_BFD? */
6053 if (per_bfd->debug_names_table != nullptr)
6054 {
850ed749 6055 dwarf_read_debug_printf ("re-using shared debug names table");
976ca316 6056 per_objfile->resize_symtabs ();
edc02ceb
TT
6057 objfile->qf.push_front (make_dwarf_debug_names ());
6058 return;
17ee85fc
TT
6059 }
6060
6061 /* Was a GDB index already read when we processed an objfile sharing
6062 PER_BFD? */
6063 if (per_bfd->index_table != nullptr)
6064 {
850ed749 6065 dwarf_read_debug_printf ("re-using shared index table");
976ca316 6066 per_objfile->resize_symtabs ();
edc02ceb
TT
6067 objfile->qf.push_front (make_dwarf_gdb_index ());
6068 return;
17ee85fc
TT
6069 }
6070
efb763a5
SM
6071 /* There might already be partial symtabs built for this BFD. This happens
6072 when loading the same binary twice with the index-cache enabled. If so,
6073 don't try to read an index. The objfile / per_objfile initialization will
6074 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6075 code path. */
6076 if (per_bfd->partial_symtabs != nullptr)
850ed749
SM
6077 {
6078 dwarf_read_debug_printf ("re-using shared partial symtabs");
edc02ceb
TT
6079 objfile->qf.push_front (make_lazy_dwarf_reader ());
6080 return;
850ed749 6081 }
efb763a5 6082
976ca316 6083 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a 6084 {
850ed749 6085 dwarf_read_debug_printf ("found debug names");
976ca316 6086 per_objfile->resize_symtabs ();
edc02ceb
TT
6087 objfile->qf.push_front (make_dwarf_debug_names ());
6088 return;
3c0aa29a 6089 }
927aa2e7 6090
976ca316 6091 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 6092 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 6093 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a 6094 {
850ed749 6095 dwarf_read_debug_printf ("found gdb index from file");
976ca316 6096 per_objfile->resize_symtabs ();
edc02ceb
TT
6097 objfile->qf.push_front (make_dwarf_gdb_index ());
6098 return;
3c0aa29a 6099 }
9291a0cd 6100
87d6a7aa 6101 /* ... otherwise, try to find the index in the index cache. */
976ca316 6102 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
6103 get_gdb_index_contents_from_cache,
6104 get_gdb_index_contents_from_cache_dwz))
6105 {
850ed749 6106 dwarf_read_debug_printf ("found gdb index from cache");
87d6a7aa 6107 global_index_cache.hit ();
976ca316 6108 per_objfile->resize_symtabs ();
edc02ceb
TT
6109 objfile->qf.push_front (make_dwarf_gdb_index ());
6110 return;
87d6a7aa
SM
6111 }
6112
6113 global_index_cache.miss ();
edc02ceb 6114 objfile->qf.push_front (make_lazy_dwarf_reader ());
9291a0cd
TT
6115}
6116
6117\f
6118
dce234bc
PP
6119/* Build a partial symbol table. */
6120
6121void
eb36a3eb 6122dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
dce234bc 6123{
976ca316
SM
6124 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6125 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
6126
6127 if (per_bfd->partial_symtabs != nullptr)
6128 {
6129 /* Partial symbols were already read, so now we can simply
6130 attach them. */
eb36a3eb
TT
6131 if (psf == nullptr)
6132 {
6133 psf = new psymbol_functions (per_bfd->partial_symtabs);
6134 objfile->qf.emplace_front (psf);
6135 }
6136 else
6137 psf->set_partial_symtabs (per_bfd->partial_symtabs);
976ca316 6138 per_objfile->resize_symtabs ();
17ee85fc
TT
6139 return;
6140 }
c9bf0622 6141
eb36a3eb
TT
6142 if (psf == nullptr)
6143 {
6144 psf = new psymbol_functions;
6145 objfile->qf.emplace_front (psf);
6146 }
6147 const std::shared_ptr<psymtab_storage> &partial_symtabs
6148 = psf->get_partial_symtabs ();
6149
51962708
TT
6150 /* Set the local reference to partial symtabs, so that we don't try
6151 to read them again if reading another objfile with the same BFD.
6152 If we can't in fact share, this won't make a difference anyway as
6153 the dwarf2_per_bfd object won't be shared. */
eb36a3eb 6154 per_bfd->partial_symtabs = partial_symtabs;
51962708 6155
a70b8144 6156 try
c9bf0622
TT
6157 {
6158 /* This isn't really ideal: all the data we allocate on the
6159 objfile's obstack is still uselessly kept around. However,
6160 freeing it seems unsafe. */
eb36a3eb 6161 psymtab_discarder psymtabs (partial_symtabs.get ());
976ca316 6162 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 6163 psymtabs.keep ();
87d6a7aa 6164
976ca316 6165 per_objfile->resize_symtabs ();
af758d11 6166
87d6a7aa 6167 /* (maybe) store an index in the cache. */
976ca316 6168 global_index_cache.store (per_objfile);
c9bf0622 6169 }
230d2906 6170 catch (const gdb_exception_error &except)
492d29ea
PA
6171 {
6172 exception_print (gdb_stderr, except);
6173 }
c906108c 6174}
c906108c 6175
3b80fe9b
DE
6176/* Find the base address of the compilation unit for range lists and
6177 location lists. It will normally be specified by DW_AT_low_pc.
6178 In DWARF-3 draft 4, the base address could be overridden by
6179 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6180 compilation units with discontinuous ranges. */
6181
6182static void
6183dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6184{
6185 struct attribute *attr;
6186
2b24b6e4 6187 cu->base_address.reset ();
3b80fe9b
DE
6188
6189 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6190 if (attr != nullptr)
95f982e5 6191 cu->base_address = attr->as_address ();
3b80fe9b
DE
6192 else
6193 {
6194 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6195 if (attr != nullptr)
95f982e5 6196 cu->base_address = attr->as_address ();
3b80fe9b
DE
6197 }
6198}
6199
36586728
TT
6200/* Helper function that returns the proper abbrev section for
6201 THIS_CU. */
6202
6203static struct dwarf2_section_info *
6204get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6205{
6206 struct dwarf2_section_info *abbrev;
c3699833 6207 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6208
6209 if (this_cu->is_dwz)
a7308ce0 6210 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
36586728 6211 else
c3699833 6212 abbrev = &per_bfd->abbrev;
36586728
TT
6213
6214 return abbrev;
6215}
6216
f4dc4d17
DE
6217/* Fetch the abbreviation table offset from a comp or type unit header. */
6218
6219static sect_offset
976ca316 6220read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 6221 struct dwarf2_section_info *section,
9c541725 6222 sect_offset sect_off)
f4dc4d17 6223{
96b79293 6224 bfd *abfd = section->get_bfd_owner ();
d521ce57 6225 const gdb_byte *info_ptr;
ac298888 6226 unsigned int initial_length_size, offset_size;
43988095 6227 uint16_t version;
f4dc4d17 6228
976ca316 6229 section->read (per_objfile->objfile);
9c541725 6230 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6231 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6232 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6233 info_ptr += initial_length_size;
6234
6235 version = read_2_bytes (abfd, info_ptr);
6236 info_ptr += 2;
6237 if (version >= 5)
6238 {
6239 /* Skip unit type and address size. */
6240 info_ptr += 2;
6241 }
6242
24aa364d 6243 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6244}
6245
b83470bf
TT
6246/* A partial symtab that is used only for include files. */
6247struct dwarf2_include_psymtab : public partial_symtab
6248{
7b249e47
TT
6249 dwarf2_include_psymtab (const char *filename,
6250 psymtab_storage *partial_symtabs,
0072c873
SM
6251 objfile_per_bfd_storage *objfile_per_bfd)
6252 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
b83470bf
TT
6253 {
6254 }
6255
6256 void read_symtab (struct objfile *objfile) override
6257 {
194d088f
TV
6258 /* It's an include file, no symbols to read for it.
6259 Everything is in the includer symtab. */
6260
6261 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6262 expansion of the includer psymtab. We use the dependencies[0] field to
6263 model the includer. But if we go the regular route of calling
6264 expand_psymtab here, and having expand_psymtab call expand_dependencies
6265 to expand the includer, we'll only use expand_psymtab on the includer
6266 (making it a non-toplevel psymtab), while if we expand the includer via
6267 another path, we'll use read_symtab (making it a toplevel psymtab).
6268 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6269 psymtab, and trigger read_symtab on the includer here directly. */
6270 includer ()->read_symtab (objfile);
b83470bf
TT
6271 }
6272
6273 void expand_psymtab (struct objfile *objfile) override
6274 {
194d088f
TV
6275 /* This is not called by read_symtab, and should not be called by any
6276 expand_dependencies. */
6277 gdb_assert (false);
b83470bf
TT
6278 }
6279
5717c425 6280 bool readin_p (struct objfile *objfile) const override
b83470bf 6281 {
5717c425 6282 return includer ()->readin_p (objfile);
b83470bf
TT
6283 }
6284
5717c425 6285 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6286 {
6287 return nullptr;
6288 }
6289
6290private:
194d088f
TV
6291 partial_symtab *includer () const
6292 {
6293 /* An include psymtab has exactly one dependency: the psymtab that
6294 includes it. */
6295 gdb_assert (this->number_of_dependencies == 1);
6296 return this->dependencies[0];
6297 }
b83470bf
TT
6298};
6299
aaa75496
JB
6300/* Allocate a new partial symtab for file named NAME and mark this new
6301 partial symtab as being an include of PST. */
6302
6303static void
84685904 6304dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
7b249e47
TT
6305 const char *name,
6306 dwarf2_psymtab *pst,
6307 psymtab_storage *partial_symtabs,
0072c873 6308 objfile_per_bfd_storage *objfile_per_bfd)
aaa75496 6309{
7b249e47 6310 dwarf2_include_psymtab *subpst
0072c873 6311 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
aaa75496 6312
fbd9ab74 6313 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6314 subpst->dirname = pst->dirname;
fbd9ab74 6315
84685904 6316 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6317 subpst->dependencies[0] = pst;
6318 subpst->number_of_dependencies = 1;
aaa75496
JB
6319}
6320
6321/* Read the Line Number Program data and extract the list of files
6322 included by the source file represented by PST. Build an include
d85a05f0 6323 partial symtab for each of these included files. */
aaa75496
JB
6324
6325static void
6326dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6327 struct die_info *die,
891813be 6328 dwarf2_psymtab *pst)
aaa75496 6329{
fff8551c 6330 line_header_up lh;
d85a05f0 6331 struct attribute *attr;
aaa75496 6332
d85a05f0 6333 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e
TT
6334 if (attr != nullptr && attr->form_is_unsigned ())
6335 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
aaa75496
JB
6336 if (lh == NULL)
6337 return; /* No linetable, so no includes. */
6338
79748972
TT
6339 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6340 that we pass in the raw text_low here; that is ok because we're
6341 only decoding the line table to make include partial symtabs, and
6342 so the addresses aren't really used. */
4ae976d1 6343 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6344 pst->raw_text_low (), 1);
aaa75496
JB
6345}
6346
348e048f 6347static hashval_t
52dc124a 6348hash_signatured_type (const void *item)
348e048f 6349{
9a3c8263
SM
6350 const struct signatured_type *sig_type
6351 = (const struct signatured_type *) item;
9a619af0 6352
348e048f 6353 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6354 return sig_type->signature;
348e048f
DE
6355}
6356
6357static int
52dc124a 6358eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6359{
9a3c8263
SM
6360 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6361 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6362
348e048f
DE
6363 return lhs->signature == rhs->signature;
6364}
6365
1fd400ff
TT
6366/* Allocate a hash table for signatured types. */
6367
b0b6a987 6368static htab_up
298e9637 6369allocate_signatured_type_table ()
1fd400ff 6370{
b0b6a987
TT
6371 return htab_up (htab_create_alloc (41,
6372 hash_signatured_type,
6373 eq_signatured_type,
6374 NULL, xcalloc, xfree));
1fd400ff
TT
6375}
6376
d467dd73 6377/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6378
6379static int
d467dd73 6380add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6381{
9a3c8263 6382 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6383 std::vector<signatured_type *> *all_type_units
6384 = (std::vector<signatured_type *> *) datum;
1fd400ff 6385
b2bdb8cf 6386 all_type_units->push_back (sigt);
1fd400ff
TT
6387
6388 return 1;
6389}
6390
78d4d2c5 6391/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6392 and fill them into TYPES_HTAB. It will process only type units,
6393 therefore DW_UT_type. */
c88ee1f0 6394
78d4d2c5 6395static void
976ca316 6396create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6397 struct dwo_file *dwo_file,
b0b6a987 6398 dwarf2_section_info *section, htab_up &types_htab,
43988095 6399 rcuh_kind section_kind)
348e048f 6400{
976ca316 6401 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6402 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6403 bfd *abfd;
6404 const gdb_byte *info_ptr, *end_ptr;
348e048f 6405
4bdcc0c1
DE
6406 abbrev_section = (dwo_file != NULL
6407 ? &dwo_file->sections.abbrev
976ca316 6408 : &per_objfile->per_bfd->abbrev);
4bdcc0c1 6409
4800761a 6410 dwarf_read_debug_printf ("Reading %s for %s",
6f738b01
SM
6411 section->get_name (),
6412 abbrev_section->get_file_name ());
09406207 6413
96b79293 6414 section->read (objfile);
78d4d2c5 6415 info_ptr = section->buffer;
348e048f 6416
78d4d2c5
JK
6417 if (info_ptr == NULL)
6418 return;
348e048f 6419
78d4d2c5
JK
6420 /* We can't set abfd until now because the section may be empty or
6421 not present, in which case the bfd is unknown. */
96b79293 6422 abfd = section->get_bfd_owner ();
348e048f 6423
c0ab21c2
TT
6424 /* We don't use cutu_reader here because we don't need to read
6425 any dies: the signature is in the header. */
3019eac3 6426
78d4d2c5
JK
6427 end_ptr = info_ptr + section->size;
6428 while (info_ptr < end_ptr)
6429 {
78d4d2c5
JK
6430 struct signatured_type *sig_type;
6431 struct dwo_unit *dwo_tu;
6432 void **slot;
6433 const gdb_byte *ptr = info_ptr;
6434 struct comp_unit_head header;
6435 unsigned int length;
8b70b953 6436
9c541725 6437 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6438
a49dd8dd
JK
6439 /* Initialize it due to a false compiler warning. */
6440 header.signature = -1;
9c541725 6441 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6442
78d4d2c5
JK
6443 /* We need to read the type's signature in order to build the hash
6444 table, but we don't need anything else just yet. */
348e048f 6445
976ca316 6446 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6447 abbrev_section, ptr, section_kind);
348e048f 6448
4057dfde 6449 length = header.get_length ();
6caca83c 6450
78d4d2c5
JK
6451 /* Skip dummy type units. */
6452 if (ptr >= info_ptr + length
43988095 6453 || peek_abbrev_code (abfd, ptr) == 0
d2854d8d
CT
6454 || (header.unit_type != DW_UT_type
6455 && header.unit_type != DW_UT_split_type))
78d4d2c5
JK
6456 {
6457 info_ptr += length;
6458 continue;
6459 }
dee91e82 6460
78d4d2c5
JK
6461 if (types_htab == NULL)
6462 {
6463 if (dwo_file)
298e9637 6464 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6465 else
298e9637 6466 types_htab = allocate_signatured_type_table ();
78d4d2c5 6467 }
8b70b953 6468
78d4d2c5
JK
6469 if (dwo_file)
6470 {
6471 sig_type = NULL;
976ca316 6472 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
78d4d2c5 6473 dwo_tu->dwo_file = dwo_file;
43988095 6474 dwo_tu->signature = header.signature;
9c541725 6475 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6476 dwo_tu->section = section;
9c541725 6477 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6478 dwo_tu->length = length;
6479 }
6480 else
6481 {
6482 /* N.B.: type_offset is not usable if this type uses a DWO file.
6483 The real type_offset is in the DWO file. */
6484 dwo_tu = NULL;
976ca316 6485 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
43988095 6486 sig_type->signature = header.signature;
9c541725 6487 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6488 sig_type->per_cu.is_debug_types = 1;
6489 sig_type->per_cu.section = section;
9c541725 6490 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6491 sig_type->per_cu.length = length;
6492 }
6493
b0b6a987 6494 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6495 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6496 INSERT);
6497 gdb_assert (slot != NULL);
6498 if (*slot != NULL)
6499 {
9c541725 6500 sect_offset dup_sect_off;
0349ea22 6501
3019eac3
DE
6502 if (dwo_file)
6503 {
78d4d2c5
JK
6504 const struct dwo_unit *dup_tu
6505 = (const struct dwo_unit *) *slot;
6506
9c541725 6507 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6508 }
6509 else
6510 {
78d4d2c5
JK
6511 const struct signatured_type *dup_tu
6512 = (const struct signatured_type *) *slot;
6513
9c541725 6514 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6515 }
8b70b953 6516
b98664d3 6517 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6518 " the entry at offset %s, signature %s"),
6519 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6520 hex_string (header.signature));
78d4d2c5
JK
6521 }
6522 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6523
6f738b01
SM
6524 dwarf_read_debug_printf_v (" offset %s, signature %s",
6525 sect_offset_str (sect_off),
6526 hex_string (header.signature));
3019eac3 6527
78d4d2c5
JK
6528 info_ptr += length;
6529 }
6530}
3019eac3 6531
78d4d2c5
JK
6532/* Create the hash table of all entries in the .debug_types
6533 (or .debug_types.dwo) section(s).
6534 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6535 otherwise it is NULL.
b3c8eb43 6536
78d4d2c5 6537 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6538
78d4d2c5 6539 Note: This function processes DWO files only, not DWP files. */
348e048f 6540
78d4d2c5 6541static void
976ca316 6542create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6543 struct dwo_file *dwo_file,
fd5866f6 6544 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6545 htab_up &types_htab)
78d4d2c5 6546{
fd5866f6 6547 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6548 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6549 rcuh_kind::TYPE);
3019eac3
DE
6550}
6551
6552/* Create the hash table of all entries in the .debug_types section,
6553 and initialize all_type_units.
6554 The result is zero if there is an error (e.g. missing .debug_types section),
6555 otherwise non-zero. */
6556
6557static int
976ca316 6558create_all_type_units (dwarf2_per_objfile *per_objfile)
3019eac3 6559{
b0b6a987 6560 htab_up types_htab;
3019eac3 6561
976ca316
SM
6562 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6563 types_htab, rcuh_kind::COMPILE);
6564 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6565 types_htab);
3019eac3
DE
6566 if (types_htab == NULL)
6567 {
976ca316 6568 per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6569 return 0;
6570 }
6571
976ca316 6572 per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6573
976ca316
SM
6574 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6575 per_objfile->per_bfd->all_type_units.reserve
6576 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6577
976ca316 6578 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6579 add_signatured_type_cu_to_table,
976ca316 6580 &per_objfile->per_bfd->all_type_units);
1fd400ff 6581
348e048f
DE
6582 return 1;
6583}
6584
5989a64e 6585/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6586 If SLOT is non-NULL, it is the entry to use in the hash table.
6587 Otherwise we find one. */
6588
6589static struct signatured_type *
976ca316 6590add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6591{
976ca316
SM
6592 if (per_objfile->per_bfd->all_type_units.size ()
6593 == per_objfile->per_bfd->all_type_units.capacity ())
6594 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6595
976ca316 6596 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6597
976ca316 6598 per_objfile->resize_symtabs ();
af758d11 6599
976ca316 6600 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6601 sig_type->signature = sig;
6602 sig_type->per_cu.is_debug_types = 1;
976ca316 6603 if (per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6604 {
6605 sig_type->per_cu.v.quick =
976ca316 6606 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6607 struct dwarf2_per_cu_quick_data);
6608 }
6609
6610 if (slot == NULL)
6611 {
976ca316 6612 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6613 sig_type, INSERT);
6614 }
6615 gdb_assert (*slot == NULL);
6616 *slot = sig_type;
6617 /* The rest of sig_type must be filled in by the caller. */
6618 return sig_type;
6619}
6620
a2ce51a0
DE
6621/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6622 Fill in SIG_ENTRY with DWO_ENTRY. */
6623
6624static void
976ca316 6625fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6626 struct signatured_type *sig_entry,
6627 struct dwo_unit *dwo_entry)
6628{
976ca316 6629 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6630
7ee85ab1 6631 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6632 gdb_assert (! sig_entry->per_cu.queued);
976ca316 6633 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6634 if (per_bfd->using_index)
6aa5f3a6
DE
6635 {
6636 gdb_assert (sig_entry->per_cu.v.quick != NULL);
976ca316 6637 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6638 }
6639 else
6640 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6641 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6642 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6643 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6644 gdb_assert (sig_entry->dwo_unit == NULL);
6645
6646 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6647 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6648 sig_entry->per_cu.length = dwo_entry->length;
6649 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6650 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6651 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6652 sig_entry->dwo_unit = dwo_entry;
6653}
6654
6655/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6656 If we haven't read the TU yet, create the signatured_type data structure
6657 for a TU to be read in directly from a DWO file, bypassing the stub.
6658 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6659 using .gdb_index, then when reading a CU we want to stay in the DWO file
6660 containing that CU. Otherwise we could end up reading several other DWO
6661 files (due to comdat folding) to process the transitive closure of all the
6662 mentioned TUs, and that can be slow. The current DWO file will have every
6663 type signature that it needs.
a2ce51a0
DE
6664 We only do this for .gdb_index because in the psymtab case we already have
6665 to read all the DWOs to build the type unit groups. */
6666
6667static struct signatured_type *
6668lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6669{
976ca316 6670 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6671 struct dwo_file *dwo_file;
6672 struct dwo_unit find_dwo_entry, *dwo_entry;
6673 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6674 void **slot;
a2ce51a0 6675
976ca316 6676 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6677
6aa5f3a6
DE
6678 /* If TU skeletons have been removed then we may not have read in any
6679 TUs yet. */
976ca316
SM
6680 if (per_objfile->per_bfd->signatured_types == NULL)
6681 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6682
6683 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6684 Use the global signatured_types array to do our own comdat-folding
6685 of types. If this is the first time we're reading this TU, and
6686 the TU has an entry in .gdb_index, replace the recorded data from
6687 .gdb_index with this TU. */
a2ce51a0 6688
a2ce51a0 6689 find_sig_entry.signature = sig;
976ca316 6690 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6691 &find_sig_entry, INSERT);
9a3c8263 6692 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6693
6694 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6695 read. Don't reassign the global entry to point to this DWO if that's
6696 the case. Also note that if the TU is already being read, it may not
6697 have come from a DWO, the program may be a mix of Fission-compiled
6698 code and non-Fission-compiled code. */
6699
6700 /* Have we already tried to read this TU?
6701 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6702 needn't exist in the global table yet). */
6703 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6704 return sig_entry;
6705
6aa5f3a6
DE
6706 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6707 dwo_unit of the TU itself. */
6708 dwo_file = cu->dwo_unit->dwo_file;
6709
a2ce51a0
DE
6710 /* Ok, this is the first time we're reading this TU. */
6711 if (dwo_file->tus == NULL)
6712 return NULL;
6713 find_dwo_entry.signature = sig;
b0b6a987
TT
6714 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6715 &find_dwo_entry);
a2ce51a0
DE
6716 if (dwo_entry == NULL)
6717 return NULL;
6718
6aa5f3a6
DE
6719 /* If the global table doesn't have an entry for this TU, add one. */
6720 if (sig_entry == NULL)
976ca316 6721 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6722
976ca316 6723 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
89e63ee4 6724 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6725 return sig_entry;
6726}
6727
a2ce51a0
DE
6728/* Subroutine of lookup_signatured_type.
6729 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6730 then try the DWP file. If the TU stub (skeleton) has been removed then
6731 it won't be in .gdb_index. */
a2ce51a0
DE
6732
6733static struct signatured_type *
6734lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6735{
976ca316
SM
6736 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6737 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6738 struct dwo_unit *dwo_entry;
6739 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6740 void **slot;
a2ce51a0 6741
976ca316 6742 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6743 gdb_assert (dwp_file != NULL);
6744
6aa5f3a6
DE
6745 /* If TU skeletons have been removed then we may not have read in any
6746 TUs yet. */
976ca316
SM
6747 if (per_objfile->per_bfd->signatured_types == NULL)
6748 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6749
6aa5f3a6 6750 find_sig_entry.signature = sig;
976ca316 6751 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6752 &find_sig_entry, INSERT);
9a3c8263 6753 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6754
6755 /* Have we already tried to read this TU?
6756 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6757 needn't exist in the global table yet). */
6758 if (sig_entry != NULL)
6759 return sig_entry;
6760
a2ce51a0
DE
6761 if (dwp_file->tus == NULL)
6762 return NULL;
976ca316
SM
6763 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6764 1 /* is_debug_types */);
a2ce51a0
DE
6765 if (dwo_entry == NULL)
6766 return NULL;
6767
976ca316
SM
6768 sig_entry = add_type_unit (per_objfile, sig, slot);
6769 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6770
a2ce51a0
DE
6771 return sig_entry;
6772}
6773
380bca97 6774/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6775 Returns NULL if signature SIG is not present in the table.
6776 It is up to the caller to complain about this. */
348e048f
DE
6777
6778static struct signatured_type *
a2ce51a0 6779lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6780{
976ca316 6781 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6782
976ca316 6783 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6784 {
6785 /* We're in a DWO/DWP file, and we're using .gdb_index.
6786 These cases require special processing. */
976ca316 6787 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6788 return lookup_dwo_signatured_type (cu, sig);
6789 else
6790 return lookup_dwp_signatured_type (cu, sig);
6791 }
6792 else
6793 {
6794 struct signatured_type find_entry, *entry;
348e048f 6795
976ca316 6796 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6797 return NULL;
6798 find_entry.signature = sig;
9a3c8263 6799 entry = ((struct signatured_type *)
976ca316 6800 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6801 &find_entry));
a2ce51a0
DE
6802 return entry;
6803 }
348e048f 6804}
18a8505e 6805
42e7ad6c 6806/* Low level DIE reading support. */
348e048f 6807
d85a05f0
DJ
6808/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6809
6810static void
6811init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6812 struct dwarf2_cu *cu,
3019eac3 6813 struct dwarf2_section_info *section,
685af9cd
TT
6814 struct dwo_file *dwo_file,
6815 struct abbrev_table *abbrev_table)
d85a05f0 6816{
fceca515 6817 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6818 reader->abfd = section->get_bfd_owner ();
d85a05f0 6819 reader->cu = cu;
3019eac3 6820 reader->dwo_file = dwo_file;
dee91e82
DE
6821 reader->die_section = section;
6822 reader->buffer = section->buffer;
f664829e 6823 reader->buffer_end = section->buffer + section->size;
685af9cd 6824 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6825}
6826
c0ab21c2 6827/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6828 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6829 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6830 already.
6831
6832 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6833 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6834 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6835 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6836 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6837 STUB_COMP_DIR may be non-NULL.
3e225074 6838 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6839 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6840 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6841 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6842 kept around for at least as long as *RESULT_READER.
6843
b0c7bfa9
DE
6844 The result is non-zero if a valid (non-dummy) DIE was found. */
6845
6846static int
4ab09049 6847read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6848 struct dwo_unit *dwo_unit,
b0c7bfa9 6849 struct die_info *stub_comp_unit_die,
a2ce51a0 6850 const char *stub_comp_dir,
b0c7bfa9 6851 struct die_reader_specs *result_reader,
d521ce57 6852 const gdb_byte **result_info_ptr,
b0c7bfa9 6853 struct die_info **result_comp_unit_die,
685af9cd 6854 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6855{
976ca316 6856 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6857 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6858 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6859 bfd *abfd;
d521ce57 6860 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6861 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6862 int i,num_extra_attrs;
6863 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6864 struct die_info *comp_unit_die;
6865
b0aeadb3
DE
6866 /* At most one of these may be provided. */
6867 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6868
b0c7bfa9
DE
6869 /* These attributes aren't processed until later:
6870 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6871 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6872 referenced later. However, these attributes are found in the stub
6873 which we won't have later. In order to not impose this complication
6874 on the rest of the code, we read them here and copy them to the
6875 DWO CU/TU die. */
b0c7bfa9
DE
6876
6877 stmt_list = NULL;
6878 low_pc = NULL;
6879 high_pc = NULL;
6880 ranges = NULL;
6881 comp_dir = NULL;
6882
6883 if (stub_comp_unit_die != NULL)
6884 {
6885 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6886 DWO file. */
4ab09049 6887 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6888 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6889 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6890 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6891 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6892 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6893
a39fdb41 6894 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6895
2b0c7f41
SM
6896 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6897 We need the value before we can process DW_AT_ranges values from the
6898 DWO. */
6899 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6900
6901 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6902 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6903 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6904 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6905 section. */
6906 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
b0c7bfa9 6907 }
a2ce51a0
DE
6908 else if (stub_comp_dir != NULL)
6909 {
6910 /* Reconstruct the comp_dir attribute to simplify the code below. */
fe56917a 6911 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6912 comp_dir->name = DW_AT_comp_dir;
6913 comp_dir->form = DW_FORM_string;
c6481205 6914 comp_dir->set_string_noncanonical (stub_comp_dir);
a2ce51a0 6915 }
b0c7bfa9
DE
6916
6917 /* Set up for reading the DWO CU/TU. */
6918 cu->dwo_unit = dwo_unit;
685af9cd 6919 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6920 section->read (objfile);
6921 abfd = section->get_bfd_owner ();
9c541725
PA
6922 begin_info_ptr = info_ptr = (section->buffer
6923 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6924 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6925
4ab09049 6926 if (per_cu->is_debug_types)
b0c7bfa9 6927 {
4ab09049 6928 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6929
976ca316
SM
6930 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6931 section, dwo_abbrev_section,
43988095 6932 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6933 /* This is not an assert because it can be caused by bad debug info. */
43988095 6934 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6935 {
6936 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6937 " TU at offset %s [in module %s]"),
a2ce51a0 6938 hex_string (sig_type->signature),
43988095 6939 hex_string (cu->header.signature),
9d8780f0 6940 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6941 bfd_get_filename (abfd));
6942 }
9c541725 6943 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6944 /* For DWOs coming from DWP files, we don't know the CU length
6945 nor the type's offset in the TU until now. */
4057dfde 6946 dwo_unit->length = cu->header.get_length ();
9c541725 6947 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6948
6949 /* Establish the type offset that can be used to lookup the type.
6950 For DWO files, we don't know it until now. */
9c541725
PA
6951 sig_type->type_offset_in_section
6952 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6953 }
6954 else
6955 {
976ca316
SM
6956 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6957 section, dwo_abbrev_section,
43988095 6958 info_ptr, rcuh_kind::COMPILE);
9c541725 6959 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6960 /* For DWOs coming from DWP files, we don't know the CU length
6961 until now. */
4057dfde 6962 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6963 }
6964
606decb2 6965 dwo_abbrev_section->read (objfile);
685af9cd 6966 *result_dwo_abbrev_table
606decb2 6967 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
685af9cd
TT
6968 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6969 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6970
6971 /* Read in the die, but leave space to copy over the attributes
6972 from the stub. This has the benefit of simplifying the rest of
6973 the code - all the work to maintain the illusion of a single
6974 DW_TAG_{compile,type}_unit DIE is done here. */
6975 num_extra_attrs = ((stmt_list != NULL)
6976 + (low_pc != NULL)
6977 + (high_pc != NULL)
6978 + (ranges != NULL)
6979 + (comp_dir != NULL));
6980 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6981 num_extra_attrs);
b0c7bfa9
DE
6982
6983 /* Copy over the attributes from the stub to the DIE we just read in. */
6984 comp_unit_die = *result_comp_unit_die;
6985 i = comp_unit_die->num_attrs;
6986 if (stmt_list != NULL)
6987 comp_unit_die->attrs[i++] = *stmt_list;
6988 if (low_pc != NULL)
6989 comp_unit_die->attrs[i++] = *low_pc;
6990 if (high_pc != NULL)
6991 comp_unit_die->attrs[i++] = *high_pc;
6992 if (ranges != NULL)
6993 comp_unit_die->attrs[i++] = *ranges;
6994 if (comp_dir != NULL)
6995 comp_unit_die->attrs[i++] = *comp_dir;
6996 comp_unit_die->num_attrs += num_extra_attrs;
6997
b4f54984 6998 if (dwarf_die_debug)
bf6af496
DE
6999 {
7000 fprintf_unfiltered (gdb_stdlog,
7001 "Read die from %s@0x%x of %s:\n",
96b79293 7002 section->get_name (),
bf6af496
DE
7003 (unsigned) (begin_info_ptr - section->buffer),
7004 bfd_get_filename (abfd));
b4f54984 7005 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7006 }
7007
b0c7bfa9
DE
7008 /* Skip dummy compilation units. */
7009 if (info_ptr >= begin_info_ptr + dwo_unit->length
7010 || peek_abbrev_code (abfd, info_ptr) == 0)
7011 return 0;
7012
7013 *result_info_ptr = info_ptr;
7014 return 1;
7015}
7016
a084a2a6
AT
7017/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7018 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7019 signature is part of the header. */
7020static gdb::optional<ULONGEST>
7021lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7022{
7023 if (cu->header.version >= 5)
7024 return cu->header.signature;
7025 struct attribute *attr;
7026 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
529908cb 7027 if (attr == nullptr || !attr->form_is_unsigned ())
a084a2a6 7028 return gdb::optional<ULONGEST> ();
529908cb 7029 return attr->as_unsigned ();
a084a2a6
AT
7030}
7031
c0ab21c2 7032/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 7033 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7034 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7035
7036static struct dwo_unit *
4ab09049 7037lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 7038{
4ab09049 7039 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 7040 struct dwo_unit *dwo_unit;
c0ab21c2 7041 const char *comp_dir;
b0c7bfa9 7042
a2ce51a0
DE
7043 gdb_assert (cu != NULL);
7044
b0c7bfa9 7045 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7046 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7047 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 7048
4ab09049
SM
7049 if (per_cu->is_debug_types)
7050 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
7051 else
7052 {
a084a2a6 7053 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 7054
a084a2a6 7055 if (!signature.has_value ())
b0c7bfa9
DE
7056 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7057 " [in module %s]"),
4ab09049
SM
7058 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7059
7060 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
7061 }
7062
b0c7bfa9
DE
7063 return dwo_unit;
7064}
7065
c0ab21c2 7066/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 7067 See it for a description of the parameters.
fcd3b13d 7068 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 7069
c0ab21c2 7070void
9e021579
SM
7071cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7072 dwarf2_per_objfile *per_objfile,
2e671100 7073 dwarf2_cu *existing_cu)
a2ce51a0 7074{
a2ce51a0 7075 struct signatured_type *sig_type;
a2ce51a0
DE
7076
7077 /* Verify we can do the following downcast, and that we have the
7078 data we need. */
7079 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7080 sig_type = (struct signatured_type *) this_cu;
7081 gdb_assert (sig_type->dwo_unit != NULL);
7082
2e671100
SM
7083 dwarf2_cu *cu;
7084
7085 if (existing_cu != nullptr)
6aa5f3a6 7086 {
2e671100
SM
7087 cu = existing_cu;
7088 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 7089 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 7090 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
7091 }
7092 else
7093 {
7188ed02 7094 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 7095 in per_objfile yet. */
7188ed02 7096 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 7097 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 7098 cu = m_new_cu.get ();
6aa5f3a6
DE
7099 }
7100
7101 /* A future optimization, if needed, would be to use an existing
7102 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7103 could share abbrev tables. */
a2ce51a0 7104
2e671100 7105 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
7106 NULL /* stub_comp_unit_die */,
7107 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 7108 this, &info_ptr,
3e225074 7109 &comp_unit_die,
c0ab21c2 7110 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7111 {
7112 /* Dummy die. */
c0ab21c2 7113 dummy_p = true;
a2ce51a0 7114 }
a2ce51a0
DE
7115}
7116
fd820528 7117/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7118 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7119
f4dc4d17
DE
7120 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7121 Otherwise the table specified in the comp unit header is read in and used.
7122 This is an optimization for when we already have the abbrev table.
7123
2e671100
SM
7124 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7125 allocated. */
aaa75496 7126
ab432490 7127cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7128 dwarf2_per_objfile *per_objfile,
c0ab21c2 7129 struct abbrev_table *abbrev_table,
2e671100 7130 dwarf2_cu *existing_cu,
c0ab21c2
TT
7131 bool skip_partial)
7132 : die_reader_specs {},
6751ebae 7133 m_this_cu (this_cu)
c906108c 7134{
976ca316 7135 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7136 struct dwarf2_section_info *section = this_cu->section;
96b79293 7137 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 7138 const gdb_byte *begin_info_ptr;
dee91e82 7139 struct signatured_type *sig_type = NULL;
4bdcc0c1 7140 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7141 /* Non-zero if CU currently points to a DWO file and we need to
7142 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7143 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7144 int rereading_dwo_cu = 0;
c906108c 7145
b4f54984 7146 if (dwarf_die_debug)
9d8780f0 7147 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7148 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7149 sect_offset_str (this_cu->sect_off));
09406207 7150
a2ce51a0
DE
7151 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7152 file (instead of going through the stub), short-circuit all of this. */
7153 if (this_cu->reading_dwo_directly)
7154 {
7155 /* Narrow down the scope of possibilities to have to understand. */
7156 gdb_assert (this_cu->is_debug_types);
7157 gdb_assert (abbrev_table == NULL);
976ca316 7158 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
7159 return;
7160 }
7161
dee91e82 7162 /* This is cheap if the section is already read in. */
96b79293 7163 section->read (objfile);
dee91e82 7164
9c541725 7165 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7166
7167 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7168
2e671100
SM
7169 dwarf2_cu *cu;
7170
7171 if (existing_cu != nullptr)
dee91e82 7172 {
2e671100 7173 cu = existing_cu;
42e7ad6c
DE
7174 /* If this CU is from a DWO file we need to start over, we need to
7175 refetch the attributes from the skeleton CU.
7176 This could be optimized by retrieving those attributes from when we
7177 were here the first time: the previous comp_unit_die was stored in
7178 comp_unit_obstack. But there's no data yet that we need this
7179 optimization. */
7180 if (cu->dwo_unit != NULL)
7181 rereading_dwo_cu = 1;
dee91e82
DE
7182 }
7183 else
7184 {
7188ed02 7185 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 7186 in per_objfile yet. */
976ca316
SM
7187 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7188 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 7189 cu = m_new_cu.get ();
42e7ad6c 7190 }
dee91e82 7191
b0c7bfa9 7192 /* Get the header. */
9c541725 7193 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7194 {
7195 /* We already have the header, there's no need to read it in again. */
9c541725 7196 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7197 }
7198 else
7199 {
3019eac3 7200 if (this_cu->is_debug_types)
dee91e82 7201 {
976ca316
SM
7202 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7203 section, abbrev_section,
7204 info_ptr, rcuh_kind::TYPE);
dee91e82 7205
42e7ad6c
DE
7206 /* Since per_cu is the first member of struct signatured_type,
7207 we can go from a pointer to one to a pointer to the other. */
7208 sig_type = (struct signatured_type *) this_cu;
43988095 7209 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7210 gdb_assert (sig_type->type_offset_in_tu
7211 == cu->header.type_cu_offset_in_tu);
7212 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7213
42e7ad6c
DE
7214 /* LENGTH has not been set yet for type units if we're
7215 using .gdb_index. */
4057dfde 7216 this_cu->length = cu->header.get_length ();
3019eac3
DE
7217
7218 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7219 sig_type->type_offset_in_section =
7220 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7221
7222 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7223 }
7224 else
7225 {
976ca316
SM
7226 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7227 section, abbrev_section,
43988095
JK
7228 info_ptr,
7229 rcuh_kind::COMPILE);
dee91e82 7230
9c541725 7231 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7232 if (this_cu->length == 0)
7233 this_cu->length = cu->header.get_length ();
7234 else
7235 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7236 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7237 }
7238 }
10b3939b 7239
6caca83c 7240 /* Skip dummy compilation units. */
dee91e82 7241 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7242 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7243 {
7244 dummy_p = true;
7245 return;
7246 }
6caca83c 7247
433df2d4
DE
7248 /* If we don't have them yet, read the abbrevs for this compilation unit.
7249 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7250 done. */
f4dc4d17 7251 if (abbrev_table != NULL)
685af9cd
TT
7252 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7253 else
f4dc4d17 7254 {
606decb2 7255 abbrev_section->read (objfile);
c0ab21c2 7256 m_abbrev_table_holder
606decb2 7257 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
c0ab21c2 7258 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7259 }
af703f96 7260
dee91e82 7261 /* Read the top level CU/TU die. */
c0ab21c2 7262 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7263 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7264
58f0c718 7265 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7266 {
7267 dummy_p = true;
7268 return;
7269 }
58f0c718 7270
b0c7bfa9 7271 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7272 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7273 table from the DWO file and pass the ownership over to us. It will be
7274 referenced from READER, so we must make sure to free it after we're done
7275 with READER.
7276
b0c7bfa9
DE
7277 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7278 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7279 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7280 if (dwo_name != nullptr)
3019eac3 7281 {
3019eac3 7282 struct dwo_unit *dwo_unit;
b0c7bfa9 7283 struct die_info *dwo_comp_unit_die;
3019eac3 7284
3e225074 7285 if (comp_unit_die->has_children)
6a506a2d 7286 {
b98664d3 7287 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7288 " has children (offset %s) [in module %s]"),
7289 sect_offset_str (this_cu->sect_off),
7290 bfd_get_filename (abfd));
6a506a2d 7291 }
4ab09049 7292 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7293 if (dwo_unit != NULL)
3019eac3 7294 {
4ab09049 7295 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7296 comp_unit_die, NULL,
c0ab21c2 7297 this, &info_ptr,
3e225074 7298 &dwo_comp_unit_die,
c0ab21c2 7299 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7300 {
7301 /* Dummy die. */
c0ab21c2 7302 dummy_p = true;
6a506a2d
DE
7303 return;
7304 }
7305 comp_unit_die = dwo_comp_unit_die;
7306 }
7307 else
7308 {
7309 /* Yikes, we couldn't find the rest of the DIE, we only have
7310 the stub. A complaint has already been logged. There's
7311 not much more we can do except pass on the stub DIE to
7312 die_reader_func. We don't want to throw an error on bad
7313 debug info. */
3019eac3
DE
7314 }
7315 }
c0ab21c2 7316}
3019eac3 7317
6751ebae
TT
7318void
7319cutu_reader::keep ()
c0ab21c2 7320{
b0c7bfa9 7321 /* Done, clean up. */
6751ebae
TT
7322 gdb_assert (!dummy_p);
7323 if (m_new_cu != NULL)
348e048f 7324 {
7188ed02 7325 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
dda83cd7 7326 now. */
7188ed02
SM
7327 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7328 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 7329 }
dee91e82
DE
7330}
7331
18a8505e
AT
7332/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7333 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7334 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7335
7336 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7337 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7338
7339 We fill in THIS_CU->length.
7340
dee91e82 7341 THIS_CU->cu is always freed when done.
3019eac3 7342 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7343 to care whether it refers to the "main" CU or the DWO CU.
7344
7345 When parent_cu is passed, it is used to provide a default value for
7346 str_offsets_base and addr_base from the parent. */
dee91e82 7347
ab432490 7348cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7349 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
7350 struct dwarf2_cu *parent_cu,
7351 struct dwo_file *dwo_file)
7352 : die_reader_specs {},
7353 m_this_cu (this_cu)
dee91e82 7354{
976ca316 7355 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7356 struct dwarf2_section_info *section = this_cu->section;
96b79293 7357 bfd *abfd = section->get_bfd_owner ();
33e80786 7358 struct dwarf2_section_info *abbrev_section;
d521ce57 7359 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7360
b4f54984 7361 if (dwarf_die_debug)
9d8780f0 7362 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7363 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7364 sect_offset_str (this_cu->sect_off));
09406207 7365
976ca316 7366 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7367
33e80786
DE
7368 abbrev_section = (dwo_file != NULL
7369 ? &dwo_file->sections.abbrev
7370 : get_abbrev_section_for_cu (this_cu));
7371
dee91e82 7372 /* This is cheap if the section is already read in. */
96b79293 7373 section->read (objfile);
dee91e82 7374
976ca316 7375 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 7376
9c541725 7377 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
7378 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7379 section, abbrev_section, info_ptr,
43988095
JK
7380 (this_cu->is_debug_types
7381 ? rcuh_kind::TYPE
7382 : rcuh_kind::COMPILE));
dee91e82 7383
18a8505e
AT
7384 if (parent_cu != nullptr)
7385 {
c0ab21c2
TT
7386 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7387 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7388 }
4057dfde 7389 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7390
7391 /* Skip dummy compilation units. */
7392 if (info_ptr >= begin_info_ptr + this_cu->length
7393 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7394 {
7395 dummy_p = true;
7396 return;
7397 }
72bf9492 7398
606decb2 7399 abbrev_section->read (objfile);
c0ab21c2 7400 m_abbrev_table_holder
606decb2 7401 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
dee91e82 7402
c0ab21c2
TT
7403 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7404 m_abbrev_table_holder.get ());
3e225074 7405 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7406}
7407
0018ea6f
DE
7408\f
7409/* Type Unit Groups.
dee91e82 7410
0018ea6f
DE
7411 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7412 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7413 so that all types coming from the same compilation (.o file) are grouped
7414 together. A future step could be to put the types in the same symtab as
7415 the CU the types ultimately came from. */
ff013f42 7416
f4dc4d17
DE
7417static hashval_t
7418hash_type_unit_group (const void *item)
7419{
9a3c8263
SM
7420 const struct type_unit_group *tu_group
7421 = (const struct type_unit_group *) item;
f4dc4d17 7422
094b34ac 7423 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7424}
348e048f
DE
7425
7426static int
f4dc4d17 7427eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7428{
9a3c8263
SM
7429 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7430 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7431
094b34ac 7432 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7433}
348e048f 7434
f4dc4d17
DE
7435/* Allocate a hash table for type unit groups. */
7436
eaa5fa8b 7437static htab_up
298e9637 7438allocate_type_unit_groups_table ()
f4dc4d17 7439{
eaa5fa8b
TT
7440 return htab_up (htab_create_alloc (3,
7441 hash_type_unit_group,
7442 eq_type_unit_group,
7443 NULL, xcalloc, xfree));
f4dc4d17 7444}
dee91e82 7445
f4dc4d17
DE
7446/* Type units that don't have DW_AT_stmt_list are grouped into their own
7447 partial symtabs. We combine several TUs per psymtab to not let the size
7448 of any one psymtab grow too big. */
7449#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7450#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7451
094b34ac 7452/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7453 Create the type_unit_group object used to hold one or more TUs. */
7454
7455static struct type_unit_group *
094b34ac 7456create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7457{
976ca316
SM
7458 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7459 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
094b34ac 7460 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7461 struct type_unit_group *tu_group;
f4dc4d17 7462
bab287cd 7463 tu_group = OBSTACK_ZALLOC (&per_bfd->obstack, type_unit_group);
094b34ac 7464 per_cu = &tu_group->per_cu;
1859c670 7465 per_cu->per_bfd = per_bfd;
f4dc4d17 7466
1859c670 7467 if (per_bfd->using_index)
094b34ac 7468 {
1859c670 7469 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7470 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7471 }
7472 else
7473 {
9c541725 7474 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7475 dwarf2_psymtab *pst;
528e1572 7476 std::string name;
094b34ac
DE
7477
7478 /* Give the symtab a useful name for debug purposes. */
7479 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7480 name = string_printf ("<type_units_%d>",
7481 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7482 else
528e1572 7483 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7484
976ca316 7485 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
6d94535f 7486 pst->anonymous = true;
094b34ac 7487 }
f4dc4d17 7488
094b34ac 7489 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7490 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7491
7492 return tu_group;
7493}
7494
094b34ac
DE
7495/* Look up the type_unit_group for type unit CU, and create it if necessary.
7496 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7497
7498static struct type_unit_group *
ff39bb5e 7499get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7500{
976ca316
SM
7501 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7502 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7503 struct type_unit_group *tu_group;
7504 void **slot;
7505 unsigned int line_offset;
7506 struct type_unit_group type_unit_group_for_lookup;
7507
976ca316
SM
7508 if (per_objfile->per_bfd->type_unit_groups == NULL)
7509 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7510
7511 /* Do we need to create a new group, or can we use an existing one? */
7512
529908cb 7513 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
f4dc4d17 7514 {
529908cb 7515 line_offset = stmt_list->as_unsigned ();
f4dc4d17
DE
7516 ++tu_stats->nr_symtab_sharers;
7517 }
7518 else
7519 {
7520 /* Ugh, no stmt_list. Rare, but we have to handle it.
7521 We can do various things here like create one group per TU or
7522 spread them over multiple groups to split up the expansion work.
7523 To avoid worst case scenarios (too many groups or too large groups)
7524 we, umm, group them in bunches. */
7525 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7526 | (tu_stats->nr_stmt_less_type_units
7527 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7528 ++tu_stats->nr_stmt_less_type_units;
7529 }
7530
094b34ac 7531 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7532 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7533 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7534 &type_unit_group_for_lookup, INSERT);
7535 if (*slot != NULL)
7536 {
9a3c8263 7537 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7538 gdb_assert (tu_group != NULL);
7539 }
7540 else
7541 {
9c541725 7542 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7543 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7544 *slot = tu_group;
7545 ++tu_stats->nr_symtabs;
7546 }
7547
7548 return tu_group;
7549}
0018ea6f
DE
7550\f
7551/* Partial symbol tables. */
7552
7553/* Create a psymtab named NAME and assign it to PER_CU.
7554
7555 The caller must fill in the following details:
7556 dirname, textlow, texthigh. */
7557
891813be 7558static dwarf2_psymtab *
7aa104c4
SM
7559create_partial_symtab (dwarf2_per_cu_data *per_cu,
7560 dwarf2_per_objfile *per_objfile,
7561 const char *name)
0018ea6f 7562{
0072c873
SM
7563 dwarf2_psymtab *pst
7564 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
7565 per_objfile->objfile->per_bfd, per_cu);
0018ea6f 7566
6d94535f 7567 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7568
7569 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7570 per_cu->v.psymtab = pst;
7571
7572 return pst;
7573}
7574
c0ab21c2 7575/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7576
7577static void
7578process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7579 const gdb_byte *info_ptr,
0018ea6f 7580 struct die_info *comp_unit_die,
c0ab21c2 7581 enum language pretend_language)
0018ea6f
DE
7582{
7583 struct dwarf2_cu *cu = reader->cu;
7aa104c4 7584 dwarf2_per_objfile *per_objfile = cu->per_objfile;
84685904 7585 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7aa104c4 7586 struct objfile *objfile = per_objfile->objfile;
08feed99 7587 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7588 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7589 CORE_ADDR baseaddr;
7590 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7591 dwarf2_psymtab *pst;
3a2b436a 7592 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7593 const char *filename;
0018ea6f 7594
0018ea6f
DE
7595 gdb_assert (! per_cu->is_debug_types);
7596
c0ab21c2 7597 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7598
0018ea6f 7599 /* Allocate a new partial symbol table structure. */
2e927613
TV
7600 gdb::unique_xmalloc_ptr<char> debug_filename;
7601 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7602 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7603 if (filename == NULL)
0018ea6f 7604 filename = "";
2e927613
TV
7605 else if (strcmp (filename, artificial) == 0)
7606 {
7607 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7608 sect_offset_str (per_cu->sect_off),
7609 (char *) NULL));
2e927613
TV
7610 filename = debug_filename.get ();
7611 }
0018ea6f 7612
7aa104c4 7613 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7614
7615 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7616 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7617
b3b3bada 7618 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7619
7620 dwarf2_find_base_address (comp_unit_die, cu);
7621
7622 /* Possibly set the default values of LOWPC and HIGHPC from
7623 `DW_AT_ranges'. */
3a2b436a
JK
7624 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7625 &best_highpc, cu, pst);
7626 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7627 {
7628 CORE_ADDR low
7629 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7630 - baseaddr);
7631 CORE_ADDR high
7632 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7633 - baseaddr - 1);
7634 /* Store the contiguous range if it is not empty; it can be
7635 empty for CUs with no code. */
84685904 7636 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 7637 low, high, pst);
79748972 7638 }
0018ea6f
DE
7639
7640 /* Check if comp unit has_children.
7641 If so, read the rest of the partial symbols from this comp unit.
7642 If not, there's no more debug_info for this comp unit. */
3e225074 7643 if (comp_unit_die->has_children)
0018ea6f
DE
7644 {
7645 struct partial_die_info *first_die;
7646 CORE_ADDR lowpc, highpc;
7647
7648 lowpc = ((CORE_ADDR) -1);
7649 highpc = ((CORE_ADDR) 0);
7650
7651 first_die = load_partial_dies (reader, info_ptr, 1);
7652
7653 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7654 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7655
7656 /* If we didn't find a lowpc, set it to highpc to avoid
7657 complaints from `maint check'. */
7658 if (lowpc == ((CORE_ADDR) -1))
7659 lowpc = highpc;
7660
7661 /* If the compilation unit didn't have an explicit address range,
7662 then use the information extracted from its child dies. */
e385593e 7663 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7664 {
7665 best_lowpc = lowpc;
7666 best_highpc = highpc;
7667 }
7668 }
4ae976d1 7669 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7670 best_lowpc + baseaddr)
7671 - baseaddr);
4ae976d1 7672 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7673 best_highpc + baseaddr)
7674 - baseaddr);
0018ea6f 7675
ae7754b2 7676 pst->end ();
0018ea6f 7677
ae640021 7678 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7679 {
7680 int i;
ae640021 7681 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7682
7683 /* Fill in 'dependencies' here; we fill in 'users' in a
7684 post-pass. */
7685 pst->number_of_dependencies = len;
a9342b62 7686 pst->dependencies
84685904 7687 = per_bfd->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7688 for (i = 0; i < len; ++i)
7689 {
7690 pst->dependencies[i]
7691 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7692 }
0018ea6f 7693
ae640021 7694 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7695 }
7696
7697 /* Get the list of files included in the current compilation unit,
7698 and build a psymtab for each of them. */
7699 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7700
6f738b01
SM
7701 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7702 ", %d global, %d static syms",
7703 per_cu->is_debug_types ? "type" : "comp",
7704 sect_offset_str (per_cu->sect_off),
7705 paddress (gdbarch, pst->text_low (objfile)),
7706 paddress (gdbarch, pst->text_high (objfile)),
7707 (int) pst->global_psymbols.size (),
7708 (int) pst->static_psymbols.size ());
0018ea6f
DE
7709}
7710
7711/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7712 Process compilation unit THIS_CU for a psymtab. */
7713
7714static void
ab432490
SM
7715process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7716 dwarf2_per_objfile *per_objfile,
135f5437 7717 bool want_partial_unit,
b93601f3 7718 enum language pretend_language)
0018ea6f
DE
7719{
7720 /* If this compilation unit was already read in, free the
7721 cached copy in order to read it in again. This is
7722 necessary because we skipped some symbols when we first
7723 read in the compilation unit (see load_partial_dies).
7724 This problem could be avoided, but the benefit is unclear. */
7188ed02 7725 per_objfile->remove_cu (this_cu);
0018ea6f 7726
2e671100 7727 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7728
58990295
TV
7729 switch (reader.comp_unit_die->tag)
7730 {
7731 case DW_TAG_compile_unit:
7732 this_cu->unit_type = DW_UT_compile;
7733 break;
7734 case DW_TAG_partial_unit:
7735 this_cu->unit_type = DW_UT_partial;
7736 break;
e77b0004
TV
7737 case DW_TAG_type_unit:
7738 this_cu->unit_type = DW_UT_type;
7739 break;
58990295 7740 default:
702cf3f5
AB
7741 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7742 dwarf_tag_name (reader.comp_unit_die->tag),
7743 sect_offset_str (reader.cu->per_cu->sect_off),
7744 objfile_name (per_objfile->objfile));
58990295
TV
7745 }
7746
c0ab21c2 7747 if (reader.dummy_p)
f1902523 7748 {
c0ab21c2 7749 /* Nothing. */
f1902523 7750 }
c0ab21c2 7751 else if (this_cu->is_debug_types)
3e225074
TT
7752 build_type_psymtabs_reader (&reader, reader.info_ptr,
7753 reader.comp_unit_die);
135f5437
TT
7754 else if (want_partial_unit
7755 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7756 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7757 reader.comp_unit_die,
c0ab21c2 7758 pretend_language);
0018ea6f 7759
7188ed02 7760 this_cu->lang = reader.cu->language;
58990295 7761
0018ea6f 7762 /* Age out any secondary CUs. */
7188ed02 7763 per_objfile->age_comp_units ();
0018ea6f 7764}
f4dc4d17
DE
7765
7766/* Reader function for build_type_psymtabs. */
7767
7768static void
7769build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7770 const gdb_byte *info_ptr,
3e225074 7771 struct die_info *type_unit_die)
f4dc4d17 7772{
976ca316 7773 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7774 struct dwarf2_cu *cu = reader->cu;
7775 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7776 struct signatured_type *sig_type;
f4dc4d17
DE
7777 struct type_unit_group *tu_group;
7778 struct attribute *attr;
7779 struct partial_die_info *first_die;
7780 CORE_ADDR lowpc, highpc;
891813be 7781 dwarf2_psymtab *pst;
f4dc4d17 7782
0186c6a7
DE
7783 gdb_assert (per_cu->is_debug_types);
7784 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7785
3e225074 7786 if (! type_unit_die->has_children)
f4dc4d17
DE
7787 return;
7788
052c8bb8 7789 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7790 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7791
df07e2c7 7792 if (tu_group->tus == nullptr)
a8b3b8e9 7793 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7794 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7795
7796 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7797 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7798 pst->anonymous = true;
f4dc4d17
DE
7799
7800 first_die = load_partial_dies (reader, info_ptr, 1);
7801
7802 lowpc = (CORE_ADDR) -1;
7803 highpc = (CORE_ADDR) 0;
7804 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7805
ae7754b2 7806 pst->end ();
f4dc4d17
DE
7807}
7808
73051182
DE
7809/* Struct used to sort TUs by their abbreviation table offset. */
7810
7811struct tu_abbrev_offset
7812{
b2bdb8cf
SM
7813 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7814 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7815 {}
7816
7817 signatured_type *sig_type;
73051182
DE
7818 sect_offset abbrev_offset;
7819};
7820
484cf504 7821/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7822
484cf504
TT
7823static bool
7824sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7825 const struct tu_abbrev_offset &b)
73051182 7826{
484cf504 7827 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7828}
7829
7830/* Efficiently read all the type units.
7831 This does the bulk of the work for build_type_psymtabs.
7832
7833 The efficiency is because we sort TUs by the abbrev table they use and
7834 only read each abbrev table once. In one program there are 200K TUs
7835 sharing 8K abbrev tables.
7836
7837 The main purpose of this function is to support building the
5989a64e 7838 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7839 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7840 can collapse the search space by grouping them by stmt_list.
7841 The savings can be significant, in the same program from above the 200K TUs
7842 share 8K stmt_list tables.
7843
7844 FUNC is expected to call get_type_unit_group, which will create the
7845 struct type_unit_group if necessary and add it to
5989a64e 7846 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7847
7848static void
976ca316 7849build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
73051182 7850{
976ca316 7851 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7852 abbrev_table_up abbrev_table;
73051182 7853 sect_offset abbrev_offset;
73051182
DE
7854
7855 /* It's up to the caller to not call us multiple times. */
976ca316 7856 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7857
976ca316 7858 if (per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7859 return;
7860
7861 /* TUs typically share abbrev tables, and there can be way more TUs than
7862 abbrev tables. Sort by abbrev table to reduce the number of times we
7863 read each abbrev table in.
7864 Alternatives are to punt or to maintain a cache of abbrev tables.
7865 This is simpler and efficient enough for now.
7866
7867 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7868 symtab to use). Typically TUs with the same abbrev offset have the same
7869 stmt_list value too so in practice this should work well.
7870
7871 The basic algorithm here is:
7872
7873 sort TUs by abbrev table
7874 for each TU with same abbrev table:
7875 read abbrev table if first user
7876 read TU top level DIE
7877 [IWBN if DWO skeletons had DW_AT_stmt_list]
7878 call FUNC */
7879
6f738b01 7880 dwarf_read_debug_printf ("Building type unit groups ...");
73051182
DE
7881
7882 /* Sort in a separate table to maintain the order of all_type_units
7883 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7884 std::vector<tu_abbrev_offset> sorted_by_abbrev;
976ca316 7885 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7886
976ca316 7887 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
b2bdb8cf 7888 sorted_by_abbrev.emplace_back
976ca316 7889 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
b2bdb8cf 7890 sig_type->per_cu.sect_off));
73051182 7891
484cf504
TT
7892 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7893 sort_tu_by_abbrev_offset);
73051182 7894
9c541725 7895 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7896
b2bdb8cf 7897 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7898 {
73051182
DE
7899 /* Switch to the next abbrev table if necessary. */
7900 if (abbrev_table == NULL
b2bdb8cf 7901 || tu.abbrev_offset != abbrev_offset)
73051182 7902 {
b2bdb8cf 7903 abbrev_offset = tu.abbrev_offset;
606decb2 7904 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
73051182 7905 abbrev_table =
606decb2 7906 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7907 ++tu_stats->nr_uniq_abbrev_tables;
7908 }
7909
976ca316 7910 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
2e671100 7911 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7912 if (!reader.dummy_p)
7913 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7914 reader.comp_unit_die);
73051182 7915 }
6aa5f3a6 7916}
73051182 7917
6aa5f3a6
DE
7918/* Print collected type unit statistics. */
7919
7920static void
976ca316 7921print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7922{
976ca316 7923 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6 7924
6f738b01
SM
7925 dwarf_read_debug_printf ("Type unit statistics:");
7926 dwarf_read_debug_printf (" %zu TUs",
7927 per_objfile->per_bfd->all_type_units.size ());
7928 dwarf_read_debug_printf (" %d uniq abbrev tables",
7929 tu_stats->nr_uniq_abbrev_tables);
7930 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7931 tu_stats->nr_symtabs);
7932 dwarf_read_debug_printf (" %d symtab sharers",
7933 tu_stats->nr_symtab_sharers);
7934 dwarf_read_debug_printf (" %d type units without a stmt_list",
7935 tu_stats->nr_stmt_less_type_units);
7936 dwarf_read_debug_printf (" %d all_type_units reallocs",
7937 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7938}
7939
f4dc4d17
DE
7940/* Traversal function for build_type_psymtabs. */
7941
7942static int
7943build_type_psymtab_dependencies (void **slot, void *info)
7944{
976ca316 7945 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
84685904 7946 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
f4dc4d17 7947 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7948 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7949 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7950 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7951 int i;
7952
7953 gdb_assert (len > 0);
197400e8 7954 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7955
7956 pst->number_of_dependencies = len;
84685904 7957 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
df07e2c7 7958 for (i = 0; i < len; ++i)
f4dc4d17 7959 {
df07e2c7 7960 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7961 gdb_assert (iter->per_cu.is_debug_types);
7962 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7963 iter->type_unit_group = tu_group;
f4dc4d17
DE
7964 }
7965
df07e2c7
AB
7966 delete tu_group->tus;
7967 tu_group->tus = nullptr;
348e048f
DE
7968
7969 return 1;
7970}
7971
7972/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7973 Build partial symbol tables for the .debug_types comp-units. */
7974
7975static void
976ca316 7976build_type_psymtabs (dwarf2_per_objfile *per_objfile)
348e048f 7977{
976ca316 7978 if (! create_all_type_units (per_objfile))
348e048f
DE
7979 return;
7980
976ca316 7981 build_type_psymtabs_1 (per_objfile);
6aa5f3a6 7982}
f4dc4d17 7983
6aa5f3a6
DE
7984/* Traversal function for process_skeletonless_type_unit.
7985 Read a TU in a DWO file and build partial symbols for it. */
7986
7987static int
7988process_skeletonless_type_unit (void **slot, void *info)
7989{
7990 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 7991 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
7992 struct signatured_type find_entry, *entry;
7993
7994 /* If this TU doesn't exist in the global table, add it and read it in. */
7995
976ca316
SM
7996 if (per_objfile->per_bfd->signatured_types == NULL)
7997 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7998
7999 find_entry.signature = dwo_unit->signature;
976ca316 8000 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 8001 &find_entry, INSERT);
6aa5f3a6
DE
8002 /* If we've already seen this type there's nothing to do. What's happening
8003 is we're doing our own version of comdat-folding here. */
8004 if (*slot != NULL)
8005 return 1;
8006
8007 /* This does the job that create_all_type_units would have done for
8008 this TU. */
976ca316
SM
8009 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
8010 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8011 *slot = entry;
8012
8013 /* This does the job that build_type_psymtabs_1 would have done. */
976ca316 8014 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
8015 if (!reader.dummy_p)
8016 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 8017 reader.comp_unit_die);
6aa5f3a6
DE
8018
8019 return 1;
8020}
8021
8022/* Traversal function for process_skeletonless_type_units. */
8023
8024static int
8025process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8026{
8027 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8028
8029 if (dwo_file->tus != NULL)
b0b6a987
TT
8030 htab_traverse_noresize (dwo_file->tus.get (),
8031 process_skeletonless_type_unit, info);
6aa5f3a6
DE
8032
8033 return 1;
8034}
8035
8036/* Scan all TUs of DWO files, verifying we've processed them.
8037 This is needed in case a TU was emitted without its skeleton.
8038 Note: This can't be done until we know what all the DWO files are. */
8039
8040static void
976ca316 8041process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
8042{
8043 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
8044 if (get_dwp_file (per_objfile) == NULL
8045 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 8046 {
976ca316 8047 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 8048 process_dwo_file_for_skeletonless_type_units,
976ca316 8049 per_objfile);
6aa5f3a6 8050 }
348e048f
DE
8051}
8052
ed2dc618 8053/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8054
8055static void
976ca316 8056set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 8057{
976ca316 8058 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 8059 {
891813be 8060 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 8061
36586728
TT
8062 if (pst == NULL)
8063 continue;
8064
b76e467d 8065 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8066 {
8067 /* Set the 'user' field only if it is not already set. */
8068 if (pst->dependencies[j]->user == NULL)
8069 pst->dependencies[j]->user = pst;
8070 }
8071 }
8072}
8073
93311388
DE
8074/* Build the partial symbol table by doing a quick pass through the
8075 .debug_info and .debug_abbrev sections. */
72bf9492 8076
93311388 8077static void
976ca316 8078dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 8079{
976ca316 8080 struct objfile *objfile = per_objfile->objfile;
84685904 8081 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
93311388 8082
6f738b01
SM
8083 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8084 objfile_name (objfile));
45cfd468 8085
76935768 8086 scoped_restore restore_reading_psyms
bab287cd 8087 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
98bfdba5 8088
84685904 8089 per_bfd->info.read (objfile);
91c24f0a 8090
93311388
DE
8091 /* Any cached compilation units will be linked by the per-objfile
8092 read_in_chain. Make sure to free them when we're done. */
976ca316 8093 free_cached_comp_units freer (per_objfile);
72bf9492 8094
976ca316 8095 build_type_psymtabs (per_objfile);
348e048f 8096
976ca316 8097 create_all_comp_units (per_objfile);
c906108c 8098
60606b2c
TT
8099 /* Create a temporary address map on a temporary obstack. We later
8100 copy this to the final obstack. */
8268c778 8101 auto_obstack temp_obstack;
791afaa2
TT
8102
8103 scoped_restore save_psymtabs_addrmap
84685904 8104 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
791afaa2 8105 addrmap_create_mutable (&temp_obstack));
72bf9492 8106
84685904 8107 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
3d5afab3
TV
8108 {
8109 if (per_cu->v.psymtab != NULL)
8110 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8111 continue;
976ca316 8112 process_psymtab_comp_unit (per_cu, per_objfile, false,
ab432490 8113 language_minimal);
3d5afab3 8114 }
ff013f42 8115
6aa5f3a6 8116 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 8117 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
8118
8119 /* Now that all TUs have been processed we can fill in the dependencies. */
84685904 8120 if (per_bfd->type_unit_groups != NULL)
6aa5f3a6 8121 {
84685904 8122 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
976ca316 8123 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
8124 }
8125
6f738b01 8126 if (dwarf_read_debug > 0)
976ca316 8127 print_tu_stats (per_objfile);
6aa5f3a6 8128
976ca316 8129 set_partial_user (per_objfile);
95554aad 8130
84685904
TT
8131 per_bfd->partial_symtabs->psymtabs_addrmap
8132 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
8133 per_bfd->partial_symtabs->obstack ());
791afaa2
TT
8134 /* At this point we want to keep the address map. */
8135 save_psymtabs_addrmap.release ();
ff013f42 8136
6f738b01
SM
8137 dwarf_read_debug_printf ("Done building psymtabs of %s",
8138 objfile_name (objfile));
ae038cb0
DJ
8139}
8140
dee91e82
DE
8141/* Load the partial DIEs for a secondary CU into memory.
8142 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8143
dee91e82 8144static void
ab432490 8145load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
8146 dwarf2_per_objfile *per_objfile,
8147 dwarf2_cu *existing_cu)
dee91e82 8148{
2e671100 8149 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
8150
8151 if (!reader.dummy_p)
8152 {
8153 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8154 language_minimal);
8155
8156 /* Check if comp unit has_children.
8157 If so, read the rest of the partial symbols from this comp unit.
8158 If not, there's no more debug_info for this comp unit. */
3e225074 8159 if (reader.comp_unit_die->has_children)
c0ab21c2 8160 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8161
8162 reader.keep ();
c0ab21c2 8163 }
ae038cb0
DJ
8164}
8165
ae038cb0 8166static void
976ca316 8167read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 8168 struct dwarf2_section_info *section,
f1902523 8169 struct dwarf2_section_info *abbrev_section,
b76e467d 8170 unsigned int is_dwz)
ae038cb0 8171{
d521ce57 8172 const gdb_byte *info_ptr;
976ca316 8173 struct objfile *objfile = per_objfile->objfile;
be391dca 8174
6f738b01
SM
8175 dwarf_read_debug_printf ("Reading %s for %s",
8176 section->get_name (),
8177 section->get_file_name ());
bf6af496 8178
96b79293 8179 section->read (objfile);
ae038cb0 8180
36586728 8181 info_ptr = section->buffer;
6e70227d 8182
36586728 8183 while (info_ptr < section->buffer + section->size)
ae038cb0 8184 {
ae038cb0 8185 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8186
9c541725 8187 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8188
f1902523 8189 comp_unit_head cu_header;
976ca316 8190 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618
SM
8191 abbrev_section, info_ptr,
8192 rcuh_kind::COMPILE);
ae038cb0
DJ
8193
8194 /* Save the compilation unit for later lookup. */
f1902523 8195 if (cu_header.unit_type != DW_UT_type)
976ca316 8196 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8197 else
8198 {
976ca316 8199 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8200 sig_type->signature = cu_header.signature;
8201 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8202 this_cu = &sig_type->per_cu;
8203 }
8204 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8205 this_cu->sect_off = sect_off;
f1902523 8206 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8207 this_cu->is_dwz = is_dwz;
8a0459fd 8208 this_cu->section = section;
ae038cb0 8209
976ca316 8210 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8211
8212 info_ptr = info_ptr + this_cu->length;
8213 }
36586728
TT
8214}
8215
8216/* Create a list of all compilation units in OBJFILE.
8217 This is only done for -readnow and building partial symtabs. */
8218
8219static void
976ca316 8220create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 8221{
976ca316
SM
8222 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8223 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8224 &per_objfile->per_bfd->abbrev, 0);
36586728 8225
976ca316 8226 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 8227 if (dwz != NULL)
976ca316 8228 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
c906108c
SS
8229}
8230
5734ee8b 8231/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8232 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8233 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8234 DW_AT_ranges). See the comments of add_partial_subprogram on how
8235 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8236
72bf9492
DJ
8237static void
8238scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8239 CORE_ADDR *highpc, int set_addrmap,
8240 struct dwarf2_cu *cu)
c906108c 8241{
72bf9492 8242 struct partial_die_info *pdi;
c906108c 8243
91c24f0a
DC
8244 /* Now, march along the PDI's, descending into ones which have
8245 interesting children but skipping the children of the other ones,
8246 until we reach the end of the compilation unit. */
c906108c 8247
72bf9492 8248 pdi = first_die;
91c24f0a 8249
72bf9492
DJ
8250 while (pdi != NULL)
8251 {
52356b79 8252 pdi->fixup (cu);
c906108c 8253
f55ee35c 8254 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8255 children, so we need to look at them. Ditto for anonymous
8256 enums. */
933c6fe4 8257
7d00ffec 8258 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8259 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8260 || pdi->tag == DW_TAG_imported_unit
8261 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8262 {
72bf9492 8263 switch (pdi->tag)
c906108c
SS
8264 {
8265 case DW_TAG_subprogram:
b1dc1806 8266 case DW_TAG_inlined_subroutine:
cdc07690 8267 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
8268 if (cu->language == language_cplus)
8269 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8270 set_addrmap, cu);
c906108c 8271 break;
72929c62 8272 case DW_TAG_constant:
c906108c
SS
8273 case DW_TAG_variable:
8274 case DW_TAG_typedef:
91c24f0a 8275 case DW_TAG_union_type:
317d2668
TV
8276 if (!pdi->is_declaration
8277 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8278 {
72bf9492 8279 add_partial_symbol (pdi, cu);
63d06c5c
DC
8280 }
8281 break;
c906108c 8282 case DW_TAG_class_type:
680b30c7 8283 case DW_TAG_interface_type:
c906108c 8284 case DW_TAG_structure_type:
72bf9492 8285 if (!pdi->is_declaration)
c906108c 8286 {
72bf9492 8287 add_partial_symbol (pdi, cu);
c906108c 8288 }
b7fee5a3
KS
8289 if ((cu->language == language_rust
8290 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8291 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8292 set_addrmap, cu);
c906108c 8293 break;
91c24f0a 8294 case DW_TAG_enumeration_type:
72bf9492
DJ
8295 if (!pdi->is_declaration)
8296 add_partial_enumeration (pdi, cu);
c906108c
SS
8297 break;
8298 case DW_TAG_base_type:
dda83cd7 8299 case DW_TAG_subrange_type:
c906108c 8300 /* File scope base type definitions are added to the partial
dda83cd7 8301 symbol table. */
72bf9492 8302 add_partial_symbol (pdi, cu);
c906108c 8303 break;
d9fa45fe 8304 case DW_TAG_namespace:
cdc07690 8305 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8306 break;
5d7cb8df 8307 case DW_TAG_module:
59c35742
AB
8308 if (!pdi->is_declaration)
8309 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8310 break;
95554aad
TT
8311 case DW_TAG_imported_unit:
8312 {
8313 struct dwarf2_per_cu_data *per_cu;
8314
f4dc4d17
DE
8315 /* For now we don't handle imported units in type units. */
8316 if (cu->per_cu->is_debug_types)
8317 {
8318 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8319 " supported in type units [in module %s]"),
5e22e966 8320 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8321 }
8322
e3b94546 8323 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8324 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8325
8326 /* Go read the partial unit, if needed. */
8327 if (per_cu->v.psymtab == NULL)
ab432490
SM
8328 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8329 cu->language);
95554aad 8330
ae640021 8331 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8332 }
8333 break;
74921315
KS
8334 case DW_TAG_imported_declaration:
8335 add_partial_symbol (pdi, cu);
8336 break;
c906108c
SS
8337 default:
8338 break;
8339 }
8340 }
8341
72bf9492
DJ
8342 /* If the die has a sibling, skip to the sibling. */
8343
8344 pdi = pdi->die_sibling;
8345 }
8346}
8347
8348/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8349
72bf9492 8350 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8351 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8352 Enumerators are an exception; they use the scope of their parent
8353 enumeration type, i.e. the name of the enumeration type is not
8354 prepended to the enumerator.
91c24f0a 8355
72bf9492
DJ
8356 There are two complexities. One is DW_AT_specification; in this
8357 case "parent" means the parent of the target of the specification,
8358 instead of the direct parent of the DIE. The other is compilers
8359 which do not emit DW_TAG_namespace; in this case we try to guess
8360 the fully qualified name of structure types from their members'
8361 linkage names. This must be done using the DIE's children rather
8362 than the children of any DW_AT_specification target. We only need
8363 to do this for structures at the top level, i.e. if the target of
8364 any DW_AT_specification (if any; otherwise the DIE itself) does not
8365 have a parent. */
8366
8367/* Compute the scope prefix associated with PDI's parent, in
8368 compilation unit CU. The result will be allocated on CU's
8369 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8370 field. NULL is returned if no prefix is necessary. */
15d034d0 8371static const char *
72bf9492
DJ
8372partial_die_parent_scope (struct partial_die_info *pdi,
8373 struct dwarf2_cu *cu)
8374{
15d034d0 8375 const char *grandparent_scope;
72bf9492 8376 struct partial_die_info *parent, *real_pdi;
91c24f0a 8377
72bf9492
DJ
8378 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8379 then this means the parent of the specification DIE. */
8380
8381 real_pdi = pdi;
72bf9492 8382 while (real_pdi->has_specification)
fb816e8b 8383 {
122cf0f2
AB
8384 auto res = find_partial_die (real_pdi->spec_offset,
8385 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8386 real_pdi = res.pdi;
8387 cu = res.cu;
8388 }
72bf9492
DJ
8389
8390 parent = real_pdi->die_parent;
8391 if (parent == NULL)
8392 return NULL;
8393
8394 if (parent->scope_set)
8395 return parent->scope;
8396
52356b79 8397 parent->fixup (cu);
72bf9492 8398
10b3939b 8399 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8400
acebe513
UW
8401 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8402 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8403 Work around this problem here. */
8404 if (cu->language == language_cplus
6e70227d 8405 && parent->tag == DW_TAG_namespace
7d00ffec 8406 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
8407 && grandparent_scope == NULL)
8408 {
8409 parent->scope = NULL;
8410 parent->scope_set = 1;
8411 return NULL;
8412 }
8413
0a4b0913 8414 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8415 if (pdi->tag == DW_TAG_enumerator)
8416 /* Enumerators should not get the name of the enumeration as a prefix. */
8417 parent->scope = grandparent_scope;
8418 else if (parent->tag == DW_TAG_namespace
f55ee35c 8419 || parent->tag == DW_TAG_module
72bf9492
DJ
8420 || parent->tag == DW_TAG_structure_type
8421 || parent->tag == DW_TAG_class_type
680b30c7 8422 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8423 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8424 || parent->tag == DW_TAG_enumeration_type
8425 || (cu->language == language_fortran
8426 && parent->tag == DW_TAG_subprogram
8427 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8428 {
8429 if (grandparent_scope == NULL)
7d00ffec 8430 parent->scope = parent->name (cu);
72bf9492 8431 else
3e43a32a
MS
8432 parent->scope = typename_concat (&cu->comp_unit_obstack,
8433 grandparent_scope,
7d00ffec 8434 parent->name (cu), 0, cu);
72bf9492 8435 }
72bf9492
DJ
8436 else
8437 {
8438 /* FIXME drow/2004-04-01: What should we be doing with
8439 function-local names? For partial symbols, we should probably be
8440 ignoring them. */
fa9c3fa0
TT
8441 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8442 dwarf_tag_name (parent->tag),
8443 sect_offset_str (pdi->sect_off));
72bf9492 8444 parent->scope = grandparent_scope;
c906108c
SS
8445 }
8446
72bf9492
DJ
8447 parent->scope_set = 1;
8448 return parent->scope;
8449}
8450
8451/* Return the fully scoped name associated with PDI, from compilation unit
8452 CU. The result will be allocated with malloc. */
4568ecf9 8453
43816ebc 8454static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8455partial_die_full_name (struct partial_die_info *pdi,
8456 struct dwarf2_cu *cu)
8457{
15d034d0 8458 const char *parent_scope;
72bf9492 8459
98bfdba5
PA
8460 /* If this is a template instantiation, we can not work out the
8461 template arguments from partial DIEs. So, unfortunately, we have
8462 to go through the full DIEs. At least any work we do building
8463 types here will be reused if full symbols are loaded later. */
8464 if (pdi->has_template_arguments)
8465 {
52356b79 8466 pdi->fixup (cu);
98bfdba5 8467
7d00ffec 8468 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8469 {
8470 struct die_info *die;
8471 struct attribute attr;
8472 struct dwarf2_cu *ref_cu = cu;
8473
b64f50a1 8474 /* DW_FORM_ref_addr is using section offset. */
b4069958 8475 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8476 attr.form = DW_FORM_ref_addr;
9c541725 8477 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8478 die = follow_die_ref (NULL, &attr, &ref_cu);
8479
43816ebc 8480 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8481 }
8482 }
8483
72bf9492
DJ
8484 parent_scope = partial_die_parent_scope (pdi, cu);
8485 if (parent_scope == NULL)
8486 return NULL;
8487 else
43816ebc 8488 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8489 pdi->name (cu),
8490 0, cu));
c906108c
SS
8491}
8492
8493static void
72bf9492 8494add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8495{
976ca316
SM
8496 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8497 struct objfile *objfile = per_objfile->objfile;
08feed99 8498 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8499 CORE_ADDR addr = 0;
15d034d0 8500 const char *actual_name = NULL;
e142c38c
DJ
8501 CORE_ADDR baseaddr;
8502
b3b3bada 8503 baseaddr = objfile->text_section_offset ();
c906108c 8504
43816ebc
TT
8505 gdb::unique_xmalloc_ptr<char> built_actual_name
8506 = partial_die_full_name (pdi, cu);
15d034d0 8507 if (built_actual_name != NULL)
43816ebc 8508 actual_name = built_actual_name.get ();
63d06c5c 8509
72bf9492 8510 if (actual_name == NULL)
7d00ffec 8511 actual_name = pdi->name (cu);
72bf9492 8512
76e288d1
TT
8513 partial_symbol psymbol;
8514 memset (&psymbol, 0, sizeof (psymbol));
8515 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
a52d653e 8516 psymbol.ginfo.set_section_index (-1);
76e288d1
TT
8517
8518 /* The code below indicates that the psymbol should be installed by
8519 setting this. */
8520 gdb::optional<psymbol_placement> where;
8521
c906108c
SS
8522 switch (pdi->tag)
8523 {
b1dc1806 8524 case DW_TAG_inlined_subroutine:
c906108c 8525 case DW_TAG_subprogram:
79748972
TT
8526 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8527 - baseaddr);
0a4b0913
AB
8528 if (pdi->is_external
8529 || cu->language == language_ada
8530 || (cu->language == language_fortran
8531 && pdi->die_parent != NULL
8532 && pdi->die_parent->tag == DW_TAG_subprogram))
8533 {
dda83cd7
SM
8534 /* Normally, only "external" DIEs are part of the global scope.
8535 But in Ada and Fortran, we want to be able to access nested
8536 procedures globally. So all Ada and Fortran subprograms are
8537 stored in the global scope. */
76e288d1 8538 where = psymbol_placement::GLOBAL;
c906108c
SS
8539 }
8540 else
76e288d1
TT
8541 where = psymbol_placement::STATIC;
8542
8543 psymbol.domain = VAR_DOMAIN;
8544 psymbol.aclass = LOC_BLOCK;
a52d653e 8545 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1 8546 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8547
8548 if (pdi->main_subprogram && actual_name != NULL)
8549 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8550 break;
72929c62 8551 case DW_TAG_constant:
76e288d1
TT
8552 psymbol.domain = VAR_DOMAIN;
8553 psymbol.aclass = LOC_STATIC;
8554 where = (pdi->is_external
8555 ? psymbol_placement::GLOBAL
8556 : psymbol_placement::STATIC);
72929c62 8557 break;
c906108c 8558 case DW_TAG_variable:
95554aad
TT
8559 if (pdi->d.locdesc)
8560 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8561
95554aad 8562 if (pdi->d.locdesc
caac4577 8563 && addr == 0
976ca316 8564 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8565 {
8566 /* A global or static variable may also have been stripped
8567 out by the linker if unused, in which case its address
8568 will be nullified; do not add such variables into partial
8569 symbol table then. */
8570 }
8571 else if (pdi->is_external)
c906108c
SS
8572 {
8573 /* Global Variable.
8574 Don't enter into the minimal symbol tables as there is
8575 a minimal symbol table entry from the ELF symbols already.
8576 Enter into partial symbol table if it has a location
8577 descriptor or a type.
8578 If the location descriptor is missing, new_symbol will create
8579 a LOC_UNRESOLVED symbol, the address of the variable will then
8580 be determined from the minimal symbol table whenever the variable
8581 is referenced.
8582 The address for the partial symbol table entry is not
8583 used by GDB, but it comes in handy for debugging partial symbol
8584 table building. */
8585
95554aad 8586 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8587 {
8588 psymbol.domain = VAR_DOMAIN;
8589 psymbol.aclass = LOC_STATIC;
a52d653e 8590 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8591 psymbol.ginfo.value.address = addr;
8592 where = psymbol_placement::GLOBAL;
8593 }
c906108c
SS
8594 }
8595 else
8596 {
ff908ebf
AW
8597 int has_loc = pdi->d.locdesc != NULL;
8598
8599 /* Static Variable. Skip symbols whose value we cannot know (those
8600 without location descriptors or constant values). */
8601 if (!has_loc && !pdi->has_const_value)
43816ebc 8602 return;
ff908ebf 8603
76e288d1
TT
8604 psymbol.domain = VAR_DOMAIN;
8605 psymbol.aclass = LOC_STATIC;
a52d653e 8606 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8607 if (has_loc)
8608 psymbol.ginfo.value.address = addr;
8609 where = psymbol_placement::STATIC;
c906108c
SS
8610 }
8611 break;
d8f62e84 8612 case DW_TAG_array_type:
c906108c
SS
8613 case DW_TAG_typedef:
8614 case DW_TAG_base_type:
a02abb62 8615 case DW_TAG_subrange_type:
76e288d1
TT
8616 psymbol.domain = VAR_DOMAIN;
8617 psymbol.aclass = LOC_TYPEDEF;
8618 where = psymbol_placement::STATIC;
c906108c 8619 break;
74921315 8620 case DW_TAG_imported_declaration:
72bf9492 8621 case DW_TAG_namespace:
76e288d1
TT
8622 psymbol.domain = VAR_DOMAIN;
8623 psymbol.aclass = LOC_TYPEDEF;
8624 where = psymbol_placement::GLOBAL;
72bf9492 8625 break;
530e8392 8626 case DW_TAG_module:
a5fd13a9 8627 /* With Fortran 77 there might be a "BLOCK DATA" module
dda83cd7
SM
8628 available without any name. If so, we skip the module as it
8629 doesn't bring any value. */
a5fd13a9 8630 if (actual_name != nullptr)
76e288d1
TT
8631 {
8632 psymbol.domain = MODULE_DOMAIN;
8633 psymbol.aclass = LOC_TYPEDEF;
8634 where = psymbol_placement::GLOBAL;
8635 }
530e8392 8636 break;
c906108c 8637 case DW_TAG_class_type:
680b30c7 8638 case DW_TAG_interface_type:
c906108c
SS
8639 case DW_TAG_structure_type:
8640 case DW_TAG_union_type:
8641 case DW_TAG_enumeration_type:
fa4028e9 8642 /* Skip external references. The DWARF standard says in the section
dda83cd7
SM
8643 about "Structure, Union, and Class Type Entries": "An incomplete
8644 structure, union or class type is represented by a structure,
8645 union or class entry that does not have a byte size attribute
8646 and that has a DW_AT_declaration attribute." */
fa4028e9 8647 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8648 return;
fa4028e9 8649
63d06c5c
DC
8650 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8651 static vs. global. */
76e288d1
TT
8652 psymbol.domain = STRUCT_DOMAIN;
8653 psymbol.aclass = LOC_TYPEDEF;
8654 where = (cu->language == language_cplus
8655 ? psymbol_placement::GLOBAL
8656 : psymbol_placement::STATIC);
c906108c
SS
8657 break;
8658 case DW_TAG_enumerator:
76e288d1
TT
8659 psymbol.domain = VAR_DOMAIN;
8660 psymbol.aclass = LOC_CONST;
8661 where = (cu->language == language_cplus
8662 ? psymbol_placement::GLOBAL
8663 : psymbol_placement::STATIC);
c906108c
SS
8664 break;
8665 default:
8666 break;
8667 }
76e288d1
TT
8668
8669 if (where.has_value ())
8670 {
f049a313
TT
8671 if (built_actual_name != nullptr)
8672 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8673 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8674 psymbol.ginfo.set_linkage_name (actual_name);
8675 else
8676 {
8677 psymbol.ginfo.set_demangled_name (actual_name,
8678 &objfile->objfile_obstack);
8679 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8680 }
7b249e47
TT
8681 cu->per_cu->v.psymtab->add_psymbol
8682 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8683 objfile);
76e288d1 8684 }
c906108c
SS
8685}
8686
5c4e30ca
DC
8687/* Read a partial die corresponding to a namespace; also, add a symbol
8688 corresponding to that namespace to the symbol table. NAMESPACE is
8689 the name of the enclosing namespace. */
91c24f0a 8690
72bf9492
DJ
8691static void
8692add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8693 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8694 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8695{
72bf9492 8696 /* Add a symbol for the namespace. */
e7c27a73 8697
72bf9492 8698 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8699
8700 /* Now scan partial symbols in that namespace. */
8701
91c24f0a 8702 if (pdi->has_children)
cdc07690 8703 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8704}
8705
5d7cb8df
JK
8706/* Read a partial die corresponding to a Fortran module. */
8707
8708static void
8709add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8710 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8711{
530e8392
KB
8712 /* Add a symbol for the namespace. */
8713
8714 add_partial_symbol (pdi, cu);
8715
f55ee35c 8716 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8717
8718 if (pdi->has_children)
cdc07690 8719 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8720}
8721
b1dc1806
XR
8722/* Read a partial die corresponding to a subprogram or an inlined
8723 subprogram and create a partial symbol for that subprogram.
8724 When the CU language allows it, this routine also defines a partial
8725 symbol for each nested subprogram that this subprogram contains.
8726 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8727 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8728
cdc07690
YQ
8729 PDI may also be a lexical block, in which case we simply search
8730 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8731 Again, this is only performed when the CU language allows this
8732 type of definitions. */
8733
8734static void
8735add_partial_subprogram (struct partial_die_info *pdi,
8736 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8737 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8738{
b1dc1806 8739 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8740 {
8741 if (pdi->has_pc_info)
dda83cd7
SM
8742 {
8743 if (pdi->lowpc < *lowpc)
8744 *lowpc = pdi->lowpc;
8745 if (pdi->highpc > *highpc)
8746 *highpc = pdi->highpc;
cdc07690 8747 if (set_addrmap)
5734ee8b 8748 {
5e22e966 8749 struct objfile *objfile = cu->per_objfile->objfile;
84685904 8750 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 8751 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8752 CORE_ADDR baseaddr;
b926417a
TT
8753 CORE_ADDR this_highpc;
8754 CORE_ADDR this_lowpc;
5734ee8b 8755
b3b3bada 8756 baseaddr = objfile->text_section_offset ();
b926417a
TT
8757 this_lowpc
8758 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8759 pdi->lowpc + baseaddr)
8760 - baseaddr);
8761 this_highpc
8762 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8763 pdi->highpc + baseaddr)
8764 - baseaddr);
84685904 8765 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
b926417a 8766 this_lowpc, this_highpc - 1,
9291a0cd 8767 cu->per_cu->v.psymtab);
5734ee8b 8768 }
dda83cd7 8769 }
481860b3
GB
8770
8771 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8772 {
dda83cd7 8773 if (!pdi->is_declaration)
e8d05480
JB
8774 /* Ignore subprogram DIEs that do not have a name, they are
8775 illegal. Do not emit a complaint at this point, we will
8776 do so when we convert this psymtab into a symtab. */
7d00ffec 8777 if (pdi->name (cu))
e8d05480 8778 add_partial_symbol (pdi, cu);
dda83cd7 8779 }
bc30ff58 8780 }
6e70227d 8781
bc30ff58
JB
8782 if (! pdi->has_children)
8783 return;
8784
0a4b0913 8785 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8786 {
8787 pdi = pdi->die_child;
8788 while (pdi != NULL)
8789 {
52356b79 8790 pdi->fixup (cu);
bc30ff58 8791 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8792 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8793 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8794 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8795 pdi = pdi->die_sibling;
8796 }
8797 }
8798}
8799
91c24f0a
DC
8800/* Read a partial die corresponding to an enumeration type. */
8801
72bf9492
DJ
8802static void
8803add_partial_enumeration (struct partial_die_info *enum_pdi,
8804 struct dwarf2_cu *cu)
91c24f0a 8805{
72bf9492 8806 struct partial_die_info *pdi;
91c24f0a 8807
7d00ffec 8808 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8809 add_partial_symbol (enum_pdi, cu);
8810
8811 pdi = enum_pdi->die_child;
8812 while (pdi)
91c24f0a 8813 {
7d00ffec 8814 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8815 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8816 else
72bf9492
DJ
8817 add_partial_symbol (pdi, cu);
8818 pdi = pdi->die_sibling;
91c24f0a 8819 }
91c24f0a
DC
8820}
8821
6caca83c
CC
8822/* Return the initial uleb128 in the die at INFO_PTR. */
8823
8824static unsigned int
d521ce57 8825peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8826{
8827 unsigned int bytes_read;
8828
8829 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8830}
8831
685af9cd
TT
8832/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8833 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8834
4bb7a0a7
DJ
8835 Return the corresponding abbrev, or NULL if the number is zero (indicating
8836 an empty DIE). In either case *BYTES_READ will be set to the length of
8837 the initial number. */
8838
7c32eebb 8839static const struct abbrev_info *
685af9cd
TT
8840peek_die_abbrev (const die_reader_specs &reader,
8841 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8842{
685af9cd 8843 dwarf2_cu *cu = reader.cu;
0280fdcc 8844 bfd *abfd = reader.abfd;
685af9cd
TT
8845 unsigned int abbrev_number
8846 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8847
8848 if (abbrev_number == 0)
8849 return NULL;
8850
7c32eebb
TT
8851 const abbrev_info *abbrev
8852 = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8853 if (!abbrev)
8854 {
422b9917 8855 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8856 " at offset %s [in module %s]"),
422b9917 8857 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8858 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8859 }
8860
8861 return abbrev;
8862}
8863
93311388
DE
8864/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8865 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8866 DIE. Any children of the skipped DIEs will also be skipped. */
8867
d521ce57
TT
8868static const gdb_byte *
8869skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8870{
4bb7a0a7
DJ
8871 while (1)
8872 {
685af9cd 8873 unsigned int bytes_read;
7c32eebb
TT
8874 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8875 &bytes_read);
685af9cd 8876
4bb7a0a7
DJ
8877 if (abbrev == NULL)
8878 return info_ptr + bytes_read;
8879 else
dee91e82 8880 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8881 }
8882}
8883
93311388
DE
8884/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8885 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8886 abbrev corresponding to that skipped uleb128 should be passed in
8887 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8888 children. */
8889
d521ce57
TT
8890static const gdb_byte *
8891skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7c32eebb 8892 const struct abbrev_info *abbrev)
4bb7a0a7
DJ
8893{
8894 unsigned int bytes_read;
8895 struct attribute attr;
dee91e82
DE
8896 bfd *abfd = reader->abfd;
8897 struct dwarf2_cu *cu = reader->cu;
d521ce57 8898 const gdb_byte *buffer = reader->buffer;
f664829e 8899 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8900 unsigned int form, i;
8901
8902 for (i = 0; i < abbrev->num_attrs; i++)
8903 {
8904 /* The only abbrev we care about is DW_AT_sibling. */
8905 if (abbrev->attrs[i].name == DW_AT_sibling)
8906 {
7a5f294d 8907 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8908 if (attr.form == DW_FORM_ref_addr)
b98664d3 8909 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8910 else
b9502d3f 8911 {
0826b30a 8912 sect_offset off = attr.get_ref_die_offset ();
9c541725 8913 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8914
8915 if (sibling_ptr < info_ptr)
b98664d3 8916 complaint (_("DW_AT_sibling points backwards"));
22869d73 8917 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8918 reader->die_section->overflow_complaint ();
b9502d3f
WN
8919 else
8920 return sibling_ptr;
8921 }
4bb7a0a7
DJ
8922 }
8923
8924 /* If it isn't DW_AT_sibling, skip this attribute. */
8925 form = abbrev->attrs[i].form;
8926 skip_attribute:
8927 switch (form)
8928 {
4bb7a0a7 8929 case DW_FORM_ref_addr:
ae411497
TT
8930 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8931 and later it is offset sized. */
8932 if (cu->header.version == 2)
8933 info_ptr += cu->header.addr_size;
8934 else
8935 info_ptr += cu->header.offset_size;
8936 break;
36586728
TT
8937 case DW_FORM_GNU_ref_alt:
8938 info_ptr += cu->header.offset_size;
8939 break;
ae411497 8940 case DW_FORM_addr:
4bb7a0a7
DJ
8941 info_ptr += cu->header.addr_size;
8942 break;
8943 case DW_FORM_data1:
8944 case DW_FORM_ref1:
8945 case DW_FORM_flag:
8fe0f950 8946 case DW_FORM_strx1:
4bb7a0a7
DJ
8947 info_ptr += 1;
8948 break;
2dc7f7b3 8949 case DW_FORM_flag_present:
43988095 8950 case DW_FORM_implicit_const:
2dc7f7b3 8951 break;
4bb7a0a7
DJ
8952 case DW_FORM_data2:
8953 case DW_FORM_ref2:
8fe0f950 8954 case DW_FORM_strx2:
4bb7a0a7
DJ
8955 info_ptr += 2;
8956 break;
8fe0f950
AT
8957 case DW_FORM_strx3:
8958 info_ptr += 3;
8959 break;
4bb7a0a7
DJ
8960 case DW_FORM_data4:
8961 case DW_FORM_ref4:
8fe0f950 8962 case DW_FORM_strx4:
4bb7a0a7
DJ
8963 info_ptr += 4;
8964 break;
8965 case DW_FORM_data8:
8966 case DW_FORM_ref8:
55f1336d 8967 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8968 info_ptr += 8;
8969 break;
0224619f
JK
8970 case DW_FORM_data16:
8971 info_ptr += 16;
8972 break;
4bb7a0a7 8973 case DW_FORM_string:
9b1c24c8 8974 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8975 info_ptr += bytes_read;
8976 break;
2dc7f7b3 8977 case DW_FORM_sec_offset:
4bb7a0a7 8978 case DW_FORM_strp:
36586728 8979 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8980 info_ptr += cu->header.offset_size;
8981 break;
2dc7f7b3 8982 case DW_FORM_exprloc:
4bb7a0a7
DJ
8983 case DW_FORM_block:
8984 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8985 info_ptr += bytes_read;
8986 break;
8987 case DW_FORM_block1:
8988 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8989 break;
8990 case DW_FORM_block2:
8991 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8992 break;
8993 case DW_FORM_block4:
8994 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8995 break;
336d760d 8996 case DW_FORM_addrx:
cf532bd1 8997 case DW_FORM_strx:
4bb7a0a7
DJ
8998 case DW_FORM_sdata:
8999 case DW_FORM_udata:
9000 case DW_FORM_ref_udata:
3019eac3
DE
9001 case DW_FORM_GNU_addr_index:
9002 case DW_FORM_GNU_str_index:
18a8505e 9003 case DW_FORM_rnglistx:
41144253 9004 case DW_FORM_loclistx:
d521ce57 9005 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9006 break;
9007 case DW_FORM_indirect:
9008 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9009 info_ptr += bytes_read;
9010 /* We need to continue parsing from here, so just go back to
9011 the top. */
9012 goto skip_attribute;
9013
9014 default:
3e43a32a
MS
9015 error (_("Dwarf Error: Cannot handle %s "
9016 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9017 dwarf_form_name (form),
9018 bfd_get_filename (abfd));
9019 }
9020 }
9021
9022 if (abbrev->has_children)
dee91e82 9023 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9024 else
9025 return info_ptr;
9026}
9027
93311388 9028/* Locate ORIG_PDI's sibling.
dee91e82 9029 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9030
d521ce57 9031static const gdb_byte *
dee91e82
DE
9032locate_pdi_sibling (const struct die_reader_specs *reader,
9033 struct partial_die_info *orig_pdi,
d521ce57 9034 const gdb_byte *info_ptr)
91c24f0a
DC
9035{
9036 /* Do we know the sibling already? */
72bf9492 9037
91c24f0a
DC
9038 if (orig_pdi->sibling)
9039 return orig_pdi->sibling;
9040
9041 /* Are there any children to deal with? */
9042
9043 if (!orig_pdi->has_children)
9044 return info_ptr;
9045
4bb7a0a7 9046 /* Skip the children the long way. */
91c24f0a 9047
dee91e82 9048 return skip_children (reader, info_ptr);
91c24f0a
DC
9049}
9050
257e7a09 9051/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9052 not NULL. */
c906108c 9053
891813be
TT
9054void
9055dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 9056{
976ca316 9057 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 9058
976ca316 9059 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 9060
077cbab2
TT
9061 /* If this psymtab is constructed from a debug-only objfile, the
9062 has_section_at_zero flag will not necessarily be correct. We
9063 can get the correct value for this flag by looking at the data
9064 associated with the (presumably stripped) associated objfile. */
9065 if (objfile->separate_debug_objfile_backlink)
c906108c 9066 {
976ca316 9067 dwarf2_per_objfile *per_objfile_backlink
077cbab2 9068 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 9069
976ca316
SM
9070 per_objfile->per_bfd->has_section_at_zero
9071 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 9072 }
98bfdba5 9073
8566b89b 9074 expand_psymtab (objfile);
95554aad 9075
976ca316 9076 process_cu_includes (per_objfile);
c906108c 9077}
9cdd5dbd
DE
9078\f
9079/* Reading in full CUs. */
c906108c 9080
10b3939b
DJ
9081/* Add PER_CU to the queue. */
9082
9083static void
120ce1b5
SM
9084queue_comp_unit (dwarf2_per_cu_data *per_cu,
9085 dwarf2_per_objfile *per_objfile,
95554aad 9086 enum language pretend_language)
10b3939b 9087{
10b3939b 9088 per_cu->queued = 1;
08ac5771
SM
9089
9090 gdb_assert (per_objfile->per_bfd->queue.has_value ());
9091 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
9092}
9093
616c069a
SM
9094/* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
9095
89e63ee4
DE
9096 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9097 dependency.
69d751e3 9098
616c069a
SM
9099 Return true if maybe_queue_comp_unit requires the caller to load the CU's
9100 DIEs, false otherwise.
9101
9102 Explanation: there is an invariant that if a CU is queued for expansion
9103 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
9104 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
9105 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
9106 are not yet loaded, the the caller must load the CU's DIEs to ensure the
9107 invariant is respected.
9108
9109 The caller is therefore not required to load the CU's DIEs (we return false)
9110 if:
9111
9112 - the CU is already expanded, and therefore does not get enqueued
9113 - the CU gets enqueued for expansion, but its DIEs are already loaded
9114
9115 Note that the caller should not use this function's return value as an
9116 indicator of whether the CU's DIEs are loaded right now, it should check
9117 that by calling `dwarf2_per_objfile::get_cu` instead. */
0907af0c
DE
9118
9119static int
89e63ee4 9120maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
9121 dwarf2_per_cu_data *per_cu,
9122 dwarf2_per_objfile *per_objfile,
0907af0c
DE
9123 enum language pretend_language)
9124{
9125 /* We may arrive here during partial symbol reading, if we need full
9126 DIEs to process an unusual case (e.g. template arguments). Do
9127 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 9128 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 9129 {
7188ed02
SM
9130 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9131
9132 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
9133 return 1;
9134 return 0;
9135 }
9136
9137 /* Mark the dependence relation so that we don't flush PER_CU
9138 too early. */
89e63ee4
DE
9139 if (dependent_cu != NULL)
9140 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9141
9142 /* If it's already on the queue, we have nothing to do. */
9143 if (per_cu->queued)
de53369b
SM
9144 {
9145 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
9146 loaded. */
9147 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
616c069a
SM
9148
9149 /* If the CU is queued for expansion, it should not already be
9150 expanded. */
9151 gdb_assert (!per_objfile->symtab_set_p (per_cu));
9152
9153 /* The DIEs are already loaded, the caller doesn't need to do it. */
de53369b
SM
9154 return 0;
9155 }
0907af0c 9156
616c069a
SM
9157 bool queued = false;
9158 if (!per_objfile->symtab_set_p (per_cu))
9159 {
9160 /* Add it to the queue. */
9161 queue_comp_unit (per_cu, per_objfile, pretend_language);
9162 queued = true;
9163 }
9164
0907af0c
DE
9165 /* If the compilation unit is already loaded, just mark it as
9166 used. */
7188ed02
SM
9167 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9168 if (cu != nullptr)
616c069a 9169 cu->last_used = 0;
0907af0c 9170
616c069a
SM
9171 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
9172 and the DIEs are not already loaded. */
9173 return queued && cu == nullptr;
0907af0c
DE
9174}
9175
10b3939b
DJ
9176/* Process the queue. */
9177
9178static void
976ca316 9179process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 9180{
6f738b01
SM
9181 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9182 objfile_name (per_objfile->objfile));
45cfd468 9183
03dd20cc
DJ
9184 /* The queue starts out with one item, but following a DIE reference
9185 may load a new CU, adding it to the end of the queue. */
08ac5771 9186 while (!per_objfile->per_bfd->queue->empty ())
10b3939b 9187 {
08ac5771 9188 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
7188ed02 9189 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 9190
976ca316 9191 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 9192 {
976ca316 9193 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 9194
7188ed02
SM
9195 /* Skip dummy CUs. */
9196 if (cu != nullptr)
73be47f5 9197 {
7188ed02
SM
9198 unsigned int debug_print_threshold;
9199 char buf[100];
9200
9201 if (per_cu->is_debug_types)
9202 {
9203 struct signatured_type *sig_type =
9204 (struct signatured_type *) per_cu;
9205
9206 sprintf (buf, "TU %s at offset %s",
9207 hex_string (sig_type->signature),
9208 sect_offset_str (per_cu->sect_off));
9209 /* There can be 100s of TUs.
9210 Only print them in verbose mode. */
9211 debug_print_threshold = 2;
9212 }
9213 else
9214 {
9215 sprintf (buf, "CU at offset %s",
9216 sect_offset_str (per_cu->sect_off));
9217 debug_print_threshold = 1;
9218 }
247f5c4f 9219
7188ed02 9220 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 9221 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
f4dc4d17 9222
7188ed02
SM
9223 if (per_cu->is_debug_types)
9224 process_full_type_unit (cu, item.pretend_language);
9225 else
9226 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 9227
7188ed02 9228 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 9229 dwarf_read_debug_printf ("Done expanding %s", buf);
7188ed02 9230 }
f4dc4d17 9231 }
10b3939b 9232
7188ed02 9233 per_cu->queued = 0;
08ac5771 9234 per_objfile->per_bfd->queue->pop ();
10b3939b
DJ
9235 }
9236
6f738b01
SM
9237 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9238 objfile_name (per_objfile->objfile));
10b3939b
DJ
9239}
9240
10b3939b
DJ
9241/* Read in full symbols for PST, and anything it depends on. */
9242
8566b89b
TT
9243void
9244dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9245{
af758d11 9246 gdb_assert (!readin_p (objfile));
95554aad 9247
17ee85fc
TT
9248 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9249 free_cached_comp_units freer (per_objfile);
48993951 9250 expand_dependencies (objfile);
aaa75496 9251
97a1449a 9252 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9253 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9254}
9255
af758d11
SM
9256/* See psympriv.h. */
9257
9258bool
9259dwarf2_psymtab::readin_p (struct objfile *objfile) const
9260{
9261 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9262 return per_objfile->symtab_set_p (per_cu_data);
9263}
9264
9265/* See psympriv.h. */
9266
9267compunit_symtab *
9268dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9269{
9270 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9271 return per_objfile->get_symtab (per_cu_data);
9272}
9273
dee91e82
DE
9274/* Trivial hash function for die_info: the hash value of a DIE
9275 is its offset in .debug_info for this objfile. */
10b3939b 9276
dee91e82
DE
9277static hashval_t
9278die_hash (const void *item)
10b3939b 9279{
9a3c8263 9280 const struct die_info *die = (const struct die_info *) item;
6502dd73 9281
9c541725 9282 return to_underlying (die->sect_off);
dee91e82 9283}
63d06c5c 9284
dee91e82
DE
9285/* Trivial comparison function for die_info structures: two DIEs
9286 are equal if they have the same offset. */
98bfdba5 9287
dee91e82
DE
9288static int
9289die_eq (const void *item_lhs, const void *item_rhs)
9290{
9a3c8263
SM
9291 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9292 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9293
9c541725 9294 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9295}
c906108c 9296
4a636814
SM
9297/* Load the DIEs associated with PER_CU into memory.
9298
9299 In some cases, the caller, while reading partial symbols, will need to load
9300 the full symbols for the CU for some reason. It will already have a
9301 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9302 rather than creating a new one. */
c906108c 9303
dee91e82 9304static void
ab432490
SM
9305load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9306 dwarf2_per_objfile *per_objfile,
4a636814 9307 dwarf2_cu *existing_cu,
c0ab21c2
TT
9308 bool skip_partial,
9309 enum language pretend_language)
dee91e82 9310{
c0ab21c2
TT
9311 gdb_assert (! this_cu->is_debug_types);
9312
7188ed02 9313 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
9314 if (reader.dummy_p)
9315 return;
9316
9317 struct dwarf2_cu *cu = reader.cu;
9318 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9319
dee91e82
DE
9320 gdb_assert (cu->die_hash == NULL);
9321 cu->die_hash =
9322 htab_create_alloc_ex (cu->header.length / 12,
9323 die_hash,
9324 die_eq,
9325 NULL,
9326 &cu->comp_unit_obstack,
9327 hashtab_obstack_allocate,
9328 dummy_obstack_deallocate);
e142c38c 9329
3e225074 9330 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9331 reader.comp_unit_die->child
9332 = read_die_and_siblings (&reader, reader.info_ptr,
9333 &info_ptr, reader.comp_unit_die);
9334 cu->dies = reader.comp_unit_die;
dee91e82 9335 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9336
9337 /* We try not to read any attributes in this function, because not
9cdd5dbd 9338 all CUs needed for references have been loaded yet, and symbol
10b3939b 9339 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9340 or we won't be able to build types correctly.
9341 Similarly, if we do not read the producer, we can not apply
9342 producer-specific interpretation. */
c0ab21c2 9343 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9344
9345 reader.keep ();
10b3939b
DJ
9346}
9347
3da10d80
KS
9348/* Add a DIE to the delayed physname list. */
9349
9350static void
9351add_to_method_list (struct type *type, int fnfield_index, int index,
9352 const char *name, struct die_info *die,
9353 struct dwarf2_cu *cu)
9354{
9355 struct delayed_method_info mi;
9356 mi.type = type;
9357 mi.fnfield_index = fnfield_index;
9358 mi.index = index;
9359 mi.name = name;
9360 mi.die = die;
c89b44cd 9361 cu->method_list.push_back (mi);
3da10d80
KS
9362}
9363
3693fdb3
PA
9364/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9365 "const" / "volatile". If so, decrements LEN by the length of the
9366 modifier and return true. Otherwise return false. */
9367
9368template<size_t N>
9369static bool
9370check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9371{
9372 size_t mod_len = sizeof (mod) - 1;
9373 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9374 {
9375 len -= mod_len;
9376 return true;
9377 }
9378 return false;
9379}
9380
3da10d80
KS
9381/* Compute the physnames of any methods on the CU's method list.
9382
9383 The computation of method physnames is delayed in order to avoid the
9384 (bad) condition that one of the method's formal parameters is of an as yet
9385 incomplete type. */
9386
9387static void
9388compute_delayed_physnames (struct dwarf2_cu *cu)
9389{
3693fdb3 9390 /* Only C++ delays computing physnames. */
c89b44cd 9391 if (cu->method_list.empty ())
3693fdb3
PA
9392 return;
9393 gdb_assert (cu->language == language_cplus);
9394
52941706 9395 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9396 {
1d06ead6 9397 const char *physname;
3da10d80 9398 struct fn_fieldlist *fn_flp
c89b44cd
TT
9399 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9400 physname = dwarf2_physname (mi.name, mi.die, cu);
9401 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9402 = physname ? physname : "";
3693fdb3
PA
9403
9404 /* Since there's no tag to indicate whether a method is a
9405 const/volatile overload, extract that information out of the
9406 demangled name. */
9407 if (physname != NULL)
9408 {
9409 size_t len = strlen (physname);
9410
9411 while (1)
9412 {
9413 if (physname[len] == ')') /* shortcut */
9414 break;
9415 else if (check_modifier (physname, len, " const"))
c89b44cd 9416 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9417 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9418 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9419 else
9420 break;
9421 }
9422 }
3da10d80 9423 }
c89b44cd
TT
9424
9425 /* The list is no longer needed. */
9426 cu->method_list.clear ();
3da10d80
KS
9427}
9428
a766d390
DE
9429/* Go objects should be embedded in a DW_TAG_module DIE,
9430 and it's not clear if/how imported objects will appear.
9431 To keep Go support simple until that's worked out,
9432 go back through what we've read and create something usable.
9433 We could do this while processing each DIE, and feels kinda cleaner,
9434 but that way is more invasive.
9435 This is to, for example, allow the user to type "p var" or "b main"
9436 without having to specify the package name, and allow lookups
9437 of module.object to work in contexts that use the expression
9438 parser. */
9439
9440static void
9441fixup_go_packaging (struct dwarf2_cu *cu)
9442{
421d1616 9443 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9444 struct pending *list;
9445 int i;
9446
c24bdb02 9447 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9448 list != NULL;
9449 list = list->next)
a766d390
DE
9450 {
9451 for (i = 0; i < list->nsyms; ++i)
9452 {
9453 struct symbol *sym = list->symbol[i];
9454
c1b5c1eb 9455 if (sym->language () == language_go
a766d390
DE
9456 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9457 {
421d1616
TT
9458 gdb::unique_xmalloc_ptr<char> this_package_name
9459 (go_symbol_package_name (sym));
a766d390
DE
9460
9461 if (this_package_name == NULL)
9462 continue;
9463 if (package_name == NULL)
421d1616 9464 package_name = std::move (this_package_name);
a766d390
DE
9465 else
9466 {
5e22e966 9467 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9468 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9469 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9470 (symbol_symtab (sym) != NULL
9471 ? symtab_to_filename_for_display
9472 (symbol_symtab (sym))
e3b94546 9473 : objfile_name (objfile)),
421d1616 9474 this_package_name.get (), package_name.get ());
a766d390
DE
9475 }
9476 }
9477 }
9478 }
9479
9480 if (package_name != NULL)
9481 {
5e22e966 9482 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9483 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9484 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9485 saved_package_name);
a766d390
DE
9486 struct symbol *sym;
9487
8c14c3a3 9488 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9489 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9490 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9491 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9492 e.g., "main" finds the "main" module and not C's main(). */
9493 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9494 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9495 SYMBOL_TYPE (sym) = type;
9496
c24bdb02 9497 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9498 }
9499}
9500
c9317f21
TT
9501/* Allocate a fully-qualified name consisting of the two parts on the
9502 obstack. */
9503
9504static const char *
9505rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9506{
9507 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9508}
9509
9c6a1327
TT
9510/* A helper that allocates a variant part to attach to a Rust enum
9511 type. OBSTACK is where the results should be allocated. TYPE is
9512 the type we're processing. DISCRIMINANT_INDEX is the index of the
57d02173
TT
9513 discriminant. It must be the index of one of the fields of TYPE,
9514 or -1 to mean there is no discriminant (univariant enum).
9c6a1327
TT
9515 DEFAULT_INDEX is the index of the default field; or -1 if there is
9516 no default. RANGES is indexed by "effective" field number (the
9517 field index, but omitting the discriminant and default fields) and
9518 must hold the discriminant values used by the variants. Note that
9519 RANGES must have a lifetime at least as long as OBSTACK -- either
9520 already allocated on it, or static. */
c9317f21 9521
9c6a1327
TT
9522static void
9523alloc_rust_variant (struct obstack *obstack, struct type *type,
9524 int discriminant_index, int default_index,
9525 gdb::array_view<discriminant_range> ranges)
9526{
57d02173
TT
9527 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9528 gdb_assert (discriminant_index == -1
9529 || (discriminant_index >= 0
9530 && discriminant_index < type->num_fields ()));
c9317f21 9531 gdb_assert (default_index == -1
1f704f76 9532 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9533
9c6a1327 9534 /* We have one variant for each non-discriminant field. */
57d02173
TT
9535 int n_variants = type->num_fields ();
9536 if (discriminant_index != -1)
9537 --n_variants;
c9317f21 9538
9c6a1327
TT
9539 variant *variants = new (obstack) variant[n_variants];
9540 int var_idx = 0;
9541 int range_idx = 0;
1f704f76 9542 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9543 {
9544 if (i == discriminant_index)
9545 continue;
c9317f21 9546
9c6a1327
TT
9547 variants[var_idx].first_field = i;
9548 variants[var_idx].last_field = i + 1;
9549
9550 /* The default field does not need a range, but other fields do.
9551 We skipped the discriminant above. */
9552 if (i != default_index)
9553 {
9554 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9555 ++range_idx;
9556 }
c9317f21 9557
9c6a1327
TT
9558 ++var_idx;
9559 }
9560
9561 gdb_assert (range_idx == ranges.size ());
9562 gdb_assert (var_idx == n_variants);
9563
9564 variant_part *part = new (obstack) variant_part;
9565 part->discriminant_index = discriminant_index;
57d02173
TT
9566 /* If there is no discriminant, then whether it is signed is of no
9567 consequence. */
9568 part->is_unsigned
9569 = (discriminant_index == -1
9570 ? false
c6d940a9 9571 : type->field (discriminant_index).type ()->is_unsigned ());
9c6a1327
TT
9572 part->variants = gdb::array_view<variant> (variants, n_variants);
9573
9574 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9575 gdb::array_view<variant_part> *prop_value
9576 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9577
9c6a1327 9578 struct dynamic_prop prop;
8c2e4e06 9579 prop.set_variant_parts (prop_value);
9c6a1327 9580
5c54719c 9581 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9582}
9583
9584/* Some versions of rustc emitted enums in an unusual way.
9585
9586 Ordinary enums were emitted as unions. The first element of each
9587 structure in the union was named "RUST$ENUM$DISR". This element
9588 held the discriminant.
9589
9590 These versions of Rust also implemented the "non-zero"
9591 optimization. When the enum had two values, and one is empty and
9592 the other holds a pointer that cannot be zero, the pointer is used
9593 as the discriminant, with a zero value meaning the empty variant.
9594 Here, the union's first member is of the form
9595 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9596 where the fieldnos are the indices of the fields that should be
9597 traversed in order to find the field (which may be several fields deep)
9598 and the variantname is the name of the variant of the case when the
9599 field is zero.
9600
9601 This function recognizes whether TYPE is of one of these forms,
9602 and, if so, smashes it to be a variant type. */
9603
9604static void
9605quirk_rust_enum (struct type *type, struct objfile *objfile)
9606{
78134374 9607 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9608
9609 /* We don't need to deal with empty enums. */
1f704f76 9610 if (type->num_fields () == 0)
c9317f21
TT
9611 return;
9612
9613#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9614 if (type->num_fields () == 1
c9317f21
TT
9615 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9616 {
9617 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9618
9619 /* Decode the field name to find the offset of the
9620 discriminant. */
9621 ULONGEST bit_offset = 0;
940da03e 9622 struct type *field_type = type->field (0).type ();
c9317f21
TT
9623 while (name[0] >= '0' && name[0] <= '9')
9624 {
9625 char *tail;
9626 unsigned long index = strtoul (name, &tail, 10);
9627 name = tail;
9628 if (*name != '$'
1f704f76 9629 || index >= field_type->num_fields ()
c9317f21
TT
9630 || (TYPE_FIELD_LOC_KIND (field_type, index)
9631 != FIELD_LOC_KIND_BITPOS))
9632 {
b98664d3 9633 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9634 "[in module %s]"),
9635 TYPE_FIELD_NAME (type, 0),
9636 objfile_name (objfile));
9637 return;
9638 }
9639 ++name;
9640
9641 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 9642 field_type = field_type->field (index).type ();
c9317f21
TT
9643 }
9644
9c6a1327
TT
9645 /* Smash this type to be a structure type. We have to do this
9646 because the type has already been recorded. */
67607e24 9647 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9648 type->set_num_fields (3);
9c6a1327 9649 /* Save the field we care about. */
ceacbf6e 9650 struct field saved_field = type->field (0);
3cabb6b0
SM
9651 type->set_fields
9652 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9653
9c6a1327 9654 /* Put the discriminant at index 0. */
5d14b6e5 9655 type->field (0).set_type (field_type);
9c6a1327
TT
9656 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9657 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9658 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9659
9660 /* The order of fields doesn't really matter, so put the real
9661 field at index 1 and the data-less field at index 2. */
ceacbf6e 9662 type->field (1) = saved_field;
9c6a1327 9663 TYPE_FIELD_NAME (type, 1)
940da03e
SM
9664 = rust_last_path_segment (type->field (1).type ()->name ());
9665 type->field (1).type ()->set_name
7d93a1e0 9666 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9667 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9668
9669 const char *dataless_name
7d93a1e0 9670 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9671 name);
9672 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9673 dataless_name);
5d14b6e5 9674 type->field (2).set_type (dataless_type);
c9317f21
TT
9675 /* NAME points into the original discriminant name, which
9676 already has the correct lifetime. */
9c6a1327 9677 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9678 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9679
9c6a1327
TT
9680 /* Indicate that this is a variant type. */
9681 static discriminant_range ranges[1] = { { 0, 0 } };
9682 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9683 }
77c2dba3
TT
9684 /* A union with a single anonymous field is probably an old-style
9685 univariant enum. */
1f704f76 9686 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9687 {
c9317f21
TT
9688 /* Smash this type to be a structure type. We have to do this
9689 because the type has already been recorded. */
67607e24 9690 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9691
940da03e 9692 struct type *field_type = type->field (0).type ();
c9317f21 9693 const char *variant_name
7d93a1e0 9694 = rust_last_path_segment (field_type->name ());
9c6a1327 9695 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9696 field_type->set_name
9697 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9698 type->name (), variant_name));
57d02173
TT
9699
9700 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
c9317f21
TT
9701 }
9702 else
9703 {
9704 struct type *disr_type = nullptr;
1f704f76 9705 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9706 {
940da03e 9707 disr_type = type->field (i).type ();
c9317f21 9708
78134374 9709 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9710 {
9711 /* All fields of a true enum will be structs. */
9712 return;
9713 }
1f704f76 9714 else if (disr_type->num_fields () == 0)
c9317f21
TT
9715 {
9716 /* Could be data-less variant, so keep going. */
a037790e 9717 disr_type = nullptr;
c9317f21
TT
9718 }
9719 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9720 "RUST$ENUM$DISR") != 0)
9721 {
9722 /* Not a Rust enum. */
9723 return;
9724 }
9725 else
9726 {
9727 /* Found one. */
9728 break;
9729 }
9730 }
9731
9732 /* If we got here without a discriminant, then it's probably
9733 just a union. */
9734 if (disr_type == nullptr)
9735 return;
9736
9737 /* Smash this type to be a structure type. We have to do this
9738 because the type has already been recorded. */
67607e24 9739 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9740
9c6a1327 9741 /* Make space for the discriminant field. */
ceacbf6e 9742 struct field *disr_field = &disr_type->field (0);
9c6a1327 9743 field *new_fields
1f704f76 9744 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9745 * sizeof (struct field)));
80fc5e77 9746 memcpy (new_fields + 1, type->fields (),
1f704f76 9747 type->num_fields () * sizeof (struct field));
3cabb6b0 9748 type->set_fields (new_fields);
1f704f76 9749 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9750
9751 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9752 type->field (0) = *disr_field;
9c6a1327
TT
9753 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9754 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9755
9756 /* We need a way to find the correct discriminant given a
9757 variant name. For convenience we build a map here. */
b6cdac4b 9758 struct type *enum_type = disr_field->type ();
c9317f21 9759 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9760 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9761 {
9762 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9763 {
9764 const char *name
9765 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9766 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9767 }
9768 }
9769
1f704f76 9770 int n_fields = type->num_fields ();
9c6a1327
TT
9771 /* We don't need a range entry for the discriminant, but we do
9772 need one for every other field, as there is no default
9773 variant. */
9774 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9775 discriminant_range,
9776 n_fields - 1);
c9317f21
TT
9777 /* Skip the discriminant here. */
9778 for (int i = 1; i < n_fields; ++i)
9779 {
9780 /* Find the final word in the name of this variant's type.
9781 That name can be used to look up the correct
9782 discriminant. */
9783 const char *variant_name
940da03e 9784 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9785
9786 auto iter = discriminant_map.find (variant_name);
9787 if (iter != discriminant_map.end ())
9c6a1327 9788 {
57d02173
TT
9789 ranges[i - 1].low = iter->second;
9790 ranges[i - 1].high = iter->second;
9c6a1327 9791 }
c9317f21 9792
57d02173
TT
9793 /* In Rust, each element should have the size of the
9794 enclosing enum. */
9795 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9796
bedda9ac 9797 /* Remove the discriminant field, if it exists. */
940da03e 9798 struct type *sub_type = type->field (i).type ();
1f704f76 9799 if (sub_type->num_fields () > 0)
bedda9ac 9800 {
5e33d5f4 9801 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9802 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9803 }
9c6a1327 9804 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9805 sub_type->set_name
9806 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9807 type->name (), variant_name));
c9317f21 9808 }
9c6a1327
TT
9809
9810 /* Indicate that this is a variant type. */
a1520ad8 9811 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9812 gdb::array_view<discriminant_range> (ranges,
9813 n_fields - 1));
c9317f21
TT
9814 }
9815}
9816
9817/* Rewrite some Rust unions to be structures with variants parts. */
9818
9819static void
9820rust_union_quirks (struct dwarf2_cu *cu)
9821{
9822 gdb_assert (cu->language == language_rust);
52941706 9823 for (type *type_ : cu->rust_unions)
5e22e966 9824 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9825 /* We don't need this any more. */
9826 cu->rust_unions.clear ();
c9317f21
TT
9827}
9828
8adb8487
TT
9829/* See read.h. */
9830
9831type_unit_group_unshareable *
9832dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9833{
9834 auto iter = this->m_type_units.find (tu_group);
9835 if (iter != this->m_type_units.end ())
9836 return iter->second.get ();
9837
9838 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9839 type_unit_group_unshareable *result = uniq.get ();
9840 this->m_type_units[tu_group] = std::move (uniq);
9841 return result;
9842}
9843
e286671b
TT
9844struct type *
9845dwarf2_per_objfile::get_type_for_signatured_type
9846 (signatured_type *sig_type) const
9847{
9848 auto iter = this->m_type_map.find (sig_type);
9849 if (iter == this->m_type_map.end ())
9850 return nullptr;
9851
9852 return iter->second;
9853}
9854
9855void dwarf2_per_objfile::set_type_for_signatured_type
9856 (signatured_type *sig_type, struct type *type)
9857{
9858 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9859
9860 this->m_type_map[sig_type] = type;
9861}
9862
95554aad
TT
9863/* A helper function for computing the list of all symbol tables
9864 included by PER_CU. */
9865
9866static void
4c39bc03 9867recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9868 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9869 dwarf2_per_cu_data *per_cu,
9870 dwarf2_per_objfile *per_objfile,
43f3e411 9871 struct compunit_symtab *immediate_parent)
95554aad 9872{
af758d11 9873 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9874 if (*slot != NULL)
9875 {
9876 /* This inclusion and its children have been processed. */
9877 return;
9878 }
9879
9880 *slot = per_cu;
af758d11 9881
95554aad 9882 /* Only add a CU if it has a symbol table. */
43182c09 9883 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9884 if (cust != NULL)
ec94af83
DE
9885 {
9886 /* If this is a type unit only add its symbol table if we haven't
9887 seen it yet (type unit per_cu's can share symtabs). */
9888 if (per_cu->is_debug_types)
9889 {
43f3e411 9890 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9891 if (*slot == NULL)
9892 {
43f3e411 9893 *slot = cust;
4c39bc03 9894 result->push_back (cust);
43f3e411
DE
9895 if (cust->user == NULL)
9896 cust->user = immediate_parent;
ec94af83
DE
9897 }
9898 }
9899 else
f9125b6c 9900 {
4c39bc03 9901 result->push_back (cust);
43f3e411
DE
9902 if (cust->user == NULL)
9903 cust->user = immediate_parent;
f9125b6c 9904 }
ec94af83 9905 }
95554aad 9906
ae640021
AB
9907 if (!per_cu->imported_symtabs_empty ())
9908 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9909 {
9910 recursively_compute_inclusions (result, all_children,
43182c09
SM
9911 all_type_symtabs, ptr, per_objfile,
9912 cust);
ae640021 9913 }
95554aad
TT
9914}
9915
43f3e411 9916/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9917 PER_CU. */
9918
9919static void
43182c09
SM
9920compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9921 dwarf2_per_objfile *per_objfile)
95554aad 9922{
f4dc4d17
DE
9923 gdb_assert (! per_cu->is_debug_types);
9924
ae640021 9925 if (!per_cu->imported_symtabs_empty ())
95554aad 9926 {
ae640021 9927 int len;
4c39bc03 9928 std::vector<compunit_symtab *> result_symtabs;
43182c09 9929 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9930
9931 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9932 if (cust == NULL)
95554aad
TT
9933 return;
9934
280a9412
TT
9935 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9936 htab_eq_pointer,
9937 NULL, xcalloc, xfree));
9938 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9939 htab_eq_pointer,
9940 NULL, xcalloc, xfree));
95554aad 9941
ae640021 9942 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83 9943 {
280a9412
TT
9944 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9945 all_type_symtabs.get (), ptr,
9946 per_objfile, cust);
ec94af83 9947 }
95554aad 9948
ec94af83 9949 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9950 len = result_symtabs.size ();
43f3e411 9951 cust->includes
f6e649dd 9952 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9953 struct compunit_symtab *, len + 1);
4c39bc03
TT
9954 memcpy (cust->includes, result_symtabs.data (),
9955 len * sizeof (compunit_symtab *));
43f3e411 9956 cust->includes[len] = NULL;
95554aad
TT
9957 }
9958}
9959
9960/* Compute the 'includes' field for the symtabs of all the CUs we just
9961 read. */
9962
9963static void
976ca316 9964process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9965{
976ca316 9966 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9967 {
9968 if (! iter->is_debug_types)
976ca316 9969 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9970 }
95554aad 9971
976ca316 9972 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9973}
9974
8fc0b21d 9975/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9976 already been loaded into memory. */
9977
9978static void
8fc0b21d 9979process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9980{
976ca316
SM
9981 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9982 struct objfile *objfile = per_objfile->objfile;
08feed99 9983 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9984 CORE_ADDR lowpc, highpc;
43f3e411 9985 struct compunit_symtab *cust;
10b3939b 9986 CORE_ADDR baseaddr;
4359dff1 9987 struct block *static_block;
3e29f34a 9988 CORE_ADDR addr;
10b3939b 9989
b3b3bada 9990 baseaddr = objfile->text_section_offset ();
10b3939b 9991
c89b44cd
TT
9992 /* Clear the list here in case something was left over. */
9993 cu->method_list.clear ();
10b3939b 9994
95554aad
TT
9995 cu->language = pretend_language;
9996 cu->language_defn = language_def (cu->language);
9997
1c47ec3e
TV
9998 dwarf2_find_base_address (cu->dies, cu);
9999
702cf3f5
AB
10000 /* Before we start reading the top-level DIE, ensure it has a valid tag
10001 type. */
10002 switch (cu->dies->tag)
10003 {
10004 case DW_TAG_compile_unit:
10005 case DW_TAG_partial_unit:
10006 case DW_TAG_type_unit:
10007 break;
10008 default:
10009 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
10010 dwarf_tag_name (cu->dies->tag),
10011 sect_offset_str (cu->per_cu->sect_off),
10012 objfile_name (per_objfile->objfile));
10013 }
10014
c906108c 10015 /* Do line number decoding in read_file_scope () */
10b3939b 10016 process_die (cu->dies, cu);
c906108c 10017
a766d390
DE
10018 /* For now fudge the Go package. */
10019 if (cu->language == language_go)
10020 fixup_go_packaging (cu);
10021
5f48f8f3 10022 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10023 should be complete, and it should now be safe to compute all of the
10024 physnames. */
10025 compute_delayed_physnames (cu);
3da10d80 10026
c9317f21
TT
10027 if (cu->language == language_rust)
10028 rust_union_quirks (cu);
10029
fae299cd
DC
10030 /* Some compilers don't define a DW_AT_high_pc attribute for the
10031 compilation unit. If the DW_AT_high_pc is missing, synthesize
10032 it, by scanning the DIE's below the compilation unit. */
10b3939b 10033 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10034
3e29f34a 10035 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10036 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10037
10038 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10039 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10040 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10041 addrmap to help ensure it has an accurate map of pc values belonging to
10042 this comp unit. */
10043 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10044
c24bdb02 10045 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10046 SECT_OFF_TEXT (objfile),
10047 0);
c906108c 10048
43f3e411 10049 if (cust != NULL)
c906108c 10050 {
df15bd07 10051 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10052
8be455d7
JK
10053 /* Set symtab language to language from DW_AT_language. If the
10054 compilation is from a C file generated by language preprocessors, do
10055 not set the language if it was already deduced by start_subfile. */
43f3e411 10056 if (!(cu->language == language_c
40e3ad0e 10057 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10058 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10059
10060 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10061 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10062 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10063 there were bugs in prologue debug info, fixed later in GCC-4.5
10064 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10065
10066 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10067 needed, it would be wrong due to missing DW_AT_producer there.
10068
10069 Still one can confuse GDB by using non-standard GCC compilation
10070 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10071 */
ab260dad 10072 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10073 cust->locations_valid = 1;
e0d00bc7
JK
10074
10075 if (gcc_4_minor >= 5)
43f3e411 10076 cust->epilogue_unwind_valid = 1;
96408a79 10077
43f3e411 10078 cust->call_site_htab = cu->call_site_htab;
c906108c 10079 }
9291a0cd 10080
976ca316 10081 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 10082
95554aad 10083 /* Push it for inclusion processing later. */
976ca316 10084 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
10085
10086 /* Not needed any more. */
c24bdb02 10087 cu->reset_builder ();
f4dc4d17 10088}
45cfd468 10089
8fc0b21d 10090/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
10091 already been loaded into memory. */
10092
10093static void
8fc0b21d 10094process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
10095 enum language pretend_language)
10096{
976ca316
SM
10097 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10098 struct objfile *objfile = per_objfile->objfile;
43f3e411 10099 struct compunit_symtab *cust;
0186c6a7
DE
10100 struct signatured_type *sig_type;
10101
8fc0b21d
SM
10102 gdb_assert (cu->per_cu->is_debug_types);
10103 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 10104
c89b44cd
TT
10105 /* Clear the list here in case something was left over. */
10106 cu->method_list.clear ();
f4dc4d17 10107
f4dc4d17
DE
10108 cu->language = pretend_language;
10109 cu->language_defn = language_def (cu->language);
10110
10111 /* The symbol tables are set up in read_type_unit_scope. */
10112 process_die (cu->dies, cu);
10113
10114 /* For now fudge the Go package. */
10115 if (cu->language == language_go)
10116 fixup_go_packaging (cu);
10117
5f48f8f3 10118 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10119 should be complete, and it should now be safe to compute all of the
10120 physnames. */
10121 compute_delayed_physnames (cu);
f4dc4d17 10122
c9317f21
TT
10123 if (cu->language == language_rust)
10124 rust_union_quirks (cu);
10125
f4dc4d17
DE
10126 /* TUs share symbol tables.
10127 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10128 of it with end_expandable_symtab. Otherwise, complete the addition of
10129 this TU's symbols to the existing symtab. */
8adb8487 10130 type_unit_group_unshareable *tug_unshare =
976ca316 10131 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 10132 if (tug_unshare->compunit_symtab == NULL)
45cfd468 10133 {
c24bdb02
KS
10134 buildsym_compunit *builder = cu->get_builder ();
10135 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 10136 tug_unshare->compunit_symtab = cust;
f4dc4d17 10137
43f3e411 10138 if (cust != NULL)
f4dc4d17
DE
10139 {
10140 /* Set symtab language to language from DW_AT_language. If the
10141 compilation is from a C file generated by language preprocessors,
10142 do not set the language if it was already deduced by
10143 start_subfile. */
43f3e411
DE
10144 if (!(cu->language == language_c
10145 && COMPUNIT_FILETABS (cust)->language != language_c))
10146 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10147 }
10148 }
10149 else
10150 {
c24bdb02 10151 cu->get_builder ()->augment_type_symtab ();
8adb8487 10152 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
10153 }
10154
976ca316 10155 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
10156
10157 /* Not needed any more. */
c24bdb02 10158 cu->reset_builder ();
c906108c
SS
10159}
10160
95554aad
TT
10161/* Process an imported unit DIE. */
10162
10163static void
10164process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10165{
10166 struct attribute *attr;
10167
f4dc4d17
DE
10168 /* For now we don't handle imported units in type units. */
10169 if (cu->per_cu->is_debug_types)
10170 {
10171 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10172 " supported in type units [in module %s]"),
5e22e966 10173 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
10174 }
10175
95554aad
TT
10176 attr = dwarf2_attr (die, DW_AT_import, cu);
10177 if (attr != NULL)
10178 {
0826b30a 10179 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 10180 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 10181 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 10182 dwarf2_per_cu_data *per_cu
ab432490 10183 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 10184
58990295
TV
10185 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10186 into another compilation unit, at root level. Regard this as a hint,
10187 and ignore it. */
10188 if (die->parent && die->parent->parent == NULL
10189 && per_cu->unit_type == DW_UT_compile
10190 && per_cu->lang == language_cplus)
10191 return;
10192
69d751e3 10193 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 10194 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
10195 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10196 false, cu->language);
95554aad 10197
ae640021 10198 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10199 }
10200}
10201
4c8aa72d
PA
10202/* RAII object that represents a process_die scope: i.e.,
10203 starts/finishes processing a DIE. */
10204class process_die_scope
adde2bff 10205{
4c8aa72d
PA
10206public:
10207 process_die_scope (die_info *die, dwarf2_cu *cu)
10208 : m_die (die), m_cu (cu)
10209 {
10210 /* We should only be processing DIEs not already in process. */
10211 gdb_assert (!m_die->in_process);
10212 m_die->in_process = true;
10213 }
8c3cb9fa 10214
4c8aa72d
PA
10215 ~process_die_scope ()
10216 {
10217 m_die->in_process = false;
10218
10219 /* If we're done processing the DIE for the CU that owns the line
10220 header, we don't need the line header anymore. */
10221 if (m_cu->line_header_die_owner == m_die)
10222 {
10223 delete m_cu->line_header;
10224 m_cu->line_header = NULL;
10225 m_cu->line_header_die_owner = NULL;
10226 }
10227 }
10228
10229private:
10230 die_info *m_die;
10231 dwarf2_cu *m_cu;
10232};
adde2bff 10233
c906108c
SS
10234/* Process a die and its children. */
10235
10236static void
e7c27a73 10237process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10238{
4c8aa72d 10239 process_die_scope scope (die, cu);
adde2bff 10240
c906108c
SS
10241 switch (die->tag)
10242 {
10243 case DW_TAG_padding:
10244 break;
10245 case DW_TAG_compile_unit:
95554aad 10246 case DW_TAG_partial_unit:
e7c27a73 10247 read_file_scope (die, cu);
c906108c 10248 break;
348e048f
DE
10249 case DW_TAG_type_unit:
10250 read_type_unit_scope (die, cu);
10251 break;
c906108c 10252 case DW_TAG_subprogram:
0a4b0913
AB
10253 /* Nested subprograms in Fortran get a prefix. */
10254 if (cu->language == language_fortran
10255 && die->parent != NULL
10256 && die->parent->tag == DW_TAG_subprogram)
10257 cu->processing_has_namespace_info = true;
10258 /* Fall through. */
c906108c 10259 case DW_TAG_inlined_subroutine:
edb3359d 10260 read_func_scope (die, cu);
c906108c
SS
10261 break;
10262 case DW_TAG_lexical_block:
14898363
L
10263 case DW_TAG_try_block:
10264 case DW_TAG_catch_block:
e7c27a73 10265 read_lexical_block_scope (die, cu);
c906108c 10266 break;
216f72a1 10267 case DW_TAG_call_site:
96408a79
SA
10268 case DW_TAG_GNU_call_site:
10269 read_call_site_scope (die, cu);
10270 break;
c906108c 10271 case DW_TAG_class_type:
680b30c7 10272 case DW_TAG_interface_type:
c906108c
SS
10273 case DW_TAG_structure_type:
10274 case DW_TAG_union_type:
134d01f1 10275 process_structure_scope (die, cu);
c906108c
SS
10276 break;
10277 case DW_TAG_enumeration_type:
134d01f1 10278 process_enumeration_scope (die, cu);
c906108c 10279 break;
134d01f1 10280
f792889a
DJ
10281 /* These dies have a type, but processing them does not create
10282 a symbol or recurse to process the children. Therefore we can
10283 read them on-demand through read_type_die. */
c906108c 10284 case DW_TAG_subroutine_type:
72019c9c 10285 case DW_TAG_set_type:
c906108c 10286 case DW_TAG_pointer_type:
c906108c 10287 case DW_TAG_ptr_to_member_type:
c906108c 10288 case DW_TAG_reference_type:
4297a3f0 10289 case DW_TAG_rvalue_reference_type:
c906108c 10290 case DW_TAG_string_type:
c906108c 10291 break;
134d01f1 10292
d8f62e84
TT
10293 case DW_TAG_array_type:
10294 /* We only need to handle this case for Ada -- in other
10295 languages, it's normal for the compiler to emit a typedef
10296 instead. */
10297 if (cu->language != language_ada)
10298 break;
10299 /* FALLTHROUGH */
c906108c 10300 case DW_TAG_base_type:
a02abb62 10301 case DW_TAG_subrange_type:
cb249c71 10302 case DW_TAG_typedef:
134d01f1 10303 /* Add a typedef symbol for the type definition, if it has a
dda83cd7 10304 DW_AT_name. */
f792889a 10305 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10306 break;
c906108c 10307 case DW_TAG_common_block:
e7c27a73 10308 read_common_block (die, cu);
c906108c
SS
10309 break;
10310 case DW_TAG_common_inclusion:
10311 break;
d9fa45fe 10312 case DW_TAG_namespace:
9068261f 10313 cu->processing_has_namespace_info = true;
e7c27a73 10314 read_namespace (die, cu);
d9fa45fe 10315 break;
5d7cb8df 10316 case DW_TAG_module:
9068261f 10317 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10318 read_module (die, cu);
10319 break;
d9fa45fe 10320 case DW_TAG_imported_declaration:
9068261f 10321 cu->processing_has_namespace_info = true;
74921315
KS
10322 if (read_namespace_alias (die, cu))
10323 break;
86a73007
TT
10324 /* The declaration is not a global namespace alias. */
10325 /* Fall through. */
d9fa45fe 10326 case DW_TAG_imported_module:
9068261f 10327 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10328 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10329 || cu->language != language_fortran))
b98664d3 10330 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10331 dwarf_tag_name (die->tag));
10332 read_import_statement (die, cu);
d9fa45fe 10333 break;
95554aad
TT
10334
10335 case DW_TAG_imported_unit:
10336 process_imported_unit_die (die, cu);
10337 break;
10338
71a3c369
TT
10339 case DW_TAG_variable:
10340 read_variable (die, cu);
10341 break;
10342
c906108c 10343 default:
e7c27a73 10344 new_symbol (die, NULL, cu);
c906108c
SS
10345 break;
10346 }
10347}
ca69b9e6
DE
10348\f
10349/* DWARF name computation. */
c906108c 10350
94af9270
KS
10351/* A helper function for dwarf2_compute_name which determines whether DIE
10352 needs to have the name of the scope prepended to the name listed in the
10353 die. */
10354
10355static int
10356die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10357{
1c809c68
TT
10358 struct attribute *attr;
10359
94af9270
KS
10360 switch (die->tag)
10361 {
10362 case DW_TAG_namespace:
10363 case DW_TAG_typedef:
10364 case DW_TAG_class_type:
10365 case DW_TAG_interface_type:
10366 case DW_TAG_structure_type:
10367 case DW_TAG_union_type:
10368 case DW_TAG_enumeration_type:
10369 case DW_TAG_enumerator:
10370 case DW_TAG_subprogram:
08a76f8a 10371 case DW_TAG_inlined_subroutine:
94af9270 10372 case DW_TAG_member:
74921315 10373 case DW_TAG_imported_declaration:
94af9270
KS
10374 return 1;
10375
10376 case DW_TAG_variable:
c2b0a229 10377 case DW_TAG_constant:
94af9270
KS
10378 /* We only need to prefix "globally" visible variables. These include
10379 any variable marked with DW_AT_external or any variable that
10380 lives in a namespace. [Variables in anonymous namespaces
10381 require prefixing, but they are not DW_AT_external.] */
10382
10383 if (dwarf2_attr (die, DW_AT_specification, cu))
10384 {
10385 struct dwarf2_cu *spec_cu = cu;
9a619af0 10386
94af9270
KS
10387 return die_needs_namespace (die_specification (die, &spec_cu),
10388 spec_cu);
10389 }
10390
1c809c68 10391 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10392 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10393 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10394 return 0;
10395 /* A variable in a lexical block of some kind does not need a
10396 namespace, even though in C++ such variables may be external
10397 and have a mangled name. */
10398 if (die->parent->tag == DW_TAG_lexical_block
10399 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10400 || die->parent->tag == DW_TAG_catch_block
10401 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10402 return 0;
10403 return 1;
94af9270
KS
10404
10405 default:
10406 return 0;
10407 }
10408}
10409
73b9be8b
KS
10410/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10411 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10412 defined for the given DIE. */
10413
10414static struct attribute *
10415dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10416{
10417 struct attribute *attr;
10418
10419 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10420 if (attr == NULL)
10421 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10422
10423 return attr;
10424}
10425
10426/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10427 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10428 defined for the given DIE. */
10429
10430static const char *
10431dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10432{
10433 const char *linkage_name;
10434
10435 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10436 if (linkage_name == NULL)
10437 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10438
787de330
TT
10439 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10440 See https://github.com/rust-lang/rust/issues/32925. */
10441 if (cu->language == language_rust && linkage_name != NULL
10442 && strchr (linkage_name, '{') != NULL)
10443 linkage_name = NULL;
10444
73b9be8b
KS
10445 return linkage_name;
10446}
10447
94af9270 10448/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10449 compute the physname for the object, which include a method's:
9c37b5ae 10450 - formal parameters (C++),
a766d390 10451 - receiver type (Go),
a766d390
DE
10452
10453 The term "physname" is a bit confusing.
10454 For C++, for example, it is the demangled name.
10455 For Go, for example, it's the mangled name.
94af9270 10456
af6b7be1
JB
10457 For Ada, return the DIE's linkage name rather than the fully qualified
10458 name. PHYSNAME is ignored..
10459
5989a64e 10460 The result is allocated on the objfile->per_bfd's obstack and
45940949 10461 canonicalized. */
94af9270
KS
10462
10463static const char *
15d034d0
TT
10464dwarf2_compute_name (const char *name,
10465 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10466 int physname)
10467{
5e22e966 10468 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10469
94af9270
KS
10470 if (name == NULL)
10471 name = dwarf2_name (die, cu);
10472
2ee7123e
DE
10473 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10474 but otherwise compute it by typename_concat inside GDB.
10475 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10476 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10477 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10478 will set the demangled name to the result of dwarf2_full_name, and it is
10479 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10480 if (cu->language == language_ada
10481 || (cu->language == language_fortran && physname))
10482 {
10483 /* For Ada unit, we prefer the linkage name over the name, as
10484 the former contains the exported name, which the user expects
10485 to be able to reference. Ideally, we want the user to be able
10486 to reference this entity using either natural or linkage name,
10487 but we haven't started looking at this enhancement yet. */
73b9be8b 10488 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10489
2ee7123e
DE
10490 if (linkage_name != NULL)
10491 return linkage_name;
f55ee35c
JK
10492 }
10493
94af9270
KS
10494 /* These are the only languages we know how to qualify names in. */
10495 if (name != NULL
9c37b5ae 10496 && (cu->language == language_cplus
c44af4eb
TT
10497 || cu->language == language_fortran || cu->language == language_d
10498 || cu->language == language_rust))
94af9270
KS
10499 {
10500 if (die_needs_namespace (die, cu))
10501 {
0d5cff50 10502 const char *prefix;
34a68019 10503 const char *canonical_name = NULL;
94af9270 10504
d7e74731
PA
10505 string_file buf;
10506
94af9270 10507 prefix = determine_prefix (die, cu);
94af9270
KS
10508 if (*prefix != '\0')
10509 {
43816ebc
TT
10510 gdb::unique_xmalloc_ptr<char> prefixed_name
10511 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10512
43816ebc 10513 buf.puts (prefixed_name.get ());
94af9270
KS
10514 }
10515 else
d7e74731 10516 buf.puts (name);
94af9270 10517
98bfdba5
PA
10518 /* Template parameters may be specified in the DIE's DW_AT_name, or
10519 as children with DW_TAG_template_type_param or
10520 DW_TAG_value_type_param. If the latter, add them to the name
10521 here. If the name already has template parameters, then
10522 skip this step; some versions of GCC emit both, and
10523 it is more efficient to use the pre-computed name.
10524
10525 Something to keep in mind about this process: it is very
10526 unlikely, or in some cases downright impossible, to produce
10527 something that will match the mangled name of a function.
10528 If the definition of the function has the same debug info,
10529 we should be able to match up with it anyway. But fallbacks
10530 using the minimal symbol, for instance to find a method
10531 implemented in a stripped copy of libstdc++, will not work.
10532 If we do not have debug info for the definition, we will have to
10533 match them up some other way.
10534
10535 When we do name matching there is a related problem with function
10536 templates; two instantiated function templates are allowed to
10537 differ only by their return types, which we do not add here. */
10538
10539 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10540 {
10541 struct attribute *attr;
10542 struct die_info *child;
10543 int first = 1;
2c75ccb2 10544 const language_defn *cplus_lang = language_def (cu->language);
98bfdba5
PA
10545
10546 die->building_fullname = 1;
10547
10548 for (child = die->child; child != NULL; child = child->sibling)
10549 {
10550 struct type *type;
12df843f 10551 LONGEST value;
d521ce57 10552 const gdb_byte *bytes;
98bfdba5
PA
10553 struct dwarf2_locexpr_baton *baton;
10554 struct value *v;
10555
10556 if (child->tag != DW_TAG_template_type_param
10557 && child->tag != DW_TAG_template_value_param)
10558 continue;
10559
10560 if (first)
10561 {
d7e74731 10562 buf.puts ("<");
98bfdba5
PA
10563 first = 0;
10564 }
10565 else
d7e74731 10566 buf.puts (", ");
98bfdba5
PA
10567
10568 attr = dwarf2_attr (child, DW_AT_type, cu);
10569 if (attr == NULL)
10570 {
b98664d3 10571 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10572 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10573 continue;
10574 }
10575 type = die_type (child, cu);
10576
10577 if (child->tag == DW_TAG_template_type_param)
10578 {
2c75ccb2
AB
10579 cplus_lang->print_type (type, "", &buf, -1, 0,
10580 &type_print_raw_options);
98bfdba5
PA
10581 continue;
10582 }
10583
10584 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10585 if (attr == NULL)
10586 {
b98664d3 10587 complaint (_("template parameter missing "
3e43a32a 10588 "DW_AT_const_value"));
d7e74731 10589 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10590 continue;
10591 }
10592
10593 dwarf2_const_value_attr (attr, type, name,
10594 &cu->comp_unit_obstack, cu,
10595 &value, &bytes, &baton);
10596
20ce4123 10597 if (type->has_no_signedness ())
98bfdba5
PA
10598 /* GDB prints characters as NUMBER 'CHAR'. If that's
10599 changed, this can use value_print instead. */
2c75ccb2 10600 cplus_lang->printchar (value, type, &buf);
98bfdba5
PA
10601 else
10602 {
10603 struct value_print_options opts;
10604
10605 if (baton != NULL)
10606 v = dwarf2_evaluate_loc_desc (type, NULL,
10607 baton->data,
10608 baton->size,
9f47c707
SM
10609 baton->per_cu,
10610 baton->per_objfile);
98bfdba5
PA
10611 else if (bytes != NULL)
10612 {
10613 v = allocate_value (type);
10614 memcpy (value_contents_writeable (v), bytes,
10615 TYPE_LENGTH (type));
10616 }
10617 else
10618 v = value_from_longest (type, value);
10619
3e43a32a
MS
10620 /* Specify decimal so that we do not depend on
10621 the radix. */
98bfdba5
PA
10622 get_formatted_print_options (&opts, 'd');
10623 opts.raw = 1;
d7e74731 10624 value_print (v, &buf, &opts);
98bfdba5 10625 release_value (v);
98bfdba5
PA
10626 }
10627 }
10628
10629 die->building_fullname = 0;
10630
10631 if (!first)
10632 {
10633 /* Close the argument list, with a space if necessary
10634 (nested templates). */
d7e74731
PA
10635 if (!buf.empty () && buf.string ().back () == '>')
10636 buf.puts (" >");
98bfdba5 10637 else
d7e74731 10638 buf.puts (">");
98bfdba5
PA
10639 }
10640 }
10641
9c37b5ae 10642 /* For C++ methods, append formal parameter type
94af9270 10643 information, if PHYSNAME. */
6e70227d 10644
94af9270 10645 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10646 && cu->language == language_cplus)
94af9270
KS
10647 {
10648 struct type *type = read_type_die (die, cu);
10649
d7e74731 10650 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10651 &type_print_raw_options);
94af9270 10652
9c37b5ae 10653 if (cu->language == language_cplus)
94af9270 10654 {
60430eff
DJ
10655 /* Assume that an artificial first parameter is
10656 "this", but do not crash if it is not. RealView
10657 marks unnamed (and thus unused) parameters as
10658 artificial; there is no way to differentiate
10659 the two cases. */
1f704f76 10660 if (type->num_fields () > 0
94af9270 10661 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
10662 && type->field (0).type ()->code () == TYPE_CODE_PTR
10663 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 10664 buf.puts (" const");
94af9270
KS
10665 }
10666 }
10667
d7e74731 10668 const std::string &intermediate_name = buf.string ();
94af9270
KS
10669
10670 if (cu->language == language_cplus)
34a68019 10671 canonical_name
322a8516 10672 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10673 objfile);
34a68019
TT
10674
10675 /* If we only computed INTERMEDIATE_NAME, or if
10676 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10677 intern it. */
322a8516 10678 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10679 name = objfile->intern (intermediate_name);
34a68019
TT
10680 else
10681 name = canonical_name;
94af9270
KS
10682 }
10683 }
10684
10685 return name;
10686}
10687
0114d602
DJ
10688/* Return the fully qualified name of DIE, based on its DW_AT_name.
10689 If scope qualifiers are appropriate they will be added. The result
34a68019 10690 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10691 not have a name. NAME may either be from a previous call to
10692 dwarf2_name or NULL.
10693
9c37b5ae 10694 The output string will be canonicalized (if C++). */
0114d602
DJ
10695
10696static const char *
15d034d0 10697dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10698{
94af9270
KS
10699 return dwarf2_compute_name (name, die, cu, 0);
10700}
0114d602 10701
94af9270
KS
10702/* Construct a physname for the given DIE in CU. NAME may either be
10703 from a previous call to dwarf2_name or NULL. The result will be
10704 allocated on the objfile_objstack or NULL if the DIE does not have a
10705 name.
0114d602 10706
9c37b5ae 10707 The output string will be canonicalized (if C++). */
0114d602 10708
94af9270 10709static const char *
15d034d0 10710dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10711{
5e22e966 10712 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10713 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10714 int need_copy = 1;
10715
10716 /* In this case dwarf2_compute_name is just a shortcut not building anything
10717 on its own. */
10718 if (!die_needs_namespace (die, cu))
10719 return dwarf2_compute_name (name, die, cu, 1);
10720
906bb4c5
TT
10721 if (cu->language != language_rust)
10722 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10723
10724 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10725 has computed. */
791afaa2 10726 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10727 if (mangled != NULL)
900e11f9 10728 {
900e11f9 10729
d3355e4d 10730 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
59cc4834
JB
10731 {
10732 /* Do nothing (do not demangle the symbol name). */
10733 }
a766d390
DE
10734 else
10735 {
0eb876f5
JB
10736 /* Use DMGL_RET_DROP for C++ template functions to suppress
10737 their return type. It is easier for GDB users to search
10738 for such functions as `name(params)' than `long name(params)'.
10739 In such case the minimal symbol names do not match the full
10740 symbol names but for template functions there is never a need
10741 to look up their definition from their declaration so
10742 the only disadvantage remains the minimal symbol variant
10743 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10744 demangled.reset (gdb_demangle (mangled,
10745 (DMGL_PARAMS | DMGL_ANSI
10746 | DMGL_RET_DROP)));
a766d390 10747 }
900e11f9 10748 if (demangled)
791afaa2 10749 canon = demangled.get ();
900e11f9
JK
10750 else
10751 {
10752 canon = mangled;
10753 need_copy = 0;
10754 }
10755 }
10756
10757 if (canon == NULL || check_physname)
10758 {
10759 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10760
10761 if (canon != NULL && strcmp (physname, canon) != 0)
10762 {
10763 /* It may not mean a bug in GDB. The compiler could also
10764 compute DW_AT_linkage_name incorrectly. But in such case
10765 GDB would need to be bug-to-bug compatible. */
10766
b98664d3 10767 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10768 "(from linkage <%s>) - DIE at %s [in module %s]"),
10769 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10770 objfile_name (objfile));
900e11f9
JK
10771
10772 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10773 is available here - over computed PHYSNAME. It is safer
10774 against both buggy GDB and buggy compilers. */
10775
10776 retval = canon;
10777 }
10778 else
10779 {
10780 retval = physname;
10781 need_copy = 0;
10782 }
10783 }
10784 else
10785 retval = canon;
10786
10787 if (need_copy)
be1e3d3e 10788 retval = objfile->intern (retval);
900e11f9 10789
900e11f9 10790 return retval;
0114d602
DJ
10791}
10792
74921315
KS
10793/* Inspect DIE in CU for a namespace alias. If one exists, record
10794 a new symbol for it.
10795
10796 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10797
10798static int
10799read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10800{
10801 struct attribute *attr;
10802
10803 /* If the die does not have a name, this is not a namespace
10804 alias. */
10805 attr = dwarf2_attr (die, DW_AT_name, cu);
10806 if (attr != NULL)
10807 {
10808 int num;
10809 struct die_info *d = die;
10810 struct dwarf2_cu *imported_cu = cu;
10811
10812 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10813 keep inspecting DIEs until we hit the underlying import. */
10814#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10815 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10816 {
10817 attr = dwarf2_attr (d, DW_AT_import, cu);
10818 if (attr == NULL)
10819 break;
10820
10821 d = follow_die_ref (d, attr, &imported_cu);
10822 if (d->tag != DW_TAG_imported_declaration)
10823 break;
10824 }
10825
10826 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10827 {
b98664d3 10828 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10829 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10830 return 0;
10831 }
10832
10833 if (attr != NULL)
10834 {
10835 struct type *type;
0826b30a 10836 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10837
aa66c379 10838 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10839 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10840 {
10841 /* This declaration is a global namespace alias. Add
10842 a symbol for it whose type is the aliased namespace. */
10843 new_symbol (die, type, cu);
10844 return 1;
10845 }
10846 }
10847 }
10848
10849 return 0;
10850}
10851
22cee43f 10852/* Return the using directives repository (global or local?) to use in the
804d2729 10853 current context for CU.
22cee43f
PMR
10854
10855 For Ada, imported declarations can materialize renamings, which *may* be
10856 global. However it is impossible (for now?) in DWARF to distinguish
10857 "external" imported declarations and "static" ones. As all imported
10858 declarations seem to be static in all other languages, make them all CU-wide
10859 global only in Ada. */
10860
10861static struct using_direct **
804d2729 10862using_directives (struct dwarf2_cu *cu)
22cee43f 10863{
c24bdb02
KS
10864 if (cu->language == language_ada
10865 && cu->get_builder ()->outermost_context_p ())
10866 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10867 else
c24bdb02 10868 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10869}
10870
27aa8d6a
SW
10871/* Read the import statement specified by the given die and record it. */
10872
10873static void
10874read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10875{
5e22e966 10876 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10877 struct attribute *import_attr;
32019081 10878 struct die_info *imported_die, *child_die;
de4affc9 10879 struct dwarf2_cu *imported_cu;
27aa8d6a 10880 const char *imported_name;
794684b6 10881 const char *imported_name_prefix;
13387711
SW
10882 const char *canonical_name;
10883 const char *import_alias;
10884 const char *imported_declaration = NULL;
794684b6 10885 const char *import_prefix;
eb1e02fd 10886 std::vector<const char *> excludes;
13387711 10887
27aa8d6a
SW
10888 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10889 if (import_attr == NULL)
10890 {
b98664d3 10891 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10892 dwarf_tag_name (die->tag));
10893 return;
10894 }
10895
de4affc9
CC
10896 imported_cu = cu;
10897 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10898 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10899 if (imported_name == NULL)
10900 {
10901 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10902
dda83cd7
SM
10903 The import in the following code:
10904 namespace A
10905 {
10906 typedef int B;
10907 }
10908
10909 int main ()
10910 {
10911 using A::B;
10912 B b;
10913 return b;
10914 }
10915
10916 ...
10917 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10918 <52> DW_AT_decl_file : 1
10919 <53> DW_AT_decl_line : 6
10920 <54> DW_AT_import : <0x75>
10921 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10922 <59> DW_AT_name : B
10923 <5b> DW_AT_decl_file : 1
10924 <5c> DW_AT_decl_line : 2
10925 <5d> DW_AT_type : <0x6e>
10926 ...
10927 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10928 <76> DW_AT_byte_size : 4
10929 <77> DW_AT_encoding : 5 (signed)
10930
10931 imports the wrong die ( 0x75 instead of 0x58 ).
10932 This case will be ignored until the gcc bug is fixed. */
27aa8d6a
SW
10933 return;
10934 }
10935
82856980
SW
10936 /* Figure out the local name after import. */
10937 import_alias = dwarf2_name (die, cu);
27aa8d6a 10938
794684b6
SW
10939 /* Figure out where the statement is being imported to. */
10940 import_prefix = determine_prefix (die, cu);
10941
10942 /* Figure out what the scope of the imported die is and prepend it
10943 to the name of the imported die. */
de4affc9 10944 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10945
f55ee35c
JK
10946 if (imported_die->tag != DW_TAG_namespace
10947 && imported_die->tag != DW_TAG_module)
794684b6 10948 {
13387711
SW
10949 imported_declaration = imported_name;
10950 canonical_name = imported_name_prefix;
794684b6 10951 }
13387711 10952 else if (strlen (imported_name_prefix) > 0)
12aaed36 10953 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10954 imported_name_prefix,
10955 (cu->language == language_d ? "." : "::"),
10956 imported_name, (char *) NULL);
13387711
SW
10957 else
10958 canonical_name = imported_name;
794684b6 10959
32019081
JK
10960 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10961 for (child_die = die->child; child_die && child_die->tag;
436c571c 10962 child_die = child_die->sibling)
32019081
JK
10963 {
10964 /* DWARF-4: A Fortran use statement with a “rename list” may be
10965 represented by an imported module entry with an import attribute
10966 referring to the module and owned entries corresponding to those
10967 entities that are renamed as part of being imported. */
10968
10969 if (child_die->tag != DW_TAG_imported_declaration)
10970 {
b98664d3 10971 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10972 "- DIE at %s [in module %s]"),
10973 sect_offset_str (child_die->sect_off),
10974 objfile_name (objfile));
32019081
JK
10975 continue;
10976 }
10977
10978 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10979 if (import_attr == NULL)
10980 {
b98664d3 10981 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10982 dwarf_tag_name (child_die->tag));
10983 continue;
10984 }
10985
10986 imported_cu = cu;
10987 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10988 &imported_cu);
10989 imported_name = dwarf2_name (imported_die, imported_cu);
10990 if (imported_name == NULL)
10991 {
b98664d3 10992 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10993 "imported name - DIE at %s [in module %s]"),
10994 sect_offset_str (child_die->sect_off),
10995 objfile_name (objfile));
32019081
JK
10996 continue;
10997 }
10998
eb1e02fd 10999 excludes.push_back (imported_name);
32019081
JK
11000
11001 process_die (child_die, cu);
11002 }
11003
804d2729 11004 add_using_directive (using_directives (cu),
22cee43f
PMR
11005 import_prefix,
11006 canonical_name,
11007 import_alias,
11008 imported_declaration,
11009 excludes,
11010 0,
11011 &objfile->objfile_obstack);
27aa8d6a
SW
11012}
11013
5230b05a
WT
11014/* ICC<14 does not output the required DW_AT_declaration on incomplete
11015 types, but gives them a size of zero. Starting with version 14,
11016 ICC is compatible with GCC. */
11017
9068261f 11018static bool
5230b05a
WT
11019producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11020{
11021 if (!cu->checked_producer)
11022 check_producer (cu);
11023
11024 return cu->producer_is_icc_lt_14;
11025}
11026
eb77c9df
AB
11027/* ICC generates a DW_AT_type for C void functions. This was observed on
11028 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11029 which says that void functions should not have a DW_AT_type. */
11030
11031static bool
11032producer_is_icc (struct dwarf2_cu *cu)
11033{
11034 if (!cu->checked_producer)
11035 check_producer (cu);
11036
11037 return cu->producer_is_icc;
11038}
11039
1b80a9fa
JK
11040/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11041 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11042 this, it was first present in GCC release 4.3.0. */
11043
9068261f 11044static bool
1b80a9fa
JK
11045producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11046{
11047 if (!cu->checked_producer)
11048 check_producer (cu);
11049
11050 return cu->producer_is_gcc_lt_4_3;
11051}
11052
d721ba37
PA
11053static file_and_directory
11054find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11055{
d721ba37
PA
11056 file_and_directory res;
11057
9291a0cd
TT
11058 /* Find the filename. Do not use dwarf2_name here, since the filename
11059 is not a source language identifier. */
d721ba37
PA
11060 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11061 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11062
d721ba37
PA
11063 if (res.comp_dir == NULL
11064 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11065 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11066 {
d721ba37
PA
11067 res.comp_dir_storage = ldirname (res.name);
11068 if (!res.comp_dir_storage.empty ())
11069 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11070 }
d721ba37 11071 if (res.comp_dir != NULL)
9291a0cd
TT
11072 {
11073 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11074 directory, get rid of it. */
d721ba37 11075 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11076
d721ba37
PA
11077 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11078 res.comp_dir = cp + 1;
9291a0cd
TT
11079 }
11080
d721ba37
PA
11081 if (res.name == NULL)
11082 res.name = "<unknown>";
11083
11084 return res;
9291a0cd
TT
11085}
11086
f4dc4d17
DE
11087/* Handle DW_AT_stmt_list for a compilation unit.
11088 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11089 COMP_DIR is the compilation directory. LOWPC is passed to
11090 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11091
11092static void
11093handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11094 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11095{
976ca316 11096 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 11097 struct attribute *attr;
527f3840
JK
11098 struct line_header line_header_local;
11099 hashval_t line_header_local_hash;
527f3840
JK
11100 void **slot;
11101 int decode_mapping;
2ab95328 11102
f4dc4d17
DE
11103 gdb_assert (! cu->per_cu->is_debug_types);
11104
2ab95328 11105 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e 11106 if (attr == NULL || !attr->form_is_unsigned ())
527f3840
JK
11107 return;
11108
d4df075e 11109 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
527f3840
JK
11110
11111 /* The line header hash table is only created if needed (it exists to
11112 prevent redundant reading of the line table for partial_units).
11113 If we're given a partial_unit, we'll need it. If we're given a
11114 compile_unit, then use the line header hash table if it's already
11115 created, but don't create one just yet. */
11116
976ca316 11117 if (per_objfile->line_header_hash == NULL
527f3840 11118 && die->tag == DW_TAG_partial_unit)
2ab95328 11119 {
976ca316 11120 per_objfile->line_header_hash
d15acc42
TT
11121 .reset (htab_create_alloc (127, line_header_hash_voidp,
11122 line_header_eq_voidp,
11123 free_line_header_voidp,
11124 xcalloc, xfree));
527f3840 11125 }
2ab95328 11126
9c541725 11127 line_header_local.sect_off = line_offset;
527f3840
JK
11128 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11129 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 11130 if (per_objfile->line_header_hash != NULL)
527f3840 11131 {
976ca316 11132 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11133 &line_header_local,
11134 line_header_local_hash, NO_INSERT);
11135
11136 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11137 is not present in *SLOT (since if there is something in *SLOT then
11138 it will be for a partial_unit). */
11139 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11140 {
527f3840 11141 gdb_assert (*slot != NULL);
9a3c8263 11142 cu->line_header = (struct line_header *) *slot;
527f3840 11143 return;
dee91e82 11144 }
2ab95328 11145 }
527f3840
JK
11146
11147 /* dwarf_decode_line_header does not yet provide sufficient information.
11148 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11149 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11150 if (lh == NULL)
527f3840 11151 return;
4c8aa72d
PA
11152
11153 cu->line_header = lh.release ();
11154 cu->line_header_die_owner = die;
527f3840 11155
976ca316 11156 if (per_objfile->line_header_hash == NULL)
527f3840
JK
11157 slot = NULL;
11158 else
11159 {
976ca316 11160 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11161 &line_header_local,
11162 line_header_local_hash, INSERT);
11163 gdb_assert (slot != NULL);
11164 }
11165 if (slot != NULL && *slot == NULL)
11166 {
11167 /* This newly decoded line number information unit will be owned
11168 by line_header_hash hash table. */
11169 *slot = cu->line_header;
4c8aa72d 11170 cu->line_header_die_owner = NULL;
527f3840
JK
11171 }
11172 else
11173 {
11174 /* We cannot free any current entry in (*slot) as that struct line_header
dda83cd7 11175 may be already used by multiple CUs. Create only temporary decoded
527f3840
JK
11176 line_header for this CU - it may happen at most once for each line
11177 number information unit. And if we're not using line_header_hash
11178 then this is what we want as well. */
11179 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11180 }
11181 decode_mapping = (die->tag != DW_TAG_partial_unit);
11182 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11183 decode_mapping);
fff8551c 11184
2ab95328
TT
11185}
11186
95554aad 11187/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11188
c906108c 11189static void
e7c27a73 11190read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11191{
976ca316
SM
11192 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11193 struct objfile *objfile = per_objfile->objfile;
08feed99 11194 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 11195 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11196 CORE_ADDR highpc = ((CORE_ADDR) 0);
11197 struct attribute *attr;
c906108c 11198 struct die_info *child_die;
e142c38c 11199 CORE_ADDR baseaddr;
6e70227d 11200
380618d6 11201 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11202 baseaddr = objfile->text_section_offset ();
c906108c 11203
fae299cd 11204 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11205
11206 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11207 from finish_block. */
2acceee2 11208 if (lowpc == ((CORE_ADDR) -1))
c906108c 11209 lowpc = highpc;
3e29f34a 11210 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11211
d721ba37 11212 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11213
f4b8a18d
KW
11214 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11215 standardised yet. As a workaround for the language detection we fall
11216 back to the DW_AT_producer string. */
11217 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11218 cu->language = language_opencl;
11219
3019eac3
DE
11220 /* Similar hack for Go. */
11221 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11222 set_cu_language (DW_LANG_Go, cu);
11223
c24bdb02 11224 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11225
11226 /* Decode line number information if present. We do this before
11227 processing child DIEs, so that the line header table is available
11228 for DW_AT_decl_file. */
d721ba37 11229 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11230
11231 /* Process all dies in compilation unit. */
11232 if (die->child != NULL)
11233 {
11234 child_die = die->child;
11235 while (child_die && child_die->tag)
11236 {
11237 process_die (child_die, cu);
436c571c 11238 child_die = child_die->sibling;
3019eac3
DE
11239 }
11240 }
11241
11242 /* Decode macro information, if present. Dwarf 2 macro information
11243 refers to information in the line number info statement program
11244 header, so we can only read it if we've read the header
11245 successfully. */
0af92d60
JK
11246 attr = dwarf2_attr (die, DW_AT_macros, cu);
11247 if (attr == NULL)
11248 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
529908cb 11249 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3
DE
11250 {
11251 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11252 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11253
529908cb 11254 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
3019eac3
DE
11255 }
11256 else
11257 {
11258 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
529908cb 11259 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3 11260 {
529908cb 11261 unsigned int macro_offset = attr->as_unsigned ();
3019eac3 11262
43f3e411 11263 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11264 }
11265 }
3019eac3
DE
11266}
11267
c24bdb02
KS
11268void
11269dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11270{
f4dc4d17
DE
11271 struct type_unit_group *tu_group;
11272 int first_time;
3019eac3 11273 struct attribute *attr;
9c541725 11274 unsigned int i;
0186c6a7 11275 struct signatured_type *sig_type;
3019eac3 11276
f4dc4d17 11277 gdb_assert (per_cu->is_debug_types);
0186c6a7 11278 sig_type = (struct signatured_type *) per_cu;
3019eac3 11279
c24bdb02 11280 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11281
f4dc4d17 11282 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11283 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11284 if (sig_type->type_unit_group == NULL)
c24bdb02 11285 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11286 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11287
11288 /* If we've already processed this stmt_list there's no real need to
11289 do it again, we could fake it and just recreate the part we need
11290 (file name,index -> symtab mapping). If data shows this optimization
11291 is useful we can do it then. */
8adb8487
TT
11292 type_unit_group_unshareable *tug_unshare
11293 = per_objfile->get_type_unit_group_unshareable (tu_group);
11294 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11295
11296 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11297 debug info. */
fff8551c 11298 line_header_up lh;
d4df075e 11299 if (attr != NULL && attr->form_is_unsigned ())
3019eac3 11300 {
d4df075e 11301 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
c24bdb02 11302 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11303 }
11304 if (lh == NULL)
11305 {
11306 if (first_time)
c24bdb02 11307 start_symtab ("", NULL, 0);
f4dc4d17
DE
11308 else
11309 {
8adb8487 11310 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11311 gdb_assert (m_builder == nullptr);
8adb8487 11312 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11313 m_builder.reset (new struct buildsym_compunit
11314 (COMPUNIT_OBJFILE (cust), "",
11315 COMPUNIT_DIRNAME (cust),
11316 compunit_language (cust),
11317 0, cust));
770479f2 11318 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11319 }
f4dc4d17 11320 return;
3019eac3
DE
11321 }
11322
c24bdb02
KS
11323 line_header = lh.release ();
11324 line_header_die_owner = die;
3019eac3 11325
f4dc4d17
DE
11326 if (first_time)
11327 {
c24bdb02 11328 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11329
1fd60fc0
DE
11330 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11331 still initializing it, and our caller (a few levels up)
11332 process_full_type_unit still needs to know if this is the first
11333 time. */
11334
8adb8487 11335 tug_unshare->symtabs
4ac93832
TT
11336 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11337 struct symtab *, line_header->file_names_size ());
3019eac3 11338
7ba99d21
AT
11339 auto &file_names = line_header->file_names ();
11340 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11341 {
7ba99d21 11342 file_entry &fe = file_names[i];
c24bdb02
KS
11343 dwarf2_start_subfile (this, fe.name,
11344 fe.include_dir (line_header));
11345 buildsym_compunit *b = get_builder ();
11346 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11347 {
4c8aa72d
PA
11348 /* NOTE: start_subfile will recognize when it's been
11349 passed a file it has already seen. So we can't
11350 assume there's a simple mapping from
11351 cu->line_header->file_names to subfiles, plus
11352 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11353 b->get_current_subfile ()->symtab
11354 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11355 }
11356
c24bdb02 11357 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11358 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11359 }
11360 }
11361 else
3019eac3 11362 {
c24bdb02 11363 gdb_assert (m_builder == nullptr);
8adb8487 11364 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11365 m_builder.reset (new struct buildsym_compunit
11366 (COMPUNIT_OBJFILE (cust), "",
11367 COMPUNIT_DIRNAME (cust),
11368 compunit_language (cust),
11369 0, cust));
770479f2 11370 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11371
7ba99d21
AT
11372 auto &file_names = line_header->file_names ();
11373 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11374 {
7ba99d21 11375 file_entry &fe = file_names[i];
8adb8487 11376 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11377 }
3019eac3
DE
11378 }
11379
f4dc4d17
DE
11380 /* The main symtab is allocated last. Type units don't have DW_AT_name
11381 so they don't have a "real" (so to speak) symtab anyway.
11382 There is later code that will assign the main symtab to all symbols
11383 that don't have one. We need to handle the case of a symbol with a
11384 missing symtab (DW_AT_decl_file) anyway. */
11385}
3019eac3 11386
f4dc4d17
DE
11387/* Process DW_TAG_type_unit.
11388 For TUs we want to skip the first top level sibling if it's not the
11389 actual type being defined by this TU. In this case the first top
11390 level sibling is there to provide context only. */
3019eac3 11391
f4dc4d17
DE
11392static void
11393read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11394{
11395 struct die_info *child_die;
3019eac3 11396
f4dc4d17
DE
11397 prepare_one_comp_unit (cu, die, language_minimal);
11398
11399 /* Initialize (or reinitialize) the machinery for building symtabs.
11400 We do this before processing child DIEs, so that the line header table
11401 is available for DW_AT_decl_file. */
c24bdb02 11402 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11403
11404 if (die->child != NULL)
11405 {
11406 child_die = die->child;
11407 while (child_die && child_die->tag)
11408 {
11409 process_die (child_die, cu);
436c571c 11410 child_die = child_die->sibling;
f4dc4d17
DE
11411 }
11412 }
3019eac3
DE
11413}
11414\f
80626a55
DE
11415/* DWO/DWP files.
11416
11417 http://gcc.gnu.org/wiki/DebugFission
11418 http://gcc.gnu.org/wiki/DebugFissionDWP
11419
11420 To simplify handling of both DWO files ("object" files with the DWARF info)
11421 and DWP files (a file with the DWOs packaged up into one file), we treat
11422 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11423
11424static hashval_t
11425hash_dwo_file (const void *item)
11426{
9a3c8263 11427 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11428 hashval_t hash;
3019eac3 11429
a2ce51a0
DE
11430 hash = htab_hash_string (dwo_file->dwo_name);
11431 if (dwo_file->comp_dir != NULL)
11432 hash += htab_hash_string (dwo_file->comp_dir);
11433 return hash;
3019eac3
DE
11434}
11435
11436static int
11437eq_dwo_file (const void *item_lhs, const void *item_rhs)
11438{
9a3c8263
SM
11439 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11440 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11441
a2ce51a0
DE
11442 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11443 return 0;
11444 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11445 return lhs->comp_dir == rhs->comp_dir;
11446 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11447}
11448
11449/* Allocate a hash table for DWO files. */
11450
51ac9db5 11451static htab_up
298e9637 11452allocate_dwo_file_hash_table ()
3019eac3 11453{
51ac9db5
SM
11454 auto delete_dwo_file = [] (void *item)
11455 {
11456 struct dwo_file *dwo_file = (struct dwo_file *) item;
11457
11458 delete dwo_file;
11459 };
11460
bc68fb19
TT
11461 return htab_up (htab_create_alloc (41,
11462 hash_dwo_file,
11463 eq_dwo_file,
11464 delete_dwo_file,
11465 xcalloc, xfree));
3019eac3
DE
11466}
11467
80626a55
DE
11468/* Lookup DWO file DWO_NAME. */
11469
11470static void **
976ca316 11471lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11472 const char *dwo_name,
11473 const char *comp_dir)
80626a55
DE
11474{
11475 struct dwo_file find_entry;
11476 void **slot;
11477
976ca316
SM
11478 if (per_objfile->per_bfd->dwo_files == NULL)
11479 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11480
0ac5b59e
DE
11481 find_entry.dwo_name = dwo_name;
11482 find_entry.comp_dir = comp_dir;
976ca316 11483 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11484 INSERT);
80626a55
DE
11485
11486 return slot;
11487}
11488
3019eac3
DE
11489static hashval_t
11490hash_dwo_unit (const void *item)
11491{
9a3c8263 11492 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11493
11494 /* This drops the top 32 bits of the id, but is ok for a hash. */
11495 return dwo_unit->signature;
11496}
11497
11498static int
11499eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11500{
9a3c8263
SM
11501 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11502 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11503
11504 /* The signature is assumed to be unique within the DWO file.
11505 So while object file CU dwo_id's always have the value zero,
11506 that's OK, assuming each object file DWO file has only one CU,
11507 and that's the rule for now. */
11508 return lhs->signature == rhs->signature;
11509}
11510
11511/* Allocate a hash table for DWO CUs,TUs.
11512 There is one of these tables for each of CUs,TUs for each DWO file. */
11513
b0b6a987 11514static htab_up
298e9637 11515allocate_dwo_unit_table ()
3019eac3
DE
11516{
11517 /* Start out with a pretty small number.
11518 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11519 return htab_up (htab_create_alloc (3,
11520 hash_dwo_unit,
11521 eq_dwo_unit,
11522 NULL, xcalloc, xfree));
3019eac3
DE
11523}
11524
19c3d4c9 11525/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11526
11527static void
19c3d4c9
DE
11528create_dwo_cu_reader (const struct die_reader_specs *reader,
11529 const gdb_byte *info_ptr,
11530 struct die_info *comp_unit_die,
c0ab21c2
TT
11531 struct dwo_file *dwo_file,
11532 struct dwo_unit *dwo_unit)
3019eac3
DE
11533{
11534 struct dwarf2_cu *cu = reader->cu;
9c541725 11535 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11536 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11537
a084a2a6
AT
11538 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11539 if (!signature.has_value ())
3019eac3 11540 {
b98664d3 11541 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11542 " its dwo_id [in module %s]"),
9d8780f0 11543 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11544 return;
11545 }
11546
3019eac3 11547 dwo_unit->dwo_file = dwo_file;
a084a2a6 11548 dwo_unit->signature = *signature;
8a0459fd 11549 dwo_unit->section = section;
9c541725 11550 dwo_unit->sect_off = sect_off;
3019eac3
DE
11551 dwo_unit->length = cu->per_cu->length;
11552
6f738b01
SM
11553 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11554 sect_offset_str (sect_off),
11555 hex_string (dwo_unit->signature));
3019eac3
DE
11556}
11557
33c5cd75 11558/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11559 Note: This function processes DWO files only, not DWP files. */
3019eac3 11560
33c5cd75 11561static void
976ca316 11562create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11563 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11564 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11565{
976ca316
SM
11566 struct objfile *objfile = per_objfile->objfile;
11567 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11568 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11569
96b79293 11570 section.read (objfile);
33c5cd75 11571 info_ptr = section.buffer;
3019eac3
DE
11572
11573 if (info_ptr == NULL)
33c5cd75 11574 return;
3019eac3 11575
6f738b01
SM
11576 dwarf_read_debug_printf ("Reading %s for %s:",
11577 section.get_name (),
11578 section.get_file_name ());
3019eac3 11579
33c5cd75 11580 end_ptr = info_ptr + section.size;
3019eac3
DE
11581 while (info_ptr < end_ptr)
11582 {
11583 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11584 struct dwo_unit read_unit {};
33c5cd75
DB
11585 struct dwo_unit *dwo_unit;
11586 void **slot;
11587 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11588
11589 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11590 per_cu.per_bfd = per_bfd;
3019eac3 11591 per_cu.is_debug_types = 0;
33c5cd75
DB
11592 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11593 per_cu.section = &section;
11594
976ca316 11595 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11596 if (!reader.dummy_p)
11597 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11598 &dwo_file, &read_unit);
33c5cd75
DB
11599 info_ptr += per_cu.length;
11600
11601 // If the unit could not be parsed, skip it.
c0ab21c2 11602 if (read_unit.dwo_file == NULL)
33c5cd75 11603 continue;
3019eac3 11604
33c5cd75 11605 if (cus_htab == NULL)
298e9637 11606 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11607
1859c670 11608 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11609 struct dwo_unit);
c0ab21c2 11610 *dwo_unit = read_unit;
b0b6a987 11611 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11612 gdb_assert (slot != NULL);
11613 if (*slot != NULL)
19c3d4c9 11614 {
33c5cd75
DB
11615 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11616 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11617
b98664d3 11618 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11619 " the entry at offset %s, signature %s"),
11620 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11621 hex_string (dwo_unit->signature));
19c3d4c9 11622 }
33c5cd75 11623 *slot = (void *)dwo_unit;
3019eac3 11624 }
3019eac3
DE
11625}
11626
80626a55
DE
11627/* DWP file .debug_{cu,tu}_index section format:
11628 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
d2854d8d
CT
11629 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11630
11631 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11632 officially standard DWP format was published with DWARF v5 and is called
11633 Version 5. There are no versions 3 or 4.
80626a55 11634
d2415c6c
DE
11635 DWP Version 1:
11636
80626a55
DE
11637 Both index sections have the same format, and serve to map a 64-bit
11638 signature to a set of section numbers. Each section begins with a header,
11639 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11640 indexes, and a pool of 32-bit section numbers. The index sections will be
11641 aligned at 8-byte boundaries in the file.
11642
d2415c6c
DE
11643 The index section header consists of:
11644
11645 V, 32 bit version number
11646 -, 32 bits unused
11647 N, 32 bit number of compilation units or type units in the index
11648 M, 32 bit number of slots in the hash table
80626a55 11649
d2415c6c 11650 Numbers are recorded using the byte order of the application binary.
80626a55 11651
d2415c6c
DE
11652 The hash table begins at offset 16 in the section, and consists of an array
11653 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11654 order of the application binary). Unused slots in the hash table are 0.
11655 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11656
d2415c6c
DE
11657 The parallel table begins immediately after the hash table
11658 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11659 array of 32-bit indexes (using the byte order of the application binary),
11660 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11661 table contains a 32-bit index into the pool of section numbers. For unused
11662 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11663
73869dc2
DE
11664 The pool of section numbers begins immediately following the hash table
11665 (at offset 16 + 12 * M from the beginning of the section). The pool of
11666 section numbers consists of an array of 32-bit words (using the byte order
11667 of the application binary). Each item in the array is indexed starting
11668 from 0. The hash table entry provides the index of the first section
11669 number in the set. Additional section numbers in the set follow, and the
11670 set is terminated by a 0 entry (section number 0 is not used in ELF).
11671
11672 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11673 section must be the first entry in the set, and the .debug_abbrev.dwo must
11674 be the second entry. Other members of the set may follow in any order.
11675
11676 ---
11677
d2854d8d 11678 DWP Versions 2 and 5:
73869dc2 11679
d2854d8d 11680 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
73869dc2
DE
11681 and the entries in the index tables are now offsets into these sections.
11682 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11683 section.
11684
11685 Index Section Contents:
11686 Header
11687 Hash Table of Signatures dwp_hash_table.hash_table
11688 Parallel Table of Indices dwp_hash_table.unit_table
d2854d8d
CT
11689 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11690 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
73869dc2
DE
11691
11692 The index section header consists of:
11693
11694 V, 32 bit version number
11695 L, 32 bit number of columns in the table of section offsets
11696 N, 32 bit number of compilation units or type units in the index
11697 M, 32 bit number of slots in the hash table
11698
11699 Numbers are recorded using the byte order of the application binary.
11700
11701 The hash table has the same format as version 1.
11702 The parallel table of indices has the same format as version 1,
11703 except that the entries are origin-1 indices into the table of sections
11704 offsets and the table of section sizes.
11705
11706 The table of offsets begins immediately following the parallel table
11707 (at offset 16 + 12 * M from the beginning of the section). The table is
11708 a two-dimensional array of 32-bit words (using the byte order of the
11709 application binary), with L columns and N+1 rows, in row-major order.
11710 Each row in the array is indexed starting from 0. The first row provides
11711 a key to the remaining rows: each column in this row provides an identifier
11712 for a debug section, and the offsets in the same column of subsequent rows
d2854d8d 11713 refer to that section. The section identifiers for Version 2 are:
73869dc2
DE
11714
11715 DW_SECT_INFO 1 .debug_info.dwo
11716 DW_SECT_TYPES 2 .debug_types.dwo
11717 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11718 DW_SECT_LINE 4 .debug_line.dwo
11719 DW_SECT_LOC 5 .debug_loc.dwo
11720 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11721 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11722 DW_SECT_MACRO 8 .debug_macro.dwo
11723
d2854d8d
CT
11724 The section identifiers for Version 5 are:
11725
11726 DW_SECT_INFO_V5 1 .debug_info.dwo
11727 DW_SECT_RESERVED_V5 2 --
11728 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11729 DW_SECT_LINE_V5 4 .debug_line.dwo
11730 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11731 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11732 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11733 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11734
73869dc2
DE
11735 The offsets provided by the CU and TU index sections are the base offsets
11736 for the contributions made by each CU or TU to the corresponding section
11737 in the package file. Each CU and TU header contains an abbrev_offset
11738 field, used to find the abbreviations table for that CU or TU within the
11739 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11740 be interpreted as relative to the base offset given in the index section.
11741 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11742 should be interpreted as relative to the base offset for .debug_line.dwo,
11743 and offsets into other debug sections obtained from DWARF attributes should
11744 also be interpreted as relative to the corresponding base offset.
11745
11746 The table of sizes begins immediately following the table of offsets.
11747 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11748 with L columns and N rows, in row-major order. Each row in the array is
11749 indexed starting from 1 (row 0 is shared by the two tables).
11750
11751 ---
11752
11753 Hash table lookup is handled the same in version 1 and 2:
11754
11755 We assume that N and M will not exceed 2^32 - 1.
11756 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11757
d2415c6c
DE
11758 Given a 64-bit compilation unit signature or a type signature S, an entry
11759 in the hash table is located as follows:
80626a55 11760
d2415c6c
DE
11761 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11762 the low-order k bits all set to 1.
80626a55 11763
d2415c6c 11764 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11765
d2415c6c
DE
11766 3) If the hash table entry at index H matches the signature, use that
11767 entry. If the hash table entry at index H is unused (all zeroes),
11768 terminate the search: the signature is not present in the table.
80626a55 11769
d2415c6c 11770 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11771
d2415c6c 11772 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11773 to stop at an unused slot or find the match. */
80626a55
DE
11774
11775/* Create a hash table to map DWO IDs to their CU/TU entry in
11776 .debug_{info,types}.dwo in DWP_FILE.
11777 Returns NULL if there isn't one.
11778 Note: This function processes DWP files only, not DWO files. */
11779
11780static struct dwp_hash_table *
976ca316 11781create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11782 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11783{
976ca316 11784 struct objfile *objfile = per_objfile->objfile;
400174b1 11785 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11786 const gdb_byte *index_ptr, *index_end;
80626a55 11787 struct dwarf2_section_info *index;
73869dc2 11788 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11789 struct dwp_hash_table *htab;
11790
11791 if (is_debug_types)
11792 index = &dwp_file->sections.tu_index;
11793 else
11794 index = &dwp_file->sections.cu_index;
11795
96b79293 11796 if (index->empty ())
80626a55 11797 return NULL;
96b79293 11798 index->read (objfile);
80626a55
DE
11799
11800 index_ptr = index->buffer;
11801 index_end = index_ptr + index->size;
11802
d2854d8d
CT
11803 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11804 For now it's safe to just read 4 bytes (particularly as it's difficult to
11805 tell if you're dealing with Version 5 before you've read the version). */
80626a55 11806 version = read_4_bytes (dbfd, index_ptr);
73869dc2 11807 index_ptr += 4;
d2854d8d 11808 if (version == 2 || version == 5)
73869dc2
DE
11809 nr_columns = read_4_bytes (dbfd, index_ptr);
11810 else
11811 nr_columns = 0;
11812 index_ptr += 4;
80626a55
DE
11813 nr_units = read_4_bytes (dbfd, index_ptr);
11814 index_ptr += 4;
11815 nr_slots = read_4_bytes (dbfd, index_ptr);
11816 index_ptr += 4;
11817
d2854d8d 11818 if (version != 1 && version != 2 && version != 5)
80626a55 11819 {
21aa081e 11820 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11821 " [in module %s]"),
21aa081e 11822 pulongest (version), dwp_file->name);
80626a55
DE
11823 }
11824 if (nr_slots != (nr_slots & -nr_slots))
11825 {
21aa081e 11826 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11827 " is not power of 2 [in module %s]"),
21aa081e 11828 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11829 }
11830
976ca316 11831 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11832 htab->version = version;
11833 htab->nr_columns = nr_columns;
80626a55
DE
11834 htab->nr_units = nr_units;
11835 htab->nr_slots = nr_slots;
11836 htab->hash_table = index_ptr;
11837 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11838
11839 /* Exit early if the table is empty. */
11840 if (nr_slots == 0 || nr_units == 0
d2854d8d
CT
11841 || (version == 2 && nr_columns == 0)
11842 || (version == 5 && nr_columns == 0))
73869dc2
DE
11843 {
11844 /* All must be zero. */
11845 if (nr_slots != 0 || nr_units != 0
d2854d8d
CT
11846 || (version == 2 && nr_columns != 0)
11847 || (version == 5 && nr_columns != 0))
73869dc2 11848 {
b98664d3 11849 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11850 " all zero [in modules %s]"),
11851 dwp_file->name);
11852 }
11853 return htab;
11854 }
11855
11856 if (version == 1)
11857 {
11858 htab->section_pool.v1.indices =
11859 htab->unit_table + sizeof (uint32_t) * nr_slots;
11860 /* It's harder to decide whether the section is too small in v1.
11861 V1 is deprecated anyway so we punt. */
11862 }
d2854d8d 11863 else if (version == 2)
73869dc2
DE
11864 {
11865 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11866 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11867 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11868 /* Reverse map for error checking. */
11869 int ids_seen[DW_SECT_MAX + 1];
11870 int i;
11871
11872 if (nr_columns < 2)
11873 {
11874 error (_("Dwarf Error: bad DWP hash table, too few columns"
11875 " in section table [in module %s]"),
11876 dwp_file->name);
11877 }
11878 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11879 {
11880 error (_("Dwarf Error: bad DWP hash table, too many columns"
11881 " in section table [in module %s]"),
11882 dwp_file->name);
11883 }
04fd5eed
GB
11884 memset (ids, 255, sizeof_ids);
11885 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11886 for (i = 0; i < nr_columns; ++i)
11887 {
11888 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11889
11890 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11891 {
11892 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11893 " in section table [in module %s]"),
11894 id, dwp_file->name);
11895 }
11896 if (ids_seen[id] != -1)
11897 {
11898 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11899 " id %d in section table [in module %s]"),
11900 id, dwp_file->name);
11901 }
11902 ids_seen[id] = i;
11903 ids[i] = id;
11904 }
11905 /* Must have exactly one info or types section. */
11906 if (((ids_seen[DW_SECT_INFO] != -1)
11907 + (ids_seen[DW_SECT_TYPES] != -1))
11908 != 1)
11909 {
11910 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11911 " DWO info/types section [in module %s]"),
11912 dwp_file->name);
11913 }
11914 /* Must have an abbrev section. */
11915 if (ids_seen[DW_SECT_ABBREV] == -1)
11916 {
11917 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11918 " section [in module %s]"),
11919 dwp_file->name);
11920 }
11921 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11922 htab->section_pool.v2.sizes =
11923 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11924 * nr_units * nr_columns);
11925 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11926 * nr_units * nr_columns))
11927 > index_end)
11928 {
11929 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11930 " [in module %s]"),
11931 dwp_file->name);
11932 }
11933 }
d2854d8d
CT
11934 else /* version == 5 */
11935 {
11936 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11937 int *ids = htab->section_pool.v5.section_ids;
11938 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11939 /* Reverse map for error checking. */
11940 int ids_seen[DW_SECT_MAX_V5 + 1];
11941
11942 if (nr_columns < 2)
11943 {
11944 error (_("Dwarf Error: bad DWP hash table, too few columns"
11945 " in section table [in module %s]"),
11946 dwp_file->name);
11947 }
11948 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11949 {
11950 error (_("Dwarf Error: bad DWP hash table, too many columns"
11951 " in section table [in module %s]"),
11952 dwp_file->name);
11953 }
11954 memset (ids, 255, sizeof_ids);
11955 memset (ids_seen, 255, sizeof (ids_seen));
11956 for (int i = 0; i < nr_columns; ++i)
11957 {
11958 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11959
11960 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11961 {
11962 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11963 " in section table [in module %s]"),
11964 id, dwp_file->name);
11965 }
11966 if (ids_seen[id] != -1)
11967 {
11968 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11969 " id %d in section table [in module %s]"),
11970 id, dwp_file->name);
11971 }
11972 ids_seen[id] = i;
11973 ids[i] = id;
11974 }
11975 /* Must have seen an info section. */
11976 if (ids_seen[DW_SECT_INFO_V5] == -1)
11977 {
11978 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11979 " DWO info/types section [in module %s]"),
11980 dwp_file->name);
11981 }
11982 /* Must have an abbrev section. */
11983 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11984 {
11985 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11986 " section [in module %s]"),
11987 dwp_file->name);
11988 }
11989 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11990 htab->section_pool.v5.sizes
11991 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11992 * nr_units * nr_columns);
11993 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11994 * nr_units * nr_columns))
11995 > index_end)
11996 {
11997 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11998 " [in module %s]"),
11999 dwp_file->name);
12000 }
12001 }
80626a55
DE
12002
12003 return htab;
12004}
12005
12006/* Update SECTIONS with the data from SECTP.
12007
5bb6e9dd
TT
12008 This function is like the other "locate" section routines, but in
12009 this context the sections to read comes from the DWP V1 hash table,
12010 not the full ELF section table.
80626a55
DE
12011
12012 The result is non-zero for success, or zero if an error was found. */
12013
12014static int
73869dc2
DE
12015locate_v1_virtual_dwo_sections (asection *sectp,
12016 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12017{
12018 const struct dwop_section_names *names = &dwop_section_names;
12019
fbedd546 12020 if (names->abbrev_dwo.matches (sectp->name))
80626a55
DE
12021 {
12022 /* There can be only one. */
049412e3 12023 if (sections->abbrev.s.section != NULL)
80626a55 12024 return 0;
049412e3 12025 sections->abbrev.s.section = sectp;
fd361982 12026 sections->abbrev.size = bfd_section_size (sectp);
80626a55 12027 }
fbedd546
TT
12028 else if (names->info_dwo.matches (sectp->name)
12029 || names->types_dwo.matches (sectp->name))
80626a55
DE
12030 {
12031 /* There can be only one. */
049412e3 12032 if (sections->info_or_types.s.section != NULL)
80626a55 12033 return 0;
049412e3 12034 sections->info_or_types.s.section = sectp;
fd361982 12035 sections->info_or_types.size = bfd_section_size (sectp);
80626a55 12036 }
fbedd546 12037 else if (names->line_dwo.matches (sectp->name))
80626a55
DE
12038 {
12039 /* There can be only one. */
049412e3 12040 if (sections->line.s.section != NULL)
80626a55 12041 return 0;
049412e3 12042 sections->line.s.section = sectp;
fd361982 12043 sections->line.size = bfd_section_size (sectp);
80626a55 12044 }
fbedd546 12045 else if (names->loc_dwo.matches (sectp->name))
80626a55
DE
12046 {
12047 /* There can be only one. */
049412e3 12048 if (sections->loc.s.section != NULL)
80626a55 12049 return 0;
049412e3 12050 sections->loc.s.section = sectp;
fd361982 12051 sections->loc.size = bfd_section_size (sectp);
80626a55 12052 }
fbedd546 12053 else if (names->macinfo_dwo.matches (sectp->name))
80626a55
DE
12054 {
12055 /* There can be only one. */
049412e3 12056 if (sections->macinfo.s.section != NULL)
80626a55 12057 return 0;
049412e3 12058 sections->macinfo.s.section = sectp;
fd361982 12059 sections->macinfo.size = bfd_section_size (sectp);
80626a55 12060 }
fbedd546 12061 else if (names->macro_dwo.matches (sectp->name))
80626a55
DE
12062 {
12063 /* There can be only one. */
049412e3 12064 if (sections->macro.s.section != NULL)
80626a55 12065 return 0;
049412e3 12066 sections->macro.s.section = sectp;
fd361982 12067 sections->macro.size = bfd_section_size (sectp);
80626a55 12068 }
fbedd546 12069 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55
DE
12070 {
12071 /* There can be only one. */
049412e3 12072 if (sections->str_offsets.s.section != NULL)
80626a55 12073 return 0;
049412e3 12074 sections->str_offsets.s.section = sectp;
fd361982 12075 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12076 }
12077 else
12078 {
12079 /* No other kind of section is valid. */
12080 return 0;
12081 }
12082
12083 return 1;
12084}
12085
73869dc2
DE
12086/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12087 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12088 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12089 This is for DWP version 1 files. */
80626a55
DE
12090
12091static struct dwo_unit *
976ca316 12092create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 12093 struct dwp_file *dwp_file,
73869dc2
DE
12094 uint32_t unit_index,
12095 const char *comp_dir,
12096 ULONGEST signature, int is_debug_types)
80626a55 12097{
73869dc2
DE
12098 const struct dwp_hash_table *dwp_htab =
12099 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12100 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12101 const char *kind = is_debug_types ? "TU" : "CU";
12102 struct dwo_file *dwo_file;
12103 struct dwo_unit *dwo_unit;
73869dc2 12104 struct virtual_v1_dwo_sections sections;
80626a55 12105 void **dwo_file_slot;
80626a55
DE
12106 int i;
12107
73869dc2
DE
12108 gdb_assert (dwp_file->version == 1);
12109
6f738b01
SM
12110 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12111 kind, pulongest (unit_index), hex_string (signature),
12112 dwp_file->name);
80626a55 12113
19ac8c2e 12114 /* Fetch the sections of this DWO unit.
80626a55
DE
12115 Put a limit on the number of sections we look for so that bad data
12116 doesn't cause us to loop forever. */
12117
73869dc2 12118#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12119 (1 /* .debug_info or .debug_types */ \
12120 + 1 /* .debug_abbrev */ \
12121 + 1 /* .debug_line */ \
12122 + 1 /* .debug_loc */ \
12123 + 1 /* .debug_str_offsets */ \
19ac8c2e 12124 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12125 + 1 /* trailing zero */)
12126
12127 memset (&sections, 0, sizeof (sections));
80626a55 12128
73869dc2 12129 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12130 {
12131 asection *sectp;
12132 uint32_t section_nr =
12133 read_4_bytes (dbfd,
73869dc2
DE
12134 dwp_htab->section_pool.v1.indices
12135 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12136
12137 if (section_nr == 0)
12138 break;
12139 if (section_nr >= dwp_file->num_sections)
12140 {
12141 error (_("Dwarf Error: bad DWP hash table, section number too large"
12142 " [in module %s]"),
12143 dwp_file->name);
12144 }
12145
12146 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12147 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12148 {
12149 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12150 " [in module %s]"),
12151 dwp_file->name);
12152 }
12153 }
12154
12155 if (i < 2
96b79293
TT
12156 || sections.info_or_types.empty ()
12157 || sections.abbrev.empty ())
80626a55
DE
12158 {
12159 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12160 " [in module %s]"),
12161 dwp_file->name);
12162 }
73869dc2 12163 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12164 {
12165 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12166 " [in module %s]"),
12167 dwp_file->name);
12168 }
12169
12170 /* It's easier for the rest of the code if we fake a struct dwo_file and
12171 have dwo_unit "live" in that. At least for now.
12172
12173 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12174 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12175 file, we can combine them back into a virtual DWO file to save space
12176 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12177 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12178
791afaa2
TT
12179 std::string virtual_dwo_name =
12180 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
12181 sections.abbrev.get_id (),
12182 sections.line.get_id (),
12183 sections.loc.get_id (),
12184 sections.str_offsets.get_id ());
80626a55 12185 /* Can we use an existing virtual DWO file? */
976ca316 12186 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12187 comp_dir);
80626a55
DE
12188 /* Create one if necessary. */
12189 if (*dwo_file_slot == NULL)
12190 {
6f738b01
SM
12191 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12192 virtual_dwo_name.c_str ());
12193
51ac9db5 12194 dwo_file = new struct dwo_file;
976ca316 12195 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 12196 dwo_file->comp_dir = comp_dir;
80626a55
DE
12197 dwo_file->sections.abbrev = sections.abbrev;
12198 dwo_file->sections.line = sections.line;
12199 dwo_file->sections.loc = sections.loc;
12200 dwo_file->sections.macinfo = sections.macinfo;
12201 dwo_file->sections.macro = sections.macro;
12202 dwo_file->sections.str_offsets = sections.str_offsets;
12203 /* The "str" section is global to the entire DWP file. */
12204 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12205 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12206 there's no need to record it in dwo_file.
12207 Also, we can't simply record type sections in dwo_file because
12208 we record a pointer into the vector in dwo_unit. As we collect more
12209 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12210 for it, invalidating all copies of pointers into the previous
12211 contents. */
80626a55
DE
12212 *dwo_file_slot = dwo_file;
12213 }
12214 else
12215 {
6f738b01
SM
12216 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12217 virtual_dwo_name.c_str ());
12218
9a3c8263 12219 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12220 }
80626a55 12221
976ca316 12222 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
12223 dwo_unit->dwo_file = dwo_file;
12224 dwo_unit->signature = signature;
8d749320 12225 dwo_unit->section =
976ca316 12226 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 12227 *dwo_unit->section = sections.info_or_types;
57d63ce2 12228 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12229
12230 return dwo_unit;
12231}
12232
d2854d8d
CT
12233/* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12234 simplify them. Given a pointer to the containing section SECTION, and
12235 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12236 virtual section of just that piece. */
73869dc2
DE
12237
12238static struct dwarf2_section_info
d2854d8d
CT
12239create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12240 struct dwarf2_section_info *section,
12241 bfd_size_type offset, bfd_size_type size)
73869dc2
DE
12242{
12243 struct dwarf2_section_info result;
12244 asection *sectp;
12245
12246 gdb_assert (section != NULL);
12247 gdb_assert (!section->is_virtual);
12248
12249 memset (&result, 0, sizeof (result));
12250 result.s.containing_section = section;
dc4ccb6f 12251 result.is_virtual = true;
73869dc2
DE
12252
12253 if (size == 0)
12254 return result;
12255
96b79293 12256 sectp = section->get_bfd_section ();
73869dc2
DE
12257
12258 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12259 bounds of the real section. This is a pretty-rare event, so just
12260 flag an error (easier) instead of a warning and trying to cope. */
12261 if (sectp == NULL
fd361982 12262 || offset + size > bfd_section_size (sectp))
73869dc2 12263 {
d2854d8d 12264 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
73869dc2 12265 " in section %s [in module %s]"),
fd361982 12266 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 12267 objfile_name (per_objfile->objfile));
73869dc2
DE
12268 }
12269
12270 result.virtual_offset = offset;
12271 result.size = size;
12272 return result;
12273}
12274
12275/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12276 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12277 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12278 This is for DWP version 2 files. */
12279
12280static struct dwo_unit *
976ca316 12281create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 12282 struct dwp_file *dwp_file,
73869dc2
DE
12283 uint32_t unit_index,
12284 const char *comp_dir,
12285 ULONGEST signature, int is_debug_types)
12286{
73869dc2
DE
12287 const struct dwp_hash_table *dwp_htab =
12288 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12289 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12290 const char *kind = is_debug_types ? "TU" : "CU";
12291 struct dwo_file *dwo_file;
12292 struct dwo_unit *dwo_unit;
d2854d8d 12293 struct virtual_v2_or_v5_dwo_sections sections;
73869dc2 12294 void **dwo_file_slot;
73869dc2
DE
12295 int i;
12296
12297 gdb_assert (dwp_file->version == 2);
12298
6f738b01
SM
12299 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12300 kind, pulongest (unit_index), hex_string (signature),
12301 dwp_file->name);
73869dc2
DE
12302
12303 /* Fetch the section offsets of this DWO unit. */
12304
12305 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12306
12307 for (i = 0; i < dwp_htab->nr_columns; ++i)
12308 {
12309 uint32_t offset = read_4_bytes (dbfd,
12310 dwp_htab->section_pool.v2.offsets
12311 + (((unit_index - 1) * dwp_htab->nr_columns
12312 + i)
12313 * sizeof (uint32_t)));
12314 uint32_t size = read_4_bytes (dbfd,
12315 dwp_htab->section_pool.v2.sizes
12316 + (((unit_index - 1) * dwp_htab->nr_columns
12317 + i)
12318 * sizeof (uint32_t)));
12319
12320 switch (dwp_htab->section_pool.v2.section_ids[i])
12321 {
12322 case DW_SECT_INFO:
12323 case DW_SECT_TYPES:
12324 sections.info_or_types_offset = offset;
12325 sections.info_or_types_size = size;
12326 break;
12327 case DW_SECT_ABBREV:
12328 sections.abbrev_offset = offset;
12329 sections.abbrev_size = size;
12330 break;
12331 case DW_SECT_LINE:
12332 sections.line_offset = offset;
12333 sections.line_size = size;
12334 break;
12335 case DW_SECT_LOC:
12336 sections.loc_offset = offset;
12337 sections.loc_size = size;
12338 break;
12339 case DW_SECT_STR_OFFSETS:
12340 sections.str_offsets_offset = offset;
12341 sections.str_offsets_size = size;
12342 break;
12343 case DW_SECT_MACINFO:
12344 sections.macinfo_offset = offset;
12345 sections.macinfo_size = size;
12346 break;
12347 case DW_SECT_MACRO:
12348 sections.macro_offset = offset;
12349 sections.macro_size = size;
12350 break;
12351 }
12352 }
12353
12354 /* It's easier for the rest of the code if we fake a struct dwo_file and
12355 have dwo_unit "live" in that. At least for now.
12356
12357 The DWP file can be made up of a random collection of CUs and TUs.
12358 However, for each CU + set of TUs that came from the same original DWO
12359 file, we can combine them back into a virtual DWO file to save space
12360 (fewer struct dwo_file objects to allocate). Remember that for really
12361 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12362
791afaa2
TT
12363 std::string virtual_dwo_name =
12364 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12365 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12366 (long) (sections.line_size ? sections.line_offset : 0),
12367 (long) (sections.loc_size ? sections.loc_offset : 0),
12368 (long) (sections.str_offsets_size
12369 ? sections.str_offsets_offset : 0));
73869dc2 12370 /* Can we use an existing virtual DWO file? */
976ca316 12371 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12372 comp_dir);
73869dc2
DE
12373 /* Create one if necessary. */
12374 if (*dwo_file_slot == NULL)
12375 {
6f738b01
SM
12376 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12377 virtual_dwo_name.c_str ());
12378
51ac9db5 12379 dwo_file = new struct dwo_file;
976ca316 12380 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12381 dwo_file->comp_dir = comp_dir;
12382 dwo_file->sections.abbrev =
d2854d8d
CT
12383 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12384 sections.abbrev_offset,
12385 sections.abbrev_size);
73869dc2 12386 dwo_file->sections.line =
d2854d8d
CT
12387 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12388 sections.line_offset,
12389 sections.line_size);
73869dc2 12390 dwo_file->sections.loc =
d2854d8d
CT
12391 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12392 sections.loc_offset, sections.loc_size);
73869dc2 12393 dwo_file->sections.macinfo =
d2854d8d
CT
12394 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12395 sections.macinfo_offset,
12396 sections.macinfo_size);
73869dc2 12397 dwo_file->sections.macro =
d2854d8d
CT
12398 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12399 sections.macro_offset,
12400 sections.macro_size);
73869dc2 12401 dwo_file->sections.str_offsets =
d2854d8d
CT
12402 create_dwp_v2_or_v5_section (per_objfile,
12403 &dwp_file->sections.str_offsets,
12404 sections.str_offsets_offset,
12405 sections.str_offsets_size);
73869dc2
DE
12406 /* The "str" section is global to the entire DWP file. */
12407 dwo_file->sections.str = dwp_file->sections.str;
12408 /* The info or types section is assigned below to dwo_unit,
12409 there's no need to record it in dwo_file.
12410 Also, we can't simply record type sections in dwo_file because
12411 we record a pointer into the vector in dwo_unit. As we collect more
12412 types we'll grow the vector and eventually have to reallocate space
12413 for it, invalidating all copies of pointers into the previous
12414 contents. */
12415 *dwo_file_slot = dwo_file;
12416 }
12417 else
12418 {
6f738b01
SM
12419 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12420 virtual_dwo_name.c_str ());
12421
9a3c8263 12422 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12423 }
73869dc2 12424
976ca316 12425 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12426 dwo_unit->dwo_file = dwo_file;
12427 dwo_unit->signature = signature;
8d749320 12428 dwo_unit->section =
976ca316 12429 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
d2854d8d 12430 *dwo_unit->section = create_dwp_v2_or_v5_section
dda83cd7 12431 (per_objfile,
d2854d8d
CT
12432 is_debug_types
12433 ? &dwp_file->sections.types
12434 : &dwp_file->sections.info,
12435 sections.info_or_types_offset,
12436 sections.info_or_types_size);
12437 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12438
12439 return dwo_unit;
12440}
12441
12442/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12443 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12444 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12445 This is for DWP version 5 files. */
12446
12447static struct dwo_unit *
12448create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12449 struct dwp_file *dwp_file,
12450 uint32_t unit_index,
12451 const char *comp_dir,
12452 ULONGEST signature, int is_debug_types)
12453{
12454 const struct dwp_hash_table *dwp_htab
12455 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12456 bfd *dbfd = dwp_file->dbfd.get ();
12457 const char *kind = is_debug_types ? "TU" : "CU";
12458 struct dwo_file *dwo_file;
12459 struct dwo_unit *dwo_unit;
12460 struct virtual_v2_or_v5_dwo_sections sections {};
12461 void **dwo_file_slot;
12462
12463 gdb_assert (dwp_file->version == 5);
12464
6f738b01
SM
12465 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12466 kind, pulongest (unit_index), hex_string (signature),
12467 dwp_file->name);
d2854d8d
CT
12468
12469 /* Fetch the section offsets of this DWO unit. */
12470
12471 /* memset (&sections, 0, sizeof (sections)); */
12472
12473 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12474 {
12475 uint32_t offset = read_4_bytes (dbfd,
dda83cd7
SM
12476 dwp_htab->section_pool.v5.offsets
12477 + (((unit_index - 1)
12478 * dwp_htab->nr_columns
12479 + i)
12480 * sizeof (uint32_t)));
d2854d8d 12481 uint32_t size = read_4_bytes (dbfd,
dda83cd7
SM
12482 dwp_htab->section_pool.v5.sizes
12483 + (((unit_index - 1) * dwp_htab->nr_columns
12484 + i)
12485 * sizeof (uint32_t)));
d2854d8d
CT
12486
12487 switch (dwp_htab->section_pool.v5.section_ids[i])
dda83cd7
SM
12488 {
12489 case DW_SECT_ABBREV_V5:
12490 sections.abbrev_offset = offset;
12491 sections.abbrev_size = size;
12492 break;
12493 case DW_SECT_INFO_V5:
12494 sections.info_or_types_offset = offset;
12495 sections.info_or_types_size = size;
12496 break;
12497 case DW_SECT_LINE_V5:
12498 sections.line_offset = offset;
12499 sections.line_size = size;
12500 break;
12501 case DW_SECT_LOCLISTS_V5:
12502 sections.loclists_offset = offset;
12503 sections.loclists_size = size;
12504 break;
12505 case DW_SECT_MACRO_V5:
12506 sections.macro_offset = offset;
12507 sections.macro_size = size;
12508 break;
12509 case DW_SECT_RNGLISTS_V5:
12510 sections.rnglists_offset = offset;
12511 sections.rnglists_size = size;
12512 break;
12513 case DW_SECT_STR_OFFSETS_V5:
12514 sections.str_offsets_offset = offset;
12515 sections.str_offsets_size = size;
12516 break;
12517 case DW_SECT_RESERVED_V5:
12518 default:
12519 break;
12520 }
d2854d8d
CT
12521 }
12522
12523 /* It's easier for the rest of the code if we fake a struct dwo_file and
12524 have dwo_unit "live" in that. At least for now.
12525
12526 The DWP file can be made up of a random collection of CUs and TUs.
12527 However, for each CU + set of TUs that came from the same original DWO
12528 file, we can combine them back into a virtual DWO file to save space
12529 (fewer struct dwo_file objects to allocate). Remember that for really
12530 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12531
12532 std::string virtual_dwo_name =
12533 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
dda83cd7
SM
12534 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12535 (long) (sections.line_size ? sections.line_offset : 0),
12536 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12537 (long) (sections.str_offsets_size
12538 ? sections.str_offsets_offset : 0),
12539 (long) (sections.macro_size ? sections.macro_offset : 0),
12540 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
d2854d8d
CT
12541 /* Can we use an existing virtual DWO file? */
12542 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
dda83cd7
SM
12543 virtual_dwo_name.c_str (),
12544 comp_dir);
d2854d8d
CT
12545 /* Create one if necessary. */
12546 if (*dwo_file_slot == NULL)
12547 {
6f738b01
SM
12548 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12549 virtual_dwo_name.c_str ());
12550
d2854d8d
CT
12551 dwo_file = new struct dwo_file;
12552 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12553 dwo_file->comp_dir = comp_dir;
12554 dwo_file->sections.abbrev =
dda83cd7
SM
12555 create_dwp_v2_or_v5_section (per_objfile,
12556 &dwp_file->sections.abbrev,
12557 sections.abbrev_offset,
12558 sections.abbrev_size);
d2854d8d 12559 dwo_file->sections.line =
dda83cd7
SM
12560 create_dwp_v2_or_v5_section (per_objfile,
12561 &dwp_file->sections.line,
12562 sections.line_offset, sections.line_size);
d2854d8d 12563 dwo_file->sections.macro =
dda83cd7
SM
12564 create_dwp_v2_or_v5_section (per_objfile,
12565 &dwp_file->sections.macro,
12566 sections.macro_offset,
12567 sections.macro_size);
d2854d8d 12568 dwo_file->sections.loclists =
dda83cd7
SM
12569 create_dwp_v2_or_v5_section (per_objfile,
12570 &dwp_file->sections.loclists,
12571 sections.loclists_offset,
12572 sections.loclists_size);
d2854d8d 12573 dwo_file->sections.rnglists =
dda83cd7
SM
12574 create_dwp_v2_or_v5_section (per_objfile,
12575 &dwp_file->sections.rnglists,
12576 sections.rnglists_offset,
12577 sections.rnglists_size);
d2854d8d 12578 dwo_file->sections.str_offsets =
dda83cd7
SM
12579 create_dwp_v2_or_v5_section (per_objfile,
12580 &dwp_file->sections.str_offsets,
12581 sections.str_offsets_offset,
12582 sections.str_offsets_size);
d2854d8d
CT
12583 /* The "str" section is global to the entire DWP file. */
12584 dwo_file->sections.str = dwp_file->sections.str;
12585 /* The info or types section is assigned below to dwo_unit,
dda83cd7
SM
12586 there's no need to record it in dwo_file.
12587 Also, we can't simply record type sections in dwo_file because
12588 we record a pointer into the vector in dwo_unit. As we collect more
12589 types we'll grow the vector and eventually have to reallocate space
12590 for it, invalidating all copies of pointers into the previous
12591 contents. */
d2854d8d
CT
12592 *dwo_file_slot = dwo_file;
12593 }
12594 else
12595 {
6f738b01
SM
12596 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12597 virtual_dwo_name.c_str ());
12598
d2854d8d
CT
12599 dwo_file = (struct dwo_file *) *dwo_file_slot;
12600 }
12601
12602 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12603 dwo_unit->dwo_file = dwo_file;
12604 dwo_unit->signature = signature;
12605 dwo_unit->section
12606 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12607 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
dda83cd7
SM
12608 &dwp_file->sections.info,
12609 sections.info_or_types_offset,
12610 sections.info_or_types_size);
73869dc2
DE
12611 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12612
12613 return dwo_unit;
12614}
12615
57d63ce2
DE
12616/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12617 Returns NULL if the signature isn't found. */
80626a55
DE
12618
12619static struct dwo_unit *
976ca316 12620lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12621 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12622 ULONGEST signature, int is_debug_types)
80626a55 12623{
57d63ce2
DE
12624 const struct dwp_hash_table *dwp_htab =
12625 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12626 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12627 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12628 uint32_t hash = signature & mask;
12629 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12630 unsigned int i;
12631 void **slot;
870f88f7 12632 struct dwo_unit find_dwo_cu;
80626a55
DE
12633
12634 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12635 find_dwo_cu.signature = signature;
19ac8c2e 12636 slot = htab_find_slot (is_debug_types
48b490f2
TT
12637 ? dwp_file->loaded_tus.get ()
12638 : dwp_file->loaded_cus.get (),
19ac8c2e 12639 &find_dwo_cu, INSERT);
80626a55
DE
12640
12641 if (*slot != NULL)
9a3c8263 12642 return (struct dwo_unit *) *slot;
80626a55
DE
12643
12644 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12645 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12646 {
12647 ULONGEST signature_in_table;
12648
12649 signature_in_table =
57d63ce2 12650 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12651 if (signature_in_table == signature)
12652 {
57d63ce2
DE
12653 uint32_t unit_index =
12654 read_4_bytes (dbfd,
12655 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12656
73869dc2
DE
12657 if (dwp_file->version == 1)
12658 {
976ca316
SM
12659 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12660 unit_index, comp_dir,
12661 signature, is_debug_types);
73869dc2 12662 }
d2854d8d 12663 else if (dwp_file->version == 2)
73869dc2 12664 {
976ca316
SM
12665 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12666 unit_index, comp_dir,
12667 signature, is_debug_types);
73869dc2 12668 }
d2854d8d
CT
12669 else /* version == 5 */
12670 {
12671 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12672 unit_index, comp_dir,
12673 signature, is_debug_types);
12674 }
9a3c8263 12675 return (struct dwo_unit *) *slot;
80626a55
DE
12676 }
12677 if (signature_in_table == 0)
12678 return NULL;
12679 hash = (hash + hash2) & mask;
12680 }
12681
12682 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12683 " [in module %s]"),
12684 dwp_file->name);
12685}
12686
ab5088bf 12687/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12688 Open the file specified by FILE_NAME and hand it off to BFD for
12689 preliminary analysis. Return a newly initialized bfd *, which
12690 includes a canonicalized copy of FILE_NAME.
80626a55 12691 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12692 SEARCH_CWD is true if the current directory is to be searched.
12693 It will be searched before debug-file-directory.
13aaf454
DE
12694 If successful, the file is added to the bfd include table of the
12695 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12696 If unable to find/open the file, return NULL.
3019eac3
DE
12697 NOTE: This function is derived from symfile_bfd_open. */
12698
192b62ce 12699static gdb_bfd_ref_ptr
976ca316 12700try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12701 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12702{
24b9144d 12703 int desc;
9c02c129
DE
12704 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12705 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12706 to debug_file_directory. */
e0cc99a6 12707 const char *search_path;
9c02c129
DE
12708 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12709
e0cc99a6 12710 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12711 if (search_cwd)
12712 {
12713 if (*debug_file_directory != '\0')
e0cc99a6
TT
12714 {
12715 search_path_holder.reset (concat (".", dirname_separator_string,
12716 debug_file_directory,
12717 (char *) NULL));
12718 search_path = search_path_holder.get ();
12719 }
6ac97d4c 12720 else
e0cc99a6 12721 search_path = ".";
6ac97d4c 12722 }
9c02c129 12723 else
e0cc99a6 12724 search_path = debug_file_directory;
3019eac3 12725
56d467f4
CT
12726 /* Add the path for the executable binary to the list of search paths. */
12727 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12728 search_path_holder.reset (concat (objfile_dir.c_str (),
12729 dirname_separator_string,
12730 search_path, nullptr));
12731 search_path = search_path_holder.get ();
12732
24b9144d 12733 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12734 if (is_dwp)
12735 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12736
12737 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12738 desc = openp (search_path, flags, file_name,
3019eac3
DE
12739 O_RDONLY | O_BINARY, &absolute_name);
12740 if (desc < 0)
12741 return NULL;
12742
e0cc99a6
TT
12743 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12744 gnutarget, desc));
9c02c129
DE
12745 if (sym_bfd == NULL)
12746 return NULL;
192b62ce 12747 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12748
192b62ce
TT
12749 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12750 return NULL;
3019eac3 12751
13aaf454
DE
12752 /* Success. Record the bfd as having been included by the objfile's bfd.
12753 This is important because things like demangled_names_hash lives in the
12754 objfile's per_bfd space and may have references to things like symbol
12755 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12756 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12757
3019eac3
DE
12758 return sym_bfd;
12759}
12760
ab5088bf 12761/* Try to open DWO file FILE_NAME.
3019eac3
DE
12762 COMP_DIR is the DW_AT_comp_dir attribute.
12763 The result is the bfd handle of the file.
12764 If there is a problem finding or opening the file, return NULL.
12765 Upon success, the canonicalized path of the file is stored in the bfd,
12766 same as symfile_bfd_open. */
12767
192b62ce 12768static gdb_bfd_ref_ptr
976ca316 12769open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12770 const char *file_name, const char *comp_dir)
3019eac3 12771{
80626a55 12772 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12773 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12774 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12775
12776 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12777
12778 if (comp_dir != NULL)
12779 {
43816ebc
TT
12780 gdb::unique_xmalloc_ptr<char> path_to_try
12781 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12782
12783 /* NOTE: If comp_dir is a relative path, this will also try the
12784 search path, which seems useful. */
976ca316 12785 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12786 0 /*is_dwp*/,
192b62ce 12787 1 /*search_cwd*/));
3019eac3
DE
12788 if (abfd != NULL)
12789 return abfd;
12790 }
12791
12792 /* That didn't work, try debug-file-directory, which, despite its name,
12793 is a list of paths. */
12794
12795 if (*debug_file_directory == '\0')
12796 return NULL;
12797
976ca316 12798 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12799 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12800}
12801
80626a55
DE
12802/* This function is mapped across the sections and remembers the offset and
12803 size of each of the DWO debugging sections we are interested in. */
12804
12805static void
5bb6e9dd
TT
12806dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12807 dwo_sections *dwo_sections)
80626a55 12808{
80626a55
DE
12809 const struct dwop_section_names *names = &dwop_section_names;
12810
fbedd546 12811 if (names->abbrev_dwo.matches (sectp->name))
80626a55 12812 {
049412e3 12813 dwo_sections->abbrev.s.section = sectp;
fd361982 12814 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55 12815 }
fbedd546 12816 else if (names->info_dwo.matches (sectp->name))
80626a55 12817 {
049412e3 12818 dwo_sections->info.s.section = sectp;
fd361982 12819 dwo_sections->info.size = bfd_section_size (sectp);
80626a55 12820 }
fbedd546 12821 else if (names->line_dwo.matches (sectp->name))
80626a55 12822 {
049412e3 12823 dwo_sections->line.s.section = sectp;
fd361982 12824 dwo_sections->line.size = bfd_section_size (sectp);
80626a55 12825 }
fbedd546 12826 else if (names->loc_dwo.matches (sectp->name))
80626a55 12827 {
049412e3 12828 dwo_sections->loc.s.section = sectp;
fd361982 12829 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12830 }
fbedd546 12831 else if (names->loclists_dwo.matches (sectp->name))
41144253 12832 {
12833 dwo_sections->loclists.s.section = sectp;
12834 dwo_sections->loclists.size = bfd_section_size (sectp);
12835 }
fbedd546 12836 else if (names->macinfo_dwo.matches (sectp->name))
80626a55 12837 {
049412e3 12838 dwo_sections->macinfo.s.section = sectp;
fd361982 12839 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55 12840 }
fbedd546 12841 else if (names->macro_dwo.matches (sectp->name))
80626a55 12842 {
049412e3 12843 dwo_sections->macro.s.section = sectp;
fd361982 12844 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55 12845 }
fbedd546 12846 else if (names->rnglists_dwo.matches (sectp->name))
d0ce17d8
CT
12847 {
12848 dwo_sections->rnglists.s.section = sectp;
12849 dwo_sections->rnglists.size = bfd_section_size (sectp);
12850 }
fbedd546 12851 else if (names->str_dwo.matches (sectp->name))
80626a55 12852 {
049412e3 12853 dwo_sections->str.s.section = sectp;
fd361982 12854 dwo_sections->str.size = bfd_section_size (sectp);
80626a55 12855 }
fbedd546 12856 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55 12857 {
049412e3 12858 dwo_sections->str_offsets.s.section = sectp;
fd361982 12859 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55 12860 }
fbedd546 12861 else if (names->types_dwo.matches (sectp->name))
80626a55
DE
12862 {
12863 struct dwarf2_section_info type_section;
12864
12865 memset (&type_section, 0, sizeof (type_section));
049412e3 12866 type_section.s.section = sectp;
fd361982 12867 type_section.size = bfd_section_size (sectp);
fd5866f6 12868 dwo_sections->types.push_back (type_section);
80626a55
DE
12869 }
12870}
12871
ab5088bf 12872/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12873 by PER_CU. This is for the non-DWP case.
80626a55 12874 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12875
12876static struct dwo_file *
4ab09049
SM
12877open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12878 const char *comp_dir)
3019eac3 12879{
976ca316 12880 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12881
976ca316 12882 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12883 if (dbfd == NULL)
12884 {
6f738b01
SM
12885 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12886
80626a55
DE
12887 return NULL;
12888 }
263db9a1 12889
51ac9db5 12890 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12891 dwo_file->dwo_name = dwo_name;
12892 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12893 dwo_file->dbfd = std::move (dbfd);
3019eac3 12894
5bb6e9dd
TT
12895 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12896 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12897 &dwo_file->sections);
3019eac3 12898
976ca316
SM
12899 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12900 dwo_file->cus);
3019eac3 12901
d2854d8d
CT
12902 if (cu->per_cu->dwarf_version < 5)
12903 {
12904 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12905 dwo_file->sections.types, dwo_file->tus);
12906 }
12907 else
12908 {
12909 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12910 &dwo_file->sections.info, dwo_file->tus,
30c80d88 12911 rcuh_kind::COMPILE);
d2854d8d 12912 }
3019eac3 12913
6f738b01 12914 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
80626a55 12915
263db9a1 12916 return dwo_file.release ();
3019eac3
DE
12917}
12918
80626a55 12919/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12920 size of each of the DWP debugging sections common to version 1 and 2 that
12921 we are interested in. */
3019eac3 12922
80626a55 12923static void
73869dc2 12924dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
5bb6e9dd 12925 dwp_file *dwp_file)
3019eac3 12926{
80626a55
DE
12927 const struct dwop_section_names *names = &dwop_section_names;
12928 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12929
80626a55 12930 /* Record the ELF section number for later lookup: this is what the
73869dc2 12931 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12932 gdb_assert (elf_section_nr < dwp_file->num_sections);
12933 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12934
80626a55 12935 /* Look for specific sections that we need. */
fbedd546 12936 if (names->str_dwo.matches (sectp->name))
80626a55 12937 {
049412e3 12938 dwp_file->sections.str.s.section = sectp;
fd361982 12939 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55 12940 }
fbedd546 12941 else if (names->cu_index.matches (sectp->name))
80626a55 12942 {
049412e3 12943 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12944 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55 12945 }
fbedd546 12946 else if (names->tu_index.matches (sectp->name))
80626a55 12947 {
049412e3 12948 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12949 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12950 }
12951}
3019eac3 12952
73869dc2
DE
12953/* This function is mapped across the sections and remembers the offset and
12954 size of each of the DWP version 2 debugging sections that we are interested
12955 in. This is split into a separate function because we don't know if we
d2854d8d 12956 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
73869dc2
DE
12957
12958static void
12959dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12960{
9a3c8263 12961 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12962 const struct dwop_section_names *names = &dwop_section_names;
12963 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12964
12965 /* Record the ELF section number for later lookup: this is what the
12966 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12967 gdb_assert (elf_section_nr < dwp_file->num_sections);
12968 dwp_file->elf_sections[elf_section_nr] = sectp;
12969
12970 /* Look for specific sections that we need. */
fbedd546 12971 if (names->abbrev_dwo.matches (sectp->name))
73869dc2 12972 {
049412e3 12973 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12974 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2 12975 }
fbedd546 12976 else if (names->info_dwo.matches (sectp->name))
73869dc2 12977 {
049412e3 12978 dwp_file->sections.info.s.section = sectp;
fd361982 12979 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2 12980 }
fbedd546 12981 else if (names->line_dwo.matches (sectp->name))
73869dc2 12982 {
049412e3 12983 dwp_file->sections.line.s.section = sectp;
fd361982 12984 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2 12985 }
fbedd546 12986 else if (names->loc_dwo.matches (sectp->name))
73869dc2 12987 {
049412e3 12988 dwp_file->sections.loc.s.section = sectp;
fd361982 12989 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2 12990 }
fbedd546 12991 else if (names->macinfo_dwo.matches (sectp->name))
73869dc2 12992 {
049412e3 12993 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12994 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2 12995 }
fbedd546 12996 else if (names->macro_dwo.matches (sectp->name))
73869dc2 12997 {
049412e3 12998 dwp_file->sections.macro.s.section = sectp;
fd361982 12999 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2 13000 }
fbedd546 13001 else if (names->str_offsets_dwo.matches (sectp->name))
73869dc2 13002 {
049412e3 13003 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 13004 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2 13005 }
fbedd546 13006 else if (names->types_dwo.matches (sectp->name))
73869dc2 13007 {
049412e3 13008 dwp_file->sections.types.s.section = sectp;
fd361982 13009 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
13010 }
13011}
13012
d2854d8d
CT
13013/* This function is mapped across the sections and remembers the offset and
13014 size of each of the DWP version 5 debugging sections that we are interested
13015 in. This is split into a separate function because we don't know if we
13016 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
13017
13018static void
13019dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13020{
13021 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13022 const struct dwop_section_names *names = &dwop_section_names;
13023 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13024
13025 /* Record the ELF section number for later lookup: this is what the
13026 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13027 gdb_assert (elf_section_nr < dwp_file->num_sections);
13028 dwp_file->elf_sections[elf_section_nr] = sectp;
13029
13030 /* Look for specific sections that we need. */
fbedd546 13031 if (names->abbrev_dwo.matches (sectp->name))
d2854d8d
CT
13032 {
13033 dwp_file->sections.abbrev.s.section = sectp;
13034 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13035 }
fbedd546 13036 else if (names->info_dwo.matches (sectp->name))
d2854d8d
CT
13037 {
13038 dwp_file->sections.info.s.section = sectp;
13039 dwp_file->sections.info.size = bfd_section_size (sectp);
13040 }
fbedd546 13041 else if (names->line_dwo.matches (sectp->name))
d2854d8d
CT
13042 {
13043 dwp_file->sections.line.s.section = sectp;
13044 dwp_file->sections.line.size = bfd_section_size (sectp);
13045 }
fbedd546 13046 else if (names->loclists_dwo.matches (sectp->name))
d2854d8d
CT
13047 {
13048 dwp_file->sections.loclists.s.section = sectp;
13049 dwp_file->sections.loclists.size = bfd_section_size (sectp);
13050 }
fbedd546 13051 else if (names->macro_dwo.matches (sectp->name))
d2854d8d
CT
13052 {
13053 dwp_file->sections.macro.s.section = sectp;
13054 dwp_file->sections.macro.size = bfd_section_size (sectp);
13055 }
fbedd546 13056 else if (names->rnglists_dwo.matches (sectp->name))
d2854d8d
CT
13057 {
13058 dwp_file->sections.rnglists.s.section = sectp;
13059 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
13060 }
fbedd546 13061 else if (names->str_offsets_dwo.matches (sectp->name))
d2854d8d
CT
13062 {
13063 dwp_file->sections.str_offsets.s.section = sectp;
13064 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13065 }
13066}
13067
80626a55 13068/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13069
80626a55
DE
13070static hashval_t
13071hash_dwp_loaded_cutus (const void *item)
13072{
9a3c8263 13073 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13074
80626a55
DE
13075 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13076 return dwo_unit->signature;
3019eac3
DE
13077}
13078
80626a55 13079/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13080
80626a55
DE
13081static int
13082eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13083{
9a3c8263
SM
13084 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13085 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13086
80626a55
DE
13087 return dua->signature == dub->signature;
13088}
3019eac3 13089
80626a55 13090/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13091
48b490f2 13092static htab_up
298e9637 13093allocate_dwp_loaded_cutus_table ()
80626a55 13094{
48b490f2
TT
13095 return htab_up (htab_create_alloc (3,
13096 hash_dwp_loaded_cutus,
13097 eq_dwp_loaded_cutus,
13098 NULL, xcalloc, xfree));
80626a55 13099}
3019eac3 13100
ab5088bf
DE
13101/* Try to open DWP file FILE_NAME.
13102 The result is the bfd handle of the file.
13103 If there is a problem finding or opening the file, return NULL.
13104 Upon success, the canonicalized path of the file is stored in the bfd,
13105 same as symfile_bfd_open. */
13106
192b62ce 13107static gdb_bfd_ref_ptr
976ca316 13108open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 13109{
976ca316 13110 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 13111 1 /*is_dwp*/,
192b62ce 13112 1 /*search_cwd*/));
6ac97d4c
DE
13113 if (abfd != NULL)
13114 return abfd;
13115
13116 /* Work around upstream bug 15652.
13117 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13118 [Whether that's a "bug" is debatable, but it is getting in our way.]
13119 We have no real idea where the dwp file is, because gdb's realpath-ing
13120 of the executable's path may have discarded the needed info.
13121 [IWBN if the dwp file name was recorded in the executable, akin to
13122 .gnu_debuglink, but that doesn't exist yet.]
13123 Strip the directory from FILE_NAME and search again. */
13124 if (*debug_file_directory != '\0')
13125 {
13126 /* Don't implicitly search the current directory here.
13127 If the user wants to search "." to handle this case,
13128 it must be added to debug-file-directory. */
976ca316
SM
13129 return try_open_dwop_file (per_objfile, lbasename (file_name),
13130 1 /*is_dwp*/,
6ac97d4c
DE
13131 0 /*search_cwd*/);
13132 }
13133
13134 return NULL;
ab5088bf
DE
13135}
13136
80626a55
DE
13137/* Initialize the use of the DWP file for the current objfile.
13138 By convention the name of the DWP file is ${objfile}.dwp.
13139 The result is NULL if it can't be found. */
a766d390 13140
400174b1 13141static std::unique_ptr<struct dwp_file>
976ca316 13142open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 13143{
976ca316 13144 struct objfile *objfile = per_objfile->objfile;
80626a55 13145
82bf32bc
JK
13146 /* Try to find first .dwp for the binary file before any symbolic links
13147 resolving. */
6c447423
DE
13148
13149 /* If the objfile is a debug file, find the name of the real binary
13150 file and get the name of dwp file from there. */
d721ba37 13151 std::string dwp_name;
6c447423
DE
13152 if (objfile->separate_debug_objfile_backlink != NULL)
13153 {
13154 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13155 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13156
d721ba37 13157 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13158 }
13159 else
d721ba37
PA
13160 dwp_name = objfile->original_name;
13161
13162 dwp_name += ".dwp";
80626a55 13163
976ca316 13164 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13165 if (dbfd == NULL
13166 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13167 {
13168 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13169 dwp_name = objfile_name (objfile);
13170 dwp_name += ".dwp";
976ca316 13171 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
13172 }
13173
80626a55
DE
13174 if (dbfd == NULL)
13175 {
6f738b01
SM
13176 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13177
400174b1 13178 return std::unique_ptr<dwp_file> ();
3019eac3 13179 }
400174b1
TT
13180
13181 const char *name = bfd_get_filename (dbfd.get ());
13182 std::unique_ptr<struct dwp_file> dwp_file
13183 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13184
0a0f4c01 13185 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 13186 dwp_file->elf_sections =
976ca316 13187 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
13188 dwp_file->num_sections, asection *);
13189
5bb6e9dd
TT
13190 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13191 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13192 dwp_file.get ());
80626a55 13193
976ca316 13194 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 13195
976ca316 13196 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 13197
73869dc2 13198 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13199 if (dwp_file->cus && dwp_file->tus
13200 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13201 {
13202 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13203 pretty bizarre. We use pulongest here because that's the established
4d65956b 13204 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13205 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13206 " TU version %s [in DWP file %s]"),
13207 pulongest (dwp_file->cus->version),
d721ba37 13208 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13209 }
08302ed2
DE
13210
13211 if (dwp_file->cus)
13212 dwp_file->version = dwp_file->cus->version;
13213 else if (dwp_file->tus)
13214 dwp_file->version = dwp_file->tus->version;
13215 else
13216 dwp_file->version = 2;
73869dc2 13217
5bb6e9dd
TT
13218 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13219 {
13220 if (dwp_file->version == 2)
13221 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13222 dwp_file.get ());
13223 else
13224 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13225 dwp_file.get ());
13226 }
73869dc2 13227
298e9637
SM
13228 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13229 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 13230
6f738b01
SM
13231 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13232 dwarf_read_debug_printf (" %s CUs, %s TUs",
13233 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13234 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13235
13236 return dwp_file;
3019eac3 13237}
c906108c 13238
ab5088bf
DE
13239/* Wrapper around open_and_init_dwp_file, only open it once. */
13240
13241static struct dwp_file *
976ca316 13242get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 13243{
976ca316 13244 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 13245 {
976ca316
SM
13246 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13247 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 13248 }
976ca316 13249 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
13250}
13251
80626a55
DE
13252/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13253 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13254 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13255 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13256 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13257
13258 This is called, for example, when wanting to read a variable with a
13259 complex location. Therefore we don't want to do file i/o for every call.
13260 Therefore we don't want to look for a DWO file on every call.
13261 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13262 then we check if we've already seen DWO_NAME, and only THEN do we check
13263 for a DWO file.
13264
1c658ad5 13265 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13266 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13267
3019eac3 13268static struct dwo_unit *
4ab09049 13269lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 13270 ULONGEST signature, int is_debug_types)
3019eac3 13271{
976ca316
SM
13272 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13273 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
13274 const char *kind = is_debug_types ? "TU" : "CU";
13275 void **dwo_file_slot;
3019eac3 13276 struct dwo_file *dwo_file;
80626a55 13277 struct dwp_file *dwp_file;
cb1df416 13278
6a506a2d
DE
13279 /* First see if there's a DWP file.
13280 If we have a DWP file but didn't find the DWO inside it, don't
13281 look for the original DWO file. It makes gdb behave differently
13282 depending on whether one is debugging in the build tree. */
cf2c3c16 13283
976ca316 13284 dwp_file = get_dwp_file (per_objfile);
80626a55 13285 if (dwp_file != NULL)
cf2c3c16 13286 {
80626a55
DE
13287 const struct dwp_hash_table *dwp_htab =
13288 is_debug_types ? dwp_file->tus : dwp_file->cus;
13289
13290 if (dwp_htab != NULL)
13291 {
13292 struct dwo_unit *dwo_cutu =
976ca316
SM
13293 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13294 is_debug_types);
80626a55
DE
13295
13296 if (dwo_cutu != NULL)
13297 {
6f738b01
SM
13298 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13299 kind, hex_string (signature),
13300 host_address_to_string (dwo_cutu));
13301
80626a55
DE
13302 return dwo_cutu;
13303 }
13304 }
13305 }
6a506a2d 13306 else
80626a55 13307 {
6a506a2d 13308 /* No DWP file, look for the DWO file. */
80626a55 13309
976ca316 13310 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 13311 if (*dwo_file_slot == NULL)
80626a55 13312 {
6a506a2d 13313 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 13314 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 13315 }
6a506a2d 13316 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13317 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13318
6a506a2d 13319 if (dwo_file != NULL)
19c3d4c9 13320 {
6a506a2d
DE
13321 struct dwo_unit *dwo_cutu = NULL;
13322
13323 if (is_debug_types && dwo_file->tus)
13324 {
13325 struct dwo_unit find_dwo_cutu;
13326
13327 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13328 find_dwo_cutu.signature = signature;
9a3c8263 13329 dwo_cutu
b0b6a987
TT
13330 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13331 &find_dwo_cutu);
6a506a2d 13332 }
33c5cd75 13333 else if (!is_debug_types && dwo_file->cus)
80626a55 13334 {
33c5cd75
DB
13335 struct dwo_unit find_dwo_cutu;
13336
13337 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13338 find_dwo_cutu.signature = signature;
b0b6a987 13339 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 13340 &find_dwo_cutu);
6a506a2d
DE
13341 }
13342
13343 if (dwo_cutu != NULL)
13344 {
6f738b01
SM
13345 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13346 kind, dwo_name, hex_string (signature),
13347 host_address_to_string (dwo_cutu));
13348
6a506a2d 13349 return dwo_cutu;
80626a55
DE
13350 }
13351 }
2e276125 13352 }
9cdd5dbd 13353
80626a55
DE
13354 /* We didn't find it. This could mean a dwo_id mismatch, or
13355 someone deleted the DWO/DWP file, or the search path isn't set up
13356 correctly to find the file. */
13357
6f738b01
SM
13358 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13359 kind, dwo_name, hex_string (signature));
3019eac3 13360
6656a72d
DE
13361 /* This is a warning and not a complaint because it can be caused by
13362 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13363 {
13364 /* Print the name of the DWP file if we looked there, helps the user
13365 better diagnose the problem. */
791afaa2 13366 std::string dwp_text;
43942612
DE
13367
13368 if (dwp_file != NULL)
791afaa2
TT
13369 dwp_text = string_printf (" [in DWP file %s]",
13370 lbasename (dwp_file->name));
43942612 13371
9d8780f0 13372 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 13373 " [in module %s]"),
4ab09049
SM
13374 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13375 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 13376 }
3019eac3 13377 return NULL;
5fb290d7
DJ
13378}
13379
80626a55
DE
13380/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13381 See lookup_dwo_cutu_unit for details. */
13382
13383static struct dwo_unit *
4ab09049 13384lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
13385 ULONGEST signature)
13386{
4ab09049
SM
13387 gdb_assert (!cu->per_cu->is_debug_types);
13388
13389 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
13390}
13391
13392/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13393 See lookup_dwo_cutu_unit for details. */
13394
13395static struct dwo_unit *
4ab09049 13396lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 13397{
4ab09049
SM
13398 gdb_assert (cu->per_cu->is_debug_types);
13399
13400 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13401
13402 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
13403}
13404
89e63ee4
DE
13405/* Traversal function for queue_and_load_all_dwo_tus. */
13406
13407static int
13408queue_and_load_dwo_tu (void **slot, void *info)
13409{
13410 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 13411 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 13412 ULONGEST signature = dwo_unit->signature;
d460f660 13413 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
13414
13415 if (sig_type != NULL)
13416 {
13417 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13418
13419 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13420 a real dependency of PER_CU on SIG_TYPE. That is detected later
13421 while processing PER_CU. */
120ce1b5 13422 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
13423 load_full_type_unit (sig_cu, cu->per_objfile);
13424 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13425 }
13426
13427 return 1;
13428}
13429
1b555f17 13430/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
13431 The DWO may have the only definition of the type, though it may not be
13432 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13433 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13434
13435static void
1b555f17 13436queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
13437{
13438 struct dwo_unit *dwo_unit;
13439 struct dwo_file *dwo_file;
13440
1b555f17
SM
13441 gdb_assert (cu != nullptr);
13442 gdb_assert (!cu->per_cu->is_debug_types);
13443 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 13444
1b555f17 13445 dwo_unit = cu->dwo_unit;
89e63ee4
DE
13446 gdb_assert (dwo_unit != NULL);
13447
13448 dwo_file = dwo_unit->dwo_file;
13449 if (dwo_file->tus != NULL)
1b555f17 13450 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
13451}
13452
3019eac3 13453/* Read in various DIEs. */
348e048f 13454
d389af10 13455/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13456 Inherit only the children of the DW_AT_abstract_origin DIE not being
13457 already referenced by DW_AT_abstract_origin from the children of the
13458 current DIE. */
d389af10
JK
13459
13460static void
13461inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13462{
13463 struct die_info *child_die;
791afaa2 13464 sect_offset *offsetp;
d389af10
JK
13465 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13466 struct die_info *origin_die;
13467 /* Iterator of the ORIGIN_DIE children. */
13468 struct die_info *origin_child_die;
d389af10 13469 struct attribute *attr;
cd02d79d
PA
13470 struct dwarf2_cu *origin_cu;
13471 struct pending **origin_previous_list_in_scope;
d389af10
JK
13472
13473 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13474 if (!attr)
13475 return;
13476
cd02d79d
PA
13477 /* Note that following die references may follow to a die in a
13478 different cu. */
13479
13480 origin_cu = cu;
13481 origin_die = follow_die_ref (die, attr, &origin_cu);
13482
13483 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13484 symbols in. */
13485 origin_previous_list_in_scope = origin_cu->list_in_scope;
13486 origin_cu->list_in_scope = cu->list_in_scope;
13487
edb3359d
DJ
13488 if (die->tag != origin_die->tag
13489 && !(die->tag == DW_TAG_inlined_subroutine
13490 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13491 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13492 sect_offset_str (die->sect_off),
13493 sect_offset_str (origin_die->sect_off));
d389af10 13494
791afaa2 13495 std::vector<sect_offset> offsets;
d389af10 13496
3ea89b92
PMR
13497 for (child_die = die->child;
13498 child_die && child_die->tag;
436c571c 13499 child_die = child_die->sibling)
3ea89b92
PMR
13500 {
13501 struct die_info *child_origin_die;
13502 struct dwarf2_cu *child_origin_cu;
13503
13504 /* We are trying to process concrete instance entries:
216f72a1 13505 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13506 it's not relevant to our analysis here. i.e. detecting DIEs that are
13507 present in the abstract instance but not referenced in the concrete
13508 one. */
216f72a1 13509 if (child_die->tag == DW_TAG_call_site
dda83cd7 13510 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13511 continue;
13512
c38f313d
DJ
13513 /* For each CHILD_DIE, find the corresponding child of
13514 ORIGIN_DIE. If there is more than one layer of
13515 DW_AT_abstract_origin, follow them all; there shouldn't be,
13516 but GCC versions at least through 4.4 generate this (GCC PR
13517 40573). */
3ea89b92
PMR
13518 child_origin_die = child_die;
13519 child_origin_cu = cu;
c38f313d
DJ
13520 while (1)
13521 {
cd02d79d
PA
13522 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13523 child_origin_cu);
c38f313d
DJ
13524 if (attr == NULL)
13525 break;
cd02d79d
PA
13526 child_origin_die = follow_die_ref (child_origin_die, attr,
13527 &child_origin_cu);
c38f313d
DJ
13528 }
13529
d389af10
JK
13530 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13531 counterpart may exist. */
c38f313d 13532 if (child_origin_die != child_die)
d389af10 13533 {
edb3359d
DJ
13534 if (child_die->tag != child_origin_die->tag
13535 && !(child_die->tag == DW_TAG_inlined_subroutine
13536 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13537 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13538 "different tags"),
9d8780f0
SM
13539 sect_offset_str (child_die->sect_off),
13540 sect_offset_str (child_origin_die->sect_off));
c38f313d 13541 if (child_origin_die->parent != origin_die)
b98664d3 13542 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13543 "different parents"),
9d8780f0
SM
13544 sect_offset_str (child_die->sect_off),
13545 sect_offset_str (child_origin_die->sect_off));
c38f313d 13546 else
791afaa2 13547 offsets.push_back (child_origin_die->sect_off);
d389af10 13548 }
d389af10 13549 }
791afaa2
TT
13550 std::sort (offsets.begin (), offsets.end ());
13551 sect_offset *offsets_end = offsets.data () + offsets.size ();
13552 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13553 if (offsetp[-1] == *offsetp)
b98664d3 13554 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13555 "to DIE %s as their abstract origin"),
13556 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13557
791afaa2 13558 offsetp = offsets.data ();
d389af10
JK
13559 origin_child_die = origin_die->child;
13560 while (origin_child_die && origin_child_die->tag)
13561 {
13562 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13563 while (offsetp < offsets_end
9c541725 13564 && *offsetp < origin_child_die->sect_off)
d389af10 13565 offsetp++;
b64f50a1 13566 if (offsetp >= offsets_end
9c541725 13567 || *offsetp > origin_child_die->sect_off)
d389af10 13568 {
adde2bff
DE
13569 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13570 Check whether we're already processing ORIGIN_CHILD_DIE.
13571 This can happen with mutually referenced abstract_origins.
13572 PR 16581. */
13573 if (!origin_child_die->in_process)
13574 process_die (origin_child_die, origin_cu);
d389af10 13575 }
436c571c 13576 origin_child_die = origin_child_die->sibling;
d389af10 13577 }
cd02d79d 13578 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13579
13580 if (cu != origin_cu)
13581 compute_delayed_physnames (origin_cu);
d389af10
JK
13582}
13583
c906108c 13584static void
e7c27a73 13585read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13586{
5e22e966 13587 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13588 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13589 struct context_stack *newobj;
c906108c
SS
13590 CORE_ADDR lowpc;
13591 CORE_ADDR highpc;
13592 struct die_info *child_die;
edb3359d 13593 struct attribute *attr, *call_line, *call_file;
15d034d0 13594 const char *name;
e142c38c 13595 CORE_ADDR baseaddr;
801e3a5b 13596 struct block *block;
edb3359d 13597 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13598 std::vector<struct symbol *> template_args;
34eaf542 13599 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13600
13601 if (inlined_func)
13602 {
13603 /* If we do not have call site information, we can't show the
13604 caller of this inlined function. That's too confusing, so
13605 only use the scope for local variables. */
13606 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13607 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13608 if (call_line == NULL || call_file == NULL)
13609 {
13610 read_lexical_block_scope (die, cu);
13611 return;
13612 }
13613 }
c906108c 13614
b3b3bada 13615 baseaddr = objfile->text_section_offset ();
e142c38c 13616
94af9270 13617 name = dwarf2_name (die, cu);
c906108c 13618
e8d05480
JB
13619 /* Ignore functions with missing or empty names. These are actually
13620 illegal according to the DWARF standard. */
13621 if (name == NULL)
13622 {
b98664d3 13623 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13624 sect_offset_str (die->sect_off));
e8d05480
JB
13625 return;
13626 }
13627
13628 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13629 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13630 <= PC_BOUNDS_INVALID)
e8d05480 13631 {
ae4d0c03 13632 attr = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 13633 if (attr == nullptr || !attr->as_boolean ())
b98664d3 13634 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13635 "for subprogram DIE at %s"),
13636 sect_offset_str (die->sect_off));
e8d05480
JB
13637 return;
13638 }
c906108c 13639
3e29f34a
MR
13640 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13641 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13642
34eaf542
TT
13643 /* If we have any template arguments, then we must allocate a
13644 different sort of symbol. */
436c571c 13645 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13646 {
13647 if (child_die->tag == DW_TAG_template_type_param
13648 || child_die->tag == DW_TAG_template_value_param)
13649 {
8c14c3a3 13650 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13651 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13652 break;
13653 }
13654 }
13655
702cf3f5 13656 gdb_assert (cu->get_builder () != nullptr);
c24bdb02 13657 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13658 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13659 (struct symbol *) templ_func);
4c2df51b 13660
81873cc8 13661 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13662 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13663 cu->language);
13664
4cecd739
DJ
13665 /* If there is a location expression for DW_AT_frame_base, record
13666 it. */
e142c38c 13667 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13668 if (attr != nullptr)
fe978cb0 13669 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13670
63e43d3a
PMR
13671 /* If there is a location for the static link, record it. */
13672 newobj->static_link = NULL;
13673 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13674 if (attr != nullptr)
63e43d3a 13675 {
224c3ddb
SM
13676 newobj->static_link
13677 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13678 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13679 cu->addr_type ());
63e43d3a
PMR
13680 }
13681
c24bdb02 13682 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13683
639d11d3 13684 if (die->child != NULL)
c906108c 13685 {
639d11d3 13686 child_die = die->child;
c906108c
SS
13687 while (child_die && child_die->tag)
13688 {
34eaf542
TT
13689 if (child_die->tag == DW_TAG_template_type_param
13690 || child_die->tag == DW_TAG_template_value_param)
13691 {
13692 struct symbol *arg = new_symbol (child_die, NULL, cu);
13693
f1078f66 13694 if (arg != NULL)
2f4732b0 13695 template_args.push_back (arg);
34eaf542
TT
13696 }
13697 else
13698 process_die (child_die, cu);
436c571c 13699 child_die = child_die->sibling;
c906108c
SS
13700 }
13701 }
13702
d389af10
JK
13703 inherit_abstract_dies (die, cu);
13704
4a811a97
UW
13705 /* If we have a DW_AT_specification, we might need to import using
13706 directives from the context of the specification DIE. See the
13707 comment in determine_prefix. */
13708 if (cu->language == language_cplus
13709 && dwarf2_attr (die, DW_AT_specification, cu))
13710 {
13711 struct dwarf2_cu *spec_cu = cu;
13712 struct die_info *spec_die = die_specification (die, &spec_cu);
13713
13714 while (spec_die)
13715 {
13716 child_die = spec_die->child;
13717 while (child_die && child_die->tag)
13718 {
13719 if (child_die->tag == DW_TAG_imported_module)
13720 process_die (child_die, spec_cu);
436c571c 13721 child_die = child_die->sibling;
4a811a97
UW
13722 }
13723
13724 /* In some cases, GCC generates specification DIEs that
13725 themselves contain DW_AT_specification attributes. */
13726 spec_die = die_specification (spec_die, &spec_cu);
13727 }
13728 }
13729
c24bdb02 13730 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13731 /* Make a block for the local symbols within. */
c24bdb02 13732 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13733 cstk.static_link, lowpc, highpc);
801e3a5b 13734
df8a16a1 13735 /* For C++, set the block's scope. */
45280282
IB
13736 if ((cu->language == language_cplus
13737 || cu->language == language_fortran
c44af4eb
TT
13738 || cu->language == language_d
13739 || cu->language == language_rust)
4d4ec4e5 13740 && cu->processing_has_namespace_info)
195a3f6c
TT
13741 block_set_scope (block, determine_prefix (die, cu),
13742 &objfile->objfile_obstack);
df8a16a1 13743
801e3a5b
JB
13744 /* If we have address ranges, record them. */
13745 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13746
a60f3166 13747 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13748
34eaf542 13749 /* Attach template arguments to function. */
2f4732b0 13750 if (!template_args.empty ())
34eaf542
TT
13751 {
13752 gdb_assert (templ_func != NULL);
13753
2f4732b0 13754 templ_func->n_template_arguments = template_args.size ();
34eaf542 13755 templ_func->template_arguments
dda83cd7 13756 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
8d749320 13757 templ_func->n_template_arguments);
34eaf542 13758 memcpy (templ_func->template_arguments,
2f4732b0 13759 template_args.data (),
34eaf542 13760 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13761
13762 /* Make sure that the symtab is set on the new symbols. Even
13763 though they don't appear in this symtab directly, other parts
13764 of gdb assume that symbols do, and this is reasonably
13765 true. */
8634679f 13766 for (symbol *sym : template_args)
3e1d3d8c 13767 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13768 }
13769
208d8187
JB
13770 /* In C++, we can have functions nested inside functions (e.g., when
13771 a function declares a class that has methods). This means that
13772 when we finish processing a function scope, we may need to go
13773 back to building a containing block's symbol lists. */
c24bdb02
KS
13774 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13775 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13776
921e78cf
JB
13777 /* If we've finished processing a top-level function, subsequent
13778 symbols go in the file symbol list. */
c24bdb02
KS
13779 if (cu->get_builder ()->outermost_context_p ())
13780 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13781}
13782
13783/* Process all the DIES contained within a lexical block scope. Start
13784 a new scope, process the dies, and then close the scope. */
13785
13786static void
e7c27a73 13787read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13788{
5e22e966 13789 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13790 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13791 CORE_ADDR lowpc, highpc;
13792 struct die_info *child_die;
e142c38c
DJ
13793 CORE_ADDR baseaddr;
13794
b3b3bada 13795 baseaddr = objfile->text_section_offset ();
c906108c
SS
13796
13797 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13798 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13799 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13800 be nasty. Might be easier to properly extend generic blocks to
af34e669 13801 describe ranges. */
e385593e
JK
13802 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13803 {
13804 case PC_BOUNDS_NOT_PRESENT:
13805 /* DW_TAG_lexical_block has no attributes, process its children as if
13806 there was no wrapping by that DW_TAG_lexical_block.
13807 GCC does no longer produces such DWARF since GCC r224161. */
13808 for (child_die = die->child;
13809 child_die != NULL && child_die->tag;
436c571c 13810 child_die = child_die->sibling)
4f7bc5ed
TT
13811 {
13812 /* We might already be processing this DIE. This can happen
13813 in an unusual circumstance -- where a subroutine A
13814 appears lexically in another subroutine B, but A actually
13815 inlines B. The recursion is broken here, rather than in
13816 inherit_abstract_dies, because it seems better to simply
13817 drop concrete children here. */
13818 if (!child_die->in_process)
13819 process_die (child_die, cu);
13820 }
e385593e
JK
13821 return;
13822 case PC_BOUNDS_INVALID:
13823 return;
13824 }
3e29f34a
MR
13825 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13826 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13827
c24bdb02 13828 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13829 if (die->child != NULL)
c906108c 13830 {
639d11d3 13831 child_die = die->child;
c906108c
SS
13832 while (child_die && child_die->tag)
13833 {
e7c27a73 13834 process_die (child_die, cu);
436c571c 13835 child_die = child_die->sibling;
c906108c
SS
13836 }
13837 }
3ea89b92 13838 inherit_abstract_dies (die, cu);
c24bdb02 13839 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13840
c24bdb02
KS
13841 if (*cu->get_builder ()->get_local_symbols () != NULL
13842 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13843 {
801e3a5b 13844 struct block *block
dda83cd7 13845 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13846 cstk.start_addr, highpc);
801e3a5b
JB
13847
13848 /* Note that recording ranges after traversing children, as we
dda83cd7
SM
13849 do here, means that recording a parent's ranges entails
13850 walking across all its children's ranges as they appear in
13851 the address map, which is quadratic behavior.
13852
13853 It would be nicer to record the parent's ranges before
13854 traversing its children, simply overriding whatever you find
13855 there. But since we don't even decide whether to create a
13856 block until after we've traversed its children, that's hard
13857 to do. */
801e3a5b 13858 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13859 }
c24bdb02
KS
13860 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13861 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13862}
13863
216f72a1 13864/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13865
13866static void
13867read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13868{
5e22e966 13869 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13870 struct objfile *objfile = per_objfile->objfile;
08feed99 13871 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13872 CORE_ADDR pc, baseaddr;
13873 struct attribute *attr;
13874 struct call_site *call_site, call_site_local;
13875 void **slot;
13876 int nparams;
13877 struct die_info *child_die;
13878
b3b3bada 13879 baseaddr = objfile->text_section_offset ();
96408a79 13880
216f72a1
JK
13881 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13882 if (attr == NULL)
13883 {
13884 /* This was a pre-DWARF-5 GNU extension alias
13885 for DW_AT_call_return_pc. */
13886 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13887 }
96408a79
SA
13888 if (!attr)
13889 {
b98664d3 13890 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13891 "DIE %s [in module %s]"),
13892 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13893 return;
13894 }
95f982e5 13895 pc = attr->as_address () + baseaddr;
3e29f34a 13896 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13897
13898 if (cu->call_site_htab == NULL)
13899 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13900 NULL, &objfile->objfile_obstack,
13901 hashtab_obstack_allocate, NULL);
13902 call_site_local.pc = pc;
13903 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13904 if (*slot != NULL)
13905 {
b98664d3 13906 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13907 "DIE %s [in module %s]"),
13908 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13909 objfile_name (objfile));
96408a79
SA
13910 return;
13911 }
13912
13913 /* Count parameters at the caller. */
13914
13915 nparams = 0;
13916 for (child_die = die->child; child_die && child_die->tag;
436c571c 13917 child_die = child_die->sibling)
96408a79 13918 {
216f72a1 13919 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13920 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13921 {
b98664d3 13922 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13923 "DW_TAG_call_site child DIE %s [in module %s]"),
13924 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13925 objfile_name (objfile));
96408a79
SA
13926 continue;
13927 }
13928
13929 nparams++;
13930 }
13931
224c3ddb
SM
13932 call_site
13933 = ((struct call_site *)
13934 obstack_alloc (&objfile->objfile_obstack,
13935 sizeof (*call_site)
13936 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13937 *slot = call_site;
13938 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13939 call_site->pc = pc;
13940
216f72a1
JK
13941 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13942 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13943 {
13944 struct die_info *func_die;
13945
13946 /* Skip also over DW_TAG_inlined_subroutine. */
13947 for (func_die = die->parent;
13948 func_die && func_die->tag != DW_TAG_subprogram
13949 && func_die->tag != DW_TAG_subroutine_type;
13950 func_die = func_die->parent);
13951
216f72a1
JK
13952 /* DW_AT_call_all_calls is a superset
13953 of DW_AT_call_all_tail_calls. */
96408a79 13954 if (func_die
dda83cd7
SM
13955 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13956 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13957 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13958 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13959 {
13960 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13961 not complete. But keep CALL_SITE for look ups via call_site_htab,
13962 both the initial caller containing the real return address PC and
13963 the final callee containing the current PC of a chain of tail
13964 calls do not need to have the tail call list complete. But any
13965 function candidate for a virtual tail call frame searched via
13966 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13967 determined unambiguously. */
13968 }
13969 else
13970 {
13971 struct type *func_type = NULL;
13972
13973 if (func_die)
13974 func_type = get_die_type (func_die, cu);
13975 if (func_type != NULL)
13976 {
78134374 13977 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13978
13979 /* Enlist this call site to the function. */
13980 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13981 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13982 }
13983 else
b98664d3 13984 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13985 "DIE %s [in module %s]"),
13986 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13987 }
13988 }
13989
216f72a1
JK
13990 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13991 if (attr == NULL)
13992 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13993 if (attr == NULL)
13994 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13995 if (attr == NULL)
216f72a1
JK
13996 {
13997 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13998 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13999 }
96408a79 14000 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9d2246fc 14001 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
96408a79 14002 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 14003 else if (attr->form_is_block ())
96408a79
SA
14004 {
14005 struct dwarf2_locexpr_baton *dlbaton;
9d2246fc 14006 struct dwarf_block *block = attr->as_block ();
96408a79 14007
8d749320 14008 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9d2246fc
TT
14009 dlbaton->data = block->data;
14010 dlbaton->size = block->size;
a50264ba 14011 dlbaton->per_objfile = per_objfile;
96408a79
SA
14012 dlbaton->per_cu = cu->per_cu;
14013
14014 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14015 }
cd6c91b4 14016 else if (attr->form_is_ref ())
96408a79 14017 {
96408a79
SA
14018 struct dwarf2_cu *target_cu = cu;
14019 struct die_info *target_die;
14020
ac9ec31b 14021 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 14022 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
14023 if (die_is_declaration (target_die, target_cu))
14024 {
7d45c7c3 14025 const char *target_physname;
9112db09
JK
14026
14027 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14028 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14029 if (target_physname == NULL)
9112db09 14030 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14031 if (target_physname == NULL)
b98664d3 14032 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 14033 "physname, for referencing DIE %s [in module %s]"),
9d8780f0 14034 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14035 else
7d455152 14036 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14037 }
14038 else
14039 {
14040 CORE_ADDR lowpc;
14041
14042 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14043 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14044 <= PC_BOUNDS_INVALID)
b98664d3 14045 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 14046 "low pc, for referencing DIE %s [in module %s]"),
9d8780f0 14047 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14048 else
3e29f34a
MR
14049 {
14050 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14051 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14052 }
96408a79
SA
14053 }
14054 }
14055 else
b98664d3 14056 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14057 "block nor reference, for DIE %s [in module %s]"),
14058 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14059
14060 call_site->per_cu = cu->per_cu;
9f47c707 14061 call_site->per_objfile = per_objfile;
96408a79
SA
14062
14063 for (child_die = die->child;
14064 child_die && child_die->tag;
436c571c 14065 child_die = child_die->sibling)
96408a79 14066 {
96408a79 14067 struct call_site_parameter *parameter;
1788b2d3 14068 struct attribute *loc, *origin;
96408a79 14069
216f72a1 14070 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 14071 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14072 {
14073 /* Already printed the complaint above. */
14074 continue;
14075 }
14076
14077 gdb_assert (call_site->parameter_count < nparams);
14078 parameter = &call_site->parameter[call_site->parameter_count];
14079
1788b2d3
JK
14080 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14081 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14082 register is contained in DW_AT_call_value. */
96408a79 14083
24c5c679 14084 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14085 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14086 if (origin == NULL)
14087 {
14088 /* This was a pre-DWARF-5 GNU extension alias
14089 for DW_AT_call_parameter. */
14090 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14091 }
cd6c91b4 14092 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 14093 {
1788b2d3 14094 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 14095
0826b30a 14096 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 14097 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
14098 {
14099 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14100 binding can be done only inside one CU. Such referenced DIE
14101 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14102 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14103 "DW_TAG_call_site child DIE %s [in module %s]"),
14104 sect_offset_str (child_die->sect_off),
9c541725 14105 objfile_name (objfile));
d76b7dbc
JK
14106 continue;
14107 }
9c541725
PA
14108 parameter->u.param_cu_off
14109 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 14110 }
4fc6c0d5 14111 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 14112 {
b98664d3 14113 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14114 "DW_TAG_call_site child DIE %s [in module %s]"),
14115 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14116 continue;
14117 }
24c5c679 14118 else
96408a79 14119 {
9d2246fc
TT
14120 struct dwarf_block *block = loc->as_block ();
14121
24c5c679 14122 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9d2246fc 14123 (block->data, &block->data[block->size]);
24c5c679
JK
14124 if (parameter->u.dwarf_reg != -1)
14125 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9d2246fc
TT
14126 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14127 &block->data[block->size],
24c5c679
JK
14128 &parameter->u.fb_offset))
14129 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14130 else
14131 {
b98664d3 14132 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14133 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14134 "DW_TAG_call_site child DIE %s "
24c5c679 14135 "[in module %s]"),
9d8780f0 14136 sect_offset_str (child_die->sect_off),
9c541725 14137 objfile_name (objfile));
24c5c679
JK
14138 continue;
14139 }
96408a79
SA
14140 }
14141
216f72a1
JK
14142 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14143 if (attr == NULL)
14144 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 14145 if (attr == NULL || !attr->form_is_block ())
96408a79 14146 {
b98664d3 14147 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14148 "DW_TAG_call_site child DIE %s [in module %s]"),
14149 sect_offset_str (child_die->sect_off),
9c541725 14150 objfile_name (objfile));
96408a79
SA
14151 continue;
14152 }
9d2246fc
TT
14153
14154 struct dwarf_block *block = attr->as_block ();
14155 parameter->value = block->data;
14156 parameter->value_size = block->size;
96408a79
SA
14157
14158 /* Parameters are not pre-cleared by memset above. */
14159 parameter->data_value = NULL;
14160 parameter->data_value_size = 0;
14161 call_site->parameter_count++;
14162
216f72a1
JK
14163 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14164 if (attr == NULL)
14165 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14166 if (attr != nullptr)
96408a79 14167 {
4fc6c0d5 14168 if (!attr->form_is_block ())
b98664d3 14169 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14170 "DW_TAG_call_site child DIE %s [in module %s]"),
14171 sect_offset_str (child_die->sect_off),
9c541725 14172 objfile_name (objfile));
96408a79
SA
14173 else
14174 {
9d2246fc
TT
14175 block = attr->as_block ();
14176 parameter->data_value = block->data;
14177 parameter->data_value_size = block->size;
96408a79
SA
14178 }
14179 }
14180 }
14181}
14182
71a3c369
TT
14183/* Helper function for read_variable. If DIE represents a virtual
14184 table, then return the type of the concrete object that is
14185 associated with the virtual table. Otherwise, return NULL. */
14186
14187static struct type *
14188rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14189{
14190 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14191 if (attr == NULL)
14192 return NULL;
14193
14194 /* Find the type DIE. */
14195 struct die_info *type_die = NULL;
14196 struct dwarf2_cu *type_cu = cu;
14197
cd6c91b4 14198 if (attr->form_is_ref ())
71a3c369
TT
14199 type_die = follow_die_ref (die, attr, &type_cu);
14200 if (type_die == NULL)
14201 return NULL;
14202
14203 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14204 return NULL;
14205 return die_containing_type (type_die, type_cu);
14206}
14207
14208/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14209
14210static void
14211read_variable (struct die_info *die, struct dwarf2_cu *cu)
14212{
14213 struct rust_vtable_symbol *storage = NULL;
14214
14215 if (cu->language == language_rust)
14216 {
14217 struct type *containing_type = rust_containing_type (die, cu);
14218
14219 if (containing_type != NULL)
14220 {
5e22e966 14221 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 14222
8c14c3a3 14223 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 14224 storage->concrete_type = containing_type;
cf724bc9 14225 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14226 }
14227 }
14228
e4a62c65
TV
14229 struct symbol *res = new_symbol (die, NULL, cu, storage);
14230 struct attribute *abstract_origin
14231 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14232 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14233 if (res == NULL && loc && abstract_origin)
14234 {
14235 /* We have a variable without a name, but with a location and an abstract
14236 origin. This may be a concrete instance of an abstract variable
14237 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14238 later. */
14239 struct dwarf2_cu *origin_cu = cu;
14240 struct die_info *origin_die
14241 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
14242 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14243 per_objfile->per_bfd->abstract_to_concrete
14244 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14245 }
71a3c369
TT
14246}
14247
43988095
JK
14248/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14249 reading .debug_rnglists.
14250 Callback's type should be:
14251 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14252 Return true if the attributes are present and valid, otherwise,
14253 return false. */
14254
14255template <typename Callback>
14256static bool
14257dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
d0ce17d8 14258 dwarf_tag tag, Callback &&callback)
43988095 14259{
976ca316
SM
14260 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14261 struct objfile *objfile = per_objfile->objfile;
43988095 14262 bfd *obfd = objfile->obfd;
43988095 14263 /* Base address selection entry. */
2b24b6e4 14264 gdb::optional<CORE_ADDR> base;
43988095 14265 const gdb_byte *buffer;
43988095
JK
14266 CORE_ADDR baseaddr;
14267 bool overflow = false;
d0ce17d8
CT
14268 ULONGEST addr_index;
14269 struct dwarf2_section_info *rnglists_section;
43988095 14270
43988095 14271 base = cu->base_address;
d0ce17d8
CT
14272 rnglists_section = cu_debug_rnglists_section (cu, tag);
14273 rnglists_section->read (objfile);
43988095 14274
d0ce17d8 14275 if (offset >= rnglists_section->size)
43988095 14276 {
b98664d3 14277 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14278 offset);
14279 return false;
14280 }
d0ce17d8 14281 buffer = rnglists_section->buffer + offset;
43988095 14282
b3b3bada 14283 baseaddr = objfile->text_section_offset ();
43988095
JK
14284
14285 while (1)
14286 {
7814882a
JK
14287 /* Initialize it due to a false compiler warning. */
14288 CORE_ADDR range_beginning = 0, range_end = 0;
d0ce17d8
CT
14289 const gdb_byte *buf_end = (rnglists_section->buffer
14290 + rnglists_section->size);
43988095
JK
14291 unsigned int bytes_read;
14292
14293 if (buffer == buf_end)
14294 {
14295 overflow = true;
14296 break;
14297 }
14298 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14299 switch (rlet)
14300 {
14301 case DW_RLE_end_of_list:
14302 break;
14303 case DW_RLE_base_address:
14304 if (buffer + cu->header.addr_size > buf_end)
14305 {
14306 overflow = true;
14307 break;
14308 }
c8a7a66f 14309 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14310 buffer += bytes_read;
14311 break;
dda83cd7
SM
14312 case DW_RLE_base_addressx:
14313 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14314 buffer += bytes_read;
14315 base = read_addr_index (cu, addr_index);
14316 break;
43988095
JK
14317 case DW_RLE_start_length:
14318 if (buffer + cu->header.addr_size > buf_end)
14319 {
14320 overflow = true;
14321 break;
14322 }
c8a7a66f
TT
14323 range_beginning = cu->header.read_address (obfd, buffer,
14324 &bytes_read);
43988095
JK
14325 buffer += bytes_read;
14326 range_end = (range_beginning
14327 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14328 buffer += bytes_read;
14329 if (buffer > buf_end)
14330 {
14331 overflow = true;
14332 break;
14333 }
14334 break;
d0ce17d8 14335 case DW_RLE_startx_length:
dda83cd7
SM
14336 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14337 buffer += bytes_read;
14338 range_beginning = read_addr_index (cu, addr_index);
14339 if (buffer > buf_end)
14340 {
14341 overflow = true;
14342 break;
14343 }
14344 range_end = (range_beginning
14345 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14346 buffer += bytes_read;
14347 break;
43988095
JK
14348 case DW_RLE_offset_pair:
14349 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14350 buffer += bytes_read;
14351 if (buffer > buf_end)
14352 {
14353 overflow = true;
14354 break;
14355 }
14356 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14357 buffer += bytes_read;
14358 if (buffer > buf_end)
14359 {
14360 overflow = true;
14361 break;
14362 }
14363 break;
14364 case DW_RLE_start_end:
14365 if (buffer + 2 * cu->header.addr_size > buf_end)
14366 {
14367 overflow = true;
14368 break;
14369 }
c8a7a66f
TT
14370 range_beginning = cu->header.read_address (obfd, buffer,
14371 &bytes_read);
43988095 14372 buffer += bytes_read;
c8a7a66f 14373 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14374 buffer += bytes_read;
14375 break;
d0ce17d8 14376 case DW_RLE_startx_endx:
dda83cd7
SM
14377 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14378 buffer += bytes_read;
14379 range_beginning = read_addr_index (cu, addr_index);
14380 if (buffer > buf_end)
14381 {
14382 overflow = true;
14383 break;
14384 }
14385 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14386 buffer += bytes_read;
14387 range_end = read_addr_index (cu, addr_index);
14388 break;
43988095 14389 default:
b98664d3 14390 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14391 return false;
14392 }
14393 if (rlet == DW_RLE_end_of_list || overflow)
14394 break;
14395 if (rlet == DW_RLE_base_address)
14396 continue;
14397
43988095
JK
14398 if (range_beginning > range_end)
14399 {
14400 /* Inverted range entries are invalid. */
b98664d3 14401 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14402 return false;
14403 }
14404
14405 /* Empty range entries have no effect. */
14406 if (range_beginning == range_end)
14407 continue;
14408
d0ce17d8
CT
14409 /* Only DW_RLE_offset_pair needs the base address added. */
14410 if (rlet == DW_RLE_offset_pair)
14411 {
14412 if (!base.has_value ())
14413 {
14414 /* We have no valid base address for the DW_RLE_offset_pair. */
14415 complaint (_("Invalid .debug_rnglists data (no base address for "
14416 "DW_RLE_offset_pair)"));
14417 return false;
14418 }
14419
14420 range_beginning += *base;
14421 range_end += *base;
14422 }
43988095
JK
14423
14424 /* A not-uncommon case of bad debug info.
14425 Don't pollute the addrmap with bad data. */
14426 if (range_beginning + baseaddr == 0
976ca316 14427 && !per_objfile->per_bfd->has_section_at_zero)
43988095 14428 {
b98664d3 14429 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14430 " [in module %s]"), objfile_name (objfile));
14431 continue;
14432 }
14433
14434 callback (range_beginning, range_end);
14435 }
14436
14437 if (overflow)
14438 {
b98664d3 14439 complaint (_("Offset %d is not terminated "
43988095
JK
14440 "for DW_AT_ranges attribute"),
14441 offset);
14442 return false;
14443 }
14444
14445 return true;
14446}
14447
14448/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14449 Callback's type should be:
14450 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14451 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14452
43988095 14453template <typename Callback>
43039443 14454static int
d0ce17d8 14455dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
43988095 14456 Callback &&callback)
43039443 14457{
5e22e966
SM
14458 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14459 struct objfile *objfile = per_objfile->objfile;
43039443
JK
14460 struct comp_unit_head *cu_header = &cu->header;
14461 bfd *obfd = objfile->obfd;
14462 unsigned int addr_size = cu_header->addr_size;
14463 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14464 /* Base address selection entry. */
2b24b6e4 14465 gdb::optional<CORE_ADDR> base;
43039443 14466 unsigned int dummy;
d521ce57 14467 const gdb_byte *buffer;
ff013f42 14468 CORE_ADDR baseaddr;
43039443 14469
43988095 14470 if (cu_header->version >= 5)
d0ce17d8 14471 return dwarf2_rnglists_process (offset, cu, tag, callback);
43988095 14472
d00adf39 14473 base = cu->base_address;
43039443 14474
5e22e966
SM
14475 per_objfile->per_bfd->ranges.read (objfile);
14476 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 14477 {
b98664d3 14478 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14479 offset);
14480 return 0;
14481 }
5e22e966 14482 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 14483
b3b3bada 14484 baseaddr = objfile->text_section_offset ();
ff013f42 14485
43039443
JK
14486 while (1)
14487 {
14488 CORE_ADDR range_beginning, range_end;
14489
c8a7a66f 14490 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 14491 buffer += addr_size;
c8a7a66f 14492 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
14493 buffer += addr_size;
14494 offset += 2 * addr_size;
14495
14496 /* An end of list marker is a pair of zero addresses. */
14497 if (range_beginning == 0 && range_end == 0)
14498 /* Found the end of list entry. */
14499 break;
14500
14501 /* Each base address selection entry is a pair of 2 values.
14502 The first is the largest possible address, the second is
14503 the base address. Check for a base address here. */
14504 if ((range_beginning & mask) == mask)
14505 {
28d2bfb9
AB
14506 /* If we found the largest possible address, then we already
14507 have the base address in range_end. */
14508 base = range_end;
43039443
JK
14509 continue;
14510 }
14511
2b24b6e4 14512 if (!base.has_value ())
43039443
JK
14513 {
14514 /* We have no valid base address for the ranges
14515 data. */
b98664d3 14516 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14517 return 0;
14518 }
14519
9277c30c
UW
14520 if (range_beginning > range_end)
14521 {
14522 /* Inverted range entries are invalid. */
b98664d3 14523 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14524 return 0;
14525 }
14526
14527 /* Empty range entries have no effect. */
14528 if (range_beginning == range_end)
14529 continue;
14530
2b24b6e4
TT
14531 range_beginning += *base;
14532 range_end += *base;
43039443 14533
01093045
DE
14534 /* A not-uncommon case of bad debug info.
14535 Don't pollute the addrmap with bad data. */
14536 if (range_beginning + baseaddr == 0
5e22e966 14537 && !per_objfile->per_bfd->has_section_at_zero)
01093045 14538 {
b98664d3 14539 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14540 " [in module %s]"), objfile_name (objfile));
01093045
DE
14541 continue;
14542 }
14543
5f46c5a5
JK
14544 callback (range_beginning, range_end);
14545 }
14546
14547 return 1;
14548}
14549
14550/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14551 Return 1 if the attributes are present and valid, otherwise, return 0.
efd7398e 14552 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
5f46c5a5
JK
14553
14554static int
14555dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14556 CORE_ADDR *high_return, struct dwarf2_cu *cu,
d0ce17d8 14557 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
5f46c5a5 14558{
5e22e966 14559 struct objfile *objfile = cu->per_objfile->objfile;
84685904 14560 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 14561 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14562 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14563 int low_set = 0;
14564 CORE_ADDR low = 0;
14565 CORE_ADDR high = 0;
14566 int retval;
14567
d0ce17d8 14568 retval = dwarf2_ranges_process (offset, cu, tag,
5f46c5a5
JK
14569 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14570 {
9277c30c 14571 if (ranges_pst != NULL)
3e29f34a
MR
14572 {
14573 CORE_ADDR lowpc;
14574 CORE_ADDR highpc;
14575
79748972
TT
14576 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14577 range_beginning + baseaddr)
14578 - baseaddr);
14579 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14580 range_end + baseaddr)
14581 - baseaddr);
84685904 14582 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 14583 lowpc, highpc - 1, ranges_pst);
3e29f34a 14584 }
ff013f42 14585
43039443
JK
14586 /* FIXME: This is recording everything as a low-high
14587 segment of consecutive addresses. We should have a
14588 data structure for discontiguous block ranges
14589 instead. */
14590 if (! low_set)
14591 {
14592 low = range_beginning;
14593 high = range_end;
14594 low_set = 1;
14595 }
14596 else
14597 {
14598 if (range_beginning < low)
14599 low = range_beginning;
14600 if (range_end > high)
14601 high = range_end;
14602 }
5f46c5a5
JK
14603 });
14604 if (!retval)
14605 return 0;
43039443
JK
14606
14607 if (! low_set)
14608 /* If the first entry is an end-of-list marker, the range
14609 describes an empty scope, i.e. no instructions. */
14610 return 0;
14611
14612 if (low_return)
14613 *low_return = low;
14614 if (high_return)
14615 *high_return = high;
14616 return 1;
14617}
14618
3a2b436a
JK
14619/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14620 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14621 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14622
3a2b436a 14623static enum pc_bounds_kind
af34e669 14624dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14625 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14626 dwarf2_psymtab *pst)
c906108c 14627{
976ca316 14628 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14629 struct attribute *attr;
91da1414 14630 struct attribute *attr_high;
af34e669
DJ
14631 CORE_ADDR low = 0;
14632 CORE_ADDR high = 0;
e385593e 14633 enum pc_bounds_kind ret;
c906108c 14634
91da1414
MW
14635 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14636 if (attr_high)
af34e669 14637 {
e142c38c 14638 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14639 if (attr != nullptr)
dda83cd7 14640 {
95f982e5
TT
14641 low = attr->as_address ();
14642 high = attr_high->as_address ();
cd6c91b4 14643 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14644 high += low;
91da1414 14645 }
af34e669
DJ
14646 else
14647 /* Found high w/o low attribute. */
e385593e 14648 return PC_BOUNDS_INVALID;
af34e669
DJ
14649
14650 /* Found consecutive range of addresses. */
3a2b436a 14651 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14652 }
c906108c 14653 else
af34e669 14654 {
e142c38c 14655 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14656 if (attr != nullptr && attr->form_is_unsigned ())
af34e669 14657 {
2b0c7f41
SM
14658 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14659 on DWARF version). */
14660 ULONGEST ranges_offset = attr->as_unsigned ();
14661
14662 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14663 this value. */
14664 if (die->tag != DW_TAG_compile_unit)
14665 ranges_offset += cu->gnu_ranges_base;
2e3cf129 14666
af34e669 14667 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14668 .debug_ranges section. */
d0ce17d8
CT
14669 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14670 die->tag))
e385593e 14671 return PC_BOUNDS_INVALID;
43039443 14672 /* Found discontinuous range of addresses. */
3a2b436a 14673 ret = PC_BOUNDS_RANGES;
af34e669 14674 }
e385593e
JK
14675 else
14676 return PC_BOUNDS_NOT_PRESENT;
af34e669 14677 }
c906108c 14678
48fbe735 14679 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14680 if (high <= low)
e385593e 14681 return PC_BOUNDS_INVALID;
c906108c
SS
14682
14683 /* When using the GNU linker, .gnu.linkonce. sections are used to
14684 eliminate duplicate copies of functions and vtables and such.
14685 The linker will arbitrarily choose one and discard the others.
14686 The AT_*_pc values for such functions refer to local labels in
14687 these sections. If the section from that file was discarded, the
14688 labels are not in the output, so the relocs get a value of 0.
14689 If this is a discarded function, mark the pc bounds as invalid,
14690 so that GDB will ignore it. */
976ca316 14691 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14692 return PC_BOUNDS_INVALID;
c906108c
SS
14693
14694 *lowpc = low;
96408a79
SA
14695 if (highpc)
14696 *highpc = high;
af34e669 14697 return ret;
c906108c
SS
14698}
14699
b084d499
JB
14700/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14701 its low and high PC addresses. Do nothing if these addresses could not
14702 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14703 and HIGHPC to the high address if greater than HIGHPC. */
14704
14705static void
14706dwarf2_get_subprogram_pc_bounds (struct die_info *die,
dda83cd7
SM
14707 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14708 struct dwarf2_cu *cu)
b084d499
JB
14709{
14710 CORE_ADDR low, high;
14711 struct die_info *child = die->child;
14712
e385593e 14713 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14714 {
325fac50
PA
14715 *lowpc = std::min (*lowpc, low);
14716 *highpc = std::max (*highpc, high);
b084d499
JB
14717 }
14718
14719 /* If the language does not allow nested subprograms (either inside
14720 subprograms or lexical blocks), we're done. */
14721 if (cu->language != language_ada)
14722 return;
6e70227d 14723
b084d499
JB
14724 /* Check all the children of the given DIE. If it contains nested
14725 subprograms, then check their pc bounds. Likewise, we need to
14726 check lexical blocks as well, as they may also contain subprogram
14727 definitions. */
14728 while (child && child->tag)
14729 {
14730 if (child->tag == DW_TAG_subprogram
dda83cd7
SM
14731 || child->tag == DW_TAG_lexical_block)
14732 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14733 child = child->sibling;
b084d499
JB
14734 }
14735}
14736
fae299cd
DC
14737/* Get the low and high pc's represented by the scope DIE, and store
14738 them in *LOWPC and *HIGHPC. If the correct values can't be
14739 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14740
14741static void
14742get_scope_pc_bounds (struct die_info *die,
14743 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14744 struct dwarf2_cu *cu)
14745{
14746 CORE_ADDR best_low = (CORE_ADDR) -1;
14747 CORE_ADDR best_high = (CORE_ADDR) 0;
14748 CORE_ADDR current_low, current_high;
14749
3a2b436a 14750 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14751 >= PC_BOUNDS_RANGES)
fae299cd
DC
14752 {
14753 best_low = current_low;
14754 best_high = current_high;
14755 }
14756 else
14757 {
14758 struct die_info *child = die->child;
14759
14760 while (child && child->tag)
14761 {
14762 switch (child->tag) {
14763 case DW_TAG_subprogram:
dda83cd7 14764 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14765 break;
14766 case DW_TAG_namespace:
f55ee35c 14767 case DW_TAG_module:
fae299cd
DC
14768 /* FIXME: carlton/2004-01-16: Should we do this for
14769 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14770 that current GCC's always emit the DIEs corresponding
14771 to definitions of methods of classes as children of a
14772 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14773 the DIEs giving the declarations, which could be
14774 anywhere). But I don't see any reason why the
14775 standards says that they have to be there. */
14776 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14777
14778 if (current_low != ((CORE_ADDR) -1))
14779 {
325fac50
PA
14780 best_low = std::min (best_low, current_low);
14781 best_high = std::max (best_high, current_high);
fae299cd
DC
14782 }
14783 break;
14784 default:
0963b4bd 14785 /* Ignore. */
fae299cd
DC
14786 break;
14787 }
14788
436c571c 14789 child = child->sibling;
fae299cd
DC
14790 }
14791 }
14792
14793 *lowpc = best_low;
14794 *highpc = best_high;
14795}
14796
801e3a5b
JB
14797/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14798 in DIE. */
380bca97 14799
801e3a5b
JB
14800static void
14801dwarf2_record_block_ranges (struct die_info *die, struct block *block,
dda83cd7 14802 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
801e3a5b 14803{
5e22e966 14804 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14805 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14806 struct attribute *attr;
91da1414 14807 struct attribute *attr_high;
801e3a5b 14808
91da1414
MW
14809 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14810 if (attr_high)
801e3a5b 14811 {
801e3a5b 14812 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14813 if (attr != nullptr)
dda83cd7 14814 {
95f982e5
TT
14815 CORE_ADDR low = attr->as_address ();
14816 CORE_ADDR high = attr_high->as_address ();
31aa7e4e 14817
cd6c91b4 14818 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14819 high += low;
9a619af0 14820
3e29f34a
MR
14821 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14822 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14823 cu->get_builder ()->record_block_range (block, low, high - 1);
dda83cd7 14824 }
801e3a5b
JB
14825 }
14826
14827 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14828 if (attr != nullptr && attr->form_is_unsigned ())
801e3a5b 14829 {
2b0c7f41
SM
14830 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14831 on DWARF version). */
14832 ULONGEST ranges_offset = attr->as_unsigned ();
801e3a5b 14833
2b0c7f41
SM
14834 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14835 this value. */
14836 if (die->tag != DW_TAG_compile_unit)
14837 ranges_offset += cu->gnu_ranges_base;
801e3a5b 14838
2d5f09ec 14839 std::vector<blockrange> blockvec;
2b0c7f41 14840 dwarf2_ranges_process (ranges_offset, cu, die->tag,
5f46c5a5
JK
14841 [&] (CORE_ADDR start, CORE_ADDR end)
14842 {
58fdfd2c
JK
14843 start += baseaddr;
14844 end += baseaddr;
5f46c5a5
JK
14845 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14846 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14847 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14848 blockvec.emplace_back (start, end);
5f46c5a5 14849 });
2d5f09ec
KB
14850
14851 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14852 }
14853}
14854
685b1105
JK
14855/* Check whether the producer field indicates either of GCC < 4.6, or the
14856 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14857
685b1105
JK
14858static void
14859check_producer (struct dwarf2_cu *cu)
60d5a603 14860{
38360086 14861 int major, minor;
60d5a603
JK
14862
14863 if (cu->producer == NULL)
14864 {
14865 /* For unknown compilers expect their behavior is DWARF version
14866 compliant.
14867
14868 GCC started to support .debug_types sections by -gdwarf-4 since
14869 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14870 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14871 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14872 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14873 }
b1ffba5a 14874 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14875 {
38360086
MW
14876 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14877 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14878 }
5230b05a 14879 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14880 {
14881 cu->producer_is_icc = true;
14882 cu->producer_is_icc_lt_14 = major < 14;
14883 }
c258c396
JD
14884 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14885 cu->producer_is_codewarrior = true;
685b1105
JK
14886 else
14887 {
14888 /* For other non-GCC compilers, expect their behavior is DWARF version
14889 compliant. */
60d5a603
JK
14890 }
14891
9068261f 14892 cu->checked_producer = true;
685b1105 14893}
ba919b58 14894
685b1105
JK
14895/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14896 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14897 during 4.6.0 experimental. */
14898
9068261f 14899static bool
685b1105
JK
14900producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14901{
14902 if (!cu->checked_producer)
14903 check_producer (cu);
14904
14905 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14906}
14907
c258c396
JD
14908
14909/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14910 with incorrect is_stmt attributes. */
14911
14912static bool
14913producer_is_codewarrior (struct dwarf2_cu *cu)
14914{
14915 if (!cu->checked_producer)
14916 check_producer (cu);
14917
14918 return cu->producer_is_codewarrior;
14919}
14920
bf23a268
TT
14921/* Return the accessibility of DIE, as given by DW_AT_accessibility.
14922 If that attribute is not available, return the appropriate
14923 default. */
60d5a603
JK
14924
14925static enum dwarf_access_attribute
bf23a268 14926dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
60d5a603 14927{
bf23a268
TT
14928 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14929 if (attr != nullptr)
14930 {
14931 LONGEST value = attr->constant_value (-1);
14932 if (value == DW_ACCESS_public
14933 || value == DW_ACCESS_protected
14934 || value == DW_ACCESS_private)
14935 return (dwarf_access_attribute) value;
14936 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14937 plongest (value));
14938 }
14939
60d5a603
JK
14940 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14941 {
14942 /* The default DWARF 2 accessibility for members is public, the default
14943 accessibility for inheritance is private. */
14944
14945 if (die->tag != DW_TAG_inheritance)
14946 return DW_ACCESS_public;
14947 else
14948 return DW_ACCESS_private;
14949 }
14950 else
14951 {
14952 /* DWARF 3+ defines the default accessibility a different way. The same
14953 rules apply now for DW_TAG_inheritance as for the members and it only
14954 depends on the container kind. */
14955
14956 if (die->parent->tag == DW_TAG_class_type)
14957 return DW_ACCESS_private;
14958 else
14959 return DW_ACCESS_public;
14960 }
14961}
14962
74ac6d43
TT
14963/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14964 offset. If the attribute was not found return 0, otherwise return
14965 1. If it was found but could not properly be handled, set *OFFSET
14966 to 0. */
14967
14968static int
14969handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14970 LONGEST *offset)
14971{
14972 struct attribute *attr;
14973
14974 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14975 if (attr != NULL)
14976 {
14977 *offset = 0;
14978
14979 /* Note that we do not check for a section offset first here.
14980 This is because DW_AT_data_member_location is new in DWARF 4,
14981 so if we see it, we can assume that a constant form is really
14982 a constant and not a section offset. */
cd6c91b4 14983 if (attr->form_is_constant ())
0826b30a 14984 *offset = attr->constant_value (0);
cd6c91b4 14985 else if (attr->form_is_section_offset ())
74ac6d43 14986 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14987 else if (attr->form_is_block ())
9d2246fc 14988 *offset = decode_locdesc (attr->as_block (), cu);
74ac6d43
TT
14989 else
14990 dwarf2_complex_location_expr_complaint ();
14991
14992 return 1;
14993 }
14994
14995 return 0;
14996}
14997
7d79de9a
TT
14998/* Look for DW_AT_data_member_location and store the results in FIELD. */
14999
15000static void
15001handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15002 struct field *field)
15003{
15004 struct attribute *attr;
15005
15006 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15007 if (attr != NULL)
15008 {
15009 if (attr->form_is_constant ())
15010 {
15011 LONGEST offset = attr->constant_value (0);
15012 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15013 }
15014 else if (attr->form_is_section_offset ())
15015 dwarf2_complex_location_expr_complaint ();
15016 else if (attr->form_is_block ())
15017 {
15018 bool handled;
9d2246fc 15019 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
7d79de9a
TT
15020 if (handled)
15021 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15022 else
15023 {
5e22e966
SM
15024 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15025 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
15026 struct dwarf2_locexpr_baton *dlbaton
15027 = XOBNEW (&objfile->objfile_obstack,
15028 struct dwarf2_locexpr_baton);
9d2246fc
TT
15029 dlbaton->data = attr->as_block ()->data;
15030 dlbaton->size = attr->as_block ()->size;
7d79de9a
TT
15031 /* When using this baton, we want to compute the address
15032 of the field, not the value. This is why
15033 is_reference is set to false here. */
15034 dlbaton->is_reference = false;
5e22e966 15035 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
15036 dlbaton->per_cu = cu->per_cu;
15037
15038 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
15039 }
15040 }
15041 else
15042 dwarf2_complex_location_expr_complaint ();
15043 }
15044}
15045
c906108c
SS
15046/* Add an aggregate field to the field list. */
15047
15048static void
107d2387 15049dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15050 struct dwarf2_cu *cu)
6e70227d 15051{
5e22e966 15052 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 15053 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
15054 struct nextfield *new_field;
15055 struct attribute *attr;
15056 struct field *fp;
15d034d0 15057 const char *fieldname = "";
c906108c 15058
7d0ccb61
DJ
15059 if (die->tag == DW_TAG_inheritance)
15060 {
be2daae6
TT
15061 fip->baseclasses.emplace_back ();
15062 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15063 }
15064 else
15065 {
be2daae6
TT
15066 fip->fields.emplace_back ();
15067 new_field = &fip->fields.back ();
7d0ccb61 15068 }
be2daae6 15069
9c6a1327
TT
15070 new_field->offset = die->sect_off;
15071
bf23a268 15072 new_field->accessibility = dwarf2_access_attribute (die, cu);
c906108c 15073 if (new_field->accessibility != DW_ACCESS_public)
264fc0e2 15074 fip->non_public_fields = true;
60d5a603 15075
e142c38c 15076 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 15077 if (attr != nullptr)
23dca5c3 15078 new_field->virtuality = attr->as_virtuality ();
60d5a603
JK
15079 else
15080 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15081
15082 fp = &new_field->field;
a9a9bd0f 15083
e142c38c 15084 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15085 {
a9a9bd0f 15086 /* Data member other than a C++ static data member. */
6e70227d 15087
c906108c 15088 /* Get type of field. */
5d14b6e5 15089 fp->set_type (die_type (die, cu));
c906108c 15090
d6a843b5 15091 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15092
c906108c 15093 /* Get bit size of field (zero if none). */
e142c38c 15094 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 15095 if (attr != nullptr)
c906108c 15096 {
529908cb 15097 FIELD_BITSIZE (*fp) = attr->constant_value (0);
c906108c
SS
15098 }
15099 else
15100 {
15101 FIELD_BITSIZE (*fp) = 0;
15102 }
15103
15104 /* Get bit offset of field. */
7d79de9a 15105 handle_data_member_location (die, cu, fp);
e142c38c 15106 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7c184d33 15107 if (attr != nullptr && attr->form_is_constant ())
c906108c 15108 {
d5a22e77 15109 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
15110 {
15111 /* For big endian bits, the DW_AT_bit_offset gives the
dda83cd7
SM
15112 additional bit offset from the MSB of the containing
15113 anonymous object to the MSB of the field. We don't
15114 have to do anything special since we don't need to
15115 know the size of the anonymous object. */
529908cb 15116 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
7c184d33 15117 + attr->constant_value (0)));
c906108c
SS
15118 }
15119 else
15120 {
15121 /* For little endian bits, compute the bit offset to the
dda83cd7
SM
15122 MSB of the anonymous object, subtract off the number of
15123 bits from the MSB of the field to the MSB of the
15124 object, and then subtract off the number of bits of
15125 the field itself. The result is the bit offset of
15126 the LSB of the field. */
c906108c 15127 int anonymous_size;
7c184d33 15128 int bit_offset = attr->constant_value (0);
c906108c 15129
e142c38c 15130 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7c184d33 15131 if (attr != nullptr && attr->form_is_constant ())
c906108c
SS
15132 {
15133 /* The size of the anonymous object containing
15134 the bit field is explicit, so use the
15135 indicated size (in bytes). */
7c184d33 15136 anonymous_size = attr->constant_value (0);
c906108c
SS
15137 }
15138 else
15139 {
15140 /* The size of the anonymous object containing
15141 the bit field must be inferred from the type
15142 attribute of the data member containing the
15143 bit field. */
5d14b6e5 15144 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 15145 }
f41f5e61
PA
15146 SET_FIELD_BITPOS (*fp,
15147 (FIELD_BITPOS (*fp)
15148 + anonymous_size * bits_per_byte
15149 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15150 }
15151 }
da5b30da
AA
15152 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15153 if (attr != NULL)
15154 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 15155 + attr->constant_value (0)));
c906108c
SS
15156
15157 /* Get name of field. */
39cbfefa
DJ
15158 fieldname = dwarf2_name (die, cu);
15159 if (fieldname == NULL)
15160 fieldname = "";
d8151005
DJ
15161
15162 /* The name is already allocated along with this objfile, so we don't
15163 need to duplicate it for the type. */
15164 fp->name = fieldname;
c906108c
SS
15165
15166 /* Change accessibility for artificial fields (e.g. virtual table
dda83cd7 15167 pointer or virtual base class pointer) to private. */
e142c38c 15168 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15169 {
d48cc9dd 15170 FIELD_ARTIFICIAL (*fp) = 1;
c906108c 15171 new_field->accessibility = DW_ACCESS_private;
264fc0e2 15172 fip->non_public_fields = true;
c906108c
SS
15173 }
15174 }
a9a9bd0f 15175 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15176 {
a9a9bd0f
DC
15177 /* C++ static member. */
15178
15179 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15180 is a declaration, but all versions of G++ as of this writing
15181 (so through at least 3.2.1) incorrectly generate
15182 DW_TAG_variable tags. */
6e70227d 15183
ff355380 15184 const char *physname;
c906108c 15185
a9a9bd0f 15186 /* Get name of field. */
39cbfefa
DJ
15187 fieldname = dwarf2_name (die, cu);
15188 if (fieldname == NULL)
c906108c
SS
15189 return;
15190
254e6b9e 15191 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15192 if (attr
15193 /* Only create a symbol if this is an external value.
15194 new_symbol checks this and puts the value in the global symbol
15195 table, which we want. If it is not external, new_symbol
15196 will try to put the value in cu->list_in_scope which is wrong. */
15197 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15198 {
15199 /* A static const member, not much different than an enum as far as
15200 we're concerned, except that we can support more types. */
15201 new_symbol (die, NULL, cu);
15202 }
15203
2df3850c 15204 /* Get physical name. */
ff355380 15205 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15206
d8151005
DJ
15207 /* The name is already allocated along with this objfile, so we don't
15208 need to duplicate it for the type. */
15209 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 15210 fp->set_type (die_type (die, cu));
d8151005 15211 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15212 }
15213 else if (die->tag == DW_TAG_inheritance)
15214 {
74ac6d43 15215 /* C++ base class field. */
7d79de9a 15216 handle_data_member_location (die, cu, fp);
c906108c 15217 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
15218 fp->set_type (die_type (die, cu));
15219 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 15220 }
2ddeaf8a
TT
15221 else
15222 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15223}
15224
883fd55a
KS
15225/* Can the type given by DIE define another type? */
15226
15227static bool
15228type_can_define_types (const struct die_info *die)
15229{
15230 switch (die->tag)
15231 {
15232 case DW_TAG_typedef:
15233 case DW_TAG_class_type:
15234 case DW_TAG_structure_type:
15235 case DW_TAG_union_type:
15236 case DW_TAG_enumeration_type:
15237 return true;
15238
15239 default:
15240 return false;
15241 }
15242}
15243
15244/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15245
15246static void
883fd55a
KS
15247dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15248 struct dwarf2_cu *cu)
6e70227d 15249{
be2daae6
TT
15250 struct decl_field fp;
15251 memset (&fp, 0, sizeof (fp));
98751a41 15252
883fd55a 15253 gdb_assert (type_can_define_types (die));
98751a41 15254
883fd55a 15255 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15256 fp.name = dwarf2_name (die, cu);
15257 fp.type = read_type_die (die, cu);
98751a41 15258
c191a687 15259 /* Save accessibility. */
bf23a268 15260 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
c191a687
KS
15261 switch (accessibility)
15262 {
15263 case DW_ACCESS_public:
15264 /* The assumed value if neither private nor protected. */
15265 break;
15266 case DW_ACCESS_private:
be2daae6 15267 fp.is_private = 1;
c191a687
KS
15268 break;
15269 case DW_ACCESS_protected:
be2daae6 15270 fp.is_protected = 1;
c191a687 15271 break;
c191a687
KS
15272 }
15273
883fd55a 15274 if (die->tag == DW_TAG_typedef)
be2daae6 15275 fip->typedef_field_list.push_back (fp);
883fd55a 15276 else
be2daae6 15277 fip->nested_types_list.push_back (fp);
98751a41
JK
15278}
15279
9c6a1327
TT
15280/* A convenience typedef that's used when finding the discriminant
15281 field for a variant part. */
1b95cdb7
SM
15282typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15283 offset_map_type;
9c6a1327
TT
15284
15285/* Compute the discriminant range for a given variant. OBSTACK is
15286 where the results will be stored. VARIANT is the variant to
15287 process. IS_UNSIGNED indicates whether the discriminant is signed
15288 or unsigned. */
15289
15290static const gdb::array_view<discriminant_range>
15291convert_variant_range (struct obstack *obstack, const variant_field &variant,
15292 bool is_unsigned)
15293{
15294 std::vector<discriminant_range> ranges;
15295
15296 if (variant.default_branch)
15297 return {};
15298
15299 if (variant.discr_list_data == nullptr)
15300 {
15301 discriminant_range r
15302 = {variant.discriminant_value, variant.discriminant_value};
15303 ranges.push_back (r);
15304 }
15305 else
15306 {
15307 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15308 variant.discr_list_data->size);
15309 while (!data.empty ())
15310 {
15311 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15312 {
15313 complaint (_("invalid discriminant marker: %d"), data[0]);
15314 break;
15315 }
15316 bool is_range = data[0] == DW_DSC_range;
15317 data = data.slice (1);
15318
15319 ULONGEST low, high;
15320 unsigned int bytes_read;
15321
15322 if (data.empty ())
15323 {
15324 complaint (_("DW_AT_discr_list missing low value"));
15325 break;
15326 }
15327 if (is_unsigned)
15328 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15329 else
15330 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15331 &bytes_read);
15332 data = data.slice (bytes_read);
15333
15334 if (is_range)
15335 {
15336 if (data.empty ())
15337 {
15338 complaint (_("DW_AT_discr_list missing high value"));
15339 break;
15340 }
15341 if (is_unsigned)
15342 high = read_unsigned_leb128 (nullptr, data.data (),
15343 &bytes_read);
15344 else
15345 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15346 &bytes_read);
15347 data = data.slice (bytes_read);
15348 }
15349 else
15350 high = low;
15351
15352 ranges.push_back ({ low, high });
15353 }
15354 }
15355
15356 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15357 ranges.size ());
15358 std::copy (ranges.begin (), ranges.end (), result);
15359 return gdb::array_view<discriminant_range> (result, ranges.size ());
15360}
15361
15362static const gdb::array_view<variant_part> create_variant_parts
15363 (struct obstack *obstack,
15364 const offset_map_type &offset_map,
15365 struct field_info *fi,
15366 const std::vector<variant_part_builder> &variant_parts);
15367
15368/* Fill in a "struct variant" for a given variant field. RESULT is
15369 the variant to fill in. OBSTACK is where any needed allocations
15370 will be done. OFFSET_MAP holds the mapping from section offsets to
15371 fields for the type. FI describes the fields of the type we're
15372 processing. FIELD is the variant field we're converting. */
15373
15374static void
15375create_one_variant (variant &result, struct obstack *obstack,
15376 const offset_map_type &offset_map,
15377 struct field_info *fi, const variant_field &field)
15378{
15379 result.discriminants = convert_variant_range (obstack, field, false);
15380 result.first_field = field.first_field + fi->baseclasses.size ();
15381 result.last_field = field.last_field + fi->baseclasses.size ();
15382 result.parts = create_variant_parts (obstack, offset_map, fi,
15383 field.variant_parts);
15384}
15385
15386/* Fill in a "struct variant_part" for a given variant part. RESULT
15387 is the variant part to fill in. OBSTACK is where any needed
15388 allocations will be done. OFFSET_MAP holds the mapping from
15389 section offsets to fields for the type. FI describes the fields of
15390 the type we're processing. BUILDER is the variant part to be
15391 converted. */
15392
15393static void
15394create_one_variant_part (variant_part &result,
15395 struct obstack *obstack,
15396 const offset_map_type &offset_map,
15397 struct field_info *fi,
15398 const variant_part_builder &builder)
15399{
15400 auto iter = offset_map.find (builder.discriminant_offset);
15401 if (iter == offset_map.end ())
15402 {
15403 result.discriminant_index = -1;
15404 /* Doesn't matter. */
15405 result.is_unsigned = false;
15406 }
15407 else
15408 {
15409 result.discriminant_index = iter->second;
15410 result.is_unsigned
c6d940a9 15411 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
9c6a1327
TT
15412 }
15413
15414 size_t n = builder.variants.size ();
15415 variant *output = new (obstack) variant[n];
15416 for (size_t i = 0; i < n; ++i)
15417 create_one_variant (output[i], obstack, offset_map, fi,
15418 builder.variants[i]);
15419
15420 result.variants = gdb::array_view<variant> (output, n);
15421}
15422
15423/* Create a vector of variant parts that can be attached to a type.
15424 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15425 holds the mapping from section offsets to fields for the type. FI
15426 describes the fields of the type we're processing. VARIANT_PARTS
15427 is the vector to convert. */
15428
15429static const gdb::array_view<variant_part>
15430create_variant_parts (struct obstack *obstack,
15431 const offset_map_type &offset_map,
15432 struct field_info *fi,
15433 const std::vector<variant_part_builder> &variant_parts)
15434{
15435 if (variant_parts.empty ())
15436 return {};
15437
15438 size_t n = variant_parts.size ();
15439 variant_part *result = new (obstack) variant_part[n];
15440 for (size_t i = 0; i < n; ++i)
15441 create_one_variant_part (result[i], obstack, offset_map, fi,
15442 variant_parts[i]);
15443
15444 return gdb::array_view<variant_part> (result, n);
15445}
15446
15447/* Compute the variant part vector for FIP, attaching it to TYPE when
15448 done. */
15449
15450static void
15451add_variant_property (struct field_info *fip, struct type *type,
15452 struct dwarf2_cu *cu)
15453{
15454 /* Map section offsets of fields to their field index. Note the
15455 field index here does not take the number of baseclasses into
15456 account. */
15457 offset_map_type offset_map;
15458 for (int i = 0; i < fip->fields.size (); ++i)
15459 offset_map[fip->fields[i].offset] = i;
15460
5e22e966 15461 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
15462 gdb::array_view<variant_part> parts
15463 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15464 fip->variant_parts);
15465
15466 struct dynamic_prop prop;
8c2e4e06
SM
15467 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15468 obstack_copy (&objfile->objfile_obstack, &parts,
15469 sizeof (parts)));
9c6a1327 15470
5c54719c 15471 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
15472}
15473
c906108c
SS
15474/* Create the vector of fields, and attach it to the type. */
15475
15476static void
fba45db2 15477dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15478 struct dwarf2_cu *cu)
c906108c 15479{
317f7127 15480 int nfields = fip->nfields ();
c906108c
SS
15481
15482 /* Record the field count, allocate space for the array of fields,
15483 and create blank accessibility bitfields if necessary. */
5e33d5f4 15484 type->set_num_fields (nfields);
3cabb6b0
SM
15485 type->set_fields
15486 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 15487
b4ba55a1 15488 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15489 {
15490 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15491
15492 TYPE_FIELD_PRIVATE_BITS (type) =
15493 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15494 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15495
15496 TYPE_FIELD_PROTECTED_BITS (type) =
15497 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15498 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15499
774b6a14
TT
15500 TYPE_FIELD_IGNORE_BITS (type) =
15501 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15502 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15503 }
15504
15505 /* If the type has baseclasses, allocate and clear a bit vector for
15506 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15507 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15508 {
be2daae6 15509 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15510 unsigned char *pointer;
c906108c
SS
15511
15512 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15513 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15514 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15515 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15516 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15517 }
15518
9c6a1327
TT
15519 if (!fip->variant_parts.empty ())
15520 add_variant_property (fip, type, cu);
2ddeaf8a 15521
be2daae6
TT
15522 /* Copy the saved-up fields into the field vector. */
15523 for (int i = 0; i < nfields; ++i)
c906108c 15524 {
be2daae6
TT
15525 struct nextfield &field
15526 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15527 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15528
ceacbf6e 15529 type->field (i) = field.field;
be2daae6 15530 switch (field.accessibility)
c906108c 15531 {
c5aa993b 15532 case DW_ACCESS_private:
b4ba55a1 15533 if (cu->language != language_ada)
be2daae6 15534 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15535 break;
c906108c 15536
c5aa993b 15537 case DW_ACCESS_protected:
b4ba55a1 15538 if (cu->language != language_ada)
be2daae6 15539 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15540 break;
c906108c 15541
c5aa993b
JM
15542 case DW_ACCESS_public:
15543 break;
c906108c 15544
c5aa993b
JM
15545 default:
15546 /* Unknown accessibility. Complain and treat it as public. */
15547 {
b98664d3 15548 complaint (_("unsupported accessibility %d"),
be2daae6 15549 field.accessibility);
c5aa993b
JM
15550 }
15551 break;
c906108c 15552 }
be2daae6 15553 if (i < fip->baseclasses.size ())
c906108c 15554 {
be2daae6 15555 switch (field.virtuality)
c906108c 15556 {
c5aa993b
JM
15557 case DW_VIRTUALITY_virtual:
15558 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15559 if (cu->language == language_ada)
a73c6dcd 15560 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15561 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15562 break;
c906108c
SS
15563 }
15564 }
c906108c
SS
15565 }
15566}
15567
7d27a96d
TT
15568/* Return true if this member function is a constructor, false
15569 otherwise. */
15570
15571static int
15572dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15573{
15574 const char *fieldname;
fe978cb0 15575 const char *type_name;
7d27a96d
TT
15576 int len;
15577
15578 if (die->parent == NULL)
15579 return 0;
15580
15581 if (die->parent->tag != DW_TAG_structure_type
15582 && die->parent->tag != DW_TAG_union_type
15583 && die->parent->tag != DW_TAG_class_type)
15584 return 0;
15585
15586 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15587 type_name = dwarf2_name (die->parent, cu);
15588 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15589 return 0;
15590
15591 len = strlen (fieldname);
fe978cb0
PA
15592 return (strncmp (fieldname, type_name, len) == 0
15593 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15594}
15595
c906108c
SS
15596/* Add a member function to the proper fieldlist. */
15597
15598static void
107d2387 15599dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15600 struct type *type, struct dwarf2_cu *cu)
c906108c 15601{
5e22e966 15602 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15603 struct attribute *attr;
c906108c 15604 int i;
be2daae6 15605 struct fnfieldlist *flp = nullptr;
c906108c 15606 struct fn_field *fnp;
15d034d0 15607 const char *fieldname;
f792889a 15608 struct type *this_type;
c906108c 15609
b4ba55a1 15610 if (cu->language == language_ada)
a73c6dcd 15611 error (_("unexpected member function in Ada type"));
b4ba55a1 15612
2df3850c 15613 /* Get name of member function. */
39cbfefa
DJ
15614 fieldname = dwarf2_name (die, cu);
15615 if (fieldname == NULL)
2df3850c 15616 return;
c906108c 15617
c906108c 15618 /* Look up member function name in fieldlist. */
be2daae6 15619 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15620 {
27bfe10e 15621 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15622 {
15623 flp = &fip->fnfieldlists[i];
15624 break;
15625 }
c906108c
SS
15626 }
15627
be2daae6
TT
15628 /* Create a new fnfieldlist if necessary. */
15629 if (flp == nullptr)
c906108c 15630 {
be2daae6
TT
15631 fip->fnfieldlists.emplace_back ();
15632 flp = &fip->fnfieldlists.back ();
c906108c 15633 flp->name = fieldname;
be2daae6 15634 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15635 }
15636
be2daae6
TT
15637 /* Create a new member function field and add it to the vector of
15638 fnfieldlists. */
15639 flp->fnfields.emplace_back ();
15640 fnp = &flp->fnfields.back ();
3da10d80
KS
15641
15642 /* Delay processing of the physname until later. */
9c37b5ae 15643 if (cu->language == language_cplus)
be2daae6
TT
15644 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15645 die, cu);
3da10d80
KS
15646 else
15647 {
1d06ead6 15648 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15649 fnp->physname = physname ? physname : "";
15650 }
15651
c906108c 15652 fnp->type = alloc_type (objfile);
f792889a 15653 this_type = read_type_die (die, cu);
78134374 15654 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15655 {
1f704f76 15656 int nparams = this_type->num_fields ();
c906108c 15657
f792889a 15658 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15659 of the method itself (TYPE_CODE_METHOD). */
15660 smash_to_method_type (fnp->type, type,
f792889a 15661 TYPE_TARGET_TYPE (this_type),
80fc5e77 15662 this_type->fields (),
1f704f76 15663 this_type->num_fields (),
a409645d 15664 this_type->has_varargs ());
c906108c
SS
15665
15666 /* Handle static member functions.
dda83cd7
SM
15667 Dwarf2 has no clean way to discern C++ static and non-static
15668 member functions. G++ helps GDB by marking the first
15669 parameter for non-static member functions (which is the this
15670 pointer) as artificial. We obtain this information from
15671 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15672 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15673 fnp->voffset = VOFFSET_STATIC;
15674 }
15675 else
b98664d3 15676 complaint (_("member function type missing for '%s'"),
3da10d80 15677 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15678
15679 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15680 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15681 fnp->fcontext = die_containing_type (die, cu);
c906108c 15682
3e43a32a
MS
15683 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15684 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15685
15686 /* Get accessibility. */
bf23a268 15687 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
60d5a603 15688 switch (accessibility)
c906108c 15689 {
60d5a603
JK
15690 case DW_ACCESS_private:
15691 fnp->is_private = 1;
15692 break;
15693 case DW_ACCESS_protected:
15694 fnp->is_protected = 1;
15695 break;
c906108c
SS
15696 }
15697
b02dede2 15698 /* Check for artificial methods. */
e142c38c 15699 attr = dwarf2_attr (die, DW_AT_artificial, cu);
c45bc3f8 15700 if (attr && attr->as_boolean ())
b02dede2
DJ
15701 fnp->is_artificial = 1;
15702
e35000a7
TBA
15703 /* Check for defaulted methods. */
15704 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
e8e5c158
TT
15705 if (attr != nullptr)
15706 fnp->defaulted = attr->defaulted ();
e35000a7
TBA
15707
15708 /* Check for deleted methods. */
15709 attr = dwarf2_attr (die, DW_AT_deleted, cu);
c45bc3f8 15710 if (attr != nullptr && attr->as_boolean ())
e35000a7
TBA
15711 fnp->is_deleted = 1;
15712
7d27a96d
TT
15713 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15714
0d564a31 15715 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15716 function. For older versions of GCC, this is an offset in the
15717 appropriate virtual table, as specified by DW_AT_containing_type.
15718 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15719 to the object address. */
15720
e142c38c 15721 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15722 if (attr != nullptr)
8e19ed76 15723 {
9d2246fc 15724 if (attr->form_is_block () && attr->as_block ()->size > 0)
dda83cd7 15725 {
9d2246fc
TT
15726 struct dwarf_block *block = attr->as_block ();
15727
15728 if (block->data[0] == DW_OP_constu)
aec5aa8b
TT
15729 {
15730 /* Old-style GCC. */
9d2246fc 15731 fnp->voffset = decode_locdesc (block, cu) + 2;
aec5aa8b 15732 }
9d2246fc
TT
15733 else if (block->data[0] == DW_OP_deref
15734 || (block->size > 1
15735 && block->data[0] == DW_OP_deref_size
15736 && block->data[1] == cu->header.addr_size))
aec5aa8b 15737 {
9d2246fc 15738 fnp->voffset = decode_locdesc (block, cu);
aec5aa8b
TT
15739 if ((fnp->voffset % cu->header.addr_size) != 0)
15740 dwarf2_complex_location_expr_complaint ();
15741 else
15742 fnp->voffset /= cu->header.addr_size;
15743 fnp->voffset += 2;
15744 }
15745 else
15746 dwarf2_complex_location_expr_complaint ();
15747
15748 if (!fnp->fcontext)
7e993ebf
KS
15749 {
15750 /* If there is no `this' field and no DW_AT_containing_type,
15751 we cannot actually find a base class context for the
15752 vtable! */
1f704f76 15753 if (this_type->num_fields () == 0
7e993ebf
KS
15754 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15755 {
b98664d3 15756 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15757 "function \"%s\" (offset %s)"),
15758 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15759 }
15760 else
15761 {
15762 fnp->fcontext
940da03e 15763 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15764 }
15765 }
aec5aa8b 15766 }
cd6c91b4 15767 else if (attr->form_is_section_offset ())
dda83cd7 15768 {
4d3c2250 15769 dwarf2_complex_location_expr_complaint ();
dda83cd7 15770 }
8e19ed76 15771 else
dda83cd7 15772 {
4d3c2250
KB
15773 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15774 fieldname);
dda83cd7 15775 }
0d564a31 15776 }
d48cc9dd
DJ
15777 else
15778 {
15779 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
23dca5c3 15780 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
d48cc9dd
DJ
15781 {
15782 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15783 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15784 "but the vtable offset is not specified"),
9d8780f0 15785 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15786 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15787 TYPE_CPLUS_DYNAMIC (type) = 1;
15788 }
15789 }
c906108c
SS
15790}
15791
15792/* Create the vector of member function fields, and attach it to the type. */
15793
15794static void
fba45db2 15795dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15796 struct dwarf2_cu *cu)
c906108c 15797{
b4ba55a1 15798 if (cu->language == language_ada)
a73c6dcd 15799 error (_("unexpected member functions in Ada type"));
b4ba55a1 15800
c906108c
SS
15801 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15802 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15803 TYPE_ALLOC (type,
15804 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15805
be2daae6 15806 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15807 {
be2daae6 15808 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15809 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15810
be2daae6
TT
15811 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15812 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15813 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15814 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15815
15816 for (int k = 0; k < nf.fnfields.size (); ++k)
15817 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15818 }
15819
be2daae6 15820 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15821}
15822
1168df01
JB
15823/* Returns non-zero if NAME is the name of a vtable member in CU's
15824 language, zero otherwise. */
15825static int
15826is_vtable_name (const char *name, struct dwarf2_cu *cu)
15827{
15828 static const char vptr[] = "_vptr";
15829
9c37b5ae
TT
15830 /* Look for the C++ form of the vtable. */
15831 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15832 return 1;
15833
15834 return 0;
15835}
15836
c0dd20ea 15837/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15838 functions, with the ABI-specified layout. If TYPE describes
15839 such a structure, smash it into a member function type.
61049d3b
DJ
15840
15841 GCC shouldn't do this; it should just output pointer to member DIEs.
15842 This is GCC PR debug/28767. */
c0dd20ea 15843
0b92b5bb
TT
15844static void
15845quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15846{
09e2d7c7 15847 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15848
15849 /* Check for a structure with no name and two children. */
1f704f76 15850 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15851 return;
c0dd20ea
DJ
15852
15853 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15854 if (TYPE_FIELD_NAME (type, 0) == NULL
15855 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15856 || TYPE_FIELD_NAME (type, 1) == NULL
15857 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15858 return;
c0dd20ea
DJ
15859
15860 /* Find the type of the method. */
940da03e 15861 pfn_type = type->field (0).type ();
c0dd20ea 15862 if (pfn_type == NULL
78134374
SM
15863 || pfn_type->code () != TYPE_CODE_PTR
15864 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15865 return;
c0dd20ea
DJ
15866
15867 /* Look for the "this" argument. */
15868 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15869 if (pfn_type->num_fields () == 0
940da03e
SM
15870 /* || pfn_type->field (0).type () == NULL */
15871 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15872 return;
c0dd20ea 15873
940da03e 15874 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15875 new_type = alloc_type (objfile);
09e2d7c7 15876 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15877 pfn_type->fields (), pfn_type->num_fields (),
a409645d 15878 pfn_type->has_varargs ());
0b92b5bb 15879 smash_to_methodptr_type (type, new_type);
c0dd20ea 15880}
1168df01 15881
e26624c6
TT
15882/* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15883 requires rewriting, then copy it and return the updated copy.
15884 Otherwise return nullptr. */
15885
15886static struct type *
15887rewrite_array_type (struct type *type)
15888{
15889 if (type->code () != TYPE_CODE_ARRAY)
15890 return nullptr;
15891
15892 struct type *index_type = type->index_type ();
15893 range_bounds *current_bounds = index_type->bounds ();
15894
15895 /* Handle multi-dimensional arrays. */
15896 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15897 if (new_target == nullptr)
15898 {
15899 /* Maybe we don't need to rewrite this array. */
15900 if (current_bounds->low.kind () == PROP_CONST
15901 && current_bounds->high.kind () == PROP_CONST)
15902 return nullptr;
15903 }
15904
15905 /* Either the target type was rewritten, or the bounds have to be
15906 updated. Either way we want to copy the type and update
15907 everything. */
15908 struct type *copy = copy_type (type);
15909 int nfields = copy->num_fields ();
15910 field *new_fields
15911 = ((struct field *) TYPE_ZALLOC (copy,
15912 nfields * sizeof (struct field)));
15913 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15914 copy->set_fields (new_fields);
15915 if (new_target != nullptr)
15916 TYPE_TARGET_TYPE (copy) = new_target;
15917
15918 struct type *index_copy = copy_type (index_type);
15919 range_bounds *bounds
15920 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15921 sizeof (range_bounds));
15922 *bounds = *current_bounds;
15923 bounds->low.set_const_val (1);
15924 bounds->high.set_const_val (0);
15925 index_copy->set_bounds (bounds);
15926 copy->set_index_type (index_copy);
15927
15928 return copy;
15929}
15930
57567375
TT
15931/* While some versions of GCC will generate complicated DWARF for an
15932 array (see quirk_ada_thick_pointer), more recent versions were
15933 modified to emit an explicit thick pointer structure. However, in
15934 this case, the array still has DWARF expressions for its ranges,
15935 and these must be ignored. */
15936
15937static void
15938quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15939 struct type *type)
15940{
15941 gdb_assert (cu->language == language_ada);
15942
15943 /* Check for a structure with two children. */
15944 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15945 return;
15946
15947 /* Check for P_ARRAY and P_BOUNDS members. */
15948 if (TYPE_FIELD_NAME (type, 0) == NULL
15949 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15950 || TYPE_FIELD_NAME (type, 1) == NULL
15951 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15952 return;
15953
15954 /* Make sure we're looking at a pointer to an array. */
15955 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15956 return;
57567375 15957
e26624c6
TT
15958 /* The Ada code already knows how to handle these types, so all that
15959 we need to do is turn the bounds into static bounds. However, we
15960 don't want to rewrite existing array or index types in-place,
15961 because those may be referenced in other contexts where this
15962 rewriting is undesirable. */
15963 struct type *new_ary_type
15964 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15965 if (new_ary_type != nullptr)
15966 type->field (0).set_type (lookup_pointer_type (new_ary_type));
57567375
TT
15967}
15968
2b4424c3
TT
15969/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15970 appropriate error checking and issuing complaints if there is a
15971 problem. */
15972
15973static ULONGEST
15974get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15975{
15976 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15977
15978 if (attr == nullptr)
15979 return 0;
15980
cd6c91b4 15981 if (!attr->form_is_constant ())
2b4424c3 15982 {
b98664d3 15983 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15984 " - DIE at %s [in module %s]"),
15985 sect_offset_str (die->sect_off),
5e22e966 15986 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15987 return 0;
15988 }
15989
529908cb
TT
15990 LONGEST val = attr->constant_value (0);
15991 if (val < 0)
2b4424c3 15992 {
529908cb
TT
15993 complaint (_("DW_AT_alignment value must not be negative"
15994 " - DIE at %s [in module %s]"),
15995 sect_offset_str (die->sect_off),
15996 objfile_name (cu->per_objfile->objfile));
15997 return 0;
2b4424c3 15998 }
529908cb 15999 ULONGEST align = val;
2b4424c3
TT
16000
16001 if (align == 0)
16002 {
b98664d3 16003 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
16004 " - DIE at %s [in module %s]"),
16005 sect_offset_str (die->sect_off),
5e22e966 16006 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
16007 return 0;
16008 }
16009 if ((align & (align - 1)) != 0)
16010 {
b98664d3 16011 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
16012 " - DIE at %s [in module %s]"),
16013 sect_offset_str (die->sect_off),
5e22e966 16014 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
16015 return 0;
16016 }
16017
16018 return align;
16019}
16020
16021/* If the DIE has a DW_AT_alignment attribute, use its value to set
16022 the alignment for TYPE. */
16023
16024static void
16025maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
16026 struct type *type)
16027{
16028 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 16029 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
16030 " - DIE at %s [in module %s]"),
16031 sect_offset_str (die->sect_off),
5e22e966 16032 objfile_name (cu->per_objfile->objfile));
2b4424c3 16033}
685b1105 16034
e35000a7
TBA
16035/* Check if the given VALUE is a valid enum dwarf_calling_convention
16036 constant for a type, according to DWARF5 spec, Table 5.5. */
16037
16038static bool
16039is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
16040{
16041 switch (value)
16042 {
16043 case DW_CC_normal:
16044 case DW_CC_pass_by_reference:
16045 case DW_CC_pass_by_value:
16046 return true;
16047
16048 default:
16049 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 16050 "(%s) for a type"), pulongest (value));
e35000a7
TBA
16051 return false;
16052 }
16053}
16054
d0922fcf
TBA
16055/* Check if the given VALUE is a valid enum dwarf_calling_convention
16056 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
16057 also according to GNU-specific values (see include/dwarf2.h). */
16058
16059static bool
16060is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
16061{
16062 switch (value)
16063 {
16064 case DW_CC_normal:
16065 case DW_CC_program:
16066 case DW_CC_nocall:
16067 return true;
16068
16069 case DW_CC_GNU_renesas_sh:
16070 case DW_CC_GNU_borland_fastcall_i386:
16071 case DW_CC_GDB_IBM_OpenCL:
16072 return true;
16073
16074 default:
16075 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 16076 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
16077 return false;
16078 }
16079}
16080
c906108c 16081/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
16082 (definition) to create a type for the structure or union. Fill in
16083 the type's name and general properties; the members will not be
83655187
DE
16084 processed until process_structure_scope. A symbol table entry for
16085 the type will also not be done until process_structure_scope (assuming
16086 the type has a name).
c906108c 16087
c767944b
DJ
16088 NOTE: we need to call these functions regardless of whether or not the
16089 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 16090 structure or union. This gets the type entered into our set of
83655187 16091 user defined types. */
c906108c 16092
f792889a 16093static struct type *
134d01f1 16094read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16095{
5e22e966 16096 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
16097 struct type *type;
16098 struct attribute *attr;
15d034d0 16099 const char *name;
c906108c 16100
348e048f
DE
16101 /* If the definition of this type lives in .debug_types, read that type.
16102 Don't follow DW_AT_specification though, that will take us back up
16103 the chain and we want to go down. */
052c8bb8 16104 attr = die->attr (DW_AT_signature);
435d3d88 16105 if (attr != nullptr)
348e048f 16106 {
ac9ec31b 16107 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16108
ac9ec31b 16109 /* The type's CU may not be the same as CU.
02142a6c 16110 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16111 return set_die_type (die, type, cu);
16112 }
16113
c0dd20ea 16114 type = alloc_type (objfile);
c906108c 16115 INIT_CPLUS_SPECIFIC (type);
93311388 16116
39cbfefa
DJ
16117 name = dwarf2_name (die, cu);
16118 if (name != NULL)
c906108c 16119 {
987504bb 16120 if (cu->language == language_cplus
c44af4eb
TT
16121 || cu->language == language_d
16122 || cu->language == language_rust)
63d06c5c 16123 {
15d034d0 16124 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
16125
16126 /* dwarf2_full_name might have already finished building the DIE's
16127 type. If so, there is no need to continue. */
16128 if (get_die_type (die, cu) != NULL)
16129 return get_die_type (die, cu);
16130
d0e39ea2 16131 type->set_name (full_name);
63d06c5c
DC
16132 }
16133 else
16134 {
d8151005
DJ
16135 /* The name is already allocated along with this objfile, so
16136 we don't need to duplicate it for the type. */
d0e39ea2 16137 type->set_name (name);
63d06c5c 16138 }
c906108c
SS
16139 }
16140
16141 if (die->tag == DW_TAG_structure_type)
16142 {
67607e24 16143 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16144 }
16145 else if (die->tag == DW_TAG_union_type)
16146 {
67607e24 16147 type->set_code (TYPE_CODE_UNION);
c906108c
SS
16148 }
16149 else
16150 {
67607e24 16151 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16152 }
16153
0cc2414c 16154 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
aa70e35c 16155 type->set_is_declared_class (true);
0cc2414c 16156
e35000a7
TBA
16157 /* Store the calling convention in the type if it's available in
16158 the die. Otherwise the calling convention remains set to
16159 the default value DW_CC_normal. */
16160 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16161 if (attr != nullptr
529908cb 16162 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
e35000a7
TBA
16163 {
16164 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16165 TYPE_CPLUS_CALLING_CONVENTION (type)
529908cb 16166 = (enum dwarf_calling_convention) (attr->constant_value (0));
e35000a7
TBA
16167 }
16168
e142c38c 16169 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16170 if (attr != nullptr)
c906108c 16171 {
cd6c91b4 16172 if (attr->form_is_constant ())
dda83cd7 16173 TYPE_LENGTH (type) = attr->constant_value (0);
155bfbd3
JB
16174 else
16175 {
f8e89861 16176 struct dynamic_prop prop;
293e7e51 16177 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 16178 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
dda83cd7 16179 TYPE_LENGTH (type) = 0;
155bfbd3 16180 }
c906108c
SS
16181 }
16182 else
16183 {
16184 TYPE_LENGTH (type) = 0;
16185 }
16186
2b4424c3
TT
16187 maybe_set_alignment (cu, die, type);
16188
5230b05a 16189 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 16190 {
5230b05a
WT
16191 /* ICC<14 does not output the required DW_AT_declaration on
16192 incomplete types, but gives them a size of zero. */
b4b73759 16193 type->set_is_stub (true);
685b1105
JK
16194 }
16195 else
9baccff6 16196 type->set_stub_is_supported (true);
685b1105 16197
dc718098 16198 if (die_is_declaration (die, cu))
b4b73759 16199 type->set_is_stub (true);
a6c727b2
DJ
16200 else if (attr == NULL && die->child == NULL
16201 && producer_is_realview (cu->producer))
16202 /* RealView does not output the required DW_AT_declaration
16203 on incomplete types. */
b4b73759 16204 type->set_is_stub (true);
dc718098 16205
c906108c
SS
16206 /* We need to add the type field to the die immediately so we don't
16207 infinitely recurse when dealing with pointers to the structure
0963b4bd 16208 type within the structure itself. */
1c379e20 16209 set_die_type (die, type, cu);
c906108c 16210
7e314c57
JK
16211 /* set_die_type should be already done. */
16212 set_descriptive_type (type, die, cu);
16213
c767944b
DJ
16214 return type;
16215}
16216
9c6a1327
TT
16217static void handle_struct_member_die
16218 (struct die_info *child_die,
16219 struct type *type,
16220 struct field_info *fi,
16221 std::vector<struct symbol *> *template_args,
16222 struct dwarf2_cu *cu);
16223
16224/* A helper for handle_struct_member_die that handles
16225 DW_TAG_variant_part. */
16226
16227static void
16228handle_variant_part (struct die_info *die, struct type *type,
16229 struct field_info *fi,
16230 std::vector<struct symbol *> *template_args,
16231 struct dwarf2_cu *cu)
16232{
16233 variant_part_builder *new_part;
16234 if (fi->current_variant_part == nullptr)
16235 {
16236 fi->variant_parts.emplace_back ();
16237 new_part = &fi->variant_parts.back ();
16238 }
16239 else if (!fi->current_variant_part->processing_variant)
16240 {
16241 complaint (_("nested DW_TAG_variant_part seen "
16242 "- DIE at %s [in module %s]"),
16243 sect_offset_str (die->sect_off),
5e22e966 16244 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16245 return;
16246 }
16247 else
16248 {
16249 variant_field &current = fi->current_variant_part->variants.back ();
16250 current.variant_parts.emplace_back ();
16251 new_part = &current.variant_parts.back ();
16252 }
16253
16254 /* When we recurse, we want callees to add to this new variant
16255 part. */
16256 scoped_restore save_current_variant_part
16257 = make_scoped_restore (&fi->current_variant_part, new_part);
16258
16259 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16260 if (discr == NULL)
16261 {
16262 /* It's a univariant form, an extension we support. */
16263 }
16264 else if (discr->form_is_ref ())
16265 {
16266 struct dwarf2_cu *target_cu = cu;
16267 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16268
16269 new_part->discriminant_offset = target_die->sect_off;
16270 }
16271 else
16272 {
16273 complaint (_("DW_AT_discr does not have DIE reference form"
16274 " - DIE at %s [in module %s]"),
16275 sect_offset_str (die->sect_off),
5e22e966 16276 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16277 }
16278
16279 for (die_info *child_die = die->child;
16280 child_die != NULL;
16281 child_die = child_die->sibling)
16282 handle_struct_member_die (child_die, type, fi, template_args, cu);
16283}
16284
16285/* A helper for handle_struct_member_die that handles
16286 DW_TAG_variant. */
16287
16288static void
16289handle_variant (struct die_info *die, struct type *type,
16290 struct field_info *fi,
16291 std::vector<struct symbol *> *template_args,
16292 struct dwarf2_cu *cu)
16293{
16294 if (fi->current_variant_part == nullptr)
16295 {
16296 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16297 "- DIE at %s [in module %s]"),
16298 sect_offset_str (die->sect_off),
5e22e966 16299 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16300 return;
16301 }
16302 if (fi->current_variant_part->processing_variant)
16303 {
16304 complaint (_("nested DW_TAG_variant seen "
16305 "- DIE at %s [in module %s]"),
16306 sect_offset_str (die->sect_off),
5e22e966 16307 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16308 return;
16309 }
16310
16311 scoped_restore save_processing_variant
16312 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16313 true);
16314
16315 fi->current_variant_part->variants.emplace_back ();
16316 variant_field &variant = fi->current_variant_part->variants.back ();
16317 variant.first_field = fi->fields.size ();
16318
16319 /* In a variant we want to get the discriminant and also add a
16320 field for our sole member child. */
16321 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
cae21f8e 16322 if (discr == nullptr || !discr->form_is_constant ())
9c6a1327
TT
16323 {
16324 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
9d2246fc 16325 if (discr == nullptr || discr->as_block ()->size == 0)
9c6a1327
TT
16326 variant.default_branch = true;
16327 else
9d2246fc 16328 variant.discr_list_data = discr->as_block ();
9c6a1327
TT
16329 }
16330 else
cae21f8e 16331 variant.discriminant_value = discr->constant_value (0);
9c6a1327
TT
16332
16333 for (die_info *variant_child = die->child;
16334 variant_child != NULL;
16335 variant_child = variant_child->sibling)
16336 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16337
16338 variant.last_field = fi->fields.size ();
16339}
16340
2ddeaf8a
TT
16341/* A helper for process_structure_scope that handles a single member
16342 DIE. */
16343
16344static void
16345handle_struct_member_die (struct die_info *child_die, struct type *type,
16346 struct field_info *fi,
16347 std::vector<struct symbol *> *template_args,
16348 struct dwarf2_cu *cu)
16349{
16350 if (child_die->tag == DW_TAG_member
9c6a1327 16351 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
16352 {
16353 /* NOTE: carlton/2002-11-05: A C++ static data member
16354 should be a DW_TAG_member that is a declaration, but
16355 all versions of G++ as of this writing (so through at
16356 least 3.2.1) incorrectly generate DW_TAG_variable
16357 tags for them instead. */
16358 dwarf2_add_field (fi, child_die, cu);
16359 }
16360 else if (child_die->tag == DW_TAG_subprogram)
16361 {
16362 /* Rust doesn't have member functions in the C++ sense.
16363 However, it does emit ordinary functions as children
16364 of a struct DIE. */
16365 if (cu->language == language_rust)
16366 read_func_scope (child_die, cu);
16367 else
16368 {
16369 /* C++ member function. */
16370 dwarf2_add_member_fn (fi, child_die, type, cu);
16371 }
16372 }
16373 else if (child_die->tag == DW_TAG_inheritance)
16374 {
16375 /* C++ base class field. */
16376 dwarf2_add_field (fi, child_die, cu);
16377 }
16378 else if (type_can_define_types (child_die))
16379 dwarf2_add_type_defn (fi, child_die, cu);
16380 else if (child_die->tag == DW_TAG_template_type_param
16381 || child_die->tag == DW_TAG_template_value_param)
16382 {
16383 struct symbol *arg = new_symbol (child_die, NULL, cu);
16384
16385 if (arg != NULL)
16386 template_args->push_back (arg);
16387 }
9c6a1327
TT
16388 else if (child_die->tag == DW_TAG_variant_part)
16389 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 16390 else if (child_die->tag == DW_TAG_variant)
9c6a1327 16391 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
16392}
16393
c767944b
DJ
16394/* Finish creating a structure or union type, including filling in
16395 its members and creating a symbol for it. */
16396
16397static void
16398process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16399{
5e22e966 16400 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 16401 struct die_info *child_die;
c767944b
DJ
16402 struct type *type;
16403
16404 type = get_die_type (die, cu);
16405 if (type == NULL)
16406 type = read_structure_type (die, cu);
16407
3e1d3d8c 16408 bool has_template_parameters = false;
e142c38c 16409 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16410 {
16411 struct field_info fi;
2f4732b0 16412 std::vector<struct symbol *> template_args;
c906108c 16413
639d11d3 16414 child_die = die->child;
c906108c
SS
16415
16416 while (child_die && child_die->tag)
16417 {
2ddeaf8a 16418 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 16419 child_die = child_die->sibling;
c906108c
SS
16420 }
16421
34eaf542 16422 /* Attach template arguments to type. */
2f4732b0 16423 if (!template_args.empty ())
34eaf542 16424 {
3e1d3d8c 16425 has_template_parameters = true;
34eaf542 16426 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16427 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16428 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16429 = XOBNEWVEC (&objfile->objfile_obstack,
16430 struct symbol *,
16431 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16432 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16433 template_args.data (),
34eaf542
TT
16434 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16435 * sizeof (struct symbol *)));
34eaf542
TT
16436 }
16437
c906108c 16438 /* Attach fields and member functions to the type. */
317f7127 16439 if (fi.nfields () > 0)
e7c27a73 16440 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16441 if (!fi.fnfieldlists.empty ())
c906108c 16442 {
e7c27a73 16443 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16444
c5aa993b 16445 /* Get the type which refers to the base class (possibly this
c906108c 16446 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16447 class from the DW_AT_containing_type attribute. This use of
16448 DW_AT_containing_type is a GNU extension. */
c906108c 16449
e142c38c 16450 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16451 {
e7c27a73 16452 struct type *t = die_containing_type (die, cu);
c906108c 16453
ae6ae975 16454 set_type_vptr_basetype (type, t);
c906108c
SS
16455 if (type == t)
16456 {
c906108c
SS
16457 int i;
16458
16459 /* Our own class provides vtbl ptr. */
1f704f76 16460 for (i = t->num_fields () - 1;
c906108c
SS
16461 i >= TYPE_N_BASECLASSES (t);
16462 --i)
16463 {
0d5cff50 16464 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16465
dda83cd7 16466 if (is_vtable_name (fieldname, cu))
c906108c 16467 {
ae6ae975 16468 set_type_vptr_fieldno (type, i);
c906108c
SS
16469 break;
16470 }
16471 }
16472
16473 /* Complain if virtual function table field not found. */
16474 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16475 complaint (_("virtual function table pointer "
3e43a32a 16476 "not found when defining class '%s'"),
7d93a1e0 16477 type->name () ? type->name () : "");
c906108c
SS
16478 }
16479 else
16480 {
ae6ae975 16481 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16482 }
16483 }
f6235d4c 16484 else if (cu->producer
61012eef 16485 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16486 {
16487 /* The IBM XLC compiler does not provide direct indication
dda83cd7
SM
16488 of the containing type, but the vtable pointer is
16489 always named __vfp. */
f6235d4c
EZ
16490
16491 int i;
16492
1f704f76 16493 for (i = type->num_fields () - 1;
f6235d4c
EZ
16494 i >= TYPE_N_BASECLASSES (type);
16495 --i)
16496 {
16497 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16498 {
ae6ae975
DE
16499 set_type_vptr_fieldno (type, i);
16500 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16501 break;
16502 }
16503 }
16504 }
c906108c 16505 }
98751a41
JK
16506
16507 /* Copy fi.typedef_field_list linked list elements content into the
16508 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16509 if (!fi.typedef_field_list.empty ())
98751a41 16510 {
be2daae6 16511 int count = fi.typedef_field_list.size ();
98751a41 16512
a0d7a4ff 16513 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16514 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16515 = ((struct decl_field *)
be2daae6
TT
16516 TYPE_ALLOC (type,
16517 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16518 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16519
be2daae6
TT
16520 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16521 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16522 }
c767944b 16523
883fd55a
KS
16524 /* Copy fi.nested_types_list linked list elements content into the
16525 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16526 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16527 {
be2daae6 16528 int count = fi.nested_types_list.size ();
883fd55a
KS
16529
16530 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16531 TYPE_NESTED_TYPES_ARRAY (type)
16532 = ((struct decl_field *)
be2daae6
TT
16533 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16534 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16535
be2daae6
TT
16536 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16537 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16538 }
c906108c 16539 }
63d06c5c 16540
bb5ed363 16541 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16542 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16543 cu->rust_unions.push_back (type);
57567375
TT
16544 else if (cu->language == language_ada)
16545 quirk_ada_thick_pointer_struct (die, cu, type);
0b92b5bb 16546
90aeadfc
DC
16547 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16548 snapshots) has been known to create a die giving a declaration
16549 for a class that has, as a child, a die giving a definition for a
16550 nested class. So we have to process our children even if the
16551 current die is a declaration. Normally, of course, a declaration
16552 won't have any children at all. */
134d01f1 16553
ca040673
DE
16554 child_die = die->child;
16555
90aeadfc
DC
16556 while (child_die != NULL && child_die->tag)
16557 {
16558 if (child_die->tag == DW_TAG_member
16559 || child_die->tag == DW_TAG_variable
34eaf542
TT
16560 || child_die->tag == DW_TAG_inheritance
16561 || child_die->tag == DW_TAG_template_value_param
16562 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16563 {
90aeadfc 16564 /* Do nothing. */
134d01f1 16565 }
90aeadfc
DC
16566 else
16567 process_die (child_die, cu);
134d01f1 16568
436c571c 16569 child_die = child_die->sibling;
134d01f1
DJ
16570 }
16571
fa4028e9
JB
16572 /* Do not consider external references. According to the DWARF standard,
16573 these DIEs are identified by the fact that they have no byte_size
16574 attribute, and a declaration attribute. */
16575 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
16576 || !die_is_declaration (die, cu)
16577 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
16578 {
16579 struct symbol *sym = new_symbol (die, type, cu);
16580
16581 if (has_template_parameters)
16582 {
a776957c
TT
16583 struct symtab *symtab;
16584 if (sym != nullptr)
16585 symtab = symbol_symtab (sym);
16586 else if (cu->line_header != nullptr)
16587 {
16588 /* Any related symtab will do. */
16589 symtab
7ba99d21 16590 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16591 }
16592 else
16593 {
16594 symtab = nullptr;
16595 complaint (_("could not find suitable "
16596 "symtab for template parameter"
16597 " - DIE at %s [in module %s]"),
16598 sect_offset_str (die->sect_off),
16599 objfile_name (objfile));
16600 }
16601
16602 if (symtab != nullptr)
16603 {
16604 /* Make sure that the symtab is set on the new symbols.
16605 Even though they don't appear in this symtab directly,
16606 other parts of gdb assume that symbols do, and this is
16607 reasonably true. */
16608 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16609 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16610 }
3e1d3d8c
TT
16611 }
16612 }
134d01f1
DJ
16613}
16614
ed6acedd
TT
16615/* Assuming DIE is an enumeration type, and TYPE is its associated
16616 type, update TYPE using some information only available in DIE's
16617 children. In particular, the fields are computed. */
55426c9d
JB
16618
16619static void
16620update_enumeration_type_from_children (struct die_info *die,
16621 struct type *type,
16622 struct dwarf2_cu *cu)
16623{
60f7655a 16624 struct die_info *child_die;
55426c9d
JB
16625 int unsigned_enum = 1;
16626 int flag_enum = 1;
55426c9d 16627
8268c778 16628 auto_obstack obstack;
ed6acedd 16629 std::vector<struct field> fields;
55426c9d 16630
60f7655a
DE
16631 for (child_die = die->child;
16632 child_die != NULL && child_die->tag;
436c571c 16633 child_die = child_die->sibling)
55426c9d
JB
16634 {
16635 struct attribute *attr;
16636 LONGEST value;
16637 const gdb_byte *bytes;
16638 struct dwarf2_locexpr_baton *baton;
16639 const char *name;
60f7655a 16640
55426c9d
JB
16641 if (child_die->tag != DW_TAG_enumerator)
16642 continue;
16643
16644 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16645 if (attr == NULL)
16646 continue;
16647
16648 name = dwarf2_name (child_die, cu);
16649 if (name == NULL)
16650 name = "<anonymous enumerator>";
16651
16652 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16653 &value, &bytes, &baton);
16654 if (value < 0)
16655 {
16656 unsigned_enum = 0;
16657 flag_enum = 0;
16658 }
55426c9d 16659 else
edd45eb0
SM
16660 {
16661 if (count_one_bits_ll (value) >= 2)
16662 flag_enum = 0;
edd45eb0 16663 }
55426c9d 16664
ed6acedd
TT
16665 fields.emplace_back ();
16666 struct field &field = fields.back ();
16667 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16668 SET_FIELD_ENUMVAL (field, value);
16669 }
16670
16671 if (!fields.empty ())
16672 {
5e33d5f4 16673 type->set_num_fields (fields.size ());
3cabb6b0
SM
16674 type->set_fields
16675 ((struct field *)
16676 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16677 memcpy (type->fields (), fields.data (),
ed6acedd 16678 sizeof (struct field) * fields.size ());
55426c9d
JB
16679 }
16680
16681 if (unsigned_enum)
653223d3
SM
16682 type->set_is_unsigned (true);
16683
55426c9d 16684 if (flag_enum)
9902b327 16685 type->set_is_flag_enum (true);
55426c9d
JB
16686}
16687
134d01f1
DJ
16688/* Given a DW_AT_enumeration_type die, set its type. We do not
16689 complete the type's fields yet, or create any symbols. */
c906108c 16690
f792889a 16691static struct type *
134d01f1 16692read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16693{
5e22e966 16694 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16695 struct type *type;
c906108c 16696 struct attribute *attr;
0114d602 16697 const char *name;
134d01f1 16698
348e048f
DE
16699 /* If the definition of this type lives in .debug_types, read that type.
16700 Don't follow DW_AT_specification though, that will take us back up
16701 the chain and we want to go down. */
052c8bb8 16702 attr = die->attr (DW_AT_signature);
435d3d88 16703 if (attr != nullptr)
348e048f 16704 {
ac9ec31b 16705 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16706
ac9ec31b 16707 /* The type's CU may not be the same as CU.
02142a6c 16708 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16709 return set_die_type (die, type, cu);
16710 }
16711
c906108c
SS
16712 type = alloc_type (objfile);
16713
67607e24 16714 type->set_code (TYPE_CODE_ENUM);
94af9270 16715 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16716 if (name != NULL)
d0e39ea2 16717 type->set_name (name);
c906108c 16718
0626fc76
TT
16719 attr = dwarf2_attr (die, DW_AT_type, cu);
16720 if (attr != NULL)
16721 {
16722 struct type *underlying_type = die_type (die, cu);
16723
16724 TYPE_TARGET_TYPE (type) = underlying_type;
16725 }
16726
e142c38c 16727 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16728 if (attr != nullptr)
c906108c 16729 {
529908cb 16730 TYPE_LENGTH (type) = attr->constant_value (0);
c906108c
SS
16731 }
16732 else
16733 {
16734 TYPE_LENGTH (type) = 0;
16735 }
16736
2b4424c3
TT
16737 maybe_set_alignment (cu, die, type);
16738
137033e9
JB
16739 /* The enumeration DIE can be incomplete. In Ada, any type can be
16740 declared as private in the package spec, and then defined only
16741 inside the package body. Such types are known as Taft Amendment
16742 Types. When another package uses such a type, an incomplete DIE
16743 may be generated by the compiler. */
02eb380e 16744 if (die_is_declaration (die, cu))
b4b73759 16745 type->set_is_stub (true);
02eb380e 16746
0626fc76
TT
16747 /* If this type has an underlying type that is not a stub, then we
16748 may use its attributes. We always use the "unsigned" attribute
16749 in this situation, because ordinarily we guess whether the type
16750 is unsigned -- but the guess can be wrong and the underlying type
16751 can tell us the reality. However, we defer to a local size
16752 attribute if one exists, because this lets the compiler override
16753 the underlying type if needed. */
e46d3488 16754 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
0626fc76 16755 {
9e7c9a03
HD
16756 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16757 underlying_type = check_typedef (underlying_type);
653223d3
SM
16758
16759 type->set_is_unsigned (underlying_type->is_unsigned ());
16760
0626fc76 16761 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16762 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
653223d3 16763
2b4424c3 16764 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16765 && TYPE_RAW_ALIGN (underlying_type) != 0)
16766 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16767 }
16768
aa70e35c 16769 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
3d567982 16770
ed6acedd
TT
16771 set_die_type (die, type, cu);
16772
16773 /* Finish the creation of this type by using the enum's children.
16774 Note that, as usual, this must come after set_die_type to avoid
16775 infinite recursion when trying to compute the names of the
16776 enumerators. */
16777 update_enumeration_type_from_children (die, type, cu);
16778
16779 return type;
134d01f1
DJ
16780}
16781
16782/* Given a pointer to a die which begins an enumeration, process all
16783 the dies that define the members of the enumeration, and create the
16784 symbol for the enumeration type.
16785
16786 NOTE: We reverse the order of the element list. */
16787
16788static void
16789process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16790{
f792889a 16791 struct type *this_type;
134d01f1 16792
f792889a
DJ
16793 this_type = get_die_type (die, cu);
16794 if (this_type == NULL)
16795 this_type = read_enumeration_type (die, cu);
9dc481d3 16796
639d11d3 16797 if (die->child != NULL)
c906108c 16798 {
9dc481d3 16799 struct die_info *child_die;
15d034d0 16800 const char *name;
9dc481d3 16801
639d11d3 16802 child_die = die->child;
c906108c
SS
16803 while (child_die && child_die->tag)
16804 {
16805 if (child_die->tag != DW_TAG_enumerator)
16806 {
e7c27a73 16807 process_die (child_die, cu);
c906108c
SS
16808 }
16809 else
16810 {
39cbfefa
DJ
16811 name = dwarf2_name (child_die, cu);
16812 if (name)
ed6acedd 16813 new_symbol (child_die, this_type, cu);
c906108c
SS
16814 }
16815
436c571c 16816 child_die = child_die->sibling;
c906108c 16817 }
c906108c 16818 }
134d01f1 16819
6c83ed52
TT
16820 /* If we are reading an enum from a .debug_types unit, and the enum
16821 is a declaration, and the enum is not the signatured type in the
16822 unit, then we do not want to add a symbol for it. Adding a
16823 symbol would in some cases obscure the true definition of the
16824 enum, giving users an incomplete type when the definition is
16825 actually available. Note that we do not want to do this for all
16826 enums which are just declarations, because C++0x allows forward
16827 enum declarations. */
3019eac3 16828 if (cu->per_cu->is_debug_types
6c83ed52
TT
16829 && die_is_declaration (die, cu))
16830 {
52dc124a 16831 struct signatured_type *sig_type;
6c83ed52 16832
c0f78cd4 16833 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16834 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16835 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16836 return;
16837 }
16838
f792889a 16839 new_symbol (die, this_type, cu);
c906108c
SS
16840}
16841
57567375
TT
16842/* Helper function for quirk_ada_thick_pointer that examines a bounds
16843 expression for an index type and finds the corresponding field
16844 offset in the hidden "P_BOUNDS" structure. Returns true on success
16845 and updates *FIELD, false if it fails to recognize an
16846 expression. */
16847
16848static bool
16849recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16850 int *bounds_offset, struct field *field,
16851 struct dwarf2_cu *cu)
16852{
16853 struct attribute *attr = dwarf2_attr (die, name, cu);
16854 if (attr == nullptr || !attr->form_is_block ())
16855 return false;
16856
16857 const struct dwarf_block *block = attr->as_block ();
16858 const gdb_byte *start = block->data;
16859 const gdb_byte *end = block->data + block->size;
16860
16861 /* The expression to recognize generally looks like:
16862
16863 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16864 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16865
16866 However, the second "plus_uconst" may be missing:
16867
16868 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16869 DW_OP_deref_size: 4)
16870
16871 This happens when the field is at the start of the structure.
16872
16873 Also, the final deref may not be sized:
16874
16875 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16876 DW_OP_deref)
16877
16878 This happens when the size of the index type happens to be the
16879 same as the architecture's word size. This can occur with or
16880 without the second plus_uconst. */
16881
16882 if (end - start < 2)
16883 return false;
16884 if (*start++ != DW_OP_push_object_address)
16885 return false;
16886 if (*start++ != DW_OP_plus_uconst)
16887 return false;
16888
16889 uint64_t this_bound_off;
16890 start = gdb_read_uleb128 (start, end, &this_bound_off);
16891 if (start == nullptr || (int) this_bound_off != this_bound_off)
16892 return false;
16893 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16894 is consistent among all bounds. */
16895 if (*bounds_offset == -1)
16896 *bounds_offset = this_bound_off;
16897 else if (*bounds_offset != this_bound_off)
16898 return false;
16899
16900 if (start == end || *start++ != DW_OP_deref)
16901 return false;
16902
16903 int offset = 0;
16904 if (start ==end)
16905 return false;
16906 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16907 {
16908 /* This means an offset of 0. */
16909 }
16910 else if (*start++ != DW_OP_plus_uconst)
16911 return false;
16912 else
16913 {
16914 /* The size is the parameter to DW_OP_plus_uconst. */
16915 uint64_t val;
16916 start = gdb_read_uleb128 (start, end, &val);
16917 if (start == nullptr)
16918 return false;
16919 if ((int) val != val)
16920 return false;
16921 offset = val;
16922 }
16923
16924 if (start == end)
16925 return false;
16926
16927 uint64_t size;
16928 if (*start == DW_OP_deref_size)
16929 {
16930 start = gdb_read_uleb128 (start + 1, end, &size);
16931 if (start == nullptr)
16932 return false;
16933 }
16934 else if (*start == DW_OP_deref)
16935 {
16936 size = cu->header.addr_size;
16937 ++start;
16938 }
16939 else
16940 return false;
16941
16942 SET_FIELD_BITPOS (*field, 8 * offset);
16943 if (size != TYPE_LENGTH (field->type ()))
16944 FIELD_BITSIZE (*field) = 8 * size;
16945
16946 return true;
16947}
16948
16949/* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16950 some kinds of Ada arrays:
16951
16952 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16953 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16954 <11e0> DW_AT_data_location: 2 byte block: 97 6
16955 (DW_OP_push_object_address; DW_OP_deref)
16956 <11e3> DW_AT_type : <0x1173>
16957 <11e7> DW_AT_sibling : <0x1201>
16958 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16959 <11ec> DW_AT_type : <0x1206>
16960 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16961 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16962 DW_OP_deref_size: 4)
16963 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16964 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16965 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16966
16967 This actually represents a "thick pointer", which is a structure
16968 with two elements: one that is a pointer to the array data, and one
16969 that is a pointer to another structure; this second structure holds
16970 the array bounds.
16971
16972 This returns a new type on success, or nullptr if this didn't
16973 recognize the type. */
16974
16975static struct type *
16976quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16977 struct type *type)
16978{
16979 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16980 /* So far we've only seen this with block form. */
16981 if (attr == nullptr || !attr->form_is_block ())
16982 return nullptr;
16983
16984 /* Note that this will fail if the structure layout is changed by
16985 the compiler. However, we have no good way to recognize some
16986 other layout, because we don't know what expression the compiler
16987 might choose to emit should this happen. */
16988 struct dwarf_block *blk = attr->as_block ();
16989 if (blk->size != 2
16990 || blk->data[0] != DW_OP_push_object_address
16991 || blk->data[1] != DW_OP_deref)
16992 return nullptr;
16993
16994 int bounds_offset = -1;
16995 int max_align = -1;
16996 std::vector<struct field> range_fields;
16997 for (struct die_info *child_die = die->child;
16998 child_die;
16999 child_die = child_die->sibling)
17000 {
17001 if (child_die->tag == DW_TAG_subrange_type)
17002 {
17003 struct type *underlying = read_subrange_index_type (child_die, cu);
17004
17005 int this_align = type_align (underlying);
17006 if (this_align > max_align)
17007 max_align = this_align;
17008
17009 range_fields.emplace_back ();
17010 range_fields.emplace_back ();
17011
17012 struct field &lower = range_fields[range_fields.size () - 2];
17013 struct field &upper = range_fields[range_fields.size () - 1];
17014
17015 lower.set_type (underlying);
17016 FIELD_ARTIFICIAL (lower) = 1;
17017
17018 upper.set_type (underlying);
17019 FIELD_ARTIFICIAL (upper) = 1;
17020
17021 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
17022 &bounds_offset, &lower, cu)
17023 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
17024 &bounds_offset, &upper, cu))
17025 return nullptr;
17026 }
17027 }
17028
17029 /* This shouldn't really happen, but double-check that we found
17030 where the bounds are stored. */
17031 if (bounds_offset == -1)
17032 return nullptr;
17033
17034 struct objfile *objfile = cu->per_objfile->objfile;
17035 for (int i = 0; i < range_fields.size (); i += 2)
17036 {
17037 char name[20];
17038
17039 /* Set the name of each field in the bounds. */
17040 xsnprintf (name, sizeof (name), "LB%d", i / 2);
17041 FIELD_NAME (range_fields[i]) = objfile->intern (name);
17042 xsnprintf (name, sizeof (name), "UB%d", i / 2);
17043 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
17044 }
17045
17046 struct type *bounds = alloc_type (objfile);
17047 bounds->set_code (TYPE_CODE_STRUCT);
17048
17049 bounds->set_num_fields (range_fields.size ());
17050 bounds->set_fields
17051 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
17052 * sizeof (struct field))));
17053 memcpy (bounds->fields (), range_fields.data (),
17054 bounds->num_fields () * sizeof (struct field));
17055
17056 int last_fieldno = range_fields.size () - 1;
17057 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
17058 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
17059 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
17060
17061 /* Rewrite the existing array type in place. Specifically, we
17062 remove any dynamic properties we might have read, and we replace
17063 the index types. */
17064 struct type *iter = type;
17065 for (int i = 0; i < range_fields.size (); i += 2)
17066 {
17067 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
17068 iter->main_type->dyn_prop_list = nullptr;
17069 iter->set_index_type
17070 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
17071 iter = TYPE_TARGET_TYPE (iter);
17072 }
17073
17074 struct type *result = alloc_type (objfile);
17075 result->set_code (TYPE_CODE_STRUCT);
17076
17077 result->set_num_fields (2);
17078 result->set_fields
17079 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
17080 * sizeof (struct field))));
17081
17082 /* The names are chosen to coincide with what the compiler does with
17083 -fgnat-encodings=all, which the Ada code in gdb already
17084 understands. */
17085 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
17086 result->field (0).set_type (lookup_pointer_type (type));
17087
17088 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
17089 result->field (1).set_type (lookup_pointer_type (bounds));
17090 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
17091
17092 result->set_name (type->name ());
17093 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17094 + TYPE_LENGTH (result->field (1).type ()));
17095
17096 return result;
17097}
17098
c906108c
SS
17099/* Extract all information from a DW_TAG_array_type DIE and put it in
17100 the DIE's type field. For now, this only handles one dimensional
17101 arrays. */
17102
f792889a 17103static struct type *
e7c27a73 17104read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17105{
5e22e966 17106 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 17107 struct die_info *child_die;
7e314c57 17108 struct type *type;
c906108c 17109 struct type *element_type, *range_type, *index_type;
c906108c 17110 struct attribute *attr;
15d034d0 17111 const char *name;
a405673c 17112 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 17113 unsigned int bit_stride = 0;
c906108c 17114
e7c27a73 17115 element_type = die_type (die, cu);
c906108c 17116
7e314c57
JK
17117 /* The die_type call above may have already set the type for this DIE. */
17118 type = get_die_type (die, cu);
17119 if (type)
17120 return type;
17121
dc53a7ad
JB
17122 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17123 if (attr != NULL)
a405673c
JB
17124 {
17125 int stride_ok;
293e7e51 17126 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
17127
17128 byte_stride_prop
17129 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
17130 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17131 prop_type);
a405673c
JB
17132 if (!stride_ok)
17133 {
b98664d3 17134 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
17135 " - DIE at %s [in module %s]"),
17136 sect_offset_str (die->sect_off),
5e22e966 17137 objfile_name (cu->per_objfile->objfile));
a405673c
JB
17138 /* Ignore this attribute. We will likely not be able to print
17139 arrays of this type correctly, but there is little we can do
17140 to help if we cannot read the attribute's value. */
17141 byte_stride_prop = NULL;
17142 }
17143 }
dc53a7ad
JB
17144
17145 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17146 if (attr != NULL)
529908cb 17147 bit_stride = attr->constant_value (0);
dc53a7ad 17148
c906108c
SS
17149 /* Irix 6.2 native cc creates array types without children for
17150 arrays with unspecified length. */
639d11d3 17151 if (die->child == NULL)
c906108c 17152 {
46bf5051 17153 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17154 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 17155 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 17156 byte_stride_prop, bit_stride);
f792889a 17157 return set_die_type (die, type, cu);
c906108c
SS
17158 }
17159
791afaa2 17160 std::vector<struct type *> range_types;
639d11d3 17161 child_die = die->child;
c906108c
SS
17162 while (child_die && child_die->tag)
17163 {
17164 if (child_die->tag == DW_TAG_subrange_type)
17165 {
f792889a 17166 struct type *child_type = read_type_die (child_die, cu);
9a619af0 17167
dda83cd7
SM
17168 if (child_type != NULL)
17169 {
0963b4bd 17170 /* The range type was succesfully read. Save it for the
dda83cd7 17171 array type creation. */
791afaa2 17172 range_types.push_back (child_type);
dda83cd7 17173 }
c906108c 17174 }
436c571c 17175 child_die = child_die->sibling;
c906108c
SS
17176 }
17177
cf2b2075
TV
17178 if (range_types.empty ())
17179 {
17180 complaint (_("unable to find array range - DIE at %s [in module %s]"),
17181 sect_offset_str (die->sect_off),
17182 objfile_name (cu->per_objfile->objfile));
17183 return NULL;
17184 }
17185
c906108c
SS
17186 /* Dwarf2 dimensions are output from left to right, create the
17187 necessary array types in backwards order. */
7ca2d3a3 17188
c906108c 17189 type = element_type;
7ca2d3a3
DL
17190
17191 if (read_array_order (die, cu) == DW_ORD_col_major)
17192 {
17193 int i = 0;
9a619af0 17194
791afaa2 17195 while (i < range_types.size ())
10f6a3ad
TT
17196 {
17197 type = create_array_type_with_stride (NULL, type, range_types[i++],
17198 byte_stride_prop, bit_stride);
17199 bit_stride = 0;
17200 byte_stride_prop = nullptr;
17201 }
7ca2d3a3
DL
17202 }
17203 else
17204 {
791afaa2 17205 size_t ndim = range_types.size ();
7ca2d3a3 17206 while (ndim-- > 0)
10f6a3ad
TT
17207 {
17208 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17209 byte_stride_prop, bit_stride);
17210 bit_stride = 0;
17211 byte_stride_prop = nullptr;
17212 }
7ca2d3a3 17213 }
c906108c 17214
cf2b2075
TV
17215 gdb_assert (type != element_type);
17216
f5f8a009
EZ
17217 /* Understand Dwarf2 support for vector types (like they occur on
17218 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17219 array type. This is not part of the Dwarf2/3 standard yet, but a
17220 custom vendor extension. The main difference between a regular
17221 array and the vector variant is that vectors are passed by value
17222 to functions. */
e142c38c 17223 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 17224 if (attr != nullptr)
ea37ba09 17225 make_vector_type (type);
f5f8a009 17226
dbc98a8b
KW
17227 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17228 implementation may choose to implement triple vectors using this
17229 attribute. */
17230 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb 17231 if (attr != nullptr && attr->form_is_unsigned ())
dbc98a8b 17232 {
529908cb
TT
17233 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17234 TYPE_LENGTH (type) = attr->as_unsigned ();
dbc98a8b 17235 else
b98664d3 17236 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 17237 "than the total size of elements"));
dbc98a8b
KW
17238 }
17239
39cbfefa
DJ
17240 name = dwarf2_name (die, cu);
17241 if (name)
d0e39ea2 17242 type->set_name (name);
6e70227d 17243
2b4424c3
TT
17244 maybe_set_alignment (cu, die, type);
17245
57567375
TT
17246 struct type *replacement_type = nullptr;
17247 if (cu->language == language_ada)
17248 {
17249 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17250 if (replacement_type != nullptr)
17251 type = replacement_type;
17252 }
17253
0963b4bd 17254 /* Install the type in the die. */
57567375 17255 set_die_type (die, type, cu, replacement_type != nullptr);
7e314c57
JK
17256
17257 /* set_die_type should be already done. */
b4ba55a1
JB
17258 set_descriptive_type (type, die, cu);
17259
7e314c57 17260 return type;
c906108c
SS
17261}
17262
7ca2d3a3 17263static enum dwarf_array_dim_ordering
6e70227d 17264read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
17265{
17266 struct attribute *attr;
17267
17268 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17269
435d3d88 17270 if (attr != nullptr)
1bc397c5
TT
17271 {
17272 LONGEST val = attr->constant_value (-1);
17273 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17274 return (enum dwarf_array_dim_ordering) val;
17275 }
7ca2d3a3 17276
0963b4bd
MS
17277 /* GNU F77 is a special case, as at 08/2004 array type info is the
17278 opposite order to the dwarf2 specification, but data is still
17279 laid out as per normal fortran.
7ca2d3a3 17280
0963b4bd
MS
17281 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17282 version checking. */
7ca2d3a3 17283
905e0470
PM
17284 if (cu->language == language_fortran
17285 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
17286 {
17287 return DW_ORD_row_major;
17288 }
17289
3a3440fb 17290 switch (cu->language_defn->array_ordering ())
7ca2d3a3
DL
17291 {
17292 case array_column_major:
17293 return DW_ORD_col_major;
17294 case array_row_major:
17295 default:
17296 return DW_ORD_row_major;
17297 };
17298}
17299
72019c9c 17300/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 17301 the DIE's type field. */
72019c9c 17302
f792889a 17303static struct type *
72019c9c
GM
17304read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17305{
7e314c57
JK
17306 struct type *domain_type, *set_type;
17307 struct attribute *attr;
f792889a 17308
7e314c57
JK
17309 domain_type = die_type (die, cu);
17310
17311 /* The die_type call above may have already set the type for this DIE. */
17312 set_type = get_die_type (die, cu);
17313 if (set_type)
17314 return set_type;
17315
17316 set_type = create_set_type (NULL, domain_type);
17317
17318 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb
TT
17319 if (attr != nullptr && attr->form_is_unsigned ())
17320 TYPE_LENGTH (set_type) = attr->as_unsigned ();
7e314c57 17321
2b4424c3
TT
17322 maybe_set_alignment (cu, die, set_type);
17323
f792889a 17324 return set_die_type (die, set_type, cu);
72019c9c 17325}
7ca2d3a3 17326
0971de02
TT
17327/* A helper for read_common_block that creates a locexpr baton.
17328 SYM is the symbol which we are marking as computed.
17329 COMMON_DIE is the DIE for the common block.
17330 COMMON_LOC is the location expression attribute for the common
17331 block itself.
17332 MEMBER_LOC is the location expression attribute for the particular
17333 member of the common block that we are processing.
17334 CU is the CU from which the above come. */
17335
17336static void
17337mark_common_block_symbol_computed (struct symbol *sym,
17338 struct die_info *common_die,
17339 struct attribute *common_loc,
17340 struct attribute *member_loc,
17341 struct dwarf2_cu *cu)
17342{
5e22e966 17343 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 17344 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
17345 struct dwarf2_locexpr_baton *baton;
17346 gdb_byte *ptr;
17347 unsigned int cu_off;
08feed99 17348 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
17349 LONGEST offset = 0;
17350
17351 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
17352 gdb_assert (common_loc->form_is_block ());
17353 gdb_assert (member_loc->form_is_block ()
cd6c91b4 17354 || member_loc->form_is_constant ());
0971de02 17355
8d749320 17356 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 17357 baton->per_objfile = per_objfile;
0971de02
TT
17358 baton->per_cu = cu->per_cu;
17359 gdb_assert (baton->per_cu);
17360
17361 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17362
cd6c91b4 17363 if (member_loc->form_is_constant ())
0971de02 17364 {
0826b30a 17365 offset = member_loc->constant_value (0);
0971de02
TT
17366 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17367 }
17368 else
9d2246fc 17369 baton->size += member_loc->as_block ()->size;
0971de02 17370
224c3ddb 17371 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
17372 baton->data = ptr;
17373
17374 *ptr++ = DW_OP_call4;
9c541725 17375 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
17376 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17377 ptr += 4;
17378
cd6c91b4 17379 if (member_loc->form_is_constant ())
0971de02
TT
17380 {
17381 *ptr++ = DW_OP_addr;
17382 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17383 ptr += cu->header.addr_size;
17384 }
17385 else
17386 {
17387 /* We have to copy the data here, because DW_OP_call4 will only
17388 use a DW_AT_location attribute. */
9d2246fc
TT
17389 struct dwarf_block *block = member_loc->as_block ();
17390 memcpy (ptr, block->data, block->size);
17391 ptr += block->size;
0971de02
TT
17392 }
17393
17394 *ptr++ = DW_OP_plus;
17395 gdb_assert (ptr - baton->data == baton->size);
17396
0971de02 17397 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 17398 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
17399}
17400
4357ac6c
TT
17401/* Create appropriate locally-scoped variables for all the
17402 DW_TAG_common_block entries. Also create a struct common_block
17403 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 17404 is used to separate the common blocks name namespace from regular
4357ac6c 17405 variable names. */
c906108c
SS
17406
17407static void
e7c27a73 17408read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17409{
0971de02
TT
17410 struct attribute *attr;
17411
17412 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 17413 if (attr != nullptr)
0971de02
TT
17414 {
17415 /* Support the .debug_loc offsets. */
4fc6c0d5 17416 if (attr->form_is_block ())
dda83cd7 17417 {
0971de02 17418 /* Ok. */
dda83cd7 17419 }
cd6c91b4 17420 else if (attr->form_is_section_offset ())
dda83cd7 17421 {
0971de02
TT
17422 dwarf2_complex_location_expr_complaint ();
17423 attr = NULL;
dda83cd7 17424 }
0971de02 17425 else
dda83cd7 17426 {
0971de02
TT
17427 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17428 "common block member");
17429 attr = NULL;
dda83cd7 17430 }
0971de02
TT
17431 }
17432
639d11d3 17433 if (die->child != NULL)
c906108c 17434 {
5e22e966 17435 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
17436 struct die_info *child_die;
17437 size_t n_entries = 0, size;
17438 struct common_block *common_block;
17439 struct symbol *sym;
74ac6d43 17440
4357ac6c
TT
17441 for (child_die = die->child;
17442 child_die && child_die->tag;
436c571c 17443 child_die = child_die->sibling)
4357ac6c
TT
17444 ++n_entries;
17445
17446 size = (sizeof (struct common_block)
17447 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
17448 common_block
17449 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17450 size);
4357ac6c
TT
17451 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17452 common_block->n_entries = 0;
17453
17454 for (child_die = die->child;
17455 child_die && child_die->tag;
436c571c 17456 child_die = child_die->sibling)
4357ac6c
TT
17457 {
17458 /* Create the symbol in the DW_TAG_common_block block in the current
17459 symbol scope. */
e7c27a73 17460 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
17461 if (sym != NULL)
17462 {
17463 struct attribute *member_loc;
17464
17465 common_block->contents[common_block->n_entries++] = sym;
17466
17467 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17468 cu);
17469 if (member_loc)
17470 {
17471 /* GDB has handled this for a long time, but it is
17472 not specified by DWARF. It seems to have been
17473 emitted by gfortran at least as recently as:
17474 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 17475 complaint (_("Variable in common block has "
0971de02 17476 "DW_AT_data_member_location "
9d8780f0
SM
17477 "- DIE at %s [in module %s]"),
17478 sect_offset_str (child_die->sect_off),
518817b3 17479 objfile_name (objfile));
0971de02 17480
cd6c91b4 17481 if (member_loc->form_is_section_offset ())
0971de02 17482 dwarf2_complex_location_expr_complaint ();
cd6c91b4 17483 else if (member_loc->form_is_constant ()
4fc6c0d5 17484 || member_loc->form_is_block ())
0971de02 17485 {
435d3d88 17486 if (attr != nullptr)
0971de02
TT
17487 mark_common_block_symbol_computed (sym, die, attr,
17488 member_loc, cu);
17489 }
17490 else
17491 dwarf2_complex_location_expr_complaint ();
17492 }
17493 }
c906108c 17494 }
4357ac6c
TT
17495
17496 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17497 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
17498 }
17499}
17500
0114d602 17501/* Create a type for a C++ namespace. */
d9fa45fe 17502
0114d602
DJ
17503static struct type *
17504read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 17505{
5e22e966 17506 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17507 const char *previous_prefix, *name;
9219021c 17508 int is_anonymous;
0114d602
DJ
17509 struct type *type;
17510
17511 /* For extensions, reuse the type of the original namespace. */
17512 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17513 {
17514 struct die_info *ext_die;
17515 struct dwarf2_cu *ext_cu = cu;
9a619af0 17516
0114d602
DJ
17517 ext_die = dwarf2_extension (die, &ext_cu);
17518 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
17519
17520 /* EXT_CU may not be the same as CU.
02142a6c 17521 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
17522 return set_die_type (die, type, cu);
17523 }
9219021c 17524
e142c38c 17525 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
17526
17527 /* Now build the name of the current namespace. */
17528
0114d602
DJ
17529 previous_prefix = determine_prefix (die, cu);
17530 if (previous_prefix[0] != '\0')
17531 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 17532 previous_prefix, name, 0, cu);
0114d602
DJ
17533
17534 /* Create the type. */
19f392bc 17535 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 17536
60531b24 17537 return set_die_type (die, type, cu);
0114d602
DJ
17538}
17539
22cee43f 17540/* Read a namespace scope. */
0114d602
DJ
17541
17542static void
17543read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17544{
5e22e966 17545 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17546 int is_anonymous;
9219021c 17547
5c4e30ca
DC
17548 /* Add a symbol associated to this if we haven't seen the namespace
17549 before. Also, add a using directive if it's an anonymous
17550 namespace. */
9219021c 17551
f2f0e013 17552 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17553 {
17554 struct type *type;
17555
0114d602 17556 type = read_type_die (die, cu);
e7c27a73 17557 new_symbol (die, type, cu);
5c4e30ca 17558
e8e80198 17559 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17560 if (is_anonymous)
0114d602
DJ
17561 {
17562 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17563
eb1e02fd 17564 std::vector<const char *> excludes;
804d2729 17565 add_using_directive (using_directives (cu),
7d93a1e0 17566 previous_prefix, type->name (), NULL,
eb1e02fd 17567 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17568 }
5c4e30ca 17569 }
9219021c 17570
639d11d3 17571 if (die->child != NULL)
d9fa45fe 17572 {
639d11d3 17573 struct die_info *child_die = die->child;
6e70227d 17574
d9fa45fe
DC
17575 while (child_die && child_die->tag)
17576 {
e7c27a73 17577 process_die (child_die, cu);
436c571c 17578 child_die = child_die->sibling;
d9fa45fe
DC
17579 }
17580 }
38d518c9
EZ
17581}
17582
f55ee35c
JK
17583/* Read a Fortran module as type. This DIE can be only a declaration used for
17584 imported module. Still we need that type as local Fortran "use ... only"
17585 declaration imports depend on the created type in determine_prefix. */
17586
17587static struct type *
17588read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17589{
5e22e966 17590 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 17591 const char *module_name;
f55ee35c
JK
17592 struct type *type;
17593
17594 module_name = dwarf2_name (die, cu);
19f392bc 17595 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17596
f55ee35c
JK
17597 return set_die_type (die, type, cu);
17598}
17599
5d7cb8df
JK
17600/* Read a Fortran module. */
17601
17602static void
17603read_module (struct die_info *die, struct dwarf2_cu *cu)
17604{
17605 struct die_info *child_die = die->child;
530e8392
KB
17606 struct type *type;
17607
17608 type = read_type_die (die, cu);
17609 new_symbol (die, type, cu);
5d7cb8df 17610
5d7cb8df
JK
17611 while (child_die && child_die->tag)
17612 {
17613 process_die (child_die, cu);
436c571c 17614 child_die = child_die->sibling;
5d7cb8df
JK
17615 }
17616}
17617
38d518c9
EZ
17618/* Return the name of the namespace represented by DIE. Set
17619 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17620 namespace. */
17621
17622static const char *
e142c38c 17623namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17624{
17625 struct die_info *current_die;
17626 const char *name = NULL;
17627
17628 /* Loop through the extensions until we find a name. */
17629
17630 for (current_die = die;
17631 current_die != NULL;
f2f0e013 17632 current_die = dwarf2_extension (die, &cu))
38d518c9 17633 {
96553a0c
DE
17634 /* We don't use dwarf2_name here so that we can detect the absence
17635 of a name -> anonymous namespace. */
7d45c7c3 17636 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17637
38d518c9
EZ
17638 if (name != NULL)
17639 break;
17640 }
17641
17642 /* Is it an anonymous namespace? */
17643
17644 *is_anonymous = (name == NULL);
17645 if (*is_anonymous)
2b1dbab0 17646 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17647
17648 return name;
d9fa45fe
DC
17649}
17650
c906108c
SS
17651/* Extract all information from a DW_TAG_pointer_type DIE and add to
17652 the user defined type vector. */
17653
f792889a 17654static struct type *
e7c27a73 17655read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17656{
5e22e966 17657 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 17658 struct comp_unit_head *cu_header = &cu->header;
c906108c 17659 struct type *type;
8b2dbe47
KB
17660 struct attribute *attr_byte_size;
17661 struct attribute *attr_address_class;
17662 int byte_size, addr_class;
7e314c57
JK
17663 struct type *target_type;
17664
17665 target_type = die_type (die, cu);
c906108c 17666
7e314c57
JK
17667 /* The die_type call above may have already set the type for this DIE. */
17668 type = get_die_type (die, cu);
17669 if (type)
17670 return type;
17671
17672 type = lookup_pointer_type (target_type);
8b2dbe47 17673
e142c38c 17674 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47 17675 if (attr_byte_size)
529908cb 17676 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
c906108c 17677 else
8b2dbe47
KB
17678 byte_size = cu_header->addr_size;
17679
e142c38c 17680 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47 17681 if (attr_address_class)
529908cb 17682 addr_class = attr_address_class->constant_value (DW_ADDR_none);
8b2dbe47
KB
17683 else
17684 addr_class = DW_ADDR_none;
17685
2b4424c3
TT
17686 ULONGEST alignment = get_alignment (cu, die);
17687
17688 /* If the pointer size, alignment, or address class is different
17689 than the default, create a type variant marked as such and set
17690 the length accordingly. */
17691 if (TYPE_LENGTH (type) != byte_size
17692 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17693 && alignment != TYPE_RAW_ALIGN (type))
17694 || addr_class != DW_ADDR_none)
c906108c 17695 {
5e2b427d 17696 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47 17697 {
314ad88d
PA
17698 type_instance_flags type_flags
17699 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17700 addr_class);
876cecd0
TT
17701 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17702 == 0);
8b2dbe47
KB
17703 type = make_type_with_address_space (type, type_flags);
17704 }
17705 else if (TYPE_LENGTH (type) != byte_size)
17706 {
b98664d3 17707 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17708 }
2b4424c3
TT
17709 else if (TYPE_RAW_ALIGN (type) != alignment)
17710 {
b98664d3 17711 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17712 " - DIE at %s [in module %s]"),
17713 sect_offset_str (die->sect_off),
5e22e966 17714 objfile_name (cu->per_objfile->objfile));
2b4424c3 17715 }
6e70227d 17716 else
9a619af0
MS
17717 {
17718 /* Should we also complain about unhandled address classes? */
17719 }
c906108c 17720 }
8b2dbe47
KB
17721
17722 TYPE_LENGTH (type) = byte_size;
2b4424c3 17723 set_type_align (type, alignment);
f792889a 17724 return set_die_type (die, type, cu);
c906108c
SS
17725}
17726
17727/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17728 the user defined type vector. */
17729
f792889a 17730static struct type *
e7c27a73 17731read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17732{
17733 struct type *type;
17734 struct type *to_type;
17735 struct type *domain;
17736
e7c27a73
DJ
17737 to_type = die_type (die, cu);
17738 domain = die_containing_type (die, cu);
0d5de010 17739
7e314c57
JK
17740 /* The calls above may have already set the type for this DIE. */
17741 type = get_die_type (die, cu);
17742 if (type)
17743 return type;
17744
78134374 17745 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 17746 type = lookup_methodptr_type (to_type);
78134374 17747 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 17748 {
5e22e966 17749 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
17750
17751 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 17752 to_type->fields (), to_type->num_fields (),
a409645d 17753 to_type->has_varargs ());
7078baeb
TT
17754 type = lookup_methodptr_type (new_type);
17755 }
0d5de010
DJ
17756 else
17757 type = lookup_memberptr_type (to_type, domain);
c906108c 17758
f792889a 17759 return set_die_type (die, type, cu);
c906108c
SS
17760}
17761
4297a3f0 17762/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17763 the user defined type vector. */
17764
f792889a 17765static struct type *
4297a3f0 17766read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
dda83cd7 17767 enum type_code refcode)
c906108c 17768{
e7c27a73 17769 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17770 struct type *type, *target_type;
c906108c
SS
17771 struct attribute *attr;
17772
4297a3f0
AV
17773 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17774
7e314c57
JK
17775 target_type = die_type (die, cu);
17776
17777 /* The die_type call above may have already set the type for this DIE. */
17778 type = get_die_type (die, cu);
17779 if (type)
17780 return type;
17781
4297a3f0 17782 type = lookup_reference_type (target_type, refcode);
e142c38c 17783 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17784 if (attr != nullptr)
c906108c 17785 {
529908cb 17786 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
c906108c
SS
17787 }
17788 else
17789 {
107d2387 17790 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17791 }
2b4424c3 17792 maybe_set_alignment (cu, die, type);
f792889a 17793 return set_die_type (die, type, cu);
c906108c
SS
17794}
17795
cf363f18
MW
17796/* Add the given cv-qualifiers to the element type of the array. GCC
17797 outputs DWARF type qualifiers that apply to an array, not the
17798 element type. But GDB relies on the array element type to carry
17799 the cv-qualifiers. This mimics section 6.7.3 of the C99
17800 specification. */
17801
17802static struct type *
17803add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17804 struct type *base_type, int cnst, int voltl)
17805{
17806 struct type *el_type, *inner_array;
17807
17808 base_type = copy_type (base_type);
17809 inner_array = base_type;
17810
78134374 17811 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17812 {
17813 TYPE_TARGET_TYPE (inner_array) =
17814 copy_type (TYPE_TARGET_TYPE (inner_array));
17815 inner_array = TYPE_TARGET_TYPE (inner_array);
17816 }
17817
17818 el_type = TYPE_TARGET_TYPE (inner_array);
17819 cnst |= TYPE_CONST (el_type);
17820 voltl |= TYPE_VOLATILE (el_type);
17821 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17822
17823 return set_die_type (die, base_type, cu);
17824}
17825
f792889a 17826static struct type *
e7c27a73 17827read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17828{
f792889a 17829 struct type *base_type, *cv_type;
c906108c 17830
e7c27a73 17831 base_type = die_type (die, cu);
7e314c57
JK
17832
17833 /* The die_type call above may have already set the type for this DIE. */
17834 cv_type = get_die_type (die, cu);
17835 if (cv_type)
17836 return cv_type;
17837
2f608a3a
KW
17838 /* In case the const qualifier is applied to an array type, the element type
17839 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 17840 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 17841 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17842
f792889a
DJ
17843 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17844 return set_die_type (die, cv_type, cu);
c906108c
SS
17845}
17846
f792889a 17847static struct type *
e7c27a73 17848read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17849{
f792889a 17850 struct type *base_type, *cv_type;
c906108c 17851
e7c27a73 17852 base_type = die_type (die, cu);
7e314c57
JK
17853
17854 /* The die_type call above may have already set the type for this DIE. */
17855 cv_type = get_die_type (die, cu);
17856 if (cv_type)
17857 return cv_type;
17858
cf363f18
MW
17859 /* In case the volatile qualifier is applied to an array type, the
17860 element type is so qualified, not the array type (section 6.7.3
17861 of C99). */
78134374 17862 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17863 return add_array_cv_type (die, cu, base_type, 0, 1);
17864
f792889a
DJ
17865 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17866 return set_die_type (die, cv_type, cu);
c906108c
SS
17867}
17868
06d66ee9
TT
17869/* Handle DW_TAG_restrict_type. */
17870
17871static struct type *
17872read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17873{
17874 struct type *base_type, *cv_type;
17875
17876 base_type = die_type (die, cu);
17877
17878 /* The die_type call above may have already set the type for this DIE. */
17879 cv_type = get_die_type (die, cu);
17880 if (cv_type)
17881 return cv_type;
17882
17883 cv_type = make_restrict_type (base_type);
17884 return set_die_type (die, cv_type, cu);
17885}
17886
a2c2acaf
MW
17887/* Handle DW_TAG_atomic_type. */
17888
17889static struct type *
17890read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17891{
17892 struct type *base_type, *cv_type;
17893
17894 base_type = die_type (die, cu);
17895
17896 /* The die_type call above may have already set the type for this DIE. */
17897 cv_type = get_die_type (die, cu);
17898 if (cv_type)
17899 return cv_type;
17900
17901 cv_type = make_atomic_type (base_type);
17902 return set_die_type (die, cv_type, cu);
17903}
17904
c906108c
SS
17905/* Extract all information from a DW_TAG_string_type DIE and add to
17906 the user defined type vector. It isn't really a user defined type,
17907 but it behaves like one, with other DIE's using an AT_user_def_type
17908 attribute to reference it. */
17909
f792889a 17910static struct type *
e7c27a73 17911read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17912{
5e22e966 17913 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17914 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17915 struct type *type, *range_type, *index_type, *char_type;
17916 struct attribute *attr;
216a7e6b
AB
17917 struct dynamic_prop prop;
17918 bool length_is_constant = true;
17919 LONGEST length;
17920
17921 /* There are a couple of places where bit sizes might be made use of
17922 when parsing a DW_TAG_string_type, however, no producer that we know
17923 of make use of these. Handling bit sizes that are a multiple of the
17924 byte size is easy enough, but what about other bit sizes? Lets deal
17925 with that problem when we have to. Warn about these attributes being
17926 unsupported, then parse the type and ignore them like we always
17927 have. */
17928 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17929 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17930 {
17931 static bool warning_printed = false;
17932 if (!warning_printed)
17933 {
17934 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17935 "currently supported on DW_TAG_string_type."));
17936 warning_printed = true;
17937 }
17938 }
c906108c 17939
e142c38c 17940 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17941 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17942 {
17943 /* The string length describes the location at which the length of
17944 the string can be found. The size of the length field can be
17945 specified with one of the attributes below. */
17946 struct type *prop_type;
17947 struct attribute *len
17948 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17949 if (len == nullptr)
17950 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17951 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17952 {
17953 /* Pass 0 as the default as we know this attribute is constant
17954 and the default value will not be returned. */
0826b30a 17955 LONGEST sz = len->constant_value (0);
293e7e51 17956 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17957 }
17958 else
17959 {
17960 /* If the size is not specified then we assume it is the size of
17961 an address on this target. */
293e7e51 17962 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17963 }
17964
17965 /* Convert the attribute into a dynamic property. */
17966 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17967 length = 1;
17968 else
17969 length_is_constant = false;
17970 }
17971 else if (attr != nullptr)
17972 {
17973 /* This DW_AT_string_length just contains the length with no
17974 indirection. There's no need to create a dynamic property in this
17975 case. Pass 0 for the default value as we know it will not be
17976 returned in this case. */
0826b30a 17977 length = attr->constant_value (0);
216a7e6b
AB
17978 }
17979 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17980 {
216a7e6b 17981 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17982 length = attr->constant_value (1);
c906108c
SS
17983 }
17984 else
17985 {
216a7e6b
AB
17986 /* Use 1 as a fallback length if we have nothing else. */
17987 length = 1;
c906108c 17988 }
6ccb9162 17989
46bf5051 17990 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17991 if (length_is_constant)
17992 range_type = create_static_range_type (NULL, index_type, 1, length);
17993 else
17994 {
17995 struct dynamic_prop low_bound;
17996
8c2e4e06 17997 low_bound.set_const_val (1);
216a7e6b
AB
17998 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17999 }
3b7538c0
UW
18000 char_type = language_string_char_type (cu->language_defn, gdbarch);
18001 type = create_string_type (NULL, char_type, range_type);
6ccb9162 18002
f792889a 18003 return set_die_type (die, type, cu);
c906108c
SS
18004}
18005
4d804846
JB
18006/* Assuming that DIE corresponds to a function, returns nonzero
18007 if the function is prototyped. */
18008
18009static int
18010prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
18011{
18012 struct attribute *attr;
18013
18014 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c45bc3f8 18015 if (attr && attr->as_boolean ())
4d804846
JB
18016 return 1;
18017
18018 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 18019 is only meaningful for C, but the concept also extends to other
4d804846
JB
18020 languages that allow unprototyped functions (Eg: Objective C).
18021 For all other languages, assume that functions are always
18022 prototyped. */
18023 if (cu->language != language_c
18024 && cu->language != language_objc
18025 && cu->language != language_opencl)
18026 return 1;
18027
18028 /* RealView does not emit DW_AT_prototyped. We can not distinguish
18029 prototyped and unprototyped functions; default to prototyped,
18030 since that is more common in modern code (and RealView warns
18031 about unprototyped functions). */
18032 if (producer_is_realview (cu->producer))
18033 return 1;
18034
18035 return 0;
18036}
18037
c906108c
SS
18038/* Handle DIES due to C code like:
18039
18040 struct foo
c5aa993b
JM
18041 {
18042 int (*funcp)(int a, long l);
18043 int b;
18044 };
c906108c 18045
0963b4bd 18046 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 18047
f792889a 18048static struct type *
e7c27a73 18049read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18050{
5e22e966 18051 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
18052 struct type *type; /* Type that this function returns. */
18053 struct type *ftype; /* Function that returns above type. */
c906108c
SS
18054 struct attribute *attr;
18055
e7c27a73 18056 type = die_type (die, cu);
7e314c57
JK
18057
18058 /* The die_type call above may have already set the type for this DIE. */
18059 ftype = get_die_type (die, cu);
18060 if (ftype)
18061 return ftype;
18062
0c8b41f1 18063 ftype = lookup_function_type (type);
c906108c 18064
4d804846 18065 if (prototyped_function_p (die, cu))
27e69b7a 18066 ftype->set_is_prototyped (true);
c906108c 18067
c055b101
CV
18068 /* Store the calling convention in the type if it's available in
18069 the subroutine die. Otherwise set the calling convention to
18070 the default value DW_CC_normal. */
18071 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf 18072 if (attr != nullptr
529908cb 18073 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
d0922fcf 18074 TYPE_CALLING_CONVENTION (ftype)
529908cb 18075 = (enum dwarf_calling_convention) attr->constant_value (0);
54fcddd0
UW
18076 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
18077 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
18078 else
18079 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 18080
743649fd
MW
18081 /* Record whether the function returns normally to its caller or not
18082 if the DWARF producer set that information. */
18083 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
c45bc3f8 18084 if (attr && attr->as_boolean ())
743649fd
MW
18085 TYPE_NO_RETURN (ftype) = 1;
18086
76c10ea2
GM
18087 /* We need to add the subroutine type to the die immediately so
18088 we don't infinitely recurse when dealing with parameters
0963b4bd 18089 declared as the same subroutine type. */
76c10ea2 18090 set_die_type (die, ftype, cu);
6e70227d 18091
639d11d3 18092 if (die->child != NULL)
c906108c 18093 {
bb5ed363 18094 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 18095 struct die_info *child_die;
8072405b 18096 int nparams, iparams;
c906108c
SS
18097
18098 /* Count the number of parameters.
dda83cd7
SM
18099 FIXME: GDB currently ignores vararg functions, but knows about
18100 vararg member functions. */
8072405b 18101 nparams = 0;
639d11d3 18102 child_die = die->child;
c906108c
SS
18103 while (child_die && child_die->tag)
18104 {
18105 if (child_die->tag == DW_TAG_formal_parameter)
18106 nparams++;
18107 else if (child_die->tag == DW_TAG_unspecified_parameters)
1d6286ed
SM
18108 ftype->set_has_varargs (true);
18109
436c571c 18110 child_die = child_die->sibling;
c906108c
SS
18111 }
18112
18113 /* Allocate storage for parameters and fill them in. */
5e33d5f4 18114 ftype->set_num_fields (nparams);
3cabb6b0
SM
18115 ftype->set_fields
18116 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 18117
8072405b
JK
18118 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18119 even if we error out during the parameters reading below. */
18120 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 18121 ftype->field (iparams).set_type (void_type);
8072405b
JK
18122
18123 iparams = 0;
639d11d3 18124 child_die = die->child;
c906108c
SS
18125 while (child_die && child_die->tag)
18126 {
18127 if (child_die->tag == DW_TAG_formal_parameter)
18128 {
3ce3b1ba
PA
18129 struct type *arg_type;
18130
18131 /* DWARF version 2 has no clean way to discern C++
18132 static and non-static member functions. G++ helps
18133 GDB by marking the first parameter for non-static
18134 member functions (which is the this pointer) as
18135 artificial. We pass this information to
18136 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18137
18138 DWARF version 3 added DW_AT_object_pointer, which GCC
18139 4.5 does not yet generate. */
e142c38c 18140 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 18141 if (attr != nullptr)
c45bc3f8 18142 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
c906108c 18143 else
9c37b5ae 18144 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
18145 arg_type = die_type (child_die, cu);
18146
18147 /* RealView does not mark THIS as const, which the testsuite
18148 expects. GCC marks THIS as const in method definitions,
18149 but not in the class specifications (GCC PR 43053). */
18150 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18151 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18152 {
18153 int is_this = 0;
18154 struct dwarf2_cu *arg_cu = cu;
18155 const char *name = dwarf2_name (child_die, cu);
18156
18157 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 18158 if (attr != nullptr)
3ce3b1ba
PA
18159 {
18160 /* If the compiler emits this, use it. */
18161 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18162 is_this = 1;
18163 }
18164 else if (name && strcmp (name, "this") == 0)
18165 /* Function definitions will have the argument names. */
18166 is_this = 1;
18167 else if (name == NULL && iparams == 0)
18168 /* Declarations may not have the names, so like
18169 elsewhere in GDB, assume an artificial first
18170 argument is "this". */
18171 is_this = 1;
18172
18173 if (is_this)
18174 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18175 arg_type, 0);
18176 }
18177
5d14b6e5 18178 ftype->field (iparams).set_type (arg_type);
c906108c
SS
18179 iparams++;
18180 }
436c571c 18181 child_die = child_die->sibling;
c906108c
SS
18182 }
18183 }
18184
76c10ea2 18185 return ftype;
c906108c
SS
18186}
18187
f792889a 18188static struct type *
e7c27a73 18189read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18190{
5e22e966 18191 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 18192 const char *name = NULL;
3c8e0968 18193 struct type *this_type, *target_type;
c906108c 18194
94af9270 18195 name = dwarf2_full_name (NULL, die, cu);
19f392bc 18196 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
8f53807e 18197 this_type->set_target_is_stub (true);
f792889a 18198 set_die_type (die, this_type, cu);
3c8e0968
DE
18199 target_type = die_type (die, cu);
18200 if (target_type != this_type)
18201 TYPE_TARGET_TYPE (this_type) = target_type;
18202 else
18203 {
18204 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18205 spec and cause infinite loops in GDB. */
b98664d3 18206 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
18207 "- DIE at %s [in module %s]"),
18208 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
18209 TYPE_TARGET_TYPE (this_type) = NULL;
18210 }
e4003a34
TV
18211 if (name == NULL)
18212 {
18213 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18214 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18215 Handle these by just returning the target type, rather than
18216 constructing an anonymous typedef type and trying to handle this
18217 elsewhere. */
18218 set_die_type (die, target_type, cu);
18219 return target_type;
18220 }
f792889a 18221 return this_type;
c906108c
SS
18222}
18223
a625a8c9
TT
18224/* Helper for get_dwarf2_rational_constant that computes the value of
18225 a given gmp_mpz given an attribute. */
18226
18227static void
18228get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
18229{
18230 /* GCC will sometimes emit a 16-byte constant value as a DWARF
18231 location expression that pushes an implicit value. */
18232 if (attr->form == DW_FORM_exprloc)
18233 {
18234 dwarf_block *blk = attr->as_block ();
18235 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18236 {
18237 uint64_t len;
18238 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18239 blk->data + blk->size,
18240 &len);
18241 if (ptr - blk->data + len <= blk->size)
18242 {
18243 mpz_import (value->val, len,
18244 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18245 1, 0, 0, ptr);
18246 return;
18247 }
18248 }
18249
18250 /* On failure set it to 1. */
18251 *value = gdb_mpz (1);
18252 }
18253 else if (attr->form_is_block ())
18254 {
18255 dwarf_block *blk = attr->as_block ();
18256 mpz_import (value->val, blk->size,
18257 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18258 1, 0, 0, blk->data);
18259 }
18260 else
18261 *value = gdb_mpz (attr->constant_value (1));
18262}
18263
09584414
JB
18264/* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18265 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18266
18267 If the numerator and/or numerator attribute is missing,
18268 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18269 untouched. */
18270
18271static void
18272get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
5cde1d82 18273 gdb_mpz *numerator, gdb_mpz *denominator)
09584414
JB
18274{
18275 struct attribute *num_attr, *denom_attr;
18276
18277 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18278 if (num_attr == nullptr)
18279 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18280 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18281
18282 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18283 if (denom_attr == nullptr)
18284 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18285 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18286
18287 if (num_attr == nullptr || denom_attr == nullptr)
18288 return;
18289
a625a8c9
TT
18290 get_mpz (cu, numerator, num_attr);
18291 get_mpz (cu, denominator, denom_attr);
09584414
JB
18292}
18293
18294/* Same as get_dwarf2_rational_constant, but extracting an unsigned
18295 rational constant, rather than a signed one.
18296
18297 If the rational constant has a negative value, a complaint
18298 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18299
18300static void
18301get_dwarf2_unsigned_rational_constant (struct die_info *die,
18302 struct dwarf2_cu *cu,
5cde1d82
TT
18303 gdb_mpz *numerator,
18304 gdb_mpz *denominator)
09584414 18305{
5cde1d82
TT
18306 gdb_mpz num (1);
18307 gdb_mpz denom (1);
09584414
JB
18308
18309 get_dwarf2_rational_constant (die, cu, &num, &denom);
5cde1d82 18310 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
09584414 18311 {
5cde1d82
TT
18312 mpz_neg (num.val, num.val);
18313 mpz_neg (denom.val, denom.val);
09584414 18314 }
5cde1d82 18315 else if (mpz_sgn (num.val) == -1)
09584414
JB
18316 {
18317 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18318 " in DIE at %s"),
18319 sect_offset_str (die->sect_off));
18320 return;
18321 }
5cde1d82 18322 else if (mpz_sgn (denom.val) == -1)
09584414
JB
18323 {
18324 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18325 " in DIE at %s"),
18326 sect_offset_str (die->sect_off));
18327 return;
18328 }
18329
5cde1d82
TT
18330 *numerator = std::move (num);
18331 *denominator = std::move (denom);
09584414
JB
18332}
18333
bbcdf9ab
TT
18334/* Assuming that ENCODING is a string whose contents starting at the
18335 K'th character is "_nn" where "nn" is a decimal number, scan that
18336 number and set RESULT to the value. K is updated to point to the
18337 character immediately following the number.
18338
18339 If the string does not conform to the format described above, false
18340 is returned, and K may or may not be changed. */
18341
18342static bool
18343ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
18344{
18345 /* The next character should be an underscore ('_') followed
18346 by a digit. */
18347 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
18348 return false;
18349
18350 /* Skip the underscore. */
18351 k++;
18352 int start = k;
18353
18354 /* Determine the number of digits for our number. */
18355 while (isdigit (encoding[k]))
18356 k++;
18357 if (k == start)
18358 return false;
18359
18360 std::string copy (&encoding[start], k - start);
18361 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
18362 return false;
18363
18364 return true;
18365}
18366
18367/* Scan two numbers from ENCODING at OFFSET, assuming the string is of
18368 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
18369 DENOM, update OFFSET, and return true on success. Return false on
18370 failure. */
18371
18372static bool
18373ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
18374 gdb_mpz *num, gdb_mpz *denom)
18375{
18376 if (!ada_get_gnat_encoded_number (encoding, offset, num))
18377 return false;
18378 return ada_get_gnat_encoded_number (encoding, offset, denom);
18379}
18380
09584414 18381/* Assuming DIE corresponds to a fixed point type, finish the creation
bbcdf9ab
TT
18382 of the corresponding TYPE by setting its type-specific data. CU is
18383 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
18384 encodings. It is nullptr if the GNAT encoding should be
18385 ignored. */
09584414
JB
18386
18387static void
bbcdf9ab
TT
18388finish_fixed_point_type (struct type *type, const char *suffix,
18389 struct die_info *die, struct dwarf2_cu *cu)
09584414 18390{
09584414
JB
18391 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18392 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18393
bbcdf9ab
TT
18394 /* If GNAT encodings are preferred, don't examine the
18395 attributes. */
18396 struct attribute *attr = nullptr;
18397 if (suffix == nullptr)
18398 {
18399 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18400 if (attr == nullptr)
18401 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18402 if (attr == nullptr)
18403 attr = dwarf2_attr (die, DW_AT_small, cu);
18404 }
09584414 18405
5cde1d82
TT
18406 /* Numerator and denominator of our fixed-point type's scaling factor.
18407 The default is a scaling factor of 1, which we use as a fallback
18408 when we are not able to decode it (problem with the debugging info,
18409 unsupported forms, bug in GDB, etc...). Using that as the default
18410 allows us to at least print the unscaled value, which might still
18411 be useful to a user. */
18412 gdb_mpz scale_num (1);
18413 gdb_mpz scale_denom (1);
18414
09584414
JB
18415 if (attr == nullptr)
18416 {
bbcdf9ab
TT
18417 int offset = 0;
18418 if (suffix != nullptr
18419 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18420 &scale_denom)
18421 /* The number might be encoded as _nn_dd_nn_dd, where the
18422 second ratio is the 'small value. In this situation, we
18423 want the second value. */
18424 && (suffix[offset] != '_'
18425 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18426 &scale_denom)))
18427 {
18428 /* Found it. */
18429 }
18430 else
18431 {
18432 /* Scaling factor not found. Assume a scaling factor of 1,
18433 and hope for the best. At least the user will be able to
18434 see the encoded value. */
18435 scale_num = 1;
18436 scale_denom = 1;
18437 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18438 sect_offset_str (die->sect_off));
18439 }
09584414
JB
18440 }
18441 else if (attr->name == DW_AT_binary_scale)
18442 {
18443 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18444 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18445
5cde1d82 18446 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
09584414
JB
18447 }
18448 else if (attr->name == DW_AT_decimal_scale)
18449 {
18450 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18451 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18452
5cde1d82 18453 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
09584414
JB
18454 }
18455 else if (attr->name == DW_AT_small)
18456 {
18457 struct die_info *scale_die;
18458 struct dwarf2_cu *scale_cu = cu;
18459
18460 scale_die = follow_die_ref (die, attr, &scale_cu);
18461 if (scale_die->tag == DW_TAG_constant)
18462 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18463 &scale_num, &scale_denom);
18464 else
18465 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18466 " (DIE at %s)"),
18467 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18468 }
18469 else
18470 {
18471 complaint (_("unsupported scale attribute %s for fixed-point type"
18472 " (DIE at %s)"),
18473 dwarf_attr_name (attr->name),
18474 sect_offset_str (die->sect_off));
18475 }
18476
2a12c336 18477 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
5cde1d82
TT
18478 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18479 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
09584414
JB
18480 mpq_canonicalize (scaling_factor.val);
18481}
18482
bbcdf9ab
TT
18483/* The gnat-encoding suffix for fixed point. */
18484
18485#define GNAT_FIXED_POINT_SUFFIX "___XF_"
18486
18487/* If NAME encodes an Ada fixed-point type, return a pointer to the
18488 "XF" suffix of the name. The text after this is what encodes the
18489 'small and 'delta information. Otherwise, return nullptr. */
18490
18491static const char *
18492gnat_encoded_fixed_point_type_info (const char *name)
18493{
18494 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18495}
18496
9b790ce7
UW
18497/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18498 (which may be different from NAME) to the architecture back-end to allow
18499 it to guess the correct format if necessary. */
18500
18501static struct type *
18502dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 18503 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 18504{
08feed99 18505 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
18506 const struct floatformat **format;
18507 struct type *type;
18508
18509 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18510 if (format)
103a685e 18511 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 18512 else
77b7c781 18513 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
18514
18515 return type;
18516}
18517
eb77c9df
AB
18518/* Allocate an integer type of size BITS and name NAME. */
18519
18520static struct type *
18521dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18522 int bits, int unsigned_p, const char *name)
18523{
18524 struct type *type;
18525
18526 /* Versions of Intel's C Compiler generate an integer type called "void"
18527 instead of using DW_TAG_unspecified_type. This has been seen on
18528 at least versions 14, 17, and 18. */
35ee2dc2
AB
18529 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18530 && strcmp (name, "void") == 0)
eb77c9df
AB
18531 type = objfile_type (objfile)->builtin_void;
18532 else
18533 type = init_integer_type (objfile, bits, unsigned_p, name);
18534
18535 return type;
18536}
18537
09584414
JB
18538/* Return true if DIE has a DW_AT_small attribute whose value is
18539 a constant rational, where both the numerator and denominator
18540 are equal to zero.
18541
18542 CU is the DIE's Compilation Unit. */
18543
18544static bool
18545has_zero_over_zero_small_attribute (struct die_info *die,
18546 struct dwarf2_cu *cu)
18547{
18548 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18549 if (attr == nullptr)
18550 return false;
18551
18552 struct dwarf2_cu *scale_cu = cu;
18553 struct die_info *scale_die
18554 = follow_die_ref (die, attr, &scale_cu);
18555
18556 if (scale_die->tag != DW_TAG_constant)
18557 return false;
18558
5cde1d82 18559 gdb_mpz num (1), denom (1);
09584414 18560 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
5cde1d82 18561 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
09584414
JB
18562}
18563
8bdc1658
AB
18564/* Initialise and return a floating point type of size BITS suitable for
18565 use as a component of a complex number. The NAME_HINT is passed through
18566 when initialising the floating point type and is the name of the complex
18567 type.
18568
18569 As DWARF doesn't currently provide an explicit name for the components
18570 of a complex number, but it can be helpful to have these components
18571 named, we try to select a suitable name based on the size of the
18572 component. */
18573static struct type *
18574dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18575 struct objfile *objfile,
103a685e
TT
18576 int bits, const char *name_hint,
18577 enum bfd_endian byte_order)
8bdc1658 18578{
08feed99 18579 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
18580 struct type *tt = nullptr;
18581
35add35e
AB
18582 /* Try to find a suitable floating point builtin type of size BITS.
18583 We're going to use the name of this type as the name for the complex
18584 target type that we are about to create. */
1db455a7 18585 switch (cu->language)
8bdc1658 18586 {
1db455a7
AB
18587 case language_fortran:
18588 switch (bits)
18589 {
18590 case 32:
18591 tt = builtin_f_type (gdbarch)->builtin_real;
18592 break;
18593 case 64:
18594 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18595 break;
18596 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18597 case 128:
18598 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18599 break;
18600 }
8bdc1658 18601 break;
1db455a7
AB
18602 default:
18603 switch (bits)
18604 {
18605 case 32:
18606 tt = builtin_type (gdbarch)->builtin_float;
18607 break;
18608 case 64:
18609 tt = builtin_type (gdbarch)->builtin_double;
18610 break;
18611 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18612 case 128:
18613 tt = builtin_type (gdbarch)->builtin_long_double;
18614 break;
18615 }
8bdc1658
AB
18616 break;
18617 }
18618
35add35e
AB
18619 /* If the type we found doesn't match the size we were looking for, then
18620 pretend we didn't find a type at all, the complex target type we
18621 create will then be nameless. */
a12e5744 18622 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
18623 tt = nullptr;
18624
7d93a1e0 18625 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 18626 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
18627}
18628
c906108c
SS
18629/* Find a representation of a given base type and install
18630 it in the TYPE field of the die. */
18631
f792889a 18632static struct type *
e7c27a73 18633read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18634{
5e22e966 18635 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
18636 struct type *type;
18637 struct attribute *attr;
19f392bc 18638 int encoding = 0, bits = 0;
15d034d0 18639 const char *name;
34877895 18640 gdbarch *arch;
c906108c 18641
e142c38c 18642 attr = dwarf2_attr (die, DW_AT_encoding, cu);
529908cb
TT
18643 if (attr != nullptr && attr->form_is_constant ())
18644 encoding = attr->constant_value (0);
e142c38c 18645 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18646 if (attr != nullptr)
529908cb 18647 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
39cbfefa 18648 name = dwarf2_name (die, cu);
6ccb9162 18649 if (!name)
34877895 18650 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 18651
08feed99 18652 arch = objfile->arch ();
103a685e
TT
18653 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18654
34877895 18655 attr = dwarf2_attr (die, DW_AT_endianity, cu);
529908cb 18656 if (attr != nullptr && attr->form_is_constant ())
103a685e 18657 {
529908cb 18658 int endianity = attr->constant_value (0);
103a685e
TT
18659
18660 switch (endianity)
18661 {
18662 case DW_END_big:
18663 byte_order = BFD_ENDIAN_BIG;
18664 break;
18665 case DW_END_little:
18666 byte_order = BFD_ENDIAN_LITTLE;
18667 break;
18668 default:
18669 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18670 break;
18671 }
18672 }
6ccb9162 18673
09584414
JB
18674 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18675 && cu->language == language_ada
18676 && has_zero_over_zero_small_attribute (die, cu))
18677 {
18678 /* brobecker/2018-02-24: This is a fixed point type for which
18679 the scaling factor is represented as fraction whose value
18680 does not make sense (zero divided by zero), so we should
18681 normally never see these. However, there is a small category
18682 of fixed point types for which GNAT is unable to provide
18683 the scaling factor via the standard DWARF mechanisms, and
18684 for which the info is provided via the GNAT encodings instead.
bbcdf9ab 18685 This is likely what this DIE is about. */
09584414
JB
18686 encoding = (encoding == DW_ATE_signed_fixed
18687 ? DW_ATE_signed
18688 : DW_ATE_unsigned);
18689 }
18690
bbcdf9ab
TT
18691 /* With GNAT encodings, fixed-point information will be encoded in
18692 the type name. Note that this can also occur with the above
18693 zero-over-zero case, which is why this is a separate "if" rather
18694 than an "else if". */
18695 const char *gnat_encoding_suffix = nullptr;
18696 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18697 && cu->language == language_ada
18698 && name != nullptr)
18699 {
18700 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18701 if (gnat_encoding_suffix != nullptr)
18702 {
18703 gdb_assert (startswith (gnat_encoding_suffix,
18704 GNAT_FIXED_POINT_SUFFIX));
18705 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18706 name, gnat_encoding_suffix - name);
18707 /* Use -1 here so that SUFFIX points at the "_" after the
18708 "XF". */
18709 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18710
18711 encoding = (encoding == DW_ATE_signed
18712 ? DW_ATE_signed_fixed
18713 : DW_ATE_unsigned_fixed);
18714 }
18715 }
18716
6ccb9162 18717 switch (encoding)
c906108c 18718 {
6ccb9162
UW
18719 case DW_ATE_address:
18720 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 18721 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 18722 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
18723 break;
18724 case DW_ATE_boolean:
19f392bc 18725 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
18726 break;
18727 case DW_ATE_complex_float:
103a685e
TT
18728 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18729 byte_order);
78134374 18730 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
18731 {
18732 if (name == nullptr)
18733 {
18734 struct obstack *obstack
5e22e966 18735 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 18736 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
18737 nullptr);
18738 }
18739 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18740 }
18741 else
18742 type = init_complex_type (name, type);
6ccb9162
UW
18743 break;
18744 case DW_ATE_decimal_float:
19f392bc 18745 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
18746 break;
18747 case DW_ATE_float:
103a685e 18748 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
18749 break;
18750 case DW_ATE_signed:
eb77c9df 18751 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18752 break;
18753 case DW_ATE_unsigned:
3b2b8fea
TT
18754 if (cu->language == language_fortran
18755 && name
61012eef 18756 && startswith (name, "character("))
19f392bc
UW
18757 type = init_character_type (objfile, bits, 1, name);
18758 else
eb77c9df 18759 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
18760 break;
18761 case DW_ATE_signed_char:
6e70227d 18762 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
18763 || cu->language == language_pascal
18764 || cu->language == language_fortran)
19f392bc
UW
18765 type = init_character_type (objfile, bits, 0, name);
18766 else
eb77c9df 18767 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18768 break;
18769 case DW_ATE_unsigned_char:
868a0084 18770 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 18771 || cu->language == language_pascal
c44af4eb
TT
18772 || cu->language == language_fortran
18773 || cu->language == language_rust)
19f392bc
UW
18774 type = init_character_type (objfile, bits, 1, name);
18775 else
eb77c9df 18776 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 18777 break;
75079b2b 18778 case DW_ATE_UTF:
53e710ac 18779 {
53e710ac
PA
18780 if (bits == 16)
18781 type = builtin_type (arch)->builtin_char16;
18782 else if (bits == 32)
18783 type = builtin_type (arch)->builtin_char32;
18784 else
18785 {
b98664d3 18786 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 18787 bits);
eb77c9df 18788 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
18789 }
18790 return set_die_type (die, type, cu);
18791 }
75079b2b 18792 break;
09584414
JB
18793 case DW_ATE_signed_fixed:
18794 type = init_fixed_point_type (objfile, bits, 0, name);
bbcdf9ab 18795 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414
JB
18796 break;
18797 case DW_ATE_unsigned_fixed:
18798 type = init_fixed_point_type (objfile, bits, 1, name);
bbcdf9ab 18799 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414 18800 break;
75079b2b 18801
6ccb9162 18802 default:
b98664d3 18803 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 18804 dwarf_type_encoding_name (encoding));
77b7c781 18805 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18806 break;
c906108c 18807 }
6ccb9162 18808
0114d602 18809 if (name && strcmp (name, "char") == 0)
15152a54 18810 type->set_has_no_signedness (true);
0114d602 18811
2b4424c3
TT
18812 maybe_set_alignment (cu, die, type);
18813
db558e34 18814 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
34877895 18815
20a5fcbd
TT
18816 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18817 {
18818 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
529908cb 18819 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
20a5fcbd 18820 {
529908cb 18821 unsigned real_bit_size = attr->as_unsigned ();
20a5fcbd
TT
18822 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18823 /* Only use the attributes if they make sense together. */
18824 if (attr == nullptr
529908cb
TT
18825 || (attr->as_unsigned () + real_bit_size
18826 <= 8 * TYPE_LENGTH (type)))
20a5fcbd
TT
18827 {
18828 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18829 = real_bit_size;
18830 if (attr != nullptr)
18831 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
529908cb 18832 = attr->as_unsigned ();
20a5fcbd
TT
18833 }
18834 }
18835 }
18836
f792889a 18837 return set_die_type (die, type, cu);
c906108c
SS
18838}
18839
80180f79
SA
18840/* Parse dwarf attribute if it's a block, reference or constant and put the
18841 resulting value of the attribute into struct bound_prop.
18842 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18843
18844static int
18845attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
18846 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18847 struct type *default_type)
80180f79
SA
18848{
18849 struct dwarf2_property_baton *baton;
5e22e966 18850 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
18851 struct objfile *objfile = per_objfile->objfile;
18852 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 18853
9a49df9d
AB
18854 gdb_assert (default_type != NULL);
18855
80180f79
SA
18856 if (attr == NULL || prop == NULL)
18857 return 0;
18858
4fc6c0d5 18859 if (attr->form_is_block ())
80180f79 18860 {
8d749320 18861 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18862 baton->property_type = default_type;
80180f79 18863 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18864 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18865
18866 struct dwarf_block *block = attr->as_block ();
18867 baton->locexpr.size = block->size;
18868 baton->locexpr.data = block->data;
216a7e6b
AB
18869 switch (attr->name)
18870 {
18871 case DW_AT_string_length:
18872 baton->locexpr.is_reference = true;
18873 break;
18874 default:
18875 baton->locexpr.is_reference = false;
18876 break;
18877 }
8c2e4e06
SM
18878
18879 prop->set_locexpr (baton);
18880 gdb_assert (prop->baton () != NULL);
80180f79 18881 }
cd6c91b4 18882 else if (attr->form_is_ref ())
80180f79
SA
18883 {
18884 struct dwarf2_cu *target_cu = cu;
18885 struct die_info *target_die;
18886 struct attribute *target_attr;
18887
18888 target_die = follow_die_ref (die, attr, &target_cu);
18889 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18890 if (target_attr == NULL)
18891 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18892 target_cu);
80180f79
SA
18893 if (target_attr == NULL)
18894 return 0;
18895
df25ebbd 18896 switch (target_attr->name)
80180f79 18897 {
df25ebbd 18898 case DW_AT_location:
cd6c91b4 18899 if (target_attr->form_is_section_offset ())
df25ebbd 18900 {
8d749320 18901 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18902 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18903 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
8c2e4e06
SM
18904 prop->set_loclist (baton);
18905 gdb_assert (prop->baton () != NULL);
df25ebbd 18906 }
4fc6c0d5 18907 else if (target_attr->form_is_block ())
df25ebbd 18908 {
8d749320 18909 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18910 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18911 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18912 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18913 struct dwarf_block *block = target_attr->as_block ();
18914 baton->locexpr.size = block->size;
18915 baton->locexpr.data = block->data;
9a49df9d 18916 baton->locexpr.is_reference = true;
8c2e4e06
SM
18917 prop->set_locexpr (baton);
18918 gdb_assert (prop->baton () != NULL);
df25ebbd
JB
18919 }
18920 else
18921 {
18922 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18923 "dynamic property");
18924 return 0;
18925 }
18926 break;
18927 case DW_AT_data_member_location:
18928 {
18929 LONGEST offset;
18930
18931 if (!handle_data_member_location (target_die, target_cu,
18932 &offset))
18933 return 0;
18934
8d749320 18935 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18936 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18937 target_cu);
df25ebbd
JB
18938 baton->offset_info.offset = offset;
18939 baton->offset_info.type = die_type (target_die, target_cu);
8c2e4e06 18940 prop->set_addr_offset (baton);
df25ebbd
JB
18941 break;
18942 }
80180f79
SA
18943 }
18944 }
cd6c91b4 18945 else if (attr->form_is_constant ())
8c2e4e06 18946 prop->set_const_val (attr->constant_value (0));
80180f79
SA
18947 else
18948 {
18949 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18950 dwarf2_name (die, cu));
18951 return 0;
18952 }
18953
18954 return 1;
18955}
18956
09ba997f 18957/* See read.h. */
9a49df9d 18958
09ba997f 18959struct type *
293e7e51 18960dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 18961{
9a49df9d
AB
18962 struct type *int_type;
18963
18964 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18965#define TRY_TYPE(F) \
18966 int_type = (unsigned_p \
18967 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18968 : objfile_type (objfile)->builtin_ ## F); \
18969 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18970 return int_type
18971
18972 TRY_TYPE (char);
18973 TRY_TYPE (short);
18974 TRY_TYPE (int);
18975 TRY_TYPE (long);
18976 TRY_TYPE (long_long);
18977
18978#undef TRY_TYPE
18979
18980 gdb_assert_not_reached ("unable to find suitable integer type");
18981}
18982
09ba997f 18983/* See read.h. */
11a8b164 18984
09ba997f 18985struct type *
293e7e51 18986dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 18987{
293e7e51
SM
18988 int addr_size = this->per_cu->addr_size ();
18989 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
18990}
18991
b86352cf
AB
18992/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18993 present (which is valid) then compute the default type based on the
18994 compilation units address size. */
18995
18996static struct type *
18997read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18998{
18999 struct type *index_type = die_type (die, cu);
19000
19001 /* Dwarf-2 specifications explicitly allows to create subrange types
19002 without specifying a base type.
19003 In that case, the base type must be set to the type of
19004 the lower bound, upper bound or count, in that order, if any of these
19005 three attributes references an object that has a type.
19006 If no base type is found, the Dwarf-2 specifications say that
19007 a signed integer type of size equal to the size of an address should
19008 be used.
19009 For the following C code: `extern char gdb_int [];'
19010 GCC produces an empty range DIE.
19011 FIXME: muller/2010-05-28: Possible references to object for low bound,
19012 high bound or count are not yet handled by this code. */
78134374 19013 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 19014 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
19015
19016 return index_type;
19017}
19018
a02abb62
JB
19019/* Read the given DW_AT_subrange DIE. */
19020
f792889a 19021static struct type *
a02abb62
JB
19022read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
19023{
4c9ad8c2 19024 struct type *base_type, *orig_base_type;
a02abb62
JB
19025 struct type *range_type;
19026 struct attribute *attr;
729efb13 19027 struct dynamic_prop low, high;
4fae6e18 19028 int low_default_is_valid;
c451ebe5 19029 int high_bound_is_count = 0;
15d034d0 19030 const char *name;
d359392f 19031 ULONGEST negative_mask;
e77813c8 19032
b86352cf
AB
19033 orig_base_type = read_subrange_index_type (die, cu);
19034
4c9ad8c2
TT
19035 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
19036 whereas the real type might be. So, we use ORIG_BASE_TYPE when
19037 creating the range type, but we use the result of check_typedef
19038 when examining properties of the type. */
19039 base_type = check_typedef (orig_base_type);
a02abb62 19040
7e314c57
JK
19041 /* The die_type call above may have already set the type for this DIE. */
19042 range_type = get_die_type (die, cu);
19043 if (range_type)
19044 return range_type;
19045
8c2e4e06 19046 high.set_const_val (0);
729efb13 19047
4fae6e18
JK
19048 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
19049 omitting DW_AT_lower_bound. */
19050 switch (cu->language)
6e70227d 19051 {
4fae6e18
JK
19052 case language_c:
19053 case language_cplus:
8c2e4e06 19054 low.set_const_val (0);
4fae6e18
JK
19055 low_default_is_valid = 1;
19056 break;
19057 case language_fortran:
8c2e4e06 19058 low.set_const_val (1);
4fae6e18
JK
19059 low_default_is_valid = 1;
19060 break;
19061 case language_d:
4fae6e18 19062 case language_objc:
c44af4eb 19063 case language_rust:
8c2e4e06 19064 low.set_const_val (0);
4fae6e18
JK
19065 low_default_is_valid = (cu->header.version >= 4);
19066 break;
19067 case language_ada:
19068 case language_m2:
19069 case language_pascal:
8c2e4e06 19070 low.set_const_val (1);
4fae6e18
JK
19071 low_default_is_valid = (cu->header.version >= 4);
19072 break;
19073 default:
8c2e4e06 19074 low.set_const_val (0);
4fae6e18
JK
19075 low_default_is_valid = 0;
19076 break;
a02abb62
JB
19077 }
19078
e142c38c 19079 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 19080 if (attr != nullptr)
9a49df9d 19081 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 19082 else if (!low_default_is_valid)
b98664d3 19083 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
19084 "- DIE at %s [in module %s]"),
19085 sect_offset_str (die->sect_off),
5e22e966 19086 objfile_name (cu->per_objfile->objfile));
a02abb62 19087
506f5c41
TV
19088 struct attribute *attr_ub, *attr_count;
19089 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 19090 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 19091 {
506f5c41 19092 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 19093 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 19094 {
c451ebe5 19095 /* If bounds are constant do the final calculation here. */
8c2e4e06
SM
19096 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
19097 high.set_const_val (low.const_val () + high.const_val () - 1);
c451ebe5
SA
19098 else
19099 high_bound_is_count = 1;
c2ff108b 19100 }
506f5c41
TV
19101 else
19102 {
19103 if (attr_ub != NULL)
19104 complaint (_("Unresolved DW_AT_upper_bound "
19105 "- DIE at %s [in module %s]"),
19106 sect_offset_str (die->sect_off),
5e22e966 19107 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
19108 if (attr_count != NULL)
19109 complaint (_("Unresolved DW_AT_count "
19110 "- DIE at %s [in module %s]"),
19111 sect_offset_str (die->sect_off),
5e22e966 19112 objfile_name (cu->per_objfile->objfile));
506f5c41 19113 }
e77813c8 19114 }
a02abb62 19115
4e962e74
TT
19116 LONGEST bias = 0;
19117 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 19118 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 19119 bias = bias_attr->constant_value (0);
4e962e74 19120
dbb9c2b1
JB
19121 /* Normally, the DWARF producers are expected to use a signed
19122 constant form (Eg. DW_FORM_sdata) to express negative bounds.
19123 But this is unfortunately not always the case, as witnessed
19124 with GCC, for instance, where the ambiguous DW_FORM_dataN form
19125 is used instead. To work around that ambiguity, we treat
19126 the bounds as signed, and thus sign-extend their values, when
19127 the base type is signed. */
6e70227d 19128 negative_mask =
d359392f 19129 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
8c2e4e06 19130 if (low.kind () == PROP_CONST
c6d940a9 19131 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
8c2e4e06
SM
19132 low.set_const_val (low.const_val () | negative_mask);
19133 if (high.kind () == PROP_CONST
c6d940a9 19134 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
8c2e4e06 19135 high.set_const_val (high.const_val () | negative_mask);
43bbcdc2 19136
5bbd8269
AB
19137 /* Check for bit and byte strides. */
19138 struct dynamic_prop byte_stride_prop;
19139 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
19140 if (attr_byte_stride != nullptr)
19141 {
293e7e51 19142 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
19143 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
19144 prop_type);
19145 }
19146
19147 struct dynamic_prop bit_stride_prop;
19148 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
19149 if (attr_bit_stride != nullptr)
19150 {
19151 /* It only makes sense to have either a bit or byte stride. */
19152 if (attr_byte_stride != nullptr)
19153 {
19154 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19155 "- DIE at %s [in module %s]"),
19156 sect_offset_str (die->sect_off),
5e22e966 19157 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
19158 attr_bit_stride = nullptr;
19159 }
19160 else
19161 {
293e7e51 19162 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
19163 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19164 prop_type);
19165 }
19166 }
19167
19168 if (attr_byte_stride != nullptr
19169 || attr_bit_stride != nullptr)
19170 {
19171 bool byte_stride_p = (attr_byte_stride != nullptr);
19172 struct dynamic_prop *stride
19173 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19174
19175 range_type
19176 = create_range_type_with_stride (NULL, orig_base_type, &low,
19177 &high, bias, stride, byte_stride_p);
19178 }
19179 else
19180 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 19181
c451ebe5 19182 if (high_bound_is_count)
599088e3 19183 range_type->bounds ()->flag_upper_bound_is_count = 1;
c451ebe5 19184
c2ff108b
JK
19185 /* Ada expects an empty array on no boundary attributes. */
19186 if (attr == NULL && cu->language != language_ada)
8c2e4e06 19187 range_type->bounds ()->high.set_undefined ();
c2ff108b 19188
39cbfefa
DJ
19189 name = dwarf2_name (die, cu);
19190 if (name)
d0e39ea2 19191 range_type->set_name (name);
6e70227d 19192
e142c38c 19193 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 19194 if (attr != nullptr)
529908cb 19195 TYPE_LENGTH (range_type) = attr->constant_value (0);
a02abb62 19196
2b4424c3
TT
19197 maybe_set_alignment (cu, die, range_type);
19198
7e314c57
JK
19199 set_die_type (die, range_type, cu);
19200
19201 /* set_die_type should be already done. */
b4ba55a1
JB
19202 set_descriptive_type (range_type, die, cu);
19203
7e314c57 19204 return range_type;
a02abb62 19205}
6e70227d 19206
f792889a 19207static struct type *
81a17f79
JB
19208read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19209{
19210 struct type *type;
81a17f79 19211
5e22e966 19212 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 19213 type->set_name (dwarf2_name (die, cu));
81a17f79 19214
74a2f8ff 19215 /* In Ada, an unspecified type is typically used when the description
85102364 19216 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
19217 such a type, we treat it as a stub, and try to resolve it later on,
19218 when needed. */
19219 if (cu->language == language_ada)
b4b73759 19220 type->set_is_stub (true);
74a2f8ff 19221
f792889a 19222 return set_die_type (die, type, cu);
81a17f79 19223}
a02abb62 19224
639d11d3
DC
19225/* Read a single die and all its descendents. Set the die's sibling
19226 field to NULL; set other fields in the die correctly, and set all
19227 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19228 location of the info_ptr after reading all of those dies. PARENT
19229 is the parent of the die in question. */
19230
19231static struct die_info *
dee91e82 19232read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
19233 const gdb_byte *info_ptr,
19234 const gdb_byte **new_info_ptr,
dee91e82 19235 struct die_info *parent)
639d11d3
DC
19236{
19237 struct die_info *die;
d521ce57 19238 const gdb_byte *cur_ptr;
639d11d3 19239
3e225074 19240 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
19241 if (die == NULL)
19242 {
19243 *new_info_ptr = cur_ptr;
19244 return NULL;
19245 }
93311388 19246 store_in_ref_table (die, reader->cu);
639d11d3 19247
3e225074 19248 if (die->has_children)
bf6af496 19249 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
19250 else
19251 {
19252 die->child = NULL;
19253 *new_info_ptr = cur_ptr;
19254 }
19255
19256 die->sibling = NULL;
19257 die->parent = parent;
19258 return die;
19259}
19260
19261/* Read a die, all of its descendents, and all of its siblings; set
19262 all of the fields of all of the dies correctly. Arguments are as
19263 in read_die_and_children. */
19264
19265static struct die_info *
bf6af496 19266read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
19267 const gdb_byte *info_ptr,
19268 const gdb_byte **new_info_ptr,
bf6af496 19269 struct die_info *parent)
639d11d3
DC
19270{
19271 struct die_info *first_die, *last_sibling;
d521ce57 19272 const gdb_byte *cur_ptr;
639d11d3 19273
c906108c 19274 cur_ptr = info_ptr;
639d11d3
DC
19275 first_die = last_sibling = NULL;
19276
19277 while (1)
c906108c 19278 {
639d11d3 19279 struct die_info *die
dee91e82 19280 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 19281
1d325ec1 19282 if (die == NULL)
c906108c 19283 {
639d11d3
DC
19284 *new_info_ptr = cur_ptr;
19285 return first_die;
c906108c 19286 }
1d325ec1
DJ
19287
19288 if (!first_die)
19289 first_die = die;
c906108c 19290 else
1d325ec1
DJ
19291 last_sibling->sibling = die;
19292
19293 last_sibling = die;
c906108c 19294 }
c906108c
SS
19295}
19296
bf6af496
DE
19297/* Read a die, all of its descendents, and all of its siblings; set
19298 all of the fields of all of the dies correctly. Arguments are as
19299 in read_die_and_children.
19300 This the main entry point for reading a DIE and all its children. */
19301
19302static struct die_info *
19303read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
19304 const gdb_byte *info_ptr,
19305 const gdb_byte **new_info_ptr,
bf6af496
DE
19306 struct die_info *parent)
19307{
19308 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19309 new_info_ptr, parent);
19310
b4f54984 19311 if (dwarf_die_debug)
bf6af496
DE
19312 {
19313 fprintf_unfiltered (gdb_stdlog,
19314 "Read die from %s@0x%x of %s:\n",
96b79293 19315 reader->die_section->get_name (),
bf6af496
DE
19316 (unsigned) (info_ptr - reader->die_section->buffer),
19317 bfd_get_filename (reader->abfd));
b4f54984 19318 dump_die (die, dwarf_die_debug);
bf6af496
DE
19319 }
19320
19321 return die;
19322}
19323
3019eac3
DE
19324/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19325 attributes.
19326 The caller is responsible for filling in the extra attributes
19327 and updating (*DIEP)->num_attrs.
19328 Set DIEP to point to a newly allocated die with its information,
3e225074 19329 except for its child, sibling, and parent fields. */
93311388 19330
d521ce57 19331static const gdb_byte *
3019eac3 19332read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 19333 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 19334 int num_extra_attrs)
93311388 19335{
b64f50a1 19336 unsigned int abbrev_number, bytes_read, i;
7c32eebb 19337 const struct abbrev_info *abbrev;
93311388
DE
19338 struct die_info *die;
19339 struct dwarf2_cu *cu = reader->cu;
19340 bfd *abfd = reader->abfd;
19341
9c541725 19342 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
19343 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19344 info_ptr += bytes_read;
19345 if (!abbrev_number)
19346 {
19347 *diep = NULL;
93311388
DE
19348 return info_ptr;
19349 }
19350
685af9cd 19351 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 19352 if (!abbrev)
348e048f
DE
19353 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19354 abbrev_number,
19355 bfd_get_filename (abfd));
19356
3019eac3 19357 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 19358 die->sect_off = sect_off;
93311388
DE
19359 die->tag = abbrev->tag;
19360 die->abbrev = abbrev_number;
3e225074 19361 die->has_children = abbrev->has_children;
93311388 19362
3019eac3
DE
19363 /* Make the result usable.
19364 The caller needs to update num_attrs after adding the extra
19365 attributes. */
93311388
DE
19366 die->num_attrs = abbrev->num_attrs;
19367
7a5f294d 19368 bool any_need_reprocess = false;
93311388 19369 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e 19370 {
7a5f294d
TT
19371 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19372 info_ptr);
19373 if (die->attrs[i].requires_reprocessing_p ())
19374 any_need_reprocess = true;
18a8505e
AT
19375 }
19376
052c8bb8 19377 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
529908cb
TT
19378 if (attr != nullptr && attr->form_is_unsigned ())
19379 cu->str_offsets_base = attr->as_unsigned ();
93311388 19380
41144253 19381 attr = die->attr (DW_AT_loclists_base);
19382 if (attr != nullptr)
529908cb 19383 cu->loclist_base = attr->as_unsigned ();
41144253 19384
a39fdb41 19385 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
19386 if (maybe_addr_base.has_value ())
19387 cu->addr_base = *maybe_addr_base;
d0ce17d8
CT
19388
19389 attr = die->attr (DW_AT_rnglists_base);
19390 if (attr != nullptr)
2b0c7f41 19391 cu->rnglists_base = attr->as_unsigned ();
d0ce17d8 19392
7a5f294d
TT
19393 if (any_need_reprocess)
19394 {
19395 for (i = 0; i < abbrev->num_attrs; ++i)
19396 {
19397 if (die->attrs[i].requires_reprocessing_p ())
19398 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19399 }
19400 }
93311388 19401 *diep = die;
93311388
DE
19402 return info_ptr;
19403}
19404
3019eac3
DE
19405/* Read a die and all its attributes.
19406 Set DIEP to point to a newly allocated die with its information,
3e225074 19407 except for its child, sibling, and parent fields. */
3019eac3 19408
d521ce57 19409static const gdb_byte *
3019eac3 19410read_full_die (const struct die_reader_specs *reader,
3e225074 19411 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 19412{
d521ce57 19413 const gdb_byte *result;
bf6af496 19414
3e225074 19415 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 19416
b4f54984 19417 if (dwarf_die_debug)
bf6af496
DE
19418 {
19419 fprintf_unfiltered (gdb_stdlog,
19420 "Read die from %s@0x%x of %s:\n",
96b79293 19421 reader->die_section->get_name (),
bf6af496
DE
19422 (unsigned) (info_ptr - reader->die_section->buffer),
19423 bfd_get_filename (reader->abfd));
b4f54984 19424 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
19425 }
19426
19427 return result;
3019eac3 19428}
433df2d4 19429\f
c906108c 19430
72bf9492
DJ
19431/* Returns nonzero if TAG represents a type that we might generate a partial
19432 symbol for. */
19433
19434static int
d8f62e84 19435is_type_tag_for_partial (int tag, enum language lang)
72bf9492
DJ
19436{
19437 switch (tag)
19438 {
19439#if 0
19440 /* Some types that would be reasonable to generate partial symbols for,
d8f62e84
TT
19441 that we don't at present. Note that normally this does not
19442 matter, mainly because C compilers don't give names to these
19443 types, but instead emit DW_TAG_typedef. */
72bf9492
DJ
19444 case DW_TAG_file_type:
19445 case DW_TAG_ptr_to_member_type:
19446 case DW_TAG_set_type:
19447 case DW_TAG_string_type:
19448 case DW_TAG_subroutine_type:
19449#endif
d8f62e84
TT
19450
19451 /* GNAT may emit an array with a name, but no typedef, so we
19452 need to make a symbol in this case. */
19453 case DW_TAG_array_type:
19454 return lang == language_ada;
19455
72bf9492
DJ
19456 case DW_TAG_base_type:
19457 case DW_TAG_class_type:
680b30c7 19458 case DW_TAG_interface_type:
72bf9492
DJ
19459 case DW_TAG_enumeration_type:
19460 case DW_TAG_structure_type:
19461 case DW_TAG_subrange_type:
19462 case DW_TAG_typedef:
19463 case DW_TAG_union_type:
19464 return 1;
19465 default:
19466 return 0;
19467 }
19468}
19469
19470/* Load all DIEs that are interesting for partial symbols into memory. */
19471
19472static struct partial_die_info *
dee91e82 19473load_partial_dies (const struct die_reader_specs *reader,
d521ce57 19474 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 19475{
dee91e82 19476 struct dwarf2_cu *cu = reader->cu;
5e22e966 19477 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 19478 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 19479 unsigned int bytes_read;
5afb4e99 19480 unsigned int load_all = 0;
72bf9492
DJ
19481 int nesting_level = 1;
19482
19483 parent_die = NULL;
19484 last_die = NULL;
19485
7adf1e79
DE
19486 gdb_assert (cu->per_cu != NULL);
19487 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
19488 load_all = 1;
19489
72bf9492
DJ
19490 cu->partial_dies
19491 = htab_create_alloc_ex (cu->header.length / 12,
19492 partial_die_hash,
19493 partial_die_eq,
19494 NULL,
19495 &cu->comp_unit_obstack,
19496 hashtab_obstack_allocate,
19497 dummy_obstack_deallocate);
19498
72bf9492
DJ
19499 while (1)
19500 {
7c32eebb
TT
19501 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19502 &bytes_read);
72bf9492
DJ
19503
19504 /* A NULL abbrev means the end of a series of children. */
19505 if (abbrev == NULL)
19506 {
19507 if (--nesting_level == 0)
cd9983dd
YQ
19508 return first_die;
19509
72bf9492
DJ
19510 info_ptr += bytes_read;
19511 last_die = parent_die;
19512 parent_die = parent_die->die_parent;
19513 continue;
19514 }
19515
98bfdba5
PA
19516 /* Check for template arguments. We never save these; if
19517 they're seen, we just mark the parent, and go on our way. */
19518 if (parent_die != NULL
19519 && cu->language == language_cplus
19520 && (abbrev->tag == DW_TAG_template_type_param
19521 || abbrev->tag == DW_TAG_template_value_param))
19522 {
19523 parent_die->has_template_arguments = 1;
19524
19525 if (!load_all)
19526 {
19527 /* We don't need a partial DIE for the template argument. */
dee91e82 19528 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19529 continue;
19530 }
19531 }
19532
0d99eb77 19533 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
19534 Skip their other children. */
19535 if (!load_all
19536 && cu->language == language_cplus
19537 && parent_die != NULL
f9b5d5ea
TV
19538 && parent_die->tag == DW_TAG_subprogram
19539 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 19540 {
dee91e82 19541 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19542 continue;
19543 }
19544
5afb4e99
DJ
19545 /* Check whether this DIE is interesting enough to save. Normally
19546 we would not be interested in members here, but there may be
19547 later variables referencing them via DW_AT_specification (for
19548 static members). */
19549 if (!load_all
d8f62e84 19550 && !is_type_tag_for_partial (abbrev->tag, cu->language)
72929c62 19551 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
19552 && abbrev->tag != DW_TAG_enumerator
19553 && abbrev->tag != DW_TAG_subprogram
b1dc1806 19554 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 19555 && abbrev->tag != DW_TAG_lexical_block
72bf9492 19556 && abbrev->tag != DW_TAG_variable
5afb4e99 19557 && abbrev->tag != DW_TAG_namespace
f55ee35c 19558 && abbrev->tag != DW_TAG_module
95554aad 19559 && abbrev->tag != DW_TAG_member
74921315
KS
19560 && abbrev->tag != DW_TAG_imported_unit
19561 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
19562 {
19563 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19564 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
19565 continue;
19566 }
19567
6f06d47b
YQ
19568 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19569 abbrev);
cd9983dd 19570
48fbe735 19571 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
19572
19573 /* This two-pass algorithm for processing partial symbols has a
19574 high cost in cache pressure. Thus, handle some simple cases
19575 here which cover the majority of C partial symbols. DIEs
19576 which neither have specification tags in them, nor could have
19577 specification tags elsewhere pointing at them, can simply be
19578 processed and discarded.
19579
19580 This segment is also optional; scan_partial_symbols and
19581 add_partial_symbol will handle these DIEs if we chain
19582 them in normally. When compilers which do not emit large
19583 quantities of duplicate debug information are more common,
19584 this code can probably be removed. */
19585
19586 /* Any complete simple types at the top level (pretty much all
19587 of them, for a language without namespaces), can be processed
19588 directly. */
19589 if (parent_die == NULL
cd9983dd
YQ
19590 && pdi.has_specification == 0
19591 && pdi.is_declaration == 0
19592 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19593 || pdi.tag == DW_TAG_base_type
d8f62e84 19594 || pdi.tag == DW_TAG_array_type
cd9983dd 19595 || pdi.tag == DW_TAG_subrange_type))
72bf9492 19596 {
7d00ffec 19597 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
19598 add_partial_symbol (&pdi, cu);
19599
cd9983dd 19600 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19601 continue;
19602 }
19603
d8228535
JK
19604 /* The exception for DW_TAG_typedef with has_children above is
19605 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 19606 type_name_or_error will error on such types later.
d8228535
JK
19607
19608 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19609 it could not find the child DIEs referenced later, this is checked
19610 above. In correct DWARF DW_TAG_typedef should have no children. */
19611
cd9983dd 19612 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 19613 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 19614 "- DIE at %s [in module %s]"),
cd9983dd 19615 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 19616
72bf9492
DJ
19617 /* If we're at the second level, and we're an enumerator, and
19618 our parent has no specification (meaning possibly lives in a
19619 namespace elsewhere), then we can add the partial symbol now
19620 instead of queueing it. */
cd9983dd 19621 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
19622 && parent_die != NULL
19623 && parent_die->die_parent == NULL
19624 && parent_die->tag == DW_TAG_enumeration_type
19625 && parent_die->has_specification == 0)
19626 {
7d00ffec 19627 if (pdi.raw_name == NULL)
b98664d3 19628 complaint (_("malformed enumerator DIE ignored"));
72bf9492 19629 else if (building_psymtab)
f0fbb768 19630 add_partial_symbol (&pdi, cu);
72bf9492 19631
cd9983dd 19632 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19633 continue;
19634 }
19635
cd9983dd 19636 struct partial_die_info *part_die
6f06d47b 19637 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 19638
72bf9492
DJ
19639 /* We'll save this DIE so link it in. */
19640 part_die->die_parent = parent_die;
19641 part_die->die_sibling = NULL;
19642 part_die->die_child = NULL;
19643
19644 if (last_die && last_die == parent_die)
19645 last_die->die_child = part_die;
19646 else if (last_die)
19647 last_die->die_sibling = part_die;
19648
19649 last_die = part_die;
19650
19651 if (first_die == NULL)
19652 first_die = part_die;
19653
19654 /* Maybe add the DIE to the hash table. Not all DIEs that we
19655 find interesting need to be in the hash table, because we
19656 also have the parent/sibling/child chains; only those that we
19657 might refer to by offset later during partial symbol reading.
19658
19659 For now this means things that might have be the target of a
19660 DW_AT_specification, DW_AT_abstract_origin, or
19661 DW_AT_extension. DW_AT_extension will refer only to
19662 namespaces; DW_AT_abstract_origin refers to functions (and
19663 many things under the function DIE, but we do not recurse
19664 into function DIEs during partial symbol reading) and
19665 possibly variables as well; DW_AT_specification refers to
19666 declarations. Declarations ought to have the DW_AT_declaration
19667 flag. It happens that GCC forgets to put it in sometimes, but
19668 only for functions, not for types.
19669
19670 Adding more things than necessary to the hash table is harmless
19671 except for the performance cost. Adding too few will result in
5afb4e99
DJ
19672 wasted time in find_partial_die, when we reread the compilation
19673 unit with load_all_dies set. */
72bf9492 19674
5afb4e99 19675 if (load_all
72929c62 19676 || abbrev->tag == DW_TAG_constant
5afb4e99 19677 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
19678 || abbrev->tag == DW_TAG_variable
19679 || abbrev->tag == DW_TAG_namespace
19680 || part_die->is_declaration)
19681 {
19682 void **slot;
19683
19684 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
19685 to_underlying (part_die->sect_off),
19686 INSERT);
72bf9492
DJ
19687 *slot = part_die;
19688 }
19689
72bf9492 19690 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 19691 we have no reason to follow the children of structures; for other
98bfdba5
PA
19692 languages we have to, so that we can get at method physnames
19693 to infer fully qualified class names, for DW_AT_specification,
19694 and for C++ template arguments. For C++, we also look one level
19695 inside functions to find template arguments (if the name of the
19696 function does not already contain the template arguments).
bc30ff58 19697
0a4b0913
AB
19698 For Ada and Fortran, we need to scan the children of subprograms
19699 and lexical blocks as well because these languages allow the
19700 definition of nested entities that could be interesting for the
19701 debugger, such as nested subprograms for instance. */
72bf9492 19702 if (last_die->has_children
5afb4e99
DJ
19703 && (load_all
19704 || last_die->tag == DW_TAG_namespace
f55ee35c 19705 || last_die->tag == DW_TAG_module
72bf9492 19706 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
19707 || (cu->language == language_cplus
19708 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
19709 && (last_die->raw_name == NULL
19710 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
19711 || (cu->language != language_c
19712 && (last_die->tag == DW_TAG_class_type
680b30c7 19713 || last_die->tag == DW_TAG_interface_type
72bf9492 19714 || last_die->tag == DW_TAG_structure_type
bc30ff58 19715 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
19716 || ((cu->language == language_ada
19717 || cu->language == language_fortran)
bc30ff58
JB
19718 && (last_die->tag == DW_TAG_subprogram
19719 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
19720 {
19721 nesting_level++;
19722 parent_die = last_die;
19723 continue;
19724 }
19725
19726 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19727 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
19728
19729 /* Back to the top, do it again. */
19730 }
19731}
19732
6f06d47b 19733partial_die_info::partial_die_info (sect_offset sect_off_,
7c32eebb 19734 const struct abbrev_info *abbrev)
6f06d47b
YQ
19735 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19736{
19737}
19738
7d00ffec
TT
19739/* See class definition. */
19740
19741const char *
19742partial_die_info::name (dwarf2_cu *cu)
19743{
19744 if (!canonical_name && raw_name != nullptr)
19745 {
19746 struct objfile *objfile = cu->per_objfile->objfile;
19747 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19748 canonical_name = 1;
19749 }
19750
19751 return raw_name;
19752}
19753
35cc7ed7
YQ
19754/* Read a minimal amount of information into the minimal die structure.
19755 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 19756
48fbe735
YQ
19757const gdb_byte *
19758partial_die_info::read (const struct die_reader_specs *reader,
19759 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 19760{
dee91e82 19761 struct dwarf2_cu *cu = reader->cu;
976ca316 19762 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 19763 unsigned int i;
c5aa993b 19764 int has_low_pc_attr = 0;
c906108c 19765 int has_high_pc_attr = 0;
91da1414 19766 int high_pc_relative = 0;
c906108c 19767
fd0a254f 19768 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 19769 {
e7da7f8f 19770 attribute attr;
7a5f294d 19771 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18a8505e 19772 /* String and address offsets that need to do the reprocessing have
dda83cd7 19773 already been read at this point, so there is no need to wait until
18a8505e 19774 the loop terminates to do the reprocessing. */
7a5f294d 19775 if (attr.requires_reprocessing_p ())
d0ce17d8 19776 read_attribute_reprocess (reader, &attr, tag);
c906108c 19777 /* Store the data if it is of an attribute we want to keep in a
dda83cd7 19778 partial symbol table. */
c906108c
SS
19779 switch (attr.name)
19780 {
19781 case DW_AT_name:
48fbe735 19782 switch (tag)
71c25dea
TT
19783 {
19784 case DW_TAG_compile_unit:
95554aad 19785 case DW_TAG_partial_unit:
348e048f 19786 case DW_TAG_type_unit:
71c25dea
TT
19787 /* Compilation units have a DW_AT_name that is a filename, not
19788 a source language identifier. */
19789 case DW_TAG_enumeration_type:
19790 case DW_TAG_enumerator:
19791 /* These tags always have simple identifiers already; no need
19792 to canonicalize them. */
7d00ffec 19793 canonical_name = 1;
2c830f54 19794 raw_name = attr.as_string ();
71c25dea
TT
19795 break;
19796 default:
7d00ffec 19797 canonical_name = 0;
2c830f54 19798 raw_name = attr.as_string ();
71c25dea
TT
19799 break;
19800 }
c906108c 19801 break;
31ef98ae 19802 case DW_AT_linkage_name:
c906108c 19803 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19804 /* Note that both forms of linkage name might appear. We
19805 assume they will be the same, and we only store the last
19806 one we see. */
95f982e5 19807 linkage_name = attr.as_string ();
c906108c
SS
19808 break;
19809 case DW_AT_low_pc:
19810 has_low_pc_attr = 1;
95f982e5 19811 lowpc = attr.as_address ();
c906108c
SS
19812 break;
19813 case DW_AT_high_pc:
19814 has_high_pc_attr = 1;
95f982e5 19815 highpc = attr.as_address ();
cd6c91b4 19816 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 19817 high_pc_relative = 1;
c906108c
SS
19818 break;
19819 case DW_AT_location:
dda83cd7
SM
19820 /* Support the .debug_loc offsets. */
19821 if (attr.form_is_block ())
19822 {
9d2246fc 19823 d.locdesc = attr.as_block ();
dda83cd7
SM
19824 }
19825 else if (attr.form_is_section_offset ())
19826 {
4d3c2250 19827 dwarf2_complex_location_expr_complaint ();
dda83cd7
SM
19828 }
19829 else
19830 {
4d3c2250
KB
19831 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19832 "partial symbol information");
dda83cd7 19833 }
c906108c 19834 break;
c906108c 19835 case DW_AT_external:
c45bc3f8 19836 is_external = attr.as_boolean ();
c906108c
SS
19837 break;
19838 case DW_AT_declaration:
c45bc3f8 19839 is_declaration = attr.as_boolean ();
c906108c
SS
19840 break;
19841 case DW_AT_type:
48fbe735 19842 has_type = 1;
c906108c
SS
19843 break;
19844 case DW_AT_abstract_origin:
19845 case DW_AT_specification:
72bf9492 19846 case DW_AT_extension:
48fbe735 19847 has_specification = 1;
0826b30a 19848 spec_offset = attr.get_ref_die_offset ();
48fbe735 19849 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19850 || cu->per_cu->is_dwz);
c906108c
SS
19851 break;
19852 case DW_AT_sibling:
19853 /* Ignore absolute siblings, they might point outside of
19854 the current compile unit. */
19855 if (attr.form == DW_FORM_ref_addr)
b98664d3 19856 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19857 else
b9502d3f 19858 {
48fbe735 19859 const gdb_byte *buffer = reader->buffer;
0826b30a 19860 sect_offset off = attr.get_ref_die_offset ();
9c541725 19861 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19862
19863 if (sibling_ptr < info_ptr)
b98664d3 19864 complaint (_("DW_AT_sibling points backwards"));
22869d73 19865 else if (sibling_ptr > reader->buffer_end)
a0194fa8 19866 reader->die_section->overflow_complaint ();
b9502d3f 19867 else
48fbe735 19868 sibling = sibling_ptr;
b9502d3f 19869 }
c906108c 19870 break;
dda83cd7
SM
19871 case DW_AT_byte_size:
19872 has_byte_size = 1;
19873 break;
19874 case DW_AT_const_value:
19875 has_const_value = 1;
19876 break;
68511cec
CES
19877 case DW_AT_calling_convention:
19878 /* DWARF doesn't provide a way to identify a program's source-level
19879 entry point. DW_AT_calling_convention attributes are only meant
19880 to describe functions' calling conventions.
19881
19882 However, because it's a necessary piece of information in
0c1b455e
TT
19883 Fortran, and before DWARF 4 DW_CC_program was the only
19884 piece of debugging information whose definition refers to
19885 a 'main program' at all, several compilers marked Fortran
19886 main programs with DW_CC_program --- even when those
19887 functions use the standard calling conventions.
19888
19889 Although DWARF now specifies a way to provide this
19890 information, we support this practice for backward
19891 compatibility. */
529908cb 19892 if (attr.constant_value (0) == DW_CC_program
0c1b455e 19893 && cu->language == language_fortran)
48fbe735 19894 main_subprogram = 1;
68511cec 19895 break;
481860b3 19896 case DW_AT_inline:
529908cb
TT
19897 {
19898 LONGEST value = attr.constant_value (-1);
19899 if (value == DW_INL_inlined
19900 || value == DW_INL_declared_inlined)
19901 may_be_inlined = 1;
19902 }
481860b3 19903 break;
95554aad
TT
19904
19905 case DW_AT_import:
48fbe735 19906 if (tag == DW_TAG_imported_unit)
36586728 19907 {
0826b30a 19908 d.sect_off = attr.get_ref_die_offset ();
48fbe735 19909 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19910 || cu->per_cu->is_dwz);
19911 }
95554aad
TT
19912 break;
19913
0c1b455e 19914 case DW_AT_main_subprogram:
c45bc3f8 19915 main_subprogram = attr.as_boolean ();
0c1b455e
TT
19916 break;
19917
05caa1d2
TT
19918 case DW_AT_ranges:
19919 {
2b0c7f41
SM
19920 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19921 on DWARF version). */
19922 ULONGEST ranges_offset = attr.as_unsigned ();
19923
19924 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19925 this value. */
19926 if (tag != DW_TAG_compile_unit)
19927 ranges_offset += cu->gnu_ranges_base;
19928
05caa1d2 19929 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
d0ce17d8 19930 nullptr, tag))
05caa1d2
TT
19931 has_pc_info = 1;
19932 }
19933 break;
19934
c906108c
SS
19935 default:
19936 break;
19937 }
19938 }
19939
10d06d82
TT
19940 /* For Ada, if both the name and the linkage name appear, we prefer
19941 the latter. This lets "catch exception" work better, regardless
19942 of the order in which the name and linkage name were emitted.
19943 Really, though, this is just a workaround for the fact that gdb
19944 doesn't store both the name and the linkage name. */
19945 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 19946 raw_name = linkage_name;
10d06d82 19947
91da1414 19948 if (high_pc_relative)
48fbe735 19949 highpc += lowpc;
91da1414 19950
9373cf26
JK
19951 if (has_low_pc_attr && has_high_pc_attr)
19952 {
19953 /* When using the GNU linker, .gnu.linkonce. sections are used to
19954 eliminate duplicate copies of functions and vtables and such.
19955 The linker will arbitrarily choose one and discard the others.
19956 The AT_*_pc values for such functions refer to local labels in
19957 these sections. If the section from that file was discarded, the
19958 labels are not in the output, so the relocs get a value of 0.
19959 If this is a discarded function, mark the pc bounds as invalid,
19960 so that GDB will ignore it. */
976ca316 19961 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 19962 {
976ca316 19963 struct objfile *objfile = per_objfile->objfile;
08feed99 19964 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19965
b98664d3 19966 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19967 "for DIE at %s [in module %s]"),
48fbe735
YQ
19968 paddress (gdbarch, lowpc),
19969 sect_offset_str (sect_off),
9d8780f0 19970 objfile_name (objfile));
9373cf26
JK
19971 }
19972 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19973 else if (lowpc >= highpc)
9373cf26 19974 {
976ca316 19975 struct objfile *objfile = per_objfile->objfile;
08feed99 19976 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19977
b98664d3 19978 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19979 "for DIE at %s [in module %s]"),
48fbe735
YQ
19980 paddress (gdbarch, lowpc),
19981 paddress (gdbarch, highpc),
19982 sect_offset_str (sect_off),
9c541725 19983 objfile_name (objfile));
9373cf26
JK
19984 }
19985 else
48fbe735 19986 has_pc_info = 1;
9373cf26 19987 }
85cbf3d3 19988
c906108c
SS
19989 return info_ptr;
19990}
19991
72bf9492
DJ
19992/* Find a cached partial DIE at OFFSET in CU. */
19993
d590ff25
YQ
19994struct partial_die_info *
19995dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19996{
19997 struct partial_die_info *lookup_die = NULL;
6f06d47b 19998 struct partial_die_info part_die (sect_off);
72bf9492 19999
9a3c8263 20000 lookup_die = ((struct partial_die_info *)
d590ff25 20001 htab_find_with_hash (partial_dies, &part_die,
9c541725 20002 to_underlying (sect_off)));
72bf9492 20003
72bf9492
DJ
20004 return lookup_die;
20005}
20006
348e048f
DE
20007/* Find a partial DIE at OFFSET, which may or may not be in CU,
20008 except in the case of .debug_types DIEs which do not reference
20009 outside their CU (they do however referencing other types via
55f1336d 20010 DW_FORM_ref_sig8). */
72bf9492 20011
122cf0f2 20012static const struct cu_partial_die_info
9c541725 20013find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 20014{
976ca316
SM
20015 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20016 struct objfile *objfile = per_objfile->objfile;
5afb4e99 20017 struct partial_die_info *pd = NULL;
72bf9492 20018
36586728 20019 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 20020 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 20021 {
d590ff25 20022 pd = cu->find_partial_die (sect_off);
5afb4e99 20023 if (pd != NULL)
fb816e8b 20024 return { cu, pd };
0d99eb77
DE
20025 /* We missed recording what we needed.
20026 Load all dies and try again. */
5afb4e99 20027 }
0d99eb77
DE
20028 else
20029 {
20030 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 20031 if (cu->per_cu->is_debug_types)
0d99eb77 20032 {
9d8780f0
SM
20033 error (_("Dwarf Error: Type Unit at offset %s contains"
20034 " external reference to offset %s [in module %s].\n"),
20035 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
20036 bfd_get_filename (objfile->obfd));
20037 }
7188ed02
SM
20038 dwarf2_per_cu_data *per_cu
20039 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 20040 per_objfile);
72bf9492 20041
976ca316 20042 cu = per_objfile->get_cu (per_cu);
7188ed02 20043 if (cu == NULL || cu->partial_dies == NULL)
976ca316 20044 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 20045
976ca316 20046 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
20047
20048 cu->last_used = 0;
20049 pd = cu->find_partial_die (sect_off);
0d99eb77 20050 }
5afb4e99 20051
dee91e82
DE
20052 /* If we didn't find it, and not all dies have been loaded,
20053 load them all and try again. */
20054
7188ed02 20055 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 20056 {
7188ed02 20057 cu->per_cu->load_all_dies = 1;
fd820528
DE
20058
20059 /* This is nasty. When we reread the DIEs, somewhere up the call chain
20060 THIS_CU->cu may already be in use. So we can't just free it and
20061 replace its DIEs with the ones we read in. Instead, we leave those
20062 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
20063 and clobber THIS_CU->cu->partial_dies with the hash table for the new
20064 set. */
976ca316 20065 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 20066
7188ed02 20067 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
20068 }
20069
20070 if (pd == NULL)
521894aa 20071 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
9d8780f0 20072 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 20073 return { cu, pd };
72bf9492
DJ
20074}
20075
abc72ce4
DE
20076/* See if we can figure out if the class lives in a namespace. We do
20077 this by looking for a member function; its demangled name will
20078 contain namespace info, if there is any. */
20079
20080static void
20081guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
20082 struct dwarf2_cu *cu)
20083{
20084 /* NOTE: carlton/2003-10-07: Getting the info this way changes
20085 what template types look like, because the demangler
20086 frequently doesn't give the same name as the debug info. We
20087 could fix this by only using the demangled name to get the
20088 prefix (but see comment in read_structure_type). */
20089
20090 struct partial_die_info *real_pdi;
20091 struct partial_die_info *child_pdi;
20092
20093 /* If this DIE (this DIE's specification, if any) has a parent, then
20094 we should not do this. We'll prepend the parent's fully qualified
20095 name when we create the partial symbol. */
20096
20097 real_pdi = struct_pdi;
20098 while (real_pdi->has_specification)
fb816e8b 20099 {
122cf0f2
AB
20100 auto res = find_partial_die (real_pdi->spec_offset,
20101 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
20102 real_pdi = res.pdi;
20103 cu = res.cu;
20104 }
abc72ce4
DE
20105
20106 if (real_pdi->die_parent != NULL)
20107 return;
20108
20109 for (child_pdi = struct_pdi->die_child;
20110 child_pdi != NULL;
20111 child_pdi = child_pdi->die_sibling)
20112 {
20113 if (child_pdi->tag == DW_TAG_subprogram
20114 && child_pdi->linkage_name != NULL)
20115 {
43816ebc 20116 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
20117 (cu->language_defn->class_name_from_physname
20118 (child_pdi->linkage_name));
abc72ce4
DE
20119 if (actual_class_name != NULL)
20120 {
5e22e966 20121 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
20122 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
20123 struct_pdi->canonical_name = 1;
abc72ce4
DE
20124 }
20125 break;
20126 }
20127 }
20128}
20129
25c11aca
TV
20130/* Return true if a DIE with TAG may have the DW_AT_const_value
20131 attribute. */
20132
20133static bool
20134can_have_DW_AT_const_value_p (enum dwarf_tag tag)
20135{
20136 switch (tag)
20137 {
20138 case DW_TAG_constant:
20139 case DW_TAG_enumerator:
20140 case DW_TAG_formal_parameter:
20141 case DW_TAG_template_value_param:
20142 case DW_TAG_variable:
20143 return true;
20144 }
20145
20146 return false;
20147}
20148
52356b79
YQ
20149void
20150partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 20151{
abc72ce4
DE
20152 /* Once we've fixed up a die, there's no point in doing so again.
20153 This also avoids a memory leak if we were to call
20154 guess_partial_die_structure_name multiple times. */
52356b79 20155 if (fixup_called)
abc72ce4
DE
20156 return;
20157
72bf9492
DJ
20158 /* If we found a reference attribute and the DIE has no name, try
20159 to find a name in the referred to DIE. */
20160
7d00ffec 20161 if (raw_name == NULL && has_specification)
72bf9492
DJ
20162 {
20163 struct partial_die_info *spec_die;
72bf9492 20164
122cf0f2 20165 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
20166 spec_die = res.pdi;
20167 cu = res.cu;
72bf9492 20168
52356b79 20169 spec_die->fixup (cu);
72bf9492 20170
7d00ffec 20171 if (spec_die->raw_name)
72bf9492 20172 {
7d00ffec
TT
20173 raw_name = spec_die->raw_name;
20174 canonical_name = spec_die->canonical_name;
72bf9492
DJ
20175
20176 /* Copy DW_AT_external attribute if it is set. */
20177 if (spec_die->is_external)
52356b79 20178 is_external = spec_die->is_external;
72bf9492
DJ
20179 }
20180 }
20181
25c11aca
TV
20182 if (!has_const_value && has_specification
20183 && can_have_DW_AT_const_value_p (tag))
20184 {
20185 struct partial_die_info *spec_die;
20186
20187 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20188 spec_die = res.pdi;
20189 cu = res.cu;
20190
20191 spec_die->fixup (cu);
20192
20193 if (spec_die->has_const_value)
20194 {
20195 /* Copy DW_AT_const_value attribute if it is set. */
20196 has_const_value = spec_die->has_const_value;
20197 }
20198 }
20199
72bf9492 20200 /* Set default names for some unnamed DIEs. */
72bf9492 20201
7d00ffec
TT
20202 if (raw_name == NULL && tag == DW_TAG_namespace)
20203 {
20204 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20205 canonical_name = 1;
20206 }
72bf9492 20207
abc72ce4
DE
20208 /* If there is no parent die to provide a namespace, and there are
20209 children, see if we can determine the namespace from their linkage
122d1940 20210 name. */
abc72ce4 20211 if (cu->language == language_cplus
5e22e966 20212 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
20213 && die_parent == NULL
20214 && has_children
20215 && (tag == DW_TAG_class_type
20216 || tag == DW_TAG_structure_type
20217 || tag == DW_TAG_union_type))
20218 guess_partial_die_structure_name (this, cu);
abc72ce4 20219
53832f31
TT
20220 /* GCC might emit a nameless struct or union that has a linkage
20221 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 20222 if (raw_name == NULL
52356b79
YQ
20223 && (tag == DW_TAG_class_type
20224 || tag == DW_TAG_interface_type
20225 || tag == DW_TAG_structure_type
20226 || tag == DW_TAG_union_type)
20227 && linkage_name != NULL)
53832f31 20228 {
43816ebc
TT
20229 gdb::unique_xmalloc_ptr<char> demangled
20230 (gdb_demangle (linkage_name, DMGL_TYPES));
20231 if (demangled != nullptr)
53832f31 20232 {
96408a79
SA
20233 const char *base;
20234
20235 /* Strip any leading namespaces/classes, keep only the base name.
20236 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
20237 base = strrchr (demangled.get (), ':');
20238 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
20239 base++;
20240 else
43816ebc 20241 base = demangled.get ();
96408a79 20242
5e22e966 20243 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
20244 raw_name = objfile->intern (base);
20245 canonical_name = 1;
53832f31
TT
20246 }
20247 }
20248
52356b79 20249 fixup_called = 1;
72bf9492
DJ
20250}
20251
d0ce17d8 20252/* Read the .debug_loclists or .debug_rnglists header (they are the same format)
a1c40103
SM
20253 contents from the given SECTION in the HEADER.
20254
20255 HEADER_OFFSET is the offset of the header in the section. */
41144253 20256static void
d0ce17d8 20257read_loclists_rnglists_header (struct loclists_rnglists_header *header,
a1c40103
SM
20258 struct dwarf2_section_info *section,
20259 sect_offset header_offset)
41144253 20260{
20261 unsigned int bytes_read;
20262 bfd *abfd = section->get_bfd_owner ();
a1c40103
SM
20263 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20264
41144253 20265 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20266 info_ptr += bytes_read;
a1c40103 20267
41144253 20268 header->version = read_2_bytes (abfd, info_ptr);
20269 info_ptr += 2;
a1c40103 20270
41144253 20271 header->addr_size = read_1_byte (abfd, info_ptr);
20272 info_ptr += 1;
a1c40103 20273
41144253 20274 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20275 info_ptr += 1;
a1c40103 20276
41144253 20277 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20278}
20279
20280/* Return the DW_AT_loclists_base value for the CU. */
20281static ULONGEST
20282lookup_loclist_base (struct dwarf2_cu *cu)
20283{
20284 /* For the .dwo unit, the loclist_base points to the first offset following
20285 the header. The header consists of the following entities-
20286 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20287 bit format)
20288 2. version (2 bytes)
20289 3. address size (1 byte)
20290 4. segment selector size (1 byte)
20291 5. offset entry count (4 bytes)
20292 These sizes are derived as per the DWARFv5 standard. */
20293 if (cu->dwo_unit != nullptr)
20294 {
20295 if (cu->header.initial_length_size == 4)
20296 return LOCLIST_HEADER_SIZE32;
20297 return LOCLIST_HEADER_SIZE64;
20298 }
20299 return cu->loclist_base;
20300}
20301
20302/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20303 array of offsets in the .debug_loclists section. */
e57933dc
SM
20304
20305static sect_offset
41144253 20306read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20307{
976ca316
SM
20308 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20309 struct objfile *objfile = per_objfile->objfile;
41144253 20310 bfd *abfd = objfile->obfd;
a1c40103
SM
20311 ULONGEST loclist_header_size =
20312 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20313 : LOCLIST_HEADER_SIZE64);
41144253 20314 ULONGEST loclist_base = lookup_loclist_base (cu);
05787bad
SM
20315
20316 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20317 ULONGEST start_offset =
20318 loclist_base + loclist_index * cu->header.offset_size;
20319
a1c40103 20320 /* Get loclists section. */
41144253 20321 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20322
a1c40103 20323 /* Read the loclists section content. */
41144253 20324 section->read (objfile);
20325 if (section->buffer == NULL)
a0c1eeba
SM
20326 error (_("DW_FORM_loclistx used without .debug_loclists "
20327 "section [in module %s]"), objfile_name (objfile));
20328
a1c40103
SM
20329 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20330 so if loclist_base is smaller than the header size, we have a problem. */
20331 if (loclist_base < loclist_header_size)
20332 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20333 objfile_name (objfile));
20334
20335 /* Read the header of the loclists contribution. */
d0ce17d8 20336 struct loclists_rnglists_header header;
a1c40103
SM
20337 read_loclists_rnglists_header (&header, section,
20338 (sect_offset) (loclist_base - loclist_header_size));
20339
20340 /* Verify the loclist index is valid. */
41144253 20341 if (loclist_index >= header.offset_entry_count)
a0c1eeba
SM
20342 error (_("DW_FORM_loclistx pointing outside of "
20343 ".debug_loclists offset array [in module %s]"),
20344 objfile_name (objfile));
20345
05787bad
SM
20346 /* Validate that reading won't go beyond the end of the section. */
20347 if (start_offset + cu->header.offset_size > section->size)
20348 error (_("Reading DW_FORM_loclistx index beyond end of"
20349 ".debug_loclists section [in module %s]"),
20350 objfile_name (objfile));
20351
20352 const gdb_byte *info_ptr = section->buffer + start_offset;
41144253 20353
20354 if (cu->header.offset_size == 4)
e57933dc 20355 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
41144253 20356 else
e57933dc 20357 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
41144253 20358}
20359
d0ce17d8
CT
20360/* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20361 array of offsets in the .debug_rnglists section. */
e57933dc
SM
20362
20363static sect_offset
d0ce17d8
CT
20364read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20365 dwarf_tag tag)
20366{
20367 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20368 struct objfile *objfile = dwarf2_per_objfile->objfile;
20369 bfd *abfd = objfile->obfd;
20370 ULONGEST rnglist_header_size =
20371 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20372 : RNGLIST_HEADER_SIZE64);
2b0c7f41
SM
20373
20374 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20375 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20376 doesn't apply. */
d0ce17d8 20377 ULONGEST rnglist_base =
2b0c7f41 20378 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
5e4d9bbc
SM
20379
20380 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
d0ce17d8
CT
20381 ULONGEST start_offset =
20382 rnglist_base + rnglist_index * cu->header.offset_size;
20383
20384 /* Get rnglists section. */
20385 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20386
20387 /* Read the rnglists section content. */
20388 section->read (objfile);
20389 if (section->buffer == nullptr)
20390 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20391 "[in module %s]"),
20392 objfile_name (objfile));
20393
a1c40103
SM
20394 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20395 so if rnglist_base is smaller than the header size, we have a problem. */
20396 if (rnglist_base < rnglist_header_size)
20397 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20398 objfile_name (objfile));
20399
20400 /* Read the header of the rnglists contribution. */
d0ce17d8 20401 struct loclists_rnglists_header header;
a1c40103
SM
20402 read_loclists_rnglists_header (&header, section,
20403 (sect_offset) (rnglist_base - rnglist_header_size));
20404
20405 /* Verify the rnglist index is valid. */
d0ce17d8
CT
20406 if (rnglist_index >= header.offset_entry_count)
20407 error (_("DW_FORM_rnglistx index pointing outside of "
20408 ".debug_rnglists offset array [in module %s]"),
20409 objfile_name (objfile));
20410
d0ce17d8 20411 /* Validate that reading won't go beyond the end of the section. */
5e4d9bbc 20412 if (start_offset + cu->header.offset_size > section->size)
d0ce17d8
CT
20413 error (_("Reading DW_FORM_rnglistx index beyond end of"
20414 ".debug_rnglists section [in module %s]"),
20415 objfile_name (objfile));
20416
20417 const gdb_byte *info_ptr = section->buffer + start_offset;
20418
20419 if (cu->header.offset_size == 4)
e57933dc 20420 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8 20421 else
e57933dc 20422 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8
CT
20423}
20424
18a8505e
AT
20425/* Process the attributes that had to be skipped in the first round. These
20426 attributes are the ones that need str_offsets_base or addr_base attributes.
20427 They could not have been processed in the first round, because at the time
20428 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
20429static void
20430read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 20431 struct attribute *attr, dwarf_tag tag)
18a8505e
AT
20432{
20433 struct dwarf2_cu *cu = reader->cu;
20434 switch (attr->form)
20435 {
20436 case DW_FORM_addrx:
20437 case DW_FORM_GNU_addr_index:
36d378cf
TT
20438 attr->set_address (read_addr_index (cu,
20439 attr->as_unsigned_reprocess ()));
dda83cd7 20440 break;
41144253 20441 case DW_FORM_loclistx:
b1829e1b 20442 {
e57933dc 20443 sect_offset loclists_sect_off
b1829e1b
SM
20444 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20445
e57933dc 20446 attr->set_unsigned (to_underlying (loclists_sect_off));
b1829e1b
SM
20447 }
20448 break;
d0ce17d8 20449 case DW_FORM_rnglistx:
b1829e1b 20450 {
e57933dc 20451 sect_offset rnglists_sect_off
b1829e1b
SM
20452 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20453
e57933dc 20454 attr->set_unsigned (to_underlying (rnglists_sect_off));
b1829e1b 20455 }
dda83cd7 20456 break;
18a8505e
AT
20457 case DW_FORM_strx:
20458 case DW_FORM_strx1:
20459 case DW_FORM_strx2:
20460 case DW_FORM_strx3:
20461 case DW_FORM_strx4:
20462 case DW_FORM_GNU_str_index:
20463 {
fe56917a 20464 unsigned int str_index = attr->as_unsigned_reprocess ();
c6481205 20465 gdb_assert (!attr->canonical_string_p ());
18a8505e 20466 if (reader->dwo_file != NULL)
c6481205
TT
20467 attr->set_string_noncanonical (read_dwo_str_index (reader,
20468 str_index));
18a8505e 20469 else
c6481205
TT
20470 attr->set_string_noncanonical (read_stub_str_index (cu,
20471 str_index));
18a8505e
AT
20472 break;
20473 }
20474 default:
20475 gdb_assert_not_reached (_("Unexpected DWARF form."));
20476 }
20477}
20478
a8329558 20479/* Read an attribute value described by an attribute form. */
c906108c 20480
d521ce57 20481static const gdb_byte *
dee91e82
DE
20482read_attribute_value (const struct die_reader_specs *reader,
20483 struct attribute *attr, unsigned form,
7a5f294d 20484 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 20485{
dee91e82 20486 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
20487 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20488 struct objfile *objfile = per_objfile->objfile;
dee91e82 20489 bfd *abfd = reader->abfd;
e7c27a73 20490 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
20491 unsigned int bytes_read;
20492 struct dwarf_block *blk;
20493
aead7601 20494 attr->form = (enum dwarf_form) form;
a8329558 20495 switch (form)
c906108c 20496 {
c906108c 20497 case DW_FORM_ref_addr:
7c290a04
TT
20498 if (cu_header->version == 2)
20499 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
414ad644 20500 &bytes_read));
ae411497 20501 else
7c290a04 20502 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20503 &bytes_read));
ae411497
TT
20504 info_ptr += bytes_read;
20505 break;
36586728 20506 case DW_FORM_GNU_ref_alt:
7c290a04 20507 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20508 &bytes_read));
36586728
TT
20509 info_ptr += bytes_read;
20510 break;
ae411497 20511 case DW_FORM_addr:
08feed99
TT
20512 {
20513 struct gdbarch *gdbarch = objfile->arch ();
7c290a04 20514 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
36d378cf
TT
20515 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20516 attr->set_address (addr);
08feed99
TT
20517 info_ptr += bytes_read;
20518 }
c906108c
SS
20519 break;
20520 case DW_FORM_block2:
7b5a2f43 20521 blk = dwarf_alloc_block (cu);
c906108c
SS
20522 blk->size = read_2_bytes (abfd, info_ptr);
20523 info_ptr += 2;
20524 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20525 info_ptr += blk->size;
9d2246fc 20526 attr->set_block (blk);
c906108c
SS
20527 break;
20528 case DW_FORM_block4:
7b5a2f43 20529 blk = dwarf_alloc_block (cu);
c906108c
SS
20530 blk->size = read_4_bytes (abfd, info_ptr);
20531 info_ptr += 4;
20532 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20533 info_ptr += blk->size;
9d2246fc 20534 attr->set_block (blk);
c906108c
SS
20535 break;
20536 case DW_FORM_data2:
414ad644 20537 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
c906108c
SS
20538 info_ptr += 2;
20539 break;
20540 case DW_FORM_data4:
414ad644 20541 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
c906108c
SS
20542 info_ptr += 4;
20543 break;
20544 case DW_FORM_data8:
414ad644 20545 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
c906108c
SS
20546 info_ptr += 8;
20547 break;
0224619f
JK
20548 case DW_FORM_data16:
20549 blk = dwarf_alloc_block (cu);
20550 blk->size = 16;
20551 blk->data = read_n_bytes (abfd, info_ptr, 16);
20552 info_ptr += 16;
9d2246fc 20553 attr->set_block (blk);
0224619f 20554 break;
2dc7f7b3 20555 case DW_FORM_sec_offset:
7c290a04 20556 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20557 &bytes_read));
2dc7f7b3
TT
20558 info_ptr += bytes_read;
20559 break;
41144253 20560 case DW_FORM_loclistx:
20561 {
7a5f294d
TT
20562 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20563 &bytes_read));
20564 info_ptr += bytes_read;
41144253 20565 }
20566 break;
c906108c 20567 case DW_FORM_string:
c6481205
TT
20568 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20569 &bytes_read));
c906108c
SS
20570 info_ptr += bytes_read;
20571 break;
4bdf3d34 20572 case DW_FORM_strp:
36586728
TT
20573 if (!cu->per_cu->is_dwz)
20574 {
c6481205
TT
20575 attr->set_string_noncanonical
20576 (read_indirect_string (per_objfile,
20577 abfd, info_ptr, cu_header,
20578 &bytes_read));
36586728
TT
20579 info_ptr += bytes_read;
20580 break;
20581 }
20582 /* FALLTHROUGH */
43988095
JK
20583 case DW_FORM_line_strp:
20584 if (!cu->per_cu->is_dwz)
20585 {
c6481205
TT
20586 attr->set_string_noncanonical
20587 (per_objfile->read_line_string (info_ptr, cu_header,
20588 &bytes_read));
43988095
JK
20589 info_ptr += bytes_read;
20590 break;
20591 }
20592 /* FALLTHROUGH */
36586728
TT
20593 case DW_FORM_GNU_strp_alt:
20594 {
a7308ce0 20595 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
8266302d
TT
20596 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20597 &bytes_read);
36586728 20598
c6481205
TT
20599 attr->set_string_noncanonical
20600 (dwz->read_string (objfile, str_offset));
36586728
TT
20601 info_ptr += bytes_read;
20602 }
4bdf3d34 20603 break;
2dc7f7b3 20604 case DW_FORM_exprloc:
c906108c 20605 case DW_FORM_block:
7b5a2f43 20606 blk = dwarf_alloc_block (cu);
c906108c
SS
20607 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20608 info_ptr += bytes_read;
20609 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20610 info_ptr += blk->size;
9d2246fc 20611 attr->set_block (blk);
c906108c
SS
20612 break;
20613 case DW_FORM_block1:
7b5a2f43 20614 blk = dwarf_alloc_block (cu);
c906108c
SS
20615 blk->size = read_1_byte (abfd, info_ptr);
20616 info_ptr += 1;
20617 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20618 info_ptr += blk->size;
9d2246fc 20619 attr->set_block (blk);
c906108c
SS
20620 break;
20621 case DW_FORM_data1:
c906108c 20622 case DW_FORM_flag:
414ad644 20623 attr->set_unsigned (read_1_byte (abfd, info_ptr));
c906108c
SS
20624 info_ptr += 1;
20625 break;
2dc7f7b3 20626 case DW_FORM_flag_present:
414ad644 20627 attr->set_unsigned (1);
2dc7f7b3 20628 break;
c906108c 20629 case DW_FORM_sdata:
1bc397c5 20630 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20631 info_ptr += bytes_read;
20632 break;
18a8505e 20633 case DW_FORM_rnglistx:
7a5f294d
TT
20634 {
20635 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20636 &bytes_read));
20637 info_ptr += bytes_read;
20638 }
20639 break;
d0ce17d8 20640 case DW_FORM_udata:
414ad644 20641 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20642 info_ptr += bytes_read;
20643 break;
20644 case DW_FORM_ref1:
7c290a04 20645 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20646 + read_1_byte (abfd, info_ptr)));
c906108c
SS
20647 info_ptr += 1;
20648 break;
20649 case DW_FORM_ref2:
7c290a04 20650 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20651 + read_2_bytes (abfd, info_ptr)));
c906108c
SS
20652 info_ptr += 2;
20653 break;
20654 case DW_FORM_ref4:
7c290a04 20655 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20656 + read_4_bytes (abfd, info_ptr)));
c906108c
SS
20657 info_ptr += 4;
20658 break;
613e1657 20659 case DW_FORM_ref8:
7c290a04 20660 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20661 + read_8_bytes (abfd, info_ptr)));
613e1657
KB
20662 info_ptr += 8;
20663 break;
55f1336d 20664 case DW_FORM_ref_sig8:
630ed6b9 20665 attr->set_signature (read_8_bytes (abfd, info_ptr));
348e048f
DE
20666 info_ptr += 8;
20667 break;
c906108c 20668 case DW_FORM_ref_udata:
7c290a04 20669 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644
TT
20670 + read_unsigned_leb128 (abfd, info_ptr,
20671 &bytes_read)));
c906108c
SS
20672 info_ptr += bytes_read;
20673 break;
c906108c 20674 case DW_FORM_indirect:
a8329558
KW
20675 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20676 info_ptr += bytes_read;
43988095
JK
20677 if (form == DW_FORM_implicit_const)
20678 {
20679 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20680 info_ptr += bytes_read;
20681 }
20682 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
7a5f294d 20683 info_ptr);
43988095
JK
20684 break;
20685 case DW_FORM_implicit_const:
1bc397c5 20686 attr->set_signed (implicit_const);
a8329558 20687 break;
336d760d 20688 case DW_FORM_addrx:
3019eac3 20689 case DW_FORM_GNU_addr_index:
fe56917a
TT
20690 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20691 &bytes_read));
3019eac3
DE
20692 info_ptr += bytes_read;
20693 break;
cf532bd1 20694 case DW_FORM_strx:
15f18d14
AT
20695 case DW_FORM_strx1:
20696 case DW_FORM_strx2:
20697 case DW_FORM_strx3:
20698 case DW_FORM_strx4:
3019eac3 20699 case DW_FORM_GNU_str_index:
3019eac3 20700 {
15f18d14
AT
20701 ULONGEST str_index;
20702 if (form == DW_FORM_strx1)
20703 {
20704 str_index = read_1_byte (abfd, info_ptr);
20705 info_ptr += 1;
20706 }
20707 else if (form == DW_FORM_strx2)
20708 {
20709 str_index = read_2_bytes (abfd, info_ptr);
20710 info_ptr += 2;
20711 }
20712 else if (form == DW_FORM_strx3)
20713 {
20714 str_index = read_3_bytes (abfd, info_ptr);
20715 info_ptr += 3;
20716 }
20717 else if (form == DW_FORM_strx4)
20718 {
20719 str_index = read_4_bytes (abfd, info_ptr);
20720 info_ptr += 4;
20721 }
20722 else
20723 {
20724 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20725 info_ptr += bytes_read;
20726 }
fe56917a 20727 attr->set_unsigned_reprocess (str_index);
7a5f294d 20728 }
3019eac3 20729 break;
c906108c 20730 default:
8a3fe4f8 20731 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
20732 dwarf_form_name (form),
20733 bfd_get_filename (abfd));
c906108c 20734 }
28e94949 20735
36586728 20736 /* Super hack. */
cd6c91b4 20737 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
20738 attr->form = DW_FORM_GNU_ref_alt;
20739
28e94949
JB
20740 /* We have seen instances where the compiler tried to emit a byte
20741 size attribute of -1 which ended up being encoded as an unsigned
20742 0xffffffff. Although 0xffffffff is technically a valid size value,
20743 an object of this size seems pretty unlikely so we can relatively
20744 safely treat these cases as if the size attribute was invalid and
20745 treat them as zero by default. */
20746 if (attr->name == DW_AT_byte_size
20747 && form == DW_FORM_data4
529908cb 20748 && attr->as_unsigned () >= 0xffffffff)
01c66ae6
JB
20749 {
20750 complaint
dda83cd7
SM
20751 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20752 hex_string (attr->as_unsigned ()));
414ad644 20753 attr->set_unsigned (0);
01c66ae6 20754 }
28e94949 20755
c906108c
SS
20756 return info_ptr;
20757}
20758
a8329558
KW
20759/* Read an attribute described by an abbreviated attribute. */
20760
d521ce57 20761static const gdb_byte *
dee91e82 20762read_attribute (const struct die_reader_specs *reader,
4444f407 20763 struct attribute *attr, const struct attr_abbrev *abbrev,
7a5f294d 20764 const gdb_byte *info_ptr)
a8329558
KW
20765{
20766 attr->name = abbrev->name;
c6481205 20767 attr->string_is_canonical = 0;
fe56917a 20768 attr->requires_reprocessing = 0;
43988095 20769 return read_attribute_value (reader, attr, abbrev->form,
7a5f294d 20770 abbrev->implicit_const, info_ptr);
a8329558
KW
20771}
20772
43988095
JK
20773/* Return pointer to string at .debug_str offset STR_OFFSET. */
20774
20775static const char *
976ca316 20776read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 20777 LONGEST str_offset)
43988095 20778{
976ca316
SM
20779 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20780 str_offset, "DW_FORM_strp");
c906108c
SS
20781}
20782
43988095
JK
20783/* Return pointer to string at .debug_str offset as read from BUF.
20784 BUF is assumed to be in a compilation unit described by CU_HEADER.
20785 Return *BYTES_READ_PTR count of bytes read from BUF. */
20786
d521ce57 20787static const char *
976ca316 20788read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 20789 const gdb_byte *buf,
cf2c3c16
TT
20790 const struct comp_unit_head *cu_header,
20791 unsigned int *bytes_read_ptr)
20792{
8266302d 20793 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 20794
976ca316 20795 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
20796}
20797
86c0bb4c 20798/* See read.h. */
43988095 20799
86c0bb4c
TT
20800const char *
20801dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
20802 const struct comp_unit_head *cu_header,
20803 unsigned int *bytes_read_ptr)
43988095 20804{
86c0bb4c 20805 bfd *abfd = objfile->obfd;
8266302d 20806 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 20807
5989a64e 20808 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
20809}
20810
3019eac3 20811/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20812 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20813 ADDR_SIZE is the size of addresses from the CU header. */
20814
20815static CORE_ADDR
976ca316
SM
20816read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20817 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 20818{
976ca316 20819 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
20820 bfd *abfd = objfile->obfd;
20821 const gdb_byte *info_ptr;
18a8505e 20822 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 20823
976ca316
SM
20824 per_objfile->per_bfd->addr.read (objfile);
20825 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 20826 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20827 objfile_name (objfile));
18a8505e 20828 if (addr_base_or_zero + addr_index * addr_size
976ca316 20829 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
20830 error (_("DW_FORM_addr_index pointing outside of "
20831 ".debug_addr section [in module %s]"),
4262abfb 20832 objfile_name (objfile));
976ca316
SM
20833 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20834 + addr_index * addr_size);
3019eac3
DE
20835 if (addr_size == 4)
20836 return bfd_get_32 (abfd, info_ptr);
20837 else
20838 return bfd_get_64 (abfd, info_ptr);
20839}
20840
20841/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20842
20843static CORE_ADDR
20844read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20845{
5e22e966 20846 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 20847 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20848}
20849
20850/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20851
20852static CORE_ADDR
d521ce57 20853read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20854 unsigned int *bytes_read)
20855{
5e22e966 20856 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
20857 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20858
20859 return read_addr_index (cu, addr_index);
20860}
20861
450a1bfc 20862/* See read.h. */
3019eac3
DE
20863
20864CORE_ADDR
82ca3f51 20865dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 20866 dwarf2_per_objfile *per_objfile,
82ca3f51 20867 unsigned int addr_index)
3019eac3 20868{
976ca316 20869 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 20870 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20871 int addr_size;
20872
3019eac3
DE
20873 /* We need addr_base and addr_size.
20874 If we don't have PER_CU->cu, we have to get it.
20875 Nasty, but the alternative is storing the needed info in PER_CU,
20876 which at this point doesn't seem justified: it's not clear how frequently
20877 it would get used and it would increase the size of every PER_CU.
20878 Entry points like dwarf2_per_cu_addr_size do a similar thing
20879 so we're not in uncharted territory here.
20880 Alas we need to be a bit more complicated as addr_base is contained
20881 in the DIE.
20882
20883 We don't need to read the entire CU(/TU).
20884 We just need the header and top level die.
a1b64ce1 20885
3019eac3 20886 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20887 For now we skip this optimization. */
3019eac3
DE
20888
20889 if (cu != NULL)
20890 {
20891 addr_base = cu->addr_base;
20892 addr_size = cu->header.addr_size;
20893 }
20894 else
20895 {
976ca316 20896 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
20897 addr_base = reader.cu->addr_base;
20898 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20899 }
20900
976ca316 20901 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
20902}
20903
18a8505e
AT
20904/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20905 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20906 DWO file. */
3019eac3 20907
d521ce57 20908static const char *
18a8505e
AT
20909read_str_index (struct dwarf2_cu *cu,
20910 struct dwarf2_section_info *str_section,
20911 struct dwarf2_section_info *str_offsets_section,
20912 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20913{
976ca316
SM
20914 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20915 struct objfile *objfile = per_objfile->objfile;
c5164cbc 20916 const char *objf_name = objfile_name (objfile);
3019eac3 20917 bfd *abfd = objfile->obfd;
d521ce57 20918 const gdb_byte *info_ptr;
3019eac3 20919 ULONGEST str_offset;
cf532bd1 20920 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20921
96b79293
TT
20922 str_section->read (objfile);
20923 str_offsets_section->read (objfile);
73869dc2 20924 if (str_section->buffer == NULL)
18a8505e 20925 error (_("%s used without %s section"
9d8780f0 20926 " in CU at offset %s [in module %s]"),
96b79293 20927 form_name, str_section->get_name (),
dda83cd7 20928 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20929 if (str_offsets_section->buffer == NULL)
18a8505e 20930 error (_("%s used without %s section"
9d8780f0 20931 " in CU at offset %s [in module %s]"),
96b79293 20932 form_name, str_section->get_name (),
dda83cd7 20933 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20934 info_ptr = (str_offsets_section->buffer
18a8505e 20935 + str_offsets_base
3019eac3
DE
20936 + str_index * cu->header.offset_size);
20937 if (cu->header.offset_size == 4)
20938 str_offset = bfd_get_32 (abfd, info_ptr);
20939 else
20940 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20941 if (str_offset >= str_section->size)
57d63ce2 20942 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20943 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20944 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20945 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20946}
20947
18a8505e
AT
20948/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20949
20950static const char *
20951read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20952{
20953 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20954 ? reader->cu->header.addr_size : 0;
20955 return read_str_index (reader->cu,
20956 &reader->dwo_file->sections.str,
20957 &reader->dwo_file->sections.str_offsets,
20958 str_offsets_base, str_index);
20959}
20960
20961/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20962
20963static const char *
20964read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20965{
5e22e966 20966 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
20967 const char *objf_name = objfile_name (objfile);
20968 static const char form_name[] = "DW_FORM_GNU_str_index";
20969 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20970
20971 if (!cu->str_offsets_base.has_value ())
20972 error (_("%s used in Fission stub without %s"
20973 " in CU at offset 0x%lx [in module %s]"),
20974 form_name, str_offsets_attr_name,
20975 (long) cu->header.offset_size, objf_name);
20976
20977 return read_str_index (cu,
5e22e966
SM
20978 &cu->per_objfile->per_bfd->str,
20979 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
20980 *cu->str_offsets_base, str_index);
20981}
20982
3019eac3
DE
20983/* Return the length of an LEB128 number in BUF. */
20984
20985static int
20986leb128_size (const gdb_byte *buf)
20987{
20988 const gdb_byte *begin = buf;
20989 gdb_byte byte;
20990
20991 while (1)
20992 {
20993 byte = *buf++;
20994 if ((byte & 128) == 0)
20995 return buf - begin;
20996 }
20997}
20998
c906108c 20999static void
e142c38c 21000set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
21001{
21002 switch (lang)
21003 {
21004 case DW_LANG_C89:
76bee0cc 21005 case DW_LANG_C99:
0cfd832f 21006 case DW_LANG_C11:
c906108c 21007 case DW_LANG_C:
d1be3247 21008 case DW_LANG_UPC:
e142c38c 21009 cu->language = language_c;
c906108c 21010 break;
9c37b5ae 21011 case DW_LANG_Java:
c906108c 21012 case DW_LANG_C_plus_plus:
0cfd832f
MW
21013 case DW_LANG_C_plus_plus_11:
21014 case DW_LANG_C_plus_plus_14:
e142c38c 21015 cu->language = language_cplus;
c906108c 21016 break;
6aecb9c2
JB
21017 case DW_LANG_D:
21018 cu->language = language_d;
21019 break;
c906108c
SS
21020 case DW_LANG_Fortran77:
21021 case DW_LANG_Fortran90:
b21b22e0 21022 case DW_LANG_Fortran95:
f7de9aab
MW
21023 case DW_LANG_Fortran03:
21024 case DW_LANG_Fortran08:
e142c38c 21025 cu->language = language_fortran;
c906108c 21026 break;
a766d390
DE
21027 case DW_LANG_Go:
21028 cu->language = language_go;
21029 break;
c906108c 21030 case DW_LANG_Mips_Assembler:
e142c38c 21031 cu->language = language_asm;
c906108c
SS
21032 break;
21033 case DW_LANG_Ada83:
8aaf0b47 21034 case DW_LANG_Ada95:
bc5f45f8
JB
21035 cu->language = language_ada;
21036 break;
72019c9c
GM
21037 case DW_LANG_Modula2:
21038 cu->language = language_m2;
21039 break;
fe8e67fd
PM
21040 case DW_LANG_Pascal83:
21041 cu->language = language_pascal;
21042 break;
22566fbd
DJ
21043 case DW_LANG_ObjC:
21044 cu->language = language_objc;
21045 break;
c44af4eb
TT
21046 case DW_LANG_Rust:
21047 case DW_LANG_Rust_old:
21048 cu->language = language_rust;
21049 break;
c906108c
SS
21050 case DW_LANG_Cobol74:
21051 case DW_LANG_Cobol85:
c906108c 21052 default:
e142c38c 21053 cu->language = language_minimal;
c906108c
SS
21054 break;
21055 }
e142c38c 21056 cu->language_defn = language_def (cu->language);
c906108c
SS
21057}
21058
21059/* Return the named attribute or NULL if not there. */
21060
21061static struct attribute *
e142c38c 21062dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 21063{
a48e046c 21064 for (;;)
c906108c 21065 {
a48e046c
TT
21066 unsigned int i;
21067 struct attribute *spec = NULL;
21068
21069 for (i = 0; i < die->num_attrs; ++i)
21070 {
21071 if (die->attrs[i].name == name)
21072 return &die->attrs[i];
21073 if (die->attrs[i].name == DW_AT_specification
21074 || die->attrs[i].name == DW_AT_abstract_origin)
21075 spec = &die->attrs[i];
21076 }
21077
21078 if (!spec)
21079 break;
c906108c 21080
f2f0e013 21081 die = follow_die_ref (die, spec, &cu);
f2f0e013 21082 }
c5aa993b 21083
c906108c
SS
21084 return NULL;
21085}
21086
7d45c7c3
KB
21087/* Return the string associated with a string-typed attribute, or NULL if it
21088 is either not found or is of an incorrect type. */
21089
21090static const char *
21091dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21092{
21093 struct attribute *attr;
21094 const char *str = NULL;
21095
21096 attr = dwarf2_attr (die, name, cu);
21097
21098 if (attr != NULL)
21099 {
95f982e5 21100 str = attr->as_string ();
e61108c9 21101 if (str == nullptr)
dda83cd7 21102 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
21103 "DIE at %s in module %s"),
21104 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 21105 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
21106 }
21107
21108 return str;
21109}
21110
a084a2a6 21111/* Return the dwo name or NULL if not present. If present, it is in either
85102364 21112 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
21113static const char *
21114dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
21115{
21116 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
21117 if (dwo_name == nullptr)
21118 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
21119 return dwo_name;
21120}
21121
05cf31d1
JB
21122/* Return non-zero iff the attribute NAME is defined for the given DIE,
21123 and holds a non-zero value. This function should only be used for
2dc7f7b3 21124 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
21125
21126static int
21127dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
21128{
21129 struct attribute *attr = dwarf2_attr (die, name, cu);
21130
c45bc3f8 21131 return attr != nullptr && attr->as_boolean ();
05cf31d1
JB
21132}
21133
3ca72b44 21134static int
e142c38c 21135die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 21136{
05cf31d1
JB
21137 /* A DIE is a declaration if it has a DW_AT_declaration attribute
21138 which value is non-zero. However, we have to be careful with
21139 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
21140 (via dwarf2_flag_true_p) follows this attribute. So we may
21141 end up accidently finding a declaration attribute that belongs
21142 to a different DIE referenced by the specification attribute,
21143 even though the given DIE does not have a declaration attribute. */
21144 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
21145 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
21146}
21147
63d06c5c 21148/* Return the die giving the specification for DIE, if there is
f2f0e013 21149 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
21150 containing the return value on output. If there is no
21151 specification, but there is an abstract origin, that is
21152 returned. */
63d06c5c
DC
21153
21154static struct die_info *
f2f0e013 21155die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 21156{
f2f0e013
DJ
21157 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
21158 *spec_cu);
63d06c5c 21159
edb3359d
DJ
21160 if (spec_attr == NULL)
21161 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
21162
63d06c5c
DC
21163 if (spec_attr == NULL)
21164 return NULL;
21165 else
f2f0e013 21166 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 21167}
c906108c 21168
527f3840
JK
21169/* Stub for free_line_header to match void * callback types. */
21170
21171static void
21172free_line_header_voidp (void *arg)
21173{
9a3c8263 21174 struct line_header *lh = (struct line_header *) arg;
527f3840 21175
fff8551c 21176 delete lh;
527f3840
JK
21177}
21178
83769d0b 21179/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
21180
21181static struct dwarf2_section_info *
21182get_debug_line_section (struct dwarf2_cu *cu)
21183{
21184 struct dwarf2_section_info *section;
976ca316 21185 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
21186
21187 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
21188 DWO file. */
21189 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21190 section = &cu->dwo_unit->dwo_file->sections.line;
21191 else if (cu->per_cu->is_dwz)
21192 {
a7308ce0 21193 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
36586728
TT
21194
21195 section = &dwz->line;
21196 }
21197 else
976ca316 21198 section = &per_objfile->per_bfd->line;
36586728
TT
21199
21200 return section;
21201}
21202
debd256d 21203/* Read the statement program header starting at OFFSET in
3019eac3 21204 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 21205 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
21206 Returns NULL if there is a problem reading the header, e.g., if it
21207 has a version we don't understand.
debd256d
JB
21208
21209 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
21210 the returned object point into the dwarf line section buffer,
21211 and must not be freed. */
ae2de4f8 21212
fff8551c 21213static line_header_up
9c541725 21214dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 21215{
3019eac3 21216 struct dwarf2_section_info *section;
976ca316 21217 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 21218
36586728 21219 section = get_debug_line_section (cu);
976ca316 21220 section->read (per_objfile->objfile);
3019eac3 21221 if (section->buffer == NULL)
debd256d 21222 {
3019eac3 21223 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 21224 complaint (_("missing .debug_line.dwo section"));
3019eac3 21225 else
b98664d3 21226 complaint (_("missing .debug_line section"));
debd256d
JB
21227 return 0;
21228 }
21229
0df7ad3a 21230 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 21231 per_objfile, section, &cu->header);
debd256d 21232}
c906108c 21233
c6da4cef 21234/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 21235 Return the file name of the psymtab for the given file_entry.
c6da4cef 21236 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
21237 If space for the result is malloc'd, *NAME_HOLDER will be set.
21238 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 21239
d521ce57 21240static const char *
7ba99d21 21241psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 21242 const dwarf2_psymtab *pst,
c89b44cd
TT
21243 const char *comp_dir,
21244 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 21245{
d521ce57
TT
21246 const char *include_name = fe.name;
21247 const char *include_name_to_compare = include_name;
72b9f47f 21248 const char *pst_filename;
c6da4cef
DE
21249 int file_is_pst;
21250
8c43009f 21251 const char *dir_name = fe.include_dir (lh);
c6da4cef 21252
c89b44cd 21253 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
21254 if (!IS_ABSOLUTE_PATH (include_name)
21255 && (dir_name != NULL || comp_dir != NULL))
21256 {
21257 /* Avoid creating a duplicate psymtab for PST.
21258 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21259 Before we do the comparison, however, we need to account
21260 for DIR_NAME and COMP_DIR.
21261 First prepend dir_name (if non-NULL). If we still don't
21262 have an absolute path prepend comp_dir (if non-NULL).
21263 However, the directory we record in the include-file's
21264 psymtab does not contain COMP_DIR (to match the
21265 corresponding symtab(s)).
21266
21267 Example:
21268
21269 bash$ cd /tmp
21270 bash$ gcc -g ./hello.c
21271 include_name = "hello.c"
21272 dir_name = "."
21273 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
21274 DW_AT_name = "./hello.c"
21275
21276 */
c6da4cef
DE
21277
21278 if (dir_name != NULL)
21279 {
c89b44cd
TT
21280 name_holder->reset (concat (dir_name, SLASH_STRING,
21281 include_name, (char *) NULL));
21282 include_name = name_holder->get ();
c6da4cef 21283 include_name_to_compare = include_name;
c6da4cef
DE
21284 }
21285 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21286 {
c89b44cd
TT
21287 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21288 include_name, (char *) NULL));
21289 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
21290 }
21291 }
21292
21293 pst_filename = pst->filename;
c89b44cd 21294 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
21295 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21296 {
c89b44cd
TT
21297 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21298 pst_filename, (char *) NULL));
21299 pst_filename = copied_name.get ();
c6da4cef
DE
21300 }
21301
1e3fad37 21302 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 21303
c6da4cef
DE
21304 if (file_is_pst)
21305 return NULL;
21306 return include_name;
21307}
21308
d9b3de22
DE
21309/* State machine to track the state of the line number program. */
21310
6f77053d 21311class lnp_state_machine
d9b3de22 21312{
6f77053d
PA
21313public:
21314 /* Initialize a machine state for the start of a line number
21315 program. */
804d2729
TT
21316 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21317 bool record_lines_p);
6f77053d 21318
8c43009f
PA
21319 file_entry *current_file ()
21320 {
21321 /* lh->file_names is 0-based, but the file name numbers in the
21322 statement program are 1-based. */
6f77053d
PA
21323 return m_line_header->file_name_at (m_file);
21324 }
21325
21326 /* Record the line in the state machine. END_SEQUENCE is true if
21327 we're processing the end of a sequence. */
21328 void record_line (bool end_sequence);
21329
a8caed5d 21330 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
7ab6656f 21331 nop-out rest of the lines in this sequence. */
6f77053d
PA
21332 void check_line_address (struct dwarf2_cu *cu,
21333 const gdb_byte *line_ptr,
7ab6656f 21334 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
21335
21336 void handle_set_discriminator (unsigned int discriminator)
21337 {
21338 m_discriminator = discriminator;
21339 m_line_has_non_zero_discriminator |= discriminator != 0;
21340 }
21341
21342 /* Handle DW_LNE_set_address. */
21343 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21344 {
21345 m_op_index = 0;
21346 address += baseaddr;
21347 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21348 }
21349
21350 /* Handle DW_LNS_advance_pc. */
21351 void handle_advance_pc (CORE_ADDR adjust);
21352
21353 /* Handle a special opcode. */
21354 void handle_special_opcode (unsigned char op_code);
21355
21356 /* Handle DW_LNS_advance_line. */
21357 void handle_advance_line (int line_delta)
21358 {
21359 advance_line (line_delta);
21360 }
21361
21362 /* Handle DW_LNS_set_file. */
21363 void handle_set_file (file_name_index file);
21364
21365 /* Handle DW_LNS_negate_stmt. */
21366 void handle_negate_stmt ()
21367 {
21368 m_is_stmt = !m_is_stmt;
21369 }
21370
21371 /* Handle DW_LNS_const_add_pc. */
21372 void handle_const_add_pc ();
21373
21374 /* Handle DW_LNS_fixed_advance_pc. */
21375 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21376 {
21377 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21378 m_op_index = 0;
21379 }
21380
21381 /* Handle DW_LNS_copy. */
21382 void handle_copy ()
21383 {
21384 record_line (false);
21385 m_discriminator = 0;
21386 }
21387
21388 /* Handle DW_LNE_end_sequence. */
21389 void handle_end_sequence ()
21390 {
804d2729 21391 m_currently_recording_lines = true;
6f77053d
PA
21392 }
21393
21394private:
21395 /* Advance the line by LINE_DELTA. */
21396 void advance_line (int line_delta)
21397 {
21398 m_line += line_delta;
21399
21400 if (line_delta != 0)
21401 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21402 }
21403
804d2729
TT
21404 struct dwarf2_cu *m_cu;
21405
6f77053d
PA
21406 gdbarch *m_gdbarch;
21407
21408 /* True if we're recording lines.
21409 Otherwise we're building partial symtabs and are just interested in
21410 finding include files mentioned by the line number program. */
21411 bool m_record_lines_p;
21412
8c43009f 21413 /* The line number header. */
6f77053d 21414 line_header *m_line_header;
8c43009f 21415
6f77053d
PA
21416 /* These are part of the standard DWARF line number state machine,
21417 and initialized according to the DWARF spec. */
d9b3de22 21418
6f77053d 21419 unsigned char m_op_index = 0;
7ba99d21
AT
21420 /* The line table index of the current file. */
21421 file_name_index m_file = 1;
6f77053d
PA
21422 unsigned int m_line = 1;
21423
21424 /* These are initialized in the constructor. */
21425
21426 CORE_ADDR m_address;
21427 bool m_is_stmt;
21428 unsigned int m_discriminator;
d9b3de22
DE
21429
21430 /* Additional bits of state we need to track. */
21431
21432 /* The last file that we called dwarf2_start_subfile for.
21433 This is only used for TLLs. */
6f77053d 21434 unsigned int m_last_file = 0;
d9b3de22 21435 /* The last file a line number was recorded for. */
6f77053d 21436 struct subfile *m_last_subfile = NULL;
d9b3de22 21437
1313c56e
AB
21438 /* The address of the last line entry. */
21439 CORE_ADDR m_last_address;
21440
21441 /* Set to true when a previous line at the same address (using
21442 m_last_address) had m_is_stmt true. This is reset to false when a
21443 line entry at a new address (m_address different to m_last_address) is
21444 processed. */
21445 bool m_stmt_at_address = false;
21446
804d2729
TT
21447 /* When true, record the lines we decode. */
21448 bool m_currently_recording_lines = false;
d9b3de22
DE
21449
21450 /* The last line number that was recorded, used to coalesce
21451 consecutive entries for the same line. This can happen, for
21452 example, when discriminators are present. PR 17276. */
6f77053d
PA
21453 unsigned int m_last_line = 0;
21454 bool m_line_has_non_zero_discriminator = false;
8c43009f 21455};
d9b3de22 21456
6f77053d
PA
21457void
21458lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21459{
21460 CORE_ADDR addr_adj = (((m_op_index + adjust)
21461 / m_line_header->maximum_ops_per_instruction)
21462 * m_line_header->minimum_instruction_length);
21463 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21464 m_op_index = ((m_op_index + adjust)
21465 % m_line_header->maximum_ops_per_instruction);
21466}
d9b3de22 21467
6f77053d
PA
21468void
21469lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21470{
6f77053d 21471 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
21472 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21473 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21474 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
21475 / m_line_header->maximum_ops_per_instruction)
21476 * m_line_header->minimum_instruction_length);
21477 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 21478 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 21479 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21480
258bf0ee 21481 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
21482 advance_line (line_delta);
21483 record_line (false);
21484 m_discriminator = 0;
21485}
d9b3de22 21486
6f77053d
PA
21487void
21488lnp_state_machine::handle_set_file (file_name_index file)
21489{
21490 m_file = file;
21491
21492 const file_entry *fe = current_file ();
21493 if (fe == NULL)
21494 dwarf2_debug_line_missing_file_complaint ();
21495 else if (m_record_lines_p)
21496 {
21497 const char *dir = fe->include_dir (m_line_header);
21498
c24bdb02 21499 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21500 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21501 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21502 }
21503}
21504
21505void
21506lnp_state_machine::handle_const_add_pc ()
21507{
21508 CORE_ADDR adjust
21509 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21510
21511 CORE_ADDR addr_adj
21512 = (((m_op_index + adjust)
21513 / m_line_header->maximum_ops_per_instruction)
21514 * m_line_header->minimum_instruction_length);
21515
21516 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21517 m_op_index = ((m_op_index + adjust)
21518 % m_line_header->maximum_ops_per_instruction);
21519}
d9b3de22 21520
a05a36a5
DE
21521/* Return non-zero if we should add LINE to the line number table.
21522 LINE is the line to add, LAST_LINE is the last line that was added,
21523 LAST_SUBFILE is the subfile for LAST_LINE.
21524 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21525 had a non-zero discriminator.
21526
21527 We have to be careful in the presence of discriminators.
21528 E.g., for this line:
21529
21530 for (i = 0; i < 100000; i++);
21531
21532 clang can emit four line number entries for that one line,
21533 each with a different discriminator.
21534 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21535
21536 However, we want gdb to coalesce all four entries into one.
21537 Otherwise the user could stepi into the middle of the line and
21538 gdb would get confused about whether the pc really was in the
21539 middle of the line.
21540
21541 Things are further complicated by the fact that two consecutive
21542 line number entries for the same line is a heuristic used by gcc
21543 to denote the end of the prologue. So we can't just discard duplicate
21544 entries, we have to be selective about it. The heuristic we use is
21545 that we only collapse consecutive entries for the same line if at least
21546 one of those entries has a non-zero discriminator. PR 17276.
21547
21548 Note: Addresses in the line number state machine can never go backwards
21549 within one sequence, thus this coalescing is ok. */
21550
21551static int
804d2729
TT
21552dwarf_record_line_p (struct dwarf2_cu *cu,
21553 unsigned int line, unsigned int last_line,
a05a36a5
DE
21554 int line_has_non_zero_discriminator,
21555 struct subfile *last_subfile)
21556{
c24bdb02 21557 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21558 return 1;
21559 if (line != last_line)
21560 return 1;
21561 /* Same line for the same file that we've seen already.
21562 As a last check, for pr 17276, only record the line if the line
21563 has never had a non-zero discriminator. */
21564 if (!line_has_non_zero_discriminator)
21565 return 1;
21566 return 0;
21567}
21568
804d2729
TT
21569/* Use the CU's builder to record line number LINE beginning at
21570 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21571
21572static void
d9b3de22 21573dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 21574 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 21575 struct dwarf2_cu *cu)
252a6764
DE
21576{
21577 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21578
27e0867f
DE
21579 if (dwarf_line_debug)
21580 {
21581 fprintf_unfiltered (gdb_stdlog,
21582 "Recording line %u, file %s, address %s\n",
21583 line, lbasename (subfile->name),
21584 paddress (gdbarch, address));
21585 }
21586
804d2729 21587 if (cu != nullptr)
8c95582d 21588 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
21589}
21590
21591/* Subroutine of dwarf_decode_lines_1 to simplify it.
21592 Mark the end of a set of line number records.
d9b3de22 21593 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21594 If SUBFILE is NULL the request is ignored. */
21595
21596static void
21597dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21598 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21599{
27e0867f
DE
21600 if (subfile == NULL)
21601 return;
21602
21603 if (dwarf_line_debug)
21604 {
21605 fprintf_unfiltered (gdb_stdlog,
21606 "Finishing current line, file %s, address %s\n",
21607 lbasename (subfile->name),
21608 paddress (gdbarch, address));
21609 }
21610
8c95582d 21611 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
21612}
21613
6f77053d
PA
21614void
21615lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21616{
d9b3de22
DE
21617 if (dwarf_line_debug)
21618 {
21619 fprintf_unfiltered (gdb_stdlog,
21620 "Processing actual line %u: file %u,"
94a72be7 21621 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 21622 m_line, m_file,
6f77053d 21623 paddress (m_gdbarch, m_address),
94a72be7
AB
21624 m_is_stmt, m_discriminator,
21625 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
21626 }
21627
6f77053d 21628 file_entry *fe = current_file ();
8c43009f
PA
21629
21630 if (fe == NULL)
d9b3de22
DE
21631 dwarf2_debug_line_missing_file_complaint ();
21632 /* For now we ignore lines not starting on an instruction boundary.
21633 But not when processing end_sequence for compatibility with the
21634 previous version of the code. */
6f77053d 21635 else if (m_op_index == 0 || end_sequence)
d9b3de22 21636 {
8c43009f 21637 fe->included_p = 1;
8c95582d 21638 if (m_record_lines_p)
d9b3de22 21639 {
1313c56e
AB
21640 /* When we switch files we insert an end maker in the first file,
21641 switch to the second file and add a new line entry. The
21642 problem is that the end marker inserted in the first file will
21643 discard any previous line entries at the same address. If the
21644 line entries in the first file are marked as is-stmt, while
21645 the new line in the second file is non-stmt, then this means
21646 the end marker will discard is-stmt lines so we can have a
21647 non-stmt line. This means that there are less addresses at
21648 which the user can insert a breakpoint.
21649
21650 To improve this we track the last address in m_last_address,
21651 and whether we have seen an is-stmt at this address. Then
21652 when switching files, if we have seen a stmt at the current
21653 address, and we are switching to create a non-stmt line, then
21654 discard the new line. */
21655 bool file_changed
21656 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21657 bool ignore_this_line
dda83cd7
SM
21658 = ((file_changed && !end_sequence && m_last_address == m_address
21659 && !m_is_stmt && m_stmt_at_address)
21660 || (!end_sequence && m_line == 0));
1313c56e
AB
21661
21662 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 21663 {
804d2729
TT
21664 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21665 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21666 }
21667
1313c56e 21668 if (!end_sequence && !ignore_this_line)
d9b3de22 21669 {
8c95582d
AB
21670 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21671
804d2729 21672 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21673 m_line_has_non_zero_discriminator,
21674 m_last_subfile))
d9b3de22 21675 {
c24bdb02 21676 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21677 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21678 builder->get_current_subfile (),
8c95582d 21679 m_line, m_address, is_stmt,
804d2729 21680 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21681 }
c24bdb02 21682 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21683 m_last_line = m_line;
d9b3de22
DE
21684 }
21685 }
21686 }
1313c56e
AB
21687
21688 /* Track whether we have seen any m_is_stmt true at m_address in case we
21689 have multiple line table entries all at m_address. */
21690 if (m_last_address != m_address)
21691 {
21692 m_stmt_at_address = false;
21693 m_last_address = m_address;
21694 }
21695 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
21696}
21697
804d2729
TT
21698lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21699 line_header *lh, bool record_lines_p)
d9b3de22 21700{
804d2729 21701 m_cu = cu;
6f77053d
PA
21702 m_gdbarch = arch;
21703 m_record_lines_p = record_lines_p;
21704 m_line_header = lh;
d9b3de22 21705
804d2729 21706 m_currently_recording_lines = true;
d9b3de22 21707
d9b3de22
DE
21708 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21709 was a line entry for it so that the backend has a chance to adjust it
21710 and also record it in case it needs it. This is currently used by MIPS
21711 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21712 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21713 m_is_stmt = lh->default_is_stmt;
21714 m_discriminator = 0;
1313c56e
AB
21715
21716 m_last_address = m_address;
21717 m_stmt_at_address = false;
252a6764
DE
21718}
21719
6f77053d
PA
21720void
21721lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21722 const gdb_byte *line_ptr,
7ab6656f 21723 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21724{
a8caed5d
FS
21725 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21726 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21727 located at 0x0. In this case, additionally check that if
21728 ADDRESS < UNRELOCATED_LOWPC. */
924c2928 21729
a8caed5d
FS
21730 if ((address == 0 && address < unrelocated_lowpc)
21731 || address == (CORE_ADDR) -1)
924c2928
DE
21732 {
21733 /* This line table is for a function which has been
21734 GCd by the linker. Ignore it. PR gdb/12528 */
21735
5e22e966 21736 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
21737 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21738
b98664d3 21739 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21740 line_offset, objfile_name (objfile));
804d2729
TT
21741 m_currently_recording_lines = false;
21742 /* Note: m_currently_recording_lines is left as false until we see
21743 DW_LNE_end_sequence. */
924c2928
DE
21744 }
21745}
21746
f3f5162e 21747/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21748 Process the line number information in LH.
21749 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21750 program in order to set included_p for every referenced header. */
debd256d 21751
c906108c 21752static void
43f3e411
DE
21753dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21754 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21755{
d521ce57
TT
21756 const gdb_byte *line_ptr, *extended_end;
21757 const gdb_byte *line_end;
a8c50c1f 21758 unsigned int bytes_read, extended_len;
699ca60a 21759 unsigned char op_code, extended_op;
e142c38c 21760 CORE_ADDR baseaddr;
5e22e966 21761 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21762 bfd *abfd = objfile->obfd;
08feed99 21763 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
21764 /* True if we're recording line info (as opposed to building partial
21765 symtabs and just interested in finding include files mentioned by
21766 the line number program). */
21767 bool record_lines_p = !decode_for_pst_p;
e142c38c 21768
b3b3bada 21769 baseaddr = objfile->text_section_offset ();
c906108c 21770
debd256d
JB
21771 line_ptr = lh->statement_program_start;
21772 line_end = lh->statement_program_end;
c906108c
SS
21773
21774 /* Read the statement sequences until there's nothing left. */
21775 while (line_ptr < line_end)
21776 {
6f77053d
PA
21777 /* The DWARF line number program state machine. Reset the state
21778 machine at the start of each sequence. */
804d2729 21779 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21780 bool end_sequence = false;
d9b3de22 21781
8c43009f 21782 if (record_lines_p)
c906108c 21783 {
8c43009f
PA
21784 /* Start a subfile for the current file of the state
21785 machine. */
21786 const file_entry *fe = state_machine.current_file ();
21787
21788 if (fe != NULL)
804d2729 21789 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21790 }
21791
a738430d 21792 /* Decode the table. */
d9b3de22 21793 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21794 {
21795 op_code = read_1_byte (abfd, line_ptr);
21796 line_ptr += 1;
9aa1fe7e 21797
debd256d 21798 if (op_code >= lh->opcode_base)
6e70227d 21799 {
8e07a239 21800 /* Special opcode. */
6f77053d 21801 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21802 }
21803 else switch (op_code)
c906108c
SS
21804 {
21805 case DW_LNS_extended_op:
3e43a32a
MS
21806 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21807 &bytes_read);
473b7be6 21808 line_ptr += bytes_read;
a8c50c1f 21809 extended_end = line_ptr + extended_len;
c906108c
SS
21810 extended_op = read_1_byte (abfd, line_ptr);
21811 line_ptr += 1;
8f34b746
TV
21812 if (DW_LNE_lo_user <= extended_op
21813 && extended_op <= DW_LNE_hi_user)
21814 {
21815 /* Vendor extension, ignore. */
21816 line_ptr = extended_end;
21817 break;
21818 }
c906108c
SS
21819 switch (extended_op)
21820 {
21821 case DW_LNE_end_sequence:
6f77053d
PA
21822 state_machine.handle_end_sequence ();
21823 end_sequence = true;
c906108c
SS
21824 break;
21825 case DW_LNE_set_address:
d9b3de22
DE
21826 {
21827 CORE_ADDR address
c8a7a66f 21828 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 21829 line_ptr += bytes_read;
6f77053d
PA
21830
21831 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21832 lowpc - baseaddr, address);
6f77053d 21833 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21834 }
c906108c
SS
21835 break;
21836 case DW_LNE_define_file:
dda83cd7
SM
21837 {
21838 const char *cur_file;
ecfb656c
PA
21839 unsigned int mod_time, length;
21840 dir_index dindex;
6e70227d 21841
dda83cd7 21842 cur_file = read_direct_string (abfd, line_ptr,
3e43a32a 21843 &bytes_read);
dda83cd7
SM
21844 line_ptr += bytes_read;
21845 dindex = (dir_index)
21846 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21847 line_ptr += bytes_read;
21848 mod_time =
21849 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21850 line_ptr += bytes_read;
21851 length =
21852 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21853 line_ptr += bytes_read;
21854 lh->add_file_name (cur_file, dindex, mod_time, length);
21855 }
c906108c 21856 break;
d0c6ba3d 21857 case DW_LNE_set_discriminator:
6f77053d
PA
21858 {
21859 /* The discriminator is not interesting to the
21860 debugger; just ignore it. We still need to
21861 check its value though:
21862 if there are consecutive entries for the same
21863 (non-prologue) line we want to coalesce them.
21864 PR 17276. */
21865 unsigned int discr
21866 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21867 line_ptr += bytes_read;
21868
21869 state_machine.handle_set_discriminator (discr);
21870 }
d0c6ba3d 21871 break;
c906108c 21872 default:
b98664d3 21873 complaint (_("mangled .debug_line section"));
debd256d 21874 return;
c906108c 21875 }
a8c50c1f
DJ
21876 /* Make sure that we parsed the extended op correctly. If e.g.
21877 we expected a different address size than the producer used,
21878 we may have read the wrong number of bytes. */
21879 if (line_ptr != extended_end)
21880 {
b98664d3 21881 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21882 return;
21883 }
c906108c
SS
21884 break;
21885 case DW_LNS_copy:
6f77053d 21886 state_machine.handle_copy ();
c906108c
SS
21887 break;
21888 case DW_LNS_advance_pc:
2dc7f7b3
TT
21889 {
21890 CORE_ADDR adjust
21891 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21892 line_ptr += bytes_read;
6f77053d
PA
21893
21894 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21895 }
c906108c
SS
21896 break;
21897 case DW_LNS_advance_line:
a05a36a5
DE
21898 {
21899 int line_delta
21900 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21901 line_ptr += bytes_read;
6f77053d
PA
21902
21903 state_machine.handle_advance_line (line_delta);
a05a36a5 21904 }
c906108c
SS
21905 break;
21906 case DW_LNS_set_file:
d9b3de22 21907 {
6f77053d 21908 file_name_index file
ecfb656c
PA
21909 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21910 &bytes_read);
d9b3de22 21911 line_ptr += bytes_read;
8c43009f 21912
6f77053d 21913 state_machine.handle_set_file (file);
d9b3de22 21914 }
c906108c
SS
21915 break;
21916 case DW_LNS_set_column:
0ad93d4f 21917 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21918 line_ptr += bytes_read;
21919 break;
21920 case DW_LNS_negate_stmt:
6f77053d 21921 state_machine.handle_negate_stmt ();
c906108c
SS
21922 break;
21923 case DW_LNS_set_basic_block:
c906108c 21924 break;
c2c6d25f
JM
21925 /* Add to the address register of the state machine the
21926 address increment value corresponding to special opcode
a738430d
MK
21927 255. I.e., this value is scaled by the minimum
21928 instruction length since special opcode 255 would have
b021a221 21929 scaled the increment. */
c906108c 21930 case DW_LNS_const_add_pc:
6f77053d 21931 state_machine.handle_const_add_pc ();
c906108c
SS
21932 break;
21933 case DW_LNS_fixed_advance_pc:
3e29f34a 21934 {
6f77053d 21935 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21936 line_ptr += 2;
6f77053d
PA
21937
21938 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21939 }
c906108c 21940 break;
9aa1fe7e 21941 default:
a738430d
MK
21942 {
21943 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21944 int i;
a738430d 21945
debd256d 21946 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21947 {
21948 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21949 line_ptr += bytes_read;
21950 }
21951 }
c906108c
SS
21952 }
21953 }
d9b3de22
DE
21954
21955 if (!end_sequence)
21956 dwarf2_debug_line_missing_end_sequence_complaint ();
21957
21958 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21959 in which case we still finish recording the last line). */
6f77053d 21960 state_machine.record_line (true);
c906108c 21961 }
f3f5162e
DE
21962}
21963
21964/* Decode the Line Number Program (LNP) for the given line_header
21965 structure and CU. The actual information extracted and the type
21966 of structures created from the LNP depends on the value of PST.
21967
21968 1. If PST is NULL, then this procedure uses the data from the program
21969 to create all necessary symbol tables, and their linetables.
21970
21971 2. If PST is not NULL, this procedure reads the program to determine
21972 the list of files included by the unit represented by PST, and
21973 builds all the associated partial symbol tables.
21974
21975 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21976 It is used for relative paths in the line table.
21977 NOTE: When processing partial symtabs (pst != NULL),
21978 comp_dir == pst->dirname.
21979
21980 NOTE: It is important that psymtabs have the same file name (via strcmp)
21981 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21982 symtab we don't use it in the name of the psymtabs we create.
21983 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21984 A good testcase for this is mb-inline.exp.
21985
527f3840
JK
21986 LOWPC is the lowest address in CU (or 0 if not known).
21987
21988 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21989 for its PC<->lines mapping information. Otherwise only the filename
21990 table is read in. */
f3f5162e
DE
21991
21992static void
21993dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21994 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21995 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21996{
5e22e966 21997 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21998 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21999
527f3840
JK
22000 if (decode_mapping)
22001 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
22002
22003 if (decode_for_pst_p)
22004 {
aaa75496 22005 /* Now that we're done scanning the Line Header Program, we can
dda83cd7 22006 create the psymtab of each included file. */
7ba99d21 22007 for (auto &file_entry : lh->file_names ())
dda83cd7
SM
22008 if (file_entry.included_p == 1)
22009 {
c89b44cd 22010 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 22011 const char *include_name =
7ba99d21
AT
22012 psymtab_include_file_name (lh, file_entry, pst,
22013 comp_dir, &name_holder);
c6da4cef 22014 if (include_name != NULL)
7b249e47
TT
22015 dwarf2_create_include_psymtab
22016 (cu->per_objfile->per_bfd, include_name, pst,
22017 cu->per_objfile->per_bfd->partial_symtabs.get (),
0072c873 22018 objfile->per_bfd);
dda83cd7 22019 }
aaa75496 22020 }
cb1df416
DJ
22021 else
22022 {
22023 /* Make sure a symtab is created for every file, even files
22024 which contain only variables (i.e. no code with associated
22025 line numbers). */
c24bdb02
KS
22026 buildsym_compunit *builder = cu->get_builder ();
22027 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 22028
7ba99d21 22029 for (auto &fe : lh->file_names ())
cb1df416 22030 {
804d2729 22031 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 22032 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 22033 {
c24bdb02 22034 builder->get_current_subfile ()->symtab
804d2729 22035 = allocate_symtab (cust,
c24bdb02 22036 builder->get_current_subfile ()->name);
43f3e411 22037 }
c24bdb02 22038 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
22039 }
22040 }
c906108c
SS
22041}
22042
22043/* Start a subfile for DWARF. FILENAME is the name of the file and
22044 DIRNAME the name of the source directory which contains FILENAME
4d663531 22045 or NULL if not known.
c906108c
SS
22046 This routine tries to keep line numbers from identical absolute and
22047 relative file names in a common subfile.
22048
22049 Using the `list' example from the GDB testsuite, which resides in
22050 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
22051 of /srcdir/list0.c yields the following debugging information for list0.c:
22052
c5aa993b 22053 DW_AT_name: /srcdir/list0.c
4d663531 22054 DW_AT_comp_dir: /compdir
357e46e7 22055 files.files[0].name: list0.h
c5aa993b 22056 files.files[0].dir: /srcdir
357e46e7 22057 files.files[1].name: list0.c
c5aa993b 22058 files.files[1].dir: /srcdir
c906108c
SS
22059
22060 The line number information for list0.c has to end up in a single
4f1520fb
FR
22061 subfile, so that `break /srcdir/list0.c:1' works as expected.
22062 start_subfile will ensure that this happens provided that we pass the
22063 concatenation of files.files[1].dir and files.files[1].name as the
22064 subfile's name. */
c906108c
SS
22065
22066static void
804d2729
TT
22067dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
22068 const char *dirname)
c906108c 22069{
43816ebc 22070 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 22071
4d663531 22072 /* In order not to lose the line information directory,
4f1520fb
FR
22073 we concatenate it to the filename when it makes sense.
22074 Note that the Dwarf3 standard says (speaking of filenames in line
22075 information): ``The directory index is ignored for file names
22076 that represent full path names''. Thus ignoring dirname in the
22077 `else' branch below isn't an issue. */
c906108c 22078
d5166ae1 22079 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 22080 {
43816ebc
TT
22081 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
22082 filename = copy.get ();
d521ce57 22083 }
c906108c 22084
c24bdb02 22085 cu->get_builder ()->start_subfile (filename);
c906108c
SS
22086}
22087
804d2729
TT
22088/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
22089 buildsym_compunit constructor. */
f4dc4d17 22090
c24bdb02
KS
22091struct compunit_symtab *
22092dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
22093 CORE_ADDR low_pc)
f4dc4d17 22094{
c24bdb02 22095 gdb_assert (m_builder == nullptr);
43f3e411 22096
c24bdb02 22097 m_builder.reset (new struct buildsym_compunit
f6e649dd 22098 (this->per_objfile->objfile,
c24bdb02 22099 name, comp_dir, language, low_pc));
93b8bea4 22100
c24bdb02 22101 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 22102
c24bdb02
KS
22103 get_builder ()->record_debugformat ("DWARF 2");
22104 get_builder ()->record_producer (producer);
f4dc4d17 22105
c24bdb02 22106 processing_has_namespace_info = false;
43f3e411 22107
c24bdb02 22108 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
22109}
22110
4c2df51b
DJ
22111static void
22112var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 22113 struct dwarf2_cu *cu)
4c2df51b 22114{
5e22e966 22115 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
22116 struct comp_unit_head *cu_header = &cu->header;
22117
4c2df51b
DJ
22118 /* NOTE drow/2003-01-30: There used to be a comment and some special
22119 code here to turn a symbol with DW_AT_external and a
22120 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
22121 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
22122 with some versions of binutils) where shared libraries could have
22123 relocations against symbols in their debug information - the
22124 minimal symbol would have the right address, but the debug info
22125 would not. It's no longer necessary, because we will explicitly
22126 apply relocations when we read in the debug information now. */
22127
22128 /* A DW_AT_location attribute with no contents indicates that a
22129 variable has been optimized away. */
9d2246fc 22130 if (attr->form_is_block () && attr->as_block ()->size == 0)
4c2df51b 22131 {
f1e6e072 22132 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
22133 return;
22134 }
22135
22136 /* Handle one degenerate form of location expression specially, to
22137 preserve GDB's previous behavior when section offsets are
336d760d
AT
22138 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
22139 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 22140
9d2246fc
TT
22141 if (attr->form_is_block ())
22142 {
22143 struct dwarf_block *block = attr->as_block ();
22144
22145 if ((block->data[0] == DW_OP_addr
22146 && block->size == 1 + cu_header->addr_size)
22147 || ((block->data[0] == DW_OP_GNU_addr_index
dda83cd7 22148 || block->data[0] == DW_OP_addrx)
9d2246fc
TT
22149 && (block->size
22150 == 1 + leb128_size (&block->data[1]))))
22151 {
22152 unsigned int dummy;
22153
22154 if (block->data[0] == DW_OP_addr)
22155 SET_SYMBOL_VALUE_ADDRESS
22156 (sym, cu->header.read_address (objfile->obfd,
22157 block->data + 1,
38583298 22158 &dummy));
9d2246fc
TT
22159 else
22160 SET_SYMBOL_VALUE_ADDRESS
22161 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
22162 &dummy));
22163 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
22164 fixup_symbol_section (sym, objfile);
22165 SET_SYMBOL_VALUE_ADDRESS
22166 (sym,
22167 SYMBOL_VALUE_ADDRESS (sym)
a52d653e 22168 + objfile->section_offsets[sym->section_index ()]);
9d2246fc
TT
22169 return;
22170 }
4c2df51b
DJ
22171 }
22172
22173 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
22174 expression evaluator, and use LOC_COMPUTED only when necessary
22175 (i.e. when the value of a register or memory location is
22176 referenced, or a thread-local block, etc.). Then again, it might
22177 not be worthwhile. I'm assuming that it isn't unless performance
22178 or memory numbers show me otherwise. */
22179
f1e6e072 22180 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 22181
f1e6e072 22182 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 22183 cu->has_loclist = true;
4c2df51b
DJ
22184}
22185
c906108c
SS
22186/* Given a pointer to a DWARF information entry, figure out if we need
22187 to make a symbol table entry for it, and if so, create a new entry
22188 and return a pointer to it.
22189 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
22190 used the passed type.
22191 If SPACE is not NULL, use it to hold the new symbol. If it is
22192 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
22193
22194static struct symbol *
5e2db402
TT
22195new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
22196 struct symbol *space)
c906108c 22197{
976ca316
SM
22198 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22199 struct objfile *objfile = per_objfile->objfile;
08feed99 22200 struct gdbarch *gdbarch = objfile->arch ();
c906108c 22201 struct symbol *sym = NULL;
15d034d0 22202 const char *name;
c906108c
SS
22203 struct attribute *attr = NULL;
22204 struct attribute *attr2 = NULL;
e142c38c 22205 CORE_ADDR baseaddr;
e37fd15a
SW
22206 struct pending **list_to_add = NULL;
22207
edb3359d 22208 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 22209
b3b3bada 22210 baseaddr = objfile->text_section_offset ();
c906108c 22211
94af9270 22212 name = dwarf2_name (die, cu);
c906108c
SS
22213 if (name)
22214 {
34eaf542 22215 int suppress_add = 0;
94af9270 22216
34eaf542
TT
22217 if (space)
22218 sym = space;
22219 else
8c14c3a3 22220 sym = new (&objfile->objfile_obstack) symbol;
c906108c 22221 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
22222
22223 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 22224 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
22225 /* Fortran does not have mangling standard and the mangling does differ
22226 between gfortran, iFort etc. */
bcfe6157
TT
22227 const char *physname
22228 = (cu->language == language_fortran
22229 ? dwarf2_full_name (name, die, cu)
22230 : dwarf2_physname (name, die, cu));
22231 const char *linkagename = dw2_linkage_name (die, cu);
22232
22233 if (linkagename == nullptr || cu->language == language_ada)
22234 sym->set_linkage_name (physname);
22235 else
22236 {
22237 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22238 sym->set_linkage_name (linkagename);
22239 }
f55ee35c 22240
c906108c 22241 /* Default assumptions.
dda83cd7 22242 Use the passed type or decode it from the die. */
176620f1 22243 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 22244 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
22245 if (type != NULL)
22246 SYMBOL_TYPE (sym) = type;
22247 else
e7c27a73 22248 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
22249 attr = dwarf2_attr (die,
22250 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22251 cu);
435d3d88 22252 if (attr != nullptr)
529908cb 22253 SYMBOL_LINE (sym) = attr->constant_value (0);
cb1df416 22254
edb3359d
DJ
22255 attr = dwarf2_attr (die,
22256 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22257 cu);
aa659cfa 22258 if (attr != nullptr && attr->is_nonnegative ())
cb1df416 22259 {
529908cb 22260 file_name_index file_index
aa659cfa 22261 = (file_name_index) attr->as_nonnegative ();
8c43009f 22262 struct file_entry *fe;
9a619af0 22263
ecfb656c
PA
22264 if (cu->line_header != NULL)
22265 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
22266 else
22267 fe = NULL;
22268
22269 if (fe == NULL)
b98664d3 22270 complaint (_("file index out of range"));
8c43009f
PA
22271 else
22272 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
22273 }
22274
c906108c
SS
22275 switch (die->tag)
22276 {
22277 case DW_TAG_label:
e142c38c 22278 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 22279 if (attr != nullptr)
3e29f34a
MR
22280 {
22281 CORE_ADDR addr;
22282
95f982e5 22283 addr = attr->as_address ();
3e29f34a 22284 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 22285 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
8f5c6526 22286 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
3e29f34a 22287 }
8f5c6526
TV
22288 else
22289 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
0f5238ed
TT
22290 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22291 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
d3cb6808 22292 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
22293 break;
22294 case DW_TAG_subprogram:
22295 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22296 finish_block. */
f1e6e072 22297 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 22298 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 22299 if ((attr2 != nullptr && attr2->as_boolean ())
0a4b0913
AB
22300 || cu->language == language_ada
22301 || cu->language == language_fortran)
c906108c 22302 {
dda83cd7
SM
22303 /* Subprograms marked external are stored as a global symbol.
22304 Ada and Fortran subprograms, whether marked external or
22305 not, are always stored as a global symbol, because we want
22306 to be able to access them globally. For instance, we want
22307 to be able to break on a nested subprogram without having
22308 to specify the context. */
c24bdb02 22309 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
22310 }
22311 else
22312 {
e37fd15a 22313 list_to_add = cu->list_in_scope;
c906108c
SS
22314 }
22315 break;
edb3359d
DJ
22316 case DW_TAG_inlined_subroutine:
22317 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22318 finish_block. */
f1e6e072 22319 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 22320 SYMBOL_INLINED (sym) = 1;
481860b3 22321 list_to_add = cu->list_in_scope;
edb3359d 22322 break;
34eaf542
TT
22323 case DW_TAG_template_value_param:
22324 suppress_add = 1;
22325 /* Fall through. */
72929c62 22326 case DW_TAG_constant:
c906108c 22327 case DW_TAG_variable:
254e6b9e 22328 case DW_TAG_member:
0963b4bd
MS
22329 /* Compilation with minimal debug info may result in
22330 variables with missing type entries. Change the
22331 misleading `void' type to something sensible. */
78134374 22332 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 22333 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 22334
e142c38c 22335 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
22336 /* In the case of DW_TAG_member, we should only be called for
22337 static const members. */
22338 if (die->tag == DW_TAG_member)
22339 {
3863f96c
DE
22340 /* dwarf2_add_field uses die_is_declaration,
22341 so we do the same. */
254e6b9e
DE
22342 gdb_assert (die_is_declaration (die, cu));
22343 gdb_assert (attr);
22344 }
435d3d88 22345 if (attr != nullptr)
c906108c 22346 {
e7c27a73 22347 dwarf2_const_value (attr, sym, cu);
e142c38c 22348 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 22349 if (!suppress_add)
34eaf542 22350 {
c45bc3f8 22351 if (attr2 != nullptr && attr2->as_boolean ())
c24bdb02 22352 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 22353 else
e37fd15a 22354 list_to_add = cu->list_in_scope;
34eaf542 22355 }
c906108c
SS
22356 break;
22357 }
e142c38c 22358 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22359 if (attr != nullptr)
c906108c 22360 {
e7c27a73 22361 var_decode_location (attr, sym, cu);
e142c38c 22362 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
22363
22364 /* Fortran explicitly imports any global symbols to the local
22365 scope by DW_TAG_common_block. */
22366 if (cu->language == language_fortran && die->parent
22367 && die->parent->tag == DW_TAG_common_block)
22368 attr2 = NULL;
22369
caac4577
JG
22370 if (SYMBOL_CLASS (sym) == LOC_STATIC
22371 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 22372 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
22373 {
22374 /* When a static variable is eliminated by the linker,
22375 the corresponding debug information is not stripped
22376 out, but the variable address is set to null;
22377 do not add such variables into symbol table. */
22378 }
c45bc3f8 22379 else if (attr2 != nullptr && attr2->as_boolean ())
1c809c68 22380 {
4b610737
TT
22381 if (SYMBOL_CLASS (sym) == LOC_STATIC
22382 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 22383 && per_objfile->per_bfd->can_copy)
4b610737
TT
22384 {
22385 /* A global static variable might be subject to
22386 copy relocation. We first check for a local
22387 minsym, though, because maybe the symbol was
22388 marked hidden, in which case this would not
22389 apply. */
22390 bound_minimal_symbol found
22391 = (lookup_minimal_symbol_linkage
987012b8 22392 (sym->linkage_name (), objfile));
4b610737
TT
22393 if (found.minsym != nullptr)
22394 sym->maybe_copied = 1;
22395 }
f55ee35c 22396
1c809c68
TT
22397 /* A variable with DW_AT_external is never static,
22398 but it may be block-scoped. */
804d2729 22399 list_to_add
c24bdb02
KS
22400 = ((cu->list_in_scope
22401 == cu->get_builder ()->get_file_symbols ())
22402 ? cu->get_builder ()->get_global_symbols ()
804d2729 22403 : cu->list_in_scope);
1c809c68 22404 }
c906108c 22405 else
e37fd15a 22406 list_to_add = cu->list_in_scope;
c906108c
SS
22407 }
22408 else
22409 {
22410 /* We do not know the address of this symbol.
dda83cd7
SM
22411 If it is an external symbol and we have type information
22412 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22413 The address of the variable will then be determined from
22414 the minimal symbol table whenever the variable is
22415 referenced. */
e142c38c 22416 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
22417
22418 /* Fortran explicitly imports any global symbols to the local
22419 scope by DW_TAG_common_block. */
22420 if (cu->language == language_fortran && die->parent
22421 && die->parent->tag == DW_TAG_common_block)
22422 {
22423 /* SYMBOL_CLASS doesn't matter here because
22424 read_common_block is going to reset it. */
22425 if (!suppress_add)
22426 list_to_add = cu->list_in_scope;
22427 }
c45bc3f8 22428 else if (attr2 != nullptr && attr2->as_boolean ()
0971de02 22429 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 22430 {
0fe7935b
DJ
22431 /* A variable with DW_AT_external is never static, but it
22432 may be block-scoped. */
804d2729 22433 list_to_add
c24bdb02
KS
22434 = ((cu->list_in_scope
22435 == cu->get_builder ()->get_file_symbols ())
22436 ? cu->get_builder ()->get_global_symbols ()
804d2729 22437 : cu->list_in_scope);
0fe7935b 22438
f1e6e072 22439 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 22440 }
442ddf59
JK
22441 else if (!die_is_declaration (die, cu))
22442 {
22443 /* Use the default LOC_OPTIMIZED_OUT class. */
22444 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22445 if (!suppress_add)
22446 list_to_add = cu->list_in_scope;
442ddf59 22447 }
c906108c
SS
22448 }
22449 break;
22450 case DW_TAG_formal_parameter:
a60f3166
TT
22451 {
22452 /* If we are inside a function, mark this as an argument. If
22453 not, we might be looking at an argument to an inlined function
22454 when we do not have enough information to show inlined frames;
22455 pretend it's a local variable in that case so that the user can
22456 still see it. */
804d2729 22457 struct context_stack *curr
c24bdb02 22458 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
22459 if (curr != nullptr && curr->name != nullptr)
22460 SYMBOL_IS_ARGUMENT (sym) = 1;
22461 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22462 if (attr != nullptr)
a60f3166
TT
22463 {
22464 var_decode_location (attr, sym, cu);
22465 }
22466 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22467 if (attr != nullptr)
a60f3166
TT
22468 {
22469 dwarf2_const_value (attr, sym, cu);
22470 }
f346a30d 22471
a60f3166
TT
22472 list_to_add = cu->list_in_scope;
22473 }
c906108c
SS
22474 break;
22475 case DW_TAG_unspecified_parameters:
22476 /* From varargs functions; gdb doesn't seem to have any
22477 interest in this information, so just ignore it for now.
22478 (FIXME?) */
22479 break;
34eaf542
TT
22480 case DW_TAG_template_type_param:
22481 suppress_add = 1;
22482 /* Fall through. */
c906108c 22483 case DW_TAG_class_type:
680b30c7 22484 case DW_TAG_interface_type:
c906108c
SS
22485 case DW_TAG_structure_type:
22486 case DW_TAG_union_type:
72019c9c 22487 case DW_TAG_set_type:
c906108c 22488 case DW_TAG_enumeration_type:
f1e6e072 22489 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22490 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22491
63d06c5c 22492 {
9c37b5ae 22493 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22494 really ever be static objects: otherwise, if you try
22495 to, say, break of a class's method and you're in a file
22496 which doesn't mention that class, it won't work unless
22497 the check for all static symbols in lookup_symbol_aux
22498 saves you. See the OtherFileClass tests in
22499 gdb.c++/namespace.exp. */
22500
e37fd15a 22501 if (!suppress_add)
34eaf542 22502 {
c24bdb02 22503 buildsym_compunit *builder = cu->get_builder ();
804d2729 22504 list_to_add
c24bdb02 22505 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22506 && cu->language == language_cplus
c24bdb02 22507 ? builder->get_global_symbols ()
804d2729 22508 : cu->list_in_scope);
63d06c5c 22509
64382290 22510 /* The semantics of C++ state that "struct foo {
9c37b5ae 22511 ... }" also defines a typedef for "foo". */
64382290 22512 if (cu->language == language_cplus
45280282 22513 || cu->language == language_ada
c44af4eb
TT
22514 || cu->language == language_d
22515 || cu->language == language_rust)
64382290
TT
22516 {
22517 /* The symbol's name is already allocated along
22518 with this objfile, so we don't need to
22519 duplicate it for the type. */
7d93a1e0 22520 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 22521 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 22522 }
63d06c5c
DC
22523 }
22524 }
c906108c
SS
22525 break;
22526 case DW_TAG_typedef:
f1e6e072 22527 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22528 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22529 list_to_add = cu->list_in_scope;
63d06c5c 22530 break;
d8f62e84 22531 case DW_TAG_array_type:
c906108c 22532 case DW_TAG_base_type:
dda83cd7 22533 case DW_TAG_subrange_type:
f1e6e072 22534 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22535 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22536 list_to_add = cu->list_in_scope;
c906108c
SS
22537 break;
22538 case DW_TAG_enumerator:
e142c38c 22539 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22540 if (attr != nullptr)
c906108c 22541 {
e7c27a73 22542 dwarf2_const_value (attr, sym, cu);
c906108c 22543 }
63d06c5c
DC
22544 {
22545 /* NOTE: carlton/2003-11-10: See comment above in the
22546 DW_TAG_class_type, etc. block. */
22547
804d2729 22548 list_to_add
c24bdb02 22549 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22550 && cu->language == language_cplus
c24bdb02 22551 ? cu->get_builder ()->get_global_symbols ()
804d2729 22552 : cu->list_in_scope);
63d06c5c 22553 }
c906108c 22554 break;
74921315 22555 case DW_TAG_imported_declaration:
5c4e30ca 22556 case DW_TAG_namespace:
f1e6e072 22557 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22558 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22559 break;
530e8392
KB
22560 case DW_TAG_module:
22561 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22562 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22563 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22564 break;
4357ac6c 22565 case DW_TAG_common_block:
f1e6e072 22566 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22567 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22568 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22569 break;
c906108c
SS
22570 default:
22571 /* Not a tag we recognize. Hopefully we aren't processing
22572 trash data, but since we must specifically ignore things
22573 we don't recognize, there is nothing else we should do at
0963b4bd 22574 this point. */
b98664d3 22575 complaint (_("unsupported tag: '%s'"),
4d3c2250 22576 dwarf_tag_name (die->tag));
c906108c
SS
22577 break;
22578 }
df8a16a1 22579
e37fd15a
SW
22580 if (suppress_add)
22581 {
22582 sym->hash_next = objfile->template_symbols;
22583 objfile->template_symbols = sym;
22584 list_to_add = NULL;
22585 }
22586
22587 if (list_to_add != NULL)
d3cb6808 22588 add_symbol_to_list (sym, list_to_add);
e37fd15a 22589
df8a16a1
DJ
22590 /* For the benefit of old versions of GCC, check for anonymous
22591 namespaces based on the demangled name. */
4d4ec4e5 22592 if (!cu->processing_has_namespace_info
94af9270 22593 && cu->language == language_cplus)
c24bdb02 22594 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22595 }
22596 return (sym);
22597}
22598
98bfdba5
PA
22599/* Given an attr with a DW_FORM_dataN value in host byte order,
22600 zero-extend it as appropriate for the symbol's type. The DWARF
22601 standard (v4) is not entirely clear about the meaning of using
22602 DW_FORM_dataN for a constant with a signed type, where the type is
22603 wider than the data. The conclusion of a discussion on the DWARF
22604 list was that this is unspecified. We choose to always zero-extend
22605 because that is the interpretation long in use by GCC. */
c906108c 22606
98bfdba5 22607static gdb_byte *
ff39bb5e 22608dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22609 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22610{
5e22e966 22611 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
22612 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22613 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
529908cb 22614 LONGEST l = attr->constant_value (0);
98bfdba5
PA
22615
22616 if (bits < sizeof (*value) * 8)
22617 {
22618 l &= ((LONGEST) 1 << bits) - 1;
22619 *value = l;
22620 }
22621 else if (bits == sizeof (*value) * 8)
22622 *value = l;
22623 else
22624 {
224c3ddb 22625 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22626 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22627 return bytes;
22628 }
22629
22630 return NULL;
22631}
22632
22633/* Read a constant value from an attribute. Either set *VALUE, or if
22634 the value does not fit in *VALUE, set *BYTES - either already
22635 allocated on the objfile obstack, or newly allocated on OBSTACK,
22636 or, set *BATON, if we translated the constant to a location
22637 expression. */
22638
22639static void
ff39bb5e 22640dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22641 const char *name, struct obstack *obstack,
22642 struct dwarf2_cu *cu,
d521ce57 22643 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22644 struct dwarf2_locexpr_baton **baton)
22645{
5e22e966 22646 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 22647 struct objfile *objfile = per_objfile->objfile;
98bfdba5 22648 struct comp_unit_head *cu_header = &cu->header;
c906108c 22649 struct dwarf_block *blk;
98bfdba5
PA
22650 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22651 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22652
22653 *value = 0;
22654 *bytes = NULL;
22655 *baton = NULL;
c906108c
SS
22656
22657 switch (attr->form)
22658 {
22659 case DW_FORM_addr:
336d760d 22660 case DW_FORM_addrx:
3019eac3 22661 case DW_FORM_GNU_addr_index:
ac56253d 22662 {
ac56253d
TT
22663 gdb_byte *data;
22664
98bfdba5
PA
22665 if (TYPE_LENGTH (type) != cu_header->addr_size)
22666 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22667 cu_header->addr_size,
98bfdba5 22668 TYPE_LENGTH (type));
ac56253d
TT
22669 /* Symbols of this form are reasonably rare, so we just
22670 piggyback on the existing location code rather than writing
22671 a new implementation of symbol_computed_ops. */
8d749320 22672 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 22673 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
22674 (*baton)->per_cu = cu->per_cu;
22675 gdb_assert ((*baton)->per_cu);
ac56253d 22676
98bfdba5 22677 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22678 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22679 (*baton)->data = data;
ac56253d
TT
22680
22681 data[0] = DW_OP_addr;
22682 store_unsigned_integer (&data[1], cu_header->addr_size,
36d378cf 22683 byte_order, attr->as_address ());
ac56253d 22684 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22685 }
c906108c 22686 break;
4ac36638 22687 case DW_FORM_string:
93b5768b 22688 case DW_FORM_strp:
cf532bd1 22689 case DW_FORM_strx:
3019eac3 22690 case DW_FORM_GNU_str_index:
36586728 22691 case DW_FORM_GNU_strp_alt:
c6481205 22692 /* The string is already allocated on the objfile obstack, point
98bfdba5 22693 directly to it. */
2c830f54 22694 *bytes = (const gdb_byte *) attr->as_string ();
93b5768b 22695 break;
c906108c
SS
22696 case DW_FORM_block1:
22697 case DW_FORM_block2:
22698 case DW_FORM_block4:
22699 case DW_FORM_block:
2dc7f7b3 22700 case DW_FORM_exprloc:
0224619f 22701 case DW_FORM_data16:
9d2246fc 22702 blk = attr->as_block ();
98bfdba5
PA
22703 if (TYPE_LENGTH (type) != blk->size)
22704 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22705 TYPE_LENGTH (type));
22706 *bytes = blk->data;
c906108c 22707 break;
2df3850c
JM
22708
22709 /* The DW_AT_const_value attributes are supposed to carry the
22710 symbol's value "represented as it would be on the target
22711 architecture." By the time we get here, it's already been
22712 converted to host endianness, so we just need to sign- or
22713 zero-extend it as appropriate. */
22714 case DW_FORM_data1:
3aef2284 22715 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22716 break;
c906108c 22717 case DW_FORM_data2:
3aef2284 22718 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22719 break;
c906108c 22720 case DW_FORM_data4:
3aef2284 22721 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22722 break;
c906108c 22723 case DW_FORM_data8:
3aef2284 22724 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22725 break;
22726
c906108c 22727 case DW_FORM_sdata:
663c44ac 22728 case DW_FORM_implicit_const:
1bc397c5 22729 *value = attr->as_signed ();
2df3850c
JM
22730 break;
22731
c906108c 22732 case DW_FORM_udata:
529908cb 22733 *value = attr->as_unsigned ();
c906108c 22734 break;
2df3850c 22735
c906108c 22736 default:
b98664d3 22737 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22738 dwarf_form_name (attr->form));
98bfdba5 22739 *value = 0;
c906108c
SS
22740 break;
22741 }
22742}
22743
2df3850c 22744
98bfdba5
PA
22745/* Copy constant value from an attribute to a symbol. */
22746
2df3850c 22747static void
ff39bb5e 22748dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22749 struct dwarf2_cu *cu)
2df3850c 22750{
5e22e966 22751 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 22752 LONGEST value;
d521ce57 22753 const gdb_byte *bytes;
98bfdba5 22754 struct dwarf2_locexpr_baton *baton;
2df3850c 22755
98bfdba5 22756 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22757 sym->print_name (),
98bfdba5
PA
22758 &objfile->objfile_obstack, cu,
22759 &value, &bytes, &baton);
2df3850c 22760
98bfdba5
PA
22761 if (baton != NULL)
22762 {
98bfdba5 22763 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22764 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22765 }
22766 else if (bytes != NULL)
22767 {
22768 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22769 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22770 }
22771 else
22772 {
22773 SYMBOL_VALUE (sym) = value;
f1e6e072 22774 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22775 }
2df3850c
JM
22776}
22777
c906108c
SS
22778/* Return the type of the die in question using its DW_AT_type attribute. */
22779
22780static struct type *
e7c27a73 22781die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22782{
c906108c 22783 struct attribute *type_attr;
c906108c 22784
e142c38c 22785 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22786 if (!type_attr)
22787 {
5e22e966 22788 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22789 /* A missing DW_AT_type represents a void type. */
518817b3 22790 return objfile_type (objfile)->builtin_void;
c906108c 22791 }
348e048f 22792
673bfd45 22793 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22794}
22795
b4ba55a1
JB
22796/* True iff CU's producer generates GNAT Ada auxiliary information
22797 that allows to find parallel types through that information instead
22798 of having to do expensive parallel lookups by type name. */
22799
22800static int
22801need_gnat_info (struct dwarf2_cu *cu)
22802{
de4cb04a
JB
22803 /* Assume that the Ada compiler was GNAT, which always produces
22804 the auxiliary information. */
22805 return (cu->language == language_ada);
b4ba55a1
JB
22806}
22807
b4ba55a1
JB
22808/* Return the auxiliary type of the die in question using its
22809 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22810 attribute is not present. */
22811
22812static struct type *
22813die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22814{
b4ba55a1 22815 struct attribute *type_attr;
b4ba55a1
JB
22816
22817 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22818 if (!type_attr)
22819 return NULL;
22820
673bfd45 22821 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22822}
22823
22824/* If DIE has a descriptive_type attribute, then set the TYPE's
22825 descriptive type accordingly. */
22826
22827static void
22828set_descriptive_type (struct type *type, struct die_info *die,
22829 struct dwarf2_cu *cu)
22830{
22831 struct type *descriptive_type = die_descriptive_type (die, cu);
22832
22833 if (descriptive_type)
22834 {
22835 ALLOCATE_GNAT_AUX_TYPE (type);
22836 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22837 }
22838}
22839
c906108c
SS
22840/* Return the containing type of the die in question using its
22841 DW_AT_containing_type attribute. */
22842
22843static struct type *
e7c27a73 22844die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22845{
c906108c 22846 struct attribute *type_attr;
5e22e966 22847 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22848
e142c38c 22849 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22850 if (!type_attr)
22851 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22852 "[in module %s]"), objfile_name (objfile));
33ac96f0 22853
673bfd45 22854 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22855}
22856
ac9ec31b
DE
22857/* Return an error marker type to use for the ill formed type in DIE/CU. */
22858
22859static struct type *
22860build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22861{
976ca316
SM
22862 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22863 struct objfile *objfile = per_objfile->objfile;
528e1572 22864 char *saved;
ac9ec31b 22865
528e1572
SM
22866 std::string message
22867 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22868 objfile_name (objfile),
22869 sect_offset_str (cu->header.sect_off),
22870 sect_offset_str (die->sect_off));
efba19b0 22871 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22872
19f392bc 22873 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22874}
22875
673bfd45 22876/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22877 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22878 DW_AT_containing_type.
673bfd45
DE
22879 If there is no type substitute an error marker. */
22880
c906108c 22881static struct type *
ff39bb5e 22882lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22883 struct dwarf2_cu *cu)
c906108c 22884{
976ca316
SM
22885 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22886 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
22887 struct type *this_type;
22888
ac9ec31b
DE
22889 gdb_assert (attr->name == DW_AT_type
22890 || attr->name == DW_AT_GNAT_descriptive_type
22891 || attr->name == DW_AT_containing_type);
22892
673bfd45
DE
22893 /* First see if we have it cached. */
22894
36586728
TT
22895 if (attr->form == DW_FORM_GNU_ref_alt)
22896 {
22897 struct dwarf2_per_cu_data *per_cu;
0826b30a 22898 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 22899
976ca316
SM
22900 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22901 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 22902 }
cd6c91b4 22903 else if (attr->form_is_ref ())
673bfd45 22904 {
0826b30a 22905 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 22906
976ca316 22907 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 22908 }
55f1336d 22909 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22910 {
630ed6b9 22911 ULONGEST signature = attr->as_signature ();
673bfd45 22912
ac9ec31b 22913 return get_signatured_type (die, signature, cu);
673bfd45
DE
22914 }
22915 else
22916 {
b98664d3 22917 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22918 " at %s [in module %s]"),
22919 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22920 objfile_name (objfile));
ac9ec31b 22921 return build_error_marker_type (cu, die);
673bfd45
DE
22922 }
22923
22924 /* If not cached we need to read it in. */
22925
22926 if (this_type == NULL)
22927 {
ac9ec31b 22928 struct die_info *type_die = NULL;
673bfd45
DE
22929 struct dwarf2_cu *type_cu = cu;
22930
cd6c91b4 22931 if (attr->form_is_ref ())
ac9ec31b
DE
22932 type_die = follow_die_ref (die, attr, &type_cu);
22933 if (type_die == NULL)
22934 return build_error_marker_type (cu, die);
22935 /* If we find the type now, it's probably because the type came
3019eac3
DE
22936 from an inter-CU reference and the type's CU got expanded before
22937 ours. */
ac9ec31b 22938 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22939 }
22940
22941 /* If we still don't have a type use an error marker. */
22942
22943 if (this_type == NULL)
ac9ec31b 22944 return build_error_marker_type (cu, die);
673bfd45 22945
f792889a 22946 return this_type;
c906108c
SS
22947}
22948
673bfd45
DE
22949/* Return the type in DIE, CU.
22950 Returns NULL for invalid types.
22951
02142a6c 22952 This first does a lookup in die_type_hash,
673bfd45
DE
22953 and only reads the die in if necessary.
22954
22955 NOTE: This can be called when reading in partial or full symbols. */
22956
f792889a 22957static struct type *
e7c27a73 22958read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22959{
f792889a
DJ
22960 struct type *this_type;
22961
22962 this_type = get_die_type (die, cu);
22963 if (this_type)
22964 return this_type;
22965
673bfd45
DE
22966 return read_type_die_1 (die, cu);
22967}
22968
22969/* Read the type in DIE, CU.
22970 Returns NULL for invalid types. */
22971
22972static struct type *
22973read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22974{
22975 struct type *this_type = NULL;
22976
c906108c
SS
22977 switch (die->tag)
22978 {
22979 case DW_TAG_class_type:
680b30c7 22980 case DW_TAG_interface_type:
c906108c
SS
22981 case DW_TAG_structure_type:
22982 case DW_TAG_union_type:
f792889a 22983 this_type = read_structure_type (die, cu);
c906108c
SS
22984 break;
22985 case DW_TAG_enumeration_type:
f792889a 22986 this_type = read_enumeration_type (die, cu);
c906108c
SS
22987 break;
22988 case DW_TAG_subprogram:
22989 case DW_TAG_subroutine_type:
edb3359d 22990 case DW_TAG_inlined_subroutine:
f792889a 22991 this_type = read_subroutine_type (die, cu);
c906108c
SS
22992 break;
22993 case DW_TAG_array_type:
f792889a 22994 this_type = read_array_type (die, cu);
c906108c 22995 break;
72019c9c 22996 case DW_TAG_set_type:
f792889a 22997 this_type = read_set_type (die, cu);
72019c9c 22998 break;
c906108c 22999 case DW_TAG_pointer_type:
f792889a 23000 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
23001 break;
23002 case DW_TAG_ptr_to_member_type:
f792889a 23003 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
23004 break;
23005 case DW_TAG_reference_type:
4297a3f0
AV
23006 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
23007 break;
23008 case DW_TAG_rvalue_reference_type:
23009 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
23010 break;
23011 case DW_TAG_const_type:
f792889a 23012 this_type = read_tag_const_type (die, cu);
c906108c
SS
23013 break;
23014 case DW_TAG_volatile_type:
f792889a 23015 this_type = read_tag_volatile_type (die, cu);
c906108c 23016 break;
06d66ee9
TT
23017 case DW_TAG_restrict_type:
23018 this_type = read_tag_restrict_type (die, cu);
23019 break;
c906108c 23020 case DW_TAG_string_type:
f792889a 23021 this_type = read_tag_string_type (die, cu);
c906108c
SS
23022 break;
23023 case DW_TAG_typedef:
f792889a 23024 this_type = read_typedef (die, cu);
c906108c 23025 break;
a02abb62 23026 case DW_TAG_subrange_type:
f792889a 23027 this_type = read_subrange_type (die, cu);
a02abb62 23028 break;
c906108c 23029 case DW_TAG_base_type:
f792889a 23030 this_type = read_base_type (die, cu);
c906108c 23031 break;
81a17f79 23032 case DW_TAG_unspecified_type:
f792889a 23033 this_type = read_unspecified_type (die, cu);
81a17f79 23034 break;
0114d602
DJ
23035 case DW_TAG_namespace:
23036 this_type = read_namespace_type (die, cu);
23037 break;
f55ee35c
JK
23038 case DW_TAG_module:
23039 this_type = read_module_type (die, cu);
23040 break;
a2c2acaf
MW
23041 case DW_TAG_atomic_type:
23042 this_type = read_tag_atomic_type (die, cu);
23043 break;
c906108c 23044 default:
b98664d3 23045 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 23046 dwarf_tag_name (die->tag));
c906108c
SS
23047 break;
23048 }
63d06c5c 23049
f792889a 23050 return this_type;
63d06c5c
DC
23051}
23052
abc72ce4
DE
23053/* See if we can figure out if the class lives in a namespace. We do
23054 this by looking for a member function; its demangled name will
23055 contain namespace info, if there is any.
23056 Return the computed name or NULL.
23057 Space for the result is allocated on the objfile's obstack.
23058 This is the full-die version of guess_partial_die_structure_name.
23059 In this case we know DIE has no useful parent. */
23060
43816ebc 23061static const char *
abc72ce4
DE
23062guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
23063{
23064 struct die_info *spec_die;
23065 struct dwarf2_cu *spec_cu;
23066 struct die_info *child;
5e22e966 23067 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
23068
23069 spec_cu = cu;
23070 spec_die = die_specification (die, &spec_cu);
23071 if (spec_die != NULL)
23072 {
23073 die = spec_die;
23074 cu = spec_cu;
23075 }
23076
23077 for (child = die->child;
23078 child != NULL;
23079 child = child->sibling)
23080 {
23081 if (child->tag == DW_TAG_subprogram)
23082 {
73b9be8b 23083 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 23084
7d45c7c3 23085 if (linkage_name != NULL)
abc72ce4 23086 {
43816ebc 23087 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 23088 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 23089 const char *name = NULL;
abc72ce4
DE
23090
23091 if (actual_name != NULL)
23092 {
15d034d0 23093 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
23094
23095 if (die_name != NULL
43816ebc 23096 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
23097 {
23098 /* Strip off the class name from the full name.
23099 We want the prefix. */
23100 int die_name_len = strlen (die_name);
43816ebc
TT
23101 int actual_name_len = strlen (actual_name.get ());
23102 const char *ptr = actual_name.get ();
abc72ce4
DE
23103
23104 /* Test for '::' as a sanity check. */
23105 if (actual_name_len > die_name_len + 2
43816ebc 23106 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 23107 name = obstack_strndup (
e3b94546 23108 &objfile->per_bfd->storage_obstack,
43816ebc 23109 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
23110 }
23111 }
abc72ce4
DE
23112 return name;
23113 }
23114 }
23115 }
23116
23117 return NULL;
23118}
23119
96408a79
SA
23120/* GCC might emit a nameless typedef that has a linkage name. Determine the
23121 prefix part in such case. See
23122 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23123
a121b7c1 23124static const char *
96408a79
SA
23125anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
23126{
23127 struct attribute *attr;
e6a959d6 23128 const char *base;
96408a79
SA
23129
23130 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
23131 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
23132 return NULL;
23133
7d45c7c3 23134 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
23135 return NULL;
23136
73b9be8b 23137 attr = dw2_linkage_name_attr (die, cu);
2c830f54
TT
23138 const char *attr_name = attr->as_string ();
23139 if (attr == NULL || attr_name == NULL)
96408a79
SA
23140 return NULL;
23141
23142 /* dwarf2_name had to be already called. */
3b64bf15 23143 gdb_assert (attr->canonical_string_p ());
96408a79
SA
23144
23145 /* Strip the base name, keep any leading namespaces/classes. */
2c830f54
TT
23146 base = strrchr (attr_name, ':');
23147 if (base == NULL || base == attr_name || base[-1] != ':')
96408a79
SA
23148 return "";
23149
5e22e966 23150 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9 23151 return obstack_strndup (&objfile->per_bfd->storage_obstack,
2c830f54
TT
23152 attr_name,
23153 &base[-1] - attr_name);
96408a79
SA
23154}
23155
fdde2d81 23156/* Return the name of the namespace/class that DIE is defined within,
0114d602 23157 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 23158
0114d602
DJ
23159 For example, if we're within the method foo() in the following
23160 code:
23161
23162 namespace N {
23163 class C {
23164 void foo () {
23165 }
23166 };
23167 }
23168
23169 then determine_prefix on foo's die will return "N::C". */
fdde2d81 23170
0d5cff50 23171static const char *
e142c38c 23172determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 23173{
976ca316 23174 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
23175 struct die_info *parent, *spec_die;
23176 struct dwarf2_cu *spec_cu;
23177 struct type *parent_type;
a121b7c1 23178 const char *retval;
63d06c5c 23179
9c37b5ae 23180 if (cu->language != language_cplus
c44af4eb
TT
23181 && cu->language != language_fortran && cu->language != language_d
23182 && cu->language != language_rust)
0114d602
DJ
23183 return "";
23184
96408a79
SA
23185 retval = anonymous_struct_prefix (die, cu);
23186 if (retval)
23187 return retval;
23188
0114d602
DJ
23189 /* We have to be careful in the presence of DW_AT_specification.
23190 For example, with GCC 3.4, given the code
23191
23192 namespace N {
23193 void foo() {
23194 // Definition of N::foo.
23195 }
23196 }
23197
23198 then we'll have a tree of DIEs like this:
23199
23200 1: DW_TAG_compile_unit
23201 2: DW_TAG_namespace // N
23202 3: DW_TAG_subprogram // declaration of N::foo
23203 4: DW_TAG_subprogram // definition of N::foo
23204 DW_AT_specification // refers to die #3
23205
23206 Thus, when processing die #4, we have to pretend that we're in
23207 the context of its DW_AT_specification, namely the contex of die
23208 #3. */
23209 spec_cu = cu;
23210 spec_die = die_specification (die, &spec_cu);
23211 if (spec_die == NULL)
23212 parent = die->parent;
23213 else
63d06c5c 23214 {
0114d602
DJ
23215 parent = spec_die->parent;
23216 cu = spec_cu;
63d06c5c 23217 }
0114d602
DJ
23218
23219 if (parent == NULL)
23220 return "";
98bfdba5
PA
23221 else if (parent->building_fullname)
23222 {
23223 const char *name;
23224 const char *parent_name;
23225
23226 /* It has been seen on RealView 2.2 built binaries,
23227 DW_TAG_template_type_param types actually _defined_ as
23228 children of the parent class:
23229
23230 enum E {};
23231 template class <class Enum> Class{};
23232 Class<enum E> class_e;
23233
dda83cd7
SM
23234 1: DW_TAG_class_type (Class)
23235 2: DW_TAG_enumeration_type (E)
23236 3: DW_TAG_enumerator (enum1:0)
23237 3: DW_TAG_enumerator (enum2:1)
23238 ...
23239 2: DW_TAG_template_type_param
23240 DW_AT_type DW_FORM_ref_udata (E)
98bfdba5
PA
23241
23242 Besides being broken debug info, it can put GDB into an
23243 infinite loop. Consider:
23244
23245 When we're building the full name for Class<E>, we'll start
23246 at Class, and go look over its template type parameters,
23247 finding E. We'll then try to build the full name of E, and
23248 reach here. We're now trying to build the full name of E,
23249 and look over the parent DIE for containing scope. In the
23250 broken case, if we followed the parent DIE of E, we'd again
23251 find Class, and once again go look at its template type
23252 arguments, etc., etc. Simply don't consider such parent die
23253 as source-level parent of this die (it can't be, the language
23254 doesn't allow it), and break the loop here. */
23255 name = dwarf2_name (die, cu);
23256 parent_name = dwarf2_name (parent, cu);
b98664d3 23257 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
23258 name ? name : "<unknown>",
23259 parent_name ? parent_name : "<unknown>");
23260 return "";
23261 }
63d06c5c 23262 else
0114d602
DJ
23263 switch (parent->tag)
23264 {
63d06c5c 23265 case DW_TAG_namespace:
0114d602 23266 parent_type = read_type_die (parent, cu);
acebe513
UW
23267 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23268 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23269 Work around this problem here. */
23270 if (cu->language == language_cplus
7d93a1e0 23271 && strcmp (parent_type->name (), "::") == 0)
acebe513 23272 return "";
0114d602 23273 /* We give a name to even anonymous namespaces. */
7d93a1e0 23274 return parent_type->name ();
63d06c5c 23275 case DW_TAG_class_type:
680b30c7 23276 case DW_TAG_interface_type:
63d06c5c 23277 case DW_TAG_structure_type:
0114d602 23278 case DW_TAG_union_type:
f55ee35c 23279 case DW_TAG_module:
0114d602 23280 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
23281 if (parent_type->name () != NULL)
23282 return parent_type->name ();
0114d602
DJ
23283 else
23284 /* An anonymous structure is only allowed non-static data
23285 members; no typedefs, no member functions, et cetera.
23286 So it does not need a prefix. */
23287 return "";
abc72ce4 23288 case DW_TAG_compile_unit:
95554aad 23289 case DW_TAG_partial_unit:
abc72ce4
DE
23290 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23291 if (cu->language == language_cplus
976ca316 23292 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
23293 && die->child != NULL
23294 && (die->tag == DW_TAG_class_type
23295 || die->tag == DW_TAG_structure_type
23296 || die->tag == DW_TAG_union_type))
23297 {
43816ebc 23298 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
23299 if (name != NULL)
23300 return name;
23301 }
23302 return "";
0a4b0913
AB
23303 case DW_TAG_subprogram:
23304 /* Nested subroutines in Fortran get a prefix with the name
23305 of the parent's subroutine. */
23306 if (cu->language == language_fortran)
23307 {
23308 if ((die->tag == DW_TAG_subprogram)
23309 && (dwarf2_name (parent, cu) != NULL))
23310 return dwarf2_name (parent, cu);
23311 }
23312 return determine_prefix (parent, cu);
3d567982
TT
23313 case DW_TAG_enumeration_type:
23314 parent_type = read_type_die (parent, cu);
3bc440a2 23315 if (parent_type->is_declared_class ())
3d567982 23316 {
7d93a1e0
SM
23317 if (parent_type->name () != NULL)
23318 return parent_type->name ();
3d567982
TT
23319 return "";
23320 }
23321 /* Fall through. */
63d06c5c 23322 default:
8176b9b8 23323 return determine_prefix (parent, cu);
63d06c5c 23324 }
63d06c5c
DC
23325}
23326
3e43a32a
MS
23327/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23328 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23329 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23330 an obconcat, otherwise allocate storage for the result. The CU argument is
23331 used to determine the language and hence, the appropriate separator. */
987504bb 23332
f55ee35c 23333#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
23334
23335static char *
f55ee35c 23336typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
dda83cd7 23337 int physname, struct dwarf2_cu *cu)
63d06c5c 23338{
f55ee35c 23339 const char *lead = "";
5c315b68 23340 const char *sep;
63d06c5c 23341
3e43a32a
MS
23342 if (suffix == NULL || suffix[0] == '\0'
23343 || prefix == NULL || prefix[0] == '\0')
987504bb 23344 sep = "";
45280282
IB
23345 else if (cu->language == language_d)
23346 {
23347 /* For D, the 'main' function could be defined in any module, but it
23348 should never be prefixed. */
23349 if (strcmp (suffix, "D main") == 0)
23350 {
23351 prefix = "";
23352 sep = "";
23353 }
23354 else
23355 sep = ".";
23356 }
f55ee35c
JK
23357 else if (cu->language == language_fortran && physname)
23358 {
23359 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23360 DW_AT_MIPS_linkage_name is preferred and used instead. */
23361
23362 lead = "__";
23363 sep = "_MOD_";
23364 }
987504bb
JJ
23365 else
23366 sep = "::";
63d06c5c 23367
6dd47d34
DE
23368 if (prefix == NULL)
23369 prefix = "";
23370 if (suffix == NULL)
23371 suffix = "";
23372
987504bb
JJ
23373 if (obs == NULL)
23374 {
3e43a32a 23375 char *retval
224c3ddb
SM
23376 = ((char *)
23377 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 23378
f55ee35c
JK
23379 strcpy (retval, lead);
23380 strcat (retval, prefix);
6dd47d34
DE
23381 strcat (retval, sep);
23382 strcat (retval, suffix);
63d06c5c
DC
23383 return retval;
23384 }
987504bb
JJ
23385 else
23386 {
23387 /* We have an obstack. */
f55ee35c 23388 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 23389 }
63d06c5c
DC
23390}
23391
71c25dea
TT
23392/* Get name of a die, return NULL if not found. */
23393
15d034d0
TT
23394static const char *
23395dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 23396 struct objfile *objfile)
71c25dea
TT
23397{
23398 if (name && cu->language == language_cplus)
23399 {
596dc4ad
TT
23400 gdb::unique_xmalloc_ptr<char> canon_name
23401 = cp_canonicalize_string (name);
71c25dea 23402
596dc4ad
TT
23403 if (canon_name != nullptr)
23404 name = objfile->intern (canon_name.get ());
71c25dea
TT
23405 }
23406
23407 return name;
c906108c
SS
23408}
23409
96553a0c
DE
23410/* Get name of a die, return NULL if not found.
23411 Anonymous namespaces are converted to their magic string. */
9219021c 23412
15d034d0 23413static const char *
e142c38c 23414dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
23415{
23416 struct attribute *attr;
5e22e966 23417 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 23418
e142c38c 23419 attr = dwarf2_attr (die, DW_AT_name, cu);
2c830f54
TT
23420 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23421 if (attr_name == nullptr
96553a0c 23422 && die->tag != DW_TAG_namespace
53832f31
TT
23423 && die->tag != DW_TAG_class_type
23424 && die->tag != DW_TAG_interface_type
23425 && die->tag != DW_TAG_structure_type
23426 && die->tag != DW_TAG_union_type)
71c25dea
TT
23427 return NULL;
23428
23429 switch (die->tag)
23430 {
23431 case DW_TAG_compile_unit:
95554aad 23432 case DW_TAG_partial_unit:
71c25dea
TT
23433 /* Compilation units have a DW_AT_name that is a filename, not
23434 a source language identifier. */
23435 case DW_TAG_enumeration_type:
23436 case DW_TAG_enumerator:
23437 /* These tags always have simple identifiers already; no need
23438 to canonicalize them. */
2c830f54 23439 return attr_name;
907af001 23440
96553a0c 23441 case DW_TAG_namespace:
2c830f54
TT
23442 if (attr_name != nullptr)
23443 return attr_name;
96553a0c
DE
23444 return CP_ANONYMOUS_NAMESPACE_STR;
23445
907af001
UW
23446 case DW_TAG_class_type:
23447 case DW_TAG_interface_type:
23448 case DW_TAG_structure_type:
23449 case DW_TAG_union_type:
23450 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23451 structures or unions. These were of the form "._%d" in GCC 4.1,
23452 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23453 and GCC 4.4. We work around this problem by ignoring these. */
2c830f54
TT
23454 if (attr_name != nullptr
23455 && (startswith (attr_name, "._")
23456 || startswith (attr_name, "<anonymous")))
907af001 23457 return NULL;
53832f31
TT
23458
23459 /* GCC might emit a nameless typedef that has a linkage name. See
23460 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
2c830f54 23461 if (!attr || attr_name == NULL)
53832f31 23462 {
73b9be8b 23463 attr = dw2_linkage_name_attr (die, cu);
95eb9e54 23464 attr_name = attr == nullptr ? nullptr : attr->as_string ();
2c830f54 23465 if (attr == NULL || attr_name == NULL)
53832f31
TT
23466 return NULL;
23467
2c830f54 23468 /* Avoid demangling attr_name the second time on a second
df5c6c50 23469 call for the same DIE. */
3b64bf15 23470 if (!attr->canonical_string_p ())
53832f31 23471 {
43816ebc 23472 gdb::unique_xmalloc_ptr<char> demangled
2c830f54 23473 (gdb_demangle (attr_name, DMGL_TYPES));
4f180d53
AT
23474 if (demangled == nullptr)
23475 return nullptr;
43816ebc 23476
c6481205 23477 attr->set_string_canonical (objfile->intern (demangled.get ()));
95eb9e54 23478 attr_name = attr->as_string ();
53832f31 23479 }
67430cd0 23480
2c830f54
TT
23481 /* Strip any leading namespaces/classes, keep only the
23482 base name. DW_AT_name for named DIEs does not
23483 contain the prefixes. */
23484 const char *base = strrchr (attr_name, ':');
23485 if (base && base > attr_name && base[-1] == ':')
67430cd0
TT
23486 return &base[1];
23487 else
2c830f54 23488 return attr_name;
53832f31 23489 }
907af001
UW
23490 break;
23491
71c25dea 23492 default:
907af001
UW
23493 break;
23494 }
23495
3b64bf15 23496 if (!attr->canonical_string_p ())
c6481205
TT
23497 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23498 objfile));
2c830f54 23499 return attr->as_string ();
9219021c
DC
23500}
23501
23502/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23503 is none. *EXT_CU is the CU containing DIE on input, and the CU
23504 containing the return value on output. */
9219021c
DC
23505
23506static struct die_info *
f2f0e013 23507dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23508{
23509 struct attribute *attr;
9219021c 23510
f2f0e013 23511 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23512 if (attr == NULL)
23513 return NULL;
23514
f2f0e013 23515 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23516}
23517
f9aca02d 23518static void
d97bc12b 23519dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23520{
23521 unsigned int i;
23522
d97bc12b 23523 print_spaces (indent, f);
9d8780f0 23524 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23525 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23526 sect_offset_str (die->sect_off));
d97bc12b
DE
23527
23528 if (die->parent != NULL)
23529 {
23530 print_spaces (indent, f);
9d8780f0
SM
23531 fprintf_unfiltered (f, " parent at offset: %s\n",
23532 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23533 }
23534
23535 print_spaces (indent, f);
23536 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23537 dwarf_bool_name (die->child != NULL));
c906108c 23538
d97bc12b
DE
23539 print_spaces (indent, f);
23540 fprintf_unfiltered (f, " attributes:\n");
23541
c906108c
SS
23542 for (i = 0; i < die->num_attrs; ++i)
23543 {
d97bc12b
DE
23544 print_spaces (indent, f);
23545 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23546 dwarf_attr_name (die->attrs[i].name),
23547 dwarf_form_name (die->attrs[i].form));
d97bc12b 23548
c906108c
SS
23549 switch (die->attrs[i].form)
23550 {
c906108c 23551 case DW_FORM_addr:
336d760d 23552 case DW_FORM_addrx:
3019eac3 23553 case DW_FORM_GNU_addr_index:
d97bc12b 23554 fprintf_unfiltered (f, "address: ");
36d378cf 23555 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
c906108c
SS
23556 break;
23557 case DW_FORM_block2:
23558 case DW_FORM_block4:
23559 case DW_FORM_block:
23560 case DW_FORM_block1:
56eb65bd 23561 fprintf_unfiltered (f, "block: size %s",
9d2246fc 23562 pulongest (die->attrs[i].as_block ()->size));
c906108c 23563 break;
2dc7f7b3 23564 case DW_FORM_exprloc:
56eb65bd 23565 fprintf_unfiltered (f, "expression: size %s",
9d2246fc 23566 pulongest (die->attrs[i].as_block ()->size));
2dc7f7b3 23567 break;
0224619f
JK
23568 case DW_FORM_data16:
23569 fprintf_unfiltered (f, "constant of 16 bytes");
23570 break;
4568ecf9
DE
23571 case DW_FORM_ref_addr:
23572 fprintf_unfiltered (f, "ref address: ");
529908cb 23573 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
4568ecf9 23574 break;
36586728
TT
23575 case DW_FORM_GNU_ref_alt:
23576 fprintf_unfiltered (f, "alt ref address: ");
529908cb 23577 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
36586728 23578 break;
10b3939b
DJ
23579 case DW_FORM_ref1:
23580 case DW_FORM_ref2:
23581 case DW_FORM_ref4:
4568ecf9
DE
23582 case DW_FORM_ref8:
23583 case DW_FORM_ref_udata:
d97bc12b 23584 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
529908cb 23585 (long) (die->attrs[i].as_unsigned ()));
10b3939b 23586 break;
c906108c
SS
23587 case DW_FORM_data1:
23588 case DW_FORM_data2:
23589 case DW_FORM_data4:
ce5d95e1 23590 case DW_FORM_data8:
c906108c 23591 case DW_FORM_udata:
43bbcdc2 23592 fprintf_unfiltered (f, "constant: %s",
529908cb 23593 pulongest (die->attrs[i].as_unsigned ()));
c906108c 23594 break;
2dc7f7b3
TT
23595 case DW_FORM_sec_offset:
23596 fprintf_unfiltered (f, "section offset: %s",
529908cb 23597 pulongest (die->attrs[i].as_unsigned ()));
2dc7f7b3 23598 break;
55f1336d 23599 case DW_FORM_ref_sig8:
ac9ec31b 23600 fprintf_unfiltered (f, "signature: %s",
630ed6b9 23601 hex_string (die->attrs[i].as_signature ()));
348e048f 23602 break;
c906108c 23603 case DW_FORM_string:
4bdf3d34 23604 case DW_FORM_strp:
43988095 23605 case DW_FORM_line_strp:
cf532bd1 23606 case DW_FORM_strx:
3019eac3 23607 case DW_FORM_GNU_str_index:
36586728 23608 case DW_FORM_GNU_strp_alt:
8285870a 23609 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c6481205
TT
23610 die->attrs[i].as_string ()
23611 ? die->attrs[i].as_string () : "",
23612 die->attrs[i].canonical_string_p () ? "is" : "not");
c906108c
SS
23613 break;
23614 case DW_FORM_flag:
c45bc3f8 23615 if (die->attrs[i].as_boolean ())
d97bc12b 23616 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23617 else
d97bc12b 23618 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23619 break;
2dc7f7b3
TT
23620 case DW_FORM_flag_present:
23621 fprintf_unfiltered (f, "flag: TRUE");
23622 break;
a8329558 23623 case DW_FORM_indirect:
0963b4bd
MS
23624 /* The reader will have reduced the indirect form to
23625 the "base form" so this form should not occur. */
5f48f8f3 23626 fprintf_unfiltered (f,
3e43a32a 23627 "unexpected attribute form: DW_FORM_indirect");
a8329558 23628 break;
1bc397c5 23629 case DW_FORM_sdata:
663c44ac
JK
23630 case DW_FORM_implicit_const:
23631 fprintf_unfiltered (f, "constant: %s",
1bc397c5 23632 plongest (die->attrs[i].as_signed ()));
663c44ac 23633 break;
c906108c 23634 default:
d97bc12b 23635 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23636 die->attrs[i].form);
d97bc12b 23637 break;
c906108c 23638 }
d97bc12b 23639 fprintf_unfiltered (f, "\n");
c906108c
SS
23640 }
23641}
23642
f9aca02d 23643static void
d97bc12b 23644dump_die_for_error (struct die_info *die)
c906108c 23645{
d97bc12b
DE
23646 dump_die_shallow (gdb_stderr, 0, die);
23647}
23648
23649static void
23650dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23651{
23652 int indent = level * 4;
23653
23654 gdb_assert (die != NULL);
23655
23656 if (level >= max_level)
23657 return;
23658
23659 dump_die_shallow (f, indent, die);
23660
23661 if (die->child != NULL)
c906108c 23662 {
d97bc12b
DE
23663 print_spaces (indent, f);
23664 fprintf_unfiltered (f, " Children:");
23665 if (level + 1 < max_level)
23666 {
23667 fprintf_unfiltered (f, "\n");
23668 dump_die_1 (f, level + 1, max_level, die->child);
23669 }
23670 else
23671 {
3e43a32a
MS
23672 fprintf_unfiltered (f,
23673 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23674 }
23675 }
23676
23677 if (die->sibling != NULL && level > 0)
23678 {
23679 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23680 }
23681}
23682
d97bc12b
DE
23683/* This is called from the pdie macro in gdbinit.in.
23684 It's not static so gcc will keep a copy callable from gdb. */
23685
23686void
23687dump_die (struct die_info *die, int max_level)
23688{
23689 dump_die_1 (gdb_stdlog, 0, max_level, die);
23690}
23691
f9aca02d 23692static void
51545339 23693store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23694{
51545339 23695 void **slot;
c906108c 23696
9c541725
PA
23697 slot = htab_find_slot_with_hash (cu->die_hash, die,
23698 to_underlying (die->sect_off),
b64f50a1 23699 INSERT);
51545339
DJ
23700
23701 *slot = die;
c906108c
SS
23702}
23703
348e048f
DE
23704/* Follow reference or signature attribute ATTR of SRC_DIE.
23705 On entry *REF_CU is the CU of SRC_DIE.
23706 On exit *REF_CU is the CU of the result. */
23707
23708static struct die_info *
ff39bb5e 23709follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23710 struct dwarf2_cu **ref_cu)
23711{
23712 struct die_info *die;
23713
cd6c91b4 23714 if (attr->form_is_ref ())
348e048f 23715 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23716 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23717 die = follow_die_sig (src_die, attr, ref_cu);
23718 else
23719 {
23720 dump_die_for_error (src_die);
23721 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 23722 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
23723 }
23724
23725 return die;
03dd20cc
DJ
23726}
23727
5c631832 23728/* Follow reference OFFSET.
673bfd45
DE
23729 On entry *REF_CU is the CU of the source die referencing OFFSET.
23730 On exit *REF_CU is the CU of the result.
23731 Returns NULL if OFFSET is invalid. */
f504f079 23732
f9aca02d 23733static struct die_info *
9c541725 23734follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23735 struct dwarf2_cu **ref_cu)
c906108c 23736{
10b3939b 23737 struct die_info temp_die;
f2f0e013 23738 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 23739 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 23740
348e048f
DE
23741 gdb_assert (cu->per_cu != NULL);
23742
98bfdba5
PA
23743 target_cu = cu;
23744
17e593e9
SM
23745 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23746 "source CU contains target offset: %d",
23747 sect_offset_str (cu->per_cu->sect_off),
23748 sect_offset_str (sect_off),
23749 cu->header.offset_in_cu_p (sect_off));
23750
3019eac3 23751 if (cu->per_cu->is_debug_types)
348e048f
DE
23752 {
23753 /* .debug_types CUs cannot reference anything outside their CU.
23754 If they need to, they have to reference a signatured type via
55f1336d 23755 DW_FORM_ref_sig8. */
4057dfde 23756 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 23757 return NULL;
348e048f 23758 }
36586728 23759 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 23760 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
23761 {
23762 struct dwarf2_per_cu_data *per_cu;
9a619af0 23763
9c541725 23764 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 23765 per_objfile);
03dd20cc 23766
17e593e9
SM
23767 dwarf_read_debug_printf_v ("target CU offset: %s, "
23768 "target CU DIEs loaded: %d",
23769 sect_offset_str (per_cu->sect_off),
23770 per_objfile->get_cu (per_cu) != nullptr);
23771
616c069a
SM
23772 /* If necessary, add it to the queue and load its DIEs.
23773
23774 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23775 it doesn't mean they are currently loaded. Since we require them
23776 to be loaded, we must check for ourselves. */
23777 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23778 || per_objfile->get_cu (per_cu) == nullptr)
4a636814
SM
23779 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23780 false, cu->language);
03dd20cc 23781
976ca316 23782 target_cu = per_objfile->get_cu (per_cu);
616c069a 23783 gdb_assert (target_cu != nullptr);
10b3939b 23784 }
98bfdba5
PA
23785 else if (cu->dies == NULL)
23786 {
23787 /* We're loading full DIEs during partial symbol reading. */
976ca316 23788 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
4a636814
SM
23789 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23790 language_minimal);
98bfdba5 23791 }
c906108c 23792
f2f0e013 23793 *ref_cu = target_cu;
9c541725 23794 temp_die.sect_off = sect_off;
c24bdb02
KS
23795
23796 if (target_cu != cu)
23797 target_cu->ancestor = cu;
23798
9a3c8263 23799 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23800 &temp_die,
23801 to_underlying (sect_off));
5c631832 23802}
10b3939b 23803
5c631832
JK
23804/* Follow reference attribute ATTR of SRC_DIE.
23805 On entry *REF_CU is the CU of SRC_DIE.
23806 On exit *REF_CU is the CU of the result. */
23807
23808static struct die_info *
ff39bb5e 23809follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23810 struct dwarf2_cu **ref_cu)
23811{
0826b30a 23812 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
23813 struct dwarf2_cu *cu = *ref_cu;
23814 struct die_info *die;
23815
9c541725 23816 die = follow_die_offset (sect_off,
36586728
TT
23817 (attr->form == DW_FORM_GNU_ref_alt
23818 || cu->per_cu->is_dwz),
23819 ref_cu);
5c631832 23820 if (!die)
9d8780f0
SM
23821 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23822 "at %s [in module %s]"),
23823 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 23824 objfile_name (cu->per_objfile->objfile));
348e048f 23825
5c631832
JK
23826 return die;
23827}
23828
d4c9a4f8 23829/* See read.h. */
5c631832
JK
23830
23831struct dwarf2_locexpr_baton
9c541725 23832dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 23833 dwarf2_per_cu_data *per_cu,
976ca316 23834 dwarf2_per_objfile *per_objfile,
041d9819
SM
23835 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23836 bool resolve_abstract_p)
5c631832 23837{
5c631832
JK
23838 struct die_info *die;
23839 struct attribute *attr;
23840 struct dwarf2_locexpr_baton retval;
976ca316 23841 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 23842
976ca316 23843 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 23844 if (cu == nullptr)
976ca316 23845 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
23846
23847 if (cu == nullptr)
cc12ce38
DE
23848 {
23849 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23850 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23851 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23852 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23853 }
918dd910 23854
9c541725 23855 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23856 if (!die)
9d8780f0
SM
23857 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23858 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23859
23860 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23861 if (!attr && resolve_abstract_p
976ca316
SM
23862 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23863 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65 23864 {
041d9819 23865 CORE_ADDR pc = get_frame_pc ();
b3b3bada 23866 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 23867 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 23868
3360b6e7 23869 for (const auto &cand_off
976ca316 23870 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 23871 {
3360b6e7
TV
23872 struct dwarf2_cu *cand_cu = cu;
23873 struct die_info *cand
23874 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23875 if (!cand
23876 || !cand->parent
e4a62c65
TV
23877 || cand->parent->tag != DW_TAG_subprogram)
23878 continue;
23879
23880 CORE_ADDR pc_low, pc_high;
23881 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23882 if (pc_low == ((CORE_ADDR) -1))
23883 continue;
23884 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23885 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23886 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23887 continue;
23888
23889 die = cand;
23890 attr = dwarf2_attr (die, DW_AT_location, cu);
23891 break;
23892 }
23893 }
23894
5c631832
JK
23895 if (!attr)
23896 {
e103e986
JK
23897 /* DWARF: "If there is no such attribute, then there is no effect.".
23898 DATA is ignored if SIZE is 0. */
5c631832 23899
e103e986 23900 retval.data = NULL;
5c631832
JK
23901 retval.size = 0;
23902 }
cd6c91b4 23903 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23904 {
23905 struct dwarf2_loclist_baton loclist_baton;
041d9819 23906 CORE_ADDR pc = get_frame_pc ();
8cf6f0b1
TT
23907 size_t size;
23908
23909 fill_in_loclist_baton (cu, &loclist_baton, attr);
23910
23911 retval.data = dwarf2_find_location_expression (&loclist_baton,
23912 &size, pc);
23913 retval.size = size;
23914 }
5c631832
JK
23915 else
23916 {
4fc6c0d5 23917 if (!attr->form_is_block ())
9d8780f0 23918 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23919 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23920 sect_offset_str (sect_off), objfile_name (objfile));
5c631832 23921
9d2246fc
TT
23922 struct dwarf_block *block = attr->as_block ();
23923 retval.data = block->data;
23924 retval.size = block->size;
5c631832 23925 }
976ca316 23926 retval.per_objfile = per_objfile;
5c631832 23927 retval.per_cu = cu->per_cu;
918dd910 23928
976ca316 23929 per_objfile->age_comp_units ();
918dd910 23930
5c631832 23931 return retval;
348e048f
DE
23932}
23933
d4c9a4f8 23934/* See read.h. */
8b9737bf
TT
23935
23936struct dwarf2_locexpr_baton
23937dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 23938 dwarf2_per_cu_data *per_cu,
14095eb3 23939 dwarf2_per_objfile *per_objfile,
041d9819 23940 gdb::function_view<CORE_ADDR ()> get_frame_pc)
8b9737bf 23941{
9c541725 23942 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23943
14095eb3 23944 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
041d9819 23945 get_frame_pc);
8b9737bf
TT
23946}
23947
b6807d98
TT
23948/* Write a constant of a given type as target-ordered bytes into
23949 OBSTACK. */
23950
23951static const gdb_byte *
23952write_constant_as_bytes (struct obstack *obstack,
23953 enum bfd_endian byte_order,
23954 struct type *type,
23955 ULONGEST value,
23956 LONGEST *len)
23957{
23958 gdb_byte *result;
23959
23960 *len = TYPE_LENGTH (type);
224c3ddb 23961 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23962 store_unsigned_integer (result, *len, byte_order, value);
23963
23964 return result;
23965}
23966
d4c9a4f8 23967/* See read.h. */
b6807d98
TT
23968
23969const gdb_byte *
9c541725 23970dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 23971 dwarf2_per_cu_data *per_cu,
14095eb3 23972 dwarf2_per_objfile *per_objfile,
d4c9a4f8 23973 obstack *obstack,
b6807d98
TT
23974 LONGEST *len)
23975{
b6807d98
TT
23976 struct die_info *die;
23977 struct attribute *attr;
23978 const gdb_byte *result = NULL;
23979 struct type *type;
23980 LONGEST value;
23981 enum bfd_endian byte_order;
14095eb3 23982 struct objfile *objfile = per_objfile->objfile;
b6807d98 23983
7188ed02 23984 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23985 if (cu == nullptr)
23986 cu = load_cu (per_cu, per_objfile, false);
23987
23988 if (cu == nullptr)
cc12ce38
DE
23989 {
23990 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23991 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23992 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23993 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23994 }
b6807d98 23995
9c541725 23996 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23997 if (!die)
9d8780f0
SM
23998 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23999 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
24000
24001 attr = dwarf2_attr (die, DW_AT_const_value, cu);
24002 if (attr == NULL)
24003 return NULL;
24004
e3b94546 24005 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
24006 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
24007
24008 switch (attr->form)
24009 {
24010 case DW_FORM_addr:
336d760d 24011 case DW_FORM_addrx:
b6807d98
TT
24012 case DW_FORM_GNU_addr_index:
24013 {
24014 gdb_byte *tem;
24015
24016 *len = cu->header.addr_size;
224c3ddb 24017 tem = (gdb_byte *) obstack_alloc (obstack, *len);
36d378cf 24018 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
b6807d98
TT
24019 result = tem;
24020 }
24021 break;
24022 case DW_FORM_string:
24023 case DW_FORM_strp:
cf532bd1 24024 case DW_FORM_strx:
b6807d98
TT
24025 case DW_FORM_GNU_str_index:
24026 case DW_FORM_GNU_strp_alt:
c6481205 24027 /* The string is already allocated on the objfile obstack, point
b6807d98 24028 directly to it. */
2c830f54
TT
24029 {
24030 const char *attr_name = attr->as_string ();
24031 result = (const gdb_byte *) attr_name;
24032 *len = strlen (attr_name);
24033 }
b6807d98
TT
24034 break;
24035 case DW_FORM_block1:
24036 case DW_FORM_block2:
24037 case DW_FORM_block4:
24038 case DW_FORM_block:
24039 case DW_FORM_exprloc:
0224619f 24040 case DW_FORM_data16:
9d2246fc
TT
24041 {
24042 struct dwarf_block *block = attr->as_block ();
24043 result = block->data;
24044 *len = block->size;
24045 }
b6807d98
TT
24046 break;
24047
24048 /* The DW_AT_const_value attributes are supposed to carry the
24049 symbol's value "represented as it would be on the target
24050 architecture." By the time we get here, it's already been
24051 converted to host endianness, so we just need to sign- or
24052 zero-extend it as appropriate. */
24053 case DW_FORM_data1:
24054 type = die_type (die, cu);
24055 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
24056 if (result == NULL)
24057 result = write_constant_as_bytes (obstack, byte_order,
24058 type, value, len);
24059 break;
24060 case DW_FORM_data2:
24061 type = die_type (die, cu);
24062 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
24063 if (result == NULL)
24064 result = write_constant_as_bytes (obstack, byte_order,
24065 type, value, len);
24066 break;
24067 case DW_FORM_data4:
24068 type = die_type (die, cu);
24069 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
24070 if (result == NULL)
24071 result = write_constant_as_bytes (obstack, byte_order,
24072 type, value, len);
24073 break;
24074 case DW_FORM_data8:
24075 type = die_type (die, cu);
24076 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
24077 if (result == NULL)
24078 result = write_constant_as_bytes (obstack, byte_order,
24079 type, value, len);
24080 break;
24081
24082 case DW_FORM_sdata:
663c44ac 24083 case DW_FORM_implicit_const:
b6807d98
TT
24084 type = die_type (die, cu);
24085 result = write_constant_as_bytes (obstack, byte_order,
1bc397c5 24086 type, attr->as_signed (), len);
b6807d98
TT
24087 break;
24088
24089 case DW_FORM_udata:
24090 type = die_type (die, cu);
24091 result = write_constant_as_bytes (obstack, byte_order,
529908cb 24092 type, attr->as_unsigned (), len);
b6807d98
TT
24093 break;
24094
24095 default:
b98664d3 24096 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
24097 dwarf_form_name (attr->form));
24098 break;
24099 }
24100
24101 return result;
24102}
24103
d4c9a4f8 24104/* See read.h. */
7942e96e
AA
24105
24106struct type *
9c541725 24107dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
24108 dwarf2_per_cu_data *per_cu,
24109 dwarf2_per_objfile *per_objfile)
7942e96e 24110{
7942e96e
AA
24111 struct die_info *die;
24112
7188ed02 24113 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
24114 if (cu == nullptr)
24115 cu = load_cu (per_cu, per_objfile, false);
24116
24117 if (cu == nullptr)
24118 return nullptr;
7942e96e 24119
9c541725 24120 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
24121 if (!die)
24122 return NULL;
24123
24124 return die_type (die, cu);
24125}
24126
8cb5117c 24127/* See read.h. */
8a9b8146
TT
24128
24129struct type *
b64f50a1 24130dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
24131 dwarf2_per_cu_data *per_cu,
24132 dwarf2_per_objfile *per_objfile)
8a9b8146 24133{
9c541725 24134 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 24135 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
24136}
24137
ac9ec31b 24138/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 24139 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
24140 On exit *REF_CU is the CU of the result.
24141 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
24142
24143static struct die_info *
ac9ec31b
DE
24144follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
24145 struct dwarf2_cu **ref_cu)
348e048f 24146{
348e048f 24147 struct die_info temp_die;
c24bdb02 24148 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 24149 struct die_info *die;
976ca316 24150 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 24151
348e048f 24152
ac9ec31b
DE
24153 /* While it might be nice to assert sig_type->type == NULL here,
24154 we can get here for DW_AT_imported_declaration where we need
24155 the DIE not the type. */
348e048f 24156
616c069a 24157 /* If necessary, add it to the queue and load its DIEs.
348e048f 24158
616c069a
SM
24159 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
24160 it doesn't mean they are currently loaded. Since we require them
24161 to be loaded, we must check for ourselves. */
976ca316 24162 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
616c069a
SM
24163 language_minimal)
24164 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
976ca316 24165 read_signatured_type (sig_type, per_objfile);
348e048f 24166
976ca316 24167 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
69d751e3 24168 gdb_assert (sig_cu != NULL);
9c541725
PA
24169 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
24170 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 24171 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 24172 to_underlying (temp_die.sect_off));
348e048f
DE
24173 if (die)
24174 {
796a7ff8
DE
24175 /* For .gdb_index version 7 keep track of included TUs.
24176 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
24177 if (per_objfile->per_bfd->index_table != NULL
24178 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 24179 {
ae640021 24180 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
24181 }
24182
348e048f 24183 *ref_cu = sig_cu;
c24bdb02
KS
24184 if (sig_cu != cu)
24185 sig_cu->ancestor = cu;
24186
348e048f
DE
24187 return die;
24188 }
24189
ac9ec31b
DE
24190 return NULL;
24191}
24192
24193/* Follow signatured type referenced by ATTR in SRC_DIE.
24194 On entry *REF_CU is the CU of SRC_DIE.
24195 On exit *REF_CU is the CU of the result.
24196 The result is the DIE of the type.
24197 If the referenced type cannot be found an error is thrown. */
24198
24199static struct die_info *
ff39bb5e 24200follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
24201 struct dwarf2_cu **ref_cu)
24202{
630ed6b9 24203 ULONGEST signature = attr->as_signature ();
ac9ec31b
DE
24204 struct signatured_type *sig_type;
24205 struct die_info *die;
24206
24207 gdb_assert (attr->form == DW_FORM_ref_sig8);
24208
a2ce51a0 24209 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
24210 /* sig_type will be NULL if the signatured type is missing from
24211 the debug info. */
24212 if (sig_type == NULL)
24213 {
24214 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
dda83cd7
SM
24215 " from DIE at %s [in module %s]"),
24216 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 24217 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
24218 }
24219
24220 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24221 if (die == NULL)
24222 {
24223 dump_die_for_error (src_die);
24224 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24225 " from DIE at %s [in module %s]"),
24226 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 24227 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
24228 }
24229
24230 return die;
24231}
24232
24233/* Get the type specified by SIGNATURE referenced in DIE/CU,
24234 reading in and processing the type unit if necessary. */
24235
24236static struct type *
24237get_signatured_type (struct die_info *die, ULONGEST signature,
24238 struct dwarf2_cu *cu)
24239{
976ca316 24240 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
24241 struct signatured_type *sig_type;
24242 struct dwarf2_cu *type_cu;
24243 struct die_info *type_die;
24244 struct type *type;
24245
a2ce51a0 24246 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
24247 /* sig_type will be NULL if the signatured type is missing from
24248 the debug info. */
24249 if (sig_type == NULL)
24250 {
b98664d3 24251 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
24252 " from DIE at %s [in module %s]"),
24253 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24254 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24255 return build_error_marker_type (cu, die);
24256 }
24257
24258 /* If we already know the type we're done. */
976ca316 24259 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
24260 if (type != nullptr)
24261 return type;
ac9ec31b
DE
24262
24263 type_cu = cu;
24264 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24265 if (type_die != NULL)
24266 {
24267 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24268 is created. This is important, for example, because for c++ classes
24269 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24270 type = read_type_die (type_die, type_cu);
24271 if (type == NULL)
24272 {
b98664d3 24273 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
24274 " referenced from DIE at %s [in module %s]"),
24275 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24276 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24277 type = build_error_marker_type (cu, die);
24278 }
24279 }
24280 else
24281 {
b98664d3 24282 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24283 " from DIE at %s [in module %s]"),
24284 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24285 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24286 type = build_error_marker_type (cu, die);
24287 }
e286671b 24288
976ca316 24289 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
24290
24291 return type;
24292}
24293
24294/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24295 reading in and processing the type unit if necessary. */
24296
24297static struct type *
ff39bb5e 24298get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 24299 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
24300{
24301 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 24302 if (attr->form_is_ref ())
ac9ec31b
DE
24303 {
24304 struct dwarf2_cu *type_cu = cu;
24305 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24306
24307 return read_type_die (type_die, type_cu);
24308 }
24309 else if (attr->form == DW_FORM_ref_sig8)
24310 {
630ed6b9 24311 return get_signatured_type (die, attr->as_signature (), cu);
ac9ec31b
DE
24312 }
24313 else
24314 {
976ca316 24315 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24316
b98664d3 24317 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
24318 " at %s [in module %s]"),
24319 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 24320 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24321 return build_error_marker_type (cu, die);
24322 }
348e048f
DE
24323}
24324
e5fe5e75 24325/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
24326
24327static void
ab432490
SM
24328load_full_type_unit (dwarf2_per_cu_data *per_cu,
24329 dwarf2_per_objfile *per_objfile)
348e048f 24330{
52dc124a 24331 struct signatured_type *sig_type;
348e048f 24332
f4dc4d17 24333 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 24334 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 24335
6721b2ec
DE
24336 /* We have the per_cu, but we need the signatured_type.
24337 Fortunately this is an easy translation. */
24338 gdb_assert (per_cu->is_debug_types);
24339 sig_type = (struct signatured_type *) per_cu;
348e048f 24340
7188ed02 24341 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24342
ab432490 24343 read_signatured_type (sig_type, per_objfile);
348e048f 24344
7188ed02 24345 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
24346}
24347
3019eac3
DE
24348/* Read in a signatured type and build its CU and DIEs.
24349 If the type is a stub for the real type in a DWO file,
24350 read in the real type from the DWO file as well. */
dee91e82
DE
24351
24352static void
ab432490
SM
24353read_signatured_type (signatured_type *sig_type,
24354 dwarf2_per_objfile *per_objfile)
dee91e82
DE
24355{
24356 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24357
3019eac3 24358 gdb_assert (per_cu->is_debug_types);
7188ed02 24359 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24360
2e671100 24361 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
24362
24363 if (!reader.dummy_p)
24364 {
24365 struct dwarf2_cu *cu = reader.cu;
24366 const gdb_byte *info_ptr = reader.info_ptr;
24367
24368 gdb_assert (cu->die_hash == NULL);
24369 cu->die_hash =
24370 htab_create_alloc_ex (cu->header.length / 12,
24371 die_hash,
24372 die_eq,
24373 NULL,
24374 &cu->comp_unit_obstack,
24375 hashtab_obstack_allocate,
24376 dummy_obstack_deallocate);
24377
3e225074 24378 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
24379 reader.comp_unit_die->child
24380 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24381 reader.comp_unit_die);
24382 cu->dies = reader.comp_unit_die;
24383 /* comp_unit_die is not stored in die_hash, no need. */
24384
24385 /* We try not to read any attributes in this function, because
24386 not all CUs needed for references have been loaded yet, and
24387 symbol table processing isn't initialized. But we have to
24388 set the CU language, or we won't be able to build types
24389 correctly. Similarly, if we do not read the producer, we can
24390 not apply producer-specific interpretation. */
24391 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
24392
24393 reader.keep ();
c0ab21c2
TT
24394 }
24395
7ee85ab1 24396 sig_type->per_cu.tu_read = 1;
c906108c
SS
24397}
24398
c906108c
SS
24399/* Decode simple location descriptions.
24400 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
24401 the location and return the value. If COMPUTED is non-null, it is
24402 set to true to indicate that decoding was successful, and false
24403 otherwise. If COMPUTED is null, then this function may emit a
24404 complaint. */
c906108c
SS
24405
24406static CORE_ADDR
7d79de9a 24407decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 24408{
5e22e966 24409 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
24410 size_t i;
24411 size_t size = blk->size;
d521ce57 24412 const gdb_byte *data = blk->data;
21ae7a4d
JK
24413 CORE_ADDR stack[64];
24414 int stacki;
24415 unsigned int bytes_read, unsnd;
24416 gdb_byte op;
c906108c 24417
7d79de9a
TT
24418 if (computed != nullptr)
24419 *computed = false;
24420
21ae7a4d
JK
24421 i = 0;
24422 stacki = 0;
24423 stack[stacki] = 0;
24424 stack[++stacki] = 0;
24425
24426 while (i < size)
24427 {
24428 op = data[i++];
24429 switch (op)
24430 {
24431 case DW_OP_lit0:
24432 case DW_OP_lit1:
24433 case DW_OP_lit2:
24434 case DW_OP_lit3:
24435 case DW_OP_lit4:
24436 case DW_OP_lit5:
24437 case DW_OP_lit6:
24438 case DW_OP_lit7:
24439 case DW_OP_lit8:
24440 case DW_OP_lit9:
24441 case DW_OP_lit10:
24442 case DW_OP_lit11:
24443 case DW_OP_lit12:
24444 case DW_OP_lit13:
24445 case DW_OP_lit14:
24446 case DW_OP_lit15:
24447 case DW_OP_lit16:
24448 case DW_OP_lit17:
24449 case DW_OP_lit18:
24450 case DW_OP_lit19:
24451 case DW_OP_lit20:
24452 case DW_OP_lit21:
24453 case DW_OP_lit22:
24454 case DW_OP_lit23:
24455 case DW_OP_lit24:
24456 case DW_OP_lit25:
24457 case DW_OP_lit26:
24458 case DW_OP_lit27:
24459 case DW_OP_lit28:
24460 case DW_OP_lit29:
24461 case DW_OP_lit30:
24462 case DW_OP_lit31:
24463 stack[++stacki] = op - DW_OP_lit0;
24464 break;
f1bea926 24465
21ae7a4d
JK
24466 case DW_OP_reg0:
24467 case DW_OP_reg1:
24468 case DW_OP_reg2:
24469 case DW_OP_reg3:
24470 case DW_OP_reg4:
24471 case DW_OP_reg5:
24472 case DW_OP_reg6:
24473 case DW_OP_reg7:
24474 case DW_OP_reg8:
24475 case DW_OP_reg9:
24476 case DW_OP_reg10:
24477 case DW_OP_reg11:
24478 case DW_OP_reg12:
24479 case DW_OP_reg13:
24480 case DW_OP_reg14:
24481 case DW_OP_reg15:
24482 case DW_OP_reg16:
24483 case DW_OP_reg17:
24484 case DW_OP_reg18:
24485 case DW_OP_reg19:
24486 case DW_OP_reg20:
24487 case DW_OP_reg21:
24488 case DW_OP_reg22:
24489 case DW_OP_reg23:
24490 case DW_OP_reg24:
24491 case DW_OP_reg25:
24492 case DW_OP_reg26:
24493 case DW_OP_reg27:
24494 case DW_OP_reg28:
24495 case DW_OP_reg29:
24496 case DW_OP_reg30:
24497 case DW_OP_reg31:
24498 stack[++stacki] = op - DW_OP_reg0;
24499 if (i < size)
7d79de9a
TT
24500 {
24501 if (computed == nullptr)
24502 dwarf2_complex_location_expr_complaint ();
24503 else
24504 return 0;
24505 }
21ae7a4d 24506 break;
c906108c 24507
21ae7a4d
JK
24508 case DW_OP_regx:
24509 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24510 i += bytes_read;
24511 stack[++stacki] = unsnd;
24512 if (i < size)
7d79de9a
TT
24513 {
24514 if (computed == nullptr)
24515 dwarf2_complex_location_expr_complaint ();
24516 else
24517 return 0;
24518 }
21ae7a4d 24519 break;
c906108c 24520
21ae7a4d 24521 case DW_OP_addr:
c8a7a66f
TT
24522 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24523 &bytes_read);
21ae7a4d
JK
24524 i += bytes_read;
24525 break;
d53d4ac5 24526
21ae7a4d
JK
24527 case DW_OP_const1u:
24528 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24529 i += 1;
24530 break;
24531
24532 case DW_OP_const1s:
24533 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24534 i += 1;
24535 break;
24536
24537 case DW_OP_const2u:
24538 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24539 i += 2;
24540 break;
24541
24542 case DW_OP_const2s:
24543 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24544 i += 2;
24545 break;
d53d4ac5 24546
21ae7a4d
JK
24547 case DW_OP_const4u:
24548 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24549 i += 4;
24550 break;
24551
24552 case DW_OP_const4s:
24553 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24554 i += 4;
24555 break;
24556
585861ea
JK
24557 case DW_OP_const8u:
24558 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24559 i += 8;
24560 break;
24561
21ae7a4d
JK
24562 case DW_OP_constu:
24563 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24564 &bytes_read);
24565 i += bytes_read;
24566 break;
24567
24568 case DW_OP_consts:
24569 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24570 i += bytes_read;
24571 break;
24572
24573 case DW_OP_dup:
24574 stack[stacki + 1] = stack[stacki];
24575 stacki++;
24576 break;
24577
24578 case DW_OP_plus:
24579 stack[stacki - 1] += stack[stacki];
24580 stacki--;
24581 break;
24582
24583 case DW_OP_plus_uconst:
24584 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24585 &bytes_read);
24586 i += bytes_read;
24587 break;
24588
24589 case DW_OP_minus:
24590 stack[stacki - 1] -= stack[stacki];
24591 stacki--;
24592 break;
24593
24594 case DW_OP_deref:
24595 /* If we're not the last op, then we definitely can't encode
24596 this using GDB's address_class enum. This is valid for partial
24597 global symbols, although the variable's address will be bogus
24598 in the psymtab. */
24599 if (i < size)
7d79de9a
TT
24600 {
24601 if (computed == nullptr)
24602 dwarf2_complex_location_expr_complaint ();
24603 else
24604 return 0;
24605 }
21ae7a4d
JK
24606 break;
24607
dda83cd7 24608 case DW_OP_GNU_push_tls_address:
4aa4e28b 24609 case DW_OP_form_tls_address:
21ae7a4d
JK
24610 /* The top of the stack has the offset from the beginning
24611 of the thread control block at which the variable is located. */
24612 /* Nothing should follow this operator, so the top of stack would
24613 be returned. */
24614 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24615 address will be bogus in the psymtab. Make it always at least
24616 non-zero to not look as a variable garbage collected by linker
24617 which have DW_OP_addr 0. */
21ae7a4d 24618 if (i < size)
7d79de9a
TT
24619 {
24620 if (computed == nullptr)
24621 dwarf2_complex_location_expr_complaint ();
24622 else
24623 return 0;
24624 }
585861ea 24625 stack[stacki]++;
dda83cd7 24626 break;
21ae7a4d
JK
24627
24628 case DW_OP_GNU_uninit:
7d79de9a
TT
24629 if (computed != nullptr)
24630 return 0;
21ae7a4d
JK
24631 break;
24632
336d760d 24633 case DW_OP_addrx:
3019eac3 24634 case DW_OP_GNU_addr_index:
49f6c839 24635 case DW_OP_GNU_const_index:
3019eac3
DE
24636 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24637 &bytes_read);
24638 i += bytes_read;
24639 break;
24640
21ae7a4d 24641 default:
7d79de9a
TT
24642 if (computed == nullptr)
24643 {
24644 const char *name = get_DW_OP_name (op);
21ae7a4d 24645
7d79de9a
TT
24646 if (name)
24647 complaint (_("unsupported stack op: '%s'"),
24648 name);
24649 else
24650 complaint (_("unsupported stack op: '%02x'"),
24651 op);
24652 }
21ae7a4d
JK
24653
24654 return (stack[stacki]);
d53d4ac5 24655 }
3c6e0cb3 24656
21ae7a4d 24657 /* Enforce maximum stack depth of SIZE-1 to avoid writing
dda83cd7 24658 outside of the allocated space. Also enforce minimum>0. */
21ae7a4d
JK
24659 if (stacki >= ARRAY_SIZE (stack) - 1)
24660 {
7d79de9a
TT
24661 if (computed == nullptr)
24662 complaint (_("location description stack overflow"));
21ae7a4d
JK
24663 return 0;
24664 }
24665
24666 if (stacki <= 0)
24667 {
7d79de9a
TT
24668 if (computed == nullptr)
24669 complaint (_("location description stack underflow"));
21ae7a4d
JK
24670 return 0;
24671 }
24672 }
7d79de9a
TT
24673
24674 if (computed != nullptr)
24675 *computed = true;
21ae7a4d 24676 return (stack[stacki]);
c906108c
SS
24677}
24678
24679/* memory allocation interface */
24680
c906108c 24681static struct dwarf_block *
7b5a2f43 24682dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24683{
8d749320 24684 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24685}
24686
c906108c 24687static struct die_info *
b60c80d6 24688dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24689{
24690 struct die_info *die;
b60c80d6
DJ
24691 size_t size = sizeof (struct die_info);
24692
24693 if (num_attrs > 1)
24694 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24695
b60c80d6 24696 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24697 memset (die, 0, sizeof (struct die_info));
24698 return (die);
24699}
2e276125
JB
24700
24701\f
a036ba48 24702
c90ec28a 24703/* Macro support. */
cf2c3c16 24704
9eac9650
TT
24705/* An overload of dwarf_decode_macros that finds the correct section
24706 and ensures it is read in before calling the other overload. */
24707
24708static void
24709dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24710 int section_is_gnu)
24711{
976ca316
SM
24712 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24713 struct objfile *objfile = per_objfile->objfile;
5a0e026f 24714 const struct line_header *lh = cu->line_header;
9eac9650
TT
24715 unsigned int offset_size = cu->header.offset_size;
24716 struct dwarf2_section_info *section;
24717 const char *section_name;
24718
24719 if (cu->dwo_unit != nullptr)
24720 {
24721 if (section_is_gnu)
24722 {
24723 section = &cu->dwo_unit->dwo_file->sections.macro;
24724 section_name = ".debug_macro.dwo";
24725 }
24726 else
24727 {
24728 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24729 section_name = ".debug_macinfo.dwo";
24730 }
24731 }
24732 else
24733 {
24734 if (section_is_gnu)
24735 {
976ca316 24736 section = &per_objfile->per_bfd->macro;
9eac9650
TT
24737 section_name = ".debug_macro";
24738 }
24739 else
24740 {
976ca316 24741 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
24742 section_name = ".debug_macinfo";
24743 }
24744 }
24745
24746 section->read (objfile);
24747 if (section->buffer == nullptr)
24748 {
24749 complaint (_("missing %s section"), section_name);
24750 return;
24751 }
24752
24753 buildsym_compunit *builder = cu->get_builder ();
24754
048fde1e 24755 struct dwarf2_section_info *str_offsets_section;
24756 struct dwarf2_section_info *str_section;
24757 ULONGEST str_offsets_base;
24758
24759 if (cu->dwo_unit != nullptr)
24760 {
24761 str_offsets_section = &cu->dwo_unit->dwo_file
24762 ->sections.str_offsets;
24763 str_section = &cu->dwo_unit->dwo_file->sections.str;
24764 str_offsets_base = cu->header.addr_size;
24765 }
24766 else
24767 {
24768 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24769 str_section = &per_objfile->per_bfd->str;
24770 str_offsets_base = *cu->str_offsets_base;
24771 }
24772
976ca316 24773 dwarf_decode_macros (per_objfile, builder, section, lh,
048fde1e 24774 offset_size, offset, str_section, str_offsets_section,
24775 str_offsets_base, section_is_gnu);
9eac9650
TT
24776}
24777
3019eac3
DE
24778/* Return the .debug_loc section to use for CU.
24779 For DWO files use .debug_loc.dwo. */
24780
24781static struct dwarf2_section_info *
24782cu_debug_loc_section (struct dwarf2_cu *cu)
24783{
976ca316 24784 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24785
3019eac3 24786 if (cu->dwo_unit)
43988095
JK
24787 {
24788 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24789
43988095
JK
24790 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24791 }
976ca316
SM
24792 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24793 : &per_objfile->per_bfd->loc);
3019eac3
DE
24794}
24795
d0ce17d8
CT
24796/* Return the .debug_rnglists section to use for CU. */
24797static struct dwarf2_section_info *
24798cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24799{
24800 if (cu->header.version < 5)
24801 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24802 cu->header.version);
24803 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24804
24805 /* Make sure we read the .debug_rnglists section from the file that
24806 contains the DW_AT_ranges attribute we are reading. Normally that
24807 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24808 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24809 program. */
24810 if (cu->dwo_unit != nullptr
24811 && tag != DW_TAG_compile_unit
24812 && tag != DW_TAG_skeleton_unit)
24813 {
24814 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24815
24816 if (sections->rnglists.size > 0)
24817 return &sections->rnglists;
24818 else
24819 error (_(".debug_rnglists section is missing from .dwo file."));
24820 }
24821 return &dwarf2_per_objfile->per_bfd->rnglists;
24822}
24823
8cf6f0b1
TT
24824/* A helper function that fills in a dwarf2_loclist_baton. */
24825
24826static void
24827fill_in_loclist_baton (struct dwarf2_cu *cu,
24828 struct dwarf2_loclist_baton *baton,
ff39bb5e 24829 const struct attribute *attr)
8cf6f0b1 24830{
976ca316 24831 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
24832 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24833
976ca316 24834 section->read (per_objfile->objfile);
8cf6f0b1 24835
976ca316 24836 baton->per_objfile = per_objfile;
8cf6f0b1
TT
24837 baton->per_cu = cu->per_cu;
24838 gdb_assert (baton->per_cu);
24839 /* We don't know how long the location list is, but make sure we
24840 don't run off the edge of the section. */
d4df075e
TT
24841 baton->size = section->size - attr->as_unsigned ();
24842 baton->data = section->buffer + attr->as_unsigned ();
2b24b6e4
TT
24843 if (cu->base_address.has_value ())
24844 baton->base_address = *cu->base_address;
24845 else
24846 baton->base_address = 0;
f664829e 24847 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24848}
24849
4c2df51b 24850static void
ff39bb5e 24851dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24852 struct dwarf2_cu *cu, int is_block)
4c2df51b 24853{
976ca316
SM
24854 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24855 struct objfile *objfile = per_objfile->objfile;
3019eac3 24856 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24857
cd6c91b4 24858 if (attr->form_is_section_offset ()
3019eac3 24859 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24860 the section. If so, fall through to the complaint in the
24861 other branch. */
d4df075e 24862 && attr->as_unsigned () < section->get_size (objfile))
4c2df51b 24863 {
0d53c4c4 24864 struct dwarf2_loclist_baton *baton;
4c2df51b 24865
8d749320 24866 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24867
8cf6f0b1 24868 fill_in_loclist_baton (cu, baton, attr);
be391dca 24869
2b24b6e4 24870 if (!cu->base_address.has_value ())
b98664d3 24871 complaint (_("Location list used without "
3e43a32a 24872 "specifying the CU base address."));
4c2df51b 24873
f1e6e072
TT
24874 SYMBOL_ACLASS_INDEX (sym) = (is_block
24875 ? dwarf2_loclist_block_index
24876 : dwarf2_loclist_index);
0d53c4c4
DJ
24877 SYMBOL_LOCATION_BATON (sym) = baton;
24878 }
24879 else
24880 {
24881 struct dwarf2_locexpr_baton *baton;
24882
8d749320 24883 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 24884 baton->per_objfile = per_objfile;
ae0d2f24
UW
24885 baton->per_cu = cu->per_cu;
24886 gdb_assert (baton->per_cu);
0d53c4c4 24887
4fc6c0d5 24888 if (attr->form_is_block ())
0d53c4c4
DJ
24889 {
24890 /* Note that we're just copying the block's data pointer
24891 here, not the actual data. We're still pointing into the
6502dd73
DJ
24892 info_buffer for SYM's objfile; right now we never release
24893 that buffer, but when we do clean up properly this may
24894 need to change. */
9d2246fc
TT
24895 struct dwarf_block *block = attr->as_block ();
24896 baton->size = block->size;
24897 baton->data = block->data;
0d53c4c4
DJ
24898 }
24899 else
24900 {
24901 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24902 sym->natural_name ());
0d53c4c4 24903 baton->size = 0;
0d53c4c4 24904 }
6e70227d 24905
f1e6e072
TT
24906 SYMBOL_ACLASS_INDEX (sym) = (is_block
24907 ? dwarf2_locexpr_block_index
24908 : dwarf2_locexpr_index);
0d53c4c4
DJ
24909 SYMBOL_LOCATION_BATON (sym) = baton;
24910 }
4c2df51b 24911}
6502dd73 24912
2e6a9f79 24913/* See read.h. */
96408a79 24914
2e6a9f79
SM
24915const comp_unit_head *
24916dwarf2_per_cu_data::get_header () const
96408a79 24917{
2e6a9f79
SM
24918 if (!m_header_read_in)
24919 {
24920 const gdb_byte *info_ptr
24921 = this->section->buffer + to_underlying (this->sect_off);
96408a79 24922
2e6a9f79 24923 memset (&m_header, 0, sizeof (m_header));
96408a79 24924
2e6a9f79
SM
24925 read_comp_unit_head (&m_header, info_ptr, this->section,
24926 rcuh_kind::COMPILE);
a9f172c6
TT
24927
24928 m_header_read_in = true;
2e6a9f79 24929 }
96408a79 24930
2e6a9f79 24931 return &m_header;
96408a79
SA
24932}
24933
09ba997f 24934/* See read.h. */
ae0d2f24 24935
98714339 24936int
09ba997f 24937dwarf2_per_cu_data::addr_size () const
ae0d2f24 24938{
2e6a9f79 24939 return this->get_header ()->addr_size;
ae0d2f24
UW
24940}
24941
09ba997f 24942/* See read.h. */
9eae7c52
TT
24943
24944int
09ba997f 24945dwarf2_per_cu_data::offset_size () const
9eae7c52 24946{
2e6a9f79 24947 return this->get_header ()->offset_size;
96408a79
SA
24948}
24949
09ba997f 24950/* See read.h. */
96408a79
SA
24951
24952int
09ba997f 24953dwarf2_per_cu_data::ref_addr_size () const
96408a79 24954{
2e6a9f79 24955 const comp_unit_head *header = this->get_header ();
96408a79 24956
2e6a9f79
SM
24957 if (header->version == 2)
24958 return header->addr_size;
96408a79 24959 else
2e6a9f79 24960 return header->offset_size;
181cebd4
JK
24961}
24962
09ba997f 24963/* See read.h. */
9aa1f1e3 24964
09ba997f 24965struct type *
293e7e51 24966dwarf2_cu::addr_type () const
9a49df9d 24967{
293e7e51 24968 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
24969 struct type *void_type = objfile_type (objfile)->builtin_void;
24970 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 24971 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
24972
24973 if (TYPE_LENGTH (addr_type) == addr_size)
24974 return addr_type;
24975
c6d940a9 24976 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
9a49df9d
AB
24977 return addr_type;
24978}
24979
22b6cd70
TT
24980/* A helper function for dwarf2_find_containing_comp_unit that returns
24981 the index of the result, and that searches a vector. It will
24982 return a result even if the offset in question does not actually
24983 occur in any CU. This is separate so that it can be unit
24984 tested. */
ae038cb0 24985
22b6cd70
TT
24986static int
24987dwarf2_find_containing_comp_unit
24988 (sect_offset sect_off,
24989 unsigned int offset_in_dwz,
24990 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 24991{
ae038cb0
DJ
24992 int low, high;
24993
ae038cb0 24994 low = 0;
22b6cd70 24995 high = all_comp_units.size () - 1;
ae038cb0
DJ
24996 while (high > low)
24997 {
36586728 24998 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24999 int mid = low + (high - low) / 2;
9a619af0 25000
22b6cd70 25001 mid_cu = all_comp_units[mid];
36586728 25002 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 25003 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 25004 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
25005 high = mid;
25006 else
25007 low = mid + 1;
25008 }
25009 gdb_assert (low == high);
22b6cd70
TT
25010 return low;
25011}
25012
25013/* Locate the .debug_info compilation unit from CU's objfile which contains
25014 the DIE at OFFSET. Raises an error on failure. */
25015
25016static struct dwarf2_per_cu_data *
25017dwarf2_find_containing_comp_unit (sect_offset sect_off,
25018 unsigned int offset_in_dwz,
976ca316 25019 dwarf2_per_objfile *per_objfile)
22b6cd70 25020{
976ca316
SM
25021 int low = dwarf2_find_containing_comp_unit
25022 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
25023 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
22b6cd70 25024
45b8ae0c 25025 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 25026 {
36586728 25027 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25028 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25029 "offset %s [in module %s]"),
25030 sect_offset_str (sect_off),
976ca316 25031 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 25032
976ca316 25033 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 25034 <= sect_off);
976ca316 25035 return per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
25036 }
25037 else
25038 {
976ca316 25039 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 25040 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25041 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25042 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25043 return this_cu;
25044 }
25045}
25046
22b6cd70
TT
25047#if GDB_SELF_TEST
25048
25049namespace selftests {
25050namespace find_containing_comp_unit {
25051
25052static void
25053run_test ()
25054{
25055 struct dwarf2_per_cu_data one {};
25056 struct dwarf2_per_cu_data two {};
25057 struct dwarf2_per_cu_data three {};
25058 struct dwarf2_per_cu_data four {};
25059
25060 one.length = 5;
25061 two.sect_off = sect_offset (one.length);
25062 two.length = 7;
25063
25064 three.length = 5;
25065 three.is_dwz = 1;
25066 four.sect_off = sect_offset (three.length);
25067 four.length = 7;
25068 four.is_dwz = 1;
25069
25070 std::vector<dwarf2_per_cu_data *> units;
25071 units.push_back (&one);
25072 units.push_back (&two);
25073 units.push_back (&three);
25074 units.push_back (&four);
25075
25076 int result;
25077
25078 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
25079 SELF_CHECK (units[result] == &one);
25080 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
25081 SELF_CHECK (units[result] == &one);
25082 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
25083 SELF_CHECK (units[result] == &two);
25084
25085 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
25086 SELF_CHECK (units[result] == &three);
25087 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
25088 SELF_CHECK (units[result] == &three);
25089 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
25090 SELF_CHECK (units[result] == &four);
25091}
25092
25093}
25094}
25095
25096#endif /* GDB_SELF_TEST */
25097
9e021579 25098/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 25099
9e021579
SM
25100dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
25101 dwarf2_per_objfile *per_objfile)
25102 : per_cu (per_cu),
25103 per_objfile (per_objfile),
9068261f
AB
25104 mark (false),
25105 has_loclist (false),
25106 checked_producer (false),
25107 producer_is_gxx_lt_4_6 (false),
25108 producer_is_gcc_lt_4_3 (false),
eb77c9df 25109 producer_is_icc (false),
9068261f 25110 producer_is_icc_lt_14 (false),
c258c396 25111 producer_is_codewarrior (false),
9068261f 25112 processing_has_namespace_info (false)
93311388 25113{
9816fde3
JK
25114}
25115
25116/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25117
25118static void
95554aad
TT
25119prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25120 enum language pretend_language)
9816fde3
JK
25121{
25122 struct attribute *attr;
25123
25124 /* Set the language we're debugging. */
25125 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 25126 if (attr != nullptr)
529908cb 25127 set_cu_language (attr->constant_value (0), cu);
9816fde3 25128 else
9cded63f 25129 {
95554aad 25130 cu->language = pretend_language;
9cded63f
TT
25131 cu->language_defn = language_def (cu->language);
25132 }
dee91e82 25133
7d45c7c3 25134 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25135}
25136
7188ed02 25137/* See read.h. */
ae038cb0 25138
7188ed02
SM
25139dwarf2_cu *
25140dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 25141{
7188ed02
SM
25142 auto it = m_dwarf2_cus.find (per_cu);
25143 if (it == m_dwarf2_cus.end ())
25144 return nullptr;
ae038cb0 25145
7188ed02
SM
25146 return it->second;
25147}
25148
25149/* See read.h. */
25150
25151void
25152dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
25153{
25154 gdb_assert (this->get_cu (per_cu) == nullptr);
25155
25156 m_dwarf2_cus[per_cu] = cu;
25157}
25158
25159/* See read.h. */
25160
25161void
25162dwarf2_per_objfile::age_comp_units ()
25163{
17e593e9
SM
25164 dwarf_read_debug_printf_v ("running");
25165
08ac5771
SM
25166 /* This is not expected to be called in the middle of CU expansion. There is
25167 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
25168 loaded in memory. Calling age_comp_units while the queue is in use could
25169 make us free the DIEs for a CU that is in the queue and therefore break
25170 that invariant. */
25171 gdb_assert (!this->per_bfd->queue.has_value ());
25172
7188ed02
SM
25173 /* Start by clearing all marks. */
25174 for (auto pair : m_dwarf2_cus)
25175 pair.second->mark = false;
25176
25177 /* Traverse all CUs, mark them and their dependencies if used recently
25178 enough. */
25179 for (auto pair : m_dwarf2_cus)
ae038cb0 25180 {
7188ed02
SM
25181 dwarf2_cu *cu = pair.second;
25182
25183 cu->last_used++;
25184 if (cu->last_used <= dwarf_max_cache_age)
25185 dwarf2_mark (cu);
ae038cb0
DJ
25186 }
25187
7188ed02
SM
25188 /* Delete all CUs still not marked. */
25189 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 25190 {
7188ed02 25191 dwarf2_cu *cu = it->second;
ae038cb0 25192
7188ed02 25193 if (!cu->mark)
ae038cb0 25194 {
17e593e9
SM
25195 dwarf_read_debug_printf_v ("deleting old CU %s",
25196 sect_offset_str (cu->per_cu->sect_off));
7188ed02
SM
25197 delete cu;
25198 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
25199 }
25200 else
7188ed02 25201 it++;
ae038cb0
DJ
25202 }
25203}
25204
7188ed02 25205/* See read.h. */
ae038cb0 25206
7188ed02
SM
25207void
25208dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 25209{
7188ed02
SM
25210 auto it = m_dwarf2_cus.find (per_cu);
25211 if (it == m_dwarf2_cus.end ())
25212 return;
ae038cb0 25213
7188ed02 25214 delete it->second;
ae038cb0 25215
7188ed02
SM
25216 m_dwarf2_cus.erase (it);
25217}
ae038cb0 25218
7188ed02
SM
25219dwarf2_per_objfile::~dwarf2_per_objfile ()
25220{
25221 remove_all_cus ();
ae038cb0
DJ
25222}
25223
dee91e82
DE
25224/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25225 We store these in a hash table separate from the DIEs, and preserve them
25226 when the DIEs are flushed out of cache.
25227
25228 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25229 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25230 or the type may come from a DWO file. Furthermore, while it's more logical
25231 to use per_cu->section+offset, with Fission the section with the data is in
25232 the DWO file but we don't know that section at the point we need it.
25233 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25234 because we can enter the lookup routine, get_die_type_at_offset, from
25235 outside this file, and thus won't necessarily have PER_CU->cu.
25236 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25237
dee91e82 25238struct dwarf2_per_cu_offset_and_type
1c379e20 25239{
dee91e82 25240 const struct dwarf2_per_cu_data *per_cu;
9c541725 25241 sect_offset sect_off;
1c379e20
DJ
25242 struct type *type;
25243};
25244
dee91e82 25245/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25246
25247static hashval_t
dee91e82 25248per_cu_offset_and_type_hash (const void *item)
1c379e20 25249{
9a3c8263
SM
25250 const struct dwarf2_per_cu_offset_and_type *ofs
25251 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25252
9c541725 25253 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25254}
25255
dee91e82 25256/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25257
25258static int
dee91e82 25259per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25260{
9a3c8263
SM
25261 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25262 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25263 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25264 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25265
dee91e82 25266 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25267 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25268}
25269
25270/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25271 table if necessary. For convenience, return TYPE.
25272
25273 The DIEs reading must have careful ordering to:
85102364 25274 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25275 reading current DIE.
25276 * Not trying to dereference contents of still incompletely read in types
25277 while reading in other DIEs.
25278 * Enable referencing still incompletely read in types just by a pointer to
25279 the type without accessing its fields.
25280
25281 Therefore caller should follow these rules:
25282 * Try to fetch any prerequisite types we may need to build this DIE type
25283 before building the type and calling set_die_type.
e71ec853 25284 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25285 possible before fetching more types to complete the current type.
25286 * Make the type as complete as possible before fetching more types. */
1c379e20 25287
f792889a 25288static struct type *
57567375
TT
25289set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25290 bool skip_data_location)
1c379e20 25291{
976ca316 25292 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 25293 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 25294 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
25295 struct attribute *attr;
25296 struct dynamic_prop prop;
1c379e20 25297
b4ba55a1
JB
25298 /* For Ada types, make sure that the gnat-specific data is always
25299 initialized (if not already set). There are a few types where
25300 we should not be doing so, because the type-specific area is
25301 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25302 where the type-specific area is used to store the floatformat).
25303 But this is not a problem, because the gnat-specific information
25304 is actually not needed for these types. */
25305 if (need_gnat_info (cu)
78134374
SM
25306 && type->code () != TYPE_CODE_FUNC
25307 && type->code () != TYPE_CODE_FLT
25308 && type->code () != TYPE_CODE_METHODPTR
25309 && type->code () != TYPE_CODE_MEMBERPTR
25310 && type->code () != TYPE_CODE_METHOD
09584414 25311 && type->code () != TYPE_CODE_FIXED_POINT
b4ba55a1
JB
25312 && !HAVE_GNAT_AUX_INFO (type))
25313 INIT_GNAT_SPECIFIC (type);
25314
3f2f83dd
KB
25315 /* Read DW_AT_allocated and set in type. */
25316 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 25317 if (attr != NULL)
3f2f83dd 25318 {
293e7e51 25319 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25320 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25321 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 25322 }
3f2f83dd
KB
25323
25324 /* Read DW_AT_associated and set in type. */
25325 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 25326 if (attr != NULL)
3f2f83dd 25327 {
293e7e51 25328 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25329 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25330 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 25331 }
3f2f83dd 25332
3cdcd0ce 25333 /* Read DW_AT_data_location and set in type. */
57567375
TT
25334 if (!skip_data_location)
25335 {
25336 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25337 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25338 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25339 }
3cdcd0ce 25340
976ca316
SM
25341 if (per_objfile->die_type_hash == NULL)
25342 per_objfile->die_type_hash
0335378b
TT
25343 = htab_up (htab_create_alloc (127,
25344 per_cu_offset_and_type_hash,
25345 per_cu_offset_and_type_eq,
25346 NULL, xcalloc, xfree));
1c379e20 25347
dee91e82 25348 ofs.per_cu = cu->per_cu;
9c541725 25349 ofs.sect_off = die->sect_off;
1c379e20 25350 ofs.type = type;
dee91e82 25351 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 25352 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 25353 if (*slot)
b98664d3 25354 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25355 sect_offset_str (die->sect_off));
8d749320
SM
25356 *slot = XOBNEW (&objfile->objfile_obstack,
25357 struct dwarf2_per_cu_offset_and_type);
1c379e20 25358 **slot = ofs;
f792889a 25359 return type;
1c379e20
DJ
25360}
25361
9c541725 25362/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25363 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25364
25365static struct type *
9c541725 25366get_die_type_at_offset (sect_offset sect_off,
aa66c379 25367 dwarf2_per_cu_data *per_cu,
976ca316 25368 dwarf2_per_objfile *per_objfile)
1c379e20 25369{
dee91e82 25370 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 25371
976ca316 25372 if (per_objfile->die_type_hash == NULL)
f792889a 25373 return NULL;
1c379e20 25374
dee91e82 25375 ofs.per_cu = per_cu;
9c541725 25376 ofs.sect_off = sect_off;
9a3c8263 25377 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 25378 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
25379 if (slot)
25380 return slot->type;
25381 else
25382 return NULL;
25383}
25384
02142a6c 25385/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25386 or return NULL if DIE does not have a saved type. */
25387
25388static struct type *
25389get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25390{
aa66c379 25391 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
25392}
25393
10b3939b
DJ
25394/* Add a dependence relationship from CU to REF_PER_CU. */
25395
25396static void
25397dwarf2_add_dependence (struct dwarf2_cu *cu,
25398 struct dwarf2_per_cu_data *ref_per_cu)
25399{
25400 void **slot;
25401
25402 if (cu->dependencies == NULL)
25403 cu->dependencies
25404 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25405 NULL, &cu->comp_unit_obstack,
25406 hashtab_obstack_allocate,
25407 dummy_obstack_deallocate);
25408
25409 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25410 if (*slot == NULL)
25411 *slot = ref_per_cu;
25412}
1c379e20 25413
f504f079
DE
25414/* Subroutine of dwarf2_mark to pass to htab_traverse.
25415 Set the mark field in every compilation unit in the
7188ed02
SM
25416 cache that we must keep because we are keeping CU.
25417
25418 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 25419
10b3939b
DJ
25420static int
25421dwarf2_mark_helper (void **slot, void *data)
25422{
7188ed02
SM
25423 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25424 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25425 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
25426
25427 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25428 reading of the chain. As such dependencies remain valid it is not much
25429 useful to track and undo them during QUIT cleanups. */
7188ed02 25430 if (cu == nullptr)
d07ed419
JK
25431 return 1;
25432
7188ed02 25433 if (cu->mark)
10b3939b 25434 return 1;
10b3939b 25435
7188ed02
SM
25436 cu->mark = true;
25437
25438 if (cu->dependencies != nullptr)
25439 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
25440
25441 return 1;
25442}
25443
f504f079
DE
25444/* Set the mark field in CU and in every other compilation unit in the
25445 cache that we must keep because we are keeping CU. */
25446
ae038cb0
DJ
25447static void
25448dwarf2_mark (struct dwarf2_cu *cu)
25449{
25450 if (cu->mark)
25451 return;
7188ed02 25452
9068261f 25453 cu->mark = true;
ae038cb0 25454
7188ed02
SM
25455 if (cu->dependencies != nullptr)
25456 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
25457}
25458
72bf9492
DJ
25459/* Trivial hash function for partial_die_info: the hash value of a DIE
25460 is its offset in .debug_info for this objfile. */
25461
25462static hashval_t
25463partial_die_hash (const void *item)
25464{
9a3c8263
SM
25465 const struct partial_die_info *part_die
25466 = (const struct partial_die_info *) item;
9a619af0 25467
9c541725 25468 return to_underlying (part_die->sect_off);
72bf9492
DJ
25469}
25470
25471/* Trivial comparison function for partial_die_info structures: two DIEs
25472 are equal if they have the same offset. */
25473
25474static int
25475partial_die_eq (const void *item_lhs, const void *item_rhs)
25476{
9a3c8263
SM
25477 const struct partial_die_info *part_die_lhs
25478 = (const struct partial_die_info *) item_lhs;
25479 const struct partial_die_info *part_die_rhs
25480 = (const struct partial_die_info *) item_rhs;
9a619af0 25481
9c541725 25482 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25483}
25484
3c3bb058
AB
25485struct cmd_list_element *set_dwarf_cmdlist;
25486struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 25487
9291a0cd 25488static void
cd4fb1b2
SM
25489show_check_physname (struct ui_file *file, int from_tty,
25490 struct cmd_list_element *c, const char *value)
9291a0cd 25491{
cd4fb1b2
SM
25492 fprintf_filtered (file,
25493 _("Whether to check \"physname\" is %s.\n"),
25494 value);
9291a0cd
TT
25495}
25496
6c265988 25497void _initialize_dwarf2_read ();
cd4fb1b2 25498void
6c265988 25499_initialize_dwarf2_read ()
9291a0cd 25500{
0743fc83 25501 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 25502Set DWARF specific variables.\n\
590042fc 25503Configure DWARF variables such as the cache size."),
0743fc83
TT
25504 &set_dwarf_cmdlist, "maintenance set dwarf ",
25505 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25506
0743fc83 25507 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
25508Show DWARF specific variables.\n\
25509Show DWARF variables such as the cache size."),
0743fc83
TT
25510 &show_dwarf_cmdlist, "maintenance show dwarf ",
25511 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25512
cd4fb1b2
SM
25513 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25514 &dwarf_max_cache_age, _("\
25515Set the upper bound on the age of cached DWARF compilation units."), _("\
25516Show the upper bound on the age of cached DWARF compilation units."), _("\
25517A higher limit means that cached compilation units will be stored\n\
25518in memory longer, and more total memory will be used. Zero disables\n\
25519caching, which can slow down startup."),
25520 NULL,
25521 show_dwarf_max_cache_age,
25522 &set_dwarf_cmdlist,
25523 &show_dwarf_cmdlist);
156942c7 25524
cd4fb1b2
SM
25525 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25526Set debugging of the DWARF reader."), _("\
25527Show debugging of the DWARF reader."), _("\
25528When enabled (non-zero), debugging messages are printed during DWARF\n\
25529reading and symtab expansion. A value of 1 (one) provides basic\n\
25530information. A value greater than 1 provides more verbose information."),
25531 NULL,
25532 NULL,
25533 &setdebuglist, &showdebuglist);
9291a0cd 25534
cd4fb1b2
SM
25535 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25536Set debugging of the DWARF DIE reader."), _("\
25537Show debugging of the DWARF DIE reader."), _("\
25538When enabled (non-zero), DIEs are dumped after they are read in.\n\
25539The value is the maximum depth to print."),
25540 NULL,
25541 NULL,
25542 &setdebuglist, &showdebuglist);
9291a0cd 25543
cd4fb1b2
SM
25544 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25545Set debugging of the dwarf line reader."), _("\
25546Show debugging of the dwarf line reader."), _("\
25547When enabled (non-zero), line number entries are dumped as they are read in.\n\
25548A value of 1 (one) provides basic information.\n\
25549A value greater than 1 provides more verbose information."),
25550 NULL,
25551 NULL,
25552 &setdebuglist, &showdebuglist);
437afbb8 25553
cd4fb1b2
SM
25554 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25555Set cross-checking of \"physname\" code against demangler."), _("\
25556Show cross-checking of \"physname\" code against demangler."), _("\
25557When enabled, GDB's internal \"physname\" code is checked against\n\
25558the demangler."),
25559 NULL, show_check_physname,
25560 &setdebuglist, &showdebuglist);
900e11f9 25561
e615022a
DE
25562 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25563 no_class, &use_deprecated_index_sections, _("\
25564Set whether to use deprecated gdb_index sections."), _("\
25565Show whether to use deprecated gdb_index sections."), _("\
25566When enabled, deprecated .gdb_index sections are used anyway.\n\
25567Normally they are ignored either because of a missing feature or\n\
25568performance issue.\n\
25569Warning: This option must be enabled before gdb reads the file."),
25570 NULL,
25571 NULL,
25572 &setlist, &showlist);
25573
f1e6e072
TT
25574 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25575 &dwarf2_locexpr_funcs);
25576 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25577 &dwarf2_loclist_funcs);
25578
25579 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25580 &dwarf2_block_frame_base_locexpr_funcs);
25581 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25582 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25583
25584#if GDB_SELF_TEST
25585 selftests::register_test ("dw2_expand_symtabs_matching",
25586 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
25587 selftests::register_test ("dwarf2_find_containing_comp_unit",
25588 selftests::find_containing_comp_unit::run_test);
c62446b1 25589#endif
6502dd73 25590}
This page took 5.827715 seconds and 4 git commands to generate.