gdb: delete SYMBOL_SECTION and MSYMBOL_SECTION macros
[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"
2b2558bf 43#include "dwarf2/stringify.h"
4de283e4
TT
44#include "bfd.h"
45#include "elf-bfd.h"
46#include "symtab.h"
47#include "gdbtypes.h"
48#include "objfiles.h"
d55e5aa6 49#include "dwarf2.h"
4de283e4
TT
50#include "buildsym.h"
51#include "demangle.h"
52#include "gdb-demangle.h"
4de283e4 53#include "filenames.h" /* for DOSish file names */
4de283e4
TT
54#include "language.h"
55#include "complaints.h"
82ca8957
TT
56#include "dwarf2/expr.h"
57#include "dwarf2/loc.h"
4de283e4
TT
58#include "cp-support.h"
59#include "hashtab.h"
60#include "command.h"
d55e5aa6 61#include "gdbcmd.h"
4de283e4
TT
62#include "block.h"
63#include "addrmap.h"
64#include "typeprint.h"
65#include "psympriv.h"
4de283e4 66#include "c-lang.h"
d55e5aa6 67#include "go-lang.h"
4de283e4
TT
68#include "valprint.h"
69#include "gdbcore.h" /* for gnutarget */
70#include "gdb/gdb-index.h"
4de283e4
TT
71#include "gdb_bfd.h"
72#include "f-lang.h"
73#include "source.h"
4de283e4 74#include "build-id.h"
d55e5aa6 75#include "namespace.h"
268a13a5
TT
76#include "gdbsupport/function-view.h"
77#include "gdbsupport/gdb_optional.h"
78#include "gdbsupport/underlying.h"
268a13a5 79#include "gdbsupport/hash_enum.h"
4de283e4 80#include "filename-seen-cache.h"
b32b108a 81#include "producer.h"
4de283e4 82#include <fcntl.h>
4de283e4 83#include <algorithm>
4de283e4 84#include <unordered_map>
268a13a5 85#include "gdbsupport/selftest.h"
c9317f21 86#include "rust-lang.h"
268a13a5 87#include "gdbsupport/pathstuff.h"
edd45eb0 88#include "count-one-bits.h"
0d79cdc4 89#include "debuginfod-support.h"
437afbb8 90
73be47f5
DE
91/* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
b4f54984
DE
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94static unsigned int dwarf_read_debug = 0;
45cfd468 95
6f738b01
SM
96/* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
97
98#define dwarf_read_debug_printf(fmt, ...) \
74b773fc
SM
99 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
100 ##__VA_ARGS__)
6f738b01
SM
101
102/* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
103
104#define dwarf_read_debug_printf_v(fmt, ...) \
74b773fc
SM
105 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
106 ##__VA_ARGS__)
6f738b01 107
d97bc12b 108/* When non-zero, dump DIEs after they are read in. */
b4f54984 109static unsigned int dwarf_die_debug = 0;
d97bc12b 110
27e0867f 111/* When non-zero, dump line number entries as they are read in. */
8fdd972c 112unsigned int dwarf_line_debug = 0;
27e0867f 113
491144b5
CB
114/* When true, cross-check physname against demangler. */
115static bool check_physname = false;
900e11f9 116
491144b5
CB
117/* When true, do not reject deprecated .gdb_index sections. */
118static bool use_deprecated_index_sections = false;
481860b3 119
17ee85fc
TT
120/* This is used to store the data that is always per objfile. */
121static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
122
123/* These are used to store the dwarf2_per_bfd objects.
124
125 objfiles having the same BFD, which doesn't require relocations, are going to
126 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
127
128 Other objfiles are not going to share a dwarf2_per_bfd with any other
129 objfiles, so they'll have their own version kept in the _objfile_data_key
130 version. */
131static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
132static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
6502dd73 133
f1e6e072
TT
134/* The "aclass" indices for various kinds of computed DWARF symbols. */
135
136static int dwarf2_locexpr_index;
137static int dwarf2_loclist_index;
138static int dwarf2_locexpr_block_index;
139static int dwarf2_loclist_block_index;
140
41144253 141/* Size of .debug_loclists section header for 32-bit DWARF format. */
142#define LOCLIST_HEADER_SIZE32 12
143
144/* Size of .debug_loclists section header for 64-bit DWARF format. */
145#define LOCLIST_HEADER_SIZE64 20
146
d0ce17d8
CT
147/* Size of .debug_rnglists section header for 32-bit DWARF format. */
148#define RNGLIST_HEADER_SIZE32 12
149
150/* Size of .debug_rnglists section header for 64-bit DWARF format. */
151#define RNGLIST_HEADER_SIZE64 20
152
3f563c84
PA
153/* An index into a (C++) symbol name component in a symbol name as
154 recorded in the mapped_index's symbol table. For each C++ symbol
155 in the symbol table, we record one entry for the start of each
156 component in the symbol in a table of name components, and then
157 sort the table, in order to be able to binary search symbol names,
158 ignoring leading namespaces, both completion and regular look up.
159 For example, for symbol "A::B::C", we'll have an entry that points
160 to "A::B::C", another that points to "B::C", and another for "C".
161 Note that function symbols in GDB index have no parameter
162 information, just the function/method names. You can convert a
163 name_component to a "const char *" using the
164 'mapped_index::symbol_name_at(offset_type)' method. */
165
166struct name_component
167{
168 /* Offset in the symbol name where the component starts. Stored as
169 a (32-bit) offset instead of a pointer to save memory and improve
170 locality on 64-bit architectures. */
171 offset_type name_offset;
172
173 /* The symbol's index in the symbol and constant pool tables of a
174 mapped_index. */
175 offset_type idx;
176};
177
44ed8f3e
PA
178/* Base class containing bits shared by both .gdb_index and
179 .debug_name indexes. */
180
181struct mapped_index_base
182{
22ca247e
TT
183 mapped_index_base () = default;
184 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
185
44ed8f3e
PA
186 /* The name_component table (a sorted vector). See name_component's
187 description above. */
188 std::vector<name_component> name_components;
189
190 /* How NAME_COMPONENTS is sorted. */
191 enum case_sensitivity name_components_casing;
192
193 /* Return the number of names in the symbol table. */
194 virtual size_t symbol_name_count () const = 0;
195
196 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
197 virtual const char *symbol_name_at
198 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
44ed8f3e
PA
199
200 /* Return whether the name at IDX in the symbol table should be
201 ignored. */
202 virtual bool symbol_name_slot_invalid (offset_type idx) const
203 {
204 return false;
205 }
206
207 /* Build the symbol name component sorted vector, if we haven't
208 yet. */
fcf23d5b 209 void build_name_components (dwarf2_per_objfile *per_objfile);
44ed8f3e
PA
210
211 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
212 possible matches for LN_NO_PARAMS in the name component
213 vector. */
214 std::pair<std::vector<name_component>::const_iterator,
215 std::vector<name_component>::const_iterator>
3b00ef10 216 find_name_components_bounds (const lookup_name_info &ln_no_params,
fcf23d5b
SM
217 enum language lang,
218 dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
219
220 /* Prevent deleting/destroying via a base class pointer. */
221protected:
222 ~mapped_index_base() = default;
223};
224
9291a0cd
TT
225/* A description of the mapped index. The file format is described in
226 a comment by the code that writes the index. */
fc898b42 227struct mapped_index final : public mapped_index_base
9291a0cd 228{
f00a2de2
PA
229 /* A slot/bucket in the symbol table hash. */
230 struct symbol_table_slot
231 {
232 const offset_type name;
233 const offset_type vec;
234 };
235
559a7a62 236 /* Index data format version. */
3063847f 237 int version = 0;
559a7a62 238
f00a2de2
PA
239 /* The address table data. */
240 gdb::array_view<const gdb_byte> address_table;
b11b1f88 241
3876f04e 242 /* The symbol table, implemented as a hash table. */
f00a2de2 243 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 244
9291a0cd 245 /* A pointer to the constant pool. */
3063847f 246 const char *constant_pool = nullptr;
3f563c84 247
44ed8f3e
PA
248 bool symbol_name_slot_invalid (offset_type idx) const override
249 {
250 const auto &bucket = this->symbol_table[idx];
9ab08412 251 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 252 }
5c58de74 253
3f563c84
PA
254 /* Convenience method to get at the name of the symbol at IDX in the
255 symbol table. */
fcf23d5b
SM
256 const char *symbol_name_at
257 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
f00a2de2 258 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 259
44ed8f3e
PA
260 size_t symbol_name_count () const override
261 { return this->symbol_table.size (); }
9291a0cd
TT
262};
263
927aa2e7
JK
264/* A description of the mapped .debug_names.
265 Uninitialized map has CU_COUNT 0. */
fc898b42 266struct mapped_debug_names final : public mapped_index_base
927aa2e7
JK
267{
268 bfd_endian dwarf5_byte_order;
269 bool dwarf5_is_dwarf64;
270 bool augmentation_is_gdb;
271 uint8_t offset_size;
272 uint32_t cu_count = 0;
273 uint32_t tu_count, bucket_count, name_count;
274 const gdb_byte *cu_table_reordered, *tu_table_reordered;
275 const uint32_t *bucket_table_reordered, *hash_table_reordered;
276 const gdb_byte *name_table_string_offs_reordered;
277 const gdb_byte *name_table_entry_offs_reordered;
278 const gdb_byte *entry_pool;
279
280 struct index_val
281 {
282 ULONGEST dwarf_tag;
283 struct attr
284 {
285 /* Attribute name DW_IDX_*. */
286 ULONGEST dw_idx;
287
288 /* Attribute form DW_FORM_*. */
289 ULONGEST form;
290
291 /* Value if FORM is DW_FORM_implicit_const. */
292 LONGEST implicit_const;
293 };
294 std::vector<attr> attr_vec;
295 };
296
297 std::unordered_map<ULONGEST, index_val> abbrev_map;
298
fcf23d5b
SM
299 const char *namei_to_name
300 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
301
302 /* Implementation of the mapped_index_base virtual interface, for
303 the name_components cache. */
304
fcf23d5b
SM
305 const char *symbol_name_at
306 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
307 { return namei_to_name (idx, per_objfile); }
44ed8f3e
PA
308
309 size_t symbol_name_count () const override
310 { return this->name_count; }
927aa2e7
JK
311};
312
cd4fb1b2 313/* See dwarf2read.h. */
ed2dc618 314
cd4fb1b2 315dwarf2_per_objfile *
ed2dc618
SM
316get_dwarf2_per_objfile (struct objfile *objfile)
317{
5bfd760d 318 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 319}
c906108c 320
251d32d9 321/* Default names of the debugging sections. */
c906108c 322
233a11ab
CS
323/* Note that if the debugging section has been compressed, it might
324 have a name like .zdebug_info. */
325
9cdd5dbd
DE
326static const struct dwarf2_debug_sections dwarf2_elf_names =
327{
251d32d9
TG
328 { ".debug_info", ".zdebug_info" },
329 { ".debug_abbrev", ".zdebug_abbrev" },
330 { ".debug_line", ".zdebug_line" },
331 { ".debug_loc", ".zdebug_loc" },
43988095 332 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 333 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 334 { ".debug_macro", ".zdebug_macro" },
251d32d9 335 { ".debug_str", ".zdebug_str" },
18a8505e 336 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 337 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 338 { ".debug_ranges", ".zdebug_ranges" },
43988095 339 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 340 { ".debug_types", ".zdebug_types" },
3019eac3 341 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
342 { ".debug_frame", ".zdebug_frame" },
343 { ".eh_frame", NULL },
24d3216f 344 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
345 { ".debug_names", ".zdebug_names" },
346 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 347 23
251d32d9 348};
c906108c 349
80626a55 350/* List of DWO/DWP sections. */
3019eac3 351
80626a55 352static const struct dwop_section_names
3019eac3
DE
353{
354 struct dwarf2_section_names abbrev_dwo;
355 struct dwarf2_section_names info_dwo;
356 struct dwarf2_section_names line_dwo;
357 struct dwarf2_section_names loc_dwo;
43988095 358 struct dwarf2_section_names loclists_dwo;
09262596
DE
359 struct dwarf2_section_names macinfo_dwo;
360 struct dwarf2_section_names macro_dwo;
d0ce17d8 361 struct dwarf2_section_names rnglists_dwo;
3019eac3
DE
362 struct dwarf2_section_names str_dwo;
363 struct dwarf2_section_names str_offsets_dwo;
364 struct dwarf2_section_names types_dwo;
80626a55
DE
365 struct dwarf2_section_names cu_index;
366 struct dwarf2_section_names tu_index;
3019eac3 367}
80626a55 368dwop_section_names =
3019eac3
DE
369{
370 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
371 { ".debug_info.dwo", ".zdebug_info.dwo" },
372 { ".debug_line.dwo", ".zdebug_line.dwo" },
373 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 374 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
375 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
376 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
d0ce17d8 377 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
3019eac3
DE
378 { ".debug_str.dwo", ".zdebug_str.dwo" },
379 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
380 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
381 { ".debug_cu_index", ".zdebug_cu_index" },
382 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
383};
384
c906108c
SS
385/* local data types */
386
d0ce17d8
CT
387/* The location list and range list sections (.debug_loclists & .debug_rnglists)
388 begin with a header, which contains the following information. */
389struct loclists_rnglists_header
41144253 390{
391 /* A 4-byte or 12-byte length containing the length of the
392 set of entries for this compilation unit, not including the
393 length field itself. */
394 unsigned int length;
395
396 /* A 2-byte version identifier. */
397 short version;
398
399 /* A 1-byte unsigned integer containing the size in bytes of an address on
400 the target system. */
401 unsigned char addr_size;
402
403 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
404 on the target system. */
405 unsigned char segment_collector_size;
406
407 /* A 4-byte count of the number of offsets that follow the header. */
408 unsigned int offset_entry_count;
409};
410
3da10d80
KS
411/* Type used for delaying computation of method physnames.
412 See comments for compute_delayed_physnames. */
413struct delayed_method_info
414{
415 /* The type to which the method is attached, i.e., its parent class. */
416 struct type *type;
417
418 /* The index of the method in the type's function fieldlists. */
419 int fnfield_index;
420
421 /* The index of the method in the fieldlist. */
422 int index;
423
424 /* The name of the DIE. */
425 const char *name;
426
427 /* The DIE associated with this method. */
428 struct die_info *die;
429};
430
e7c27a73
DJ
431/* Internal state when decoding a particular compilation unit. */
432struct dwarf2_cu
433{
9e021579
SM
434 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
435 dwarf2_per_objfile *per_objfile);
fcd3b13d
SM
436
437 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
438
c24bdb02
KS
439 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
440 Create the set of symtabs used by this TU, or if this TU is sharing
441 symtabs with another TU and the symtabs have already been created
442 then restore those symtabs in the line header.
443 We don't need the pc/line-number mapping for type units. */
444 void setup_type_unit_groups (struct die_info *die);
445
446 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
447 buildsym_compunit constructor. */
448 struct compunit_symtab *start_symtab (const char *name,
449 const char *comp_dir,
450 CORE_ADDR low_pc);
451
452 /* Reset the builder. */
453 void reset_builder () { m_builder.reset (); }
454
293e7e51
SM
455 /* Return a type that is a generic pointer type, the size of which
456 matches the address size given in the compilation unit header for
457 this CU. */
458 struct type *addr_type () const;
459
460 /* Find an integer type the same size as the address size given in
461 the compilation unit header for this CU. UNSIGNED_P controls if
462 the integer is unsigned or not. */
463 struct type *addr_sized_int_type (bool unsigned_p) const;
464
d00adf39 465 /* The header of the compilation unit. */
fcd3b13d 466 struct comp_unit_head header {};
e142c38c 467
d00adf39 468 /* Base address of this compilation unit. */
2b24b6e4 469 gdb::optional<CORE_ADDR> base_address;
d00adf39 470
e142c38c 471 /* The language we are debugging. */
fcd3b13d
SM
472 enum language language = language_unknown;
473 const struct language_defn *language_defn = nullptr;
e142c38c 474
fcd3b13d 475 const char *producer = nullptr;
b0f35d58 476
c24bdb02 477private:
804d2729
TT
478 /* The symtab builder for this CU. This is only non-NULL when full
479 symbols are being read. */
c24bdb02 480 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 481
c24bdb02 482public:
e142c38c
DJ
483 /* The generic symbol table building routines have separate lists for
484 file scope symbols and all all other scopes (local scopes). So
485 we need to select the right one to pass to add_symbol_to_list().
486 We do it by keeping a pointer to the correct list in list_in_scope.
487
488 FIXME: The original dwarf code just treated the file scope as the
489 first local scope, and all other local scopes as nested local
490 scopes, and worked fine. Check to see if we really need to
491 distinguish these in buildsym.c. */
fcd3b13d 492 struct pending **list_in_scope = nullptr;
e142c38c 493
b64f50a1
JK
494 /* Hash table holding all the loaded partial DIEs
495 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 496 htab_t partial_dies = nullptr;
72bf9492
DJ
497
498 /* Storage for things with the same lifetime as this read-in compilation
499 unit, including partial DIEs. */
fcd3b13d 500 auto_obstack comp_unit_obstack;
72bf9492 501
69d751e3 502 /* Backlink to our per_cu entry. */
ae038cb0
DJ
503 struct dwarf2_per_cu_data *per_cu;
504
9e021579 505 /* The dwarf2_per_objfile that owns this. */
976ca316 506 dwarf2_per_objfile *per_objfile;
9e021579 507
ae038cb0 508 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 509 int last_used = 0;
ae038cb0 510
b64f50a1
JK
511 /* A hash table of DIE cu_offset for following references with
512 die_info->offset.sect_off as hash. */
fcd3b13d 513 htab_t die_hash = nullptr;
10b3939b
DJ
514
515 /* Full DIEs if read in. */
fcd3b13d 516 struct die_info *dies = nullptr;
10b3939b
DJ
517
518 /* A set of pointers to dwarf2_per_cu_data objects for compilation
519 units referenced by this one. Only set during full symbol processing;
520 partial symbol tables do not have dependencies. */
fcd3b13d 521 htab_t dependencies = nullptr;
10b3939b 522
cb1df416 523 /* Header data from the line table, during full symbol processing. */
fcd3b13d 524 struct line_header *line_header = nullptr;
4c8aa72d 525 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
5989a64e 526 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
4c8aa72d
PA
527 this is the DW_TAG_compile_unit die for this CU. We'll hold on
528 to the line header as long as this DIE is being processed. See
529 process_die_scope. */
fcd3b13d 530 die_info *line_header_die_owner = nullptr;
cb1df416 531
3da10d80
KS
532 /* A list of methods which need to have physnames computed
533 after all type information has been read. */
c89b44cd 534 std::vector<delayed_method_info> method_list;
3da10d80 535
96408a79 536 /* To be copied to symtab->call_site_htab. */
fcd3b13d 537 htab_t call_site_htab = nullptr;
96408a79 538
034e5797
DE
539 /* Non-NULL if this CU came from a DWO file.
540 There is an invariant here that is important to remember:
541 Except for attributes copied from the top level DIE in the "main"
542 (or "stub") file in preparation for reading the DWO file
18a8505e 543 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
544 Either there isn't a DWO file (in which case this is NULL and the point
545 is moot), or there is and either we're not going to read it (in which
546 case this is NULL) or there is and we are reading it (in which case this
547 is non-NULL). */
fcd3b13d 548 struct dwo_unit *dwo_unit = nullptr;
3019eac3 549
18a8505e 550 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 551 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 552 gdb::optional<ULONGEST> addr_base;
3019eac3 553
2b0c7f41
SM
554 /* The DW_AT_GNU_ranges_base attribute, if present.
555
556 This is only relevant in the context of pre-DWARF 5 split units. In this
557 context, there is a .debug_ranges section in the linked executable,
558 containing all the ranges data for all the compilation units. Each
559 skeleton/stub unit has (if needed) a DW_AT_GNU_ranges_base attribute that
560 indicates the base of its contribution to that section. The DW_AT_ranges
561 attributes in the split-unit are of the form DW_FORM_sec_offset and point
562 into the .debug_ranges section of the linked file. However, they are not
563 "true" DW_FORM_sec_offset, because they are relative to the base of their
564 compilation unit's contribution, rather than relative to the beginning of
565 the section. The DW_AT_GNU_ranges_base value must be added to it to make
566 it relative to the beginning of the section.
567
568 Note that the value is zero when we are not in a pre-DWARF 5 split-unit
569 case, so this value can be added without needing to know whether we are in
570 this case or not.
571
572 N.B. If a DW_AT_ranges attribute is found on the DW_TAG_compile_unit in the
573 skeleton/stub, it must not have the base added, as it already points to the
574 right place. And since the DW_TAG_compile_unit DIE in the split-unit can't
575 have a DW_AT_ranges attribute, we can use the
576
577 die->tag != DW_AT_compile_unit
578
579 to determine whether the base should be added or not. */
580 ULONGEST gnu_ranges_base = 0;
581
582 /* The DW_AT_rnglists_base attribute, if present.
583
584 This is used when processing attributes of form DW_FORM_rnglistx in
585 non-split units. Attributes of this form found in a split unit don't
586 use it, as split-unit files have their own non-shared .debug_rnglists.dwo
587 section. */
588 ULONGEST rnglists_base = 0;
2e3cf129 589
41144253 590 /* The DW_AT_loclists_base attribute if present. */
591 ULONGEST loclist_base = 0;
592
c9317f21
TT
593 /* When reading debug info generated by older versions of rustc, we
594 have to rewrite some union types to be struct types with a
595 variant part. This rewriting must be done after the CU is fully
596 read in, because otherwise at the point of rewriting some struct
597 type might not have been fully processed. So, we keep a list of
598 all such types here and process them after expansion. */
599 std::vector<struct type *> rust_unions;
600
18a8505e
AT
601 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
602 files, the value is implicitly zero. For DWARF 5 version DWO files, the
603 value is often implicit and is the size of the header of
604 .debug_str_offsets section (8 or 4, depending on the address size). */
605 gdb::optional<ULONGEST> str_offsets_base;
606
ae038cb0 607 /* Mark used when releasing cached dies. */
9068261f 608 bool mark : 1;
ae038cb0 609
8be455d7
JK
610 /* This CU references .debug_loc. See the symtab->locations_valid field.
611 This test is imperfect as there may exist optimized debug code not using
612 any location list and still facing inlining issues if handled as
613 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 614 bool has_loclist : 1;
ba919b58 615
9068261f 616 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
617 if all the producer_is_* fields are valid. This information is cached
618 because profiling CU expansion showed excessive time spent in
619 producer_is_gxx_lt_4_6. */
9068261f
AB
620 bool checked_producer : 1;
621 bool producer_is_gxx_lt_4_6 : 1;
622 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 623 bool producer_is_icc : 1;
9068261f 624 bool producer_is_icc_lt_14 : 1;
c258c396 625 bool producer_is_codewarrior : 1;
4d4ec4e5 626
9068261f 627 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
628 debugging info for C++ namespaces. GCC 3.3.x did not produce
629 this information, but later versions do. */
630
9068261f 631 bool processing_has_namespace_info : 1;
d590ff25
YQ
632
633 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
634
635 /* If this CU was inherited by another CU (via specification,
636 abstract_origin, etc), this is the ancestor CU. */
637 dwarf2_cu *ancestor;
638
639 /* Get the buildsym_compunit for this CU. */
640 buildsym_compunit *get_builder ()
641 {
642 /* If this CU has a builder associated with it, use that. */
643 if (m_builder != nullptr)
644 return m_builder.get ();
645
646 /* Otherwise, search ancestors for a valid builder. */
647 if (ancestor != nullptr)
648 return ancestor->get_builder ();
649
650 return nullptr;
651 }
e7c27a73
DJ
652};
653
094b34ac
DE
654/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
655 This includes type_unit_group and quick_file_names. */
656
657struct stmt_list_hash
658{
659 /* The DWO unit this table is from or NULL if there is none. */
660 struct dwo_unit *dwo_unit;
661
662 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 663 sect_offset line_sect_off;
094b34ac
DE
664};
665
5989a64e 666/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
8adb8487
TT
667 an object of this type. This contains elements of type unit groups
668 that can be shared across objfiles. The non-shareable parts are in
669 type_unit_group_unshareable. */
f4dc4d17
DE
670
671struct type_unit_group
672{
0186c6a7 673 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
674 To simplify things we create an artificial CU that "includes" all the
675 type units using this stmt_list so that the rest of the code still has
197400e8 676 a "per_cu" handle on the symtab. */
094b34ac
DE
677 struct dwarf2_per_cu_data per_cu;
678
0186c6a7
DE
679 /* The TUs that share this DW_AT_stmt_list entry.
680 This is added to while parsing type units to build partial symtabs,
681 and is deleted afterwards and not used again. */
a8b3b8e9 682 std::vector<signatured_type *> *tus;
f4dc4d17 683
094b34ac
DE
684 /* The data used to construct the hash key. */
685 struct stmt_list_hash hash;
f4dc4d17
DE
686};
687
73869dc2 688/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
689
690struct dwo_sections
691{
692 struct dwarf2_section_info abbrev;
3019eac3
DE
693 struct dwarf2_section_info line;
694 struct dwarf2_section_info loc;
43988095 695 struct dwarf2_section_info loclists;
09262596
DE
696 struct dwarf2_section_info macinfo;
697 struct dwarf2_section_info macro;
d0ce17d8 698 struct dwarf2_section_info rnglists;
3019eac3
DE
699 struct dwarf2_section_info str;
700 struct dwarf2_section_info str_offsets;
80626a55
DE
701 /* In the case of a virtual DWO file, these two are unused. */
702 struct dwarf2_section_info info;
fd5866f6 703 std::vector<dwarf2_section_info> types;
3019eac3
DE
704};
705
c88ee1f0 706/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
707
708struct dwo_unit
709{
710 /* Backlink to the containing struct dwo_file. */
711 struct dwo_file *dwo_file;
712
713 /* The "id" that distinguishes this CU/TU.
714 .debug_info calls this "dwo_id", .debug_types calls this "signature".
715 Since signatures came first, we stick with it for consistency. */
716 ULONGEST signature;
717
718 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 719 struct dwarf2_section_info *section;
3019eac3 720
9c541725
PA
721 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
722 sect_offset sect_off;
3019eac3
DE
723 unsigned int length;
724
725 /* For types, offset in the type's DIE of the type defined by this TU. */
726 cu_offset type_offset_in_tu;
727};
728
73869dc2
DE
729/* include/dwarf2.h defines the DWP section codes.
730 It defines a max value but it doesn't define a min value, which we
731 use for error checking, so provide one. */
732
733enum dwp_v2_section_ids
734{
735 DW_SECT_MIN = 1
736};
737
80626a55 738/* Data for one DWO file.
57d63ce2
DE
739
740 This includes virtual DWO files (a virtual DWO file is a DWO file as it
741 appears in a DWP file). DWP files don't really have DWO files per se -
742 comdat folding of types "loses" the DWO file they came from, and from
743 a high level view DWP files appear to contain a mass of random types.
744 However, to maintain consistency with the non-DWP case we pretend DWP
745 files contain virtual DWO files, and we assign each TU with one virtual
746 DWO file (generally based on the line and abbrev section offsets -
747 a heuristic that seems to work in practice). */
3019eac3
DE
748
749struct dwo_file
750{
51ac9db5
SM
751 dwo_file () = default;
752 DISABLE_COPY_AND_ASSIGN (dwo_file);
753
18a8505e 754 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
755 For virtual DWO files the name is constructed from the section offsets
756 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
757 from related CU+TUs. */
51ac9db5 758 const char *dwo_name = nullptr;
0ac5b59e
DE
759
760 /* The DW_AT_comp_dir attribute. */
51ac9db5 761 const char *comp_dir = nullptr;
3019eac3 762
80626a55
DE
763 /* The bfd, when the file is open. Otherwise this is NULL.
764 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 765 gdb_bfd_ref_ptr dbfd;
3019eac3 766
73869dc2 767 /* The sections that make up this DWO file.
d2854d8d 768 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
73869dc2 769 sections (for lack of a better name). */
51ac9db5 770 struct dwo_sections sections {};
3019eac3 771
33c5cd75
DB
772 /* The CUs in the file.
773 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
774 an extension to handle LLVM's Link Time Optimization output (where
775 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 776 htab_up cus;
3019eac3
DE
777
778 /* Table of TUs in the file.
779 Each element is a struct dwo_unit. */
b0b6a987 780 htab_up tus;
3019eac3
DE
781};
782
80626a55
DE
783/* These sections are what may appear in a DWP file. */
784
785struct dwp_sections
786{
d2854d8d 787 /* These are used by all DWP versions (1, 2 and 5). */
80626a55
DE
788 struct dwarf2_section_info str;
789 struct dwarf2_section_info cu_index;
790 struct dwarf2_section_info tu_index;
73869dc2 791
d2854d8d 792 /* These are only used by DWP version 2 and version 5 files.
73869dc2
DE
793 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
794 sections are referenced by section number, and are not recorded here.
d2854d8d
CT
795 In DWP version 2 or 5 there is at most one copy of all these sections,
796 each section being (effectively) comprised of the concatenation of all of
797 the individual sections that exist in the version 1 format.
73869dc2
DE
798 To keep the code simple we treat each of these concatenated pieces as a
799 section itself (a virtual section?). */
800 struct dwarf2_section_info abbrev;
801 struct dwarf2_section_info info;
802 struct dwarf2_section_info line;
803 struct dwarf2_section_info loc;
d2854d8d 804 struct dwarf2_section_info loclists;
73869dc2
DE
805 struct dwarf2_section_info macinfo;
806 struct dwarf2_section_info macro;
d2854d8d 807 struct dwarf2_section_info rnglists;
73869dc2
DE
808 struct dwarf2_section_info str_offsets;
809 struct dwarf2_section_info types;
80626a55
DE
810};
811
73869dc2
DE
812/* These sections are what may appear in a virtual DWO file in DWP version 1.
813 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 814
73869dc2 815struct virtual_v1_dwo_sections
80626a55
DE
816{
817 struct dwarf2_section_info abbrev;
818 struct dwarf2_section_info line;
819 struct dwarf2_section_info loc;
820 struct dwarf2_section_info macinfo;
821 struct dwarf2_section_info macro;
822 struct dwarf2_section_info str_offsets;
823 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 824 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
825 struct dwarf2_section_info info_or_types;
826};
827
d2854d8d 828/* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
73869dc2
DE
829 In version 2, the sections of the DWO files are concatenated together
830 and stored in one section of that name. Thus each ELF section contains
831 several "virtual" sections. */
832
d2854d8d 833struct virtual_v2_or_v5_dwo_sections
73869dc2
DE
834{
835 bfd_size_type abbrev_offset;
836 bfd_size_type abbrev_size;
837
838 bfd_size_type line_offset;
839 bfd_size_type line_size;
840
841 bfd_size_type loc_offset;
842 bfd_size_type loc_size;
843
d2854d8d
CT
844 bfd_size_type loclists_offset;
845 bfd_size_type loclists_size;
846
73869dc2
DE
847 bfd_size_type macinfo_offset;
848 bfd_size_type macinfo_size;
849
850 bfd_size_type macro_offset;
851 bfd_size_type macro_size;
852
d2854d8d
CT
853 bfd_size_type rnglists_offset;
854 bfd_size_type rnglists_size;
855
73869dc2
DE
856 bfd_size_type str_offsets_offset;
857 bfd_size_type str_offsets_size;
858
859 /* Each DWP hash table entry records one CU or one TU.
860 That is recorded here, and copied to dwo_unit.section. */
861 bfd_size_type info_or_types_offset;
862 bfd_size_type info_or_types_size;
863};
864
80626a55
DE
865/* Contents of DWP hash tables. */
866
867struct dwp_hash_table
868{
73869dc2 869 uint32_t version, nr_columns;
80626a55 870 uint32_t nr_units, nr_slots;
73869dc2
DE
871 const gdb_byte *hash_table, *unit_table;
872 union
873 {
874 struct
875 {
876 const gdb_byte *indices;
877 } v1;
878 struct
879 {
880 /* This is indexed by column number and gives the id of the section
881 in that column. */
882#define MAX_NR_V2_DWO_SECTIONS \
883 (1 /* .debug_info or .debug_types */ \
884 + 1 /* .debug_abbrev */ \
885 + 1 /* .debug_line */ \
886 + 1 /* .debug_loc */ \
887 + 1 /* .debug_str_offsets */ \
888 + 1 /* .debug_macro or .debug_macinfo */)
889 int section_ids[MAX_NR_V2_DWO_SECTIONS];
890 const gdb_byte *offsets;
891 const gdb_byte *sizes;
892 } v2;
d2854d8d
CT
893 struct
894 {
895 /* This is indexed by column number and gives the id of the section
896 in that column. */
897#define MAX_NR_V5_DWO_SECTIONS \
898 (1 /* .debug_info */ \
899 + 1 /* .debug_abbrev */ \
900 + 1 /* .debug_line */ \
901 + 1 /* .debug_loclists */ \
902 + 1 /* .debug_str_offsets */ \
903 + 1 /* .debug_macro */ \
904 + 1 /* .debug_rnglists */)
905 int section_ids[MAX_NR_V5_DWO_SECTIONS];
906 const gdb_byte *offsets;
907 const gdb_byte *sizes;
908 } v5;
73869dc2 909 } section_pool;
80626a55
DE
910};
911
912/* Data for one DWP file. */
913
914struct dwp_file
915{
400174b1
TT
916 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
917 : name (name_),
918 dbfd (std::move (abfd))
919 {
920 }
921
80626a55
DE
922 /* Name of the file. */
923 const char *name;
924
73869dc2 925 /* File format version. */
400174b1 926 int version = 0;
73869dc2 927
93417882 928 /* The bfd. */
400174b1 929 gdb_bfd_ref_ptr dbfd;
80626a55
DE
930
931 /* Section info for this file. */
400174b1 932 struct dwp_sections sections {};
80626a55 933
57d63ce2 934 /* Table of CUs in the file. */
400174b1 935 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
936
937 /* Table of TUs in the file. */
400174b1 938 const struct dwp_hash_table *tus = nullptr;
80626a55 939
19ac8c2e 940 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
941 htab_up loaded_cus;
942 htab_up loaded_tus;
80626a55 943
73869dc2
DE
944 /* Table to map ELF section numbers to their sections.
945 This is only needed for the DWP V1 file format. */
400174b1
TT
946 unsigned int num_sections = 0;
947 asection **elf_sections = nullptr;
80626a55
DE
948};
949
0963b4bd
MS
950/* Struct used to pass misc. parameters to read_die_and_children, et
951 al. which are used for both .debug_info and .debug_types dies.
952 All parameters here are unchanging for the life of the call. This
dee91e82 953 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
954
955struct die_reader_specs
956{
a32a8923 957 /* The bfd of die_section. */
93311388
DE
958 bfd* abfd;
959
960 /* The CU of the DIE we are parsing. */
961 struct dwarf2_cu *cu;
962
80626a55 963 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
964 struct dwo_file *dwo_file;
965
dee91e82 966 /* The section the die comes from.
3019eac3 967 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
968 struct dwarf2_section_info *die_section;
969
970 /* die_section->buffer. */
d521ce57 971 const gdb_byte *buffer;
f664829e
DE
972
973 /* The end of the buffer. */
974 const gdb_byte *buffer_end;
a2ce51a0 975
685af9cd
TT
976 /* The abbreviation table to use when reading the DIEs. */
977 struct abbrev_table *abbrev_table;
93311388
DE
978};
979
c0ab21c2
TT
980/* A subclass of die_reader_specs that holds storage and has complex
981 constructor and destructor behavior. */
982
983class cutu_reader : public die_reader_specs
984{
985public:
986
ab432490
SM
987 cutu_reader (dwarf2_per_cu_data *this_cu,
988 dwarf2_per_objfile *per_objfile,
c0ab21c2 989 struct abbrev_table *abbrev_table,
2e671100 990 dwarf2_cu *existing_cu,
c0ab21c2
TT
991 bool skip_partial);
992
993 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 994 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
995 struct dwarf2_cu *parent_cu = nullptr,
996 struct dwo_file *dwo_file = nullptr);
997
c0ab21c2
TT
998 DISABLE_COPY_AND_ASSIGN (cutu_reader);
999
1000 const gdb_byte *info_ptr = nullptr;
1001 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
1002 bool dummy_p = false;
1003
6751ebae
TT
1004 /* Release the new CU, putting it on the chain. This cannot be done
1005 for dummy CUs. */
1006 void keep ();
1007
c0ab21c2 1008private:
9e021579
SM
1009 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
1010 dwarf2_per_objfile *per_objfile,
2e671100 1011 dwarf2_cu *existing_cu);
c0ab21c2
TT
1012
1013 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
1014 std::unique_ptr<dwarf2_cu> m_new_cu;
1015
1016 /* The ordinary abbreviation table. */
1017 abbrev_table_up m_abbrev_table_holder;
1018
1019 /* The DWO abbreviation table. */
1020 abbrev_table_up m_dwo_abbrev_table;
1021};
dee91e82 1022
c906108c 1023/* When we construct a partial symbol table entry we only
0963b4bd 1024 need this much information. */
6f06d47b 1025struct partial_die_info : public allocate_on_obstack
c906108c 1026 {
6f06d47b
YQ
1027 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1028
1029 /* Disable assign but still keep copy ctor, which is needed
1030 load_partial_dies. */
1031 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1032
52356b79
YQ
1033 /* Adjust the partial die before generating a symbol for it. This
1034 function may set the is_external flag or change the DIE's
1035 name. */
1036 void fixup (struct dwarf2_cu *cu);
1037
48fbe735
YQ
1038 /* Read a minimal amount of information into the minimal die
1039 structure. */
1040 const gdb_byte *read (const struct die_reader_specs *reader,
1041 const struct abbrev_info &abbrev,
1042 const gdb_byte *info_ptr);
1043
7d00ffec
TT
1044 /* Compute the name of this partial DIE. This memoizes the
1045 result, so it is safe to call multiple times. */
1046 const char *name (dwarf2_cu *cu);
1047
72bf9492 1048 /* Offset of this DIE. */
6f06d47b 1049 const sect_offset sect_off;
72bf9492
DJ
1050
1051 /* DWARF-2 tag for this DIE. */
6f06d47b 1052 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1053
72bf9492 1054 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1055 const unsigned int has_children : 1;
1056
72bf9492
DJ
1057 unsigned int is_external : 1;
1058 unsigned int is_declaration : 1;
1059 unsigned int has_type : 1;
1060 unsigned int has_specification : 1;
1061 unsigned int has_pc_info : 1;
481860b3 1062 unsigned int may_be_inlined : 1;
72bf9492 1063
0c1b455e
TT
1064 /* This DIE has been marked DW_AT_main_subprogram. */
1065 unsigned int main_subprogram : 1;
1066
72bf9492
DJ
1067 /* Flag set if the SCOPE field of this structure has been
1068 computed. */
1069 unsigned int scope_set : 1;
1070
fa4028e9
JB
1071 /* Flag set if the DIE has a byte_size attribute. */
1072 unsigned int has_byte_size : 1;
1073
ff908ebf
AW
1074 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1075 unsigned int has_const_value : 1;
1076
98bfdba5
PA
1077 /* Flag set if any of the DIE's children are template arguments. */
1078 unsigned int has_template_arguments : 1;
1079
52356b79 1080 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1081 unsigned int fixup_called : 1;
1082
36586728
TT
1083 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1084 unsigned int is_dwz : 1;
1085
1086 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1087 unsigned int spec_is_dwz : 1;
1088
7d00ffec
TT
1089 unsigned int canonical_name : 1;
1090
72bf9492 1091 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1092 sometimes a default name for unnamed DIEs. */
7d00ffec 1093 const char *raw_name = nullptr;
72bf9492 1094
abc72ce4 1095 /* The linkage name, if present. */
6f06d47b 1096 const char *linkage_name = nullptr;
abc72ce4 1097
72bf9492
DJ
1098 /* The scope to prepend to our children. This is generally
1099 allocated on the comp_unit_obstack, so will disappear
1100 when this compilation unit leaves the cache. */
6f06d47b 1101 const char *scope = nullptr;
72bf9492 1102
95554aad
TT
1103 /* Some data associated with the partial DIE. The tag determines
1104 which field is live. */
1105 union
1106 {
1107 /* The location description associated with this DIE, if any. */
1108 struct dwarf_block *locdesc;
1109 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1110 sect_offset sect_off;
6f06d47b 1111 } d {};
72bf9492
DJ
1112
1113 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1114 CORE_ADDR lowpc = 0;
1115 CORE_ADDR highpc = 0;
72bf9492 1116
93311388 1117 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1118 DW_AT_sibling, if any. */
48fbe735
YQ
1119 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1120 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1121 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1122
1123 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1124 DW_AT_specification (or DW_AT_abstract_origin or
1125 DW_AT_extension). */
6f06d47b 1126 sect_offset spec_offset {};
72bf9492
DJ
1127
1128 /* Pointers to this DIE's parent, first child, and next sibling,
1129 if any. */
6f06d47b
YQ
1130 struct partial_die_info *die_parent = nullptr;
1131 struct partial_die_info *die_child = nullptr;
1132 struct partial_die_info *die_sibling = nullptr;
1133
1134 friend struct partial_die_info *
1135 dwarf2_cu::find_partial_die (sect_offset sect_off);
1136
1137 private:
1138 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1139 partial_die_info (sect_offset sect_off)
1140 : partial_die_info (sect_off, DW_TAG_padding, 0)
1141 {
1142 }
1143
1144 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1145 int has_children_)
1146 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1147 {
1148 is_external = 0;
1149 is_declaration = 0;
1150 has_type = 0;
1151 has_specification = 0;
1152 has_pc_info = 0;
1153 may_be_inlined = 0;
1154 main_subprogram = 0;
1155 scope_set = 0;
1156 has_byte_size = 0;
1157 has_const_value = 0;
1158 has_template_arguments = 0;
1159 fixup_called = 0;
1160 is_dwz = 0;
1161 spec_is_dwz = 0;
7d00ffec 1162 canonical_name = 0;
6f06d47b 1163 }
c906108c
SS
1164 };
1165
c906108c
SS
1166/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1167 but this would require a corresponding change in unpack_field_as_long
1168 and friends. */
1169static int bits_per_byte = 8;
1170
9c6a1327
TT
1171struct variant_part_builder;
1172
1173/* When reading a variant, we track a bit more information about the
1174 field, and store it in an object of this type. */
2ddeaf8a
TT
1175
1176struct variant_field
1177{
9c6a1327
TT
1178 int first_field = -1;
1179 int last_field = -1;
1180
1181 /* A variant can contain other variant parts. */
1182 std::vector<variant_part_builder> variant_parts;
1183
2ddeaf8a
TT
1184 /* If we see a DW_TAG_variant, then this will be set if this is the
1185 default branch. */
9c6a1327
TT
1186 bool default_branch = false;
1187 /* If we see a DW_AT_discr_value, then this will be the discriminant
1188 value. */
1189 ULONGEST discriminant_value = 0;
1190 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1191 data. */
1192 struct dwarf_block *discr_list_data = nullptr;
1193};
1194
1195/* This represents a DW_TAG_variant_part. */
1196
1197struct variant_part_builder
1198{
1199 /* The offset of the discriminant field. */
1200 sect_offset discriminant_offset {};
1201
1202 /* Variants that are direct children of this variant part. */
1203 std::vector<variant_field> variants;
1204
1205 /* True if we're currently reading a variant. */
1206 bool processing_variant = false;
2ddeaf8a
TT
1207};
1208
52059ffd
TT
1209struct nextfield
1210{
be2daae6
TT
1211 int accessibility = 0;
1212 int virtuality = 0;
9c6a1327
TT
1213 /* Variant parts need to find the discriminant, which is a DIE
1214 reference. We track the section offset of each field to make
1215 this link. */
1216 sect_offset offset;
be2daae6 1217 struct field field {};
52059ffd
TT
1218};
1219
1220struct fnfieldlist
1221{
be2daae6
TT
1222 const char *name = nullptr;
1223 std::vector<struct fn_field> fnfields;
52059ffd
TT
1224};
1225
c906108c
SS
1226/* The routines that read and process dies for a C struct or C++ class
1227 pass lists of data member fields and lists of member function fields
1228 in an instance of a field_info structure, as defined below. */
1229struct field_info
2de01bdb
SM
1230{
1231 /* List of data member and baseclasses fields. */
1232 std::vector<struct nextfield> fields;
1233 std::vector<struct nextfield> baseclasses;
1234
1235 /* Set if the accessibility of one of the fields is not public. */
264fc0e2 1236 bool non_public_fields = false;
2de01bdb
SM
1237
1238 /* Member function fieldlist array, contains name of possibly overloaded
1239 member function, number of overloaded member functions and a pointer
1240 to the head of the member function field chain. */
1241 std::vector<struct fnfieldlist> fnfieldlists;
1242
1243 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1244 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1245 std::vector<struct decl_field> typedef_field_list;
1246
1247 /* Nested types defined by this class and the number of elements in this
1248 list. */
1249 std::vector<struct decl_field> nested_types_list;
1250
1251 /* If non-null, this is the variant part we are currently
1252 reading. */
1253 variant_part_builder *current_variant_part = nullptr;
1254 /* This holds all the top-level variant parts attached to the type
1255 we're reading. */
1256 std::vector<variant_part_builder> variant_parts;
1257
1258 /* Return the total number of fields (including baseclasses). */
1259 int nfields () const
c5aa993b 1260 {
2de01bdb
SM
1261 return fields.size () + baseclasses.size ();
1262 }
1263};
c906108c 1264
ae038cb0
DJ
1265/* Loaded secondary compilation units are kept in memory until they
1266 have not been referenced for the processing of this many
1267 compilation units. Set this to zero to disable caching. Cache
1268 sizes of up to at least twenty will improve startup time for
1269 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1270static int dwarf_max_cache_age = 5;
920d2a44 1271static void
b4f54984
DE
1272show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1273 struct cmd_list_element *c, const char *value)
920d2a44 1274{
3e43a32a 1275 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1276 "DWARF compilation units is %s.\n"),
920d2a44
AC
1277 value);
1278}
4390d890 1279\f
c906108c
SS
1280/* local function prototypes */
1281
918dd910
JK
1282static void dwarf2_find_base_address (struct die_info *die,
1283 struct dwarf2_cu *cu);
1284
891813be 1285static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1286 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1287 const char *name);
0018ea6f 1288
f1902523
JK
1289static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1290 const gdb_byte *info_ptr,
3e225074 1291 struct die_info *type_unit_die);
f1902523 1292
976ca316 1293static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
c906108c 1294
72bf9492
DJ
1295static void scan_partial_symbols (struct partial_die_info *,
1296 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1297 int, struct dwarf2_cu *);
c906108c 1298
72bf9492
DJ
1299static void add_partial_symbol (struct partial_die_info *,
1300 struct dwarf2_cu *);
63d06c5c 1301
72bf9492
DJ
1302static void add_partial_namespace (struct partial_die_info *pdi,
1303 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1304 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1305
5d7cb8df 1306static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1307 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1308 struct dwarf2_cu *cu);
1309
72bf9492
DJ
1310static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1311 struct dwarf2_cu *cu);
91c24f0a 1312
bc30ff58
JB
1313static void add_partial_subprogram (struct partial_die_info *pdi,
1314 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1315 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1316
d521ce57 1317static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1318
dee91e82 1319static struct partial_die_info *load_partial_dies
d521ce57 1320 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1321
fb816e8b
TV
1322/* A pair of partial_die_info and compilation unit. */
1323struct cu_partial_die_info
1324{
1325 /* The compilation unit of the partial_die_info. */
1326 struct dwarf2_cu *cu;
1327 /* A partial_die_info. */
1328 struct partial_die_info *pdi;
122cf0f2
AB
1329
1330 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1331 : cu (cu),
1332 pdi (pdi)
405feb71 1333 { /* Nothing. */ }
122cf0f2
AB
1334
1335private:
1336 cu_partial_die_info () = delete;
fb816e8b
TV
1337};
1338
122cf0f2
AB
1339static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1340 struct dwarf2_cu *);
72bf9492 1341
d521ce57
TT
1342static const gdb_byte *read_attribute (const struct die_reader_specs *,
1343 struct attribute *, struct attr_abbrev *,
7a5f294d 1344 const gdb_byte *);
18a8505e
AT
1345
1346static void read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 1347 struct attribute *attr, dwarf_tag tag);
18a8505e
AT
1348
1349static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1350
976ca316
SM
1351static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1352 dwarf2_section_info *, sect_offset);
f4dc4d17 1353
ed2dc618 1354static const char *read_indirect_string
976ca316 1355 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
ed2dc618 1356 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1357
ed2dc618 1358static const char *read_indirect_string_at_offset
976ca316 1359 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
927aa2e7 1360
d521ce57
TT
1361static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1362 const gdb_byte *,
3019eac3
DE
1363 unsigned int *);
1364
18a8505e
AT
1365static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1366 ULONGEST str_index);
1367
1368static const char *read_stub_str_index (struct dwarf2_cu *cu,
1369 ULONGEST str_index);
3019eac3 1370
e142c38c 1371static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1372
e142c38c
DJ
1373static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1374 struct dwarf2_cu *);
c906108c 1375
7d45c7c3 1376static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
dda83cd7 1377 struct dwarf2_cu *cu);
7d45c7c3 1378
a084a2a6
AT
1379static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1380
05cf31d1 1381static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
dda83cd7 1382 struct dwarf2_cu *cu);
05cf31d1 1383
e142c38c 1384static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1385
e142c38c 1386static struct die_info *die_specification (struct die_info *die,
f2f0e013 1387 struct dwarf2_cu **);
63d06c5c 1388
9c541725 1389static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1390 struct dwarf2_cu *cu);
debd256d 1391
f3f5162e 1392static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1393 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1394 CORE_ADDR, int decode_mapping);
c906108c 1395
804d2729
TT
1396static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1397 const char *);
c906108c 1398
a14ed312 1399static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1400 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1401
ff39bb5e 1402static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1403 struct dwarf2_cu *);
c906108c 1404
ff39bb5e 1405static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1406 struct type *type,
1407 const char *name,
1408 struct obstack *obstack,
12df843f 1409 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1410 const gdb_byte **bytes,
98bfdba5 1411 struct dwarf2_locexpr_baton **baton);
2df3850c 1412
57567375
TT
1413static struct type *read_subrange_index_type (struct die_info *die,
1414 struct dwarf2_cu *cu);
1415
e7c27a73 1416static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1417
b4ba55a1
JB
1418static int need_gnat_info (struct dwarf2_cu *);
1419
3e43a32a
MS
1420static struct type *die_descriptive_type (struct die_info *,
1421 struct dwarf2_cu *);
b4ba55a1
JB
1422
1423static void set_descriptive_type (struct type *, struct die_info *,
1424 struct dwarf2_cu *);
1425
e7c27a73
DJ
1426static struct type *die_containing_type (struct die_info *,
1427 struct dwarf2_cu *);
c906108c 1428
ff39bb5e 1429static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1430 struct dwarf2_cu *);
c906108c 1431
f792889a 1432static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1433
673bfd45
DE
1434static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1435
0d5cff50 1436static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1437
6e70227d 1438static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1439 const char *suffix, int physname,
1440 struct dwarf2_cu *cu);
63d06c5c 1441
e7c27a73 1442static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1443
348e048f
DE
1444static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1445
e7c27a73 1446static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1447
e7c27a73 1448static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1449
96408a79
SA
1450static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1451
71a3c369
TT
1452static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1453
41144253 1454/* Return the .debug_loclists section to use for cu. */
1455static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1456
d0ce17d8
CT
1457/* Return the .debug_rnglists section to use for cu. */
1458static struct dwarf2_section_info *cu_debug_rnglists_section
1459 (struct dwarf2_cu *cu, dwarf_tag tag);
1460
3a2b436a 1461/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1462 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1463enum pc_bounds_kind
1464{
e385593e 1465 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1466 PC_BOUNDS_NOT_PRESENT,
1467
e385593e
JK
1468 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1469 were present but they do not form a valid range of PC addresses. */
1470 PC_BOUNDS_INVALID,
1471
3a2b436a
JK
1472 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1473 PC_BOUNDS_RANGES,
1474
1475 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1476 PC_BOUNDS_HIGH_LOW,
1477};
1478
1479static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1480 CORE_ADDR *, CORE_ADDR *,
1481 struct dwarf2_cu *,
891813be 1482 dwarf2_psymtab *);
c906108c 1483
fae299cd
DC
1484static void get_scope_pc_bounds (struct die_info *,
1485 CORE_ADDR *, CORE_ADDR *,
1486 struct dwarf2_cu *);
1487
801e3a5b 1488static void dwarf2_record_block_ranges (struct die_info *, struct block *,
dda83cd7 1489 CORE_ADDR, struct dwarf2_cu *);
801e3a5b 1490
a14ed312 1491static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1492 struct dwarf2_cu *);
c906108c 1493
a14ed312 1494static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1495 struct type *, struct dwarf2_cu *);
c906108c 1496
a14ed312 1497static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1498 struct die_info *, struct type *,
e7c27a73 1499 struct dwarf2_cu *);
c906108c 1500
a14ed312 1501static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1502 struct type *,
1503 struct dwarf2_cu *);
c906108c 1504
134d01f1 1505static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1506
e7c27a73 1507static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1508
e7c27a73 1509static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1510
5d7cb8df
JK
1511static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1512
804d2729 1513static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1514
27aa8d6a
SW
1515static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1516
74921315
KS
1517static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1518
f55ee35c
JK
1519static struct type *read_module_type (struct die_info *die,
1520 struct dwarf2_cu *cu);
1521
38d518c9 1522static const char *namespace_name (struct die_info *die,
e142c38c 1523 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1524
134d01f1 1525static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1526
7d79de9a
TT
1527static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1528 bool * = nullptr);
c906108c 1529
6e70227d 1530static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1531 struct dwarf2_cu *);
1532
bf6af496 1533static struct die_info *read_die_and_siblings_1
d521ce57 1534 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1535 struct die_info *);
639d11d3 1536
dee91e82 1537static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1538 const gdb_byte *info_ptr,
1539 const gdb_byte **new_info_ptr,
639d11d3
DC
1540 struct die_info *parent);
1541
d521ce57
TT
1542static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1543 struct die_info **, const gdb_byte *,
3e225074 1544 int);
3019eac3 1545
d521ce57 1546static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1547 struct die_info **, const gdb_byte *);
93311388 1548
e7c27a73 1549static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1550
15d034d0 1551static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1552 struct objfile *);
71c25dea 1553
15d034d0 1554static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1555
15d034d0 1556static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1557 struct die_info *die,
1558 struct dwarf2_cu *cu);
1559
ca69b9e6
DE
1560static const char *dwarf2_physname (const char *name, struct die_info *die,
1561 struct dwarf2_cu *cu);
1562
e142c38c 1563static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1564 struct dwarf2_cu **);
9219021c 1565
d97bc12b
DE
1566static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1567
1568static void dump_die_for_error (struct die_info *);
1569
1570static void dump_die_1 (struct ui_file *, int level, int max_level,
1571 struct die_info *);
c906108c 1572
d97bc12b 1573/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1574
51545339 1575static void store_in_ref_table (struct die_info *,
10b3939b 1576 struct dwarf2_cu *);
c906108c 1577
348e048f 1578static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1579 const struct attribute *,
348e048f
DE
1580 struct dwarf2_cu **);
1581
10b3939b 1582static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1583 const struct attribute *,
f2f0e013 1584 struct dwarf2_cu **);
c906108c 1585
348e048f 1586static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1587 const struct attribute *,
348e048f
DE
1588 struct dwarf2_cu **);
1589
ac9ec31b
DE
1590static struct type *get_signatured_type (struct die_info *, ULONGEST,
1591 struct dwarf2_cu *);
1592
1593static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1594 const struct attribute *,
ac9ec31b
DE
1595 struct dwarf2_cu *);
1596
ab432490
SM
1597static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1598 dwarf2_per_objfile *per_objfile);
348e048f 1599
ab432490
SM
1600static void read_signatured_type (signatured_type *sig_type,
1601 dwarf2_per_objfile *per_objfile);
348e048f 1602
63e43d3a
PMR
1603static int attr_to_dynamic_prop (const struct attribute *attr,
1604 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1605 struct dynamic_prop *prop, struct type *type);
63e43d3a 1606
c906108c
SS
1607/* memory allocation interface */
1608
7b5a2f43 1609static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1610
b60c80d6 1611static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1612
43f3e411 1613static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1614
8cf6f0b1
TT
1615static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1616 struct dwarf2_loclist_baton *baton,
ff39bb5e 1617 const struct attribute *attr);
8cf6f0b1 1618
ff39bb5e 1619static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1620 struct symbol *sym,
f1e6e072
TT
1621 struct dwarf2_cu *cu,
1622 int is_block);
4c2df51b 1623
d521ce57
TT
1624static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1625 const gdb_byte *info_ptr,
1626 struct abbrev_info *abbrev);
4bb7a0a7 1627
72bf9492
DJ
1628static hashval_t partial_die_hash (const void *item);
1629
1630static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1631
ae038cb0 1632static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618 1633 (sect_offset sect_off, unsigned int offset_in_dwz,
976ca316 1634 dwarf2_per_objfile *per_objfile);
ae038cb0 1635
9816fde3 1636static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1637 struct die_info *comp_unit_die,
1638 enum language pretend_language);
93311388 1639
f792889a 1640static struct type *set_die_type (struct die_info *, struct type *,
57567375 1641 struct dwarf2_cu *, bool = false);
1c379e20 1642
976ca316 1643static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
ae038cb0 1644
976ca316 1645static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1fd400ff 1646
ab432490
SM
1647static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1648 dwarf2_per_objfile *per_objfile,
4a636814 1649 dwarf2_cu *existing_cu,
ab432490
SM
1650 bool skip_partial,
1651 enum language pretend_language);
10b3939b 1652
8fc0b21d 1653static void process_full_comp_unit (dwarf2_cu *cu,
47b14e86 1654 enum language pretend_language);
10b3939b 1655
8fc0b21d 1656static void process_full_type_unit (dwarf2_cu *cu,
47b14e86 1657 enum language pretend_language);
f4dc4d17 1658
10b3939b
DJ
1659static void dwarf2_add_dependence (struct dwarf2_cu *,
1660 struct dwarf2_per_cu_data *);
1661
ae038cb0
DJ
1662static void dwarf2_mark (struct dwarf2_cu *);
1663
b64f50a1 1664static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1665 dwarf2_per_cu_data *per_cu,
1666 dwarf2_per_objfile *per_objfile);
673bfd45 1667
f792889a 1668static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1669
120ce1b5
SM
1670static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1671 dwarf2_per_objfile *per_objfile,
95554aad
TT
1672 enum language pretend_language);
1673
976ca316 1674static void process_queue (dwarf2_per_objfile *per_objfile);
9291a0cd 1675
b303c6f6
AB
1676/* Class, the destructor of which frees all allocated queue entries. This
1677 will only have work to do if an error was thrown while processing the
1678 dwarf. If no error was thrown then the queue entries should have all
1679 been processed, and freed, as we went along. */
1680
1681class dwarf2_queue_guard
1682{
1683public:
39856def
TT
1684 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1685 : m_per_objfile (per_objfile)
1686 {
1687 }
b303c6f6
AB
1688
1689 /* Free any entries remaining on the queue. There should only be
1690 entries left if we hit an error while processing the dwarf. */
1691 ~dwarf2_queue_guard ()
1692 {
39856def
TT
1693 /* Ensure that no memory is allocated by the queue. */
1694 std::queue<dwarf2_queue_item> empty;
5989a64e 1695 std::swap (m_per_objfile->per_bfd->queue, empty);
39856def 1696 }
b303c6f6 1697
39856def 1698 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1699
39856def
TT
1700private:
1701 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1702};
1703
39856def
TT
1704dwarf2_queue_item::~dwarf2_queue_item ()
1705{
1706 /* Anything still marked queued is likely to be in an
1707 inconsistent state, so discard it. */
1708 if (per_cu->queued)
1709 {
7188ed02 1710 per_objfile->remove_cu (per_cu);
39856def
TT
1711 per_cu->queued = 0;
1712 }
1713}
1714
d721ba37
PA
1715/* The return type of find_file_and_directory. Note, the enclosed
1716 string pointers are only valid while this object is valid. */
1717
1718struct file_and_directory
1719{
1720 /* The filename. This is never NULL. */
1721 const char *name;
1722
1723 /* The compilation directory. NULL if not known. If we needed to
1724 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1725 points directly to the DW_AT_comp_dir string attribute owned by
1726 the obstack that owns the DIE. */
1727 const char *comp_dir;
1728
1729 /* If we needed to build a new string for comp_dir, this is what
1730 owns the storage. */
1731 std::string comp_dir_storage;
1732};
1733
1734static file_and_directory find_file_and_directory (struct die_info *die,
1735 struct dwarf2_cu *cu);
9291a0cd 1736
298e9637 1737static htab_up allocate_signatured_type_table ();
1fd400ff 1738
298e9637 1739static htab_up allocate_dwo_unit_table ();
3019eac3 1740
57d63ce2 1741static struct dwo_unit *lookup_dwo_unit_in_dwp
976ca316
SM
1742 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1743 const char *comp_dir, ULONGEST signature, int is_debug_types);
a2ce51a0 1744
976ca316 1745static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
a2ce51a0 1746
3019eac3 1747static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1748 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1749 ULONGEST signature);
3019eac3
DE
1750
1751static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1752 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1753
1b555f17 1754static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
89e63ee4 1755
263db9a1
TT
1756/* A unique pointer to a dwo_file. */
1757
51ac9db5 1758typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1759
976ca316 1760static void process_cu_includes (dwarf2_per_objfile *per_objfile);
95554aad 1761
1b80a9fa 1762static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1763
1764static void free_line_header_voidp (void *arg);
4390d890
DE
1765\f
1766/* Various complaints about symbol reading that don't abort the process. */
1767
4390d890
DE
1768static void
1769dwarf2_debug_line_missing_file_complaint (void)
1770{
b98664d3 1771 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1772}
1773
1774static void
1775dwarf2_debug_line_missing_end_sequence_complaint (void)
1776{
b98664d3 1777 complaint (_(".debug_line section has line "
4390d890
DE
1778 "program sequence without an end"));
1779}
1780
1781static void
1782dwarf2_complex_location_expr_complaint (void)
1783{
b98664d3 1784 complaint (_("location expression too complex"));
4390d890
DE
1785}
1786
1787static void
1788dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1789 int arg3)
1790{
b98664d3 1791 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1792 arg1, arg2, arg3);
1793}
1794
4390d890
DE
1795static void
1796dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1797{
b98664d3 1798 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1799 arg1, arg2);
1800}
527f3840
JK
1801
1802/* Hash function for line_header_hash. */
1803
1804static hashval_t
1805line_header_hash (const struct line_header *ofs)
1806{
9c541725 1807 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1808}
1809
1810/* Hash function for htab_create_alloc_ex for line_header_hash. */
1811
1812static hashval_t
1813line_header_hash_voidp (const void *item)
1814{
9a3c8263 1815 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1816
1817 return line_header_hash (ofs);
1818}
1819
1820/* Equality function for line_header_hash. */
1821
1822static int
1823line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1824{
9a3c8263
SM
1825 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1826 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1827
9c541725 1828 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1829 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1830}
1831
4390d890 1832\f
9291a0cd 1833
330cdd98
PA
1834/* See declaration. */
1835
5989a64e
SM
1836dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1837 bool can_copy_)
c3699833
SM
1838 : obfd (obfd),
1839 can_copy (can_copy_)
330cdd98
PA
1840{
1841 if (names == NULL)
1842 names = &dwarf2_elf_names;
1843
330cdd98
PA
1844 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1845 locate_sections (obfd, sec, *names);
1846}
1847
5989a64e 1848dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98 1849{
b76e467d 1850 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1851 per_cu->imported_symtabs_free ();
fc8e7e75 1852
b2bdb8cf 1853 for (signatured_type *sig_type : all_type_units)
ae640021 1854 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1855
5989a64e 1856 /* Everything else should be on this->obstack. */
330cdd98
PA
1857}
1858
7188ed02 1859/* See read.h. */
330cdd98
PA
1860
1861void
7188ed02 1862dwarf2_per_objfile::remove_all_cus ()
330cdd98 1863{
7188ed02
SM
1864 for (auto pair : m_dwarf2_cus)
1865 delete pair.second;
330cdd98 1866
7188ed02 1867 m_dwarf2_cus.clear ();
330cdd98
PA
1868}
1869
11ed8cad
TT
1870/* A helper class that calls free_cached_comp_units on
1871 destruction. */
1872
1873class free_cached_comp_units
1874{
1875public:
1876
1877 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1878 : m_per_objfile (per_objfile)
1879 {
1880 }
1881
1882 ~free_cached_comp_units ()
1883 {
7188ed02 1884 m_per_objfile->remove_all_cus ();
11ed8cad
TT
1885 }
1886
1887 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1888
1889private:
1890
1891 dwarf2_per_objfile *m_per_objfile;
1892};
1893
af758d11
SM
1894/* See read.h. */
1895
1896bool
1897dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1898{
1899 gdb_assert (per_cu->index < this->m_symtabs.size ());
1900
1901 return this->m_symtabs[per_cu->index] != nullptr;
1902}
1903
1904/* See read.h. */
1905
1906compunit_symtab *
1907dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1908{
1909 gdb_assert (per_cu->index < this->m_symtabs.size ());
1910
1911 return this->m_symtabs[per_cu->index];
1912}
1913
1914/* See read.h. */
1915
1916void
1917dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1918 compunit_symtab *symtab)
1919{
1920 gdb_assert (per_cu->index < this->m_symtabs.size ());
1921 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1922
1923 this->m_symtabs[per_cu->index] = symtab;
1924}
1925
c906108c 1926/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1927 information and return true if we have enough to do something.
1928 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1929 ELF names are used. CAN_COPY is true for formats where symbol
1930 interposition is possible and so symbol values must follow copy
1931 relocation rules. */
c906108c
SS
1932
1933int
251d32d9 1934dwarf2_has_info (struct objfile *objfile,
dda83cd7 1935 const struct dwarf2_debug_sections *names,
4b610737 1936 bool can_copy)
c906108c 1937{
97cbe998
SDJ
1938 if (objfile->flags & OBJF_READNEVER)
1939 return 0;
1940
976ca316 1941 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 1942
976ca316 1943 if (per_objfile == NULL)
5989a64e 1944 {
17ee85fc
TT
1945 dwarf2_per_bfd *per_bfd;
1946
1947 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
dda83cd7 1948 doesn't require relocations and if there aren't partial symbols
17ee85fc
TT
1949 from some other reader. */
1950 if (!objfile_has_partial_symbols (objfile)
1951 && !gdb_bfd_requires_relocations (objfile->obfd))
1952 {
1953 /* See if one has been created for this BFD yet. */
1954 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1955
1956 if (per_bfd == nullptr)
1957 {
1958 /* No, create it now. */
1959 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1960 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1961 }
1962 }
1963 else
1964 {
1965 /* No sharing possible, create one specifically for this objfile. */
1966 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1967 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1968 }
5989a64e 1969
976ca316 1970 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 1971 }
5bfd760d 1972
976ca316
SM
1973 return (!per_objfile->per_bfd->info.is_virtual
1974 && per_objfile->per_bfd->info.s.section != NULL
1975 && !per_objfile->per_bfd->abbrev.is_virtual
1976 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1977}
1978
251d32d9
TG
1979/* When loading sections, we look either for uncompressed section or for
1980 compressed section names. */
233a11ab
CS
1981
1982static int
251d32d9 1983section_is_p (const char *section_name,
dda83cd7 1984 const struct dwarf2_section_names *names)
233a11ab 1985{
251d32d9
TG
1986 if (names->normal != NULL
1987 && strcmp (section_name, names->normal) == 0)
1988 return 1;
1989 if (names->compressed != NULL
1990 && strcmp (section_name, names->compressed) == 0)
1991 return 1;
1992 return 0;
233a11ab
CS
1993}
1994
330cdd98 1995/* See declaration. */
c906108c 1996
330cdd98 1997void
5989a64e
SM
1998dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1999 const dwarf2_debug_sections &names)
c906108c 2000{
fd361982 2001 flagword aflag = bfd_section_flags (sectp);
251d32d9 2002
dc7650b8
JK
2003 if ((aflag & SEC_HAS_CONTENTS) == 0)
2004 {
2005 }
950b7495
KS
2006 else if (elf_section_data (sectp)->this_hdr.sh_size
2007 > bfd_get_file_size (abfd))
2008 {
2009 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2010 warning (_("Discarding section %s which has a section size (%s"
2011 ") larger than the file size [in module %s]"),
2012 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2013 bfd_get_filename (abfd));
2014 }
330cdd98 2015 else if (section_is_p (sectp->name, &names.info))
c906108c 2016 {
330cdd98 2017 this->info.s.section = sectp;
fd361982 2018 this->info.size = bfd_section_size (sectp);
c906108c 2019 }
330cdd98 2020 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2021 {
330cdd98 2022 this->abbrev.s.section = sectp;
fd361982 2023 this->abbrev.size = bfd_section_size (sectp);
c906108c 2024 }
330cdd98 2025 else if (section_is_p (sectp->name, &names.line))
c906108c 2026 {
330cdd98 2027 this->line.s.section = sectp;
fd361982 2028 this->line.size = bfd_section_size (sectp);
c906108c 2029 }
330cdd98 2030 else if (section_is_p (sectp->name, &names.loc))
c906108c 2031 {
330cdd98 2032 this->loc.s.section = sectp;
fd361982 2033 this->loc.size = bfd_section_size (sectp);
c906108c 2034 }
330cdd98 2035 else if (section_is_p (sectp->name, &names.loclists))
43988095 2036 {
330cdd98 2037 this->loclists.s.section = sectp;
fd361982 2038 this->loclists.size = bfd_section_size (sectp);
43988095 2039 }
330cdd98 2040 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2041 {
330cdd98 2042 this->macinfo.s.section = sectp;
fd361982 2043 this->macinfo.size = bfd_section_size (sectp);
c906108c 2044 }
330cdd98 2045 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2046 {
330cdd98 2047 this->macro.s.section = sectp;
fd361982 2048 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2049 }
330cdd98 2050 else if (section_is_p (sectp->name, &names.str))
c906108c 2051 {
330cdd98 2052 this->str.s.section = sectp;
fd361982 2053 this->str.size = bfd_section_size (sectp);
c906108c 2054 }
18a8505e
AT
2055 else if (section_is_p (sectp->name, &names.str_offsets))
2056 {
2057 this->str_offsets.s.section = sectp;
2058 this->str_offsets.size = bfd_section_size (sectp);
2059 }
330cdd98 2060 else if (section_is_p (sectp->name, &names.line_str))
43988095 2061 {
330cdd98 2062 this->line_str.s.section = sectp;
fd361982 2063 this->line_str.size = bfd_section_size (sectp);
43988095 2064 }
330cdd98 2065 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2066 {
330cdd98 2067 this->addr.s.section = sectp;
fd361982 2068 this->addr.size = bfd_section_size (sectp);
3019eac3 2069 }
330cdd98 2070 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2071 {
330cdd98 2072 this->frame.s.section = sectp;
fd361982 2073 this->frame.size = bfd_section_size (sectp);
b6af0555 2074 }
330cdd98 2075 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2076 {
330cdd98 2077 this->eh_frame.s.section = sectp;
fd361982 2078 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2079 }
330cdd98 2080 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2081 {
330cdd98 2082 this->ranges.s.section = sectp;
fd361982 2083 this->ranges.size = bfd_section_size (sectp);
af34e669 2084 }
330cdd98 2085 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2086 {
330cdd98 2087 this->rnglists.s.section = sectp;
fd361982 2088 this->rnglists.size = bfd_section_size (sectp);
43988095 2089 }
330cdd98 2090 else if (section_is_p (sectp->name, &names.types))
348e048f 2091 {
8b70b953
TT
2092 struct dwarf2_section_info type_section;
2093
2094 memset (&type_section, 0, sizeof (type_section));
049412e3 2095 type_section.s.section = sectp;
fd361982 2096 type_section.size = bfd_section_size (sectp);
8b70b953 2097
fd5866f6 2098 this->types.push_back (type_section);
348e048f 2099 }
330cdd98 2100 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2101 {
330cdd98 2102 this->gdb_index.s.section = sectp;
fd361982 2103 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2104 }
927aa2e7
JK
2105 else if (section_is_p (sectp->name, &names.debug_names))
2106 {
2107 this->debug_names.s.section = sectp;
fd361982 2108 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2109 }
2110 else if (section_is_p (sectp->name, &names.debug_aranges))
2111 {
2112 this->debug_aranges.s.section = sectp;
fd361982 2113 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2114 }
dce234bc 2115
fd361982
AM
2116 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2117 && bfd_section_vma (sectp) == 0)
330cdd98 2118 this->has_section_at_zero = true;
c906108c
SS
2119}
2120
dce234bc 2121/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2122 SECTION_NAME. */
af34e669 2123
dce234bc 2124void
3017a003 2125dwarf2_get_section_info (struct objfile *objfile,
dda83cd7
SM
2126 enum dwarf2_section_enum sect,
2127 asection **sectp, const gdb_byte **bufp,
2128 bfd_size_type *sizep)
dce234bc 2129{
976ca316 2130 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 2131 struct dwarf2_section_info *info;
a3b2a86b
TT
2132
2133 /* We may see an objfile without any DWARF, in which case we just
2134 return nothing. */
976ca316 2135 if (per_objfile == NULL)
a3b2a86b
TT
2136 {
2137 *sectp = NULL;
2138 *bufp = NULL;
2139 *sizep = 0;
2140 return;
2141 }
3017a003
TG
2142 switch (sect)
2143 {
2144 case DWARF2_DEBUG_FRAME:
976ca316 2145 info = &per_objfile->per_bfd->frame;
3017a003
TG
2146 break;
2147 case DWARF2_EH_FRAME:
976ca316 2148 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
2149 break;
2150 default:
2151 gdb_assert_not_reached ("unexpected section");
2152 }
dce234bc 2153
96b79293 2154 info->read (objfile);
dce234bc 2155
96b79293 2156 *sectp = info->get_bfd_section ();
dce234bc
PP
2157 *bufp = info->buffer;
2158 *sizep = info->size;
2159}
2160
36586728
TT
2161/* A helper function to find the sections for a .dwz file. */
2162
2163static void
5bb6e9dd 2164locate_dwz_sections (bfd *abfd, asection *sectp, dwz_file *dwz_file)
36586728 2165{
36586728
TT
2166 /* Note that we only support the standard ELF names, because .dwz
2167 is ELF-only (at the time of writing). */
2168 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2169 {
049412e3 2170 dwz_file->abbrev.s.section = sectp;
fd361982 2171 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2172 }
2173 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2174 {
049412e3 2175 dwz_file->info.s.section = sectp;
fd361982 2176 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2177 }
2178 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2179 {
049412e3 2180 dwz_file->str.s.section = sectp;
fd361982 2181 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2182 }
2183 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2184 {
049412e3 2185 dwz_file->line.s.section = sectp;
fd361982 2186 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2187 }
2188 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2189 {
049412e3 2190 dwz_file->macro.s.section = sectp;
fd361982 2191 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2192 }
2ec9a5e0
TT
2193 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2194 {
049412e3 2195 dwz_file->gdb_index.s.section = sectp;
fd361982 2196 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2197 }
927aa2e7
JK
2198 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2199 {
2200 dwz_file->debug_names.s.section = sectp;
fd361982 2201 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2202 }
36586728
TT
2203}
2204
2bf3b79d
SDJ
2205/* Attempt to find a .dwz file (whose full path is represented by
2206 FILENAME) in all of the specified debug file directories provided.
2207
2208 Return the equivalent gdb_bfd_ref_ptr of the .dwz file found, or
2209 nullptr if it could not find anything. */
2210
2211static gdb_bfd_ref_ptr
2212dwz_search_other_debugdirs (std::string &filename, bfd_byte *buildid,
2213 size_t buildid_len)
2214{
2215 /* Let's assume that the path represented by FILENAME has the
2216 "/.dwz/" subpath in it. This is what (most) GNU/Linux
2217 distributions do, anyway. */
2218 size_t dwz_pos = filename.find ("/.dwz/");
2219
2220 if (dwz_pos == std::string::npos)
2221 return nullptr;
2222
2223 /* This is an obvious assertion, but it's here more to educate
2224 future readers of this code that FILENAME at DWZ_POS *must*
2225 contain a directory separator. */
2226 gdb_assert (IS_DIR_SEPARATOR (filename[dwz_pos]));
2227
2228 gdb_bfd_ref_ptr dwz_bfd;
2229 std::vector<gdb::unique_xmalloc_ptr<char>> debugdir_vec
2230 = dirnames_to_char_ptr_vec (debug_file_directory);
2231
2232 for (const gdb::unique_xmalloc_ptr<char> &debugdir : debugdir_vec)
2233 {
2234 /* The idea is to iterate over the
2235 debug file directories provided by the user and
2236 replace the hard-coded path in the "filename" by each
2237 debug-file-directory.
2238
2239 For example, suppose that filename is:
2240
2241 /usr/lib/debug/.dwz/foo.dwz
2242
2243 And suppose that we have "$HOME/bar" as the
2244 debug-file-directory. We would then adjust filename
2245 to look like:
2246
2247 $HOME/bar/.dwz/foo.dwz
2248
2249 which would hopefully allow us to find the alt debug
2250 file. */
2251 std::string ddir = debugdir.get ();
2252
2253 if (ddir.empty ())
2254 continue;
2255
2256 /* Make sure the current debug-file-directory ends with a
2257 directory separator. This is needed because, if FILENAME
2258 contains something like "/usr/lib/abcde/.dwz/foo.dwz" and
2259 DDIR is "/usr/lib/abc", then could wrongfully skip it
2260 below. */
2261 if (!IS_DIR_SEPARATOR (ddir.back ()))
2262 ddir += SLASH_STRING;
2263
2264 /* Check whether the beginning of FILENAME is DDIR. If it is,
2265 then we are dealing with a file which we already attempted to
2266 open before, so we just skip it and continue processing the
2267 remaining debug file directories. */
2268 if (filename.size () > ddir.size ()
2269 && filename.compare (0, ddir.size (), ddir) == 0)
2270 continue;
2271
2272 /* Replace FILENAME's default debug-file-directory with
2273 DDIR. */
2274 std::string new_filename = ddir + &filename[dwz_pos + 1];
2275
2276 dwz_bfd = gdb_bfd_open (new_filename.c_str (), gnutarget);
2277
2278 if (dwz_bfd == nullptr)
2279 continue;
2280
2281 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2282 {
2283 dwz_bfd.reset (nullptr);
2284 continue;
2285 }
2286
2287 /* Found it. */
2288 break;
2289 }
2290
2291 return dwz_bfd;
2292}
2293
c4973306 2294/* See dwarf2read.h. */
36586728 2295
c4973306 2296struct dwz_file *
c3699833 2297dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
36586728 2298{
acd13123 2299 bfd_size_type buildid_len_arg;
dc294be5
TT
2300 size_t buildid_len;
2301 bfd_byte *buildid;
36586728 2302
c3699833
SM
2303 if (per_bfd->dwz_file != NULL)
2304 return per_bfd->dwz_file.get ();
36586728 2305
4db1a1dc 2306 bfd_set_error (bfd_error_no_error);
791afaa2 2307 gdb::unique_xmalloc_ptr<char> data
c3699833 2308 (bfd_get_alt_debug_link_info (per_bfd->obfd,
791afaa2 2309 &buildid_len_arg, &buildid));
4db1a1dc
TT
2310 if (data == NULL)
2311 {
2312 if (bfd_get_error () == bfd_error_no_error)
2313 return NULL;
2314 error (_("could not read '.gnu_debugaltlink' section: %s"),
2315 bfd_errmsg (bfd_get_error ()));
2316 }
791afaa2
TT
2317
2318 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2319
acd13123
TT
2320 buildid_len = (size_t) buildid_len_arg;
2321
2bf3b79d 2322 std::string filename = data.get ();
d721ba37 2323
2bf3b79d 2324 if (!IS_ABSOLUTE_PATH (filename.c_str ()))
36586728 2325 {
14278e1f 2326 gdb::unique_xmalloc_ptr<char> abs
c3699833 2327 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
36586728 2328
2bf3b79d 2329 filename = ldirname (abs.get ()) + SLASH_STRING + filename;
36586728
TT
2330 }
2331
dc294be5
TT
2332 /* First try the file name given in the section. If that doesn't
2333 work, try to use the build-id instead. */
2bf3b79d 2334 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename.c_str (), gnutarget));
dc294be5 2335 if (dwz_bfd != NULL)
36586728 2336 {
192b62ce 2337 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2338 dwz_bfd.reset (nullptr);
36586728
TT
2339 }
2340
dc294be5
TT
2341 if (dwz_bfd == NULL)
2342 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2343
2bf3b79d
SDJ
2344 if (dwz_bfd == nullptr)
2345 {
2346 /* If the user has provided us with different
2347 debug file directories, we can try them in order. */
2348 dwz_bfd = dwz_search_other_debugdirs (filename, buildid, buildid_len);
2349 }
2350
0d79cdc4
AM
2351 if (dwz_bfd == nullptr)
2352 {
2353 gdb::unique_xmalloc_ptr<char> alt_filename;
c3699833 2354 const char *origname = bfd_get_filename (per_bfd->obfd);
0d79cdc4
AM
2355
2356 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2357 buildid_len,
2358 origname,
2359 &alt_filename));
2360
2361 if (fd.get () >= 0)
2362 {
2363 /* File successfully retrieved from server. */
ad80db5b 2364 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2365
2366 if (dwz_bfd == nullptr)
2367 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2368 alt_filename.get ());
2369 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2370 dwz_bfd.reset (nullptr);
2371 }
2372 }
2373
dc294be5
TT
2374 if (dwz_bfd == NULL)
2375 error (_("could not find '.gnu_debugaltlink' file for %s"),
c3699833 2376 bfd_get_filename (per_bfd->obfd));
dc294be5 2377
7ff8cb8c
TT
2378 std::unique_ptr<struct dwz_file> result
2379 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2380
5bb6e9dd
TT
2381 for (asection *sec : gdb_bfd_sections (result->dwz_bfd))
2382 locate_dwz_sections (result->dwz_bfd.get (), sec, result.get ());
36586728 2383
c3699833
SM
2384 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2385 per_bfd->dwz_file = std::move (result);
2386 return per_bfd->dwz_file.get ();
36586728 2387}
9291a0cd 2388\f
7b9f3c50
DE
2389/* DWARF quick_symbols_functions support. */
2390
2391/* TUs can share .debug_line entries, and there can be a lot more TUs than
2392 unique line tables, so we maintain a separate table of all .debug_line
2393 derived entries to support the sharing.
2394 All the quick functions need is the list of file names. We discard the
2395 line_header when we're done and don't need to record it here. */
2396struct quick_file_names
2397{
094b34ac
DE
2398 /* The data used to construct the hash key. */
2399 struct stmt_list_hash hash;
7b9f3c50
DE
2400
2401 /* The number of entries in file_names, real_names. */
2402 unsigned int num_file_names;
2403
2404 /* The file names from the line table, after being run through
2405 file_full_name. */
2406 const char **file_names;
2407
2408 /* The file names from the line table after being run through
2409 gdb_realpath. These are computed lazily. */
2410 const char **real_names;
2411};
2412
2413/* When using the index (and thus not using psymtabs), each CU has an
2414 object of this type. This is used to hold information needed by
2415 the various "quick" methods. */
2416struct dwarf2_per_cu_quick_data
2417{
2418 /* The file table. This can be NULL if there was no file table
2419 or it's currently not read in.
5989a64e 2420 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2421 struct quick_file_names *file_names;
2422
7b9f3c50
DE
2423 /* A temporary mark bit used when iterating over all CUs in
2424 expand_symtabs_matching. */
2425 unsigned int mark : 1;
2426
2427 /* True if we've tried to read the file table and found there isn't one.
2428 There will be no point in trying to read it again next time. */
2429 unsigned int no_file_data : 1;
2430};
2431
094b34ac
DE
2432/* Utility hash function for a stmt_list_hash. */
2433
2434static hashval_t
2435hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2436{
2437 hashval_t v = 0;
2438
2439 if (stmt_list_hash->dwo_unit != NULL)
2440 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2441 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2442 return v;
2443}
2444
2445/* Utility equality function for a stmt_list_hash. */
2446
2447static int
2448eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2449 const struct stmt_list_hash *rhs)
2450{
2451 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2452 return 0;
2453 if (lhs->dwo_unit != NULL
2454 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2455 return 0;
2456
9c541725 2457 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2458}
2459
7b9f3c50
DE
2460/* Hash function for a quick_file_names. */
2461
2462static hashval_t
2463hash_file_name_entry (const void *e)
2464{
9a3c8263
SM
2465 const struct quick_file_names *file_data
2466 = (const struct quick_file_names *) e;
7b9f3c50 2467
094b34ac 2468 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2469}
2470
2471/* Equality function for a quick_file_names. */
2472
2473static int
2474eq_file_name_entry (const void *a, const void *b)
2475{
9a3c8263
SM
2476 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2477 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2478
094b34ac 2479 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2480}
2481
2482/* Delete function for a quick_file_names. */
2483
2484static void
2485delete_file_name_entry (void *e)
2486{
9a3c8263 2487 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2488 int i;
2489
2490 for (i = 0; i < file_data->num_file_names; ++i)
2491 {
2492 xfree ((void*) file_data->file_names[i]);
2493 if (file_data->real_names)
2494 xfree ((void*) file_data->real_names[i]);
2495 }
2496
45940949
TT
2497 /* The space for the struct itself lives on the obstack, so we don't
2498 free it here. */
7b9f3c50
DE
2499}
2500
2501/* Create a quick_file_names hash table. */
2502
5895093f 2503static htab_up
7b9f3c50
DE
2504create_quick_file_names_table (unsigned int nr_initial_entries)
2505{
5895093f
TT
2506 return htab_up (htab_create_alloc (nr_initial_entries,
2507 hash_file_name_entry, eq_file_name_entry,
2508 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2509}
9291a0cd 2510
ab432490
SM
2511/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2512 function is unrelated to symtabs, symtab would have to be created afterwards.
2513 You should call age_cached_comp_units after processing the CU. */
918dd910 2514
1b555f17 2515static dwarf2_cu *
ab432490
SM
2516load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2517 bool skip_partial)
918dd910 2518{
3019eac3 2519 if (per_cu->is_debug_types)
ab432490 2520 load_full_type_unit (per_cu, per_objfile);
918dd910 2521 else
4a636814
SM
2522 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2523 skip_partial, language_minimal);
918dd910 2524
7188ed02
SM
2525 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2526 if (cu == nullptr)
1b555f17 2527 return nullptr; /* Dummy CU. */
2dc860c0 2528
7188ed02 2529 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2530
7188ed02 2531 return cu;
918dd910
JK
2532}
2533
1350c3b4 2534/* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2fdf6df6 2535
9291a0cd 2536static void
97a1449a 2537dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2538 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2539{
f4dc4d17
DE
2540 /* Skip type_unit_groups, reading the type units they contain
2541 is handled elsewhere. */
197400e8 2542 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2543 return;
2544
b303c6f6
AB
2545 /* The destructor of dwarf2_queue_guard frees any entries left on
2546 the queue. After this point we're guaranteed to leave this function
2547 with the dwarf queue empty. */
bf6e5d01 2548 dwarf2_queue_guard q_guard (per_objfile);
9291a0cd 2549
976ca316 2550 if (!per_objfile->symtab_set_p (per_cu))
95554aad 2551 {
976ca316
SM
2552 queue_comp_unit (per_cu, per_objfile, language_minimal);
2553 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
89e63ee4
DE
2554
2555 /* If we just loaded a CU from a DWO, and we're working with an index
2556 that may badly handle TUs, load all the TUs in that DWO as well.
2557 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2558 if (!per_cu->is_debug_types
1b555f17
SM
2559 && cu != NULL
2560 && cu->dwo_unit != NULL
976ca316
SM
2561 && per_objfile->per_bfd->index_table != NULL
2562 && per_objfile->per_bfd->index_table->version <= 7
89e63ee4 2563 /* DWP files aren't supported yet. */
976ca316 2564 && get_dwp_file (per_objfile) == NULL)
1b555f17 2565 queue_and_load_all_dwo_tus (cu);
95554aad 2566 }
9291a0cd 2567
976ca316 2568 process_queue (per_objfile);
9291a0cd
TT
2569
2570 /* Age the cache, releasing compilation units that have not
2571 been used recently. */
976ca316 2572 per_objfile->age_comp_units ();
9291a0cd
TT
2573}
2574
97a1449a
SM
2575/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2576 the per-objfile for which this symtab is instantiated.
2577
2578 Returns the resulting symbol table. */
2fdf6df6 2579
43f3e411 2580static struct compunit_symtab *
97a1449a 2581dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2582 dwarf2_per_objfile *per_objfile,
97a1449a 2583 bool skip_partial)
9291a0cd 2584{
976ca316 2585 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2586
976ca316 2587 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2588 {
976ca316 2589 free_cached_comp_units freer (per_objfile);
c83dd867 2590 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2591 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2592 process_cu_includes (per_objfile);
9291a0cd 2593 }
f194fefb 2594
976ca316 2595 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2596}
2597
ff4c9fec 2598/* See declaration. */
f4dc4d17 2599
ff4c9fec 2600dwarf2_per_cu_data *
5989a64e 2601dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2602{
b76e467d 2603 if (index >= this->all_comp_units.size ())
ff4c9fec 2604 {
b76e467d 2605 index -= this->all_comp_units.size ();
b2bdb8cf 2606 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2607 return &this->all_type_units[index]->per_cu;
2608 }
f4dc4d17 2609
ff4c9fec
SM
2610 return this->all_comp_units[index];
2611}
f4dc4d17 2612
ff4c9fec 2613/* See declaration. */
2fdf6df6 2614
ff4c9fec 2615dwarf2_per_cu_data *
5989a64e 2616dwarf2_per_bfd::get_cu (int index)
1fd400ff 2617{
b76e467d 2618 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2619
ff4c9fec 2620 return this->all_comp_units[index];
f4dc4d17
DE
2621}
2622
ff4c9fec 2623/* See declaration. */
f4dc4d17 2624
ff4c9fec 2625signatured_type *
5989a64e 2626dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2627{
b2bdb8cf 2628 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2629
ff4c9fec 2630 return this->all_type_units[index];
1fd400ff
TT
2631}
2632
d3473f0c
TT
2633/* See read.h. */
2634
2635dwarf2_per_cu_data *
5989a64e 2636dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2637{
2638 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2639 result->per_bfd = this;
d3473f0c
TT
2640 result->index = m_num_psymtabs++;
2641 return result;
2642}
2643
2644/* See read.h. */
2645
2646signatured_type *
5989a64e 2647dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2648{
2649 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2650 result->per_cu.per_bfd = this;
d3473f0c
TT
2651 result->per_cu.index = m_num_psymtabs++;
2652 return result;
2653}
2654
168c9250 2655/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2656 obstack, and constructed with the specified field values. */
4b514bc8
JK
2657
2658static dwarf2_per_cu_data *
168c9250
SM
2659create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2660 struct dwarf2_section_info *section,
2661 int is_dwz,
2662 sect_offset sect_off, ULONGEST length)
4b514bc8 2663{
168c9250 2664 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2665 the_cu->sect_off = sect_off;
2666 the_cu->length = length;
4b514bc8 2667 the_cu->section = section;
168c9250 2668 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2669 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2670 the_cu->is_dwz = is_dwz;
2671 return the_cu;
2672}
2673
2ec9a5e0
TT
2674/* A helper for create_cus_from_index that handles a given list of
2675 CUs. */
2fdf6df6 2676
74a0d9f6 2677static void
168c9250 2678create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2679 const gdb_byte *cu_list, offset_type n_elements,
2680 struct dwarf2_section_info *section,
b76e467d 2681 int is_dwz)
9291a0cd 2682{
12359b5e 2683 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2684 {
74a0d9f6 2685 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2686
2687 sect_offset sect_off
2688 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2689 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2690 cu_list += 2 * 8;
2691
b76e467d 2692 dwarf2_per_cu_data *per_cu
168c9250
SM
2693 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2694 length);
2695 per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2696 }
9291a0cd
TT
2697}
2698
2ec9a5e0 2699/* Read the CU list from the mapped index, and use it to create all
168c9250 2700 the CU objects for PER_BFD. */
2ec9a5e0 2701
74a0d9f6 2702static void
168c9250 2703create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2704 const gdb_byte *cu_list, offset_type cu_list_elements,
2705 const gdb_byte *dwz_list, offset_type dwz_elements)
2706{
168c9250
SM
2707 gdb_assert (per_bfd->all_comp_units.empty ());
2708 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2709
168c9250
SM
2710 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2711 &per_bfd->info, 0);
2ec9a5e0
TT
2712
2713 if (dwz_elements == 0)
74a0d9f6 2714 return;
2ec9a5e0 2715
168c9250
SM
2716 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2717 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2718 &dwz->info, 1);
2ec9a5e0
TT
2719}
2720
1fd400ff 2721/* Create the signatured type hash table from the index. */
673bfd45 2722
74a0d9f6 2723static void
12359b5e 2724create_signatured_type_table_from_index
168c9250
SM
2725 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2726 const gdb_byte *bytes, offset_type elements)
1fd400ff 2727{
168c9250
SM
2728 gdb_assert (per_bfd->all_type_units.empty ());
2729 per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2730
298e9637 2731 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2732
12359b5e 2733 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2734 {
52dc124a 2735 struct signatured_type *sig_type;
9c541725 2736 ULONGEST signature;
1fd400ff 2737 void **slot;
9c541725 2738 cu_offset type_offset_in_tu;
1fd400ff 2739
74a0d9f6 2740 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2741 sect_offset sect_off
2742 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2743 type_offset_in_tu
2744 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2745 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2746 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2747 bytes += 3 * 8;
2748
168c9250 2749 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2750 sig_type->signature = signature;
9c541725 2751 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2752 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2753 sig_type->per_cu.section = section;
9c541725 2754 sig_type->per_cu.sect_off = sect_off;
52dc124a 2755 sig_type->per_cu.v.quick
168c9250 2756 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2757 struct dwarf2_per_cu_quick_data);
2758
b0b6a987 2759 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2760 *slot = sig_type;
1fd400ff 2761
168c9250 2762 per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2763 }
2764
168c9250 2765 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2766}
2767
927aa2e7
JK
2768/* Create the signatured type hash table from .debug_names. */
2769
2770static void
2771create_signatured_type_table_from_debug_names
976ca316 2772 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2773 const mapped_debug_names &map,
2774 struct dwarf2_section_info *section,
2775 struct dwarf2_section_info *abbrev_section)
2776{
976ca316 2777 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2778
96b79293
TT
2779 section->read (objfile);
2780 abbrev_section->read (objfile);
927aa2e7 2781
976ca316
SM
2782 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2783 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2784
298e9637 2785 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2786
2787 for (uint32_t i = 0; i < map.tu_count; ++i)
2788 {
2789 struct signatured_type *sig_type;
927aa2e7 2790 void **slot;
927aa2e7
JK
2791
2792 sect_offset sect_off
2793 = (sect_offset) (extract_unsigned_integer
2794 (map.tu_table_reordered + i * map.offset_size,
2795 map.offset_size,
2796 map.dwarf5_byte_order));
2797
2798 comp_unit_head cu_header;
976ca316 2799 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2800 abbrev_section,
927aa2e7
JK
2801 section->buffer + to_underlying (sect_off),
2802 rcuh_kind::TYPE);
2803
976ca316 2804 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2805 sig_type->signature = cu_header.signature;
2806 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2807 sig_type->per_cu.is_debug_types = 1;
2808 sig_type->per_cu.section = section;
2809 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2810 sig_type->per_cu.v.quick
976ca316 2811 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2812 struct dwarf2_per_cu_quick_data);
2813
b0b6a987 2814 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2815 *slot = sig_type;
2816
976ca316 2817 per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2818 }
2819
976ca316 2820 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2821}
2822
9291a0cd
TT
2823/* Read the address map data from the mapped index, and use it to
2824 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2825
9291a0cd 2826static void
976ca316 2827create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2828 struct mapped_index *index)
9291a0cd 2829{
976ca316 2830 struct objfile *objfile = per_objfile->objfile;
08feed99 2831 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2832 const gdb_byte *iter, *end;
9291a0cd 2833 struct addrmap *mutable_map;
9291a0cd
TT
2834 CORE_ADDR baseaddr;
2835
8268c778
PA
2836 auto_obstack temp_obstack;
2837
9291a0cd
TT
2838 mutable_map = addrmap_create_mutable (&temp_obstack);
2839
f00a2de2
PA
2840 iter = index->address_table.data ();
2841 end = iter + index->address_table.size ();
9291a0cd 2842
b3b3bada 2843 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2844
2845 while (iter < end)
2846 {
2847 ULONGEST hi, lo, cu_index;
2848 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2849 iter += 8;
2850 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2851 iter += 8;
2852 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2853 iter += 4;
f652bce2 2854
24a55014 2855 if (lo > hi)
f652bce2 2856 {
b98664d3 2857 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2858 hex_string (lo), hex_string (hi));
24a55014 2859 continue;
f652bce2 2860 }
24a55014 2861
976ca316 2862 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2863 {
b98664d3 2864 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2865 (unsigned) cu_index);
24a55014 2866 continue;
f652bce2 2867 }
24a55014 2868
79748972
TT
2869 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2870 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2871 addrmap_set_empty (mutable_map, lo, hi - 1,
976ca316 2872 per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2873 }
2874
d320c2b5 2875 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2876 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2877}
2878
927aa2e7
JK
2879/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2880 populate the objfile's psymtabs_addrmap. */
2881
2882static void
976ca316 2883create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2884 struct dwarf2_section_info *section)
2885{
976ca316 2886 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2887 bfd *abfd = objfile->obfd;
08feed99 2888 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2889 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2890
2891 auto_obstack temp_obstack;
2892 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2893
2894 std::unordered_map<sect_offset,
2895 dwarf2_per_cu_data *,
2896 gdb::hash_enum<sect_offset>>
2897 debug_info_offset_to_per_cu;
976ca316 2898 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 2899 {
927aa2e7
JK
2900 const auto insertpair
2901 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2902 if (!insertpair.second)
2903 {
2904 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2905 "debug_info_offset %s, ignoring .debug_aranges."),
2906 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2907 return;
2908 }
2909 }
2910
96b79293 2911 section->read (objfile);
927aa2e7
JK
2912
2913 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2914
2915 const gdb_byte *addr = section->buffer;
2916
2917 while (addr < section->buffer + section->size)
2918 {
2919 const gdb_byte *const entry_addr = addr;
2920 unsigned int bytes_read;
2921
2922 const LONGEST entry_length = read_initial_length (abfd, addr,
2923 &bytes_read);
2924 addr += bytes_read;
2925
2926 const gdb_byte *const entry_end = addr + entry_length;
2927 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2928 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2929 if (addr + entry_length > section->buffer + section->size)
2930 {
47e3f474 2931 warning (_("Section .debug_aranges in %s entry at offset %s "
dda83cd7 2932 "length %s exceeds section length %s, "
927aa2e7 2933 "ignoring .debug_aranges."),
47e3f474
TV
2934 objfile_name (objfile),
2935 plongest (entry_addr - section->buffer),
927aa2e7
JK
2936 plongest (bytes_read + entry_length),
2937 pulongest (section->size));
2938 return;
2939 }
2940
2941 /* The version number. */
2942 const uint16_t version = read_2_bytes (abfd, addr);
2943 addr += 2;
2944 if (version != 2)
2945 {
47e3f474 2946 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2947 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2948 objfile_name (objfile),
2949 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2950 return;
2951 }
2952
2953 const uint64_t debug_info_offset
2954 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2955 addr += offset_size;
2956 const auto per_cu_it
2957 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2958 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2959 {
47e3f474 2960 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2961 "debug_info_offset %s does not exists, "
2962 "ignoring .debug_aranges."),
47e3f474
TV
2963 objfile_name (objfile),
2964 plongest (entry_addr - section->buffer),
927aa2e7
JK
2965 pulongest (debug_info_offset));
2966 return;
2967 }
2968 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2969
2970 const uint8_t address_size = *addr++;
2971 if (address_size < 1 || address_size > 8)
2972 {
47e3f474 2973 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2974 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2975 objfile_name (objfile),
2976 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2977 return;
2978 }
2979
2980 const uint8_t segment_selector_size = *addr++;
2981 if (segment_selector_size != 0)
2982 {
47e3f474 2983 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2984 "segment_selector_size %u is not supported, "
2985 "ignoring .debug_aranges."),
47e3f474
TV
2986 objfile_name (objfile),
2987 plongest (entry_addr - section->buffer),
927aa2e7
JK
2988 segment_selector_size);
2989 return;
2990 }
2991
2992 /* Must pad to an alignment boundary that is twice the address
dda83cd7
SM
2993 size. It is undocumented by the DWARF standard but GCC does
2994 use it. */
927aa2e7
JK
2995 for (size_t padding = ((-(addr - section->buffer))
2996 & (2 * address_size - 1));
dda83cd7 2997 padding > 0; padding--)
927aa2e7
JK
2998 if (*addr++ != 0)
2999 {
47e3f474 3000 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3001 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
3002 objfile_name (objfile),
3003 plongest (entry_addr - section->buffer));
927aa2e7
JK
3004 return;
3005 }
3006
3007 for (;;)
3008 {
3009 if (addr + 2 * address_size > entry_end)
3010 {
47e3f474 3011 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3012 "address list is not properly terminated, "
3013 "ignoring .debug_aranges."),
47e3f474
TV
3014 objfile_name (objfile),
3015 plongest (entry_addr - section->buffer));
927aa2e7
JK
3016 return;
3017 }
3018 ULONGEST start = extract_unsigned_integer (addr, address_size,
3019 dwarf5_byte_order);
3020 addr += address_size;
3021 ULONGEST length = extract_unsigned_integer (addr, address_size,
3022 dwarf5_byte_order);
3023 addr += address_size;
3024 if (start == 0 && length == 0)
3025 break;
976ca316 3026 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
3027 {
3028 /* Symbol was eliminated due to a COMDAT group. */
3029 continue;
3030 }
3031 ULONGEST end = start + length;
79748972
TT
3032 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3033 - baseaddr);
3034 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3035 - baseaddr);
927aa2e7
JK
3036 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3037 }
3038 }
3039
d320c2b5 3040 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3041 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3042}
3043
9291a0cd
TT
3044/* Find a slot in the mapped index INDEX for the object named NAME.
3045 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3046 constant pool and return true. If NAME cannot be found, return
3047 false. */
2fdf6df6 3048
109483d9 3049static bool
9291a0cd
TT
3050find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3051 offset_type **vec_out)
3052{
0cf03b49 3053 offset_type hash;
9291a0cd 3054 offset_type slot, step;
559a7a62 3055 int (*cmp) (const char *, const char *);
9291a0cd 3056
791afaa2 3057 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3058 if (current_language->la_language == language_cplus
45280282
IB
3059 || current_language->la_language == language_fortran
3060 || current_language->la_language == language_d)
0cf03b49
JK
3061 {
3062 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3063 not contain any. */
a8719064 3064
72998fb3 3065 if (strchr (name, '(') != NULL)
0cf03b49 3066 {
109483d9 3067 without_params = cp_remove_params (name);
0cf03b49 3068
72998fb3 3069 if (without_params != NULL)
791afaa2 3070 name = without_params.get ();
0cf03b49
JK
3071 }
3072 }
3073
559a7a62 3074 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3075 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3076 simulate our NAME being searched is also lowercased. */
3077 hash = mapped_index_string_hash ((index->version == 4
dda83cd7 3078 && case_sensitivity == case_sensitive_off
559a7a62
JK
3079 ? 5 : index->version),
3080 name);
3081
f00a2de2
PA
3082 slot = hash & (index->symbol_table.size () - 1);
3083 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3084 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3085
3086 for (;;)
3087 {
9291a0cd 3088 const char *str;
f00a2de2
PA
3089
3090 const auto &bucket = index->symbol_table[slot];
3091 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3092 return false;
9291a0cd 3093
f00a2de2 3094 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3095 if (!cmp (name, str))
9291a0cd
TT
3096 {
3097 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3098 + MAYBE_SWAP (bucket.vec));
109483d9 3099 return true;
9291a0cd
TT
3100 }
3101
f00a2de2 3102 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3103 }
3104}
3105
4485a1c1
SM
3106/* A helper function that reads the .gdb_index from BUFFER and fills
3107 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3108 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3109 ok to use deprecated sections.
3110
3111 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3112 out parameters that are filled in with information about the CU and
3113 TU lists in the section.
3114
4485a1c1 3115 Returns true if all went well, false otherwise. */
2fdf6df6 3116
d33bc52e 3117static bool
3810f182 3118read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
3119 bool deprecated_ok,
3120 gdb::array_view<const gdb_byte> buffer,
3121 struct mapped_index *map,
3122 const gdb_byte **cu_list,
3123 offset_type *cu_list_elements,
3124 const gdb_byte **types_list,
3125 offset_type *types_list_elements)
3126{
3127 const gdb_byte *addr = &buffer[0];
82430852 3128
9291a0cd 3129 /* Version check. */
4485a1c1 3130 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3131 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3132 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3133 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3134 indices. */
831adc1f 3135 if (version < 4)
481860b3
GB
3136 {
3137 static int warning_printed = 0;
3138 if (!warning_printed)
3139 {
3140 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3141 filename);
481860b3
GB
3142 warning_printed = 1;
3143 }
3144 return 0;
3145 }
3146 /* Index version 4 uses a different hash function than index version
3147 5 and later.
3148
3149 Versions earlier than 6 did not emit psymbols for inlined
3150 functions. Using these files will cause GDB not to be able to
3151 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3152 indices unless the user has done
3153 "set use-deprecated-index-sections on". */
2ec9a5e0 3154 if (version < 6 && !deprecated_ok)
481860b3
GB
3155 {
3156 static int warning_printed = 0;
3157 if (!warning_printed)
3158 {
e615022a
DE
3159 warning (_("\
3160Skipping deprecated .gdb_index section in %s.\n\
3161Do \"set use-deprecated-index-sections on\" before the file is read\n\
3162to use the section anyway."),
2ec9a5e0 3163 filename);
481860b3
GB
3164 warning_printed = 1;
3165 }
3166 return 0;
3167 }
796a7ff8 3168 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3169 of the TU (for symbols coming from TUs),
3170 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3171 Plus gold-generated indices can have duplicate entries for global symbols,
3172 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3173 These are just performance bugs, and we can't distinguish gdb-generated
3174 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3175
481860b3 3176 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3177 longer backward compatible. */
796a7ff8 3178 if (version > 8)
594e8718 3179 return 0;
9291a0cd 3180
559a7a62 3181 map->version = version;
9291a0cd 3182
4485a1c1 3183 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3184
4485a1c1 3185 int i = 0;
2ec9a5e0
TT
3186 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3187 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3188 / 8);
1fd400ff
TT
3189 ++i;
3190
2ec9a5e0
TT
3191 *types_list = addr + MAYBE_SWAP (metadata[i]);
3192 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3193 - MAYBE_SWAP (metadata[i]))
3194 / 8);
987d643c 3195 ++i;
1fd400ff 3196
f00a2de2
PA
3197 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3198 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3199 map->address_table
3200 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3201 ++i;
3202
f00a2de2
PA
3203 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3204 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3205 map->symbol_table
3206 = gdb::array_view<mapped_index::symbol_table_slot>
3207 ((mapped_index::symbol_table_slot *) symbol_table,
3208 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3209
f00a2de2 3210 ++i;
f9d83a0b 3211 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3212
2ec9a5e0
TT
3213 return 1;
3214}
3215
4485a1c1
SM
3216/* Callback types for dwarf2_read_gdb_index. */
3217
3218typedef gdb::function_view
5989a64e 3219 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3220 get_gdb_index_contents_ftype;
3221typedef gdb::function_view
3222 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3223 get_gdb_index_contents_dwz_ftype;
3224
927aa2e7 3225/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3226 elements of all the CUs and return 1. Otherwise, return 0. */
3227
3228static int
4485a1c1 3229dwarf2_read_gdb_index
976ca316 3230 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
3231 get_gdb_index_contents_ftype get_gdb_index_contents,
3232 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3233{
2ec9a5e0
TT
3234 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3235 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3236 struct dwz_file *dwz;
976ca316 3237 struct objfile *objfile = per_objfile->objfile;
f8c41851 3238 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 3239
4485a1c1 3240 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 3241 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
3242
3243 if (main_index_contents.empty ())
3244 return 0;
3245
3063847f 3246 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3247 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3248 use_deprecated_index_sections,
3249 main_index_contents, map.get (), &cu_list,
3250 &cu_list_elements, &types_list,
3251 &types_list_elements))
2ec9a5e0
TT
3252 return 0;
3253
0fefef59 3254 /* Don't use the index if it's empty. */
3063847f 3255 if (map->symbol_table.empty ())
0fefef59
DE
3256 return 0;
3257
2ec9a5e0
TT
3258 /* If there is a .dwz file, read it so we can get its CU list as
3259 well. */
f8c41851 3260 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 3261 if (dwz != NULL)
2ec9a5e0 3262 {
2ec9a5e0
TT
3263 struct mapped_index dwz_map;
3264 const gdb_byte *dwz_types_ignore;
3265 offset_type dwz_types_elements_ignore;
3266
4485a1c1
SM
3267 gdb::array_view<const gdb_byte> dwz_index_content
3268 = get_gdb_index_contents_dwz (objfile, dwz);
3269
3270 if (dwz_index_content.empty ())
3271 return 0;
3272
3810f182 3273 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3274 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3275 &dwz_list, &dwz_list_elements,
3276 &dwz_types_ignore,
3277 &dwz_types_elements_ignore))
2ec9a5e0
TT
3278 {
3279 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3280 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3281 return 0;
3282 }
3283 }
3284
f8c41851
SM
3285 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3286 dwz_list_elements);
1fd400ff 3287
8b70b953
TT
3288 if (types_list_elements)
3289 {
8b70b953
TT
3290 /* We can only handle a single .debug_types when we have an
3291 index. */
f8c41851 3292 if (per_bfd->types.size () != 1)
8b70b953
TT
3293 return 0;
3294
f8c41851 3295 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 3296
f8c41851 3297 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 3298 types_list_elements);
8b70b953 3299 }
9291a0cd 3300
976ca316 3301 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 3302
f8c41851
SM
3303 per_bfd->index_table = std::move (map);
3304 per_bfd->using_index = 1;
3305 per_bfd->quick_file_names_table =
3306 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3307
3308 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3309 objfiles using the same BFD. */
3310 gdb_assert (per_bfd->partial_symtabs == nullptr);
3311 per_bfd->partial_symtabs = objfile->partial_symtabs;
9291a0cd
TT
3312
3313 return 1;
3314}
3315
dee91e82 3316/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3317
dee91e82
DE
3318static void
3319dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3320 const gdb_byte *info_ptr,
3e225074 3321 struct die_info *comp_unit_die)
9291a0cd 3322{
dee91e82 3323 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3324 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 3325 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 3326 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3327 struct attribute *attr;
7b9f3c50
DE
3328 void **slot;
3329 struct quick_file_names *qfn;
9291a0cd 3330
0186c6a7
DE
3331 gdb_assert (! this_cu->is_debug_types);
3332
07261596
TT
3333 /* Our callers never want to match partial units -- instead they
3334 will match the enclosing full CU. */
3335 if (comp_unit_die->tag == DW_TAG_partial_unit)
3336 {
3337 this_cu->v.quick->no_file_data = 1;
3338 return;
3339 }
3340
0186c6a7 3341 lh_cu = this_cu;
7b9f3c50 3342 slot = NULL;
dee91e82 3343
fff8551c 3344 line_header_up lh;
9c541725 3345 sect_offset line_offset {};
fff8551c 3346
dee91e82 3347 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
d4df075e 3348 if (attr != nullptr && attr->form_is_unsigned ())
9291a0cd 3349 {
7b9f3c50
DE
3350 struct quick_file_names find_entry;
3351
d4df075e 3352 line_offset = (sect_offset) attr->as_unsigned ();
7b9f3c50
DE
3353
3354 /* We may have already read in this line header (TU line header sharing).
3355 If we have we're done. */
094b34ac 3356 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3357 find_entry.hash.line_sect_off = line_offset;
976ca316 3358 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3359 &find_entry, INSERT);
3360 if (*slot != NULL)
3361 {
9a3c8263 3362 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3363 return;
7b9f3c50
DE
3364 }
3365
3019eac3 3366 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3367 }
3368 if (lh == NULL)
3369 {
094b34ac 3370 lh_cu->v.quick->no_file_data = 1;
dee91e82 3371 return;
9291a0cd
TT
3372 }
3373
976ca316 3374 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3375 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3376 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3377 gdb_assert (slot != NULL);
3378 *slot = qfn;
9291a0cd 3379
d721ba37 3380 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3381
aa391654
TT
3382 int offset = 0;
3383 if (strcmp (fnd.name, "<unknown>") != 0)
3384 ++offset;
3385
7ba99d21 3386 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3387 qfn->file_names =
976ca316 3388 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 3389 qfn->num_file_names);
aa391654
TT
3390 if (offset != 0)
3391 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3392 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3393 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3394 fnd.comp_dir).release ();
7b9f3c50 3395 qfn->real_names = NULL;
9291a0cd 3396
094b34ac 3397 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3398}
3399
3400/* A helper for the "quick" functions which attempts to read the line
3401 table for THIS_CU. */
3402
3403static struct quick_file_names *
ab432490
SM
3404dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3405 dwarf2_per_objfile *per_objfile)
dee91e82 3406{
0186c6a7
DE
3407 /* This should never be called for TUs. */
3408 gdb_assert (! this_cu->is_debug_types);
3409 /* Nor type unit groups. */
197400e8 3410 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3411
dee91e82
DE
3412 if (this_cu->v.quick->file_names != NULL)
3413 return this_cu->v.quick->file_names;
3414 /* If we know there is no line data, no point in looking again. */
3415 if (this_cu->v.quick->no_file_data)
3416 return NULL;
3417
ab432490 3418 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3419 if (!reader.dummy_p)
3e225074 3420 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3421
3422 if (this_cu->v.quick->no_file_data)
3423 return NULL;
3424 return this_cu->v.quick->file_names;
9291a0cd
TT
3425}
3426
3427/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3428 real path for a given file name from the line table. */
2fdf6df6 3429
9291a0cd 3430static const char *
976ca316 3431dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3432 struct quick_file_names *qfn, int index)
9291a0cd 3433{
7b9f3c50 3434 if (qfn->real_names == NULL)
976ca316 3435 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3436 qfn->num_file_names, const char *);
9291a0cd 3437
7b9f3c50 3438 if (qfn->real_names[index] == NULL)
14278e1f 3439 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3440
7b9f3c50 3441 return qfn->real_names[index];
9291a0cd
TT
3442}
3443
3444static struct symtab *
3445dw2_find_last_source_symtab (struct objfile *objfile)
3446{
976ca316
SM
3447 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3448 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3449 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3450
43f3e411
DE
3451 if (cust == NULL)
3452 return NULL;
ed2dc618 3453
43f3e411 3454 return compunit_primary_filetab (cust);
9291a0cd
TT
3455}
3456
7b9f3c50
DE
3457/* Traversal function for dw2_forget_cached_source_info. */
3458
3459static int
3460dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3461{
7b9f3c50 3462 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3463
7b9f3c50 3464 if (file_data->real_names)
9291a0cd 3465 {
7b9f3c50 3466 int i;
9291a0cd 3467
7b9f3c50 3468 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3469 {
7b9f3c50
DE
3470 xfree ((void*) file_data->real_names[i]);
3471 file_data->real_names[i] = NULL;
9291a0cd
TT
3472 }
3473 }
7b9f3c50
DE
3474
3475 return 1;
3476}
3477
3478static void
3479dw2_forget_cached_source_info (struct objfile *objfile)
3480{
976ca316 3481 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3482
976ca316 3483 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3484 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3485}
3486
f8eba3c6
TT
3487/* Helper function for dw2_map_symtabs_matching_filename that expands
3488 the symtabs and calls the iterator. */
3489
3490static int
3491dw2_map_expand_apply (struct objfile *objfile,
3492 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3493 const char *name, const char *real_path,
14bc53a8 3494 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3495{
43f3e411 3496 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3497
3498 /* Don't visit already-expanded CUs. */
af758d11
SM
3499 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3500 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3501 return 0;
3502
3503 /* This may expand more than one symtab, and we want to iterate over
3504 all of them. */
97a1449a 3505 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3506
14bc53a8
PA
3507 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3508 last_made, callback);
f8eba3c6
TT
3509}
3510
3511/* Implementation of the map_symtabs_matching_filename method. */
3512
14bc53a8
PA
3513static bool
3514dw2_map_symtabs_matching_filename
3515 (struct objfile *objfile, const char *name, const char *real_path,
3516 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3517{
c011a4f4 3518 const char *name_basename = lbasename (name);
976ca316 3519 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 3520
848e3e78
DE
3521 /* The rule is CUs specify all the files, including those used by
3522 any TU, so there's no need to scan TUs here. */
f4dc4d17 3523
976ca316 3524 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3525 {
3d7bb9d9 3526 /* We only need to look at symtabs not already expanded. */
976ca316 3527 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3528 continue;
3529
976ca316 3530 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3531 if (file_data == NULL)
9291a0cd
TT
3532 continue;
3533
b76e467d 3534 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3535 {
7b9f3c50 3536 const char *this_name = file_data->file_names[j];
da235a7c 3537 const char *this_real_name;
9291a0cd 3538
af529f8f 3539 if (compare_filenames_for_search (this_name, name))
9291a0cd 3540 {
f5b95b50 3541 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3542 callback))
3543 return true;
288e77a7 3544 continue;
4aac40c8 3545 }
9291a0cd 3546
c011a4f4
DE
3547 /* Before we invoke realpath, which can get expensive when many
3548 files are involved, do a quick comparison of the basenames. */
3549 if (! basenames_may_differ
3550 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3551 continue;
3552
976ca316 3553 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
da235a7c 3554 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3555 {
da235a7c 3556 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3557 callback))
3558 return true;
288e77a7 3559 continue;
da235a7c 3560 }
9291a0cd 3561
da235a7c
JK
3562 if (real_path != NULL)
3563 {
af529f8f
JK
3564 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3565 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3566 if (this_real_name != NULL
af529f8f 3567 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3568 {
f5b95b50 3569 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3570 callback))
3571 return true;
288e77a7 3572 continue;
9291a0cd
TT
3573 }
3574 }
3575 }
3576 }
3577
14bc53a8 3578 return false;
9291a0cd
TT
3579}
3580
da51c347
DE
3581/* Struct used to manage iterating over all CUs looking for a symbol. */
3582
3583struct dw2_symtab_iterator
9291a0cd 3584{
ed2dc618 3585 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3586 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3587 /* If set, only look for symbols that match that block. Valid values are
3588 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3589 gdb::optional<block_enum> block_index;
da51c347
DE
3590 /* The kind of symbol we're looking for. */
3591 domain_enum domain;
3592 /* The list of CUs from the index entry of the symbol,
3593 or NULL if not found. */
3594 offset_type *vec;
3595 /* The next element in VEC to look at. */
3596 int next;
3597 /* The number of elements in VEC, or zero if there is no match. */
3598 int length;
8943b874
DE
3599 /* Have we seen a global version of the symbol?
3600 If so we can ignore all further global instances.
3601 This is to work around gold/15646, inefficient gold-generated
3602 indices. */
3603 int global_seen;
da51c347 3604};
9291a0cd 3605
e5f3ece2 3606/* Initialize the index symtab iterator ITER, common part. */
2fdf6df6 3607
9291a0cd 3608static void
e5f3ece2
TV
3609dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3610 dwarf2_per_objfile *per_objfile,
3611 gdb::optional<block_enum> block_index,
3612 domain_enum domain)
da51c347 3613{
976ca316 3614 iter->per_objfile = per_objfile;
da51c347
DE
3615 iter->block_index = block_index;
3616 iter->domain = domain;
3617 iter->next = 0;
8943b874 3618 iter->global_seen = 0;
e5f3ece2
TV
3619 iter->vec = NULL;
3620 iter->length = 0;
3621}
da51c347 3622
e5f3ece2
TV
3623/* Initialize the index symtab iterator ITER, const char *NAME variant. */
3624
3625static void
3626dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3627 dwarf2_per_objfile *per_objfile,
3628 gdb::optional<block_enum> block_index,
3629 domain_enum domain,
3630 const char *name)
3631{
3632 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
ed2dc618 3633
e5f3ece2 3634 mapped_index *index = per_objfile->per_bfd->index_table.get ();
ed2dc618 3635 /* index is NULL if OBJF_READNOW. */
e5f3ece2
TV
3636 if (index == NULL)
3637 return;
3638
3639 if (find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347 3640 iter->length = MAYBE_SWAP (*iter->vec);
e5f3ece2
TV
3641}
3642
3643/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3644
3645static void
3646dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3647 dwarf2_per_objfile *per_objfile,
3648 gdb::optional<block_enum> block_index,
3649 domain_enum domain, offset_type namei)
3650{
3651 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3652
3653 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3654 /* index is NULL if OBJF_READNOW. */
3655 if (index == NULL)
3656 return;
3657
3658 gdb_assert (!index->symbol_name_slot_invalid (namei));
3659 const auto &bucket = index->symbol_table[namei];
3660
3661 iter->vec = (offset_type *) (index->constant_pool
3662 + MAYBE_SWAP (bucket.vec));
3663 iter->length = MAYBE_SWAP (*iter->vec);
da51c347
DE
3664}
3665
3666/* Return the next matching CU or NULL if there are no more. */
3667
3668static struct dwarf2_per_cu_data *
3669dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3670{
976ca316 3671 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3672
da51c347
DE
3673 for ( ; iter->next < iter->length; ++iter->next)
3674 {
3675 offset_type cu_index_and_attrs =
3676 MAYBE_SWAP (iter->vec[iter->next + 1]);
3677 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3678 gdb_index_symbol_kind symbol_kind =
3679 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3680 /* Only check the symbol attributes if they're present.
3681 Indices prior to version 7 don't record them,
3682 and indices >= 7 may elide them for certain symbols
3683 (gold does this). */
3684 int attrs_valid =
976ca316 3685 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3686 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3687
3190f0c6 3688 /* Don't crash on bad data. */
976ca316
SM
3689 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3690 + per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3691 {
b98664d3 3692 complaint (_(".gdb_index entry has bad CU index"
976ca316 3693 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3694 continue;
3695 }
3696
976ca316 3697 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3698
da51c347 3699 /* Skip if already read in. */
976ca316 3700 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3701 continue;
3702
8943b874
DE
3703 /* Check static vs global. */
3704 if (attrs_valid)
3705 {
2b79f376
SM
3706 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3707
3708 if (iter->block_index.has_value ())
3709 {
3710 bool want_static = *iter->block_index == STATIC_BLOCK;
3711
3712 if (is_static != want_static)
3713 continue;
3714 }
3715
8943b874 3716 /* Work around gold/15646. */
f030440d
TV
3717 if (!is_static
3718 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3719 {
3720 if (iter->global_seen)
3721 continue;
3722
3723 iter->global_seen = 1;
3724 }
8943b874 3725 }
da51c347
DE
3726
3727 /* Only check the symbol's kind if it has one. */
3728 if (attrs_valid)
3729 {
3730 switch (iter->domain)
3731 {
3732 case VAR_DOMAIN:
3733 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3734 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3735 /* Some types are also in VAR_DOMAIN. */
3736 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3737 continue;
3738 break;
3739 case STRUCT_DOMAIN:
3740 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3741 continue;
3742 break;
3743 case LABEL_DOMAIN:
3744 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3745 continue;
3746 break;
59c35742
AB
3747 case MODULE_DOMAIN:
3748 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3749 continue;
3750 break;
da51c347
DE
3751 default:
3752 break;
3753 }
3754 }
3755
3756 ++iter->next;
3757 return per_cu;
3758 }
3759
3760 return NULL;
3761}
3762
43f3e411 3763static struct compunit_symtab *
c7f839cb 3764dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3765 const char *name, domain_enum domain)
9291a0cd 3766{
43f3e411 3767 struct compunit_symtab *stab_best = NULL;
976ca316 3768 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 3769
b5ec771e
PA
3770 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3771
ed2dc618
SM
3772 struct dw2_symtab_iterator iter;
3773 struct dwarf2_per_cu_data *per_cu;
da51c347 3774
976ca316 3775 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
9291a0cd 3776
ed2dc618
SM
3777 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3778 {
3779 struct symbol *sym, *with_opaque = NULL;
97a1449a 3780 struct compunit_symtab *stab
976ca316 3781 = dw2_instantiate_symtab (per_cu, per_objfile, false);
ed2dc618 3782 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3783 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3784
ed2dc618
SM
3785 sym = block_find_symbol (block, name, domain,
3786 block_find_non_opaque_type_preferred,
3787 &with_opaque);
b2e2f908 3788
ed2dc618
SM
3789 /* Some caution must be observed with overloaded functions
3790 and methods, since the index will not contain any overload
3791 information (but NAME might contain it). */
da51c347 3792
ed2dc618
SM
3793 if (sym != NULL
3794 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3795 return stab;
3796 if (with_opaque != NULL
3797 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3798 stab_best = stab;
da51c347 3799
ed2dc618 3800 /* Keep looking through other CUs. */
9291a0cd 3801 }
9291a0cd 3802
da51c347 3803 return stab_best;
9291a0cd
TT
3804}
3805
3806static void
3807dw2_print_stats (struct objfile *objfile)
3808{
976ca316
SM
3809 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3810 int total = (per_objfile->per_bfd->all_comp_units.size ()
3811 + per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3812 int count = 0;
9291a0cd 3813
ed2dc618 3814 for (int i = 0; i < total; ++i)
9291a0cd 3815 {
976ca316 3816 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3817
976ca316 3818 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3819 ++count;
3820 }
e4a48d9d 3821 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3822 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3823}
3824
779bd270
DE
3825/* This dumps minimal information about the index.
3826 It is called via "mt print objfiles".
3827 One use is to verify .gdb_index has been loaded by the
3828 gdb.dwarf2/gdb-index.exp testcase. */
3829
9291a0cd
TT
3830static void
3831dw2_dump (struct objfile *objfile)
3832{
976ca316 3833 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3834
976ca316 3835 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3836 printf_filtered (".gdb_index:");
976ca316 3837 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3838 {
3839 printf_filtered (" version %d\n",
976ca316 3840 per_objfile->per_bfd->index_table->version);
779bd270
DE
3841 }
3842 else
3843 printf_filtered (" faked for \"readnow\"\n");
3844 printf_filtered ("\n");
9291a0cd
TT
3845}
3846
9291a0cd
TT
3847static void
3848dw2_expand_symtabs_for_function (struct objfile *objfile,
3849 const char *func_name)
3850{
976ca316 3851 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
da51c347 3852
ed2dc618
SM
3853 struct dw2_symtab_iterator iter;
3854 struct dwarf2_per_cu_data *per_cu;
da51c347 3855
976ca316 3856 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3857
ed2dc618 3858 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3859 dw2_instantiate_symtab (per_cu, per_objfile, false);
da51c347 3860
9291a0cd
TT
3861}
3862
3863static void
3864dw2_expand_all_symtabs (struct objfile *objfile)
3865{
976ca316
SM
3866 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3867 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3868 + per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3869
ed2dc618 3870 for (int i = 0; i < total_units; ++i)
9291a0cd 3871 {
976ca316 3872 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3873
58f0c718
TT
3874 /* We don't want to directly expand a partial CU, because if we
3875 read it with the wrong language, then assertion failures can
3876 be triggered later on. See PR symtab/23010. So, tell
3877 dw2_instantiate_symtab to skip partial CUs -- any important
3878 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3879 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3880 }
3881}
3882
3883static void
652a8996
JK
3884dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3885 const char *fullname)
9291a0cd 3886{
976ca316 3887 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3888
3889 /* We don't need to consider type units here.
3890 This is only called for examining code, e.g. expand_line_sal.
3891 There can be an order of magnitude (or more) more type units
3892 than comp units, and we avoid them if we can. */
3893
976ca316 3894 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3895 {
3d7bb9d9 3896 /* We only need to look at symtabs not already expanded. */
976ca316 3897 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3898 continue;
3899
976ca316 3900 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3901 if (file_data == NULL)
9291a0cd
TT
3902 continue;
3903
b76e467d 3904 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3905 {
652a8996
JK
3906 const char *this_fullname = file_data->file_names[j];
3907
3908 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3909 {
976ca316 3910 dw2_instantiate_symtab (per_cu, per_objfile, false);
9291a0cd
TT
3911 break;
3912 }
3913 }
3914 }
3915}
3916
9a0bacfb
TV
3917static void
3918dw2_expand_symtabs_matching_symbol
3919 (mapped_index_base &index,
3920 const lookup_name_info &lookup_name_in,
3921 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3922 enum search_domain kind,
fcf23d5b
SM
3923 gdb::function_view<bool (offset_type)> match_callback,
3924 dwarf2_per_objfile *per_objfile);
9a0bacfb
TV
3925
3926static void
3927dw2_expand_symtabs_matching_one
97a1449a
SM
3928 (dwarf2_per_cu_data *per_cu,
3929 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3930 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3931 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3932
9291a0cd 3933static void
199b4314
TT
3934dw2_map_matching_symbols
3935 (struct objfile *objfile,
b054970d 3936 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3937 int global,
3938 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3939 symbol_compare_ftype *ordered_compare)
9291a0cd 3940{
1aa98955 3941 /* Used for Ada. */
976ca316 3942 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3943
9a0bacfb
TV
3944 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3945
976ca316 3946 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3947 {
976ca316 3948 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3949
9a0bacfb
TV
3950 const char *match_name = name.ada ().lookup_name ().c_str ();
3951 auto matcher = [&] (const char *symname)
3952 {
3953 if (ordered_compare == nullptr)
3954 return true;
3955 return ordered_compare (symname, match_name) == 0;
3956 };
3957
3958 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3959 [&] (offset_type namei)
3960 {
3961 struct dw2_symtab_iterator iter;
3962 struct dwarf2_per_cu_data *per_cu;
3963
976ca316 3964 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3965 namei);
9a0bacfb 3966 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3967 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3968 nullptr);
9a0bacfb 3969 return true;
976ca316 3970 }, per_objfile);
9a0bacfb
TV
3971 }
3972 else
3973 {
3974 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3975 proceed assuming all symtabs have been read in. */
3976 }
1aa98955
TV
3977
3978 for (compunit_symtab *cust : objfile->compunits ())
3979 {
3980 const struct block *block;
3981
3982 if (cust == NULL)
3983 continue;
3984 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3985 if (!iterate_over_symbols_terminated (block, name,
3986 domain, callback))
3987 return;
3988 }
9291a0cd
TT
3989}
3990
e1ef7d7a
PA
3991/* Starting from a search name, return the string that finds the upper
3992 bound of all strings that start with SEARCH_NAME in a sorted name
3993 list. Returns the empty string to indicate that the upper bound is
3994 the end of the list. */
3995
3996static std::string
3997make_sort_after_prefix_name (const char *search_name)
3998{
3999 /* When looking to complete "func", we find the upper bound of all
4000 symbols that start with "func" by looking for where we'd insert
4001 the closest string that would follow "func" in lexicographical
4002 order. Usually, that's "func"-with-last-character-incremented,
4003 i.e. "fund". Mind non-ASCII characters, though. Usually those
4004 will be UTF-8 multi-byte sequences, but we can't be certain.
4005 Especially mind the 0xff character, which is a valid character in
4006 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4007 rule out compilers allowing it in identifiers. Note that
4008 conveniently, strcmp/strcasecmp are specified to compare
4009 characters interpreted as unsigned char. So what we do is treat
4010 the whole string as a base 256 number composed of a sequence of
4011 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4012 to 0, and carries 1 to the following more-significant position.
4013 If the very first character in SEARCH_NAME ends up incremented
4014 and carries/overflows, then the upper bound is the end of the
4015 list. The string after the empty string is also the empty
4016 string.
4017
4018 Some examples of this operation:
4019
4020 SEARCH_NAME => "+1" RESULT
4021
4022 "abc" => "abd"
4023 "ab\xff" => "ac"
4024 "\xff" "a" "\xff" => "\xff" "b"
4025 "\xff" => ""
4026 "\xff\xff" => ""
4027 "" => ""
4028
4029 Then, with these symbols for example:
4030
4031 func
4032 func1
4033 fund
4034
4035 completing "func" looks for symbols between "func" and
4036 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4037 which finds "func" and "func1", but not "fund".
4038
4039 And with:
4040
4041 funcÿ (Latin1 'ÿ' [0xff])
4042 funcÿ1
4043 fund
4044
4045 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4046 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4047
4048 And with:
4049
4050 ÿÿ (Latin1 'ÿ' [0xff])
4051 ÿÿ1
4052
4053 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4054 the end of the list.
4055 */
4056 std::string after = search_name;
4057 while (!after.empty () && (unsigned char) after.back () == 0xff)
4058 after.pop_back ();
4059 if (!after.empty ())
4060 after.back () = (unsigned char) after.back () + 1;
4061 return after;
4062}
4063
5c58de74 4064/* See declaration. */
61d96d7e 4065
5c58de74
PA
4066std::pair<std::vector<name_component>::const_iterator,
4067 std::vector<name_component>::const_iterator>
44ed8f3e 4068mapped_index_base::find_name_components_bounds
fcf23d5b
SM
4069 (const lookup_name_info &lookup_name_without_params, language lang,
4070 dwarf2_per_objfile *per_objfile) const
3f563c84 4071{
5c58de74
PA
4072 auto *name_cmp
4073 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 4074
3b00ef10 4075 const char *lang_name
e0802d59 4076 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 4077
3f563c84
PA
4078 /* Comparison function object for lower_bound that matches against a
4079 given symbol name. */
4080 auto lookup_compare_lower = [&] (const name_component &elem,
4081 const char *name)
4082 {
fcf23d5b 4083 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
4084 const char *elem_name = elem_qualified + elem.name_offset;
4085 return name_cmp (elem_name, name) < 0;
4086 };
4087
4088 /* Comparison function object for upper_bound that matches against a
4089 given symbol name. */
4090 auto lookup_compare_upper = [&] (const char *name,
4091 const name_component &elem)
4092 {
fcf23d5b 4093 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
4094 const char *elem_name = elem_qualified + elem.name_offset;
4095 return name_cmp (name, elem_name) < 0;
4096 };
4097
5c58de74
PA
4098 auto begin = this->name_components.begin ();
4099 auto end = this->name_components.end ();
3f563c84
PA
4100
4101 /* Find the lower bound. */
4102 auto lower = [&] ()
4103 {
3b00ef10 4104 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4105 return begin;
4106 else
3b00ef10 4107 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4108 } ();
4109
4110 /* Find the upper bound. */
4111 auto upper = [&] ()
4112 {
5c58de74 4113 if (lookup_name_without_params.completion_mode ())
3f563c84 4114 {
e1ef7d7a
PA
4115 /* In completion mode, we want UPPER to point past all
4116 symbols names that have the same prefix. I.e., with
4117 these symbols, and completing "func":
4118
4119 function << lower bound
4120 function1
4121 other_function << upper bound
4122
4123 We find the upper bound by looking for the insertion
4124 point of "func"-with-last-character-incremented,
4125 i.e. "fund". */
3b00ef10 4126 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4127 if (after.empty ())
3f563c84 4128 return end;
e6b2f5ef
PA
4129 return std::lower_bound (lower, end, after.c_str (),
4130 lookup_compare_lower);
3f563c84
PA
4131 }
4132 else
3b00ef10 4133 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4134 } ();
4135
5c58de74
PA
4136 return {lower, upper};
4137}
4138
4139/* See declaration. */
4140
4141void
fcf23d5b 4142mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
4143{
4144 if (!this->name_components.empty ())
4145 return;
4146
4147 this->name_components_casing = case_sensitivity;
4148 auto *name_cmp
4149 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4150
4151 /* The code below only knows how to break apart components of C++
4152 symbol names (and other languages that use '::' as
3b00ef10 4153 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4154 auto count = this->symbol_name_count ();
4155 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4156 {
44ed8f3e 4157 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4158 continue;
4159
fcf23d5b 4160 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
4161
4162 /* Add each name component to the name component table. */
4163 unsigned int previous_len = 0;
3b00ef10
TT
4164
4165 if (strstr (name, "::") != nullptr)
4166 {
4167 for (unsigned int current_len = cp_find_first_component (name);
4168 name[current_len] != '\0';
4169 current_len += cp_find_first_component (name + current_len))
4170 {
4171 gdb_assert (name[current_len] == ':');
4172 this->name_components.push_back ({previous_len, idx});
4173 /* Skip the '::'. */
4174 current_len += 2;
4175 previous_len = current_len;
4176 }
4177 }
4178 else
5c58de74 4179 {
3b00ef10
TT
4180 /* Handle the Ada encoded (aka mangled) form here. */
4181 for (const char *iter = strstr (name, "__");
4182 iter != nullptr;
4183 iter = strstr (iter, "__"))
4184 {
4185 this->name_components.push_back ({previous_len, idx});
4186 iter += 2;
4187 previous_len = iter - name;
4188 }
5c58de74 4189 }
3b00ef10 4190
5c58de74
PA
4191 this->name_components.push_back ({previous_len, idx});
4192 }
4193
4194 /* Sort name_components elements by name. */
4195 auto name_comp_compare = [&] (const name_component &left,
4196 const name_component &right)
4197 {
fcf23d5b
SM
4198 const char *left_qualified
4199 = this->symbol_name_at (left.idx, per_objfile);
4200 const char *right_qualified
4201 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
4202
4203 const char *left_name = left_qualified + left.name_offset;
4204 const char *right_name = right_qualified + right.name_offset;
4205
4206 return name_cmp (left_name, right_name) < 0;
4207 };
4208
4209 std::sort (this->name_components.begin (),
4210 this->name_components.end (),
4211 name_comp_compare);
4212}
4213
4214/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4215 mapped_index_base instead of the containing objfile. This is split
4216 to a separate function in order to be able to unit test the
4217 name_components matching using a mock mapped_index_base. For each
5c58de74 4218 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4219 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4220
4221static void
4222dw2_expand_symtabs_matching_symbol
44ed8f3e 4223 (mapped_index_base &index,
5c58de74
PA
4224 const lookup_name_info &lookup_name_in,
4225 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4226 enum search_domain kind,
fcf23d5b
SM
4227 gdb::function_view<bool (offset_type)> match_callback,
4228 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4229{
4230 lookup_name_info lookup_name_without_params
4231 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4232
4233 /* Build the symbol name component sorted vector, if we haven't
4234 yet. */
fcf23d5b 4235 index.build_name_components (per_objfile);
5c58de74 4236
3f563c84
PA
4237 /* The same symbol may appear more than once in the range though.
4238 E.g., if we're looking for symbols that complete "w", and we have
4239 a symbol named "w1::w2", we'll find the two name components for
4240 that same symbol in the range. To be sure we only call the
4241 callback once per symbol, we first collect the symbol name
4242 indexes that matched in a temporary vector and ignore
4243 duplicates. */
4244 std::vector<offset_type> matches;
3f563c84 4245
3b00ef10
TT
4246 struct name_and_matcher
4247 {
4248 symbol_name_matcher_ftype *matcher;
ecc6c606 4249 const char *name;
3b00ef10
TT
4250
4251 bool operator== (const name_and_matcher &other) const
3f563c84 4252 {
ecc6c606 4253 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4254 }
4255 };
4256
4257 /* A vector holding all the different symbol name matchers, for all
4258 languages. */
4259 std::vector<name_and_matcher> matchers;
4260
4261 for (int i = 0; i < nr_languages; i++)
4262 {
4263 enum language lang_e = (enum language) i;
4264
4265 const language_defn *lang = language_def (lang_e);
4266 symbol_name_matcher_ftype *name_matcher
c9debfb9 4267 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 4268
3b00ef10 4269 name_and_matcher key {
dda83cd7 4270 name_matcher,
3b00ef10
TT
4271 lookup_name_without_params.language_lookup_name (lang_e)
4272 };
4273
4274 /* Don't insert the same comparison routine more than once.
4275 Note that we do this linear walk. This is not a problem in
4276 practice because the number of supported languages is
4277 low. */
4278 if (std::find (matchers.begin (), matchers.end (), key)
4279 != matchers.end ())
9291a0cd 4280 continue;
3b00ef10
TT
4281 matchers.push_back (std::move (key));
4282
4283 auto bounds
4284 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 4285 lang_e, per_objfile);
3b00ef10
TT
4286
4287 /* Now for each symbol name in range, check to see if we have a name
4288 match, and if so, call the MATCH_CALLBACK callback. */
4289
4290 for (; bounds.first != bounds.second; ++bounds.first)
4291 {
fcf23d5b
SM
4292 const char *qualified
4293 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
4294
4295 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4296 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4297 continue;
9291a0cd 4298
3b00ef10
TT
4299 matches.push_back (bounds.first->idx);
4300 }
3f563c84
PA
4301 }
4302
4303 std::sort (matches.begin (), matches.end ());
4304
4305 /* Finally call the callback, once per match. */
4306 ULONGEST prev = -1;
4307 for (offset_type idx : matches)
4308 {
4309 if (prev != idx)
4310 {
3b00ef10
TT
4311 if (!match_callback (idx))
4312 break;
3f563c84
PA
4313 prev = idx;
4314 }
4315 }
4316
4317 /* Above we use a type wider than idx's for 'prev', since 0 and
4318 (offset_type)-1 are both possible values. */
4319 static_assert (sizeof (prev) > sizeof (offset_type), "");
4320}
4321
c62446b1
PA
4322#if GDB_SELF_TEST
4323
4324namespace selftests { namespace dw2_expand_symtabs_matching {
4325
a3c5fafd
PA
4326/* A mock .gdb_index/.debug_names-like name index table, enough to
4327 exercise dw2_expand_symtabs_matching_symbol, which works with the
4328 mapped_index_base interface. Builds an index from the symbol list
4329 passed as parameter to the constructor. */
4330class mock_mapped_index : public mapped_index_base
c62446b1
PA
4331{
4332public:
a3c5fafd
PA
4333 mock_mapped_index (gdb::array_view<const char *> symbols)
4334 : m_symbol_table (symbols)
c62446b1
PA
4335 {}
4336
a3c5fafd 4337 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4338
a3c5fafd 4339 /* Return the number of names in the symbol table. */
632e107b 4340 size_t symbol_name_count () const override
c62446b1 4341 {
a3c5fafd 4342 return m_symbol_table.size ();
c62446b1
PA
4343 }
4344
a3c5fafd 4345 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
4346 const char *symbol_name_at
4347 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
4348 {
4349 return m_symbol_table[idx];
4350 }
c62446b1 4351
a3c5fafd
PA
4352private:
4353 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4354};
4355
4356/* Convenience function that converts a NULL pointer to a "<null>"
4357 string, to pass to print routines. */
4358
4359static const char *
4360string_or_null (const char *str)
4361{
4362 return str != NULL ? str : "<null>";
4363}
4364
4365/* Check if a lookup_name_info built from
4366 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4367 index. EXPECTED_LIST is the list of expected matches, in expected
4368 matching order. If no match expected, then an empty list is
4369 specified. Returns true on success. On failure prints a warning
4370 indicating the file:line that failed, and returns false. */
4371
4372static bool
4373check_match (const char *file, int line,
4374 mock_mapped_index &mock_index,
4375 const char *name, symbol_name_match_type match_type,
4376 bool completion_mode,
fcf23d5b
SM
4377 std::initializer_list<const char *> expected_list,
4378 dwarf2_per_objfile *per_objfile)
c62446b1
PA
4379{
4380 lookup_name_info lookup_name (name, match_type, completion_mode);
4381
4382 bool matched = true;
4383
4384 auto mismatch = [&] (const char *expected_str,
4385 const char *got)
4386 {
4387 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4388 "expected=\"%s\", got=\"%s\"\n"),
4389 file, line,
4390 (match_type == symbol_name_match_type::FULL
4391 ? "FULL" : "WILD"),
4392 name, string_or_null (expected_str), string_or_null (got));
4393 matched = false;
4394 };
4395
4396 auto expected_it = expected_list.begin ();
4397 auto expected_end = expected_list.end ();
4398
a3c5fafd 4399 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4400 NULL, ALL_DOMAIN,
4401 [&] (offset_type idx)
4402 {
fcf23d5b 4403 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
4404 const char *expected_str
4405 = expected_it == expected_end ? NULL : *expected_it++;
4406
4407 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4408 mismatch (expected_str, matched_name);
3b00ef10 4409 return true;
fcf23d5b 4410 }, per_objfile);
c62446b1
PA
4411
4412 const char *expected_str
4413 = expected_it == expected_end ? NULL : *expected_it++;
4414 if (expected_str != NULL)
4415 mismatch (expected_str, NULL);
4416
4417 return matched;
4418}
4419
4420/* The symbols added to the mock mapped_index for testing (in
4421 canonical form). */
4422static const char *test_symbols[] = {
4423 "function",
4424 "std::bar",
4425 "std::zfunction",
4426 "std::zfunction2",
4427 "w1::w2",
4428 "ns::foo<char*>",
4429 "ns::foo<int>",
4430 "ns::foo<long>",
a20714ff
PA
4431 "ns2::tmpl<int>::foo2",
4432 "(anonymous namespace)::A::B::C",
c62446b1 4433
e1ef7d7a
PA
4434 /* These are used to check that the increment-last-char in the
4435 matching algorithm for completion doesn't match "t1_fund" when
4436 completing "t1_func". */
4437 "t1_func",
4438 "t1_func1",
4439 "t1_fund",
4440 "t1_fund1",
4441
4442 /* A UTF-8 name with multi-byte sequences to make sure that
4443 cp-name-parser understands this as a single identifier ("função"
4444 is "function" in PT). */
4445 u8"u8função",
4446
4447 /* \377 (0xff) is Latin1 'ÿ'. */
4448 "yfunc\377",
4449
4450 /* \377 (0xff) is Latin1 'ÿ'. */
4451 "\377",
4452 "\377\377123",
4453
c62446b1
PA
4454 /* A name with all sorts of complications. Starts with "z" to make
4455 it easier for the completion tests below. */
4456#define Z_SYM_NAME \
4457 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4458 "::tuple<(anonymous namespace)::ui*, " \
4459 "std::default_delete<(anonymous namespace)::ui>, void>"
4460
4461 Z_SYM_NAME
4462};
4463
a3c5fafd
PA
4464/* Returns true if the mapped_index_base::find_name_component_bounds
4465 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4466 in completion mode. */
5c58de74
PA
4467
4468static bool
a3c5fafd 4469check_find_bounds_finds (mapped_index_base &index,
5c58de74 4470 const char *search_name,
fcf23d5b
SM
4471 gdb::array_view<const char *> expected_syms,
4472 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4473{
4474 lookup_name_info lookup_name (search_name,
4475 symbol_name_match_type::FULL, true);
4476
3b00ef10 4477 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4478 language_cplus,
4479 per_objfile);
5c58de74
PA
4480
4481 size_t distance = std::distance (bounds.first, bounds.second);
4482 if (distance != expected_syms.size ())
4483 return false;
4484
4485 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4486 {
4487 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4488 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4489 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4490 return false;
4491 }
4492
4493 return true;
4494}
4495
4496/* Test the lower-level mapped_index::find_name_component_bounds
4497 method. */
4498
c62446b1 4499static void
5c58de74
PA
4500test_mapped_index_find_name_component_bounds ()
4501{
4502 mock_mapped_index mock_index (test_symbols);
4503
fcf23d5b 4504 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4505
4506 /* Test the lower-level mapped_index::find_name_component_bounds
4507 method in completion mode. */
4508 {
4509 static const char *expected_syms[] = {
4510 "t1_func",
4511 "t1_func1",
5c58de74
PA
4512 };
4513
fcf23d5b
SM
4514 SELF_CHECK (check_find_bounds_finds
4515 (mock_index, "t1_func", expected_syms,
4516 NULL /* per_objfile */));
5c58de74
PA
4517 }
4518
4519 /* Check that the increment-last-char in the name matching algorithm
4520 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4521 {
4522 static const char *expected_syms1[] = {
4523 "\377",
4524 "\377\377123",
4525 };
fcf23d5b
SM
4526 SELF_CHECK (check_find_bounds_finds
4527 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4528
4529 static const char *expected_syms2[] = {
4530 "\377\377123",
4531 };
fcf23d5b
SM
4532 SELF_CHECK (check_find_bounds_finds
4533 (mock_index, "\377\377", expected_syms2,
4534 NULL /* per_objfile */));
5c58de74
PA
4535 }
4536}
4537
4538/* Test dw2_expand_symtabs_matching_symbol. */
4539
4540static void
4541test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4542{
4543 mock_mapped_index mock_index (test_symbols);
4544
4545 /* We let all tests run until the end even if some fails, for debug
4546 convenience. */
4547 bool any_mismatch = false;
4548
4549 /* Create the expected symbols list (an initializer_list). Needed
4550 because lists have commas, and we need to pass them to CHECK,
4551 which is a macro. */
4552#define EXPECT(...) { __VA_ARGS__ }
4553
4554 /* Wrapper for check_match that passes down the current
4555 __FILE__/__LINE__. */
4556#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4557 any_mismatch |= !check_match (__FILE__, __LINE__, \
4558 mock_index, \
4559 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4560 EXPECTED_LIST, NULL)
c62446b1
PA
4561
4562 /* Identity checks. */
4563 for (const char *sym : test_symbols)
4564 {
4565 /* Should be able to match all existing symbols. */
4566 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4567 EXPECT (sym));
4568
4569 /* Should be able to match all existing symbols with
4570 parameters. */
4571 std::string with_params = std::string (sym) + "(int)";
4572 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4573 EXPECT (sym));
4574
4575 /* Should be able to match all existing symbols with
4576 parameters and qualifiers. */
4577 with_params = std::string (sym) + " ( int ) const";
4578 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4579 EXPECT (sym));
4580
4581 /* This should really find sym, but cp-name-parser.y doesn't
4582 know about lvalue/rvalue qualifiers yet. */
4583 with_params = std::string (sym) + " ( int ) &&";
4584 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4585 {});
4586 }
4587
e1ef7d7a
PA
4588 /* Check that the name matching algorithm for completion doesn't get
4589 confused with Latin1 'ÿ' / 0xff. */
4590 {
4591 static const char str[] = "\377";
4592 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4593 EXPECT ("\377", "\377\377123"));
4594 }
4595
4596 /* Check that the increment-last-char in the matching algorithm for
4597 completion doesn't match "t1_fund" when completing "t1_func". */
4598 {
4599 static const char str[] = "t1_func";
4600 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4601 EXPECT ("t1_func", "t1_func1"));
4602 }
4603
c62446b1
PA
4604 /* Check that completion mode works at each prefix of the expected
4605 symbol name. */
4606 {
4607 static const char str[] = "function(int)";
4608 size_t len = strlen (str);
4609 std::string lookup;
4610
4611 for (size_t i = 1; i < len; i++)
4612 {
4613 lookup.assign (str, i);
4614 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4615 EXPECT ("function"));
4616 }
4617 }
4618
4619 /* While "w" is a prefix of both components, the match function
4620 should still only be called once. */
4621 {
4622 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4623 EXPECT ("w1::w2"));
a20714ff
PA
4624 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4625 EXPECT ("w1::w2"));
c62446b1
PA
4626 }
4627
4628 /* Same, with a "complicated" symbol. */
4629 {
4630 static const char str[] = Z_SYM_NAME;
4631 size_t len = strlen (str);
4632 std::string lookup;
4633
4634 for (size_t i = 1; i < len; i++)
4635 {
4636 lookup.assign (str, i);
4637 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4638 EXPECT (Z_SYM_NAME));
4639 }
4640 }
4641
4642 /* In FULL mode, an incomplete symbol doesn't match. */
4643 {
4644 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4645 {});
4646 }
4647
4648 /* A complete symbol with parameters matches any overload, since the
4649 index has no overload info. */
4650 {
4651 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4652 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4653 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4654 EXPECT ("std::zfunction", "std::zfunction2"));
4655 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4656 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4657 }
4658
4659 /* Check that whitespace is ignored appropriately. A symbol with a
4660 template argument list. */
4661 {
4662 static const char expected[] = "ns::foo<int>";
4663 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4664 EXPECT (expected));
a20714ff
PA
4665 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4666 EXPECT (expected));
c62446b1
PA
4667 }
4668
4669 /* Check that whitespace is ignored appropriately. A symbol with a
4670 template argument list that includes a pointer. */
4671 {
4672 static const char expected[] = "ns::foo<char*>";
4673 /* Try both completion and non-completion modes. */
4674 static const bool completion_mode[2] = {false, true};
4675 for (size_t i = 0; i < 2; i++)
4676 {
4677 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4678 completion_mode[i], EXPECT (expected));
a20714ff
PA
4679 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4680 completion_mode[i], EXPECT (expected));
c62446b1
PA
4681
4682 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4683 completion_mode[i], EXPECT (expected));
a20714ff
PA
4684 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4685 completion_mode[i], EXPECT (expected));
c62446b1
PA
4686 }
4687 }
4688
4689 {
4690 /* Check method qualifiers are ignored. */
4691 static const char expected[] = "ns::foo<char*>";
4692 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4693 symbol_name_match_type::FULL, true, EXPECT (expected));
4694 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4695 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4696 CHECK_MATCH ("foo < char * > ( int ) const",
4697 symbol_name_match_type::WILD, true, EXPECT (expected));
4698 CHECK_MATCH ("foo < char * > ( int ) &&",
4699 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4700 }
4701
4702 /* Test lookup names that don't match anything. */
4703 {
a20714ff
PA
4704 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4705 {});
4706
c62446b1
PA
4707 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4708 {});
4709 }
4710
a20714ff
PA
4711 /* Some wild matching tests, exercising "(anonymous namespace)",
4712 which should not be confused with a parameter list. */
4713 {
4714 static const char *syms[] = {
4715 "A::B::C",
4716 "B::C",
4717 "C",
4718 "A :: B :: C ( int )",
4719 "B :: C ( int )",
4720 "C ( int )",
4721 };
4722
4723 for (const char *s : syms)
4724 {
4725 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4726 EXPECT ("(anonymous namespace)::A::B::C"));
4727 }
4728 }
4729
4730 {
4731 static const char expected[] = "ns2::tmpl<int>::foo2";
4732 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4733 EXPECT (expected));
4734 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4735 EXPECT (expected));
4736 }
4737
c62446b1
PA
4738 SELF_CHECK (!any_mismatch);
4739
4740#undef EXPECT
4741#undef CHECK_MATCH
4742}
4743
5c58de74
PA
4744static void
4745run_test ()
4746{
4747 test_mapped_index_find_name_component_bounds ();
4748 test_dw2_expand_symtabs_matching_symbol ();
4749}
4750
c62446b1
PA
4751}} // namespace selftests::dw2_expand_symtabs_matching
4752
4753#endif /* GDB_SELF_TEST */
4754
4b514bc8
JK
4755/* If FILE_MATCHER is NULL or if PER_CU has
4756 dwarf2_per_cu_quick_data::MARK set (see
4757 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4758 EXPANSION_NOTIFY on it. */
4759
4760static void
4761dw2_expand_symtabs_matching_one
97a1449a
SM
4762 (dwarf2_per_cu_data *per_cu,
4763 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4764 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4765 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4766{
4767 if (file_matcher == NULL || per_cu->v.quick->mark)
4768 {
af758d11 4769 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4770
97a1449a
SM
4771 compunit_symtab *symtab
4772 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4773 gdb_assert (symtab != nullptr);
4b514bc8 4774
af758d11
SM
4775 if (expansion_notify != NULL && symtab_was_null)
4776 expansion_notify (symtab);
4b514bc8
JK
4777 }
4778}
4779
3f563c84
PA
4780/* Helper for dw2_expand_matching symtabs. Called on each symbol
4781 matched, to expand corresponding CUs that were marked. IDX is the
4782 index of the symbol name that matched. */
4783
4784static void
4785dw2_expand_marked_cus
976ca316 4786 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4787 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4788 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4789 search_domain kind)
4790{
3f563c84
PA
4791 offset_type *vec, vec_len, vec_idx;
4792 bool global_seen = false;
976ca316 4793 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4794
61920122 4795 vec = (offset_type *) (index.constant_pool
f00a2de2 4796 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4797 vec_len = MAYBE_SWAP (vec[0]);
4798 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4799 {
61920122
PA
4800 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4801 /* This value is only valid for index versions >= 7. */
4802 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4803 gdb_index_symbol_kind symbol_kind =
4804 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4805 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4806 /* Only check the symbol attributes if they're present.
4807 Indices prior to version 7 don't record them,
4808 and indices >= 7 may elide them for certain symbols
4809 (gold does this). */
4810 int attrs_valid =
4811 (index.version >= 7
4812 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4813
4814 /* Work around gold/15646. */
f030440d
TV
4815 if (attrs_valid
4816 && !is_static
4817 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4818 {
f030440d 4819 if (global_seen)
61920122 4820 continue;
f030440d
TV
4821
4822 global_seen = true;
61920122 4823 }
3190f0c6 4824
61920122
PA
4825 /* Only check the symbol's kind if it has one. */
4826 if (attrs_valid)
4827 {
4828 switch (kind)
8943b874 4829 {
61920122
PA
4830 case VARIABLES_DOMAIN:
4831 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4832 continue;
4833 break;
4834 case FUNCTIONS_DOMAIN:
4835 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4836 continue;
61920122
PA
4837 break;
4838 case TYPES_DOMAIN:
4839 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4840 continue;
4841 break;
59c35742
AB
4842 case MODULES_DOMAIN:
4843 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4844 continue;
4845 break;
61920122
PA
4846 default:
4847 break;
8943b874 4848 }
61920122 4849 }
8943b874 4850
61920122 4851 /* Don't crash on bad data. */
976ca316
SM
4852 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4853 + per_objfile->per_bfd->all_type_units.size ()))
61920122 4854 {
b98664d3 4855 complaint (_(".gdb_index entry has bad CU index"
976ca316 4856 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4857 continue;
4858 }
4859
976ca316
SM
4860 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4861 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4b514bc8 4862 expansion_notify);
61920122
PA
4863 }
4864}
4865
4b514bc8
JK
4866/* If FILE_MATCHER is non-NULL, set all the
4867 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4868 that match FILE_MATCHER. */
4869
61920122 4870static void
4b514bc8 4871dw_expand_symtabs_matching_file_matcher
976ca316 4872 (dwarf2_per_objfile *per_objfile,
ed2dc618 4873 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4874{
4b514bc8 4875 if (file_matcher == NULL)
61920122
PA
4876 return;
4877
4b514bc8
JK
4878 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4879 htab_eq_pointer,
4880 NULL, xcalloc, xfree));
4881 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4882 htab_eq_pointer,
4883 NULL, xcalloc, xfree));
61920122 4884
4b514bc8
JK
4885 /* The rule is CUs specify all the files, including those used by
4886 any TU, so there's no need to scan TUs here. */
61920122 4887
976ca316 4888 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4889 {
927aa2e7
JK
4890 QUIT;
4891
4892 per_cu->v.quick->mark = 0;
4893
4894 /* We only need to look at symtabs not already expanded. */
976ca316 4895 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4896 continue;
4897
976ca316 4898 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4899 if (file_data == NULL)
4900 continue;
4901
4902 if (htab_find (visited_not_found.get (), file_data) != NULL)
4903 continue;
4904 else if (htab_find (visited_found.get (), file_data) != NULL)
4905 {
4906 per_cu->v.quick->mark = 1;
4907 continue;
4908 }
4909
b76e467d 4910 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4911 {
4912 const char *this_real_name;
4913
4914 if (file_matcher (file_data->file_names[j], false))
4915 {
4916 per_cu->v.quick->mark = 1;
4917 break;
4918 }
4919
4920 /* Before we invoke realpath, which can get expensive when many
4921 files are involved, do a quick comparison of the basenames. */
4922 if (!basenames_may_differ
4923 && !file_matcher (lbasename (file_data->file_names[j]),
4924 true))
4925 continue;
4926
976ca316 4927 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4928 if (file_matcher (this_real_name, false))
4929 {
4930 per_cu->v.quick->mark = 1;
4931 break;
4932 }
4933 }
4934
b76e467d
SM
4935 void **slot = htab_find_slot (per_cu->v.quick->mark
4936 ? visited_found.get ()
4937 : visited_not_found.get (),
4938 file_data, INSERT);
927aa2e7
JK
4939 *slot = file_data;
4940 }
4941}
4942
4943static void
4944dw2_expand_symtabs_matching
4945 (struct objfile *objfile,
4946 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4947 const lookup_name_info *lookup_name,
927aa2e7
JK
4948 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4949 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4950 enum search_domain kind)
4951{
976ca316 4952 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4953
4954 /* index_table is NULL if OBJF_READNOW. */
976ca316 4955 if (!per_objfile->per_bfd->index_table)
927aa2e7
JK
4956 return;
4957
976ca316 4958 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4959
c1a66c06
TV
4960 if (symbol_matcher == NULL && lookup_name == NULL)
4961 {
976ca316 4962 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4963 {
4964 QUIT;
4965
976ca316 4966 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
97a1449a 4967 file_matcher, expansion_notify);
c1a66c06
TV
4968 }
4969 return;
4970 }
4971
976ca316 4972 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4973
c1a66c06 4974 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4975 symbol_matcher,
4976 kind, [&] (offset_type idx)
4977 {
976ca316
SM
4978 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4979 kind);
3b00ef10 4980 return true;
976ca316 4981 }, per_objfile);
927aa2e7
JK
4982}
4983
4984/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4985 symtab. */
4986
4987static struct compunit_symtab *
4988recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4989 CORE_ADDR pc)
4990{
4991 int i;
4992
4993 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4994 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4995 return cust;
4996
4997 if (cust->includes == NULL)
4998 return NULL;
4999
5000 for (i = 0; cust->includes[i]; ++i)
5001 {
5002 struct compunit_symtab *s = cust->includes[i];
5003
5004 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5005 if (s != NULL)
5006 return s;
5007 }
5008
5009 return NULL;
5010}
5011
5012static struct compunit_symtab *
5013dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5014 struct bound_minimal_symbol msymbol,
5015 CORE_ADDR pc,
5016 struct obj_section *section,
5017 int warn_if_readin)
5018{
5019 struct dwarf2_per_cu_data *data;
5020 struct compunit_symtab *result;
5021
d320c2b5 5022 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
5023 return NULL;
5024
b3b3bada 5025 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
5026 data = (struct dwarf2_per_cu_data *) addrmap_find
5027 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5028 if (!data)
5029 return NULL;
5030
af758d11
SM
5031 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5032 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 5033 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 5034 paddress (objfile->arch (), pc));
927aa2e7 5035
97a1449a
SM
5036 result = recursively_find_pc_sect_compunit_symtab
5037 (dw2_instantiate_symtab (data, per_objfile, false), pc);
5038
927aa2e7
JK
5039 gdb_assert (result != NULL);
5040 return result;
5041}
5042
5043static void
5044dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5045 void *data, int need_fullname)
5046{
976ca316 5047 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 5048
976ca316 5049 if (!per_objfile->per_bfd->filenames_cache)
927aa2e7 5050 {
976ca316 5051 per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
5052
5053 htab_up visited (htab_create_alloc (10,
5054 htab_hash_pointer, htab_eq_pointer,
5055 NULL, xcalloc, xfree));
5056
5057 /* The rule is CUs specify all the files, including those used
5058 by any TU, so there's no need to scan TUs here. We can
5059 ignore file names coming from already-expanded CUs. */
5060
976ca316 5061 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 5062 {
976ca316 5063 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5064 {
5065 void **slot = htab_find_slot (visited.get (),
5066 per_cu->v.quick->file_names,
5067 INSERT);
5068
5069 *slot = per_cu->v.quick->file_names;
5070 }
5071 }
5072
976ca316 5073 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 5074 {
927aa2e7 5075 /* We only need to look at symtabs not already expanded. */
976ca316 5076 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5077 continue;
5078
ab432490 5079 quick_file_names *file_data
976ca316 5080 = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
5081 if (file_data == NULL)
5082 continue;
5083
b76e467d 5084 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5085 if (*slot)
5086 {
5087 /* Already visited. */
5088 continue;
5089 }
5090 *slot = file_data;
5091
5092 for (int j = 0; j < file_data->num_file_names; ++j)
5093 {
5094 const char *filename = file_data->file_names[j];
976ca316 5095 per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
5096 }
5097 }
5098 }
5099
976ca316 5100 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
5101 {
5102 gdb::unique_xmalloc_ptr<char> this_real_name;
5103
5104 if (need_fullname)
5105 this_real_name = gdb_realpath (filename);
5106 (*fun) (filename, this_real_name.get (), data);
5107 });
5108}
5109
5110static int
5111dw2_has_symbols (struct objfile *objfile)
5112{
5113 return 1;
5114}
5115
5116const struct quick_symbol_functions dwarf2_gdb_index_functions =
5117{
5118 dw2_has_symbols,
5119 dw2_find_last_source_symtab,
5120 dw2_forget_cached_source_info,
5121 dw2_map_symtabs_matching_filename,
5122 dw2_lookup_symbol,
d3214198 5123 NULL,
927aa2e7
JK
5124 dw2_print_stats,
5125 dw2_dump,
927aa2e7
JK
5126 dw2_expand_symtabs_for_function,
5127 dw2_expand_all_symtabs,
5128 dw2_expand_symtabs_with_fullname,
5129 dw2_map_matching_symbols,
5130 dw2_expand_symtabs_matching,
5131 dw2_find_pc_sect_compunit_symtab,
5132 NULL,
5133 dw2_map_symbol_filenames
5134};
5135
5136/* DWARF-5 debug_names reader. */
5137
5138/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5139static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5140
5141/* A helper function that reads the .debug_names section in SECTION
5142 and fills in MAP. FILENAME is the name of the file containing the
5143 section; it is used for error reporting.
5144
5145 Returns true if all went well, false otherwise. */
5146
5147static bool
5148read_debug_names_from_section (struct objfile *objfile,
5149 const char *filename,
5150 struct dwarf2_section_info *section,
5151 mapped_debug_names &map)
5152{
96b79293 5153 if (section->empty ())
927aa2e7
JK
5154 return false;
5155
5156 /* Older elfutils strip versions could keep the section in the main
5157 executable while splitting it for the separate debug info file. */
96b79293 5158 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
5159 return false;
5160
96b79293 5161 section->read (objfile);
927aa2e7 5162
08feed99 5163 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
5164
5165 const gdb_byte *addr = section->buffer;
5166
96b79293 5167 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
5168
5169 unsigned int bytes_read;
5170 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5171 addr += bytes_read;
5172
5173 map.dwarf5_is_dwarf64 = bytes_read != 4;
5174 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5175 if (bytes_read + length != section->size)
5176 {
5177 /* There may be multiple per-CU indices. */
5178 warning (_("Section .debug_names in %s length %s does not match "
5179 "section length %s, ignoring .debug_names."),
5180 filename, plongest (bytes_read + length),
5181 pulongest (section->size));
5182 return false;
5183 }
5184
5185 /* The version number. */
5186 uint16_t version = read_2_bytes (abfd, addr);
5187 addr += 2;
5188 if (version != 5)
5189 {
5190 warning (_("Section .debug_names in %s has unsupported version %d, "
5191 "ignoring .debug_names."),
5192 filename, version);
5193 return false;
5194 }
5195
5196 /* Padding. */
5197 uint16_t padding = read_2_bytes (abfd, addr);
5198 addr += 2;
5199 if (padding != 0)
5200 {
5201 warning (_("Section .debug_names in %s has unsupported padding %d, "
5202 "ignoring .debug_names."),
5203 filename, padding);
5204 return false;
5205 }
5206
5207 /* comp_unit_count - The number of CUs in the CU list. */
5208 map.cu_count = read_4_bytes (abfd, addr);
5209 addr += 4;
5210
5211 /* local_type_unit_count - The number of TUs in the local TU
5212 list. */
5213 map.tu_count = read_4_bytes (abfd, addr);
5214 addr += 4;
5215
5216 /* foreign_type_unit_count - The number of TUs in the foreign TU
5217 list. */
5218 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5219 addr += 4;
5220 if (foreign_tu_count != 0)
5221 {
5222 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5223 "ignoring .debug_names."),
5224 filename, static_cast<unsigned long> (foreign_tu_count));
5225 return false;
5226 }
5227
5228 /* bucket_count - The number of hash buckets in the hash lookup
5229 table. */
5230 map.bucket_count = read_4_bytes (abfd, addr);
5231 addr += 4;
5232
5233 /* name_count - The number of unique names in the index. */
5234 map.name_count = read_4_bytes (abfd, addr);
5235 addr += 4;
5236
5237 /* abbrev_table_size - The size in bytes of the abbreviations
5238 table. */
5239 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5240 addr += 4;
5241
5242 /* augmentation_string_size - The size in bytes of the augmentation
5243 string. This value is rounded up to a multiple of 4. */
5244 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5245 addr += 4;
5246 map.augmentation_is_gdb = ((augmentation_string_size
5247 == sizeof (dwarf5_augmentation))
5248 && memcmp (addr, dwarf5_augmentation,
5249 sizeof (dwarf5_augmentation)) == 0);
5250 augmentation_string_size += (-augmentation_string_size) & 3;
5251 addr += augmentation_string_size;
5252
5253 /* List of CUs */
5254 map.cu_table_reordered = addr;
5255 addr += map.cu_count * map.offset_size;
5256
5257 /* List of Local TUs */
5258 map.tu_table_reordered = addr;
5259 addr += map.tu_count * map.offset_size;
5260
5261 /* Hash Lookup Table */
5262 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5263 addr += map.bucket_count * 4;
5264 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5265 addr += map.name_count * 4;
5266
5267 /* Name Table */
5268 map.name_table_string_offs_reordered = addr;
5269 addr += map.name_count * map.offset_size;
5270 map.name_table_entry_offs_reordered = addr;
5271 addr += map.name_count * map.offset_size;
5272
5273 const gdb_byte *abbrev_table_start = addr;
5274 for (;;)
5275 {
927aa2e7
JK
5276 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5277 addr += bytes_read;
5278 if (index_num == 0)
5279 break;
5280
5281 const auto insertpair
5282 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5283 if (!insertpair.second)
5284 {
5285 warning (_("Section .debug_names in %s has duplicate index %s, "
5286 "ignoring .debug_names."),
5287 filename, pulongest (index_num));
5288 return false;
5289 }
5290 mapped_debug_names::index_val &indexval = insertpair.first->second;
5291 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5292 addr += bytes_read;
5293
5294 for (;;)
5295 {
5296 mapped_debug_names::index_val::attr attr;
5297 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5298 addr += bytes_read;
5299 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5300 addr += bytes_read;
5301 if (attr.form == DW_FORM_implicit_const)
5302 {
5303 attr.implicit_const = read_signed_leb128 (abfd, addr,
5304 &bytes_read);
5305 addr += bytes_read;
5306 }
5307 if (attr.dw_idx == 0 && attr.form == 0)
5308 break;
5309 indexval.attr_vec.push_back (std::move (attr));
5310 }
5311 }
5312 if (addr != abbrev_table_start + abbrev_table_size)
5313 {
5314 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5315 "of size %s vs. written as %u, ignoring .debug_names."),
5316 filename, plongest (addr - abbrev_table_start),
5317 abbrev_table_size);
927aa2e7
JK
5318 return false;
5319 }
5320 map.entry_pool = addr;
5321
5322 return true;
5323}
5324
5325/* A helper for create_cus_from_debug_names that handles the MAP's CU
5326 list. */
5327
5328static void
168c9250 5329create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5330 const mapped_debug_names &map,
5331 dwarf2_section_info &section,
b76e467d 5332 bool is_dwz)
927aa2e7 5333{
3ee6bb11
TV
5334 if (!map.augmentation_is_gdb)
5335 {
fc9a13fb
TV
5336 for (uint32_t i = 0; i < map.cu_count; ++i)
5337 {
5338 sect_offset sect_off
5339 = (sect_offset) (extract_unsigned_integer
5340 (map.cu_table_reordered + i * map.offset_size,
5341 map.offset_size,
5342 map.dwarf5_byte_order));
5343 /* We don't know the length of the CU, because the CU list in a
5344 .debug_names index can be incomplete, so we can't use the start
5345 of the next CU as end of this CU. We create the CUs here with
5346 length 0, and in cutu_reader::cutu_reader we'll fill in the
5347 actual length. */
5348 dwarf2_per_cu_data *per_cu
5349 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5350 sect_off, 0);
5351 per_bfd->all_comp_units.push_back (per_cu);
5352 }
d3b54e63 5353 return;
3ee6bb11
TV
5354 }
5355
927aa2e7
JK
5356 sect_offset sect_off_prev;
5357 for (uint32_t i = 0; i <= map.cu_count; ++i)
5358 {
5359 sect_offset sect_off_next;
5360 if (i < map.cu_count)
5361 {
5362 sect_off_next
5363 = (sect_offset) (extract_unsigned_integer
5364 (map.cu_table_reordered + i * map.offset_size,
5365 map.offset_size,
5366 map.dwarf5_byte_order));
5367 }
5368 else
5369 sect_off_next = (sect_offset) section.size;
5370 if (i >= 1)
5371 {
5372 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5373 dwarf2_per_cu_data *per_cu
168c9250 5374 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5375 sect_off_prev, length);
168c9250 5376 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5377 }
5378 sect_off_prev = sect_off_next;
5379 }
5380}
5381
5382/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5383 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5384
5385static void
168c9250 5386create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5387 const mapped_debug_names &map,
5388 const mapped_debug_names &dwz_map)
5389{
168c9250
SM
5390 gdb_assert (per_bfd->all_comp_units.empty ());
5391 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5392
168c9250 5393 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5394 false /* is_dwz */);
927aa2e7
JK
5395
5396 if (dwz_map.cu_count == 0)
5397 return;
5398
168c9250
SM
5399 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5400 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5401 true /* is_dwz */);
927aa2e7
JK
5402}
5403
5404/* Read .debug_names. If everything went ok, initialize the "quick"
5405 elements of all the CUs and return true. Otherwise, return false. */
5406
5407static bool
976ca316 5408dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 5409{
fcf23d5b
SM
5410 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5411 mapped_debug_names dwz_map;
976ca316 5412 struct objfile *objfile = per_objfile->objfile;
f8c41851 5413 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
5414
5415 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
976ca316 5416 &per_objfile->per_bfd->debug_names, *map))
927aa2e7
JK
5417 return false;
5418
5419 /* Don't use the index if it's empty. */
22ca247e 5420 if (map->name_count == 0)
927aa2e7
JK
5421 return false;
5422
5423 /* If there is a .dwz file, read it so we can get its CU list as
5424 well. */
f8c41851 5425 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
5426 if (dwz != NULL)
5427 {
5428 if (!read_debug_names_from_section (objfile,
00f93c44 5429 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5430 &dwz->debug_names, dwz_map))
5431 {
5432 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5433 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5434 return false;
5435 }
5436 }
5437
f8c41851 5438 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 5439
22ca247e 5440 if (map->tu_count != 0)
927aa2e7
JK
5441 {
5442 /* We can only handle a single .debug_types when we have an
5443 index. */
f8c41851 5444 if (per_bfd->types.size () != 1)
927aa2e7
JK
5445 return false;
5446
f8c41851 5447 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
5448
5449 create_signatured_type_table_from_debug_names
f8c41851 5450 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
5451 }
5452
f8c41851 5453 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 5454
f8c41851
SM
5455 per_bfd->debug_names_table = std::move (map);
5456 per_bfd->using_index = 1;
5457 per_bfd->quick_file_names_table =
976ca316 5458 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
927aa2e7 5459
f8c41851
SM
5460 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5461 objfiles using the same BFD. */
5462 gdb_assert (per_bfd->partial_symtabs == nullptr);
5463 per_bfd->partial_symtabs = objfile->partial_symtabs;
5464
927aa2e7
JK
5465 return true;
5466}
5467
927aa2e7
JK
5468/* Type used to manage iterating over all CUs looking for a symbol for
5469 .debug_names. */
5470
5471class dw2_debug_names_iterator
5472{
5473public:
927aa2e7 5474 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5475 gdb::optional<block_enum> block_index,
5476 domain_enum domain,
fcf23d5b 5477 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5478 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5479 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5480 m_per_objfile (per_objfile)
927aa2e7
JK
5481 {}
5482
5483 dw2_debug_names_iterator (const mapped_debug_names &map,
fcf23d5b 5484 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5485 : m_map (map),
5486 m_search (search),
fcf23d5b
SM
5487 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5488 m_per_objfile (per_objfile)
927aa2e7
JK
5489 {}
5490
3b00ef10
TT
5491 dw2_debug_names_iterator (const mapped_debug_names &map,
5492 block_enum block_index, domain_enum domain,
fcf23d5b 5493 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5494 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5495 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5496 m_per_objfile (per_objfile)
3b00ef10
TT
5497 {}
5498
927aa2e7
JK
5499 /* Return the next matching CU or NULL if there are no more. */
5500 dwarf2_per_cu_data *next ();
5501
5502private:
5503 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5504 const char *name,
5505 dwarf2_per_objfile *per_objfile);
927aa2e7 5506 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5507 uint32_t namei,
5508 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5509
5510 /* The internalized form of .debug_names. */
5511 const mapped_debug_names &m_map;
5512
2b79f376
SM
5513 /* If set, only look for symbols that match that block. Valid values are
5514 GLOBAL_BLOCK and STATIC_BLOCK. */
5515 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5516
5517 /* The kind of symbol we're looking for. */
5518 const domain_enum m_domain = UNDEF_DOMAIN;
5519 const search_domain m_search = ALL_DOMAIN;
5520
5521 /* The list of CUs from the index entry of the symbol, or NULL if
5522 not found. */
5523 const gdb_byte *m_addr;
fcf23d5b
SM
5524
5525 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5526};
5527
5528const char *
fcf23d5b 5529mapped_debug_names::namei_to_name
976ca316 5530 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5531{
5532 const ULONGEST namei_string_offs
5533 = extract_unsigned_integer ((name_table_string_offs_reordered
5534 + namei * offset_size),
5535 offset_size,
5536 dwarf5_byte_order);
976ca316 5537 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5538}
5539
5540/* Find a slot in .debug_names for the object named NAME. If NAME is
5541 found, return pointer to its pool data. If NAME cannot be found,
5542 return NULL. */
5543
5544const gdb_byte *
5545dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5546 (const mapped_debug_names &map, const char *name,
5547 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5548{
5549 int (*cmp) (const char *, const char *);
5550
54ee4252 5551 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5552 if (current_language->la_language == language_cplus
5553 || current_language->la_language == language_fortran
5554 || current_language->la_language == language_d)
5555 {
5556 /* NAME is already canonical. Drop any qualifiers as
5557 .debug_names does not contain any. */
5558
5559 if (strchr (name, '(') != NULL)
5560 {
54ee4252 5561 without_params = cp_remove_params (name);
927aa2e7 5562 if (without_params != NULL)
54ee4252 5563 name = without_params.get ();
927aa2e7
JK
5564 }
5565 }
5566
5567 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5568
5569 const uint32_t full_hash = dwarf5_djb_hash (name);
5570 uint32_t namei
5571 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5572 (map.bucket_table_reordered
5573 + (full_hash % map.bucket_count)), 4,
5574 map.dwarf5_byte_order);
5575 if (namei == 0)
5576 return NULL;
5577 --namei;
5578 if (namei >= map.name_count)
5579 {
b98664d3 5580 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5581 "[in module %s]"),
5582 namei, map.name_count,
fcf23d5b 5583 objfile_name (per_objfile->objfile));
927aa2e7
JK
5584 return NULL;
5585 }
5586
5587 for (;;)
5588 {
5589 const uint32_t namei_full_hash
5590 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5591 (map.hash_table_reordered + namei), 4,
5592 map.dwarf5_byte_order);
5593 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5594 return NULL;
5595
5596 if (full_hash == namei_full_hash)
5597 {
fcf23d5b 5598 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5599
5600#if 0 /* An expensive sanity check. */
5601 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5602 {
b98664d3 5603 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5604 "[in module %s]"),
5605 namei, objfile_name (dwarf2_per_objfile->objfile));
5606 return NULL;
5607 }
5608#endif
5609
5610 if (cmp (namei_string, name) == 0)
5611 {
5612 const ULONGEST namei_entry_offs
5613 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5614 + namei * map.offset_size),
5615 map.offset_size, map.dwarf5_byte_order);
5616 return map.entry_pool + namei_entry_offs;
5617 }
5618 }
5619
5620 ++namei;
5621 if (namei >= map.name_count)
5622 return NULL;
5623 }
5624}
5625
5626const gdb_byte *
5627dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5628 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5629{
5630 if (namei >= map.name_count)
5631 {
b98664d3 5632 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5633 "[in module %s]"),
5634 namei, map.name_count,
fcf23d5b 5635 objfile_name (per_objfile->objfile));
927aa2e7
JK
5636 return NULL;
5637 }
5638
5639 const ULONGEST namei_entry_offs
5640 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5641 + namei * map.offset_size),
5642 map.offset_size, map.dwarf5_byte_order);
5643 return map.entry_pool + namei_entry_offs;
5644}
5645
5646/* See dw2_debug_names_iterator. */
5647
5648dwarf2_per_cu_data *
5649dw2_debug_names_iterator::next ()
5650{
5651 if (m_addr == NULL)
5652 return NULL;
5653
fcf23d5b
SM
5654 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5655 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5656 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5657
5658 again:
5659
5660 unsigned int bytes_read;
5661 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5662 m_addr += bytes_read;
5663 if (abbrev == 0)
5664 return NULL;
5665
5666 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5667 if (indexval_it == m_map.abbrev_map.cend ())
5668 {
b98664d3 5669 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5670 "[in module %s]"),
ed2dc618 5671 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5672 return NULL;
5673 }
5674 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5675 enum class symbol_linkage {
5676 unknown,
5677 static_,
5678 extern_,
23c13d42 5679 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5680 dwarf2_per_cu_data *per_cu = NULL;
5681 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5682 {
5683 ULONGEST ull;
5684 switch (attr.form)
5685 {
5686 case DW_FORM_implicit_const:
5687 ull = attr.implicit_const;
5688 break;
5689 case DW_FORM_flag_present:
5690 ull = 1;
5691 break;
5692 case DW_FORM_udata:
5693 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5694 m_addr += bytes_read;
5695 break;
6dc55ce9 5696 case DW_FORM_ref4:
5697 ull = read_4_bytes (abfd, m_addr);
5698 m_addr += 4;
5699 break;
5700 case DW_FORM_ref8:
5701 ull = read_8_bytes (abfd, m_addr);
5702 m_addr += 8;
5703 break;
5704 case DW_FORM_ref_sig8:
5705 ull = read_8_bytes (abfd, m_addr);
5706 m_addr += 8;
5707 break;
927aa2e7 5708 default:
b98664d3 5709 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5710 dwarf_form_name (attr.form),
ed2dc618 5711 objfile_name (objfile));
927aa2e7
JK
5712 return NULL;
5713 }
5714 switch (attr.dw_idx)
5715 {
5716 case DW_IDX_compile_unit:
5717 /* Don't crash on bad data. */
fcf23d5b 5718 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5719 {
b98664d3 5720 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5721 " [in module %s]"),
5722 pulongest (ull),
fcf23d5b 5723 objfile_name (objfile));
927aa2e7
JK
5724 continue;
5725 }
fcf23d5b 5726 per_cu = per_bfd->get_cutu (ull);
927aa2e7 5727 break;
8af5c486
JK
5728 case DW_IDX_type_unit:
5729 /* Don't crash on bad data. */
fcf23d5b 5730 if (ull >= per_bfd->all_type_units.size ())
8af5c486 5731 {
b98664d3 5732 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5733 " [in module %s]"),
5734 pulongest (ull),
fcf23d5b 5735 objfile_name (objfile));
8af5c486
JK
5736 continue;
5737 }
fcf23d5b 5738 per_cu = &per_bfd->get_tu (ull)->per_cu;
8af5c486 5739 break;
6dc55ce9 5740 case DW_IDX_die_offset:
5741 /* In a per-CU index (as opposed to a per-module index), index
5742 entries without CU attribute implicitly refer to the single CU. */
5743 if (per_cu == NULL)
fcf23d5b 5744 per_cu = per_bfd->get_cu (0);
6dc55ce9 5745 break;
927aa2e7
JK
5746 case DW_IDX_GNU_internal:
5747 if (!m_map.augmentation_is_gdb)
5748 break;
23c13d42 5749 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5750 break;
5751 case DW_IDX_GNU_external:
5752 if (!m_map.augmentation_is_gdb)
5753 break;
23c13d42 5754 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5755 break;
5756 }
5757 }
5758
5759 /* Skip if already read in. */
fcf23d5b 5760 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5761 goto again;
5762
5763 /* Check static vs global. */
23c13d42 5764 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5765 {
2b79f376 5766 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5767 const bool symbol_is_static =
5768 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5769 if (want_static != symbol_is_static)
2b79f376 5770 goto again;
927aa2e7
JK
5771 }
5772
5773 /* Match dw2_symtab_iter_next, symbol_kind
5774 and debug_names::psymbol_tag. */
5775 switch (m_domain)
5776 {
5777 case VAR_DOMAIN:
5778 switch (indexval.dwarf_tag)
5779 {
5780 case DW_TAG_variable:
5781 case DW_TAG_subprogram:
5782 /* Some types are also in VAR_DOMAIN. */
5783 case DW_TAG_typedef:
5784 case DW_TAG_structure_type:
5785 break;
5786 default:
5787 goto again;
5788 }
5789 break;
5790 case STRUCT_DOMAIN:
5791 switch (indexval.dwarf_tag)
5792 {
5793 case DW_TAG_typedef:
5794 case DW_TAG_structure_type:
5795 break;
5796 default:
5797 goto again;
5798 }
5799 break;
5800 case LABEL_DOMAIN:
5801 switch (indexval.dwarf_tag)
5802 {
5803 case 0:
5804 case DW_TAG_variable:
5805 break;
5806 default:
5807 goto again;
5808 }
5809 break;
59c35742
AB
5810 case MODULE_DOMAIN:
5811 switch (indexval.dwarf_tag)
5812 {
5813 case DW_TAG_module:
5814 break;
5815 default:
5816 goto again;
5817 }
5818 break;
927aa2e7
JK
5819 default:
5820 break;
5821 }
5822
5823 /* Match dw2_expand_symtabs_matching, symbol_kind and
5824 debug_names::psymbol_tag. */
5825 switch (m_search)
4b514bc8 5826 {
927aa2e7
JK
5827 case VARIABLES_DOMAIN:
5828 switch (indexval.dwarf_tag)
4b514bc8 5829 {
927aa2e7
JK
5830 case DW_TAG_variable:
5831 break;
5832 default:
5833 goto again;
4b514bc8 5834 }
927aa2e7
JK
5835 break;
5836 case FUNCTIONS_DOMAIN:
5837 switch (indexval.dwarf_tag)
4b514bc8 5838 {
927aa2e7
JK
5839 case DW_TAG_subprogram:
5840 break;
5841 default:
5842 goto again;
4b514bc8 5843 }
927aa2e7
JK
5844 break;
5845 case TYPES_DOMAIN:
5846 switch (indexval.dwarf_tag)
5847 {
5848 case DW_TAG_typedef:
5849 case DW_TAG_structure_type:
5850 break;
5851 default:
5852 goto again;
5853 }
5854 break;
59c35742
AB
5855 case MODULES_DOMAIN:
5856 switch (indexval.dwarf_tag)
5857 {
5858 case DW_TAG_module:
5859 break;
5860 default:
5861 goto again;
5862 }
927aa2e7
JK
5863 default:
5864 break;
4b514bc8 5865 }
927aa2e7
JK
5866
5867 return per_cu;
4b514bc8 5868}
61920122 5869
927aa2e7 5870static struct compunit_symtab *
c7f839cb 5871dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5872 const char *name, domain_enum domain)
4b514bc8 5873{
976ca316 5874 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
61920122 5875
976ca316 5876 const auto &mapp = per_objfile->per_bfd->debug_names_table;
927aa2e7 5877 if (!mapp)
61920122 5878 {
927aa2e7
JK
5879 /* index is NULL if OBJF_READNOW. */
5880 return NULL;
5881 }
5882 const auto &map = *mapp;
9291a0cd 5883
976ca316 5884 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
9703b513 5885
927aa2e7
JK
5886 struct compunit_symtab *stab_best = NULL;
5887 struct dwarf2_per_cu_data *per_cu;
5888 while ((per_cu = iter.next ()) != NULL)
5889 {
5890 struct symbol *sym, *with_opaque = NULL;
97a1449a 5891 compunit_symtab *stab
976ca316 5892 = dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7 5893 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5894 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5895
927aa2e7
JK
5896 sym = block_find_symbol (block, name, domain,
5897 block_find_non_opaque_type_preferred,
5898 &with_opaque);
9703b513 5899
927aa2e7
JK
5900 /* Some caution must be observed with overloaded functions and
5901 methods, since the index will not contain any overload
5902 information (but NAME might contain it). */
a3ec0bb1 5903
927aa2e7 5904 if (sym != NULL
987012b8 5905 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5906 return stab;
5907 if (with_opaque != NULL
987012b8 5908 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5909 stab_best = stab;
9703b513 5910
927aa2e7 5911 /* Keep looking through other CUs. */
9703b513
TT
5912 }
5913
927aa2e7 5914 return stab_best;
9703b513
TT
5915}
5916
927aa2e7
JK
5917/* This dumps minimal information about .debug_names. It is called
5918 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5919 uses this to verify that .debug_names has been loaded. */
9291a0cd 5920
927aa2e7
JK
5921static void
5922dw2_debug_names_dump (struct objfile *objfile)
5923{
976ca316 5924 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5925
976ca316 5926 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5927 printf_filtered (".debug_names:");
976ca316 5928 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5929 printf_filtered (" exists\n");
5930 else
5931 printf_filtered (" faked for \"readnow\"\n");
5932 printf_filtered ("\n");
9291a0cd
TT
5933}
5934
9291a0cd 5935static void
927aa2e7
JK
5936dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5937 const char *func_name)
9291a0cd 5938{
976ca316 5939 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 5940
976ca316
SM
5941 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5942 if (per_objfile->per_bfd->debug_names_table)
24c79950 5943 {
976ca316 5944 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
24c79950 5945
fcf23d5b 5946 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
976ca316 5947 per_objfile);
24c79950 5948
927aa2e7
JK
5949 struct dwarf2_per_cu_data *per_cu;
5950 while ((per_cu = iter.next ()) != NULL)
976ca316 5951 dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7
JK
5952 }
5953}
24c79950 5954
3b00ef10
TT
5955static void
5956dw2_debug_names_map_matching_symbols
5957 (struct objfile *objfile,
5958 const lookup_name_info &name, domain_enum domain,
5959 int global,
5960 gdb::function_view<symbol_found_callback_ftype> callback,
5961 symbol_compare_ftype *ordered_compare)
5962{
976ca316 5963 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5964
5965 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5966 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5967 return;
5968
976ca316 5969 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5970 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5971
5972 const char *match_name = name.ada ().lookup_name ().c_str ();
5973 auto matcher = [&] (const char *symname)
5974 {
5975 if (ordered_compare == nullptr)
5976 return true;
5977 return ordered_compare (symname, match_name) == 0;
5978 };
5979
5980 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5981 [&] (offset_type namei)
5982 {
5983 /* The name was matched, now expand corresponding CUs that were
5984 marked. */
fcf23d5b 5985 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
976ca316 5986 per_objfile);
3b00ef10
TT
5987
5988 struct dwarf2_per_cu_data *per_cu;
5989 while ((per_cu = iter.next ()) != NULL)
976ca316 5990 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5991 nullptr);
3b00ef10 5992 return true;
976ca316 5993 }, per_objfile);
3b00ef10
TT
5994
5995 /* It's a shame we couldn't do this inside the
5996 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5997 that have already been expanded. Instead, this loop matches what
5998 the psymtab code does. */
976ca316 5999 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3b00ef10 6000 {
976ca316 6001 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
af758d11 6002 if (symtab != nullptr)
3b00ef10
TT
6003 {
6004 const struct block *block
af758d11 6005 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
6006 if (!iterate_over_symbols_terminated (block, name,
6007 domain, callback))
6008 break;
6009 }
6010 }
6011}
6012
927aa2e7
JK
6013static void
6014dw2_debug_names_expand_symtabs_matching
6015 (struct objfile *objfile,
6016 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 6017 const lookup_name_info *lookup_name,
927aa2e7
JK
6018 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6019 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6020 enum search_domain kind)
6021{
976ca316 6022 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 6023
927aa2e7 6024 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 6025 if (!per_objfile->per_bfd->debug_names_table)
927aa2e7 6026 return;
9291a0cd 6027
976ca316 6028 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 6029
c1a66c06
TV
6030 if (symbol_matcher == NULL && lookup_name == NULL)
6031 {
976ca316 6032 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
6033 {
6034 QUIT;
6035
976ca316
SM
6036 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
6037 expansion_notify);
c1a66c06
TV
6038 }
6039 return;
6040 }
6041
976ca316 6042 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 6043
c1a66c06 6044 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
6045 symbol_matcher,
6046 kind, [&] (offset_type namei)
927aa2e7 6047 {
927aa2e7
JK
6048 /* The name was matched, now expand corresponding CUs that were
6049 marked. */
976ca316 6050 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
bbf2f4df 6051
927aa2e7
JK
6052 struct dwarf2_per_cu_data *per_cu;
6053 while ((per_cu = iter.next ()) != NULL)
976ca316
SM
6054 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
6055 expansion_notify);
3b00ef10 6056 return true;
976ca316 6057 }, per_objfile);
9291a0cd
TT
6058}
6059
927aa2e7 6060const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6061{
6062 dw2_has_symbols,
6063 dw2_find_last_source_symtab,
6064 dw2_forget_cached_source_info,
f8eba3c6 6065 dw2_map_symtabs_matching_filename,
927aa2e7 6066 dw2_debug_names_lookup_symbol,
d3214198 6067 NULL,
9291a0cd 6068 dw2_print_stats,
927aa2e7 6069 dw2_debug_names_dump,
927aa2e7 6070 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6071 dw2_expand_all_symtabs,
652a8996 6072 dw2_expand_symtabs_with_fullname,
3b00ef10 6073 dw2_debug_names_map_matching_symbols,
927aa2e7 6074 dw2_debug_names_expand_symtabs_matching,
43f3e411 6075 dw2_find_pc_sect_compunit_symtab,
71a3c369 6076 NULL,
9291a0cd
TT
6077 dw2_map_symbol_filenames
6078};
6079
4485a1c1 6080/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 6081 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
6082
6083template <typename T>
6084static gdb::array_view<const gdb_byte>
6085get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6086{
6087 dwarf2_section_info *section = &section_owner->gdb_index;
6088
96b79293 6089 if (section->empty ())
4485a1c1
SM
6090 return {};
6091
6092 /* Older elfutils strip versions could keep the section in the main
6093 executable while splitting it for the separate debug info file. */
96b79293 6094 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
6095 return {};
6096
96b79293 6097 section->read (obj);
4485a1c1 6098
8bebfcda
PA
6099 /* dwarf2_section_info::size is a bfd_size_type, while
6100 gdb::array_view works with size_t. On 32-bit hosts, with
6101 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6102 is 32-bit. So we need an explicit narrowing conversion here.
6103 This is fine, because it's impossible to allocate or mmap an
6104 array/buffer larger than what size_t can represent. */
6105 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6106}
6107
87d6a7aa
SM
6108/* Lookup the index cache for the contents of the index associated to
6109 DWARF2_OBJ. */
6110
6111static gdb::array_view<const gdb_byte>
5989a64e 6112get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
6113{
6114 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6115 if (build_id == nullptr)
6116 return {};
6117
6118 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 6119 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
6120}
6121
6122/* Same as the above, but for DWZ. */
6123
6124static gdb::array_view<const gdb_byte>
6125get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6126{
6127 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6128 if (build_id == nullptr)
6129 return {};
6130
6131 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6132}
6133
3c0aa29a 6134/* See symfile.h. */
9291a0cd 6135
3c0aa29a
PA
6136bool
6137dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6138{
976ca316
SM
6139 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6140 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 6141
9291a0cd
TT
6142 /* If we're about to read full symbols, don't bother with the
6143 indices. In this case we also don't care if some other debug
6144 format is making psymtabs, because they are all about to be
6145 expanded anyway. */
6146 if ((objfile->flags & OBJF_READNOW))
6147 {
17ee85fc
TT
6148 /* When using READNOW, the using_index flag (set below) indicates that
6149 PER_BFD was already initialized, when we loaded some other objfile. */
6150 if (per_bfd->using_index)
6151 {
6152 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6153 per_objfile->resize_symtabs ();
17ee85fc
TT
6154 return true;
6155 }
6156
6157 per_bfd->using_index = 1;
976ca316
SM
6158 create_all_comp_units (per_objfile);
6159 create_all_type_units (per_objfile);
17ee85fc
TT
6160 per_bfd->quick_file_names_table
6161 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 6162 per_objfile->resize_symtabs ();
9291a0cd 6163
17ee85fc
TT
6164 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6165 + per_bfd->all_type_units.size ()); ++i)
9291a0cd 6166 {
17ee85fc 6167 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
9291a0cd 6168
17ee85fc 6169 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 6170 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6171 }
6172
6173 /* Return 1 so that gdb sees the "quick" functions. However,
6174 these functions will be no-ops because we will have expanded
6175 all symtabs. */
3c0aa29a
PA
6176 *index_kind = dw_index_kind::GDB_INDEX;
6177 return true;
9291a0cd
TT
6178 }
6179
17ee85fc
TT
6180 /* Was a debug names index already read when we processed an objfile sharing
6181 PER_BFD? */
6182 if (per_bfd->debug_names_table != nullptr)
6183 {
6184 *index_kind = dw_index_kind::DEBUG_NAMES;
f8c41851 6185 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6186 per_objfile->resize_symtabs ();
17ee85fc
TT
6187 return true;
6188 }
6189
6190 /* Was a GDB index already read when we processed an objfile sharing
6191 PER_BFD? */
6192 if (per_bfd->index_table != nullptr)
6193 {
6194 *index_kind = dw_index_kind::GDB_INDEX;
f8c41851 6195 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6196 per_objfile->resize_symtabs ();
17ee85fc
TT
6197 return true;
6198 }
6199
efb763a5
SM
6200 /* There might already be partial symtabs built for this BFD. This happens
6201 when loading the same binary twice with the index-cache enabled. If so,
6202 don't try to read an index. The objfile / per_objfile initialization will
6203 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6204 code path. */
6205 if (per_bfd->partial_symtabs != nullptr)
6206 return false;
6207
976ca316 6208 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a
PA
6209 {
6210 *index_kind = dw_index_kind::DEBUG_NAMES;
976ca316 6211 per_objfile->resize_symtabs ();
3c0aa29a
PA
6212 return true;
6213 }
927aa2e7 6214
976ca316 6215 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 6216 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 6217 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6218 {
6219 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6220 per_objfile->resize_symtabs ();
3c0aa29a
PA
6221 return true;
6222 }
9291a0cd 6223
87d6a7aa 6224 /* ... otherwise, try to find the index in the index cache. */
976ca316 6225 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
6226 get_gdb_index_contents_from_cache,
6227 get_gdb_index_contents_from_cache_dwz))
6228 {
6229 global_index_cache.hit ();
6230 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6231 per_objfile->resize_symtabs ();
87d6a7aa
SM
6232 return true;
6233 }
6234
6235 global_index_cache.miss ();
3c0aa29a 6236 return false;
9291a0cd
TT
6237}
6238
6239\f
6240
dce234bc
PP
6241/* Build a partial symbol table. */
6242
6243void
f29dff0a 6244dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6245{
976ca316
SM
6246 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6247 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
6248
6249 if (per_bfd->partial_symtabs != nullptr)
6250 {
6251 /* Partial symbols were already read, so now we can simply
6252 attach them. */
6253 objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6254 per_objfile->resize_symtabs ();
17ee85fc
TT
6255 return;
6256 }
c9bf0622 6257
a70b8144 6258 try
c9bf0622
TT
6259 {
6260 /* This isn't really ideal: all the data we allocate on the
6261 objfile's obstack is still uselessly kept around. However,
6262 freeing it seems unsafe. */
906768f9 6263 psymtab_discarder psymtabs (objfile);
976ca316 6264 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 6265 psymtabs.keep ();
87d6a7aa 6266
976ca316 6267 per_objfile->resize_symtabs ();
af758d11 6268
87d6a7aa 6269 /* (maybe) store an index in the cache. */
976ca316 6270 global_index_cache.store (per_objfile);
c9bf0622 6271 }
230d2906 6272 catch (const gdb_exception_error &except)
492d29ea
PA
6273 {
6274 exception_print (gdb_stderr, except);
6275 }
17ee85fc
TT
6276
6277 /* Finish by setting the local reference to partial symtabs, so that
6278 we don't try to read them again if reading another objfile with the same
6279 BFD. If we can't in fact share, this won't make a difference anyway as
6280 the dwarf2_per_bfd object won't be shared. */
6281 per_bfd->partial_symtabs = objfile->partial_symtabs;
c906108c 6282}
c906108c 6283
3b80fe9b
DE
6284/* Find the base address of the compilation unit for range lists and
6285 location lists. It will normally be specified by DW_AT_low_pc.
6286 In DWARF-3 draft 4, the base address could be overridden by
6287 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6288 compilation units with discontinuous ranges. */
6289
6290static void
6291dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6292{
6293 struct attribute *attr;
6294
2b24b6e4 6295 cu->base_address.reset ();
3b80fe9b
DE
6296
6297 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6298 if (attr != nullptr)
95f982e5 6299 cu->base_address = attr->as_address ();
3b80fe9b
DE
6300 else
6301 {
6302 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6303 if (attr != nullptr)
95f982e5 6304 cu->base_address = attr->as_address ();
3b80fe9b
DE
6305 }
6306}
6307
36586728
TT
6308/* Helper function that returns the proper abbrev section for
6309 THIS_CU. */
6310
6311static struct dwarf2_section_info *
6312get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6313{
6314 struct dwarf2_section_info *abbrev;
c3699833 6315 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6316
6317 if (this_cu->is_dwz)
c3699833 6318 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6319 else
c3699833 6320 abbrev = &per_bfd->abbrev;
36586728
TT
6321
6322 return abbrev;
6323}
6324
f4dc4d17
DE
6325/* Fetch the abbreviation table offset from a comp or type unit header. */
6326
6327static sect_offset
976ca316 6328read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 6329 struct dwarf2_section_info *section,
9c541725 6330 sect_offset sect_off)
f4dc4d17 6331{
96b79293 6332 bfd *abfd = section->get_bfd_owner ();
d521ce57 6333 const gdb_byte *info_ptr;
ac298888 6334 unsigned int initial_length_size, offset_size;
43988095 6335 uint16_t version;
f4dc4d17 6336
976ca316 6337 section->read (per_objfile->objfile);
9c541725 6338 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6339 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6340 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6341 info_ptr += initial_length_size;
6342
6343 version = read_2_bytes (abfd, info_ptr);
6344 info_ptr += 2;
6345 if (version >= 5)
6346 {
6347 /* Skip unit type and address size. */
6348 info_ptr += 2;
6349 }
6350
24aa364d 6351 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6352}
6353
b83470bf
TT
6354/* A partial symtab that is used only for include files. */
6355struct dwarf2_include_psymtab : public partial_symtab
6356{
6357 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6358 : partial_symtab (filename, objfile)
6359 {
6360 }
6361
6362 void read_symtab (struct objfile *objfile) override
6363 {
194d088f
TV
6364 /* It's an include file, no symbols to read for it.
6365 Everything is in the includer symtab. */
6366
6367 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6368 expansion of the includer psymtab. We use the dependencies[0] field to
6369 model the includer. But if we go the regular route of calling
6370 expand_psymtab here, and having expand_psymtab call expand_dependencies
6371 to expand the includer, we'll only use expand_psymtab on the includer
6372 (making it a non-toplevel psymtab), while if we expand the includer via
6373 another path, we'll use read_symtab (making it a toplevel psymtab).
6374 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6375 psymtab, and trigger read_symtab on the includer here directly. */
6376 includer ()->read_symtab (objfile);
b83470bf
TT
6377 }
6378
6379 void expand_psymtab (struct objfile *objfile) override
6380 {
194d088f
TV
6381 /* This is not called by read_symtab, and should not be called by any
6382 expand_dependencies. */
6383 gdb_assert (false);
b83470bf
TT
6384 }
6385
5717c425 6386 bool readin_p (struct objfile *objfile) const override
b83470bf 6387 {
5717c425 6388 return includer ()->readin_p (objfile);
b83470bf
TT
6389 }
6390
5717c425 6391 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6392 {
6393 return nullptr;
6394 }
6395
6396private:
194d088f
TV
6397 partial_symtab *includer () const
6398 {
6399 /* An include psymtab has exactly one dependency: the psymtab that
6400 includes it. */
6401 gdb_assert (this->number_of_dependencies == 1);
6402 return this->dependencies[0];
6403 }
b83470bf
TT
6404};
6405
aaa75496
JB
6406/* Allocate a new partial symtab for file named NAME and mark this new
6407 partial symtab as being an include of PST. */
6408
6409static void
891813be 6410dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
dda83cd7 6411 struct objfile *objfile)
aaa75496 6412{
b83470bf 6413 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6414
fbd9ab74 6415 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6416 subpst->dirname = pst->dirname;
fbd9ab74 6417
a9342b62 6418 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6419 subpst->dependencies[0] = pst;
6420 subpst->number_of_dependencies = 1;
aaa75496
JB
6421}
6422
6423/* Read the Line Number Program data and extract the list of files
6424 included by the source file represented by PST. Build an include
d85a05f0 6425 partial symtab for each of these included files. */
aaa75496
JB
6426
6427static void
6428dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6429 struct die_info *die,
891813be 6430 dwarf2_psymtab *pst)
aaa75496 6431{
fff8551c 6432 line_header_up lh;
d85a05f0 6433 struct attribute *attr;
aaa75496 6434
d85a05f0 6435 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e
TT
6436 if (attr != nullptr && attr->form_is_unsigned ())
6437 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
aaa75496
JB
6438 if (lh == NULL)
6439 return; /* No linetable, so no includes. */
6440
79748972
TT
6441 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6442 that we pass in the raw text_low here; that is ok because we're
6443 only decoding the line table to make include partial symtabs, and
6444 so the addresses aren't really used. */
4ae976d1 6445 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6446 pst->raw_text_low (), 1);
aaa75496
JB
6447}
6448
348e048f 6449static hashval_t
52dc124a 6450hash_signatured_type (const void *item)
348e048f 6451{
9a3c8263
SM
6452 const struct signatured_type *sig_type
6453 = (const struct signatured_type *) item;
9a619af0 6454
348e048f 6455 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6456 return sig_type->signature;
348e048f
DE
6457}
6458
6459static int
52dc124a 6460eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6461{
9a3c8263
SM
6462 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6463 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6464
348e048f
DE
6465 return lhs->signature == rhs->signature;
6466}
6467
1fd400ff
TT
6468/* Allocate a hash table for signatured types. */
6469
b0b6a987 6470static htab_up
298e9637 6471allocate_signatured_type_table ()
1fd400ff 6472{
b0b6a987
TT
6473 return htab_up (htab_create_alloc (41,
6474 hash_signatured_type,
6475 eq_signatured_type,
6476 NULL, xcalloc, xfree));
1fd400ff
TT
6477}
6478
d467dd73 6479/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6480
6481static int
d467dd73 6482add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6483{
9a3c8263 6484 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6485 std::vector<signatured_type *> *all_type_units
6486 = (std::vector<signatured_type *> *) datum;
1fd400ff 6487
b2bdb8cf 6488 all_type_units->push_back (sigt);
1fd400ff
TT
6489
6490 return 1;
6491}
6492
78d4d2c5 6493/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6494 and fill them into TYPES_HTAB. It will process only type units,
6495 therefore DW_UT_type. */
c88ee1f0 6496
78d4d2c5 6497static void
976ca316 6498create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6499 struct dwo_file *dwo_file,
b0b6a987 6500 dwarf2_section_info *section, htab_up &types_htab,
43988095 6501 rcuh_kind section_kind)
348e048f 6502{
976ca316 6503 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6504 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6505 bfd *abfd;
6506 const gdb_byte *info_ptr, *end_ptr;
348e048f 6507
4bdcc0c1
DE
6508 abbrev_section = (dwo_file != NULL
6509 ? &dwo_file->sections.abbrev
976ca316 6510 : &per_objfile->per_bfd->abbrev);
4bdcc0c1 6511
6f738b01
SM
6512 dwarf_read_debug_printf ("Reading %s for %s:",
6513 section->get_name (),
6514 abbrev_section->get_file_name ());
09406207 6515
96b79293 6516 section->read (objfile);
78d4d2c5 6517 info_ptr = section->buffer;
348e048f 6518
78d4d2c5
JK
6519 if (info_ptr == NULL)
6520 return;
348e048f 6521
78d4d2c5
JK
6522 /* We can't set abfd until now because the section may be empty or
6523 not present, in which case the bfd is unknown. */
96b79293 6524 abfd = section->get_bfd_owner ();
348e048f 6525
c0ab21c2
TT
6526 /* We don't use cutu_reader here because we don't need to read
6527 any dies: the signature is in the header. */
3019eac3 6528
78d4d2c5
JK
6529 end_ptr = info_ptr + section->size;
6530 while (info_ptr < end_ptr)
6531 {
78d4d2c5
JK
6532 struct signatured_type *sig_type;
6533 struct dwo_unit *dwo_tu;
6534 void **slot;
6535 const gdb_byte *ptr = info_ptr;
6536 struct comp_unit_head header;
6537 unsigned int length;
8b70b953 6538
9c541725 6539 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6540
a49dd8dd
JK
6541 /* Initialize it due to a false compiler warning. */
6542 header.signature = -1;
9c541725 6543 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6544
78d4d2c5
JK
6545 /* We need to read the type's signature in order to build the hash
6546 table, but we don't need anything else just yet. */
348e048f 6547
976ca316 6548 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6549 abbrev_section, ptr, section_kind);
348e048f 6550
4057dfde 6551 length = header.get_length ();
6caca83c 6552
78d4d2c5
JK
6553 /* Skip dummy type units. */
6554 if (ptr >= info_ptr + length
43988095 6555 || peek_abbrev_code (abfd, ptr) == 0
d2854d8d
CT
6556 || (header.unit_type != DW_UT_type
6557 && header.unit_type != DW_UT_split_type))
78d4d2c5
JK
6558 {
6559 info_ptr += length;
6560 continue;
6561 }
dee91e82 6562
78d4d2c5
JK
6563 if (types_htab == NULL)
6564 {
6565 if (dwo_file)
298e9637 6566 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6567 else
298e9637 6568 types_htab = allocate_signatured_type_table ();
78d4d2c5 6569 }
8b70b953 6570
78d4d2c5
JK
6571 if (dwo_file)
6572 {
6573 sig_type = NULL;
976ca316 6574 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
78d4d2c5 6575 dwo_tu->dwo_file = dwo_file;
43988095 6576 dwo_tu->signature = header.signature;
9c541725 6577 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6578 dwo_tu->section = section;
9c541725 6579 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6580 dwo_tu->length = length;
6581 }
6582 else
6583 {
6584 /* N.B.: type_offset is not usable if this type uses a DWO file.
6585 The real type_offset is in the DWO file. */
6586 dwo_tu = NULL;
976ca316 6587 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
43988095 6588 sig_type->signature = header.signature;
9c541725 6589 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6590 sig_type->per_cu.is_debug_types = 1;
6591 sig_type->per_cu.section = section;
9c541725 6592 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6593 sig_type->per_cu.length = length;
6594 }
6595
b0b6a987 6596 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6597 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6598 INSERT);
6599 gdb_assert (slot != NULL);
6600 if (*slot != NULL)
6601 {
9c541725 6602 sect_offset dup_sect_off;
0349ea22 6603
3019eac3
DE
6604 if (dwo_file)
6605 {
78d4d2c5
JK
6606 const struct dwo_unit *dup_tu
6607 = (const struct dwo_unit *) *slot;
6608
9c541725 6609 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6610 }
6611 else
6612 {
78d4d2c5
JK
6613 const struct signatured_type *dup_tu
6614 = (const struct signatured_type *) *slot;
6615
9c541725 6616 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6617 }
8b70b953 6618
b98664d3 6619 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6620 " the entry at offset %s, signature %s"),
6621 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6622 hex_string (header.signature));
78d4d2c5
JK
6623 }
6624 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6625
6f738b01
SM
6626 dwarf_read_debug_printf_v (" offset %s, signature %s",
6627 sect_offset_str (sect_off),
6628 hex_string (header.signature));
3019eac3 6629
78d4d2c5
JK
6630 info_ptr += length;
6631 }
6632}
3019eac3 6633
78d4d2c5
JK
6634/* Create the hash table of all entries in the .debug_types
6635 (or .debug_types.dwo) section(s).
6636 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6637 otherwise it is NULL.
b3c8eb43 6638
78d4d2c5 6639 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6640
78d4d2c5 6641 Note: This function processes DWO files only, not DWP files. */
348e048f 6642
78d4d2c5 6643static void
976ca316 6644create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6645 struct dwo_file *dwo_file,
fd5866f6 6646 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6647 htab_up &types_htab)
78d4d2c5 6648{
fd5866f6 6649 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6650 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6651 rcuh_kind::TYPE);
3019eac3
DE
6652}
6653
6654/* Create the hash table of all entries in the .debug_types section,
6655 and initialize all_type_units.
6656 The result is zero if there is an error (e.g. missing .debug_types section),
6657 otherwise non-zero. */
6658
6659static int
976ca316 6660create_all_type_units (dwarf2_per_objfile *per_objfile)
3019eac3 6661{
b0b6a987 6662 htab_up types_htab;
3019eac3 6663
976ca316
SM
6664 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6665 types_htab, rcuh_kind::COMPILE);
6666 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6667 types_htab);
3019eac3
DE
6668 if (types_htab == NULL)
6669 {
976ca316 6670 per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6671 return 0;
6672 }
6673
976ca316 6674 per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6675
976ca316
SM
6676 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6677 per_objfile->per_bfd->all_type_units.reserve
6678 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6679
976ca316 6680 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6681 add_signatured_type_cu_to_table,
976ca316 6682 &per_objfile->per_bfd->all_type_units);
1fd400ff 6683
348e048f
DE
6684 return 1;
6685}
6686
5989a64e 6687/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6688 If SLOT is non-NULL, it is the entry to use in the hash table.
6689 Otherwise we find one. */
6690
6691static struct signatured_type *
976ca316 6692add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6693{
976ca316
SM
6694 if (per_objfile->per_bfd->all_type_units.size ()
6695 == per_objfile->per_bfd->all_type_units.capacity ())
6696 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6697
976ca316 6698 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6699
976ca316 6700 per_objfile->resize_symtabs ();
af758d11 6701
976ca316 6702 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6703 sig_type->signature = sig;
6704 sig_type->per_cu.is_debug_types = 1;
976ca316 6705 if (per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6706 {
6707 sig_type->per_cu.v.quick =
976ca316 6708 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6709 struct dwarf2_per_cu_quick_data);
6710 }
6711
6712 if (slot == NULL)
6713 {
976ca316 6714 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6715 sig_type, INSERT);
6716 }
6717 gdb_assert (*slot == NULL);
6718 *slot = sig_type;
6719 /* The rest of sig_type must be filled in by the caller. */
6720 return sig_type;
6721}
6722
a2ce51a0
DE
6723/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6724 Fill in SIG_ENTRY with DWO_ENTRY. */
6725
6726static void
976ca316 6727fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6728 struct signatured_type *sig_entry,
6729 struct dwo_unit *dwo_entry)
6730{
976ca316 6731 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6732
7ee85ab1 6733 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6734 gdb_assert (! sig_entry->per_cu.queued);
976ca316 6735 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6736 if (per_bfd->using_index)
6aa5f3a6
DE
6737 {
6738 gdb_assert (sig_entry->per_cu.v.quick != NULL);
976ca316 6739 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6740 }
6741 else
6742 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6743 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6744 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6745 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6746 gdb_assert (sig_entry->dwo_unit == NULL);
6747
6748 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6749 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6750 sig_entry->per_cu.length = dwo_entry->length;
6751 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6752 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6753 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6754 sig_entry->dwo_unit = dwo_entry;
6755}
6756
6757/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6758 If we haven't read the TU yet, create the signatured_type data structure
6759 for a TU to be read in directly from a DWO file, bypassing the stub.
6760 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6761 using .gdb_index, then when reading a CU we want to stay in the DWO file
6762 containing that CU. Otherwise we could end up reading several other DWO
6763 files (due to comdat folding) to process the transitive closure of all the
6764 mentioned TUs, and that can be slow. The current DWO file will have every
6765 type signature that it needs.
a2ce51a0
DE
6766 We only do this for .gdb_index because in the psymtab case we already have
6767 to read all the DWOs to build the type unit groups. */
6768
6769static struct signatured_type *
6770lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6771{
976ca316 6772 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6773 struct dwo_file *dwo_file;
6774 struct dwo_unit find_dwo_entry, *dwo_entry;
6775 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6776 void **slot;
a2ce51a0 6777
976ca316 6778 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6779
6aa5f3a6
DE
6780 /* If TU skeletons have been removed then we may not have read in any
6781 TUs yet. */
976ca316
SM
6782 if (per_objfile->per_bfd->signatured_types == NULL)
6783 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6784
6785 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6786 Use the global signatured_types array to do our own comdat-folding
6787 of types. If this is the first time we're reading this TU, and
6788 the TU has an entry in .gdb_index, replace the recorded data from
6789 .gdb_index with this TU. */
a2ce51a0 6790
a2ce51a0 6791 find_sig_entry.signature = sig;
976ca316 6792 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6793 &find_sig_entry, INSERT);
9a3c8263 6794 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6795
6796 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6797 read. Don't reassign the global entry to point to this DWO if that's
6798 the case. Also note that if the TU is already being read, it may not
6799 have come from a DWO, the program may be a mix of Fission-compiled
6800 code and non-Fission-compiled code. */
6801
6802 /* Have we already tried to read this TU?
6803 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6804 needn't exist in the global table yet). */
6805 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6806 return sig_entry;
6807
6aa5f3a6
DE
6808 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6809 dwo_unit of the TU itself. */
6810 dwo_file = cu->dwo_unit->dwo_file;
6811
a2ce51a0
DE
6812 /* Ok, this is the first time we're reading this TU. */
6813 if (dwo_file->tus == NULL)
6814 return NULL;
6815 find_dwo_entry.signature = sig;
b0b6a987
TT
6816 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6817 &find_dwo_entry);
a2ce51a0
DE
6818 if (dwo_entry == NULL)
6819 return NULL;
6820
6aa5f3a6
DE
6821 /* If the global table doesn't have an entry for this TU, add one. */
6822 if (sig_entry == NULL)
976ca316 6823 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6824
976ca316 6825 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
89e63ee4 6826 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6827 return sig_entry;
6828}
6829
a2ce51a0
DE
6830/* Subroutine of lookup_signatured_type.
6831 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6832 then try the DWP file. If the TU stub (skeleton) has been removed then
6833 it won't be in .gdb_index. */
a2ce51a0
DE
6834
6835static struct signatured_type *
6836lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6837{
976ca316
SM
6838 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6839 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6840 struct dwo_unit *dwo_entry;
6841 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6842 void **slot;
a2ce51a0 6843
976ca316 6844 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6845 gdb_assert (dwp_file != NULL);
6846
6aa5f3a6
DE
6847 /* If TU skeletons have been removed then we may not have read in any
6848 TUs yet. */
976ca316
SM
6849 if (per_objfile->per_bfd->signatured_types == NULL)
6850 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6851
6aa5f3a6 6852 find_sig_entry.signature = sig;
976ca316 6853 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6854 &find_sig_entry, INSERT);
9a3c8263 6855 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6856
6857 /* Have we already tried to read this TU?
6858 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6859 needn't exist in the global table yet). */
6860 if (sig_entry != NULL)
6861 return sig_entry;
6862
a2ce51a0
DE
6863 if (dwp_file->tus == NULL)
6864 return NULL;
976ca316
SM
6865 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6866 1 /* is_debug_types */);
a2ce51a0
DE
6867 if (dwo_entry == NULL)
6868 return NULL;
6869
976ca316
SM
6870 sig_entry = add_type_unit (per_objfile, sig, slot);
6871 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6872
a2ce51a0
DE
6873 return sig_entry;
6874}
6875
380bca97 6876/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6877 Returns NULL if signature SIG is not present in the table.
6878 It is up to the caller to complain about this. */
348e048f
DE
6879
6880static struct signatured_type *
a2ce51a0 6881lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6882{
976ca316 6883 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6884
976ca316 6885 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6886 {
6887 /* We're in a DWO/DWP file, and we're using .gdb_index.
6888 These cases require special processing. */
976ca316 6889 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6890 return lookup_dwo_signatured_type (cu, sig);
6891 else
6892 return lookup_dwp_signatured_type (cu, sig);
6893 }
6894 else
6895 {
6896 struct signatured_type find_entry, *entry;
348e048f 6897
976ca316 6898 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6899 return NULL;
6900 find_entry.signature = sig;
9a3c8263 6901 entry = ((struct signatured_type *)
976ca316 6902 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6903 &find_entry));
a2ce51a0
DE
6904 return entry;
6905 }
348e048f 6906}
18a8505e 6907
42e7ad6c 6908/* Low level DIE reading support. */
348e048f 6909
d85a05f0
DJ
6910/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6911
6912static void
6913init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6914 struct dwarf2_cu *cu,
3019eac3 6915 struct dwarf2_section_info *section,
685af9cd
TT
6916 struct dwo_file *dwo_file,
6917 struct abbrev_table *abbrev_table)
d85a05f0 6918{
fceca515 6919 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6920 reader->abfd = section->get_bfd_owner ();
d85a05f0 6921 reader->cu = cu;
3019eac3 6922 reader->dwo_file = dwo_file;
dee91e82
DE
6923 reader->die_section = section;
6924 reader->buffer = section->buffer;
f664829e 6925 reader->buffer_end = section->buffer + section->size;
685af9cd 6926 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6927}
6928
c0ab21c2 6929/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6930 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6931 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6932 already.
6933
6934 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6935 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6936 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6937 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6938 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6939 STUB_COMP_DIR may be non-NULL.
3e225074 6940 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6941 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6942 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6943 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6944 kept around for at least as long as *RESULT_READER.
6945
b0c7bfa9
DE
6946 The result is non-zero if a valid (non-dummy) DIE was found. */
6947
6948static int
4ab09049 6949read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6950 struct dwo_unit *dwo_unit,
b0c7bfa9 6951 struct die_info *stub_comp_unit_die,
a2ce51a0 6952 const char *stub_comp_dir,
b0c7bfa9 6953 struct die_reader_specs *result_reader,
d521ce57 6954 const gdb_byte **result_info_ptr,
b0c7bfa9 6955 struct die_info **result_comp_unit_die,
685af9cd 6956 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6957{
976ca316 6958 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6959 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6960 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6961 bfd *abfd;
d521ce57 6962 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6963 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6964 int i,num_extra_attrs;
6965 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6966 struct die_info *comp_unit_die;
6967
b0aeadb3
DE
6968 /* At most one of these may be provided. */
6969 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6970
b0c7bfa9
DE
6971 /* These attributes aren't processed until later:
6972 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6973 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6974 referenced later. However, these attributes are found in the stub
6975 which we won't have later. In order to not impose this complication
6976 on the rest of the code, we read them here and copy them to the
6977 DWO CU/TU die. */
b0c7bfa9
DE
6978
6979 stmt_list = NULL;
6980 low_pc = NULL;
6981 high_pc = NULL;
6982 ranges = NULL;
6983 comp_dir = NULL;
6984
6985 if (stub_comp_unit_die != NULL)
6986 {
6987 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6988 DWO file. */
4ab09049 6989 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6990 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6991 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6992 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6993 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6994 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6995
a39fdb41 6996 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6997
2b0c7f41
SM
6998 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6999 We need the value before we can process DW_AT_ranges values from the
7000 DWO. */
7001 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
7002
7003 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
7004 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
7005 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
7006 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
7007 section. */
7008 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
b0c7bfa9 7009 }
a2ce51a0
DE
7010 else if (stub_comp_dir != NULL)
7011 {
7012 /* Reconstruct the comp_dir attribute to simplify the code below. */
fe56917a 7013 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7014 comp_dir->name = DW_AT_comp_dir;
7015 comp_dir->form = DW_FORM_string;
c6481205 7016 comp_dir->set_string_noncanonical (stub_comp_dir);
a2ce51a0 7017 }
b0c7bfa9
DE
7018
7019 /* Set up for reading the DWO CU/TU. */
7020 cu->dwo_unit = dwo_unit;
685af9cd 7021 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
7022 section->read (objfile);
7023 abfd = section->get_bfd_owner ();
9c541725
PA
7024 begin_info_ptr = info_ptr = (section->buffer
7025 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7026 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 7027
4ab09049 7028 if (per_cu->is_debug_types)
b0c7bfa9 7029 {
4ab09049 7030 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 7031
976ca316
SM
7032 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7033 section, dwo_abbrev_section,
43988095 7034 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7035 /* This is not an assert because it can be caused by bad debug info. */
43988095 7036 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7037 {
7038 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7039 " TU at offset %s [in module %s]"),
a2ce51a0 7040 hex_string (sig_type->signature),
43988095 7041 hex_string (cu->header.signature),
9d8780f0 7042 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7043 bfd_get_filename (abfd));
7044 }
9c541725 7045 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7046 /* For DWOs coming from DWP files, we don't know the CU length
7047 nor the type's offset in the TU until now. */
4057dfde 7048 dwo_unit->length = cu->header.get_length ();
9c541725 7049 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7050
7051 /* Establish the type offset that can be used to lookup the type.
7052 For DWO files, we don't know it until now. */
9c541725
PA
7053 sig_type->type_offset_in_section
7054 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7055 }
7056 else
7057 {
976ca316
SM
7058 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7059 section, dwo_abbrev_section,
43988095 7060 info_ptr, rcuh_kind::COMPILE);
9c541725 7061 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7062 /* For DWOs coming from DWP files, we don't know the CU length
7063 until now. */
4057dfde 7064 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
7065 }
7066
606decb2 7067 dwo_abbrev_section->read (objfile);
685af9cd 7068 *result_dwo_abbrev_table
606decb2 7069 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
685af9cd
TT
7070 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7071 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7072
7073 /* Read in the die, but leave space to copy over the attributes
7074 from the stub. This has the benefit of simplifying the rest of
7075 the code - all the work to maintain the illusion of a single
7076 DW_TAG_{compile,type}_unit DIE is done here. */
7077 num_extra_attrs = ((stmt_list != NULL)
7078 + (low_pc != NULL)
7079 + (high_pc != NULL)
7080 + (ranges != NULL)
7081 + (comp_dir != NULL));
7082 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 7083 num_extra_attrs);
b0c7bfa9
DE
7084
7085 /* Copy over the attributes from the stub to the DIE we just read in. */
7086 comp_unit_die = *result_comp_unit_die;
7087 i = comp_unit_die->num_attrs;
7088 if (stmt_list != NULL)
7089 comp_unit_die->attrs[i++] = *stmt_list;
7090 if (low_pc != NULL)
7091 comp_unit_die->attrs[i++] = *low_pc;
7092 if (high_pc != NULL)
7093 comp_unit_die->attrs[i++] = *high_pc;
7094 if (ranges != NULL)
7095 comp_unit_die->attrs[i++] = *ranges;
7096 if (comp_dir != NULL)
7097 comp_unit_die->attrs[i++] = *comp_dir;
7098 comp_unit_die->num_attrs += num_extra_attrs;
7099
b4f54984 7100 if (dwarf_die_debug)
bf6af496
DE
7101 {
7102 fprintf_unfiltered (gdb_stdlog,
7103 "Read die from %s@0x%x of %s:\n",
96b79293 7104 section->get_name (),
bf6af496
DE
7105 (unsigned) (begin_info_ptr - section->buffer),
7106 bfd_get_filename (abfd));
b4f54984 7107 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7108 }
7109
b0c7bfa9
DE
7110 /* Skip dummy compilation units. */
7111 if (info_ptr >= begin_info_ptr + dwo_unit->length
7112 || peek_abbrev_code (abfd, info_ptr) == 0)
7113 return 0;
7114
7115 *result_info_ptr = info_ptr;
7116 return 1;
7117}
7118
a084a2a6
AT
7119/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7120 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7121 signature is part of the header. */
7122static gdb::optional<ULONGEST>
7123lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7124{
7125 if (cu->header.version >= 5)
7126 return cu->header.signature;
7127 struct attribute *attr;
7128 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
529908cb 7129 if (attr == nullptr || !attr->form_is_unsigned ())
a084a2a6 7130 return gdb::optional<ULONGEST> ();
529908cb 7131 return attr->as_unsigned ();
a084a2a6
AT
7132}
7133
c0ab21c2 7134/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 7135 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7136 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7137
7138static struct dwo_unit *
4ab09049 7139lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 7140{
4ab09049 7141 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 7142 struct dwo_unit *dwo_unit;
c0ab21c2 7143 const char *comp_dir;
b0c7bfa9 7144
a2ce51a0
DE
7145 gdb_assert (cu != NULL);
7146
b0c7bfa9 7147 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7148 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7149 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 7150
4ab09049
SM
7151 if (per_cu->is_debug_types)
7152 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
7153 else
7154 {
a084a2a6 7155 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 7156
a084a2a6 7157 if (!signature.has_value ())
b0c7bfa9
DE
7158 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7159 " [in module %s]"),
4ab09049
SM
7160 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7161
7162 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
7163 }
7164
b0c7bfa9
DE
7165 return dwo_unit;
7166}
7167
c0ab21c2 7168/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 7169 See it for a description of the parameters.
fcd3b13d 7170 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 7171
c0ab21c2 7172void
9e021579
SM
7173cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7174 dwarf2_per_objfile *per_objfile,
2e671100 7175 dwarf2_cu *existing_cu)
a2ce51a0 7176{
a2ce51a0 7177 struct signatured_type *sig_type;
a2ce51a0
DE
7178
7179 /* Verify we can do the following downcast, and that we have the
7180 data we need. */
7181 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7182 sig_type = (struct signatured_type *) this_cu;
7183 gdb_assert (sig_type->dwo_unit != NULL);
7184
2e671100
SM
7185 dwarf2_cu *cu;
7186
7187 if (existing_cu != nullptr)
6aa5f3a6 7188 {
2e671100
SM
7189 cu = existing_cu;
7190 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 7191 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 7192 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
7193 }
7194 else
7195 {
7188ed02 7196 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 7197 in per_objfile yet. */
7188ed02 7198 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 7199 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 7200 cu = m_new_cu.get ();
6aa5f3a6
DE
7201 }
7202
7203 /* A future optimization, if needed, would be to use an existing
7204 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7205 could share abbrev tables. */
a2ce51a0 7206
2e671100 7207 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
7208 NULL /* stub_comp_unit_die */,
7209 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 7210 this, &info_ptr,
3e225074 7211 &comp_unit_die,
c0ab21c2 7212 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7213 {
7214 /* Dummy die. */
c0ab21c2 7215 dummy_p = true;
a2ce51a0 7216 }
a2ce51a0
DE
7217}
7218
fd820528 7219/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7220 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7221
f4dc4d17
DE
7222 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7223 Otherwise the table specified in the comp unit header is read in and used.
7224 This is an optimization for when we already have the abbrev table.
7225
2e671100
SM
7226 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7227 allocated. */
aaa75496 7228
ab432490 7229cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7230 dwarf2_per_objfile *per_objfile,
c0ab21c2 7231 struct abbrev_table *abbrev_table,
2e671100 7232 dwarf2_cu *existing_cu,
c0ab21c2
TT
7233 bool skip_partial)
7234 : die_reader_specs {},
6751ebae 7235 m_this_cu (this_cu)
c906108c 7236{
976ca316 7237 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7238 struct dwarf2_section_info *section = this_cu->section;
96b79293 7239 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 7240 const gdb_byte *begin_info_ptr;
dee91e82 7241 struct signatured_type *sig_type = NULL;
4bdcc0c1 7242 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7243 /* Non-zero if CU currently points to a DWO file and we need to
7244 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7245 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7246 int rereading_dwo_cu = 0;
c906108c 7247
b4f54984 7248 if (dwarf_die_debug)
9d8780f0 7249 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7250 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7251 sect_offset_str (this_cu->sect_off));
09406207 7252
a2ce51a0
DE
7253 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7254 file (instead of going through the stub), short-circuit all of this. */
7255 if (this_cu->reading_dwo_directly)
7256 {
7257 /* Narrow down the scope of possibilities to have to understand. */
7258 gdb_assert (this_cu->is_debug_types);
7259 gdb_assert (abbrev_table == NULL);
976ca316 7260 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
7261 return;
7262 }
7263
dee91e82 7264 /* This is cheap if the section is already read in. */
96b79293 7265 section->read (objfile);
dee91e82 7266
9c541725 7267 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7268
7269 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7270
2e671100
SM
7271 dwarf2_cu *cu;
7272
7273 if (existing_cu != nullptr)
dee91e82 7274 {
2e671100 7275 cu = existing_cu;
42e7ad6c
DE
7276 /* If this CU is from a DWO file we need to start over, we need to
7277 refetch the attributes from the skeleton CU.
7278 This could be optimized by retrieving those attributes from when we
7279 were here the first time: the previous comp_unit_die was stored in
7280 comp_unit_obstack. But there's no data yet that we need this
7281 optimization. */
7282 if (cu->dwo_unit != NULL)
7283 rereading_dwo_cu = 1;
dee91e82
DE
7284 }
7285 else
7286 {
7188ed02 7287 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 7288 in per_objfile yet. */
976ca316
SM
7289 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7290 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 7291 cu = m_new_cu.get ();
42e7ad6c 7292 }
dee91e82 7293
b0c7bfa9 7294 /* Get the header. */
9c541725 7295 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7296 {
7297 /* We already have the header, there's no need to read it in again. */
9c541725 7298 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7299 }
7300 else
7301 {
3019eac3 7302 if (this_cu->is_debug_types)
dee91e82 7303 {
976ca316
SM
7304 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7305 section, abbrev_section,
7306 info_ptr, rcuh_kind::TYPE);
dee91e82 7307
42e7ad6c
DE
7308 /* Since per_cu is the first member of struct signatured_type,
7309 we can go from a pointer to one to a pointer to the other. */
7310 sig_type = (struct signatured_type *) this_cu;
43988095 7311 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7312 gdb_assert (sig_type->type_offset_in_tu
7313 == cu->header.type_cu_offset_in_tu);
7314 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7315
42e7ad6c
DE
7316 /* LENGTH has not been set yet for type units if we're
7317 using .gdb_index. */
4057dfde 7318 this_cu->length = cu->header.get_length ();
3019eac3
DE
7319
7320 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7321 sig_type->type_offset_in_section =
7322 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7323
7324 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7325 }
7326 else
7327 {
976ca316
SM
7328 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7329 section, abbrev_section,
43988095
JK
7330 info_ptr,
7331 rcuh_kind::COMPILE);
dee91e82 7332
9c541725 7333 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7334 if (this_cu->length == 0)
7335 this_cu->length = cu->header.get_length ();
7336 else
7337 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7338 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7339 }
7340 }
10b3939b 7341
6caca83c 7342 /* Skip dummy compilation units. */
dee91e82 7343 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7344 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7345 {
7346 dummy_p = true;
7347 return;
7348 }
6caca83c 7349
433df2d4
DE
7350 /* If we don't have them yet, read the abbrevs for this compilation unit.
7351 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7352 done. */
f4dc4d17 7353 if (abbrev_table != NULL)
685af9cd
TT
7354 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7355 else
f4dc4d17 7356 {
606decb2 7357 abbrev_section->read (objfile);
c0ab21c2 7358 m_abbrev_table_holder
606decb2 7359 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
c0ab21c2 7360 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7361 }
af703f96 7362
dee91e82 7363 /* Read the top level CU/TU die. */
c0ab21c2 7364 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7365 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7366
58f0c718 7367 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7368 {
7369 dummy_p = true;
7370 return;
7371 }
58f0c718 7372
b0c7bfa9 7373 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7374 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7375 table from the DWO file and pass the ownership over to us. It will be
7376 referenced from READER, so we must make sure to free it after we're done
7377 with READER.
7378
b0c7bfa9
DE
7379 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7380 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7381 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7382 if (dwo_name != nullptr)
3019eac3 7383 {
3019eac3 7384 struct dwo_unit *dwo_unit;
b0c7bfa9 7385 struct die_info *dwo_comp_unit_die;
3019eac3 7386
3e225074 7387 if (comp_unit_die->has_children)
6a506a2d 7388 {
b98664d3 7389 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7390 " has children (offset %s) [in module %s]"),
7391 sect_offset_str (this_cu->sect_off),
7392 bfd_get_filename (abfd));
6a506a2d 7393 }
4ab09049 7394 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7395 if (dwo_unit != NULL)
3019eac3 7396 {
4ab09049 7397 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7398 comp_unit_die, NULL,
c0ab21c2 7399 this, &info_ptr,
3e225074 7400 &dwo_comp_unit_die,
c0ab21c2 7401 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7402 {
7403 /* Dummy die. */
c0ab21c2 7404 dummy_p = true;
6a506a2d
DE
7405 return;
7406 }
7407 comp_unit_die = dwo_comp_unit_die;
7408 }
7409 else
7410 {
7411 /* Yikes, we couldn't find the rest of the DIE, we only have
7412 the stub. A complaint has already been logged. There's
7413 not much more we can do except pass on the stub DIE to
7414 die_reader_func. We don't want to throw an error on bad
7415 debug info. */
3019eac3
DE
7416 }
7417 }
c0ab21c2 7418}
3019eac3 7419
6751ebae
TT
7420void
7421cutu_reader::keep ()
c0ab21c2 7422{
b0c7bfa9 7423 /* Done, clean up. */
6751ebae
TT
7424 gdb_assert (!dummy_p);
7425 if (m_new_cu != NULL)
348e048f 7426 {
7188ed02 7427 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
dda83cd7 7428 now. */
7188ed02
SM
7429 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7430 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 7431 }
dee91e82
DE
7432}
7433
18a8505e
AT
7434/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7435 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7436 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7437
7438 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7439 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7440
7441 We fill in THIS_CU->length.
7442
dee91e82 7443 THIS_CU->cu is always freed when done.
3019eac3 7444 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7445 to care whether it refers to the "main" CU or the DWO CU.
7446
7447 When parent_cu is passed, it is used to provide a default value for
7448 str_offsets_base and addr_base from the parent. */
dee91e82 7449
ab432490 7450cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7451 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
7452 struct dwarf2_cu *parent_cu,
7453 struct dwo_file *dwo_file)
7454 : die_reader_specs {},
7455 m_this_cu (this_cu)
dee91e82 7456{
976ca316 7457 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7458 struct dwarf2_section_info *section = this_cu->section;
96b79293 7459 bfd *abfd = section->get_bfd_owner ();
33e80786 7460 struct dwarf2_section_info *abbrev_section;
d521ce57 7461 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7462
b4f54984 7463 if (dwarf_die_debug)
9d8780f0 7464 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7465 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7466 sect_offset_str (this_cu->sect_off));
09406207 7467
976ca316 7468 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7469
33e80786
DE
7470 abbrev_section = (dwo_file != NULL
7471 ? &dwo_file->sections.abbrev
7472 : get_abbrev_section_for_cu (this_cu));
7473
dee91e82 7474 /* This is cheap if the section is already read in. */
96b79293 7475 section->read (objfile);
dee91e82 7476
976ca316 7477 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 7478
9c541725 7479 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
7480 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7481 section, abbrev_section, info_ptr,
43988095
JK
7482 (this_cu->is_debug_types
7483 ? rcuh_kind::TYPE
7484 : rcuh_kind::COMPILE));
dee91e82 7485
18a8505e
AT
7486 if (parent_cu != nullptr)
7487 {
c0ab21c2
TT
7488 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7489 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7490 }
4057dfde 7491 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7492
7493 /* Skip dummy compilation units. */
7494 if (info_ptr >= begin_info_ptr + this_cu->length
7495 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7496 {
7497 dummy_p = true;
7498 return;
7499 }
72bf9492 7500
606decb2 7501 abbrev_section->read (objfile);
c0ab21c2 7502 m_abbrev_table_holder
606decb2 7503 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
dee91e82 7504
c0ab21c2
TT
7505 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7506 m_abbrev_table_holder.get ());
3e225074 7507 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7508}
7509
0018ea6f
DE
7510\f
7511/* Type Unit Groups.
dee91e82 7512
0018ea6f
DE
7513 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7514 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7515 so that all types coming from the same compilation (.o file) are grouped
7516 together. A future step could be to put the types in the same symtab as
7517 the CU the types ultimately came from. */
ff013f42 7518
f4dc4d17
DE
7519static hashval_t
7520hash_type_unit_group (const void *item)
7521{
9a3c8263
SM
7522 const struct type_unit_group *tu_group
7523 = (const struct type_unit_group *) item;
f4dc4d17 7524
094b34ac 7525 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7526}
348e048f
DE
7527
7528static int
f4dc4d17 7529eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7530{
9a3c8263
SM
7531 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7532 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7533
094b34ac 7534 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7535}
348e048f 7536
f4dc4d17
DE
7537/* Allocate a hash table for type unit groups. */
7538
eaa5fa8b 7539static htab_up
298e9637 7540allocate_type_unit_groups_table ()
f4dc4d17 7541{
eaa5fa8b
TT
7542 return htab_up (htab_create_alloc (3,
7543 hash_type_unit_group,
7544 eq_type_unit_group,
7545 NULL, xcalloc, xfree));
f4dc4d17 7546}
dee91e82 7547
f4dc4d17
DE
7548/* Type units that don't have DW_AT_stmt_list are grouped into their own
7549 partial symtabs. We combine several TUs per psymtab to not let the size
7550 of any one psymtab grow too big. */
7551#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7552#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7553
094b34ac 7554/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7555 Create the type_unit_group object used to hold one or more TUs. */
7556
7557static struct type_unit_group *
094b34ac 7558create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7559{
976ca316
SM
7560 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7561 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
094b34ac 7562 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7563 struct type_unit_group *tu_group;
f4dc4d17 7564
976ca316 7565 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
094b34ac 7566 per_cu = &tu_group->per_cu;
1859c670 7567 per_cu->per_bfd = per_bfd;
f4dc4d17 7568
1859c670 7569 if (per_bfd->using_index)
094b34ac 7570 {
1859c670 7571 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7572 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7573 }
7574 else
7575 {
9c541725 7576 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7577 dwarf2_psymtab *pst;
528e1572 7578 std::string name;
094b34ac
DE
7579
7580 /* Give the symtab a useful name for debug purposes. */
7581 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7582 name = string_printf ("<type_units_%d>",
7583 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7584 else
528e1572 7585 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7586
976ca316 7587 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
6d94535f 7588 pst->anonymous = true;
094b34ac 7589 }
f4dc4d17 7590
094b34ac 7591 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7592 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7593
7594 return tu_group;
7595}
7596
094b34ac
DE
7597/* Look up the type_unit_group for type unit CU, and create it if necessary.
7598 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7599
7600static struct type_unit_group *
ff39bb5e 7601get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7602{
976ca316
SM
7603 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7604 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7605 struct type_unit_group *tu_group;
7606 void **slot;
7607 unsigned int line_offset;
7608 struct type_unit_group type_unit_group_for_lookup;
7609
976ca316
SM
7610 if (per_objfile->per_bfd->type_unit_groups == NULL)
7611 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7612
7613 /* Do we need to create a new group, or can we use an existing one? */
7614
529908cb 7615 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
f4dc4d17 7616 {
529908cb 7617 line_offset = stmt_list->as_unsigned ();
f4dc4d17
DE
7618 ++tu_stats->nr_symtab_sharers;
7619 }
7620 else
7621 {
7622 /* Ugh, no stmt_list. Rare, but we have to handle it.
7623 We can do various things here like create one group per TU or
7624 spread them over multiple groups to split up the expansion work.
7625 To avoid worst case scenarios (too many groups or too large groups)
7626 we, umm, group them in bunches. */
7627 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7628 | (tu_stats->nr_stmt_less_type_units
7629 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7630 ++tu_stats->nr_stmt_less_type_units;
7631 }
7632
094b34ac 7633 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7634 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7635 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7636 &type_unit_group_for_lookup, INSERT);
7637 if (*slot != NULL)
7638 {
9a3c8263 7639 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7640 gdb_assert (tu_group != NULL);
7641 }
7642 else
7643 {
9c541725 7644 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7645 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7646 *slot = tu_group;
7647 ++tu_stats->nr_symtabs;
7648 }
7649
7650 return tu_group;
7651}
0018ea6f
DE
7652\f
7653/* Partial symbol tables. */
7654
7655/* Create a psymtab named NAME and assign it to PER_CU.
7656
7657 The caller must fill in the following details:
7658 dirname, textlow, texthigh. */
7659
891813be 7660static dwarf2_psymtab *
7aa104c4
SM
7661create_partial_symtab (dwarf2_per_cu_data *per_cu,
7662 dwarf2_per_objfile *per_objfile,
7663 const char *name)
0018ea6f 7664{
7aa104c4 7665 struct objfile *objfile = per_objfile->objfile;
891813be 7666 dwarf2_psymtab *pst;
0018ea6f 7667
9f4e76a4 7668 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7669
6d94535f 7670 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7671
7672 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7673 per_cu->v.psymtab = pst;
7674
7675 return pst;
7676}
7677
c0ab21c2 7678/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7679
7680static void
7681process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7682 const gdb_byte *info_ptr,
0018ea6f 7683 struct die_info *comp_unit_die,
c0ab21c2 7684 enum language pretend_language)
0018ea6f
DE
7685{
7686 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7687 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7688 struct objfile *objfile = per_objfile->objfile;
08feed99 7689 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7690 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7691 CORE_ADDR baseaddr;
7692 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7693 dwarf2_psymtab *pst;
3a2b436a 7694 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7695 const char *filename;
0018ea6f 7696
0018ea6f
DE
7697 gdb_assert (! per_cu->is_debug_types);
7698
c0ab21c2 7699 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7700
0018ea6f 7701 /* Allocate a new partial symbol table structure. */
2e927613
TV
7702 gdb::unique_xmalloc_ptr<char> debug_filename;
7703 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7704 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7705 if (filename == NULL)
0018ea6f 7706 filename = "";
2e927613
TV
7707 else if (strcmp (filename, artificial) == 0)
7708 {
7709 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7710 sect_offset_str (per_cu->sect_off),
7711 (char *) NULL));
2e927613
TV
7712 filename = debug_filename.get ();
7713 }
0018ea6f 7714
7aa104c4 7715 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7716
7717 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7718 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7719
b3b3bada 7720 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7721
7722 dwarf2_find_base_address (comp_unit_die, cu);
7723
7724 /* Possibly set the default values of LOWPC and HIGHPC from
7725 `DW_AT_ranges'. */
3a2b436a
JK
7726 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7727 &best_highpc, cu, pst);
7728 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7729 {
7730 CORE_ADDR low
7731 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7732 - baseaddr);
7733 CORE_ADDR high
7734 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7735 - baseaddr - 1);
7736 /* Store the contiguous range if it is not empty; it can be
7737 empty for CUs with no code. */
d320c2b5
TT
7738 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7739 low, high, pst);
79748972 7740 }
0018ea6f
DE
7741
7742 /* Check if comp unit has_children.
7743 If so, read the rest of the partial symbols from this comp unit.
7744 If not, there's no more debug_info for this comp unit. */
3e225074 7745 if (comp_unit_die->has_children)
0018ea6f
DE
7746 {
7747 struct partial_die_info *first_die;
7748 CORE_ADDR lowpc, highpc;
7749
7750 lowpc = ((CORE_ADDR) -1);
7751 highpc = ((CORE_ADDR) 0);
7752
7753 first_die = load_partial_dies (reader, info_ptr, 1);
7754
7755 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7756 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7757
7758 /* If we didn't find a lowpc, set it to highpc to avoid
7759 complaints from `maint check'. */
7760 if (lowpc == ((CORE_ADDR) -1))
7761 lowpc = highpc;
7762
7763 /* If the compilation unit didn't have an explicit address range,
7764 then use the information extracted from its child dies. */
e385593e 7765 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7766 {
7767 best_lowpc = lowpc;
7768 best_highpc = highpc;
7769 }
7770 }
4ae976d1 7771 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7772 best_lowpc + baseaddr)
7773 - baseaddr);
4ae976d1 7774 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7775 best_highpc + baseaddr)
7776 - baseaddr);
0018ea6f 7777
ae7754b2 7778 pst->end ();
0018ea6f 7779
ae640021 7780 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7781 {
7782 int i;
ae640021 7783 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7784
7785 /* Fill in 'dependencies' here; we fill in 'users' in a
7786 post-pass. */
7787 pst->number_of_dependencies = len;
a9342b62
TT
7788 pst->dependencies
7789 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7790 for (i = 0; i < len; ++i)
7791 {
7792 pst->dependencies[i]
7793 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7794 }
0018ea6f 7795
ae640021 7796 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7797 }
7798
7799 /* Get the list of files included in the current compilation unit,
7800 and build a psymtab for each of them. */
7801 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7802
6f738b01
SM
7803 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7804 ", %d global, %d static syms",
7805 per_cu->is_debug_types ? "type" : "comp",
7806 sect_offset_str (per_cu->sect_off),
7807 paddress (gdbarch, pst->text_low (objfile)),
7808 paddress (gdbarch, pst->text_high (objfile)),
7809 (int) pst->global_psymbols.size (),
7810 (int) pst->static_psymbols.size ());
0018ea6f
DE
7811}
7812
7813/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7814 Process compilation unit THIS_CU for a psymtab. */
7815
7816static void
ab432490
SM
7817process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7818 dwarf2_per_objfile *per_objfile,
135f5437 7819 bool want_partial_unit,
b93601f3 7820 enum language pretend_language)
0018ea6f
DE
7821{
7822 /* If this compilation unit was already read in, free the
7823 cached copy in order to read it in again. This is
7824 necessary because we skipped some symbols when we first
7825 read in the compilation unit (see load_partial_dies).
7826 This problem could be avoided, but the benefit is unclear. */
7188ed02 7827 per_objfile->remove_cu (this_cu);
0018ea6f 7828
2e671100 7829 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7830
58990295
TV
7831 switch (reader.comp_unit_die->tag)
7832 {
7833 case DW_TAG_compile_unit:
7834 this_cu->unit_type = DW_UT_compile;
7835 break;
7836 case DW_TAG_partial_unit:
7837 this_cu->unit_type = DW_UT_partial;
7838 break;
e77b0004
TV
7839 case DW_TAG_type_unit:
7840 this_cu->unit_type = DW_UT_type;
7841 break;
58990295
TV
7842 default:
7843 abort ();
7844 }
7845
c0ab21c2 7846 if (reader.dummy_p)
f1902523 7847 {
c0ab21c2 7848 /* Nothing. */
f1902523 7849 }
c0ab21c2 7850 else if (this_cu->is_debug_types)
3e225074
TT
7851 build_type_psymtabs_reader (&reader, reader.info_ptr,
7852 reader.comp_unit_die);
135f5437
TT
7853 else if (want_partial_unit
7854 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7855 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7856 reader.comp_unit_die,
c0ab21c2 7857 pretend_language);
0018ea6f 7858
7188ed02 7859 this_cu->lang = reader.cu->language;
58990295 7860
0018ea6f 7861 /* Age out any secondary CUs. */
7188ed02 7862 per_objfile->age_comp_units ();
0018ea6f 7863}
f4dc4d17
DE
7864
7865/* Reader function for build_type_psymtabs. */
7866
7867static void
7868build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7869 const gdb_byte *info_ptr,
3e225074 7870 struct die_info *type_unit_die)
f4dc4d17 7871{
976ca316 7872 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7873 struct dwarf2_cu *cu = reader->cu;
7874 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7875 struct signatured_type *sig_type;
f4dc4d17
DE
7876 struct type_unit_group *tu_group;
7877 struct attribute *attr;
7878 struct partial_die_info *first_die;
7879 CORE_ADDR lowpc, highpc;
891813be 7880 dwarf2_psymtab *pst;
f4dc4d17 7881
0186c6a7
DE
7882 gdb_assert (per_cu->is_debug_types);
7883 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7884
3e225074 7885 if (! type_unit_die->has_children)
f4dc4d17
DE
7886 return;
7887
052c8bb8 7888 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7889 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7890
df07e2c7 7891 if (tu_group->tus == nullptr)
a8b3b8e9 7892 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7893 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7894
7895 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7896 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7897 pst->anonymous = true;
f4dc4d17
DE
7898
7899 first_die = load_partial_dies (reader, info_ptr, 1);
7900
7901 lowpc = (CORE_ADDR) -1;
7902 highpc = (CORE_ADDR) 0;
7903 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7904
ae7754b2 7905 pst->end ();
f4dc4d17
DE
7906}
7907
73051182
DE
7908/* Struct used to sort TUs by their abbreviation table offset. */
7909
7910struct tu_abbrev_offset
7911{
b2bdb8cf
SM
7912 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7913 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7914 {}
7915
7916 signatured_type *sig_type;
73051182
DE
7917 sect_offset abbrev_offset;
7918};
7919
484cf504 7920/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7921
484cf504
TT
7922static bool
7923sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7924 const struct tu_abbrev_offset &b)
73051182 7925{
484cf504 7926 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7927}
7928
7929/* Efficiently read all the type units.
7930 This does the bulk of the work for build_type_psymtabs.
7931
7932 The efficiency is because we sort TUs by the abbrev table they use and
7933 only read each abbrev table once. In one program there are 200K TUs
7934 sharing 8K abbrev tables.
7935
7936 The main purpose of this function is to support building the
5989a64e 7937 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7938 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7939 can collapse the search space by grouping them by stmt_list.
7940 The savings can be significant, in the same program from above the 200K TUs
7941 share 8K stmt_list tables.
7942
7943 FUNC is expected to call get_type_unit_group, which will create the
7944 struct type_unit_group if necessary and add it to
5989a64e 7945 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7946
7947static void
976ca316 7948build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
73051182 7949{
976ca316 7950 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7951 abbrev_table_up abbrev_table;
73051182 7952 sect_offset abbrev_offset;
73051182
DE
7953
7954 /* It's up to the caller to not call us multiple times. */
976ca316 7955 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7956
976ca316 7957 if (per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7958 return;
7959
7960 /* TUs typically share abbrev tables, and there can be way more TUs than
7961 abbrev tables. Sort by abbrev table to reduce the number of times we
7962 read each abbrev table in.
7963 Alternatives are to punt or to maintain a cache of abbrev tables.
7964 This is simpler and efficient enough for now.
7965
7966 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7967 symtab to use). Typically TUs with the same abbrev offset have the same
7968 stmt_list value too so in practice this should work well.
7969
7970 The basic algorithm here is:
7971
7972 sort TUs by abbrev table
7973 for each TU with same abbrev table:
7974 read abbrev table if first user
7975 read TU top level DIE
7976 [IWBN if DWO skeletons had DW_AT_stmt_list]
7977 call FUNC */
7978
6f738b01 7979 dwarf_read_debug_printf ("Building type unit groups ...");
73051182
DE
7980
7981 /* Sort in a separate table to maintain the order of all_type_units
7982 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7983 std::vector<tu_abbrev_offset> sorted_by_abbrev;
976ca316 7984 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7985
976ca316 7986 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
b2bdb8cf 7987 sorted_by_abbrev.emplace_back
976ca316 7988 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
b2bdb8cf 7989 sig_type->per_cu.sect_off));
73051182 7990
484cf504
TT
7991 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7992 sort_tu_by_abbrev_offset);
73051182 7993
9c541725 7994 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7995
b2bdb8cf 7996 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7997 {
73051182
DE
7998 /* Switch to the next abbrev table if necessary. */
7999 if (abbrev_table == NULL
b2bdb8cf 8000 || tu.abbrev_offset != abbrev_offset)
73051182 8001 {
b2bdb8cf 8002 abbrev_offset = tu.abbrev_offset;
606decb2 8003 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
73051182 8004 abbrev_table =
606decb2 8005 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
8006 ++tu_stats->nr_uniq_abbrev_tables;
8007 }
8008
976ca316 8009 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
2e671100 8010 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
8011 if (!reader.dummy_p)
8012 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 8013 reader.comp_unit_die);
73051182 8014 }
6aa5f3a6 8015}
73051182 8016
6aa5f3a6
DE
8017/* Print collected type unit statistics. */
8018
8019static void
976ca316 8020print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 8021{
976ca316 8022 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6 8023
6f738b01
SM
8024 dwarf_read_debug_printf ("Type unit statistics:");
8025 dwarf_read_debug_printf (" %zu TUs",
8026 per_objfile->per_bfd->all_type_units.size ());
8027 dwarf_read_debug_printf (" %d uniq abbrev tables",
8028 tu_stats->nr_uniq_abbrev_tables);
8029 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
8030 tu_stats->nr_symtabs);
8031 dwarf_read_debug_printf (" %d symtab sharers",
8032 tu_stats->nr_symtab_sharers);
8033 dwarf_read_debug_printf (" %d type units without a stmt_list",
8034 tu_stats->nr_stmt_less_type_units);
8035 dwarf_read_debug_printf (" %d all_type_units reallocs",
8036 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8037}
8038
f4dc4d17
DE
8039/* Traversal function for build_type_psymtabs. */
8040
8041static int
8042build_type_psymtab_dependencies (void **slot, void *info)
8043{
976ca316
SM
8044 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
8045 struct objfile *objfile = per_objfile->objfile;
f4dc4d17 8046 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8047 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 8048 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 8049 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
8050 int i;
8051
8052 gdb_assert (len > 0);
197400e8 8053 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
8054
8055 pst->number_of_dependencies = len;
a9342b62 8056 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 8057 for (i = 0; i < len; ++i)
f4dc4d17 8058 {
df07e2c7 8059 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
8060 gdb_assert (iter->per_cu.is_debug_types);
8061 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8062 iter->type_unit_group = tu_group;
f4dc4d17
DE
8063 }
8064
df07e2c7
AB
8065 delete tu_group->tus;
8066 tu_group->tus = nullptr;
348e048f
DE
8067
8068 return 1;
8069}
8070
8071/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8072 Build partial symbol tables for the .debug_types comp-units. */
8073
8074static void
976ca316 8075build_type_psymtabs (dwarf2_per_objfile *per_objfile)
348e048f 8076{
976ca316 8077 if (! create_all_type_units (per_objfile))
348e048f
DE
8078 return;
8079
976ca316 8080 build_type_psymtabs_1 (per_objfile);
6aa5f3a6 8081}
f4dc4d17 8082
6aa5f3a6
DE
8083/* Traversal function for process_skeletonless_type_unit.
8084 Read a TU in a DWO file and build partial symbols for it. */
8085
8086static int
8087process_skeletonless_type_unit (void **slot, void *info)
8088{
8089 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 8090 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
8091 struct signatured_type find_entry, *entry;
8092
8093 /* If this TU doesn't exist in the global table, add it and read it in. */
8094
976ca316
SM
8095 if (per_objfile->per_bfd->signatured_types == NULL)
8096 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
8097
8098 find_entry.signature = dwo_unit->signature;
976ca316 8099 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 8100 &find_entry, INSERT);
6aa5f3a6
DE
8101 /* If we've already seen this type there's nothing to do. What's happening
8102 is we're doing our own version of comdat-folding here. */
8103 if (*slot != NULL)
8104 return 1;
8105
8106 /* This does the job that create_all_type_units would have done for
8107 this TU. */
976ca316
SM
8108 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
8109 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8110 *slot = entry;
8111
8112 /* This does the job that build_type_psymtabs_1 would have done. */
976ca316 8113 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
8114 if (!reader.dummy_p)
8115 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 8116 reader.comp_unit_die);
6aa5f3a6
DE
8117
8118 return 1;
8119}
8120
8121/* Traversal function for process_skeletonless_type_units. */
8122
8123static int
8124process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8125{
8126 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8127
8128 if (dwo_file->tus != NULL)
b0b6a987
TT
8129 htab_traverse_noresize (dwo_file->tus.get (),
8130 process_skeletonless_type_unit, info);
6aa5f3a6
DE
8131
8132 return 1;
8133}
8134
8135/* Scan all TUs of DWO files, verifying we've processed them.
8136 This is needed in case a TU was emitted without its skeleton.
8137 Note: This can't be done until we know what all the DWO files are. */
8138
8139static void
976ca316 8140process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
8141{
8142 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
8143 if (get_dwp_file (per_objfile) == NULL
8144 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 8145 {
976ca316 8146 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 8147 process_dwo_file_for_skeletonless_type_units,
976ca316 8148 per_objfile);
6aa5f3a6 8149 }
348e048f
DE
8150}
8151
ed2dc618 8152/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8153
8154static void
976ca316 8155set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 8156{
976ca316 8157 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 8158 {
891813be 8159 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 8160
36586728
TT
8161 if (pst == NULL)
8162 continue;
8163
b76e467d 8164 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8165 {
8166 /* Set the 'user' field only if it is not already set. */
8167 if (pst->dependencies[j]->user == NULL)
8168 pst->dependencies[j]->user = pst;
8169 }
8170 }
8171}
8172
93311388
DE
8173/* Build the partial symbol table by doing a quick pass through the
8174 .debug_info and .debug_abbrev sections. */
72bf9492 8175
93311388 8176static void
976ca316 8177dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 8178{
976ca316 8179 struct objfile *objfile = per_objfile->objfile;
93311388 8180
6f738b01
SM
8181 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8182 objfile_name (objfile));
45cfd468 8183
76935768 8184 scoped_restore restore_reading_psyms
976ca316 8185 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
76935768 8186 true);
98bfdba5 8187
976ca316 8188 per_objfile->per_bfd->info.read (objfile);
91c24f0a 8189
93311388
DE
8190 /* Any cached compilation units will be linked by the per-objfile
8191 read_in_chain. Make sure to free them when we're done. */
976ca316 8192 free_cached_comp_units freer (per_objfile);
72bf9492 8193
976ca316 8194 build_type_psymtabs (per_objfile);
348e048f 8195
976ca316 8196 create_all_comp_units (per_objfile);
c906108c 8197
60606b2c
TT
8198 /* Create a temporary address map on a temporary obstack. We later
8199 copy this to the final obstack. */
8268c778 8200 auto_obstack temp_obstack;
791afaa2
TT
8201
8202 scoped_restore save_psymtabs_addrmap
d320c2b5 8203 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8204 addrmap_create_mutable (&temp_obstack));
72bf9492 8205
976ca316 8206 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
8207 {
8208 if (per_cu->v.psymtab != NULL)
8209 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8210 continue;
976ca316 8211 process_psymtab_comp_unit (per_cu, per_objfile, false,
ab432490 8212 language_minimal);
3d5afab3 8213 }
ff013f42 8214
6aa5f3a6 8215 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 8216 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
8217
8218 /* Now that all TUs have been processed we can fill in the dependencies. */
976ca316 8219 if (per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 8220 {
976ca316
SM
8221 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8222 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
8223 }
8224
6f738b01 8225 if (dwarf_read_debug > 0)
976ca316 8226 print_tu_stats (per_objfile);
6aa5f3a6 8227
976ca316 8228 set_partial_user (per_objfile);
95554aad 8229
d320c2b5
TT
8230 objfile->partial_symtabs->psymtabs_addrmap
8231 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8232 objfile->partial_symtabs->obstack ());
791afaa2
TT
8233 /* At this point we want to keep the address map. */
8234 save_psymtabs_addrmap.release ();
ff013f42 8235
6f738b01
SM
8236 dwarf_read_debug_printf ("Done building psymtabs of %s",
8237 objfile_name (objfile));
ae038cb0
DJ
8238}
8239
dee91e82
DE
8240/* Load the partial DIEs for a secondary CU into memory.
8241 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8242
dee91e82 8243static void
ab432490 8244load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
8245 dwarf2_per_objfile *per_objfile,
8246 dwarf2_cu *existing_cu)
dee91e82 8247{
2e671100 8248 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
8249
8250 if (!reader.dummy_p)
8251 {
8252 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8253 language_minimal);
8254
8255 /* Check if comp unit has_children.
8256 If so, read the rest of the partial symbols from this comp unit.
8257 If not, there's no more debug_info for this comp unit. */
3e225074 8258 if (reader.comp_unit_die->has_children)
c0ab21c2 8259 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8260
8261 reader.keep ();
c0ab21c2 8262 }
ae038cb0
DJ
8263}
8264
ae038cb0 8265static void
976ca316 8266read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 8267 struct dwarf2_section_info *section,
f1902523 8268 struct dwarf2_section_info *abbrev_section,
b76e467d 8269 unsigned int is_dwz)
ae038cb0 8270{
d521ce57 8271 const gdb_byte *info_ptr;
976ca316 8272 struct objfile *objfile = per_objfile->objfile;
be391dca 8273
6f738b01
SM
8274 dwarf_read_debug_printf ("Reading %s for %s",
8275 section->get_name (),
8276 section->get_file_name ());
bf6af496 8277
96b79293 8278 section->read (objfile);
ae038cb0 8279
36586728 8280 info_ptr = section->buffer;
6e70227d 8281
36586728 8282 while (info_ptr < section->buffer + section->size)
ae038cb0 8283 {
ae038cb0 8284 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8285
9c541725 8286 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8287
f1902523 8288 comp_unit_head cu_header;
976ca316 8289 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618
SM
8290 abbrev_section, info_ptr,
8291 rcuh_kind::COMPILE);
ae038cb0
DJ
8292
8293 /* Save the compilation unit for later lookup. */
f1902523 8294 if (cu_header.unit_type != DW_UT_type)
976ca316 8295 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8296 else
8297 {
976ca316 8298 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8299 sig_type->signature = cu_header.signature;
8300 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8301 this_cu = &sig_type->per_cu;
8302 }
8303 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8304 this_cu->sect_off = sect_off;
f1902523 8305 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8306 this_cu->is_dwz = is_dwz;
8a0459fd 8307 this_cu->section = section;
ae038cb0 8308
976ca316 8309 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8310
8311 info_ptr = info_ptr + this_cu->length;
8312 }
36586728
TT
8313}
8314
8315/* Create a list of all compilation units in OBJFILE.
8316 This is only done for -readnow and building partial symtabs. */
8317
8318static void
976ca316 8319create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 8320{
976ca316
SM
8321 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8322 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8323 &per_objfile->per_bfd->abbrev, 0);
36586728 8324
976ca316 8325 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 8326 if (dwz != NULL)
976ca316 8327 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
c906108c
SS
8328}
8329
5734ee8b 8330/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8331 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8332 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8333 DW_AT_ranges). See the comments of add_partial_subprogram on how
8334 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8335
72bf9492
DJ
8336static void
8337scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8338 CORE_ADDR *highpc, int set_addrmap,
8339 struct dwarf2_cu *cu)
c906108c 8340{
72bf9492 8341 struct partial_die_info *pdi;
c906108c 8342
91c24f0a
DC
8343 /* Now, march along the PDI's, descending into ones which have
8344 interesting children but skipping the children of the other ones,
8345 until we reach the end of the compilation unit. */
c906108c 8346
72bf9492 8347 pdi = first_die;
91c24f0a 8348
72bf9492
DJ
8349 while (pdi != NULL)
8350 {
52356b79 8351 pdi->fixup (cu);
c906108c 8352
f55ee35c 8353 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8354 children, so we need to look at them. Ditto for anonymous
8355 enums. */
933c6fe4 8356
7d00ffec 8357 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8358 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8359 || pdi->tag == DW_TAG_imported_unit
8360 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8361 {
72bf9492 8362 switch (pdi->tag)
c906108c
SS
8363 {
8364 case DW_TAG_subprogram:
b1dc1806 8365 case DW_TAG_inlined_subroutine:
cdc07690 8366 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
8367 if (cu->language == language_cplus)
8368 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8369 set_addrmap, cu);
c906108c 8370 break;
72929c62 8371 case DW_TAG_constant:
c906108c
SS
8372 case DW_TAG_variable:
8373 case DW_TAG_typedef:
91c24f0a 8374 case DW_TAG_union_type:
317d2668
TV
8375 if (!pdi->is_declaration
8376 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8377 {
72bf9492 8378 add_partial_symbol (pdi, cu);
63d06c5c
DC
8379 }
8380 break;
c906108c 8381 case DW_TAG_class_type:
680b30c7 8382 case DW_TAG_interface_type:
c906108c 8383 case DW_TAG_structure_type:
72bf9492 8384 if (!pdi->is_declaration)
c906108c 8385 {
72bf9492 8386 add_partial_symbol (pdi, cu);
c906108c 8387 }
b7fee5a3
KS
8388 if ((cu->language == language_rust
8389 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8390 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8391 set_addrmap, cu);
c906108c 8392 break;
91c24f0a 8393 case DW_TAG_enumeration_type:
72bf9492
DJ
8394 if (!pdi->is_declaration)
8395 add_partial_enumeration (pdi, cu);
c906108c
SS
8396 break;
8397 case DW_TAG_base_type:
dda83cd7 8398 case DW_TAG_subrange_type:
c906108c 8399 /* File scope base type definitions are added to the partial
dda83cd7 8400 symbol table. */
72bf9492 8401 add_partial_symbol (pdi, cu);
c906108c 8402 break;
d9fa45fe 8403 case DW_TAG_namespace:
cdc07690 8404 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8405 break;
5d7cb8df 8406 case DW_TAG_module:
59c35742
AB
8407 if (!pdi->is_declaration)
8408 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8409 break;
95554aad
TT
8410 case DW_TAG_imported_unit:
8411 {
8412 struct dwarf2_per_cu_data *per_cu;
8413
f4dc4d17
DE
8414 /* For now we don't handle imported units in type units. */
8415 if (cu->per_cu->is_debug_types)
8416 {
8417 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8418 " supported in type units [in module %s]"),
5e22e966 8419 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8420 }
8421
e3b94546 8422 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8423 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8424
8425 /* Go read the partial unit, if needed. */
8426 if (per_cu->v.psymtab == NULL)
ab432490
SM
8427 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8428 cu->language);
95554aad 8429
ae640021 8430 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8431 }
8432 break;
74921315
KS
8433 case DW_TAG_imported_declaration:
8434 add_partial_symbol (pdi, cu);
8435 break;
c906108c
SS
8436 default:
8437 break;
8438 }
8439 }
8440
72bf9492
DJ
8441 /* If the die has a sibling, skip to the sibling. */
8442
8443 pdi = pdi->die_sibling;
8444 }
8445}
8446
8447/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8448
72bf9492 8449 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8450 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8451 Enumerators are an exception; they use the scope of their parent
8452 enumeration type, i.e. the name of the enumeration type is not
8453 prepended to the enumerator.
91c24f0a 8454
72bf9492
DJ
8455 There are two complexities. One is DW_AT_specification; in this
8456 case "parent" means the parent of the target of the specification,
8457 instead of the direct parent of the DIE. The other is compilers
8458 which do not emit DW_TAG_namespace; in this case we try to guess
8459 the fully qualified name of structure types from their members'
8460 linkage names. This must be done using the DIE's children rather
8461 than the children of any DW_AT_specification target. We only need
8462 to do this for structures at the top level, i.e. if the target of
8463 any DW_AT_specification (if any; otherwise the DIE itself) does not
8464 have a parent. */
8465
8466/* Compute the scope prefix associated with PDI's parent, in
8467 compilation unit CU. The result will be allocated on CU's
8468 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8469 field. NULL is returned if no prefix is necessary. */
15d034d0 8470static const char *
72bf9492
DJ
8471partial_die_parent_scope (struct partial_die_info *pdi,
8472 struct dwarf2_cu *cu)
8473{
15d034d0 8474 const char *grandparent_scope;
72bf9492 8475 struct partial_die_info *parent, *real_pdi;
91c24f0a 8476
72bf9492
DJ
8477 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8478 then this means the parent of the specification DIE. */
8479
8480 real_pdi = pdi;
72bf9492 8481 while (real_pdi->has_specification)
fb816e8b 8482 {
122cf0f2
AB
8483 auto res = find_partial_die (real_pdi->spec_offset,
8484 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8485 real_pdi = res.pdi;
8486 cu = res.cu;
8487 }
72bf9492
DJ
8488
8489 parent = real_pdi->die_parent;
8490 if (parent == NULL)
8491 return NULL;
8492
8493 if (parent->scope_set)
8494 return parent->scope;
8495
52356b79 8496 parent->fixup (cu);
72bf9492 8497
10b3939b 8498 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8499
acebe513
UW
8500 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8501 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8502 Work around this problem here. */
8503 if (cu->language == language_cplus
6e70227d 8504 && parent->tag == DW_TAG_namespace
7d00ffec 8505 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
8506 && grandparent_scope == NULL)
8507 {
8508 parent->scope = NULL;
8509 parent->scope_set = 1;
8510 return NULL;
8511 }
8512
0a4b0913 8513 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8514 if (pdi->tag == DW_TAG_enumerator)
8515 /* Enumerators should not get the name of the enumeration as a prefix. */
8516 parent->scope = grandparent_scope;
8517 else if (parent->tag == DW_TAG_namespace
f55ee35c 8518 || parent->tag == DW_TAG_module
72bf9492
DJ
8519 || parent->tag == DW_TAG_structure_type
8520 || parent->tag == DW_TAG_class_type
680b30c7 8521 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8522 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8523 || parent->tag == DW_TAG_enumeration_type
8524 || (cu->language == language_fortran
8525 && parent->tag == DW_TAG_subprogram
8526 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8527 {
8528 if (grandparent_scope == NULL)
7d00ffec 8529 parent->scope = parent->name (cu);
72bf9492 8530 else
3e43a32a
MS
8531 parent->scope = typename_concat (&cu->comp_unit_obstack,
8532 grandparent_scope,
7d00ffec 8533 parent->name (cu), 0, cu);
72bf9492 8534 }
72bf9492
DJ
8535 else
8536 {
8537 /* FIXME drow/2004-04-01: What should we be doing with
8538 function-local names? For partial symbols, we should probably be
8539 ignoring them. */
fa9c3fa0
TT
8540 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8541 dwarf_tag_name (parent->tag),
8542 sect_offset_str (pdi->sect_off));
72bf9492 8543 parent->scope = grandparent_scope;
c906108c
SS
8544 }
8545
72bf9492
DJ
8546 parent->scope_set = 1;
8547 return parent->scope;
8548}
8549
8550/* Return the fully scoped name associated with PDI, from compilation unit
8551 CU. The result will be allocated with malloc. */
4568ecf9 8552
43816ebc 8553static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8554partial_die_full_name (struct partial_die_info *pdi,
8555 struct dwarf2_cu *cu)
8556{
15d034d0 8557 const char *parent_scope;
72bf9492 8558
98bfdba5
PA
8559 /* If this is a template instantiation, we can not work out the
8560 template arguments from partial DIEs. So, unfortunately, we have
8561 to go through the full DIEs. At least any work we do building
8562 types here will be reused if full symbols are loaded later. */
8563 if (pdi->has_template_arguments)
8564 {
52356b79 8565 pdi->fixup (cu);
98bfdba5 8566
7d00ffec 8567 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8568 {
8569 struct die_info *die;
8570 struct attribute attr;
8571 struct dwarf2_cu *ref_cu = cu;
8572
b64f50a1 8573 /* DW_FORM_ref_addr is using section offset. */
b4069958 8574 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8575 attr.form = DW_FORM_ref_addr;
9c541725 8576 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8577 die = follow_die_ref (NULL, &attr, &ref_cu);
8578
43816ebc 8579 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8580 }
8581 }
8582
72bf9492
DJ
8583 parent_scope = partial_die_parent_scope (pdi, cu);
8584 if (parent_scope == NULL)
8585 return NULL;
8586 else
43816ebc 8587 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8588 pdi->name (cu),
8589 0, cu));
c906108c
SS
8590}
8591
8592static void
72bf9492 8593add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8594{
976ca316
SM
8595 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8596 struct objfile *objfile = per_objfile->objfile;
08feed99 8597 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8598 CORE_ADDR addr = 0;
15d034d0 8599 const char *actual_name = NULL;
e142c38c
DJ
8600 CORE_ADDR baseaddr;
8601
b3b3bada 8602 baseaddr = objfile->text_section_offset ();
c906108c 8603
43816ebc
TT
8604 gdb::unique_xmalloc_ptr<char> built_actual_name
8605 = partial_die_full_name (pdi, cu);
15d034d0 8606 if (built_actual_name != NULL)
43816ebc 8607 actual_name = built_actual_name.get ();
63d06c5c 8608
72bf9492 8609 if (actual_name == NULL)
7d00ffec 8610 actual_name = pdi->name (cu);
72bf9492 8611
76e288d1
TT
8612 partial_symbol psymbol;
8613 memset (&psymbol, 0, sizeof (psymbol));
8614 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
a52d653e 8615 psymbol.ginfo.set_section_index (-1);
76e288d1
TT
8616
8617 /* The code below indicates that the psymbol should be installed by
8618 setting this. */
8619 gdb::optional<psymbol_placement> where;
8620
c906108c
SS
8621 switch (pdi->tag)
8622 {
b1dc1806 8623 case DW_TAG_inlined_subroutine:
c906108c 8624 case DW_TAG_subprogram:
79748972
TT
8625 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8626 - baseaddr);
0a4b0913
AB
8627 if (pdi->is_external
8628 || cu->language == language_ada
8629 || (cu->language == language_fortran
8630 && pdi->die_parent != NULL
8631 && pdi->die_parent->tag == DW_TAG_subprogram))
8632 {
dda83cd7
SM
8633 /* Normally, only "external" DIEs are part of the global scope.
8634 But in Ada and Fortran, we want to be able to access nested
8635 procedures globally. So all Ada and Fortran subprograms are
8636 stored in the global scope. */
76e288d1 8637 where = psymbol_placement::GLOBAL;
c906108c
SS
8638 }
8639 else
76e288d1
TT
8640 where = psymbol_placement::STATIC;
8641
8642 psymbol.domain = VAR_DOMAIN;
8643 psymbol.aclass = LOC_BLOCK;
a52d653e 8644 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1 8645 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8646
8647 if (pdi->main_subprogram && actual_name != NULL)
8648 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8649 break;
72929c62 8650 case DW_TAG_constant:
76e288d1
TT
8651 psymbol.domain = VAR_DOMAIN;
8652 psymbol.aclass = LOC_STATIC;
8653 where = (pdi->is_external
8654 ? psymbol_placement::GLOBAL
8655 : psymbol_placement::STATIC);
72929c62 8656 break;
c906108c 8657 case DW_TAG_variable:
95554aad
TT
8658 if (pdi->d.locdesc)
8659 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8660
95554aad 8661 if (pdi->d.locdesc
caac4577 8662 && addr == 0
976ca316 8663 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8664 {
8665 /* A global or static variable may also have been stripped
8666 out by the linker if unused, in which case its address
8667 will be nullified; do not add such variables into partial
8668 symbol table then. */
8669 }
8670 else if (pdi->is_external)
c906108c
SS
8671 {
8672 /* Global Variable.
8673 Don't enter into the minimal symbol tables as there is
8674 a minimal symbol table entry from the ELF symbols already.
8675 Enter into partial symbol table if it has a location
8676 descriptor or a type.
8677 If the location descriptor is missing, new_symbol will create
8678 a LOC_UNRESOLVED symbol, the address of the variable will then
8679 be determined from the minimal symbol table whenever the variable
8680 is referenced.
8681 The address for the partial symbol table entry is not
8682 used by GDB, but it comes in handy for debugging partial symbol
8683 table building. */
8684
95554aad 8685 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8686 {
8687 psymbol.domain = VAR_DOMAIN;
8688 psymbol.aclass = LOC_STATIC;
a52d653e 8689 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8690 psymbol.ginfo.value.address = addr;
8691 where = psymbol_placement::GLOBAL;
8692 }
c906108c
SS
8693 }
8694 else
8695 {
ff908ebf
AW
8696 int has_loc = pdi->d.locdesc != NULL;
8697
8698 /* Static Variable. Skip symbols whose value we cannot know (those
8699 without location descriptors or constant values). */
8700 if (!has_loc && !pdi->has_const_value)
43816ebc 8701 return;
ff908ebf 8702
76e288d1
TT
8703 psymbol.domain = VAR_DOMAIN;
8704 psymbol.aclass = LOC_STATIC;
a52d653e 8705 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8706 if (has_loc)
8707 psymbol.ginfo.value.address = addr;
8708 where = psymbol_placement::STATIC;
c906108c
SS
8709 }
8710 break;
d8f62e84 8711 case DW_TAG_array_type:
c906108c
SS
8712 case DW_TAG_typedef:
8713 case DW_TAG_base_type:
a02abb62 8714 case DW_TAG_subrange_type:
76e288d1
TT
8715 psymbol.domain = VAR_DOMAIN;
8716 psymbol.aclass = LOC_TYPEDEF;
8717 where = psymbol_placement::STATIC;
c906108c 8718 break;
74921315 8719 case DW_TAG_imported_declaration:
72bf9492 8720 case DW_TAG_namespace:
76e288d1
TT
8721 psymbol.domain = VAR_DOMAIN;
8722 psymbol.aclass = LOC_TYPEDEF;
8723 where = psymbol_placement::GLOBAL;
72bf9492 8724 break;
530e8392 8725 case DW_TAG_module:
a5fd13a9 8726 /* With Fortran 77 there might be a "BLOCK DATA" module
dda83cd7
SM
8727 available without any name. If so, we skip the module as it
8728 doesn't bring any value. */
a5fd13a9 8729 if (actual_name != nullptr)
76e288d1
TT
8730 {
8731 psymbol.domain = MODULE_DOMAIN;
8732 psymbol.aclass = LOC_TYPEDEF;
8733 where = psymbol_placement::GLOBAL;
8734 }
530e8392 8735 break;
c906108c 8736 case DW_TAG_class_type:
680b30c7 8737 case DW_TAG_interface_type:
c906108c
SS
8738 case DW_TAG_structure_type:
8739 case DW_TAG_union_type:
8740 case DW_TAG_enumeration_type:
fa4028e9 8741 /* Skip external references. The DWARF standard says in the section
dda83cd7
SM
8742 about "Structure, Union, and Class Type Entries": "An incomplete
8743 structure, union or class type is represented by a structure,
8744 union or class entry that does not have a byte size attribute
8745 and that has a DW_AT_declaration attribute." */
fa4028e9 8746 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8747 return;
fa4028e9 8748
63d06c5c
DC
8749 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8750 static vs. global. */
76e288d1
TT
8751 psymbol.domain = STRUCT_DOMAIN;
8752 psymbol.aclass = LOC_TYPEDEF;
8753 where = (cu->language == language_cplus
8754 ? psymbol_placement::GLOBAL
8755 : psymbol_placement::STATIC);
c906108c
SS
8756 break;
8757 case DW_TAG_enumerator:
76e288d1
TT
8758 psymbol.domain = VAR_DOMAIN;
8759 psymbol.aclass = LOC_CONST;
8760 where = (cu->language == language_cplus
8761 ? psymbol_placement::GLOBAL
8762 : psymbol_placement::STATIC);
c906108c
SS
8763 break;
8764 default:
8765 break;
8766 }
76e288d1
TT
8767
8768 if (where.has_value ())
8769 {
f049a313
TT
8770 if (built_actual_name != nullptr)
8771 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8772 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8773 psymbol.ginfo.set_linkage_name (actual_name);
8774 else
8775 {
8776 psymbol.ginfo.set_demangled_name (actual_name,
8777 &objfile->objfile_obstack);
8778 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8779 }
932539d7 8780 cu->per_cu->v.psymtab->add_psymbol (psymbol, *where, objfile);
76e288d1 8781 }
c906108c
SS
8782}
8783
5c4e30ca
DC
8784/* Read a partial die corresponding to a namespace; also, add a symbol
8785 corresponding to that namespace to the symbol table. NAMESPACE is
8786 the name of the enclosing namespace. */
91c24f0a 8787
72bf9492
DJ
8788static void
8789add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8790 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8791 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8792{
72bf9492 8793 /* Add a symbol for the namespace. */
e7c27a73 8794
72bf9492 8795 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8796
8797 /* Now scan partial symbols in that namespace. */
8798
91c24f0a 8799 if (pdi->has_children)
cdc07690 8800 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8801}
8802
5d7cb8df
JK
8803/* Read a partial die corresponding to a Fortran module. */
8804
8805static void
8806add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8807 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8808{
530e8392
KB
8809 /* Add a symbol for the namespace. */
8810
8811 add_partial_symbol (pdi, cu);
8812
f55ee35c 8813 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8814
8815 if (pdi->has_children)
cdc07690 8816 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8817}
8818
b1dc1806
XR
8819/* Read a partial die corresponding to a subprogram or an inlined
8820 subprogram and create a partial symbol for that subprogram.
8821 When the CU language allows it, this routine also defines a partial
8822 symbol for each nested subprogram that this subprogram contains.
8823 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8824 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8825
cdc07690
YQ
8826 PDI may also be a lexical block, in which case we simply search
8827 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8828 Again, this is only performed when the CU language allows this
8829 type of definitions. */
8830
8831static void
8832add_partial_subprogram (struct partial_die_info *pdi,
8833 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8834 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8835{
b1dc1806 8836 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8837 {
8838 if (pdi->has_pc_info)
dda83cd7
SM
8839 {
8840 if (pdi->lowpc < *lowpc)
8841 *lowpc = pdi->lowpc;
8842 if (pdi->highpc > *highpc)
8843 *highpc = pdi->highpc;
cdc07690 8844 if (set_addrmap)
5734ee8b 8845 {
5e22e966 8846 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8847 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8848 CORE_ADDR baseaddr;
b926417a
TT
8849 CORE_ADDR this_highpc;
8850 CORE_ADDR this_lowpc;
5734ee8b 8851
b3b3bada 8852 baseaddr = objfile->text_section_offset ();
b926417a
TT
8853 this_lowpc
8854 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8855 pdi->lowpc + baseaddr)
8856 - baseaddr);
8857 this_highpc
8858 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8859 pdi->highpc + baseaddr)
8860 - baseaddr);
d320c2b5 8861 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8862 this_lowpc, this_highpc - 1,
9291a0cd 8863 cu->per_cu->v.psymtab);
5734ee8b 8864 }
dda83cd7 8865 }
481860b3
GB
8866
8867 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8868 {
dda83cd7 8869 if (!pdi->is_declaration)
e8d05480
JB
8870 /* Ignore subprogram DIEs that do not have a name, they are
8871 illegal. Do not emit a complaint at this point, we will
8872 do so when we convert this psymtab into a symtab. */
7d00ffec 8873 if (pdi->name (cu))
e8d05480 8874 add_partial_symbol (pdi, cu);
dda83cd7 8875 }
bc30ff58 8876 }
6e70227d 8877
bc30ff58
JB
8878 if (! pdi->has_children)
8879 return;
8880
0a4b0913 8881 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8882 {
8883 pdi = pdi->die_child;
8884 while (pdi != NULL)
8885 {
52356b79 8886 pdi->fixup (cu);
bc30ff58 8887 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8888 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8889 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8890 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8891 pdi = pdi->die_sibling;
8892 }
8893 }
8894}
8895
91c24f0a
DC
8896/* Read a partial die corresponding to an enumeration type. */
8897
72bf9492
DJ
8898static void
8899add_partial_enumeration (struct partial_die_info *enum_pdi,
8900 struct dwarf2_cu *cu)
91c24f0a 8901{
72bf9492 8902 struct partial_die_info *pdi;
91c24f0a 8903
7d00ffec 8904 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8905 add_partial_symbol (enum_pdi, cu);
8906
8907 pdi = enum_pdi->die_child;
8908 while (pdi)
91c24f0a 8909 {
7d00ffec 8910 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8911 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8912 else
72bf9492
DJ
8913 add_partial_symbol (pdi, cu);
8914 pdi = pdi->die_sibling;
91c24f0a 8915 }
91c24f0a
DC
8916}
8917
6caca83c
CC
8918/* Return the initial uleb128 in the die at INFO_PTR. */
8919
8920static unsigned int
d521ce57 8921peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8922{
8923 unsigned int bytes_read;
8924
8925 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8926}
8927
685af9cd
TT
8928/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8929 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8930
4bb7a0a7
DJ
8931 Return the corresponding abbrev, or NULL if the number is zero (indicating
8932 an empty DIE). In either case *BYTES_READ will be set to the length of
8933 the initial number. */
8934
8935static struct abbrev_info *
685af9cd
TT
8936peek_die_abbrev (const die_reader_specs &reader,
8937 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8938{
685af9cd 8939 dwarf2_cu *cu = reader.cu;
5e22e966 8940 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8941 unsigned int abbrev_number
8942 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8943
8944 if (abbrev_number == 0)
8945 return NULL;
8946
685af9cd 8947 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8948 if (!abbrev)
8949 {
422b9917 8950 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8951 " at offset %s [in module %s]"),
422b9917 8952 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8953 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8954 }
8955
8956 return abbrev;
8957}
8958
93311388
DE
8959/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8960 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8961 DIE. Any children of the skipped DIEs will also be skipped. */
8962
d521ce57
TT
8963static const gdb_byte *
8964skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8965{
4bb7a0a7
DJ
8966 while (1)
8967 {
685af9cd
TT
8968 unsigned int bytes_read;
8969 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8970
4bb7a0a7
DJ
8971 if (abbrev == NULL)
8972 return info_ptr + bytes_read;
8973 else
dee91e82 8974 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8975 }
8976}
8977
93311388
DE
8978/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8979 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8980 abbrev corresponding to that skipped uleb128 should be passed in
8981 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8982 children. */
8983
d521ce57
TT
8984static const gdb_byte *
8985skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8986 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8987{
8988 unsigned int bytes_read;
8989 struct attribute attr;
dee91e82
DE
8990 bfd *abfd = reader->abfd;
8991 struct dwarf2_cu *cu = reader->cu;
d521ce57 8992 const gdb_byte *buffer = reader->buffer;
f664829e 8993 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8994 unsigned int form, i;
8995
8996 for (i = 0; i < abbrev->num_attrs; i++)
8997 {
8998 /* The only abbrev we care about is DW_AT_sibling. */
8999 if (abbrev->attrs[i].name == DW_AT_sibling)
9000 {
7a5f294d 9001 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9002 if (attr.form == DW_FORM_ref_addr)
b98664d3 9003 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9004 else
b9502d3f 9005 {
0826b30a 9006 sect_offset off = attr.get_ref_die_offset ();
9c541725 9007 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9008
9009 if (sibling_ptr < info_ptr)
b98664d3 9010 complaint (_("DW_AT_sibling points backwards"));
22869d73 9011 else if (sibling_ptr > reader->buffer_end)
a0194fa8 9012 reader->die_section->overflow_complaint ();
b9502d3f
WN
9013 else
9014 return sibling_ptr;
9015 }
4bb7a0a7
DJ
9016 }
9017
9018 /* If it isn't DW_AT_sibling, skip this attribute. */
9019 form = abbrev->attrs[i].form;
9020 skip_attribute:
9021 switch (form)
9022 {
4bb7a0a7 9023 case DW_FORM_ref_addr:
ae411497
TT
9024 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9025 and later it is offset sized. */
9026 if (cu->header.version == 2)
9027 info_ptr += cu->header.addr_size;
9028 else
9029 info_ptr += cu->header.offset_size;
9030 break;
36586728
TT
9031 case DW_FORM_GNU_ref_alt:
9032 info_ptr += cu->header.offset_size;
9033 break;
ae411497 9034 case DW_FORM_addr:
4bb7a0a7
DJ
9035 info_ptr += cu->header.addr_size;
9036 break;
9037 case DW_FORM_data1:
9038 case DW_FORM_ref1:
9039 case DW_FORM_flag:
8fe0f950 9040 case DW_FORM_strx1:
4bb7a0a7
DJ
9041 info_ptr += 1;
9042 break;
2dc7f7b3 9043 case DW_FORM_flag_present:
43988095 9044 case DW_FORM_implicit_const:
2dc7f7b3 9045 break;
4bb7a0a7
DJ
9046 case DW_FORM_data2:
9047 case DW_FORM_ref2:
8fe0f950 9048 case DW_FORM_strx2:
4bb7a0a7
DJ
9049 info_ptr += 2;
9050 break;
8fe0f950
AT
9051 case DW_FORM_strx3:
9052 info_ptr += 3;
9053 break;
4bb7a0a7
DJ
9054 case DW_FORM_data4:
9055 case DW_FORM_ref4:
8fe0f950 9056 case DW_FORM_strx4:
4bb7a0a7
DJ
9057 info_ptr += 4;
9058 break;
9059 case DW_FORM_data8:
9060 case DW_FORM_ref8:
55f1336d 9061 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9062 info_ptr += 8;
9063 break;
0224619f
JK
9064 case DW_FORM_data16:
9065 info_ptr += 16;
9066 break;
4bb7a0a7 9067 case DW_FORM_string:
9b1c24c8 9068 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9069 info_ptr += bytes_read;
9070 break;
2dc7f7b3 9071 case DW_FORM_sec_offset:
4bb7a0a7 9072 case DW_FORM_strp:
36586728 9073 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9074 info_ptr += cu->header.offset_size;
9075 break;
2dc7f7b3 9076 case DW_FORM_exprloc:
4bb7a0a7
DJ
9077 case DW_FORM_block:
9078 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9079 info_ptr += bytes_read;
9080 break;
9081 case DW_FORM_block1:
9082 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9083 break;
9084 case DW_FORM_block2:
9085 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9086 break;
9087 case DW_FORM_block4:
9088 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9089 break;
336d760d 9090 case DW_FORM_addrx:
cf532bd1 9091 case DW_FORM_strx:
4bb7a0a7
DJ
9092 case DW_FORM_sdata:
9093 case DW_FORM_udata:
9094 case DW_FORM_ref_udata:
3019eac3
DE
9095 case DW_FORM_GNU_addr_index:
9096 case DW_FORM_GNU_str_index:
18a8505e 9097 case DW_FORM_rnglistx:
41144253 9098 case DW_FORM_loclistx:
d521ce57 9099 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9100 break;
9101 case DW_FORM_indirect:
9102 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9103 info_ptr += bytes_read;
9104 /* We need to continue parsing from here, so just go back to
9105 the top. */
9106 goto skip_attribute;
9107
9108 default:
3e43a32a
MS
9109 error (_("Dwarf Error: Cannot handle %s "
9110 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9111 dwarf_form_name (form),
9112 bfd_get_filename (abfd));
9113 }
9114 }
9115
9116 if (abbrev->has_children)
dee91e82 9117 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9118 else
9119 return info_ptr;
9120}
9121
93311388 9122/* Locate ORIG_PDI's sibling.
dee91e82 9123 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9124
d521ce57 9125static const gdb_byte *
dee91e82
DE
9126locate_pdi_sibling (const struct die_reader_specs *reader,
9127 struct partial_die_info *orig_pdi,
d521ce57 9128 const gdb_byte *info_ptr)
91c24f0a
DC
9129{
9130 /* Do we know the sibling already? */
72bf9492 9131
91c24f0a
DC
9132 if (orig_pdi->sibling)
9133 return orig_pdi->sibling;
9134
9135 /* Are there any children to deal with? */
9136
9137 if (!orig_pdi->has_children)
9138 return info_ptr;
9139
4bb7a0a7 9140 /* Skip the children the long way. */
91c24f0a 9141
dee91e82 9142 return skip_children (reader, info_ptr);
91c24f0a
DC
9143}
9144
257e7a09 9145/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9146 not NULL. */
c906108c 9147
891813be
TT
9148void
9149dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 9150{
976ca316 9151 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 9152
976ca316 9153 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 9154
077cbab2
TT
9155 /* If this psymtab is constructed from a debug-only objfile, the
9156 has_section_at_zero flag will not necessarily be correct. We
9157 can get the correct value for this flag by looking at the data
9158 associated with the (presumably stripped) associated objfile. */
9159 if (objfile->separate_debug_objfile_backlink)
c906108c 9160 {
976ca316 9161 dwarf2_per_objfile *per_objfile_backlink
077cbab2 9162 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 9163
976ca316
SM
9164 per_objfile->per_bfd->has_section_at_zero
9165 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 9166 }
98bfdba5 9167
8566b89b 9168 expand_psymtab (objfile);
95554aad 9169
976ca316 9170 process_cu_includes (per_objfile);
c906108c 9171}
9cdd5dbd
DE
9172\f
9173/* Reading in full CUs. */
c906108c 9174
10b3939b
DJ
9175/* Add PER_CU to the queue. */
9176
9177static void
120ce1b5
SM
9178queue_comp_unit (dwarf2_per_cu_data *per_cu,
9179 dwarf2_per_objfile *per_objfile,
95554aad 9180 enum language pretend_language)
10b3939b 9181{
10b3939b 9182 per_cu->queued = 1;
120ce1b5 9183 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
9184}
9185
89e63ee4
DE
9186/* If PER_CU is not yet queued, add it to the queue.
9187 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9188 dependency.
0907af0c 9189 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9190 meaning either PER_CU is already queued or it is already loaded.
9191
9192 N.B. There is an invariant here that if a CU is queued then it is loaded.
9193 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9194
9195static int
89e63ee4 9196maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
9197 dwarf2_per_cu_data *per_cu,
9198 dwarf2_per_objfile *per_objfile,
0907af0c
DE
9199 enum language pretend_language)
9200{
9201 /* We may arrive here during partial symbol reading, if we need full
9202 DIEs to process an unusual case (e.g. template arguments). Do
9203 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 9204 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 9205 {
7188ed02
SM
9206 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9207
9208 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
9209 return 1;
9210 return 0;
9211 }
9212
9213 /* Mark the dependence relation so that we don't flush PER_CU
9214 too early. */
89e63ee4
DE
9215 if (dependent_cu != NULL)
9216 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9217
9218 /* If it's already on the queue, we have nothing to do. */
9219 if (per_cu->queued)
de53369b
SM
9220 {
9221 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
9222 loaded. */
9223 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
9224 return 0;
9225 }
0907af0c
DE
9226
9227 /* If the compilation unit is already loaded, just mark it as
9228 used. */
7188ed02
SM
9229 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9230 if (cu != nullptr)
0907af0c 9231 {
7188ed02 9232 cu->last_used = 0;
0907af0c
DE
9233 return 0;
9234 }
9235
9236 /* Add it to the queue. */
120ce1b5 9237 queue_comp_unit (per_cu, per_objfile, pretend_language);
0907af0c
DE
9238
9239 return 1;
9240}
9241
10b3939b
DJ
9242/* Process the queue. */
9243
9244static void
976ca316 9245process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 9246{
6f738b01
SM
9247 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9248 objfile_name (per_objfile->objfile));
45cfd468 9249
03dd20cc
DJ
9250 /* The queue starts out with one item, but following a DIE reference
9251 may load a new CU, adding it to the end of the queue. */
976ca316 9252 while (!per_objfile->per_bfd->queue.empty ())
10b3939b 9253 {
976ca316 9254 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
7188ed02 9255 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 9256
976ca316 9257 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 9258 {
976ca316 9259 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 9260
7188ed02
SM
9261 /* Skip dummy CUs. */
9262 if (cu != nullptr)
73be47f5 9263 {
7188ed02
SM
9264 unsigned int debug_print_threshold;
9265 char buf[100];
9266
9267 if (per_cu->is_debug_types)
9268 {
9269 struct signatured_type *sig_type =
9270 (struct signatured_type *) per_cu;
9271
9272 sprintf (buf, "TU %s at offset %s",
9273 hex_string (sig_type->signature),
9274 sect_offset_str (per_cu->sect_off));
9275 /* There can be 100s of TUs.
9276 Only print them in verbose mode. */
9277 debug_print_threshold = 2;
9278 }
9279 else
9280 {
9281 sprintf (buf, "CU at offset %s",
9282 sect_offset_str (per_cu->sect_off));
9283 debug_print_threshold = 1;
9284 }
247f5c4f 9285
7188ed02 9286 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 9287 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
f4dc4d17 9288
7188ed02
SM
9289 if (per_cu->is_debug_types)
9290 process_full_type_unit (cu, item.pretend_language);
9291 else
9292 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 9293
7188ed02 9294 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 9295 dwarf_read_debug_printf ("Done expanding %s", buf);
7188ed02 9296 }
f4dc4d17 9297 }
10b3939b 9298
7188ed02 9299 per_cu->queued = 0;
976ca316 9300 per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9301 }
9302
6f738b01
SM
9303 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9304 objfile_name (per_objfile->objfile));
10b3939b
DJ
9305}
9306
10b3939b
DJ
9307/* Read in full symbols for PST, and anything it depends on. */
9308
8566b89b
TT
9309void
9310dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9311{
af758d11 9312 gdb_assert (!readin_p (objfile));
95554aad 9313
17ee85fc
TT
9314 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9315 free_cached_comp_units freer (per_objfile);
48993951 9316 expand_dependencies (objfile);
aaa75496 9317
97a1449a 9318 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9319 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9320}
9321
af758d11
SM
9322/* See psympriv.h. */
9323
9324bool
9325dwarf2_psymtab::readin_p (struct objfile *objfile) const
9326{
9327 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9328 return per_objfile->symtab_set_p (per_cu_data);
9329}
9330
9331/* See psympriv.h. */
9332
9333compunit_symtab *
9334dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9335{
9336 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9337 return per_objfile->get_symtab (per_cu_data);
9338}
9339
dee91e82
DE
9340/* Trivial hash function for die_info: the hash value of a DIE
9341 is its offset in .debug_info for this objfile. */
10b3939b 9342
dee91e82
DE
9343static hashval_t
9344die_hash (const void *item)
10b3939b 9345{
9a3c8263 9346 const struct die_info *die = (const struct die_info *) item;
6502dd73 9347
9c541725 9348 return to_underlying (die->sect_off);
dee91e82 9349}
63d06c5c 9350
dee91e82
DE
9351/* Trivial comparison function for die_info structures: two DIEs
9352 are equal if they have the same offset. */
98bfdba5 9353
dee91e82
DE
9354static int
9355die_eq (const void *item_lhs, const void *item_rhs)
9356{
9a3c8263
SM
9357 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9358 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9359
9c541725 9360 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9361}
c906108c 9362
4a636814
SM
9363/* Load the DIEs associated with PER_CU into memory.
9364
9365 In some cases, the caller, while reading partial symbols, will need to load
9366 the full symbols for the CU for some reason. It will already have a
9367 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9368 rather than creating a new one. */
c906108c 9369
dee91e82 9370static void
ab432490
SM
9371load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9372 dwarf2_per_objfile *per_objfile,
4a636814 9373 dwarf2_cu *existing_cu,
c0ab21c2
TT
9374 bool skip_partial,
9375 enum language pretend_language)
dee91e82 9376{
c0ab21c2
TT
9377 gdb_assert (! this_cu->is_debug_types);
9378
7188ed02 9379 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
9380 if (reader.dummy_p)
9381 return;
9382
9383 struct dwarf2_cu *cu = reader.cu;
9384 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9385
dee91e82
DE
9386 gdb_assert (cu->die_hash == NULL);
9387 cu->die_hash =
9388 htab_create_alloc_ex (cu->header.length / 12,
9389 die_hash,
9390 die_eq,
9391 NULL,
9392 &cu->comp_unit_obstack,
9393 hashtab_obstack_allocate,
9394 dummy_obstack_deallocate);
e142c38c 9395
3e225074 9396 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9397 reader.comp_unit_die->child
9398 = read_die_and_siblings (&reader, reader.info_ptr,
9399 &info_ptr, reader.comp_unit_die);
9400 cu->dies = reader.comp_unit_die;
dee91e82 9401 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9402
9403 /* We try not to read any attributes in this function, because not
9cdd5dbd 9404 all CUs needed for references have been loaded yet, and symbol
10b3939b 9405 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9406 or we won't be able to build types correctly.
9407 Similarly, if we do not read the producer, we can not apply
9408 producer-specific interpretation. */
c0ab21c2 9409 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9410
9411 reader.keep ();
10b3939b
DJ
9412}
9413
3da10d80
KS
9414/* Add a DIE to the delayed physname list. */
9415
9416static void
9417add_to_method_list (struct type *type, int fnfield_index, int index,
9418 const char *name, struct die_info *die,
9419 struct dwarf2_cu *cu)
9420{
9421 struct delayed_method_info mi;
9422 mi.type = type;
9423 mi.fnfield_index = fnfield_index;
9424 mi.index = index;
9425 mi.name = name;
9426 mi.die = die;
c89b44cd 9427 cu->method_list.push_back (mi);
3da10d80
KS
9428}
9429
3693fdb3
PA
9430/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9431 "const" / "volatile". If so, decrements LEN by the length of the
9432 modifier and return true. Otherwise return false. */
9433
9434template<size_t N>
9435static bool
9436check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9437{
9438 size_t mod_len = sizeof (mod) - 1;
9439 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9440 {
9441 len -= mod_len;
9442 return true;
9443 }
9444 return false;
9445}
9446
3da10d80
KS
9447/* Compute the physnames of any methods on the CU's method list.
9448
9449 The computation of method physnames is delayed in order to avoid the
9450 (bad) condition that one of the method's formal parameters is of an as yet
9451 incomplete type. */
9452
9453static void
9454compute_delayed_physnames (struct dwarf2_cu *cu)
9455{
3693fdb3 9456 /* Only C++ delays computing physnames. */
c89b44cd 9457 if (cu->method_list.empty ())
3693fdb3
PA
9458 return;
9459 gdb_assert (cu->language == language_cplus);
9460
52941706 9461 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9462 {
1d06ead6 9463 const char *physname;
3da10d80 9464 struct fn_fieldlist *fn_flp
c89b44cd
TT
9465 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9466 physname = dwarf2_physname (mi.name, mi.die, cu);
9467 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9468 = physname ? physname : "";
3693fdb3
PA
9469
9470 /* Since there's no tag to indicate whether a method is a
9471 const/volatile overload, extract that information out of the
9472 demangled name. */
9473 if (physname != NULL)
9474 {
9475 size_t len = strlen (physname);
9476
9477 while (1)
9478 {
9479 if (physname[len] == ')') /* shortcut */
9480 break;
9481 else if (check_modifier (physname, len, " const"))
c89b44cd 9482 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9483 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9484 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9485 else
9486 break;
9487 }
9488 }
3da10d80 9489 }
c89b44cd
TT
9490
9491 /* The list is no longer needed. */
9492 cu->method_list.clear ();
3da10d80
KS
9493}
9494
a766d390
DE
9495/* Go objects should be embedded in a DW_TAG_module DIE,
9496 and it's not clear if/how imported objects will appear.
9497 To keep Go support simple until that's worked out,
9498 go back through what we've read and create something usable.
9499 We could do this while processing each DIE, and feels kinda cleaner,
9500 but that way is more invasive.
9501 This is to, for example, allow the user to type "p var" or "b main"
9502 without having to specify the package name, and allow lookups
9503 of module.object to work in contexts that use the expression
9504 parser. */
9505
9506static void
9507fixup_go_packaging (struct dwarf2_cu *cu)
9508{
421d1616 9509 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9510 struct pending *list;
9511 int i;
9512
c24bdb02 9513 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9514 list != NULL;
9515 list = list->next)
a766d390
DE
9516 {
9517 for (i = 0; i < list->nsyms; ++i)
9518 {
9519 struct symbol *sym = list->symbol[i];
9520
c1b5c1eb 9521 if (sym->language () == language_go
a766d390
DE
9522 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9523 {
421d1616
TT
9524 gdb::unique_xmalloc_ptr<char> this_package_name
9525 (go_symbol_package_name (sym));
a766d390
DE
9526
9527 if (this_package_name == NULL)
9528 continue;
9529 if (package_name == NULL)
421d1616 9530 package_name = std::move (this_package_name);
a766d390
DE
9531 else
9532 {
5e22e966 9533 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9534 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9535 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9536 (symbol_symtab (sym) != NULL
9537 ? symtab_to_filename_for_display
9538 (symbol_symtab (sym))
e3b94546 9539 : objfile_name (objfile)),
421d1616 9540 this_package_name.get (), package_name.get ());
a766d390
DE
9541 }
9542 }
9543 }
9544 }
9545
9546 if (package_name != NULL)
9547 {
5e22e966 9548 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9549 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9550 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9551 saved_package_name);
a766d390
DE
9552 struct symbol *sym;
9553
8c14c3a3 9554 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9555 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9556 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9557 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9558 e.g., "main" finds the "main" module and not C's main(). */
9559 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9560 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9561 SYMBOL_TYPE (sym) = type;
9562
c24bdb02 9563 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9564 }
9565}
9566
c9317f21
TT
9567/* Allocate a fully-qualified name consisting of the two parts on the
9568 obstack. */
9569
9570static const char *
9571rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9572{
9573 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9574}
9575
9c6a1327
TT
9576/* A helper that allocates a variant part to attach to a Rust enum
9577 type. OBSTACK is where the results should be allocated. TYPE is
9578 the type we're processing. DISCRIMINANT_INDEX is the index of the
57d02173
TT
9579 discriminant. It must be the index of one of the fields of TYPE,
9580 or -1 to mean there is no discriminant (univariant enum).
9c6a1327
TT
9581 DEFAULT_INDEX is the index of the default field; or -1 if there is
9582 no default. RANGES is indexed by "effective" field number (the
9583 field index, but omitting the discriminant and default fields) and
9584 must hold the discriminant values used by the variants. Note that
9585 RANGES must have a lifetime at least as long as OBSTACK -- either
9586 already allocated on it, or static. */
c9317f21 9587
9c6a1327
TT
9588static void
9589alloc_rust_variant (struct obstack *obstack, struct type *type,
9590 int discriminant_index, int default_index,
9591 gdb::array_view<discriminant_range> ranges)
9592{
57d02173
TT
9593 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9594 gdb_assert (discriminant_index == -1
9595 || (discriminant_index >= 0
9596 && discriminant_index < type->num_fields ()));
c9317f21 9597 gdb_assert (default_index == -1
1f704f76 9598 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9599
9c6a1327 9600 /* We have one variant for each non-discriminant field. */
57d02173
TT
9601 int n_variants = type->num_fields ();
9602 if (discriminant_index != -1)
9603 --n_variants;
c9317f21 9604
9c6a1327
TT
9605 variant *variants = new (obstack) variant[n_variants];
9606 int var_idx = 0;
9607 int range_idx = 0;
1f704f76 9608 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9609 {
9610 if (i == discriminant_index)
9611 continue;
c9317f21 9612
9c6a1327
TT
9613 variants[var_idx].first_field = i;
9614 variants[var_idx].last_field = i + 1;
9615
9616 /* The default field does not need a range, but other fields do.
9617 We skipped the discriminant above. */
9618 if (i != default_index)
9619 {
9620 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9621 ++range_idx;
9622 }
c9317f21 9623
9c6a1327
TT
9624 ++var_idx;
9625 }
9626
9627 gdb_assert (range_idx == ranges.size ());
9628 gdb_assert (var_idx == n_variants);
9629
9630 variant_part *part = new (obstack) variant_part;
9631 part->discriminant_index = discriminant_index;
57d02173
TT
9632 /* If there is no discriminant, then whether it is signed is of no
9633 consequence. */
9634 part->is_unsigned
9635 = (discriminant_index == -1
9636 ? false
c6d940a9 9637 : type->field (discriminant_index).type ()->is_unsigned ());
9c6a1327
TT
9638 part->variants = gdb::array_view<variant> (variants, n_variants);
9639
9640 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9641 gdb::array_view<variant_part> *prop_value
9642 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9643
9c6a1327 9644 struct dynamic_prop prop;
8c2e4e06 9645 prop.set_variant_parts (prop_value);
9c6a1327 9646
5c54719c 9647 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9648}
9649
9650/* Some versions of rustc emitted enums in an unusual way.
9651
9652 Ordinary enums were emitted as unions. The first element of each
9653 structure in the union was named "RUST$ENUM$DISR". This element
9654 held the discriminant.
9655
9656 These versions of Rust also implemented the "non-zero"
9657 optimization. When the enum had two values, and one is empty and
9658 the other holds a pointer that cannot be zero, the pointer is used
9659 as the discriminant, with a zero value meaning the empty variant.
9660 Here, the union's first member is of the form
9661 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9662 where the fieldnos are the indices of the fields that should be
9663 traversed in order to find the field (which may be several fields deep)
9664 and the variantname is the name of the variant of the case when the
9665 field is zero.
9666
9667 This function recognizes whether TYPE is of one of these forms,
9668 and, if so, smashes it to be a variant type. */
9669
9670static void
9671quirk_rust_enum (struct type *type, struct objfile *objfile)
9672{
78134374 9673 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9674
9675 /* We don't need to deal with empty enums. */
1f704f76 9676 if (type->num_fields () == 0)
c9317f21
TT
9677 return;
9678
9679#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9680 if (type->num_fields () == 1
c9317f21
TT
9681 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9682 {
9683 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9684
9685 /* Decode the field name to find the offset of the
9686 discriminant. */
9687 ULONGEST bit_offset = 0;
940da03e 9688 struct type *field_type = type->field (0).type ();
c9317f21
TT
9689 while (name[0] >= '0' && name[0] <= '9')
9690 {
9691 char *tail;
9692 unsigned long index = strtoul (name, &tail, 10);
9693 name = tail;
9694 if (*name != '$'
1f704f76 9695 || index >= field_type->num_fields ()
c9317f21
TT
9696 || (TYPE_FIELD_LOC_KIND (field_type, index)
9697 != FIELD_LOC_KIND_BITPOS))
9698 {
b98664d3 9699 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9700 "[in module %s]"),
9701 TYPE_FIELD_NAME (type, 0),
9702 objfile_name (objfile));
9703 return;
9704 }
9705 ++name;
9706
9707 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 9708 field_type = field_type->field (index).type ();
c9317f21
TT
9709 }
9710
9c6a1327
TT
9711 /* Smash this type to be a structure type. We have to do this
9712 because the type has already been recorded. */
67607e24 9713 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9714 type->set_num_fields (3);
9c6a1327 9715 /* Save the field we care about. */
ceacbf6e 9716 struct field saved_field = type->field (0);
3cabb6b0
SM
9717 type->set_fields
9718 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9719
9c6a1327 9720 /* Put the discriminant at index 0. */
5d14b6e5 9721 type->field (0).set_type (field_type);
9c6a1327
TT
9722 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9723 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9724 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9725
9726 /* The order of fields doesn't really matter, so put the real
9727 field at index 1 and the data-less field at index 2. */
ceacbf6e 9728 type->field (1) = saved_field;
9c6a1327 9729 TYPE_FIELD_NAME (type, 1)
940da03e
SM
9730 = rust_last_path_segment (type->field (1).type ()->name ());
9731 type->field (1).type ()->set_name
7d93a1e0 9732 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9733 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9734
9735 const char *dataless_name
7d93a1e0 9736 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9737 name);
9738 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9739 dataless_name);
5d14b6e5 9740 type->field (2).set_type (dataless_type);
c9317f21
TT
9741 /* NAME points into the original discriminant name, which
9742 already has the correct lifetime. */
9c6a1327 9743 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9744 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9745
9c6a1327
TT
9746 /* Indicate that this is a variant type. */
9747 static discriminant_range ranges[1] = { { 0, 0 } };
9748 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9749 }
77c2dba3
TT
9750 /* A union with a single anonymous field is probably an old-style
9751 univariant enum. */
1f704f76 9752 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9753 {
c9317f21
TT
9754 /* Smash this type to be a structure type. We have to do this
9755 because the type has already been recorded. */
67607e24 9756 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9757
940da03e 9758 struct type *field_type = type->field (0).type ();
c9317f21 9759 const char *variant_name
7d93a1e0 9760 = rust_last_path_segment (field_type->name ());
9c6a1327 9761 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9762 field_type->set_name
9763 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9764 type->name (), variant_name));
57d02173
TT
9765
9766 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
c9317f21
TT
9767 }
9768 else
9769 {
9770 struct type *disr_type = nullptr;
1f704f76 9771 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9772 {
940da03e 9773 disr_type = type->field (i).type ();
c9317f21 9774
78134374 9775 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9776 {
9777 /* All fields of a true enum will be structs. */
9778 return;
9779 }
1f704f76 9780 else if (disr_type->num_fields () == 0)
c9317f21
TT
9781 {
9782 /* Could be data-less variant, so keep going. */
a037790e 9783 disr_type = nullptr;
c9317f21
TT
9784 }
9785 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9786 "RUST$ENUM$DISR") != 0)
9787 {
9788 /* Not a Rust enum. */
9789 return;
9790 }
9791 else
9792 {
9793 /* Found one. */
9794 break;
9795 }
9796 }
9797
9798 /* If we got here without a discriminant, then it's probably
9799 just a union. */
9800 if (disr_type == nullptr)
9801 return;
9802
9803 /* Smash this type to be a structure type. We have to do this
9804 because the type has already been recorded. */
67607e24 9805 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9806
9c6a1327 9807 /* Make space for the discriminant field. */
ceacbf6e 9808 struct field *disr_field = &disr_type->field (0);
9c6a1327 9809 field *new_fields
1f704f76 9810 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9811 * sizeof (struct field)));
80fc5e77 9812 memcpy (new_fields + 1, type->fields (),
1f704f76 9813 type->num_fields () * sizeof (struct field));
3cabb6b0 9814 type->set_fields (new_fields);
1f704f76 9815 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9816
9817 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9818 type->field (0) = *disr_field;
9c6a1327
TT
9819 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9820 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9821
9822 /* We need a way to find the correct discriminant given a
9823 variant name. For convenience we build a map here. */
b6cdac4b 9824 struct type *enum_type = disr_field->type ();
c9317f21 9825 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9826 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9827 {
9828 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9829 {
9830 const char *name
9831 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9832 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9833 }
9834 }
9835
1f704f76 9836 int n_fields = type->num_fields ();
9c6a1327
TT
9837 /* We don't need a range entry for the discriminant, but we do
9838 need one for every other field, as there is no default
9839 variant. */
9840 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9841 discriminant_range,
9842 n_fields - 1);
c9317f21
TT
9843 /* Skip the discriminant here. */
9844 for (int i = 1; i < n_fields; ++i)
9845 {
9846 /* Find the final word in the name of this variant's type.
9847 That name can be used to look up the correct
9848 discriminant. */
9849 const char *variant_name
940da03e 9850 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9851
9852 auto iter = discriminant_map.find (variant_name);
9853 if (iter != discriminant_map.end ())
9c6a1327 9854 {
57d02173
TT
9855 ranges[i - 1].low = iter->second;
9856 ranges[i - 1].high = iter->second;
9c6a1327 9857 }
c9317f21 9858
57d02173
TT
9859 /* In Rust, each element should have the size of the
9860 enclosing enum. */
9861 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9862
bedda9ac 9863 /* Remove the discriminant field, if it exists. */
940da03e 9864 struct type *sub_type = type->field (i).type ();
1f704f76 9865 if (sub_type->num_fields () > 0)
bedda9ac 9866 {
5e33d5f4 9867 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9868 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9869 }
9c6a1327 9870 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9871 sub_type->set_name
9872 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9873 type->name (), variant_name));
c9317f21 9874 }
9c6a1327
TT
9875
9876 /* Indicate that this is a variant type. */
a1520ad8 9877 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9878 gdb::array_view<discriminant_range> (ranges,
9879 n_fields - 1));
c9317f21
TT
9880 }
9881}
9882
9883/* Rewrite some Rust unions to be structures with variants parts. */
9884
9885static void
9886rust_union_quirks (struct dwarf2_cu *cu)
9887{
9888 gdb_assert (cu->language == language_rust);
52941706 9889 for (type *type_ : cu->rust_unions)
5e22e966 9890 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9891 /* We don't need this any more. */
9892 cu->rust_unions.clear ();
c9317f21
TT
9893}
9894
8adb8487
TT
9895/* See read.h. */
9896
9897type_unit_group_unshareable *
9898dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9899{
9900 auto iter = this->m_type_units.find (tu_group);
9901 if (iter != this->m_type_units.end ())
9902 return iter->second.get ();
9903
9904 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9905 type_unit_group_unshareable *result = uniq.get ();
9906 this->m_type_units[tu_group] = std::move (uniq);
9907 return result;
9908}
9909
e286671b
TT
9910struct type *
9911dwarf2_per_objfile::get_type_for_signatured_type
9912 (signatured_type *sig_type) const
9913{
9914 auto iter = this->m_type_map.find (sig_type);
9915 if (iter == this->m_type_map.end ())
9916 return nullptr;
9917
9918 return iter->second;
9919}
9920
9921void dwarf2_per_objfile::set_type_for_signatured_type
9922 (signatured_type *sig_type, struct type *type)
9923{
9924 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9925
9926 this->m_type_map[sig_type] = type;
9927}
9928
95554aad
TT
9929/* A helper function for computing the list of all symbol tables
9930 included by PER_CU. */
9931
9932static void
4c39bc03 9933recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9934 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9935 dwarf2_per_cu_data *per_cu,
9936 dwarf2_per_objfile *per_objfile,
43f3e411 9937 struct compunit_symtab *immediate_parent)
95554aad 9938{
af758d11 9939 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9940 if (*slot != NULL)
9941 {
9942 /* This inclusion and its children have been processed. */
9943 return;
9944 }
9945
9946 *slot = per_cu;
af758d11 9947
95554aad 9948 /* Only add a CU if it has a symbol table. */
43182c09 9949 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9950 if (cust != NULL)
ec94af83
DE
9951 {
9952 /* If this is a type unit only add its symbol table if we haven't
9953 seen it yet (type unit per_cu's can share symtabs). */
9954 if (per_cu->is_debug_types)
9955 {
43f3e411 9956 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9957 if (*slot == NULL)
9958 {
43f3e411 9959 *slot = cust;
4c39bc03 9960 result->push_back (cust);
43f3e411
DE
9961 if (cust->user == NULL)
9962 cust->user = immediate_parent;
ec94af83
DE
9963 }
9964 }
9965 else
f9125b6c 9966 {
4c39bc03 9967 result->push_back (cust);
43f3e411
DE
9968 if (cust->user == NULL)
9969 cust->user = immediate_parent;
f9125b6c 9970 }
ec94af83 9971 }
95554aad 9972
ae640021
AB
9973 if (!per_cu->imported_symtabs_empty ())
9974 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9975 {
9976 recursively_compute_inclusions (result, all_children,
43182c09
SM
9977 all_type_symtabs, ptr, per_objfile,
9978 cust);
ae640021 9979 }
95554aad
TT
9980}
9981
43f3e411 9982/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9983 PER_CU. */
9984
9985static void
43182c09
SM
9986compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9987 dwarf2_per_objfile *per_objfile)
95554aad 9988{
f4dc4d17
DE
9989 gdb_assert (! per_cu->is_debug_types);
9990
ae640021 9991 if (!per_cu->imported_symtabs_empty ())
95554aad 9992 {
ae640021 9993 int len;
4c39bc03 9994 std::vector<compunit_symtab *> result_symtabs;
43182c09 9995 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9996
9997 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9998 if (cust == NULL)
95554aad
TT
9999 return;
10000
280a9412
TT
10001 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
10002 htab_eq_pointer,
10003 NULL, xcalloc, xfree));
10004 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
10005 htab_eq_pointer,
10006 NULL, xcalloc, xfree));
95554aad 10007
ae640021 10008 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83 10009 {
280a9412
TT
10010 recursively_compute_inclusions (&result_symtabs, all_children.get (),
10011 all_type_symtabs.get (), ptr,
10012 per_objfile, cust);
ec94af83 10013 }
95554aad 10014
ec94af83 10015 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 10016 len = result_symtabs.size ();
43f3e411 10017 cust->includes
f6e649dd 10018 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 10019 struct compunit_symtab *, len + 1);
4c39bc03
TT
10020 memcpy (cust->includes, result_symtabs.data (),
10021 len * sizeof (compunit_symtab *));
43f3e411 10022 cust->includes[len] = NULL;
95554aad
TT
10023 }
10024}
10025
10026/* Compute the 'includes' field for the symtabs of all the CUs we just
10027 read. */
10028
10029static void
976ca316 10030process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 10031{
976ca316 10032 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
10033 {
10034 if (! iter->is_debug_types)
976ca316 10035 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 10036 }
95554aad 10037
976ca316 10038 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
10039}
10040
8fc0b21d 10041/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
10042 already been loaded into memory. */
10043
10044static void
8fc0b21d 10045process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 10046{
976ca316
SM
10047 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10048 struct objfile *objfile = per_objfile->objfile;
08feed99 10049 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 10050 CORE_ADDR lowpc, highpc;
43f3e411 10051 struct compunit_symtab *cust;
10b3939b 10052 CORE_ADDR baseaddr;
4359dff1 10053 struct block *static_block;
3e29f34a 10054 CORE_ADDR addr;
10b3939b 10055
b3b3bada 10056 baseaddr = objfile->text_section_offset ();
10b3939b 10057
c89b44cd
TT
10058 /* Clear the list here in case something was left over. */
10059 cu->method_list.clear ();
10b3939b 10060
95554aad
TT
10061 cu->language = pretend_language;
10062 cu->language_defn = language_def (cu->language);
10063
1c47ec3e
TV
10064 dwarf2_find_base_address (cu->dies, cu);
10065
c906108c 10066 /* Do line number decoding in read_file_scope () */
10b3939b 10067 process_die (cu->dies, cu);
c906108c 10068
a766d390
DE
10069 /* For now fudge the Go package. */
10070 if (cu->language == language_go)
10071 fixup_go_packaging (cu);
10072
5f48f8f3 10073 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10074 should be complete, and it should now be safe to compute all of the
10075 physnames. */
10076 compute_delayed_physnames (cu);
3da10d80 10077
c9317f21
TT
10078 if (cu->language == language_rust)
10079 rust_union_quirks (cu);
10080
fae299cd
DC
10081 /* Some compilers don't define a DW_AT_high_pc attribute for the
10082 compilation unit. If the DW_AT_high_pc is missing, synthesize
10083 it, by scanning the DIE's below the compilation unit. */
10b3939b 10084 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10085
3e29f34a 10086 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10087 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10088
10089 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10090 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10091 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10092 addrmap to help ensure it has an accurate map of pc values belonging to
10093 this comp unit. */
10094 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10095
c24bdb02 10096 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10097 SECT_OFF_TEXT (objfile),
10098 0);
c906108c 10099
43f3e411 10100 if (cust != NULL)
c906108c 10101 {
df15bd07 10102 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10103
8be455d7
JK
10104 /* Set symtab language to language from DW_AT_language. If the
10105 compilation is from a C file generated by language preprocessors, do
10106 not set the language if it was already deduced by start_subfile. */
43f3e411 10107 if (!(cu->language == language_c
40e3ad0e 10108 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10109 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10110
10111 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10112 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10113 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10114 there were bugs in prologue debug info, fixed later in GCC-4.5
10115 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10116
10117 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10118 needed, it would be wrong due to missing DW_AT_producer there.
10119
10120 Still one can confuse GDB by using non-standard GCC compilation
10121 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10122 */
ab260dad 10123 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10124 cust->locations_valid = 1;
e0d00bc7
JK
10125
10126 if (gcc_4_minor >= 5)
43f3e411 10127 cust->epilogue_unwind_valid = 1;
96408a79 10128
43f3e411 10129 cust->call_site_htab = cu->call_site_htab;
c906108c 10130 }
9291a0cd 10131
976ca316 10132 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 10133
95554aad 10134 /* Push it for inclusion processing later. */
976ca316 10135 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
10136
10137 /* Not needed any more. */
c24bdb02 10138 cu->reset_builder ();
f4dc4d17 10139}
45cfd468 10140
8fc0b21d 10141/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
10142 already been loaded into memory. */
10143
10144static void
8fc0b21d 10145process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
10146 enum language pretend_language)
10147{
976ca316
SM
10148 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10149 struct objfile *objfile = per_objfile->objfile;
43f3e411 10150 struct compunit_symtab *cust;
0186c6a7
DE
10151 struct signatured_type *sig_type;
10152
8fc0b21d
SM
10153 gdb_assert (cu->per_cu->is_debug_types);
10154 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 10155
c89b44cd
TT
10156 /* Clear the list here in case something was left over. */
10157 cu->method_list.clear ();
f4dc4d17 10158
f4dc4d17
DE
10159 cu->language = pretend_language;
10160 cu->language_defn = language_def (cu->language);
10161
10162 /* The symbol tables are set up in read_type_unit_scope. */
10163 process_die (cu->dies, cu);
10164
10165 /* For now fudge the Go package. */
10166 if (cu->language == language_go)
10167 fixup_go_packaging (cu);
10168
5f48f8f3 10169 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10170 should be complete, and it should now be safe to compute all of the
10171 physnames. */
10172 compute_delayed_physnames (cu);
f4dc4d17 10173
c9317f21
TT
10174 if (cu->language == language_rust)
10175 rust_union_quirks (cu);
10176
f4dc4d17
DE
10177 /* TUs share symbol tables.
10178 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10179 of it with end_expandable_symtab. Otherwise, complete the addition of
10180 this TU's symbols to the existing symtab. */
8adb8487 10181 type_unit_group_unshareable *tug_unshare =
976ca316 10182 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 10183 if (tug_unshare->compunit_symtab == NULL)
45cfd468 10184 {
c24bdb02
KS
10185 buildsym_compunit *builder = cu->get_builder ();
10186 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 10187 tug_unshare->compunit_symtab = cust;
f4dc4d17 10188
43f3e411 10189 if (cust != NULL)
f4dc4d17
DE
10190 {
10191 /* Set symtab language to language from DW_AT_language. If the
10192 compilation is from a C file generated by language preprocessors,
10193 do not set the language if it was already deduced by
10194 start_subfile. */
43f3e411
DE
10195 if (!(cu->language == language_c
10196 && COMPUNIT_FILETABS (cust)->language != language_c))
10197 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10198 }
10199 }
10200 else
10201 {
c24bdb02 10202 cu->get_builder ()->augment_type_symtab ();
8adb8487 10203 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
10204 }
10205
976ca316 10206 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
10207
10208 /* Not needed any more. */
c24bdb02 10209 cu->reset_builder ();
c906108c
SS
10210}
10211
95554aad
TT
10212/* Process an imported unit DIE. */
10213
10214static void
10215process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10216{
10217 struct attribute *attr;
10218
f4dc4d17
DE
10219 /* For now we don't handle imported units in type units. */
10220 if (cu->per_cu->is_debug_types)
10221 {
10222 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10223 " supported in type units [in module %s]"),
5e22e966 10224 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
10225 }
10226
95554aad
TT
10227 attr = dwarf2_attr (die, DW_AT_import, cu);
10228 if (attr != NULL)
10229 {
0826b30a 10230 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 10231 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 10232 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 10233 dwarf2_per_cu_data *per_cu
ab432490 10234 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 10235
58990295
TV
10236 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10237 into another compilation unit, at root level. Regard this as a hint,
10238 and ignore it. */
10239 if (die->parent && die->parent->parent == NULL
10240 && per_cu->unit_type == DW_UT_compile
10241 && per_cu->lang == language_cplus)
10242 return;
10243
69d751e3 10244 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 10245 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
10246 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10247 false, cu->language);
95554aad 10248
ae640021 10249 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10250 }
10251}
10252
4c8aa72d
PA
10253/* RAII object that represents a process_die scope: i.e.,
10254 starts/finishes processing a DIE. */
10255class process_die_scope
adde2bff 10256{
4c8aa72d
PA
10257public:
10258 process_die_scope (die_info *die, dwarf2_cu *cu)
10259 : m_die (die), m_cu (cu)
10260 {
10261 /* We should only be processing DIEs not already in process. */
10262 gdb_assert (!m_die->in_process);
10263 m_die->in_process = true;
10264 }
8c3cb9fa 10265
4c8aa72d
PA
10266 ~process_die_scope ()
10267 {
10268 m_die->in_process = false;
10269
10270 /* If we're done processing the DIE for the CU that owns the line
10271 header, we don't need the line header anymore. */
10272 if (m_cu->line_header_die_owner == m_die)
10273 {
10274 delete m_cu->line_header;
10275 m_cu->line_header = NULL;
10276 m_cu->line_header_die_owner = NULL;
10277 }
10278 }
10279
10280private:
10281 die_info *m_die;
10282 dwarf2_cu *m_cu;
10283};
adde2bff 10284
c906108c
SS
10285/* Process a die and its children. */
10286
10287static void
e7c27a73 10288process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10289{
4c8aa72d 10290 process_die_scope scope (die, cu);
adde2bff 10291
c906108c
SS
10292 switch (die->tag)
10293 {
10294 case DW_TAG_padding:
10295 break;
10296 case DW_TAG_compile_unit:
95554aad 10297 case DW_TAG_partial_unit:
e7c27a73 10298 read_file_scope (die, cu);
c906108c 10299 break;
348e048f
DE
10300 case DW_TAG_type_unit:
10301 read_type_unit_scope (die, cu);
10302 break;
c906108c 10303 case DW_TAG_subprogram:
0a4b0913
AB
10304 /* Nested subprograms in Fortran get a prefix. */
10305 if (cu->language == language_fortran
10306 && die->parent != NULL
10307 && die->parent->tag == DW_TAG_subprogram)
10308 cu->processing_has_namespace_info = true;
10309 /* Fall through. */
c906108c 10310 case DW_TAG_inlined_subroutine:
edb3359d 10311 read_func_scope (die, cu);
c906108c
SS
10312 break;
10313 case DW_TAG_lexical_block:
14898363
L
10314 case DW_TAG_try_block:
10315 case DW_TAG_catch_block:
e7c27a73 10316 read_lexical_block_scope (die, cu);
c906108c 10317 break;
216f72a1 10318 case DW_TAG_call_site:
96408a79
SA
10319 case DW_TAG_GNU_call_site:
10320 read_call_site_scope (die, cu);
10321 break;
c906108c 10322 case DW_TAG_class_type:
680b30c7 10323 case DW_TAG_interface_type:
c906108c
SS
10324 case DW_TAG_structure_type:
10325 case DW_TAG_union_type:
134d01f1 10326 process_structure_scope (die, cu);
c906108c
SS
10327 break;
10328 case DW_TAG_enumeration_type:
134d01f1 10329 process_enumeration_scope (die, cu);
c906108c 10330 break;
134d01f1 10331
f792889a
DJ
10332 /* These dies have a type, but processing them does not create
10333 a symbol or recurse to process the children. Therefore we can
10334 read them on-demand through read_type_die. */
c906108c 10335 case DW_TAG_subroutine_type:
72019c9c 10336 case DW_TAG_set_type:
c906108c 10337 case DW_TAG_pointer_type:
c906108c 10338 case DW_TAG_ptr_to_member_type:
c906108c 10339 case DW_TAG_reference_type:
4297a3f0 10340 case DW_TAG_rvalue_reference_type:
c906108c 10341 case DW_TAG_string_type:
c906108c 10342 break;
134d01f1 10343
d8f62e84
TT
10344 case DW_TAG_array_type:
10345 /* We only need to handle this case for Ada -- in other
10346 languages, it's normal for the compiler to emit a typedef
10347 instead. */
10348 if (cu->language != language_ada)
10349 break;
10350 /* FALLTHROUGH */
c906108c 10351 case DW_TAG_base_type:
a02abb62 10352 case DW_TAG_subrange_type:
cb249c71 10353 case DW_TAG_typedef:
134d01f1 10354 /* Add a typedef symbol for the type definition, if it has a
dda83cd7 10355 DW_AT_name. */
f792889a 10356 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10357 break;
c906108c 10358 case DW_TAG_common_block:
e7c27a73 10359 read_common_block (die, cu);
c906108c
SS
10360 break;
10361 case DW_TAG_common_inclusion:
10362 break;
d9fa45fe 10363 case DW_TAG_namespace:
9068261f 10364 cu->processing_has_namespace_info = true;
e7c27a73 10365 read_namespace (die, cu);
d9fa45fe 10366 break;
5d7cb8df 10367 case DW_TAG_module:
9068261f 10368 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10369 read_module (die, cu);
10370 break;
d9fa45fe 10371 case DW_TAG_imported_declaration:
9068261f 10372 cu->processing_has_namespace_info = true;
74921315
KS
10373 if (read_namespace_alias (die, cu))
10374 break;
86a73007
TT
10375 /* The declaration is not a global namespace alias. */
10376 /* Fall through. */
d9fa45fe 10377 case DW_TAG_imported_module:
9068261f 10378 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10379 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10380 || cu->language != language_fortran))
b98664d3 10381 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10382 dwarf_tag_name (die->tag));
10383 read_import_statement (die, cu);
d9fa45fe 10384 break;
95554aad
TT
10385
10386 case DW_TAG_imported_unit:
10387 process_imported_unit_die (die, cu);
10388 break;
10389
71a3c369
TT
10390 case DW_TAG_variable:
10391 read_variable (die, cu);
10392 break;
10393
c906108c 10394 default:
e7c27a73 10395 new_symbol (die, NULL, cu);
c906108c
SS
10396 break;
10397 }
10398}
ca69b9e6
DE
10399\f
10400/* DWARF name computation. */
c906108c 10401
94af9270
KS
10402/* A helper function for dwarf2_compute_name which determines whether DIE
10403 needs to have the name of the scope prepended to the name listed in the
10404 die. */
10405
10406static int
10407die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10408{
1c809c68
TT
10409 struct attribute *attr;
10410
94af9270
KS
10411 switch (die->tag)
10412 {
10413 case DW_TAG_namespace:
10414 case DW_TAG_typedef:
10415 case DW_TAG_class_type:
10416 case DW_TAG_interface_type:
10417 case DW_TAG_structure_type:
10418 case DW_TAG_union_type:
10419 case DW_TAG_enumeration_type:
10420 case DW_TAG_enumerator:
10421 case DW_TAG_subprogram:
08a76f8a 10422 case DW_TAG_inlined_subroutine:
94af9270 10423 case DW_TAG_member:
74921315 10424 case DW_TAG_imported_declaration:
94af9270
KS
10425 return 1;
10426
10427 case DW_TAG_variable:
c2b0a229 10428 case DW_TAG_constant:
94af9270
KS
10429 /* We only need to prefix "globally" visible variables. These include
10430 any variable marked with DW_AT_external or any variable that
10431 lives in a namespace. [Variables in anonymous namespaces
10432 require prefixing, but they are not DW_AT_external.] */
10433
10434 if (dwarf2_attr (die, DW_AT_specification, cu))
10435 {
10436 struct dwarf2_cu *spec_cu = cu;
9a619af0 10437
94af9270
KS
10438 return die_needs_namespace (die_specification (die, &spec_cu),
10439 spec_cu);
10440 }
10441
1c809c68 10442 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10443 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10444 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10445 return 0;
10446 /* A variable in a lexical block of some kind does not need a
10447 namespace, even though in C++ such variables may be external
10448 and have a mangled name. */
10449 if (die->parent->tag == DW_TAG_lexical_block
10450 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10451 || die->parent->tag == DW_TAG_catch_block
10452 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10453 return 0;
10454 return 1;
94af9270
KS
10455
10456 default:
10457 return 0;
10458 }
10459}
10460
73b9be8b
KS
10461/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10462 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10463 defined for the given DIE. */
10464
10465static struct attribute *
10466dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10467{
10468 struct attribute *attr;
10469
10470 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10471 if (attr == NULL)
10472 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10473
10474 return attr;
10475}
10476
10477/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10478 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10479 defined for the given DIE. */
10480
10481static const char *
10482dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10483{
10484 const char *linkage_name;
10485
10486 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10487 if (linkage_name == NULL)
10488 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10489
787de330
TT
10490 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10491 See https://github.com/rust-lang/rust/issues/32925. */
10492 if (cu->language == language_rust && linkage_name != NULL
10493 && strchr (linkage_name, '{') != NULL)
10494 linkage_name = NULL;
10495
73b9be8b
KS
10496 return linkage_name;
10497}
10498
94af9270 10499/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10500 compute the physname for the object, which include a method's:
9c37b5ae 10501 - formal parameters (C++),
a766d390 10502 - receiver type (Go),
a766d390
DE
10503
10504 The term "physname" is a bit confusing.
10505 For C++, for example, it is the demangled name.
10506 For Go, for example, it's the mangled name.
94af9270 10507
af6b7be1
JB
10508 For Ada, return the DIE's linkage name rather than the fully qualified
10509 name. PHYSNAME is ignored..
10510
5989a64e 10511 The result is allocated on the objfile->per_bfd's obstack and
45940949 10512 canonicalized. */
94af9270
KS
10513
10514static const char *
15d034d0
TT
10515dwarf2_compute_name (const char *name,
10516 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10517 int physname)
10518{
5e22e966 10519 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10520
94af9270
KS
10521 if (name == NULL)
10522 name = dwarf2_name (die, cu);
10523
2ee7123e
DE
10524 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10525 but otherwise compute it by typename_concat inside GDB.
10526 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10527 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10528 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10529 will set the demangled name to the result of dwarf2_full_name, and it is
10530 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10531 if (cu->language == language_ada
10532 || (cu->language == language_fortran && physname))
10533 {
10534 /* For Ada unit, we prefer the linkage name over the name, as
10535 the former contains the exported name, which the user expects
10536 to be able to reference. Ideally, we want the user to be able
10537 to reference this entity using either natural or linkage name,
10538 but we haven't started looking at this enhancement yet. */
73b9be8b 10539 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10540
2ee7123e
DE
10541 if (linkage_name != NULL)
10542 return linkage_name;
f55ee35c
JK
10543 }
10544
94af9270
KS
10545 /* These are the only languages we know how to qualify names in. */
10546 if (name != NULL
9c37b5ae 10547 && (cu->language == language_cplus
c44af4eb
TT
10548 || cu->language == language_fortran || cu->language == language_d
10549 || cu->language == language_rust))
94af9270
KS
10550 {
10551 if (die_needs_namespace (die, cu))
10552 {
0d5cff50 10553 const char *prefix;
34a68019 10554 const char *canonical_name = NULL;
94af9270 10555
d7e74731
PA
10556 string_file buf;
10557
94af9270 10558 prefix = determine_prefix (die, cu);
94af9270
KS
10559 if (*prefix != '\0')
10560 {
43816ebc
TT
10561 gdb::unique_xmalloc_ptr<char> prefixed_name
10562 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10563
43816ebc 10564 buf.puts (prefixed_name.get ());
94af9270
KS
10565 }
10566 else
d7e74731 10567 buf.puts (name);
94af9270 10568
98bfdba5
PA
10569 /* Template parameters may be specified in the DIE's DW_AT_name, or
10570 as children with DW_TAG_template_type_param or
10571 DW_TAG_value_type_param. If the latter, add them to the name
10572 here. If the name already has template parameters, then
10573 skip this step; some versions of GCC emit both, and
10574 it is more efficient to use the pre-computed name.
10575
10576 Something to keep in mind about this process: it is very
10577 unlikely, or in some cases downright impossible, to produce
10578 something that will match the mangled name of a function.
10579 If the definition of the function has the same debug info,
10580 we should be able to match up with it anyway. But fallbacks
10581 using the minimal symbol, for instance to find a method
10582 implemented in a stripped copy of libstdc++, will not work.
10583 If we do not have debug info for the definition, we will have to
10584 match them up some other way.
10585
10586 When we do name matching there is a related problem with function
10587 templates; two instantiated function templates are allowed to
10588 differ only by their return types, which we do not add here. */
10589
10590 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10591 {
10592 struct attribute *attr;
10593 struct die_info *child;
10594 int first = 1;
2c75ccb2 10595 const language_defn *cplus_lang = language_def (cu->language);
98bfdba5
PA
10596
10597 die->building_fullname = 1;
10598
10599 for (child = die->child; child != NULL; child = child->sibling)
10600 {
10601 struct type *type;
12df843f 10602 LONGEST value;
d521ce57 10603 const gdb_byte *bytes;
98bfdba5
PA
10604 struct dwarf2_locexpr_baton *baton;
10605 struct value *v;
10606
10607 if (child->tag != DW_TAG_template_type_param
10608 && child->tag != DW_TAG_template_value_param)
10609 continue;
10610
10611 if (first)
10612 {
d7e74731 10613 buf.puts ("<");
98bfdba5
PA
10614 first = 0;
10615 }
10616 else
d7e74731 10617 buf.puts (", ");
98bfdba5
PA
10618
10619 attr = dwarf2_attr (child, DW_AT_type, cu);
10620 if (attr == NULL)
10621 {
b98664d3 10622 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10623 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10624 continue;
10625 }
10626 type = die_type (child, cu);
10627
10628 if (child->tag == DW_TAG_template_type_param)
10629 {
2c75ccb2
AB
10630 cplus_lang->print_type (type, "", &buf, -1, 0,
10631 &type_print_raw_options);
98bfdba5
PA
10632 continue;
10633 }
10634
10635 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10636 if (attr == NULL)
10637 {
b98664d3 10638 complaint (_("template parameter missing "
3e43a32a 10639 "DW_AT_const_value"));
d7e74731 10640 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10641 continue;
10642 }
10643
10644 dwarf2_const_value_attr (attr, type, name,
10645 &cu->comp_unit_obstack, cu,
10646 &value, &bytes, &baton);
10647
20ce4123 10648 if (type->has_no_signedness ())
98bfdba5
PA
10649 /* GDB prints characters as NUMBER 'CHAR'. If that's
10650 changed, this can use value_print instead. */
2c75ccb2 10651 cplus_lang->printchar (value, type, &buf);
98bfdba5
PA
10652 else
10653 {
10654 struct value_print_options opts;
10655
10656 if (baton != NULL)
10657 v = dwarf2_evaluate_loc_desc (type, NULL,
10658 baton->data,
10659 baton->size,
9f47c707
SM
10660 baton->per_cu,
10661 baton->per_objfile);
98bfdba5
PA
10662 else if (bytes != NULL)
10663 {
10664 v = allocate_value (type);
10665 memcpy (value_contents_writeable (v), bytes,
10666 TYPE_LENGTH (type));
10667 }
10668 else
10669 v = value_from_longest (type, value);
10670
3e43a32a
MS
10671 /* Specify decimal so that we do not depend on
10672 the radix. */
98bfdba5
PA
10673 get_formatted_print_options (&opts, 'd');
10674 opts.raw = 1;
d7e74731 10675 value_print (v, &buf, &opts);
98bfdba5 10676 release_value (v);
98bfdba5
PA
10677 }
10678 }
10679
10680 die->building_fullname = 0;
10681
10682 if (!first)
10683 {
10684 /* Close the argument list, with a space if necessary
10685 (nested templates). */
d7e74731
PA
10686 if (!buf.empty () && buf.string ().back () == '>')
10687 buf.puts (" >");
98bfdba5 10688 else
d7e74731 10689 buf.puts (">");
98bfdba5
PA
10690 }
10691 }
10692
9c37b5ae 10693 /* For C++ methods, append formal parameter type
94af9270 10694 information, if PHYSNAME. */
6e70227d 10695
94af9270 10696 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10697 && cu->language == language_cplus)
94af9270
KS
10698 {
10699 struct type *type = read_type_die (die, cu);
10700
d7e74731 10701 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10702 &type_print_raw_options);
94af9270 10703
9c37b5ae 10704 if (cu->language == language_cplus)
94af9270 10705 {
60430eff
DJ
10706 /* Assume that an artificial first parameter is
10707 "this", but do not crash if it is not. RealView
10708 marks unnamed (and thus unused) parameters as
10709 artificial; there is no way to differentiate
10710 the two cases. */
1f704f76 10711 if (type->num_fields () > 0
94af9270 10712 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
10713 && type->field (0).type ()->code () == TYPE_CODE_PTR
10714 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 10715 buf.puts (" const");
94af9270
KS
10716 }
10717 }
10718
d7e74731 10719 const std::string &intermediate_name = buf.string ();
94af9270
KS
10720
10721 if (cu->language == language_cplus)
34a68019 10722 canonical_name
322a8516 10723 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10724 objfile);
34a68019
TT
10725
10726 /* If we only computed INTERMEDIATE_NAME, or if
10727 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10728 intern it. */
322a8516 10729 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10730 name = objfile->intern (intermediate_name);
34a68019
TT
10731 else
10732 name = canonical_name;
94af9270
KS
10733 }
10734 }
10735
10736 return name;
10737}
10738
0114d602
DJ
10739/* Return the fully qualified name of DIE, based on its DW_AT_name.
10740 If scope qualifiers are appropriate they will be added. The result
34a68019 10741 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10742 not have a name. NAME may either be from a previous call to
10743 dwarf2_name or NULL.
10744
9c37b5ae 10745 The output string will be canonicalized (if C++). */
0114d602
DJ
10746
10747static const char *
15d034d0 10748dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10749{
94af9270
KS
10750 return dwarf2_compute_name (name, die, cu, 0);
10751}
0114d602 10752
94af9270
KS
10753/* Construct a physname for the given DIE in CU. NAME may either be
10754 from a previous call to dwarf2_name or NULL. The result will be
10755 allocated on the objfile_objstack or NULL if the DIE does not have a
10756 name.
0114d602 10757
9c37b5ae 10758 The output string will be canonicalized (if C++). */
0114d602 10759
94af9270 10760static const char *
15d034d0 10761dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10762{
5e22e966 10763 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10764 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10765 int need_copy = 1;
10766
10767 /* In this case dwarf2_compute_name is just a shortcut not building anything
10768 on its own. */
10769 if (!die_needs_namespace (die, cu))
10770 return dwarf2_compute_name (name, die, cu, 1);
10771
906bb4c5
TT
10772 if (cu->language != language_rust)
10773 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10774
10775 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10776 has computed. */
791afaa2 10777 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10778 if (mangled != NULL)
900e11f9 10779 {
900e11f9 10780
d3355e4d 10781 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
59cc4834
JB
10782 {
10783 /* Do nothing (do not demangle the symbol name). */
10784 }
a766d390
DE
10785 else
10786 {
0eb876f5
JB
10787 /* Use DMGL_RET_DROP for C++ template functions to suppress
10788 their return type. It is easier for GDB users to search
10789 for such functions as `name(params)' than `long name(params)'.
10790 In such case the minimal symbol names do not match the full
10791 symbol names but for template functions there is never a need
10792 to look up their definition from their declaration so
10793 the only disadvantage remains the minimal symbol variant
10794 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10795 demangled.reset (gdb_demangle (mangled,
10796 (DMGL_PARAMS | DMGL_ANSI
10797 | DMGL_RET_DROP)));
a766d390 10798 }
900e11f9 10799 if (demangled)
791afaa2 10800 canon = demangled.get ();
900e11f9
JK
10801 else
10802 {
10803 canon = mangled;
10804 need_copy = 0;
10805 }
10806 }
10807
10808 if (canon == NULL || check_physname)
10809 {
10810 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10811
10812 if (canon != NULL && strcmp (physname, canon) != 0)
10813 {
10814 /* It may not mean a bug in GDB. The compiler could also
10815 compute DW_AT_linkage_name incorrectly. But in such case
10816 GDB would need to be bug-to-bug compatible. */
10817
b98664d3 10818 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10819 "(from linkage <%s>) - DIE at %s [in module %s]"),
10820 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10821 objfile_name (objfile));
900e11f9
JK
10822
10823 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10824 is available here - over computed PHYSNAME. It is safer
10825 against both buggy GDB and buggy compilers. */
10826
10827 retval = canon;
10828 }
10829 else
10830 {
10831 retval = physname;
10832 need_copy = 0;
10833 }
10834 }
10835 else
10836 retval = canon;
10837
10838 if (need_copy)
be1e3d3e 10839 retval = objfile->intern (retval);
900e11f9 10840
900e11f9 10841 return retval;
0114d602
DJ
10842}
10843
74921315
KS
10844/* Inspect DIE in CU for a namespace alias. If one exists, record
10845 a new symbol for it.
10846
10847 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10848
10849static int
10850read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10851{
10852 struct attribute *attr;
10853
10854 /* If the die does not have a name, this is not a namespace
10855 alias. */
10856 attr = dwarf2_attr (die, DW_AT_name, cu);
10857 if (attr != NULL)
10858 {
10859 int num;
10860 struct die_info *d = die;
10861 struct dwarf2_cu *imported_cu = cu;
10862
10863 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10864 keep inspecting DIEs until we hit the underlying import. */
10865#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10866 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10867 {
10868 attr = dwarf2_attr (d, DW_AT_import, cu);
10869 if (attr == NULL)
10870 break;
10871
10872 d = follow_die_ref (d, attr, &imported_cu);
10873 if (d->tag != DW_TAG_imported_declaration)
10874 break;
10875 }
10876
10877 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10878 {
b98664d3 10879 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10880 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10881 return 0;
10882 }
10883
10884 if (attr != NULL)
10885 {
10886 struct type *type;
0826b30a 10887 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10888
aa66c379 10889 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10890 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10891 {
10892 /* This declaration is a global namespace alias. Add
10893 a symbol for it whose type is the aliased namespace. */
10894 new_symbol (die, type, cu);
10895 return 1;
10896 }
10897 }
10898 }
10899
10900 return 0;
10901}
10902
22cee43f 10903/* Return the using directives repository (global or local?) to use in the
804d2729 10904 current context for CU.
22cee43f
PMR
10905
10906 For Ada, imported declarations can materialize renamings, which *may* be
10907 global. However it is impossible (for now?) in DWARF to distinguish
10908 "external" imported declarations and "static" ones. As all imported
10909 declarations seem to be static in all other languages, make them all CU-wide
10910 global only in Ada. */
10911
10912static struct using_direct **
804d2729 10913using_directives (struct dwarf2_cu *cu)
22cee43f 10914{
c24bdb02
KS
10915 if (cu->language == language_ada
10916 && cu->get_builder ()->outermost_context_p ())
10917 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10918 else
c24bdb02 10919 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10920}
10921
27aa8d6a
SW
10922/* Read the import statement specified by the given die and record it. */
10923
10924static void
10925read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10926{
5e22e966 10927 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10928 struct attribute *import_attr;
32019081 10929 struct die_info *imported_die, *child_die;
de4affc9 10930 struct dwarf2_cu *imported_cu;
27aa8d6a 10931 const char *imported_name;
794684b6 10932 const char *imported_name_prefix;
13387711
SW
10933 const char *canonical_name;
10934 const char *import_alias;
10935 const char *imported_declaration = NULL;
794684b6 10936 const char *import_prefix;
eb1e02fd 10937 std::vector<const char *> excludes;
13387711 10938
27aa8d6a
SW
10939 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10940 if (import_attr == NULL)
10941 {
b98664d3 10942 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10943 dwarf_tag_name (die->tag));
10944 return;
10945 }
10946
de4affc9
CC
10947 imported_cu = cu;
10948 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10949 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10950 if (imported_name == NULL)
10951 {
10952 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10953
dda83cd7
SM
10954 The import in the following code:
10955 namespace A
10956 {
10957 typedef int B;
10958 }
10959
10960 int main ()
10961 {
10962 using A::B;
10963 B b;
10964 return b;
10965 }
10966
10967 ...
10968 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10969 <52> DW_AT_decl_file : 1
10970 <53> DW_AT_decl_line : 6
10971 <54> DW_AT_import : <0x75>
10972 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10973 <59> DW_AT_name : B
10974 <5b> DW_AT_decl_file : 1
10975 <5c> DW_AT_decl_line : 2
10976 <5d> DW_AT_type : <0x6e>
10977 ...
10978 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10979 <76> DW_AT_byte_size : 4
10980 <77> DW_AT_encoding : 5 (signed)
10981
10982 imports the wrong die ( 0x75 instead of 0x58 ).
10983 This case will be ignored until the gcc bug is fixed. */
27aa8d6a
SW
10984 return;
10985 }
10986
82856980
SW
10987 /* Figure out the local name after import. */
10988 import_alias = dwarf2_name (die, cu);
27aa8d6a 10989
794684b6
SW
10990 /* Figure out where the statement is being imported to. */
10991 import_prefix = determine_prefix (die, cu);
10992
10993 /* Figure out what the scope of the imported die is and prepend it
10994 to the name of the imported die. */
de4affc9 10995 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10996
f55ee35c
JK
10997 if (imported_die->tag != DW_TAG_namespace
10998 && imported_die->tag != DW_TAG_module)
794684b6 10999 {
13387711
SW
11000 imported_declaration = imported_name;
11001 canonical_name = imported_name_prefix;
794684b6 11002 }
13387711 11003 else if (strlen (imported_name_prefix) > 0)
12aaed36 11004 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11005 imported_name_prefix,
11006 (cu->language == language_d ? "." : "::"),
11007 imported_name, (char *) NULL);
13387711
SW
11008 else
11009 canonical_name = imported_name;
794684b6 11010
32019081
JK
11011 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11012 for (child_die = die->child; child_die && child_die->tag;
436c571c 11013 child_die = child_die->sibling)
32019081
JK
11014 {
11015 /* DWARF-4: A Fortran use statement with a “rename list” may be
11016 represented by an imported module entry with an import attribute
11017 referring to the module and owned entries corresponding to those
11018 entities that are renamed as part of being imported. */
11019
11020 if (child_die->tag != DW_TAG_imported_declaration)
11021 {
b98664d3 11022 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11023 "- DIE at %s [in module %s]"),
11024 sect_offset_str (child_die->sect_off),
11025 objfile_name (objfile));
32019081
JK
11026 continue;
11027 }
11028
11029 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11030 if (import_attr == NULL)
11031 {
b98664d3 11032 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11033 dwarf_tag_name (child_die->tag));
11034 continue;
11035 }
11036
11037 imported_cu = cu;
11038 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11039 &imported_cu);
11040 imported_name = dwarf2_name (imported_die, imported_cu);
11041 if (imported_name == NULL)
11042 {
b98664d3 11043 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11044 "imported name - DIE at %s [in module %s]"),
11045 sect_offset_str (child_die->sect_off),
11046 objfile_name (objfile));
32019081
JK
11047 continue;
11048 }
11049
eb1e02fd 11050 excludes.push_back (imported_name);
32019081
JK
11051
11052 process_die (child_die, cu);
11053 }
11054
804d2729 11055 add_using_directive (using_directives (cu),
22cee43f
PMR
11056 import_prefix,
11057 canonical_name,
11058 import_alias,
11059 imported_declaration,
11060 excludes,
11061 0,
11062 &objfile->objfile_obstack);
27aa8d6a
SW
11063}
11064
5230b05a
WT
11065/* ICC<14 does not output the required DW_AT_declaration on incomplete
11066 types, but gives them a size of zero. Starting with version 14,
11067 ICC is compatible with GCC. */
11068
9068261f 11069static bool
5230b05a
WT
11070producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11071{
11072 if (!cu->checked_producer)
11073 check_producer (cu);
11074
11075 return cu->producer_is_icc_lt_14;
11076}
11077
eb77c9df
AB
11078/* ICC generates a DW_AT_type for C void functions. This was observed on
11079 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11080 which says that void functions should not have a DW_AT_type. */
11081
11082static bool
11083producer_is_icc (struct dwarf2_cu *cu)
11084{
11085 if (!cu->checked_producer)
11086 check_producer (cu);
11087
11088 return cu->producer_is_icc;
11089}
11090
1b80a9fa
JK
11091/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11092 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11093 this, it was first present in GCC release 4.3.0. */
11094
9068261f 11095static bool
1b80a9fa
JK
11096producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11097{
11098 if (!cu->checked_producer)
11099 check_producer (cu);
11100
11101 return cu->producer_is_gcc_lt_4_3;
11102}
11103
d721ba37
PA
11104static file_and_directory
11105find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11106{
d721ba37
PA
11107 file_and_directory res;
11108
9291a0cd
TT
11109 /* Find the filename. Do not use dwarf2_name here, since the filename
11110 is not a source language identifier. */
d721ba37
PA
11111 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11112 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11113
d721ba37
PA
11114 if (res.comp_dir == NULL
11115 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11116 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11117 {
d721ba37
PA
11118 res.comp_dir_storage = ldirname (res.name);
11119 if (!res.comp_dir_storage.empty ())
11120 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11121 }
d721ba37 11122 if (res.comp_dir != NULL)
9291a0cd
TT
11123 {
11124 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11125 directory, get rid of it. */
d721ba37 11126 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11127
d721ba37
PA
11128 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11129 res.comp_dir = cp + 1;
9291a0cd
TT
11130 }
11131
d721ba37
PA
11132 if (res.name == NULL)
11133 res.name = "<unknown>";
11134
11135 return res;
9291a0cd
TT
11136}
11137
f4dc4d17
DE
11138/* Handle DW_AT_stmt_list for a compilation unit.
11139 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11140 COMP_DIR is the compilation directory. LOWPC is passed to
11141 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11142
11143static void
11144handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11145 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11146{
976ca316 11147 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 11148 struct attribute *attr;
527f3840
JK
11149 struct line_header line_header_local;
11150 hashval_t line_header_local_hash;
527f3840
JK
11151 void **slot;
11152 int decode_mapping;
2ab95328 11153
f4dc4d17
DE
11154 gdb_assert (! cu->per_cu->is_debug_types);
11155
2ab95328 11156 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e 11157 if (attr == NULL || !attr->form_is_unsigned ())
527f3840
JK
11158 return;
11159
d4df075e 11160 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
527f3840
JK
11161
11162 /* The line header hash table is only created if needed (it exists to
11163 prevent redundant reading of the line table for partial_units).
11164 If we're given a partial_unit, we'll need it. If we're given a
11165 compile_unit, then use the line header hash table if it's already
11166 created, but don't create one just yet. */
11167
976ca316 11168 if (per_objfile->line_header_hash == NULL
527f3840 11169 && die->tag == DW_TAG_partial_unit)
2ab95328 11170 {
976ca316 11171 per_objfile->line_header_hash
d15acc42
TT
11172 .reset (htab_create_alloc (127, line_header_hash_voidp,
11173 line_header_eq_voidp,
11174 free_line_header_voidp,
11175 xcalloc, xfree));
527f3840 11176 }
2ab95328 11177
9c541725 11178 line_header_local.sect_off = line_offset;
527f3840
JK
11179 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11180 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 11181 if (per_objfile->line_header_hash != NULL)
527f3840 11182 {
976ca316 11183 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11184 &line_header_local,
11185 line_header_local_hash, NO_INSERT);
11186
11187 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11188 is not present in *SLOT (since if there is something in *SLOT then
11189 it will be for a partial_unit). */
11190 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11191 {
527f3840 11192 gdb_assert (*slot != NULL);
9a3c8263 11193 cu->line_header = (struct line_header *) *slot;
527f3840 11194 return;
dee91e82 11195 }
2ab95328 11196 }
527f3840
JK
11197
11198 /* dwarf_decode_line_header does not yet provide sufficient information.
11199 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11200 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11201 if (lh == NULL)
527f3840 11202 return;
4c8aa72d
PA
11203
11204 cu->line_header = lh.release ();
11205 cu->line_header_die_owner = die;
527f3840 11206
976ca316 11207 if (per_objfile->line_header_hash == NULL)
527f3840
JK
11208 slot = NULL;
11209 else
11210 {
976ca316 11211 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11212 &line_header_local,
11213 line_header_local_hash, INSERT);
11214 gdb_assert (slot != NULL);
11215 }
11216 if (slot != NULL && *slot == NULL)
11217 {
11218 /* This newly decoded line number information unit will be owned
11219 by line_header_hash hash table. */
11220 *slot = cu->line_header;
4c8aa72d 11221 cu->line_header_die_owner = NULL;
527f3840
JK
11222 }
11223 else
11224 {
11225 /* We cannot free any current entry in (*slot) as that struct line_header
dda83cd7 11226 may be already used by multiple CUs. Create only temporary decoded
527f3840
JK
11227 line_header for this CU - it may happen at most once for each line
11228 number information unit. And if we're not using line_header_hash
11229 then this is what we want as well. */
11230 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11231 }
11232 decode_mapping = (die->tag != DW_TAG_partial_unit);
11233 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11234 decode_mapping);
fff8551c 11235
2ab95328
TT
11236}
11237
95554aad 11238/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11239
c906108c 11240static void
e7c27a73 11241read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11242{
976ca316
SM
11243 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11244 struct objfile *objfile = per_objfile->objfile;
08feed99 11245 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 11246 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11247 CORE_ADDR highpc = ((CORE_ADDR) 0);
11248 struct attribute *attr;
c906108c 11249 struct die_info *child_die;
e142c38c 11250 CORE_ADDR baseaddr;
6e70227d 11251
380618d6 11252 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11253 baseaddr = objfile->text_section_offset ();
c906108c 11254
fae299cd 11255 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11256
11257 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11258 from finish_block. */
2acceee2 11259 if (lowpc == ((CORE_ADDR) -1))
c906108c 11260 lowpc = highpc;
3e29f34a 11261 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11262
d721ba37 11263 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11264
f4b8a18d
KW
11265 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11266 standardised yet. As a workaround for the language detection we fall
11267 back to the DW_AT_producer string. */
11268 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11269 cu->language = language_opencl;
11270
3019eac3
DE
11271 /* Similar hack for Go. */
11272 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11273 set_cu_language (DW_LANG_Go, cu);
11274
c24bdb02 11275 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11276
11277 /* Decode line number information if present. We do this before
11278 processing child DIEs, so that the line header table is available
11279 for DW_AT_decl_file. */
d721ba37 11280 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11281
11282 /* Process all dies in compilation unit. */
11283 if (die->child != NULL)
11284 {
11285 child_die = die->child;
11286 while (child_die && child_die->tag)
11287 {
11288 process_die (child_die, cu);
436c571c 11289 child_die = child_die->sibling;
3019eac3
DE
11290 }
11291 }
11292
11293 /* Decode macro information, if present. Dwarf 2 macro information
11294 refers to information in the line number info statement program
11295 header, so we can only read it if we've read the header
11296 successfully. */
0af92d60
JK
11297 attr = dwarf2_attr (die, DW_AT_macros, cu);
11298 if (attr == NULL)
11299 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
529908cb 11300 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3
DE
11301 {
11302 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11303 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11304
529908cb 11305 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
3019eac3
DE
11306 }
11307 else
11308 {
11309 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
529908cb 11310 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3 11311 {
529908cb 11312 unsigned int macro_offset = attr->as_unsigned ();
3019eac3 11313
43f3e411 11314 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11315 }
11316 }
3019eac3
DE
11317}
11318
c24bdb02
KS
11319void
11320dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11321{
f4dc4d17
DE
11322 struct type_unit_group *tu_group;
11323 int first_time;
3019eac3 11324 struct attribute *attr;
9c541725 11325 unsigned int i;
0186c6a7 11326 struct signatured_type *sig_type;
3019eac3 11327
f4dc4d17 11328 gdb_assert (per_cu->is_debug_types);
0186c6a7 11329 sig_type = (struct signatured_type *) per_cu;
3019eac3 11330
c24bdb02 11331 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11332
f4dc4d17 11333 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11334 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11335 if (sig_type->type_unit_group == NULL)
c24bdb02 11336 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11337 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11338
11339 /* If we've already processed this stmt_list there's no real need to
11340 do it again, we could fake it and just recreate the part we need
11341 (file name,index -> symtab mapping). If data shows this optimization
11342 is useful we can do it then. */
8adb8487
TT
11343 type_unit_group_unshareable *tug_unshare
11344 = per_objfile->get_type_unit_group_unshareable (tu_group);
11345 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11346
11347 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11348 debug info. */
fff8551c 11349 line_header_up lh;
d4df075e 11350 if (attr != NULL && attr->form_is_unsigned ())
3019eac3 11351 {
d4df075e 11352 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
c24bdb02 11353 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11354 }
11355 if (lh == NULL)
11356 {
11357 if (first_time)
c24bdb02 11358 start_symtab ("", NULL, 0);
f4dc4d17
DE
11359 else
11360 {
8adb8487 11361 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11362 gdb_assert (m_builder == nullptr);
8adb8487 11363 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11364 m_builder.reset (new struct buildsym_compunit
11365 (COMPUNIT_OBJFILE (cust), "",
11366 COMPUNIT_DIRNAME (cust),
11367 compunit_language (cust),
11368 0, cust));
770479f2 11369 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11370 }
f4dc4d17 11371 return;
3019eac3
DE
11372 }
11373
c24bdb02
KS
11374 line_header = lh.release ();
11375 line_header_die_owner = die;
3019eac3 11376
f4dc4d17
DE
11377 if (first_time)
11378 {
c24bdb02 11379 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11380
1fd60fc0
DE
11381 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11382 still initializing it, and our caller (a few levels up)
11383 process_full_type_unit still needs to know if this is the first
11384 time. */
11385
8adb8487 11386 tug_unshare->symtabs
4ac93832
TT
11387 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11388 struct symtab *, line_header->file_names_size ());
3019eac3 11389
7ba99d21
AT
11390 auto &file_names = line_header->file_names ();
11391 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11392 {
7ba99d21 11393 file_entry &fe = file_names[i];
c24bdb02
KS
11394 dwarf2_start_subfile (this, fe.name,
11395 fe.include_dir (line_header));
11396 buildsym_compunit *b = get_builder ();
11397 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11398 {
4c8aa72d
PA
11399 /* NOTE: start_subfile will recognize when it's been
11400 passed a file it has already seen. So we can't
11401 assume there's a simple mapping from
11402 cu->line_header->file_names to subfiles, plus
11403 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11404 b->get_current_subfile ()->symtab
11405 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11406 }
11407
c24bdb02 11408 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11409 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11410 }
11411 }
11412 else
3019eac3 11413 {
c24bdb02 11414 gdb_assert (m_builder == nullptr);
8adb8487 11415 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11416 m_builder.reset (new struct buildsym_compunit
11417 (COMPUNIT_OBJFILE (cust), "",
11418 COMPUNIT_DIRNAME (cust),
11419 compunit_language (cust),
11420 0, cust));
770479f2 11421 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11422
7ba99d21
AT
11423 auto &file_names = line_header->file_names ();
11424 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11425 {
7ba99d21 11426 file_entry &fe = file_names[i];
8adb8487 11427 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11428 }
3019eac3
DE
11429 }
11430
f4dc4d17
DE
11431 /* The main symtab is allocated last. Type units don't have DW_AT_name
11432 so they don't have a "real" (so to speak) symtab anyway.
11433 There is later code that will assign the main symtab to all symbols
11434 that don't have one. We need to handle the case of a symbol with a
11435 missing symtab (DW_AT_decl_file) anyway. */
11436}
3019eac3 11437
f4dc4d17
DE
11438/* Process DW_TAG_type_unit.
11439 For TUs we want to skip the first top level sibling if it's not the
11440 actual type being defined by this TU. In this case the first top
11441 level sibling is there to provide context only. */
3019eac3 11442
f4dc4d17
DE
11443static void
11444read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11445{
11446 struct die_info *child_die;
3019eac3 11447
f4dc4d17
DE
11448 prepare_one_comp_unit (cu, die, language_minimal);
11449
11450 /* Initialize (or reinitialize) the machinery for building symtabs.
11451 We do this before processing child DIEs, so that the line header table
11452 is available for DW_AT_decl_file. */
c24bdb02 11453 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11454
11455 if (die->child != NULL)
11456 {
11457 child_die = die->child;
11458 while (child_die && child_die->tag)
11459 {
11460 process_die (child_die, cu);
436c571c 11461 child_die = child_die->sibling;
f4dc4d17
DE
11462 }
11463 }
3019eac3
DE
11464}
11465\f
80626a55
DE
11466/* DWO/DWP files.
11467
11468 http://gcc.gnu.org/wiki/DebugFission
11469 http://gcc.gnu.org/wiki/DebugFissionDWP
11470
11471 To simplify handling of both DWO files ("object" files with the DWARF info)
11472 and DWP files (a file with the DWOs packaged up into one file), we treat
11473 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11474
11475static hashval_t
11476hash_dwo_file (const void *item)
11477{
9a3c8263 11478 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11479 hashval_t hash;
3019eac3 11480
a2ce51a0
DE
11481 hash = htab_hash_string (dwo_file->dwo_name);
11482 if (dwo_file->comp_dir != NULL)
11483 hash += htab_hash_string (dwo_file->comp_dir);
11484 return hash;
3019eac3
DE
11485}
11486
11487static int
11488eq_dwo_file (const void *item_lhs, const void *item_rhs)
11489{
9a3c8263
SM
11490 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11491 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11492
a2ce51a0
DE
11493 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11494 return 0;
11495 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11496 return lhs->comp_dir == rhs->comp_dir;
11497 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11498}
11499
11500/* Allocate a hash table for DWO files. */
11501
51ac9db5 11502static htab_up
298e9637 11503allocate_dwo_file_hash_table ()
3019eac3 11504{
51ac9db5
SM
11505 auto delete_dwo_file = [] (void *item)
11506 {
11507 struct dwo_file *dwo_file = (struct dwo_file *) item;
11508
11509 delete dwo_file;
11510 };
11511
bc68fb19
TT
11512 return htab_up (htab_create_alloc (41,
11513 hash_dwo_file,
11514 eq_dwo_file,
11515 delete_dwo_file,
11516 xcalloc, xfree));
3019eac3
DE
11517}
11518
80626a55
DE
11519/* Lookup DWO file DWO_NAME. */
11520
11521static void **
976ca316 11522lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11523 const char *dwo_name,
11524 const char *comp_dir)
80626a55
DE
11525{
11526 struct dwo_file find_entry;
11527 void **slot;
11528
976ca316
SM
11529 if (per_objfile->per_bfd->dwo_files == NULL)
11530 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11531
0ac5b59e
DE
11532 find_entry.dwo_name = dwo_name;
11533 find_entry.comp_dir = comp_dir;
976ca316 11534 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11535 INSERT);
80626a55
DE
11536
11537 return slot;
11538}
11539
3019eac3
DE
11540static hashval_t
11541hash_dwo_unit (const void *item)
11542{
9a3c8263 11543 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11544
11545 /* This drops the top 32 bits of the id, but is ok for a hash. */
11546 return dwo_unit->signature;
11547}
11548
11549static int
11550eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11551{
9a3c8263
SM
11552 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11553 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11554
11555 /* The signature is assumed to be unique within the DWO file.
11556 So while object file CU dwo_id's always have the value zero,
11557 that's OK, assuming each object file DWO file has only one CU,
11558 and that's the rule for now. */
11559 return lhs->signature == rhs->signature;
11560}
11561
11562/* Allocate a hash table for DWO CUs,TUs.
11563 There is one of these tables for each of CUs,TUs for each DWO file. */
11564
b0b6a987 11565static htab_up
298e9637 11566allocate_dwo_unit_table ()
3019eac3
DE
11567{
11568 /* Start out with a pretty small number.
11569 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11570 return htab_up (htab_create_alloc (3,
11571 hash_dwo_unit,
11572 eq_dwo_unit,
11573 NULL, xcalloc, xfree));
3019eac3
DE
11574}
11575
19c3d4c9 11576/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11577
11578static void
19c3d4c9
DE
11579create_dwo_cu_reader (const struct die_reader_specs *reader,
11580 const gdb_byte *info_ptr,
11581 struct die_info *comp_unit_die,
c0ab21c2
TT
11582 struct dwo_file *dwo_file,
11583 struct dwo_unit *dwo_unit)
3019eac3
DE
11584{
11585 struct dwarf2_cu *cu = reader->cu;
9c541725 11586 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11587 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11588
a084a2a6
AT
11589 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11590 if (!signature.has_value ())
3019eac3 11591 {
b98664d3 11592 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11593 " its dwo_id [in module %s]"),
9d8780f0 11594 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11595 return;
11596 }
11597
3019eac3 11598 dwo_unit->dwo_file = dwo_file;
a084a2a6 11599 dwo_unit->signature = *signature;
8a0459fd 11600 dwo_unit->section = section;
9c541725 11601 dwo_unit->sect_off = sect_off;
3019eac3
DE
11602 dwo_unit->length = cu->per_cu->length;
11603
6f738b01
SM
11604 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11605 sect_offset_str (sect_off),
11606 hex_string (dwo_unit->signature));
3019eac3
DE
11607}
11608
33c5cd75 11609/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11610 Note: This function processes DWO files only, not DWP files. */
3019eac3 11611
33c5cd75 11612static void
976ca316 11613create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11614 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11615 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11616{
976ca316
SM
11617 struct objfile *objfile = per_objfile->objfile;
11618 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11619 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11620
96b79293 11621 section.read (objfile);
33c5cd75 11622 info_ptr = section.buffer;
3019eac3
DE
11623
11624 if (info_ptr == NULL)
33c5cd75 11625 return;
3019eac3 11626
6f738b01
SM
11627 dwarf_read_debug_printf ("Reading %s for %s:",
11628 section.get_name (),
11629 section.get_file_name ());
3019eac3 11630
33c5cd75 11631 end_ptr = info_ptr + section.size;
3019eac3
DE
11632 while (info_ptr < end_ptr)
11633 {
11634 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11635 struct dwo_unit read_unit {};
33c5cd75
DB
11636 struct dwo_unit *dwo_unit;
11637 void **slot;
11638 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11639
11640 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11641 per_cu.per_bfd = per_bfd;
3019eac3 11642 per_cu.is_debug_types = 0;
33c5cd75
DB
11643 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11644 per_cu.section = &section;
11645
976ca316 11646 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11647 if (!reader.dummy_p)
11648 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11649 &dwo_file, &read_unit);
33c5cd75
DB
11650 info_ptr += per_cu.length;
11651
11652 // If the unit could not be parsed, skip it.
c0ab21c2 11653 if (read_unit.dwo_file == NULL)
33c5cd75 11654 continue;
3019eac3 11655
33c5cd75 11656 if (cus_htab == NULL)
298e9637 11657 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11658
1859c670 11659 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11660 struct dwo_unit);
c0ab21c2 11661 *dwo_unit = read_unit;
b0b6a987 11662 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11663 gdb_assert (slot != NULL);
11664 if (*slot != NULL)
19c3d4c9 11665 {
33c5cd75
DB
11666 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11667 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11668
b98664d3 11669 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11670 " the entry at offset %s, signature %s"),
11671 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11672 hex_string (dwo_unit->signature));
19c3d4c9 11673 }
33c5cd75 11674 *slot = (void *)dwo_unit;
3019eac3 11675 }
3019eac3
DE
11676}
11677
80626a55
DE
11678/* DWP file .debug_{cu,tu}_index section format:
11679 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
d2854d8d
CT
11680 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11681
11682 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11683 officially standard DWP format was published with DWARF v5 and is called
11684 Version 5. There are no versions 3 or 4.
80626a55 11685
d2415c6c
DE
11686 DWP Version 1:
11687
80626a55
DE
11688 Both index sections have the same format, and serve to map a 64-bit
11689 signature to a set of section numbers. Each section begins with a header,
11690 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11691 indexes, and a pool of 32-bit section numbers. The index sections will be
11692 aligned at 8-byte boundaries in the file.
11693
d2415c6c
DE
11694 The index section header consists of:
11695
11696 V, 32 bit version number
11697 -, 32 bits unused
11698 N, 32 bit number of compilation units or type units in the index
11699 M, 32 bit number of slots in the hash table
80626a55 11700
d2415c6c 11701 Numbers are recorded using the byte order of the application binary.
80626a55 11702
d2415c6c
DE
11703 The hash table begins at offset 16 in the section, and consists of an array
11704 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11705 order of the application binary). Unused slots in the hash table are 0.
11706 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11707
d2415c6c
DE
11708 The parallel table begins immediately after the hash table
11709 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11710 array of 32-bit indexes (using the byte order of the application binary),
11711 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11712 table contains a 32-bit index into the pool of section numbers. For unused
11713 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11714
73869dc2
DE
11715 The pool of section numbers begins immediately following the hash table
11716 (at offset 16 + 12 * M from the beginning of the section). The pool of
11717 section numbers consists of an array of 32-bit words (using the byte order
11718 of the application binary). Each item in the array is indexed starting
11719 from 0. The hash table entry provides the index of the first section
11720 number in the set. Additional section numbers in the set follow, and the
11721 set is terminated by a 0 entry (section number 0 is not used in ELF).
11722
11723 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11724 section must be the first entry in the set, and the .debug_abbrev.dwo must
11725 be the second entry. Other members of the set may follow in any order.
11726
11727 ---
11728
d2854d8d 11729 DWP Versions 2 and 5:
73869dc2 11730
d2854d8d 11731 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
73869dc2
DE
11732 and the entries in the index tables are now offsets into these sections.
11733 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11734 section.
11735
11736 Index Section Contents:
11737 Header
11738 Hash Table of Signatures dwp_hash_table.hash_table
11739 Parallel Table of Indices dwp_hash_table.unit_table
d2854d8d
CT
11740 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11741 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
73869dc2
DE
11742
11743 The index section header consists of:
11744
11745 V, 32 bit version number
11746 L, 32 bit number of columns in the table of section offsets
11747 N, 32 bit number of compilation units or type units in the index
11748 M, 32 bit number of slots in the hash table
11749
11750 Numbers are recorded using the byte order of the application binary.
11751
11752 The hash table has the same format as version 1.
11753 The parallel table of indices has the same format as version 1,
11754 except that the entries are origin-1 indices into the table of sections
11755 offsets and the table of section sizes.
11756
11757 The table of offsets begins immediately following the parallel table
11758 (at offset 16 + 12 * M from the beginning of the section). The table is
11759 a two-dimensional array of 32-bit words (using the byte order of the
11760 application binary), with L columns and N+1 rows, in row-major order.
11761 Each row in the array is indexed starting from 0. The first row provides
11762 a key to the remaining rows: each column in this row provides an identifier
11763 for a debug section, and the offsets in the same column of subsequent rows
d2854d8d 11764 refer to that section. The section identifiers for Version 2 are:
73869dc2
DE
11765
11766 DW_SECT_INFO 1 .debug_info.dwo
11767 DW_SECT_TYPES 2 .debug_types.dwo
11768 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11769 DW_SECT_LINE 4 .debug_line.dwo
11770 DW_SECT_LOC 5 .debug_loc.dwo
11771 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11772 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11773 DW_SECT_MACRO 8 .debug_macro.dwo
11774
d2854d8d
CT
11775 The section identifiers for Version 5 are:
11776
11777 DW_SECT_INFO_V5 1 .debug_info.dwo
11778 DW_SECT_RESERVED_V5 2 --
11779 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11780 DW_SECT_LINE_V5 4 .debug_line.dwo
11781 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11782 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11783 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11784 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11785
73869dc2
DE
11786 The offsets provided by the CU and TU index sections are the base offsets
11787 for the contributions made by each CU or TU to the corresponding section
11788 in the package file. Each CU and TU header contains an abbrev_offset
11789 field, used to find the abbreviations table for that CU or TU within the
11790 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11791 be interpreted as relative to the base offset given in the index section.
11792 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11793 should be interpreted as relative to the base offset for .debug_line.dwo,
11794 and offsets into other debug sections obtained from DWARF attributes should
11795 also be interpreted as relative to the corresponding base offset.
11796
11797 The table of sizes begins immediately following the table of offsets.
11798 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11799 with L columns and N rows, in row-major order. Each row in the array is
11800 indexed starting from 1 (row 0 is shared by the two tables).
11801
11802 ---
11803
11804 Hash table lookup is handled the same in version 1 and 2:
11805
11806 We assume that N and M will not exceed 2^32 - 1.
11807 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11808
d2415c6c
DE
11809 Given a 64-bit compilation unit signature or a type signature S, an entry
11810 in the hash table is located as follows:
80626a55 11811
d2415c6c
DE
11812 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11813 the low-order k bits all set to 1.
80626a55 11814
d2415c6c 11815 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11816
d2415c6c
DE
11817 3) If the hash table entry at index H matches the signature, use that
11818 entry. If the hash table entry at index H is unused (all zeroes),
11819 terminate the search: the signature is not present in the table.
80626a55 11820
d2415c6c 11821 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11822
d2415c6c 11823 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11824 to stop at an unused slot or find the match. */
80626a55
DE
11825
11826/* Create a hash table to map DWO IDs to their CU/TU entry in
11827 .debug_{info,types}.dwo in DWP_FILE.
11828 Returns NULL if there isn't one.
11829 Note: This function processes DWP files only, not DWO files. */
11830
11831static struct dwp_hash_table *
976ca316 11832create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11833 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11834{
976ca316 11835 struct objfile *objfile = per_objfile->objfile;
400174b1 11836 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11837 const gdb_byte *index_ptr, *index_end;
80626a55 11838 struct dwarf2_section_info *index;
73869dc2 11839 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11840 struct dwp_hash_table *htab;
11841
11842 if (is_debug_types)
11843 index = &dwp_file->sections.tu_index;
11844 else
11845 index = &dwp_file->sections.cu_index;
11846
96b79293 11847 if (index->empty ())
80626a55 11848 return NULL;
96b79293 11849 index->read (objfile);
80626a55
DE
11850
11851 index_ptr = index->buffer;
11852 index_end = index_ptr + index->size;
11853
d2854d8d
CT
11854 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11855 For now it's safe to just read 4 bytes (particularly as it's difficult to
11856 tell if you're dealing with Version 5 before you've read the version). */
80626a55 11857 version = read_4_bytes (dbfd, index_ptr);
73869dc2 11858 index_ptr += 4;
d2854d8d 11859 if (version == 2 || version == 5)
73869dc2
DE
11860 nr_columns = read_4_bytes (dbfd, index_ptr);
11861 else
11862 nr_columns = 0;
11863 index_ptr += 4;
80626a55
DE
11864 nr_units = read_4_bytes (dbfd, index_ptr);
11865 index_ptr += 4;
11866 nr_slots = read_4_bytes (dbfd, index_ptr);
11867 index_ptr += 4;
11868
d2854d8d 11869 if (version != 1 && version != 2 && version != 5)
80626a55 11870 {
21aa081e 11871 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11872 " [in module %s]"),
21aa081e 11873 pulongest (version), dwp_file->name);
80626a55
DE
11874 }
11875 if (nr_slots != (nr_slots & -nr_slots))
11876 {
21aa081e 11877 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11878 " is not power of 2 [in module %s]"),
21aa081e 11879 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11880 }
11881
976ca316 11882 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11883 htab->version = version;
11884 htab->nr_columns = nr_columns;
80626a55
DE
11885 htab->nr_units = nr_units;
11886 htab->nr_slots = nr_slots;
11887 htab->hash_table = index_ptr;
11888 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11889
11890 /* Exit early if the table is empty. */
11891 if (nr_slots == 0 || nr_units == 0
d2854d8d
CT
11892 || (version == 2 && nr_columns == 0)
11893 || (version == 5 && nr_columns == 0))
73869dc2
DE
11894 {
11895 /* All must be zero. */
11896 if (nr_slots != 0 || nr_units != 0
d2854d8d
CT
11897 || (version == 2 && nr_columns != 0)
11898 || (version == 5 && nr_columns != 0))
73869dc2 11899 {
b98664d3 11900 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11901 " all zero [in modules %s]"),
11902 dwp_file->name);
11903 }
11904 return htab;
11905 }
11906
11907 if (version == 1)
11908 {
11909 htab->section_pool.v1.indices =
11910 htab->unit_table + sizeof (uint32_t) * nr_slots;
11911 /* It's harder to decide whether the section is too small in v1.
11912 V1 is deprecated anyway so we punt. */
11913 }
d2854d8d 11914 else if (version == 2)
73869dc2
DE
11915 {
11916 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11917 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11918 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11919 /* Reverse map for error checking. */
11920 int ids_seen[DW_SECT_MAX + 1];
11921 int i;
11922
11923 if (nr_columns < 2)
11924 {
11925 error (_("Dwarf Error: bad DWP hash table, too few columns"
11926 " in section table [in module %s]"),
11927 dwp_file->name);
11928 }
11929 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11930 {
11931 error (_("Dwarf Error: bad DWP hash table, too many columns"
11932 " in section table [in module %s]"),
11933 dwp_file->name);
11934 }
04fd5eed
GB
11935 memset (ids, 255, sizeof_ids);
11936 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11937 for (i = 0; i < nr_columns; ++i)
11938 {
11939 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11940
11941 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11942 {
11943 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11944 " in section table [in module %s]"),
11945 id, dwp_file->name);
11946 }
11947 if (ids_seen[id] != -1)
11948 {
11949 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11950 " id %d in section table [in module %s]"),
11951 id, dwp_file->name);
11952 }
11953 ids_seen[id] = i;
11954 ids[i] = id;
11955 }
11956 /* Must have exactly one info or types section. */
11957 if (((ids_seen[DW_SECT_INFO] != -1)
11958 + (ids_seen[DW_SECT_TYPES] != -1))
11959 != 1)
11960 {
11961 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11962 " DWO info/types section [in module %s]"),
11963 dwp_file->name);
11964 }
11965 /* Must have an abbrev section. */
11966 if (ids_seen[DW_SECT_ABBREV] == -1)
11967 {
11968 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11969 " section [in module %s]"),
11970 dwp_file->name);
11971 }
11972 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11973 htab->section_pool.v2.sizes =
11974 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11975 * nr_units * nr_columns);
11976 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11977 * nr_units * nr_columns))
11978 > index_end)
11979 {
11980 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11981 " [in module %s]"),
11982 dwp_file->name);
11983 }
11984 }
d2854d8d
CT
11985 else /* version == 5 */
11986 {
11987 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11988 int *ids = htab->section_pool.v5.section_ids;
11989 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11990 /* Reverse map for error checking. */
11991 int ids_seen[DW_SECT_MAX_V5 + 1];
11992
11993 if (nr_columns < 2)
11994 {
11995 error (_("Dwarf Error: bad DWP hash table, too few columns"
11996 " in section table [in module %s]"),
11997 dwp_file->name);
11998 }
11999 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
12000 {
12001 error (_("Dwarf Error: bad DWP hash table, too many columns"
12002 " in section table [in module %s]"),
12003 dwp_file->name);
12004 }
12005 memset (ids, 255, sizeof_ids);
12006 memset (ids_seen, 255, sizeof (ids_seen));
12007 for (int i = 0; i < nr_columns; ++i)
12008 {
12009 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12010
12011 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
12012 {
12013 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12014 " in section table [in module %s]"),
12015 id, dwp_file->name);
12016 }
12017 if (ids_seen[id] != -1)
12018 {
12019 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12020 " id %d in section table [in module %s]"),
12021 id, dwp_file->name);
12022 }
12023 ids_seen[id] = i;
12024 ids[i] = id;
12025 }
12026 /* Must have seen an info section. */
12027 if (ids_seen[DW_SECT_INFO_V5] == -1)
12028 {
12029 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12030 " DWO info/types section [in module %s]"),
12031 dwp_file->name);
12032 }
12033 /* Must have an abbrev section. */
12034 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
12035 {
12036 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12037 " section [in module %s]"),
12038 dwp_file->name);
12039 }
12040 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12041 htab->section_pool.v5.sizes
12042 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
12043 * nr_units * nr_columns);
12044 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
12045 * nr_units * nr_columns))
12046 > index_end)
12047 {
12048 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12049 " [in module %s]"),
12050 dwp_file->name);
12051 }
12052 }
80626a55
DE
12053
12054 return htab;
12055}
12056
12057/* Update SECTIONS with the data from SECTP.
12058
5bb6e9dd
TT
12059 This function is like the other "locate" section routines, but in
12060 this context the sections to read comes from the DWP V1 hash table,
12061 not the full ELF section table.
80626a55
DE
12062
12063 The result is non-zero for success, or zero if an error was found. */
12064
12065static int
73869dc2
DE
12066locate_v1_virtual_dwo_sections (asection *sectp,
12067 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12068{
12069 const struct dwop_section_names *names = &dwop_section_names;
12070
12071 if (section_is_p (sectp->name, &names->abbrev_dwo))
12072 {
12073 /* There can be only one. */
049412e3 12074 if (sections->abbrev.s.section != NULL)
80626a55 12075 return 0;
049412e3 12076 sections->abbrev.s.section = sectp;
fd361982 12077 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12078 }
12079 else if (section_is_p (sectp->name, &names->info_dwo)
12080 || section_is_p (sectp->name, &names->types_dwo))
12081 {
12082 /* There can be only one. */
049412e3 12083 if (sections->info_or_types.s.section != NULL)
80626a55 12084 return 0;
049412e3 12085 sections->info_or_types.s.section = sectp;
fd361982 12086 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
12087 }
12088 else if (section_is_p (sectp->name, &names->line_dwo))
12089 {
12090 /* There can be only one. */
049412e3 12091 if (sections->line.s.section != NULL)
80626a55 12092 return 0;
049412e3 12093 sections->line.s.section = sectp;
fd361982 12094 sections->line.size = bfd_section_size (sectp);
80626a55
DE
12095 }
12096 else if (section_is_p (sectp->name, &names->loc_dwo))
12097 {
12098 /* There can be only one. */
049412e3 12099 if (sections->loc.s.section != NULL)
80626a55 12100 return 0;
049412e3 12101 sections->loc.s.section = sectp;
fd361982 12102 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12103 }
12104 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12105 {
12106 /* There can be only one. */
049412e3 12107 if (sections->macinfo.s.section != NULL)
80626a55 12108 return 0;
049412e3 12109 sections->macinfo.s.section = sectp;
fd361982 12110 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12111 }
12112 else if (section_is_p (sectp->name, &names->macro_dwo))
12113 {
12114 /* There can be only one. */
049412e3 12115 if (sections->macro.s.section != NULL)
80626a55 12116 return 0;
049412e3 12117 sections->macro.s.section = sectp;
fd361982 12118 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12119 }
12120 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12121 {
12122 /* There can be only one. */
049412e3 12123 if (sections->str_offsets.s.section != NULL)
80626a55 12124 return 0;
049412e3 12125 sections->str_offsets.s.section = sectp;
fd361982 12126 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12127 }
12128 else
12129 {
12130 /* No other kind of section is valid. */
12131 return 0;
12132 }
12133
12134 return 1;
12135}
12136
73869dc2
DE
12137/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12138 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12139 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12140 This is for DWP version 1 files. */
80626a55
DE
12141
12142static struct dwo_unit *
976ca316 12143create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 12144 struct dwp_file *dwp_file,
73869dc2
DE
12145 uint32_t unit_index,
12146 const char *comp_dir,
12147 ULONGEST signature, int is_debug_types)
80626a55 12148{
73869dc2
DE
12149 const struct dwp_hash_table *dwp_htab =
12150 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12151 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12152 const char *kind = is_debug_types ? "TU" : "CU";
12153 struct dwo_file *dwo_file;
12154 struct dwo_unit *dwo_unit;
73869dc2 12155 struct virtual_v1_dwo_sections sections;
80626a55 12156 void **dwo_file_slot;
80626a55
DE
12157 int i;
12158
73869dc2
DE
12159 gdb_assert (dwp_file->version == 1);
12160
6f738b01
SM
12161 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12162 kind, pulongest (unit_index), hex_string (signature),
12163 dwp_file->name);
80626a55 12164
19ac8c2e 12165 /* Fetch the sections of this DWO unit.
80626a55
DE
12166 Put a limit on the number of sections we look for so that bad data
12167 doesn't cause us to loop forever. */
12168
73869dc2 12169#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12170 (1 /* .debug_info or .debug_types */ \
12171 + 1 /* .debug_abbrev */ \
12172 + 1 /* .debug_line */ \
12173 + 1 /* .debug_loc */ \
12174 + 1 /* .debug_str_offsets */ \
19ac8c2e 12175 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12176 + 1 /* trailing zero */)
12177
12178 memset (&sections, 0, sizeof (sections));
80626a55 12179
73869dc2 12180 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12181 {
12182 asection *sectp;
12183 uint32_t section_nr =
12184 read_4_bytes (dbfd,
73869dc2
DE
12185 dwp_htab->section_pool.v1.indices
12186 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12187
12188 if (section_nr == 0)
12189 break;
12190 if (section_nr >= dwp_file->num_sections)
12191 {
12192 error (_("Dwarf Error: bad DWP hash table, section number too large"
12193 " [in module %s]"),
12194 dwp_file->name);
12195 }
12196
12197 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12198 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12199 {
12200 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12201 " [in module %s]"),
12202 dwp_file->name);
12203 }
12204 }
12205
12206 if (i < 2
96b79293
TT
12207 || sections.info_or_types.empty ()
12208 || sections.abbrev.empty ())
80626a55
DE
12209 {
12210 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12211 " [in module %s]"),
12212 dwp_file->name);
12213 }
73869dc2 12214 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12215 {
12216 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12217 " [in module %s]"),
12218 dwp_file->name);
12219 }
12220
12221 /* It's easier for the rest of the code if we fake a struct dwo_file and
12222 have dwo_unit "live" in that. At least for now.
12223
12224 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12225 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12226 file, we can combine them back into a virtual DWO file to save space
12227 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12228 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12229
791afaa2
TT
12230 std::string virtual_dwo_name =
12231 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
12232 sections.abbrev.get_id (),
12233 sections.line.get_id (),
12234 sections.loc.get_id (),
12235 sections.str_offsets.get_id ());
80626a55 12236 /* Can we use an existing virtual DWO file? */
976ca316 12237 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12238 comp_dir);
80626a55
DE
12239 /* Create one if necessary. */
12240 if (*dwo_file_slot == NULL)
12241 {
6f738b01
SM
12242 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12243 virtual_dwo_name.c_str ());
12244
51ac9db5 12245 dwo_file = new struct dwo_file;
976ca316 12246 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 12247 dwo_file->comp_dir = comp_dir;
80626a55
DE
12248 dwo_file->sections.abbrev = sections.abbrev;
12249 dwo_file->sections.line = sections.line;
12250 dwo_file->sections.loc = sections.loc;
12251 dwo_file->sections.macinfo = sections.macinfo;
12252 dwo_file->sections.macro = sections.macro;
12253 dwo_file->sections.str_offsets = sections.str_offsets;
12254 /* The "str" section is global to the entire DWP file. */
12255 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12256 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12257 there's no need to record it in dwo_file.
12258 Also, we can't simply record type sections in dwo_file because
12259 we record a pointer into the vector in dwo_unit. As we collect more
12260 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12261 for it, invalidating all copies of pointers into the previous
12262 contents. */
80626a55
DE
12263 *dwo_file_slot = dwo_file;
12264 }
12265 else
12266 {
6f738b01
SM
12267 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12268 virtual_dwo_name.c_str ());
12269
9a3c8263 12270 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12271 }
80626a55 12272
976ca316 12273 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
12274 dwo_unit->dwo_file = dwo_file;
12275 dwo_unit->signature = signature;
8d749320 12276 dwo_unit->section =
976ca316 12277 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 12278 *dwo_unit->section = sections.info_or_types;
57d63ce2 12279 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12280
12281 return dwo_unit;
12282}
12283
d2854d8d
CT
12284/* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12285 simplify them. Given a pointer to the containing section SECTION, and
12286 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12287 virtual section of just that piece. */
73869dc2
DE
12288
12289static struct dwarf2_section_info
d2854d8d
CT
12290create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12291 struct dwarf2_section_info *section,
12292 bfd_size_type offset, bfd_size_type size)
73869dc2
DE
12293{
12294 struct dwarf2_section_info result;
12295 asection *sectp;
12296
12297 gdb_assert (section != NULL);
12298 gdb_assert (!section->is_virtual);
12299
12300 memset (&result, 0, sizeof (result));
12301 result.s.containing_section = section;
dc4ccb6f 12302 result.is_virtual = true;
73869dc2
DE
12303
12304 if (size == 0)
12305 return result;
12306
96b79293 12307 sectp = section->get_bfd_section ();
73869dc2
DE
12308
12309 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12310 bounds of the real section. This is a pretty-rare event, so just
12311 flag an error (easier) instead of a warning and trying to cope. */
12312 if (sectp == NULL
fd361982 12313 || offset + size > bfd_section_size (sectp))
73869dc2 12314 {
d2854d8d 12315 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
73869dc2 12316 " in section %s [in module %s]"),
fd361982 12317 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 12318 objfile_name (per_objfile->objfile));
73869dc2
DE
12319 }
12320
12321 result.virtual_offset = offset;
12322 result.size = size;
12323 return result;
12324}
12325
12326/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12327 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12328 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12329 This is for DWP version 2 files. */
12330
12331static struct dwo_unit *
976ca316 12332create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 12333 struct dwp_file *dwp_file,
73869dc2
DE
12334 uint32_t unit_index,
12335 const char *comp_dir,
12336 ULONGEST signature, int is_debug_types)
12337{
73869dc2
DE
12338 const struct dwp_hash_table *dwp_htab =
12339 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12340 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12341 const char *kind = is_debug_types ? "TU" : "CU";
12342 struct dwo_file *dwo_file;
12343 struct dwo_unit *dwo_unit;
d2854d8d 12344 struct virtual_v2_or_v5_dwo_sections sections;
73869dc2 12345 void **dwo_file_slot;
73869dc2
DE
12346 int i;
12347
12348 gdb_assert (dwp_file->version == 2);
12349
6f738b01
SM
12350 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12351 kind, pulongest (unit_index), hex_string (signature),
12352 dwp_file->name);
73869dc2
DE
12353
12354 /* Fetch the section offsets of this DWO unit. */
12355
12356 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12357
12358 for (i = 0; i < dwp_htab->nr_columns; ++i)
12359 {
12360 uint32_t offset = read_4_bytes (dbfd,
12361 dwp_htab->section_pool.v2.offsets
12362 + (((unit_index - 1) * dwp_htab->nr_columns
12363 + i)
12364 * sizeof (uint32_t)));
12365 uint32_t size = read_4_bytes (dbfd,
12366 dwp_htab->section_pool.v2.sizes
12367 + (((unit_index - 1) * dwp_htab->nr_columns
12368 + i)
12369 * sizeof (uint32_t)));
12370
12371 switch (dwp_htab->section_pool.v2.section_ids[i])
12372 {
12373 case DW_SECT_INFO:
12374 case DW_SECT_TYPES:
12375 sections.info_or_types_offset = offset;
12376 sections.info_or_types_size = size;
12377 break;
12378 case DW_SECT_ABBREV:
12379 sections.abbrev_offset = offset;
12380 sections.abbrev_size = size;
12381 break;
12382 case DW_SECT_LINE:
12383 sections.line_offset = offset;
12384 sections.line_size = size;
12385 break;
12386 case DW_SECT_LOC:
12387 sections.loc_offset = offset;
12388 sections.loc_size = size;
12389 break;
12390 case DW_SECT_STR_OFFSETS:
12391 sections.str_offsets_offset = offset;
12392 sections.str_offsets_size = size;
12393 break;
12394 case DW_SECT_MACINFO:
12395 sections.macinfo_offset = offset;
12396 sections.macinfo_size = size;
12397 break;
12398 case DW_SECT_MACRO:
12399 sections.macro_offset = offset;
12400 sections.macro_size = size;
12401 break;
12402 }
12403 }
12404
12405 /* It's easier for the rest of the code if we fake a struct dwo_file and
12406 have dwo_unit "live" in that. At least for now.
12407
12408 The DWP file can be made up of a random collection of CUs and TUs.
12409 However, for each CU + set of TUs that came from the same original DWO
12410 file, we can combine them back into a virtual DWO file to save space
12411 (fewer struct dwo_file objects to allocate). Remember that for really
12412 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12413
791afaa2
TT
12414 std::string virtual_dwo_name =
12415 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12416 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12417 (long) (sections.line_size ? sections.line_offset : 0),
12418 (long) (sections.loc_size ? sections.loc_offset : 0),
12419 (long) (sections.str_offsets_size
12420 ? sections.str_offsets_offset : 0));
73869dc2 12421 /* Can we use an existing virtual DWO file? */
976ca316 12422 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12423 comp_dir);
73869dc2
DE
12424 /* Create one if necessary. */
12425 if (*dwo_file_slot == NULL)
12426 {
6f738b01
SM
12427 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12428 virtual_dwo_name.c_str ());
12429
51ac9db5 12430 dwo_file = new struct dwo_file;
976ca316 12431 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12432 dwo_file->comp_dir = comp_dir;
12433 dwo_file->sections.abbrev =
d2854d8d
CT
12434 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12435 sections.abbrev_offset,
12436 sections.abbrev_size);
73869dc2 12437 dwo_file->sections.line =
d2854d8d
CT
12438 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12439 sections.line_offset,
12440 sections.line_size);
73869dc2 12441 dwo_file->sections.loc =
d2854d8d
CT
12442 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12443 sections.loc_offset, sections.loc_size);
73869dc2 12444 dwo_file->sections.macinfo =
d2854d8d
CT
12445 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12446 sections.macinfo_offset,
12447 sections.macinfo_size);
73869dc2 12448 dwo_file->sections.macro =
d2854d8d
CT
12449 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12450 sections.macro_offset,
12451 sections.macro_size);
73869dc2 12452 dwo_file->sections.str_offsets =
d2854d8d
CT
12453 create_dwp_v2_or_v5_section (per_objfile,
12454 &dwp_file->sections.str_offsets,
12455 sections.str_offsets_offset,
12456 sections.str_offsets_size);
73869dc2
DE
12457 /* The "str" section is global to the entire DWP file. */
12458 dwo_file->sections.str = dwp_file->sections.str;
12459 /* The info or types section is assigned below to dwo_unit,
12460 there's no need to record it in dwo_file.
12461 Also, we can't simply record type sections in dwo_file because
12462 we record a pointer into the vector in dwo_unit. As we collect more
12463 types we'll grow the vector and eventually have to reallocate space
12464 for it, invalidating all copies of pointers into the previous
12465 contents. */
12466 *dwo_file_slot = dwo_file;
12467 }
12468 else
12469 {
6f738b01
SM
12470 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12471 virtual_dwo_name.c_str ());
12472
9a3c8263 12473 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12474 }
73869dc2 12475
976ca316 12476 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12477 dwo_unit->dwo_file = dwo_file;
12478 dwo_unit->signature = signature;
8d749320 12479 dwo_unit->section =
976ca316 12480 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
d2854d8d 12481 *dwo_unit->section = create_dwp_v2_or_v5_section
dda83cd7 12482 (per_objfile,
d2854d8d
CT
12483 is_debug_types
12484 ? &dwp_file->sections.types
12485 : &dwp_file->sections.info,
12486 sections.info_or_types_offset,
12487 sections.info_or_types_size);
12488 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12489
12490 return dwo_unit;
12491}
12492
12493/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12494 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12495 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12496 This is for DWP version 5 files. */
12497
12498static struct dwo_unit *
12499create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12500 struct dwp_file *dwp_file,
12501 uint32_t unit_index,
12502 const char *comp_dir,
12503 ULONGEST signature, int is_debug_types)
12504{
12505 const struct dwp_hash_table *dwp_htab
12506 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12507 bfd *dbfd = dwp_file->dbfd.get ();
12508 const char *kind = is_debug_types ? "TU" : "CU";
12509 struct dwo_file *dwo_file;
12510 struct dwo_unit *dwo_unit;
12511 struct virtual_v2_or_v5_dwo_sections sections {};
12512 void **dwo_file_slot;
12513
12514 gdb_assert (dwp_file->version == 5);
12515
6f738b01
SM
12516 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12517 kind, pulongest (unit_index), hex_string (signature),
12518 dwp_file->name);
d2854d8d
CT
12519
12520 /* Fetch the section offsets of this DWO unit. */
12521
12522 /* memset (&sections, 0, sizeof (sections)); */
12523
12524 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12525 {
12526 uint32_t offset = read_4_bytes (dbfd,
dda83cd7
SM
12527 dwp_htab->section_pool.v5.offsets
12528 + (((unit_index - 1)
12529 * dwp_htab->nr_columns
12530 + i)
12531 * sizeof (uint32_t)));
d2854d8d 12532 uint32_t size = read_4_bytes (dbfd,
dda83cd7
SM
12533 dwp_htab->section_pool.v5.sizes
12534 + (((unit_index - 1) * dwp_htab->nr_columns
12535 + i)
12536 * sizeof (uint32_t)));
d2854d8d
CT
12537
12538 switch (dwp_htab->section_pool.v5.section_ids[i])
dda83cd7
SM
12539 {
12540 case DW_SECT_ABBREV_V5:
12541 sections.abbrev_offset = offset;
12542 sections.abbrev_size = size;
12543 break;
12544 case DW_SECT_INFO_V5:
12545 sections.info_or_types_offset = offset;
12546 sections.info_or_types_size = size;
12547 break;
12548 case DW_SECT_LINE_V5:
12549 sections.line_offset = offset;
12550 sections.line_size = size;
12551 break;
12552 case DW_SECT_LOCLISTS_V5:
12553 sections.loclists_offset = offset;
12554 sections.loclists_size = size;
12555 break;
12556 case DW_SECT_MACRO_V5:
12557 sections.macro_offset = offset;
12558 sections.macro_size = size;
12559 break;
12560 case DW_SECT_RNGLISTS_V5:
12561 sections.rnglists_offset = offset;
12562 sections.rnglists_size = size;
12563 break;
12564 case DW_SECT_STR_OFFSETS_V5:
12565 sections.str_offsets_offset = offset;
12566 sections.str_offsets_size = size;
12567 break;
12568 case DW_SECT_RESERVED_V5:
12569 default:
12570 break;
12571 }
d2854d8d
CT
12572 }
12573
12574 /* It's easier for the rest of the code if we fake a struct dwo_file and
12575 have dwo_unit "live" in that. At least for now.
12576
12577 The DWP file can be made up of a random collection of CUs and TUs.
12578 However, for each CU + set of TUs that came from the same original DWO
12579 file, we can combine them back into a virtual DWO file to save space
12580 (fewer struct dwo_file objects to allocate). Remember that for really
12581 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12582
12583 std::string virtual_dwo_name =
12584 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
dda83cd7
SM
12585 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12586 (long) (sections.line_size ? sections.line_offset : 0),
12587 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12588 (long) (sections.str_offsets_size
12589 ? sections.str_offsets_offset : 0),
12590 (long) (sections.macro_size ? sections.macro_offset : 0),
12591 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
d2854d8d
CT
12592 /* Can we use an existing virtual DWO file? */
12593 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
dda83cd7
SM
12594 virtual_dwo_name.c_str (),
12595 comp_dir);
d2854d8d
CT
12596 /* Create one if necessary. */
12597 if (*dwo_file_slot == NULL)
12598 {
6f738b01
SM
12599 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12600 virtual_dwo_name.c_str ());
12601
d2854d8d
CT
12602 dwo_file = new struct dwo_file;
12603 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12604 dwo_file->comp_dir = comp_dir;
12605 dwo_file->sections.abbrev =
dda83cd7
SM
12606 create_dwp_v2_or_v5_section (per_objfile,
12607 &dwp_file->sections.abbrev,
12608 sections.abbrev_offset,
12609 sections.abbrev_size);
d2854d8d 12610 dwo_file->sections.line =
dda83cd7
SM
12611 create_dwp_v2_or_v5_section (per_objfile,
12612 &dwp_file->sections.line,
12613 sections.line_offset, sections.line_size);
d2854d8d 12614 dwo_file->sections.macro =
dda83cd7
SM
12615 create_dwp_v2_or_v5_section (per_objfile,
12616 &dwp_file->sections.macro,
12617 sections.macro_offset,
12618 sections.macro_size);
d2854d8d 12619 dwo_file->sections.loclists =
dda83cd7
SM
12620 create_dwp_v2_or_v5_section (per_objfile,
12621 &dwp_file->sections.loclists,
12622 sections.loclists_offset,
12623 sections.loclists_size);
d2854d8d 12624 dwo_file->sections.rnglists =
dda83cd7
SM
12625 create_dwp_v2_or_v5_section (per_objfile,
12626 &dwp_file->sections.rnglists,
12627 sections.rnglists_offset,
12628 sections.rnglists_size);
d2854d8d 12629 dwo_file->sections.str_offsets =
dda83cd7
SM
12630 create_dwp_v2_or_v5_section (per_objfile,
12631 &dwp_file->sections.str_offsets,
12632 sections.str_offsets_offset,
12633 sections.str_offsets_size);
d2854d8d
CT
12634 /* The "str" section is global to the entire DWP file. */
12635 dwo_file->sections.str = dwp_file->sections.str;
12636 /* The info or types section is assigned below to dwo_unit,
dda83cd7
SM
12637 there's no need to record it in dwo_file.
12638 Also, we can't simply record type sections in dwo_file because
12639 we record a pointer into the vector in dwo_unit. As we collect more
12640 types we'll grow the vector and eventually have to reallocate space
12641 for it, invalidating all copies of pointers into the previous
12642 contents. */
d2854d8d
CT
12643 *dwo_file_slot = dwo_file;
12644 }
12645 else
12646 {
6f738b01
SM
12647 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12648 virtual_dwo_name.c_str ());
12649
d2854d8d
CT
12650 dwo_file = (struct dwo_file *) *dwo_file_slot;
12651 }
12652
12653 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12654 dwo_unit->dwo_file = dwo_file;
12655 dwo_unit->signature = signature;
12656 dwo_unit->section
12657 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12658 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
dda83cd7
SM
12659 &dwp_file->sections.info,
12660 sections.info_or_types_offset,
12661 sections.info_or_types_size);
73869dc2
DE
12662 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12663
12664 return dwo_unit;
12665}
12666
57d63ce2
DE
12667/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12668 Returns NULL if the signature isn't found. */
80626a55
DE
12669
12670static struct dwo_unit *
976ca316 12671lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12672 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12673 ULONGEST signature, int is_debug_types)
80626a55 12674{
57d63ce2
DE
12675 const struct dwp_hash_table *dwp_htab =
12676 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12677 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12678 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12679 uint32_t hash = signature & mask;
12680 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12681 unsigned int i;
12682 void **slot;
870f88f7 12683 struct dwo_unit find_dwo_cu;
80626a55
DE
12684
12685 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12686 find_dwo_cu.signature = signature;
19ac8c2e 12687 slot = htab_find_slot (is_debug_types
48b490f2
TT
12688 ? dwp_file->loaded_tus.get ()
12689 : dwp_file->loaded_cus.get (),
19ac8c2e 12690 &find_dwo_cu, INSERT);
80626a55
DE
12691
12692 if (*slot != NULL)
9a3c8263 12693 return (struct dwo_unit *) *slot;
80626a55
DE
12694
12695 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12696 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12697 {
12698 ULONGEST signature_in_table;
12699
12700 signature_in_table =
57d63ce2 12701 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12702 if (signature_in_table == signature)
12703 {
57d63ce2
DE
12704 uint32_t unit_index =
12705 read_4_bytes (dbfd,
12706 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12707
73869dc2
DE
12708 if (dwp_file->version == 1)
12709 {
976ca316
SM
12710 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12711 unit_index, comp_dir,
12712 signature, is_debug_types);
73869dc2 12713 }
d2854d8d 12714 else if (dwp_file->version == 2)
73869dc2 12715 {
976ca316
SM
12716 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12717 unit_index, comp_dir,
12718 signature, is_debug_types);
73869dc2 12719 }
d2854d8d
CT
12720 else /* version == 5 */
12721 {
12722 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12723 unit_index, comp_dir,
12724 signature, is_debug_types);
12725 }
9a3c8263 12726 return (struct dwo_unit *) *slot;
80626a55
DE
12727 }
12728 if (signature_in_table == 0)
12729 return NULL;
12730 hash = (hash + hash2) & mask;
12731 }
12732
12733 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12734 " [in module %s]"),
12735 dwp_file->name);
12736}
12737
ab5088bf 12738/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12739 Open the file specified by FILE_NAME and hand it off to BFD for
12740 preliminary analysis. Return a newly initialized bfd *, which
12741 includes a canonicalized copy of FILE_NAME.
80626a55 12742 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12743 SEARCH_CWD is true if the current directory is to be searched.
12744 It will be searched before debug-file-directory.
13aaf454
DE
12745 If successful, the file is added to the bfd include table of the
12746 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12747 If unable to find/open the file, return NULL.
3019eac3
DE
12748 NOTE: This function is derived from symfile_bfd_open. */
12749
192b62ce 12750static gdb_bfd_ref_ptr
976ca316 12751try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12752 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12753{
24b9144d 12754 int desc;
9c02c129
DE
12755 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12756 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12757 to debug_file_directory. */
e0cc99a6 12758 const char *search_path;
9c02c129
DE
12759 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12760
e0cc99a6 12761 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12762 if (search_cwd)
12763 {
12764 if (*debug_file_directory != '\0')
e0cc99a6
TT
12765 {
12766 search_path_holder.reset (concat (".", dirname_separator_string,
12767 debug_file_directory,
12768 (char *) NULL));
12769 search_path = search_path_holder.get ();
12770 }
6ac97d4c 12771 else
e0cc99a6 12772 search_path = ".";
6ac97d4c 12773 }
9c02c129 12774 else
e0cc99a6 12775 search_path = debug_file_directory;
3019eac3 12776
24b9144d 12777 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12778 if (is_dwp)
12779 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12780
12781 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12782 desc = openp (search_path, flags, file_name,
3019eac3
DE
12783 O_RDONLY | O_BINARY, &absolute_name);
12784 if (desc < 0)
12785 return NULL;
12786
e0cc99a6
TT
12787 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12788 gnutarget, desc));
9c02c129
DE
12789 if (sym_bfd == NULL)
12790 return NULL;
192b62ce 12791 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12792
192b62ce
TT
12793 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12794 return NULL;
3019eac3 12795
13aaf454
DE
12796 /* Success. Record the bfd as having been included by the objfile's bfd.
12797 This is important because things like demangled_names_hash lives in the
12798 objfile's per_bfd space and may have references to things like symbol
12799 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12800 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12801
3019eac3
DE
12802 return sym_bfd;
12803}
12804
ab5088bf 12805/* Try to open DWO file FILE_NAME.
3019eac3
DE
12806 COMP_DIR is the DW_AT_comp_dir attribute.
12807 The result is the bfd handle of the file.
12808 If there is a problem finding or opening the file, return NULL.
12809 Upon success, the canonicalized path of the file is stored in the bfd,
12810 same as symfile_bfd_open. */
12811
192b62ce 12812static gdb_bfd_ref_ptr
976ca316 12813open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12814 const char *file_name, const char *comp_dir)
3019eac3 12815{
80626a55 12816 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12817 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12818 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12819
12820 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12821
12822 if (comp_dir != NULL)
12823 {
43816ebc
TT
12824 gdb::unique_xmalloc_ptr<char> path_to_try
12825 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12826
12827 /* NOTE: If comp_dir is a relative path, this will also try the
12828 search path, which seems useful. */
976ca316 12829 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12830 0 /*is_dwp*/,
192b62ce 12831 1 /*search_cwd*/));
3019eac3
DE
12832 if (abfd != NULL)
12833 return abfd;
12834 }
12835
12836 /* That didn't work, try debug-file-directory, which, despite its name,
12837 is a list of paths. */
12838
12839 if (*debug_file_directory == '\0')
12840 return NULL;
12841
976ca316 12842 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12843 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12844}
12845
80626a55
DE
12846/* This function is mapped across the sections and remembers the offset and
12847 size of each of the DWO debugging sections we are interested in. */
12848
12849static void
5bb6e9dd
TT
12850dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12851 dwo_sections *dwo_sections)
80626a55 12852{
80626a55
DE
12853 const struct dwop_section_names *names = &dwop_section_names;
12854
12855 if (section_is_p (sectp->name, &names->abbrev_dwo))
12856 {
049412e3 12857 dwo_sections->abbrev.s.section = sectp;
fd361982 12858 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12859 }
12860 else if (section_is_p (sectp->name, &names->info_dwo))
12861 {
049412e3 12862 dwo_sections->info.s.section = sectp;
fd361982 12863 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12864 }
12865 else if (section_is_p (sectp->name, &names->line_dwo))
12866 {
049412e3 12867 dwo_sections->line.s.section = sectp;
fd361982 12868 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12869 }
12870 else if (section_is_p (sectp->name, &names->loc_dwo))
12871 {
049412e3 12872 dwo_sections->loc.s.section = sectp;
fd361982 12873 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12874 }
41144253 12875 else if (section_is_p (sectp->name, &names->loclists_dwo))
12876 {
12877 dwo_sections->loclists.s.section = sectp;
12878 dwo_sections->loclists.size = bfd_section_size (sectp);
12879 }
80626a55
DE
12880 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12881 {
049412e3 12882 dwo_sections->macinfo.s.section = sectp;
fd361982 12883 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12884 }
12885 else if (section_is_p (sectp->name, &names->macro_dwo))
12886 {
049412e3 12887 dwo_sections->macro.s.section = sectp;
fd361982 12888 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55 12889 }
d0ce17d8
CT
12890 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12891 {
12892 dwo_sections->rnglists.s.section = sectp;
12893 dwo_sections->rnglists.size = bfd_section_size (sectp);
12894 }
80626a55
DE
12895 else if (section_is_p (sectp->name, &names->str_dwo))
12896 {
049412e3 12897 dwo_sections->str.s.section = sectp;
fd361982 12898 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12899 }
12900 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12901 {
049412e3 12902 dwo_sections->str_offsets.s.section = sectp;
fd361982 12903 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12904 }
12905 else if (section_is_p (sectp->name, &names->types_dwo))
12906 {
12907 struct dwarf2_section_info type_section;
12908
12909 memset (&type_section, 0, sizeof (type_section));
049412e3 12910 type_section.s.section = sectp;
fd361982 12911 type_section.size = bfd_section_size (sectp);
fd5866f6 12912 dwo_sections->types.push_back (type_section);
80626a55
DE
12913 }
12914}
12915
ab5088bf 12916/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12917 by PER_CU. This is for the non-DWP case.
80626a55 12918 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12919
12920static struct dwo_file *
4ab09049
SM
12921open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12922 const char *comp_dir)
3019eac3 12923{
976ca316 12924 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12925
976ca316 12926 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12927 if (dbfd == NULL)
12928 {
6f738b01
SM
12929 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12930
80626a55
DE
12931 return NULL;
12932 }
263db9a1 12933
51ac9db5 12934 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12935 dwo_file->dwo_name = dwo_name;
12936 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12937 dwo_file->dbfd = std::move (dbfd);
3019eac3 12938
5bb6e9dd
TT
12939 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12940 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12941 &dwo_file->sections);
3019eac3 12942
976ca316
SM
12943 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12944 dwo_file->cus);
3019eac3 12945
d2854d8d
CT
12946 if (cu->per_cu->dwarf_version < 5)
12947 {
12948 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12949 dwo_file->sections.types, dwo_file->tus);
12950 }
12951 else
12952 {
12953 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12954 &dwo_file->sections.info, dwo_file->tus,
12955 rcuh_kind::TYPE);
12956 }
3019eac3 12957
6f738b01 12958 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
80626a55 12959
263db9a1 12960 return dwo_file.release ();
3019eac3
DE
12961}
12962
80626a55 12963/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12964 size of each of the DWP debugging sections common to version 1 and 2 that
12965 we are interested in. */
3019eac3 12966
80626a55 12967static void
73869dc2 12968dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
5bb6e9dd 12969 dwp_file *dwp_file)
3019eac3 12970{
80626a55
DE
12971 const struct dwop_section_names *names = &dwop_section_names;
12972 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12973
80626a55 12974 /* Record the ELF section number for later lookup: this is what the
73869dc2 12975 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12976 gdb_assert (elf_section_nr < dwp_file->num_sections);
12977 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12978
80626a55
DE
12979 /* Look for specific sections that we need. */
12980 if (section_is_p (sectp->name, &names->str_dwo))
12981 {
049412e3 12982 dwp_file->sections.str.s.section = sectp;
fd361982 12983 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12984 }
12985 else if (section_is_p (sectp->name, &names->cu_index))
12986 {
049412e3 12987 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12988 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12989 }
12990 else if (section_is_p (sectp->name, &names->tu_index))
12991 {
049412e3 12992 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12993 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12994 }
12995}
3019eac3 12996
73869dc2
DE
12997/* This function is mapped across the sections and remembers the offset and
12998 size of each of the DWP version 2 debugging sections that we are interested
12999 in. This is split into a separate function because we don't know if we
d2854d8d 13000 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
73869dc2
DE
13001
13002static void
13003dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13004{
9a3c8263 13005 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13006 const struct dwop_section_names *names = &dwop_section_names;
13007 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13008
13009 /* Record the ELF section number for later lookup: this is what the
13010 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13011 gdb_assert (elf_section_nr < dwp_file->num_sections);
13012 dwp_file->elf_sections[elf_section_nr] = sectp;
13013
13014 /* Look for specific sections that we need. */
13015 if (section_is_p (sectp->name, &names->abbrev_dwo))
13016 {
049412e3 13017 dwp_file->sections.abbrev.s.section = sectp;
fd361982 13018 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
13019 }
13020 else if (section_is_p (sectp->name, &names->info_dwo))
13021 {
049412e3 13022 dwp_file->sections.info.s.section = sectp;
fd361982 13023 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
13024 }
13025 else if (section_is_p (sectp->name, &names->line_dwo))
13026 {
049412e3 13027 dwp_file->sections.line.s.section = sectp;
fd361982 13028 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
13029 }
13030 else if (section_is_p (sectp->name, &names->loc_dwo))
13031 {
049412e3 13032 dwp_file->sections.loc.s.section = sectp;
fd361982 13033 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
13034 }
13035 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13036 {
049412e3 13037 dwp_file->sections.macinfo.s.section = sectp;
fd361982 13038 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
13039 }
13040 else if (section_is_p (sectp->name, &names->macro_dwo))
13041 {
049412e3 13042 dwp_file->sections.macro.s.section = sectp;
fd361982 13043 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
13044 }
13045 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13046 {
049412e3 13047 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 13048 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
13049 }
13050 else if (section_is_p (sectp->name, &names->types_dwo))
13051 {
049412e3 13052 dwp_file->sections.types.s.section = sectp;
fd361982 13053 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
13054 }
13055}
13056
d2854d8d
CT
13057/* This function is mapped across the sections and remembers the offset and
13058 size of each of the DWP version 5 debugging sections that we are interested
13059 in. This is split into a separate function because we don't know if we
13060 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
13061
13062static void
13063dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13064{
13065 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13066 const struct dwop_section_names *names = &dwop_section_names;
13067 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13068
13069 /* Record the ELF section number for later lookup: this is what the
13070 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13071 gdb_assert (elf_section_nr < dwp_file->num_sections);
13072 dwp_file->elf_sections[elf_section_nr] = sectp;
13073
13074 /* Look for specific sections that we need. */
13075 if (section_is_p (sectp->name, &names->abbrev_dwo))
13076 {
13077 dwp_file->sections.abbrev.s.section = sectp;
13078 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13079 }
13080 else if (section_is_p (sectp->name, &names->info_dwo))
13081 {
13082 dwp_file->sections.info.s.section = sectp;
13083 dwp_file->sections.info.size = bfd_section_size (sectp);
13084 }
13085 else if (section_is_p (sectp->name, &names->line_dwo))
13086 {
13087 dwp_file->sections.line.s.section = sectp;
13088 dwp_file->sections.line.size = bfd_section_size (sectp);
13089 }
13090 else if (section_is_p (sectp->name, &names->loclists_dwo))
13091 {
13092 dwp_file->sections.loclists.s.section = sectp;
13093 dwp_file->sections.loclists.size = bfd_section_size (sectp);
13094 }
13095 else if (section_is_p (sectp->name, &names->macro_dwo))
13096 {
13097 dwp_file->sections.macro.s.section = sectp;
13098 dwp_file->sections.macro.size = bfd_section_size (sectp);
13099 }
13100 else if (section_is_p (sectp->name, &names->rnglists_dwo))
13101 {
13102 dwp_file->sections.rnglists.s.section = sectp;
13103 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
13104 }
13105 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13106 {
13107 dwp_file->sections.str_offsets.s.section = sectp;
13108 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13109 }
13110}
13111
80626a55 13112/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13113
80626a55
DE
13114static hashval_t
13115hash_dwp_loaded_cutus (const void *item)
13116{
9a3c8263 13117 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13118
80626a55
DE
13119 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13120 return dwo_unit->signature;
3019eac3
DE
13121}
13122
80626a55 13123/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13124
80626a55
DE
13125static int
13126eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13127{
9a3c8263
SM
13128 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13129 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13130
80626a55
DE
13131 return dua->signature == dub->signature;
13132}
3019eac3 13133
80626a55 13134/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13135
48b490f2 13136static htab_up
298e9637 13137allocate_dwp_loaded_cutus_table ()
80626a55 13138{
48b490f2
TT
13139 return htab_up (htab_create_alloc (3,
13140 hash_dwp_loaded_cutus,
13141 eq_dwp_loaded_cutus,
13142 NULL, xcalloc, xfree));
80626a55 13143}
3019eac3 13144
ab5088bf
DE
13145/* Try to open DWP file FILE_NAME.
13146 The result is the bfd handle of the file.
13147 If there is a problem finding or opening the file, return NULL.
13148 Upon success, the canonicalized path of the file is stored in the bfd,
13149 same as symfile_bfd_open. */
13150
192b62ce 13151static gdb_bfd_ref_ptr
976ca316 13152open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 13153{
976ca316 13154 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 13155 1 /*is_dwp*/,
192b62ce 13156 1 /*search_cwd*/));
6ac97d4c
DE
13157 if (abfd != NULL)
13158 return abfd;
13159
13160 /* Work around upstream bug 15652.
13161 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13162 [Whether that's a "bug" is debatable, but it is getting in our way.]
13163 We have no real idea where the dwp file is, because gdb's realpath-ing
13164 of the executable's path may have discarded the needed info.
13165 [IWBN if the dwp file name was recorded in the executable, akin to
13166 .gnu_debuglink, but that doesn't exist yet.]
13167 Strip the directory from FILE_NAME and search again. */
13168 if (*debug_file_directory != '\0')
13169 {
13170 /* Don't implicitly search the current directory here.
13171 If the user wants to search "." to handle this case,
13172 it must be added to debug-file-directory. */
976ca316
SM
13173 return try_open_dwop_file (per_objfile, lbasename (file_name),
13174 1 /*is_dwp*/,
6ac97d4c
DE
13175 0 /*search_cwd*/);
13176 }
13177
13178 return NULL;
ab5088bf
DE
13179}
13180
80626a55
DE
13181/* Initialize the use of the DWP file for the current objfile.
13182 By convention the name of the DWP file is ${objfile}.dwp.
13183 The result is NULL if it can't be found. */
a766d390 13184
400174b1 13185static std::unique_ptr<struct dwp_file>
976ca316 13186open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 13187{
976ca316 13188 struct objfile *objfile = per_objfile->objfile;
80626a55 13189
82bf32bc
JK
13190 /* Try to find first .dwp for the binary file before any symbolic links
13191 resolving. */
6c447423
DE
13192
13193 /* If the objfile is a debug file, find the name of the real binary
13194 file and get the name of dwp file from there. */
d721ba37 13195 std::string dwp_name;
6c447423
DE
13196 if (objfile->separate_debug_objfile_backlink != NULL)
13197 {
13198 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13199 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13200
d721ba37 13201 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13202 }
13203 else
d721ba37
PA
13204 dwp_name = objfile->original_name;
13205
13206 dwp_name += ".dwp";
80626a55 13207
976ca316 13208 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13209 if (dbfd == NULL
13210 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13211 {
13212 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13213 dwp_name = objfile_name (objfile);
13214 dwp_name += ".dwp";
976ca316 13215 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
13216 }
13217
80626a55
DE
13218 if (dbfd == NULL)
13219 {
6f738b01
SM
13220 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13221
400174b1 13222 return std::unique_ptr<dwp_file> ();
3019eac3 13223 }
400174b1
TT
13224
13225 const char *name = bfd_get_filename (dbfd.get ());
13226 std::unique_ptr<struct dwp_file> dwp_file
13227 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13228
0a0f4c01 13229 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 13230 dwp_file->elf_sections =
976ca316 13231 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
13232 dwp_file->num_sections, asection *);
13233
5bb6e9dd
TT
13234 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13235 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13236 dwp_file.get ());
80626a55 13237
976ca316 13238 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 13239
976ca316 13240 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 13241
73869dc2 13242 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13243 if (dwp_file->cus && dwp_file->tus
13244 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13245 {
13246 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13247 pretty bizarre. We use pulongest here because that's the established
4d65956b 13248 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13249 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13250 " TU version %s [in DWP file %s]"),
13251 pulongest (dwp_file->cus->version),
d721ba37 13252 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13253 }
08302ed2
DE
13254
13255 if (dwp_file->cus)
13256 dwp_file->version = dwp_file->cus->version;
13257 else if (dwp_file->tus)
13258 dwp_file->version = dwp_file->tus->version;
13259 else
13260 dwp_file->version = 2;
73869dc2 13261
5bb6e9dd
TT
13262 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13263 {
13264 if (dwp_file->version == 2)
13265 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13266 dwp_file.get ());
13267 else
13268 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13269 dwp_file.get ());
13270 }
73869dc2 13271
298e9637
SM
13272 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13273 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 13274
6f738b01
SM
13275 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13276 dwarf_read_debug_printf (" %s CUs, %s TUs",
13277 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13278 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13279
13280 return dwp_file;
3019eac3 13281}
c906108c 13282
ab5088bf
DE
13283/* Wrapper around open_and_init_dwp_file, only open it once. */
13284
13285static struct dwp_file *
976ca316 13286get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 13287{
976ca316 13288 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 13289 {
976ca316
SM
13290 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13291 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 13292 }
976ca316 13293 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
13294}
13295
80626a55
DE
13296/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13297 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13298 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13299 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13300 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13301
13302 This is called, for example, when wanting to read a variable with a
13303 complex location. Therefore we don't want to do file i/o for every call.
13304 Therefore we don't want to look for a DWO file on every call.
13305 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13306 then we check if we've already seen DWO_NAME, and only THEN do we check
13307 for a DWO file.
13308
1c658ad5 13309 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13310 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13311
3019eac3 13312static struct dwo_unit *
4ab09049 13313lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 13314 ULONGEST signature, int is_debug_types)
3019eac3 13315{
976ca316
SM
13316 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13317 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
13318 const char *kind = is_debug_types ? "TU" : "CU";
13319 void **dwo_file_slot;
3019eac3 13320 struct dwo_file *dwo_file;
80626a55 13321 struct dwp_file *dwp_file;
cb1df416 13322
6a506a2d
DE
13323 /* First see if there's a DWP file.
13324 If we have a DWP file but didn't find the DWO inside it, don't
13325 look for the original DWO file. It makes gdb behave differently
13326 depending on whether one is debugging in the build tree. */
cf2c3c16 13327
976ca316 13328 dwp_file = get_dwp_file (per_objfile);
80626a55 13329 if (dwp_file != NULL)
cf2c3c16 13330 {
80626a55
DE
13331 const struct dwp_hash_table *dwp_htab =
13332 is_debug_types ? dwp_file->tus : dwp_file->cus;
13333
13334 if (dwp_htab != NULL)
13335 {
13336 struct dwo_unit *dwo_cutu =
976ca316
SM
13337 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13338 is_debug_types);
80626a55
DE
13339
13340 if (dwo_cutu != NULL)
13341 {
6f738b01
SM
13342 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13343 kind, hex_string (signature),
13344 host_address_to_string (dwo_cutu));
13345
80626a55
DE
13346 return dwo_cutu;
13347 }
13348 }
13349 }
6a506a2d 13350 else
80626a55 13351 {
6a506a2d 13352 /* No DWP file, look for the DWO file. */
80626a55 13353
976ca316 13354 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 13355 if (*dwo_file_slot == NULL)
80626a55 13356 {
6a506a2d 13357 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 13358 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 13359 }
6a506a2d 13360 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13361 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13362
6a506a2d 13363 if (dwo_file != NULL)
19c3d4c9 13364 {
6a506a2d
DE
13365 struct dwo_unit *dwo_cutu = NULL;
13366
13367 if (is_debug_types && dwo_file->tus)
13368 {
13369 struct dwo_unit find_dwo_cutu;
13370
13371 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13372 find_dwo_cutu.signature = signature;
9a3c8263 13373 dwo_cutu
b0b6a987
TT
13374 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13375 &find_dwo_cutu);
6a506a2d 13376 }
33c5cd75 13377 else if (!is_debug_types && dwo_file->cus)
80626a55 13378 {
33c5cd75
DB
13379 struct dwo_unit find_dwo_cutu;
13380
13381 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13382 find_dwo_cutu.signature = signature;
b0b6a987 13383 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 13384 &find_dwo_cutu);
6a506a2d
DE
13385 }
13386
13387 if (dwo_cutu != NULL)
13388 {
6f738b01
SM
13389 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13390 kind, dwo_name, hex_string (signature),
13391 host_address_to_string (dwo_cutu));
13392
6a506a2d 13393 return dwo_cutu;
80626a55
DE
13394 }
13395 }
2e276125 13396 }
9cdd5dbd 13397
80626a55
DE
13398 /* We didn't find it. This could mean a dwo_id mismatch, or
13399 someone deleted the DWO/DWP file, or the search path isn't set up
13400 correctly to find the file. */
13401
6f738b01
SM
13402 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13403 kind, dwo_name, hex_string (signature));
3019eac3 13404
6656a72d
DE
13405 /* This is a warning and not a complaint because it can be caused by
13406 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13407 {
13408 /* Print the name of the DWP file if we looked there, helps the user
13409 better diagnose the problem. */
791afaa2 13410 std::string dwp_text;
43942612
DE
13411
13412 if (dwp_file != NULL)
791afaa2
TT
13413 dwp_text = string_printf (" [in DWP file %s]",
13414 lbasename (dwp_file->name));
43942612 13415
9d8780f0 13416 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 13417 " [in module %s]"),
4ab09049
SM
13418 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13419 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 13420 }
3019eac3 13421 return NULL;
5fb290d7
DJ
13422}
13423
80626a55
DE
13424/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13425 See lookup_dwo_cutu_unit for details. */
13426
13427static struct dwo_unit *
4ab09049 13428lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
13429 ULONGEST signature)
13430{
4ab09049
SM
13431 gdb_assert (!cu->per_cu->is_debug_types);
13432
13433 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
13434}
13435
13436/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13437 See lookup_dwo_cutu_unit for details. */
13438
13439static struct dwo_unit *
4ab09049 13440lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 13441{
4ab09049
SM
13442 gdb_assert (cu->per_cu->is_debug_types);
13443
13444 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13445
13446 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
13447}
13448
89e63ee4
DE
13449/* Traversal function for queue_and_load_all_dwo_tus. */
13450
13451static int
13452queue_and_load_dwo_tu (void **slot, void *info)
13453{
13454 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 13455 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 13456 ULONGEST signature = dwo_unit->signature;
d460f660 13457 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
13458
13459 if (sig_type != NULL)
13460 {
13461 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13462
13463 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13464 a real dependency of PER_CU on SIG_TYPE. That is detected later
13465 while processing PER_CU. */
120ce1b5 13466 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
13467 load_full_type_unit (sig_cu, cu->per_objfile);
13468 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13469 }
13470
13471 return 1;
13472}
13473
1b555f17 13474/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
13475 The DWO may have the only definition of the type, though it may not be
13476 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13477 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13478
13479static void
1b555f17 13480queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
13481{
13482 struct dwo_unit *dwo_unit;
13483 struct dwo_file *dwo_file;
13484
1b555f17
SM
13485 gdb_assert (cu != nullptr);
13486 gdb_assert (!cu->per_cu->is_debug_types);
13487 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 13488
1b555f17 13489 dwo_unit = cu->dwo_unit;
89e63ee4
DE
13490 gdb_assert (dwo_unit != NULL);
13491
13492 dwo_file = dwo_unit->dwo_file;
13493 if (dwo_file->tus != NULL)
1b555f17 13494 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
13495}
13496
3019eac3 13497/* Read in various DIEs. */
348e048f 13498
d389af10 13499/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13500 Inherit only the children of the DW_AT_abstract_origin DIE not being
13501 already referenced by DW_AT_abstract_origin from the children of the
13502 current DIE. */
d389af10
JK
13503
13504static void
13505inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13506{
13507 struct die_info *child_die;
791afaa2 13508 sect_offset *offsetp;
d389af10
JK
13509 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13510 struct die_info *origin_die;
13511 /* Iterator of the ORIGIN_DIE children. */
13512 struct die_info *origin_child_die;
d389af10 13513 struct attribute *attr;
cd02d79d
PA
13514 struct dwarf2_cu *origin_cu;
13515 struct pending **origin_previous_list_in_scope;
d389af10
JK
13516
13517 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13518 if (!attr)
13519 return;
13520
cd02d79d
PA
13521 /* Note that following die references may follow to a die in a
13522 different cu. */
13523
13524 origin_cu = cu;
13525 origin_die = follow_die_ref (die, attr, &origin_cu);
13526
13527 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13528 symbols in. */
13529 origin_previous_list_in_scope = origin_cu->list_in_scope;
13530 origin_cu->list_in_scope = cu->list_in_scope;
13531
edb3359d
DJ
13532 if (die->tag != origin_die->tag
13533 && !(die->tag == DW_TAG_inlined_subroutine
13534 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13535 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13536 sect_offset_str (die->sect_off),
13537 sect_offset_str (origin_die->sect_off));
d389af10 13538
791afaa2 13539 std::vector<sect_offset> offsets;
d389af10 13540
3ea89b92
PMR
13541 for (child_die = die->child;
13542 child_die && child_die->tag;
436c571c 13543 child_die = child_die->sibling)
3ea89b92
PMR
13544 {
13545 struct die_info *child_origin_die;
13546 struct dwarf2_cu *child_origin_cu;
13547
13548 /* We are trying to process concrete instance entries:
216f72a1 13549 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13550 it's not relevant to our analysis here. i.e. detecting DIEs that are
13551 present in the abstract instance but not referenced in the concrete
13552 one. */
216f72a1 13553 if (child_die->tag == DW_TAG_call_site
dda83cd7 13554 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13555 continue;
13556
c38f313d
DJ
13557 /* For each CHILD_DIE, find the corresponding child of
13558 ORIGIN_DIE. If there is more than one layer of
13559 DW_AT_abstract_origin, follow them all; there shouldn't be,
13560 but GCC versions at least through 4.4 generate this (GCC PR
13561 40573). */
3ea89b92
PMR
13562 child_origin_die = child_die;
13563 child_origin_cu = cu;
c38f313d
DJ
13564 while (1)
13565 {
cd02d79d
PA
13566 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13567 child_origin_cu);
c38f313d
DJ
13568 if (attr == NULL)
13569 break;
cd02d79d
PA
13570 child_origin_die = follow_die_ref (child_origin_die, attr,
13571 &child_origin_cu);
c38f313d
DJ
13572 }
13573
d389af10
JK
13574 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13575 counterpart may exist. */
c38f313d 13576 if (child_origin_die != child_die)
d389af10 13577 {
edb3359d
DJ
13578 if (child_die->tag != child_origin_die->tag
13579 && !(child_die->tag == DW_TAG_inlined_subroutine
13580 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13581 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13582 "different tags"),
9d8780f0
SM
13583 sect_offset_str (child_die->sect_off),
13584 sect_offset_str (child_origin_die->sect_off));
c38f313d 13585 if (child_origin_die->parent != origin_die)
b98664d3 13586 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13587 "different parents"),
9d8780f0
SM
13588 sect_offset_str (child_die->sect_off),
13589 sect_offset_str (child_origin_die->sect_off));
c38f313d 13590 else
791afaa2 13591 offsets.push_back (child_origin_die->sect_off);
d389af10 13592 }
d389af10 13593 }
791afaa2
TT
13594 std::sort (offsets.begin (), offsets.end ());
13595 sect_offset *offsets_end = offsets.data () + offsets.size ();
13596 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13597 if (offsetp[-1] == *offsetp)
b98664d3 13598 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13599 "to DIE %s as their abstract origin"),
13600 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13601
791afaa2 13602 offsetp = offsets.data ();
d389af10
JK
13603 origin_child_die = origin_die->child;
13604 while (origin_child_die && origin_child_die->tag)
13605 {
13606 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13607 while (offsetp < offsets_end
9c541725 13608 && *offsetp < origin_child_die->sect_off)
d389af10 13609 offsetp++;
b64f50a1 13610 if (offsetp >= offsets_end
9c541725 13611 || *offsetp > origin_child_die->sect_off)
d389af10 13612 {
adde2bff
DE
13613 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13614 Check whether we're already processing ORIGIN_CHILD_DIE.
13615 This can happen with mutually referenced abstract_origins.
13616 PR 16581. */
13617 if (!origin_child_die->in_process)
13618 process_die (origin_child_die, origin_cu);
d389af10 13619 }
436c571c 13620 origin_child_die = origin_child_die->sibling;
d389af10 13621 }
cd02d79d 13622 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13623
13624 if (cu != origin_cu)
13625 compute_delayed_physnames (origin_cu);
d389af10
JK
13626}
13627
c906108c 13628static void
e7c27a73 13629read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13630{
5e22e966 13631 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13632 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13633 struct context_stack *newobj;
c906108c
SS
13634 CORE_ADDR lowpc;
13635 CORE_ADDR highpc;
13636 struct die_info *child_die;
edb3359d 13637 struct attribute *attr, *call_line, *call_file;
15d034d0 13638 const char *name;
e142c38c 13639 CORE_ADDR baseaddr;
801e3a5b 13640 struct block *block;
edb3359d 13641 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13642 std::vector<struct symbol *> template_args;
34eaf542 13643 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13644
13645 if (inlined_func)
13646 {
13647 /* If we do not have call site information, we can't show the
13648 caller of this inlined function. That's too confusing, so
13649 only use the scope for local variables. */
13650 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13651 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13652 if (call_line == NULL || call_file == NULL)
13653 {
13654 read_lexical_block_scope (die, cu);
13655 return;
13656 }
13657 }
c906108c 13658
b3b3bada 13659 baseaddr = objfile->text_section_offset ();
e142c38c 13660
94af9270 13661 name = dwarf2_name (die, cu);
c906108c 13662
e8d05480
JB
13663 /* Ignore functions with missing or empty names. These are actually
13664 illegal according to the DWARF standard. */
13665 if (name == NULL)
13666 {
b98664d3 13667 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13668 sect_offset_str (die->sect_off));
e8d05480
JB
13669 return;
13670 }
13671
13672 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13673 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13674 <= PC_BOUNDS_INVALID)
e8d05480 13675 {
ae4d0c03 13676 attr = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 13677 if (attr == nullptr || !attr->as_boolean ())
b98664d3 13678 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13679 "for subprogram DIE at %s"),
13680 sect_offset_str (die->sect_off));
e8d05480
JB
13681 return;
13682 }
c906108c 13683
3e29f34a
MR
13684 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13685 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13686
34eaf542
TT
13687 /* If we have any template arguments, then we must allocate a
13688 different sort of symbol. */
436c571c 13689 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13690 {
13691 if (child_die->tag == DW_TAG_template_type_param
13692 || child_die->tag == DW_TAG_template_value_param)
13693 {
8c14c3a3 13694 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13695 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13696 break;
13697 }
13698 }
13699
c24bdb02 13700 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13701 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13702 (struct symbol *) templ_func);
4c2df51b 13703
81873cc8 13704 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13705 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13706 cu->language);
13707
4cecd739
DJ
13708 /* If there is a location expression for DW_AT_frame_base, record
13709 it. */
e142c38c 13710 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13711 if (attr != nullptr)
fe978cb0 13712 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13713
63e43d3a
PMR
13714 /* If there is a location for the static link, record it. */
13715 newobj->static_link = NULL;
13716 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13717 if (attr != nullptr)
63e43d3a 13718 {
224c3ddb
SM
13719 newobj->static_link
13720 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13721 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13722 cu->addr_type ());
63e43d3a
PMR
13723 }
13724
c24bdb02 13725 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13726
639d11d3 13727 if (die->child != NULL)
c906108c 13728 {
639d11d3 13729 child_die = die->child;
c906108c
SS
13730 while (child_die && child_die->tag)
13731 {
34eaf542
TT
13732 if (child_die->tag == DW_TAG_template_type_param
13733 || child_die->tag == DW_TAG_template_value_param)
13734 {
13735 struct symbol *arg = new_symbol (child_die, NULL, cu);
13736
f1078f66 13737 if (arg != NULL)
2f4732b0 13738 template_args.push_back (arg);
34eaf542
TT
13739 }
13740 else
13741 process_die (child_die, cu);
436c571c 13742 child_die = child_die->sibling;
c906108c
SS
13743 }
13744 }
13745
d389af10
JK
13746 inherit_abstract_dies (die, cu);
13747
4a811a97
UW
13748 /* If we have a DW_AT_specification, we might need to import using
13749 directives from the context of the specification DIE. See the
13750 comment in determine_prefix. */
13751 if (cu->language == language_cplus
13752 && dwarf2_attr (die, DW_AT_specification, cu))
13753 {
13754 struct dwarf2_cu *spec_cu = cu;
13755 struct die_info *spec_die = die_specification (die, &spec_cu);
13756
13757 while (spec_die)
13758 {
13759 child_die = spec_die->child;
13760 while (child_die && child_die->tag)
13761 {
13762 if (child_die->tag == DW_TAG_imported_module)
13763 process_die (child_die, spec_cu);
436c571c 13764 child_die = child_die->sibling;
4a811a97
UW
13765 }
13766
13767 /* In some cases, GCC generates specification DIEs that
13768 themselves contain DW_AT_specification attributes. */
13769 spec_die = die_specification (spec_die, &spec_cu);
13770 }
13771 }
13772
c24bdb02 13773 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13774 /* Make a block for the local symbols within. */
c24bdb02 13775 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13776 cstk.static_link, lowpc, highpc);
801e3a5b 13777
df8a16a1 13778 /* For C++, set the block's scope. */
45280282
IB
13779 if ((cu->language == language_cplus
13780 || cu->language == language_fortran
c44af4eb
TT
13781 || cu->language == language_d
13782 || cu->language == language_rust)
4d4ec4e5 13783 && cu->processing_has_namespace_info)
195a3f6c
TT
13784 block_set_scope (block, determine_prefix (die, cu),
13785 &objfile->objfile_obstack);
df8a16a1 13786
801e3a5b
JB
13787 /* If we have address ranges, record them. */
13788 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13789
a60f3166 13790 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13791
34eaf542 13792 /* Attach template arguments to function. */
2f4732b0 13793 if (!template_args.empty ())
34eaf542
TT
13794 {
13795 gdb_assert (templ_func != NULL);
13796
2f4732b0 13797 templ_func->n_template_arguments = template_args.size ();
34eaf542 13798 templ_func->template_arguments
dda83cd7 13799 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
8d749320 13800 templ_func->n_template_arguments);
34eaf542 13801 memcpy (templ_func->template_arguments,
2f4732b0 13802 template_args.data (),
34eaf542 13803 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13804
13805 /* Make sure that the symtab is set on the new symbols. Even
13806 though they don't appear in this symtab directly, other parts
13807 of gdb assume that symbols do, and this is reasonably
13808 true. */
8634679f 13809 for (symbol *sym : template_args)
3e1d3d8c 13810 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13811 }
13812
208d8187
JB
13813 /* In C++, we can have functions nested inside functions (e.g., when
13814 a function declares a class that has methods). This means that
13815 when we finish processing a function scope, we may need to go
13816 back to building a containing block's symbol lists. */
c24bdb02
KS
13817 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13818 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13819
921e78cf
JB
13820 /* If we've finished processing a top-level function, subsequent
13821 symbols go in the file symbol list. */
c24bdb02
KS
13822 if (cu->get_builder ()->outermost_context_p ())
13823 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13824}
13825
13826/* Process all the DIES contained within a lexical block scope. Start
13827 a new scope, process the dies, and then close the scope. */
13828
13829static void
e7c27a73 13830read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13831{
5e22e966 13832 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13833 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13834 CORE_ADDR lowpc, highpc;
13835 struct die_info *child_die;
e142c38c
DJ
13836 CORE_ADDR baseaddr;
13837
b3b3bada 13838 baseaddr = objfile->text_section_offset ();
c906108c
SS
13839
13840 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13841 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13842 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13843 be nasty. Might be easier to properly extend generic blocks to
af34e669 13844 describe ranges. */
e385593e
JK
13845 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13846 {
13847 case PC_BOUNDS_NOT_PRESENT:
13848 /* DW_TAG_lexical_block has no attributes, process its children as if
13849 there was no wrapping by that DW_TAG_lexical_block.
13850 GCC does no longer produces such DWARF since GCC r224161. */
13851 for (child_die = die->child;
13852 child_die != NULL && child_die->tag;
436c571c 13853 child_die = child_die->sibling)
4f7bc5ed
TT
13854 {
13855 /* We might already be processing this DIE. This can happen
13856 in an unusual circumstance -- where a subroutine A
13857 appears lexically in another subroutine B, but A actually
13858 inlines B. The recursion is broken here, rather than in
13859 inherit_abstract_dies, because it seems better to simply
13860 drop concrete children here. */
13861 if (!child_die->in_process)
13862 process_die (child_die, cu);
13863 }
e385593e
JK
13864 return;
13865 case PC_BOUNDS_INVALID:
13866 return;
13867 }
3e29f34a
MR
13868 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13869 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13870
c24bdb02 13871 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13872 if (die->child != NULL)
c906108c 13873 {
639d11d3 13874 child_die = die->child;
c906108c
SS
13875 while (child_die && child_die->tag)
13876 {
e7c27a73 13877 process_die (child_die, cu);
436c571c 13878 child_die = child_die->sibling;
c906108c
SS
13879 }
13880 }
3ea89b92 13881 inherit_abstract_dies (die, cu);
c24bdb02 13882 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13883
c24bdb02
KS
13884 if (*cu->get_builder ()->get_local_symbols () != NULL
13885 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13886 {
801e3a5b 13887 struct block *block
dda83cd7 13888 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13889 cstk.start_addr, highpc);
801e3a5b
JB
13890
13891 /* Note that recording ranges after traversing children, as we
dda83cd7
SM
13892 do here, means that recording a parent's ranges entails
13893 walking across all its children's ranges as they appear in
13894 the address map, which is quadratic behavior.
13895
13896 It would be nicer to record the parent's ranges before
13897 traversing its children, simply overriding whatever you find
13898 there. But since we don't even decide whether to create a
13899 block until after we've traversed its children, that's hard
13900 to do. */
801e3a5b 13901 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13902 }
c24bdb02
KS
13903 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13904 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13905}
13906
216f72a1 13907/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13908
13909static void
13910read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13911{
5e22e966 13912 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13913 struct objfile *objfile = per_objfile->objfile;
08feed99 13914 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13915 CORE_ADDR pc, baseaddr;
13916 struct attribute *attr;
13917 struct call_site *call_site, call_site_local;
13918 void **slot;
13919 int nparams;
13920 struct die_info *child_die;
13921
b3b3bada 13922 baseaddr = objfile->text_section_offset ();
96408a79 13923
216f72a1
JK
13924 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13925 if (attr == NULL)
13926 {
13927 /* This was a pre-DWARF-5 GNU extension alias
13928 for DW_AT_call_return_pc. */
13929 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13930 }
96408a79
SA
13931 if (!attr)
13932 {
b98664d3 13933 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13934 "DIE %s [in module %s]"),
13935 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13936 return;
13937 }
95f982e5 13938 pc = attr->as_address () + baseaddr;
3e29f34a 13939 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13940
13941 if (cu->call_site_htab == NULL)
13942 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13943 NULL, &objfile->objfile_obstack,
13944 hashtab_obstack_allocate, NULL);
13945 call_site_local.pc = pc;
13946 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13947 if (*slot != NULL)
13948 {
b98664d3 13949 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13950 "DIE %s [in module %s]"),
13951 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13952 objfile_name (objfile));
96408a79
SA
13953 return;
13954 }
13955
13956 /* Count parameters at the caller. */
13957
13958 nparams = 0;
13959 for (child_die = die->child; child_die && child_die->tag;
436c571c 13960 child_die = child_die->sibling)
96408a79 13961 {
216f72a1 13962 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13963 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13964 {
b98664d3 13965 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13966 "DW_TAG_call_site child DIE %s [in module %s]"),
13967 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13968 objfile_name (objfile));
96408a79
SA
13969 continue;
13970 }
13971
13972 nparams++;
13973 }
13974
224c3ddb
SM
13975 call_site
13976 = ((struct call_site *)
13977 obstack_alloc (&objfile->objfile_obstack,
13978 sizeof (*call_site)
13979 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13980 *slot = call_site;
13981 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13982 call_site->pc = pc;
13983
216f72a1
JK
13984 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13985 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13986 {
13987 struct die_info *func_die;
13988
13989 /* Skip also over DW_TAG_inlined_subroutine. */
13990 for (func_die = die->parent;
13991 func_die && func_die->tag != DW_TAG_subprogram
13992 && func_die->tag != DW_TAG_subroutine_type;
13993 func_die = func_die->parent);
13994
216f72a1
JK
13995 /* DW_AT_call_all_calls is a superset
13996 of DW_AT_call_all_tail_calls. */
96408a79 13997 if (func_die
dda83cd7
SM
13998 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13999 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14000 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14001 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14002 {
14003 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14004 not complete. But keep CALL_SITE for look ups via call_site_htab,
14005 both the initial caller containing the real return address PC and
14006 the final callee containing the current PC of a chain of tail
14007 calls do not need to have the tail call list complete. But any
14008 function candidate for a virtual tail call frame searched via
14009 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14010 determined unambiguously. */
14011 }
14012 else
14013 {
14014 struct type *func_type = NULL;
14015
14016 if (func_die)
14017 func_type = get_die_type (func_die, cu);
14018 if (func_type != NULL)
14019 {
78134374 14020 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
14021
14022 /* Enlist this call site to the function. */
14023 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14024 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14025 }
14026 else
b98664d3 14027 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14028 "DIE %s [in module %s]"),
14029 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14030 }
14031 }
14032
216f72a1
JK
14033 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14034 if (attr == NULL)
14035 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14036 if (attr == NULL)
14037 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14038 if (attr == NULL)
216f72a1
JK
14039 {
14040 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14041 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14042 }
96408a79 14043 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9d2246fc 14044 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
96408a79 14045 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 14046 else if (attr->form_is_block ())
96408a79
SA
14047 {
14048 struct dwarf2_locexpr_baton *dlbaton;
9d2246fc 14049 struct dwarf_block *block = attr->as_block ();
96408a79 14050
8d749320 14051 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9d2246fc
TT
14052 dlbaton->data = block->data;
14053 dlbaton->size = block->size;
a50264ba 14054 dlbaton->per_objfile = per_objfile;
96408a79
SA
14055 dlbaton->per_cu = cu->per_cu;
14056
14057 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14058 }
cd6c91b4 14059 else if (attr->form_is_ref ())
96408a79 14060 {
96408a79
SA
14061 struct dwarf2_cu *target_cu = cu;
14062 struct die_info *target_die;
14063
ac9ec31b 14064 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 14065 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
14066 if (die_is_declaration (target_die, target_cu))
14067 {
7d45c7c3 14068 const char *target_physname;
9112db09
JK
14069
14070 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14071 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14072 if (target_physname == NULL)
9112db09 14073 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14074 if (target_physname == NULL)
b98664d3 14075 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 14076 "physname, for referencing DIE %s [in module %s]"),
9d8780f0 14077 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14078 else
7d455152 14079 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14080 }
14081 else
14082 {
14083 CORE_ADDR lowpc;
14084
14085 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14086 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14087 <= PC_BOUNDS_INVALID)
b98664d3 14088 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 14089 "low pc, for referencing DIE %s [in module %s]"),
9d8780f0 14090 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14091 else
3e29f34a
MR
14092 {
14093 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14094 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14095 }
96408a79
SA
14096 }
14097 }
14098 else
b98664d3 14099 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14100 "block nor reference, for DIE %s [in module %s]"),
14101 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14102
14103 call_site->per_cu = cu->per_cu;
9f47c707 14104 call_site->per_objfile = per_objfile;
96408a79
SA
14105
14106 for (child_die = die->child;
14107 child_die && child_die->tag;
436c571c 14108 child_die = child_die->sibling)
96408a79 14109 {
96408a79 14110 struct call_site_parameter *parameter;
1788b2d3 14111 struct attribute *loc, *origin;
96408a79 14112
216f72a1 14113 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 14114 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14115 {
14116 /* Already printed the complaint above. */
14117 continue;
14118 }
14119
14120 gdb_assert (call_site->parameter_count < nparams);
14121 parameter = &call_site->parameter[call_site->parameter_count];
14122
1788b2d3
JK
14123 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14124 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14125 register is contained in DW_AT_call_value. */
96408a79 14126
24c5c679 14127 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14128 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14129 if (origin == NULL)
14130 {
14131 /* This was a pre-DWARF-5 GNU extension alias
14132 for DW_AT_call_parameter. */
14133 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14134 }
cd6c91b4 14135 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 14136 {
1788b2d3 14137 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 14138
0826b30a 14139 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 14140 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
14141 {
14142 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14143 binding can be done only inside one CU. Such referenced DIE
14144 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14145 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14146 "DW_TAG_call_site child DIE %s [in module %s]"),
14147 sect_offset_str (child_die->sect_off),
9c541725 14148 objfile_name (objfile));
d76b7dbc
JK
14149 continue;
14150 }
9c541725
PA
14151 parameter->u.param_cu_off
14152 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 14153 }
4fc6c0d5 14154 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 14155 {
b98664d3 14156 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14157 "DW_TAG_call_site child DIE %s [in module %s]"),
14158 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14159 continue;
14160 }
24c5c679 14161 else
96408a79 14162 {
9d2246fc
TT
14163 struct dwarf_block *block = loc->as_block ();
14164
24c5c679 14165 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9d2246fc 14166 (block->data, &block->data[block->size]);
24c5c679
JK
14167 if (parameter->u.dwarf_reg != -1)
14168 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9d2246fc
TT
14169 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14170 &block->data[block->size],
24c5c679
JK
14171 &parameter->u.fb_offset))
14172 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14173 else
14174 {
b98664d3 14175 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14176 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14177 "DW_TAG_call_site child DIE %s "
24c5c679 14178 "[in module %s]"),
9d8780f0 14179 sect_offset_str (child_die->sect_off),
9c541725 14180 objfile_name (objfile));
24c5c679
JK
14181 continue;
14182 }
96408a79
SA
14183 }
14184
216f72a1
JK
14185 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14186 if (attr == NULL)
14187 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 14188 if (attr == NULL || !attr->form_is_block ())
96408a79 14189 {
b98664d3 14190 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14191 "DW_TAG_call_site child DIE %s [in module %s]"),
14192 sect_offset_str (child_die->sect_off),
9c541725 14193 objfile_name (objfile));
96408a79
SA
14194 continue;
14195 }
9d2246fc
TT
14196
14197 struct dwarf_block *block = attr->as_block ();
14198 parameter->value = block->data;
14199 parameter->value_size = block->size;
96408a79
SA
14200
14201 /* Parameters are not pre-cleared by memset above. */
14202 parameter->data_value = NULL;
14203 parameter->data_value_size = 0;
14204 call_site->parameter_count++;
14205
216f72a1
JK
14206 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14207 if (attr == NULL)
14208 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14209 if (attr != nullptr)
96408a79 14210 {
4fc6c0d5 14211 if (!attr->form_is_block ())
b98664d3 14212 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14213 "DW_TAG_call_site child DIE %s [in module %s]"),
14214 sect_offset_str (child_die->sect_off),
9c541725 14215 objfile_name (objfile));
96408a79
SA
14216 else
14217 {
9d2246fc
TT
14218 block = attr->as_block ();
14219 parameter->data_value = block->data;
14220 parameter->data_value_size = block->size;
96408a79
SA
14221 }
14222 }
14223 }
14224}
14225
71a3c369
TT
14226/* Helper function for read_variable. If DIE represents a virtual
14227 table, then return the type of the concrete object that is
14228 associated with the virtual table. Otherwise, return NULL. */
14229
14230static struct type *
14231rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14232{
14233 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14234 if (attr == NULL)
14235 return NULL;
14236
14237 /* Find the type DIE. */
14238 struct die_info *type_die = NULL;
14239 struct dwarf2_cu *type_cu = cu;
14240
cd6c91b4 14241 if (attr->form_is_ref ())
71a3c369
TT
14242 type_die = follow_die_ref (die, attr, &type_cu);
14243 if (type_die == NULL)
14244 return NULL;
14245
14246 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14247 return NULL;
14248 return die_containing_type (type_die, type_cu);
14249}
14250
14251/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14252
14253static void
14254read_variable (struct die_info *die, struct dwarf2_cu *cu)
14255{
14256 struct rust_vtable_symbol *storage = NULL;
14257
14258 if (cu->language == language_rust)
14259 {
14260 struct type *containing_type = rust_containing_type (die, cu);
14261
14262 if (containing_type != NULL)
14263 {
5e22e966 14264 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 14265
8c14c3a3 14266 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 14267 storage->concrete_type = containing_type;
cf724bc9 14268 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14269 }
14270 }
14271
e4a62c65
TV
14272 struct symbol *res = new_symbol (die, NULL, cu, storage);
14273 struct attribute *abstract_origin
14274 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14275 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14276 if (res == NULL && loc && abstract_origin)
14277 {
14278 /* We have a variable without a name, but with a location and an abstract
14279 origin. This may be a concrete instance of an abstract variable
14280 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14281 later. */
14282 struct dwarf2_cu *origin_cu = cu;
14283 struct die_info *origin_die
14284 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
14285 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14286 per_objfile->per_bfd->abstract_to_concrete
14287 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14288 }
71a3c369
TT
14289}
14290
43988095
JK
14291/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14292 reading .debug_rnglists.
14293 Callback's type should be:
14294 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14295 Return true if the attributes are present and valid, otherwise,
14296 return false. */
14297
14298template <typename Callback>
14299static bool
14300dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
d0ce17d8 14301 dwarf_tag tag, Callback &&callback)
43988095 14302{
976ca316
SM
14303 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14304 struct objfile *objfile = per_objfile->objfile;
43988095 14305 bfd *obfd = objfile->obfd;
43988095 14306 /* Base address selection entry. */
2b24b6e4 14307 gdb::optional<CORE_ADDR> base;
43988095 14308 const gdb_byte *buffer;
43988095
JK
14309 CORE_ADDR baseaddr;
14310 bool overflow = false;
d0ce17d8
CT
14311 ULONGEST addr_index;
14312 struct dwarf2_section_info *rnglists_section;
43988095 14313
43988095 14314 base = cu->base_address;
d0ce17d8
CT
14315 rnglists_section = cu_debug_rnglists_section (cu, tag);
14316 rnglists_section->read (objfile);
43988095 14317
d0ce17d8 14318 if (offset >= rnglists_section->size)
43988095 14319 {
b98664d3 14320 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14321 offset);
14322 return false;
14323 }
d0ce17d8 14324 buffer = rnglists_section->buffer + offset;
43988095 14325
b3b3bada 14326 baseaddr = objfile->text_section_offset ();
43988095
JK
14327
14328 while (1)
14329 {
7814882a
JK
14330 /* Initialize it due to a false compiler warning. */
14331 CORE_ADDR range_beginning = 0, range_end = 0;
d0ce17d8
CT
14332 const gdb_byte *buf_end = (rnglists_section->buffer
14333 + rnglists_section->size);
43988095
JK
14334 unsigned int bytes_read;
14335
14336 if (buffer == buf_end)
14337 {
14338 overflow = true;
14339 break;
14340 }
14341 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14342 switch (rlet)
14343 {
14344 case DW_RLE_end_of_list:
14345 break;
14346 case DW_RLE_base_address:
14347 if (buffer + cu->header.addr_size > buf_end)
14348 {
14349 overflow = true;
14350 break;
14351 }
c8a7a66f 14352 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14353 buffer += bytes_read;
14354 break;
dda83cd7
SM
14355 case DW_RLE_base_addressx:
14356 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14357 buffer += bytes_read;
14358 base = read_addr_index (cu, addr_index);
14359 break;
43988095
JK
14360 case DW_RLE_start_length:
14361 if (buffer + cu->header.addr_size > buf_end)
14362 {
14363 overflow = true;
14364 break;
14365 }
c8a7a66f
TT
14366 range_beginning = cu->header.read_address (obfd, buffer,
14367 &bytes_read);
43988095
JK
14368 buffer += bytes_read;
14369 range_end = (range_beginning
14370 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14371 buffer += bytes_read;
14372 if (buffer > buf_end)
14373 {
14374 overflow = true;
14375 break;
14376 }
14377 break;
d0ce17d8 14378 case DW_RLE_startx_length:
dda83cd7
SM
14379 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14380 buffer += bytes_read;
14381 range_beginning = read_addr_index (cu, addr_index);
14382 if (buffer > buf_end)
14383 {
14384 overflow = true;
14385 break;
14386 }
14387 range_end = (range_beginning
14388 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14389 buffer += bytes_read;
14390 break;
43988095
JK
14391 case DW_RLE_offset_pair:
14392 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14393 buffer += bytes_read;
14394 if (buffer > buf_end)
14395 {
14396 overflow = true;
14397 break;
14398 }
14399 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14400 buffer += bytes_read;
14401 if (buffer > buf_end)
14402 {
14403 overflow = true;
14404 break;
14405 }
14406 break;
14407 case DW_RLE_start_end:
14408 if (buffer + 2 * cu->header.addr_size > buf_end)
14409 {
14410 overflow = true;
14411 break;
14412 }
c8a7a66f
TT
14413 range_beginning = cu->header.read_address (obfd, buffer,
14414 &bytes_read);
43988095 14415 buffer += bytes_read;
c8a7a66f 14416 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14417 buffer += bytes_read;
14418 break;
d0ce17d8 14419 case DW_RLE_startx_endx:
dda83cd7
SM
14420 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14421 buffer += bytes_read;
14422 range_beginning = read_addr_index (cu, addr_index);
14423 if (buffer > buf_end)
14424 {
14425 overflow = true;
14426 break;
14427 }
14428 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14429 buffer += bytes_read;
14430 range_end = read_addr_index (cu, addr_index);
14431 break;
43988095 14432 default:
b98664d3 14433 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14434 return false;
14435 }
14436 if (rlet == DW_RLE_end_of_list || overflow)
14437 break;
14438 if (rlet == DW_RLE_base_address)
14439 continue;
14440
43988095
JK
14441 if (range_beginning > range_end)
14442 {
14443 /* Inverted range entries are invalid. */
b98664d3 14444 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14445 return false;
14446 }
14447
14448 /* Empty range entries have no effect. */
14449 if (range_beginning == range_end)
14450 continue;
14451
d0ce17d8
CT
14452 /* Only DW_RLE_offset_pair needs the base address added. */
14453 if (rlet == DW_RLE_offset_pair)
14454 {
14455 if (!base.has_value ())
14456 {
14457 /* We have no valid base address for the DW_RLE_offset_pair. */
14458 complaint (_("Invalid .debug_rnglists data (no base address for "
14459 "DW_RLE_offset_pair)"));
14460 return false;
14461 }
14462
14463 range_beginning += *base;
14464 range_end += *base;
14465 }
43988095
JK
14466
14467 /* A not-uncommon case of bad debug info.
14468 Don't pollute the addrmap with bad data. */
14469 if (range_beginning + baseaddr == 0
976ca316 14470 && !per_objfile->per_bfd->has_section_at_zero)
43988095 14471 {
b98664d3 14472 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14473 " [in module %s]"), objfile_name (objfile));
14474 continue;
14475 }
14476
14477 callback (range_beginning, range_end);
14478 }
14479
14480 if (overflow)
14481 {
b98664d3 14482 complaint (_("Offset %d is not terminated "
43988095
JK
14483 "for DW_AT_ranges attribute"),
14484 offset);
14485 return false;
14486 }
14487
14488 return true;
14489}
14490
14491/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14492 Callback's type should be:
14493 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14494 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14495
43988095 14496template <typename Callback>
43039443 14497static int
d0ce17d8 14498dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
43988095 14499 Callback &&callback)
43039443 14500{
5e22e966
SM
14501 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14502 struct objfile *objfile = per_objfile->objfile;
43039443
JK
14503 struct comp_unit_head *cu_header = &cu->header;
14504 bfd *obfd = objfile->obfd;
14505 unsigned int addr_size = cu_header->addr_size;
14506 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14507 /* Base address selection entry. */
2b24b6e4 14508 gdb::optional<CORE_ADDR> base;
43039443 14509 unsigned int dummy;
d521ce57 14510 const gdb_byte *buffer;
ff013f42 14511 CORE_ADDR baseaddr;
43039443 14512
43988095 14513 if (cu_header->version >= 5)
d0ce17d8 14514 return dwarf2_rnglists_process (offset, cu, tag, callback);
43988095 14515
d00adf39 14516 base = cu->base_address;
43039443 14517
5e22e966
SM
14518 per_objfile->per_bfd->ranges.read (objfile);
14519 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 14520 {
b98664d3 14521 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14522 offset);
14523 return 0;
14524 }
5e22e966 14525 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 14526
b3b3bada 14527 baseaddr = objfile->text_section_offset ();
ff013f42 14528
43039443
JK
14529 while (1)
14530 {
14531 CORE_ADDR range_beginning, range_end;
14532
c8a7a66f 14533 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 14534 buffer += addr_size;
c8a7a66f 14535 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
14536 buffer += addr_size;
14537 offset += 2 * addr_size;
14538
14539 /* An end of list marker is a pair of zero addresses. */
14540 if (range_beginning == 0 && range_end == 0)
14541 /* Found the end of list entry. */
14542 break;
14543
14544 /* Each base address selection entry is a pair of 2 values.
14545 The first is the largest possible address, the second is
14546 the base address. Check for a base address here. */
14547 if ((range_beginning & mask) == mask)
14548 {
28d2bfb9
AB
14549 /* If we found the largest possible address, then we already
14550 have the base address in range_end. */
14551 base = range_end;
43039443
JK
14552 continue;
14553 }
14554
2b24b6e4 14555 if (!base.has_value ())
43039443
JK
14556 {
14557 /* We have no valid base address for the ranges
14558 data. */
b98664d3 14559 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14560 return 0;
14561 }
14562
9277c30c
UW
14563 if (range_beginning > range_end)
14564 {
14565 /* Inverted range entries are invalid. */
b98664d3 14566 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14567 return 0;
14568 }
14569
14570 /* Empty range entries have no effect. */
14571 if (range_beginning == range_end)
14572 continue;
14573
2b24b6e4
TT
14574 range_beginning += *base;
14575 range_end += *base;
43039443 14576
01093045
DE
14577 /* A not-uncommon case of bad debug info.
14578 Don't pollute the addrmap with bad data. */
14579 if (range_beginning + baseaddr == 0
5e22e966 14580 && !per_objfile->per_bfd->has_section_at_zero)
01093045 14581 {
b98664d3 14582 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14583 " [in module %s]"), objfile_name (objfile));
01093045
DE
14584 continue;
14585 }
14586
5f46c5a5
JK
14587 callback (range_beginning, range_end);
14588 }
14589
14590 return 1;
14591}
14592
14593/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14594 Return 1 if the attributes are present and valid, otherwise, return 0.
14595 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14596
14597static int
14598dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14599 CORE_ADDR *high_return, struct dwarf2_cu *cu,
d0ce17d8 14600 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
5f46c5a5 14601{
5e22e966 14602 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14603 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14604 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14605 int low_set = 0;
14606 CORE_ADDR low = 0;
14607 CORE_ADDR high = 0;
14608 int retval;
14609
d0ce17d8 14610 retval = dwarf2_ranges_process (offset, cu, tag,
5f46c5a5
JK
14611 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14612 {
9277c30c 14613 if (ranges_pst != NULL)
3e29f34a
MR
14614 {
14615 CORE_ADDR lowpc;
14616 CORE_ADDR highpc;
14617
79748972
TT
14618 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14619 range_beginning + baseaddr)
14620 - baseaddr);
14621 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14622 range_end + baseaddr)
14623 - baseaddr);
d320c2b5
TT
14624 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14625 lowpc, highpc - 1, ranges_pst);
3e29f34a 14626 }
ff013f42 14627
43039443
JK
14628 /* FIXME: This is recording everything as a low-high
14629 segment of consecutive addresses. We should have a
14630 data structure for discontiguous block ranges
14631 instead. */
14632 if (! low_set)
14633 {
14634 low = range_beginning;
14635 high = range_end;
14636 low_set = 1;
14637 }
14638 else
14639 {
14640 if (range_beginning < low)
14641 low = range_beginning;
14642 if (range_end > high)
14643 high = range_end;
14644 }
5f46c5a5
JK
14645 });
14646 if (!retval)
14647 return 0;
43039443
JK
14648
14649 if (! low_set)
14650 /* If the first entry is an end-of-list marker, the range
14651 describes an empty scope, i.e. no instructions. */
14652 return 0;
14653
14654 if (low_return)
14655 *low_return = low;
14656 if (high_return)
14657 *high_return = high;
14658 return 1;
14659}
14660
3a2b436a
JK
14661/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14662 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14663 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14664
3a2b436a 14665static enum pc_bounds_kind
af34e669 14666dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14667 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14668 dwarf2_psymtab *pst)
c906108c 14669{
976ca316 14670 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14671 struct attribute *attr;
91da1414 14672 struct attribute *attr_high;
af34e669
DJ
14673 CORE_ADDR low = 0;
14674 CORE_ADDR high = 0;
e385593e 14675 enum pc_bounds_kind ret;
c906108c 14676
91da1414
MW
14677 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14678 if (attr_high)
af34e669 14679 {
e142c38c 14680 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14681 if (attr != nullptr)
dda83cd7 14682 {
95f982e5
TT
14683 low = attr->as_address ();
14684 high = attr_high->as_address ();
cd6c91b4 14685 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14686 high += low;
91da1414 14687 }
af34e669
DJ
14688 else
14689 /* Found high w/o low attribute. */
e385593e 14690 return PC_BOUNDS_INVALID;
af34e669
DJ
14691
14692 /* Found consecutive range of addresses. */
3a2b436a 14693 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14694 }
c906108c 14695 else
af34e669 14696 {
e142c38c 14697 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14698 if (attr != nullptr && attr->form_is_unsigned ())
af34e669 14699 {
2b0c7f41
SM
14700 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14701 on DWARF version). */
14702 ULONGEST ranges_offset = attr->as_unsigned ();
14703
14704 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14705 this value. */
14706 if (die->tag != DW_TAG_compile_unit)
14707 ranges_offset += cu->gnu_ranges_base;
2e3cf129 14708
af34e669 14709 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14710 .debug_ranges section. */
d0ce17d8
CT
14711 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14712 die->tag))
e385593e 14713 return PC_BOUNDS_INVALID;
43039443 14714 /* Found discontinuous range of addresses. */
3a2b436a 14715 ret = PC_BOUNDS_RANGES;
af34e669 14716 }
e385593e
JK
14717 else
14718 return PC_BOUNDS_NOT_PRESENT;
af34e669 14719 }
c906108c 14720
48fbe735 14721 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14722 if (high <= low)
e385593e 14723 return PC_BOUNDS_INVALID;
c906108c
SS
14724
14725 /* When using the GNU linker, .gnu.linkonce. sections are used to
14726 eliminate duplicate copies of functions and vtables and such.
14727 The linker will arbitrarily choose one and discard the others.
14728 The AT_*_pc values for such functions refer to local labels in
14729 these sections. If the section from that file was discarded, the
14730 labels are not in the output, so the relocs get a value of 0.
14731 If this is a discarded function, mark the pc bounds as invalid,
14732 so that GDB will ignore it. */
976ca316 14733 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14734 return PC_BOUNDS_INVALID;
c906108c
SS
14735
14736 *lowpc = low;
96408a79
SA
14737 if (highpc)
14738 *highpc = high;
af34e669 14739 return ret;
c906108c
SS
14740}
14741
b084d499
JB
14742/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14743 its low and high PC addresses. Do nothing if these addresses could not
14744 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14745 and HIGHPC to the high address if greater than HIGHPC. */
14746
14747static void
14748dwarf2_get_subprogram_pc_bounds (struct die_info *die,
dda83cd7
SM
14749 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14750 struct dwarf2_cu *cu)
b084d499
JB
14751{
14752 CORE_ADDR low, high;
14753 struct die_info *child = die->child;
14754
e385593e 14755 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14756 {
325fac50
PA
14757 *lowpc = std::min (*lowpc, low);
14758 *highpc = std::max (*highpc, high);
b084d499
JB
14759 }
14760
14761 /* If the language does not allow nested subprograms (either inside
14762 subprograms or lexical blocks), we're done. */
14763 if (cu->language != language_ada)
14764 return;
6e70227d 14765
b084d499
JB
14766 /* Check all the children of the given DIE. If it contains nested
14767 subprograms, then check their pc bounds. Likewise, we need to
14768 check lexical blocks as well, as they may also contain subprogram
14769 definitions. */
14770 while (child && child->tag)
14771 {
14772 if (child->tag == DW_TAG_subprogram
dda83cd7
SM
14773 || child->tag == DW_TAG_lexical_block)
14774 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14775 child = child->sibling;
b084d499
JB
14776 }
14777}
14778
fae299cd
DC
14779/* Get the low and high pc's represented by the scope DIE, and store
14780 them in *LOWPC and *HIGHPC. If the correct values can't be
14781 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14782
14783static void
14784get_scope_pc_bounds (struct die_info *die,
14785 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14786 struct dwarf2_cu *cu)
14787{
14788 CORE_ADDR best_low = (CORE_ADDR) -1;
14789 CORE_ADDR best_high = (CORE_ADDR) 0;
14790 CORE_ADDR current_low, current_high;
14791
3a2b436a 14792 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14793 >= PC_BOUNDS_RANGES)
fae299cd
DC
14794 {
14795 best_low = current_low;
14796 best_high = current_high;
14797 }
14798 else
14799 {
14800 struct die_info *child = die->child;
14801
14802 while (child && child->tag)
14803 {
14804 switch (child->tag) {
14805 case DW_TAG_subprogram:
dda83cd7 14806 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14807 break;
14808 case DW_TAG_namespace:
f55ee35c 14809 case DW_TAG_module:
fae299cd
DC
14810 /* FIXME: carlton/2004-01-16: Should we do this for
14811 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14812 that current GCC's always emit the DIEs corresponding
14813 to definitions of methods of classes as children of a
14814 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14815 the DIEs giving the declarations, which could be
14816 anywhere). But I don't see any reason why the
14817 standards says that they have to be there. */
14818 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14819
14820 if (current_low != ((CORE_ADDR) -1))
14821 {
325fac50
PA
14822 best_low = std::min (best_low, current_low);
14823 best_high = std::max (best_high, current_high);
fae299cd
DC
14824 }
14825 break;
14826 default:
0963b4bd 14827 /* Ignore. */
fae299cd
DC
14828 break;
14829 }
14830
436c571c 14831 child = child->sibling;
fae299cd
DC
14832 }
14833 }
14834
14835 *lowpc = best_low;
14836 *highpc = best_high;
14837}
14838
801e3a5b
JB
14839/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14840 in DIE. */
380bca97 14841
801e3a5b
JB
14842static void
14843dwarf2_record_block_ranges (struct die_info *die, struct block *block,
dda83cd7 14844 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
801e3a5b 14845{
5e22e966 14846 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14847 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14848 struct attribute *attr;
91da1414 14849 struct attribute *attr_high;
801e3a5b 14850
91da1414
MW
14851 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14852 if (attr_high)
801e3a5b 14853 {
801e3a5b 14854 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14855 if (attr != nullptr)
dda83cd7 14856 {
95f982e5
TT
14857 CORE_ADDR low = attr->as_address ();
14858 CORE_ADDR high = attr_high->as_address ();
31aa7e4e 14859
cd6c91b4 14860 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14861 high += low;
9a619af0 14862
3e29f34a
MR
14863 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14864 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14865 cu->get_builder ()->record_block_range (block, low, high - 1);
dda83cd7 14866 }
801e3a5b
JB
14867 }
14868
14869 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14870 if (attr != nullptr && attr->form_is_unsigned ())
801e3a5b 14871 {
2b0c7f41
SM
14872 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14873 on DWARF version). */
14874 ULONGEST ranges_offset = attr->as_unsigned ();
801e3a5b 14875
2b0c7f41
SM
14876 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14877 this value. */
14878 if (die->tag != DW_TAG_compile_unit)
14879 ranges_offset += cu->gnu_ranges_base;
801e3a5b 14880
2d5f09ec 14881 std::vector<blockrange> blockvec;
2b0c7f41 14882 dwarf2_ranges_process (ranges_offset, cu, die->tag,
5f46c5a5
JK
14883 [&] (CORE_ADDR start, CORE_ADDR end)
14884 {
58fdfd2c
JK
14885 start += baseaddr;
14886 end += baseaddr;
5f46c5a5
JK
14887 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14888 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14889 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14890 blockvec.emplace_back (start, end);
5f46c5a5 14891 });
2d5f09ec
KB
14892
14893 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14894 }
14895}
14896
685b1105
JK
14897/* Check whether the producer field indicates either of GCC < 4.6, or the
14898 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14899
685b1105
JK
14900static void
14901check_producer (struct dwarf2_cu *cu)
60d5a603 14902{
38360086 14903 int major, minor;
60d5a603
JK
14904
14905 if (cu->producer == NULL)
14906 {
14907 /* For unknown compilers expect their behavior is DWARF version
14908 compliant.
14909
14910 GCC started to support .debug_types sections by -gdwarf-4 since
14911 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14912 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14913 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14914 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14915 }
b1ffba5a 14916 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14917 {
38360086
MW
14918 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14919 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14920 }
5230b05a 14921 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14922 {
14923 cu->producer_is_icc = true;
14924 cu->producer_is_icc_lt_14 = major < 14;
14925 }
c258c396
JD
14926 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14927 cu->producer_is_codewarrior = true;
685b1105
JK
14928 else
14929 {
14930 /* For other non-GCC compilers, expect their behavior is DWARF version
14931 compliant. */
60d5a603
JK
14932 }
14933
9068261f 14934 cu->checked_producer = true;
685b1105 14935}
ba919b58 14936
685b1105
JK
14937/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14938 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14939 during 4.6.0 experimental. */
14940
9068261f 14941static bool
685b1105
JK
14942producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14943{
14944 if (!cu->checked_producer)
14945 check_producer (cu);
14946
14947 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14948}
14949
c258c396
JD
14950
14951/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14952 with incorrect is_stmt attributes. */
14953
14954static bool
14955producer_is_codewarrior (struct dwarf2_cu *cu)
14956{
14957 if (!cu->checked_producer)
14958 check_producer (cu);
14959
14960 return cu->producer_is_codewarrior;
14961}
14962
bf23a268
TT
14963/* Return the accessibility of DIE, as given by DW_AT_accessibility.
14964 If that attribute is not available, return the appropriate
14965 default. */
60d5a603
JK
14966
14967static enum dwarf_access_attribute
bf23a268 14968dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
60d5a603 14969{
bf23a268
TT
14970 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14971 if (attr != nullptr)
14972 {
14973 LONGEST value = attr->constant_value (-1);
14974 if (value == DW_ACCESS_public
14975 || value == DW_ACCESS_protected
14976 || value == DW_ACCESS_private)
14977 return (dwarf_access_attribute) value;
14978 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14979 plongest (value));
14980 }
14981
60d5a603
JK
14982 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14983 {
14984 /* The default DWARF 2 accessibility for members is public, the default
14985 accessibility for inheritance is private. */
14986
14987 if (die->tag != DW_TAG_inheritance)
14988 return DW_ACCESS_public;
14989 else
14990 return DW_ACCESS_private;
14991 }
14992 else
14993 {
14994 /* DWARF 3+ defines the default accessibility a different way. The same
14995 rules apply now for DW_TAG_inheritance as for the members and it only
14996 depends on the container kind. */
14997
14998 if (die->parent->tag == DW_TAG_class_type)
14999 return DW_ACCESS_private;
15000 else
15001 return DW_ACCESS_public;
15002 }
15003}
15004
74ac6d43
TT
15005/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15006 offset. If the attribute was not found return 0, otherwise return
15007 1. If it was found but could not properly be handled, set *OFFSET
15008 to 0. */
15009
15010static int
15011handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15012 LONGEST *offset)
15013{
15014 struct attribute *attr;
15015
15016 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15017 if (attr != NULL)
15018 {
15019 *offset = 0;
15020
15021 /* Note that we do not check for a section offset first here.
15022 This is because DW_AT_data_member_location is new in DWARF 4,
15023 so if we see it, we can assume that a constant form is really
15024 a constant and not a section offset. */
cd6c91b4 15025 if (attr->form_is_constant ())
0826b30a 15026 *offset = attr->constant_value (0);
cd6c91b4 15027 else if (attr->form_is_section_offset ())
74ac6d43 15028 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 15029 else if (attr->form_is_block ())
9d2246fc 15030 *offset = decode_locdesc (attr->as_block (), cu);
74ac6d43
TT
15031 else
15032 dwarf2_complex_location_expr_complaint ();
15033
15034 return 1;
15035 }
15036
15037 return 0;
15038}
15039
7d79de9a
TT
15040/* Look for DW_AT_data_member_location and store the results in FIELD. */
15041
15042static void
15043handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15044 struct field *field)
15045{
15046 struct attribute *attr;
15047
15048 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15049 if (attr != NULL)
15050 {
15051 if (attr->form_is_constant ())
15052 {
15053 LONGEST offset = attr->constant_value (0);
15054 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15055 }
15056 else if (attr->form_is_section_offset ())
15057 dwarf2_complex_location_expr_complaint ();
15058 else if (attr->form_is_block ())
15059 {
15060 bool handled;
9d2246fc 15061 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
7d79de9a
TT
15062 if (handled)
15063 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15064 else
15065 {
5e22e966
SM
15066 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15067 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
15068 struct dwarf2_locexpr_baton *dlbaton
15069 = XOBNEW (&objfile->objfile_obstack,
15070 struct dwarf2_locexpr_baton);
9d2246fc
TT
15071 dlbaton->data = attr->as_block ()->data;
15072 dlbaton->size = attr->as_block ()->size;
7d79de9a
TT
15073 /* When using this baton, we want to compute the address
15074 of the field, not the value. This is why
15075 is_reference is set to false here. */
15076 dlbaton->is_reference = false;
5e22e966 15077 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
15078 dlbaton->per_cu = cu->per_cu;
15079
15080 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
15081 }
15082 }
15083 else
15084 dwarf2_complex_location_expr_complaint ();
15085 }
15086}
15087
c906108c
SS
15088/* Add an aggregate field to the field list. */
15089
15090static void
107d2387 15091dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15092 struct dwarf2_cu *cu)
6e70227d 15093{
5e22e966 15094 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 15095 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
15096 struct nextfield *new_field;
15097 struct attribute *attr;
15098 struct field *fp;
15d034d0 15099 const char *fieldname = "";
c906108c 15100
7d0ccb61
DJ
15101 if (die->tag == DW_TAG_inheritance)
15102 {
be2daae6
TT
15103 fip->baseclasses.emplace_back ();
15104 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15105 }
15106 else
15107 {
be2daae6
TT
15108 fip->fields.emplace_back ();
15109 new_field = &fip->fields.back ();
7d0ccb61 15110 }
be2daae6 15111
9c6a1327
TT
15112 new_field->offset = die->sect_off;
15113
bf23a268 15114 new_field->accessibility = dwarf2_access_attribute (die, cu);
c906108c 15115 if (new_field->accessibility != DW_ACCESS_public)
264fc0e2 15116 fip->non_public_fields = true;
60d5a603 15117
e142c38c 15118 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 15119 if (attr != nullptr)
23dca5c3 15120 new_field->virtuality = attr->as_virtuality ();
60d5a603
JK
15121 else
15122 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15123
15124 fp = &new_field->field;
a9a9bd0f 15125
e142c38c 15126 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15127 {
a9a9bd0f 15128 /* Data member other than a C++ static data member. */
6e70227d 15129
c906108c 15130 /* Get type of field. */
5d14b6e5 15131 fp->set_type (die_type (die, cu));
c906108c 15132
d6a843b5 15133 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15134
c906108c 15135 /* Get bit size of field (zero if none). */
e142c38c 15136 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 15137 if (attr != nullptr)
c906108c 15138 {
529908cb 15139 FIELD_BITSIZE (*fp) = attr->constant_value (0);
c906108c
SS
15140 }
15141 else
15142 {
15143 FIELD_BITSIZE (*fp) = 0;
15144 }
15145
15146 /* Get bit offset of field. */
7d79de9a 15147 handle_data_member_location (die, cu, fp);
e142c38c 15148 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7c184d33 15149 if (attr != nullptr && attr->form_is_constant ())
c906108c 15150 {
d5a22e77 15151 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
15152 {
15153 /* For big endian bits, the DW_AT_bit_offset gives the
dda83cd7
SM
15154 additional bit offset from the MSB of the containing
15155 anonymous object to the MSB of the field. We don't
15156 have to do anything special since we don't need to
15157 know the size of the anonymous object. */
529908cb 15158 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
7c184d33 15159 + attr->constant_value (0)));
c906108c
SS
15160 }
15161 else
15162 {
15163 /* For little endian bits, compute the bit offset to the
dda83cd7
SM
15164 MSB of the anonymous object, subtract off the number of
15165 bits from the MSB of the field to the MSB of the
15166 object, and then subtract off the number of bits of
15167 the field itself. The result is the bit offset of
15168 the LSB of the field. */
c906108c 15169 int anonymous_size;
7c184d33 15170 int bit_offset = attr->constant_value (0);
c906108c 15171
e142c38c 15172 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7c184d33 15173 if (attr != nullptr && attr->form_is_constant ())
c906108c
SS
15174 {
15175 /* The size of the anonymous object containing
15176 the bit field is explicit, so use the
15177 indicated size (in bytes). */
7c184d33 15178 anonymous_size = attr->constant_value (0);
c906108c
SS
15179 }
15180 else
15181 {
15182 /* The size of the anonymous object containing
15183 the bit field must be inferred from the type
15184 attribute of the data member containing the
15185 bit field. */
5d14b6e5 15186 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 15187 }
f41f5e61
PA
15188 SET_FIELD_BITPOS (*fp,
15189 (FIELD_BITPOS (*fp)
15190 + anonymous_size * bits_per_byte
15191 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15192 }
15193 }
da5b30da
AA
15194 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15195 if (attr != NULL)
15196 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 15197 + attr->constant_value (0)));
c906108c
SS
15198
15199 /* Get name of field. */
39cbfefa
DJ
15200 fieldname = dwarf2_name (die, cu);
15201 if (fieldname == NULL)
15202 fieldname = "";
d8151005
DJ
15203
15204 /* The name is already allocated along with this objfile, so we don't
15205 need to duplicate it for the type. */
15206 fp->name = fieldname;
c906108c
SS
15207
15208 /* Change accessibility for artificial fields (e.g. virtual table
dda83cd7 15209 pointer or virtual base class pointer) to private. */
e142c38c 15210 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15211 {
d48cc9dd 15212 FIELD_ARTIFICIAL (*fp) = 1;
c906108c 15213 new_field->accessibility = DW_ACCESS_private;
264fc0e2 15214 fip->non_public_fields = true;
c906108c
SS
15215 }
15216 }
a9a9bd0f 15217 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15218 {
a9a9bd0f
DC
15219 /* C++ static member. */
15220
15221 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15222 is a declaration, but all versions of G++ as of this writing
15223 (so through at least 3.2.1) incorrectly generate
15224 DW_TAG_variable tags. */
6e70227d 15225
ff355380 15226 const char *physname;
c906108c 15227
a9a9bd0f 15228 /* Get name of field. */
39cbfefa
DJ
15229 fieldname = dwarf2_name (die, cu);
15230 if (fieldname == NULL)
c906108c
SS
15231 return;
15232
254e6b9e 15233 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15234 if (attr
15235 /* Only create a symbol if this is an external value.
15236 new_symbol checks this and puts the value in the global symbol
15237 table, which we want. If it is not external, new_symbol
15238 will try to put the value in cu->list_in_scope which is wrong. */
15239 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15240 {
15241 /* A static const member, not much different than an enum as far as
15242 we're concerned, except that we can support more types. */
15243 new_symbol (die, NULL, cu);
15244 }
15245
2df3850c 15246 /* Get physical name. */
ff355380 15247 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15248
d8151005
DJ
15249 /* The name is already allocated along with this objfile, so we don't
15250 need to duplicate it for the type. */
15251 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 15252 fp->set_type (die_type (die, cu));
d8151005 15253 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15254 }
15255 else if (die->tag == DW_TAG_inheritance)
15256 {
74ac6d43 15257 /* C++ base class field. */
7d79de9a 15258 handle_data_member_location (die, cu, fp);
c906108c 15259 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
15260 fp->set_type (die_type (die, cu));
15261 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 15262 }
2ddeaf8a
TT
15263 else
15264 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15265}
15266
883fd55a
KS
15267/* Can the type given by DIE define another type? */
15268
15269static bool
15270type_can_define_types (const struct die_info *die)
15271{
15272 switch (die->tag)
15273 {
15274 case DW_TAG_typedef:
15275 case DW_TAG_class_type:
15276 case DW_TAG_structure_type:
15277 case DW_TAG_union_type:
15278 case DW_TAG_enumeration_type:
15279 return true;
15280
15281 default:
15282 return false;
15283 }
15284}
15285
15286/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15287
15288static void
883fd55a
KS
15289dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15290 struct dwarf2_cu *cu)
6e70227d 15291{
be2daae6
TT
15292 struct decl_field fp;
15293 memset (&fp, 0, sizeof (fp));
98751a41 15294
883fd55a 15295 gdb_assert (type_can_define_types (die));
98751a41 15296
883fd55a 15297 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15298 fp.name = dwarf2_name (die, cu);
15299 fp.type = read_type_die (die, cu);
98751a41 15300
c191a687 15301 /* Save accessibility. */
bf23a268 15302 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
c191a687
KS
15303 switch (accessibility)
15304 {
15305 case DW_ACCESS_public:
15306 /* The assumed value if neither private nor protected. */
15307 break;
15308 case DW_ACCESS_private:
be2daae6 15309 fp.is_private = 1;
c191a687
KS
15310 break;
15311 case DW_ACCESS_protected:
be2daae6 15312 fp.is_protected = 1;
c191a687 15313 break;
c191a687
KS
15314 }
15315
883fd55a 15316 if (die->tag == DW_TAG_typedef)
be2daae6 15317 fip->typedef_field_list.push_back (fp);
883fd55a 15318 else
be2daae6 15319 fip->nested_types_list.push_back (fp);
98751a41
JK
15320}
15321
9c6a1327
TT
15322/* A convenience typedef that's used when finding the discriminant
15323 field for a variant part. */
1b95cdb7
SM
15324typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15325 offset_map_type;
9c6a1327
TT
15326
15327/* Compute the discriminant range for a given variant. OBSTACK is
15328 where the results will be stored. VARIANT is the variant to
15329 process. IS_UNSIGNED indicates whether the discriminant is signed
15330 or unsigned. */
15331
15332static const gdb::array_view<discriminant_range>
15333convert_variant_range (struct obstack *obstack, const variant_field &variant,
15334 bool is_unsigned)
15335{
15336 std::vector<discriminant_range> ranges;
15337
15338 if (variant.default_branch)
15339 return {};
15340
15341 if (variant.discr_list_data == nullptr)
15342 {
15343 discriminant_range r
15344 = {variant.discriminant_value, variant.discriminant_value};
15345 ranges.push_back (r);
15346 }
15347 else
15348 {
15349 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15350 variant.discr_list_data->size);
15351 while (!data.empty ())
15352 {
15353 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15354 {
15355 complaint (_("invalid discriminant marker: %d"), data[0]);
15356 break;
15357 }
15358 bool is_range = data[0] == DW_DSC_range;
15359 data = data.slice (1);
15360
15361 ULONGEST low, high;
15362 unsigned int bytes_read;
15363
15364 if (data.empty ())
15365 {
15366 complaint (_("DW_AT_discr_list missing low value"));
15367 break;
15368 }
15369 if (is_unsigned)
15370 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15371 else
15372 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15373 &bytes_read);
15374 data = data.slice (bytes_read);
15375
15376 if (is_range)
15377 {
15378 if (data.empty ())
15379 {
15380 complaint (_("DW_AT_discr_list missing high value"));
15381 break;
15382 }
15383 if (is_unsigned)
15384 high = read_unsigned_leb128 (nullptr, data.data (),
15385 &bytes_read);
15386 else
15387 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15388 &bytes_read);
15389 data = data.slice (bytes_read);
15390 }
15391 else
15392 high = low;
15393
15394 ranges.push_back ({ low, high });
15395 }
15396 }
15397
15398 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15399 ranges.size ());
15400 std::copy (ranges.begin (), ranges.end (), result);
15401 return gdb::array_view<discriminant_range> (result, ranges.size ());
15402}
15403
15404static const gdb::array_view<variant_part> create_variant_parts
15405 (struct obstack *obstack,
15406 const offset_map_type &offset_map,
15407 struct field_info *fi,
15408 const std::vector<variant_part_builder> &variant_parts);
15409
15410/* Fill in a "struct variant" for a given variant field. RESULT is
15411 the variant to fill in. OBSTACK is where any needed allocations
15412 will be done. OFFSET_MAP holds the mapping from section offsets to
15413 fields for the type. FI describes the fields of the type we're
15414 processing. FIELD is the variant field we're converting. */
15415
15416static void
15417create_one_variant (variant &result, struct obstack *obstack,
15418 const offset_map_type &offset_map,
15419 struct field_info *fi, const variant_field &field)
15420{
15421 result.discriminants = convert_variant_range (obstack, field, false);
15422 result.first_field = field.first_field + fi->baseclasses.size ();
15423 result.last_field = field.last_field + fi->baseclasses.size ();
15424 result.parts = create_variant_parts (obstack, offset_map, fi,
15425 field.variant_parts);
15426}
15427
15428/* Fill in a "struct variant_part" for a given variant part. RESULT
15429 is the variant part to fill in. OBSTACK is where any needed
15430 allocations will be done. OFFSET_MAP holds the mapping from
15431 section offsets to fields for the type. FI describes the fields of
15432 the type we're processing. BUILDER is the variant part to be
15433 converted. */
15434
15435static void
15436create_one_variant_part (variant_part &result,
15437 struct obstack *obstack,
15438 const offset_map_type &offset_map,
15439 struct field_info *fi,
15440 const variant_part_builder &builder)
15441{
15442 auto iter = offset_map.find (builder.discriminant_offset);
15443 if (iter == offset_map.end ())
15444 {
15445 result.discriminant_index = -1;
15446 /* Doesn't matter. */
15447 result.is_unsigned = false;
15448 }
15449 else
15450 {
15451 result.discriminant_index = iter->second;
15452 result.is_unsigned
c6d940a9 15453 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
9c6a1327
TT
15454 }
15455
15456 size_t n = builder.variants.size ();
15457 variant *output = new (obstack) variant[n];
15458 for (size_t i = 0; i < n; ++i)
15459 create_one_variant (output[i], obstack, offset_map, fi,
15460 builder.variants[i]);
15461
15462 result.variants = gdb::array_view<variant> (output, n);
15463}
15464
15465/* Create a vector of variant parts that can be attached to a type.
15466 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15467 holds the mapping from section offsets to fields for the type. FI
15468 describes the fields of the type we're processing. VARIANT_PARTS
15469 is the vector to convert. */
15470
15471static const gdb::array_view<variant_part>
15472create_variant_parts (struct obstack *obstack,
15473 const offset_map_type &offset_map,
15474 struct field_info *fi,
15475 const std::vector<variant_part_builder> &variant_parts)
15476{
15477 if (variant_parts.empty ())
15478 return {};
15479
15480 size_t n = variant_parts.size ();
15481 variant_part *result = new (obstack) variant_part[n];
15482 for (size_t i = 0; i < n; ++i)
15483 create_one_variant_part (result[i], obstack, offset_map, fi,
15484 variant_parts[i]);
15485
15486 return gdb::array_view<variant_part> (result, n);
15487}
15488
15489/* Compute the variant part vector for FIP, attaching it to TYPE when
15490 done. */
15491
15492static void
15493add_variant_property (struct field_info *fip, struct type *type,
15494 struct dwarf2_cu *cu)
15495{
15496 /* Map section offsets of fields to their field index. Note the
15497 field index here does not take the number of baseclasses into
15498 account. */
15499 offset_map_type offset_map;
15500 for (int i = 0; i < fip->fields.size (); ++i)
15501 offset_map[fip->fields[i].offset] = i;
15502
5e22e966 15503 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
15504 gdb::array_view<variant_part> parts
15505 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15506 fip->variant_parts);
15507
15508 struct dynamic_prop prop;
8c2e4e06
SM
15509 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15510 obstack_copy (&objfile->objfile_obstack, &parts,
15511 sizeof (parts)));
9c6a1327 15512
5c54719c 15513 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
15514}
15515
c906108c
SS
15516/* Create the vector of fields, and attach it to the type. */
15517
15518static void
fba45db2 15519dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15520 struct dwarf2_cu *cu)
c906108c 15521{
317f7127 15522 int nfields = fip->nfields ();
c906108c
SS
15523
15524 /* Record the field count, allocate space for the array of fields,
15525 and create blank accessibility bitfields if necessary. */
5e33d5f4 15526 type->set_num_fields (nfields);
3cabb6b0
SM
15527 type->set_fields
15528 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 15529
b4ba55a1 15530 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15531 {
15532 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15533
15534 TYPE_FIELD_PRIVATE_BITS (type) =
15535 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15536 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15537
15538 TYPE_FIELD_PROTECTED_BITS (type) =
15539 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15540 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15541
774b6a14
TT
15542 TYPE_FIELD_IGNORE_BITS (type) =
15543 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15544 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15545 }
15546
15547 /* If the type has baseclasses, allocate and clear a bit vector for
15548 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15549 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15550 {
be2daae6 15551 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15552 unsigned char *pointer;
c906108c
SS
15553
15554 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15555 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15556 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15557 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15558 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15559 }
15560
9c6a1327
TT
15561 if (!fip->variant_parts.empty ())
15562 add_variant_property (fip, type, cu);
2ddeaf8a 15563
be2daae6
TT
15564 /* Copy the saved-up fields into the field vector. */
15565 for (int i = 0; i < nfields; ++i)
c906108c 15566 {
be2daae6
TT
15567 struct nextfield &field
15568 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15569 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15570
ceacbf6e 15571 type->field (i) = field.field;
be2daae6 15572 switch (field.accessibility)
c906108c 15573 {
c5aa993b 15574 case DW_ACCESS_private:
b4ba55a1 15575 if (cu->language != language_ada)
be2daae6 15576 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15577 break;
c906108c 15578
c5aa993b 15579 case DW_ACCESS_protected:
b4ba55a1 15580 if (cu->language != language_ada)
be2daae6 15581 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15582 break;
c906108c 15583
c5aa993b
JM
15584 case DW_ACCESS_public:
15585 break;
c906108c 15586
c5aa993b
JM
15587 default:
15588 /* Unknown accessibility. Complain and treat it as public. */
15589 {
b98664d3 15590 complaint (_("unsupported accessibility %d"),
be2daae6 15591 field.accessibility);
c5aa993b
JM
15592 }
15593 break;
c906108c 15594 }
be2daae6 15595 if (i < fip->baseclasses.size ())
c906108c 15596 {
be2daae6 15597 switch (field.virtuality)
c906108c 15598 {
c5aa993b
JM
15599 case DW_VIRTUALITY_virtual:
15600 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15601 if (cu->language == language_ada)
a73c6dcd 15602 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15603 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15604 break;
c906108c
SS
15605 }
15606 }
c906108c
SS
15607 }
15608}
15609
7d27a96d
TT
15610/* Return true if this member function is a constructor, false
15611 otherwise. */
15612
15613static int
15614dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15615{
15616 const char *fieldname;
fe978cb0 15617 const char *type_name;
7d27a96d
TT
15618 int len;
15619
15620 if (die->parent == NULL)
15621 return 0;
15622
15623 if (die->parent->tag != DW_TAG_structure_type
15624 && die->parent->tag != DW_TAG_union_type
15625 && die->parent->tag != DW_TAG_class_type)
15626 return 0;
15627
15628 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15629 type_name = dwarf2_name (die->parent, cu);
15630 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15631 return 0;
15632
15633 len = strlen (fieldname);
fe978cb0
PA
15634 return (strncmp (fieldname, type_name, len) == 0
15635 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15636}
15637
c906108c
SS
15638/* Add a member function to the proper fieldlist. */
15639
15640static void
107d2387 15641dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15642 struct type *type, struct dwarf2_cu *cu)
c906108c 15643{
5e22e966 15644 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15645 struct attribute *attr;
c906108c 15646 int i;
be2daae6 15647 struct fnfieldlist *flp = nullptr;
c906108c 15648 struct fn_field *fnp;
15d034d0 15649 const char *fieldname;
f792889a 15650 struct type *this_type;
c906108c 15651
b4ba55a1 15652 if (cu->language == language_ada)
a73c6dcd 15653 error (_("unexpected member function in Ada type"));
b4ba55a1 15654
2df3850c 15655 /* Get name of member function. */
39cbfefa
DJ
15656 fieldname = dwarf2_name (die, cu);
15657 if (fieldname == NULL)
2df3850c 15658 return;
c906108c 15659
c906108c 15660 /* Look up member function name in fieldlist. */
be2daae6 15661 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15662 {
27bfe10e 15663 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15664 {
15665 flp = &fip->fnfieldlists[i];
15666 break;
15667 }
c906108c
SS
15668 }
15669
be2daae6
TT
15670 /* Create a new fnfieldlist if necessary. */
15671 if (flp == nullptr)
c906108c 15672 {
be2daae6
TT
15673 fip->fnfieldlists.emplace_back ();
15674 flp = &fip->fnfieldlists.back ();
c906108c 15675 flp->name = fieldname;
be2daae6 15676 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15677 }
15678
be2daae6
TT
15679 /* Create a new member function field and add it to the vector of
15680 fnfieldlists. */
15681 flp->fnfields.emplace_back ();
15682 fnp = &flp->fnfields.back ();
3da10d80
KS
15683
15684 /* Delay processing of the physname until later. */
9c37b5ae 15685 if (cu->language == language_cplus)
be2daae6
TT
15686 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15687 die, cu);
3da10d80
KS
15688 else
15689 {
1d06ead6 15690 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15691 fnp->physname = physname ? physname : "";
15692 }
15693
c906108c 15694 fnp->type = alloc_type (objfile);
f792889a 15695 this_type = read_type_die (die, cu);
78134374 15696 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15697 {
1f704f76 15698 int nparams = this_type->num_fields ();
c906108c 15699
f792889a 15700 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15701 of the method itself (TYPE_CODE_METHOD). */
15702 smash_to_method_type (fnp->type, type,
f792889a 15703 TYPE_TARGET_TYPE (this_type),
80fc5e77 15704 this_type->fields (),
1f704f76 15705 this_type->num_fields (),
a409645d 15706 this_type->has_varargs ());
c906108c
SS
15707
15708 /* Handle static member functions.
dda83cd7
SM
15709 Dwarf2 has no clean way to discern C++ static and non-static
15710 member functions. G++ helps GDB by marking the first
15711 parameter for non-static member functions (which is the this
15712 pointer) as artificial. We obtain this information from
15713 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15714 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15715 fnp->voffset = VOFFSET_STATIC;
15716 }
15717 else
b98664d3 15718 complaint (_("member function type missing for '%s'"),
3da10d80 15719 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15720
15721 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15722 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15723 fnp->fcontext = die_containing_type (die, cu);
c906108c 15724
3e43a32a
MS
15725 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15726 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15727
15728 /* Get accessibility. */
bf23a268 15729 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
60d5a603 15730 switch (accessibility)
c906108c 15731 {
60d5a603
JK
15732 case DW_ACCESS_private:
15733 fnp->is_private = 1;
15734 break;
15735 case DW_ACCESS_protected:
15736 fnp->is_protected = 1;
15737 break;
c906108c
SS
15738 }
15739
b02dede2 15740 /* Check for artificial methods. */
e142c38c 15741 attr = dwarf2_attr (die, DW_AT_artificial, cu);
c45bc3f8 15742 if (attr && attr->as_boolean ())
b02dede2
DJ
15743 fnp->is_artificial = 1;
15744
e35000a7
TBA
15745 /* Check for defaulted methods. */
15746 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
e8e5c158
TT
15747 if (attr != nullptr)
15748 fnp->defaulted = attr->defaulted ();
e35000a7
TBA
15749
15750 /* Check for deleted methods. */
15751 attr = dwarf2_attr (die, DW_AT_deleted, cu);
c45bc3f8 15752 if (attr != nullptr && attr->as_boolean ())
e35000a7
TBA
15753 fnp->is_deleted = 1;
15754
7d27a96d
TT
15755 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15756
0d564a31 15757 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15758 function. For older versions of GCC, this is an offset in the
15759 appropriate virtual table, as specified by DW_AT_containing_type.
15760 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15761 to the object address. */
15762
e142c38c 15763 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15764 if (attr != nullptr)
8e19ed76 15765 {
9d2246fc 15766 if (attr->form_is_block () && attr->as_block ()->size > 0)
dda83cd7 15767 {
9d2246fc
TT
15768 struct dwarf_block *block = attr->as_block ();
15769
15770 if (block->data[0] == DW_OP_constu)
aec5aa8b
TT
15771 {
15772 /* Old-style GCC. */
9d2246fc 15773 fnp->voffset = decode_locdesc (block, cu) + 2;
aec5aa8b 15774 }
9d2246fc
TT
15775 else if (block->data[0] == DW_OP_deref
15776 || (block->size > 1
15777 && block->data[0] == DW_OP_deref_size
15778 && block->data[1] == cu->header.addr_size))
aec5aa8b 15779 {
9d2246fc 15780 fnp->voffset = decode_locdesc (block, cu);
aec5aa8b
TT
15781 if ((fnp->voffset % cu->header.addr_size) != 0)
15782 dwarf2_complex_location_expr_complaint ();
15783 else
15784 fnp->voffset /= cu->header.addr_size;
15785 fnp->voffset += 2;
15786 }
15787 else
15788 dwarf2_complex_location_expr_complaint ();
15789
15790 if (!fnp->fcontext)
7e993ebf
KS
15791 {
15792 /* If there is no `this' field and no DW_AT_containing_type,
15793 we cannot actually find a base class context for the
15794 vtable! */
1f704f76 15795 if (this_type->num_fields () == 0
7e993ebf
KS
15796 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15797 {
b98664d3 15798 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15799 "function \"%s\" (offset %s)"),
15800 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15801 }
15802 else
15803 {
15804 fnp->fcontext
940da03e 15805 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15806 }
15807 }
aec5aa8b 15808 }
cd6c91b4 15809 else if (attr->form_is_section_offset ())
dda83cd7 15810 {
4d3c2250 15811 dwarf2_complex_location_expr_complaint ();
dda83cd7 15812 }
8e19ed76 15813 else
dda83cd7 15814 {
4d3c2250
KB
15815 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15816 fieldname);
dda83cd7 15817 }
0d564a31 15818 }
d48cc9dd
DJ
15819 else
15820 {
15821 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
23dca5c3 15822 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
d48cc9dd
DJ
15823 {
15824 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15825 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15826 "but the vtable offset is not specified"),
9d8780f0 15827 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15828 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15829 TYPE_CPLUS_DYNAMIC (type) = 1;
15830 }
15831 }
c906108c
SS
15832}
15833
15834/* Create the vector of member function fields, and attach it to the type. */
15835
15836static void
fba45db2 15837dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15838 struct dwarf2_cu *cu)
c906108c 15839{
b4ba55a1 15840 if (cu->language == language_ada)
a73c6dcd 15841 error (_("unexpected member functions in Ada type"));
b4ba55a1 15842
c906108c
SS
15843 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15844 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15845 TYPE_ALLOC (type,
15846 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15847
be2daae6 15848 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15849 {
be2daae6 15850 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15851 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15852
be2daae6
TT
15853 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15854 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15855 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15856 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15857
15858 for (int k = 0; k < nf.fnfields.size (); ++k)
15859 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15860 }
15861
be2daae6 15862 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15863}
15864
1168df01
JB
15865/* Returns non-zero if NAME is the name of a vtable member in CU's
15866 language, zero otherwise. */
15867static int
15868is_vtable_name (const char *name, struct dwarf2_cu *cu)
15869{
15870 static const char vptr[] = "_vptr";
15871
9c37b5ae
TT
15872 /* Look for the C++ form of the vtable. */
15873 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15874 return 1;
15875
15876 return 0;
15877}
15878
c0dd20ea 15879/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15880 functions, with the ABI-specified layout. If TYPE describes
15881 such a structure, smash it into a member function type.
61049d3b
DJ
15882
15883 GCC shouldn't do this; it should just output pointer to member DIEs.
15884 This is GCC PR debug/28767. */
c0dd20ea 15885
0b92b5bb
TT
15886static void
15887quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15888{
09e2d7c7 15889 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15890
15891 /* Check for a structure with no name and two children. */
1f704f76 15892 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15893 return;
c0dd20ea
DJ
15894
15895 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15896 if (TYPE_FIELD_NAME (type, 0) == NULL
15897 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15898 || TYPE_FIELD_NAME (type, 1) == NULL
15899 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15900 return;
c0dd20ea
DJ
15901
15902 /* Find the type of the method. */
940da03e 15903 pfn_type = type->field (0).type ();
c0dd20ea 15904 if (pfn_type == NULL
78134374
SM
15905 || pfn_type->code () != TYPE_CODE_PTR
15906 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15907 return;
c0dd20ea
DJ
15908
15909 /* Look for the "this" argument. */
15910 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15911 if (pfn_type->num_fields () == 0
940da03e
SM
15912 /* || pfn_type->field (0).type () == NULL */
15913 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15914 return;
c0dd20ea 15915
940da03e 15916 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15917 new_type = alloc_type (objfile);
09e2d7c7 15918 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15919 pfn_type->fields (), pfn_type->num_fields (),
a409645d 15920 pfn_type->has_varargs ());
0b92b5bb 15921 smash_to_methodptr_type (type, new_type);
c0dd20ea 15922}
1168df01 15923
e26624c6
TT
15924/* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15925 requires rewriting, then copy it and return the updated copy.
15926 Otherwise return nullptr. */
15927
15928static struct type *
15929rewrite_array_type (struct type *type)
15930{
15931 if (type->code () != TYPE_CODE_ARRAY)
15932 return nullptr;
15933
15934 struct type *index_type = type->index_type ();
15935 range_bounds *current_bounds = index_type->bounds ();
15936
15937 /* Handle multi-dimensional arrays. */
15938 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15939 if (new_target == nullptr)
15940 {
15941 /* Maybe we don't need to rewrite this array. */
15942 if (current_bounds->low.kind () == PROP_CONST
15943 && current_bounds->high.kind () == PROP_CONST)
15944 return nullptr;
15945 }
15946
15947 /* Either the target type was rewritten, or the bounds have to be
15948 updated. Either way we want to copy the type and update
15949 everything. */
15950 struct type *copy = copy_type (type);
15951 int nfields = copy->num_fields ();
15952 field *new_fields
15953 = ((struct field *) TYPE_ZALLOC (copy,
15954 nfields * sizeof (struct field)));
15955 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15956 copy->set_fields (new_fields);
15957 if (new_target != nullptr)
15958 TYPE_TARGET_TYPE (copy) = new_target;
15959
15960 struct type *index_copy = copy_type (index_type);
15961 range_bounds *bounds
15962 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15963 sizeof (range_bounds));
15964 *bounds = *current_bounds;
15965 bounds->low.set_const_val (1);
15966 bounds->high.set_const_val (0);
15967 index_copy->set_bounds (bounds);
15968 copy->set_index_type (index_copy);
15969
15970 return copy;
15971}
15972
57567375
TT
15973/* While some versions of GCC will generate complicated DWARF for an
15974 array (see quirk_ada_thick_pointer), more recent versions were
15975 modified to emit an explicit thick pointer structure. However, in
15976 this case, the array still has DWARF expressions for its ranges,
15977 and these must be ignored. */
15978
15979static void
15980quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15981 struct type *type)
15982{
15983 gdb_assert (cu->language == language_ada);
15984
15985 /* Check for a structure with two children. */
15986 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15987 return;
15988
15989 /* Check for P_ARRAY and P_BOUNDS members. */
15990 if (TYPE_FIELD_NAME (type, 0) == NULL
15991 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15992 || TYPE_FIELD_NAME (type, 1) == NULL
15993 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15994 return;
15995
15996 /* Make sure we're looking at a pointer to an array. */
15997 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15998 return;
57567375 15999
e26624c6
TT
16000 /* The Ada code already knows how to handle these types, so all that
16001 we need to do is turn the bounds into static bounds. However, we
16002 don't want to rewrite existing array or index types in-place,
16003 because those may be referenced in other contexts where this
16004 rewriting is undesirable. */
16005 struct type *new_ary_type
16006 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
16007 if (new_ary_type != nullptr)
16008 type->field (0).set_type (lookup_pointer_type (new_ary_type));
57567375
TT
16009}
16010
2b4424c3
TT
16011/* If the DIE has a DW_AT_alignment attribute, return its value, doing
16012 appropriate error checking and issuing complaints if there is a
16013 problem. */
16014
16015static ULONGEST
16016get_alignment (struct dwarf2_cu *cu, struct die_info *die)
16017{
16018 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
16019
16020 if (attr == nullptr)
16021 return 0;
16022
cd6c91b4 16023 if (!attr->form_is_constant ())
2b4424c3 16024 {
b98664d3 16025 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
16026 " - DIE at %s [in module %s]"),
16027 sect_offset_str (die->sect_off),
5e22e966 16028 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
16029 return 0;
16030 }
16031
529908cb
TT
16032 LONGEST val = attr->constant_value (0);
16033 if (val < 0)
2b4424c3 16034 {
529908cb
TT
16035 complaint (_("DW_AT_alignment value must not be negative"
16036 " - DIE at %s [in module %s]"),
16037 sect_offset_str (die->sect_off),
16038 objfile_name (cu->per_objfile->objfile));
16039 return 0;
2b4424c3 16040 }
529908cb 16041 ULONGEST align = val;
2b4424c3
TT
16042
16043 if (align == 0)
16044 {
b98664d3 16045 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
16046 " - DIE at %s [in module %s]"),
16047 sect_offset_str (die->sect_off),
5e22e966 16048 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
16049 return 0;
16050 }
16051 if ((align & (align - 1)) != 0)
16052 {
b98664d3 16053 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
16054 " - DIE at %s [in module %s]"),
16055 sect_offset_str (die->sect_off),
5e22e966 16056 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
16057 return 0;
16058 }
16059
16060 return align;
16061}
16062
16063/* If the DIE has a DW_AT_alignment attribute, use its value to set
16064 the alignment for TYPE. */
16065
16066static void
16067maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
16068 struct type *type)
16069{
16070 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 16071 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
16072 " - DIE at %s [in module %s]"),
16073 sect_offset_str (die->sect_off),
5e22e966 16074 objfile_name (cu->per_objfile->objfile));
2b4424c3 16075}
685b1105 16076
e35000a7
TBA
16077/* Check if the given VALUE is a valid enum dwarf_calling_convention
16078 constant for a type, according to DWARF5 spec, Table 5.5. */
16079
16080static bool
16081is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
16082{
16083 switch (value)
16084 {
16085 case DW_CC_normal:
16086 case DW_CC_pass_by_reference:
16087 case DW_CC_pass_by_value:
16088 return true;
16089
16090 default:
16091 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 16092 "(%s) for a type"), pulongest (value));
e35000a7
TBA
16093 return false;
16094 }
16095}
16096
d0922fcf
TBA
16097/* Check if the given VALUE is a valid enum dwarf_calling_convention
16098 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
16099 also according to GNU-specific values (see include/dwarf2.h). */
16100
16101static bool
16102is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
16103{
16104 switch (value)
16105 {
16106 case DW_CC_normal:
16107 case DW_CC_program:
16108 case DW_CC_nocall:
16109 return true;
16110
16111 case DW_CC_GNU_renesas_sh:
16112 case DW_CC_GNU_borland_fastcall_i386:
16113 case DW_CC_GDB_IBM_OpenCL:
16114 return true;
16115
16116 default:
16117 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 16118 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
16119 return false;
16120 }
16121}
16122
c906108c 16123/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
16124 (definition) to create a type for the structure or union. Fill in
16125 the type's name and general properties; the members will not be
83655187
DE
16126 processed until process_structure_scope. A symbol table entry for
16127 the type will also not be done until process_structure_scope (assuming
16128 the type has a name).
c906108c 16129
c767944b
DJ
16130 NOTE: we need to call these functions regardless of whether or not the
16131 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 16132 structure or union. This gets the type entered into our set of
83655187 16133 user defined types. */
c906108c 16134
f792889a 16135static struct type *
134d01f1 16136read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16137{
5e22e966 16138 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
16139 struct type *type;
16140 struct attribute *attr;
15d034d0 16141 const char *name;
c906108c 16142
348e048f
DE
16143 /* If the definition of this type lives in .debug_types, read that type.
16144 Don't follow DW_AT_specification though, that will take us back up
16145 the chain and we want to go down. */
052c8bb8 16146 attr = die->attr (DW_AT_signature);
435d3d88 16147 if (attr != nullptr)
348e048f 16148 {
ac9ec31b 16149 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16150
ac9ec31b 16151 /* The type's CU may not be the same as CU.
02142a6c 16152 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16153 return set_die_type (die, type, cu);
16154 }
16155
c0dd20ea 16156 type = alloc_type (objfile);
c906108c 16157 INIT_CPLUS_SPECIFIC (type);
93311388 16158
39cbfefa
DJ
16159 name = dwarf2_name (die, cu);
16160 if (name != NULL)
c906108c 16161 {
987504bb 16162 if (cu->language == language_cplus
c44af4eb
TT
16163 || cu->language == language_d
16164 || cu->language == language_rust)
63d06c5c 16165 {
15d034d0 16166 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
16167
16168 /* dwarf2_full_name might have already finished building the DIE's
16169 type. If so, there is no need to continue. */
16170 if (get_die_type (die, cu) != NULL)
16171 return get_die_type (die, cu);
16172
d0e39ea2 16173 type->set_name (full_name);
63d06c5c
DC
16174 }
16175 else
16176 {
d8151005
DJ
16177 /* The name is already allocated along with this objfile, so
16178 we don't need to duplicate it for the type. */
d0e39ea2 16179 type->set_name (name);
63d06c5c 16180 }
c906108c
SS
16181 }
16182
16183 if (die->tag == DW_TAG_structure_type)
16184 {
67607e24 16185 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16186 }
16187 else if (die->tag == DW_TAG_union_type)
16188 {
67607e24 16189 type->set_code (TYPE_CODE_UNION);
c906108c
SS
16190 }
16191 else
16192 {
67607e24 16193 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16194 }
16195
0cc2414c
TT
16196 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16197 TYPE_DECLARED_CLASS (type) = 1;
16198
e35000a7
TBA
16199 /* Store the calling convention in the type if it's available in
16200 the die. Otherwise the calling convention remains set to
16201 the default value DW_CC_normal. */
16202 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16203 if (attr != nullptr
529908cb 16204 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
e35000a7
TBA
16205 {
16206 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16207 TYPE_CPLUS_CALLING_CONVENTION (type)
529908cb 16208 = (enum dwarf_calling_convention) (attr->constant_value (0));
e35000a7
TBA
16209 }
16210
e142c38c 16211 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16212 if (attr != nullptr)
c906108c 16213 {
cd6c91b4 16214 if (attr->form_is_constant ())
dda83cd7 16215 TYPE_LENGTH (type) = attr->constant_value (0);
155bfbd3
JB
16216 else
16217 {
f8e89861 16218 struct dynamic_prop prop;
293e7e51 16219 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 16220 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
dda83cd7 16221 TYPE_LENGTH (type) = 0;
155bfbd3 16222 }
c906108c
SS
16223 }
16224 else
16225 {
16226 TYPE_LENGTH (type) = 0;
16227 }
16228
2b4424c3
TT
16229 maybe_set_alignment (cu, die, type);
16230
5230b05a 16231 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 16232 {
5230b05a
WT
16233 /* ICC<14 does not output the required DW_AT_declaration on
16234 incomplete types, but gives them a size of zero. */
b4b73759 16235 type->set_is_stub (true);
685b1105
JK
16236 }
16237 else
9baccff6 16238 type->set_stub_is_supported (true);
685b1105 16239
dc718098 16240 if (die_is_declaration (die, cu))
b4b73759 16241 type->set_is_stub (true);
a6c727b2
DJ
16242 else if (attr == NULL && die->child == NULL
16243 && producer_is_realview (cu->producer))
16244 /* RealView does not output the required DW_AT_declaration
16245 on incomplete types. */
b4b73759 16246 type->set_is_stub (true);
dc718098 16247
c906108c
SS
16248 /* We need to add the type field to the die immediately so we don't
16249 infinitely recurse when dealing with pointers to the structure
0963b4bd 16250 type within the structure itself. */
1c379e20 16251 set_die_type (die, type, cu);
c906108c 16252
7e314c57
JK
16253 /* set_die_type should be already done. */
16254 set_descriptive_type (type, die, cu);
16255
c767944b
DJ
16256 return type;
16257}
16258
9c6a1327
TT
16259static void handle_struct_member_die
16260 (struct die_info *child_die,
16261 struct type *type,
16262 struct field_info *fi,
16263 std::vector<struct symbol *> *template_args,
16264 struct dwarf2_cu *cu);
16265
16266/* A helper for handle_struct_member_die that handles
16267 DW_TAG_variant_part. */
16268
16269static void
16270handle_variant_part (struct die_info *die, struct type *type,
16271 struct field_info *fi,
16272 std::vector<struct symbol *> *template_args,
16273 struct dwarf2_cu *cu)
16274{
16275 variant_part_builder *new_part;
16276 if (fi->current_variant_part == nullptr)
16277 {
16278 fi->variant_parts.emplace_back ();
16279 new_part = &fi->variant_parts.back ();
16280 }
16281 else if (!fi->current_variant_part->processing_variant)
16282 {
16283 complaint (_("nested DW_TAG_variant_part seen "
16284 "- DIE at %s [in module %s]"),
16285 sect_offset_str (die->sect_off),
5e22e966 16286 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16287 return;
16288 }
16289 else
16290 {
16291 variant_field &current = fi->current_variant_part->variants.back ();
16292 current.variant_parts.emplace_back ();
16293 new_part = &current.variant_parts.back ();
16294 }
16295
16296 /* When we recurse, we want callees to add to this new variant
16297 part. */
16298 scoped_restore save_current_variant_part
16299 = make_scoped_restore (&fi->current_variant_part, new_part);
16300
16301 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16302 if (discr == NULL)
16303 {
16304 /* It's a univariant form, an extension we support. */
16305 }
16306 else if (discr->form_is_ref ())
16307 {
16308 struct dwarf2_cu *target_cu = cu;
16309 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16310
16311 new_part->discriminant_offset = target_die->sect_off;
16312 }
16313 else
16314 {
16315 complaint (_("DW_AT_discr does not have DIE reference form"
16316 " - DIE at %s [in module %s]"),
16317 sect_offset_str (die->sect_off),
5e22e966 16318 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16319 }
16320
16321 for (die_info *child_die = die->child;
16322 child_die != NULL;
16323 child_die = child_die->sibling)
16324 handle_struct_member_die (child_die, type, fi, template_args, cu);
16325}
16326
16327/* A helper for handle_struct_member_die that handles
16328 DW_TAG_variant. */
16329
16330static void
16331handle_variant (struct die_info *die, struct type *type,
16332 struct field_info *fi,
16333 std::vector<struct symbol *> *template_args,
16334 struct dwarf2_cu *cu)
16335{
16336 if (fi->current_variant_part == nullptr)
16337 {
16338 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16339 "- DIE at %s [in module %s]"),
16340 sect_offset_str (die->sect_off),
5e22e966 16341 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16342 return;
16343 }
16344 if (fi->current_variant_part->processing_variant)
16345 {
16346 complaint (_("nested DW_TAG_variant seen "
16347 "- DIE at %s [in module %s]"),
16348 sect_offset_str (die->sect_off),
5e22e966 16349 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16350 return;
16351 }
16352
16353 scoped_restore save_processing_variant
16354 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16355 true);
16356
16357 fi->current_variant_part->variants.emplace_back ();
16358 variant_field &variant = fi->current_variant_part->variants.back ();
16359 variant.first_field = fi->fields.size ();
16360
16361 /* In a variant we want to get the discriminant and also add a
16362 field for our sole member child. */
16363 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
cae21f8e 16364 if (discr == nullptr || !discr->form_is_constant ())
9c6a1327
TT
16365 {
16366 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
9d2246fc 16367 if (discr == nullptr || discr->as_block ()->size == 0)
9c6a1327
TT
16368 variant.default_branch = true;
16369 else
9d2246fc 16370 variant.discr_list_data = discr->as_block ();
9c6a1327
TT
16371 }
16372 else
cae21f8e 16373 variant.discriminant_value = discr->constant_value (0);
9c6a1327
TT
16374
16375 for (die_info *variant_child = die->child;
16376 variant_child != NULL;
16377 variant_child = variant_child->sibling)
16378 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16379
16380 variant.last_field = fi->fields.size ();
16381}
16382
2ddeaf8a
TT
16383/* A helper for process_structure_scope that handles a single member
16384 DIE. */
16385
16386static void
16387handle_struct_member_die (struct die_info *child_die, struct type *type,
16388 struct field_info *fi,
16389 std::vector<struct symbol *> *template_args,
16390 struct dwarf2_cu *cu)
16391{
16392 if (child_die->tag == DW_TAG_member
9c6a1327 16393 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
16394 {
16395 /* NOTE: carlton/2002-11-05: A C++ static data member
16396 should be a DW_TAG_member that is a declaration, but
16397 all versions of G++ as of this writing (so through at
16398 least 3.2.1) incorrectly generate DW_TAG_variable
16399 tags for them instead. */
16400 dwarf2_add_field (fi, child_die, cu);
16401 }
16402 else if (child_die->tag == DW_TAG_subprogram)
16403 {
16404 /* Rust doesn't have member functions in the C++ sense.
16405 However, it does emit ordinary functions as children
16406 of a struct DIE. */
16407 if (cu->language == language_rust)
16408 read_func_scope (child_die, cu);
16409 else
16410 {
16411 /* C++ member function. */
16412 dwarf2_add_member_fn (fi, child_die, type, cu);
16413 }
16414 }
16415 else if (child_die->tag == DW_TAG_inheritance)
16416 {
16417 /* C++ base class field. */
16418 dwarf2_add_field (fi, child_die, cu);
16419 }
16420 else if (type_can_define_types (child_die))
16421 dwarf2_add_type_defn (fi, child_die, cu);
16422 else if (child_die->tag == DW_TAG_template_type_param
16423 || child_die->tag == DW_TAG_template_value_param)
16424 {
16425 struct symbol *arg = new_symbol (child_die, NULL, cu);
16426
16427 if (arg != NULL)
16428 template_args->push_back (arg);
16429 }
9c6a1327
TT
16430 else if (child_die->tag == DW_TAG_variant_part)
16431 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 16432 else if (child_die->tag == DW_TAG_variant)
9c6a1327 16433 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
16434}
16435
c767944b
DJ
16436/* Finish creating a structure or union type, including filling in
16437 its members and creating a symbol for it. */
16438
16439static void
16440process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16441{
5e22e966 16442 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 16443 struct die_info *child_die;
c767944b
DJ
16444 struct type *type;
16445
16446 type = get_die_type (die, cu);
16447 if (type == NULL)
16448 type = read_structure_type (die, cu);
16449
3e1d3d8c 16450 bool has_template_parameters = false;
e142c38c 16451 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16452 {
16453 struct field_info fi;
2f4732b0 16454 std::vector<struct symbol *> template_args;
c906108c 16455
639d11d3 16456 child_die = die->child;
c906108c
SS
16457
16458 while (child_die && child_die->tag)
16459 {
2ddeaf8a 16460 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 16461 child_die = child_die->sibling;
c906108c
SS
16462 }
16463
34eaf542 16464 /* Attach template arguments to type. */
2f4732b0 16465 if (!template_args.empty ())
34eaf542 16466 {
3e1d3d8c 16467 has_template_parameters = true;
34eaf542 16468 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16469 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16470 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16471 = XOBNEWVEC (&objfile->objfile_obstack,
16472 struct symbol *,
16473 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16474 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16475 template_args.data (),
34eaf542
TT
16476 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16477 * sizeof (struct symbol *)));
34eaf542
TT
16478 }
16479
c906108c 16480 /* Attach fields and member functions to the type. */
317f7127 16481 if (fi.nfields () > 0)
e7c27a73 16482 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16483 if (!fi.fnfieldlists.empty ())
c906108c 16484 {
e7c27a73 16485 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16486
c5aa993b 16487 /* Get the type which refers to the base class (possibly this
c906108c 16488 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16489 class from the DW_AT_containing_type attribute. This use of
16490 DW_AT_containing_type is a GNU extension. */
c906108c 16491
e142c38c 16492 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16493 {
e7c27a73 16494 struct type *t = die_containing_type (die, cu);
c906108c 16495
ae6ae975 16496 set_type_vptr_basetype (type, t);
c906108c
SS
16497 if (type == t)
16498 {
c906108c
SS
16499 int i;
16500
16501 /* Our own class provides vtbl ptr. */
1f704f76 16502 for (i = t->num_fields () - 1;
c906108c
SS
16503 i >= TYPE_N_BASECLASSES (t);
16504 --i)
16505 {
0d5cff50 16506 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16507
dda83cd7 16508 if (is_vtable_name (fieldname, cu))
c906108c 16509 {
ae6ae975 16510 set_type_vptr_fieldno (type, i);
c906108c
SS
16511 break;
16512 }
16513 }
16514
16515 /* Complain if virtual function table field not found. */
16516 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16517 complaint (_("virtual function table pointer "
3e43a32a 16518 "not found when defining class '%s'"),
7d93a1e0 16519 type->name () ? type->name () : "");
c906108c
SS
16520 }
16521 else
16522 {
ae6ae975 16523 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16524 }
16525 }
f6235d4c 16526 else if (cu->producer
61012eef 16527 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16528 {
16529 /* The IBM XLC compiler does not provide direct indication
dda83cd7
SM
16530 of the containing type, but the vtable pointer is
16531 always named __vfp. */
f6235d4c
EZ
16532
16533 int i;
16534
1f704f76 16535 for (i = type->num_fields () - 1;
f6235d4c
EZ
16536 i >= TYPE_N_BASECLASSES (type);
16537 --i)
16538 {
16539 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16540 {
ae6ae975
DE
16541 set_type_vptr_fieldno (type, i);
16542 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16543 break;
16544 }
16545 }
16546 }
c906108c 16547 }
98751a41
JK
16548
16549 /* Copy fi.typedef_field_list linked list elements content into the
16550 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16551 if (!fi.typedef_field_list.empty ())
98751a41 16552 {
be2daae6 16553 int count = fi.typedef_field_list.size ();
98751a41 16554
a0d7a4ff 16555 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16556 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16557 = ((struct decl_field *)
be2daae6
TT
16558 TYPE_ALLOC (type,
16559 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16560 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16561
be2daae6
TT
16562 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16563 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16564 }
c767944b 16565
883fd55a
KS
16566 /* Copy fi.nested_types_list linked list elements content into the
16567 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16568 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16569 {
be2daae6 16570 int count = fi.nested_types_list.size ();
883fd55a
KS
16571
16572 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16573 TYPE_NESTED_TYPES_ARRAY (type)
16574 = ((struct decl_field *)
be2daae6
TT
16575 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16576 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16577
be2daae6
TT
16578 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16579 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16580 }
c906108c 16581 }
63d06c5c 16582
bb5ed363 16583 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16584 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16585 cu->rust_unions.push_back (type);
57567375
TT
16586 else if (cu->language == language_ada)
16587 quirk_ada_thick_pointer_struct (die, cu, type);
0b92b5bb 16588
90aeadfc
DC
16589 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16590 snapshots) has been known to create a die giving a declaration
16591 for a class that has, as a child, a die giving a definition for a
16592 nested class. So we have to process our children even if the
16593 current die is a declaration. Normally, of course, a declaration
16594 won't have any children at all. */
134d01f1 16595
ca040673
DE
16596 child_die = die->child;
16597
90aeadfc
DC
16598 while (child_die != NULL && child_die->tag)
16599 {
16600 if (child_die->tag == DW_TAG_member
16601 || child_die->tag == DW_TAG_variable
34eaf542
TT
16602 || child_die->tag == DW_TAG_inheritance
16603 || child_die->tag == DW_TAG_template_value_param
16604 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16605 {
90aeadfc 16606 /* Do nothing. */
134d01f1 16607 }
90aeadfc
DC
16608 else
16609 process_die (child_die, cu);
134d01f1 16610
436c571c 16611 child_die = child_die->sibling;
134d01f1
DJ
16612 }
16613
fa4028e9
JB
16614 /* Do not consider external references. According to the DWARF standard,
16615 these DIEs are identified by the fact that they have no byte_size
16616 attribute, and a declaration attribute. */
16617 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
16618 || !die_is_declaration (die, cu)
16619 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
16620 {
16621 struct symbol *sym = new_symbol (die, type, cu);
16622
16623 if (has_template_parameters)
16624 {
a776957c
TT
16625 struct symtab *symtab;
16626 if (sym != nullptr)
16627 symtab = symbol_symtab (sym);
16628 else if (cu->line_header != nullptr)
16629 {
16630 /* Any related symtab will do. */
16631 symtab
7ba99d21 16632 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16633 }
16634 else
16635 {
16636 symtab = nullptr;
16637 complaint (_("could not find suitable "
16638 "symtab for template parameter"
16639 " - DIE at %s [in module %s]"),
16640 sect_offset_str (die->sect_off),
16641 objfile_name (objfile));
16642 }
16643
16644 if (symtab != nullptr)
16645 {
16646 /* Make sure that the symtab is set on the new symbols.
16647 Even though they don't appear in this symtab directly,
16648 other parts of gdb assume that symbols do, and this is
16649 reasonably true. */
16650 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16651 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16652 }
3e1d3d8c
TT
16653 }
16654 }
134d01f1
DJ
16655}
16656
ed6acedd
TT
16657/* Assuming DIE is an enumeration type, and TYPE is its associated
16658 type, update TYPE using some information only available in DIE's
16659 children. In particular, the fields are computed. */
55426c9d
JB
16660
16661static void
16662update_enumeration_type_from_children (struct die_info *die,
16663 struct type *type,
16664 struct dwarf2_cu *cu)
16665{
60f7655a 16666 struct die_info *child_die;
55426c9d
JB
16667 int unsigned_enum = 1;
16668 int flag_enum = 1;
55426c9d 16669
8268c778 16670 auto_obstack obstack;
ed6acedd 16671 std::vector<struct field> fields;
55426c9d 16672
60f7655a
DE
16673 for (child_die = die->child;
16674 child_die != NULL && child_die->tag;
436c571c 16675 child_die = child_die->sibling)
55426c9d
JB
16676 {
16677 struct attribute *attr;
16678 LONGEST value;
16679 const gdb_byte *bytes;
16680 struct dwarf2_locexpr_baton *baton;
16681 const char *name;
60f7655a 16682
55426c9d
JB
16683 if (child_die->tag != DW_TAG_enumerator)
16684 continue;
16685
16686 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16687 if (attr == NULL)
16688 continue;
16689
16690 name = dwarf2_name (child_die, cu);
16691 if (name == NULL)
16692 name = "<anonymous enumerator>";
16693
16694 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16695 &value, &bytes, &baton);
16696 if (value < 0)
16697 {
16698 unsigned_enum = 0;
16699 flag_enum = 0;
16700 }
55426c9d 16701 else
edd45eb0
SM
16702 {
16703 if (count_one_bits_ll (value) >= 2)
16704 flag_enum = 0;
edd45eb0 16705 }
55426c9d 16706
ed6acedd
TT
16707 fields.emplace_back ();
16708 struct field &field = fields.back ();
16709 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16710 SET_FIELD_ENUMVAL (field, value);
16711 }
16712
16713 if (!fields.empty ())
16714 {
5e33d5f4 16715 type->set_num_fields (fields.size ());
3cabb6b0
SM
16716 type->set_fields
16717 ((struct field *)
16718 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16719 memcpy (type->fields (), fields.data (),
ed6acedd 16720 sizeof (struct field) * fields.size ());
55426c9d
JB
16721 }
16722
16723 if (unsigned_enum)
653223d3
SM
16724 type->set_is_unsigned (true);
16725
55426c9d
JB
16726 if (flag_enum)
16727 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16728}
16729
134d01f1
DJ
16730/* Given a DW_AT_enumeration_type die, set its type. We do not
16731 complete the type's fields yet, or create any symbols. */
c906108c 16732
f792889a 16733static struct type *
134d01f1 16734read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16735{
5e22e966 16736 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16737 struct type *type;
c906108c 16738 struct attribute *attr;
0114d602 16739 const char *name;
134d01f1 16740
348e048f
DE
16741 /* If the definition of this type lives in .debug_types, read that type.
16742 Don't follow DW_AT_specification though, that will take us back up
16743 the chain and we want to go down. */
052c8bb8 16744 attr = die->attr (DW_AT_signature);
435d3d88 16745 if (attr != nullptr)
348e048f 16746 {
ac9ec31b 16747 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16748
ac9ec31b 16749 /* The type's CU may not be the same as CU.
02142a6c 16750 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16751 return set_die_type (die, type, cu);
16752 }
16753
c906108c
SS
16754 type = alloc_type (objfile);
16755
67607e24 16756 type->set_code (TYPE_CODE_ENUM);
94af9270 16757 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16758 if (name != NULL)
d0e39ea2 16759 type->set_name (name);
c906108c 16760
0626fc76
TT
16761 attr = dwarf2_attr (die, DW_AT_type, cu);
16762 if (attr != NULL)
16763 {
16764 struct type *underlying_type = die_type (die, cu);
16765
16766 TYPE_TARGET_TYPE (type) = underlying_type;
16767 }
16768
e142c38c 16769 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16770 if (attr != nullptr)
c906108c 16771 {
529908cb 16772 TYPE_LENGTH (type) = attr->constant_value (0);
c906108c
SS
16773 }
16774 else
16775 {
16776 TYPE_LENGTH (type) = 0;
16777 }
16778
2b4424c3
TT
16779 maybe_set_alignment (cu, die, type);
16780
137033e9
JB
16781 /* The enumeration DIE can be incomplete. In Ada, any type can be
16782 declared as private in the package spec, and then defined only
16783 inside the package body. Such types are known as Taft Amendment
16784 Types. When another package uses such a type, an incomplete DIE
16785 may be generated by the compiler. */
02eb380e 16786 if (die_is_declaration (die, cu))
b4b73759 16787 type->set_is_stub (true);
02eb380e 16788
0626fc76
TT
16789 /* If this type has an underlying type that is not a stub, then we
16790 may use its attributes. We always use the "unsigned" attribute
16791 in this situation, because ordinarily we guess whether the type
16792 is unsigned -- but the guess can be wrong and the underlying type
16793 can tell us the reality. However, we defer to a local size
16794 attribute if one exists, because this lets the compiler override
16795 the underlying type if needed. */
e46d3488 16796 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
0626fc76 16797 {
9e7c9a03
HD
16798 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16799 underlying_type = check_typedef (underlying_type);
653223d3
SM
16800
16801 type->set_is_unsigned (underlying_type->is_unsigned ());
16802
0626fc76 16803 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16804 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
653223d3 16805
2b4424c3 16806 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16807 && TYPE_RAW_ALIGN (underlying_type) != 0)
16808 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16809 }
16810
3d567982
TT
16811 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16812
ed6acedd
TT
16813 set_die_type (die, type, cu);
16814
16815 /* Finish the creation of this type by using the enum's children.
16816 Note that, as usual, this must come after set_die_type to avoid
16817 infinite recursion when trying to compute the names of the
16818 enumerators. */
16819 update_enumeration_type_from_children (die, type, cu);
16820
16821 return type;
134d01f1
DJ
16822}
16823
16824/* Given a pointer to a die which begins an enumeration, process all
16825 the dies that define the members of the enumeration, and create the
16826 symbol for the enumeration type.
16827
16828 NOTE: We reverse the order of the element list. */
16829
16830static void
16831process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16832{
f792889a 16833 struct type *this_type;
134d01f1 16834
f792889a
DJ
16835 this_type = get_die_type (die, cu);
16836 if (this_type == NULL)
16837 this_type = read_enumeration_type (die, cu);
9dc481d3 16838
639d11d3 16839 if (die->child != NULL)
c906108c 16840 {
9dc481d3 16841 struct die_info *child_die;
15d034d0 16842 const char *name;
9dc481d3 16843
639d11d3 16844 child_die = die->child;
c906108c
SS
16845 while (child_die && child_die->tag)
16846 {
16847 if (child_die->tag != DW_TAG_enumerator)
16848 {
e7c27a73 16849 process_die (child_die, cu);
c906108c
SS
16850 }
16851 else
16852 {
39cbfefa
DJ
16853 name = dwarf2_name (child_die, cu);
16854 if (name)
ed6acedd 16855 new_symbol (child_die, this_type, cu);
c906108c
SS
16856 }
16857
436c571c 16858 child_die = child_die->sibling;
c906108c 16859 }
c906108c 16860 }
134d01f1 16861
6c83ed52
TT
16862 /* If we are reading an enum from a .debug_types unit, and the enum
16863 is a declaration, and the enum is not the signatured type in the
16864 unit, then we do not want to add a symbol for it. Adding a
16865 symbol would in some cases obscure the true definition of the
16866 enum, giving users an incomplete type when the definition is
16867 actually available. Note that we do not want to do this for all
16868 enums which are just declarations, because C++0x allows forward
16869 enum declarations. */
3019eac3 16870 if (cu->per_cu->is_debug_types
6c83ed52
TT
16871 && die_is_declaration (die, cu))
16872 {
52dc124a 16873 struct signatured_type *sig_type;
6c83ed52 16874
c0f78cd4 16875 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16876 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16877 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16878 return;
16879 }
16880
f792889a 16881 new_symbol (die, this_type, cu);
c906108c
SS
16882}
16883
57567375
TT
16884/* Helper function for quirk_ada_thick_pointer that examines a bounds
16885 expression for an index type and finds the corresponding field
16886 offset in the hidden "P_BOUNDS" structure. Returns true on success
16887 and updates *FIELD, false if it fails to recognize an
16888 expression. */
16889
16890static bool
16891recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16892 int *bounds_offset, struct field *field,
16893 struct dwarf2_cu *cu)
16894{
16895 struct attribute *attr = dwarf2_attr (die, name, cu);
16896 if (attr == nullptr || !attr->form_is_block ())
16897 return false;
16898
16899 const struct dwarf_block *block = attr->as_block ();
16900 const gdb_byte *start = block->data;
16901 const gdb_byte *end = block->data + block->size;
16902
16903 /* The expression to recognize generally looks like:
16904
16905 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16906 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16907
16908 However, the second "plus_uconst" may be missing:
16909
16910 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16911 DW_OP_deref_size: 4)
16912
16913 This happens when the field is at the start of the structure.
16914
16915 Also, the final deref may not be sized:
16916
16917 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16918 DW_OP_deref)
16919
16920 This happens when the size of the index type happens to be the
16921 same as the architecture's word size. This can occur with or
16922 without the second plus_uconst. */
16923
16924 if (end - start < 2)
16925 return false;
16926 if (*start++ != DW_OP_push_object_address)
16927 return false;
16928 if (*start++ != DW_OP_plus_uconst)
16929 return false;
16930
16931 uint64_t this_bound_off;
16932 start = gdb_read_uleb128 (start, end, &this_bound_off);
16933 if (start == nullptr || (int) this_bound_off != this_bound_off)
16934 return false;
16935 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16936 is consistent among all bounds. */
16937 if (*bounds_offset == -1)
16938 *bounds_offset = this_bound_off;
16939 else if (*bounds_offset != this_bound_off)
16940 return false;
16941
16942 if (start == end || *start++ != DW_OP_deref)
16943 return false;
16944
16945 int offset = 0;
16946 if (start ==end)
16947 return false;
16948 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16949 {
16950 /* This means an offset of 0. */
16951 }
16952 else if (*start++ != DW_OP_plus_uconst)
16953 return false;
16954 else
16955 {
16956 /* The size is the parameter to DW_OP_plus_uconst. */
16957 uint64_t val;
16958 start = gdb_read_uleb128 (start, end, &val);
16959 if (start == nullptr)
16960 return false;
16961 if ((int) val != val)
16962 return false;
16963 offset = val;
16964 }
16965
16966 if (start == end)
16967 return false;
16968
16969 uint64_t size;
16970 if (*start == DW_OP_deref_size)
16971 {
16972 start = gdb_read_uleb128 (start + 1, end, &size);
16973 if (start == nullptr)
16974 return false;
16975 }
16976 else if (*start == DW_OP_deref)
16977 {
16978 size = cu->header.addr_size;
16979 ++start;
16980 }
16981 else
16982 return false;
16983
16984 SET_FIELD_BITPOS (*field, 8 * offset);
16985 if (size != TYPE_LENGTH (field->type ()))
16986 FIELD_BITSIZE (*field) = 8 * size;
16987
16988 return true;
16989}
16990
16991/* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16992 some kinds of Ada arrays:
16993
16994 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16995 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16996 <11e0> DW_AT_data_location: 2 byte block: 97 6
16997 (DW_OP_push_object_address; DW_OP_deref)
16998 <11e3> DW_AT_type : <0x1173>
16999 <11e7> DW_AT_sibling : <0x1201>
17000 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
17001 <11ec> DW_AT_type : <0x1206>
17002 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
17003 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
17004 DW_OP_deref_size: 4)
17005 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
17006 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
17007 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
17008
17009 This actually represents a "thick pointer", which is a structure
17010 with two elements: one that is a pointer to the array data, and one
17011 that is a pointer to another structure; this second structure holds
17012 the array bounds.
17013
17014 This returns a new type on success, or nullptr if this didn't
17015 recognize the type. */
17016
17017static struct type *
17018quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
17019 struct type *type)
17020{
17021 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
17022 /* So far we've only seen this with block form. */
17023 if (attr == nullptr || !attr->form_is_block ())
17024 return nullptr;
17025
17026 /* Note that this will fail if the structure layout is changed by
17027 the compiler. However, we have no good way to recognize some
17028 other layout, because we don't know what expression the compiler
17029 might choose to emit should this happen. */
17030 struct dwarf_block *blk = attr->as_block ();
17031 if (blk->size != 2
17032 || blk->data[0] != DW_OP_push_object_address
17033 || blk->data[1] != DW_OP_deref)
17034 return nullptr;
17035
17036 int bounds_offset = -1;
17037 int max_align = -1;
17038 std::vector<struct field> range_fields;
17039 for (struct die_info *child_die = die->child;
17040 child_die;
17041 child_die = child_die->sibling)
17042 {
17043 if (child_die->tag == DW_TAG_subrange_type)
17044 {
17045 struct type *underlying = read_subrange_index_type (child_die, cu);
17046
17047 int this_align = type_align (underlying);
17048 if (this_align > max_align)
17049 max_align = this_align;
17050
17051 range_fields.emplace_back ();
17052 range_fields.emplace_back ();
17053
17054 struct field &lower = range_fields[range_fields.size () - 2];
17055 struct field &upper = range_fields[range_fields.size () - 1];
17056
17057 lower.set_type (underlying);
17058 FIELD_ARTIFICIAL (lower) = 1;
17059
17060 upper.set_type (underlying);
17061 FIELD_ARTIFICIAL (upper) = 1;
17062
17063 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
17064 &bounds_offset, &lower, cu)
17065 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
17066 &bounds_offset, &upper, cu))
17067 return nullptr;
17068 }
17069 }
17070
17071 /* This shouldn't really happen, but double-check that we found
17072 where the bounds are stored. */
17073 if (bounds_offset == -1)
17074 return nullptr;
17075
17076 struct objfile *objfile = cu->per_objfile->objfile;
17077 for (int i = 0; i < range_fields.size (); i += 2)
17078 {
17079 char name[20];
17080
17081 /* Set the name of each field in the bounds. */
17082 xsnprintf (name, sizeof (name), "LB%d", i / 2);
17083 FIELD_NAME (range_fields[i]) = objfile->intern (name);
17084 xsnprintf (name, sizeof (name), "UB%d", i / 2);
17085 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
17086 }
17087
17088 struct type *bounds = alloc_type (objfile);
17089 bounds->set_code (TYPE_CODE_STRUCT);
17090
17091 bounds->set_num_fields (range_fields.size ());
17092 bounds->set_fields
17093 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
17094 * sizeof (struct field))));
17095 memcpy (bounds->fields (), range_fields.data (),
17096 bounds->num_fields () * sizeof (struct field));
17097
17098 int last_fieldno = range_fields.size () - 1;
17099 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
17100 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
17101 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
17102
17103 /* Rewrite the existing array type in place. Specifically, we
17104 remove any dynamic properties we might have read, and we replace
17105 the index types. */
17106 struct type *iter = type;
17107 for (int i = 0; i < range_fields.size (); i += 2)
17108 {
17109 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
17110 iter->main_type->dyn_prop_list = nullptr;
17111 iter->set_index_type
17112 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
17113 iter = TYPE_TARGET_TYPE (iter);
17114 }
17115
17116 struct type *result = alloc_type (objfile);
17117 result->set_code (TYPE_CODE_STRUCT);
17118
17119 result->set_num_fields (2);
17120 result->set_fields
17121 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
17122 * sizeof (struct field))));
17123
17124 /* The names are chosen to coincide with what the compiler does with
17125 -fgnat-encodings=all, which the Ada code in gdb already
17126 understands. */
17127 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
17128 result->field (0).set_type (lookup_pointer_type (type));
17129
17130 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
17131 result->field (1).set_type (lookup_pointer_type (bounds));
17132 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
17133
17134 result->set_name (type->name ());
17135 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17136 + TYPE_LENGTH (result->field (1).type ()));
17137
17138 return result;
17139}
17140
c906108c
SS
17141/* Extract all information from a DW_TAG_array_type DIE and put it in
17142 the DIE's type field. For now, this only handles one dimensional
17143 arrays. */
17144
f792889a 17145static struct type *
e7c27a73 17146read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17147{
5e22e966 17148 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 17149 struct die_info *child_die;
7e314c57 17150 struct type *type;
c906108c 17151 struct type *element_type, *range_type, *index_type;
c906108c 17152 struct attribute *attr;
15d034d0 17153 const char *name;
a405673c 17154 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 17155 unsigned int bit_stride = 0;
c906108c 17156
e7c27a73 17157 element_type = die_type (die, cu);
c906108c 17158
7e314c57
JK
17159 /* The die_type call above may have already set the type for this DIE. */
17160 type = get_die_type (die, cu);
17161 if (type)
17162 return type;
17163
dc53a7ad
JB
17164 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17165 if (attr != NULL)
a405673c
JB
17166 {
17167 int stride_ok;
293e7e51 17168 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
17169
17170 byte_stride_prop
17171 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
17172 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17173 prop_type);
a405673c
JB
17174 if (!stride_ok)
17175 {
b98664d3 17176 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
17177 " - DIE at %s [in module %s]"),
17178 sect_offset_str (die->sect_off),
5e22e966 17179 objfile_name (cu->per_objfile->objfile));
a405673c
JB
17180 /* Ignore this attribute. We will likely not be able to print
17181 arrays of this type correctly, but there is little we can do
17182 to help if we cannot read the attribute's value. */
17183 byte_stride_prop = NULL;
17184 }
17185 }
dc53a7ad
JB
17186
17187 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17188 if (attr != NULL)
529908cb 17189 bit_stride = attr->constant_value (0);
dc53a7ad 17190
c906108c
SS
17191 /* Irix 6.2 native cc creates array types without children for
17192 arrays with unspecified length. */
639d11d3 17193 if (die->child == NULL)
c906108c 17194 {
46bf5051 17195 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17196 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 17197 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 17198 byte_stride_prop, bit_stride);
f792889a 17199 return set_die_type (die, type, cu);
c906108c
SS
17200 }
17201
791afaa2 17202 std::vector<struct type *> range_types;
639d11d3 17203 child_die = die->child;
c906108c
SS
17204 while (child_die && child_die->tag)
17205 {
17206 if (child_die->tag == DW_TAG_subrange_type)
17207 {
f792889a 17208 struct type *child_type = read_type_die (child_die, cu);
9a619af0 17209
dda83cd7
SM
17210 if (child_type != NULL)
17211 {
0963b4bd 17212 /* The range type was succesfully read. Save it for the
dda83cd7 17213 array type creation. */
791afaa2 17214 range_types.push_back (child_type);
dda83cd7 17215 }
c906108c 17216 }
436c571c 17217 child_die = child_die->sibling;
c906108c
SS
17218 }
17219
cf2b2075
TV
17220 if (range_types.empty ())
17221 {
17222 complaint (_("unable to find array range - DIE at %s [in module %s]"),
17223 sect_offset_str (die->sect_off),
17224 objfile_name (cu->per_objfile->objfile));
17225 return NULL;
17226 }
17227
c906108c
SS
17228 /* Dwarf2 dimensions are output from left to right, create the
17229 necessary array types in backwards order. */
7ca2d3a3 17230
c906108c 17231 type = element_type;
7ca2d3a3
DL
17232
17233 if (read_array_order (die, cu) == DW_ORD_col_major)
17234 {
17235 int i = 0;
9a619af0 17236
791afaa2 17237 while (i < range_types.size ())
10f6a3ad
TT
17238 {
17239 type = create_array_type_with_stride (NULL, type, range_types[i++],
17240 byte_stride_prop, bit_stride);
17241 bit_stride = 0;
17242 byte_stride_prop = nullptr;
17243 }
7ca2d3a3
DL
17244 }
17245 else
17246 {
791afaa2 17247 size_t ndim = range_types.size ();
7ca2d3a3 17248 while (ndim-- > 0)
10f6a3ad
TT
17249 {
17250 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17251 byte_stride_prop, bit_stride);
17252 bit_stride = 0;
17253 byte_stride_prop = nullptr;
17254 }
7ca2d3a3 17255 }
c906108c 17256
cf2b2075
TV
17257 gdb_assert (type != element_type);
17258
f5f8a009
EZ
17259 /* Understand Dwarf2 support for vector types (like they occur on
17260 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17261 array type. This is not part of the Dwarf2/3 standard yet, but a
17262 custom vendor extension. The main difference between a regular
17263 array and the vector variant is that vectors are passed by value
17264 to functions. */
e142c38c 17265 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 17266 if (attr != nullptr)
ea37ba09 17267 make_vector_type (type);
f5f8a009 17268
dbc98a8b
KW
17269 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17270 implementation may choose to implement triple vectors using this
17271 attribute. */
17272 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb 17273 if (attr != nullptr && attr->form_is_unsigned ())
dbc98a8b 17274 {
529908cb
TT
17275 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17276 TYPE_LENGTH (type) = attr->as_unsigned ();
dbc98a8b 17277 else
b98664d3 17278 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 17279 "than the total size of elements"));
dbc98a8b
KW
17280 }
17281
39cbfefa
DJ
17282 name = dwarf2_name (die, cu);
17283 if (name)
d0e39ea2 17284 type->set_name (name);
6e70227d 17285
2b4424c3
TT
17286 maybe_set_alignment (cu, die, type);
17287
57567375
TT
17288 struct type *replacement_type = nullptr;
17289 if (cu->language == language_ada)
17290 {
17291 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17292 if (replacement_type != nullptr)
17293 type = replacement_type;
17294 }
17295
0963b4bd 17296 /* Install the type in the die. */
57567375 17297 set_die_type (die, type, cu, replacement_type != nullptr);
7e314c57
JK
17298
17299 /* set_die_type should be already done. */
b4ba55a1
JB
17300 set_descriptive_type (type, die, cu);
17301
7e314c57 17302 return type;
c906108c
SS
17303}
17304
7ca2d3a3 17305static enum dwarf_array_dim_ordering
6e70227d 17306read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
17307{
17308 struct attribute *attr;
17309
17310 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17311
435d3d88 17312 if (attr != nullptr)
1bc397c5
TT
17313 {
17314 LONGEST val = attr->constant_value (-1);
17315 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17316 return (enum dwarf_array_dim_ordering) val;
17317 }
7ca2d3a3 17318
0963b4bd
MS
17319 /* GNU F77 is a special case, as at 08/2004 array type info is the
17320 opposite order to the dwarf2 specification, but data is still
17321 laid out as per normal fortran.
7ca2d3a3 17322
0963b4bd
MS
17323 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17324 version checking. */
7ca2d3a3 17325
905e0470
PM
17326 if (cu->language == language_fortran
17327 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
17328 {
17329 return DW_ORD_row_major;
17330 }
17331
3a3440fb 17332 switch (cu->language_defn->array_ordering ())
7ca2d3a3
DL
17333 {
17334 case array_column_major:
17335 return DW_ORD_col_major;
17336 case array_row_major:
17337 default:
17338 return DW_ORD_row_major;
17339 };
17340}
17341
72019c9c 17342/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 17343 the DIE's type field. */
72019c9c 17344
f792889a 17345static struct type *
72019c9c
GM
17346read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17347{
7e314c57
JK
17348 struct type *domain_type, *set_type;
17349 struct attribute *attr;
f792889a 17350
7e314c57
JK
17351 domain_type = die_type (die, cu);
17352
17353 /* The die_type call above may have already set the type for this DIE. */
17354 set_type = get_die_type (die, cu);
17355 if (set_type)
17356 return set_type;
17357
17358 set_type = create_set_type (NULL, domain_type);
17359
17360 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb
TT
17361 if (attr != nullptr && attr->form_is_unsigned ())
17362 TYPE_LENGTH (set_type) = attr->as_unsigned ();
7e314c57 17363
2b4424c3
TT
17364 maybe_set_alignment (cu, die, set_type);
17365
f792889a 17366 return set_die_type (die, set_type, cu);
72019c9c 17367}
7ca2d3a3 17368
0971de02
TT
17369/* A helper for read_common_block that creates a locexpr baton.
17370 SYM is the symbol which we are marking as computed.
17371 COMMON_DIE is the DIE for the common block.
17372 COMMON_LOC is the location expression attribute for the common
17373 block itself.
17374 MEMBER_LOC is the location expression attribute for the particular
17375 member of the common block that we are processing.
17376 CU is the CU from which the above come. */
17377
17378static void
17379mark_common_block_symbol_computed (struct symbol *sym,
17380 struct die_info *common_die,
17381 struct attribute *common_loc,
17382 struct attribute *member_loc,
17383 struct dwarf2_cu *cu)
17384{
5e22e966 17385 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 17386 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
17387 struct dwarf2_locexpr_baton *baton;
17388 gdb_byte *ptr;
17389 unsigned int cu_off;
08feed99 17390 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
17391 LONGEST offset = 0;
17392
17393 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
17394 gdb_assert (common_loc->form_is_block ());
17395 gdb_assert (member_loc->form_is_block ()
cd6c91b4 17396 || member_loc->form_is_constant ());
0971de02 17397
8d749320 17398 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 17399 baton->per_objfile = per_objfile;
0971de02
TT
17400 baton->per_cu = cu->per_cu;
17401 gdb_assert (baton->per_cu);
17402
17403 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17404
cd6c91b4 17405 if (member_loc->form_is_constant ())
0971de02 17406 {
0826b30a 17407 offset = member_loc->constant_value (0);
0971de02
TT
17408 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17409 }
17410 else
9d2246fc 17411 baton->size += member_loc->as_block ()->size;
0971de02 17412
224c3ddb 17413 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
17414 baton->data = ptr;
17415
17416 *ptr++ = DW_OP_call4;
9c541725 17417 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
17418 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17419 ptr += 4;
17420
cd6c91b4 17421 if (member_loc->form_is_constant ())
0971de02
TT
17422 {
17423 *ptr++ = DW_OP_addr;
17424 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17425 ptr += cu->header.addr_size;
17426 }
17427 else
17428 {
17429 /* We have to copy the data here, because DW_OP_call4 will only
17430 use a DW_AT_location attribute. */
9d2246fc
TT
17431 struct dwarf_block *block = member_loc->as_block ();
17432 memcpy (ptr, block->data, block->size);
17433 ptr += block->size;
0971de02
TT
17434 }
17435
17436 *ptr++ = DW_OP_plus;
17437 gdb_assert (ptr - baton->data == baton->size);
17438
0971de02 17439 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 17440 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
17441}
17442
4357ac6c
TT
17443/* Create appropriate locally-scoped variables for all the
17444 DW_TAG_common_block entries. Also create a struct common_block
17445 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 17446 is used to separate the common blocks name namespace from regular
4357ac6c 17447 variable names. */
c906108c
SS
17448
17449static void
e7c27a73 17450read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17451{
0971de02
TT
17452 struct attribute *attr;
17453
17454 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 17455 if (attr != nullptr)
0971de02
TT
17456 {
17457 /* Support the .debug_loc offsets. */
4fc6c0d5 17458 if (attr->form_is_block ())
dda83cd7 17459 {
0971de02 17460 /* Ok. */
dda83cd7 17461 }
cd6c91b4 17462 else if (attr->form_is_section_offset ())
dda83cd7 17463 {
0971de02
TT
17464 dwarf2_complex_location_expr_complaint ();
17465 attr = NULL;
dda83cd7 17466 }
0971de02 17467 else
dda83cd7 17468 {
0971de02
TT
17469 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17470 "common block member");
17471 attr = NULL;
dda83cd7 17472 }
0971de02
TT
17473 }
17474
639d11d3 17475 if (die->child != NULL)
c906108c 17476 {
5e22e966 17477 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
17478 struct die_info *child_die;
17479 size_t n_entries = 0, size;
17480 struct common_block *common_block;
17481 struct symbol *sym;
74ac6d43 17482
4357ac6c
TT
17483 for (child_die = die->child;
17484 child_die && child_die->tag;
436c571c 17485 child_die = child_die->sibling)
4357ac6c
TT
17486 ++n_entries;
17487
17488 size = (sizeof (struct common_block)
17489 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
17490 common_block
17491 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17492 size);
4357ac6c
TT
17493 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17494 common_block->n_entries = 0;
17495
17496 for (child_die = die->child;
17497 child_die && child_die->tag;
436c571c 17498 child_die = child_die->sibling)
4357ac6c
TT
17499 {
17500 /* Create the symbol in the DW_TAG_common_block block in the current
17501 symbol scope. */
e7c27a73 17502 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
17503 if (sym != NULL)
17504 {
17505 struct attribute *member_loc;
17506
17507 common_block->contents[common_block->n_entries++] = sym;
17508
17509 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17510 cu);
17511 if (member_loc)
17512 {
17513 /* GDB has handled this for a long time, but it is
17514 not specified by DWARF. It seems to have been
17515 emitted by gfortran at least as recently as:
17516 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 17517 complaint (_("Variable in common block has "
0971de02 17518 "DW_AT_data_member_location "
9d8780f0
SM
17519 "- DIE at %s [in module %s]"),
17520 sect_offset_str (child_die->sect_off),
518817b3 17521 objfile_name (objfile));
0971de02 17522
cd6c91b4 17523 if (member_loc->form_is_section_offset ())
0971de02 17524 dwarf2_complex_location_expr_complaint ();
cd6c91b4 17525 else if (member_loc->form_is_constant ()
4fc6c0d5 17526 || member_loc->form_is_block ())
0971de02 17527 {
435d3d88 17528 if (attr != nullptr)
0971de02
TT
17529 mark_common_block_symbol_computed (sym, die, attr,
17530 member_loc, cu);
17531 }
17532 else
17533 dwarf2_complex_location_expr_complaint ();
17534 }
17535 }
c906108c 17536 }
4357ac6c
TT
17537
17538 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17539 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
17540 }
17541}
17542
0114d602 17543/* Create a type for a C++ namespace. */
d9fa45fe 17544
0114d602
DJ
17545static struct type *
17546read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 17547{
5e22e966 17548 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17549 const char *previous_prefix, *name;
9219021c 17550 int is_anonymous;
0114d602
DJ
17551 struct type *type;
17552
17553 /* For extensions, reuse the type of the original namespace. */
17554 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17555 {
17556 struct die_info *ext_die;
17557 struct dwarf2_cu *ext_cu = cu;
9a619af0 17558
0114d602
DJ
17559 ext_die = dwarf2_extension (die, &ext_cu);
17560 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
17561
17562 /* EXT_CU may not be the same as CU.
02142a6c 17563 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
17564 return set_die_type (die, type, cu);
17565 }
9219021c 17566
e142c38c 17567 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
17568
17569 /* Now build the name of the current namespace. */
17570
0114d602
DJ
17571 previous_prefix = determine_prefix (die, cu);
17572 if (previous_prefix[0] != '\0')
17573 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 17574 previous_prefix, name, 0, cu);
0114d602
DJ
17575
17576 /* Create the type. */
19f392bc 17577 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 17578
60531b24 17579 return set_die_type (die, type, cu);
0114d602
DJ
17580}
17581
22cee43f 17582/* Read a namespace scope. */
0114d602
DJ
17583
17584static void
17585read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17586{
5e22e966 17587 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17588 int is_anonymous;
9219021c 17589
5c4e30ca
DC
17590 /* Add a symbol associated to this if we haven't seen the namespace
17591 before. Also, add a using directive if it's an anonymous
17592 namespace. */
9219021c 17593
f2f0e013 17594 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17595 {
17596 struct type *type;
17597
0114d602 17598 type = read_type_die (die, cu);
e7c27a73 17599 new_symbol (die, type, cu);
5c4e30ca 17600
e8e80198 17601 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17602 if (is_anonymous)
0114d602
DJ
17603 {
17604 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17605
eb1e02fd 17606 std::vector<const char *> excludes;
804d2729 17607 add_using_directive (using_directives (cu),
7d93a1e0 17608 previous_prefix, type->name (), NULL,
eb1e02fd 17609 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17610 }
5c4e30ca 17611 }
9219021c 17612
639d11d3 17613 if (die->child != NULL)
d9fa45fe 17614 {
639d11d3 17615 struct die_info *child_die = die->child;
6e70227d 17616
d9fa45fe
DC
17617 while (child_die && child_die->tag)
17618 {
e7c27a73 17619 process_die (child_die, cu);
436c571c 17620 child_die = child_die->sibling;
d9fa45fe
DC
17621 }
17622 }
38d518c9
EZ
17623}
17624
f55ee35c
JK
17625/* Read a Fortran module as type. This DIE can be only a declaration used for
17626 imported module. Still we need that type as local Fortran "use ... only"
17627 declaration imports depend on the created type in determine_prefix. */
17628
17629static struct type *
17630read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17631{
5e22e966 17632 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 17633 const char *module_name;
f55ee35c
JK
17634 struct type *type;
17635
17636 module_name = dwarf2_name (die, cu);
19f392bc 17637 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17638
f55ee35c
JK
17639 return set_die_type (die, type, cu);
17640}
17641
5d7cb8df
JK
17642/* Read a Fortran module. */
17643
17644static void
17645read_module (struct die_info *die, struct dwarf2_cu *cu)
17646{
17647 struct die_info *child_die = die->child;
530e8392
KB
17648 struct type *type;
17649
17650 type = read_type_die (die, cu);
17651 new_symbol (die, type, cu);
5d7cb8df 17652
5d7cb8df
JK
17653 while (child_die && child_die->tag)
17654 {
17655 process_die (child_die, cu);
436c571c 17656 child_die = child_die->sibling;
5d7cb8df
JK
17657 }
17658}
17659
38d518c9
EZ
17660/* Return the name of the namespace represented by DIE. Set
17661 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17662 namespace. */
17663
17664static const char *
e142c38c 17665namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17666{
17667 struct die_info *current_die;
17668 const char *name = NULL;
17669
17670 /* Loop through the extensions until we find a name. */
17671
17672 for (current_die = die;
17673 current_die != NULL;
f2f0e013 17674 current_die = dwarf2_extension (die, &cu))
38d518c9 17675 {
96553a0c
DE
17676 /* We don't use dwarf2_name here so that we can detect the absence
17677 of a name -> anonymous namespace. */
7d45c7c3 17678 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17679
38d518c9
EZ
17680 if (name != NULL)
17681 break;
17682 }
17683
17684 /* Is it an anonymous namespace? */
17685
17686 *is_anonymous = (name == NULL);
17687 if (*is_anonymous)
2b1dbab0 17688 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17689
17690 return name;
d9fa45fe
DC
17691}
17692
c906108c
SS
17693/* Extract all information from a DW_TAG_pointer_type DIE and add to
17694 the user defined type vector. */
17695
f792889a 17696static struct type *
e7c27a73 17697read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17698{
5e22e966 17699 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 17700 struct comp_unit_head *cu_header = &cu->header;
c906108c 17701 struct type *type;
8b2dbe47
KB
17702 struct attribute *attr_byte_size;
17703 struct attribute *attr_address_class;
17704 int byte_size, addr_class;
7e314c57
JK
17705 struct type *target_type;
17706
17707 target_type = die_type (die, cu);
c906108c 17708
7e314c57
JK
17709 /* The die_type call above may have already set the type for this DIE. */
17710 type = get_die_type (die, cu);
17711 if (type)
17712 return type;
17713
17714 type = lookup_pointer_type (target_type);
8b2dbe47 17715
e142c38c 17716 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47 17717 if (attr_byte_size)
529908cb 17718 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
c906108c 17719 else
8b2dbe47
KB
17720 byte_size = cu_header->addr_size;
17721
e142c38c 17722 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47 17723 if (attr_address_class)
529908cb 17724 addr_class = attr_address_class->constant_value (DW_ADDR_none);
8b2dbe47
KB
17725 else
17726 addr_class = DW_ADDR_none;
17727
2b4424c3
TT
17728 ULONGEST alignment = get_alignment (cu, die);
17729
17730 /* If the pointer size, alignment, or address class is different
17731 than the default, create a type variant marked as such and set
17732 the length accordingly. */
17733 if (TYPE_LENGTH (type) != byte_size
17734 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17735 && alignment != TYPE_RAW_ALIGN (type))
17736 || addr_class != DW_ADDR_none)
c906108c 17737 {
5e2b427d 17738 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47 17739 {
314ad88d
PA
17740 type_instance_flags type_flags
17741 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17742 addr_class);
876cecd0
TT
17743 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17744 == 0);
8b2dbe47
KB
17745 type = make_type_with_address_space (type, type_flags);
17746 }
17747 else if (TYPE_LENGTH (type) != byte_size)
17748 {
b98664d3 17749 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17750 }
2b4424c3
TT
17751 else if (TYPE_RAW_ALIGN (type) != alignment)
17752 {
b98664d3 17753 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17754 " - DIE at %s [in module %s]"),
17755 sect_offset_str (die->sect_off),
5e22e966 17756 objfile_name (cu->per_objfile->objfile));
2b4424c3 17757 }
6e70227d 17758 else
9a619af0
MS
17759 {
17760 /* Should we also complain about unhandled address classes? */
17761 }
c906108c 17762 }
8b2dbe47
KB
17763
17764 TYPE_LENGTH (type) = byte_size;
2b4424c3 17765 set_type_align (type, alignment);
f792889a 17766 return set_die_type (die, type, cu);
c906108c
SS
17767}
17768
17769/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17770 the user defined type vector. */
17771
f792889a 17772static struct type *
e7c27a73 17773read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17774{
17775 struct type *type;
17776 struct type *to_type;
17777 struct type *domain;
17778
e7c27a73
DJ
17779 to_type = die_type (die, cu);
17780 domain = die_containing_type (die, cu);
0d5de010 17781
7e314c57
JK
17782 /* The calls above may have already set the type for this DIE. */
17783 type = get_die_type (die, cu);
17784 if (type)
17785 return type;
17786
78134374 17787 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 17788 type = lookup_methodptr_type (to_type);
78134374 17789 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 17790 {
5e22e966 17791 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
17792
17793 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 17794 to_type->fields (), to_type->num_fields (),
a409645d 17795 to_type->has_varargs ());
7078baeb
TT
17796 type = lookup_methodptr_type (new_type);
17797 }
0d5de010
DJ
17798 else
17799 type = lookup_memberptr_type (to_type, domain);
c906108c 17800
f792889a 17801 return set_die_type (die, type, cu);
c906108c
SS
17802}
17803
4297a3f0 17804/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17805 the user defined type vector. */
17806
f792889a 17807static struct type *
4297a3f0 17808read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
dda83cd7 17809 enum type_code refcode)
c906108c 17810{
e7c27a73 17811 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17812 struct type *type, *target_type;
c906108c
SS
17813 struct attribute *attr;
17814
4297a3f0
AV
17815 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17816
7e314c57
JK
17817 target_type = die_type (die, cu);
17818
17819 /* The die_type call above may have already set the type for this DIE. */
17820 type = get_die_type (die, cu);
17821 if (type)
17822 return type;
17823
4297a3f0 17824 type = lookup_reference_type (target_type, refcode);
e142c38c 17825 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17826 if (attr != nullptr)
c906108c 17827 {
529908cb 17828 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
c906108c
SS
17829 }
17830 else
17831 {
107d2387 17832 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17833 }
2b4424c3 17834 maybe_set_alignment (cu, die, type);
f792889a 17835 return set_die_type (die, type, cu);
c906108c
SS
17836}
17837
cf363f18
MW
17838/* Add the given cv-qualifiers to the element type of the array. GCC
17839 outputs DWARF type qualifiers that apply to an array, not the
17840 element type. But GDB relies on the array element type to carry
17841 the cv-qualifiers. This mimics section 6.7.3 of the C99
17842 specification. */
17843
17844static struct type *
17845add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17846 struct type *base_type, int cnst, int voltl)
17847{
17848 struct type *el_type, *inner_array;
17849
17850 base_type = copy_type (base_type);
17851 inner_array = base_type;
17852
78134374 17853 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17854 {
17855 TYPE_TARGET_TYPE (inner_array) =
17856 copy_type (TYPE_TARGET_TYPE (inner_array));
17857 inner_array = TYPE_TARGET_TYPE (inner_array);
17858 }
17859
17860 el_type = TYPE_TARGET_TYPE (inner_array);
17861 cnst |= TYPE_CONST (el_type);
17862 voltl |= TYPE_VOLATILE (el_type);
17863 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17864
17865 return set_die_type (die, base_type, cu);
17866}
17867
f792889a 17868static struct type *
e7c27a73 17869read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17870{
f792889a 17871 struct type *base_type, *cv_type;
c906108c 17872
e7c27a73 17873 base_type = die_type (die, cu);
7e314c57
JK
17874
17875 /* The die_type call above may have already set the type for this DIE. */
17876 cv_type = get_die_type (die, cu);
17877 if (cv_type)
17878 return cv_type;
17879
2f608a3a
KW
17880 /* In case the const qualifier is applied to an array type, the element type
17881 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 17882 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 17883 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17884
f792889a
DJ
17885 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17886 return set_die_type (die, cv_type, cu);
c906108c
SS
17887}
17888
f792889a 17889static struct type *
e7c27a73 17890read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17891{
f792889a 17892 struct type *base_type, *cv_type;
c906108c 17893
e7c27a73 17894 base_type = die_type (die, cu);
7e314c57
JK
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
cf363f18
MW
17901 /* In case the volatile qualifier is applied to an array type, the
17902 element type is so qualified, not the array type (section 6.7.3
17903 of C99). */
78134374 17904 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17905 return add_array_cv_type (die, cu, base_type, 0, 1);
17906
f792889a
DJ
17907 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17908 return set_die_type (die, cv_type, cu);
c906108c
SS
17909}
17910
06d66ee9
TT
17911/* Handle DW_TAG_restrict_type. */
17912
17913static struct type *
17914read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17915{
17916 struct type *base_type, *cv_type;
17917
17918 base_type = die_type (die, cu);
17919
17920 /* The die_type call above may have already set the type for this DIE. */
17921 cv_type = get_die_type (die, cu);
17922 if (cv_type)
17923 return cv_type;
17924
17925 cv_type = make_restrict_type (base_type);
17926 return set_die_type (die, cv_type, cu);
17927}
17928
a2c2acaf
MW
17929/* Handle DW_TAG_atomic_type. */
17930
17931static struct type *
17932read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17933{
17934 struct type *base_type, *cv_type;
17935
17936 base_type = die_type (die, cu);
17937
17938 /* The die_type call above may have already set the type for this DIE. */
17939 cv_type = get_die_type (die, cu);
17940 if (cv_type)
17941 return cv_type;
17942
17943 cv_type = make_atomic_type (base_type);
17944 return set_die_type (die, cv_type, cu);
17945}
17946
c906108c
SS
17947/* Extract all information from a DW_TAG_string_type DIE and add to
17948 the user defined type vector. It isn't really a user defined type,
17949 but it behaves like one, with other DIE's using an AT_user_def_type
17950 attribute to reference it. */
17951
f792889a 17952static struct type *
e7c27a73 17953read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17954{
5e22e966 17955 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17956 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17957 struct type *type, *range_type, *index_type, *char_type;
17958 struct attribute *attr;
216a7e6b
AB
17959 struct dynamic_prop prop;
17960 bool length_is_constant = true;
17961 LONGEST length;
17962
17963 /* There are a couple of places where bit sizes might be made use of
17964 when parsing a DW_TAG_string_type, however, no producer that we know
17965 of make use of these. Handling bit sizes that are a multiple of the
17966 byte size is easy enough, but what about other bit sizes? Lets deal
17967 with that problem when we have to. Warn about these attributes being
17968 unsupported, then parse the type and ignore them like we always
17969 have. */
17970 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17971 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17972 {
17973 static bool warning_printed = false;
17974 if (!warning_printed)
17975 {
17976 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17977 "currently supported on DW_TAG_string_type."));
17978 warning_printed = true;
17979 }
17980 }
c906108c 17981
e142c38c 17982 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17983 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17984 {
17985 /* The string length describes the location at which the length of
17986 the string can be found. The size of the length field can be
17987 specified with one of the attributes below. */
17988 struct type *prop_type;
17989 struct attribute *len
17990 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17991 if (len == nullptr)
17992 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17993 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17994 {
17995 /* Pass 0 as the default as we know this attribute is constant
17996 and the default value will not be returned. */
0826b30a 17997 LONGEST sz = len->constant_value (0);
293e7e51 17998 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17999 }
18000 else
18001 {
18002 /* If the size is not specified then we assume it is the size of
18003 an address on this target. */
293e7e51 18004 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
18005 }
18006
18007 /* Convert the attribute into a dynamic property. */
18008 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
18009 length = 1;
18010 else
18011 length_is_constant = false;
18012 }
18013 else if (attr != nullptr)
18014 {
18015 /* This DW_AT_string_length just contains the length with no
18016 indirection. There's no need to create a dynamic property in this
18017 case. Pass 0 for the default value as we know it will not be
18018 returned in this case. */
0826b30a 18019 length = attr->constant_value (0);
216a7e6b
AB
18020 }
18021 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 18022 {
216a7e6b 18023 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 18024 length = attr->constant_value (1);
c906108c
SS
18025 }
18026 else
18027 {
216a7e6b
AB
18028 /* Use 1 as a fallback length if we have nothing else. */
18029 length = 1;
c906108c 18030 }
6ccb9162 18031
46bf5051 18032 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
18033 if (length_is_constant)
18034 range_type = create_static_range_type (NULL, index_type, 1, length);
18035 else
18036 {
18037 struct dynamic_prop low_bound;
18038
8c2e4e06 18039 low_bound.set_const_val (1);
216a7e6b
AB
18040 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
18041 }
3b7538c0
UW
18042 char_type = language_string_char_type (cu->language_defn, gdbarch);
18043 type = create_string_type (NULL, char_type, range_type);
6ccb9162 18044
f792889a 18045 return set_die_type (die, type, cu);
c906108c
SS
18046}
18047
4d804846
JB
18048/* Assuming that DIE corresponds to a function, returns nonzero
18049 if the function is prototyped. */
18050
18051static int
18052prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
18053{
18054 struct attribute *attr;
18055
18056 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c45bc3f8 18057 if (attr && attr->as_boolean ())
4d804846
JB
18058 return 1;
18059
18060 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 18061 is only meaningful for C, but the concept also extends to other
4d804846
JB
18062 languages that allow unprototyped functions (Eg: Objective C).
18063 For all other languages, assume that functions are always
18064 prototyped. */
18065 if (cu->language != language_c
18066 && cu->language != language_objc
18067 && cu->language != language_opencl)
18068 return 1;
18069
18070 /* RealView does not emit DW_AT_prototyped. We can not distinguish
18071 prototyped and unprototyped functions; default to prototyped,
18072 since that is more common in modern code (and RealView warns
18073 about unprototyped functions). */
18074 if (producer_is_realview (cu->producer))
18075 return 1;
18076
18077 return 0;
18078}
18079
c906108c
SS
18080/* Handle DIES due to C code like:
18081
18082 struct foo
c5aa993b
JM
18083 {
18084 int (*funcp)(int a, long l);
18085 int b;
18086 };
c906108c 18087
0963b4bd 18088 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 18089
f792889a 18090static struct type *
e7c27a73 18091read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18092{
5e22e966 18093 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
18094 struct type *type; /* Type that this function returns. */
18095 struct type *ftype; /* Function that returns above type. */
c906108c
SS
18096 struct attribute *attr;
18097
e7c27a73 18098 type = die_type (die, cu);
7e314c57
JK
18099
18100 /* The die_type call above may have already set the type for this DIE. */
18101 ftype = get_die_type (die, cu);
18102 if (ftype)
18103 return ftype;
18104
0c8b41f1 18105 ftype = lookup_function_type (type);
c906108c 18106
4d804846 18107 if (prototyped_function_p (die, cu))
27e69b7a 18108 ftype->set_is_prototyped (true);
c906108c 18109
c055b101
CV
18110 /* Store the calling convention in the type if it's available in
18111 the subroutine die. Otherwise set the calling convention to
18112 the default value DW_CC_normal. */
18113 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf 18114 if (attr != nullptr
529908cb 18115 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
d0922fcf 18116 TYPE_CALLING_CONVENTION (ftype)
529908cb 18117 = (enum dwarf_calling_convention) attr->constant_value (0);
54fcddd0
UW
18118 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
18119 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
18120 else
18121 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 18122
743649fd
MW
18123 /* Record whether the function returns normally to its caller or not
18124 if the DWARF producer set that information. */
18125 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
c45bc3f8 18126 if (attr && attr->as_boolean ())
743649fd
MW
18127 TYPE_NO_RETURN (ftype) = 1;
18128
76c10ea2
GM
18129 /* We need to add the subroutine type to the die immediately so
18130 we don't infinitely recurse when dealing with parameters
0963b4bd 18131 declared as the same subroutine type. */
76c10ea2 18132 set_die_type (die, ftype, cu);
6e70227d 18133
639d11d3 18134 if (die->child != NULL)
c906108c 18135 {
bb5ed363 18136 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 18137 struct die_info *child_die;
8072405b 18138 int nparams, iparams;
c906108c
SS
18139
18140 /* Count the number of parameters.
dda83cd7
SM
18141 FIXME: GDB currently ignores vararg functions, but knows about
18142 vararg member functions. */
8072405b 18143 nparams = 0;
639d11d3 18144 child_die = die->child;
c906108c
SS
18145 while (child_die && child_die->tag)
18146 {
18147 if (child_die->tag == DW_TAG_formal_parameter)
18148 nparams++;
18149 else if (child_die->tag == DW_TAG_unspecified_parameters)
1d6286ed
SM
18150 ftype->set_has_varargs (true);
18151
436c571c 18152 child_die = child_die->sibling;
c906108c
SS
18153 }
18154
18155 /* Allocate storage for parameters and fill them in. */
5e33d5f4 18156 ftype->set_num_fields (nparams);
3cabb6b0
SM
18157 ftype->set_fields
18158 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 18159
8072405b
JK
18160 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18161 even if we error out during the parameters reading below. */
18162 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 18163 ftype->field (iparams).set_type (void_type);
8072405b
JK
18164
18165 iparams = 0;
639d11d3 18166 child_die = die->child;
c906108c
SS
18167 while (child_die && child_die->tag)
18168 {
18169 if (child_die->tag == DW_TAG_formal_parameter)
18170 {
3ce3b1ba
PA
18171 struct type *arg_type;
18172
18173 /* DWARF version 2 has no clean way to discern C++
18174 static and non-static member functions. G++ helps
18175 GDB by marking the first parameter for non-static
18176 member functions (which is the this pointer) as
18177 artificial. We pass this information to
18178 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18179
18180 DWARF version 3 added DW_AT_object_pointer, which GCC
18181 4.5 does not yet generate. */
e142c38c 18182 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 18183 if (attr != nullptr)
c45bc3f8 18184 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
c906108c 18185 else
9c37b5ae 18186 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
18187 arg_type = die_type (child_die, cu);
18188
18189 /* RealView does not mark THIS as const, which the testsuite
18190 expects. GCC marks THIS as const in method definitions,
18191 but not in the class specifications (GCC PR 43053). */
18192 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18193 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18194 {
18195 int is_this = 0;
18196 struct dwarf2_cu *arg_cu = cu;
18197 const char *name = dwarf2_name (child_die, cu);
18198
18199 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 18200 if (attr != nullptr)
3ce3b1ba
PA
18201 {
18202 /* If the compiler emits this, use it. */
18203 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18204 is_this = 1;
18205 }
18206 else if (name && strcmp (name, "this") == 0)
18207 /* Function definitions will have the argument names. */
18208 is_this = 1;
18209 else if (name == NULL && iparams == 0)
18210 /* Declarations may not have the names, so like
18211 elsewhere in GDB, assume an artificial first
18212 argument is "this". */
18213 is_this = 1;
18214
18215 if (is_this)
18216 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18217 arg_type, 0);
18218 }
18219
5d14b6e5 18220 ftype->field (iparams).set_type (arg_type);
c906108c
SS
18221 iparams++;
18222 }
436c571c 18223 child_die = child_die->sibling;
c906108c
SS
18224 }
18225 }
18226
76c10ea2 18227 return ftype;
c906108c
SS
18228}
18229
f792889a 18230static struct type *
e7c27a73 18231read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18232{
5e22e966 18233 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 18234 const char *name = NULL;
3c8e0968 18235 struct type *this_type, *target_type;
c906108c 18236
94af9270 18237 name = dwarf2_full_name (NULL, die, cu);
19f392bc 18238 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
8f53807e 18239 this_type->set_target_is_stub (true);
f792889a 18240 set_die_type (die, this_type, cu);
3c8e0968
DE
18241 target_type = die_type (die, cu);
18242 if (target_type != this_type)
18243 TYPE_TARGET_TYPE (this_type) = target_type;
18244 else
18245 {
18246 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18247 spec and cause infinite loops in GDB. */
b98664d3 18248 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
18249 "- DIE at %s [in module %s]"),
18250 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
18251 TYPE_TARGET_TYPE (this_type) = NULL;
18252 }
e4003a34
TV
18253 if (name == NULL)
18254 {
18255 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18256 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18257 Handle these by just returning the target type, rather than
18258 constructing an anonymous typedef type and trying to handle this
18259 elsewhere. */
18260 set_die_type (die, target_type, cu);
18261 return target_type;
18262 }
f792889a 18263 return this_type;
c906108c
SS
18264}
18265
a625a8c9
TT
18266/* Helper for get_dwarf2_rational_constant that computes the value of
18267 a given gmp_mpz given an attribute. */
18268
18269static void
18270get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
18271{
18272 /* GCC will sometimes emit a 16-byte constant value as a DWARF
18273 location expression that pushes an implicit value. */
18274 if (attr->form == DW_FORM_exprloc)
18275 {
18276 dwarf_block *blk = attr->as_block ();
18277 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18278 {
18279 uint64_t len;
18280 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18281 blk->data + blk->size,
18282 &len);
18283 if (ptr - blk->data + len <= blk->size)
18284 {
18285 mpz_import (value->val, len,
18286 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18287 1, 0, 0, ptr);
18288 return;
18289 }
18290 }
18291
18292 /* On failure set it to 1. */
18293 *value = gdb_mpz (1);
18294 }
18295 else if (attr->form_is_block ())
18296 {
18297 dwarf_block *blk = attr->as_block ();
18298 mpz_import (value->val, blk->size,
18299 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18300 1, 0, 0, blk->data);
18301 }
18302 else
18303 *value = gdb_mpz (attr->constant_value (1));
18304}
18305
09584414
JB
18306/* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18307 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18308
18309 If the numerator and/or numerator attribute is missing,
18310 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18311 untouched. */
18312
18313static void
18314get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
5cde1d82 18315 gdb_mpz *numerator, gdb_mpz *denominator)
09584414
JB
18316{
18317 struct attribute *num_attr, *denom_attr;
18318
18319 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18320 if (num_attr == nullptr)
18321 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18322 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18323
18324 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18325 if (denom_attr == nullptr)
18326 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18327 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18328
18329 if (num_attr == nullptr || denom_attr == nullptr)
18330 return;
18331
a625a8c9
TT
18332 get_mpz (cu, numerator, num_attr);
18333 get_mpz (cu, denominator, denom_attr);
09584414
JB
18334}
18335
18336/* Same as get_dwarf2_rational_constant, but extracting an unsigned
18337 rational constant, rather than a signed one.
18338
18339 If the rational constant has a negative value, a complaint
18340 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18341
18342static void
18343get_dwarf2_unsigned_rational_constant (struct die_info *die,
18344 struct dwarf2_cu *cu,
5cde1d82
TT
18345 gdb_mpz *numerator,
18346 gdb_mpz *denominator)
09584414 18347{
5cde1d82
TT
18348 gdb_mpz num (1);
18349 gdb_mpz denom (1);
09584414
JB
18350
18351 get_dwarf2_rational_constant (die, cu, &num, &denom);
5cde1d82 18352 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
09584414 18353 {
5cde1d82
TT
18354 mpz_neg (num.val, num.val);
18355 mpz_neg (denom.val, denom.val);
09584414 18356 }
5cde1d82 18357 else if (mpz_sgn (num.val) == -1)
09584414
JB
18358 {
18359 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18360 " in DIE at %s"),
18361 sect_offset_str (die->sect_off));
18362 return;
18363 }
5cde1d82 18364 else if (mpz_sgn (denom.val) == -1)
09584414
JB
18365 {
18366 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18367 " in DIE at %s"),
18368 sect_offset_str (die->sect_off));
18369 return;
18370 }
18371
5cde1d82
TT
18372 *numerator = std::move (num);
18373 *denominator = std::move (denom);
09584414
JB
18374}
18375
18376/* Assuming DIE corresponds to a fixed point type, finish the creation
2a12c336 18377 of the corresponding TYPE by setting its type-specific data.
09584414
JB
18378 CU is the DIE's CU. */
18379
18380static void
18381finish_fixed_point_type (struct type *type, struct die_info *die,
18382 struct dwarf2_cu *cu)
18383{
18384 struct attribute *attr;
09584414
JB
18385
18386 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18387 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18388
18389 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18390 if (!attr)
18391 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18392 if (!attr)
18393 attr = dwarf2_attr (die, DW_AT_small, cu);
18394
5cde1d82
TT
18395 /* Numerator and denominator of our fixed-point type's scaling factor.
18396 The default is a scaling factor of 1, which we use as a fallback
18397 when we are not able to decode it (problem with the debugging info,
18398 unsupported forms, bug in GDB, etc...). Using that as the default
18399 allows us to at least print the unscaled value, which might still
18400 be useful to a user. */
18401 gdb_mpz scale_num (1);
18402 gdb_mpz scale_denom (1);
18403
09584414
JB
18404 if (attr == nullptr)
18405 {
18406 /* Scaling factor not found. Assume a scaling factor of 1,
18407 and hope for the best. At least the user will be able to see
18408 the encoded value. */
18409 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18410 sect_offset_str (die->sect_off));
18411 }
18412 else if (attr->name == DW_AT_binary_scale)
18413 {
18414 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18415 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18416
5cde1d82 18417 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
09584414
JB
18418 }
18419 else if (attr->name == DW_AT_decimal_scale)
18420 {
18421 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18422 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18423
5cde1d82 18424 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
09584414
JB
18425 }
18426 else if (attr->name == DW_AT_small)
18427 {
18428 struct die_info *scale_die;
18429 struct dwarf2_cu *scale_cu = cu;
18430
18431 scale_die = follow_die_ref (die, attr, &scale_cu);
18432 if (scale_die->tag == DW_TAG_constant)
18433 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18434 &scale_num, &scale_denom);
18435 else
18436 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18437 " (DIE at %s)"),
18438 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18439 }
18440 else
18441 {
18442 complaint (_("unsupported scale attribute %s for fixed-point type"
18443 " (DIE at %s)"),
18444 dwarf_attr_name (attr->name),
18445 sect_offset_str (die->sect_off));
18446 }
18447
2a12c336 18448 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
5cde1d82
TT
18449 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18450 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
09584414
JB
18451 mpq_canonicalize (scaling_factor.val);
18452}
18453
9b790ce7
UW
18454/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18455 (which may be different from NAME) to the architecture back-end to allow
18456 it to guess the correct format if necessary. */
18457
18458static struct type *
18459dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 18460 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 18461{
08feed99 18462 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
18463 const struct floatformat **format;
18464 struct type *type;
18465
18466 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18467 if (format)
103a685e 18468 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 18469 else
77b7c781 18470 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
18471
18472 return type;
18473}
18474
eb77c9df
AB
18475/* Allocate an integer type of size BITS and name NAME. */
18476
18477static struct type *
18478dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18479 int bits, int unsigned_p, const char *name)
18480{
18481 struct type *type;
18482
18483 /* Versions of Intel's C Compiler generate an integer type called "void"
18484 instead of using DW_TAG_unspecified_type. This has been seen on
18485 at least versions 14, 17, and 18. */
35ee2dc2
AB
18486 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18487 && strcmp (name, "void") == 0)
eb77c9df
AB
18488 type = objfile_type (objfile)->builtin_void;
18489 else
18490 type = init_integer_type (objfile, bits, unsigned_p, name);
18491
18492 return type;
18493}
18494
09584414
JB
18495/* Return true if DIE has a DW_AT_small attribute whose value is
18496 a constant rational, where both the numerator and denominator
18497 are equal to zero.
18498
18499 CU is the DIE's Compilation Unit. */
18500
18501static bool
18502has_zero_over_zero_small_attribute (struct die_info *die,
18503 struct dwarf2_cu *cu)
18504{
18505 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18506 if (attr == nullptr)
18507 return false;
18508
18509 struct dwarf2_cu *scale_cu = cu;
18510 struct die_info *scale_die
18511 = follow_die_ref (die, attr, &scale_cu);
18512
18513 if (scale_die->tag != DW_TAG_constant)
18514 return false;
18515
5cde1d82 18516 gdb_mpz num (1), denom (1);
09584414 18517 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
5cde1d82 18518 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
09584414
JB
18519}
18520
8bdc1658
AB
18521/* Initialise and return a floating point type of size BITS suitable for
18522 use as a component of a complex number. The NAME_HINT is passed through
18523 when initialising the floating point type and is the name of the complex
18524 type.
18525
18526 As DWARF doesn't currently provide an explicit name for the components
18527 of a complex number, but it can be helpful to have these components
18528 named, we try to select a suitable name based on the size of the
18529 component. */
18530static struct type *
18531dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18532 struct objfile *objfile,
103a685e
TT
18533 int bits, const char *name_hint,
18534 enum bfd_endian byte_order)
8bdc1658 18535{
08feed99 18536 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
18537 struct type *tt = nullptr;
18538
35add35e
AB
18539 /* Try to find a suitable floating point builtin type of size BITS.
18540 We're going to use the name of this type as the name for the complex
18541 target type that we are about to create. */
1db455a7 18542 switch (cu->language)
8bdc1658 18543 {
1db455a7
AB
18544 case language_fortran:
18545 switch (bits)
18546 {
18547 case 32:
18548 tt = builtin_f_type (gdbarch)->builtin_real;
18549 break;
18550 case 64:
18551 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18552 break;
18553 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18554 case 128:
18555 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18556 break;
18557 }
8bdc1658 18558 break;
1db455a7
AB
18559 default:
18560 switch (bits)
18561 {
18562 case 32:
18563 tt = builtin_type (gdbarch)->builtin_float;
18564 break;
18565 case 64:
18566 tt = builtin_type (gdbarch)->builtin_double;
18567 break;
18568 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18569 case 128:
18570 tt = builtin_type (gdbarch)->builtin_long_double;
18571 break;
18572 }
8bdc1658
AB
18573 break;
18574 }
18575
35add35e
AB
18576 /* If the type we found doesn't match the size we were looking for, then
18577 pretend we didn't find a type at all, the complex target type we
18578 create will then be nameless. */
a12e5744 18579 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
18580 tt = nullptr;
18581
7d93a1e0 18582 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 18583 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
18584}
18585
c906108c
SS
18586/* Find a representation of a given base type and install
18587 it in the TYPE field of the die. */
18588
f792889a 18589static struct type *
e7c27a73 18590read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18591{
5e22e966 18592 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
18593 struct type *type;
18594 struct attribute *attr;
19f392bc 18595 int encoding = 0, bits = 0;
15d034d0 18596 const char *name;
34877895 18597 gdbarch *arch;
c906108c 18598
e142c38c 18599 attr = dwarf2_attr (die, DW_AT_encoding, cu);
529908cb
TT
18600 if (attr != nullptr && attr->form_is_constant ())
18601 encoding = attr->constant_value (0);
e142c38c 18602 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18603 if (attr != nullptr)
529908cb 18604 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
39cbfefa 18605 name = dwarf2_name (die, cu);
6ccb9162 18606 if (!name)
34877895 18607 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 18608
08feed99 18609 arch = objfile->arch ();
103a685e
TT
18610 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18611
34877895 18612 attr = dwarf2_attr (die, DW_AT_endianity, cu);
529908cb 18613 if (attr != nullptr && attr->form_is_constant ())
103a685e 18614 {
529908cb 18615 int endianity = attr->constant_value (0);
103a685e
TT
18616
18617 switch (endianity)
18618 {
18619 case DW_END_big:
18620 byte_order = BFD_ENDIAN_BIG;
18621 break;
18622 case DW_END_little:
18623 byte_order = BFD_ENDIAN_LITTLE;
18624 break;
18625 default:
18626 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18627 break;
18628 }
18629 }
6ccb9162 18630
09584414
JB
18631 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18632 && cu->language == language_ada
18633 && has_zero_over_zero_small_attribute (die, cu))
18634 {
18635 /* brobecker/2018-02-24: This is a fixed point type for which
18636 the scaling factor is represented as fraction whose value
18637 does not make sense (zero divided by zero), so we should
18638 normally never see these. However, there is a small category
18639 of fixed point types for which GNAT is unable to provide
18640 the scaling factor via the standard DWARF mechanisms, and
18641 for which the info is provided via the GNAT encodings instead.
18642 This is likely what this DIE is about.
18643
18644 Ideally, GNAT should be declaring this type the same way
18645 it declares other fixed point types when using the legacy
18646 GNAT encoding, which is to use a simple signed or unsigned
18647 base type. A report to the GNAT team has been created to
18648 look into it. In the meantime, pretend this type is a simple
18649 signed or unsigned integral, rather than a fixed point type,
18650 to avoid any confusion later on as to how to process this type. */
18651 encoding = (encoding == DW_ATE_signed_fixed
18652 ? DW_ATE_signed
18653 : DW_ATE_unsigned);
18654 }
18655
6ccb9162 18656 switch (encoding)
c906108c 18657 {
6ccb9162
UW
18658 case DW_ATE_address:
18659 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 18660 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 18661 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
18662 break;
18663 case DW_ATE_boolean:
19f392bc 18664 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
18665 break;
18666 case DW_ATE_complex_float:
103a685e
TT
18667 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18668 byte_order);
78134374 18669 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
18670 {
18671 if (name == nullptr)
18672 {
18673 struct obstack *obstack
5e22e966 18674 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 18675 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
18676 nullptr);
18677 }
18678 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18679 }
18680 else
18681 type = init_complex_type (name, type);
6ccb9162
UW
18682 break;
18683 case DW_ATE_decimal_float:
19f392bc 18684 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
18685 break;
18686 case DW_ATE_float:
103a685e 18687 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
18688 break;
18689 case DW_ATE_signed:
eb77c9df 18690 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18691 break;
18692 case DW_ATE_unsigned:
3b2b8fea
TT
18693 if (cu->language == language_fortran
18694 && name
61012eef 18695 && startswith (name, "character("))
19f392bc
UW
18696 type = init_character_type (objfile, bits, 1, name);
18697 else
eb77c9df 18698 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
18699 break;
18700 case DW_ATE_signed_char:
6e70227d 18701 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
18702 || cu->language == language_pascal
18703 || cu->language == language_fortran)
19f392bc
UW
18704 type = init_character_type (objfile, bits, 0, name);
18705 else
eb77c9df 18706 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18707 break;
18708 case DW_ATE_unsigned_char:
868a0084 18709 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 18710 || cu->language == language_pascal
c44af4eb
TT
18711 || cu->language == language_fortran
18712 || cu->language == language_rust)
19f392bc
UW
18713 type = init_character_type (objfile, bits, 1, name);
18714 else
eb77c9df 18715 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 18716 break;
75079b2b 18717 case DW_ATE_UTF:
53e710ac 18718 {
53e710ac
PA
18719 if (bits == 16)
18720 type = builtin_type (arch)->builtin_char16;
18721 else if (bits == 32)
18722 type = builtin_type (arch)->builtin_char32;
18723 else
18724 {
b98664d3 18725 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 18726 bits);
eb77c9df 18727 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
18728 }
18729 return set_die_type (die, type, cu);
18730 }
75079b2b 18731 break;
09584414
JB
18732 case DW_ATE_signed_fixed:
18733 type = init_fixed_point_type (objfile, bits, 0, name);
18734 finish_fixed_point_type (type, die, cu);
18735 break;
18736 case DW_ATE_unsigned_fixed:
18737 type = init_fixed_point_type (objfile, bits, 1, name);
18738 finish_fixed_point_type (type, die, cu);
18739 break;
75079b2b 18740
6ccb9162 18741 default:
b98664d3 18742 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 18743 dwarf_type_encoding_name (encoding));
77b7c781 18744 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18745 break;
c906108c 18746 }
6ccb9162 18747
0114d602 18748 if (name && strcmp (name, "char") == 0)
15152a54 18749 type->set_has_no_signedness (true);
0114d602 18750
2b4424c3
TT
18751 maybe_set_alignment (cu, die, type);
18752
db558e34 18753 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
34877895 18754
20a5fcbd
TT
18755 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18756 {
18757 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
529908cb 18758 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
20a5fcbd 18759 {
529908cb 18760 unsigned real_bit_size = attr->as_unsigned ();
20a5fcbd
TT
18761 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18762 /* Only use the attributes if they make sense together. */
18763 if (attr == nullptr
529908cb
TT
18764 || (attr->as_unsigned () + real_bit_size
18765 <= 8 * TYPE_LENGTH (type)))
20a5fcbd
TT
18766 {
18767 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18768 = real_bit_size;
18769 if (attr != nullptr)
18770 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
529908cb 18771 = attr->as_unsigned ();
20a5fcbd
TT
18772 }
18773 }
18774 }
18775
f792889a 18776 return set_die_type (die, type, cu);
c906108c
SS
18777}
18778
80180f79
SA
18779/* Parse dwarf attribute if it's a block, reference or constant and put the
18780 resulting value of the attribute into struct bound_prop.
18781 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18782
18783static int
18784attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
18785 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18786 struct type *default_type)
80180f79
SA
18787{
18788 struct dwarf2_property_baton *baton;
5e22e966 18789 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
18790 struct objfile *objfile = per_objfile->objfile;
18791 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 18792
9a49df9d
AB
18793 gdb_assert (default_type != NULL);
18794
80180f79
SA
18795 if (attr == NULL || prop == NULL)
18796 return 0;
18797
4fc6c0d5 18798 if (attr->form_is_block ())
80180f79 18799 {
8d749320 18800 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18801 baton->property_type = default_type;
80180f79 18802 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18803 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18804
18805 struct dwarf_block *block = attr->as_block ();
18806 baton->locexpr.size = block->size;
18807 baton->locexpr.data = block->data;
216a7e6b
AB
18808 switch (attr->name)
18809 {
18810 case DW_AT_string_length:
18811 baton->locexpr.is_reference = true;
18812 break;
18813 default:
18814 baton->locexpr.is_reference = false;
18815 break;
18816 }
8c2e4e06
SM
18817
18818 prop->set_locexpr (baton);
18819 gdb_assert (prop->baton () != NULL);
80180f79 18820 }
cd6c91b4 18821 else if (attr->form_is_ref ())
80180f79
SA
18822 {
18823 struct dwarf2_cu *target_cu = cu;
18824 struct die_info *target_die;
18825 struct attribute *target_attr;
18826
18827 target_die = follow_die_ref (die, attr, &target_cu);
18828 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18829 if (target_attr == NULL)
18830 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18831 target_cu);
80180f79
SA
18832 if (target_attr == NULL)
18833 return 0;
18834
df25ebbd 18835 switch (target_attr->name)
80180f79 18836 {
df25ebbd 18837 case DW_AT_location:
cd6c91b4 18838 if (target_attr->form_is_section_offset ())
df25ebbd 18839 {
8d749320 18840 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18841 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18842 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
8c2e4e06
SM
18843 prop->set_loclist (baton);
18844 gdb_assert (prop->baton () != NULL);
df25ebbd 18845 }
4fc6c0d5 18846 else if (target_attr->form_is_block ())
df25ebbd 18847 {
8d749320 18848 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18849 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18850 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18851 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18852 struct dwarf_block *block = target_attr->as_block ();
18853 baton->locexpr.size = block->size;
18854 baton->locexpr.data = block->data;
9a49df9d 18855 baton->locexpr.is_reference = true;
8c2e4e06
SM
18856 prop->set_locexpr (baton);
18857 gdb_assert (prop->baton () != NULL);
df25ebbd
JB
18858 }
18859 else
18860 {
18861 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18862 "dynamic property");
18863 return 0;
18864 }
18865 break;
18866 case DW_AT_data_member_location:
18867 {
18868 LONGEST offset;
18869
18870 if (!handle_data_member_location (target_die, target_cu,
18871 &offset))
18872 return 0;
18873
8d749320 18874 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18875 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18876 target_cu);
df25ebbd
JB
18877 baton->offset_info.offset = offset;
18878 baton->offset_info.type = die_type (target_die, target_cu);
8c2e4e06 18879 prop->set_addr_offset (baton);
df25ebbd
JB
18880 break;
18881 }
80180f79
SA
18882 }
18883 }
cd6c91b4 18884 else if (attr->form_is_constant ())
8c2e4e06 18885 prop->set_const_val (attr->constant_value (0));
80180f79
SA
18886 else
18887 {
18888 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18889 dwarf2_name (die, cu));
18890 return 0;
18891 }
18892
18893 return 1;
18894}
18895
09ba997f 18896/* See read.h. */
9a49df9d 18897
09ba997f 18898struct type *
293e7e51 18899dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 18900{
9a49df9d
AB
18901 struct type *int_type;
18902
18903 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18904#define TRY_TYPE(F) \
18905 int_type = (unsigned_p \
18906 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18907 : objfile_type (objfile)->builtin_ ## F); \
18908 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18909 return int_type
18910
18911 TRY_TYPE (char);
18912 TRY_TYPE (short);
18913 TRY_TYPE (int);
18914 TRY_TYPE (long);
18915 TRY_TYPE (long_long);
18916
18917#undef TRY_TYPE
18918
18919 gdb_assert_not_reached ("unable to find suitable integer type");
18920}
18921
09ba997f 18922/* See read.h. */
11a8b164 18923
09ba997f 18924struct type *
293e7e51 18925dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 18926{
293e7e51
SM
18927 int addr_size = this->per_cu->addr_size ();
18928 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
18929}
18930
b86352cf
AB
18931/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18932 present (which is valid) then compute the default type based on the
18933 compilation units address size. */
18934
18935static struct type *
18936read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18937{
18938 struct type *index_type = die_type (die, cu);
18939
18940 /* Dwarf-2 specifications explicitly allows to create subrange types
18941 without specifying a base type.
18942 In that case, the base type must be set to the type of
18943 the lower bound, upper bound or count, in that order, if any of these
18944 three attributes references an object that has a type.
18945 If no base type is found, the Dwarf-2 specifications say that
18946 a signed integer type of size equal to the size of an address should
18947 be used.
18948 For the following C code: `extern char gdb_int [];'
18949 GCC produces an empty range DIE.
18950 FIXME: muller/2010-05-28: Possible references to object for low bound,
18951 high bound or count are not yet handled by this code. */
78134374 18952 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 18953 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
18954
18955 return index_type;
18956}
18957
a02abb62
JB
18958/* Read the given DW_AT_subrange DIE. */
18959
f792889a 18960static struct type *
a02abb62
JB
18961read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18962{
4c9ad8c2 18963 struct type *base_type, *orig_base_type;
a02abb62
JB
18964 struct type *range_type;
18965 struct attribute *attr;
729efb13 18966 struct dynamic_prop low, high;
4fae6e18 18967 int low_default_is_valid;
c451ebe5 18968 int high_bound_is_count = 0;
15d034d0 18969 const char *name;
d359392f 18970 ULONGEST negative_mask;
e77813c8 18971
b86352cf
AB
18972 orig_base_type = read_subrange_index_type (die, cu);
18973
4c9ad8c2
TT
18974 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18975 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18976 creating the range type, but we use the result of check_typedef
18977 when examining properties of the type. */
18978 base_type = check_typedef (orig_base_type);
a02abb62 18979
7e314c57
JK
18980 /* The die_type call above may have already set the type for this DIE. */
18981 range_type = get_die_type (die, cu);
18982 if (range_type)
18983 return range_type;
18984
8c2e4e06 18985 high.set_const_val (0);
729efb13 18986
4fae6e18
JK
18987 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18988 omitting DW_AT_lower_bound. */
18989 switch (cu->language)
6e70227d 18990 {
4fae6e18
JK
18991 case language_c:
18992 case language_cplus:
8c2e4e06 18993 low.set_const_val (0);
4fae6e18
JK
18994 low_default_is_valid = 1;
18995 break;
18996 case language_fortran:
8c2e4e06 18997 low.set_const_val (1);
4fae6e18
JK
18998 low_default_is_valid = 1;
18999 break;
19000 case language_d:
4fae6e18 19001 case language_objc:
c44af4eb 19002 case language_rust:
8c2e4e06 19003 low.set_const_val (0);
4fae6e18
JK
19004 low_default_is_valid = (cu->header.version >= 4);
19005 break;
19006 case language_ada:
19007 case language_m2:
19008 case language_pascal:
8c2e4e06 19009 low.set_const_val (1);
4fae6e18
JK
19010 low_default_is_valid = (cu->header.version >= 4);
19011 break;
19012 default:
8c2e4e06 19013 low.set_const_val (0);
4fae6e18
JK
19014 low_default_is_valid = 0;
19015 break;
a02abb62
JB
19016 }
19017
e142c38c 19018 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 19019 if (attr != nullptr)
9a49df9d 19020 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 19021 else if (!low_default_is_valid)
b98664d3 19022 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
19023 "- DIE at %s [in module %s]"),
19024 sect_offset_str (die->sect_off),
5e22e966 19025 objfile_name (cu->per_objfile->objfile));
a02abb62 19026
506f5c41
TV
19027 struct attribute *attr_ub, *attr_count;
19028 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 19029 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 19030 {
506f5c41 19031 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 19032 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 19033 {
c451ebe5 19034 /* If bounds are constant do the final calculation here. */
8c2e4e06
SM
19035 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
19036 high.set_const_val (low.const_val () + high.const_val () - 1);
c451ebe5
SA
19037 else
19038 high_bound_is_count = 1;
c2ff108b 19039 }
506f5c41
TV
19040 else
19041 {
19042 if (attr_ub != NULL)
19043 complaint (_("Unresolved DW_AT_upper_bound "
19044 "- DIE at %s [in module %s]"),
19045 sect_offset_str (die->sect_off),
5e22e966 19046 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
19047 if (attr_count != NULL)
19048 complaint (_("Unresolved DW_AT_count "
19049 "- DIE at %s [in module %s]"),
19050 sect_offset_str (die->sect_off),
5e22e966 19051 objfile_name (cu->per_objfile->objfile));
506f5c41 19052 }
e77813c8 19053 }
a02abb62 19054
4e962e74
TT
19055 LONGEST bias = 0;
19056 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 19057 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 19058 bias = bias_attr->constant_value (0);
4e962e74 19059
dbb9c2b1
JB
19060 /* Normally, the DWARF producers are expected to use a signed
19061 constant form (Eg. DW_FORM_sdata) to express negative bounds.
19062 But this is unfortunately not always the case, as witnessed
19063 with GCC, for instance, where the ambiguous DW_FORM_dataN form
19064 is used instead. To work around that ambiguity, we treat
19065 the bounds as signed, and thus sign-extend their values, when
19066 the base type is signed. */
6e70227d 19067 negative_mask =
d359392f 19068 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
8c2e4e06 19069 if (low.kind () == PROP_CONST
c6d940a9 19070 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
8c2e4e06
SM
19071 low.set_const_val (low.const_val () | negative_mask);
19072 if (high.kind () == PROP_CONST
c6d940a9 19073 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
8c2e4e06 19074 high.set_const_val (high.const_val () | negative_mask);
43bbcdc2 19075
5bbd8269
AB
19076 /* Check for bit and byte strides. */
19077 struct dynamic_prop byte_stride_prop;
19078 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
19079 if (attr_byte_stride != nullptr)
19080 {
293e7e51 19081 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
19082 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
19083 prop_type);
19084 }
19085
19086 struct dynamic_prop bit_stride_prop;
19087 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
19088 if (attr_bit_stride != nullptr)
19089 {
19090 /* It only makes sense to have either a bit or byte stride. */
19091 if (attr_byte_stride != nullptr)
19092 {
19093 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19094 "- DIE at %s [in module %s]"),
19095 sect_offset_str (die->sect_off),
5e22e966 19096 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
19097 attr_bit_stride = nullptr;
19098 }
19099 else
19100 {
293e7e51 19101 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
19102 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19103 prop_type);
19104 }
19105 }
19106
19107 if (attr_byte_stride != nullptr
19108 || attr_bit_stride != nullptr)
19109 {
19110 bool byte_stride_p = (attr_byte_stride != nullptr);
19111 struct dynamic_prop *stride
19112 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19113
19114 range_type
19115 = create_range_type_with_stride (NULL, orig_base_type, &low,
19116 &high, bias, stride, byte_stride_p);
19117 }
19118 else
19119 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 19120
c451ebe5 19121 if (high_bound_is_count)
599088e3 19122 range_type->bounds ()->flag_upper_bound_is_count = 1;
c451ebe5 19123
c2ff108b
JK
19124 /* Ada expects an empty array on no boundary attributes. */
19125 if (attr == NULL && cu->language != language_ada)
8c2e4e06 19126 range_type->bounds ()->high.set_undefined ();
c2ff108b 19127
39cbfefa
DJ
19128 name = dwarf2_name (die, cu);
19129 if (name)
d0e39ea2 19130 range_type->set_name (name);
6e70227d 19131
e142c38c 19132 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 19133 if (attr != nullptr)
529908cb 19134 TYPE_LENGTH (range_type) = attr->constant_value (0);
a02abb62 19135
2b4424c3
TT
19136 maybe_set_alignment (cu, die, range_type);
19137
7e314c57
JK
19138 set_die_type (die, range_type, cu);
19139
19140 /* set_die_type should be already done. */
b4ba55a1
JB
19141 set_descriptive_type (range_type, die, cu);
19142
7e314c57 19143 return range_type;
a02abb62 19144}
6e70227d 19145
f792889a 19146static struct type *
81a17f79
JB
19147read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19148{
19149 struct type *type;
81a17f79 19150
5e22e966 19151 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 19152 type->set_name (dwarf2_name (die, cu));
81a17f79 19153
74a2f8ff 19154 /* In Ada, an unspecified type is typically used when the description
85102364 19155 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
19156 such a type, we treat it as a stub, and try to resolve it later on,
19157 when needed. */
19158 if (cu->language == language_ada)
b4b73759 19159 type->set_is_stub (true);
74a2f8ff 19160
f792889a 19161 return set_die_type (die, type, cu);
81a17f79 19162}
a02abb62 19163
639d11d3
DC
19164/* Read a single die and all its descendents. Set the die's sibling
19165 field to NULL; set other fields in the die correctly, and set all
19166 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19167 location of the info_ptr after reading all of those dies. PARENT
19168 is the parent of the die in question. */
19169
19170static struct die_info *
dee91e82 19171read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
19172 const gdb_byte *info_ptr,
19173 const gdb_byte **new_info_ptr,
dee91e82 19174 struct die_info *parent)
639d11d3
DC
19175{
19176 struct die_info *die;
d521ce57 19177 const gdb_byte *cur_ptr;
639d11d3 19178
3e225074 19179 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
19180 if (die == NULL)
19181 {
19182 *new_info_ptr = cur_ptr;
19183 return NULL;
19184 }
93311388 19185 store_in_ref_table (die, reader->cu);
639d11d3 19186
3e225074 19187 if (die->has_children)
bf6af496 19188 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
19189 else
19190 {
19191 die->child = NULL;
19192 *new_info_ptr = cur_ptr;
19193 }
19194
19195 die->sibling = NULL;
19196 die->parent = parent;
19197 return die;
19198}
19199
19200/* Read a die, all of its descendents, and all of its siblings; set
19201 all of the fields of all of the dies correctly. Arguments are as
19202 in read_die_and_children. */
19203
19204static struct die_info *
bf6af496 19205read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
19206 const gdb_byte *info_ptr,
19207 const gdb_byte **new_info_ptr,
bf6af496 19208 struct die_info *parent)
639d11d3
DC
19209{
19210 struct die_info *first_die, *last_sibling;
d521ce57 19211 const gdb_byte *cur_ptr;
639d11d3 19212
c906108c 19213 cur_ptr = info_ptr;
639d11d3
DC
19214 first_die = last_sibling = NULL;
19215
19216 while (1)
c906108c 19217 {
639d11d3 19218 struct die_info *die
dee91e82 19219 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 19220
1d325ec1 19221 if (die == NULL)
c906108c 19222 {
639d11d3
DC
19223 *new_info_ptr = cur_ptr;
19224 return first_die;
c906108c 19225 }
1d325ec1
DJ
19226
19227 if (!first_die)
19228 first_die = die;
c906108c 19229 else
1d325ec1
DJ
19230 last_sibling->sibling = die;
19231
19232 last_sibling = die;
c906108c 19233 }
c906108c
SS
19234}
19235
bf6af496
DE
19236/* Read a die, all of its descendents, and all of its siblings; set
19237 all of the fields of all of the dies correctly. Arguments are as
19238 in read_die_and_children.
19239 This the main entry point for reading a DIE and all its children. */
19240
19241static struct die_info *
19242read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
19243 const gdb_byte *info_ptr,
19244 const gdb_byte **new_info_ptr,
bf6af496
DE
19245 struct die_info *parent)
19246{
19247 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19248 new_info_ptr, parent);
19249
b4f54984 19250 if (dwarf_die_debug)
bf6af496
DE
19251 {
19252 fprintf_unfiltered (gdb_stdlog,
19253 "Read die from %s@0x%x of %s:\n",
96b79293 19254 reader->die_section->get_name (),
bf6af496
DE
19255 (unsigned) (info_ptr - reader->die_section->buffer),
19256 bfd_get_filename (reader->abfd));
b4f54984 19257 dump_die (die, dwarf_die_debug);
bf6af496
DE
19258 }
19259
19260 return die;
19261}
19262
3019eac3
DE
19263/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19264 attributes.
19265 The caller is responsible for filling in the extra attributes
19266 and updating (*DIEP)->num_attrs.
19267 Set DIEP to point to a newly allocated die with its information,
3e225074 19268 except for its child, sibling, and parent fields. */
93311388 19269
d521ce57 19270static const gdb_byte *
3019eac3 19271read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 19272 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 19273 int num_extra_attrs)
93311388 19274{
b64f50a1 19275 unsigned int abbrev_number, bytes_read, i;
93311388
DE
19276 struct abbrev_info *abbrev;
19277 struct die_info *die;
19278 struct dwarf2_cu *cu = reader->cu;
19279 bfd *abfd = reader->abfd;
19280
9c541725 19281 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
19282 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19283 info_ptr += bytes_read;
19284 if (!abbrev_number)
19285 {
19286 *diep = NULL;
93311388
DE
19287 return info_ptr;
19288 }
19289
685af9cd 19290 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 19291 if (!abbrev)
348e048f
DE
19292 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19293 abbrev_number,
19294 bfd_get_filename (abfd));
19295
3019eac3 19296 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 19297 die->sect_off = sect_off;
93311388
DE
19298 die->tag = abbrev->tag;
19299 die->abbrev = abbrev_number;
3e225074 19300 die->has_children = abbrev->has_children;
93311388 19301
3019eac3
DE
19302 /* Make the result usable.
19303 The caller needs to update num_attrs after adding the extra
19304 attributes. */
93311388
DE
19305 die->num_attrs = abbrev->num_attrs;
19306
7a5f294d 19307 bool any_need_reprocess = false;
93311388 19308 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e 19309 {
7a5f294d
TT
19310 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19311 info_ptr);
19312 if (die->attrs[i].requires_reprocessing_p ())
19313 any_need_reprocess = true;
18a8505e
AT
19314 }
19315
052c8bb8 19316 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
529908cb
TT
19317 if (attr != nullptr && attr->form_is_unsigned ())
19318 cu->str_offsets_base = attr->as_unsigned ();
93311388 19319
41144253 19320 attr = die->attr (DW_AT_loclists_base);
19321 if (attr != nullptr)
529908cb 19322 cu->loclist_base = attr->as_unsigned ();
41144253 19323
a39fdb41 19324 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
19325 if (maybe_addr_base.has_value ())
19326 cu->addr_base = *maybe_addr_base;
d0ce17d8
CT
19327
19328 attr = die->attr (DW_AT_rnglists_base);
19329 if (attr != nullptr)
2b0c7f41 19330 cu->rnglists_base = attr->as_unsigned ();
d0ce17d8 19331
7a5f294d
TT
19332 if (any_need_reprocess)
19333 {
19334 for (i = 0; i < abbrev->num_attrs; ++i)
19335 {
19336 if (die->attrs[i].requires_reprocessing_p ())
19337 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19338 }
19339 }
93311388 19340 *diep = die;
93311388
DE
19341 return info_ptr;
19342}
19343
3019eac3
DE
19344/* Read a die and all its attributes.
19345 Set DIEP to point to a newly allocated die with its information,
3e225074 19346 except for its child, sibling, and parent fields. */
3019eac3 19347
d521ce57 19348static const gdb_byte *
3019eac3 19349read_full_die (const struct die_reader_specs *reader,
3e225074 19350 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 19351{
d521ce57 19352 const gdb_byte *result;
bf6af496 19353
3e225074 19354 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 19355
b4f54984 19356 if (dwarf_die_debug)
bf6af496
DE
19357 {
19358 fprintf_unfiltered (gdb_stdlog,
19359 "Read die from %s@0x%x of %s:\n",
96b79293 19360 reader->die_section->get_name (),
bf6af496
DE
19361 (unsigned) (info_ptr - reader->die_section->buffer),
19362 bfd_get_filename (reader->abfd));
b4f54984 19363 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
19364 }
19365
19366 return result;
3019eac3 19367}
433df2d4 19368\f
c906108c 19369
72bf9492
DJ
19370/* Returns nonzero if TAG represents a type that we might generate a partial
19371 symbol for. */
19372
19373static int
d8f62e84 19374is_type_tag_for_partial (int tag, enum language lang)
72bf9492
DJ
19375{
19376 switch (tag)
19377 {
19378#if 0
19379 /* Some types that would be reasonable to generate partial symbols for,
d8f62e84
TT
19380 that we don't at present. Note that normally this does not
19381 matter, mainly because C compilers don't give names to these
19382 types, but instead emit DW_TAG_typedef. */
72bf9492
DJ
19383 case DW_TAG_file_type:
19384 case DW_TAG_ptr_to_member_type:
19385 case DW_TAG_set_type:
19386 case DW_TAG_string_type:
19387 case DW_TAG_subroutine_type:
19388#endif
d8f62e84
TT
19389
19390 /* GNAT may emit an array with a name, but no typedef, so we
19391 need to make a symbol in this case. */
19392 case DW_TAG_array_type:
19393 return lang == language_ada;
19394
72bf9492
DJ
19395 case DW_TAG_base_type:
19396 case DW_TAG_class_type:
680b30c7 19397 case DW_TAG_interface_type:
72bf9492
DJ
19398 case DW_TAG_enumeration_type:
19399 case DW_TAG_structure_type:
19400 case DW_TAG_subrange_type:
19401 case DW_TAG_typedef:
19402 case DW_TAG_union_type:
19403 return 1;
19404 default:
19405 return 0;
19406 }
19407}
19408
19409/* Load all DIEs that are interesting for partial symbols into memory. */
19410
19411static struct partial_die_info *
dee91e82 19412load_partial_dies (const struct die_reader_specs *reader,
d521ce57 19413 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 19414{
dee91e82 19415 struct dwarf2_cu *cu = reader->cu;
5e22e966 19416 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 19417 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 19418 unsigned int bytes_read;
5afb4e99 19419 unsigned int load_all = 0;
72bf9492
DJ
19420 int nesting_level = 1;
19421
19422 parent_die = NULL;
19423 last_die = NULL;
19424
7adf1e79
DE
19425 gdb_assert (cu->per_cu != NULL);
19426 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
19427 load_all = 1;
19428
72bf9492
DJ
19429 cu->partial_dies
19430 = htab_create_alloc_ex (cu->header.length / 12,
19431 partial_die_hash,
19432 partial_die_eq,
19433 NULL,
19434 &cu->comp_unit_obstack,
19435 hashtab_obstack_allocate,
19436 dummy_obstack_deallocate);
19437
72bf9492
DJ
19438 while (1)
19439 {
685af9cd 19440 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
19441
19442 /* A NULL abbrev means the end of a series of children. */
19443 if (abbrev == NULL)
19444 {
19445 if (--nesting_level == 0)
cd9983dd
YQ
19446 return first_die;
19447
72bf9492
DJ
19448 info_ptr += bytes_read;
19449 last_die = parent_die;
19450 parent_die = parent_die->die_parent;
19451 continue;
19452 }
19453
98bfdba5
PA
19454 /* Check for template arguments. We never save these; if
19455 they're seen, we just mark the parent, and go on our way. */
19456 if (parent_die != NULL
19457 && cu->language == language_cplus
19458 && (abbrev->tag == DW_TAG_template_type_param
19459 || abbrev->tag == DW_TAG_template_value_param))
19460 {
19461 parent_die->has_template_arguments = 1;
19462
19463 if (!load_all)
19464 {
19465 /* We don't need a partial DIE for the template argument. */
dee91e82 19466 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19467 continue;
19468 }
19469 }
19470
0d99eb77 19471 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
19472 Skip their other children. */
19473 if (!load_all
19474 && cu->language == language_cplus
19475 && parent_die != NULL
f9b5d5ea
TV
19476 && parent_die->tag == DW_TAG_subprogram
19477 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 19478 {
dee91e82 19479 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19480 continue;
19481 }
19482
5afb4e99
DJ
19483 /* Check whether this DIE is interesting enough to save. Normally
19484 we would not be interested in members here, but there may be
19485 later variables referencing them via DW_AT_specification (for
19486 static members). */
19487 if (!load_all
d8f62e84 19488 && !is_type_tag_for_partial (abbrev->tag, cu->language)
72929c62 19489 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
19490 && abbrev->tag != DW_TAG_enumerator
19491 && abbrev->tag != DW_TAG_subprogram
b1dc1806 19492 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 19493 && abbrev->tag != DW_TAG_lexical_block
72bf9492 19494 && abbrev->tag != DW_TAG_variable
5afb4e99 19495 && abbrev->tag != DW_TAG_namespace
f55ee35c 19496 && abbrev->tag != DW_TAG_module
95554aad 19497 && abbrev->tag != DW_TAG_member
74921315
KS
19498 && abbrev->tag != DW_TAG_imported_unit
19499 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
19500 {
19501 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19502 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
19503 continue;
19504 }
19505
6f06d47b
YQ
19506 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19507 abbrev);
cd9983dd 19508
48fbe735 19509 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
19510
19511 /* This two-pass algorithm for processing partial symbols has a
19512 high cost in cache pressure. Thus, handle some simple cases
19513 here which cover the majority of C partial symbols. DIEs
19514 which neither have specification tags in them, nor could have
19515 specification tags elsewhere pointing at them, can simply be
19516 processed and discarded.
19517
19518 This segment is also optional; scan_partial_symbols and
19519 add_partial_symbol will handle these DIEs if we chain
19520 them in normally. When compilers which do not emit large
19521 quantities of duplicate debug information are more common,
19522 this code can probably be removed. */
19523
19524 /* Any complete simple types at the top level (pretty much all
19525 of them, for a language without namespaces), can be processed
19526 directly. */
19527 if (parent_die == NULL
cd9983dd
YQ
19528 && pdi.has_specification == 0
19529 && pdi.is_declaration == 0
19530 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19531 || pdi.tag == DW_TAG_base_type
d8f62e84 19532 || pdi.tag == DW_TAG_array_type
cd9983dd 19533 || pdi.tag == DW_TAG_subrange_type))
72bf9492 19534 {
7d00ffec 19535 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
19536 add_partial_symbol (&pdi, cu);
19537
cd9983dd 19538 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19539 continue;
19540 }
19541
d8228535
JK
19542 /* The exception for DW_TAG_typedef with has_children above is
19543 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 19544 type_name_or_error will error on such types later.
d8228535
JK
19545
19546 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19547 it could not find the child DIEs referenced later, this is checked
19548 above. In correct DWARF DW_TAG_typedef should have no children. */
19549
cd9983dd 19550 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 19551 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 19552 "- DIE at %s [in module %s]"),
cd9983dd 19553 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 19554
72bf9492
DJ
19555 /* If we're at the second level, and we're an enumerator, and
19556 our parent has no specification (meaning possibly lives in a
19557 namespace elsewhere), then we can add the partial symbol now
19558 instead of queueing it. */
cd9983dd 19559 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
19560 && parent_die != NULL
19561 && parent_die->die_parent == NULL
19562 && parent_die->tag == DW_TAG_enumeration_type
19563 && parent_die->has_specification == 0)
19564 {
7d00ffec 19565 if (pdi.raw_name == NULL)
b98664d3 19566 complaint (_("malformed enumerator DIE ignored"));
72bf9492 19567 else if (building_psymtab)
f0fbb768 19568 add_partial_symbol (&pdi, cu);
72bf9492 19569
cd9983dd 19570 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19571 continue;
19572 }
19573
cd9983dd 19574 struct partial_die_info *part_die
6f06d47b 19575 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 19576
72bf9492
DJ
19577 /* We'll save this DIE so link it in. */
19578 part_die->die_parent = parent_die;
19579 part_die->die_sibling = NULL;
19580 part_die->die_child = NULL;
19581
19582 if (last_die && last_die == parent_die)
19583 last_die->die_child = part_die;
19584 else if (last_die)
19585 last_die->die_sibling = part_die;
19586
19587 last_die = part_die;
19588
19589 if (first_die == NULL)
19590 first_die = part_die;
19591
19592 /* Maybe add the DIE to the hash table. Not all DIEs that we
19593 find interesting need to be in the hash table, because we
19594 also have the parent/sibling/child chains; only those that we
19595 might refer to by offset later during partial symbol reading.
19596
19597 For now this means things that might have be the target of a
19598 DW_AT_specification, DW_AT_abstract_origin, or
19599 DW_AT_extension. DW_AT_extension will refer only to
19600 namespaces; DW_AT_abstract_origin refers to functions (and
19601 many things under the function DIE, but we do not recurse
19602 into function DIEs during partial symbol reading) and
19603 possibly variables as well; DW_AT_specification refers to
19604 declarations. Declarations ought to have the DW_AT_declaration
19605 flag. It happens that GCC forgets to put it in sometimes, but
19606 only for functions, not for types.
19607
19608 Adding more things than necessary to the hash table is harmless
19609 except for the performance cost. Adding too few will result in
5afb4e99
DJ
19610 wasted time in find_partial_die, when we reread the compilation
19611 unit with load_all_dies set. */
72bf9492 19612
5afb4e99 19613 if (load_all
72929c62 19614 || abbrev->tag == DW_TAG_constant
5afb4e99 19615 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
19616 || abbrev->tag == DW_TAG_variable
19617 || abbrev->tag == DW_TAG_namespace
19618 || part_die->is_declaration)
19619 {
19620 void **slot;
19621
19622 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
19623 to_underlying (part_die->sect_off),
19624 INSERT);
72bf9492
DJ
19625 *slot = part_die;
19626 }
19627
72bf9492 19628 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 19629 we have no reason to follow the children of structures; for other
98bfdba5
PA
19630 languages we have to, so that we can get at method physnames
19631 to infer fully qualified class names, for DW_AT_specification,
19632 and for C++ template arguments. For C++, we also look one level
19633 inside functions to find template arguments (if the name of the
19634 function does not already contain the template arguments).
bc30ff58 19635
0a4b0913
AB
19636 For Ada and Fortran, we need to scan the children of subprograms
19637 and lexical blocks as well because these languages allow the
19638 definition of nested entities that could be interesting for the
19639 debugger, such as nested subprograms for instance. */
72bf9492 19640 if (last_die->has_children
5afb4e99
DJ
19641 && (load_all
19642 || last_die->tag == DW_TAG_namespace
f55ee35c 19643 || last_die->tag == DW_TAG_module
72bf9492 19644 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
19645 || (cu->language == language_cplus
19646 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
19647 && (last_die->raw_name == NULL
19648 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
19649 || (cu->language != language_c
19650 && (last_die->tag == DW_TAG_class_type
680b30c7 19651 || last_die->tag == DW_TAG_interface_type
72bf9492 19652 || last_die->tag == DW_TAG_structure_type
bc30ff58 19653 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
19654 || ((cu->language == language_ada
19655 || cu->language == language_fortran)
bc30ff58
JB
19656 && (last_die->tag == DW_TAG_subprogram
19657 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
19658 {
19659 nesting_level++;
19660 parent_die = last_die;
19661 continue;
19662 }
19663
19664 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19665 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
19666
19667 /* Back to the top, do it again. */
19668 }
19669}
19670
6f06d47b
YQ
19671partial_die_info::partial_die_info (sect_offset sect_off_,
19672 struct abbrev_info *abbrev)
19673 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19674{
19675}
19676
7d00ffec
TT
19677/* See class definition. */
19678
19679const char *
19680partial_die_info::name (dwarf2_cu *cu)
19681{
19682 if (!canonical_name && raw_name != nullptr)
19683 {
19684 struct objfile *objfile = cu->per_objfile->objfile;
19685 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19686 canonical_name = 1;
19687 }
19688
19689 return raw_name;
19690}
19691
35cc7ed7
YQ
19692/* Read a minimal amount of information into the minimal die structure.
19693 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 19694
48fbe735
YQ
19695const gdb_byte *
19696partial_die_info::read (const struct die_reader_specs *reader,
19697 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 19698{
dee91e82 19699 struct dwarf2_cu *cu = reader->cu;
976ca316 19700 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 19701 unsigned int i;
c5aa993b 19702 int has_low_pc_attr = 0;
c906108c 19703 int has_high_pc_attr = 0;
91da1414 19704 int high_pc_relative = 0;
c906108c 19705
fd0a254f 19706 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 19707 {
e7da7f8f 19708 attribute attr;
7a5f294d 19709 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18a8505e 19710 /* String and address offsets that need to do the reprocessing have
dda83cd7 19711 already been read at this point, so there is no need to wait until
18a8505e 19712 the loop terminates to do the reprocessing. */
7a5f294d 19713 if (attr.requires_reprocessing_p ())
d0ce17d8 19714 read_attribute_reprocess (reader, &attr, tag);
c906108c 19715 /* Store the data if it is of an attribute we want to keep in a
dda83cd7 19716 partial symbol table. */
c906108c
SS
19717 switch (attr.name)
19718 {
19719 case DW_AT_name:
48fbe735 19720 switch (tag)
71c25dea
TT
19721 {
19722 case DW_TAG_compile_unit:
95554aad 19723 case DW_TAG_partial_unit:
348e048f 19724 case DW_TAG_type_unit:
71c25dea
TT
19725 /* Compilation units have a DW_AT_name that is a filename, not
19726 a source language identifier. */
19727 case DW_TAG_enumeration_type:
19728 case DW_TAG_enumerator:
19729 /* These tags always have simple identifiers already; no need
19730 to canonicalize them. */
7d00ffec 19731 canonical_name = 1;
2c830f54 19732 raw_name = attr.as_string ();
71c25dea
TT
19733 break;
19734 default:
7d00ffec 19735 canonical_name = 0;
2c830f54 19736 raw_name = attr.as_string ();
71c25dea
TT
19737 break;
19738 }
c906108c 19739 break;
31ef98ae 19740 case DW_AT_linkage_name:
c906108c 19741 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19742 /* Note that both forms of linkage name might appear. We
19743 assume they will be the same, and we only store the last
19744 one we see. */
95f982e5 19745 linkage_name = attr.as_string ();
c906108c
SS
19746 break;
19747 case DW_AT_low_pc:
19748 has_low_pc_attr = 1;
95f982e5 19749 lowpc = attr.as_address ();
c906108c
SS
19750 break;
19751 case DW_AT_high_pc:
19752 has_high_pc_attr = 1;
95f982e5 19753 highpc = attr.as_address ();
cd6c91b4 19754 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 19755 high_pc_relative = 1;
c906108c
SS
19756 break;
19757 case DW_AT_location:
dda83cd7
SM
19758 /* Support the .debug_loc offsets. */
19759 if (attr.form_is_block ())
19760 {
9d2246fc 19761 d.locdesc = attr.as_block ();
dda83cd7
SM
19762 }
19763 else if (attr.form_is_section_offset ())
19764 {
4d3c2250 19765 dwarf2_complex_location_expr_complaint ();
dda83cd7
SM
19766 }
19767 else
19768 {
4d3c2250
KB
19769 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19770 "partial symbol information");
dda83cd7 19771 }
c906108c 19772 break;
c906108c 19773 case DW_AT_external:
c45bc3f8 19774 is_external = attr.as_boolean ();
c906108c
SS
19775 break;
19776 case DW_AT_declaration:
c45bc3f8 19777 is_declaration = attr.as_boolean ();
c906108c
SS
19778 break;
19779 case DW_AT_type:
48fbe735 19780 has_type = 1;
c906108c
SS
19781 break;
19782 case DW_AT_abstract_origin:
19783 case DW_AT_specification:
72bf9492 19784 case DW_AT_extension:
48fbe735 19785 has_specification = 1;
0826b30a 19786 spec_offset = attr.get_ref_die_offset ();
48fbe735 19787 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19788 || cu->per_cu->is_dwz);
c906108c
SS
19789 break;
19790 case DW_AT_sibling:
19791 /* Ignore absolute siblings, they might point outside of
19792 the current compile unit. */
19793 if (attr.form == DW_FORM_ref_addr)
b98664d3 19794 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19795 else
b9502d3f 19796 {
48fbe735 19797 const gdb_byte *buffer = reader->buffer;
0826b30a 19798 sect_offset off = attr.get_ref_die_offset ();
9c541725 19799 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19800
19801 if (sibling_ptr < info_ptr)
b98664d3 19802 complaint (_("DW_AT_sibling points backwards"));
22869d73 19803 else if (sibling_ptr > reader->buffer_end)
a0194fa8 19804 reader->die_section->overflow_complaint ();
b9502d3f 19805 else
48fbe735 19806 sibling = sibling_ptr;
b9502d3f 19807 }
c906108c 19808 break;
dda83cd7
SM
19809 case DW_AT_byte_size:
19810 has_byte_size = 1;
19811 break;
19812 case DW_AT_const_value:
19813 has_const_value = 1;
19814 break;
68511cec
CES
19815 case DW_AT_calling_convention:
19816 /* DWARF doesn't provide a way to identify a program's source-level
19817 entry point. DW_AT_calling_convention attributes are only meant
19818 to describe functions' calling conventions.
19819
19820 However, because it's a necessary piece of information in
0c1b455e
TT
19821 Fortran, and before DWARF 4 DW_CC_program was the only
19822 piece of debugging information whose definition refers to
19823 a 'main program' at all, several compilers marked Fortran
19824 main programs with DW_CC_program --- even when those
19825 functions use the standard calling conventions.
19826
19827 Although DWARF now specifies a way to provide this
19828 information, we support this practice for backward
19829 compatibility. */
529908cb 19830 if (attr.constant_value (0) == DW_CC_program
0c1b455e 19831 && cu->language == language_fortran)
48fbe735 19832 main_subprogram = 1;
68511cec 19833 break;
481860b3 19834 case DW_AT_inline:
529908cb
TT
19835 {
19836 LONGEST value = attr.constant_value (-1);
19837 if (value == DW_INL_inlined
19838 || value == DW_INL_declared_inlined)
19839 may_be_inlined = 1;
19840 }
481860b3 19841 break;
95554aad
TT
19842
19843 case DW_AT_import:
48fbe735 19844 if (tag == DW_TAG_imported_unit)
36586728 19845 {
0826b30a 19846 d.sect_off = attr.get_ref_die_offset ();
48fbe735 19847 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19848 || cu->per_cu->is_dwz);
19849 }
95554aad
TT
19850 break;
19851
0c1b455e 19852 case DW_AT_main_subprogram:
c45bc3f8 19853 main_subprogram = attr.as_boolean ();
0c1b455e
TT
19854 break;
19855
05caa1d2
TT
19856 case DW_AT_ranges:
19857 {
2b0c7f41
SM
19858 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19859 on DWARF version). */
19860 ULONGEST ranges_offset = attr.as_unsigned ();
19861
19862 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19863 this value. */
19864 if (tag != DW_TAG_compile_unit)
19865 ranges_offset += cu->gnu_ranges_base;
19866
05caa1d2 19867 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
d0ce17d8 19868 nullptr, tag))
05caa1d2
TT
19869 has_pc_info = 1;
19870 }
19871 break;
19872
c906108c
SS
19873 default:
19874 break;
19875 }
19876 }
19877
10d06d82
TT
19878 /* For Ada, if both the name and the linkage name appear, we prefer
19879 the latter. This lets "catch exception" work better, regardless
19880 of the order in which the name and linkage name were emitted.
19881 Really, though, this is just a workaround for the fact that gdb
19882 doesn't store both the name and the linkage name. */
19883 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 19884 raw_name = linkage_name;
10d06d82 19885
91da1414 19886 if (high_pc_relative)
48fbe735 19887 highpc += lowpc;
91da1414 19888
9373cf26
JK
19889 if (has_low_pc_attr && has_high_pc_attr)
19890 {
19891 /* When using the GNU linker, .gnu.linkonce. sections are used to
19892 eliminate duplicate copies of functions and vtables and such.
19893 The linker will arbitrarily choose one and discard the others.
19894 The AT_*_pc values for such functions refer to local labels in
19895 these sections. If the section from that file was discarded, the
19896 labels are not in the output, so the relocs get a value of 0.
19897 If this is a discarded function, mark the pc bounds as invalid,
19898 so that GDB will ignore it. */
976ca316 19899 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 19900 {
976ca316 19901 struct objfile *objfile = per_objfile->objfile;
08feed99 19902 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19903
b98664d3 19904 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19905 "for DIE at %s [in module %s]"),
48fbe735
YQ
19906 paddress (gdbarch, lowpc),
19907 sect_offset_str (sect_off),
9d8780f0 19908 objfile_name (objfile));
9373cf26
JK
19909 }
19910 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19911 else if (lowpc >= highpc)
9373cf26 19912 {
976ca316 19913 struct objfile *objfile = per_objfile->objfile;
08feed99 19914 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19915
b98664d3 19916 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19917 "for DIE at %s [in module %s]"),
48fbe735
YQ
19918 paddress (gdbarch, lowpc),
19919 paddress (gdbarch, highpc),
19920 sect_offset_str (sect_off),
9c541725 19921 objfile_name (objfile));
9373cf26
JK
19922 }
19923 else
48fbe735 19924 has_pc_info = 1;
9373cf26 19925 }
85cbf3d3 19926
c906108c
SS
19927 return info_ptr;
19928}
19929
72bf9492
DJ
19930/* Find a cached partial DIE at OFFSET in CU. */
19931
d590ff25
YQ
19932struct partial_die_info *
19933dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19934{
19935 struct partial_die_info *lookup_die = NULL;
6f06d47b 19936 struct partial_die_info part_die (sect_off);
72bf9492 19937
9a3c8263 19938 lookup_die = ((struct partial_die_info *)
d590ff25 19939 htab_find_with_hash (partial_dies, &part_die,
9c541725 19940 to_underlying (sect_off)));
72bf9492 19941
72bf9492
DJ
19942 return lookup_die;
19943}
19944
348e048f
DE
19945/* Find a partial DIE at OFFSET, which may or may not be in CU,
19946 except in the case of .debug_types DIEs which do not reference
19947 outside their CU (they do however referencing other types via
55f1336d 19948 DW_FORM_ref_sig8). */
72bf9492 19949
122cf0f2 19950static const struct cu_partial_die_info
9c541725 19951find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19952{
976ca316
SM
19953 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19954 struct objfile *objfile = per_objfile->objfile;
5afb4e99 19955 struct partial_die_info *pd = NULL;
72bf9492 19956
36586728 19957 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 19958 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 19959 {
d590ff25 19960 pd = cu->find_partial_die (sect_off);
5afb4e99 19961 if (pd != NULL)
fb816e8b 19962 return { cu, pd };
0d99eb77
DE
19963 /* We missed recording what we needed.
19964 Load all dies and try again. */
5afb4e99 19965 }
0d99eb77
DE
19966 else
19967 {
19968 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19969 if (cu->per_cu->is_debug_types)
0d99eb77 19970 {
9d8780f0
SM
19971 error (_("Dwarf Error: Type Unit at offset %s contains"
19972 " external reference to offset %s [in module %s].\n"),
19973 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19974 bfd_get_filename (objfile->obfd));
19975 }
7188ed02
SM
19976 dwarf2_per_cu_data *per_cu
19977 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 19978 per_objfile);
72bf9492 19979
976ca316 19980 cu = per_objfile->get_cu (per_cu);
7188ed02 19981 if (cu == NULL || cu->partial_dies == NULL)
976ca316 19982 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 19983
976ca316 19984 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
19985
19986 cu->last_used = 0;
19987 pd = cu->find_partial_die (sect_off);
0d99eb77 19988 }
5afb4e99 19989
dee91e82
DE
19990 /* If we didn't find it, and not all dies have been loaded,
19991 load them all and try again. */
19992
7188ed02 19993 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 19994 {
7188ed02 19995 cu->per_cu->load_all_dies = 1;
fd820528
DE
19996
19997 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19998 THIS_CU->cu may already be in use. So we can't just free it and
19999 replace its DIEs with the ones we read in. Instead, we leave those
20000 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
20001 and clobber THIS_CU->cu->partial_dies with the hash table for the new
20002 set. */
976ca316 20003 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 20004
7188ed02 20005 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
20006 }
20007
20008 if (pd == NULL)
521894aa 20009 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
9d8780f0 20010 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 20011 return { cu, pd };
72bf9492
DJ
20012}
20013
abc72ce4
DE
20014/* See if we can figure out if the class lives in a namespace. We do
20015 this by looking for a member function; its demangled name will
20016 contain namespace info, if there is any. */
20017
20018static void
20019guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
20020 struct dwarf2_cu *cu)
20021{
20022 /* NOTE: carlton/2003-10-07: Getting the info this way changes
20023 what template types look like, because the demangler
20024 frequently doesn't give the same name as the debug info. We
20025 could fix this by only using the demangled name to get the
20026 prefix (but see comment in read_structure_type). */
20027
20028 struct partial_die_info *real_pdi;
20029 struct partial_die_info *child_pdi;
20030
20031 /* If this DIE (this DIE's specification, if any) has a parent, then
20032 we should not do this. We'll prepend the parent's fully qualified
20033 name when we create the partial symbol. */
20034
20035 real_pdi = struct_pdi;
20036 while (real_pdi->has_specification)
fb816e8b 20037 {
122cf0f2
AB
20038 auto res = find_partial_die (real_pdi->spec_offset,
20039 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
20040 real_pdi = res.pdi;
20041 cu = res.cu;
20042 }
abc72ce4
DE
20043
20044 if (real_pdi->die_parent != NULL)
20045 return;
20046
20047 for (child_pdi = struct_pdi->die_child;
20048 child_pdi != NULL;
20049 child_pdi = child_pdi->die_sibling)
20050 {
20051 if (child_pdi->tag == DW_TAG_subprogram
20052 && child_pdi->linkage_name != NULL)
20053 {
43816ebc 20054 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
20055 (cu->language_defn->class_name_from_physname
20056 (child_pdi->linkage_name));
abc72ce4
DE
20057 if (actual_class_name != NULL)
20058 {
5e22e966 20059 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
20060 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
20061 struct_pdi->canonical_name = 1;
abc72ce4
DE
20062 }
20063 break;
20064 }
20065 }
20066}
20067
25c11aca
TV
20068/* Return true if a DIE with TAG may have the DW_AT_const_value
20069 attribute. */
20070
20071static bool
20072can_have_DW_AT_const_value_p (enum dwarf_tag tag)
20073{
20074 switch (tag)
20075 {
20076 case DW_TAG_constant:
20077 case DW_TAG_enumerator:
20078 case DW_TAG_formal_parameter:
20079 case DW_TAG_template_value_param:
20080 case DW_TAG_variable:
20081 return true;
20082 }
20083
20084 return false;
20085}
20086
52356b79
YQ
20087void
20088partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 20089{
abc72ce4
DE
20090 /* Once we've fixed up a die, there's no point in doing so again.
20091 This also avoids a memory leak if we were to call
20092 guess_partial_die_structure_name multiple times. */
52356b79 20093 if (fixup_called)
abc72ce4
DE
20094 return;
20095
72bf9492
DJ
20096 /* If we found a reference attribute and the DIE has no name, try
20097 to find a name in the referred to DIE. */
20098
7d00ffec 20099 if (raw_name == NULL && has_specification)
72bf9492
DJ
20100 {
20101 struct partial_die_info *spec_die;
72bf9492 20102
122cf0f2 20103 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
20104 spec_die = res.pdi;
20105 cu = res.cu;
72bf9492 20106
52356b79 20107 spec_die->fixup (cu);
72bf9492 20108
7d00ffec 20109 if (spec_die->raw_name)
72bf9492 20110 {
7d00ffec
TT
20111 raw_name = spec_die->raw_name;
20112 canonical_name = spec_die->canonical_name;
72bf9492
DJ
20113
20114 /* Copy DW_AT_external attribute if it is set. */
20115 if (spec_die->is_external)
52356b79 20116 is_external = spec_die->is_external;
72bf9492
DJ
20117 }
20118 }
20119
25c11aca
TV
20120 if (!has_const_value && has_specification
20121 && can_have_DW_AT_const_value_p (tag))
20122 {
20123 struct partial_die_info *spec_die;
20124
20125 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20126 spec_die = res.pdi;
20127 cu = res.cu;
20128
20129 spec_die->fixup (cu);
20130
20131 if (spec_die->has_const_value)
20132 {
20133 /* Copy DW_AT_const_value attribute if it is set. */
20134 has_const_value = spec_die->has_const_value;
20135 }
20136 }
20137
72bf9492 20138 /* Set default names for some unnamed DIEs. */
72bf9492 20139
7d00ffec
TT
20140 if (raw_name == NULL && tag == DW_TAG_namespace)
20141 {
20142 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20143 canonical_name = 1;
20144 }
72bf9492 20145
abc72ce4
DE
20146 /* If there is no parent die to provide a namespace, and there are
20147 children, see if we can determine the namespace from their linkage
122d1940 20148 name. */
abc72ce4 20149 if (cu->language == language_cplus
5e22e966 20150 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
20151 && die_parent == NULL
20152 && has_children
20153 && (tag == DW_TAG_class_type
20154 || tag == DW_TAG_structure_type
20155 || tag == DW_TAG_union_type))
20156 guess_partial_die_structure_name (this, cu);
abc72ce4 20157
53832f31
TT
20158 /* GCC might emit a nameless struct or union that has a linkage
20159 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 20160 if (raw_name == NULL
52356b79
YQ
20161 && (tag == DW_TAG_class_type
20162 || tag == DW_TAG_interface_type
20163 || tag == DW_TAG_structure_type
20164 || tag == DW_TAG_union_type)
20165 && linkage_name != NULL)
53832f31 20166 {
43816ebc
TT
20167 gdb::unique_xmalloc_ptr<char> demangled
20168 (gdb_demangle (linkage_name, DMGL_TYPES));
20169 if (demangled != nullptr)
53832f31 20170 {
96408a79
SA
20171 const char *base;
20172
20173 /* Strip any leading namespaces/classes, keep only the base name.
20174 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
20175 base = strrchr (demangled.get (), ':');
20176 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
20177 base++;
20178 else
43816ebc 20179 base = demangled.get ();
96408a79 20180
5e22e966 20181 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
20182 raw_name = objfile->intern (base);
20183 canonical_name = 1;
53832f31
TT
20184 }
20185 }
20186
52356b79 20187 fixup_called = 1;
72bf9492
DJ
20188}
20189
d0ce17d8 20190/* Read the .debug_loclists or .debug_rnglists header (they are the same format)
a1c40103
SM
20191 contents from the given SECTION in the HEADER.
20192
20193 HEADER_OFFSET is the offset of the header in the section. */
41144253 20194static void
d0ce17d8 20195read_loclists_rnglists_header (struct loclists_rnglists_header *header,
a1c40103
SM
20196 struct dwarf2_section_info *section,
20197 sect_offset header_offset)
41144253 20198{
20199 unsigned int bytes_read;
20200 bfd *abfd = section->get_bfd_owner ();
a1c40103
SM
20201 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20202
41144253 20203 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20204 info_ptr += bytes_read;
a1c40103 20205
41144253 20206 header->version = read_2_bytes (abfd, info_ptr);
20207 info_ptr += 2;
a1c40103 20208
41144253 20209 header->addr_size = read_1_byte (abfd, info_ptr);
20210 info_ptr += 1;
a1c40103 20211
41144253 20212 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20213 info_ptr += 1;
a1c40103 20214
41144253 20215 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20216}
20217
20218/* Return the DW_AT_loclists_base value for the CU. */
20219static ULONGEST
20220lookup_loclist_base (struct dwarf2_cu *cu)
20221{
20222 /* For the .dwo unit, the loclist_base points to the first offset following
20223 the header. The header consists of the following entities-
20224 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20225 bit format)
20226 2. version (2 bytes)
20227 3. address size (1 byte)
20228 4. segment selector size (1 byte)
20229 5. offset entry count (4 bytes)
20230 These sizes are derived as per the DWARFv5 standard. */
20231 if (cu->dwo_unit != nullptr)
20232 {
20233 if (cu->header.initial_length_size == 4)
20234 return LOCLIST_HEADER_SIZE32;
20235 return LOCLIST_HEADER_SIZE64;
20236 }
20237 return cu->loclist_base;
20238}
20239
20240/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20241 array of offsets in the .debug_loclists section. */
e57933dc
SM
20242
20243static sect_offset
41144253 20244read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20245{
976ca316
SM
20246 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20247 struct objfile *objfile = per_objfile->objfile;
41144253 20248 bfd *abfd = objfile->obfd;
a1c40103
SM
20249 ULONGEST loclist_header_size =
20250 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20251 : LOCLIST_HEADER_SIZE64);
41144253 20252 ULONGEST loclist_base = lookup_loclist_base (cu);
05787bad
SM
20253
20254 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20255 ULONGEST start_offset =
20256 loclist_base + loclist_index * cu->header.offset_size;
20257
a1c40103 20258 /* Get loclists section. */
41144253 20259 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20260
a1c40103 20261 /* Read the loclists section content. */
41144253 20262 section->read (objfile);
20263 if (section->buffer == NULL)
a0c1eeba
SM
20264 error (_("DW_FORM_loclistx used without .debug_loclists "
20265 "section [in module %s]"), objfile_name (objfile));
20266
a1c40103
SM
20267 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20268 so if loclist_base is smaller than the header size, we have a problem. */
20269 if (loclist_base < loclist_header_size)
20270 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20271 objfile_name (objfile));
20272
20273 /* Read the header of the loclists contribution. */
d0ce17d8 20274 struct loclists_rnglists_header header;
a1c40103
SM
20275 read_loclists_rnglists_header (&header, section,
20276 (sect_offset) (loclist_base - loclist_header_size));
20277
20278 /* Verify the loclist index is valid. */
41144253 20279 if (loclist_index >= header.offset_entry_count)
a0c1eeba
SM
20280 error (_("DW_FORM_loclistx pointing outside of "
20281 ".debug_loclists offset array [in module %s]"),
20282 objfile_name (objfile));
20283
05787bad
SM
20284 /* Validate that reading won't go beyond the end of the section. */
20285 if (start_offset + cu->header.offset_size > section->size)
20286 error (_("Reading DW_FORM_loclistx index beyond end of"
20287 ".debug_loclists section [in module %s]"),
20288 objfile_name (objfile));
20289
20290 const gdb_byte *info_ptr = section->buffer + start_offset;
41144253 20291
20292 if (cu->header.offset_size == 4)
e57933dc 20293 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
41144253 20294 else
e57933dc 20295 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
41144253 20296}
20297
d0ce17d8
CT
20298/* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20299 array of offsets in the .debug_rnglists section. */
e57933dc
SM
20300
20301static sect_offset
d0ce17d8
CT
20302read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20303 dwarf_tag tag)
20304{
20305 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20306 struct objfile *objfile = dwarf2_per_objfile->objfile;
20307 bfd *abfd = objfile->obfd;
20308 ULONGEST rnglist_header_size =
20309 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20310 : RNGLIST_HEADER_SIZE64);
2b0c7f41
SM
20311
20312 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20313 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20314 doesn't apply. */
d0ce17d8 20315 ULONGEST rnglist_base =
2b0c7f41 20316 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
5e4d9bbc
SM
20317
20318 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
d0ce17d8
CT
20319 ULONGEST start_offset =
20320 rnglist_base + rnglist_index * cu->header.offset_size;
20321
20322 /* Get rnglists section. */
20323 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20324
20325 /* Read the rnglists section content. */
20326 section->read (objfile);
20327 if (section->buffer == nullptr)
20328 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20329 "[in module %s]"),
20330 objfile_name (objfile));
20331
a1c40103
SM
20332 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20333 so if rnglist_base is smaller than the header size, we have a problem. */
20334 if (rnglist_base < rnglist_header_size)
20335 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20336 objfile_name (objfile));
20337
20338 /* Read the header of the rnglists contribution. */
d0ce17d8 20339 struct loclists_rnglists_header header;
a1c40103
SM
20340 read_loclists_rnglists_header (&header, section,
20341 (sect_offset) (rnglist_base - rnglist_header_size));
20342
20343 /* Verify the rnglist index is valid. */
d0ce17d8
CT
20344 if (rnglist_index >= header.offset_entry_count)
20345 error (_("DW_FORM_rnglistx index pointing outside of "
20346 ".debug_rnglists offset array [in module %s]"),
20347 objfile_name (objfile));
20348
d0ce17d8 20349 /* Validate that reading won't go beyond the end of the section. */
5e4d9bbc 20350 if (start_offset + cu->header.offset_size > section->size)
d0ce17d8
CT
20351 error (_("Reading DW_FORM_rnglistx index beyond end of"
20352 ".debug_rnglists section [in module %s]"),
20353 objfile_name (objfile));
20354
20355 const gdb_byte *info_ptr = section->buffer + start_offset;
20356
20357 if (cu->header.offset_size == 4)
e57933dc 20358 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8 20359 else
e57933dc 20360 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8
CT
20361}
20362
18a8505e
AT
20363/* Process the attributes that had to be skipped in the first round. These
20364 attributes are the ones that need str_offsets_base or addr_base attributes.
20365 They could not have been processed in the first round, because at the time
20366 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
20367static void
20368read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 20369 struct attribute *attr, dwarf_tag tag)
18a8505e
AT
20370{
20371 struct dwarf2_cu *cu = reader->cu;
20372 switch (attr->form)
20373 {
20374 case DW_FORM_addrx:
20375 case DW_FORM_GNU_addr_index:
36d378cf
TT
20376 attr->set_address (read_addr_index (cu,
20377 attr->as_unsigned_reprocess ()));
dda83cd7 20378 break;
41144253 20379 case DW_FORM_loclistx:
b1829e1b 20380 {
e57933dc 20381 sect_offset loclists_sect_off
b1829e1b
SM
20382 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20383
e57933dc 20384 attr->set_unsigned (to_underlying (loclists_sect_off));
b1829e1b
SM
20385 }
20386 break;
d0ce17d8 20387 case DW_FORM_rnglistx:
b1829e1b 20388 {
e57933dc 20389 sect_offset rnglists_sect_off
b1829e1b
SM
20390 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20391
e57933dc 20392 attr->set_unsigned (to_underlying (rnglists_sect_off));
b1829e1b 20393 }
dda83cd7 20394 break;
18a8505e
AT
20395 case DW_FORM_strx:
20396 case DW_FORM_strx1:
20397 case DW_FORM_strx2:
20398 case DW_FORM_strx3:
20399 case DW_FORM_strx4:
20400 case DW_FORM_GNU_str_index:
20401 {
fe56917a 20402 unsigned int str_index = attr->as_unsigned_reprocess ();
c6481205 20403 gdb_assert (!attr->canonical_string_p ());
18a8505e 20404 if (reader->dwo_file != NULL)
c6481205
TT
20405 attr->set_string_noncanonical (read_dwo_str_index (reader,
20406 str_index));
18a8505e 20407 else
c6481205
TT
20408 attr->set_string_noncanonical (read_stub_str_index (cu,
20409 str_index));
18a8505e
AT
20410 break;
20411 }
20412 default:
20413 gdb_assert_not_reached (_("Unexpected DWARF form."));
20414 }
20415}
20416
a8329558 20417/* Read an attribute value described by an attribute form. */
c906108c 20418
d521ce57 20419static const gdb_byte *
dee91e82
DE
20420read_attribute_value (const struct die_reader_specs *reader,
20421 struct attribute *attr, unsigned form,
7a5f294d 20422 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 20423{
dee91e82 20424 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
20425 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20426 struct objfile *objfile = per_objfile->objfile;
dee91e82 20427 bfd *abfd = reader->abfd;
e7c27a73 20428 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
20429 unsigned int bytes_read;
20430 struct dwarf_block *blk;
20431
aead7601 20432 attr->form = (enum dwarf_form) form;
a8329558 20433 switch (form)
c906108c 20434 {
c906108c 20435 case DW_FORM_ref_addr:
ae411497 20436 if (cu->header.version == 2)
414ad644
TT
20437 attr->set_unsigned (cu->header.read_address (abfd, info_ptr,
20438 &bytes_read));
ae411497 20439 else
414ad644
TT
20440 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20441 &bytes_read));
ae411497
TT
20442 info_ptr += bytes_read;
20443 break;
36586728 20444 case DW_FORM_GNU_ref_alt:
414ad644
TT
20445 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20446 &bytes_read));
36586728
TT
20447 info_ptr += bytes_read;
20448 break;
ae411497 20449 case DW_FORM_addr:
08feed99
TT
20450 {
20451 struct gdbarch *gdbarch = objfile->arch ();
36d378cf
TT
20452 CORE_ADDR addr = cu->header.read_address (abfd, info_ptr, &bytes_read);
20453 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20454 attr->set_address (addr);
08feed99
TT
20455 info_ptr += bytes_read;
20456 }
c906108c
SS
20457 break;
20458 case DW_FORM_block2:
7b5a2f43 20459 blk = dwarf_alloc_block (cu);
c906108c
SS
20460 blk->size = read_2_bytes (abfd, info_ptr);
20461 info_ptr += 2;
20462 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20463 info_ptr += blk->size;
9d2246fc 20464 attr->set_block (blk);
c906108c
SS
20465 break;
20466 case DW_FORM_block4:
7b5a2f43 20467 blk = dwarf_alloc_block (cu);
c906108c
SS
20468 blk->size = read_4_bytes (abfd, info_ptr);
20469 info_ptr += 4;
20470 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20471 info_ptr += blk->size;
9d2246fc 20472 attr->set_block (blk);
c906108c
SS
20473 break;
20474 case DW_FORM_data2:
414ad644 20475 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
c906108c
SS
20476 info_ptr += 2;
20477 break;
20478 case DW_FORM_data4:
414ad644 20479 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
c906108c
SS
20480 info_ptr += 4;
20481 break;
20482 case DW_FORM_data8:
414ad644 20483 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
c906108c
SS
20484 info_ptr += 8;
20485 break;
0224619f
JK
20486 case DW_FORM_data16:
20487 blk = dwarf_alloc_block (cu);
20488 blk->size = 16;
20489 blk->data = read_n_bytes (abfd, info_ptr, 16);
20490 info_ptr += 16;
9d2246fc 20491 attr->set_block (blk);
0224619f 20492 break;
2dc7f7b3 20493 case DW_FORM_sec_offset:
414ad644
TT
20494 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20495 &bytes_read));
2dc7f7b3
TT
20496 info_ptr += bytes_read;
20497 break;
41144253 20498 case DW_FORM_loclistx:
20499 {
7a5f294d
TT
20500 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20501 &bytes_read));
20502 info_ptr += bytes_read;
41144253 20503 }
20504 break;
c906108c 20505 case DW_FORM_string:
c6481205
TT
20506 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20507 &bytes_read));
c906108c
SS
20508 info_ptr += bytes_read;
20509 break;
4bdf3d34 20510 case DW_FORM_strp:
36586728
TT
20511 if (!cu->per_cu->is_dwz)
20512 {
c6481205
TT
20513 attr->set_string_noncanonical
20514 (read_indirect_string (per_objfile,
20515 abfd, info_ptr, cu_header,
20516 &bytes_read));
36586728
TT
20517 info_ptr += bytes_read;
20518 break;
20519 }
20520 /* FALLTHROUGH */
43988095
JK
20521 case DW_FORM_line_strp:
20522 if (!cu->per_cu->is_dwz)
20523 {
c6481205
TT
20524 attr->set_string_noncanonical
20525 (per_objfile->read_line_string (info_ptr, cu_header,
20526 &bytes_read));
43988095
JK
20527 info_ptr += bytes_read;
20528 break;
20529 }
20530 /* FALLTHROUGH */
36586728
TT
20531 case DW_FORM_GNU_strp_alt:
20532 {
976ca316 20533 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8266302d
TT
20534 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20535 &bytes_read);
36586728 20536
c6481205
TT
20537 attr->set_string_noncanonical
20538 (dwz->read_string (objfile, str_offset));
36586728
TT
20539 info_ptr += bytes_read;
20540 }
4bdf3d34 20541 break;
2dc7f7b3 20542 case DW_FORM_exprloc:
c906108c 20543 case DW_FORM_block:
7b5a2f43 20544 blk = dwarf_alloc_block (cu);
c906108c
SS
20545 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20546 info_ptr += bytes_read;
20547 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20548 info_ptr += blk->size;
9d2246fc 20549 attr->set_block (blk);
c906108c
SS
20550 break;
20551 case DW_FORM_block1:
7b5a2f43 20552 blk = dwarf_alloc_block (cu);
c906108c
SS
20553 blk->size = read_1_byte (abfd, info_ptr);
20554 info_ptr += 1;
20555 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20556 info_ptr += blk->size;
9d2246fc 20557 attr->set_block (blk);
c906108c
SS
20558 break;
20559 case DW_FORM_data1:
c906108c 20560 case DW_FORM_flag:
414ad644 20561 attr->set_unsigned (read_1_byte (abfd, info_ptr));
c906108c
SS
20562 info_ptr += 1;
20563 break;
2dc7f7b3 20564 case DW_FORM_flag_present:
414ad644 20565 attr->set_unsigned (1);
2dc7f7b3 20566 break;
c906108c 20567 case DW_FORM_sdata:
1bc397c5 20568 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20569 info_ptr += bytes_read;
20570 break;
18a8505e 20571 case DW_FORM_rnglistx:
7a5f294d
TT
20572 {
20573 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20574 &bytes_read));
20575 info_ptr += bytes_read;
20576 }
20577 break;
d0ce17d8 20578 case DW_FORM_udata:
414ad644 20579 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20580 info_ptr += bytes_read;
20581 break;
20582 case DW_FORM_ref1:
414ad644
TT
20583 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20584 + read_1_byte (abfd, info_ptr)));
c906108c
SS
20585 info_ptr += 1;
20586 break;
20587 case DW_FORM_ref2:
414ad644
TT
20588 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20589 + read_2_bytes (abfd, info_ptr)));
c906108c
SS
20590 info_ptr += 2;
20591 break;
20592 case DW_FORM_ref4:
414ad644
TT
20593 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20594 + read_4_bytes (abfd, info_ptr)));
c906108c
SS
20595 info_ptr += 4;
20596 break;
613e1657 20597 case DW_FORM_ref8:
414ad644
TT
20598 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20599 + read_8_bytes (abfd, info_ptr)));
613e1657
KB
20600 info_ptr += 8;
20601 break;
55f1336d 20602 case DW_FORM_ref_sig8:
630ed6b9 20603 attr->set_signature (read_8_bytes (abfd, info_ptr));
348e048f
DE
20604 info_ptr += 8;
20605 break;
c906108c 20606 case DW_FORM_ref_udata:
414ad644
TT
20607 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20608 + read_unsigned_leb128 (abfd, info_ptr,
20609 &bytes_read)));
c906108c
SS
20610 info_ptr += bytes_read;
20611 break;
c906108c 20612 case DW_FORM_indirect:
a8329558
KW
20613 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20614 info_ptr += bytes_read;
43988095
JK
20615 if (form == DW_FORM_implicit_const)
20616 {
20617 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20618 info_ptr += bytes_read;
20619 }
20620 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
7a5f294d 20621 info_ptr);
43988095
JK
20622 break;
20623 case DW_FORM_implicit_const:
1bc397c5 20624 attr->set_signed (implicit_const);
a8329558 20625 break;
336d760d 20626 case DW_FORM_addrx:
3019eac3 20627 case DW_FORM_GNU_addr_index:
fe56917a
TT
20628 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20629 &bytes_read));
3019eac3
DE
20630 info_ptr += bytes_read;
20631 break;
cf532bd1 20632 case DW_FORM_strx:
15f18d14
AT
20633 case DW_FORM_strx1:
20634 case DW_FORM_strx2:
20635 case DW_FORM_strx3:
20636 case DW_FORM_strx4:
3019eac3 20637 case DW_FORM_GNU_str_index:
3019eac3 20638 {
15f18d14
AT
20639 ULONGEST str_index;
20640 if (form == DW_FORM_strx1)
20641 {
20642 str_index = read_1_byte (abfd, info_ptr);
20643 info_ptr += 1;
20644 }
20645 else if (form == DW_FORM_strx2)
20646 {
20647 str_index = read_2_bytes (abfd, info_ptr);
20648 info_ptr += 2;
20649 }
20650 else if (form == DW_FORM_strx3)
20651 {
20652 str_index = read_3_bytes (abfd, info_ptr);
20653 info_ptr += 3;
20654 }
20655 else if (form == DW_FORM_strx4)
20656 {
20657 str_index = read_4_bytes (abfd, info_ptr);
20658 info_ptr += 4;
20659 }
20660 else
20661 {
20662 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20663 info_ptr += bytes_read;
20664 }
fe56917a 20665 attr->set_unsigned_reprocess (str_index);
7a5f294d 20666 }
3019eac3 20667 break;
c906108c 20668 default:
8a3fe4f8 20669 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
20670 dwarf_form_name (form),
20671 bfd_get_filename (abfd));
c906108c 20672 }
28e94949 20673
36586728 20674 /* Super hack. */
cd6c91b4 20675 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
20676 attr->form = DW_FORM_GNU_ref_alt;
20677
28e94949
JB
20678 /* We have seen instances where the compiler tried to emit a byte
20679 size attribute of -1 which ended up being encoded as an unsigned
20680 0xffffffff. Although 0xffffffff is technically a valid size value,
20681 an object of this size seems pretty unlikely so we can relatively
20682 safely treat these cases as if the size attribute was invalid and
20683 treat them as zero by default. */
20684 if (attr->name == DW_AT_byte_size
20685 && form == DW_FORM_data4
529908cb 20686 && attr->as_unsigned () >= 0xffffffff)
01c66ae6
JB
20687 {
20688 complaint
dda83cd7
SM
20689 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20690 hex_string (attr->as_unsigned ()));
414ad644 20691 attr->set_unsigned (0);
01c66ae6 20692 }
28e94949 20693
c906108c
SS
20694 return info_ptr;
20695}
20696
a8329558
KW
20697/* Read an attribute described by an abbreviated attribute. */
20698
d521ce57 20699static const gdb_byte *
dee91e82
DE
20700read_attribute (const struct die_reader_specs *reader,
20701 struct attribute *attr, struct attr_abbrev *abbrev,
7a5f294d 20702 const gdb_byte *info_ptr)
a8329558
KW
20703{
20704 attr->name = abbrev->name;
c6481205 20705 attr->string_is_canonical = 0;
fe56917a 20706 attr->requires_reprocessing = 0;
43988095 20707 return read_attribute_value (reader, attr, abbrev->form,
7a5f294d 20708 abbrev->implicit_const, info_ptr);
a8329558
KW
20709}
20710
43988095
JK
20711/* Return pointer to string at .debug_str offset STR_OFFSET. */
20712
20713static const char *
976ca316 20714read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 20715 LONGEST str_offset)
43988095 20716{
976ca316
SM
20717 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20718 str_offset, "DW_FORM_strp");
c906108c
SS
20719}
20720
43988095
JK
20721/* Return pointer to string at .debug_str offset as read from BUF.
20722 BUF is assumed to be in a compilation unit described by CU_HEADER.
20723 Return *BYTES_READ_PTR count of bytes read from BUF. */
20724
d521ce57 20725static const char *
976ca316 20726read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 20727 const gdb_byte *buf,
cf2c3c16
TT
20728 const struct comp_unit_head *cu_header,
20729 unsigned int *bytes_read_ptr)
20730{
8266302d 20731 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 20732
976ca316 20733 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
20734}
20735
86c0bb4c 20736/* See read.h. */
43988095 20737
86c0bb4c
TT
20738const char *
20739dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
20740 const struct comp_unit_head *cu_header,
20741 unsigned int *bytes_read_ptr)
43988095 20742{
86c0bb4c 20743 bfd *abfd = objfile->obfd;
8266302d 20744 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 20745
5989a64e 20746 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
20747}
20748
3019eac3 20749/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20750 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20751 ADDR_SIZE is the size of addresses from the CU header. */
20752
20753static CORE_ADDR
976ca316
SM
20754read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20755 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 20756{
976ca316 20757 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
20758 bfd *abfd = objfile->obfd;
20759 const gdb_byte *info_ptr;
18a8505e 20760 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 20761
976ca316
SM
20762 per_objfile->per_bfd->addr.read (objfile);
20763 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 20764 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20765 objfile_name (objfile));
18a8505e 20766 if (addr_base_or_zero + addr_index * addr_size
976ca316 20767 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
20768 error (_("DW_FORM_addr_index pointing outside of "
20769 ".debug_addr section [in module %s]"),
4262abfb 20770 objfile_name (objfile));
976ca316
SM
20771 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20772 + addr_index * addr_size);
3019eac3
DE
20773 if (addr_size == 4)
20774 return bfd_get_32 (abfd, info_ptr);
20775 else
20776 return bfd_get_64 (abfd, info_ptr);
20777}
20778
20779/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20780
20781static CORE_ADDR
20782read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20783{
5e22e966 20784 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 20785 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20786}
20787
20788/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20789
20790static CORE_ADDR
d521ce57 20791read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20792 unsigned int *bytes_read)
20793{
5e22e966 20794 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
20795 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20796
20797 return read_addr_index (cu, addr_index);
20798}
20799
450a1bfc 20800/* See read.h. */
3019eac3
DE
20801
20802CORE_ADDR
82ca3f51 20803dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 20804 dwarf2_per_objfile *per_objfile,
82ca3f51 20805 unsigned int addr_index)
3019eac3 20806{
976ca316 20807 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 20808 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20809 int addr_size;
20810
3019eac3
DE
20811 /* We need addr_base and addr_size.
20812 If we don't have PER_CU->cu, we have to get it.
20813 Nasty, but the alternative is storing the needed info in PER_CU,
20814 which at this point doesn't seem justified: it's not clear how frequently
20815 it would get used and it would increase the size of every PER_CU.
20816 Entry points like dwarf2_per_cu_addr_size do a similar thing
20817 so we're not in uncharted territory here.
20818 Alas we need to be a bit more complicated as addr_base is contained
20819 in the DIE.
20820
20821 We don't need to read the entire CU(/TU).
20822 We just need the header and top level die.
a1b64ce1 20823
3019eac3 20824 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20825 For now we skip this optimization. */
3019eac3
DE
20826
20827 if (cu != NULL)
20828 {
20829 addr_base = cu->addr_base;
20830 addr_size = cu->header.addr_size;
20831 }
20832 else
20833 {
976ca316 20834 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
20835 addr_base = reader.cu->addr_base;
20836 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20837 }
20838
976ca316 20839 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
20840}
20841
18a8505e
AT
20842/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20843 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20844 DWO file. */
3019eac3 20845
d521ce57 20846static const char *
18a8505e
AT
20847read_str_index (struct dwarf2_cu *cu,
20848 struct dwarf2_section_info *str_section,
20849 struct dwarf2_section_info *str_offsets_section,
20850 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20851{
976ca316
SM
20852 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20853 struct objfile *objfile = per_objfile->objfile;
c5164cbc 20854 const char *objf_name = objfile_name (objfile);
3019eac3 20855 bfd *abfd = objfile->obfd;
d521ce57 20856 const gdb_byte *info_ptr;
3019eac3 20857 ULONGEST str_offset;
cf532bd1 20858 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20859
96b79293
TT
20860 str_section->read (objfile);
20861 str_offsets_section->read (objfile);
73869dc2 20862 if (str_section->buffer == NULL)
18a8505e 20863 error (_("%s used without %s section"
9d8780f0 20864 " in CU at offset %s [in module %s]"),
96b79293 20865 form_name, str_section->get_name (),
dda83cd7 20866 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20867 if (str_offsets_section->buffer == NULL)
18a8505e 20868 error (_("%s used without %s section"
9d8780f0 20869 " in CU at offset %s [in module %s]"),
96b79293 20870 form_name, str_section->get_name (),
dda83cd7 20871 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20872 info_ptr = (str_offsets_section->buffer
18a8505e 20873 + str_offsets_base
3019eac3
DE
20874 + str_index * cu->header.offset_size);
20875 if (cu->header.offset_size == 4)
20876 str_offset = bfd_get_32 (abfd, info_ptr);
20877 else
20878 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20879 if (str_offset >= str_section->size)
57d63ce2 20880 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20881 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20882 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20883 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20884}
20885
18a8505e
AT
20886/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20887
20888static const char *
20889read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20890{
20891 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20892 ? reader->cu->header.addr_size : 0;
20893 return read_str_index (reader->cu,
20894 &reader->dwo_file->sections.str,
20895 &reader->dwo_file->sections.str_offsets,
20896 str_offsets_base, str_index);
20897}
20898
20899/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20900
20901static const char *
20902read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20903{
5e22e966 20904 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
20905 const char *objf_name = objfile_name (objfile);
20906 static const char form_name[] = "DW_FORM_GNU_str_index";
20907 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20908
20909 if (!cu->str_offsets_base.has_value ())
20910 error (_("%s used in Fission stub without %s"
20911 " in CU at offset 0x%lx [in module %s]"),
20912 form_name, str_offsets_attr_name,
20913 (long) cu->header.offset_size, objf_name);
20914
20915 return read_str_index (cu,
5e22e966
SM
20916 &cu->per_objfile->per_bfd->str,
20917 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
20918 *cu->str_offsets_base, str_index);
20919}
20920
3019eac3
DE
20921/* Return the length of an LEB128 number in BUF. */
20922
20923static int
20924leb128_size (const gdb_byte *buf)
20925{
20926 const gdb_byte *begin = buf;
20927 gdb_byte byte;
20928
20929 while (1)
20930 {
20931 byte = *buf++;
20932 if ((byte & 128) == 0)
20933 return buf - begin;
20934 }
20935}
20936
c906108c 20937static void
e142c38c 20938set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20939{
20940 switch (lang)
20941 {
20942 case DW_LANG_C89:
76bee0cc 20943 case DW_LANG_C99:
0cfd832f 20944 case DW_LANG_C11:
c906108c 20945 case DW_LANG_C:
d1be3247 20946 case DW_LANG_UPC:
e142c38c 20947 cu->language = language_c;
c906108c 20948 break;
9c37b5ae 20949 case DW_LANG_Java:
c906108c 20950 case DW_LANG_C_plus_plus:
0cfd832f
MW
20951 case DW_LANG_C_plus_plus_11:
20952 case DW_LANG_C_plus_plus_14:
e142c38c 20953 cu->language = language_cplus;
c906108c 20954 break;
6aecb9c2
JB
20955 case DW_LANG_D:
20956 cu->language = language_d;
20957 break;
c906108c
SS
20958 case DW_LANG_Fortran77:
20959 case DW_LANG_Fortran90:
b21b22e0 20960 case DW_LANG_Fortran95:
f7de9aab
MW
20961 case DW_LANG_Fortran03:
20962 case DW_LANG_Fortran08:
e142c38c 20963 cu->language = language_fortran;
c906108c 20964 break;
a766d390
DE
20965 case DW_LANG_Go:
20966 cu->language = language_go;
20967 break;
c906108c 20968 case DW_LANG_Mips_Assembler:
e142c38c 20969 cu->language = language_asm;
c906108c
SS
20970 break;
20971 case DW_LANG_Ada83:
8aaf0b47 20972 case DW_LANG_Ada95:
bc5f45f8
JB
20973 cu->language = language_ada;
20974 break;
72019c9c
GM
20975 case DW_LANG_Modula2:
20976 cu->language = language_m2;
20977 break;
fe8e67fd
PM
20978 case DW_LANG_Pascal83:
20979 cu->language = language_pascal;
20980 break;
22566fbd
DJ
20981 case DW_LANG_ObjC:
20982 cu->language = language_objc;
20983 break;
c44af4eb
TT
20984 case DW_LANG_Rust:
20985 case DW_LANG_Rust_old:
20986 cu->language = language_rust;
20987 break;
c906108c
SS
20988 case DW_LANG_Cobol74:
20989 case DW_LANG_Cobol85:
c906108c 20990 default:
e142c38c 20991 cu->language = language_minimal;
c906108c
SS
20992 break;
20993 }
e142c38c 20994 cu->language_defn = language_def (cu->language);
c906108c
SS
20995}
20996
20997/* Return the named attribute or NULL if not there. */
20998
20999static struct attribute *
e142c38c 21000dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 21001{
a48e046c 21002 for (;;)
c906108c 21003 {
a48e046c
TT
21004 unsigned int i;
21005 struct attribute *spec = NULL;
21006
21007 for (i = 0; i < die->num_attrs; ++i)
21008 {
21009 if (die->attrs[i].name == name)
21010 return &die->attrs[i];
21011 if (die->attrs[i].name == DW_AT_specification
21012 || die->attrs[i].name == DW_AT_abstract_origin)
21013 spec = &die->attrs[i];
21014 }
21015
21016 if (!spec)
21017 break;
c906108c 21018
f2f0e013 21019 die = follow_die_ref (die, spec, &cu);
f2f0e013 21020 }
c5aa993b 21021
c906108c
SS
21022 return NULL;
21023}
21024
7d45c7c3
KB
21025/* Return the string associated with a string-typed attribute, or NULL if it
21026 is either not found or is of an incorrect type. */
21027
21028static const char *
21029dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21030{
21031 struct attribute *attr;
21032 const char *str = NULL;
21033
21034 attr = dwarf2_attr (die, name, cu);
21035
21036 if (attr != NULL)
21037 {
95f982e5 21038 str = attr->as_string ();
e61108c9 21039 if (str == nullptr)
dda83cd7 21040 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
21041 "DIE at %s in module %s"),
21042 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 21043 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
21044 }
21045
21046 return str;
21047}
21048
a084a2a6 21049/* Return the dwo name or NULL if not present. If present, it is in either
85102364 21050 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
21051static const char *
21052dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
21053{
21054 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
21055 if (dwo_name == nullptr)
21056 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
21057 return dwo_name;
21058}
21059
05cf31d1
JB
21060/* Return non-zero iff the attribute NAME is defined for the given DIE,
21061 and holds a non-zero value. This function should only be used for
2dc7f7b3 21062 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
21063
21064static int
21065dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
21066{
21067 struct attribute *attr = dwarf2_attr (die, name, cu);
21068
c45bc3f8 21069 return attr != nullptr && attr->as_boolean ();
05cf31d1
JB
21070}
21071
3ca72b44 21072static int
e142c38c 21073die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 21074{
05cf31d1
JB
21075 /* A DIE is a declaration if it has a DW_AT_declaration attribute
21076 which value is non-zero. However, we have to be careful with
21077 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
21078 (via dwarf2_flag_true_p) follows this attribute. So we may
21079 end up accidently finding a declaration attribute that belongs
21080 to a different DIE referenced by the specification attribute,
21081 even though the given DIE does not have a declaration attribute. */
21082 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
21083 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
21084}
21085
63d06c5c 21086/* Return the die giving the specification for DIE, if there is
f2f0e013 21087 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
21088 containing the return value on output. If there is no
21089 specification, but there is an abstract origin, that is
21090 returned. */
63d06c5c
DC
21091
21092static struct die_info *
f2f0e013 21093die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 21094{
f2f0e013
DJ
21095 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
21096 *spec_cu);
63d06c5c 21097
edb3359d
DJ
21098 if (spec_attr == NULL)
21099 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
21100
63d06c5c
DC
21101 if (spec_attr == NULL)
21102 return NULL;
21103 else
f2f0e013 21104 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 21105}
c906108c 21106
527f3840
JK
21107/* Stub for free_line_header to match void * callback types. */
21108
21109static void
21110free_line_header_voidp (void *arg)
21111{
9a3c8263 21112 struct line_header *lh = (struct line_header *) arg;
527f3840 21113
fff8551c 21114 delete lh;
527f3840
JK
21115}
21116
83769d0b 21117/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
21118
21119static struct dwarf2_section_info *
21120get_debug_line_section (struct dwarf2_cu *cu)
21121{
21122 struct dwarf2_section_info *section;
976ca316 21123 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
21124
21125 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
21126 DWO file. */
21127 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21128 section = &cu->dwo_unit->dwo_file->sections.line;
21129 else if (cu->per_cu->is_dwz)
21130 {
976ca316 21131 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
36586728
TT
21132
21133 section = &dwz->line;
21134 }
21135 else
976ca316 21136 section = &per_objfile->per_bfd->line;
36586728
TT
21137
21138 return section;
21139}
21140
debd256d 21141/* Read the statement program header starting at OFFSET in
3019eac3 21142 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 21143 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
21144 Returns NULL if there is a problem reading the header, e.g., if it
21145 has a version we don't understand.
debd256d
JB
21146
21147 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
21148 the returned object point into the dwarf line section buffer,
21149 and must not be freed. */
ae2de4f8 21150
fff8551c 21151static line_header_up
9c541725 21152dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 21153{
3019eac3 21154 struct dwarf2_section_info *section;
976ca316 21155 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 21156
36586728 21157 section = get_debug_line_section (cu);
976ca316 21158 section->read (per_objfile->objfile);
3019eac3 21159 if (section->buffer == NULL)
debd256d 21160 {
3019eac3 21161 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 21162 complaint (_("missing .debug_line.dwo section"));
3019eac3 21163 else
b98664d3 21164 complaint (_("missing .debug_line section"));
debd256d
JB
21165 return 0;
21166 }
21167
0df7ad3a 21168 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 21169 per_objfile, section, &cu->header);
debd256d 21170}
c906108c 21171
c6da4cef 21172/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 21173 Return the file name of the psymtab for the given file_entry.
c6da4cef 21174 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
21175 If space for the result is malloc'd, *NAME_HOLDER will be set.
21176 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 21177
d521ce57 21178static const char *
7ba99d21 21179psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 21180 const dwarf2_psymtab *pst,
c89b44cd
TT
21181 const char *comp_dir,
21182 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 21183{
d521ce57
TT
21184 const char *include_name = fe.name;
21185 const char *include_name_to_compare = include_name;
72b9f47f 21186 const char *pst_filename;
c6da4cef
DE
21187 int file_is_pst;
21188
8c43009f 21189 const char *dir_name = fe.include_dir (lh);
c6da4cef 21190
c89b44cd 21191 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
21192 if (!IS_ABSOLUTE_PATH (include_name)
21193 && (dir_name != NULL || comp_dir != NULL))
21194 {
21195 /* Avoid creating a duplicate psymtab for PST.
21196 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21197 Before we do the comparison, however, we need to account
21198 for DIR_NAME and COMP_DIR.
21199 First prepend dir_name (if non-NULL). If we still don't
21200 have an absolute path prepend comp_dir (if non-NULL).
21201 However, the directory we record in the include-file's
21202 psymtab does not contain COMP_DIR (to match the
21203 corresponding symtab(s)).
21204
21205 Example:
21206
21207 bash$ cd /tmp
21208 bash$ gcc -g ./hello.c
21209 include_name = "hello.c"
21210 dir_name = "."
21211 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
21212 DW_AT_name = "./hello.c"
21213
21214 */
c6da4cef
DE
21215
21216 if (dir_name != NULL)
21217 {
c89b44cd
TT
21218 name_holder->reset (concat (dir_name, SLASH_STRING,
21219 include_name, (char *) NULL));
21220 include_name = name_holder->get ();
c6da4cef 21221 include_name_to_compare = include_name;
c6da4cef
DE
21222 }
21223 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21224 {
c89b44cd
TT
21225 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21226 include_name, (char *) NULL));
21227 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
21228 }
21229 }
21230
21231 pst_filename = pst->filename;
c89b44cd 21232 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
21233 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21234 {
c89b44cd
TT
21235 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21236 pst_filename, (char *) NULL));
21237 pst_filename = copied_name.get ();
c6da4cef
DE
21238 }
21239
1e3fad37 21240 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 21241
c6da4cef
DE
21242 if (file_is_pst)
21243 return NULL;
21244 return include_name;
21245}
21246
d9b3de22
DE
21247/* State machine to track the state of the line number program. */
21248
6f77053d 21249class lnp_state_machine
d9b3de22 21250{
6f77053d
PA
21251public:
21252 /* Initialize a machine state for the start of a line number
21253 program. */
804d2729
TT
21254 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21255 bool record_lines_p);
6f77053d 21256
8c43009f
PA
21257 file_entry *current_file ()
21258 {
21259 /* lh->file_names is 0-based, but the file name numbers in the
21260 statement program are 1-based. */
6f77053d
PA
21261 return m_line_header->file_name_at (m_file);
21262 }
21263
21264 /* Record the line in the state machine. END_SEQUENCE is true if
21265 we're processing the end of a sequence. */
21266 void record_line (bool end_sequence);
21267
a8caed5d 21268 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
7ab6656f 21269 nop-out rest of the lines in this sequence. */
6f77053d
PA
21270 void check_line_address (struct dwarf2_cu *cu,
21271 const gdb_byte *line_ptr,
7ab6656f 21272 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
21273
21274 void handle_set_discriminator (unsigned int discriminator)
21275 {
21276 m_discriminator = discriminator;
21277 m_line_has_non_zero_discriminator |= discriminator != 0;
21278 }
21279
21280 /* Handle DW_LNE_set_address. */
21281 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21282 {
21283 m_op_index = 0;
21284 address += baseaddr;
21285 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21286 }
21287
21288 /* Handle DW_LNS_advance_pc. */
21289 void handle_advance_pc (CORE_ADDR adjust);
21290
21291 /* Handle a special opcode. */
21292 void handle_special_opcode (unsigned char op_code);
21293
21294 /* Handle DW_LNS_advance_line. */
21295 void handle_advance_line (int line_delta)
21296 {
21297 advance_line (line_delta);
21298 }
21299
21300 /* Handle DW_LNS_set_file. */
21301 void handle_set_file (file_name_index file);
21302
21303 /* Handle DW_LNS_negate_stmt. */
21304 void handle_negate_stmt ()
21305 {
21306 m_is_stmt = !m_is_stmt;
21307 }
21308
21309 /* Handle DW_LNS_const_add_pc. */
21310 void handle_const_add_pc ();
21311
21312 /* Handle DW_LNS_fixed_advance_pc. */
21313 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21314 {
21315 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21316 m_op_index = 0;
21317 }
21318
21319 /* Handle DW_LNS_copy. */
21320 void handle_copy ()
21321 {
21322 record_line (false);
21323 m_discriminator = 0;
21324 }
21325
21326 /* Handle DW_LNE_end_sequence. */
21327 void handle_end_sequence ()
21328 {
804d2729 21329 m_currently_recording_lines = true;
6f77053d
PA
21330 }
21331
21332private:
21333 /* Advance the line by LINE_DELTA. */
21334 void advance_line (int line_delta)
21335 {
21336 m_line += line_delta;
21337
21338 if (line_delta != 0)
21339 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21340 }
21341
804d2729
TT
21342 struct dwarf2_cu *m_cu;
21343
6f77053d
PA
21344 gdbarch *m_gdbarch;
21345
21346 /* True if we're recording lines.
21347 Otherwise we're building partial symtabs and are just interested in
21348 finding include files mentioned by the line number program. */
21349 bool m_record_lines_p;
21350
8c43009f 21351 /* The line number header. */
6f77053d 21352 line_header *m_line_header;
8c43009f 21353
6f77053d
PA
21354 /* These are part of the standard DWARF line number state machine,
21355 and initialized according to the DWARF spec. */
d9b3de22 21356
6f77053d 21357 unsigned char m_op_index = 0;
7ba99d21
AT
21358 /* The line table index of the current file. */
21359 file_name_index m_file = 1;
6f77053d
PA
21360 unsigned int m_line = 1;
21361
21362 /* These are initialized in the constructor. */
21363
21364 CORE_ADDR m_address;
21365 bool m_is_stmt;
21366 unsigned int m_discriminator;
d9b3de22
DE
21367
21368 /* Additional bits of state we need to track. */
21369
21370 /* The last file that we called dwarf2_start_subfile for.
21371 This is only used for TLLs. */
6f77053d 21372 unsigned int m_last_file = 0;
d9b3de22 21373 /* The last file a line number was recorded for. */
6f77053d 21374 struct subfile *m_last_subfile = NULL;
d9b3de22 21375
1313c56e
AB
21376 /* The address of the last line entry. */
21377 CORE_ADDR m_last_address;
21378
21379 /* Set to true when a previous line at the same address (using
21380 m_last_address) had m_is_stmt true. This is reset to false when a
21381 line entry at a new address (m_address different to m_last_address) is
21382 processed. */
21383 bool m_stmt_at_address = false;
21384
804d2729
TT
21385 /* When true, record the lines we decode. */
21386 bool m_currently_recording_lines = false;
d9b3de22
DE
21387
21388 /* The last line number that was recorded, used to coalesce
21389 consecutive entries for the same line. This can happen, for
21390 example, when discriminators are present. PR 17276. */
6f77053d
PA
21391 unsigned int m_last_line = 0;
21392 bool m_line_has_non_zero_discriminator = false;
8c43009f 21393};
d9b3de22 21394
6f77053d
PA
21395void
21396lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21397{
21398 CORE_ADDR addr_adj = (((m_op_index + adjust)
21399 / m_line_header->maximum_ops_per_instruction)
21400 * m_line_header->minimum_instruction_length);
21401 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21402 m_op_index = ((m_op_index + adjust)
21403 % m_line_header->maximum_ops_per_instruction);
21404}
d9b3de22 21405
6f77053d
PA
21406void
21407lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21408{
6f77053d 21409 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
21410 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21411 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21412 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
21413 / m_line_header->maximum_ops_per_instruction)
21414 * m_line_header->minimum_instruction_length);
21415 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 21416 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 21417 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21418
258bf0ee 21419 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
21420 advance_line (line_delta);
21421 record_line (false);
21422 m_discriminator = 0;
21423}
d9b3de22 21424
6f77053d
PA
21425void
21426lnp_state_machine::handle_set_file (file_name_index file)
21427{
21428 m_file = file;
21429
21430 const file_entry *fe = current_file ();
21431 if (fe == NULL)
21432 dwarf2_debug_line_missing_file_complaint ();
21433 else if (m_record_lines_p)
21434 {
21435 const char *dir = fe->include_dir (m_line_header);
21436
c24bdb02 21437 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21438 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21439 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21440 }
21441}
21442
21443void
21444lnp_state_machine::handle_const_add_pc ()
21445{
21446 CORE_ADDR adjust
21447 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21448
21449 CORE_ADDR addr_adj
21450 = (((m_op_index + adjust)
21451 / m_line_header->maximum_ops_per_instruction)
21452 * m_line_header->minimum_instruction_length);
21453
21454 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21455 m_op_index = ((m_op_index + adjust)
21456 % m_line_header->maximum_ops_per_instruction);
21457}
d9b3de22 21458
a05a36a5
DE
21459/* Return non-zero if we should add LINE to the line number table.
21460 LINE is the line to add, LAST_LINE is the last line that was added,
21461 LAST_SUBFILE is the subfile for LAST_LINE.
21462 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21463 had a non-zero discriminator.
21464
21465 We have to be careful in the presence of discriminators.
21466 E.g., for this line:
21467
21468 for (i = 0; i < 100000; i++);
21469
21470 clang can emit four line number entries for that one line,
21471 each with a different discriminator.
21472 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21473
21474 However, we want gdb to coalesce all four entries into one.
21475 Otherwise the user could stepi into the middle of the line and
21476 gdb would get confused about whether the pc really was in the
21477 middle of the line.
21478
21479 Things are further complicated by the fact that two consecutive
21480 line number entries for the same line is a heuristic used by gcc
21481 to denote the end of the prologue. So we can't just discard duplicate
21482 entries, we have to be selective about it. The heuristic we use is
21483 that we only collapse consecutive entries for the same line if at least
21484 one of those entries has a non-zero discriminator. PR 17276.
21485
21486 Note: Addresses in the line number state machine can never go backwards
21487 within one sequence, thus this coalescing is ok. */
21488
21489static int
804d2729
TT
21490dwarf_record_line_p (struct dwarf2_cu *cu,
21491 unsigned int line, unsigned int last_line,
a05a36a5
DE
21492 int line_has_non_zero_discriminator,
21493 struct subfile *last_subfile)
21494{
c24bdb02 21495 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21496 return 1;
21497 if (line != last_line)
21498 return 1;
21499 /* Same line for the same file that we've seen already.
21500 As a last check, for pr 17276, only record the line if the line
21501 has never had a non-zero discriminator. */
21502 if (!line_has_non_zero_discriminator)
21503 return 1;
21504 return 0;
21505}
21506
804d2729
TT
21507/* Use the CU's builder to record line number LINE beginning at
21508 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21509
21510static void
d9b3de22 21511dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 21512 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 21513 struct dwarf2_cu *cu)
252a6764
DE
21514{
21515 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21516
27e0867f
DE
21517 if (dwarf_line_debug)
21518 {
21519 fprintf_unfiltered (gdb_stdlog,
21520 "Recording line %u, file %s, address %s\n",
21521 line, lbasename (subfile->name),
21522 paddress (gdbarch, address));
21523 }
21524
804d2729 21525 if (cu != nullptr)
8c95582d 21526 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
21527}
21528
21529/* Subroutine of dwarf_decode_lines_1 to simplify it.
21530 Mark the end of a set of line number records.
d9b3de22 21531 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21532 If SUBFILE is NULL the request is ignored. */
21533
21534static void
21535dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21536 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21537{
27e0867f
DE
21538 if (subfile == NULL)
21539 return;
21540
21541 if (dwarf_line_debug)
21542 {
21543 fprintf_unfiltered (gdb_stdlog,
21544 "Finishing current line, file %s, address %s\n",
21545 lbasename (subfile->name),
21546 paddress (gdbarch, address));
21547 }
21548
8c95582d 21549 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
21550}
21551
6f77053d
PA
21552void
21553lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21554{
d9b3de22
DE
21555 if (dwarf_line_debug)
21556 {
21557 fprintf_unfiltered (gdb_stdlog,
21558 "Processing actual line %u: file %u,"
94a72be7 21559 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 21560 m_line, m_file,
6f77053d 21561 paddress (m_gdbarch, m_address),
94a72be7
AB
21562 m_is_stmt, m_discriminator,
21563 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
21564 }
21565
6f77053d 21566 file_entry *fe = current_file ();
8c43009f
PA
21567
21568 if (fe == NULL)
d9b3de22
DE
21569 dwarf2_debug_line_missing_file_complaint ();
21570 /* For now we ignore lines not starting on an instruction boundary.
21571 But not when processing end_sequence for compatibility with the
21572 previous version of the code. */
6f77053d 21573 else if (m_op_index == 0 || end_sequence)
d9b3de22 21574 {
8c43009f 21575 fe->included_p = 1;
8c95582d 21576 if (m_record_lines_p)
d9b3de22 21577 {
1313c56e
AB
21578 /* When we switch files we insert an end maker in the first file,
21579 switch to the second file and add a new line entry. The
21580 problem is that the end marker inserted in the first file will
21581 discard any previous line entries at the same address. If the
21582 line entries in the first file are marked as is-stmt, while
21583 the new line in the second file is non-stmt, then this means
21584 the end marker will discard is-stmt lines so we can have a
21585 non-stmt line. This means that there are less addresses at
21586 which the user can insert a breakpoint.
21587
21588 To improve this we track the last address in m_last_address,
21589 and whether we have seen an is-stmt at this address. Then
21590 when switching files, if we have seen a stmt at the current
21591 address, and we are switching to create a non-stmt line, then
21592 discard the new line. */
21593 bool file_changed
21594 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21595 bool ignore_this_line
dda83cd7
SM
21596 = ((file_changed && !end_sequence && m_last_address == m_address
21597 && !m_is_stmt && m_stmt_at_address)
21598 || (!end_sequence && m_line == 0));
1313c56e
AB
21599
21600 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 21601 {
804d2729
TT
21602 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21603 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21604 }
21605
1313c56e 21606 if (!end_sequence && !ignore_this_line)
d9b3de22 21607 {
8c95582d
AB
21608 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21609
804d2729 21610 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21611 m_line_has_non_zero_discriminator,
21612 m_last_subfile))
d9b3de22 21613 {
c24bdb02 21614 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21615 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21616 builder->get_current_subfile (),
8c95582d 21617 m_line, m_address, is_stmt,
804d2729 21618 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21619 }
c24bdb02 21620 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21621 m_last_line = m_line;
d9b3de22
DE
21622 }
21623 }
21624 }
1313c56e
AB
21625
21626 /* Track whether we have seen any m_is_stmt true at m_address in case we
21627 have multiple line table entries all at m_address. */
21628 if (m_last_address != m_address)
21629 {
21630 m_stmt_at_address = false;
21631 m_last_address = m_address;
21632 }
21633 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
21634}
21635
804d2729
TT
21636lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21637 line_header *lh, bool record_lines_p)
d9b3de22 21638{
804d2729 21639 m_cu = cu;
6f77053d
PA
21640 m_gdbarch = arch;
21641 m_record_lines_p = record_lines_p;
21642 m_line_header = lh;
d9b3de22 21643
804d2729 21644 m_currently_recording_lines = true;
d9b3de22 21645
d9b3de22
DE
21646 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21647 was a line entry for it so that the backend has a chance to adjust it
21648 and also record it in case it needs it. This is currently used by MIPS
21649 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21650 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21651 m_is_stmt = lh->default_is_stmt;
21652 m_discriminator = 0;
1313c56e
AB
21653
21654 m_last_address = m_address;
21655 m_stmt_at_address = false;
252a6764
DE
21656}
21657
6f77053d
PA
21658void
21659lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21660 const gdb_byte *line_ptr,
7ab6656f 21661 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21662{
a8caed5d
FS
21663 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21664 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21665 located at 0x0. In this case, additionally check that if
21666 ADDRESS < UNRELOCATED_LOWPC. */
924c2928 21667
a8caed5d
FS
21668 if ((address == 0 && address < unrelocated_lowpc)
21669 || address == (CORE_ADDR) -1)
924c2928
DE
21670 {
21671 /* This line table is for a function which has been
21672 GCd by the linker. Ignore it. PR gdb/12528 */
21673
5e22e966 21674 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
21675 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21676
b98664d3 21677 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21678 line_offset, objfile_name (objfile));
804d2729
TT
21679 m_currently_recording_lines = false;
21680 /* Note: m_currently_recording_lines is left as false until we see
21681 DW_LNE_end_sequence. */
924c2928
DE
21682 }
21683}
21684
f3f5162e 21685/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21686 Process the line number information in LH.
21687 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21688 program in order to set included_p for every referenced header. */
debd256d 21689
c906108c 21690static void
43f3e411
DE
21691dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21692 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21693{
d521ce57
TT
21694 const gdb_byte *line_ptr, *extended_end;
21695 const gdb_byte *line_end;
a8c50c1f 21696 unsigned int bytes_read, extended_len;
699ca60a 21697 unsigned char op_code, extended_op;
e142c38c 21698 CORE_ADDR baseaddr;
5e22e966 21699 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21700 bfd *abfd = objfile->obfd;
08feed99 21701 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
21702 /* True if we're recording line info (as opposed to building partial
21703 symtabs and just interested in finding include files mentioned by
21704 the line number program). */
21705 bool record_lines_p = !decode_for_pst_p;
e142c38c 21706
b3b3bada 21707 baseaddr = objfile->text_section_offset ();
c906108c 21708
debd256d
JB
21709 line_ptr = lh->statement_program_start;
21710 line_end = lh->statement_program_end;
c906108c
SS
21711
21712 /* Read the statement sequences until there's nothing left. */
21713 while (line_ptr < line_end)
21714 {
6f77053d
PA
21715 /* The DWARF line number program state machine. Reset the state
21716 machine at the start of each sequence. */
804d2729 21717 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21718 bool end_sequence = false;
d9b3de22 21719
8c43009f 21720 if (record_lines_p)
c906108c 21721 {
8c43009f
PA
21722 /* Start a subfile for the current file of the state
21723 machine. */
21724 const file_entry *fe = state_machine.current_file ();
21725
21726 if (fe != NULL)
804d2729 21727 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21728 }
21729
a738430d 21730 /* Decode the table. */
d9b3de22 21731 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21732 {
21733 op_code = read_1_byte (abfd, line_ptr);
21734 line_ptr += 1;
9aa1fe7e 21735
debd256d 21736 if (op_code >= lh->opcode_base)
6e70227d 21737 {
8e07a239 21738 /* Special opcode. */
6f77053d 21739 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21740 }
21741 else switch (op_code)
c906108c
SS
21742 {
21743 case DW_LNS_extended_op:
3e43a32a
MS
21744 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21745 &bytes_read);
473b7be6 21746 line_ptr += bytes_read;
a8c50c1f 21747 extended_end = line_ptr + extended_len;
c906108c
SS
21748 extended_op = read_1_byte (abfd, line_ptr);
21749 line_ptr += 1;
8f34b746
TV
21750 if (DW_LNE_lo_user <= extended_op
21751 && extended_op <= DW_LNE_hi_user)
21752 {
21753 /* Vendor extension, ignore. */
21754 line_ptr = extended_end;
21755 break;
21756 }
c906108c
SS
21757 switch (extended_op)
21758 {
21759 case DW_LNE_end_sequence:
6f77053d
PA
21760 state_machine.handle_end_sequence ();
21761 end_sequence = true;
c906108c
SS
21762 break;
21763 case DW_LNE_set_address:
d9b3de22
DE
21764 {
21765 CORE_ADDR address
c8a7a66f 21766 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 21767 line_ptr += bytes_read;
6f77053d
PA
21768
21769 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21770 lowpc - baseaddr, address);
6f77053d 21771 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21772 }
c906108c
SS
21773 break;
21774 case DW_LNE_define_file:
dda83cd7
SM
21775 {
21776 const char *cur_file;
ecfb656c
PA
21777 unsigned int mod_time, length;
21778 dir_index dindex;
6e70227d 21779
dda83cd7 21780 cur_file = read_direct_string (abfd, line_ptr,
3e43a32a 21781 &bytes_read);
dda83cd7
SM
21782 line_ptr += bytes_read;
21783 dindex = (dir_index)
21784 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21785 line_ptr += bytes_read;
21786 mod_time =
21787 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21788 line_ptr += bytes_read;
21789 length =
21790 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21791 line_ptr += bytes_read;
21792 lh->add_file_name (cur_file, dindex, mod_time, length);
21793 }
c906108c 21794 break;
d0c6ba3d 21795 case DW_LNE_set_discriminator:
6f77053d
PA
21796 {
21797 /* The discriminator is not interesting to the
21798 debugger; just ignore it. We still need to
21799 check its value though:
21800 if there are consecutive entries for the same
21801 (non-prologue) line we want to coalesce them.
21802 PR 17276. */
21803 unsigned int discr
21804 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21805 line_ptr += bytes_read;
21806
21807 state_machine.handle_set_discriminator (discr);
21808 }
d0c6ba3d 21809 break;
c906108c 21810 default:
b98664d3 21811 complaint (_("mangled .debug_line section"));
debd256d 21812 return;
c906108c 21813 }
a8c50c1f
DJ
21814 /* Make sure that we parsed the extended op correctly. If e.g.
21815 we expected a different address size than the producer used,
21816 we may have read the wrong number of bytes. */
21817 if (line_ptr != extended_end)
21818 {
b98664d3 21819 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21820 return;
21821 }
c906108c
SS
21822 break;
21823 case DW_LNS_copy:
6f77053d 21824 state_machine.handle_copy ();
c906108c
SS
21825 break;
21826 case DW_LNS_advance_pc:
2dc7f7b3
TT
21827 {
21828 CORE_ADDR adjust
21829 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21830 line_ptr += bytes_read;
6f77053d
PA
21831
21832 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21833 }
c906108c
SS
21834 break;
21835 case DW_LNS_advance_line:
a05a36a5
DE
21836 {
21837 int line_delta
21838 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21839 line_ptr += bytes_read;
6f77053d
PA
21840
21841 state_machine.handle_advance_line (line_delta);
a05a36a5 21842 }
c906108c
SS
21843 break;
21844 case DW_LNS_set_file:
d9b3de22 21845 {
6f77053d 21846 file_name_index file
ecfb656c
PA
21847 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21848 &bytes_read);
d9b3de22 21849 line_ptr += bytes_read;
8c43009f 21850
6f77053d 21851 state_machine.handle_set_file (file);
d9b3de22 21852 }
c906108c
SS
21853 break;
21854 case DW_LNS_set_column:
0ad93d4f 21855 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21856 line_ptr += bytes_read;
21857 break;
21858 case DW_LNS_negate_stmt:
6f77053d 21859 state_machine.handle_negate_stmt ();
c906108c
SS
21860 break;
21861 case DW_LNS_set_basic_block:
c906108c 21862 break;
c2c6d25f
JM
21863 /* Add to the address register of the state machine the
21864 address increment value corresponding to special opcode
a738430d
MK
21865 255. I.e., this value is scaled by the minimum
21866 instruction length since special opcode 255 would have
b021a221 21867 scaled the increment. */
c906108c 21868 case DW_LNS_const_add_pc:
6f77053d 21869 state_machine.handle_const_add_pc ();
c906108c
SS
21870 break;
21871 case DW_LNS_fixed_advance_pc:
3e29f34a 21872 {
6f77053d 21873 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21874 line_ptr += 2;
6f77053d
PA
21875
21876 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21877 }
c906108c 21878 break;
9aa1fe7e 21879 default:
a738430d
MK
21880 {
21881 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21882 int i;
a738430d 21883
debd256d 21884 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21885 {
21886 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21887 line_ptr += bytes_read;
21888 }
21889 }
c906108c
SS
21890 }
21891 }
d9b3de22
DE
21892
21893 if (!end_sequence)
21894 dwarf2_debug_line_missing_end_sequence_complaint ();
21895
21896 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21897 in which case we still finish recording the last line). */
6f77053d 21898 state_machine.record_line (true);
c906108c 21899 }
f3f5162e
DE
21900}
21901
21902/* Decode the Line Number Program (LNP) for the given line_header
21903 structure and CU. The actual information extracted and the type
21904 of structures created from the LNP depends on the value of PST.
21905
21906 1. If PST is NULL, then this procedure uses the data from the program
21907 to create all necessary symbol tables, and their linetables.
21908
21909 2. If PST is not NULL, this procedure reads the program to determine
21910 the list of files included by the unit represented by PST, and
21911 builds all the associated partial symbol tables.
21912
21913 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21914 It is used for relative paths in the line table.
21915 NOTE: When processing partial symtabs (pst != NULL),
21916 comp_dir == pst->dirname.
21917
21918 NOTE: It is important that psymtabs have the same file name (via strcmp)
21919 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21920 symtab we don't use it in the name of the psymtabs we create.
21921 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21922 A good testcase for this is mb-inline.exp.
21923
527f3840
JK
21924 LOWPC is the lowest address in CU (or 0 if not known).
21925
21926 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21927 for its PC<->lines mapping information. Otherwise only the filename
21928 table is read in. */
f3f5162e
DE
21929
21930static void
21931dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21932 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21933 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21934{
5e22e966 21935 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21936 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21937
527f3840
JK
21938 if (decode_mapping)
21939 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21940
21941 if (decode_for_pst_p)
21942 {
aaa75496 21943 /* Now that we're done scanning the Line Header Program, we can
dda83cd7 21944 create the psymtab of each included file. */
7ba99d21 21945 for (auto &file_entry : lh->file_names ())
dda83cd7
SM
21946 if (file_entry.included_p == 1)
21947 {
c89b44cd 21948 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21949 const char *include_name =
7ba99d21
AT
21950 psymtab_include_file_name (lh, file_entry, pst,
21951 comp_dir, &name_holder);
c6da4cef 21952 if (include_name != NULL)
dda83cd7
SM
21953 dwarf2_create_include_psymtab (include_name, pst, objfile);
21954 }
aaa75496 21955 }
cb1df416
DJ
21956 else
21957 {
21958 /* Make sure a symtab is created for every file, even files
21959 which contain only variables (i.e. no code with associated
21960 line numbers). */
c24bdb02
KS
21961 buildsym_compunit *builder = cu->get_builder ();
21962 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21963
7ba99d21 21964 for (auto &fe : lh->file_names ())
cb1df416 21965 {
804d2729 21966 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21967 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21968 {
c24bdb02 21969 builder->get_current_subfile ()->symtab
804d2729 21970 = allocate_symtab (cust,
c24bdb02 21971 builder->get_current_subfile ()->name);
43f3e411 21972 }
c24bdb02 21973 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21974 }
21975 }
c906108c
SS
21976}
21977
21978/* Start a subfile for DWARF. FILENAME is the name of the file and
21979 DIRNAME the name of the source directory which contains FILENAME
4d663531 21980 or NULL if not known.
c906108c
SS
21981 This routine tries to keep line numbers from identical absolute and
21982 relative file names in a common subfile.
21983
21984 Using the `list' example from the GDB testsuite, which resides in
21985 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21986 of /srcdir/list0.c yields the following debugging information for list0.c:
21987
c5aa993b 21988 DW_AT_name: /srcdir/list0.c
4d663531 21989 DW_AT_comp_dir: /compdir
357e46e7 21990 files.files[0].name: list0.h
c5aa993b 21991 files.files[0].dir: /srcdir
357e46e7 21992 files.files[1].name: list0.c
c5aa993b 21993 files.files[1].dir: /srcdir
c906108c
SS
21994
21995 The line number information for list0.c has to end up in a single
4f1520fb
FR
21996 subfile, so that `break /srcdir/list0.c:1' works as expected.
21997 start_subfile will ensure that this happens provided that we pass the
21998 concatenation of files.files[1].dir and files.files[1].name as the
21999 subfile's name. */
c906108c
SS
22000
22001static void
804d2729
TT
22002dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
22003 const char *dirname)
c906108c 22004{
43816ebc 22005 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 22006
4d663531 22007 /* In order not to lose the line information directory,
4f1520fb
FR
22008 we concatenate it to the filename when it makes sense.
22009 Note that the Dwarf3 standard says (speaking of filenames in line
22010 information): ``The directory index is ignored for file names
22011 that represent full path names''. Thus ignoring dirname in the
22012 `else' branch below isn't an issue. */
c906108c 22013
d5166ae1 22014 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 22015 {
43816ebc
TT
22016 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
22017 filename = copy.get ();
d521ce57 22018 }
c906108c 22019
c24bdb02 22020 cu->get_builder ()->start_subfile (filename);
c906108c
SS
22021}
22022
804d2729
TT
22023/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
22024 buildsym_compunit constructor. */
f4dc4d17 22025
c24bdb02
KS
22026struct compunit_symtab *
22027dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
22028 CORE_ADDR low_pc)
f4dc4d17 22029{
c24bdb02 22030 gdb_assert (m_builder == nullptr);
43f3e411 22031
c24bdb02 22032 m_builder.reset (new struct buildsym_compunit
f6e649dd 22033 (this->per_objfile->objfile,
c24bdb02 22034 name, comp_dir, language, low_pc));
93b8bea4 22035
c24bdb02 22036 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 22037
c24bdb02
KS
22038 get_builder ()->record_debugformat ("DWARF 2");
22039 get_builder ()->record_producer (producer);
f4dc4d17 22040
c24bdb02 22041 processing_has_namespace_info = false;
43f3e411 22042
c24bdb02 22043 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
22044}
22045
4c2df51b
DJ
22046static void
22047var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 22048 struct dwarf2_cu *cu)
4c2df51b 22049{
5e22e966 22050 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
22051 struct comp_unit_head *cu_header = &cu->header;
22052
4c2df51b
DJ
22053 /* NOTE drow/2003-01-30: There used to be a comment and some special
22054 code here to turn a symbol with DW_AT_external and a
22055 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
22056 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
22057 with some versions of binutils) where shared libraries could have
22058 relocations against symbols in their debug information - the
22059 minimal symbol would have the right address, but the debug info
22060 would not. It's no longer necessary, because we will explicitly
22061 apply relocations when we read in the debug information now. */
22062
22063 /* A DW_AT_location attribute with no contents indicates that a
22064 variable has been optimized away. */
9d2246fc 22065 if (attr->form_is_block () && attr->as_block ()->size == 0)
4c2df51b 22066 {
f1e6e072 22067 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
22068 return;
22069 }
22070
22071 /* Handle one degenerate form of location expression specially, to
22072 preserve GDB's previous behavior when section offsets are
336d760d
AT
22073 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
22074 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 22075
9d2246fc
TT
22076 if (attr->form_is_block ())
22077 {
22078 struct dwarf_block *block = attr->as_block ();
22079
22080 if ((block->data[0] == DW_OP_addr
22081 && block->size == 1 + cu_header->addr_size)
22082 || ((block->data[0] == DW_OP_GNU_addr_index
dda83cd7 22083 || block->data[0] == DW_OP_addrx)
9d2246fc
TT
22084 && (block->size
22085 == 1 + leb128_size (&block->data[1]))))
22086 {
22087 unsigned int dummy;
22088
22089 if (block->data[0] == DW_OP_addr)
22090 SET_SYMBOL_VALUE_ADDRESS
22091 (sym, cu->header.read_address (objfile->obfd,
22092 block->data + 1,
38583298 22093 &dummy));
9d2246fc
TT
22094 else
22095 SET_SYMBOL_VALUE_ADDRESS
22096 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
22097 &dummy));
22098 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
22099 fixup_symbol_section (sym, objfile);
22100 SET_SYMBOL_VALUE_ADDRESS
22101 (sym,
22102 SYMBOL_VALUE_ADDRESS (sym)
a52d653e 22103 + objfile->section_offsets[sym->section_index ()]);
9d2246fc
TT
22104 return;
22105 }
4c2df51b
DJ
22106 }
22107
22108 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
22109 expression evaluator, and use LOC_COMPUTED only when necessary
22110 (i.e. when the value of a register or memory location is
22111 referenced, or a thread-local block, etc.). Then again, it might
22112 not be worthwhile. I'm assuming that it isn't unless performance
22113 or memory numbers show me otherwise. */
22114
f1e6e072 22115 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 22116
f1e6e072 22117 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 22118 cu->has_loclist = true;
4c2df51b
DJ
22119}
22120
c906108c
SS
22121/* Given a pointer to a DWARF information entry, figure out if we need
22122 to make a symbol table entry for it, and if so, create a new entry
22123 and return a pointer to it.
22124 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
22125 used the passed type.
22126 If SPACE is not NULL, use it to hold the new symbol. If it is
22127 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
22128
22129static struct symbol *
5e2db402
TT
22130new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
22131 struct symbol *space)
c906108c 22132{
976ca316
SM
22133 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22134 struct objfile *objfile = per_objfile->objfile;
08feed99 22135 struct gdbarch *gdbarch = objfile->arch ();
c906108c 22136 struct symbol *sym = NULL;
15d034d0 22137 const char *name;
c906108c
SS
22138 struct attribute *attr = NULL;
22139 struct attribute *attr2 = NULL;
e142c38c 22140 CORE_ADDR baseaddr;
e37fd15a
SW
22141 struct pending **list_to_add = NULL;
22142
edb3359d 22143 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 22144
b3b3bada 22145 baseaddr = objfile->text_section_offset ();
c906108c 22146
94af9270 22147 name = dwarf2_name (die, cu);
c906108c
SS
22148 if (name)
22149 {
34eaf542 22150 int suppress_add = 0;
94af9270 22151
34eaf542
TT
22152 if (space)
22153 sym = space;
22154 else
8c14c3a3 22155 sym = new (&objfile->objfile_obstack) symbol;
c906108c 22156 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
22157
22158 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 22159 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
22160 /* Fortran does not have mangling standard and the mangling does differ
22161 between gfortran, iFort etc. */
bcfe6157
TT
22162 const char *physname
22163 = (cu->language == language_fortran
22164 ? dwarf2_full_name (name, die, cu)
22165 : dwarf2_physname (name, die, cu));
22166 const char *linkagename = dw2_linkage_name (die, cu);
22167
22168 if (linkagename == nullptr || cu->language == language_ada)
22169 sym->set_linkage_name (physname);
22170 else
22171 {
22172 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22173 sym->set_linkage_name (linkagename);
22174 }
f55ee35c 22175
c906108c 22176 /* Default assumptions.
dda83cd7 22177 Use the passed type or decode it from the die. */
176620f1 22178 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 22179 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
22180 if (type != NULL)
22181 SYMBOL_TYPE (sym) = type;
22182 else
e7c27a73 22183 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
22184 attr = dwarf2_attr (die,
22185 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22186 cu);
435d3d88 22187 if (attr != nullptr)
529908cb 22188 SYMBOL_LINE (sym) = attr->constant_value (0);
cb1df416 22189
edb3359d
DJ
22190 attr = dwarf2_attr (die,
22191 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22192 cu);
529908cb 22193 if (attr != nullptr && attr->form_is_unsigned ())
cb1df416 22194 {
529908cb
TT
22195 file_name_index file_index
22196 = (file_name_index) attr->as_unsigned ();
8c43009f 22197 struct file_entry *fe;
9a619af0 22198
ecfb656c
PA
22199 if (cu->line_header != NULL)
22200 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
22201 else
22202 fe = NULL;
22203
22204 if (fe == NULL)
b98664d3 22205 complaint (_("file index out of range"));
8c43009f
PA
22206 else
22207 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
22208 }
22209
c906108c
SS
22210 switch (die->tag)
22211 {
22212 case DW_TAG_label:
e142c38c 22213 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 22214 if (attr != nullptr)
3e29f34a
MR
22215 {
22216 CORE_ADDR addr;
22217
95f982e5 22218 addr = attr->as_address ();
3e29f34a 22219 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 22220 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
8f5c6526 22221 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
3e29f34a 22222 }
8f5c6526
TV
22223 else
22224 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
0f5238ed
TT
22225 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22226 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
d3cb6808 22227 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
22228 break;
22229 case DW_TAG_subprogram:
22230 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22231 finish_block. */
f1e6e072 22232 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 22233 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 22234 if ((attr2 != nullptr && attr2->as_boolean ())
0a4b0913
AB
22235 || cu->language == language_ada
22236 || cu->language == language_fortran)
c906108c 22237 {
dda83cd7
SM
22238 /* Subprograms marked external are stored as a global symbol.
22239 Ada and Fortran subprograms, whether marked external or
22240 not, are always stored as a global symbol, because we want
22241 to be able to access them globally. For instance, we want
22242 to be able to break on a nested subprogram without having
22243 to specify the context. */
c24bdb02 22244 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
22245 }
22246 else
22247 {
e37fd15a 22248 list_to_add = cu->list_in_scope;
c906108c
SS
22249 }
22250 break;
edb3359d
DJ
22251 case DW_TAG_inlined_subroutine:
22252 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22253 finish_block. */
f1e6e072 22254 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 22255 SYMBOL_INLINED (sym) = 1;
481860b3 22256 list_to_add = cu->list_in_scope;
edb3359d 22257 break;
34eaf542
TT
22258 case DW_TAG_template_value_param:
22259 suppress_add = 1;
22260 /* Fall through. */
72929c62 22261 case DW_TAG_constant:
c906108c 22262 case DW_TAG_variable:
254e6b9e 22263 case DW_TAG_member:
0963b4bd
MS
22264 /* Compilation with minimal debug info may result in
22265 variables with missing type entries. Change the
22266 misleading `void' type to something sensible. */
78134374 22267 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 22268 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 22269
e142c38c 22270 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
22271 /* In the case of DW_TAG_member, we should only be called for
22272 static const members. */
22273 if (die->tag == DW_TAG_member)
22274 {
3863f96c
DE
22275 /* dwarf2_add_field uses die_is_declaration,
22276 so we do the same. */
254e6b9e
DE
22277 gdb_assert (die_is_declaration (die, cu));
22278 gdb_assert (attr);
22279 }
435d3d88 22280 if (attr != nullptr)
c906108c 22281 {
e7c27a73 22282 dwarf2_const_value (attr, sym, cu);
e142c38c 22283 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 22284 if (!suppress_add)
34eaf542 22285 {
c45bc3f8 22286 if (attr2 != nullptr && attr2->as_boolean ())
c24bdb02 22287 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 22288 else
e37fd15a 22289 list_to_add = cu->list_in_scope;
34eaf542 22290 }
c906108c
SS
22291 break;
22292 }
e142c38c 22293 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22294 if (attr != nullptr)
c906108c 22295 {
e7c27a73 22296 var_decode_location (attr, sym, cu);
e142c38c 22297 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
22298
22299 /* Fortran explicitly imports any global symbols to the local
22300 scope by DW_TAG_common_block. */
22301 if (cu->language == language_fortran && die->parent
22302 && die->parent->tag == DW_TAG_common_block)
22303 attr2 = NULL;
22304
caac4577
JG
22305 if (SYMBOL_CLASS (sym) == LOC_STATIC
22306 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 22307 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
22308 {
22309 /* When a static variable is eliminated by the linker,
22310 the corresponding debug information is not stripped
22311 out, but the variable address is set to null;
22312 do not add such variables into symbol table. */
22313 }
c45bc3f8 22314 else if (attr2 != nullptr && attr2->as_boolean ())
1c809c68 22315 {
4b610737
TT
22316 if (SYMBOL_CLASS (sym) == LOC_STATIC
22317 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 22318 && per_objfile->per_bfd->can_copy)
4b610737
TT
22319 {
22320 /* A global static variable might be subject to
22321 copy relocation. We first check for a local
22322 minsym, though, because maybe the symbol was
22323 marked hidden, in which case this would not
22324 apply. */
22325 bound_minimal_symbol found
22326 = (lookup_minimal_symbol_linkage
987012b8 22327 (sym->linkage_name (), objfile));
4b610737
TT
22328 if (found.minsym != nullptr)
22329 sym->maybe_copied = 1;
22330 }
f55ee35c 22331
1c809c68
TT
22332 /* A variable with DW_AT_external is never static,
22333 but it may be block-scoped. */
804d2729 22334 list_to_add
c24bdb02
KS
22335 = ((cu->list_in_scope
22336 == cu->get_builder ()->get_file_symbols ())
22337 ? cu->get_builder ()->get_global_symbols ()
804d2729 22338 : cu->list_in_scope);
1c809c68 22339 }
c906108c 22340 else
e37fd15a 22341 list_to_add = cu->list_in_scope;
c906108c
SS
22342 }
22343 else
22344 {
22345 /* We do not know the address of this symbol.
dda83cd7
SM
22346 If it is an external symbol and we have type information
22347 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22348 The address of the variable will then be determined from
22349 the minimal symbol table whenever the variable is
22350 referenced. */
e142c38c 22351 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
22352
22353 /* Fortran explicitly imports any global symbols to the local
22354 scope by DW_TAG_common_block. */
22355 if (cu->language == language_fortran && die->parent
22356 && die->parent->tag == DW_TAG_common_block)
22357 {
22358 /* SYMBOL_CLASS doesn't matter here because
22359 read_common_block is going to reset it. */
22360 if (!suppress_add)
22361 list_to_add = cu->list_in_scope;
22362 }
c45bc3f8 22363 else if (attr2 != nullptr && attr2->as_boolean ()
0971de02 22364 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 22365 {
0fe7935b
DJ
22366 /* A variable with DW_AT_external is never static, but it
22367 may be block-scoped. */
804d2729 22368 list_to_add
c24bdb02
KS
22369 = ((cu->list_in_scope
22370 == cu->get_builder ()->get_file_symbols ())
22371 ? cu->get_builder ()->get_global_symbols ()
804d2729 22372 : cu->list_in_scope);
0fe7935b 22373
f1e6e072 22374 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 22375 }
442ddf59
JK
22376 else if (!die_is_declaration (die, cu))
22377 {
22378 /* Use the default LOC_OPTIMIZED_OUT class. */
22379 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22380 if (!suppress_add)
22381 list_to_add = cu->list_in_scope;
442ddf59 22382 }
c906108c
SS
22383 }
22384 break;
22385 case DW_TAG_formal_parameter:
a60f3166
TT
22386 {
22387 /* If we are inside a function, mark this as an argument. If
22388 not, we might be looking at an argument to an inlined function
22389 when we do not have enough information to show inlined frames;
22390 pretend it's a local variable in that case so that the user can
22391 still see it. */
804d2729 22392 struct context_stack *curr
c24bdb02 22393 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
22394 if (curr != nullptr && curr->name != nullptr)
22395 SYMBOL_IS_ARGUMENT (sym) = 1;
22396 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22397 if (attr != nullptr)
a60f3166
TT
22398 {
22399 var_decode_location (attr, sym, cu);
22400 }
22401 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22402 if (attr != nullptr)
a60f3166
TT
22403 {
22404 dwarf2_const_value (attr, sym, cu);
22405 }
f346a30d 22406
a60f3166
TT
22407 list_to_add = cu->list_in_scope;
22408 }
c906108c
SS
22409 break;
22410 case DW_TAG_unspecified_parameters:
22411 /* From varargs functions; gdb doesn't seem to have any
22412 interest in this information, so just ignore it for now.
22413 (FIXME?) */
22414 break;
34eaf542
TT
22415 case DW_TAG_template_type_param:
22416 suppress_add = 1;
22417 /* Fall through. */
c906108c 22418 case DW_TAG_class_type:
680b30c7 22419 case DW_TAG_interface_type:
c906108c
SS
22420 case DW_TAG_structure_type:
22421 case DW_TAG_union_type:
72019c9c 22422 case DW_TAG_set_type:
c906108c 22423 case DW_TAG_enumeration_type:
f1e6e072 22424 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22425 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22426
63d06c5c 22427 {
9c37b5ae 22428 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22429 really ever be static objects: otherwise, if you try
22430 to, say, break of a class's method and you're in a file
22431 which doesn't mention that class, it won't work unless
22432 the check for all static symbols in lookup_symbol_aux
22433 saves you. See the OtherFileClass tests in
22434 gdb.c++/namespace.exp. */
22435
e37fd15a 22436 if (!suppress_add)
34eaf542 22437 {
c24bdb02 22438 buildsym_compunit *builder = cu->get_builder ();
804d2729 22439 list_to_add
c24bdb02 22440 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22441 && cu->language == language_cplus
c24bdb02 22442 ? builder->get_global_symbols ()
804d2729 22443 : cu->list_in_scope);
63d06c5c 22444
64382290 22445 /* The semantics of C++ state that "struct foo {
9c37b5ae 22446 ... }" also defines a typedef for "foo". */
64382290 22447 if (cu->language == language_cplus
45280282 22448 || cu->language == language_ada
c44af4eb
TT
22449 || cu->language == language_d
22450 || cu->language == language_rust)
64382290
TT
22451 {
22452 /* The symbol's name is already allocated along
22453 with this objfile, so we don't need to
22454 duplicate it for the type. */
7d93a1e0 22455 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 22456 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 22457 }
63d06c5c
DC
22458 }
22459 }
c906108c
SS
22460 break;
22461 case DW_TAG_typedef:
f1e6e072 22462 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22463 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22464 list_to_add = cu->list_in_scope;
63d06c5c 22465 break;
d8f62e84 22466 case DW_TAG_array_type:
c906108c 22467 case DW_TAG_base_type:
dda83cd7 22468 case DW_TAG_subrange_type:
f1e6e072 22469 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22470 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22471 list_to_add = cu->list_in_scope;
c906108c
SS
22472 break;
22473 case DW_TAG_enumerator:
e142c38c 22474 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22475 if (attr != nullptr)
c906108c 22476 {
e7c27a73 22477 dwarf2_const_value (attr, sym, cu);
c906108c 22478 }
63d06c5c
DC
22479 {
22480 /* NOTE: carlton/2003-11-10: See comment above in the
22481 DW_TAG_class_type, etc. block. */
22482
804d2729 22483 list_to_add
c24bdb02 22484 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22485 && cu->language == language_cplus
c24bdb02 22486 ? cu->get_builder ()->get_global_symbols ()
804d2729 22487 : cu->list_in_scope);
63d06c5c 22488 }
c906108c 22489 break;
74921315 22490 case DW_TAG_imported_declaration:
5c4e30ca 22491 case DW_TAG_namespace:
f1e6e072 22492 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22493 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22494 break;
530e8392
KB
22495 case DW_TAG_module:
22496 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22497 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22498 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22499 break;
4357ac6c 22500 case DW_TAG_common_block:
f1e6e072 22501 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22502 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22503 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22504 break;
c906108c
SS
22505 default:
22506 /* Not a tag we recognize. Hopefully we aren't processing
22507 trash data, but since we must specifically ignore things
22508 we don't recognize, there is nothing else we should do at
0963b4bd 22509 this point. */
b98664d3 22510 complaint (_("unsupported tag: '%s'"),
4d3c2250 22511 dwarf_tag_name (die->tag));
c906108c
SS
22512 break;
22513 }
df8a16a1 22514
e37fd15a
SW
22515 if (suppress_add)
22516 {
22517 sym->hash_next = objfile->template_symbols;
22518 objfile->template_symbols = sym;
22519 list_to_add = NULL;
22520 }
22521
22522 if (list_to_add != NULL)
d3cb6808 22523 add_symbol_to_list (sym, list_to_add);
e37fd15a 22524
df8a16a1
DJ
22525 /* For the benefit of old versions of GCC, check for anonymous
22526 namespaces based on the demangled name. */
4d4ec4e5 22527 if (!cu->processing_has_namespace_info
94af9270 22528 && cu->language == language_cplus)
c24bdb02 22529 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22530 }
22531 return (sym);
22532}
22533
98bfdba5
PA
22534/* Given an attr with a DW_FORM_dataN value in host byte order,
22535 zero-extend it as appropriate for the symbol's type. The DWARF
22536 standard (v4) is not entirely clear about the meaning of using
22537 DW_FORM_dataN for a constant with a signed type, where the type is
22538 wider than the data. The conclusion of a discussion on the DWARF
22539 list was that this is unspecified. We choose to always zero-extend
22540 because that is the interpretation long in use by GCC. */
c906108c 22541
98bfdba5 22542static gdb_byte *
ff39bb5e 22543dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22544 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22545{
5e22e966 22546 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
22547 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22548 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
529908cb 22549 LONGEST l = attr->constant_value (0);
98bfdba5
PA
22550
22551 if (bits < sizeof (*value) * 8)
22552 {
22553 l &= ((LONGEST) 1 << bits) - 1;
22554 *value = l;
22555 }
22556 else if (bits == sizeof (*value) * 8)
22557 *value = l;
22558 else
22559 {
224c3ddb 22560 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22561 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22562 return bytes;
22563 }
22564
22565 return NULL;
22566}
22567
22568/* Read a constant value from an attribute. Either set *VALUE, or if
22569 the value does not fit in *VALUE, set *BYTES - either already
22570 allocated on the objfile obstack, or newly allocated on OBSTACK,
22571 or, set *BATON, if we translated the constant to a location
22572 expression. */
22573
22574static void
ff39bb5e 22575dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22576 const char *name, struct obstack *obstack,
22577 struct dwarf2_cu *cu,
d521ce57 22578 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22579 struct dwarf2_locexpr_baton **baton)
22580{
5e22e966 22581 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 22582 struct objfile *objfile = per_objfile->objfile;
98bfdba5 22583 struct comp_unit_head *cu_header = &cu->header;
c906108c 22584 struct dwarf_block *blk;
98bfdba5
PA
22585 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22586 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22587
22588 *value = 0;
22589 *bytes = NULL;
22590 *baton = NULL;
c906108c
SS
22591
22592 switch (attr->form)
22593 {
22594 case DW_FORM_addr:
336d760d 22595 case DW_FORM_addrx:
3019eac3 22596 case DW_FORM_GNU_addr_index:
ac56253d 22597 {
ac56253d
TT
22598 gdb_byte *data;
22599
98bfdba5
PA
22600 if (TYPE_LENGTH (type) != cu_header->addr_size)
22601 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22602 cu_header->addr_size,
98bfdba5 22603 TYPE_LENGTH (type));
ac56253d
TT
22604 /* Symbols of this form are reasonably rare, so we just
22605 piggyback on the existing location code rather than writing
22606 a new implementation of symbol_computed_ops. */
8d749320 22607 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 22608 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
22609 (*baton)->per_cu = cu->per_cu;
22610 gdb_assert ((*baton)->per_cu);
ac56253d 22611
98bfdba5 22612 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22613 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22614 (*baton)->data = data;
ac56253d
TT
22615
22616 data[0] = DW_OP_addr;
22617 store_unsigned_integer (&data[1], cu_header->addr_size,
36d378cf 22618 byte_order, attr->as_address ());
ac56253d 22619 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22620 }
c906108c 22621 break;
4ac36638 22622 case DW_FORM_string:
93b5768b 22623 case DW_FORM_strp:
cf532bd1 22624 case DW_FORM_strx:
3019eac3 22625 case DW_FORM_GNU_str_index:
36586728 22626 case DW_FORM_GNU_strp_alt:
c6481205 22627 /* The string is already allocated on the objfile obstack, point
98bfdba5 22628 directly to it. */
2c830f54 22629 *bytes = (const gdb_byte *) attr->as_string ();
93b5768b 22630 break;
c906108c
SS
22631 case DW_FORM_block1:
22632 case DW_FORM_block2:
22633 case DW_FORM_block4:
22634 case DW_FORM_block:
2dc7f7b3 22635 case DW_FORM_exprloc:
0224619f 22636 case DW_FORM_data16:
9d2246fc 22637 blk = attr->as_block ();
98bfdba5
PA
22638 if (TYPE_LENGTH (type) != blk->size)
22639 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22640 TYPE_LENGTH (type));
22641 *bytes = blk->data;
c906108c 22642 break;
2df3850c
JM
22643
22644 /* The DW_AT_const_value attributes are supposed to carry the
22645 symbol's value "represented as it would be on the target
22646 architecture." By the time we get here, it's already been
22647 converted to host endianness, so we just need to sign- or
22648 zero-extend it as appropriate. */
22649 case DW_FORM_data1:
3aef2284 22650 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22651 break;
c906108c 22652 case DW_FORM_data2:
3aef2284 22653 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22654 break;
c906108c 22655 case DW_FORM_data4:
3aef2284 22656 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22657 break;
c906108c 22658 case DW_FORM_data8:
3aef2284 22659 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22660 break;
22661
c906108c 22662 case DW_FORM_sdata:
663c44ac 22663 case DW_FORM_implicit_const:
1bc397c5 22664 *value = attr->as_signed ();
2df3850c
JM
22665 break;
22666
c906108c 22667 case DW_FORM_udata:
529908cb 22668 *value = attr->as_unsigned ();
c906108c 22669 break;
2df3850c 22670
c906108c 22671 default:
b98664d3 22672 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22673 dwarf_form_name (attr->form));
98bfdba5 22674 *value = 0;
c906108c
SS
22675 break;
22676 }
22677}
22678
2df3850c 22679
98bfdba5
PA
22680/* Copy constant value from an attribute to a symbol. */
22681
2df3850c 22682static void
ff39bb5e 22683dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22684 struct dwarf2_cu *cu)
2df3850c 22685{
5e22e966 22686 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 22687 LONGEST value;
d521ce57 22688 const gdb_byte *bytes;
98bfdba5 22689 struct dwarf2_locexpr_baton *baton;
2df3850c 22690
98bfdba5 22691 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22692 sym->print_name (),
98bfdba5
PA
22693 &objfile->objfile_obstack, cu,
22694 &value, &bytes, &baton);
2df3850c 22695
98bfdba5
PA
22696 if (baton != NULL)
22697 {
98bfdba5 22698 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22699 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22700 }
22701 else if (bytes != NULL)
22702 {
22703 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22704 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22705 }
22706 else
22707 {
22708 SYMBOL_VALUE (sym) = value;
f1e6e072 22709 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22710 }
2df3850c
JM
22711}
22712
c906108c
SS
22713/* Return the type of the die in question using its DW_AT_type attribute. */
22714
22715static struct type *
e7c27a73 22716die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22717{
c906108c 22718 struct attribute *type_attr;
c906108c 22719
e142c38c 22720 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22721 if (!type_attr)
22722 {
5e22e966 22723 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22724 /* A missing DW_AT_type represents a void type. */
518817b3 22725 return objfile_type (objfile)->builtin_void;
c906108c 22726 }
348e048f 22727
673bfd45 22728 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22729}
22730
b4ba55a1
JB
22731/* True iff CU's producer generates GNAT Ada auxiliary information
22732 that allows to find parallel types through that information instead
22733 of having to do expensive parallel lookups by type name. */
22734
22735static int
22736need_gnat_info (struct dwarf2_cu *cu)
22737{
de4cb04a
JB
22738 /* Assume that the Ada compiler was GNAT, which always produces
22739 the auxiliary information. */
22740 return (cu->language == language_ada);
b4ba55a1
JB
22741}
22742
b4ba55a1
JB
22743/* Return the auxiliary type of the die in question using its
22744 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22745 attribute is not present. */
22746
22747static struct type *
22748die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22749{
b4ba55a1 22750 struct attribute *type_attr;
b4ba55a1
JB
22751
22752 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22753 if (!type_attr)
22754 return NULL;
22755
673bfd45 22756 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22757}
22758
22759/* If DIE has a descriptive_type attribute, then set the TYPE's
22760 descriptive type accordingly. */
22761
22762static void
22763set_descriptive_type (struct type *type, struct die_info *die,
22764 struct dwarf2_cu *cu)
22765{
22766 struct type *descriptive_type = die_descriptive_type (die, cu);
22767
22768 if (descriptive_type)
22769 {
22770 ALLOCATE_GNAT_AUX_TYPE (type);
22771 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22772 }
22773}
22774
c906108c
SS
22775/* Return the containing type of the die in question using its
22776 DW_AT_containing_type attribute. */
22777
22778static struct type *
e7c27a73 22779die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22780{
c906108c 22781 struct attribute *type_attr;
5e22e966 22782 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22783
e142c38c 22784 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22785 if (!type_attr)
22786 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22787 "[in module %s]"), objfile_name (objfile));
33ac96f0 22788
673bfd45 22789 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22790}
22791
ac9ec31b
DE
22792/* Return an error marker type to use for the ill formed type in DIE/CU. */
22793
22794static struct type *
22795build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22796{
976ca316
SM
22797 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22798 struct objfile *objfile = per_objfile->objfile;
528e1572 22799 char *saved;
ac9ec31b 22800
528e1572
SM
22801 std::string message
22802 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22803 objfile_name (objfile),
22804 sect_offset_str (cu->header.sect_off),
22805 sect_offset_str (die->sect_off));
efba19b0 22806 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22807
19f392bc 22808 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22809}
22810
673bfd45 22811/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22812 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22813 DW_AT_containing_type.
673bfd45
DE
22814 If there is no type substitute an error marker. */
22815
c906108c 22816static struct type *
ff39bb5e 22817lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22818 struct dwarf2_cu *cu)
c906108c 22819{
976ca316
SM
22820 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22821 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
22822 struct type *this_type;
22823
ac9ec31b
DE
22824 gdb_assert (attr->name == DW_AT_type
22825 || attr->name == DW_AT_GNAT_descriptive_type
22826 || attr->name == DW_AT_containing_type);
22827
673bfd45
DE
22828 /* First see if we have it cached. */
22829
36586728
TT
22830 if (attr->form == DW_FORM_GNU_ref_alt)
22831 {
22832 struct dwarf2_per_cu_data *per_cu;
0826b30a 22833 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 22834
976ca316
SM
22835 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22836 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 22837 }
cd6c91b4 22838 else if (attr->form_is_ref ())
673bfd45 22839 {
0826b30a 22840 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 22841
976ca316 22842 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 22843 }
55f1336d 22844 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22845 {
630ed6b9 22846 ULONGEST signature = attr->as_signature ();
673bfd45 22847
ac9ec31b 22848 return get_signatured_type (die, signature, cu);
673bfd45
DE
22849 }
22850 else
22851 {
b98664d3 22852 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22853 " at %s [in module %s]"),
22854 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22855 objfile_name (objfile));
ac9ec31b 22856 return build_error_marker_type (cu, die);
673bfd45
DE
22857 }
22858
22859 /* If not cached we need to read it in. */
22860
22861 if (this_type == NULL)
22862 {
ac9ec31b 22863 struct die_info *type_die = NULL;
673bfd45
DE
22864 struct dwarf2_cu *type_cu = cu;
22865
cd6c91b4 22866 if (attr->form_is_ref ())
ac9ec31b
DE
22867 type_die = follow_die_ref (die, attr, &type_cu);
22868 if (type_die == NULL)
22869 return build_error_marker_type (cu, die);
22870 /* If we find the type now, it's probably because the type came
3019eac3
DE
22871 from an inter-CU reference and the type's CU got expanded before
22872 ours. */
ac9ec31b 22873 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22874 }
22875
22876 /* If we still don't have a type use an error marker. */
22877
22878 if (this_type == NULL)
ac9ec31b 22879 return build_error_marker_type (cu, die);
673bfd45 22880
f792889a 22881 return this_type;
c906108c
SS
22882}
22883
673bfd45
DE
22884/* Return the type in DIE, CU.
22885 Returns NULL for invalid types.
22886
02142a6c 22887 This first does a lookup in die_type_hash,
673bfd45
DE
22888 and only reads the die in if necessary.
22889
22890 NOTE: This can be called when reading in partial or full symbols. */
22891
f792889a 22892static struct type *
e7c27a73 22893read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22894{
f792889a
DJ
22895 struct type *this_type;
22896
22897 this_type = get_die_type (die, cu);
22898 if (this_type)
22899 return this_type;
22900
673bfd45
DE
22901 return read_type_die_1 (die, cu);
22902}
22903
22904/* Read the type in DIE, CU.
22905 Returns NULL for invalid types. */
22906
22907static struct type *
22908read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22909{
22910 struct type *this_type = NULL;
22911
c906108c
SS
22912 switch (die->tag)
22913 {
22914 case DW_TAG_class_type:
680b30c7 22915 case DW_TAG_interface_type:
c906108c
SS
22916 case DW_TAG_structure_type:
22917 case DW_TAG_union_type:
f792889a 22918 this_type = read_structure_type (die, cu);
c906108c
SS
22919 break;
22920 case DW_TAG_enumeration_type:
f792889a 22921 this_type = read_enumeration_type (die, cu);
c906108c
SS
22922 break;
22923 case DW_TAG_subprogram:
22924 case DW_TAG_subroutine_type:
edb3359d 22925 case DW_TAG_inlined_subroutine:
f792889a 22926 this_type = read_subroutine_type (die, cu);
c906108c
SS
22927 break;
22928 case DW_TAG_array_type:
f792889a 22929 this_type = read_array_type (die, cu);
c906108c 22930 break;
72019c9c 22931 case DW_TAG_set_type:
f792889a 22932 this_type = read_set_type (die, cu);
72019c9c 22933 break;
c906108c 22934 case DW_TAG_pointer_type:
f792889a 22935 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22936 break;
22937 case DW_TAG_ptr_to_member_type:
f792889a 22938 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22939 break;
22940 case DW_TAG_reference_type:
4297a3f0
AV
22941 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22942 break;
22943 case DW_TAG_rvalue_reference_type:
22944 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22945 break;
22946 case DW_TAG_const_type:
f792889a 22947 this_type = read_tag_const_type (die, cu);
c906108c
SS
22948 break;
22949 case DW_TAG_volatile_type:
f792889a 22950 this_type = read_tag_volatile_type (die, cu);
c906108c 22951 break;
06d66ee9
TT
22952 case DW_TAG_restrict_type:
22953 this_type = read_tag_restrict_type (die, cu);
22954 break;
c906108c 22955 case DW_TAG_string_type:
f792889a 22956 this_type = read_tag_string_type (die, cu);
c906108c
SS
22957 break;
22958 case DW_TAG_typedef:
f792889a 22959 this_type = read_typedef (die, cu);
c906108c 22960 break;
a02abb62 22961 case DW_TAG_subrange_type:
f792889a 22962 this_type = read_subrange_type (die, cu);
a02abb62 22963 break;
c906108c 22964 case DW_TAG_base_type:
f792889a 22965 this_type = read_base_type (die, cu);
c906108c 22966 break;
81a17f79 22967 case DW_TAG_unspecified_type:
f792889a 22968 this_type = read_unspecified_type (die, cu);
81a17f79 22969 break;
0114d602
DJ
22970 case DW_TAG_namespace:
22971 this_type = read_namespace_type (die, cu);
22972 break;
f55ee35c
JK
22973 case DW_TAG_module:
22974 this_type = read_module_type (die, cu);
22975 break;
a2c2acaf
MW
22976 case DW_TAG_atomic_type:
22977 this_type = read_tag_atomic_type (die, cu);
22978 break;
c906108c 22979 default:
b98664d3 22980 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22981 dwarf_tag_name (die->tag));
c906108c
SS
22982 break;
22983 }
63d06c5c 22984
f792889a 22985 return this_type;
63d06c5c
DC
22986}
22987
abc72ce4
DE
22988/* See if we can figure out if the class lives in a namespace. We do
22989 this by looking for a member function; its demangled name will
22990 contain namespace info, if there is any.
22991 Return the computed name or NULL.
22992 Space for the result is allocated on the objfile's obstack.
22993 This is the full-die version of guess_partial_die_structure_name.
22994 In this case we know DIE has no useful parent. */
22995
43816ebc 22996static const char *
abc72ce4
DE
22997guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22998{
22999 struct die_info *spec_die;
23000 struct dwarf2_cu *spec_cu;
23001 struct die_info *child;
5e22e966 23002 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
23003
23004 spec_cu = cu;
23005 spec_die = die_specification (die, &spec_cu);
23006 if (spec_die != NULL)
23007 {
23008 die = spec_die;
23009 cu = spec_cu;
23010 }
23011
23012 for (child = die->child;
23013 child != NULL;
23014 child = child->sibling)
23015 {
23016 if (child->tag == DW_TAG_subprogram)
23017 {
73b9be8b 23018 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 23019
7d45c7c3 23020 if (linkage_name != NULL)
abc72ce4 23021 {
43816ebc 23022 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 23023 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 23024 const char *name = NULL;
abc72ce4
DE
23025
23026 if (actual_name != NULL)
23027 {
15d034d0 23028 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
23029
23030 if (die_name != NULL
43816ebc 23031 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
23032 {
23033 /* Strip off the class name from the full name.
23034 We want the prefix. */
23035 int die_name_len = strlen (die_name);
43816ebc
TT
23036 int actual_name_len = strlen (actual_name.get ());
23037 const char *ptr = actual_name.get ();
abc72ce4
DE
23038
23039 /* Test for '::' as a sanity check. */
23040 if (actual_name_len > die_name_len + 2
43816ebc 23041 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 23042 name = obstack_strndup (
e3b94546 23043 &objfile->per_bfd->storage_obstack,
43816ebc 23044 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
23045 }
23046 }
abc72ce4
DE
23047 return name;
23048 }
23049 }
23050 }
23051
23052 return NULL;
23053}
23054
96408a79
SA
23055/* GCC might emit a nameless typedef that has a linkage name. Determine the
23056 prefix part in such case. See
23057 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23058
a121b7c1 23059static const char *
96408a79
SA
23060anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
23061{
23062 struct attribute *attr;
e6a959d6 23063 const char *base;
96408a79
SA
23064
23065 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
23066 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
23067 return NULL;
23068
7d45c7c3 23069 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
23070 return NULL;
23071
73b9be8b 23072 attr = dw2_linkage_name_attr (die, cu);
2c830f54
TT
23073 const char *attr_name = attr->as_string ();
23074 if (attr == NULL || attr_name == NULL)
96408a79
SA
23075 return NULL;
23076
23077 /* dwarf2_name had to be already called. */
3b64bf15 23078 gdb_assert (attr->canonical_string_p ());
96408a79
SA
23079
23080 /* Strip the base name, keep any leading namespaces/classes. */
2c830f54
TT
23081 base = strrchr (attr_name, ':');
23082 if (base == NULL || base == attr_name || base[-1] != ':')
96408a79
SA
23083 return "";
23084
5e22e966 23085 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9 23086 return obstack_strndup (&objfile->per_bfd->storage_obstack,
2c830f54
TT
23087 attr_name,
23088 &base[-1] - attr_name);
96408a79
SA
23089}
23090
fdde2d81 23091/* Return the name of the namespace/class that DIE is defined within,
0114d602 23092 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 23093
0114d602
DJ
23094 For example, if we're within the method foo() in the following
23095 code:
23096
23097 namespace N {
23098 class C {
23099 void foo () {
23100 }
23101 };
23102 }
23103
23104 then determine_prefix on foo's die will return "N::C". */
fdde2d81 23105
0d5cff50 23106static const char *
e142c38c 23107determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 23108{
976ca316 23109 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
23110 struct die_info *parent, *spec_die;
23111 struct dwarf2_cu *spec_cu;
23112 struct type *parent_type;
a121b7c1 23113 const char *retval;
63d06c5c 23114
9c37b5ae 23115 if (cu->language != language_cplus
c44af4eb
TT
23116 && cu->language != language_fortran && cu->language != language_d
23117 && cu->language != language_rust)
0114d602
DJ
23118 return "";
23119
96408a79
SA
23120 retval = anonymous_struct_prefix (die, cu);
23121 if (retval)
23122 return retval;
23123
0114d602
DJ
23124 /* We have to be careful in the presence of DW_AT_specification.
23125 For example, with GCC 3.4, given the code
23126
23127 namespace N {
23128 void foo() {
23129 // Definition of N::foo.
23130 }
23131 }
23132
23133 then we'll have a tree of DIEs like this:
23134
23135 1: DW_TAG_compile_unit
23136 2: DW_TAG_namespace // N
23137 3: DW_TAG_subprogram // declaration of N::foo
23138 4: DW_TAG_subprogram // definition of N::foo
23139 DW_AT_specification // refers to die #3
23140
23141 Thus, when processing die #4, we have to pretend that we're in
23142 the context of its DW_AT_specification, namely the contex of die
23143 #3. */
23144 spec_cu = cu;
23145 spec_die = die_specification (die, &spec_cu);
23146 if (spec_die == NULL)
23147 parent = die->parent;
23148 else
63d06c5c 23149 {
0114d602
DJ
23150 parent = spec_die->parent;
23151 cu = spec_cu;
63d06c5c 23152 }
0114d602
DJ
23153
23154 if (parent == NULL)
23155 return "";
98bfdba5
PA
23156 else if (parent->building_fullname)
23157 {
23158 const char *name;
23159 const char *parent_name;
23160
23161 /* It has been seen on RealView 2.2 built binaries,
23162 DW_TAG_template_type_param types actually _defined_ as
23163 children of the parent class:
23164
23165 enum E {};
23166 template class <class Enum> Class{};
23167 Class<enum E> class_e;
23168
dda83cd7
SM
23169 1: DW_TAG_class_type (Class)
23170 2: DW_TAG_enumeration_type (E)
23171 3: DW_TAG_enumerator (enum1:0)
23172 3: DW_TAG_enumerator (enum2:1)
23173 ...
23174 2: DW_TAG_template_type_param
23175 DW_AT_type DW_FORM_ref_udata (E)
98bfdba5
PA
23176
23177 Besides being broken debug info, it can put GDB into an
23178 infinite loop. Consider:
23179
23180 When we're building the full name for Class<E>, we'll start
23181 at Class, and go look over its template type parameters,
23182 finding E. We'll then try to build the full name of E, and
23183 reach here. We're now trying to build the full name of E,
23184 and look over the parent DIE for containing scope. In the
23185 broken case, if we followed the parent DIE of E, we'd again
23186 find Class, and once again go look at its template type
23187 arguments, etc., etc. Simply don't consider such parent die
23188 as source-level parent of this die (it can't be, the language
23189 doesn't allow it), and break the loop here. */
23190 name = dwarf2_name (die, cu);
23191 parent_name = dwarf2_name (parent, cu);
b98664d3 23192 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
23193 name ? name : "<unknown>",
23194 parent_name ? parent_name : "<unknown>");
23195 return "";
23196 }
63d06c5c 23197 else
0114d602
DJ
23198 switch (parent->tag)
23199 {
63d06c5c 23200 case DW_TAG_namespace:
0114d602 23201 parent_type = read_type_die (parent, cu);
acebe513
UW
23202 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23203 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23204 Work around this problem here. */
23205 if (cu->language == language_cplus
7d93a1e0 23206 && strcmp (parent_type->name (), "::") == 0)
acebe513 23207 return "";
0114d602 23208 /* We give a name to even anonymous namespaces. */
7d93a1e0 23209 return parent_type->name ();
63d06c5c 23210 case DW_TAG_class_type:
680b30c7 23211 case DW_TAG_interface_type:
63d06c5c 23212 case DW_TAG_structure_type:
0114d602 23213 case DW_TAG_union_type:
f55ee35c 23214 case DW_TAG_module:
0114d602 23215 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
23216 if (parent_type->name () != NULL)
23217 return parent_type->name ();
0114d602
DJ
23218 else
23219 /* An anonymous structure is only allowed non-static data
23220 members; no typedefs, no member functions, et cetera.
23221 So it does not need a prefix. */
23222 return "";
abc72ce4 23223 case DW_TAG_compile_unit:
95554aad 23224 case DW_TAG_partial_unit:
abc72ce4
DE
23225 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23226 if (cu->language == language_cplus
976ca316 23227 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
23228 && die->child != NULL
23229 && (die->tag == DW_TAG_class_type
23230 || die->tag == DW_TAG_structure_type
23231 || die->tag == DW_TAG_union_type))
23232 {
43816ebc 23233 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
23234 if (name != NULL)
23235 return name;
23236 }
23237 return "";
0a4b0913
AB
23238 case DW_TAG_subprogram:
23239 /* Nested subroutines in Fortran get a prefix with the name
23240 of the parent's subroutine. */
23241 if (cu->language == language_fortran)
23242 {
23243 if ((die->tag == DW_TAG_subprogram)
23244 && (dwarf2_name (parent, cu) != NULL))
23245 return dwarf2_name (parent, cu);
23246 }
23247 return determine_prefix (parent, cu);
3d567982
TT
23248 case DW_TAG_enumeration_type:
23249 parent_type = read_type_die (parent, cu);
23250 if (TYPE_DECLARED_CLASS (parent_type))
23251 {
7d93a1e0
SM
23252 if (parent_type->name () != NULL)
23253 return parent_type->name ();
3d567982
TT
23254 return "";
23255 }
23256 /* Fall through. */
63d06c5c 23257 default:
8176b9b8 23258 return determine_prefix (parent, cu);
63d06c5c 23259 }
63d06c5c
DC
23260}
23261
3e43a32a
MS
23262/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23263 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23264 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23265 an obconcat, otherwise allocate storage for the result. The CU argument is
23266 used to determine the language and hence, the appropriate separator. */
987504bb 23267
f55ee35c 23268#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
23269
23270static char *
f55ee35c 23271typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
dda83cd7 23272 int physname, struct dwarf2_cu *cu)
63d06c5c 23273{
f55ee35c 23274 const char *lead = "";
5c315b68 23275 const char *sep;
63d06c5c 23276
3e43a32a
MS
23277 if (suffix == NULL || suffix[0] == '\0'
23278 || prefix == NULL || prefix[0] == '\0')
987504bb 23279 sep = "";
45280282
IB
23280 else if (cu->language == language_d)
23281 {
23282 /* For D, the 'main' function could be defined in any module, but it
23283 should never be prefixed. */
23284 if (strcmp (suffix, "D main") == 0)
23285 {
23286 prefix = "";
23287 sep = "";
23288 }
23289 else
23290 sep = ".";
23291 }
f55ee35c
JK
23292 else if (cu->language == language_fortran && physname)
23293 {
23294 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23295 DW_AT_MIPS_linkage_name is preferred and used instead. */
23296
23297 lead = "__";
23298 sep = "_MOD_";
23299 }
987504bb
JJ
23300 else
23301 sep = "::";
63d06c5c 23302
6dd47d34
DE
23303 if (prefix == NULL)
23304 prefix = "";
23305 if (suffix == NULL)
23306 suffix = "";
23307
987504bb
JJ
23308 if (obs == NULL)
23309 {
3e43a32a 23310 char *retval
224c3ddb
SM
23311 = ((char *)
23312 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 23313
f55ee35c
JK
23314 strcpy (retval, lead);
23315 strcat (retval, prefix);
6dd47d34
DE
23316 strcat (retval, sep);
23317 strcat (retval, suffix);
63d06c5c
DC
23318 return retval;
23319 }
987504bb
JJ
23320 else
23321 {
23322 /* We have an obstack. */
f55ee35c 23323 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 23324 }
63d06c5c
DC
23325}
23326
71c25dea
TT
23327/* Get name of a die, return NULL if not found. */
23328
15d034d0
TT
23329static const char *
23330dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 23331 struct objfile *objfile)
71c25dea
TT
23332{
23333 if (name && cu->language == language_cplus)
23334 {
596dc4ad
TT
23335 gdb::unique_xmalloc_ptr<char> canon_name
23336 = cp_canonicalize_string (name);
71c25dea 23337
596dc4ad
TT
23338 if (canon_name != nullptr)
23339 name = objfile->intern (canon_name.get ());
71c25dea
TT
23340 }
23341
23342 return name;
c906108c
SS
23343}
23344
96553a0c
DE
23345/* Get name of a die, return NULL if not found.
23346 Anonymous namespaces are converted to their magic string. */
9219021c 23347
15d034d0 23348static const char *
e142c38c 23349dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
23350{
23351 struct attribute *attr;
5e22e966 23352 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 23353
e142c38c 23354 attr = dwarf2_attr (die, DW_AT_name, cu);
2c830f54
TT
23355 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23356 if (attr_name == nullptr
96553a0c 23357 && die->tag != DW_TAG_namespace
53832f31
TT
23358 && die->tag != DW_TAG_class_type
23359 && die->tag != DW_TAG_interface_type
23360 && die->tag != DW_TAG_structure_type
23361 && die->tag != DW_TAG_union_type)
71c25dea
TT
23362 return NULL;
23363
23364 switch (die->tag)
23365 {
23366 case DW_TAG_compile_unit:
95554aad 23367 case DW_TAG_partial_unit:
71c25dea
TT
23368 /* Compilation units have a DW_AT_name that is a filename, not
23369 a source language identifier. */
23370 case DW_TAG_enumeration_type:
23371 case DW_TAG_enumerator:
23372 /* These tags always have simple identifiers already; no need
23373 to canonicalize them. */
2c830f54 23374 return attr_name;
907af001 23375
96553a0c 23376 case DW_TAG_namespace:
2c830f54
TT
23377 if (attr_name != nullptr)
23378 return attr_name;
96553a0c
DE
23379 return CP_ANONYMOUS_NAMESPACE_STR;
23380
907af001
UW
23381 case DW_TAG_class_type:
23382 case DW_TAG_interface_type:
23383 case DW_TAG_structure_type:
23384 case DW_TAG_union_type:
23385 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23386 structures or unions. These were of the form "._%d" in GCC 4.1,
23387 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23388 and GCC 4.4. We work around this problem by ignoring these. */
2c830f54
TT
23389 if (attr_name != nullptr
23390 && (startswith (attr_name, "._")
23391 || startswith (attr_name, "<anonymous")))
907af001 23392 return NULL;
53832f31
TT
23393
23394 /* GCC might emit a nameless typedef that has a linkage name. See
23395 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
2c830f54 23396 if (!attr || attr_name == NULL)
53832f31 23397 {
73b9be8b 23398 attr = dw2_linkage_name_attr (die, cu);
95eb9e54 23399 attr_name = attr == nullptr ? nullptr : attr->as_string ();
2c830f54 23400 if (attr == NULL || attr_name == NULL)
53832f31
TT
23401 return NULL;
23402
2c830f54 23403 /* Avoid demangling attr_name the second time on a second
df5c6c50 23404 call for the same DIE. */
3b64bf15 23405 if (!attr->canonical_string_p ())
53832f31 23406 {
43816ebc 23407 gdb::unique_xmalloc_ptr<char> demangled
2c830f54 23408 (gdb_demangle (attr_name, DMGL_TYPES));
4f180d53
AT
23409 if (demangled == nullptr)
23410 return nullptr;
43816ebc 23411
c6481205 23412 attr->set_string_canonical (objfile->intern (demangled.get ()));
95eb9e54 23413 attr_name = attr->as_string ();
53832f31 23414 }
67430cd0 23415
2c830f54
TT
23416 /* Strip any leading namespaces/classes, keep only the
23417 base name. DW_AT_name for named DIEs does not
23418 contain the prefixes. */
23419 const char *base = strrchr (attr_name, ':');
23420 if (base && base > attr_name && base[-1] == ':')
67430cd0
TT
23421 return &base[1];
23422 else
2c830f54 23423 return attr_name;
53832f31 23424 }
907af001
UW
23425 break;
23426
71c25dea 23427 default:
907af001
UW
23428 break;
23429 }
23430
3b64bf15 23431 if (!attr->canonical_string_p ())
c6481205
TT
23432 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23433 objfile));
2c830f54 23434 return attr->as_string ();
9219021c
DC
23435}
23436
23437/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23438 is none. *EXT_CU is the CU containing DIE on input, and the CU
23439 containing the return value on output. */
9219021c
DC
23440
23441static struct die_info *
f2f0e013 23442dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23443{
23444 struct attribute *attr;
9219021c 23445
f2f0e013 23446 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23447 if (attr == NULL)
23448 return NULL;
23449
f2f0e013 23450 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23451}
23452
f9aca02d 23453static void
d97bc12b 23454dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23455{
23456 unsigned int i;
23457
d97bc12b 23458 print_spaces (indent, f);
9d8780f0 23459 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23460 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23461 sect_offset_str (die->sect_off));
d97bc12b
DE
23462
23463 if (die->parent != NULL)
23464 {
23465 print_spaces (indent, f);
9d8780f0
SM
23466 fprintf_unfiltered (f, " parent at offset: %s\n",
23467 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23468 }
23469
23470 print_spaces (indent, f);
23471 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23472 dwarf_bool_name (die->child != NULL));
c906108c 23473
d97bc12b
DE
23474 print_spaces (indent, f);
23475 fprintf_unfiltered (f, " attributes:\n");
23476
c906108c
SS
23477 for (i = 0; i < die->num_attrs; ++i)
23478 {
d97bc12b
DE
23479 print_spaces (indent, f);
23480 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23481 dwarf_attr_name (die->attrs[i].name),
23482 dwarf_form_name (die->attrs[i].form));
d97bc12b 23483
c906108c
SS
23484 switch (die->attrs[i].form)
23485 {
c906108c 23486 case DW_FORM_addr:
336d760d 23487 case DW_FORM_addrx:
3019eac3 23488 case DW_FORM_GNU_addr_index:
d97bc12b 23489 fprintf_unfiltered (f, "address: ");
36d378cf 23490 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
c906108c
SS
23491 break;
23492 case DW_FORM_block2:
23493 case DW_FORM_block4:
23494 case DW_FORM_block:
23495 case DW_FORM_block1:
56eb65bd 23496 fprintf_unfiltered (f, "block: size %s",
9d2246fc 23497 pulongest (die->attrs[i].as_block ()->size));
c906108c 23498 break;
2dc7f7b3 23499 case DW_FORM_exprloc:
56eb65bd 23500 fprintf_unfiltered (f, "expression: size %s",
9d2246fc 23501 pulongest (die->attrs[i].as_block ()->size));
2dc7f7b3 23502 break;
0224619f
JK
23503 case DW_FORM_data16:
23504 fprintf_unfiltered (f, "constant of 16 bytes");
23505 break;
4568ecf9
DE
23506 case DW_FORM_ref_addr:
23507 fprintf_unfiltered (f, "ref address: ");
529908cb 23508 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
4568ecf9 23509 break;
36586728
TT
23510 case DW_FORM_GNU_ref_alt:
23511 fprintf_unfiltered (f, "alt ref address: ");
529908cb 23512 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
36586728 23513 break;
10b3939b
DJ
23514 case DW_FORM_ref1:
23515 case DW_FORM_ref2:
23516 case DW_FORM_ref4:
4568ecf9
DE
23517 case DW_FORM_ref8:
23518 case DW_FORM_ref_udata:
d97bc12b 23519 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
529908cb 23520 (long) (die->attrs[i].as_unsigned ()));
10b3939b 23521 break;
c906108c
SS
23522 case DW_FORM_data1:
23523 case DW_FORM_data2:
23524 case DW_FORM_data4:
ce5d95e1 23525 case DW_FORM_data8:
c906108c 23526 case DW_FORM_udata:
43bbcdc2 23527 fprintf_unfiltered (f, "constant: %s",
529908cb 23528 pulongest (die->attrs[i].as_unsigned ()));
c906108c 23529 break;
2dc7f7b3
TT
23530 case DW_FORM_sec_offset:
23531 fprintf_unfiltered (f, "section offset: %s",
529908cb 23532 pulongest (die->attrs[i].as_unsigned ()));
2dc7f7b3 23533 break;
55f1336d 23534 case DW_FORM_ref_sig8:
ac9ec31b 23535 fprintf_unfiltered (f, "signature: %s",
630ed6b9 23536 hex_string (die->attrs[i].as_signature ()));
348e048f 23537 break;
c906108c 23538 case DW_FORM_string:
4bdf3d34 23539 case DW_FORM_strp:
43988095 23540 case DW_FORM_line_strp:
cf532bd1 23541 case DW_FORM_strx:
3019eac3 23542 case DW_FORM_GNU_str_index:
36586728 23543 case DW_FORM_GNU_strp_alt:
8285870a 23544 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c6481205
TT
23545 die->attrs[i].as_string ()
23546 ? die->attrs[i].as_string () : "",
23547 die->attrs[i].canonical_string_p () ? "is" : "not");
c906108c
SS
23548 break;
23549 case DW_FORM_flag:
c45bc3f8 23550 if (die->attrs[i].as_boolean ())
d97bc12b 23551 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23552 else
d97bc12b 23553 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23554 break;
2dc7f7b3
TT
23555 case DW_FORM_flag_present:
23556 fprintf_unfiltered (f, "flag: TRUE");
23557 break;
a8329558 23558 case DW_FORM_indirect:
0963b4bd
MS
23559 /* The reader will have reduced the indirect form to
23560 the "base form" so this form should not occur. */
5f48f8f3 23561 fprintf_unfiltered (f,
3e43a32a 23562 "unexpected attribute form: DW_FORM_indirect");
a8329558 23563 break;
1bc397c5 23564 case DW_FORM_sdata:
663c44ac
JK
23565 case DW_FORM_implicit_const:
23566 fprintf_unfiltered (f, "constant: %s",
1bc397c5 23567 plongest (die->attrs[i].as_signed ()));
663c44ac 23568 break;
c906108c 23569 default:
d97bc12b 23570 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23571 die->attrs[i].form);
d97bc12b 23572 break;
c906108c 23573 }
d97bc12b 23574 fprintf_unfiltered (f, "\n");
c906108c
SS
23575 }
23576}
23577
f9aca02d 23578static void
d97bc12b 23579dump_die_for_error (struct die_info *die)
c906108c 23580{
d97bc12b
DE
23581 dump_die_shallow (gdb_stderr, 0, die);
23582}
23583
23584static void
23585dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23586{
23587 int indent = level * 4;
23588
23589 gdb_assert (die != NULL);
23590
23591 if (level >= max_level)
23592 return;
23593
23594 dump_die_shallow (f, indent, die);
23595
23596 if (die->child != NULL)
c906108c 23597 {
d97bc12b
DE
23598 print_spaces (indent, f);
23599 fprintf_unfiltered (f, " Children:");
23600 if (level + 1 < max_level)
23601 {
23602 fprintf_unfiltered (f, "\n");
23603 dump_die_1 (f, level + 1, max_level, die->child);
23604 }
23605 else
23606 {
3e43a32a
MS
23607 fprintf_unfiltered (f,
23608 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23609 }
23610 }
23611
23612 if (die->sibling != NULL && level > 0)
23613 {
23614 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23615 }
23616}
23617
d97bc12b
DE
23618/* This is called from the pdie macro in gdbinit.in.
23619 It's not static so gcc will keep a copy callable from gdb. */
23620
23621void
23622dump_die (struct die_info *die, int max_level)
23623{
23624 dump_die_1 (gdb_stdlog, 0, max_level, die);
23625}
23626
f9aca02d 23627static void
51545339 23628store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23629{
51545339 23630 void **slot;
c906108c 23631
9c541725
PA
23632 slot = htab_find_slot_with_hash (cu->die_hash, die,
23633 to_underlying (die->sect_off),
b64f50a1 23634 INSERT);
51545339
DJ
23635
23636 *slot = die;
c906108c
SS
23637}
23638
348e048f
DE
23639/* Follow reference or signature attribute ATTR of SRC_DIE.
23640 On entry *REF_CU is the CU of SRC_DIE.
23641 On exit *REF_CU is the CU of the result. */
23642
23643static struct die_info *
ff39bb5e 23644follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23645 struct dwarf2_cu **ref_cu)
23646{
23647 struct die_info *die;
23648
cd6c91b4 23649 if (attr->form_is_ref ())
348e048f 23650 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23651 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23652 die = follow_die_sig (src_die, attr, ref_cu);
23653 else
23654 {
23655 dump_die_for_error (src_die);
23656 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 23657 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
23658 }
23659
23660 return die;
03dd20cc
DJ
23661}
23662
5c631832 23663/* Follow reference OFFSET.
673bfd45
DE
23664 On entry *REF_CU is the CU of the source die referencing OFFSET.
23665 On exit *REF_CU is the CU of the result.
23666 Returns NULL if OFFSET is invalid. */
f504f079 23667
f9aca02d 23668static struct die_info *
9c541725 23669follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23670 struct dwarf2_cu **ref_cu)
c906108c 23671{
10b3939b 23672 struct die_info temp_die;
f2f0e013 23673 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 23674 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 23675
348e048f
DE
23676 gdb_assert (cu->per_cu != NULL);
23677
98bfdba5
PA
23678 target_cu = cu;
23679
17e593e9
SM
23680 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23681 "source CU contains target offset: %d",
23682 sect_offset_str (cu->per_cu->sect_off),
23683 sect_offset_str (sect_off),
23684 cu->header.offset_in_cu_p (sect_off));
23685
3019eac3 23686 if (cu->per_cu->is_debug_types)
348e048f
DE
23687 {
23688 /* .debug_types CUs cannot reference anything outside their CU.
23689 If they need to, they have to reference a signatured type via
55f1336d 23690 DW_FORM_ref_sig8. */
4057dfde 23691 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 23692 return NULL;
348e048f 23693 }
36586728 23694 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 23695 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
23696 {
23697 struct dwarf2_per_cu_data *per_cu;
9a619af0 23698
9c541725 23699 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 23700 per_objfile);
03dd20cc 23701
17e593e9
SM
23702 dwarf_read_debug_printf_v ("target CU offset: %s, "
23703 "target CU DIEs loaded: %d",
23704 sect_offset_str (per_cu->sect_off),
23705 per_objfile->get_cu (per_cu) != nullptr);
23706
03dd20cc 23707 /* If necessary, add it to the queue and load its DIEs. */
976ca316 23708 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
23709 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23710 false, cu->language);
03dd20cc 23711
976ca316 23712 target_cu = per_objfile->get_cu (per_cu);
10b3939b 23713 }
98bfdba5
PA
23714 else if (cu->dies == NULL)
23715 {
23716 /* We're loading full DIEs during partial symbol reading. */
976ca316 23717 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
4a636814
SM
23718 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23719 language_minimal);
98bfdba5 23720 }
c906108c 23721
f2f0e013 23722 *ref_cu = target_cu;
9c541725 23723 temp_die.sect_off = sect_off;
c24bdb02
KS
23724
23725 if (target_cu != cu)
23726 target_cu->ancestor = cu;
23727
9a3c8263 23728 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23729 &temp_die,
23730 to_underlying (sect_off));
5c631832 23731}
10b3939b 23732
5c631832
JK
23733/* Follow reference attribute ATTR of SRC_DIE.
23734 On entry *REF_CU is the CU of SRC_DIE.
23735 On exit *REF_CU is the CU of the result. */
23736
23737static struct die_info *
ff39bb5e 23738follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23739 struct dwarf2_cu **ref_cu)
23740{
0826b30a 23741 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
23742 struct dwarf2_cu *cu = *ref_cu;
23743 struct die_info *die;
23744
9c541725 23745 die = follow_die_offset (sect_off,
36586728
TT
23746 (attr->form == DW_FORM_GNU_ref_alt
23747 || cu->per_cu->is_dwz),
23748 ref_cu);
5c631832 23749 if (!die)
9d8780f0
SM
23750 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23751 "at %s [in module %s]"),
23752 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 23753 objfile_name (cu->per_objfile->objfile));
348e048f 23754
5c631832
JK
23755 return die;
23756}
23757
d4c9a4f8 23758/* See read.h. */
5c631832
JK
23759
23760struct dwarf2_locexpr_baton
9c541725 23761dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 23762 dwarf2_per_cu_data *per_cu,
976ca316 23763 dwarf2_per_objfile *per_objfile,
041d9819
SM
23764 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23765 bool resolve_abstract_p)
5c631832 23766{
5c631832
JK
23767 struct die_info *die;
23768 struct attribute *attr;
23769 struct dwarf2_locexpr_baton retval;
976ca316 23770 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 23771
976ca316 23772 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 23773 if (cu == nullptr)
976ca316 23774 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
23775
23776 if (cu == nullptr)
cc12ce38
DE
23777 {
23778 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23779 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23780 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23781 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23782 }
918dd910 23783
9c541725 23784 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23785 if (!die)
9d8780f0
SM
23786 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23787 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23788
23789 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23790 if (!attr && resolve_abstract_p
976ca316
SM
23791 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23792 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65 23793 {
041d9819 23794 CORE_ADDR pc = get_frame_pc ();
b3b3bada 23795 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 23796 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 23797
3360b6e7 23798 for (const auto &cand_off
976ca316 23799 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 23800 {
3360b6e7
TV
23801 struct dwarf2_cu *cand_cu = cu;
23802 struct die_info *cand
23803 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23804 if (!cand
23805 || !cand->parent
e4a62c65
TV
23806 || cand->parent->tag != DW_TAG_subprogram)
23807 continue;
23808
23809 CORE_ADDR pc_low, pc_high;
23810 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23811 if (pc_low == ((CORE_ADDR) -1))
23812 continue;
23813 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23814 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23815 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23816 continue;
23817
23818 die = cand;
23819 attr = dwarf2_attr (die, DW_AT_location, cu);
23820 break;
23821 }
23822 }
23823
5c631832
JK
23824 if (!attr)
23825 {
e103e986
JK
23826 /* DWARF: "If there is no such attribute, then there is no effect.".
23827 DATA is ignored if SIZE is 0. */
5c631832 23828
e103e986 23829 retval.data = NULL;
5c631832
JK
23830 retval.size = 0;
23831 }
cd6c91b4 23832 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23833 {
23834 struct dwarf2_loclist_baton loclist_baton;
041d9819 23835 CORE_ADDR pc = get_frame_pc ();
8cf6f0b1
TT
23836 size_t size;
23837
23838 fill_in_loclist_baton (cu, &loclist_baton, attr);
23839
23840 retval.data = dwarf2_find_location_expression (&loclist_baton,
23841 &size, pc);
23842 retval.size = size;
23843 }
5c631832
JK
23844 else
23845 {
4fc6c0d5 23846 if (!attr->form_is_block ())
9d8780f0 23847 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23848 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23849 sect_offset_str (sect_off), objfile_name (objfile));
5c631832 23850
9d2246fc
TT
23851 struct dwarf_block *block = attr->as_block ();
23852 retval.data = block->data;
23853 retval.size = block->size;
5c631832 23854 }
976ca316 23855 retval.per_objfile = per_objfile;
5c631832 23856 retval.per_cu = cu->per_cu;
918dd910 23857
976ca316 23858 per_objfile->age_comp_units ();
918dd910 23859
5c631832 23860 return retval;
348e048f
DE
23861}
23862
d4c9a4f8 23863/* See read.h. */
8b9737bf
TT
23864
23865struct dwarf2_locexpr_baton
23866dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 23867 dwarf2_per_cu_data *per_cu,
14095eb3 23868 dwarf2_per_objfile *per_objfile,
041d9819 23869 gdb::function_view<CORE_ADDR ()> get_frame_pc)
8b9737bf 23870{
9c541725 23871 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23872
14095eb3 23873 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
041d9819 23874 get_frame_pc);
8b9737bf
TT
23875}
23876
b6807d98
TT
23877/* Write a constant of a given type as target-ordered bytes into
23878 OBSTACK. */
23879
23880static const gdb_byte *
23881write_constant_as_bytes (struct obstack *obstack,
23882 enum bfd_endian byte_order,
23883 struct type *type,
23884 ULONGEST value,
23885 LONGEST *len)
23886{
23887 gdb_byte *result;
23888
23889 *len = TYPE_LENGTH (type);
224c3ddb 23890 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23891 store_unsigned_integer (result, *len, byte_order, value);
23892
23893 return result;
23894}
23895
d4c9a4f8 23896/* See read.h. */
b6807d98
TT
23897
23898const gdb_byte *
9c541725 23899dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 23900 dwarf2_per_cu_data *per_cu,
14095eb3 23901 dwarf2_per_objfile *per_objfile,
d4c9a4f8 23902 obstack *obstack,
b6807d98
TT
23903 LONGEST *len)
23904{
b6807d98
TT
23905 struct die_info *die;
23906 struct attribute *attr;
23907 const gdb_byte *result = NULL;
23908 struct type *type;
23909 LONGEST value;
23910 enum bfd_endian byte_order;
14095eb3 23911 struct objfile *objfile = per_objfile->objfile;
b6807d98 23912
7188ed02 23913 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23914 if (cu == nullptr)
23915 cu = load_cu (per_cu, per_objfile, false);
23916
23917 if (cu == nullptr)
cc12ce38
DE
23918 {
23919 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23920 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23921 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23922 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23923 }
b6807d98 23924
9c541725 23925 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23926 if (!die)
9d8780f0
SM
23927 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23928 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23929
23930 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23931 if (attr == NULL)
23932 return NULL;
23933
e3b94546 23934 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23935 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23936
23937 switch (attr->form)
23938 {
23939 case DW_FORM_addr:
336d760d 23940 case DW_FORM_addrx:
b6807d98
TT
23941 case DW_FORM_GNU_addr_index:
23942 {
23943 gdb_byte *tem;
23944
23945 *len = cu->header.addr_size;
224c3ddb 23946 tem = (gdb_byte *) obstack_alloc (obstack, *len);
36d378cf 23947 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
b6807d98
TT
23948 result = tem;
23949 }
23950 break;
23951 case DW_FORM_string:
23952 case DW_FORM_strp:
cf532bd1 23953 case DW_FORM_strx:
b6807d98
TT
23954 case DW_FORM_GNU_str_index:
23955 case DW_FORM_GNU_strp_alt:
c6481205 23956 /* The string is already allocated on the objfile obstack, point
b6807d98 23957 directly to it. */
2c830f54
TT
23958 {
23959 const char *attr_name = attr->as_string ();
23960 result = (const gdb_byte *) attr_name;
23961 *len = strlen (attr_name);
23962 }
b6807d98
TT
23963 break;
23964 case DW_FORM_block1:
23965 case DW_FORM_block2:
23966 case DW_FORM_block4:
23967 case DW_FORM_block:
23968 case DW_FORM_exprloc:
0224619f 23969 case DW_FORM_data16:
9d2246fc
TT
23970 {
23971 struct dwarf_block *block = attr->as_block ();
23972 result = block->data;
23973 *len = block->size;
23974 }
b6807d98
TT
23975 break;
23976
23977 /* The DW_AT_const_value attributes are supposed to carry the
23978 symbol's value "represented as it would be on the target
23979 architecture." By the time we get here, it's already been
23980 converted to host endianness, so we just need to sign- or
23981 zero-extend it as appropriate. */
23982 case DW_FORM_data1:
23983 type = die_type (die, cu);
23984 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23985 if (result == NULL)
23986 result = write_constant_as_bytes (obstack, byte_order,
23987 type, value, len);
23988 break;
23989 case DW_FORM_data2:
23990 type = die_type (die, cu);
23991 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23992 if (result == NULL)
23993 result = write_constant_as_bytes (obstack, byte_order,
23994 type, value, len);
23995 break;
23996 case DW_FORM_data4:
23997 type = die_type (die, cu);
23998 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23999 if (result == NULL)
24000 result = write_constant_as_bytes (obstack, byte_order,
24001 type, value, len);
24002 break;
24003 case DW_FORM_data8:
24004 type = die_type (die, cu);
24005 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
24006 if (result == NULL)
24007 result = write_constant_as_bytes (obstack, byte_order,
24008 type, value, len);
24009 break;
24010
24011 case DW_FORM_sdata:
663c44ac 24012 case DW_FORM_implicit_const:
b6807d98
TT
24013 type = die_type (die, cu);
24014 result = write_constant_as_bytes (obstack, byte_order,
1bc397c5 24015 type, attr->as_signed (), len);
b6807d98
TT
24016 break;
24017
24018 case DW_FORM_udata:
24019 type = die_type (die, cu);
24020 result = write_constant_as_bytes (obstack, byte_order,
529908cb 24021 type, attr->as_unsigned (), len);
b6807d98
TT
24022 break;
24023
24024 default:
b98664d3 24025 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
24026 dwarf_form_name (attr->form));
24027 break;
24028 }
24029
24030 return result;
24031}
24032
d4c9a4f8 24033/* See read.h. */
7942e96e
AA
24034
24035struct type *
9c541725 24036dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
24037 dwarf2_per_cu_data *per_cu,
24038 dwarf2_per_objfile *per_objfile)
7942e96e 24039{
7942e96e
AA
24040 struct die_info *die;
24041
7188ed02 24042 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
24043 if (cu == nullptr)
24044 cu = load_cu (per_cu, per_objfile, false);
24045
24046 if (cu == nullptr)
24047 return nullptr;
7942e96e 24048
9c541725 24049 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
24050 if (!die)
24051 return NULL;
24052
24053 return die_type (die, cu);
24054}
24055
8cb5117c 24056/* See read.h. */
8a9b8146
TT
24057
24058struct type *
b64f50a1 24059dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
24060 dwarf2_per_cu_data *per_cu,
24061 dwarf2_per_objfile *per_objfile)
8a9b8146 24062{
9c541725 24063 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 24064 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
24065}
24066
ac9ec31b 24067/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 24068 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
24069 On exit *REF_CU is the CU of the result.
24070 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
24071
24072static struct die_info *
ac9ec31b
DE
24073follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
24074 struct dwarf2_cu **ref_cu)
348e048f 24075{
348e048f 24076 struct die_info temp_die;
c24bdb02 24077 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 24078 struct die_info *die;
976ca316 24079 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 24080
348e048f 24081
ac9ec31b
DE
24082 /* While it might be nice to assert sig_type->type == NULL here,
24083 we can get here for DW_AT_imported_declaration where we need
24084 the DIE not the type. */
348e048f
DE
24085
24086 /* If necessary, add it to the queue and load its DIEs. */
24087
976ca316 24088 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
120ce1b5 24089 language_minimal))
976ca316 24090 read_signatured_type (sig_type, per_objfile);
348e048f 24091
976ca316 24092 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
69d751e3 24093 gdb_assert (sig_cu != NULL);
9c541725
PA
24094 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
24095 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 24096 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 24097 to_underlying (temp_die.sect_off));
348e048f
DE
24098 if (die)
24099 {
796a7ff8
DE
24100 /* For .gdb_index version 7 keep track of included TUs.
24101 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
24102 if (per_objfile->per_bfd->index_table != NULL
24103 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 24104 {
ae640021 24105 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
24106 }
24107
348e048f 24108 *ref_cu = sig_cu;
c24bdb02
KS
24109 if (sig_cu != cu)
24110 sig_cu->ancestor = cu;
24111
348e048f
DE
24112 return die;
24113 }
24114
ac9ec31b
DE
24115 return NULL;
24116}
24117
24118/* Follow signatured type referenced by ATTR in SRC_DIE.
24119 On entry *REF_CU is the CU of SRC_DIE.
24120 On exit *REF_CU is the CU of the result.
24121 The result is the DIE of the type.
24122 If the referenced type cannot be found an error is thrown. */
24123
24124static struct die_info *
ff39bb5e 24125follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
24126 struct dwarf2_cu **ref_cu)
24127{
630ed6b9 24128 ULONGEST signature = attr->as_signature ();
ac9ec31b
DE
24129 struct signatured_type *sig_type;
24130 struct die_info *die;
24131
24132 gdb_assert (attr->form == DW_FORM_ref_sig8);
24133
a2ce51a0 24134 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
24135 /* sig_type will be NULL if the signatured type is missing from
24136 the debug info. */
24137 if (sig_type == NULL)
24138 {
24139 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
dda83cd7
SM
24140 " from DIE at %s [in module %s]"),
24141 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 24142 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
24143 }
24144
24145 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24146 if (die == NULL)
24147 {
24148 dump_die_for_error (src_die);
24149 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24150 " from DIE at %s [in module %s]"),
24151 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 24152 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
24153 }
24154
24155 return die;
24156}
24157
24158/* Get the type specified by SIGNATURE referenced in DIE/CU,
24159 reading in and processing the type unit if necessary. */
24160
24161static struct type *
24162get_signatured_type (struct die_info *die, ULONGEST signature,
24163 struct dwarf2_cu *cu)
24164{
976ca316 24165 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
24166 struct signatured_type *sig_type;
24167 struct dwarf2_cu *type_cu;
24168 struct die_info *type_die;
24169 struct type *type;
24170
a2ce51a0 24171 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
24172 /* sig_type will be NULL if the signatured type is missing from
24173 the debug info. */
24174 if (sig_type == NULL)
24175 {
b98664d3 24176 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
24177 " from DIE at %s [in module %s]"),
24178 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24179 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24180 return build_error_marker_type (cu, die);
24181 }
24182
24183 /* If we already know the type we're done. */
976ca316 24184 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
24185 if (type != nullptr)
24186 return type;
ac9ec31b
DE
24187
24188 type_cu = cu;
24189 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24190 if (type_die != NULL)
24191 {
24192 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24193 is created. This is important, for example, because for c++ classes
24194 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24195 type = read_type_die (type_die, type_cu);
24196 if (type == NULL)
24197 {
b98664d3 24198 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
24199 " referenced from DIE at %s [in module %s]"),
24200 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24201 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24202 type = build_error_marker_type (cu, die);
24203 }
24204 }
24205 else
24206 {
b98664d3 24207 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24208 " from DIE at %s [in module %s]"),
24209 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24210 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24211 type = build_error_marker_type (cu, die);
24212 }
e286671b 24213
976ca316 24214 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
24215
24216 return type;
24217}
24218
24219/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24220 reading in and processing the type unit if necessary. */
24221
24222static struct type *
ff39bb5e 24223get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 24224 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
24225{
24226 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 24227 if (attr->form_is_ref ())
ac9ec31b
DE
24228 {
24229 struct dwarf2_cu *type_cu = cu;
24230 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24231
24232 return read_type_die (type_die, type_cu);
24233 }
24234 else if (attr->form == DW_FORM_ref_sig8)
24235 {
630ed6b9 24236 return get_signatured_type (die, attr->as_signature (), cu);
ac9ec31b
DE
24237 }
24238 else
24239 {
976ca316 24240 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24241
b98664d3 24242 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
24243 " at %s [in module %s]"),
24244 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 24245 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24246 return build_error_marker_type (cu, die);
24247 }
348e048f
DE
24248}
24249
e5fe5e75 24250/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
24251
24252static void
ab432490
SM
24253load_full_type_unit (dwarf2_per_cu_data *per_cu,
24254 dwarf2_per_objfile *per_objfile)
348e048f 24255{
52dc124a 24256 struct signatured_type *sig_type;
348e048f 24257
f4dc4d17 24258 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 24259 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 24260
6721b2ec
DE
24261 /* We have the per_cu, but we need the signatured_type.
24262 Fortunately this is an easy translation. */
24263 gdb_assert (per_cu->is_debug_types);
24264 sig_type = (struct signatured_type *) per_cu;
348e048f 24265
7188ed02 24266 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24267
ab432490 24268 read_signatured_type (sig_type, per_objfile);
348e048f 24269
7188ed02 24270 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
24271}
24272
3019eac3
DE
24273/* Read in a signatured type and build its CU and DIEs.
24274 If the type is a stub for the real type in a DWO file,
24275 read in the real type from the DWO file as well. */
dee91e82
DE
24276
24277static void
ab432490
SM
24278read_signatured_type (signatured_type *sig_type,
24279 dwarf2_per_objfile *per_objfile)
dee91e82
DE
24280{
24281 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24282
3019eac3 24283 gdb_assert (per_cu->is_debug_types);
7188ed02 24284 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24285
2e671100 24286 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
24287
24288 if (!reader.dummy_p)
24289 {
24290 struct dwarf2_cu *cu = reader.cu;
24291 const gdb_byte *info_ptr = reader.info_ptr;
24292
24293 gdb_assert (cu->die_hash == NULL);
24294 cu->die_hash =
24295 htab_create_alloc_ex (cu->header.length / 12,
24296 die_hash,
24297 die_eq,
24298 NULL,
24299 &cu->comp_unit_obstack,
24300 hashtab_obstack_allocate,
24301 dummy_obstack_deallocate);
24302
3e225074 24303 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
24304 reader.comp_unit_die->child
24305 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24306 reader.comp_unit_die);
24307 cu->dies = reader.comp_unit_die;
24308 /* comp_unit_die is not stored in die_hash, no need. */
24309
24310 /* We try not to read any attributes in this function, because
24311 not all CUs needed for references have been loaded yet, and
24312 symbol table processing isn't initialized. But we have to
24313 set the CU language, or we won't be able to build types
24314 correctly. Similarly, if we do not read the producer, we can
24315 not apply producer-specific interpretation. */
24316 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
24317
24318 reader.keep ();
c0ab21c2
TT
24319 }
24320
7ee85ab1 24321 sig_type->per_cu.tu_read = 1;
c906108c
SS
24322}
24323
c906108c
SS
24324/* Decode simple location descriptions.
24325 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
24326 the location and return the value. If COMPUTED is non-null, it is
24327 set to true to indicate that decoding was successful, and false
24328 otherwise. If COMPUTED is null, then this function may emit a
24329 complaint. */
c906108c
SS
24330
24331static CORE_ADDR
7d79de9a 24332decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 24333{
5e22e966 24334 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
24335 size_t i;
24336 size_t size = blk->size;
d521ce57 24337 const gdb_byte *data = blk->data;
21ae7a4d
JK
24338 CORE_ADDR stack[64];
24339 int stacki;
24340 unsigned int bytes_read, unsnd;
24341 gdb_byte op;
c906108c 24342
7d79de9a
TT
24343 if (computed != nullptr)
24344 *computed = false;
24345
21ae7a4d
JK
24346 i = 0;
24347 stacki = 0;
24348 stack[stacki] = 0;
24349 stack[++stacki] = 0;
24350
24351 while (i < size)
24352 {
24353 op = data[i++];
24354 switch (op)
24355 {
24356 case DW_OP_lit0:
24357 case DW_OP_lit1:
24358 case DW_OP_lit2:
24359 case DW_OP_lit3:
24360 case DW_OP_lit4:
24361 case DW_OP_lit5:
24362 case DW_OP_lit6:
24363 case DW_OP_lit7:
24364 case DW_OP_lit8:
24365 case DW_OP_lit9:
24366 case DW_OP_lit10:
24367 case DW_OP_lit11:
24368 case DW_OP_lit12:
24369 case DW_OP_lit13:
24370 case DW_OP_lit14:
24371 case DW_OP_lit15:
24372 case DW_OP_lit16:
24373 case DW_OP_lit17:
24374 case DW_OP_lit18:
24375 case DW_OP_lit19:
24376 case DW_OP_lit20:
24377 case DW_OP_lit21:
24378 case DW_OP_lit22:
24379 case DW_OP_lit23:
24380 case DW_OP_lit24:
24381 case DW_OP_lit25:
24382 case DW_OP_lit26:
24383 case DW_OP_lit27:
24384 case DW_OP_lit28:
24385 case DW_OP_lit29:
24386 case DW_OP_lit30:
24387 case DW_OP_lit31:
24388 stack[++stacki] = op - DW_OP_lit0;
24389 break;
f1bea926 24390
21ae7a4d
JK
24391 case DW_OP_reg0:
24392 case DW_OP_reg1:
24393 case DW_OP_reg2:
24394 case DW_OP_reg3:
24395 case DW_OP_reg4:
24396 case DW_OP_reg5:
24397 case DW_OP_reg6:
24398 case DW_OP_reg7:
24399 case DW_OP_reg8:
24400 case DW_OP_reg9:
24401 case DW_OP_reg10:
24402 case DW_OP_reg11:
24403 case DW_OP_reg12:
24404 case DW_OP_reg13:
24405 case DW_OP_reg14:
24406 case DW_OP_reg15:
24407 case DW_OP_reg16:
24408 case DW_OP_reg17:
24409 case DW_OP_reg18:
24410 case DW_OP_reg19:
24411 case DW_OP_reg20:
24412 case DW_OP_reg21:
24413 case DW_OP_reg22:
24414 case DW_OP_reg23:
24415 case DW_OP_reg24:
24416 case DW_OP_reg25:
24417 case DW_OP_reg26:
24418 case DW_OP_reg27:
24419 case DW_OP_reg28:
24420 case DW_OP_reg29:
24421 case DW_OP_reg30:
24422 case DW_OP_reg31:
24423 stack[++stacki] = op - DW_OP_reg0;
24424 if (i < size)
7d79de9a
TT
24425 {
24426 if (computed == nullptr)
24427 dwarf2_complex_location_expr_complaint ();
24428 else
24429 return 0;
24430 }
21ae7a4d 24431 break;
c906108c 24432
21ae7a4d
JK
24433 case DW_OP_regx:
24434 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24435 i += bytes_read;
24436 stack[++stacki] = unsnd;
24437 if (i < size)
7d79de9a
TT
24438 {
24439 if (computed == nullptr)
24440 dwarf2_complex_location_expr_complaint ();
24441 else
24442 return 0;
24443 }
21ae7a4d 24444 break;
c906108c 24445
21ae7a4d 24446 case DW_OP_addr:
c8a7a66f
TT
24447 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24448 &bytes_read);
21ae7a4d
JK
24449 i += bytes_read;
24450 break;
d53d4ac5 24451
21ae7a4d
JK
24452 case DW_OP_const1u:
24453 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24454 i += 1;
24455 break;
24456
24457 case DW_OP_const1s:
24458 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24459 i += 1;
24460 break;
24461
24462 case DW_OP_const2u:
24463 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24464 i += 2;
24465 break;
24466
24467 case DW_OP_const2s:
24468 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24469 i += 2;
24470 break;
d53d4ac5 24471
21ae7a4d
JK
24472 case DW_OP_const4u:
24473 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24474 i += 4;
24475 break;
24476
24477 case DW_OP_const4s:
24478 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24479 i += 4;
24480 break;
24481
585861ea
JK
24482 case DW_OP_const8u:
24483 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24484 i += 8;
24485 break;
24486
21ae7a4d
JK
24487 case DW_OP_constu:
24488 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24489 &bytes_read);
24490 i += bytes_read;
24491 break;
24492
24493 case DW_OP_consts:
24494 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24495 i += bytes_read;
24496 break;
24497
24498 case DW_OP_dup:
24499 stack[stacki + 1] = stack[stacki];
24500 stacki++;
24501 break;
24502
24503 case DW_OP_plus:
24504 stack[stacki - 1] += stack[stacki];
24505 stacki--;
24506 break;
24507
24508 case DW_OP_plus_uconst:
24509 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24510 &bytes_read);
24511 i += bytes_read;
24512 break;
24513
24514 case DW_OP_minus:
24515 stack[stacki - 1] -= stack[stacki];
24516 stacki--;
24517 break;
24518
24519 case DW_OP_deref:
24520 /* If we're not the last op, then we definitely can't encode
24521 this using GDB's address_class enum. This is valid for partial
24522 global symbols, although the variable's address will be bogus
24523 in the psymtab. */
24524 if (i < size)
7d79de9a
TT
24525 {
24526 if (computed == nullptr)
24527 dwarf2_complex_location_expr_complaint ();
24528 else
24529 return 0;
24530 }
21ae7a4d
JK
24531 break;
24532
dda83cd7 24533 case DW_OP_GNU_push_tls_address:
4aa4e28b 24534 case DW_OP_form_tls_address:
21ae7a4d
JK
24535 /* The top of the stack has the offset from the beginning
24536 of the thread control block at which the variable is located. */
24537 /* Nothing should follow this operator, so the top of stack would
24538 be returned. */
24539 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24540 address will be bogus in the psymtab. Make it always at least
24541 non-zero to not look as a variable garbage collected by linker
24542 which have DW_OP_addr 0. */
21ae7a4d 24543 if (i < size)
7d79de9a
TT
24544 {
24545 if (computed == nullptr)
24546 dwarf2_complex_location_expr_complaint ();
24547 else
24548 return 0;
24549 }
585861ea 24550 stack[stacki]++;
dda83cd7 24551 break;
21ae7a4d
JK
24552
24553 case DW_OP_GNU_uninit:
7d79de9a
TT
24554 if (computed != nullptr)
24555 return 0;
21ae7a4d
JK
24556 break;
24557
336d760d 24558 case DW_OP_addrx:
3019eac3 24559 case DW_OP_GNU_addr_index:
49f6c839 24560 case DW_OP_GNU_const_index:
3019eac3
DE
24561 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24562 &bytes_read);
24563 i += bytes_read;
24564 break;
24565
21ae7a4d 24566 default:
7d79de9a
TT
24567 if (computed == nullptr)
24568 {
24569 const char *name = get_DW_OP_name (op);
21ae7a4d 24570
7d79de9a
TT
24571 if (name)
24572 complaint (_("unsupported stack op: '%s'"),
24573 name);
24574 else
24575 complaint (_("unsupported stack op: '%02x'"),
24576 op);
24577 }
21ae7a4d
JK
24578
24579 return (stack[stacki]);
d53d4ac5 24580 }
3c6e0cb3 24581
21ae7a4d 24582 /* Enforce maximum stack depth of SIZE-1 to avoid writing
dda83cd7 24583 outside of the allocated space. Also enforce minimum>0. */
21ae7a4d
JK
24584 if (stacki >= ARRAY_SIZE (stack) - 1)
24585 {
7d79de9a
TT
24586 if (computed == nullptr)
24587 complaint (_("location description stack overflow"));
21ae7a4d
JK
24588 return 0;
24589 }
24590
24591 if (stacki <= 0)
24592 {
7d79de9a
TT
24593 if (computed == nullptr)
24594 complaint (_("location description stack underflow"));
21ae7a4d
JK
24595 return 0;
24596 }
24597 }
7d79de9a
TT
24598
24599 if (computed != nullptr)
24600 *computed = true;
21ae7a4d 24601 return (stack[stacki]);
c906108c
SS
24602}
24603
24604/* memory allocation interface */
24605
c906108c 24606static struct dwarf_block *
7b5a2f43 24607dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24608{
8d749320 24609 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24610}
24611
c906108c 24612static struct die_info *
b60c80d6 24613dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24614{
24615 struct die_info *die;
b60c80d6
DJ
24616 size_t size = sizeof (struct die_info);
24617
24618 if (num_attrs > 1)
24619 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24620
b60c80d6 24621 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24622 memset (die, 0, sizeof (struct die_info));
24623 return (die);
24624}
2e276125
JB
24625
24626\f
a036ba48 24627
c90ec28a 24628/* Macro support. */
cf2c3c16 24629
9eac9650
TT
24630/* An overload of dwarf_decode_macros that finds the correct section
24631 and ensures it is read in before calling the other overload. */
24632
24633static void
24634dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24635 int section_is_gnu)
24636{
976ca316
SM
24637 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24638 struct objfile *objfile = per_objfile->objfile;
5a0e026f 24639 const struct line_header *lh = cu->line_header;
9eac9650
TT
24640 unsigned int offset_size = cu->header.offset_size;
24641 struct dwarf2_section_info *section;
24642 const char *section_name;
24643
24644 if (cu->dwo_unit != nullptr)
24645 {
24646 if (section_is_gnu)
24647 {
24648 section = &cu->dwo_unit->dwo_file->sections.macro;
24649 section_name = ".debug_macro.dwo";
24650 }
24651 else
24652 {
24653 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24654 section_name = ".debug_macinfo.dwo";
24655 }
24656 }
24657 else
24658 {
24659 if (section_is_gnu)
24660 {
976ca316 24661 section = &per_objfile->per_bfd->macro;
9eac9650
TT
24662 section_name = ".debug_macro";
24663 }
24664 else
24665 {
976ca316 24666 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
24667 section_name = ".debug_macinfo";
24668 }
24669 }
24670
24671 section->read (objfile);
24672 if (section->buffer == nullptr)
24673 {
24674 complaint (_("missing %s section"), section_name);
24675 return;
24676 }
24677
24678 buildsym_compunit *builder = cu->get_builder ();
24679
048fde1e 24680 struct dwarf2_section_info *str_offsets_section;
24681 struct dwarf2_section_info *str_section;
24682 ULONGEST str_offsets_base;
24683
24684 if (cu->dwo_unit != nullptr)
24685 {
24686 str_offsets_section = &cu->dwo_unit->dwo_file
24687 ->sections.str_offsets;
24688 str_section = &cu->dwo_unit->dwo_file->sections.str;
24689 str_offsets_base = cu->header.addr_size;
24690 }
24691 else
24692 {
24693 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24694 str_section = &per_objfile->per_bfd->str;
24695 str_offsets_base = *cu->str_offsets_base;
24696 }
24697
976ca316 24698 dwarf_decode_macros (per_objfile, builder, section, lh,
048fde1e 24699 offset_size, offset, str_section, str_offsets_section,
24700 str_offsets_base, section_is_gnu);
9eac9650
TT
24701}
24702
3019eac3
DE
24703/* Return the .debug_loc section to use for CU.
24704 For DWO files use .debug_loc.dwo. */
24705
24706static struct dwarf2_section_info *
24707cu_debug_loc_section (struct dwarf2_cu *cu)
24708{
976ca316 24709 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24710
3019eac3 24711 if (cu->dwo_unit)
43988095
JK
24712 {
24713 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24714
43988095
JK
24715 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24716 }
976ca316
SM
24717 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24718 : &per_objfile->per_bfd->loc);
3019eac3
DE
24719}
24720
d0ce17d8
CT
24721/* Return the .debug_rnglists section to use for CU. */
24722static struct dwarf2_section_info *
24723cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24724{
24725 if (cu->header.version < 5)
24726 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24727 cu->header.version);
24728 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24729
24730 /* Make sure we read the .debug_rnglists section from the file that
24731 contains the DW_AT_ranges attribute we are reading. Normally that
24732 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24733 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24734 program. */
24735 if (cu->dwo_unit != nullptr
24736 && tag != DW_TAG_compile_unit
24737 && tag != DW_TAG_skeleton_unit)
24738 {
24739 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24740
24741 if (sections->rnglists.size > 0)
24742 return &sections->rnglists;
24743 else
24744 error (_(".debug_rnglists section is missing from .dwo file."));
24745 }
24746 return &dwarf2_per_objfile->per_bfd->rnglists;
24747}
24748
8cf6f0b1
TT
24749/* A helper function that fills in a dwarf2_loclist_baton. */
24750
24751static void
24752fill_in_loclist_baton (struct dwarf2_cu *cu,
24753 struct dwarf2_loclist_baton *baton,
ff39bb5e 24754 const struct attribute *attr)
8cf6f0b1 24755{
976ca316 24756 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
24757 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24758
976ca316 24759 section->read (per_objfile->objfile);
8cf6f0b1 24760
976ca316 24761 baton->per_objfile = per_objfile;
8cf6f0b1
TT
24762 baton->per_cu = cu->per_cu;
24763 gdb_assert (baton->per_cu);
24764 /* We don't know how long the location list is, but make sure we
24765 don't run off the edge of the section. */
d4df075e
TT
24766 baton->size = section->size - attr->as_unsigned ();
24767 baton->data = section->buffer + attr->as_unsigned ();
2b24b6e4
TT
24768 if (cu->base_address.has_value ())
24769 baton->base_address = *cu->base_address;
24770 else
24771 baton->base_address = 0;
f664829e 24772 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24773}
24774
4c2df51b 24775static void
ff39bb5e 24776dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24777 struct dwarf2_cu *cu, int is_block)
4c2df51b 24778{
976ca316
SM
24779 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24780 struct objfile *objfile = per_objfile->objfile;
3019eac3 24781 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24782
cd6c91b4 24783 if (attr->form_is_section_offset ()
3019eac3 24784 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24785 the section. If so, fall through to the complaint in the
24786 other branch. */
d4df075e 24787 && attr->as_unsigned () < section->get_size (objfile))
4c2df51b 24788 {
0d53c4c4 24789 struct dwarf2_loclist_baton *baton;
4c2df51b 24790
8d749320 24791 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24792
8cf6f0b1 24793 fill_in_loclist_baton (cu, baton, attr);
be391dca 24794
2b24b6e4 24795 if (!cu->base_address.has_value ())
b98664d3 24796 complaint (_("Location list used without "
3e43a32a 24797 "specifying the CU base address."));
4c2df51b 24798
f1e6e072
TT
24799 SYMBOL_ACLASS_INDEX (sym) = (is_block
24800 ? dwarf2_loclist_block_index
24801 : dwarf2_loclist_index);
0d53c4c4
DJ
24802 SYMBOL_LOCATION_BATON (sym) = baton;
24803 }
24804 else
24805 {
24806 struct dwarf2_locexpr_baton *baton;
24807
8d749320 24808 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 24809 baton->per_objfile = per_objfile;
ae0d2f24
UW
24810 baton->per_cu = cu->per_cu;
24811 gdb_assert (baton->per_cu);
0d53c4c4 24812
4fc6c0d5 24813 if (attr->form_is_block ())
0d53c4c4
DJ
24814 {
24815 /* Note that we're just copying the block's data pointer
24816 here, not the actual data. We're still pointing into the
6502dd73
DJ
24817 info_buffer for SYM's objfile; right now we never release
24818 that buffer, but when we do clean up properly this may
24819 need to change. */
9d2246fc
TT
24820 struct dwarf_block *block = attr->as_block ();
24821 baton->size = block->size;
24822 baton->data = block->data;
0d53c4c4
DJ
24823 }
24824 else
24825 {
24826 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24827 sym->natural_name ());
0d53c4c4 24828 baton->size = 0;
0d53c4c4 24829 }
6e70227d 24830
f1e6e072
TT
24831 SYMBOL_ACLASS_INDEX (sym) = (is_block
24832 ? dwarf2_locexpr_block_index
24833 : dwarf2_locexpr_index);
0d53c4c4
DJ
24834 SYMBOL_LOCATION_BATON (sym) = baton;
24835 }
4c2df51b 24836}
6502dd73 24837
2e6a9f79 24838/* See read.h. */
96408a79 24839
2e6a9f79
SM
24840const comp_unit_head *
24841dwarf2_per_cu_data::get_header () const
96408a79 24842{
2e6a9f79
SM
24843 if (!m_header_read_in)
24844 {
24845 const gdb_byte *info_ptr
24846 = this->section->buffer + to_underlying (this->sect_off);
96408a79 24847
2e6a9f79 24848 memset (&m_header, 0, sizeof (m_header));
96408a79 24849
2e6a9f79
SM
24850 read_comp_unit_head (&m_header, info_ptr, this->section,
24851 rcuh_kind::COMPILE);
24852 }
96408a79 24853
2e6a9f79 24854 return &m_header;
96408a79
SA
24855}
24856
09ba997f 24857/* See read.h. */
ae0d2f24 24858
98714339 24859int
09ba997f 24860dwarf2_per_cu_data::addr_size () const
ae0d2f24 24861{
2e6a9f79 24862 return this->get_header ()->addr_size;
ae0d2f24
UW
24863}
24864
09ba997f 24865/* See read.h. */
9eae7c52
TT
24866
24867int
09ba997f 24868dwarf2_per_cu_data::offset_size () const
9eae7c52 24869{
2e6a9f79 24870 return this->get_header ()->offset_size;
96408a79
SA
24871}
24872
09ba997f 24873/* See read.h. */
96408a79
SA
24874
24875int
09ba997f 24876dwarf2_per_cu_data::ref_addr_size () const
96408a79 24877{
2e6a9f79 24878 const comp_unit_head *header = this->get_header ();
96408a79 24879
2e6a9f79
SM
24880 if (header->version == 2)
24881 return header->addr_size;
96408a79 24882 else
2e6a9f79 24883 return header->offset_size;
181cebd4
JK
24884}
24885
09ba997f 24886/* See read.h. */
9aa1f1e3 24887
09ba997f 24888struct type *
293e7e51 24889dwarf2_cu::addr_type () const
9a49df9d 24890{
293e7e51 24891 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
24892 struct type *void_type = objfile_type (objfile)->builtin_void;
24893 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 24894 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
24895
24896 if (TYPE_LENGTH (addr_type) == addr_size)
24897 return addr_type;
24898
c6d940a9 24899 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
9a49df9d
AB
24900 return addr_type;
24901}
24902
22b6cd70
TT
24903/* A helper function for dwarf2_find_containing_comp_unit that returns
24904 the index of the result, and that searches a vector. It will
24905 return a result even if the offset in question does not actually
24906 occur in any CU. This is separate so that it can be unit
24907 tested. */
ae038cb0 24908
22b6cd70
TT
24909static int
24910dwarf2_find_containing_comp_unit
24911 (sect_offset sect_off,
24912 unsigned int offset_in_dwz,
24913 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 24914{
ae038cb0
DJ
24915 int low, high;
24916
ae038cb0 24917 low = 0;
22b6cd70 24918 high = all_comp_units.size () - 1;
ae038cb0
DJ
24919 while (high > low)
24920 {
36586728 24921 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24922 int mid = low + (high - low) / 2;
9a619af0 24923
22b6cd70 24924 mid_cu = all_comp_units[mid];
36586728 24925 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24926 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24927 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24928 high = mid;
24929 else
24930 low = mid + 1;
24931 }
24932 gdb_assert (low == high);
22b6cd70
TT
24933 return low;
24934}
24935
24936/* Locate the .debug_info compilation unit from CU's objfile which contains
24937 the DIE at OFFSET. Raises an error on failure. */
24938
24939static struct dwarf2_per_cu_data *
24940dwarf2_find_containing_comp_unit (sect_offset sect_off,
24941 unsigned int offset_in_dwz,
976ca316 24942 dwarf2_per_objfile *per_objfile)
22b6cd70 24943{
976ca316
SM
24944 int low = dwarf2_find_containing_comp_unit
24945 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24946 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
22b6cd70 24947
45b8ae0c 24948 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24949 {
36586728 24950 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24951 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24952 "offset %s [in module %s]"),
24953 sect_offset_str (sect_off),
976ca316 24954 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 24955
976ca316 24956 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 24957 <= sect_off);
976ca316 24958 return per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
24959 }
24960 else
24961 {
976ca316 24962 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 24963 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24964 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24965 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24966 return this_cu;
24967 }
24968}
24969
22b6cd70
TT
24970#if GDB_SELF_TEST
24971
24972namespace selftests {
24973namespace find_containing_comp_unit {
24974
24975static void
24976run_test ()
24977{
24978 struct dwarf2_per_cu_data one {};
24979 struct dwarf2_per_cu_data two {};
24980 struct dwarf2_per_cu_data three {};
24981 struct dwarf2_per_cu_data four {};
24982
24983 one.length = 5;
24984 two.sect_off = sect_offset (one.length);
24985 two.length = 7;
24986
24987 three.length = 5;
24988 three.is_dwz = 1;
24989 four.sect_off = sect_offset (three.length);
24990 four.length = 7;
24991 four.is_dwz = 1;
24992
24993 std::vector<dwarf2_per_cu_data *> units;
24994 units.push_back (&one);
24995 units.push_back (&two);
24996 units.push_back (&three);
24997 units.push_back (&four);
24998
24999 int result;
25000
25001 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
25002 SELF_CHECK (units[result] == &one);
25003 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
25004 SELF_CHECK (units[result] == &one);
25005 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
25006 SELF_CHECK (units[result] == &two);
25007
25008 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
25009 SELF_CHECK (units[result] == &three);
25010 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
25011 SELF_CHECK (units[result] == &three);
25012 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
25013 SELF_CHECK (units[result] == &four);
25014}
25015
25016}
25017}
25018
25019#endif /* GDB_SELF_TEST */
25020
9e021579 25021/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 25022
9e021579
SM
25023dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
25024 dwarf2_per_objfile *per_objfile)
25025 : per_cu (per_cu),
25026 per_objfile (per_objfile),
9068261f
AB
25027 mark (false),
25028 has_loclist (false),
25029 checked_producer (false),
25030 producer_is_gxx_lt_4_6 (false),
25031 producer_is_gcc_lt_4_3 (false),
eb77c9df 25032 producer_is_icc (false),
9068261f 25033 producer_is_icc_lt_14 (false),
c258c396 25034 producer_is_codewarrior (false),
9068261f 25035 processing_has_namespace_info (false)
93311388 25036{
9816fde3
JK
25037}
25038
25039/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25040
25041static void
95554aad
TT
25042prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25043 enum language pretend_language)
9816fde3
JK
25044{
25045 struct attribute *attr;
25046
25047 /* Set the language we're debugging. */
25048 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 25049 if (attr != nullptr)
529908cb 25050 set_cu_language (attr->constant_value (0), cu);
9816fde3 25051 else
9cded63f 25052 {
95554aad 25053 cu->language = pretend_language;
9cded63f
TT
25054 cu->language_defn = language_def (cu->language);
25055 }
dee91e82 25056
7d45c7c3 25057 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25058}
25059
7188ed02 25060/* See read.h. */
ae038cb0 25061
7188ed02
SM
25062dwarf2_cu *
25063dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 25064{
7188ed02
SM
25065 auto it = m_dwarf2_cus.find (per_cu);
25066 if (it == m_dwarf2_cus.end ())
25067 return nullptr;
ae038cb0 25068
7188ed02
SM
25069 return it->second;
25070}
25071
25072/* See read.h. */
25073
25074void
25075dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
25076{
25077 gdb_assert (this->get_cu (per_cu) == nullptr);
25078
25079 m_dwarf2_cus[per_cu] = cu;
25080}
25081
25082/* See read.h. */
25083
25084void
25085dwarf2_per_objfile::age_comp_units ()
25086{
17e593e9
SM
25087 dwarf_read_debug_printf_v ("running");
25088
7188ed02
SM
25089 /* Start by clearing all marks. */
25090 for (auto pair : m_dwarf2_cus)
25091 pair.second->mark = false;
25092
25093 /* Traverse all CUs, mark them and their dependencies if used recently
25094 enough. */
25095 for (auto pair : m_dwarf2_cus)
ae038cb0 25096 {
7188ed02
SM
25097 dwarf2_cu *cu = pair.second;
25098
25099 cu->last_used++;
25100 if (cu->last_used <= dwarf_max_cache_age)
25101 dwarf2_mark (cu);
ae038cb0
DJ
25102 }
25103
7188ed02
SM
25104 /* Delete all CUs still not marked. */
25105 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 25106 {
7188ed02 25107 dwarf2_cu *cu = it->second;
ae038cb0 25108
7188ed02 25109 if (!cu->mark)
ae038cb0 25110 {
17e593e9
SM
25111 dwarf_read_debug_printf_v ("deleting old CU %s",
25112 sect_offset_str (cu->per_cu->sect_off));
7188ed02
SM
25113 delete cu;
25114 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
25115 }
25116 else
7188ed02 25117 it++;
ae038cb0
DJ
25118 }
25119}
25120
7188ed02 25121/* See read.h. */
ae038cb0 25122
7188ed02
SM
25123void
25124dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 25125{
7188ed02
SM
25126 auto it = m_dwarf2_cus.find (per_cu);
25127 if (it == m_dwarf2_cus.end ())
25128 return;
ae038cb0 25129
7188ed02 25130 delete it->second;
ae038cb0 25131
7188ed02
SM
25132 m_dwarf2_cus.erase (it);
25133}
ae038cb0 25134
7188ed02
SM
25135dwarf2_per_objfile::~dwarf2_per_objfile ()
25136{
25137 remove_all_cus ();
ae038cb0
DJ
25138}
25139
dee91e82
DE
25140/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25141 We store these in a hash table separate from the DIEs, and preserve them
25142 when the DIEs are flushed out of cache.
25143
25144 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25145 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25146 or the type may come from a DWO file. Furthermore, while it's more logical
25147 to use per_cu->section+offset, with Fission the section with the data is in
25148 the DWO file but we don't know that section at the point we need it.
25149 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25150 because we can enter the lookup routine, get_die_type_at_offset, from
25151 outside this file, and thus won't necessarily have PER_CU->cu.
25152 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25153
dee91e82 25154struct dwarf2_per_cu_offset_and_type
1c379e20 25155{
dee91e82 25156 const struct dwarf2_per_cu_data *per_cu;
9c541725 25157 sect_offset sect_off;
1c379e20
DJ
25158 struct type *type;
25159};
25160
dee91e82 25161/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25162
25163static hashval_t
dee91e82 25164per_cu_offset_and_type_hash (const void *item)
1c379e20 25165{
9a3c8263
SM
25166 const struct dwarf2_per_cu_offset_and_type *ofs
25167 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25168
9c541725 25169 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25170}
25171
dee91e82 25172/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25173
25174static int
dee91e82 25175per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25176{
9a3c8263
SM
25177 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25178 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25179 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25180 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25181
dee91e82 25182 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25183 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25184}
25185
25186/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25187 table if necessary. For convenience, return TYPE.
25188
25189 The DIEs reading must have careful ordering to:
85102364 25190 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25191 reading current DIE.
25192 * Not trying to dereference contents of still incompletely read in types
25193 while reading in other DIEs.
25194 * Enable referencing still incompletely read in types just by a pointer to
25195 the type without accessing its fields.
25196
25197 Therefore caller should follow these rules:
25198 * Try to fetch any prerequisite types we may need to build this DIE type
25199 before building the type and calling set_die_type.
e71ec853 25200 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25201 possible before fetching more types to complete the current type.
25202 * Make the type as complete as possible before fetching more types. */
1c379e20 25203
f792889a 25204static struct type *
57567375
TT
25205set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25206 bool skip_data_location)
1c379e20 25207{
976ca316 25208 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 25209 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 25210 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
25211 struct attribute *attr;
25212 struct dynamic_prop prop;
1c379e20 25213
b4ba55a1
JB
25214 /* For Ada types, make sure that the gnat-specific data is always
25215 initialized (if not already set). There are a few types where
25216 we should not be doing so, because the type-specific area is
25217 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25218 where the type-specific area is used to store the floatformat).
25219 But this is not a problem, because the gnat-specific information
25220 is actually not needed for these types. */
25221 if (need_gnat_info (cu)
78134374
SM
25222 && type->code () != TYPE_CODE_FUNC
25223 && type->code () != TYPE_CODE_FLT
25224 && type->code () != TYPE_CODE_METHODPTR
25225 && type->code () != TYPE_CODE_MEMBERPTR
25226 && type->code () != TYPE_CODE_METHOD
09584414 25227 && type->code () != TYPE_CODE_FIXED_POINT
b4ba55a1
JB
25228 && !HAVE_GNAT_AUX_INFO (type))
25229 INIT_GNAT_SPECIFIC (type);
25230
3f2f83dd
KB
25231 /* Read DW_AT_allocated and set in type. */
25232 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 25233 if (attr != NULL)
3f2f83dd 25234 {
293e7e51 25235 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25236 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25237 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 25238 }
3f2f83dd
KB
25239
25240 /* Read DW_AT_associated and set in type. */
25241 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 25242 if (attr != NULL)
3f2f83dd 25243 {
293e7e51 25244 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25245 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25246 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 25247 }
3f2f83dd 25248
3cdcd0ce 25249 /* Read DW_AT_data_location and set in type. */
57567375
TT
25250 if (!skip_data_location)
25251 {
25252 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25253 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25254 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25255 }
3cdcd0ce 25256
976ca316
SM
25257 if (per_objfile->die_type_hash == NULL)
25258 per_objfile->die_type_hash
0335378b
TT
25259 = htab_up (htab_create_alloc (127,
25260 per_cu_offset_and_type_hash,
25261 per_cu_offset_and_type_eq,
25262 NULL, xcalloc, xfree));
1c379e20 25263
dee91e82 25264 ofs.per_cu = cu->per_cu;
9c541725 25265 ofs.sect_off = die->sect_off;
1c379e20 25266 ofs.type = type;
dee91e82 25267 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 25268 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 25269 if (*slot)
b98664d3 25270 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25271 sect_offset_str (die->sect_off));
8d749320
SM
25272 *slot = XOBNEW (&objfile->objfile_obstack,
25273 struct dwarf2_per_cu_offset_and_type);
1c379e20 25274 **slot = ofs;
f792889a 25275 return type;
1c379e20
DJ
25276}
25277
9c541725 25278/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25279 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25280
25281static struct type *
9c541725 25282get_die_type_at_offset (sect_offset sect_off,
aa66c379 25283 dwarf2_per_cu_data *per_cu,
976ca316 25284 dwarf2_per_objfile *per_objfile)
1c379e20 25285{
dee91e82 25286 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 25287
976ca316 25288 if (per_objfile->die_type_hash == NULL)
f792889a 25289 return NULL;
1c379e20 25290
dee91e82 25291 ofs.per_cu = per_cu;
9c541725 25292 ofs.sect_off = sect_off;
9a3c8263 25293 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 25294 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
25295 if (slot)
25296 return slot->type;
25297 else
25298 return NULL;
25299}
25300
02142a6c 25301/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25302 or return NULL if DIE does not have a saved type. */
25303
25304static struct type *
25305get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25306{
aa66c379 25307 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
25308}
25309
10b3939b
DJ
25310/* Add a dependence relationship from CU to REF_PER_CU. */
25311
25312static void
25313dwarf2_add_dependence (struct dwarf2_cu *cu,
25314 struct dwarf2_per_cu_data *ref_per_cu)
25315{
25316 void **slot;
25317
25318 if (cu->dependencies == NULL)
25319 cu->dependencies
25320 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25321 NULL, &cu->comp_unit_obstack,
25322 hashtab_obstack_allocate,
25323 dummy_obstack_deallocate);
25324
25325 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25326 if (*slot == NULL)
25327 *slot = ref_per_cu;
25328}
1c379e20 25329
f504f079
DE
25330/* Subroutine of dwarf2_mark to pass to htab_traverse.
25331 Set the mark field in every compilation unit in the
7188ed02
SM
25332 cache that we must keep because we are keeping CU.
25333
25334 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 25335
10b3939b
DJ
25336static int
25337dwarf2_mark_helper (void **slot, void *data)
25338{
7188ed02
SM
25339 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25340 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25341 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
25342
25343 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25344 reading of the chain. As such dependencies remain valid it is not much
25345 useful to track and undo them during QUIT cleanups. */
7188ed02 25346 if (cu == nullptr)
d07ed419
JK
25347 return 1;
25348
7188ed02 25349 if (cu->mark)
10b3939b 25350 return 1;
10b3939b 25351
7188ed02
SM
25352 cu->mark = true;
25353
25354 if (cu->dependencies != nullptr)
25355 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
25356
25357 return 1;
25358}
25359
f504f079
DE
25360/* Set the mark field in CU and in every other compilation unit in the
25361 cache that we must keep because we are keeping CU. */
25362
ae038cb0
DJ
25363static void
25364dwarf2_mark (struct dwarf2_cu *cu)
25365{
25366 if (cu->mark)
25367 return;
7188ed02 25368
9068261f 25369 cu->mark = true;
ae038cb0 25370
7188ed02
SM
25371 if (cu->dependencies != nullptr)
25372 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
25373}
25374
72bf9492
DJ
25375/* Trivial hash function for partial_die_info: the hash value of a DIE
25376 is its offset in .debug_info for this objfile. */
25377
25378static hashval_t
25379partial_die_hash (const void *item)
25380{
9a3c8263
SM
25381 const struct partial_die_info *part_die
25382 = (const struct partial_die_info *) item;
9a619af0 25383
9c541725 25384 return to_underlying (part_die->sect_off);
72bf9492
DJ
25385}
25386
25387/* Trivial comparison function for partial_die_info structures: two DIEs
25388 are equal if they have the same offset. */
25389
25390static int
25391partial_die_eq (const void *item_lhs, const void *item_rhs)
25392{
9a3c8263
SM
25393 const struct partial_die_info *part_die_lhs
25394 = (const struct partial_die_info *) item_lhs;
25395 const struct partial_die_info *part_die_rhs
25396 = (const struct partial_die_info *) item_rhs;
9a619af0 25397
9c541725 25398 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25399}
25400
3c3bb058
AB
25401struct cmd_list_element *set_dwarf_cmdlist;
25402struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 25403
9291a0cd 25404static void
cd4fb1b2
SM
25405show_check_physname (struct ui_file *file, int from_tty,
25406 struct cmd_list_element *c, const char *value)
9291a0cd 25407{
cd4fb1b2
SM
25408 fprintf_filtered (file,
25409 _("Whether to check \"physname\" is %s.\n"),
25410 value);
9291a0cd
TT
25411}
25412
6c265988 25413void _initialize_dwarf2_read ();
cd4fb1b2 25414void
6c265988 25415_initialize_dwarf2_read ()
9291a0cd 25416{
0743fc83 25417 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 25418Set DWARF specific variables.\n\
590042fc 25419Configure DWARF variables such as the cache size."),
0743fc83
TT
25420 &set_dwarf_cmdlist, "maintenance set dwarf ",
25421 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25422
0743fc83 25423 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
25424Show DWARF specific variables.\n\
25425Show DWARF variables such as the cache size."),
0743fc83
TT
25426 &show_dwarf_cmdlist, "maintenance show dwarf ",
25427 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25428
cd4fb1b2
SM
25429 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25430 &dwarf_max_cache_age, _("\
25431Set the upper bound on the age of cached DWARF compilation units."), _("\
25432Show the upper bound on the age of cached DWARF compilation units."), _("\
25433A higher limit means that cached compilation units will be stored\n\
25434in memory longer, and more total memory will be used. Zero disables\n\
25435caching, which can slow down startup."),
25436 NULL,
25437 show_dwarf_max_cache_age,
25438 &set_dwarf_cmdlist,
25439 &show_dwarf_cmdlist);
156942c7 25440
cd4fb1b2
SM
25441 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25442Set debugging of the DWARF reader."), _("\
25443Show debugging of the DWARF reader."), _("\
25444When enabled (non-zero), debugging messages are printed during DWARF\n\
25445reading and symtab expansion. A value of 1 (one) provides basic\n\
25446information. A value greater than 1 provides more verbose information."),
25447 NULL,
25448 NULL,
25449 &setdebuglist, &showdebuglist);
9291a0cd 25450
cd4fb1b2
SM
25451 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25452Set debugging of the DWARF DIE reader."), _("\
25453Show debugging of the DWARF DIE reader."), _("\
25454When enabled (non-zero), DIEs are dumped after they are read in.\n\
25455The value is the maximum depth to print."),
25456 NULL,
25457 NULL,
25458 &setdebuglist, &showdebuglist);
9291a0cd 25459
cd4fb1b2
SM
25460 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25461Set debugging of the dwarf line reader."), _("\
25462Show debugging of the dwarf line reader."), _("\
25463When enabled (non-zero), line number entries are dumped as they are read in.\n\
25464A value of 1 (one) provides basic information.\n\
25465A value greater than 1 provides more verbose information."),
25466 NULL,
25467 NULL,
25468 &setdebuglist, &showdebuglist);
437afbb8 25469
cd4fb1b2
SM
25470 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25471Set cross-checking of \"physname\" code against demangler."), _("\
25472Show cross-checking of \"physname\" code against demangler."), _("\
25473When enabled, GDB's internal \"physname\" code is checked against\n\
25474the demangler."),
25475 NULL, show_check_physname,
25476 &setdebuglist, &showdebuglist);
900e11f9 25477
e615022a
DE
25478 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25479 no_class, &use_deprecated_index_sections, _("\
25480Set whether to use deprecated gdb_index sections."), _("\
25481Show whether to use deprecated gdb_index sections."), _("\
25482When enabled, deprecated .gdb_index sections are used anyway.\n\
25483Normally they are ignored either because of a missing feature or\n\
25484performance issue.\n\
25485Warning: This option must be enabled before gdb reads the file."),
25486 NULL,
25487 NULL,
25488 &setlist, &showlist);
25489
f1e6e072
TT
25490 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25491 &dwarf2_locexpr_funcs);
25492 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25493 &dwarf2_loclist_funcs);
25494
25495 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25496 &dwarf2_block_frame_base_locexpr_funcs);
25497 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25498 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25499
25500#if GDB_SELF_TEST
25501 selftests::register_test ("dw2_expand_symtabs_matching",
25502 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
25503 selftests::register_test ("dwarf2_find_containing_comp_unit",
25504 selftests::find_containing_comp_unit::run_test);
c62446b1 25505#endif
6502dd73 25506}
This page took 6.414676 seconds and 4 git commands to generate.