[gdb/symtab] Handle DW_AT_decl_file with form DW_FORM_implicit_const
[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 {
08ac5771
SM
1687 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1688
1689 m_per_objfile->per_bfd->queue.emplace ();
39856def 1690 }
b303c6f6
AB
1691
1692 /* Free any entries remaining on the queue. There should only be
1693 entries left if we hit an error while processing the dwarf. */
1694 ~dwarf2_queue_guard ()
1695 {
08ac5771
SM
1696 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1697
1698 m_per_objfile->per_bfd->queue.reset ();
39856def 1699 }
b303c6f6 1700
39856def 1701 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1702
39856def
TT
1703private:
1704 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1705};
1706
39856def
TT
1707dwarf2_queue_item::~dwarf2_queue_item ()
1708{
1709 /* Anything still marked queued is likely to be in an
1710 inconsistent state, so discard it. */
1711 if (per_cu->queued)
1712 {
7188ed02 1713 per_objfile->remove_cu (per_cu);
39856def
TT
1714 per_cu->queued = 0;
1715 }
1716}
1717
d721ba37
PA
1718/* The return type of find_file_and_directory. Note, the enclosed
1719 string pointers are only valid while this object is valid. */
1720
1721struct file_and_directory
1722{
1723 /* The filename. This is never NULL. */
1724 const char *name;
1725
1726 /* The compilation directory. NULL if not known. If we needed to
1727 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1728 points directly to the DW_AT_comp_dir string attribute owned by
1729 the obstack that owns the DIE. */
1730 const char *comp_dir;
1731
1732 /* If we needed to build a new string for comp_dir, this is what
1733 owns the storage. */
1734 std::string comp_dir_storage;
1735};
1736
1737static file_and_directory find_file_and_directory (struct die_info *die,
1738 struct dwarf2_cu *cu);
9291a0cd 1739
298e9637 1740static htab_up allocate_signatured_type_table ();
1fd400ff 1741
298e9637 1742static htab_up allocate_dwo_unit_table ();
3019eac3 1743
57d63ce2 1744static struct dwo_unit *lookup_dwo_unit_in_dwp
976ca316
SM
1745 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1746 const char *comp_dir, ULONGEST signature, int is_debug_types);
a2ce51a0 1747
976ca316 1748static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
a2ce51a0 1749
3019eac3 1750static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1751 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1752 ULONGEST signature);
3019eac3
DE
1753
1754static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1755 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1756
1b555f17 1757static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
89e63ee4 1758
263db9a1
TT
1759/* A unique pointer to a dwo_file. */
1760
51ac9db5 1761typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1762
976ca316 1763static void process_cu_includes (dwarf2_per_objfile *per_objfile);
95554aad 1764
1b80a9fa 1765static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1766
1767static void free_line_header_voidp (void *arg);
4390d890
DE
1768\f
1769/* Various complaints about symbol reading that don't abort the process. */
1770
4390d890
DE
1771static void
1772dwarf2_debug_line_missing_file_complaint (void)
1773{
b98664d3 1774 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1775}
1776
1777static void
1778dwarf2_debug_line_missing_end_sequence_complaint (void)
1779{
b98664d3 1780 complaint (_(".debug_line section has line "
4390d890
DE
1781 "program sequence without an end"));
1782}
1783
1784static void
1785dwarf2_complex_location_expr_complaint (void)
1786{
b98664d3 1787 complaint (_("location expression too complex"));
4390d890
DE
1788}
1789
1790static void
1791dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1792 int arg3)
1793{
b98664d3 1794 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1795 arg1, arg2, arg3);
1796}
1797
4390d890
DE
1798static void
1799dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1800{
b98664d3 1801 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1802 arg1, arg2);
1803}
527f3840
JK
1804
1805/* Hash function for line_header_hash. */
1806
1807static hashval_t
1808line_header_hash (const struct line_header *ofs)
1809{
9c541725 1810 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1811}
1812
1813/* Hash function for htab_create_alloc_ex for line_header_hash. */
1814
1815static hashval_t
1816line_header_hash_voidp (const void *item)
1817{
9a3c8263 1818 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1819
1820 return line_header_hash (ofs);
1821}
1822
1823/* Equality function for line_header_hash. */
1824
1825static int
1826line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1827{
9a3c8263
SM
1828 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1829 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1830
9c541725 1831 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1832 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1833}
1834
4390d890 1835\f
9291a0cd 1836
330cdd98
PA
1837/* See declaration. */
1838
5989a64e
SM
1839dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1840 bool can_copy_)
c3699833
SM
1841 : obfd (obfd),
1842 can_copy (can_copy_)
330cdd98
PA
1843{
1844 if (names == NULL)
1845 names = &dwarf2_elf_names;
1846
330cdd98
PA
1847 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1848 locate_sections (obfd, sec, *names);
1849}
1850
5989a64e 1851dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98 1852{
b76e467d 1853 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1854 per_cu->imported_symtabs_free ();
fc8e7e75 1855
b2bdb8cf 1856 for (signatured_type *sig_type : all_type_units)
ae640021 1857 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1858
5989a64e 1859 /* Everything else should be on this->obstack. */
330cdd98
PA
1860}
1861
7188ed02 1862/* See read.h. */
330cdd98
PA
1863
1864void
7188ed02 1865dwarf2_per_objfile::remove_all_cus ()
330cdd98 1866{
08ac5771
SM
1867 gdb_assert (!this->per_bfd->queue.has_value ());
1868
7188ed02
SM
1869 for (auto pair : m_dwarf2_cus)
1870 delete pair.second;
330cdd98 1871
7188ed02 1872 m_dwarf2_cus.clear ();
330cdd98
PA
1873}
1874
11ed8cad
TT
1875/* A helper class that calls free_cached_comp_units on
1876 destruction. */
1877
1878class free_cached_comp_units
1879{
1880public:
1881
1882 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1883 : m_per_objfile (per_objfile)
1884 {
1885 }
1886
1887 ~free_cached_comp_units ()
1888 {
7188ed02 1889 m_per_objfile->remove_all_cus ();
11ed8cad
TT
1890 }
1891
1892 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1893
1894private:
1895
1896 dwarf2_per_objfile *m_per_objfile;
1897};
1898
af758d11
SM
1899/* See read.h. */
1900
1901bool
1902dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1903{
1904 gdb_assert (per_cu->index < this->m_symtabs.size ());
1905
1906 return this->m_symtabs[per_cu->index] != nullptr;
1907}
1908
1909/* See read.h. */
1910
1911compunit_symtab *
1912dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1913{
1914 gdb_assert (per_cu->index < this->m_symtabs.size ());
1915
1916 return this->m_symtabs[per_cu->index];
1917}
1918
1919/* See read.h. */
1920
1921void
1922dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1923 compunit_symtab *symtab)
1924{
1925 gdb_assert (per_cu->index < this->m_symtabs.size ());
1926 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1927
1928 this->m_symtabs[per_cu->index] = symtab;
1929}
1930
c906108c 1931/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1932 information and return true if we have enough to do something.
1933 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1934 ELF names are used. CAN_COPY is true for formats where symbol
1935 interposition is possible and so symbol values must follow copy
1936 relocation rules. */
c906108c
SS
1937
1938int
251d32d9 1939dwarf2_has_info (struct objfile *objfile,
dda83cd7 1940 const struct dwarf2_debug_sections *names,
4b610737 1941 bool can_copy)
c906108c 1942{
97cbe998
SDJ
1943 if (objfile->flags & OBJF_READNEVER)
1944 return 0;
1945
976ca316 1946 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 1947
976ca316 1948 if (per_objfile == NULL)
5989a64e 1949 {
17ee85fc
TT
1950 dwarf2_per_bfd *per_bfd;
1951
1952 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
dda83cd7 1953 doesn't require relocations and if there aren't partial symbols
17ee85fc
TT
1954 from some other reader. */
1955 if (!objfile_has_partial_symbols (objfile)
1956 && !gdb_bfd_requires_relocations (objfile->obfd))
1957 {
1958 /* See if one has been created for this BFD yet. */
1959 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1960
1961 if (per_bfd == nullptr)
1962 {
1963 /* No, create it now. */
1964 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1965 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1966 }
1967 }
1968 else
1969 {
1970 /* No sharing possible, create one specifically for this objfile. */
1971 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1972 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1973 }
5989a64e 1974
976ca316 1975 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 1976 }
5bfd760d 1977
976ca316
SM
1978 return (!per_objfile->per_bfd->info.is_virtual
1979 && per_objfile->per_bfd->info.s.section != NULL
1980 && !per_objfile->per_bfd->abbrev.is_virtual
1981 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1982}
1983
251d32d9
TG
1984/* When loading sections, we look either for uncompressed section or for
1985 compressed section names. */
233a11ab
CS
1986
1987static int
251d32d9 1988section_is_p (const char *section_name,
dda83cd7 1989 const struct dwarf2_section_names *names)
233a11ab 1990{
251d32d9
TG
1991 if (names->normal != NULL
1992 && strcmp (section_name, names->normal) == 0)
1993 return 1;
1994 if (names->compressed != NULL
1995 && strcmp (section_name, names->compressed) == 0)
1996 return 1;
1997 return 0;
233a11ab
CS
1998}
1999
330cdd98 2000/* See declaration. */
c906108c 2001
330cdd98 2002void
5989a64e
SM
2003dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
2004 const dwarf2_debug_sections &names)
c906108c 2005{
fd361982 2006 flagword aflag = bfd_section_flags (sectp);
251d32d9 2007
dc7650b8
JK
2008 if ((aflag & SEC_HAS_CONTENTS) == 0)
2009 {
2010 }
950b7495
KS
2011 else if (elf_section_data (sectp)->this_hdr.sh_size
2012 > bfd_get_file_size (abfd))
2013 {
2014 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2015 warning (_("Discarding section %s which has a section size (%s"
2016 ") larger than the file size [in module %s]"),
2017 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2018 bfd_get_filename (abfd));
2019 }
330cdd98 2020 else if (section_is_p (sectp->name, &names.info))
c906108c 2021 {
330cdd98 2022 this->info.s.section = sectp;
fd361982 2023 this->info.size = bfd_section_size (sectp);
c906108c 2024 }
330cdd98 2025 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2026 {
330cdd98 2027 this->abbrev.s.section = sectp;
fd361982 2028 this->abbrev.size = bfd_section_size (sectp);
c906108c 2029 }
330cdd98 2030 else if (section_is_p (sectp->name, &names.line))
c906108c 2031 {
330cdd98 2032 this->line.s.section = sectp;
fd361982 2033 this->line.size = bfd_section_size (sectp);
c906108c 2034 }
330cdd98 2035 else if (section_is_p (sectp->name, &names.loc))
c906108c 2036 {
330cdd98 2037 this->loc.s.section = sectp;
fd361982 2038 this->loc.size = bfd_section_size (sectp);
c906108c 2039 }
330cdd98 2040 else if (section_is_p (sectp->name, &names.loclists))
43988095 2041 {
330cdd98 2042 this->loclists.s.section = sectp;
fd361982 2043 this->loclists.size = bfd_section_size (sectp);
43988095 2044 }
330cdd98 2045 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2046 {
330cdd98 2047 this->macinfo.s.section = sectp;
fd361982 2048 this->macinfo.size = bfd_section_size (sectp);
c906108c 2049 }
330cdd98 2050 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2051 {
330cdd98 2052 this->macro.s.section = sectp;
fd361982 2053 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2054 }
330cdd98 2055 else if (section_is_p (sectp->name, &names.str))
c906108c 2056 {
330cdd98 2057 this->str.s.section = sectp;
fd361982 2058 this->str.size = bfd_section_size (sectp);
c906108c 2059 }
18a8505e
AT
2060 else if (section_is_p (sectp->name, &names.str_offsets))
2061 {
2062 this->str_offsets.s.section = sectp;
2063 this->str_offsets.size = bfd_section_size (sectp);
2064 }
330cdd98 2065 else if (section_is_p (sectp->name, &names.line_str))
43988095 2066 {
330cdd98 2067 this->line_str.s.section = sectp;
fd361982 2068 this->line_str.size = bfd_section_size (sectp);
43988095 2069 }
330cdd98 2070 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2071 {
330cdd98 2072 this->addr.s.section = sectp;
fd361982 2073 this->addr.size = bfd_section_size (sectp);
3019eac3 2074 }
330cdd98 2075 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2076 {
330cdd98 2077 this->frame.s.section = sectp;
fd361982 2078 this->frame.size = bfd_section_size (sectp);
b6af0555 2079 }
330cdd98 2080 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2081 {
330cdd98 2082 this->eh_frame.s.section = sectp;
fd361982 2083 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2084 }
330cdd98 2085 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2086 {
330cdd98 2087 this->ranges.s.section = sectp;
fd361982 2088 this->ranges.size = bfd_section_size (sectp);
af34e669 2089 }
330cdd98 2090 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2091 {
330cdd98 2092 this->rnglists.s.section = sectp;
fd361982 2093 this->rnglists.size = bfd_section_size (sectp);
43988095 2094 }
330cdd98 2095 else if (section_is_p (sectp->name, &names.types))
348e048f 2096 {
8b70b953
TT
2097 struct dwarf2_section_info type_section;
2098
2099 memset (&type_section, 0, sizeof (type_section));
049412e3 2100 type_section.s.section = sectp;
fd361982 2101 type_section.size = bfd_section_size (sectp);
8b70b953 2102
fd5866f6 2103 this->types.push_back (type_section);
348e048f 2104 }
330cdd98 2105 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2106 {
330cdd98 2107 this->gdb_index.s.section = sectp;
fd361982 2108 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2109 }
927aa2e7
JK
2110 else if (section_is_p (sectp->name, &names.debug_names))
2111 {
2112 this->debug_names.s.section = sectp;
fd361982 2113 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2114 }
2115 else if (section_is_p (sectp->name, &names.debug_aranges))
2116 {
2117 this->debug_aranges.s.section = sectp;
fd361982 2118 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2119 }
dce234bc 2120
fd361982
AM
2121 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2122 && bfd_section_vma (sectp) == 0)
330cdd98 2123 this->has_section_at_zero = true;
c906108c
SS
2124}
2125
dce234bc 2126/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2127 SECTION_NAME. */
af34e669 2128
dce234bc 2129void
3017a003 2130dwarf2_get_section_info (struct objfile *objfile,
dda83cd7
SM
2131 enum dwarf2_section_enum sect,
2132 asection **sectp, const gdb_byte **bufp,
2133 bfd_size_type *sizep)
dce234bc 2134{
976ca316 2135 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 2136 struct dwarf2_section_info *info;
a3b2a86b
TT
2137
2138 /* We may see an objfile without any DWARF, in which case we just
2139 return nothing. */
976ca316 2140 if (per_objfile == NULL)
a3b2a86b
TT
2141 {
2142 *sectp = NULL;
2143 *bufp = NULL;
2144 *sizep = 0;
2145 return;
2146 }
3017a003
TG
2147 switch (sect)
2148 {
2149 case DWARF2_DEBUG_FRAME:
976ca316 2150 info = &per_objfile->per_bfd->frame;
3017a003
TG
2151 break;
2152 case DWARF2_EH_FRAME:
976ca316 2153 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
2154 break;
2155 default:
2156 gdb_assert_not_reached ("unexpected section");
2157 }
dce234bc 2158
96b79293 2159 info->read (objfile);
dce234bc 2160
96b79293 2161 *sectp = info->get_bfd_section ();
dce234bc
PP
2162 *bufp = info->buffer;
2163 *sizep = info->size;
2164}
2165
36586728
TT
2166/* A helper function to find the sections for a .dwz file. */
2167
2168static void
5bb6e9dd 2169locate_dwz_sections (bfd *abfd, asection *sectp, dwz_file *dwz_file)
36586728 2170{
36586728
TT
2171 /* Note that we only support the standard ELF names, because .dwz
2172 is ELF-only (at the time of writing). */
2173 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2174 {
049412e3 2175 dwz_file->abbrev.s.section = sectp;
fd361982 2176 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2177 }
2178 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2179 {
049412e3 2180 dwz_file->info.s.section = sectp;
fd361982 2181 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2182 }
2183 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2184 {
049412e3 2185 dwz_file->str.s.section = sectp;
fd361982 2186 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2187 }
2188 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2189 {
049412e3 2190 dwz_file->line.s.section = sectp;
fd361982 2191 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2192 }
2193 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2194 {
049412e3 2195 dwz_file->macro.s.section = sectp;
fd361982 2196 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2197 }
2ec9a5e0
TT
2198 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2199 {
049412e3 2200 dwz_file->gdb_index.s.section = sectp;
fd361982 2201 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2202 }
927aa2e7
JK
2203 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2204 {
2205 dwz_file->debug_names.s.section = sectp;
fd361982 2206 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2207 }
36586728
TT
2208}
2209
2bf3b79d
SDJ
2210/* Attempt to find a .dwz file (whose full path is represented by
2211 FILENAME) in all of the specified debug file directories provided.
2212
2213 Return the equivalent gdb_bfd_ref_ptr of the .dwz file found, or
2214 nullptr if it could not find anything. */
2215
2216static gdb_bfd_ref_ptr
2217dwz_search_other_debugdirs (std::string &filename, bfd_byte *buildid,
2218 size_t buildid_len)
2219{
2220 /* Let's assume that the path represented by FILENAME has the
2221 "/.dwz/" subpath in it. This is what (most) GNU/Linux
2222 distributions do, anyway. */
2223 size_t dwz_pos = filename.find ("/.dwz/");
2224
2225 if (dwz_pos == std::string::npos)
2226 return nullptr;
2227
2228 /* This is an obvious assertion, but it's here more to educate
2229 future readers of this code that FILENAME at DWZ_POS *must*
2230 contain a directory separator. */
2231 gdb_assert (IS_DIR_SEPARATOR (filename[dwz_pos]));
2232
2233 gdb_bfd_ref_ptr dwz_bfd;
2234 std::vector<gdb::unique_xmalloc_ptr<char>> debugdir_vec
2235 = dirnames_to_char_ptr_vec (debug_file_directory);
2236
2237 for (const gdb::unique_xmalloc_ptr<char> &debugdir : debugdir_vec)
2238 {
2239 /* The idea is to iterate over the
2240 debug file directories provided by the user and
2241 replace the hard-coded path in the "filename" by each
2242 debug-file-directory.
2243
2244 For example, suppose that filename is:
2245
2246 /usr/lib/debug/.dwz/foo.dwz
2247
2248 And suppose that we have "$HOME/bar" as the
2249 debug-file-directory. We would then adjust filename
2250 to look like:
2251
2252 $HOME/bar/.dwz/foo.dwz
2253
2254 which would hopefully allow us to find the alt debug
2255 file. */
2256 std::string ddir = debugdir.get ();
2257
2258 if (ddir.empty ())
2259 continue;
2260
2261 /* Make sure the current debug-file-directory ends with a
2262 directory separator. This is needed because, if FILENAME
2263 contains something like "/usr/lib/abcde/.dwz/foo.dwz" and
2264 DDIR is "/usr/lib/abc", then could wrongfully skip it
2265 below. */
2266 if (!IS_DIR_SEPARATOR (ddir.back ()))
2267 ddir += SLASH_STRING;
2268
2269 /* Check whether the beginning of FILENAME is DDIR. If it is,
2270 then we are dealing with a file which we already attempted to
2271 open before, so we just skip it and continue processing the
2272 remaining debug file directories. */
2273 if (filename.size () > ddir.size ()
2274 && filename.compare (0, ddir.size (), ddir) == 0)
2275 continue;
2276
2277 /* Replace FILENAME's default debug-file-directory with
2278 DDIR. */
2279 std::string new_filename = ddir + &filename[dwz_pos + 1];
2280
2281 dwz_bfd = gdb_bfd_open (new_filename.c_str (), gnutarget);
2282
2283 if (dwz_bfd == nullptr)
2284 continue;
2285
2286 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2287 {
2288 dwz_bfd.reset (nullptr);
2289 continue;
2290 }
2291
2292 /* Found it. */
2293 break;
2294 }
2295
2296 return dwz_bfd;
2297}
2298
c4973306 2299/* See dwarf2read.h. */
36586728 2300
c4973306 2301struct dwz_file *
c3699833 2302dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
36586728 2303{
acd13123 2304 bfd_size_type buildid_len_arg;
dc294be5
TT
2305 size_t buildid_len;
2306 bfd_byte *buildid;
36586728 2307
c3699833
SM
2308 if (per_bfd->dwz_file != NULL)
2309 return per_bfd->dwz_file.get ();
36586728 2310
4db1a1dc 2311 bfd_set_error (bfd_error_no_error);
791afaa2 2312 gdb::unique_xmalloc_ptr<char> data
c3699833 2313 (bfd_get_alt_debug_link_info (per_bfd->obfd,
791afaa2 2314 &buildid_len_arg, &buildid));
4db1a1dc
TT
2315 if (data == NULL)
2316 {
2317 if (bfd_get_error () == bfd_error_no_error)
2318 return NULL;
2319 error (_("could not read '.gnu_debugaltlink' section: %s"),
2320 bfd_errmsg (bfd_get_error ()));
2321 }
791afaa2
TT
2322
2323 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2324
acd13123
TT
2325 buildid_len = (size_t) buildid_len_arg;
2326
2bf3b79d 2327 std::string filename = data.get ();
d721ba37 2328
2bf3b79d 2329 if (!IS_ABSOLUTE_PATH (filename.c_str ()))
36586728 2330 {
14278e1f 2331 gdb::unique_xmalloc_ptr<char> abs
c3699833 2332 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
36586728 2333
2bf3b79d 2334 filename = ldirname (abs.get ()) + SLASH_STRING + filename;
36586728
TT
2335 }
2336
dc294be5
TT
2337 /* First try the file name given in the section. If that doesn't
2338 work, try to use the build-id instead. */
2bf3b79d 2339 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename.c_str (), gnutarget));
dc294be5 2340 if (dwz_bfd != NULL)
36586728 2341 {
192b62ce 2342 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2343 dwz_bfd.reset (nullptr);
36586728
TT
2344 }
2345
dc294be5
TT
2346 if (dwz_bfd == NULL)
2347 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2348
2bf3b79d
SDJ
2349 if (dwz_bfd == nullptr)
2350 {
2351 /* If the user has provided us with different
2352 debug file directories, we can try them in order. */
2353 dwz_bfd = dwz_search_other_debugdirs (filename, buildid, buildid_len);
2354 }
2355
0d79cdc4
AM
2356 if (dwz_bfd == nullptr)
2357 {
2358 gdb::unique_xmalloc_ptr<char> alt_filename;
c3699833 2359 const char *origname = bfd_get_filename (per_bfd->obfd);
0d79cdc4
AM
2360
2361 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2362 buildid_len,
2363 origname,
2364 &alt_filename));
2365
2366 if (fd.get () >= 0)
2367 {
2368 /* File successfully retrieved from server. */
ad80db5b 2369 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2370
2371 if (dwz_bfd == nullptr)
2372 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2373 alt_filename.get ());
2374 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2375 dwz_bfd.reset (nullptr);
2376 }
2377 }
2378
dc294be5
TT
2379 if (dwz_bfd == NULL)
2380 error (_("could not find '.gnu_debugaltlink' file for %s"),
c3699833 2381 bfd_get_filename (per_bfd->obfd));
dc294be5 2382
7ff8cb8c
TT
2383 std::unique_ptr<struct dwz_file> result
2384 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2385
5bb6e9dd
TT
2386 for (asection *sec : gdb_bfd_sections (result->dwz_bfd))
2387 locate_dwz_sections (result->dwz_bfd.get (), sec, result.get ());
36586728 2388
c3699833
SM
2389 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2390 per_bfd->dwz_file = std::move (result);
2391 return per_bfd->dwz_file.get ();
36586728 2392}
9291a0cd 2393\f
7b9f3c50
DE
2394/* DWARF quick_symbols_functions support. */
2395
2396/* TUs can share .debug_line entries, and there can be a lot more TUs than
2397 unique line tables, so we maintain a separate table of all .debug_line
2398 derived entries to support the sharing.
2399 All the quick functions need is the list of file names. We discard the
2400 line_header when we're done and don't need to record it here. */
2401struct quick_file_names
2402{
094b34ac
DE
2403 /* The data used to construct the hash key. */
2404 struct stmt_list_hash hash;
7b9f3c50
DE
2405
2406 /* The number of entries in file_names, real_names. */
2407 unsigned int num_file_names;
2408
2409 /* The file names from the line table, after being run through
2410 file_full_name. */
2411 const char **file_names;
2412
2413 /* The file names from the line table after being run through
2414 gdb_realpath. These are computed lazily. */
2415 const char **real_names;
2416};
2417
2418/* When using the index (and thus not using psymtabs), each CU has an
2419 object of this type. This is used to hold information needed by
2420 the various "quick" methods. */
2421struct dwarf2_per_cu_quick_data
2422{
2423 /* The file table. This can be NULL if there was no file table
2424 or it's currently not read in.
5989a64e 2425 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2426 struct quick_file_names *file_names;
2427
7b9f3c50
DE
2428 /* A temporary mark bit used when iterating over all CUs in
2429 expand_symtabs_matching. */
2430 unsigned int mark : 1;
2431
2432 /* True if we've tried to read the file table and found there isn't one.
2433 There will be no point in trying to read it again next time. */
2434 unsigned int no_file_data : 1;
2435};
2436
094b34ac
DE
2437/* Utility hash function for a stmt_list_hash. */
2438
2439static hashval_t
2440hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2441{
2442 hashval_t v = 0;
2443
2444 if (stmt_list_hash->dwo_unit != NULL)
2445 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2446 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2447 return v;
2448}
2449
2450/* Utility equality function for a stmt_list_hash. */
2451
2452static int
2453eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2454 const struct stmt_list_hash *rhs)
2455{
2456 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2457 return 0;
2458 if (lhs->dwo_unit != NULL
2459 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2460 return 0;
2461
9c541725 2462 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2463}
2464
7b9f3c50
DE
2465/* Hash function for a quick_file_names. */
2466
2467static hashval_t
2468hash_file_name_entry (const void *e)
2469{
9a3c8263
SM
2470 const struct quick_file_names *file_data
2471 = (const struct quick_file_names *) e;
7b9f3c50 2472
094b34ac 2473 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2474}
2475
2476/* Equality function for a quick_file_names. */
2477
2478static int
2479eq_file_name_entry (const void *a, const void *b)
2480{
9a3c8263
SM
2481 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2482 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2483
094b34ac 2484 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2485}
2486
2487/* Delete function for a quick_file_names. */
2488
2489static void
2490delete_file_name_entry (void *e)
2491{
9a3c8263 2492 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2493 int i;
2494
2495 for (i = 0; i < file_data->num_file_names; ++i)
2496 {
2497 xfree ((void*) file_data->file_names[i]);
2498 if (file_data->real_names)
2499 xfree ((void*) file_data->real_names[i]);
2500 }
2501
45940949
TT
2502 /* The space for the struct itself lives on the obstack, so we don't
2503 free it here. */
7b9f3c50
DE
2504}
2505
2506/* Create a quick_file_names hash table. */
2507
5895093f 2508static htab_up
7b9f3c50
DE
2509create_quick_file_names_table (unsigned int nr_initial_entries)
2510{
5895093f
TT
2511 return htab_up (htab_create_alloc (nr_initial_entries,
2512 hash_file_name_entry, eq_file_name_entry,
2513 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2514}
9291a0cd 2515
ab432490
SM
2516/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2517 function is unrelated to symtabs, symtab would have to be created afterwards.
2518 You should call age_cached_comp_units after processing the CU. */
918dd910 2519
1b555f17 2520static dwarf2_cu *
ab432490
SM
2521load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2522 bool skip_partial)
918dd910 2523{
3019eac3 2524 if (per_cu->is_debug_types)
ab432490 2525 load_full_type_unit (per_cu, per_objfile);
918dd910 2526 else
4a636814
SM
2527 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2528 skip_partial, language_minimal);
918dd910 2529
7188ed02
SM
2530 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2531 if (cu == nullptr)
1b555f17 2532 return nullptr; /* Dummy CU. */
2dc860c0 2533
7188ed02 2534 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2535
7188ed02 2536 return cu;
918dd910
JK
2537}
2538
1350c3b4 2539/* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2fdf6df6 2540
9291a0cd 2541static void
97a1449a 2542dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2543 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2544{
f4dc4d17
DE
2545 /* Skip type_unit_groups, reading the type units they contain
2546 is handled elsewhere. */
197400e8 2547 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2548 return;
2549
08ac5771
SM
2550 {
2551 /* The destructor of dwarf2_queue_guard frees any entries left on
2552 the queue. After this point we're guaranteed to leave this function
2553 with the dwarf queue empty. */
2554 dwarf2_queue_guard q_guard (per_objfile);
89e63ee4 2555
08ac5771
SM
2556 if (!per_objfile->symtab_set_p (per_cu))
2557 {
2558 queue_comp_unit (per_cu, per_objfile, language_minimal);
2559 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2560
2561 /* If we just loaded a CU from a DWO, and we're working with an index
2562 that may badly handle TUs, load all the TUs in that DWO as well.
2563 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2564 if (!per_cu->is_debug_types
2565 && cu != NULL
2566 && cu->dwo_unit != NULL
2567 && per_objfile->per_bfd->index_table != NULL
2568 && per_objfile->per_bfd->index_table->version <= 7
2569 /* DWP files aren't supported yet. */
2570 && get_dwp_file (per_objfile) == NULL)
2571 queue_and_load_all_dwo_tus (cu);
2572 }
9291a0cd 2573
08ac5771
SM
2574 process_queue (per_objfile);
2575 }
9291a0cd
TT
2576
2577 /* Age the cache, releasing compilation units that have not
2578 been used recently. */
976ca316 2579 per_objfile->age_comp_units ();
9291a0cd
TT
2580}
2581
97a1449a
SM
2582/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2583 the per-objfile for which this symtab is instantiated.
2584
2585 Returns the resulting symbol table. */
2fdf6df6 2586
43f3e411 2587static struct compunit_symtab *
97a1449a 2588dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2589 dwarf2_per_objfile *per_objfile,
97a1449a 2590 bool skip_partial)
9291a0cd 2591{
976ca316 2592 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2593
976ca316 2594 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2595 {
976ca316 2596 free_cached_comp_units freer (per_objfile);
c83dd867 2597 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2598 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2599 process_cu_includes (per_objfile);
9291a0cd 2600 }
f194fefb 2601
976ca316 2602 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2603}
2604
ff4c9fec 2605/* See declaration. */
f4dc4d17 2606
ff4c9fec 2607dwarf2_per_cu_data *
5989a64e 2608dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2609{
b76e467d 2610 if (index >= this->all_comp_units.size ())
ff4c9fec 2611 {
b76e467d 2612 index -= this->all_comp_units.size ();
b2bdb8cf 2613 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2614 return &this->all_type_units[index]->per_cu;
2615 }
f4dc4d17 2616
ff4c9fec
SM
2617 return this->all_comp_units[index];
2618}
f4dc4d17 2619
ff4c9fec 2620/* See declaration. */
2fdf6df6 2621
ff4c9fec 2622dwarf2_per_cu_data *
5989a64e 2623dwarf2_per_bfd::get_cu (int index)
1fd400ff 2624{
b76e467d 2625 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2626
ff4c9fec 2627 return this->all_comp_units[index];
f4dc4d17
DE
2628}
2629
ff4c9fec 2630/* See declaration. */
f4dc4d17 2631
ff4c9fec 2632signatured_type *
5989a64e 2633dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2634{
b2bdb8cf 2635 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2636
ff4c9fec 2637 return this->all_type_units[index];
1fd400ff
TT
2638}
2639
d3473f0c
TT
2640/* See read.h. */
2641
2642dwarf2_per_cu_data *
5989a64e 2643dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2644{
2645 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2646 result->per_bfd = this;
d3473f0c
TT
2647 result->index = m_num_psymtabs++;
2648 return result;
2649}
2650
2651/* See read.h. */
2652
2653signatured_type *
5989a64e 2654dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2655{
2656 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2657 result->per_cu.per_bfd = this;
d3473f0c
TT
2658 result->per_cu.index = m_num_psymtabs++;
2659 return result;
2660}
2661
168c9250 2662/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2663 obstack, and constructed with the specified field values. */
4b514bc8
JK
2664
2665static dwarf2_per_cu_data *
168c9250
SM
2666create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2667 struct dwarf2_section_info *section,
2668 int is_dwz,
2669 sect_offset sect_off, ULONGEST length)
4b514bc8 2670{
168c9250 2671 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2672 the_cu->sect_off = sect_off;
2673 the_cu->length = length;
4b514bc8 2674 the_cu->section = section;
168c9250 2675 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2676 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2677 the_cu->is_dwz = is_dwz;
2678 return the_cu;
2679}
2680
2ec9a5e0
TT
2681/* A helper for create_cus_from_index that handles a given list of
2682 CUs. */
2fdf6df6 2683
74a0d9f6 2684static void
168c9250 2685create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2686 const gdb_byte *cu_list, offset_type n_elements,
2687 struct dwarf2_section_info *section,
b76e467d 2688 int is_dwz)
9291a0cd 2689{
12359b5e 2690 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2691 {
74a0d9f6 2692 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2693
2694 sect_offset sect_off
2695 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2696 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2697 cu_list += 2 * 8;
2698
b76e467d 2699 dwarf2_per_cu_data *per_cu
168c9250
SM
2700 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2701 length);
2702 per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2703 }
9291a0cd
TT
2704}
2705
2ec9a5e0 2706/* Read the CU list from the mapped index, and use it to create all
168c9250 2707 the CU objects for PER_BFD. */
2ec9a5e0 2708
74a0d9f6 2709static void
168c9250 2710create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2711 const gdb_byte *cu_list, offset_type cu_list_elements,
2712 const gdb_byte *dwz_list, offset_type dwz_elements)
2713{
168c9250
SM
2714 gdb_assert (per_bfd->all_comp_units.empty ());
2715 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2716
168c9250
SM
2717 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2718 &per_bfd->info, 0);
2ec9a5e0
TT
2719
2720 if (dwz_elements == 0)
74a0d9f6 2721 return;
2ec9a5e0 2722
168c9250
SM
2723 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2724 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2725 &dwz->info, 1);
2ec9a5e0
TT
2726}
2727
1fd400ff 2728/* Create the signatured type hash table from the index. */
673bfd45 2729
74a0d9f6 2730static void
12359b5e 2731create_signatured_type_table_from_index
168c9250
SM
2732 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2733 const gdb_byte *bytes, offset_type elements)
1fd400ff 2734{
168c9250
SM
2735 gdb_assert (per_bfd->all_type_units.empty ());
2736 per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2737
298e9637 2738 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2739
12359b5e 2740 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2741 {
52dc124a 2742 struct signatured_type *sig_type;
9c541725 2743 ULONGEST signature;
1fd400ff 2744 void **slot;
9c541725 2745 cu_offset type_offset_in_tu;
1fd400ff 2746
74a0d9f6 2747 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2748 sect_offset sect_off
2749 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2750 type_offset_in_tu
2751 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2752 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2753 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2754 bytes += 3 * 8;
2755
168c9250 2756 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2757 sig_type->signature = signature;
9c541725 2758 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2759 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2760 sig_type->per_cu.section = section;
9c541725 2761 sig_type->per_cu.sect_off = sect_off;
52dc124a 2762 sig_type->per_cu.v.quick
168c9250 2763 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2764 struct dwarf2_per_cu_quick_data);
2765
b0b6a987 2766 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2767 *slot = sig_type;
1fd400ff 2768
168c9250 2769 per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2770 }
2771
168c9250 2772 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2773}
2774
927aa2e7
JK
2775/* Create the signatured type hash table from .debug_names. */
2776
2777static void
2778create_signatured_type_table_from_debug_names
976ca316 2779 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2780 const mapped_debug_names &map,
2781 struct dwarf2_section_info *section,
2782 struct dwarf2_section_info *abbrev_section)
2783{
976ca316 2784 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2785
96b79293
TT
2786 section->read (objfile);
2787 abbrev_section->read (objfile);
927aa2e7 2788
976ca316
SM
2789 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2790 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2791
298e9637 2792 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2793
2794 for (uint32_t i = 0; i < map.tu_count; ++i)
2795 {
2796 struct signatured_type *sig_type;
927aa2e7 2797 void **slot;
927aa2e7
JK
2798
2799 sect_offset sect_off
2800 = (sect_offset) (extract_unsigned_integer
2801 (map.tu_table_reordered + i * map.offset_size,
2802 map.offset_size,
2803 map.dwarf5_byte_order));
2804
2805 comp_unit_head cu_header;
976ca316 2806 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2807 abbrev_section,
927aa2e7
JK
2808 section->buffer + to_underlying (sect_off),
2809 rcuh_kind::TYPE);
2810
976ca316 2811 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2812 sig_type->signature = cu_header.signature;
2813 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2814 sig_type->per_cu.is_debug_types = 1;
2815 sig_type->per_cu.section = section;
2816 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2817 sig_type->per_cu.v.quick
976ca316 2818 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2819 struct dwarf2_per_cu_quick_data);
2820
b0b6a987 2821 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2822 *slot = sig_type;
2823
976ca316 2824 per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2825 }
2826
976ca316 2827 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2828}
2829
9291a0cd
TT
2830/* Read the address map data from the mapped index, and use it to
2831 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2832
9291a0cd 2833static void
976ca316 2834create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2835 struct mapped_index *index)
9291a0cd 2836{
976ca316 2837 struct objfile *objfile = per_objfile->objfile;
08feed99 2838 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2839 const gdb_byte *iter, *end;
9291a0cd 2840 struct addrmap *mutable_map;
9291a0cd
TT
2841 CORE_ADDR baseaddr;
2842
8268c778
PA
2843 auto_obstack temp_obstack;
2844
9291a0cd
TT
2845 mutable_map = addrmap_create_mutable (&temp_obstack);
2846
f00a2de2
PA
2847 iter = index->address_table.data ();
2848 end = iter + index->address_table.size ();
9291a0cd 2849
b3b3bada 2850 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2851
2852 while (iter < end)
2853 {
2854 ULONGEST hi, lo, cu_index;
2855 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2856 iter += 8;
2857 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2858 iter += 8;
2859 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2860 iter += 4;
f652bce2 2861
24a55014 2862 if (lo > hi)
f652bce2 2863 {
b98664d3 2864 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2865 hex_string (lo), hex_string (hi));
24a55014 2866 continue;
f652bce2 2867 }
24a55014 2868
976ca316 2869 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2870 {
b98664d3 2871 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2872 (unsigned) cu_index);
24a55014 2873 continue;
f652bce2 2874 }
24a55014 2875
79748972
TT
2876 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2877 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2878 addrmap_set_empty (mutable_map, lo, hi - 1,
976ca316 2879 per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2880 }
2881
d320c2b5 2882 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2883 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2884}
2885
927aa2e7
JK
2886/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2887 populate the objfile's psymtabs_addrmap. */
2888
2889static void
976ca316 2890create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2891 struct dwarf2_section_info *section)
2892{
976ca316 2893 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2894 bfd *abfd = objfile->obfd;
08feed99 2895 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2896 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2897
2898 auto_obstack temp_obstack;
2899 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2900
2901 std::unordered_map<sect_offset,
2902 dwarf2_per_cu_data *,
2903 gdb::hash_enum<sect_offset>>
2904 debug_info_offset_to_per_cu;
976ca316 2905 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 2906 {
927aa2e7
JK
2907 const auto insertpair
2908 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2909 if (!insertpair.second)
2910 {
2911 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2912 "debug_info_offset %s, ignoring .debug_aranges."),
2913 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2914 return;
2915 }
2916 }
2917
96b79293 2918 section->read (objfile);
927aa2e7
JK
2919
2920 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2921
2922 const gdb_byte *addr = section->buffer;
2923
2924 while (addr < section->buffer + section->size)
2925 {
2926 const gdb_byte *const entry_addr = addr;
2927 unsigned int bytes_read;
2928
2929 const LONGEST entry_length = read_initial_length (abfd, addr,
2930 &bytes_read);
2931 addr += bytes_read;
2932
2933 const gdb_byte *const entry_end = addr + entry_length;
2934 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2935 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2936 if (addr + entry_length > section->buffer + section->size)
2937 {
47e3f474 2938 warning (_("Section .debug_aranges in %s entry at offset %s "
dda83cd7 2939 "length %s exceeds section length %s, "
927aa2e7 2940 "ignoring .debug_aranges."),
47e3f474
TV
2941 objfile_name (objfile),
2942 plongest (entry_addr - section->buffer),
927aa2e7
JK
2943 plongest (bytes_read + entry_length),
2944 pulongest (section->size));
2945 return;
2946 }
2947
2948 /* The version number. */
2949 const uint16_t version = read_2_bytes (abfd, addr);
2950 addr += 2;
2951 if (version != 2)
2952 {
47e3f474 2953 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2954 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2955 objfile_name (objfile),
2956 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2957 return;
2958 }
2959
2960 const uint64_t debug_info_offset
2961 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2962 addr += offset_size;
2963 const auto per_cu_it
2964 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2965 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2966 {
47e3f474 2967 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2968 "debug_info_offset %s does not exists, "
2969 "ignoring .debug_aranges."),
47e3f474
TV
2970 objfile_name (objfile),
2971 plongest (entry_addr - section->buffer),
927aa2e7
JK
2972 pulongest (debug_info_offset));
2973 return;
2974 }
2975 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2976
2977 const uint8_t address_size = *addr++;
2978 if (address_size < 1 || address_size > 8)
2979 {
47e3f474 2980 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2981 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2982 objfile_name (objfile),
2983 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2984 return;
2985 }
2986
2987 const uint8_t segment_selector_size = *addr++;
2988 if (segment_selector_size != 0)
2989 {
47e3f474 2990 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2991 "segment_selector_size %u is not supported, "
2992 "ignoring .debug_aranges."),
47e3f474
TV
2993 objfile_name (objfile),
2994 plongest (entry_addr - section->buffer),
927aa2e7
JK
2995 segment_selector_size);
2996 return;
2997 }
2998
2999 /* Must pad to an alignment boundary that is twice the address
dda83cd7
SM
3000 size. It is undocumented by the DWARF standard but GCC does
3001 use it. */
927aa2e7
JK
3002 for (size_t padding = ((-(addr - section->buffer))
3003 & (2 * address_size - 1));
dda83cd7 3004 padding > 0; padding--)
927aa2e7
JK
3005 if (*addr++ != 0)
3006 {
47e3f474 3007 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3008 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
3009 objfile_name (objfile),
3010 plongest (entry_addr - section->buffer));
927aa2e7
JK
3011 return;
3012 }
3013
3014 for (;;)
3015 {
3016 if (addr + 2 * address_size > entry_end)
3017 {
47e3f474 3018 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3019 "address list is not properly terminated, "
3020 "ignoring .debug_aranges."),
47e3f474
TV
3021 objfile_name (objfile),
3022 plongest (entry_addr - section->buffer));
927aa2e7
JK
3023 return;
3024 }
3025 ULONGEST start = extract_unsigned_integer (addr, address_size,
3026 dwarf5_byte_order);
3027 addr += address_size;
3028 ULONGEST length = extract_unsigned_integer (addr, address_size,
3029 dwarf5_byte_order);
3030 addr += address_size;
3031 if (start == 0 && length == 0)
3032 break;
976ca316 3033 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
3034 {
3035 /* Symbol was eliminated due to a COMDAT group. */
3036 continue;
3037 }
3038 ULONGEST end = start + length;
79748972
TT
3039 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3040 - baseaddr);
3041 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3042 - baseaddr);
927aa2e7
JK
3043 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3044 }
3045 }
3046
d320c2b5 3047 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3048 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3049}
3050
9291a0cd
TT
3051/* Find a slot in the mapped index INDEX for the object named NAME.
3052 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3053 constant pool and return true. If NAME cannot be found, return
3054 false. */
2fdf6df6 3055
109483d9 3056static bool
9291a0cd
TT
3057find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3058 offset_type **vec_out)
3059{
0cf03b49 3060 offset_type hash;
9291a0cd 3061 offset_type slot, step;
559a7a62 3062 int (*cmp) (const char *, const char *);
9291a0cd 3063
791afaa2 3064 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3065 if (current_language->la_language == language_cplus
45280282
IB
3066 || current_language->la_language == language_fortran
3067 || current_language->la_language == language_d)
0cf03b49
JK
3068 {
3069 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3070 not contain any. */
a8719064 3071
72998fb3 3072 if (strchr (name, '(') != NULL)
0cf03b49 3073 {
109483d9 3074 without_params = cp_remove_params (name);
0cf03b49 3075
72998fb3 3076 if (without_params != NULL)
791afaa2 3077 name = without_params.get ();
0cf03b49
JK
3078 }
3079 }
3080
559a7a62 3081 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3082 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3083 simulate our NAME being searched is also lowercased. */
3084 hash = mapped_index_string_hash ((index->version == 4
dda83cd7 3085 && case_sensitivity == case_sensitive_off
559a7a62
JK
3086 ? 5 : index->version),
3087 name);
3088
f00a2de2
PA
3089 slot = hash & (index->symbol_table.size () - 1);
3090 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3091 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3092
3093 for (;;)
3094 {
9291a0cd 3095 const char *str;
f00a2de2
PA
3096
3097 const auto &bucket = index->symbol_table[slot];
3098 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3099 return false;
9291a0cd 3100
f00a2de2 3101 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3102 if (!cmp (name, str))
9291a0cd
TT
3103 {
3104 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3105 + MAYBE_SWAP (bucket.vec));
109483d9 3106 return true;
9291a0cd
TT
3107 }
3108
f00a2de2 3109 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3110 }
3111}
3112
4485a1c1
SM
3113/* A helper function that reads the .gdb_index from BUFFER and fills
3114 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3115 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3116 ok to use deprecated sections.
3117
3118 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3119 out parameters that are filled in with information about the CU and
3120 TU lists in the section.
3121
4485a1c1 3122 Returns true if all went well, false otherwise. */
2fdf6df6 3123
d33bc52e 3124static bool
3810f182 3125read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
3126 bool deprecated_ok,
3127 gdb::array_view<const gdb_byte> buffer,
3128 struct mapped_index *map,
3129 const gdb_byte **cu_list,
3130 offset_type *cu_list_elements,
3131 const gdb_byte **types_list,
3132 offset_type *types_list_elements)
3133{
3134 const gdb_byte *addr = &buffer[0];
82430852 3135
9291a0cd 3136 /* Version check. */
4485a1c1 3137 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3138 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3139 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3140 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3141 indices. */
831adc1f 3142 if (version < 4)
481860b3
GB
3143 {
3144 static int warning_printed = 0;
3145 if (!warning_printed)
3146 {
3147 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3148 filename);
481860b3
GB
3149 warning_printed = 1;
3150 }
3151 return 0;
3152 }
3153 /* Index version 4 uses a different hash function than index version
3154 5 and later.
3155
3156 Versions earlier than 6 did not emit psymbols for inlined
3157 functions. Using these files will cause GDB not to be able to
3158 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3159 indices unless the user has done
3160 "set use-deprecated-index-sections on". */
2ec9a5e0 3161 if (version < 6 && !deprecated_ok)
481860b3
GB
3162 {
3163 static int warning_printed = 0;
3164 if (!warning_printed)
3165 {
e615022a
DE
3166 warning (_("\
3167Skipping deprecated .gdb_index section in %s.\n\
3168Do \"set use-deprecated-index-sections on\" before the file is read\n\
3169to use the section anyway."),
2ec9a5e0 3170 filename);
481860b3
GB
3171 warning_printed = 1;
3172 }
3173 return 0;
3174 }
796a7ff8 3175 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3176 of the TU (for symbols coming from TUs),
3177 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3178 Plus gold-generated indices can have duplicate entries for global symbols,
3179 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3180 These are just performance bugs, and we can't distinguish gdb-generated
3181 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3182
481860b3 3183 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3184 longer backward compatible. */
796a7ff8 3185 if (version > 8)
594e8718 3186 return 0;
9291a0cd 3187
559a7a62 3188 map->version = version;
9291a0cd 3189
4485a1c1 3190 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3191
4485a1c1 3192 int i = 0;
2ec9a5e0
TT
3193 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3194 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3195 / 8);
1fd400ff
TT
3196 ++i;
3197
2ec9a5e0
TT
3198 *types_list = addr + MAYBE_SWAP (metadata[i]);
3199 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3200 - MAYBE_SWAP (metadata[i]))
3201 / 8);
987d643c 3202 ++i;
1fd400ff 3203
f00a2de2
PA
3204 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3205 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3206 map->address_table
3207 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3208 ++i;
3209
f00a2de2
PA
3210 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3211 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3212 map->symbol_table
3213 = gdb::array_view<mapped_index::symbol_table_slot>
3214 ((mapped_index::symbol_table_slot *) symbol_table,
3215 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3216
f00a2de2 3217 ++i;
f9d83a0b 3218 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3219
2ec9a5e0
TT
3220 return 1;
3221}
3222
4485a1c1
SM
3223/* Callback types for dwarf2_read_gdb_index. */
3224
3225typedef gdb::function_view
5989a64e 3226 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3227 get_gdb_index_contents_ftype;
3228typedef gdb::function_view
3229 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3230 get_gdb_index_contents_dwz_ftype;
3231
927aa2e7 3232/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3233 elements of all the CUs and return 1. Otherwise, return 0. */
3234
3235static int
4485a1c1 3236dwarf2_read_gdb_index
976ca316 3237 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
3238 get_gdb_index_contents_ftype get_gdb_index_contents,
3239 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3240{
2ec9a5e0
TT
3241 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3242 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3243 struct dwz_file *dwz;
976ca316 3244 struct objfile *objfile = per_objfile->objfile;
f8c41851 3245 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 3246
4485a1c1 3247 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 3248 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
3249
3250 if (main_index_contents.empty ())
3251 return 0;
3252
3063847f 3253 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3254 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3255 use_deprecated_index_sections,
3256 main_index_contents, map.get (), &cu_list,
3257 &cu_list_elements, &types_list,
3258 &types_list_elements))
2ec9a5e0
TT
3259 return 0;
3260
0fefef59 3261 /* Don't use the index if it's empty. */
3063847f 3262 if (map->symbol_table.empty ())
0fefef59
DE
3263 return 0;
3264
2ec9a5e0
TT
3265 /* If there is a .dwz file, read it so we can get its CU list as
3266 well. */
f8c41851 3267 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 3268 if (dwz != NULL)
2ec9a5e0 3269 {
2ec9a5e0
TT
3270 struct mapped_index dwz_map;
3271 const gdb_byte *dwz_types_ignore;
3272 offset_type dwz_types_elements_ignore;
3273
4485a1c1
SM
3274 gdb::array_view<const gdb_byte> dwz_index_content
3275 = get_gdb_index_contents_dwz (objfile, dwz);
3276
3277 if (dwz_index_content.empty ())
3278 return 0;
3279
3810f182 3280 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3281 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3282 &dwz_list, &dwz_list_elements,
3283 &dwz_types_ignore,
3284 &dwz_types_elements_ignore))
2ec9a5e0
TT
3285 {
3286 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3287 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3288 return 0;
3289 }
3290 }
3291
f8c41851
SM
3292 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3293 dwz_list_elements);
1fd400ff 3294
8b70b953
TT
3295 if (types_list_elements)
3296 {
8b70b953
TT
3297 /* We can only handle a single .debug_types when we have an
3298 index. */
f8c41851 3299 if (per_bfd->types.size () != 1)
8b70b953
TT
3300 return 0;
3301
f8c41851 3302 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 3303
f8c41851 3304 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 3305 types_list_elements);
8b70b953 3306 }
9291a0cd 3307
976ca316 3308 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 3309
f8c41851
SM
3310 per_bfd->index_table = std::move (map);
3311 per_bfd->using_index = 1;
3312 per_bfd->quick_file_names_table =
3313 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3314
3315 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3316 objfiles using the same BFD. */
3317 gdb_assert (per_bfd->partial_symtabs == nullptr);
3318 per_bfd->partial_symtabs = objfile->partial_symtabs;
9291a0cd
TT
3319
3320 return 1;
3321}
3322
dee91e82 3323/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3324
dee91e82
DE
3325static void
3326dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3327 const gdb_byte *info_ptr,
3e225074 3328 struct die_info *comp_unit_die)
9291a0cd 3329{
dee91e82 3330 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3331 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 3332 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 3333 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3334 struct attribute *attr;
7b9f3c50
DE
3335 void **slot;
3336 struct quick_file_names *qfn;
9291a0cd 3337
0186c6a7
DE
3338 gdb_assert (! this_cu->is_debug_types);
3339
07261596
TT
3340 /* Our callers never want to match partial units -- instead they
3341 will match the enclosing full CU. */
3342 if (comp_unit_die->tag == DW_TAG_partial_unit)
3343 {
3344 this_cu->v.quick->no_file_data = 1;
3345 return;
3346 }
3347
0186c6a7 3348 lh_cu = this_cu;
7b9f3c50 3349 slot = NULL;
dee91e82 3350
fff8551c 3351 line_header_up lh;
9c541725 3352 sect_offset line_offset {};
fff8551c 3353
dee91e82 3354 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
d4df075e 3355 if (attr != nullptr && attr->form_is_unsigned ())
9291a0cd 3356 {
7b9f3c50
DE
3357 struct quick_file_names find_entry;
3358
d4df075e 3359 line_offset = (sect_offset) attr->as_unsigned ();
7b9f3c50
DE
3360
3361 /* We may have already read in this line header (TU line header sharing).
3362 If we have we're done. */
094b34ac 3363 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3364 find_entry.hash.line_sect_off = line_offset;
976ca316 3365 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3366 &find_entry, INSERT);
3367 if (*slot != NULL)
3368 {
9a3c8263 3369 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3370 return;
7b9f3c50
DE
3371 }
3372
3019eac3 3373 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3374 }
3375 if (lh == NULL)
3376 {
094b34ac 3377 lh_cu->v.quick->no_file_data = 1;
dee91e82 3378 return;
9291a0cd
TT
3379 }
3380
976ca316 3381 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3382 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3383 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3384 gdb_assert (slot != NULL);
3385 *slot = qfn;
9291a0cd 3386
d721ba37 3387 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3388
aa391654
TT
3389 int offset = 0;
3390 if (strcmp (fnd.name, "<unknown>") != 0)
3391 ++offset;
3392
7ba99d21 3393 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3394 qfn->file_names =
976ca316 3395 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 3396 qfn->num_file_names);
aa391654
TT
3397 if (offset != 0)
3398 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3399 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3400 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3401 fnd.comp_dir).release ();
7b9f3c50 3402 qfn->real_names = NULL;
9291a0cd 3403
094b34ac 3404 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3405}
3406
3407/* A helper for the "quick" functions which attempts to read the line
3408 table for THIS_CU. */
3409
3410static struct quick_file_names *
ab432490
SM
3411dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3412 dwarf2_per_objfile *per_objfile)
dee91e82 3413{
0186c6a7
DE
3414 /* This should never be called for TUs. */
3415 gdb_assert (! this_cu->is_debug_types);
3416 /* Nor type unit groups. */
197400e8 3417 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3418
dee91e82
DE
3419 if (this_cu->v.quick->file_names != NULL)
3420 return this_cu->v.quick->file_names;
3421 /* If we know there is no line data, no point in looking again. */
3422 if (this_cu->v.quick->no_file_data)
3423 return NULL;
3424
ab432490 3425 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3426 if (!reader.dummy_p)
3e225074 3427 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3428
3429 if (this_cu->v.quick->no_file_data)
3430 return NULL;
3431 return this_cu->v.quick->file_names;
9291a0cd
TT
3432}
3433
3434/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3435 real path for a given file name from the line table. */
2fdf6df6 3436
9291a0cd 3437static const char *
976ca316 3438dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3439 struct quick_file_names *qfn, int index)
9291a0cd 3440{
7b9f3c50 3441 if (qfn->real_names == NULL)
976ca316 3442 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3443 qfn->num_file_names, const char *);
9291a0cd 3444
7b9f3c50 3445 if (qfn->real_names[index] == NULL)
14278e1f 3446 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3447
7b9f3c50 3448 return qfn->real_names[index];
9291a0cd
TT
3449}
3450
3451static struct symtab *
3452dw2_find_last_source_symtab (struct objfile *objfile)
3453{
976ca316
SM
3454 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3455 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3456 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3457
43f3e411
DE
3458 if (cust == NULL)
3459 return NULL;
ed2dc618 3460
43f3e411 3461 return compunit_primary_filetab (cust);
9291a0cd
TT
3462}
3463
7b9f3c50
DE
3464/* Traversal function for dw2_forget_cached_source_info. */
3465
3466static int
3467dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3468{
7b9f3c50 3469 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3470
7b9f3c50 3471 if (file_data->real_names)
9291a0cd 3472 {
7b9f3c50 3473 int i;
9291a0cd 3474
7b9f3c50 3475 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3476 {
7b9f3c50
DE
3477 xfree ((void*) file_data->real_names[i]);
3478 file_data->real_names[i] = NULL;
9291a0cd
TT
3479 }
3480 }
7b9f3c50
DE
3481
3482 return 1;
3483}
3484
3485static void
3486dw2_forget_cached_source_info (struct objfile *objfile)
3487{
976ca316 3488 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3489
976ca316 3490 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3491 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3492}
3493
f8eba3c6
TT
3494/* Helper function for dw2_map_symtabs_matching_filename that expands
3495 the symtabs and calls the iterator. */
3496
3497static int
3498dw2_map_expand_apply (struct objfile *objfile,
3499 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3500 const char *name, const char *real_path,
14bc53a8 3501 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3502{
43f3e411 3503 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3504
3505 /* Don't visit already-expanded CUs. */
af758d11
SM
3506 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3507 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3508 return 0;
3509
3510 /* This may expand more than one symtab, and we want to iterate over
3511 all of them. */
97a1449a 3512 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3513
14bc53a8
PA
3514 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3515 last_made, callback);
f8eba3c6
TT
3516}
3517
3518/* Implementation of the map_symtabs_matching_filename method. */
3519
14bc53a8
PA
3520static bool
3521dw2_map_symtabs_matching_filename
3522 (struct objfile *objfile, const char *name, const char *real_path,
3523 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3524{
c011a4f4 3525 const char *name_basename = lbasename (name);
976ca316 3526 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 3527
848e3e78
DE
3528 /* The rule is CUs specify all the files, including those used by
3529 any TU, so there's no need to scan TUs here. */
f4dc4d17 3530
976ca316 3531 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3532 {
3d7bb9d9 3533 /* We only need to look at symtabs not already expanded. */
976ca316 3534 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3535 continue;
3536
976ca316 3537 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3538 if (file_data == NULL)
9291a0cd
TT
3539 continue;
3540
b76e467d 3541 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3542 {
7b9f3c50 3543 const char *this_name = file_data->file_names[j];
da235a7c 3544 const char *this_real_name;
9291a0cd 3545
af529f8f 3546 if (compare_filenames_for_search (this_name, name))
9291a0cd 3547 {
f5b95b50 3548 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3549 callback))
3550 return true;
288e77a7 3551 continue;
4aac40c8 3552 }
9291a0cd 3553
c011a4f4
DE
3554 /* Before we invoke realpath, which can get expensive when many
3555 files are involved, do a quick comparison of the basenames. */
3556 if (! basenames_may_differ
3557 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3558 continue;
3559
976ca316 3560 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
da235a7c 3561 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3562 {
da235a7c 3563 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3564 callback))
3565 return true;
288e77a7 3566 continue;
da235a7c 3567 }
9291a0cd 3568
da235a7c
JK
3569 if (real_path != NULL)
3570 {
af529f8f
JK
3571 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3572 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3573 if (this_real_name != NULL
af529f8f 3574 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3575 {
f5b95b50 3576 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3577 callback))
3578 return true;
288e77a7 3579 continue;
9291a0cd
TT
3580 }
3581 }
3582 }
3583 }
3584
14bc53a8 3585 return false;
9291a0cd
TT
3586}
3587
da51c347
DE
3588/* Struct used to manage iterating over all CUs looking for a symbol. */
3589
3590struct dw2_symtab_iterator
9291a0cd 3591{
ed2dc618 3592 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3593 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3594 /* If set, only look for symbols that match that block. Valid values are
3595 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3596 gdb::optional<block_enum> block_index;
da51c347
DE
3597 /* The kind of symbol we're looking for. */
3598 domain_enum domain;
3599 /* The list of CUs from the index entry of the symbol,
3600 or NULL if not found. */
3601 offset_type *vec;
3602 /* The next element in VEC to look at. */
3603 int next;
3604 /* The number of elements in VEC, or zero if there is no match. */
3605 int length;
8943b874
DE
3606 /* Have we seen a global version of the symbol?
3607 If so we can ignore all further global instances.
3608 This is to work around gold/15646, inefficient gold-generated
3609 indices. */
3610 int global_seen;
da51c347 3611};
9291a0cd 3612
e5f3ece2 3613/* Initialize the index symtab iterator ITER, common part. */
2fdf6df6 3614
9291a0cd 3615static void
e5f3ece2
TV
3616dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3617 dwarf2_per_objfile *per_objfile,
3618 gdb::optional<block_enum> block_index,
3619 domain_enum domain)
da51c347 3620{
976ca316 3621 iter->per_objfile = per_objfile;
da51c347
DE
3622 iter->block_index = block_index;
3623 iter->domain = domain;
3624 iter->next = 0;
8943b874 3625 iter->global_seen = 0;
e5f3ece2
TV
3626 iter->vec = NULL;
3627 iter->length = 0;
3628}
da51c347 3629
e5f3ece2
TV
3630/* Initialize the index symtab iterator ITER, const char *NAME variant. */
3631
3632static void
3633dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3634 dwarf2_per_objfile *per_objfile,
3635 gdb::optional<block_enum> block_index,
3636 domain_enum domain,
3637 const char *name)
3638{
3639 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
ed2dc618 3640
e5f3ece2 3641 mapped_index *index = per_objfile->per_bfd->index_table.get ();
ed2dc618 3642 /* index is NULL if OBJF_READNOW. */
e5f3ece2
TV
3643 if (index == NULL)
3644 return;
3645
3646 if (find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347 3647 iter->length = MAYBE_SWAP (*iter->vec);
e5f3ece2
TV
3648}
3649
3650/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3651
3652static void
3653dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3654 dwarf2_per_objfile *per_objfile,
3655 gdb::optional<block_enum> block_index,
3656 domain_enum domain, offset_type namei)
3657{
3658 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3659
3660 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3661 /* index is NULL if OBJF_READNOW. */
3662 if (index == NULL)
3663 return;
3664
3665 gdb_assert (!index->symbol_name_slot_invalid (namei));
3666 const auto &bucket = index->symbol_table[namei];
3667
3668 iter->vec = (offset_type *) (index->constant_pool
3669 + MAYBE_SWAP (bucket.vec));
3670 iter->length = MAYBE_SWAP (*iter->vec);
da51c347
DE
3671}
3672
3673/* Return the next matching CU or NULL if there are no more. */
3674
3675static struct dwarf2_per_cu_data *
3676dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3677{
976ca316 3678 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3679
da51c347
DE
3680 for ( ; iter->next < iter->length; ++iter->next)
3681 {
3682 offset_type cu_index_and_attrs =
3683 MAYBE_SWAP (iter->vec[iter->next + 1]);
3684 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3685 gdb_index_symbol_kind symbol_kind =
3686 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3687 /* Only check the symbol attributes if they're present.
3688 Indices prior to version 7 don't record them,
3689 and indices >= 7 may elide them for certain symbols
3690 (gold does this). */
3691 int attrs_valid =
976ca316 3692 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3693 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3694
3190f0c6 3695 /* Don't crash on bad data. */
976ca316
SM
3696 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3697 + per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3698 {
b98664d3 3699 complaint (_(".gdb_index entry has bad CU index"
976ca316 3700 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3701 continue;
3702 }
3703
976ca316 3704 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3705
da51c347 3706 /* Skip if already read in. */
976ca316 3707 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3708 continue;
3709
8943b874
DE
3710 /* Check static vs global. */
3711 if (attrs_valid)
3712 {
2b79f376
SM
3713 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3714
3715 if (iter->block_index.has_value ())
3716 {
3717 bool want_static = *iter->block_index == STATIC_BLOCK;
3718
3719 if (is_static != want_static)
3720 continue;
3721 }
3722
8943b874 3723 /* Work around gold/15646. */
f030440d
TV
3724 if (!is_static
3725 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3726 {
3727 if (iter->global_seen)
3728 continue;
3729
3730 iter->global_seen = 1;
3731 }
8943b874 3732 }
da51c347
DE
3733
3734 /* Only check the symbol's kind if it has one. */
3735 if (attrs_valid)
3736 {
3737 switch (iter->domain)
3738 {
3739 case VAR_DOMAIN:
3740 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3741 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3742 /* Some types are also in VAR_DOMAIN. */
3743 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3744 continue;
3745 break;
3746 case STRUCT_DOMAIN:
3747 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3748 continue;
3749 break;
3750 case LABEL_DOMAIN:
3751 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3752 continue;
3753 break;
59c35742
AB
3754 case MODULE_DOMAIN:
3755 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3756 continue;
3757 break;
da51c347
DE
3758 default:
3759 break;
3760 }
3761 }
3762
3763 ++iter->next;
3764 return per_cu;
3765 }
3766
3767 return NULL;
3768}
3769
43f3e411 3770static struct compunit_symtab *
c7f839cb 3771dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3772 const char *name, domain_enum domain)
9291a0cd 3773{
43f3e411 3774 struct compunit_symtab *stab_best = NULL;
976ca316 3775 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 3776
b5ec771e
PA
3777 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3778
ed2dc618
SM
3779 struct dw2_symtab_iterator iter;
3780 struct dwarf2_per_cu_data *per_cu;
da51c347 3781
976ca316 3782 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
9291a0cd 3783
ed2dc618
SM
3784 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3785 {
3786 struct symbol *sym, *with_opaque = NULL;
97a1449a 3787 struct compunit_symtab *stab
976ca316 3788 = dw2_instantiate_symtab (per_cu, per_objfile, false);
ed2dc618 3789 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3790 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3791
ed2dc618
SM
3792 sym = block_find_symbol (block, name, domain,
3793 block_find_non_opaque_type_preferred,
3794 &with_opaque);
b2e2f908 3795
ed2dc618
SM
3796 /* Some caution must be observed with overloaded functions
3797 and methods, since the index will not contain any overload
3798 information (but NAME might contain it). */
da51c347 3799
ed2dc618
SM
3800 if (sym != NULL
3801 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3802 return stab;
3803 if (with_opaque != NULL
3804 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3805 stab_best = stab;
da51c347 3806
ed2dc618 3807 /* Keep looking through other CUs. */
9291a0cd 3808 }
9291a0cd 3809
da51c347 3810 return stab_best;
9291a0cd
TT
3811}
3812
3813static void
3814dw2_print_stats (struct objfile *objfile)
3815{
976ca316
SM
3816 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3817 int total = (per_objfile->per_bfd->all_comp_units.size ()
3818 + per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3819 int count = 0;
9291a0cd 3820
ed2dc618 3821 for (int i = 0; i < total; ++i)
9291a0cd 3822 {
976ca316 3823 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3824
976ca316 3825 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3826 ++count;
3827 }
e4a48d9d 3828 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3829 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3830}
3831
779bd270
DE
3832/* This dumps minimal information about the index.
3833 It is called via "mt print objfiles".
3834 One use is to verify .gdb_index has been loaded by the
3835 gdb.dwarf2/gdb-index.exp testcase. */
3836
9291a0cd
TT
3837static void
3838dw2_dump (struct objfile *objfile)
3839{
976ca316 3840 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3841
976ca316 3842 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3843 printf_filtered (".gdb_index:");
976ca316 3844 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3845 {
3846 printf_filtered (" version %d\n",
976ca316 3847 per_objfile->per_bfd->index_table->version);
779bd270
DE
3848 }
3849 else
3850 printf_filtered (" faked for \"readnow\"\n");
3851 printf_filtered ("\n");
9291a0cd
TT
3852}
3853
9291a0cd
TT
3854static void
3855dw2_expand_symtabs_for_function (struct objfile *objfile,
3856 const char *func_name)
3857{
976ca316 3858 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
da51c347 3859
ed2dc618
SM
3860 struct dw2_symtab_iterator iter;
3861 struct dwarf2_per_cu_data *per_cu;
da51c347 3862
976ca316 3863 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3864
ed2dc618 3865 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3866 dw2_instantiate_symtab (per_cu, per_objfile, false);
da51c347 3867
9291a0cd
TT
3868}
3869
3870static void
3871dw2_expand_all_symtabs (struct objfile *objfile)
3872{
976ca316
SM
3873 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3874 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3875 + per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3876
ed2dc618 3877 for (int i = 0; i < total_units; ++i)
9291a0cd 3878 {
976ca316 3879 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3880
58f0c718
TT
3881 /* We don't want to directly expand a partial CU, because if we
3882 read it with the wrong language, then assertion failures can
3883 be triggered later on. See PR symtab/23010. So, tell
3884 dw2_instantiate_symtab to skip partial CUs -- any important
3885 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3886 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3887 }
3888}
3889
3890static void
652a8996
JK
3891dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3892 const char *fullname)
9291a0cd 3893{
976ca316 3894 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3895
3896 /* We don't need to consider type units here.
3897 This is only called for examining code, e.g. expand_line_sal.
3898 There can be an order of magnitude (or more) more type units
3899 than comp units, and we avoid them if we can. */
3900
976ca316 3901 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3902 {
3d7bb9d9 3903 /* We only need to look at symtabs not already expanded. */
976ca316 3904 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3905 continue;
3906
976ca316 3907 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3908 if (file_data == NULL)
9291a0cd
TT
3909 continue;
3910
b76e467d 3911 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3912 {
652a8996
JK
3913 const char *this_fullname = file_data->file_names[j];
3914
3915 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3916 {
976ca316 3917 dw2_instantiate_symtab (per_cu, per_objfile, false);
9291a0cd
TT
3918 break;
3919 }
3920 }
3921 }
3922}
3923
9a0bacfb
TV
3924static void
3925dw2_expand_symtabs_matching_symbol
3926 (mapped_index_base &index,
3927 const lookup_name_info &lookup_name_in,
3928 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3929 enum search_domain kind,
fcf23d5b
SM
3930 gdb::function_view<bool (offset_type)> match_callback,
3931 dwarf2_per_objfile *per_objfile);
9a0bacfb
TV
3932
3933static void
3934dw2_expand_symtabs_matching_one
97a1449a
SM
3935 (dwarf2_per_cu_data *per_cu,
3936 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3937 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3938 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3939
9291a0cd 3940static void
199b4314
TT
3941dw2_map_matching_symbols
3942 (struct objfile *objfile,
b054970d 3943 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3944 int global,
3945 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3946 symbol_compare_ftype *ordered_compare)
9291a0cd 3947{
1aa98955 3948 /* Used for Ada. */
976ca316 3949 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3950
9a0bacfb
TV
3951 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3952
976ca316 3953 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3954 {
976ca316 3955 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3956
9a0bacfb
TV
3957 const char *match_name = name.ada ().lookup_name ().c_str ();
3958 auto matcher = [&] (const char *symname)
3959 {
3960 if (ordered_compare == nullptr)
3961 return true;
3962 return ordered_compare (symname, match_name) == 0;
3963 };
3964
3965 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3966 [&] (offset_type namei)
3967 {
3968 struct dw2_symtab_iterator iter;
3969 struct dwarf2_per_cu_data *per_cu;
3970
976ca316 3971 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3972 namei);
9a0bacfb 3973 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3974 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3975 nullptr);
9a0bacfb 3976 return true;
976ca316 3977 }, per_objfile);
9a0bacfb
TV
3978 }
3979 else
3980 {
3981 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3982 proceed assuming all symtabs have been read in. */
3983 }
1aa98955
TV
3984
3985 for (compunit_symtab *cust : objfile->compunits ())
3986 {
3987 const struct block *block;
3988
3989 if (cust == NULL)
3990 continue;
3991 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3992 if (!iterate_over_symbols_terminated (block, name,
3993 domain, callback))
3994 return;
3995 }
9291a0cd
TT
3996}
3997
e1ef7d7a
PA
3998/* Starting from a search name, return the string that finds the upper
3999 bound of all strings that start with SEARCH_NAME in a sorted name
4000 list. Returns the empty string to indicate that the upper bound is
4001 the end of the list. */
4002
4003static std::string
4004make_sort_after_prefix_name (const char *search_name)
4005{
4006 /* When looking to complete "func", we find the upper bound of all
4007 symbols that start with "func" by looking for where we'd insert
4008 the closest string that would follow "func" in lexicographical
4009 order. Usually, that's "func"-with-last-character-incremented,
4010 i.e. "fund". Mind non-ASCII characters, though. Usually those
4011 will be UTF-8 multi-byte sequences, but we can't be certain.
4012 Especially mind the 0xff character, which is a valid character in
4013 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4014 rule out compilers allowing it in identifiers. Note that
4015 conveniently, strcmp/strcasecmp are specified to compare
4016 characters interpreted as unsigned char. So what we do is treat
4017 the whole string as a base 256 number composed of a sequence of
4018 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4019 to 0, and carries 1 to the following more-significant position.
4020 If the very first character in SEARCH_NAME ends up incremented
4021 and carries/overflows, then the upper bound is the end of the
4022 list. The string after the empty string is also the empty
4023 string.
4024
4025 Some examples of this operation:
4026
4027 SEARCH_NAME => "+1" RESULT
4028
4029 "abc" => "abd"
4030 "ab\xff" => "ac"
4031 "\xff" "a" "\xff" => "\xff" "b"
4032 "\xff" => ""
4033 "\xff\xff" => ""
4034 "" => ""
4035
4036 Then, with these symbols for example:
4037
4038 func
4039 func1
4040 fund
4041
4042 completing "func" looks for symbols between "func" and
4043 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4044 which finds "func" and "func1", but not "fund".
4045
4046 And with:
4047
4048 funcÿ (Latin1 'ÿ' [0xff])
4049 funcÿ1
4050 fund
4051
4052 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4053 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4054
4055 And with:
4056
4057 ÿÿ (Latin1 'ÿ' [0xff])
4058 ÿÿ1
4059
4060 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4061 the end of the list.
4062 */
4063 std::string after = search_name;
4064 while (!after.empty () && (unsigned char) after.back () == 0xff)
4065 after.pop_back ();
4066 if (!after.empty ())
4067 after.back () = (unsigned char) after.back () + 1;
4068 return after;
4069}
4070
5c58de74 4071/* See declaration. */
61d96d7e 4072
5c58de74
PA
4073std::pair<std::vector<name_component>::const_iterator,
4074 std::vector<name_component>::const_iterator>
44ed8f3e 4075mapped_index_base::find_name_components_bounds
fcf23d5b
SM
4076 (const lookup_name_info &lookup_name_without_params, language lang,
4077 dwarf2_per_objfile *per_objfile) const
3f563c84 4078{
5c58de74
PA
4079 auto *name_cmp
4080 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 4081
3b00ef10 4082 const char *lang_name
e0802d59 4083 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 4084
3f563c84
PA
4085 /* Comparison function object for lower_bound that matches against a
4086 given symbol name. */
4087 auto lookup_compare_lower = [&] (const name_component &elem,
4088 const char *name)
4089 {
fcf23d5b 4090 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
4091 const char *elem_name = elem_qualified + elem.name_offset;
4092 return name_cmp (elem_name, name) < 0;
4093 };
4094
4095 /* Comparison function object for upper_bound that matches against a
4096 given symbol name. */
4097 auto lookup_compare_upper = [&] (const char *name,
4098 const name_component &elem)
4099 {
fcf23d5b 4100 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
4101 const char *elem_name = elem_qualified + elem.name_offset;
4102 return name_cmp (name, elem_name) < 0;
4103 };
4104
5c58de74
PA
4105 auto begin = this->name_components.begin ();
4106 auto end = this->name_components.end ();
3f563c84
PA
4107
4108 /* Find the lower bound. */
4109 auto lower = [&] ()
4110 {
3b00ef10 4111 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4112 return begin;
4113 else
3b00ef10 4114 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4115 } ();
4116
4117 /* Find the upper bound. */
4118 auto upper = [&] ()
4119 {
5c58de74 4120 if (lookup_name_without_params.completion_mode ())
3f563c84 4121 {
e1ef7d7a
PA
4122 /* In completion mode, we want UPPER to point past all
4123 symbols names that have the same prefix. I.e., with
4124 these symbols, and completing "func":
4125
4126 function << lower bound
4127 function1
4128 other_function << upper bound
4129
4130 We find the upper bound by looking for the insertion
4131 point of "func"-with-last-character-incremented,
4132 i.e. "fund". */
3b00ef10 4133 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4134 if (after.empty ())
3f563c84 4135 return end;
e6b2f5ef
PA
4136 return std::lower_bound (lower, end, after.c_str (),
4137 lookup_compare_lower);
3f563c84
PA
4138 }
4139 else
3b00ef10 4140 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4141 } ();
4142
5c58de74
PA
4143 return {lower, upper};
4144}
4145
4146/* See declaration. */
4147
4148void
fcf23d5b 4149mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
4150{
4151 if (!this->name_components.empty ())
4152 return;
4153
4154 this->name_components_casing = case_sensitivity;
4155 auto *name_cmp
4156 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4157
4158 /* The code below only knows how to break apart components of C++
4159 symbol names (and other languages that use '::' as
3b00ef10 4160 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4161 auto count = this->symbol_name_count ();
4162 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4163 {
44ed8f3e 4164 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4165 continue;
4166
fcf23d5b 4167 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
4168
4169 /* Add each name component to the name component table. */
4170 unsigned int previous_len = 0;
3b00ef10
TT
4171
4172 if (strstr (name, "::") != nullptr)
4173 {
4174 for (unsigned int current_len = cp_find_first_component (name);
4175 name[current_len] != '\0';
4176 current_len += cp_find_first_component (name + current_len))
4177 {
4178 gdb_assert (name[current_len] == ':');
4179 this->name_components.push_back ({previous_len, idx});
4180 /* Skip the '::'. */
4181 current_len += 2;
4182 previous_len = current_len;
4183 }
4184 }
4185 else
5c58de74 4186 {
3b00ef10
TT
4187 /* Handle the Ada encoded (aka mangled) form here. */
4188 for (const char *iter = strstr (name, "__");
4189 iter != nullptr;
4190 iter = strstr (iter, "__"))
4191 {
4192 this->name_components.push_back ({previous_len, idx});
4193 iter += 2;
4194 previous_len = iter - name;
4195 }
5c58de74 4196 }
3b00ef10 4197
5c58de74
PA
4198 this->name_components.push_back ({previous_len, idx});
4199 }
4200
4201 /* Sort name_components elements by name. */
4202 auto name_comp_compare = [&] (const name_component &left,
4203 const name_component &right)
4204 {
fcf23d5b
SM
4205 const char *left_qualified
4206 = this->symbol_name_at (left.idx, per_objfile);
4207 const char *right_qualified
4208 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
4209
4210 const char *left_name = left_qualified + left.name_offset;
4211 const char *right_name = right_qualified + right.name_offset;
4212
4213 return name_cmp (left_name, right_name) < 0;
4214 };
4215
4216 std::sort (this->name_components.begin (),
4217 this->name_components.end (),
4218 name_comp_compare);
4219}
4220
4221/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4222 mapped_index_base instead of the containing objfile. This is split
4223 to a separate function in order to be able to unit test the
4224 name_components matching using a mock mapped_index_base. For each
5c58de74 4225 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4226 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4227
4228static void
4229dw2_expand_symtabs_matching_symbol
44ed8f3e 4230 (mapped_index_base &index,
5c58de74
PA
4231 const lookup_name_info &lookup_name_in,
4232 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4233 enum search_domain kind,
fcf23d5b
SM
4234 gdb::function_view<bool (offset_type)> match_callback,
4235 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4236{
4237 lookup_name_info lookup_name_without_params
4238 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4239
4240 /* Build the symbol name component sorted vector, if we haven't
4241 yet. */
fcf23d5b 4242 index.build_name_components (per_objfile);
5c58de74 4243
3f563c84
PA
4244 /* The same symbol may appear more than once in the range though.
4245 E.g., if we're looking for symbols that complete "w", and we have
4246 a symbol named "w1::w2", we'll find the two name components for
4247 that same symbol in the range. To be sure we only call the
4248 callback once per symbol, we first collect the symbol name
4249 indexes that matched in a temporary vector and ignore
4250 duplicates. */
4251 std::vector<offset_type> matches;
3f563c84 4252
3b00ef10
TT
4253 struct name_and_matcher
4254 {
4255 symbol_name_matcher_ftype *matcher;
ecc6c606 4256 const char *name;
3b00ef10
TT
4257
4258 bool operator== (const name_and_matcher &other) const
3f563c84 4259 {
ecc6c606 4260 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4261 }
4262 };
4263
4264 /* A vector holding all the different symbol name matchers, for all
4265 languages. */
4266 std::vector<name_and_matcher> matchers;
4267
4268 for (int i = 0; i < nr_languages; i++)
4269 {
4270 enum language lang_e = (enum language) i;
4271
4272 const language_defn *lang = language_def (lang_e);
4273 symbol_name_matcher_ftype *name_matcher
c9debfb9 4274 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 4275
3b00ef10 4276 name_and_matcher key {
dda83cd7 4277 name_matcher,
3b00ef10
TT
4278 lookup_name_without_params.language_lookup_name (lang_e)
4279 };
4280
4281 /* Don't insert the same comparison routine more than once.
4282 Note that we do this linear walk. This is not a problem in
4283 practice because the number of supported languages is
4284 low. */
4285 if (std::find (matchers.begin (), matchers.end (), key)
4286 != matchers.end ())
9291a0cd 4287 continue;
3b00ef10
TT
4288 matchers.push_back (std::move (key));
4289
4290 auto bounds
4291 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 4292 lang_e, per_objfile);
3b00ef10
TT
4293
4294 /* Now for each symbol name in range, check to see if we have a name
4295 match, and if so, call the MATCH_CALLBACK callback. */
4296
4297 for (; bounds.first != bounds.second; ++bounds.first)
4298 {
fcf23d5b
SM
4299 const char *qualified
4300 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
4301
4302 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4303 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4304 continue;
9291a0cd 4305
3b00ef10
TT
4306 matches.push_back (bounds.first->idx);
4307 }
3f563c84
PA
4308 }
4309
4310 std::sort (matches.begin (), matches.end ());
4311
4312 /* Finally call the callback, once per match. */
4313 ULONGEST prev = -1;
4314 for (offset_type idx : matches)
4315 {
4316 if (prev != idx)
4317 {
3b00ef10
TT
4318 if (!match_callback (idx))
4319 break;
3f563c84
PA
4320 prev = idx;
4321 }
4322 }
4323
4324 /* Above we use a type wider than idx's for 'prev', since 0 and
4325 (offset_type)-1 are both possible values. */
4326 static_assert (sizeof (prev) > sizeof (offset_type), "");
4327}
4328
c62446b1
PA
4329#if GDB_SELF_TEST
4330
4331namespace selftests { namespace dw2_expand_symtabs_matching {
4332
a3c5fafd
PA
4333/* A mock .gdb_index/.debug_names-like name index table, enough to
4334 exercise dw2_expand_symtabs_matching_symbol, which works with the
4335 mapped_index_base interface. Builds an index from the symbol list
4336 passed as parameter to the constructor. */
4337class mock_mapped_index : public mapped_index_base
c62446b1
PA
4338{
4339public:
a3c5fafd
PA
4340 mock_mapped_index (gdb::array_view<const char *> symbols)
4341 : m_symbol_table (symbols)
c62446b1
PA
4342 {}
4343
a3c5fafd 4344 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4345
a3c5fafd 4346 /* Return the number of names in the symbol table. */
632e107b 4347 size_t symbol_name_count () const override
c62446b1 4348 {
a3c5fafd 4349 return m_symbol_table.size ();
c62446b1
PA
4350 }
4351
a3c5fafd 4352 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
4353 const char *symbol_name_at
4354 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
4355 {
4356 return m_symbol_table[idx];
4357 }
c62446b1 4358
a3c5fafd
PA
4359private:
4360 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4361};
4362
4363/* Convenience function that converts a NULL pointer to a "<null>"
4364 string, to pass to print routines. */
4365
4366static const char *
4367string_or_null (const char *str)
4368{
4369 return str != NULL ? str : "<null>";
4370}
4371
4372/* Check if a lookup_name_info built from
4373 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4374 index. EXPECTED_LIST is the list of expected matches, in expected
4375 matching order. If no match expected, then an empty list is
4376 specified. Returns true on success. On failure prints a warning
4377 indicating the file:line that failed, and returns false. */
4378
4379static bool
4380check_match (const char *file, int line,
4381 mock_mapped_index &mock_index,
4382 const char *name, symbol_name_match_type match_type,
4383 bool completion_mode,
fcf23d5b
SM
4384 std::initializer_list<const char *> expected_list,
4385 dwarf2_per_objfile *per_objfile)
c62446b1
PA
4386{
4387 lookup_name_info lookup_name (name, match_type, completion_mode);
4388
4389 bool matched = true;
4390
4391 auto mismatch = [&] (const char *expected_str,
4392 const char *got)
4393 {
4394 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4395 "expected=\"%s\", got=\"%s\"\n"),
4396 file, line,
4397 (match_type == symbol_name_match_type::FULL
4398 ? "FULL" : "WILD"),
4399 name, string_or_null (expected_str), string_or_null (got));
4400 matched = false;
4401 };
4402
4403 auto expected_it = expected_list.begin ();
4404 auto expected_end = expected_list.end ();
4405
a3c5fafd 4406 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4407 NULL, ALL_DOMAIN,
4408 [&] (offset_type idx)
4409 {
fcf23d5b 4410 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
4411 const char *expected_str
4412 = expected_it == expected_end ? NULL : *expected_it++;
4413
4414 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4415 mismatch (expected_str, matched_name);
3b00ef10 4416 return true;
fcf23d5b 4417 }, per_objfile);
c62446b1
PA
4418
4419 const char *expected_str
4420 = expected_it == expected_end ? NULL : *expected_it++;
4421 if (expected_str != NULL)
4422 mismatch (expected_str, NULL);
4423
4424 return matched;
4425}
4426
4427/* The symbols added to the mock mapped_index for testing (in
4428 canonical form). */
4429static const char *test_symbols[] = {
4430 "function",
4431 "std::bar",
4432 "std::zfunction",
4433 "std::zfunction2",
4434 "w1::w2",
4435 "ns::foo<char*>",
4436 "ns::foo<int>",
4437 "ns::foo<long>",
a20714ff
PA
4438 "ns2::tmpl<int>::foo2",
4439 "(anonymous namespace)::A::B::C",
c62446b1 4440
e1ef7d7a
PA
4441 /* These are used to check that the increment-last-char in the
4442 matching algorithm for completion doesn't match "t1_fund" when
4443 completing "t1_func". */
4444 "t1_func",
4445 "t1_func1",
4446 "t1_fund",
4447 "t1_fund1",
4448
4449 /* A UTF-8 name with multi-byte sequences to make sure that
4450 cp-name-parser understands this as a single identifier ("função"
4451 is "function" in PT). */
4452 u8"u8função",
4453
4454 /* \377 (0xff) is Latin1 'ÿ'. */
4455 "yfunc\377",
4456
4457 /* \377 (0xff) is Latin1 'ÿ'. */
4458 "\377",
4459 "\377\377123",
4460
c62446b1
PA
4461 /* A name with all sorts of complications. Starts with "z" to make
4462 it easier for the completion tests below. */
4463#define Z_SYM_NAME \
4464 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4465 "::tuple<(anonymous namespace)::ui*, " \
4466 "std::default_delete<(anonymous namespace)::ui>, void>"
4467
4468 Z_SYM_NAME
4469};
4470
a3c5fafd
PA
4471/* Returns true if the mapped_index_base::find_name_component_bounds
4472 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4473 in completion mode. */
5c58de74
PA
4474
4475static bool
a3c5fafd 4476check_find_bounds_finds (mapped_index_base &index,
5c58de74 4477 const char *search_name,
fcf23d5b
SM
4478 gdb::array_view<const char *> expected_syms,
4479 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4480{
4481 lookup_name_info lookup_name (search_name,
4482 symbol_name_match_type::FULL, true);
4483
3b00ef10 4484 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4485 language_cplus,
4486 per_objfile);
5c58de74
PA
4487
4488 size_t distance = std::distance (bounds.first, bounds.second);
4489 if (distance != expected_syms.size ())
4490 return false;
4491
4492 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4493 {
4494 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4495 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4496 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4497 return false;
4498 }
4499
4500 return true;
4501}
4502
4503/* Test the lower-level mapped_index::find_name_component_bounds
4504 method. */
4505
c62446b1 4506static void
5c58de74
PA
4507test_mapped_index_find_name_component_bounds ()
4508{
4509 mock_mapped_index mock_index (test_symbols);
4510
fcf23d5b 4511 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4512
4513 /* Test the lower-level mapped_index::find_name_component_bounds
4514 method in completion mode. */
4515 {
4516 static const char *expected_syms[] = {
4517 "t1_func",
4518 "t1_func1",
5c58de74
PA
4519 };
4520
fcf23d5b
SM
4521 SELF_CHECK (check_find_bounds_finds
4522 (mock_index, "t1_func", expected_syms,
4523 NULL /* per_objfile */));
5c58de74
PA
4524 }
4525
4526 /* Check that the increment-last-char in the name matching algorithm
4527 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4528 {
4529 static const char *expected_syms1[] = {
4530 "\377",
4531 "\377\377123",
4532 };
fcf23d5b
SM
4533 SELF_CHECK (check_find_bounds_finds
4534 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4535
4536 static const char *expected_syms2[] = {
4537 "\377\377123",
4538 };
fcf23d5b
SM
4539 SELF_CHECK (check_find_bounds_finds
4540 (mock_index, "\377\377", expected_syms2,
4541 NULL /* per_objfile */));
5c58de74
PA
4542 }
4543}
4544
4545/* Test dw2_expand_symtabs_matching_symbol. */
4546
4547static void
4548test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4549{
4550 mock_mapped_index mock_index (test_symbols);
4551
4552 /* We let all tests run until the end even if some fails, for debug
4553 convenience. */
4554 bool any_mismatch = false;
4555
4556 /* Create the expected symbols list (an initializer_list). Needed
4557 because lists have commas, and we need to pass them to CHECK,
4558 which is a macro. */
4559#define EXPECT(...) { __VA_ARGS__ }
4560
4561 /* Wrapper for check_match that passes down the current
4562 __FILE__/__LINE__. */
4563#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4564 any_mismatch |= !check_match (__FILE__, __LINE__, \
4565 mock_index, \
4566 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4567 EXPECTED_LIST, NULL)
c62446b1
PA
4568
4569 /* Identity checks. */
4570 for (const char *sym : test_symbols)
4571 {
4572 /* Should be able to match all existing symbols. */
4573 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4574 EXPECT (sym));
4575
4576 /* Should be able to match all existing symbols with
4577 parameters. */
4578 std::string with_params = std::string (sym) + "(int)";
4579 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4580 EXPECT (sym));
4581
4582 /* Should be able to match all existing symbols with
4583 parameters and qualifiers. */
4584 with_params = std::string (sym) + " ( int ) const";
4585 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4586 EXPECT (sym));
4587
4588 /* This should really find sym, but cp-name-parser.y doesn't
4589 know about lvalue/rvalue qualifiers yet. */
4590 with_params = std::string (sym) + " ( int ) &&";
4591 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4592 {});
4593 }
4594
e1ef7d7a
PA
4595 /* Check that the name matching algorithm for completion doesn't get
4596 confused with Latin1 'ÿ' / 0xff. */
4597 {
4598 static const char str[] = "\377";
4599 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4600 EXPECT ("\377", "\377\377123"));
4601 }
4602
4603 /* Check that the increment-last-char in the matching algorithm for
4604 completion doesn't match "t1_fund" when completing "t1_func". */
4605 {
4606 static const char str[] = "t1_func";
4607 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4608 EXPECT ("t1_func", "t1_func1"));
4609 }
4610
c62446b1
PA
4611 /* Check that completion mode works at each prefix of the expected
4612 symbol name. */
4613 {
4614 static const char str[] = "function(int)";
4615 size_t len = strlen (str);
4616 std::string lookup;
4617
4618 for (size_t i = 1; i < len; i++)
4619 {
4620 lookup.assign (str, i);
4621 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4622 EXPECT ("function"));
4623 }
4624 }
4625
4626 /* While "w" is a prefix of both components, the match function
4627 should still only be called once. */
4628 {
4629 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4630 EXPECT ("w1::w2"));
a20714ff
PA
4631 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4632 EXPECT ("w1::w2"));
c62446b1
PA
4633 }
4634
4635 /* Same, with a "complicated" symbol. */
4636 {
4637 static const char str[] = Z_SYM_NAME;
4638 size_t len = strlen (str);
4639 std::string lookup;
4640
4641 for (size_t i = 1; i < len; i++)
4642 {
4643 lookup.assign (str, i);
4644 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4645 EXPECT (Z_SYM_NAME));
4646 }
4647 }
4648
4649 /* In FULL mode, an incomplete symbol doesn't match. */
4650 {
4651 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4652 {});
4653 }
4654
4655 /* A complete symbol with parameters matches any overload, since the
4656 index has no overload info. */
4657 {
4658 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4659 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4660 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4661 EXPECT ("std::zfunction", "std::zfunction2"));
4662 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4663 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4664 }
4665
4666 /* Check that whitespace is ignored appropriately. A symbol with a
4667 template argument list. */
4668 {
4669 static const char expected[] = "ns::foo<int>";
4670 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4671 EXPECT (expected));
a20714ff
PA
4672 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4673 EXPECT (expected));
c62446b1
PA
4674 }
4675
4676 /* Check that whitespace is ignored appropriately. A symbol with a
4677 template argument list that includes a pointer. */
4678 {
4679 static const char expected[] = "ns::foo<char*>";
4680 /* Try both completion and non-completion modes. */
4681 static const bool completion_mode[2] = {false, true};
4682 for (size_t i = 0; i < 2; i++)
4683 {
4684 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4685 completion_mode[i], EXPECT (expected));
a20714ff
PA
4686 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4687 completion_mode[i], EXPECT (expected));
c62446b1
PA
4688
4689 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4690 completion_mode[i], EXPECT (expected));
a20714ff
PA
4691 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4692 completion_mode[i], EXPECT (expected));
c62446b1
PA
4693 }
4694 }
4695
4696 {
4697 /* Check method qualifiers are ignored. */
4698 static const char expected[] = "ns::foo<char*>";
4699 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4700 symbol_name_match_type::FULL, true, EXPECT (expected));
4701 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4702 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4703 CHECK_MATCH ("foo < char * > ( int ) const",
4704 symbol_name_match_type::WILD, true, EXPECT (expected));
4705 CHECK_MATCH ("foo < char * > ( int ) &&",
4706 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4707 }
4708
4709 /* Test lookup names that don't match anything. */
4710 {
a20714ff
PA
4711 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4712 {});
4713
c62446b1
PA
4714 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4715 {});
4716 }
4717
a20714ff
PA
4718 /* Some wild matching tests, exercising "(anonymous namespace)",
4719 which should not be confused with a parameter list. */
4720 {
4721 static const char *syms[] = {
4722 "A::B::C",
4723 "B::C",
4724 "C",
4725 "A :: B :: C ( int )",
4726 "B :: C ( int )",
4727 "C ( int )",
4728 };
4729
4730 for (const char *s : syms)
4731 {
4732 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4733 EXPECT ("(anonymous namespace)::A::B::C"));
4734 }
4735 }
4736
4737 {
4738 static const char expected[] = "ns2::tmpl<int>::foo2";
4739 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4740 EXPECT (expected));
4741 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4742 EXPECT (expected));
4743 }
4744
c62446b1
PA
4745 SELF_CHECK (!any_mismatch);
4746
4747#undef EXPECT
4748#undef CHECK_MATCH
4749}
4750
5c58de74
PA
4751static void
4752run_test ()
4753{
4754 test_mapped_index_find_name_component_bounds ();
4755 test_dw2_expand_symtabs_matching_symbol ();
4756}
4757
c62446b1
PA
4758}} // namespace selftests::dw2_expand_symtabs_matching
4759
4760#endif /* GDB_SELF_TEST */
4761
4b514bc8
JK
4762/* If FILE_MATCHER is NULL or if PER_CU has
4763 dwarf2_per_cu_quick_data::MARK set (see
4764 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4765 EXPANSION_NOTIFY on it. */
4766
4767static void
4768dw2_expand_symtabs_matching_one
97a1449a
SM
4769 (dwarf2_per_cu_data *per_cu,
4770 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4771 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4772 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4773{
4774 if (file_matcher == NULL || per_cu->v.quick->mark)
4775 {
af758d11 4776 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4777
97a1449a
SM
4778 compunit_symtab *symtab
4779 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4780 gdb_assert (symtab != nullptr);
4b514bc8 4781
af758d11
SM
4782 if (expansion_notify != NULL && symtab_was_null)
4783 expansion_notify (symtab);
4b514bc8
JK
4784 }
4785}
4786
3f563c84
PA
4787/* Helper for dw2_expand_matching symtabs. Called on each symbol
4788 matched, to expand corresponding CUs that were marked. IDX is the
4789 index of the symbol name that matched. */
4790
4791static void
4792dw2_expand_marked_cus
976ca316 4793 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4794 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4795 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4796 search_domain kind)
4797{
3f563c84
PA
4798 offset_type *vec, vec_len, vec_idx;
4799 bool global_seen = false;
976ca316 4800 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4801
61920122 4802 vec = (offset_type *) (index.constant_pool
f00a2de2 4803 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4804 vec_len = MAYBE_SWAP (vec[0]);
4805 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4806 {
61920122
PA
4807 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4808 /* This value is only valid for index versions >= 7. */
4809 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4810 gdb_index_symbol_kind symbol_kind =
4811 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4812 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4813 /* Only check the symbol attributes if they're present.
4814 Indices prior to version 7 don't record them,
4815 and indices >= 7 may elide them for certain symbols
4816 (gold does this). */
4817 int attrs_valid =
4818 (index.version >= 7
4819 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4820
4821 /* Work around gold/15646. */
f030440d
TV
4822 if (attrs_valid
4823 && !is_static
4824 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4825 {
f030440d 4826 if (global_seen)
61920122 4827 continue;
f030440d
TV
4828
4829 global_seen = true;
61920122 4830 }
3190f0c6 4831
61920122
PA
4832 /* Only check the symbol's kind if it has one. */
4833 if (attrs_valid)
4834 {
4835 switch (kind)
8943b874 4836 {
61920122
PA
4837 case VARIABLES_DOMAIN:
4838 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4839 continue;
4840 break;
4841 case FUNCTIONS_DOMAIN:
4842 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4843 continue;
61920122
PA
4844 break;
4845 case TYPES_DOMAIN:
4846 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4847 continue;
4848 break;
59c35742
AB
4849 case MODULES_DOMAIN:
4850 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4851 continue;
4852 break;
61920122
PA
4853 default:
4854 break;
8943b874 4855 }
61920122 4856 }
8943b874 4857
61920122 4858 /* Don't crash on bad data. */
976ca316
SM
4859 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4860 + per_objfile->per_bfd->all_type_units.size ()))
61920122 4861 {
b98664d3 4862 complaint (_(".gdb_index entry has bad CU index"
976ca316 4863 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4864 continue;
4865 }
4866
976ca316
SM
4867 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4868 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4b514bc8 4869 expansion_notify);
61920122
PA
4870 }
4871}
4872
4b514bc8
JK
4873/* If FILE_MATCHER is non-NULL, set all the
4874 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4875 that match FILE_MATCHER. */
4876
61920122 4877static void
4b514bc8 4878dw_expand_symtabs_matching_file_matcher
976ca316 4879 (dwarf2_per_objfile *per_objfile,
ed2dc618 4880 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4881{
4b514bc8 4882 if (file_matcher == NULL)
61920122
PA
4883 return;
4884
4b514bc8
JK
4885 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4886 htab_eq_pointer,
4887 NULL, xcalloc, xfree));
4888 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4889 htab_eq_pointer,
4890 NULL, xcalloc, xfree));
61920122 4891
4b514bc8
JK
4892 /* The rule is CUs specify all the files, including those used by
4893 any TU, so there's no need to scan TUs here. */
61920122 4894
976ca316 4895 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4896 {
927aa2e7
JK
4897 QUIT;
4898
4899 per_cu->v.quick->mark = 0;
4900
4901 /* We only need to look at symtabs not already expanded. */
976ca316 4902 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4903 continue;
4904
976ca316 4905 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4906 if (file_data == NULL)
4907 continue;
4908
4909 if (htab_find (visited_not_found.get (), file_data) != NULL)
4910 continue;
4911 else if (htab_find (visited_found.get (), file_data) != NULL)
4912 {
4913 per_cu->v.quick->mark = 1;
4914 continue;
4915 }
4916
b76e467d 4917 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4918 {
4919 const char *this_real_name;
4920
4921 if (file_matcher (file_data->file_names[j], false))
4922 {
4923 per_cu->v.quick->mark = 1;
4924 break;
4925 }
4926
4927 /* Before we invoke realpath, which can get expensive when many
4928 files are involved, do a quick comparison of the basenames. */
4929 if (!basenames_may_differ
4930 && !file_matcher (lbasename (file_data->file_names[j]),
4931 true))
4932 continue;
4933
976ca316 4934 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4935 if (file_matcher (this_real_name, false))
4936 {
4937 per_cu->v.quick->mark = 1;
4938 break;
4939 }
4940 }
4941
b76e467d
SM
4942 void **slot = htab_find_slot (per_cu->v.quick->mark
4943 ? visited_found.get ()
4944 : visited_not_found.get (),
4945 file_data, INSERT);
927aa2e7
JK
4946 *slot = file_data;
4947 }
4948}
4949
4950static void
4951dw2_expand_symtabs_matching
4952 (struct objfile *objfile,
4953 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4954 const lookup_name_info *lookup_name,
927aa2e7
JK
4955 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4956 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4957 enum search_domain kind)
4958{
976ca316 4959 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4960
4961 /* index_table is NULL if OBJF_READNOW. */
976ca316 4962 if (!per_objfile->per_bfd->index_table)
927aa2e7
JK
4963 return;
4964
976ca316 4965 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4966
c1a66c06
TV
4967 if (symbol_matcher == NULL && lookup_name == NULL)
4968 {
976ca316 4969 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4970 {
4971 QUIT;
4972
976ca316 4973 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
97a1449a 4974 file_matcher, expansion_notify);
c1a66c06
TV
4975 }
4976 return;
4977 }
4978
976ca316 4979 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4980
c1a66c06 4981 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4982 symbol_matcher,
4983 kind, [&] (offset_type idx)
4984 {
976ca316
SM
4985 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4986 kind);
3b00ef10 4987 return true;
976ca316 4988 }, per_objfile);
927aa2e7
JK
4989}
4990
4991/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4992 symtab. */
4993
4994static struct compunit_symtab *
4995recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4996 CORE_ADDR pc)
4997{
4998 int i;
4999
5000 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5001 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5002 return cust;
5003
5004 if (cust->includes == NULL)
5005 return NULL;
5006
5007 for (i = 0; cust->includes[i]; ++i)
5008 {
5009 struct compunit_symtab *s = cust->includes[i];
5010
5011 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5012 if (s != NULL)
5013 return s;
5014 }
5015
5016 return NULL;
5017}
5018
5019static struct compunit_symtab *
5020dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5021 struct bound_minimal_symbol msymbol,
5022 CORE_ADDR pc,
5023 struct obj_section *section,
5024 int warn_if_readin)
5025{
5026 struct dwarf2_per_cu_data *data;
5027 struct compunit_symtab *result;
5028
d320c2b5 5029 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
5030 return NULL;
5031
b3b3bada 5032 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
5033 data = (struct dwarf2_per_cu_data *) addrmap_find
5034 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5035 if (!data)
5036 return NULL;
5037
af758d11
SM
5038 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5039 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 5040 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 5041 paddress (objfile->arch (), pc));
927aa2e7 5042
97a1449a
SM
5043 result = recursively_find_pc_sect_compunit_symtab
5044 (dw2_instantiate_symtab (data, per_objfile, false), pc);
5045
927aa2e7
JK
5046 gdb_assert (result != NULL);
5047 return result;
5048}
5049
5050static void
5051dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5052 void *data, int need_fullname)
5053{
976ca316 5054 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 5055
976ca316 5056 if (!per_objfile->per_bfd->filenames_cache)
927aa2e7 5057 {
976ca316 5058 per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
5059
5060 htab_up visited (htab_create_alloc (10,
5061 htab_hash_pointer, htab_eq_pointer,
5062 NULL, xcalloc, xfree));
5063
5064 /* The rule is CUs specify all the files, including those used
5065 by any TU, so there's no need to scan TUs here. We can
5066 ignore file names coming from already-expanded CUs. */
5067
976ca316 5068 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 5069 {
976ca316 5070 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5071 {
5072 void **slot = htab_find_slot (visited.get (),
5073 per_cu->v.quick->file_names,
5074 INSERT);
5075
5076 *slot = per_cu->v.quick->file_names;
5077 }
5078 }
5079
976ca316 5080 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 5081 {
927aa2e7 5082 /* We only need to look at symtabs not already expanded. */
976ca316 5083 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5084 continue;
5085
ab432490 5086 quick_file_names *file_data
976ca316 5087 = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
5088 if (file_data == NULL)
5089 continue;
5090
b76e467d 5091 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5092 if (*slot)
5093 {
5094 /* Already visited. */
5095 continue;
5096 }
5097 *slot = file_data;
5098
5099 for (int j = 0; j < file_data->num_file_names; ++j)
5100 {
5101 const char *filename = file_data->file_names[j];
976ca316 5102 per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
5103 }
5104 }
5105 }
5106
976ca316 5107 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
5108 {
5109 gdb::unique_xmalloc_ptr<char> this_real_name;
5110
5111 if (need_fullname)
5112 this_real_name = gdb_realpath (filename);
5113 (*fun) (filename, this_real_name.get (), data);
5114 });
5115}
5116
5117static int
5118dw2_has_symbols (struct objfile *objfile)
5119{
5120 return 1;
5121}
5122
5123const struct quick_symbol_functions dwarf2_gdb_index_functions =
5124{
5125 dw2_has_symbols,
5126 dw2_find_last_source_symtab,
5127 dw2_forget_cached_source_info,
5128 dw2_map_symtabs_matching_filename,
5129 dw2_lookup_symbol,
d3214198 5130 NULL,
927aa2e7
JK
5131 dw2_print_stats,
5132 dw2_dump,
927aa2e7
JK
5133 dw2_expand_symtabs_for_function,
5134 dw2_expand_all_symtabs,
5135 dw2_expand_symtabs_with_fullname,
5136 dw2_map_matching_symbols,
5137 dw2_expand_symtabs_matching,
5138 dw2_find_pc_sect_compunit_symtab,
5139 NULL,
5140 dw2_map_symbol_filenames
5141};
5142
5143/* DWARF-5 debug_names reader. */
5144
5145/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5146static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5147
5148/* A helper function that reads the .debug_names section in SECTION
5149 and fills in MAP. FILENAME is the name of the file containing the
5150 section; it is used for error reporting.
5151
5152 Returns true if all went well, false otherwise. */
5153
5154static bool
5155read_debug_names_from_section (struct objfile *objfile,
5156 const char *filename,
5157 struct dwarf2_section_info *section,
5158 mapped_debug_names &map)
5159{
96b79293 5160 if (section->empty ())
927aa2e7
JK
5161 return false;
5162
5163 /* Older elfutils strip versions could keep the section in the main
5164 executable while splitting it for the separate debug info file. */
96b79293 5165 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
5166 return false;
5167
96b79293 5168 section->read (objfile);
927aa2e7 5169
08feed99 5170 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
5171
5172 const gdb_byte *addr = section->buffer;
5173
96b79293 5174 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
5175
5176 unsigned int bytes_read;
5177 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5178 addr += bytes_read;
5179
5180 map.dwarf5_is_dwarf64 = bytes_read != 4;
5181 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5182 if (bytes_read + length != section->size)
5183 {
5184 /* There may be multiple per-CU indices. */
5185 warning (_("Section .debug_names in %s length %s does not match "
5186 "section length %s, ignoring .debug_names."),
5187 filename, plongest (bytes_read + length),
5188 pulongest (section->size));
5189 return false;
5190 }
5191
5192 /* The version number. */
5193 uint16_t version = read_2_bytes (abfd, addr);
5194 addr += 2;
5195 if (version != 5)
5196 {
5197 warning (_("Section .debug_names in %s has unsupported version %d, "
5198 "ignoring .debug_names."),
5199 filename, version);
5200 return false;
5201 }
5202
5203 /* Padding. */
5204 uint16_t padding = read_2_bytes (abfd, addr);
5205 addr += 2;
5206 if (padding != 0)
5207 {
5208 warning (_("Section .debug_names in %s has unsupported padding %d, "
5209 "ignoring .debug_names."),
5210 filename, padding);
5211 return false;
5212 }
5213
5214 /* comp_unit_count - The number of CUs in the CU list. */
5215 map.cu_count = read_4_bytes (abfd, addr);
5216 addr += 4;
5217
5218 /* local_type_unit_count - The number of TUs in the local TU
5219 list. */
5220 map.tu_count = read_4_bytes (abfd, addr);
5221 addr += 4;
5222
5223 /* foreign_type_unit_count - The number of TUs in the foreign TU
5224 list. */
5225 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5226 addr += 4;
5227 if (foreign_tu_count != 0)
5228 {
5229 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5230 "ignoring .debug_names."),
5231 filename, static_cast<unsigned long> (foreign_tu_count));
5232 return false;
5233 }
5234
5235 /* bucket_count - The number of hash buckets in the hash lookup
5236 table. */
5237 map.bucket_count = read_4_bytes (abfd, addr);
5238 addr += 4;
5239
5240 /* name_count - The number of unique names in the index. */
5241 map.name_count = read_4_bytes (abfd, addr);
5242 addr += 4;
5243
5244 /* abbrev_table_size - The size in bytes of the abbreviations
5245 table. */
5246 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5247 addr += 4;
5248
5249 /* augmentation_string_size - The size in bytes of the augmentation
5250 string. This value is rounded up to a multiple of 4. */
5251 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5252 addr += 4;
5253 map.augmentation_is_gdb = ((augmentation_string_size
5254 == sizeof (dwarf5_augmentation))
5255 && memcmp (addr, dwarf5_augmentation,
5256 sizeof (dwarf5_augmentation)) == 0);
5257 augmentation_string_size += (-augmentation_string_size) & 3;
5258 addr += augmentation_string_size;
5259
5260 /* List of CUs */
5261 map.cu_table_reordered = addr;
5262 addr += map.cu_count * map.offset_size;
5263
5264 /* List of Local TUs */
5265 map.tu_table_reordered = addr;
5266 addr += map.tu_count * map.offset_size;
5267
5268 /* Hash Lookup Table */
5269 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5270 addr += map.bucket_count * 4;
5271 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5272 addr += map.name_count * 4;
5273
5274 /* Name Table */
5275 map.name_table_string_offs_reordered = addr;
5276 addr += map.name_count * map.offset_size;
5277 map.name_table_entry_offs_reordered = addr;
5278 addr += map.name_count * map.offset_size;
5279
5280 const gdb_byte *abbrev_table_start = addr;
5281 for (;;)
5282 {
927aa2e7
JK
5283 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5284 addr += bytes_read;
5285 if (index_num == 0)
5286 break;
5287
5288 const auto insertpair
5289 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5290 if (!insertpair.second)
5291 {
5292 warning (_("Section .debug_names in %s has duplicate index %s, "
5293 "ignoring .debug_names."),
5294 filename, pulongest (index_num));
5295 return false;
5296 }
5297 mapped_debug_names::index_val &indexval = insertpair.first->second;
5298 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5299 addr += bytes_read;
5300
5301 for (;;)
5302 {
5303 mapped_debug_names::index_val::attr attr;
5304 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5305 addr += bytes_read;
5306 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5307 addr += bytes_read;
5308 if (attr.form == DW_FORM_implicit_const)
5309 {
5310 attr.implicit_const = read_signed_leb128 (abfd, addr,
5311 &bytes_read);
5312 addr += bytes_read;
5313 }
5314 if (attr.dw_idx == 0 && attr.form == 0)
5315 break;
5316 indexval.attr_vec.push_back (std::move (attr));
5317 }
5318 }
5319 if (addr != abbrev_table_start + abbrev_table_size)
5320 {
5321 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5322 "of size %s vs. written as %u, ignoring .debug_names."),
5323 filename, plongest (addr - abbrev_table_start),
5324 abbrev_table_size);
927aa2e7
JK
5325 return false;
5326 }
5327 map.entry_pool = addr;
5328
5329 return true;
5330}
5331
5332/* A helper for create_cus_from_debug_names that handles the MAP's CU
5333 list. */
5334
5335static void
168c9250 5336create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5337 const mapped_debug_names &map,
5338 dwarf2_section_info &section,
b76e467d 5339 bool is_dwz)
927aa2e7 5340{
3ee6bb11
TV
5341 if (!map.augmentation_is_gdb)
5342 {
fc9a13fb
TV
5343 for (uint32_t i = 0; i < map.cu_count; ++i)
5344 {
5345 sect_offset sect_off
5346 = (sect_offset) (extract_unsigned_integer
5347 (map.cu_table_reordered + i * map.offset_size,
5348 map.offset_size,
5349 map.dwarf5_byte_order));
5350 /* We don't know the length of the CU, because the CU list in a
5351 .debug_names index can be incomplete, so we can't use the start
5352 of the next CU as end of this CU. We create the CUs here with
5353 length 0, and in cutu_reader::cutu_reader we'll fill in the
5354 actual length. */
5355 dwarf2_per_cu_data *per_cu
5356 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5357 sect_off, 0);
5358 per_bfd->all_comp_units.push_back (per_cu);
5359 }
d3b54e63 5360 return;
3ee6bb11
TV
5361 }
5362
927aa2e7
JK
5363 sect_offset sect_off_prev;
5364 for (uint32_t i = 0; i <= map.cu_count; ++i)
5365 {
5366 sect_offset sect_off_next;
5367 if (i < map.cu_count)
5368 {
5369 sect_off_next
5370 = (sect_offset) (extract_unsigned_integer
5371 (map.cu_table_reordered + i * map.offset_size,
5372 map.offset_size,
5373 map.dwarf5_byte_order));
5374 }
5375 else
5376 sect_off_next = (sect_offset) section.size;
5377 if (i >= 1)
5378 {
5379 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5380 dwarf2_per_cu_data *per_cu
168c9250 5381 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5382 sect_off_prev, length);
168c9250 5383 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5384 }
5385 sect_off_prev = sect_off_next;
5386 }
5387}
5388
5389/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5390 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5391
5392static void
168c9250 5393create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5394 const mapped_debug_names &map,
5395 const mapped_debug_names &dwz_map)
5396{
168c9250
SM
5397 gdb_assert (per_bfd->all_comp_units.empty ());
5398 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5399
168c9250 5400 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5401 false /* is_dwz */);
927aa2e7
JK
5402
5403 if (dwz_map.cu_count == 0)
5404 return;
5405
168c9250
SM
5406 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5407 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5408 true /* is_dwz */);
927aa2e7
JK
5409}
5410
5411/* Read .debug_names. If everything went ok, initialize the "quick"
5412 elements of all the CUs and return true. Otherwise, return false. */
5413
5414static bool
976ca316 5415dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 5416{
fcf23d5b
SM
5417 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5418 mapped_debug_names dwz_map;
976ca316 5419 struct objfile *objfile = per_objfile->objfile;
f8c41851 5420 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
5421
5422 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
976ca316 5423 &per_objfile->per_bfd->debug_names, *map))
927aa2e7
JK
5424 return false;
5425
5426 /* Don't use the index if it's empty. */
22ca247e 5427 if (map->name_count == 0)
927aa2e7
JK
5428 return false;
5429
5430 /* If there is a .dwz file, read it so we can get its CU list as
5431 well. */
f8c41851 5432 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
5433 if (dwz != NULL)
5434 {
5435 if (!read_debug_names_from_section (objfile,
00f93c44 5436 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5437 &dwz->debug_names, dwz_map))
5438 {
5439 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5440 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5441 return false;
5442 }
5443 }
5444
f8c41851 5445 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 5446
22ca247e 5447 if (map->tu_count != 0)
927aa2e7
JK
5448 {
5449 /* We can only handle a single .debug_types when we have an
5450 index. */
f8c41851 5451 if (per_bfd->types.size () != 1)
927aa2e7
JK
5452 return false;
5453
f8c41851 5454 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
5455
5456 create_signatured_type_table_from_debug_names
f8c41851 5457 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
5458 }
5459
f8c41851 5460 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 5461
f8c41851
SM
5462 per_bfd->debug_names_table = std::move (map);
5463 per_bfd->using_index = 1;
5464 per_bfd->quick_file_names_table =
976ca316 5465 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
927aa2e7 5466
f8c41851
SM
5467 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5468 objfiles using the same BFD. */
5469 gdb_assert (per_bfd->partial_symtabs == nullptr);
5470 per_bfd->partial_symtabs = objfile->partial_symtabs;
5471
927aa2e7
JK
5472 return true;
5473}
5474
927aa2e7
JK
5475/* Type used to manage iterating over all CUs looking for a symbol for
5476 .debug_names. */
5477
5478class dw2_debug_names_iterator
5479{
5480public:
927aa2e7 5481 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5482 gdb::optional<block_enum> block_index,
5483 domain_enum domain,
fcf23d5b 5484 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5485 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5486 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5487 m_per_objfile (per_objfile)
927aa2e7
JK
5488 {}
5489
5490 dw2_debug_names_iterator (const mapped_debug_names &map,
fcf23d5b 5491 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5492 : m_map (map),
5493 m_search (search),
fcf23d5b
SM
5494 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5495 m_per_objfile (per_objfile)
927aa2e7
JK
5496 {}
5497
3b00ef10
TT
5498 dw2_debug_names_iterator (const mapped_debug_names &map,
5499 block_enum block_index, domain_enum domain,
fcf23d5b 5500 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5501 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5502 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5503 m_per_objfile (per_objfile)
3b00ef10
TT
5504 {}
5505
927aa2e7
JK
5506 /* Return the next matching CU or NULL if there are no more. */
5507 dwarf2_per_cu_data *next ();
5508
5509private:
5510 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5511 const char *name,
5512 dwarf2_per_objfile *per_objfile);
927aa2e7 5513 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5514 uint32_t namei,
5515 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5516
5517 /* The internalized form of .debug_names. */
5518 const mapped_debug_names &m_map;
5519
2b79f376
SM
5520 /* If set, only look for symbols that match that block. Valid values are
5521 GLOBAL_BLOCK and STATIC_BLOCK. */
5522 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5523
5524 /* The kind of symbol we're looking for. */
5525 const domain_enum m_domain = UNDEF_DOMAIN;
5526 const search_domain m_search = ALL_DOMAIN;
5527
5528 /* The list of CUs from the index entry of the symbol, or NULL if
5529 not found. */
5530 const gdb_byte *m_addr;
fcf23d5b
SM
5531
5532 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5533};
5534
5535const char *
fcf23d5b 5536mapped_debug_names::namei_to_name
976ca316 5537 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5538{
5539 const ULONGEST namei_string_offs
5540 = extract_unsigned_integer ((name_table_string_offs_reordered
5541 + namei * offset_size),
5542 offset_size,
5543 dwarf5_byte_order);
976ca316 5544 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5545}
5546
5547/* Find a slot in .debug_names for the object named NAME. If NAME is
5548 found, return pointer to its pool data. If NAME cannot be found,
5549 return NULL. */
5550
5551const gdb_byte *
5552dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5553 (const mapped_debug_names &map, const char *name,
5554 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5555{
5556 int (*cmp) (const char *, const char *);
5557
54ee4252 5558 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5559 if (current_language->la_language == language_cplus
5560 || current_language->la_language == language_fortran
5561 || current_language->la_language == language_d)
5562 {
5563 /* NAME is already canonical. Drop any qualifiers as
5564 .debug_names does not contain any. */
5565
5566 if (strchr (name, '(') != NULL)
5567 {
54ee4252 5568 without_params = cp_remove_params (name);
927aa2e7 5569 if (without_params != NULL)
54ee4252 5570 name = without_params.get ();
927aa2e7
JK
5571 }
5572 }
5573
5574 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5575
5576 const uint32_t full_hash = dwarf5_djb_hash (name);
5577 uint32_t namei
5578 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5579 (map.bucket_table_reordered
5580 + (full_hash % map.bucket_count)), 4,
5581 map.dwarf5_byte_order);
5582 if (namei == 0)
5583 return NULL;
5584 --namei;
5585 if (namei >= map.name_count)
5586 {
b98664d3 5587 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5588 "[in module %s]"),
5589 namei, map.name_count,
fcf23d5b 5590 objfile_name (per_objfile->objfile));
927aa2e7
JK
5591 return NULL;
5592 }
5593
5594 for (;;)
5595 {
5596 const uint32_t namei_full_hash
5597 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5598 (map.hash_table_reordered + namei), 4,
5599 map.dwarf5_byte_order);
5600 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5601 return NULL;
5602
5603 if (full_hash == namei_full_hash)
5604 {
fcf23d5b 5605 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5606
5607#if 0 /* An expensive sanity check. */
5608 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5609 {
b98664d3 5610 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5611 "[in module %s]"),
5612 namei, objfile_name (dwarf2_per_objfile->objfile));
5613 return NULL;
5614 }
5615#endif
5616
5617 if (cmp (namei_string, name) == 0)
5618 {
5619 const ULONGEST namei_entry_offs
5620 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5621 + namei * map.offset_size),
5622 map.offset_size, map.dwarf5_byte_order);
5623 return map.entry_pool + namei_entry_offs;
5624 }
5625 }
5626
5627 ++namei;
5628 if (namei >= map.name_count)
5629 return NULL;
5630 }
5631}
5632
5633const gdb_byte *
5634dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5635 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5636{
5637 if (namei >= map.name_count)
5638 {
b98664d3 5639 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5640 "[in module %s]"),
5641 namei, map.name_count,
fcf23d5b 5642 objfile_name (per_objfile->objfile));
927aa2e7
JK
5643 return NULL;
5644 }
5645
5646 const ULONGEST namei_entry_offs
5647 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5648 + namei * map.offset_size),
5649 map.offset_size, map.dwarf5_byte_order);
5650 return map.entry_pool + namei_entry_offs;
5651}
5652
5653/* See dw2_debug_names_iterator. */
5654
5655dwarf2_per_cu_data *
5656dw2_debug_names_iterator::next ()
5657{
5658 if (m_addr == NULL)
5659 return NULL;
5660
fcf23d5b
SM
5661 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5662 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5663 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5664
5665 again:
5666
5667 unsigned int bytes_read;
5668 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5669 m_addr += bytes_read;
5670 if (abbrev == 0)
5671 return NULL;
5672
5673 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5674 if (indexval_it == m_map.abbrev_map.cend ())
5675 {
b98664d3 5676 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5677 "[in module %s]"),
ed2dc618 5678 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5679 return NULL;
5680 }
5681 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5682 enum class symbol_linkage {
5683 unknown,
5684 static_,
5685 extern_,
23c13d42 5686 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5687 dwarf2_per_cu_data *per_cu = NULL;
5688 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5689 {
5690 ULONGEST ull;
5691 switch (attr.form)
5692 {
5693 case DW_FORM_implicit_const:
5694 ull = attr.implicit_const;
5695 break;
5696 case DW_FORM_flag_present:
5697 ull = 1;
5698 break;
5699 case DW_FORM_udata:
5700 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5701 m_addr += bytes_read;
5702 break;
6dc55ce9 5703 case DW_FORM_ref4:
5704 ull = read_4_bytes (abfd, m_addr);
5705 m_addr += 4;
5706 break;
5707 case DW_FORM_ref8:
5708 ull = read_8_bytes (abfd, m_addr);
5709 m_addr += 8;
5710 break;
5711 case DW_FORM_ref_sig8:
5712 ull = read_8_bytes (abfd, m_addr);
5713 m_addr += 8;
5714 break;
927aa2e7 5715 default:
b98664d3 5716 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5717 dwarf_form_name (attr.form),
ed2dc618 5718 objfile_name (objfile));
927aa2e7
JK
5719 return NULL;
5720 }
5721 switch (attr.dw_idx)
5722 {
5723 case DW_IDX_compile_unit:
5724 /* Don't crash on bad data. */
fcf23d5b 5725 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5726 {
b98664d3 5727 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5728 " [in module %s]"),
5729 pulongest (ull),
fcf23d5b 5730 objfile_name (objfile));
927aa2e7
JK
5731 continue;
5732 }
fcf23d5b 5733 per_cu = per_bfd->get_cutu (ull);
927aa2e7 5734 break;
8af5c486
JK
5735 case DW_IDX_type_unit:
5736 /* Don't crash on bad data. */
fcf23d5b 5737 if (ull >= per_bfd->all_type_units.size ())
8af5c486 5738 {
b98664d3 5739 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5740 " [in module %s]"),
5741 pulongest (ull),
fcf23d5b 5742 objfile_name (objfile));
8af5c486
JK
5743 continue;
5744 }
fcf23d5b 5745 per_cu = &per_bfd->get_tu (ull)->per_cu;
8af5c486 5746 break;
6dc55ce9 5747 case DW_IDX_die_offset:
5748 /* In a per-CU index (as opposed to a per-module index), index
5749 entries without CU attribute implicitly refer to the single CU. */
5750 if (per_cu == NULL)
fcf23d5b 5751 per_cu = per_bfd->get_cu (0);
6dc55ce9 5752 break;
927aa2e7
JK
5753 case DW_IDX_GNU_internal:
5754 if (!m_map.augmentation_is_gdb)
5755 break;
23c13d42 5756 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5757 break;
5758 case DW_IDX_GNU_external:
5759 if (!m_map.augmentation_is_gdb)
5760 break;
23c13d42 5761 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5762 break;
5763 }
5764 }
5765
5766 /* Skip if already read in. */
fcf23d5b 5767 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5768 goto again;
5769
5770 /* Check static vs global. */
23c13d42 5771 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5772 {
2b79f376 5773 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5774 const bool symbol_is_static =
5775 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5776 if (want_static != symbol_is_static)
2b79f376 5777 goto again;
927aa2e7
JK
5778 }
5779
5780 /* Match dw2_symtab_iter_next, symbol_kind
5781 and debug_names::psymbol_tag. */
5782 switch (m_domain)
5783 {
5784 case VAR_DOMAIN:
5785 switch (indexval.dwarf_tag)
5786 {
5787 case DW_TAG_variable:
5788 case DW_TAG_subprogram:
5789 /* Some types are also in VAR_DOMAIN. */
5790 case DW_TAG_typedef:
5791 case DW_TAG_structure_type:
5792 break;
5793 default:
5794 goto again;
5795 }
5796 break;
5797 case STRUCT_DOMAIN:
5798 switch (indexval.dwarf_tag)
5799 {
5800 case DW_TAG_typedef:
5801 case DW_TAG_structure_type:
5802 break;
5803 default:
5804 goto again;
5805 }
5806 break;
5807 case LABEL_DOMAIN:
5808 switch (indexval.dwarf_tag)
5809 {
5810 case 0:
5811 case DW_TAG_variable:
5812 break;
5813 default:
5814 goto again;
5815 }
5816 break;
59c35742
AB
5817 case MODULE_DOMAIN:
5818 switch (indexval.dwarf_tag)
5819 {
5820 case DW_TAG_module:
5821 break;
5822 default:
5823 goto again;
5824 }
5825 break;
927aa2e7
JK
5826 default:
5827 break;
5828 }
5829
5830 /* Match dw2_expand_symtabs_matching, symbol_kind and
5831 debug_names::psymbol_tag. */
5832 switch (m_search)
4b514bc8 5833 {
927aa2e7
JK
5834 case VARIABLES_DOMAIN:
5835 switch (indexval.dwarf_tag)
4b514bc8 5836 {
927aa2e7
JK
5837 case DW_TAG_variable:
5838 break;
5839 default:
5840 goto again;
4b514bc8 5841 }
927aa2e7
JK
5842 break;
5843 case FUNCTIONS_DOMAIN:
5844 switch (indexval.dwarf_tag)
4b514bc8 5845 {
927aa2e7
JK
5846 case DW_TAG_subprogram:
5847 break;
5848 default:
5849 goto again;
4b514bc8 5850 }
927aa2e7
JK
5851 break;
5852 case TYPES_DOMAIN:
5853 switch (indexval.dwarf_tag)
5854 {
5855 case DW_TAG_typedef:
5856 case DW_TAG_structure_type:
5857 break;
5858 default:
5859 goto again;
5860 }
5861 break;
59c35742
AB
5862 case MODULES_DOMAIN:
5863 switch (indexval.dwarf_tag)
5864 {
5865 case DW_TAG_module:
5866 break;
5867 default:
5868 goto again;
5869 }
927aa2e7
JK
5870 default:
5871 break;
4b514bc8 5872 }
927aa2e7
JK
5873
5874 return per_cu;
4b514bc8 5875}
61920122 5876
927aa2e7 5877static struct compunit_symtab *
c7f839cb 5878dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5879 const char *name, domain_enum domain)
4b514bc8 5880{
976ca316 5881 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
61920122 5882
976ca316 5883 const auto &mapp = per_objfile->per_bfd->debug_names_table;
927aa2e7 5884 if (!mapp)
61920122 5885 {
927aa2e7
JK
5886 /* index is NULL if OBJF_READNOW. */
5887 return NULL;
5888 }
5889 const auto &map = *mapp;
9291a0cd 5890
976ca316 5891 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
9703b513 5892
927aa2e7
JK
5893 struct compunit_symtab *stab_best = NULL;
5894 struct dwarf2_per_cu_data *per_cu;
5895 while ((per_cu = iter.next ()) != NULL)
5896 {
5897 struct symbol *sym, *with_opaque = NULL;
97a1449a 5898 compunit_symtab *stab
976ca316 5899 = dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7 5900 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5901 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5902
927aa2e7
JK
5903 sym = block_find_symbol (block, name, domain,
5904 block_find_non_opaque_type_preferred,
5905 &with_opaque);
9703b513 5906
927aa2e7
JK
5907 /* Some caution must be observed with overloaded functions and
5908 methods, since the index will not contain any overload
5909 information (but NAME might contain it). */
a3ec0bb1 5910
927aa2e7 5911 if (sym != NULL
987012b8 5912 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5913 return stab;
5914 if (with_opaque != NULL
987012b8 5915 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5916 stab_best = stab;
9703b513 5917
927aa2e7 5918 /* Keep looking through other CUs. */
9703b513
TT
5919 }
5920
927aa2e7 5921 return stab_best;
9703b513
TT
5922}
5923
927aa2e7
JK
5924/* This dumps minimal information about .debug_names. It is called
5925 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5926 uses this to verify that .debug_names has been loaded. */
9291a0cd 5927
927aa2e7
JK
5928static void
5929dw2_debug_names_dump (struct objfile *objfile)
5930{
976ca316 5931 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5932
976ca316 5933 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5934 printf_filtered (".debug_names:");
976ca316 5935 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5936 printf_filtered (" exists\n");
5937 else
5938 printf_filtered (" faked for \"readnow\"\n");
5939 printf_filtered ("\n");
9291a0cd
TT
5940}
5941
9291a0cd 5942static void
927aa2e7
JK
5943dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5944 const char *func_name)
9291a0cd 5945{
976ca316 5946 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 5947
976ca316
SM
5948 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5949 if (per_objfile->per_bfd->debug_names_table)
24c79950 5950 {
976ca316 5951 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
24c79950 5952
fcf23d5b 5953 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
976ca316 5954 per_objfile);
24c79950 5955
927aa2e7
JK
5956 struct dwarf2_per_cu_data *per_cu;
5957 while ((per_cu = iter.next ()) != NULL)
976ca316 5958 dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7
JK
5959 }
5960}
24c79950 5961
3b00ef10
TT
5962static void
5963dw2_debug_names_map_matching_symbols
5964 (struct objfile *objfile,
5965 const lookup_name_info &name, domain_enum domain,
5966 int global,
5967 gdb::function_view<symbol_found_callback_ftype> callback,
5968 symbol_compare_ftype *ordered_compare)
5969{
976ca316 5970 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5971
5972 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5973 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5974 return;
5975
976ca316 5976 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5977 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5978
5979 const char *match_name = name.ada ().lookup_name ().c_str ();
5980 auto matcher = [&] (const char *symname)
5981 {
5982 if (ordered_compare == nullptr)
5983 return true;
5984 return ordered_compare (symname, match_name) == 0;
5985 };
5986
5987 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5988 [&] (offset_type namei)
5989 {
5990 /* The name was matched, now expand corresponding CUs that were
5991 marked. */
fcf23d5b 5992 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
976ca316 5993 per_objfile);
3b00ef10
TT
5994
5995 struct dwarf2_per_cu_data *per_cu;
5996 while ((per_cu = iter.next ()) != NULL)
976ca316 5997 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5998 nullptr);
3b00ef10 5999 return true;
976ca316 6000 }, per_objfile);
3b00ef10
TT
6001
6002 /* It's a shame we couldn't do this inside the
6003 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6004 that have already been expanded. Instead, this loop matches what
6005 the psymtab code does. */
976ca316 6006 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3b00ef10 6007 {
976ca316 6008 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
af758d11 6009 if (symtab != nullptr)
3b00ef10
TT
6010 {
6011 const struct block *block
af758d11 6012 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
6013 if (!iterate_over_symbols_terminated (block, name,
6014 domain, callback))
6015 break;
6016 }
6017 }
6018}
6019
927aa2e7
JK
6020static void
6021dw2_debug_names_expand_symtabs_matching
6022 (struct objfile *objfile,
6023 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 6024 const lookup_name_info *lookup_name,
927aa2e7
JK
6025 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6026 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6027 enum search_domain kind)
6028{
976ca316 6029 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 6030
927aa2e7 6031 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 6032 if (!per_objfile->per_bfd->debug_names_table)
927aa2e7 6033 return;
9291a0cd 6034
976ca316 6035 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 6036
c1a66c06
TV
6037 if (symbol_matcher == NULL && lookup_name == NULL)
6038 {
976ca316 6039 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
6040 {
6041 QUIT;
6042
976ca316
SM
6043 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
6044 expansion_notify);
c1a66c06
TV
6045 }
6046 return;
6047 }
6048
976ca316 6049 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 6050
c1a66c06 6051 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
6052 symbol_matcher,
6053 kind, [&] (offset_type namei)
927aa2e7 6054 {
927aa2e7
JK
6055 /* The name was matched, now expand corresponding CUs that were
6056 marked. */
976ca316 6057 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
bbf2f4df 6058
927aa2e7
JK
6059 struct dwarf2_per_cu_data *per_cu;
6060 while ((per_cu = iter.next ()) != NULL)
976ca316
SM
6061 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
6062 expansion_notify);
3b00ef10 6063 return true;
976ca316 6064 }, per_objfile);
9291a0cd
TT
6065}
6066
927aa2e7 6067const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6068{
6069 dw2_has_symbols,
6070 dw2_find_last_source_symtab,
6071 dw2_forget_cached_source_info,
f8eba3c6 6072 dw2_map_symtabs_matching_filename,
927aa2e7 6073 dw2_debug_names_lookup_symbol,
d3214198 6074 NULL,
9291a0cd 6075 dw2_print_stats,
927aa2e7 6076 dw2_debug_names_dump,
927aa2e7 6077 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6078 dw2_expand_all_symtabs,
652a8996 6079 dw2_expand_symtabs_with_fullname,
3b00ef10 6080 dw2_debug_names_map_matching_symbols,
927aa2e7 6081 dw2_debug_names_expand_symtabs_matching,
43f3e411 6082 dw2_find_pc_sect_compunit_symtab,
71a3c369 6083 NULL,
9291a0cd
TT
6084 dw2_map_symbol_filenames
6085};
6086
4485a1c1 6087/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 6088 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
6089
6090template <typename T>
6091static gdb::array_view<const gdb_byte>
6092get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6093{
6094 dwarf2_section_info *section = &section_owner->gdb_index;
6095
96b79293 6096 if (section->empty ())
4485a1c1
SM
6097 return {};
6098
6099 /* Older elfutils strip versions could keep the section in the main
6100 executable while splitting it for the separate debug info file. */
96b79293 6101 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
6102 return {};
6103
96b79293 6104 section->read (obj);
4485a1c1 6105
8bebfcda
PA
6106 /* dwarf2_section_info::size is a bfd_size_type, while
6107 gdb::array_view works with size_t. On 32-bit hosts, with
6108 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6109 is 32-bit. So we need an explicit narrowing conversion here.
6110 This is fine, because it's impossible to allocate or mmap an
6111 array/buffer larger than what size_t can represent. */
6112 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6113}
6114
87d6a7aa
SM
6115/* Lookup the index cache for the contents of the index associated to
6116 DWARF2_OBJ. */
6117
6118static gdb::array_view<const gdb_byte>
5989a64e 6119get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
6120{
6121 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6122 if (build_id == nullptr)
6123 return {};
6124
6125 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 6126 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
6127}
6128
6129/* Same as the above, but for DWZ. */
6130
6131static gdb::array_view<const gdb_byte>
6132get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6133{
6134 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6135 if (build_id == nullptr)
6136 return {};
6137
6138 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6139}
6140
3c0aa29a 6141/* See symfile.h. */
9291a0cd 6142
3c0aa29a
PA
6143bool
6144dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6145{
976ca316
SM
6146 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6147 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 6148
9291a0cd
TT
6149 /* If we're about to read full symbols, don't bother with the
6150 indices. In this case we also don't care if some other debug
6151 format is making psymtabs, because they are all about to be
6152 expanded anyway. */
6153 if ((objfile->flags & OBJF_READNOW))
6154 {
17ee85fc
TT
6155 /* When using READNOW, the using_index flag (set below) indicates that
6156 PER_BFD was already initialized, when we loaded some other objfile. */
6157 if (per_bfd->using_index)
6158 {
6159 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6160 per_objfile->resize_symtabs ();
17ee85fc
TT
6161 return true;
6162 }
6163
6164 per_bfd->using_index = 1;
976ca316
SM
6165 create_all_comp_units (per_objfile);
6166 create_all_type_units (per_objfile);
17ee85fc
TT
6167 per_bfd->quick_file_names_table
6168 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 6169 per_objfile->resize_symtabs ();
9291a0cd 6170
17ee85fc
TT
6171 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6172 + per_bfd->all_type_units.size ()); ++i)
9291a0cd 6173 {
17ee85fc 6174 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
9291a0cd 6175
17ee85fc 6176 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 6177 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6178 }
6179
6180 /* Return 1 so that gdb sees the "quick" functions. However,
6181 these functions will be no-ops because we will have expanded
6182 all symtabs. */
3c0aa29a
PA
6183 *index_kind = dw_index_kind::GDB_INDEX;
6184 return true;
9291a0cd
TT
6185 }
6186
17ee85fc
TT
6187 /* Was a debug names index already read when we processed an objfile sharing
6188 PER_BFD? */
6189 if (per_bfd->debug_names_table != nullptr)
6190 {
6191 *index_kind = dw_index_kind::DEBUG_NAMES;
f8c41851 6192 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6193 per_objfile->resize_symtabs ();
17ee85fc
TT
6194 return true;
6195 }
6196
6197 /* Was a GDB index already read when we processed an objfile sharing
6198 PER_BFD? */
6199 if (per_bfd->index_table != nullptr)
6200 {
6201 *index_kind = dw_index_kind::GDB_INDEX;
f8c41851 6202 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6203 per_objfile->resize_symtabs ();
17ee85fc
TT
6204 return true;
6205 }
6206
efb763a5
SM
6207 /* There might already be partial symtabs built for this BFD. This happens
6208 when loading the same binary twice with the index-cache enabled. If so,
6209 don't try to read an index. The objfile / per_objfile initialization will
6210 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6211 code path. */
6212 if (per_bfd->partial_symtabs != nullptr)
6213 return false;
6214
976ca316 6215 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a
PA
6216 {
6217 *index_kind = dw_index_kind::DEBUG_NAMES;
976ca316 6218 per_objfile->resize_symtabs ();
3c0aa29a
PA
6219 return true;
6220 }
927aa2e7 6221
976ca316 6222 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 6223 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 6224 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6225 {
6226 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6227 per_objfile->resize_symtabs ();
3c0aa29a
PA
6228 return true;
6229 }
9291a0cd 6230
87d6a7aa 6231 /* ... otherwise, try to find the index in the index cache. */
976ca316 6232 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
6233 get_gdb_index_contents_from_cache,
6234 get_gdb_index_contents_from_cache_dwz))
6235 {
6236 global_index_cache.hit ();
6237 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6238 per_objfile->resize_symtabs ();
87d6a7aa
SM
6239 return true;
6240 }
6241
6242 global_index_cache.miss ();
3c0aa29a 6243 return false;
9291a0cd
TT
6244}
6245
6246\f
6247
dce234bc
PP
6248/* Build a partial symbol table. */
6249
6250void
f29dff0a 6251dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6252{
976ca316
SM
6253 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6254 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
6255
6256 if (per_bfd->partial_symtabs != nullptr)
6257 {
6258 /* Partial symbols were already read, so now we can simply
6259 attach them. */
6260 objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6261 per_objfile->resize_symtabs ();
17ee85fc
TT
6262 return;
6263 }
c9bf0622 6264
a70b8144 6265 try
c9bf0622
TT
6266 {
6267 /* This isn't really ideal: all the data we allocate on the
6268 objfile's obstack is still uselessly kept around. However,
6269 freeing it seems unsafe. */
906768f9 6270 psymtab_discarder psymtabs (objfile);
976ca316 6271 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 6272 psymtabs.keep ();
87d6a7aa 6273
976ca316 6274 per_objfile->resize_symtabs ();
af758d11 6275
87d6a7aa 6276 /* (maybe) store an index in the cache. */
976ca316 6277 global_index_cache.store (per_objfile);
c9bf0622 6278 }
230d2906 6279 catch (const gdb_exception_error &except)
492d29ea
PA
6280 {
6281 exception_print (gdb_stderr, except);
6282 }
17ee85fc
TT
6283
6284 /* Finish by setting the local reference to partial symtabs, so that
6285 we don't try to read them again if reading another objfile with the same
6286 BFD. If we can't in fact share, this won't make a difference anyway as
6287 the dwarf2_per_bfd object won't be shared. */
6288 per_bfd->partial_symtabs = objfile->partial_symtabs;
c906108c 6289}
c906108c 6290
3b80fe9b
DE
6291/* Find the base address of the compilation unit for range lists and
6292 location lists. It will normally be specified by DW_AT_low_pc.
6293 In DWARF-3 draft 4, the base address could be overridden by
6294 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6295 compilation units with discontinuous ranges. */
6296
6297static void
6298dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6299{
6300 struct attribute *attr;
6301
2b24b6e4 6302 cu->base_address.reset ();
3b80fe9b
DE
6303
6304 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6305 if (attr != nullptr)
95f982e5 6306 cu->base_address = attr->as_address ();
3b80fe9b
DE
6307 else
6308 {
6309 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6310 if (attr != nullptr)
95f982e5 6311 cu->base_address = attr->as_address ();
3b80fe9b
DE
6312 }
6313}
6314
36586728
TT
6315/* Helper function that returns the proper abbrev section for
6316 THIS_CU. */
6317
6318static struct dwarf2_section_info *
6319get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6320{
6321 struct dwarf2_section_info *abbrev;
c3699833 6322 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6323
6324 if (this_cu->is_dwz)
c3699833 6325 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6326 else
c3699833 6327 abbrev = &per_bfd->abbrev;
36586728
TT
6328
6329 return abbrev;
6330}
6331
f4dc4d17
DE
6332/* Fetch the abbreviation table offset from a comp or type unit header. */
6333
6334static sect_offset
976ca316 6335read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 6336 struct dwarf2_section_info *section,
9c541725 6337 sect_offset sect_off)
f4dc4d17 6338{
96b79293 6339 bfd *abfd = section->get_bfd_owner ();
d521ce57 6340 const gdb_byte *info_ptr;
ac298888 6341 unsigned int initial_length_size, offset_size;
43988095 6342 uint16_t version;
f4dc4d17 6343
976ca316 6344 section->read (per_objfile->objfile);
9c541725 6345 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6346 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6347 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6348 info_ptr += initial_length_size;
6349
6350 version = read_2_bytes (abfd, info_ptr);
6351 info_ptr += 2;
6352 if (version >= 5)
6353 {
6354 /* Skip unit type and address size. */
6355 info_ptr += 2;
6356 }
6357
24aa364d 6358 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6359}
6360
b83470bf
TT
6361/* A partial symtab that is used only for include files. */
6362struct dwarf2_include_psymtab : public partial_symtab
6363{
6364 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6365 : partial_symtab (filename, objfile)
6366 {
6367 }
6368
6369 void read_symtab (struct objfile *objfile) override
6370 {
194d088f
TV
6371 /* It's an include file, no symbols to read for it.
6372 Everything is in the includer symtab. */
6373
6374 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6375 expansion of the includer psymtab. We use the dependencies[0] field to
6376 model the includer. But if we go the regular route of calling
6377 expand_psymtab here, and having expand_psymtab call expand_dependencies
6378 to expand the includer, we'll only use expand_psymtab on the includer
6379 (making it a non-toplevel psymtab), while if we expand the includer via
6380 another path, we'll use read_symtab (making it a toplevel psymtab).
6381 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6382 psymtab, and trigger read_symtab on the includer here directly. */
6383 includer ()->read_symtab (objfile);
b83470bf
TT
6384 }
6385
6386 void expand_psymtab (struct objfile *objfile) override
6387 {
194d088f
TV
6388 /* This is not called by read_symtab, and should not be called by any
6389 expand_dependencies. */
6390 gdb_assert (false);
b83470bf
TT
6391 }
6392
5717c425 6393 bool readin_p (struct objfile *objfile) const override
b83470bf 6394 {
5717c425 6395 return includer ()->readin_p (objfile);
b83470bf
TT
6396 }
6397
5717c425 6398 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6399 {
6400 return nullptr;
6401 }
6402
6403private:
194d088f
TV
6404 partial_symtab *includer () const
6405 {
6406 /* An include psymtab has exactly one dependency: the psymtab that
6407 includes it. */
6408 gdb_assert (this->number_of_dependencies == 1);
6409 return this->dependencies[0];
6410 }
b83470bf
TT
6411};
6412
aaa75496
JB
6413/* Allocate a new partial symtab for file named NAME and mark this new
6414 partial symtab as being an include of PST. */
6415
6416static void
891813be 6417dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
dda83cd7 6418 struct objfile *objfile)
aaa75496 6419{
b83470bf 6420 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6421
fbd9ab74 6422 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6423 subpst->dirname = pst->dirname;
fbd9ab74 6424
a9342b62 6425 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6426 subpst->dependencies[0] = pst;
6427 subpst->number_of_dependencies = 1;
aaa75496
JB
6428}
6429
6430/* Read the Line Number Program data and extract the list of files
6431 included by the source file represented by PST. Build an include
d85a05f0 6432 partial symtab for each of these included files. */
aaa75496
JB
6433
6434static void
6435dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6436 struct die_info *die,
891813be 6437 dwarf2_psymtab *pst)
aaa75496 6438{
fff8551c 6439 line_header_up lh;
d85a05f0 6440 struct attribute *attr;
aaa75496 6441
d85a05f0 6442 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e
TT
6443 if (attr != nullptr && attr->form_is_unsigned ())
6444 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
aaa75496
JB
6445 if (lh == NULL)
6446 return; /* No linetable, so no includes. */
6447
79748972
TT
6448 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6449 that we pass in the raw text_low here; that is ok because we're
6450 only decoding the line table to make include partial symtabs, and
6451 so the addresses aren't really used. */
4ae976d1 6452 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6453 pst->raw_text_low (), 1);
aaa75496
JB
6454}
6455
348e048f 6456static hashval_t
52dc124a 6457hash_signatured_type (const void *item)
348e048f 6458{
9a3c8263
SM
6459 const struct signatured_type *sig_type
6460 = (const struct signatured_type *) item;
9a619af0 6461
348e048f 6462 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6463 return sig_type->signature;
348e048f
DE
6464}
6465
6466static int
52dc124a 6467eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6468{
9a3c8263
SM
6469 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6470 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6471
348e048f
DE
6472 return lhs->signature == rhs->signature;
6473}
6474
1fd400ff
TT
6475/* Allocate a hash table for signatured types. */
6476
b0b6a987 6477static htab_up
298e9637 6478allocate_signatured_type_table ()
1fd400ff 6479{
b0b6a987
TT
6480 return htab_up (htab_create_alloc (41,
6481 hash_signatured_type,
6482 eq_signatured_type,
6483 NULL, xcalloc, xfree));
1fd400ff
TT
6484}
6485
d467dd73 6486/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6487
6488static int
d467dd73 6489add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6490{
9a3c8263 6491 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6492 std::vector<signatured_type *> *all_type_units
6493 = (std::vector<signatured_type *> *) datum;
1fd400ff 6494
b2bdb8cf 6495 all_type_units->push_back (sigt);
1fd400ff
TT
6496
6497 return 1;
6498}
6499
78d4d2c5 6500/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6501 and fill them into TYPES_HTAB. It will process only type units,
6502 therefore DW_UT_type. */
c88ee1f0 6503
78d4d2c5 6504static void
976ca316 6505create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6506 struct dwo_file *dwo_file,
b0b6a987 6507 dwarf2_section_info *section, htab_up &types_htab,
43988095 6508 rcuh_kind section_kind)
348e048f 6509{
976ca316 6510 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6511 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6512 bfd *abfd;
6513 const gdb_byte *info_ptr, *end_ptr;
348e048f 6514
4bdcc0c1
DE
6515 abbrev_section = (dwo_file != NULL
6516 ? &dwo_file->sections.abbrev
976ca316 6517 : &per_objfile->per_bfd->abbrev);
4bdcc0c1 6518
6f738b01
SM
6519 dwarf_read_debug_printf ("Reading %s for %s:",
6520 section->get_name (),
6521 abbrev_section->get_file_name ());
09406207 6522
96b79293 6523 section->read (objfile);
78d4d2c5 6524 info_ptr = section->buffer;
348e048f 6525
78d4d2c5
JK
6526 if (info_ptr == NULL)
6527 return;
348e048f 6528
78d4d2c5
JK
6529 /* We can't set abfd until now because the section may be empty or
6530 not present, in which case the bfd is unknown. */
96b79293 6531 abfd = section->get_bfd_owner ();
348e048f 6532
c0ab21c2
TT
6533 /* We don't use cutu_reader here because we don't need to read
6534 any dies: the signature is in the header. */
3019eac3 6535
78d4d2c5
JK
6536 end_ptr = info_ptr + section->size;
6537 while (info_ptr < end_ptr)
6538 {
78d4d2c5
JK
6539 struct signatured_type *sig_type;
6540 struct dwo_unit *dwo_tu;
6541 void **slot;
6542 const gdb_byte *ptr = info_ptr;
6543 struct comp_unit_head header;
6544 unsigned int length;
8b70b953 6545
9c541725 6546 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6547
a49dd8dd
JK
6548 /* Initialize it due to a false compiler warning. */
6549 header.signature = -1;
9c541725 6550 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6551
78d4d2c5
JK
6552 /* We need to read the type's signature in order to build the hash
6553 table, but we don't need anything else just yet. */
348e048f 6554
976ca316 6555 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6556 abbrev_section, ptr, section_kind);
348e048f 6557
4057dfde 6558 length = header.get_length ();
6caca83c 6559
78d4d2c5
JK
6560 /* Skip dummy type units. */
6561 if (ptr >= info_ptr + length
43988095 6562 || peek_abbrev_code (abfd, ptr) == 0
d2854d8d
CT
6563 || (header.unit_type != DW_UT_type
6564 && header.unit_type != DW_UT_split_type))
78d4d2c5
JK
6565 {
6566 info_ptr += length;
6567 continue;
6568 }
dee91e82 6569
78d4d2c5
JK
6570 if (types_htab == NULL)
6571 {
6572 if (dwo_file)
298e9637 6573 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6574 else
298e9637 6575 types_htab = allocate_signatured_type_table ();
78d4d2c5 6576 }
8b70b953 6577
78d4d2c5
JK
6578 if (dwo_file)
6579 {
6580 sig_type = NULL;
976ca316 6581 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
78d4d2c5 6582 dwo_tu->dwo_file = dwo_file;
43988095 6583 dwo_tu->signature = header.signature;
9c541725 6584 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6585 dwo_tu->section = section;
9c541725 6586 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6587 dwo_tu->length = length;
6588 }
6589 else
6590 {
6591 /* N.B.: type_offset is not usable if this type uses a DWO file.
6592 The real type_offset is in the DWO file. */
6593 dwo_tu = NULL;
976ca316 6594 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
43988095 6595 sig_type->signature = header.signature;
9c541725 6596 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6597 sig_type->per_cu.is_debug_types = 1;
6598 sig_type->per_cu.section = section;
9c541725 6599 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6600 sig_type->per_cu.length = length;
6601 }
6602
b0b6a987 6603 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6604 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6605 INSERT);
6606 gdb_assert (slot != NULL);
6607 if (*slot != NULL)
6608 {
9c541725 6609 sect_offset dup_sect_off;
0349ea22 6610
3019eac3
DE
6611 if (dwo_file)
6612 {
78d4d2c5
JK
6613 const struct dwo_unit *dup_tu
6614 = (const struct dwo_unit *) *slot;
6615
9c541725 6616 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6617 }
6618 else
6619 {
78d4d2c5
JK
6620 const struct signatured_type *dup_tu
6621 = (const struct signatured_type *) *slot;
6622
9c541725 6623 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6624 }
8b70b953 6625
b98664d3 6626 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6627 " the entry at offset %s, signature %s"),
6628 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6629 hex_string (header.signature));
78d4d2c5
JK
6630 }
6631 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6632
6f738b01
SM
6633 dwarf_read_debug_printf_v (" offset %s, signature %s",
6634 sect_offset_str (sect_off),
6635 hex_string (header.signature));
3019eac3 6636
78d4d2c5
JK
6637 info_ptr += length;
6638 }
6639}
3019eac3 6640
78d4d2c5
JK
6641/* Create the hash table of all entries in the .debug_types
6642 (or .debug_types.dwo) section(s).
6643 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6644 otherwise it is NULL.
b3c8eb43 6645
78d4d2c5 6646 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6647
78d4d2c5 6648 Note: This function processes DWO files only, not DWP files. */
348e048f 6649
78d4d2c5 6650static void
976ca316 6651create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6652 struct dwo_file *dwo_file,
fd5866f6 6653 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6654 htab_up &types_htab)
78d4d2c5 6655{
fd5866f6 6656 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6657 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6658 rcuh_kind::TYPE);
3019eac3
DE
6659}
6660
6661/* Create the hash table of all entries in the .debug_types section,
6662 and initialize all_type_units.
6663 The result is zero if there is an error (e.g. missing .debug_types section),
6664 otherwise non-zero. */
6665
6666static int
976ca316 6667create_all_type_units (dwarf2_per_objfile *per_objfile)
3019eac3 6668{
b0b6a987 6669 htab_up types_htab;
3019eac3 6670
976ca316
SM
6671 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6672 types_htab, rcuh_kind::COMPILE);
6673 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6674 types_htab);
3019eac3
DE
6675 if (types_htab == NULL)
6676 {
976ca316 6677 per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6678 return 0;
6679 }
6680
976ca316 6681 per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6682
976ca316
SM
6683 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6684 per_objfile->per_bfd->all_type_units.reserve
6685 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6686
976ca316 6687 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6688 add_signatured_type_cu_to_table,
976ca316 6689 &per_objfile->per_bfd->all_type_units);
1fd400ff 6690
348e048f
DE
6691 return 1;
6692}
6693
5989a64e 6694/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6695 If SLOT is non-NULL, it is the entry to use in the hash table.
6696 Otherwise we find one. */
6697
6698static struct signatured_type *
976ca316 6699add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6700{
976ca316
SM
6701 if (per_objfile->per_bfd->all_type_units.size ()
6702 == per_objfile->per_bfd->all_type_units.capacity ())
6703 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6704
976ca316 6705 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6706
976ca316 6707 per_objfile->resize_symtabs ();
af758d11 6708
976ca316 6709 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6710 sig_type->signature = sig;
6711 sig_type->per_cu.is_debug_types = 1;
976ca316 6712 if (per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6713 {
6714 sig_type->per_cu.v.quick =
976ca316 6715 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6716 struct dwarf2_per_cu_quick_data);
6717 }
6718
6719 if (slot == NULL)
6720 {
976ca316 6721 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6722 sig_type, INSERT);
6723 }
6724 gdb_assert (*slot == NULL);
6725 *slot = sig_type;
6726 /* The rest of sig_type must be filled in by the caller. */
6727 return sig_type;
6728}
6729
a2ce51a0
DE
6730/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6731 Fill in SIG_ENTRY with DWO_ENTRY. */
6732
6733static void
976ca316 6734fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6735 struct signatured_type *sig_entry,
6736 struct dwo_unit *dwo_entry)
6737{
976ca316 6738 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6739
7ee85ab1 6740 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6741 gdb_assert (! sig_entry->per_cu.queued);
976ca316 6742 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6743 if (per_bfd->using_index)
6aa5f3a6
DE
6744 {
6745 gdb_assert (sig_entry->per_cu.v.quick != NULL);
976ca316 6746 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6747 }
6748 else
6749 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6750 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6751 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6752 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6753 gdb_assert (sig_entry->dwo_unit == NULL);
6754
6755 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6756 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6757 sig_entry->per_cu.length = dwo_entry->length;
6758 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6759 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6760 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6761 sig_entry->dwo_unit = dwo_entry;
6762}
6763
6764/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6765 If we haven't read the TU yet, create the signatured_type data structure
6766 for a TU to be read in directly from a DWO file, bypassing the stub.
6767 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6768 using .gdb_index, then when reading a CU we want to stay in the DWO file
6769 containing that CU. Otherwise we could end up reading several other DWO
6770 files (due to comdat folding) to process the transitive closure of all the
6771 mentioned TUs, and that can be slow. The current DWO file will have every
6772 type signature that it needs.
a2ce51a0
DE
6773 We only do this for .gdb_index because in the psymtab case we already have
6774 to read all the DWOs to build the type unit groups. */
6775
6776static struct signatured_type *
6777lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6778{
976ca316 6779 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6780 struct dwo_file *dwo_file;
6781 struct dwo_unit find_dwo_entry, *dwo_entry;
6782 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6783 void **slot;
a2ce51a0 6784
976ca316 6785 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6786
6aa5f3a6
DE
6787 /* If TU skeletons have been removed then we may not have read in any
6788 TUs yet. */
976ca316
SM
6789 if (per_objfile->per_bfd->signatured_types == NULL)
6790 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6791
6792 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6793 Use the global signatured_types array to do our own comdat-folding
6794 of types. If this is the first time we're reading this TU, and
6795 the TU has an entry in .gdb_index, replace the recorded data from
6796 .gdb_index with this TU. */
a2ce51a0 6797
a2ce51a0 6798 find_sig_entry.signature = sig;
976ca316 6799 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6800 &find_sig_entry, INSERT);
9a3c8263 6801 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6802
6803 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6804 read. Don't reassign the global entry to point to this DWO if that's
6805 the case. Also note that if the TU is already being read, it may not
6806 have come from a DWO, the program may be a mix of Fission-compiled
6807 code and non-Fission-compiled code. */
6808
6809 /* Have we already tried to read this TU?
6810 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6811 needn't exist in the global table yet). */
6812 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6813 return sig_entry;
6814
6aa5f3a6
DE
6815 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6816 dwo_unit of the TU itself. */
6817 dwo_file = cu->dwo_unit->dwo_file;
6818
a2ce51a0
DE
6819 /* Ok, this is the first time we're reading this TU. */
6820 if (dwo_file->tus == NULL)
6821 return NULL;
6822 find_dwo_entry.signature = sig;
b0b6a987
TT
6823 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6824 &find_dwo_entry);
a2ce51a0
DE
6825 if (dwo_entry == NULL)
6826 return NULL;
6827
6aa5f3a6
DE
6828 /* If the global table doesn't have an entry for this TU, add one. */
6829 if (sig_entry == NULL)
976ca316 6830 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6831
976ca316 6832 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
89e63ee4 6833 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6834 return sig_entry;
6835}
6836
a2ce51a0
DE
6837/* Subroutine of lookup_signatured_type.
6838 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6839 then try the DWP file. If the TU stub (skeleton) has been removed then
6840 it won't be in .gdb_index. */
a2ce51a0
DE
6841
6842static struct signatured_type *
6843lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6844{
976ca316
SM
6845 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6846 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6847 struct dwo_unit *dwo_entry;
6848 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6849 void **slot;
a2ce51a0 6850
976ca316 6851 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6852 gdb_assert (dwp_file != NULL);
6853
6aa5f3a6
DE
6854 /* If TU skeletons have been removed then we may not have read in any
6855 TUs yet. */
976ca316
SM
6856 if (per_objfile->per_bfd->signatured_types == NULL)
6857 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6858
6aa5f3a6 6859 find_sig_entry.signature = sig;
976ca316 6860 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6861 &find_sig_entry, INSERT);
9a3c8263 6862 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6863
6864 /* Have we already tried to read this TU?
6865 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6866 needn't exist in the global table yet). */
6867 if (sig_entry != NULL)
6868 return sig_entry;
6869
a2ce51a0
DE
6870 if (dwp_file->tus == NULL)
6871 return NULL;
976ca316
SM
6872 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6873 1 /* is_debug_types */);
a2ce51a0
DE
6874 if (dwo_entry == NULL)
6875 return NULL;
6876
976ca316
SM
6877 sig_entry = add_type_unit (per_objfile, sig, slot);
6878 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6879
a2ce51a0
DE
6880 return sig_entry;
6881}
6882
380bca97 6883/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6884 Returns NULL if signature SIG is not present in the table.
6885 It is up to the caller to complain about this. */
348e048f
DE
6886
6887static struct signatured_type *
a2ce51a0 6888lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6889{
976ca316 6890 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6891
976ca316 6892 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6893 {
6894 /* We're in a DWO/DWP file, and we're using .gdb_index.
6895 These cases require special processing. */
976ca316 6896 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6897 return lookup_dwo_signatured_type (cu, sig);
6898 else
6899 return lookup_dwp_signatured_type (cu, sig);
6900 }
6901 else
6902 {
6903 struct signatured_type find_entry, *entry;
348e048f 6904
976ca316 6905 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6906 return NULL;
6907 find_entry.signature = sig;
9a3c8263 6908 entry = ((struct signatured_type *)
976ca316 6909 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6910 &find_entry));
a2ce51a0
DE
6911 return entry;
6912 }
348e048f 6913}
18a8505e 6914
42e7ad6c 6915/* Low level DIE reading support. */
348e048f 6916
d85a05f0
DJ
6917/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6918
6919static void
6920init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6921 struct dwarf2_cu *cu,
3019eac3 6922 struct dwarf2_section_info *section,
685af9cd
TT
6923 struct dwo_file *dwo_file,
6924 struct abbrev_table *abbrev_table)
d85a05f0 6925{
fceca515 6926 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6927 reader->abfd = section->get_bfd_owner ();
d85a05f0 6928 reader->cu = cu;
3019eac3 6929 reader->dwo_file = dwo_file;
dee91e82
DE
6930 reader->die_section = section;
6931 reader->buffer = section->buffer;
f664829e 6932 reader->buffer_end = section->buffer + section->size;
685af9cd 6933 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6934}
6935
c0ab21c2 6936/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6937 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6938 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6939 already.
6940
6941 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6942 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6943 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6944 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6945 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6946 STUB_COMP_DIR may be non-NULL.
3e225074 6947 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6948 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6949 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6950 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6951 kept around for at least as long as *RESULT_READER.
6952
b0c7bfa9
DE
6953 The result is non-zero if a valid (non-dummy) DIE was found. */
6954
6955static int
4ab09049 6956read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6957 struct dwo_unit *dwo_unit,
b0c7bfa9 6958 struct die_info *stub_comp_unit_die,
a2ce51a0 6959 const char *stub_comp_dir,
b0c7bfa9 6960 struct die_reader_specs *result_reader,
d521ce57 6961 const gdb_byte **result_info_ptr,
b0c7bfa9 6962 struct die_info **result_comp_unit_die,
685af9cd 6963 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6964{
976ca316 6965 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6966 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6967 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6968 bfd *abfd;
d521ce57 6969 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6970 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6971 int i,num_extra_attrs;
6972 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6973 struct die_info *comp_unit_die;
6974
b0aeadb3
DE
6975 /* At most one of these may be provided. */
6976 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6977
b0c7bfa9
DE
6978 /* These attributes aren't processed until later:
6979 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6980 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6981 referenced later. However, these attributes are found in the stub
6982 which we won't have later. In order to not impose this complication
6983 on the rest of the code, we read them here and copy them to the
6984 DWO CU/TU die. */
b0c7bfa9
DE
6985
6986 stmt_list = NULL;
6987 low_pc = NULL;
6988 high_pc = NULL;
6989 ranges = NULL;
6990 comp_dir = NULL;
6991
6992 if (stub_comp_unit_die != NULL)
6993 {
6994 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6995 DWO file. */
4ab09049 6996 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6997 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6998 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6999 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7000 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7001 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7002
a39fdb41 7003 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 7004
2b0c7f41
SM
7005 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
7006 We need the value before we can process DW_AT_ranges values from the
7007 DWO. */
7008 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
7009
7010 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
7011 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
7012 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
7013 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
7014 section. */
7015 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
b0c7bfa9 7016 }
a2ce51a0
DE
7017 else if (stub_comp_dir != NULL)
7018 {
7019 /* Reconstruct the comp_dir attribute to simplify the code below. */
fe56917a 7020 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7021 comp_dir->name = DW_AT_comp_dir;
7022 comp_dir->form = DW_FORM_string;
c6481205 7023 comp_dir->set_string_noncanonical (stub_comp_dir);
a2ce51a0 7024 }
b0c7bfa9
DE
7025
7026 /* Set up for reading the DWO CU/TU. */
7027 cu->dwo_unit = dwo_unit;
685af9cd 7028 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
7029 section->read (objfile);
7030 abfd = section->get_bfd_owner ();
9c541725
PA
7031 begin_info_ptr = info_ptr = (section->buffer
7032 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7033 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 7034
4ab09049 7035 if (per_cu->is_debug_types)
b0c7bfa9 7036 {
4ab09049 7037 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 7038
976ca316
SM
7039 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7040 section, dwo_abbrev_section,
43988095 7041 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7042 /* This is not an assert because it can be caused by bad debug info. */
43988095 7043 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7044 {
7045 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7046 " TU at offset %s [in module %s]"),
a2ce51a0 7047 hex_string (sig_type->signature),
43988095 7048 hex_string (cu->header.signature),
9d8780f0 7049 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7050 bfd_get_filename (abfd));
7051 }
9c541725 7052 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7053 /* For DWOs coming from DWP files, we don't know the CU length
7054 nor the type's offset in the TU until now. */
4057dfde 7055 dwo_unit->length = cu->header.get_length ();
9c541725 7056 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7057
7058 /* Establish the type offset that can be used to lookup the type.
7059 For DWO files, we don't know it until now. */
9c541725
PA
7060 sig_type->type_offset_in_section
7061 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7062 }
7063 else
7064 {
976ca316
SM
7065 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7066 section, dwo_abbrev_section,
43988095 7067 info_ptr, rcuh_kind::COMPILE);
9c541725 7068 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7069 /* For DWOs coming from DWP files, we don't know the CU length
7070 until now. */
4057dfde 7071 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
7072 }
7073
606decb2 7074 dwo_abbrev_section->read (objfile);
685af9cd 7075 *result_dwo_abbrev_table
606decb2 7076 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
685af9cd
TT
7077 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7078 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7079
7080 /* Read in the die, but leave space to copy over the attributes
7081 from the stub. This has the benefit of simplifying the rest of
7082 the code - all the work to maintain the illusion of a single
7083 DW_TAG_{compile,type}_unit DIE is done here. */
7084 num_extra_attrs = ((stmt_list != NULL)
7085 + (low_pc != NULL)
7086 + (high_pc != NULL)
7087 + (ranges != NULL)
7088 + (comp_dir != NULL));
7089 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 7090 num_extra_attrs);
b0c7bfa9
DE
7091
7092 /* Copy over the attributes from the stub to the DIE we just read in. */
7093 comp_unit_die = *result_comp_unit_die;
7094 i = comp_unit_die->num_attrs;
7095 if (stmt_list != NULL)
7096 comp_unit_die->attrs[i++] = *stmt_list;
7097 if (low_pc != NULL)
7098 comp_unit_die->attrs[i++] = *low_pc;
7099 if (high_pc != NULL)
7100 comp_unit_die->attrs[i++] = *high_pc;
7101 if (ranges != NULL)
7102 comp_unit_die->attrs[i++] = *ranges;
7103 if (comp_dir != NULL)
7104 comp_unit_die->attrs[i++] = *comp_dir;
7105 comp_unit_die->num_attrs += num_extra_attrs;
7106
b4f54984 7107 if (dwarf_die_debug)
bf6af496
DE
7108 {
7109 fprintf_unfiltered (gdb_stdlog,
7110 "Read die from %s@0x%x of %s:\n",
96b79293 7111 section->get_name (),
bf6af496
DE
7112 (unsigned) (begin_info_ptr - section->buffer),
7113 bfd_get_filename (abfd));
b4f54984 7114 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7115 }
7116
b0c7bfa9
DE
7117 /* Skip dummy compilation units. */
7118 if (info_ptr >= begin_info_ptr + dwo_unit->length
7119 || peek_abbrev_code (abfd, info_ptr) == 0)
7120 return 0;
7121
7122 *result_info_ptr = info_ptr;
7123 return 1;
7124}
7125
a084a2a6
AT
7126/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7127 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7128 signature is part of the header. */
7129static gdb::optional<ULONGEST>
7130lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7131{
7132 if (cu->header.version >= 5)
7133 return cu->header.signature;
7134 struct attribute *attr;
7135 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
529908cb 7136 if (attr == nullptr || !attr->form_is_unsigned ())
a084a2a6 7137 return gdb::optional<ULONGEST> ();
529908cb 7138 return attr->as_unsigned ();
a084a2a6
AT
7139}
7140
c0ab21c2 7141/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 7142 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7143 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7144
7145static struct dwo_unit *
4ab09049 7146lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 7147{
4ab09049 7148 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 7149 struct dwo_unit *dwo_unit;
c0ab21c2 7150 const char *comp_dir;
b0c7bfa9 7151
a2ce51a0
DE
7152 gdb_assert (cu != NULL);
7153
b0c7bfa9 7154 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7155 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7156 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 7157
4ab09049
SM
7158 if (per_cu->is_debug_types)
7159 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
7160 else
7161 {
a084a2a6 7162 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 7163
a084a2a6 7164 if (!signature.has_value ())
b0c7bfa9
DE
7165 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7166 " [in module %s]"),
4ab09049
SM
7167 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7168
7169 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
7170 }
7171
b0c7bfa9
DE
7172 return dwo_unit;
7173}
7174
c0ab21c2 7175/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 7176 See it for a description of the parameters.
fcd3b13d 7177 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 7178
c0ab21c2 7179void
9e021579
SM
7180cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7181 dwarf2_per_objfile *per_objfile,
2e671100 7182 dwarf2_cu *existing_cu)
a2ce51a0 7183{
a2ce51a0 7184 struct signatured_type *sig_type;
a2ce51a0
DE
7185
7186 /* Verify we can do the following downcast, and that we have the
7187 data we need. */
7188 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7189 sig_type = (struct signatured_type *) this_cu;
7190 gdb_assert (sig_type->dwo_unit != NULL);
7191
2e671100
SM
7192 dwarf2_cu *cu;
7193
7194 if (existing_cu != nullptr)
6aa5f3a6 7195 {
2e671100
SM
7196 cu = existing_cu;
7197 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 7198 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 7199 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
7200 }
7201 else
7202 {
7188ed02 7203 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 7204 in per_objfile yet. */
7188ed02 7205 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 7206 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 7207 cu = m_new_cu.get ();
6aa5f3a6
DE
7208 }
7209
7210 /* A future optimization, if needed, would be to use an existing
7211 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7212 could share abbrev tables. */
a2ce51a0 7213
2e671100 7214 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
7215 NULL /* stub_comp_unit_die */,
7216 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 7217 this, &info_ptr,
3e225074 7218 &comp_unit_die,
c0ab21c2 7219 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7220 {
7221 /* Dummy die. */
c0ab21c2 7222 dummy_p = true;
a2ce51a0 7223 }
a2ce51a0
DE
7224}
7225
fd820528 7226/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7227 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7228
f4dc4d17
DE
7229 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7230 Otherwise the table specified in the comp unit header is read in and used.
7231 This is an optimization for when we already have the abbrev table.
7232
2e671100
SM
7233 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7234 allocated. */
aaa75496 7235
ab432490 7236cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7237 dwarf2_per_objfile *per_objfile,
c0ab21c2 7238 struct abbrev_table *abbrev_table,
2e671100 7239 dwarf2_cu *existing_cu,
c0ab21c2
TT
7240 bool skip_partial)
7241 : die_reader_specs {},
6751ebae 7242 m_this_cu (this_cu)
c906108c 7243{
976ca316 7244 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7245 struct dwarf2_section_info *section = this_cu->section;
96b79293 7246 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 7247 const gdb_byte *begin_info_ptr;
dee91e82 7248 struct signatured_type *sig_type = NULL;
4bdcc0c1 7249 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7250 /* Non-zero if CU currently points to a DWO file and we need to
7251 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7252 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7253 int rereading_dwo_cu = 0;
c906108c 7254
b4f54984 7255 if (dwarf_die_debug)
9d8780f0 7256 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7257 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7258 sect_offset_str (this_cu->sect_off));
09406207 7259
a2ce51a0
DE
7260 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7261 file (instead of going through the stub), short-circuit all of this. */
7262 if (this_cu->reading_dwo_directly)
7263 {
7264 /* Narrow down the scope of possibilities to have to understand. */
7265 gdb_assert (this_cu->is_debug_types);
7266 gdb_assert (abbrev_table == NULL);
976ca316 7267 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
7268 return;
7269 }
7270
dee91e82 7271 /* This is cheap if the section is already read in. */
96b79293 7272 section->read (objfile);
dee91e82 7273
9c541725 7274 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7275
7276 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7277
2e671100
SM
7278 dwarf2_cu *cu;
7279
7280 if (existing_cu != nullptr)
dee91e82 7281 {
2e671100 7282 cu = existing_cu;
42e7ad6c
DE
7283 /* If this CU is from a DWO file we need to start over, we need to
7284 refetch the attributes from the skeleton CU.
7285 This could be optimized by retrieving those attributes from when we
7286 were here the first time: the previous comp_unit_die was stored in
7287 comp_unit_obstack. But there's no data yet that we need this
7288 optimization. */
7289 if (cu->dwo_unit != NULL)
7290 rereading_dwo_cu = 1;
dee91e82
DE
7291 }
7292 else
7293 {
7188ed02 7294 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 7295 in per_objfile yet. */
976ca316
SM
7296 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7297 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 7298 cu = m_new_cu.get ();
42e7ad6c 7299 }
dee91e82 7300
b0c7bfa9 7301 /* Get the header. */
9c541725 7302 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7303 {
7304 /* We already have the header, there's no need to read it in again. */
9c541725 7305 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7306 }
7307 else
7308 {
3019eac3 7309 if (this_cu->is_debug_types)
dee91e82 7310 {
976ca316
SM
7311 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7312 section, abbrev_section,
7313 info_ptr, rcuh_kind::TYPE);
dee91e82 7314
42e7ad6c
DE
7315 /* Since per_cu is the first member of struct signatured_type,
7316 we can go from a pointer to one to a pointer to the other. */
7317 sig_type = (struct signatured_type *) this_cu;
43988095 7318 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7319 gdb_assert (sig_type->type_offset_in_tu
7320 == cu->header.type_cu_offset_in_tu);
7321 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7322
42e7ad6c
DE
7323 /* LENGTH has not been set yet for type units if we're
7324 using .gdb_index. */
4057dfde 7325 this_cu->length = cu->header.get_length ();
3019eac3
DE
7326
7327 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7328 sig_type->type_offset_in_section =
7329 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7330
7331 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7332 }
7333 else
7334 {
976ca316
SM
7335 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7336 section, abbrev_section,
43988095
JK
7337 info_ptr,
7338 rcuh_kind::COMPILE);
dee91e82 7339
9c541725 7340 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7341 if (this_cu->length == 0)
7342 this_cu->length = cu->header.get_length ();
7343 else
7344 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7345 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7346 }
7347 }
10b3939b 7348
6caca83c 7349 /* Skip dummy compilation units. */
dee91e82 7350 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7351 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7352 {
7353 dummy_p = true;
7354 return;
7355 }
6caca83c 7356
433df2d4
DE
7357 /* If we don't have them yet, read the abbrevs for this compilation unit.
7358 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7359 done. */
f4dc4d17 7360 if (abbrev_table != NULL)
685af9cd
TT
7361 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7362 else
f4dc4d17 7363 {
606decb2 7364 abbrev_section->read (objfile);
c0ab21c2 7365 m_abbrev_table_holder
606decb2 7366 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
c0ab21c2 7367 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7368 }
af703f96 7369
dee91e82 7370 /* Read the top level CU/TU die. */
c0ab21c2 7371 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7372 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7373
58f0c718 7374 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7375 {
7376 dummy_p = true;
7377 return;
7378 }
58f0c718 7379
b0c7bfa9 7380 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7381 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7382 table from the DWO file and pass the ownership over to us. It will be
7383 referenced from READER, so we must make sure to free it after we're done
7384 with READER.
7385
b0c7bfa9
DE
7386 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7387 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7388 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7389 if (dwo_name != nullptr)
3019eac3 7390 {
3019eac3 7391 struct dwo_unit *dwo_unit;
b0c7bfa9 7392 struct die_info *dwo_comp_unit_die;
3019eac3 7393
3e225074 7394 if (comp_unit_die->has_children)
6a506a2d 7395 {
b98664d3 7396 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7397 " has children (offset %s) [in module %s]"),
7398 sect_offset_str (this_cu->sect_off),
7399 bfd_get_filename (abfd));
6a506a2d 7400 }
4ab09049 7401 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7402 if (dwo_unit != NULL)
3019eac3 7403 {
4ab09049 7404 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7405 comp_unit_die, NULL,
c0ab21c2 7406 this, &info_ptr,
3e225074 7407 &dwo_comp_unit_die,
c0ab21c2 7408 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7409 {
7410 /* Dummy die. */
c0ab21c2 7411 dummy_p = true;
6a506a2d
DE
7412 return;
7413 }
7414 comp_unit_die = dwo_comp_unit_die;
7415 }
7416 else
7417 {
7418 /* Yikes, we couldn't find the rest of the DIE, we only have
7419 the stub. A complaint has already been logged. There's
7420 not much more we can do except pass on the stub DIE to
7421 die_reader_func. We don't want to throw an error on bad
7422 debug info. */
3019eac3
DE
7423 }
7424 }
c0ab21c2 7425}
3019eac3 7426
6751ebae
TT
7427void
7428cutu_reader::keep ()
c0ab21c2 7429{
b0c7bfa9 7430 /* Done, clean up. */
6751ebae
TT
7431 gdb_assert (!dummy_p);
7432 if (m_new_cu != NULL)
348e048f 7433 {
7188ed02 7434 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
dda83cd7 7435 now. */
7188ed02
SM
7436 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7437 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 7438 }
dee91e82
DE
7439}
7440
18a8505e
AT
7441/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7442 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7443 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7444
7445 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7446 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7447
7448 We fill in THIS_CU->length.
7449
dee91e82 7450 THIS_CU->cu is always freed when done.
3019eac3 7451 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7452 to care whether it refers to the "main" CU or the DWO CU.
7453
7454 When parent_cu is passed, it is used to provide a default value for
7455 str_offsets_base and addr_base from the parent. */
dee91e82 7456
ab432490 7457cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7458 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
7459 struct dwarf2_cu *parent_cu,
7460 struct dwo_file *dwo_file)
7461 : die_reader_specs {},
7462 m_this_cu (this_cu)
dee91e82 7463{
976ca316 7464 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7465 struct dwarf2_section_info *section = this_cu->section;
96b79293 7466 bfd *abfd = section->get_bfd_owner ();
33e80786 7467 struct dwarf2_section_info *abbrev_section;
d521ce57 7468 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7469
b4f54984 7470 if (dwarf_die_debug)
9d8780f0 7471 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7472 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7473 sect_offset_str (this_cu->sect_off));
09406207 7474
976ca316 7475 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7476
33e80786
DE
7477 abbrev_section = (dwo_file != NULL
7478 ? &dwo_file->sections.abbrev
7479 : get_abbrev_section_for_cu (this_cu));
7480
dee91e82 7481 /* This is cheap if the section is already read in. */
96b79293 7482 section->read (objfile);
dee91e82 7483
976ca316 7484 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 7485
9c541725 7486 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
7487 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7488 section, abbrev_section, info_ptr,
43988095
JK
7489 (this_cu->is_debug_types
7490 ? rcuh_kind::TYPE
7491 : rcuh_kind::COMPILE));
dee91e82 7492
18a8505e
AT
7493 if (parent_cu != nullptr)
7494 {
c0ab21c2
TT
7495 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7496 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7497 }
4057dfde 7498 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7499
7500 /* Skip dummy compilation units. */
7501 if (info_ptr >= begin_info_ptr + this_cu->length
7502 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7503 {
7504 dummy_p = true;
7505 return;
7506 }
72bf9492 7507
606decb2 7508 abbrev_section->read (objfile);
c0ab21c2 7509 m_abbrev_table_holder
606decb2 7510 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
dee91e82 7511
c0ab21c2
TT
7512 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7513 m_abbrev_table_holder.get ());
3e225074 7514 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7515}
7516
0018ea6f
DE
7517\f
7518/* Type Unit Groups.
dee91e82 7519
0018ea6f
DE
7520 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7521 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7522 so that all types coming from the same compilation (.o file) are grouped
7523 together. A future step could be to put the types in the same symtab as
7524 the CU the types ultimately came from. */
ff013f42 7525
f4dc4d17
DE
7526static hashval_t
7527hash_type_unit_group (const void *item)
7528{
9a3c8263
SM
7529 const struct type_unit_group *tu_group
7530 = (const struct type_unit_group *) item;
f4dc4d17 7531
094b34ac 7532 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7533}
348e048f
DE
7534
7535static int
f4dc4d17 7536eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7537{
9a3c8263
SM
7538 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7539 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7540
094b34ac 7541 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7542}
348e048f 7543
f4dc4d17
DE
7544/* Allocate a hash table for type unit groups. */
7545
eaa5fa8b 7546static htab_up
298e9637 7547allocate_type_unit_groups_table ()
f4dc4d17 7548{
eaa5fa8b
TT
7549 return htab_up (htab_create_alloc (3,
7550 hash_type_unit_group,
7551 eq_type_unit_group,
7552 NULL, xcalloc, xfree));
f4dc4d17 7553}
dee91e82 7554
f4dc4d17
DE
7555/* Type units that don't have DW_AT_stmt_list are grouped into their own
7556 partial symtabs. We combine several TUs per psymtab to not let the size
7557 of any one psymtab grow too big. */
7558#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7559#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7560
094b34ac 7561/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7562 Create the type_unit_group object used to hold one or more TUs. */
7563
7564static struct type_unit_group *
094b34ac 7565create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7566{
976ca316
SM
7567 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7568 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
094b34ac 7569 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7570 struct type_unit_group *tu_group;
f4dc4d17 7571
976ca316 7572 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
094b34ac 7573 per_cu = &tu_group->per_cu;
1859c670 7574 per_cu->per_bfd = per_bfd;
f4dc4d17 7575
1859c670 7576 if (per_bfd->using_index)
094b34ac 7577 {
1859c670 7578 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7579 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7580 }
7581 else
7582 {
9c541725 7583 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7584 dwarf2_psymtab *pst;
528e1572 7585 std::string name;
094b34ac
DE
7586
7587 /* Give the symtab a useful name for debug purposes. */
7588 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7589 name = string_printf ("<type_units_%d>",
7590 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7591 else
528e1572 7592 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7593
976ca316 7594 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
6d94535f 7595 pst->anonymous = true;
094b34ac 7596 }
f4dc4d17 7597
094b34ac 7598 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7599 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7600
7601 return tu_group;
7602}
7603
094b34ac
DE
7604/* Look up the type_unit_group for type unit CU, and create it if necessary.
7605 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7606
7607static struct type_unit_group *
ff39bb5e 7608get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7609{
976ca316
SM
7610 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7611 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7612 struct type_unit_group *tu_group;
7613 void **slot;
7614 unsigned int line_offset;
7615 struct type_unit_group type_unit_group_for_lookup;
7616
976ca316
SM
7617 if (per_objfile->per_bfd->type_unit_groups == NULL)
7618 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7619
7620 /* Do we need to create a new group, or can we use an existing one? */
7621
529908cb 7622 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
f4dc4d17 7623 {
529908cb 7624 line_offset = stmt_list->as_unsigned ();
f4dc4d17
DE
7625 ++tu_stats->nr_symtab_sharers;
7626 }
7627 else
7628 {
7629 /* Ugh, no stmt_list. Rare, but we have to handle it.
7630 We can do various things here like create one group per TU or
7631 spread them over multiple groups to split up the expansion work.
7632 To avoid worst case scenarios (too many groups or too large groups)
7633 we, umm, group them in bunches. */
7634 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7635 | (tu_stats->nr_stmt_less_type_units
7636 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7637 ++tu_stats->nr_stmt_less_type_units;
7638 }
7639
094b34ac 7640 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7641 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7642 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7643 &type_unit_group_for_lookup, INSERT);
7644 if (*slot != NULL)
7645 {
9a3c8263 7646 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7647 gdb_assert (tu_group != NULL);
7648 }
7649 else
7650 {
9c541725 7651 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7652 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7653 *slot = tu_group;
7654 ++tu_stats->nr_symtabs;
7655 }
7656
7657 return tu_group;
7658}
0018ea6f
DE
7659\f
7660/* Partial symbol tables. */
7661
7662/* Create a psymtab named NAME and assign it to PER_CU.
7663
7664 The caller must fill in the following details:
7665 dirname, textlow, texthigh. */
7666
891813be 7667static dwarf2_psymtab *
7aa104c4
SM
7668create_partial_symtab (dwarf2_per_cu_data *per_cu,
7669 dwarf2_per_objfile *per_objfile,
7670 const char *name)
0018ea6f 7671{
7aa104c4 7672 struct objfile *objfile = per_objfile->objfile;
891813be 7673 dwarf2_psymtab *pst;
0018ea6f 7674
9f4e76a4 7675 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7676
6d94535f 7677 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7678
7679 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7680 per_cu->v.psymtab = pst;
7681
7682 return pst;
7683}
7684
c0ab21c2 7685/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7686
7687static void
7688process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7689 const gdb_byte *info_ptr,
0018ea6f 7690 struct die_info *comp_unit_die,
c0ab21c2 7691 enum language pretend_language)
0018ea6f
DE
7692{
7693 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7694 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7695 struct objfile *objfile = per_objfile->objfile;
08feed99 7696 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7697 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7698 CORE_ADDR baseaddr;
7699 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7700 dwarf2_psymtab *pst;
3a2b436a 7701 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7702 const char *filename;
0018ea6f 7703
0018ea6f
DE
7704 gdb_assert (! per_cu->is_debug_types);
7705
c0ab21c2 7706 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7707
0018ea6f 7708 /* Allocate a new partial symbol table structure. */
2e927613
TV
7709 gdb::unique_xmalloc_ptr<char> debug_filename;
7710 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7711 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7712 if (filename == NULL)
0018ea6f 7713 filename = "";
2e927613
TV
7714 else if (strcmp (filename, artificial) == 0)
7715 {
7716 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7717 sect_offset_str (per_cu->sect_off),
7718 (char *) NULL));
2e927613
TV
7719 filename = debug_filename.get ();
7720 }
0018ea6f 7721
7aa104c4 7722 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7723
7724 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7725 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7726
b3b3bada 7727 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7728
7729 dwarf2_find_base_address (comp_unit_die, cu);
7730
7731 /* Possibly set the default values of LOWPC and HIGHPC from
7732 `DW_AT_ranges'. */
3a2b436a
JK
7733 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7734 &best_highpc, cu, pst);
7735 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7736 {
7737 CORE_ADDR low
7738 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7739 - baseaddr);
7740 CORE_ADDR high
7741 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7742 - baseaddr - 1);
7743 /* Store the contiguous range if it is not empty; it can be
7744 empty for CUs with no code. */
d320c2b5
TT
7745 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7746 low, high, pst);
79748972 7747 }
0018ea6f
DE
7748
7749 /* Check if comp unit has_children.
7750 If so, read the rest of the partial symbols from this comp unit.
7751 If not, there's no more debug_info for this comp unit. */
3e225074 7752 if (comp_unit_die->has_children)
0018ea6f
DE
7753 {
7754 struct partial_die_info *first_die;
7755 CORE_ADDR lowpc, highpc;
7756
7757 lowpc = ((CORE_ADDR) -1);
7758 highpc = ((CORE_ADDR) 0);
7759
7760 first_die = load_partial_dies (reader, info_ptr, 1);
7761
7762 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7763 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7764
7765 /* If we didn't find a lowpc, set it to highpc to avoid
7766 complaints from `maint check'. */
7767 if (lowpc == ((CORE_ADDR) -1))
7768 lowpc = highpc;
7769
7770 /* If the compilation unit didn't have an explicit address range,
7771 then use the information extracted from its child dies. */
e385593e 7772 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7773 {
7774 best_lowpc = lowpc;
7775 best_highpc = highpc;
7776 }
7777 }
4ae976d1 7778 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7779 best_lowpc + baseaddr)
7780 - baseaddr);
4ae976d1 7781 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7782 best_highpc + baseaddr)
7783 - baseaddr);
0018ea6f 7784
ae7754b2 7785 pst->end ();
0018ea6f 7786
ae640021 7787 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7788 {
7789 int i;
ae640021 7790 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7791
7792 /* Fill in 'dependencies' here; we fill in 'users' in a
7793 post-pass. */
7794 pst->number_of_dependencies = len;
a9342b62
TT
7795 pst->dependencies
7796 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7797 for (i = 0; i < len; ++i)
7798 {
7799 pst->dependencies[i]
7800 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7801 }
0018ea6f 7802
ae640021 7803 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7804 }
7805
7806 /* Get the list of files included in the current compilation unit,
7807 and build a psymtab for each of them. */
7808 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7809
6f738b01
SM
7810 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7811 ", %d global, %d static syms",
7812 per_cu->is_debug_types ? "type" : "comp",
7813 sect_offset_str (per_cu->sect_off),
7814 paddress (gdbarch, pst->text_low (objfile)),
7815 paddress (gdbarch, pst->text_high (objfile)),
7816 (int) pst->global_psymbols.size (),
7817 (int) pst->static_psymbols.size ());
0018ea6f
DE
7818}
7819
7820/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7821 Process compilation unit THIS_CU for a psymtab. */
7822
7823static void
ab432490
SM
7824process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7825 dwarf2_per_objfile *per_objfile,
135f5437 7826 bool want_partial_unit,
b93601f3 7827 enum language pretend_language)
0018ea6f
DE
7828{
7829 /* If this compilation unit was already read in, free the
7830 cached copy in order to read it in again. This is
7831 necessary because we skipped some symbols when we first
7832 read in the compilation unit (see load_partial_dies).
7833 This problem could be avoided, but the benefit is unclear. */
7188ed02 7834 per_objfile->remove_cu (this_cu);
0018ea6f 7835
2e671100 7836 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7837
58990295
TV
7838 switch (reader.comp_unit_die->tag)
7839 {
7840 case DW_TAG_compile_unit:
7841 this_cu->unit_type = DW_UT_compile;
7842 break;
7843 case DW_TAG_partial_unit:
7844 this_cu->unit_type = DW_UT_partial;
7845 break;
e77b0004
TV
7846 case DW_TAG_type_unit:
7847 this_cu->unit_type = DW_UT_type;
7848 break;
58990295
TV
7849 default:
7850 abort ();
7851 }
7852
c0ab21c2 7853 if (reader.dummy_p)
f1902523 7854 {
c0ab21c2 7855 /* Nothing. */
f1902523 7856 }
c0ab21c2 7857 else if (this_cu->is_debug_types)
3e225074
TT
7858 build_type_psymtabs_reader (&reader, reader.info_ptr,
7859 reader.comp_unit_die);
135f5437
TT
7860 else if (want_partial_unit
7861 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7862 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7863 reader.comp_unit_die,
c0ab21c2 7864 pretend_language);
0018ea6f 7865
7188ed02 7866 this_cu->lang = reader.cu->language;
58990295 7867
0018ea6f 7868 /* Age out any secondary CUs. */
7188ed02 7869 per_objfile->age_comp_units ();
0018ea6f 7870}
f4dc4d17
DE
7871
7872/* Reader function for build_type_psymtabs. */
7873
7874static void
7875build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7876 const gdb_byte *info_ptr,
3e225074 7877 struct die_info *type_unit_die)
f4dc4d17 7878{
976ca316 7879 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7880 struct dwarf2_cu *cu = reader->cu;
7881 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7882 struct signatured_type *sig_type;
f4dc4d17
DE
7883 struct type_unit_group *tu_group;
7884 struct attribute *attr;
7885 struct partial_die_info *first_die;
7886 CORE_ADDR lowpc, highpc;
891813be 7887 dwarf2_psymtab *pst;
f4dc4d17 7888
0186c6a7
DE
7889 gdb_assert (per_cu->is_debug_types);
7890 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7891
3e225074 7892 if (! type_unit_die->has_children)
f4dc4d17
DE
7893 return;
7894
052c8bb8 7895 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7896 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7897
df07e2c7 7898 if (tu_group->tus == nullptr)
a8b3b8e9 7899 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7900 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7901
7902 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7903 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7904 pst->anonymous = true;
f4dc4d17
DE
7905
7906 first_die = load_partial_dies (reader, info_ptr, 1);
7907
7908 lowpc = (CORE_ADDR) -1;
7909 highpc = (CORE_ADDR) 0;
7910 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7911
ae7754b2 7912 pst->end ();
f4dc4d17
DE
7913}
7914
73051182
DE
7915/* Struct used to sort TUs by their abbreviation table offset. */
7916
7917struct tu_abbrev_offset
7918{
b2bdb8cf
SM
7919 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7920 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7921 {}
7922
7923 signatured_type *sig_type;
73051182
DE
7924 sect_offset abbrev_offset;
7925};
7926
484cf504 7927/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7928
484cf504
TT
7929static bool
7930sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7931 const struct tu_abbrev_offset &b)
73051182 7932{
484cf504 7933 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7934}
7935
7936/* Efficiently read all the type units.
7937 This does the bulk of the work for build_type_psymtabs.
7938
7939 The efficiency is because we sort TUs by the abbrev table they use and
7940 only read each abbrev table once. In one program there are 200K TUs
7941 sharing 8K abbrev tables.
7942
7943 The main purpose of this function is to support building the
5989a64e 7944 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7945 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7946 can collapse the search space by grouping them by stmt_list.
7947 The savings can be significant, in the same program from above the 200K TUs
7948 share 8K stmt_list tables.
7949
7950 FUNC is expected to call get_type_unit_group, which will create the
7951 struct type_unit_group if necessary and add it to
5989a64e 7952 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7953
7954static void
976ca316 7955build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
73051182 7956{
976ca316 7957 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7958 abbrev_table_up abbrev_table;
73051182 7959 sect_offset abbrev_offset;
73051182
DE
7960
7961 /* It's up to the caller to not call us multiple times. */
976ca316 7962 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7963
976ca316 7964 if (per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7965 return;
7966
7967 /* TUs typically share abbrev tables, and there can be way more TUs than
7968 abbrev tables. Sort by abbrev table to reduce the number of times we
7969 read each abbrev table in.
7970 Alternatives are to punt or to maintain a cache of abbrev tables.
7971 This is simpler and efficient enough for now.
7972
7973 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7974 symtab to use). Typically TUs with the same abbrev offset have the same
7975 stmt_list value too so in practice this should work well.
7976
7977 The basic algorithm here is:
7978
7979 sort TUs by abbrev table
7980 for each TU with same abbrev table:
7981 read abbrev table if first user
7982 read TU top level DIE
7983 [IWBN if DWO skeletons had DW_AT_stmt_list]
7984 call FUNC */
7985
6f738b01 7986 dwarf_read_debug_printf ("Building type unit groups ...");
73051182
DE
7987
7988 /* Sort in a separate table to maintain the order of all_type_units
7989 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7990 std::vector<tu_abbrev_offset> sorted_by_abbrev;
976ca316 7991 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7992
976ca316 7993 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
b2bdb8cf 7994 sorted_by_abbrev.emplace_back
976ca316 7995 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
b2bdb8cf 7996 sig_type->per_cu.sect_off));
73051182 7997
484cf504
TT
7998 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7999 sort_tu_by_abbrev_offset);
73051182 8000
9c541725 8001 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8002
b2bdb8cf 8003 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8004 {
73051182
DE
8005 /* Switch to the next abbrev table if necessary. */
8006 if (abbrev_table == NULL
b2bdb8cf 8007 || tu.abbrev_offset != abbrev_offset)
73051182 8008 {
b2bdb8cf 8009 abbrev_offset = tu.abbrev_offset;
606decb2 8010 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
73051182 8011 abbrev_table =
606decb2 8012 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
8013 ++tu_stats->nr_uniq_abbrev_tables;
8014 }
8015
976ca316 8016 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
2e671100 8017 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
8018 if (!reader.dummy_p)
8019 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 8020 reader.comp_unit_die);
73051182 8021 }
6aa5f3a6 8022}
73051182 8023
6aa5f3a6
DE
8024/* Print collected type unit statistics. */
8025
8026static void
976ca316 8027print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 8028{
976ca316 8029 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6 8030
6f738b01
SM
8031 dwarf_read_debug_printf ("Type unit statistics:");
8032 dwarf_read_debug_printf (" %zu TUs",
8033 per_objfile->per_bfd->all_type_units.size ());
8034 dwarf_read_debug_printf (" %d uniq abbrev tables",
8035 tu_stats->nr_uniq_abbrev_tables);
8036 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
8037 tu_stats->nr_symtabs);
8038 dwarf_read_debug_printf (" %d symtab sharers",
8039 tu_stats->nr_symtab_sharers);
8040 dwarf_read_debug_printf (" %d type units without a stmt_list",
8041 tu_stats->nr_stmt_less_type_units);
8042 dwarf_read_debug_printf (" %d all_type_units reallocs",
8043 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8044}
8045
f4dc4d17
DE
8046/* Traversal function for build_type_psymtabs. */
8047
8048static int
8049build_type_psymtab_dependencies (void **slot, void *info)
8050{
976ca316
SM
8051 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
8052 struct objfile *objfile = per_objfile->objfile;
f4dc4d17 8053 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8054 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 8055 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 8056 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
8057 int i;
8058
8059 gdb_assert (len > 0);
197400e8 8060 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
8061
8062 pst->number_of_dependencies = len;
a9342b62 8063 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 8064 for (i = 0; i < len; ++i)
f4dc4d17 8065 {
df07e2c7 8066 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
8067 gdb_assert (iter->per_cu.is_debug_types);
8068 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8069 iter->type_unit_group = tu_group;
f4dc4d17
DE
8070 }
8071
df07e2c7
AB
8072 delete tu_group->tus;
8073 tu_group->tus = nullptr;
348e048f
DE
8074
8075 return 1;
8076}
8077
8078/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8079 Build partial symbol tables for the .debug_types comp-units. */
8080
8081static void
976ca316 8082build_type_psymtabs (dwarf2_per_objfile *per_objfile)
348e048f 8083{
976ca316 8084 if (! create_all_type_units (per_objfile))
348e048f
DE
8085 return;
8086
976ca316 8087 build_type_psymtabs_1 (per_objfile);
6aa5f3a6 8088}
f4dc4d17 8089
6aa5f3a6
DE
8090/* Traversal function for process_skeletonless_type_unit.
8091 Read a TU in a DWO file and build partial symbols for it. */
8092
8093static int
8094process_skeletonless_type_unit (void **slot, void *info)
8095{
8096 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 8097 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
8098 struct signatured_type find_entry, *entry;
8099
8100 /* If this TU doesn't exist in the global table, add it and read it in. */
8101
976ca316
SM
8102 if (per_objfile->per_bfd->signatured_types == NULL)
8103 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
8104
8105 find_entry.signature = dwo_unit->signature;
976ca316 8106 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 8107 &find_entry, INSERT);
6aa5f3a6
DE
8108 /* If we've already seen this type there's nothing to do. What's happening
8109 is we're doing our own version of comdat-folding here. */
8110 if (*slot != NULL)
8111 return 1;
8112
8113 /* This does the job that create_all_type_units would have done for
8114 this TU. */
976ca316
SM
8115 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
8116 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8117 *slot = entry;
8118
8119 /* This does the job that build_type_psymtabs_1 would have done. */
976ca316 8120 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
8121 if (!reader.dummy_p)
8122 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 8123 reader.comp_unit_die);
6aa5f3a6
DE
8124
8125 return 1;
8126}
8127
8128/* Traversal function for process_skeletonless_type_units. */
8129
8130static int
8131process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8132{
8133 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8134
8135 if (dwo_file->tus != NULL)
b0b6a987
TT
8136 htab_traverse_noresize (dwo_file->tus.get (),
8137 process_skeletonless_type_unit, info);
6aa5f3a6
DE
8138
8139 return 1;
8140}
8141
8142/* Scan all TUs of DWO files, verifying we've processed them.
8143 This is needed in case a TU was emitted without its skeleton.
8144 Note: This can't be done until we know what all the DWO files are. */
8145
8146static void
976ca316 8147process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
8148{
8149 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
8150 if (get_dwp_file (per_objfile) == NULL
8151 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 8152 {
976ca316 8153 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 8154 process_dwo_file_for_skeletonless_type_units,
976ca316 8155 per_objfile);
6aa5f3a6 8156 }
348e048f
DE
8157}
8158
ed2dc618 8159/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8160
8161static void
976ca316 8162set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 8163{
976ca316 8164 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 8165 {
891813be 8166 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 8167
36586728
TT
8168 if (pst == NULL)
8169 continue;
8170
b76e467d 8171 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8172 {
8173 /* Set the 'user' field only if it is not already set. */
8174 if (pst->dependencies[j]->user == NULL)
8175 pst->dependencies[j]->user = pst;
8176 }
8177 }
8178}
8179
93311388
DE
8180/* Build the partial symbol table by doing a quick pass through the
8181 .debug_info and .debug_abbrev sections. */
72bf9492 8182
93311388 8183static void
976ca316 8184dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 8185{
976ca316 8186 struct objfile *objfile = per_objfile->objfile;
93311388 8187
6f738b01
SM
8188 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8189 objfile_name (objfile));
45cfd468 8190
76935768 8191 scoped_restore restore_reading_psyms
976ca316 8192 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
76935768 8193 true);
98bfdba5 8194
976ca316 8195 per_objfile->per_bfd->info.read (objfile);
91c24f0a 8196
93311388
DE
8197 /* Any cached compilation units will be linked by the per-objfile
8198 read_in_chain. Make sure to free them when we're done. */
976ca316 8199 free_cached_comp_units freer (per_objfile);
72bf9492 8200
976ca316 8201 build_type_psymtabs (per_objfile);
348e048f 8202
976ca316 8203 create_all_comp_units (per_objfile);
c906108c 8204
60606b2c
TT
8205 /* Create a temporary address map on a temporary obstack. We later
8206 copy this to the final obstack. */
8268c778 8207 auto_obstack temp_obstack;
791afaa2
TT
8208
8209 scoped_restore save_psymtabs_addrmap
d320c2b5 8210 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8211 addrmap_create_mutable (&temp_obstack));
72bf9492 8212
976ca316 8213 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
8214 {
8215 if (per_cu->v.psymtab != NULL)
8216 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8217 continue;
976ca316 8218 process_psymtab_comp_unit (per_cu, per_objfile, false,
ab432490 8219 language_minimal);
3d5afab3 8220 }
ff013f42 8221
6aa5f3a6 8222 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 8223 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
8224
8225 /* Now that all TUs have been processed we can fill in the dependencies. */
976ca316 8226 if (per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 8227 {
976ca316
SM
8228 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8229 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
8230 }
8231
6f738b01 8232 if (dwarf_read_debug > 0)
976ca316 8233 print_tu_stats (per_objfile);
6aa5f3a6 8234
976ca316 8235 set_partial_user (per_objfile);
95554aad 8236
d320c2b5
TT
8237 objfile->partial_symtabs->psymtabs_addrmap
8238 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8239 objfile->partial_symtabs->obstack ());
791afaa2
TT
8240 /* At this point we want to keep the address map. */
8241 save_psymtabs_addrmap.release ();
ff013f42 8242
6f738b01
SM
8243 dwarf_read_debug_printf ("Done building psymtabs of %s",
8244 objfile_name (objfile));
ae038cb0
DJ
8245}
8246
dee91e82
DE
8247/* Load the partial DIEs for a secondary CU into memory.
8248 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8249
dee91e82 8250static void
ab432490 8251load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
8252 dwarf2_per_objfile *per_objfile,
8253 dwarf2_cu *existing_cu)
dee91e82 8254{
2e671100 8255 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
8256
8257 if (!reader.dummy_p)
8258 {
8259 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8260 language_minimal);
8261
8262 /* Check if comp unit has_children.
8263 If so, read the rest of the partial symbols from this comp unit.
8264 If not, there's no more debug_info for this comp unit. */
3e225074 8265 if (reader.comp_unit_die->has_children)
c0ab21c2 8266 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8267
8268 reader.keep ();
c0ab21c2 8269 }
ae038cb0
DJ
8270}
8271
ae038cb0 8272static void
976ca316 8273read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 8274 struct dwarf2_section_info *section,
f1902523 8275 struct dwarf2_section_info *abbrev_section,
b76e467d 8276 unsigned int is_dwz)
ae038cb0 8277{
d521ce57 8278 const gdb_byte *info_ptr;
976ca316 8279 struct objfile *objfile = per_objfile->objfile;
be391dca 8280
6f738b01
SM
8281 dwarf_read_debug_printf ("Reading %s for %s",
8282 section->get_name (),
8283 section->get_file_name ());
bf6af496 8284
96b79293 8285 section->read (objfile);
ae038cb0 8286
36586728 8287 info_ptr = section->buffer;
6e70227d 8288
36586728 8289 while (info_ptr < section->buffer + section->size)
ae038cb0 8290 {
ae038cb0 8291 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8292
9c541725 8293 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8294
f1902523 8295 comp_unit_head cu_header;
976ca316 8296 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618
SM
8297 abbrev_section, info_ptr,
8298 rcuh_kind::COMPILE);
ae038cb0
DJ
8299
8300 /* Save the compilation unit for later lookup. */
f1902523 8301 if (cu_header.unit_type != DW_UT_type)
976ca316 8302 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8303 else
8304 {
976ca316 8305 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8306 sig_type->signature = cu_header.signature;
8307 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8308 this_cu = &sig_type->per_cu;
8309 }
8310 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8311 this_cu->sect_off = sect_off;
f1902523 8312 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8313 this_cu->is_dwz = is_dwz;
8a0459fd 8314 this_cu->section = section;
ae038cb0 8315
976ca316 8316 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8317
8318 info_ptr = info_ptr + this_cu->length;
8319 }
36586728
TT
8320}
8321
8322/* Create a list of all compilation units in OBJFILE.
8323 This is only done for -readnow and building partial symtabs. */
8324
8325static void
976ca316 8326create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 8327{
976ca316
SM
8328 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8329 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8330 &per_objfile->per_bfd->abbrev, 0);
36586728 8331
976ca316 8332 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 8333 if (dwz != NULL)
976ca316 8334 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
c906108c
SS
8335}
8336
5734ee8b 8337/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8338 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8339 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8340 DW_AT_ranges). See the comments of add_partial_subprogram on how
8341 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8342
72bf9492
DJ
8343static void
8344scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8345 CORE_ADDR *highpc, int set_addrmap,
8346 struct dwarf2_cu *cu)
c906108c 8347{
72bf9492 8348 struct partial_die_info *pdi;
c906108c 8349
91c24f0a
DC
8350 /* Now, march along the PDI's, descending into ones which have
8351 interesting children but skipping the children of the other ones,
8352 until we reach the end of the compilation unit. */
c906108c 8353
72bf9492 8354 pdi = first_die;
91c24f0a 8355
72bf9492
DJ
8356 while (pdi != NULL)
8357 {
52356b79 8358 pdi->fixup (cu);
c906108c 8359
f55ee35c 8360 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8361 children, so we need to look at them. Ditto for anonymous
8362 enums. */
933c6fe4 8363
7d00ffec 8364 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8365 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8366 || pdi->tag == DW_TAG_imported_unit
8367 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8368 {
72bf9492 8369 switch (pdi->tag)
c906108c
SS
8370 {
8371 case DW_TAG_subprogram:
b1dc1806 8372 case DW_TAG_inlined_subroutine:
cdc07690 8373 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
8374 if (cu->language == language_cplus)
8375 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8376 set_addrmap, cu);
c906108c 8377 break;
72929c62 8378 case DW_TAG_constant:
c906108c
SS
8379 case DW_TAG_variable:
8380 case DW_TAG_typedef:
91c24f0a 8381 case DW_TAG_union_type:
317d2668
TV
8382 if (!pdi->is_declaration
8383 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8384 {
72bf9492 8385 add_partial_symbol (pdi, cu);
63d06c5c
DC
8386 }
8387 break;
c906108c 8388 case DW_TAG_class_type:
680b30c7 8389 case DW_TAG_interface_type:
c906108c 8390 case DW_TAG_structure_type:
72bf9492 8391 if (!pdi->is_declaration)
c906108c 8392 {
72bf9492 8393 add_partial_symbol (pdi, cu);
c906108c 8394 }
b7fee5a3
KS
8395 if ((cu->language == language_rust
8396 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8397 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8398 set_addrmap, cu);
c906108c 8399 break;
91c24f0a 8400 case DW_TAG_enumeration_type:
72bf9492
DJ
8401 if (!pdi->is_declaration)
8402 add_partial_enumeration (pdi, cu);
c906108c
SS
8403 break;
8404 case DW_TAG_base_type:
dda83cd7 8405 case DW_TAG_subrange_type:
c906108c 8406 /* File scope base type definitions are added to the partial
dda83cd7 8407 symbol table. */
72bf9492 8408 add_partial_symbol (pdi, cu);
c906108c 8409 break;
d9fa45fe 8410 case DW_TAG_namespace:
cdc07690 8411 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8412 break;
5d7cb8df 8413 case DW_TAG_module:
59c35742
AB
8414 if (!pdi->is_declaration)
8415 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8416 break;
95554aad
TT
8417 case DW_TAG_imported_unit:
8418 {
8419 struct dwarf2_per_cu_data *per_cu;
8420
f4dc4d17
DE
8421 /* For now we don't handle imported units in type units. */
8422 if (cu->per_cu->is_debug_types)
8423 {
8424 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8425 " supported in type units [in module %s]"),
5e22e966 8426 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8427 }
8428
e3b94546 8429 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8430 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8431
8432 /* Go read the partial unit, if needed. */
8433 if (per_cu->v.psymtab == NULL)
ab432490
SM
8434 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8435 cu->language);
95554aad 8436
ae640021 8437 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8438 }
8439 break;
74921315
KS
8440 case DW_TAG_imported_declaration:
8441 add_partial_symbol (pdi, cu);
8442 break;
c906108c
SS
8443 default:
8444 break;
8445 }
8446 }
8447
72bf9492
DJ
8448 /* If the die has a sibling, skip to the sibling. */
8449
8450 pdi = pdi->die_sibling;
8451 }
8452}
8453
8454/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8455
72bf9492 8456 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8457 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8458 Enumerators are an exception; they use the scope of their parent
8459 enumeration type, i.e. the name of the enumeration type is not
8460 prepended to the enumerator.
91c24f0a 8461
72bf9492
DJ
8462 There are two complexities. One is DW_AT_specification; in this
8463 case "parent" means the parent of the target of the specification,
8464 instead of the direct parent of the DIE. The other is compilers
8465 which do not emit DW_TAG_namespace; in this case we try to guess
8466 the fully qualified name of structure types from their members'
8467 linkage names. This must be done using the DIE's children rather
8468 than the children of any DW_AT_specification target. We only need
8469 to do this for structures at the top level, i.e. if the target of
8470 any DW_AT_specification (if any; otherwise the DIE itself) does not
8471 have a parent. */
8472
8473/* Compute the scope prefix associated with PDI's parent, in
8474 compilation unit CU. The result will be allocated on CU's
8475 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8476 field. NULL is returned if no prefix is necessary. */
15d034d0 8477static const char *
72bf9492
DJ
8478partial_die_parent_scope (struct partial_die_info *pdi,
8479 struct dwarf2_cu *cu)
8480{
15d034d0 8481 const char *grandparent_scope;
72bf9492 8482 struct partial_die_info *parent, *real_pdi;
91c24f0a 8483
72bf9492
DJ
8484 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8485 then this means the parent of the specification DIE. */
8486
8487 real_pdi = pdi;
72bf9492 8488 while (real_pdi->has_specification)
fb816e8b 8489 {
122cf0f2
AB
8490 auto res = find_partial_die (real_pdi->spec_offset,
8491 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8492 real_pdi = res.pdi;
8493 cu = res.cu;
8494 }
72bf9492
DJ
8495
8496 parent = real_pdi->die_parent;
8497 if (parent == NULL)
8498 return NULL;
8499
8500 if (parent->scope_set)
8501 return parent->scope;
8502
52356b79 8503 parent->fixup (cu);
72bf9492 8504
10b3939b 8505 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8506
acebe513
UW
8507 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8508 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8509 Work around this problem here. */
8510 if (cu->language == language_cplus
6e70227d 8511 && parent->tag == DW_TAG_namespace
7d00ffec 8512 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
8513 && grandparent_scope == NULL)
8514 {
8515 parent->scope = NULL;
8516 parent->scope_set = 1;
8517 return NULL;
8518 }
8519
0a4b0913 8520 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8521 if (pdi->tag == DW_TAG_enumerator)
8522 /* Enumerators should not get the name of the enumeration as a prefix. */
8523 parent->scope = grandparent_scope;
8524 else if (parent->tag == DW_TAG_namespace
f55ee35c 8525 || parent->tag == DW_TAG_module
72bf9492
DJ
8526 || parent->tag == DW_TAG_structure_type
8527 || parent->tag == DW_TAG_class_type
680b30c7 8528 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8529 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8530 || parent->tag == DW_TAG_enumeration_type
8531 || (cu->language == language_fortran
8532 && parent->tag == DW_TAG_subprogram
8533 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8534 {
8535 if (grandparent_scope == NULL)
7d00ffec 8536 parent->scope = parent->name (cu);
72bf9492 8537 else
3e43a32a
MS
8538 parent->scope = typename_concat (&cu->comp_unit_obstack,
8539 grandparent_scope,
7d00ffec 8540 parent->name (cu), 0, cu);
72bf9492 8541 }
72bf9492
DJ
8542 else
8543 {
8544 /* FIXME drow/2004-04-01: What should we be doing with
8545 function-local names? For partial symbols, we should probably be
8546 ignoring them. */
fa9c3fa0
TT
8547 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8548 dwarf_tag_name (parent->tag),
8549 sect_offset_str (pdi->sect_off));
72bf9492 8550 parent->scope = grandparent_scope;
c906108c
SS
8551 }
8552
72bf9492
DJ
8553 parent->scope_set = 1;
8554 return parent->scope;
8555}
8556
8557/* Return the fully scoped name associated with PDI, from compilation unit
8558 CU. The result will be allocated with malloc. */
4568ecf9 8559
43816ebc 8560static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8561partial_die_full_name (struct partial_die_info *pdi,
8562 struct dwarf2_cu *cu)
8563{
15d034d0 8564 const char *parent_scope;
72bf9492 8565
98bfdba5
PA
8566 /* If this is a template instantiation, we can not work out the
8567 template arguments from partial DIEs. So, unfortunately, we have
8568 to go through the full DIEs. At least any work we do building
8569 types here will be reused if full symbols are loaded later. */
8570 if (pdi->has_template_arguments)
8571 {
52356b79 8572 pdi->fixup (cu);
98bfdba5 8573
7d00ffec 8574 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8575 {
8576 struct die_info *die;
8577 struct attribute attr;
8578 struct dwarf2_cu *ref_cu = cu;
8579
b64f50a1 8580 /* DW_FORM_ref_addr is using section offset. */
b4069958 8581 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8582 attr.form = DW_FORM_ref_addr;
9c541725 8583 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8584 die = follow_die_ref (NULL, &attr, &ref_cu);
8585
43816ebc 8586 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8587 }
8588 }
8589
72bf9492
DJ
8590 parent_scope = partial_die_parent_scope (pdi, cu);
8591 if (parent_scope == NULL)
8592 return NULL;
8593 else
43816ebc 8594 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8595 pdi->name (cu),
8596 0, cu));
c906108c
SS
8597}
8598
8599static void
72bf9492 8600add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8601{
976ca316
SM
8602 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8603 struct objfile *objfile = per_objfile->objfile;
08feed99 8604 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8605 CORE_ADDR addr = 0;
15d034d0 8606 const char *actual_name = NULL;
e142c38c
DJ
8607 CORE_ADDR baseaddr;
8608
b3b3bada 8609 baseaddr = objfile->text_section_offset ();
c906108c 8610
43816ebc
TT
8611 gdb::unique_xmalloc_ptr<char> built_actual_name
8612 = partial_die_full_name (pdi, cu);
15d034d0 8613 if (built_actual_name != NULL)
43816ebc 8614 actual_name = built_actual_name.get ();
63d06c5c 8615
72bf9492 8616 if (actual_name == NULL)
7d00ffec 8617 actual_name = pdi->name (cu);
72bf9492 8618
76e288d1
TT
8619 partial_symbol psymbol;
8620 memset (&psymbol, 0, sizeof (psymbol));
8621 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
a52d653e 8622 psymbol.ginfo.set_section_index (-1);
76e288d1
TT
8623
8624 /* The code below indicates that the psymbol should be installed by
8625 setting this. */
8626 gdb::optional<psymbol_placement> where;
8627
c906108c
SS
8628 switch (pdi->tag)
8629 {
b1dc1806 8630 case DW_TAG_inlined_subroutine:
c906108c 8631 case DW_TAG_subprogram:
79748972
TT
8632 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8633 - baseaddr);
0a4b0913
AB
8634 if (pdi->is_external
8635 || cu->language == language_ada
8636 || (cu->language == language_fortran
8637 && pdi->die_parent != NULL
8638 && pdi->die_parent->tag == DW_TAG_subprogram))
8639 {
dda83cd7
SM
8640 /* Normally, only "external" DIEs are part of the global scope.
8641 But in Ada and Fortran, we want to be able to access nested
8642 procedures globally. So all Ada and Fortran subprograms are
8643 stored in the global scope. */
76e288d1 8644 where = psymbol_placement::GLOBAL;
c906108c
SS
8645 }
8646 else
76e288d1
TT
8647 where = psymbol_placement::STATIC;
8648
8649 psymbol.domain = VAR_DOMAIN;
8650 psymbol.aclass = LOC_BLOCK;
a52d653e 8651 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1 8652 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8653
8654 if (pdi->main_subprogram && actual_name != NULL)
8655 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8656 break;
72929c62 8657 case DW_TAG_constant:
76e288d1
TT
8658 psymbol.domain = VAR_DOMAIN;
8659 psymbol.aclass = LOC_STATIC;
8660 where = (pdi->is_external
8661 ? psymbol_placement::GLOBAL
8662 : psymbol_placement::STATIC);
72929c62 8663 break;
c906108c 8664 case DW_TAG_variable:
95554aad
TT
8665 if (pdi->d.locdesc)
8666 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8667
95554aad 8668 if (pdi->d.locdesc
caac4577 8669 && addr == 0
976ca316 8670 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8671 {
8672 /* A global or static variable may also have been stripped
8673 out by the linker if unused, in which case its address
8674 will be nullified; do not add such variables into partial
8675 symbol table then. */
8676 }
8677 else if (pdi->is_external)
c906108c
SS
8678 {
8679 /* Global Variable.
8680 Don't enter into the minimal symbol tables as there is
8681 a minimal symbol table entry from the ELF symbols already.
8682 Enter into partial symbol table if it has a location
8683 descriptor or a type.
8684 If the location descriptor is missing, new_symbol will create
8685 a LOC_UNRESOLVED symbol, the address of the variable will then
8686 be determined from the minimal symbol table whenever the variable
8687 is referenced.
8688 The address for the partial symbol table entry is not
8689 used by GDB, but it comes in handy for debugging partial symbol
8690 table building. */
8691
95554aad 8692 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8693 {
8694 psymbol.domain = VAR_DOMAIN;
8695 psymbol.aclass = LOC_STATIC;
a52d653e 8696 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8697 psymbol.ginfo.value.address = addr;
8698 where = psymbol_placement::GLOBAL;
8699 }
c906108c
SS
8700 }
8701 else
8702 {
ff908ebf
AW
8703 int has_loc = pdi->d.locdesc != NULL;
8704
8705 /* Static Variable. Skip symbols whose value we cannot know (those
8706 without location descriptors or constant values). */
8707 if (!has_loc && !pdi->has_const_value)
43816ebc 8708 return;
ff908ebf 8709
76e288d1
TT
8710 psymbol.domain = VAR_DOMAIN;
8711 psymbol.aclass = LOC_STATIC;
a52d653e 8712 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8713 if (has_loc)
8714 psymbol.ginfo.value.address = addr;
8715 where = psymbol_placement::STATIC;
c906108c
SS
8716 }
8717 break;
d8f62e84 8718 case DW_TAG_array_type:
c906108c
SS
8719 case DW_TAG_typedef:
8720 case DW_TAG_base_type:
a02abb62 8721 case DW_TAG_subrange_type:
76e288d1
TT
8722 psymbol.domain = VAR_DOMAIN;
8723 psymbol.aclass = LOC_TYPEDEF;
8724 where = psymbol_placement::STATIC;
c906108c 8725 break;
74921315 8726 case DW_TAG_imported_declaration:
72bf9492 8727 case DW_TAG_namespace:
76e288d1
TT
8728 psymbol.domain = VAR_DOMAIN;
8729 psymbol.aclass = LOC_TYPEDEF;
8730 where = psymbol_placement::GLOBAL;
72bf9492 8731 break;
530e8392 8732 case DW_TAG_module:
a5fd13a9 8733 /* With Fortran 77 there might be a "BLOCK DATA" module
dda83cd7
SM
8734 available without any name. If so, we skip the module as it
8735 doesn't bring any value. */
a5fd13a9 8736 if (actual_name != nullptr)
76e288d1
TT
8737 {
8738 psymbol.domain = MODULE_DOMAIN;
8739 psymbol.aclass = LOC_TYPEDEF;
8740 where = psymbol_placement::GLOBAL;
8741 }
530e8392 8742 break;
c906108c 8743 case DW_TAG_class_type:
680b30c7 8744 case DW_TAG_interface_type:
c906108c
SS
8745 case DW_TAG_structure_type:
8746 case DW_TAG_union_type:
8747 case DW_TAG_enumeration_type:
fa4028e9 8748 /* Skip external references. The DWARF standard says in the section
dda83cd7
SM
8749 about "Structure, Union, and Class Type Entries": "An incomplete
8750 structure, union or class type is represented by a structure,
8751 union or class entry that does not have a byte size attribute
8752 and that has a DW_AT_declaration attribute." */
fa4028e9 8753 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8754 return;
fa4028e9 8755
63d06c5c
DC
8756 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8757 static vs. global. */
76e288d1
TT
8758 psymbol.domain = STRUCT_DOMAIN;
8759 psymbol.aclass = LOC_TYPEDEF;
8760 where = (cu->language == language_cplus
8761 ? psymbol_placement::GLOBAL
8762 : psymbol_placement::STATIC);
c906108c
SS
8763 break;
8764 case DW_TAG_enumerator:
76e288d1
TT
8765 psymbol.domain = VAR_DOMAIN;
8766 psymbol.aclass = LOC_CONST;
8767 where = (cu->language == language_cplus
8768 ? psymbol_placement::GLOBAL
8769 : psymbol_placement::STATIC);
c906108c
SS
8770 break;
8771 default:
8772 break;
8773 }
76e288d1
TT
8774
8775 if (where.has_value ())
8776 {
f049a313
TT
8777 if (built_actual_name != nullptr)
8778 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8779 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8780 psymbol.ginfo.set_linkage_name (actual_name);
8781 else
8782 {
8783 psymbol.ginfo.set_demangled_name (actual_name,
8784 &objfile->objfile_obstack);
8785 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8786 }
932539d7 8787 cu->per_cu->v.psymtab->add_psymbol (psymbol, *where, objfile);
76e288d1 8788 }
c906108c
SS
8789}
8790
5c4e30ca
DC
8791/* Read a partial die corresponding to a namespace; also, add a symbol
8792 corresponding to that namespace to the symbol table. NAMESPACE is
8793 the name of the enclosing namespace. */
91c24f0a 8794
72bf9492
DJ
8795static void
8796add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8797 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8798 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8799{
72bf9492 8800 /* Add a symbol for the namespace. */
e7c27a73 8801
72bf9492 8802 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8803
8804 /* Now scan partial symbols in that namespace. */
8805
91c24f0a 8806 if (pdi->has_children)
cdc07690 8807 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8808}
8809
5d7cb8df
JK
8810/* Read a partial die corresponding to a Fortran module. */
8811
8812static void
8813add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8814 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8815{
530e8392
KB
8816 /* Add a symbol for the namespace. */
8817
8818 add_partial_symbol (pdi, cu);
8819
f55ee35c 8820 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8821
8822 if (pdi->has_children)
cdc07690 8823 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8824}
8825
b1dc1806
XR
8826/* Read a partial die corresponding to a subprogram or an inlined
8827 subprogram and create a partial symbol for that subprogram.
8828 When the CU language allows it, this routine also defines a partial
8829 symbol for each nested subprogram that this subprogram contains.
8830 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8831 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8832
cdc07690
YQ
8833 PDI may also be a lexical block, in which case we simply search
8834 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8835 Again, this is only performed when the CU language allows this
8836 type of definitions. */
8837
8838static void
8839add_partial_subprogram (struct partial_die_info *pdi,
8840 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8841 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8842{
b1dc1806 8843 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8844 {
8845 if (pdi->has_pc_info)
dda83cd7
SM
8846 {
8847 if (pdi->lowpc < *lowpc)
8848 *lowpc = pdi->lowpc;
8849 if (pdi->highpc > *highpc)
8850 *highpc = pdi->highpc;
cdc07690 8851 if (set_addrmap)
5734ee8b 8852 {
5e22e966 8853 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8854 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8855 CORE_ADDR baseaddr;
b926417a
TT
8856 CORE_ADDR this_highpc;
8857 CORE_ADDR this_lowpc;
5734ee8b 8858
b3b3bada 8859 baseaddr = objfile->text_section_offset ();
b926417a
TT
8860 this_lowpc
8861 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8862 pdi->lowpc + baseaddr)
8863 - baseaddr);
8864 this_highpc
8865 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8866 pdi->highpc + baseaddr)
8867 - baseaddr);
d320c2b5 8868 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8869 this_lowpc, this_highpc - 1,
9291a0cd 8870 cu->per_cu->v.psymtab);
5734ee8b 8871 }
dda83cd7 8872 }
481860b3
GB
8873
8874 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8875 {
dda83cd7 8876 if (!pdi->is_declaration)
e8d05480
JB
8877 /* Ignore subprogram DIEs that do not have a name, they are
8878 illegal. Do not emit a complaint at this point, we will
8879 do so when we convert this psymtab into a symtab. */
7d00ffec 8880 if (pdi->name (cu))
e8d05480 8881 add_partial_symbol (pdi, cu);
dda83cd7 8882 }
bc30ff58 8883 }
6e70227d 8884
bc30ff58
JB
8885 if (! pdi->has_children)
8886 return;
8887
0a4b0913 8888 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8889 {
8890 pdi = pdi->die_child;
8891 while (pdi != NULL)
8892 {
52356b79 8893 pdi->fixup (cu);
bc30ff58 8894 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8895 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8896 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8897 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8898 pdi = pdi->die_sibling;
8899 }
8900 }
8901}
8902
91c24f0a
DC
8903/* Read a partial die corresponding to an enumeration type. */
8904
72bf9492
DJ
8905static void
8906add_partial_enumeration (struct partial_die_info *enum_pdi,
8907 struct dwarf2_cu *cu)
91c24f0a 8908{
72bf9492 8909 struct partial_die_info *pdi;
91c24f0a 8910
7d00ffec 8911 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8912 add_partial_symbol (enum_pdi, cu);
8913
8914 pdi = enum_pdi->die_child;
8915 while (pdi)
91c24f0a 8916 {
7d00ffec 8917 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8918 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8919 else
72bf9492
DJ
8920 add_partial_symbol (pdi, cu);
8921 pdi = pdi->die_sibling;
91c24f0a 8922 }
91c24f0a
DC
8923}
8924
6caca83c
CC
8925/* Return the initial uleb128 in the die at INFO_PTR. */
8926
8927static unsigned int
d521ce57 8928peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8929{
8930 unsigned int bytes_read;
8931
8932 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8933}
8934
685af9cd
TT
8935/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8936 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8937
4bb7a0a7
DJ
8938 Return the corresponding abbrev, or NULL if the number is zero (indicating
8939 an empty DIE). In either case *BYTES_READ will be set to the length of
8940 the initial number. */
8941
8942static struct abbrev_info *
685af9cd
TT
8943peek_die_abbrev (const die_reader_specs &reader,
8944 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8945{
685af9cd 8946 dwarf2_cu *cu = reader.cu;
5e22e966 8947 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8948 unsigned int abbrev_number
8949 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8950
8951 if (abbrev_number == 0)
8952 return NULL;
8953
685af9cd 8954 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8955 if (!abbrev)
8956 {
422b9917 8957 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8958 " at offset %s [in module %s]"),
422b9917 8959 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8960 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8961 }
8962
8963 return abbrev;
8964}
8965
93311388
DE
8966/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8967 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8968 DIE. Any children of the skipped DIEs will also be skipped. */
8969
d521ce57
TT
8970static const gdb_byte *
8971skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8972{
4bb7a0a7
DJ
8973 while (1)
8974 {
685af9cd
TT
8975 unsigned int bytes_read;
8976 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8977
4bb7a0a7
DJ
8978 if (abbrev == NULL)
8979 return info_ptr + bytes_read;
8980 else
dee91e82 8981 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8982 }
8983}
8984
93311388
DE
8985/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8986 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8987 abbrev corresponding to that skipped uleb128 should be passed in
8988 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8989 children. */
8990
d521ce57
TT
8991static const gdb_byte *
8992skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8993 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8994{
8995 unsigned int bytes_read;
8996 struct attribute attr;
dee91e82
DE
8997 bfd *abfd = reader->abfd;
8998 struct dwarf2_cu *cu = reader->cu;
d521ce57 8999 const gdb_byte *buffer = reader->buffer;
f664829e 9000 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9001 unsigned int form, i;
9002
9003 for (i = 0; i < abbrev->num_attrs; i++)
9004 {
9005 /* The only abbrev we care about is DW_AT_sibling. */
9006 if (abbrev->attrs[i].name == DW_AT_sibling)
9007 {
7a5f294d 9008 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9009 if (attr.form == DW_FORM_ref_addr)
b98664d3 9010 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9011 else
b9502d3f 9012 {
0826b30a 9013 sect_offset off = attr.get_ref_die_offset ();
9c541725 9014 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9015
9016 if (sibling_ptr < info_ptr)
b98664d3 9017 complaint (_("DW_AT_sibling points backwards"));
22869d73 9018 else if (sibling_ptr > reader->buffer_end)
a0194fa8 9019 reader->die_section->overflow_complaint ();
b9502d3f
WN
9020 else
9021 return sibling_ptr;
9022 }
4bb7a0a7
DJ
9023 }
9024
9025 /* If it isn't DW_AT_sibling, skip this attribute. */
9026 form = abbrev->attrs[i].form;
9027 skip_attribute:
9028 switch (form)
9029 {
4bb7a0a7 9030 case DW_FORM_ref_addr:
ae411497
TT
9031 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9032 and later it is offset sized. */
9033 if (cu->header.version == 2)
9034 info_ptr += cu->header.addr_size;
9035 else
9036 info_ptr += cu->header.offset_size;
9037 break;
36586728
TT
9038 case DW_FORM_GNU_ref_alt:
9039 info_ptr += cu->header.offset_size;
9040 break;
ae411497 9041 case DW_FORM_addr:
4bb7a0a7
DJ
9042 info_ptr += cu->header.addr_size;
9043 break;
9044 case DW_FORM_data1:
9045 case DW_FORM_ref1:
9046 case DW_FORM_flag:
8fe0f950 9047 case DW_FORM_strx1:
4bb7a0a7
DJ
9048 info_ptr += 1;
9049 break;
2dc7f7b3 9050 case DW_FORM_flag_present:
43988095 9051 case DW_FORM_implicit_const:
2dc7f7b3 9052 break;
4bb7a0a7
DJ
9053 case DW_FORM_data2:
9054 case DW_FORM_ref2:
8fe0f950 9055 case DW_FORM_strx2:
4bb7a0a7
DJ
9056 info_ptr += 2;
9057 break;
8fe0f950
AT
9058 case DW_FORM_strx3:
9059 info_ptr += 3;
9060 break;
4bb7a0a7
DJ
9061 case DW_FORM_data4:
9062 case DW_FORM_ref4:
8fe0f950 9063 case DW_FORM_strx4:
4bb7a0a7
DJ
9064 info_ptr += 4;
9065 break;
9066 case DW_FORM_data8:
9067 case DW_FORM_ref8:
55f1336d 9068 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9069 info_ptr += 8;
9070 break;
0224619f
JK
9071 case DW_FORM_data16:
9072 info_ptr += 16;
9073 break;
4bb7a0a7 9074 case DW_FORM_string:
9b1c24c8 9075 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9076 info_ptr += bytes_read;
9077 break;
2dc7f7b3 9078 case DW_FORM_sec_offset:
4bb7a0a7 9079 case DW_FORM_strp:
36586728 9080 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9081 info_ptr += cu->header.offset_size;
9082 break;
2dc7f7b3 9083 case DW_FORM_exprloc:
4bb7a0a7
DJ
9084 case DW_FORM_block:
9085 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9086 info_ptr += bytes_read;
9087 break;
9088 case DW_FORM_block1:
9089 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9090 break;
9091 case DW_FORM_block2:
9092 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9093 break;
9094 case DW_FORM_block4:
9095 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9096 break;
336d760d 9097 case DW_FORM_addrx:
cf532bd1 9098 case DW_FORM_strx:
4bb7a0a7
DJ
9099 case DW_FORM_sdata:
9100 case DW_FORM_udata:
9101 case DW_FORM_ref_udata:
3019eac3
DE
9102 case DW_FORM_GNU_addr_index:
9103 case DW_FORM_GNU_str_index:
18a8505e 9104 case DW_FORM_rnglistx:
41144253 9105 case DW_FORM_loclistx:
d521ce57 9106 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9107 break;
9108 case DW_FORM_indirect:
9109 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9110 info_ptr += bytes_read;
9111 /* We need to continue parsing from here, so just go back to
9112 the top. */
9113 goto skip_attribute;
9114
9115 default:
3e43a32a
MS
9116 error (_("Dwarf Error: Cannot handle %s "
9117 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9118 dwarf_form_name (form),
9119 bfd_get_filename (abfd));
9120 }
9121 }
9122
9123 if (abbrev->has_children)
dee91e82 9124 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9125 else
9126 return info_ptr;
9127}
9128
93311388 9129/* Locate ORIG_PDI's sibling.
dee91e82 9130 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9131
d521ce57 9132static const gdb_byte *
dee91e82
DE
9133locate_pdi_sibling (const struct die_reader_specs *reader,
9134 struct partial_die_info *orig_pdi,
d521ce57 9135 const gdb_byte *info_ptr)
91c24f0a
DC
9136{
9137 /* Do we know the sibling already? */
72bf9492 9138
91c24f0a
DC
9139 if (orig_pdi->sibling)
9140 return orig_pdi->sibling;
9141
9142 /* Are there any children to deal with? */
9143
9144 if (!orig_pdi->has_children)
9145 return info_ptr;
9146
4bb7a0a7 9147 /* Skip the children the long way. */
91c24f0a 9148
dee91e82 9149 return skip_children (reader, info_ptr);
91c24f0a
DC
9150}
9151
257e7a09 9152/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9153 not NULL. */
c906108c 9154
891813be
TT
9155void
9156dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 9157{
976ca316 9158 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 9159
976ca316 9160 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 9161
077cbab2
TT
9162 /* If this psymtab is constructed from a debug-only objfile, the
9163 has_section_at_zero flag will not necessarily be correct. We
9164 can get the correct value for this flag by looking at the data
9165 associated with the (presumably stripped) associated objfile. */
9166 if (objfile->separate_debug_objfile_backlink)
c906108c 9167 {
976ca316 9168 dwarf2_per_objfile *per_objfile_backlink
077cbab2 9169 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 9170
976ca316
SM
9171 per_objfile->per_bfd->has_section_at_zero
9172 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 9173 }
98bfdba5 9174
8566b89b 9175 expand_psymtab (objfile);
95554aad 9176
976ca316 9177 process_cu_includes (per_objfile);
c906108c 9178}
9cdd5dbd
DE
9179\f
9180/* Reading in full CUs. */
c906108c 9181
10b3939b
DJ
9182/* Add PER_CU to the queue. */
9183
9184static void
120ce1b5
SM
9185queue_comp_unit (dwarf2_per_cu_data *per_cu,
9186 dwarf2_per_objfile *per_objfile,
95554aad 9187 enum language pretend_language)
10b3939b 9188{
10b3939b 9189 per_cu->queued = 1;
08ac5771
SM
9190
9191 gdb_assert (per_objfile->per_bfd->queue.has_value ());
9192 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
9193}
9194
616c069a
SM
9195/* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
9196
89e63ee4
DE
9197 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9198 dependency.
69d751e3 9199
616c069a
SM
9200 Return true if maybe_queue_comp_unit requires the caller to load the CU's
9201 DIEs, false otherwise.
9202
9203 Explanation: there is an invariant that if a CU is queued for expansion
9204 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
9205 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
9206 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
9207 are not yet loaded, the the caller must load the CU's DIEs to ensure the
9208 invariant is respected.
9209
9210 The caller is therefore not required to load the CU's DIEs (we return false)
9211 if:
9212
9213 - the CU is already expanded, and therefore does not get enqueued
9214 - the CU gets enqueued for expansion, but its DIEs are already loaded
9215
9216 Note that the caller should not use this function's return value as an
9217 indicator of whether the CU's DIEs are loaded right now, it should check
9218 that by calling `dwarf2_per_objfile::get_cu` instead. */
0907af0c
DE
9219
9220static int
89e63ee4 9221maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
9222 dwarf2_per_cu_data *per_cu,
9223 dwarf2_per_objfile *per_objfile,
0907af0c
DE
9224 enum language pretend_language)
9225{
9226 /* We may arrive here during partial symbol reading, if we need full
9227 DIEs to process an unusual case (e.g. template arguments). Do
9228 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 9229 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 9230 {
7188ed02
SM
9231 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9232
9233 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
9234 return 1;
9235 return 0;
9236 }
9237
9238 /* Mark the dependence relation so that we don't flush PER_CU
9239 too early. */
89e63ee4
DE
9240 if (dependent_cu != NULL)
9241 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9242
9243 /* If it's already on the queue, we have nothing to do. */
9244 if (per_cu->queued)
de53369b
SM
9245 {
9246 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
9247 loaded. */
9248 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
616c069a
SM
9249
9250 /* If the CU is queued for expansion, it should not already be
9251 expanded. */
9252 gdb_assert (!per_objfile->symtab_set_p (per_cu));
9253
9254 /* The DIEs are already loaded, the caller doesn't need to do it. */
de53369b
SM
9255 return 0;
9256 }
0907af0c 9257
616c069a
SM
9258 bool queued = false;
9259 if (!per_objfile->symtab_set_p (per_cu))
9260 {
9261 /* Add it to the queue. */
9262 queue_comp_unit (per_cu, per_objfile, pretend_language);
9263 queued = true;
9264 }
9265
0907af0c
DE
9266 /* If the compilation unit is already loaded, just mark it as
9267 used. */
7188ed02
SM
9268 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9269 if (cu != nullptr)
616c069a 9270 cu->last_used = 0;
0907af0c 9271
616c069a
SM
9272 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
9273 and the DIEs are not already loaded. */
9274 return queued && cu == nullptr;
0907af0c
DE
9275}
9276
10b3939b
DJ
9277/* Process the queue. */
9278
9279static void
976ca316 9280process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 9281{
6f738b01
SM
9282 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9283 objfile_name (per_objfile->objfile));
45cfd468 9284
03dd20cc
DJ
9285 /* The queue starts out with one item, but following a DIE reference
9286 may load a new CU, adding it to the end of the queue. */
08ac5771 9287 while (!per_objfile->per_bfd->queue->empty ())
10b3939b 9288 {
08ac5771 9289 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
7188ed02 9290 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 9291
976ca316 9292 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 9293 {
976ca316 9294 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 9295
7188ed02
SM
9296 /* Skip dummy CUs. */
9297 if (cu != nullptr)
73be47f5 9298 {
7188ed02
SM
9299 unsigned int debug_print_threshold;
9300 char buf[100];
9301
9302 if (per_cu->is_debug_types)
9303 {
9304 struct signatured_type *sig_type =
9305 (struct signatured_type *) per_cu;
9306
9307 sprintf (buf, "TU %s at offset %s",
9308 hex_string (sig_type->signature),
9309 sect_offset_str (per_cu->sect_off));
9310 /* There can be 100s of TUs.
9311 Only print them in verbose mode. */
9312 debug_print_threshold = 2;
9313 }
9314 else
9315 {
9316 sprintf (buf, "CU at offset %s",
9317 sect_offset_str (per_cu->sect_off));
9318 debug_print_threshold = 1;
9319 }
247f5c4f 9320
7188ed02 9321 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 9322 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
f4dc4d17 9323
7188ed02
SM
9324 if (per_cu->is_debug_types)
9325 process_full_type_unit (cu, item.pretend_language);
9326 else
9327 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 9328
7188ed02 9329 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 9330 dwarf_read_debug_printf ("Done expanding %s", buf);
7188ed02 9331 }
f4dc4d17 9332 }
10b3939b 9333
7188ed02 9334 per_cu->queued = 0;
08ac5771 9335 per_objfile->per_bfd->queue->pop ();
10b3939b
DJ
9336 }
9337
6f738b01
SM
9338 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9339 objfile_name (per_objfile->objfile));
10b3939b
DJ
9340}
9341
10b3939b
DJ
9342/* Read in full symbols for PST, and anything it depends on. */
9343
8566b89b
TT
9344void
9345dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9346{
af758d11 9347 gdb_assert (!readin_p (objfile));
95554aad 9348
17ee85fc
TT
9349 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9350 free_cached_comp_units freer (per_objfile);
48993951 9351 expand_dependencies (objfile);
aaa75496 9352
97a1449a 9353 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9354 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9355}
9356
af758d11
SM
9357/* See psympriv.h. */
9358
9359bool
9360dwarf2_psymtab::readin_p (struct objfile *objfile) const
9361{
9362 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9363 return per_objfile->symtab_set_p (per_cu_data);
9364}
9365
9366/* See psympriv.h. */
9367
9368compunit_symtab *
9369dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9370{
9371 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9372 return per_objfile->get_symtab (per_cu_data);
9373}
9374
dee91e82
DE
9375/* Trivial hash function for die_info: the hash value of a DIE
9376 is its offset in .debug_info for this objfile. */
10b3939b 9377
dee91e82
DE
9378static hashval_t
9379die_hash (const void *item)
10b3939b 9380{
9a3c8263 9381 const struct die_info *die = (const struct die_info *) item;
6502dd73 9382
9c541725 9383 return to_underlying (die->sect_off);
dee91e82 9384}
63d06c5c 9385
dee91e82
DE
9386/* Trivial comparison function for die_info structures: two DIEs
9387 are equal if they have the same offset. */
98bfdba5 9388
dee91e82
DE
9389static int
9390die_eq (const void *item_lhs, const void *item_rhs)
9391{
9a3c8263
SM
9392 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9393 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9394
9c541725 9395 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9396}
c906108c 9397
4a636814
SM
9398/* Load the DIEs associated with PER_CU into memory.
9399
9400 In some cases, the caller, while reading partial symbols, will need to load
9401 the full symbols for the CU for some reason. It will already have a
9402 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9403 rather than creating a new one. */
c906108c 9404
dee91e82 9405static void
ab432490
SM
9406load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9407 dwarf2_per_objfile *per_objfile,
4a636814 9408 dwarf2_cu *existing_cu,
c0ab21c2
TT
9409 bool skip_partial,
9410 enum language pretend_language)
dee91e82 9411{
c0ab21c2
TT
9412 gdb_assert (! this_cu->is_debug_types);
9413
7188ed02 9414 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
9415 if (reader.dummy_p)
9416 return;
9417
9418 struct dwarf2_cu *cu = reader.cu;
9419 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9420
dee91e82
DE
9421 gdb_assert (cu->die_hash == NULL);
9422 cu->die_hash =
9423 htab_create_alloc_ex (cu->header.length / 12,
9424 die_hash,
9425 die_eq,
9426 NULL,
9427 &cu->comp_unit_obstack,
9428 hashtab_obstack_allocate,
9429 dummy_obstack_deallocate);
e142c38c 9430
3e225074 9431 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9432 reader.comp_unit_die->child
9433 = read_die_and_siblings (&reader, reader.info_ptr,
9434 &info_ptr, reader.comp_unit_die);
9435 cu->dies = reader.comp_unit_die;
dee91e82 9436 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9437
9438 /* We try not to read any attributes in this function, because not
9cdd5dbd 9439 all CUs needed for references have been loaded yet, and symbol
10b3939b 9440 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9441 or we won't be able to build types correctly.
9442 Similarly, if we do not read the producer, we can not apply
9443 producer-specific interpretation. */
c0ab21c2 9444 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9445
9446 reader.keep ();
10b3939b
DJ
9447}
9448
3da10d80
KS
9449/* Add a DIE to the delayed physname list. */
9450
9451static void
9452add_to_method_list (struct type *type, int fnfield_index, int index,
9453 const char *name, struct die_info *die,
9454 struct dwarf2_cu *cu)
9455{
9456 struct delayed_method_info mi;
9457 mi.type = type;
9458 mi.fnfield_index = fnfield_index;
9459 mi.index = index;
9460 mi.name = name;
9461 mi.die = die;
c89b44cd 9462 cu->method_list.push_back (mi);
3da10d80
KS
9463}
9464
3693fdb3
PA
9465/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9466 "const" / "volatile". If so, decrements LEN by the length of the
9467 modifier and return true. Otherwise return false. */
9468
9469template<size_t N>
9470static bool
9471check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9472{
9473 size_t mod_len = sizeof (mod) - 1;
9474 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9475 {
9476 len -= mod_len;
9477 return true;
9478 }
9479 return false;
9480}
9481
3da10d80
KS
9482/* Compute the physnames of any methods on the CU's method list.
9483
9484 The computation of method physnames is delayed in order to avoid the
9485 (bad) condition that one of the method's formal parameters is of an as yet
9486 incomplete type. */
9487
9488static void
9489compute_delayed_physnames (struct dwarf2_cu *cu)
9490{
3693fdb3 9491 /* Only C++ delays computing physnames. */
c89b44cd 9492 if (cu->method_list.empty ())
3693fdb3
PA
9493 return;
9494 gdb_assert (cu->language == language_cplus);
9495
52941706 9496 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9497 {
1d06ead6 9498 const char *physname;
3da10d80 9499 struct fn_fieldlist *fn_flp
c89b44cd
TT
9500 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9501 physname = dwarf2_physname (mi.name, mi.die, cu);
9502 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9503 = physname ? physname : "";
3693fdb3
PA
9504
9505 /* Since there's no tag to indicate whether a method is a
9506 const/volatile overload, extract that information out of the
9507 demangled name. */
9508 if (physname != NULL)
9509 {
9510 size_t len = strlen (physname);
9511
9512 while (1)
9513 {
9514 if (physname[len] == ')') /* shortcut */
9515 break;
9516 else if (check_modifier (physname, len, " const"))
c89b44cd 9517 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9518 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9519 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9520 else
9521 break;
9522 }
9523 }
3da10d80 9524 }
c89b44cd
TT
9525
9526 /* The list is no longer needed. */
9527 cu->method_list.clear ();
3da10d80
KS
9528}
9529
a766d390
DE
9530/* Go objects should be embedded in a DW_TAG_module DIE,
9531 and it's not clear if/how imported objects will appear.
9532 To keep Go support simple until that's worked out,
9533 go back through what we've read and create something usable.
9534 We could do this while processing each DIE, and feels kinda cleaner,
9535 but that way is more invasive.
9536 This is to, for example, allow the user to type "p var" or "b main"
9537 without having to specify the package name, and allow lookups
9538 of module.object to work in contexts that use the expression
9539 parser. */
9540
9541static void
9542fixup_go_packaging (struct dwarf2_cu *cu)
9543{
421d1616 9544 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9545 struct pending *list;
9546 int i;
9547
c24bdb02 9548 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9549 list != NULL;
9550 list = list->next)
a766d390
DE
9551 {
9552 for (i = 0; i < list->nsyms; ++i)
9553 {
9554 struct symbol *sym = list->symbol[i];
9555
c1b5c1eb 9556 if (sym->language () == language_go
a766d390
DE
9557 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9558 {
421d1616
TT
9559 gdb::unique_xmalloc_ptr<char> this_package_name
9560 (go_symbol_package_name (sym));
a766d390
DE
9561
9562 if (this_package_name == NULL)
9563 continue;
9564 if (package_name == NULL)
421d1616 9565 package_name = std::move (this_package_name);
a766d390
DE
9566 else
9567 {
5e22e966 9568 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9569 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9570 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9571 (symbol_symtab (sym) != NULL
9572 ? symtab_to_filename_for_display
9573 (symbol_symtab (sym))
e3b94546 9574 : objfile_name (objfile)),
421d1616 9575 this_package_name.get (), package_name.get ());
a766d390
DE
9576 }
9577 }
9578 }
9579 }
9580
9581 if (package_name != NULL)
9582 {
5e22e966 9583 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9584 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9585 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9586 saved_package_name);
a766d390
DE
9587 struct symbol *sym;
9588
8c14c3a3 9589 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9590 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9591 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9592 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9593 e.g., "main" finds the "main" module and not C's main(). */
9594 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9595 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9596 SYMBOL_TYPE (sym) = type;
9597
c24bdb02 9598 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9599 }
9600}
9601
c9317f21
TT
9602/* Allocate a fully-qualified name consisting of the two parts on the
9603 obstack. */
9604
9605static const char *
9606rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9607{
9608 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9609}
9610
9c6a1327
TT
9611/* A helper that allocates a variant part to attach to a Rust enum
9612 type. OBSTACK is where the results should be allocated. TYPE is
9613 the type we're processing. DISCRIMINANT_INDEX is the index of the
57d02173
TT
9614 discriminant. It must be the index of one of the fields of TYPE,
9615 or -1 to mean there is no discriminant (univariant enum).
9c6a1327
TT
9616 DEFAULT_INDEX is the index of the default field; or -1 if there is
9617 no default. RANGES is indexed by "effective" field number (the
9618 field index, but omitting the discriminant and default fields) and
9619 must hold the discriminant values used by the variants. Note that
9620 RANGES must have a lifetime at least as long as OBSTACK -- either
9621 already allocated on it, or static. */
c9317f21 9622
9c6a1327
TT
9623static void
9624alloc_rust_variant (struct obstack *obstack, struct type *type,
9625 int discriminant_index, int default_index,
9626 gdb::array_view<discriminant_range> ranges)
9627{
57d02173
TT
9628 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9629 gdb_assert (discriminant_index == -1
9630 || (discriminant_index >= 0
9631 && discriminant_index < type->num_fields ()));
c9317f21 9632 gdb_assert (default_index == -1
1f704f76 9633 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9634
9c6a1327 9635 /* We have one variant for each non-discriminant field. */
57d02173
TT
9636 int n_variants = type->num_fields ();
9637 if (discriminant_index != -1)
9638 --n_variants;
c9317f21 9639
9c6a1327
TT
9640 variant *variants = new (obstack) variant[n_variants];
9641 int var_idx = 0;
9642 int range_idx = 0;
1f704f76 9643 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9644 {
9645 if (i == discriminant_index)
9646 continue;
c9317f21 9647
9c6a1327
TT
9648 variants[var_idx].first_field = i;
9649 variants[var_idx].last_field = i + 1;
9650
9651 /* The default field does not need a range, but other fields do.
9652 We skipped the discriminant above. */
9653 if (i != default_index)
9654 {
9655 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9656 ++range_idx;
9657 }
c9317f21 9658
9c6a1327
TT
9659 ++var_idx;
9660 }
9661
9662 gdb_assert (range_idx == ranges.size ());
9663 gdb_assert (var_idx == n_variants);
9664
9665 variant_part *part = new (obstack) variant_part;
9666 part->discriminant_index = discriminant_index;
57d02173
TT
9667 /* If there is no discriminant, then whether it is signed is of no
9668 consequence. */
9669 part->is_unsigned
9670 = (discriminant_index == -1
9671 ? false
c6d940a9 9672 : type->field (discriminant_index).type ()->is_unsigned ());
9c6a1327
TT
9673 part->variants = gdb::array_view<variant> (variants, n_variants);
9674
9675 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9676 gdb::array_view<variant_part> *prop_value
9677 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9678
9c6a1327 9679 struct dynamic_prop prop;
8c2e4e06 9680 prop.set_variant_parts (prop_value);
9c6a1327 9681
5c54719c 9682 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9683}
9684
9685/* Some versions of rustc emitted enums in an unusual way.
9686
9687 Ordinary enums were emitted as unions. The first element of each
9688 structure in the union was named "RUST$ENUM$DISR". This element
9689 held the discriminant.
9690
9691 These versions of Rust also implemented the "non-zero"
9692 optimization. When the enum had two values, and one is empty and
9693 the other holds a pointer that cannot be zero, the pointer is used
9694 as the discriminant, with a zero value meaning the empty variant.
9695 Here, the union's first member is of the form
9696 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9697 where the fieldnos are the indices of the fields that should be
9698 traversed in order to find the field (which may be several fields deep)
9699 and the variantname is the name of the variant of the case when the
9700 field is zero.
9701
9702 This function recognizes whether TYPE is of one of these forms,
9703 and, if so, smashes it to be a variant type. */
9704
9705static void
9706quirk_rust_enum (struct type *type, struct objfile *objfile)
9707{
78134374 9708 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9709
9710 /* We don't need to deal with empty enums. */
1f704f76 9711 if (type->num_fields () == 0)
c9317f21
TT
9712 return;
9713
9714#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9715 if (type->num_fields () == 1
c9317f21
TT
9716 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9717 {
9718 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9719
9720 /* Decode the field name to find the offset of the
9721 discriminant. */
9722 ULONGEST bit_offset = 0;
940da03e 9723 struct type *field_type = type->field (0).type ();
c9317f21
TT
9724 while (name[0] >= '0' && name[0] <= '9')
9725 {
9726 char *tail;
9727 unsigned long index = strtoul (name, &tail, 10);
9728 name = tail;
9729 if (*name != '$'
1f704f76 9730 || index >= field_type->num_fields ()
c9317f21
TT
9731 || (TYPE_FIELD_LOC_KIND (field_type, index)
9732 != FIELD_LOC_KIND_BITPOS))
9733 {
b98664d3 9734 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9735 "[in module %s]"),
9736 TYPE_FIELD_NAME (type, 0),
9737 objfile_name (objfile));
9738 return;
9739 }
9740 ++name;
9741
9742 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 9743 field_type = field_type->field (index).type ();
c9317f21
TT
9744 }
9745
9c6a1327
TT
9746 /* Smash this type to be a structure type. We have to do this
9747 because the type has already been recorded. */
67607e24 9748 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9749 type->set_num_fields (3);
9c6a1327 9750 /* Save the field we care about. */
ceacbf6e 9751 struct field saved_field = type->field (0);
3cabb6b0
SM
9752 type->set_fields
9753 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9754
9c6a1327 9755 /* Put the discriminant at index 0. */
5d14b6e5 9756 type->field (0).set_type (field_type);
9c6a1327
TT
9757 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9758 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9759 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9760
9761 /* The order of fields doesn't really matter, so put the real
9762 field at index 1 and the data-less field at index 2. */
ceacbf6e 9763 type->field (1) = saved_field;
9c6a1327 9764 TYPE_FIELD_NAME (type, 1)
940da03e
SM
9765 = rust_last_path_segment (type->field (1).type ()->name ());
9766 type->field (1).type ()->set_name
7d93a1e0 9767 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9768 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9769
9770 const char *dataless_name
7d93a1e0 9771 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9772 name);
9773 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9774 dataless_name);
5d14b6e5 9775 type->field (2).set_type (dataless_type);
c9317f21
TT
9776 /* NAME points into the original discriminant name, which
9777 already has the correct lifetime. */
9c6a1327 9778 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9779 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9780
9c6a1327
TT
9781 /* Indicate that this is a variant type. */
9782 static discriminant_range ranges[1] = { { 0, 0 } };
9783 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9784 }
77c2dba3
TT
9785 /* A union with a single anonymous field is probably an old-style
9786 univariant enum. */
1f704f76 9787 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9788 {
c9317f21
TT
9789 /* Smash this type to be a structure type. We have to do this
9790 because the type has already been recorded. */
67607e24 9791 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9792
940da03e 9793 struct type *field_type = type->field (0).type ();
c9317f21 9794 const char *variant_name
7d93a1e0 9795 = rust_last_path_segment (field_type->name ());
9c6a1327 9796 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9797 field_type->set_name
9798 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9799 type->name (), variant_name));
57d02173
TT
9800
9801 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
c9317f21
TT
9802 }
9803 else
9804 {
9805 struct type *disr_type = nullptr;
1f704f76 9806 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9807 {
940da03e 9808 disr_type = type->field (i).type ();
c9317f21 9809
78134374 9810 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9811 {
9812 /* All fields of a true enum will be structs. */
9813 return;
9814 }
1f704f76 9815 else if (disr_type->num_fields () == 0)
c9317f21
TT
9816 {
9817 /* Could be data-less variant, so keep going. */
a037790e 9818 disr_type = nullptr;
c9317f21
TT
9819 }
9820 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9821 "RUST$ENUM$DISR") != 0)
9822 {
9823 /* Not a Rust enum. */
9824 return;
9825 }
9826 else
9827 {
9828 /* Found one. */
9829 break;
9830 }
9831 }
9832
9833 /* If we got here without a discriminant, then it's probably
9834 just a union. */
9835 if (disr_type == nullptr)
9836 return;
9837
9838 /* Smash this type to be a structure type. We have to do this
9839 because the type has already been recorded. */
67607e24 9840 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9841
9c6a1327 9842 /* Make space for the discriminant field. */
ceacbf6e 9843 struct field *disr_field = &disr_type->field (0);
9c6a1327 9844 field *new_fields
1f704f76 9845 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9846 * sizeof (struct field)));
80fc5e77 9847 memcpy (new_fields + 1, type->fields (),
1f704f76 9848 type->num_fields () * sizeof (struct field));
3cabb6b0 9849 type->set_fields (new_fields);
1f704f76 9850 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9851
9852 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9853 type->field (0) = *disr_field;
9c6a1327
TT
9854 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9855 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9856
9857 /* We need a way to find the correct discriminant given a
9858 variant name. For convenience we build a map here. */
b6cdac4b 9859 struct type *enum_type = disr_field->type ();
c9317f21 9860 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9861 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9862 {
9863 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9864 {
9865 const char *name
9866 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9867 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9868 }
9869 }
9870
1f704f76 9871 int n_fields = type->num_fields ();
9c6a1327
TT
9872 /* We don't need a range entry for the discriminant, but we do
9873 need one for every other field, as there is no default
9874 variant. */
9875 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9876 discriminant_range,
9877 n_fields - 1);
c9317f21
TT
9878 /* Skip the discriminant here. */
9879 for (int i = 1; i < n_fields; ++i)
9880 {
9881 /* Find the final word in the name of this variant's type.
9882 That name can be used to look up the correct
9883 discriminant. */
9884 const char *variant_name
940da03e 9885 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9886
9887 auto iter = discriminant_map.find (variant_name);
9888 if (iter != discriminant_map.end ())
9c6a1327 9889 {
57d02173
TT
9890 ranges[i - 1].low = iter->second;
9891 ranges[i - 1].high = iter->second;
9c6a1327 9892 }
c9317f21 9893
57d02173
TT
9894 /* In Rust, each element should have the size of the
9895 enclosing enum. */
9896 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9897
bedda9ac 9898 /* Remove the discriminant field, if it exists. */
940da03e 9899 struct type *sub_type = type->field (i).type ();
1f704f76 9900 if (sub_type->num_fields () > 0)
bedda9ac 9901 {
5e33d5f4 9902 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9903 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9904 }
9c6a1327 9905 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9906 sub_type->set_name
9907 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9908 type->name (), variant_name));
c9317f21 9909 }
9c6a1327
TT
9910
9911 /* Indicate that this is a variant type. */
a1520ad8 9912 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9913 gdb::array_view<discriminant_range> (ranges,
9914 n_fields - 1));
c9317f21
TT
9915 }
9916}
9917
9918/* Rewrite some Rust unions to be structures with variants parts. */
9919
9920static void
9921rust_union_quirks (struct dwarf2_cu *cu)
9922{
9923 gdb_assert (cu->language == language_rust);
52941706 9924 for (type *type_ : cu->rust_unions)
5e22e966 9925 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9926 /* We don't need this any more. */
9927 cu->rust_unions.clear ();
c9317f21
TT
9928}
9929
8adb8487
TT
9930/* See read.h. */
9931
9932type_unit_group_unshareable *
9933dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9934{
9935 auto iter = this->m_type_units.find (tu_group);
9936 if (iter != this->m_type_units.end ())
9937 return iter->second.get ();
9938
9939 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9940 type_unit_group_unshareable *result = uniq.get ();
9941 this->m_type_units[tu_group] = std::move (uniq);
9942 return result;
9943}
9944
e286671b
TT
9945struct type *
9946dwarf2_per_objfile::get_type_for_signatured_type
9947 (signatured_type *sig_type) const
9948{
9949 auto iter = this->m_type_map.find (sig_type);
9950 if (iter == this->m_type_map.end ())
9951 return nullptr;
9952
9953 return iter->second;
9954}
9955
9956void dwarf2_per_objfile::set_type_for_signatured_type
9957 (signatured_type *sig_type, struct type *type)
9958{
9959 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9960
9961 this->m_type_map[sig_type] = type;
9962}
9963
95554aad
TT
9964/* A helper function for computing the list of all symbol tables
9965 included by PER_CU. */
9966
9967static void
4c39bc03 9968recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9969 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9970 dwarf2_per_cu_data *per_cu,
9971 dwarf2_per_objfile *per_objfile,
43f3e411 9972 struct compunit_symtab *immediate_parent)
95554aad 9973{
af758d11 9974 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9975 if (*slot != NULL)
9976 {
9977 /* This inclusion and its children have been processed. */
9978 return;
9979 }
9980
9981 *slot = per_cu;
af758d11 9982
95554aad 9983 /* Only add a CU if it has a symbol table. */
43182c09 9984 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9985 if (cust != NULL)
ec94af83
DE
9986 {
9987 /* If this is a type unit only add its symbol table if we haven't
9988 seen it yet (type unit per_cu's can share symtabs). */
9989 if (per_cu->is_debug_types)
9990 {
43f3e411 9991 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9992 if (*slot == NULL)
9993 {
43f3e411 9994 *slot = cust;
4c39bc03 9995 result->push_back (cust);
43f3e411
DE
9996 if (cust->user == NULL)
9997 cust->user = immediate_parent;
ec94af83
DE
9998 }
9999 }
10000 else
f9125b6c 10001 {
4c39bc03 10002 result->push_back (cust);
43f3e411
DE
10003 if (cust->user == NULL)
10004 cust->user = immediate_parent;
f9125b6c 10005 }
ec94af83 10006 }
95554aad 10007
ae640021
AB
10008 if (!per_cu->imported_symtabs_empty ())
10009 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10010 {
10011 recursively_compute_inclusions (result, all_children,
43182c09
SM
10012 all_type_symtabs, ptr, per_objfile,
10013 cust);
ae640021 10014 }
95554aad
TT
10015}
10016
43f3e411 10017/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10018 PER_CU. */
10019
10020static void
43182c09
SM
10021compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
10022 dwarf2_per_objfile *per_objfile)
95554aad 10023{
f4dc4d17
DE
10024 gdb_assert (! per_cu->is_debug_types);
10025
ae640021 10026 if (!per_cu->imported_symtabs_empty ())
95554aad 10027 {
ae640021 10028 int len;
4c39bc03 10029 std::vector<compunit_symtab *> result_symtabs;
43182c09 10030 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
10031
10032 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10033 if (cust == NULL)
95554aad
TT
10034 return;
10035
280a9412
TT
10036 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
10037 htab_eq_pointer,
10038 NULL, xcalloc, xfree));
10039 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
10040 htab_eq_pointer,
10041 NULL, xcalloc, xfree));
95554aad 10042
ae640021 10043 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83 10044 {
280a9412
TT
10045 recursively_compute_inclusions (&result_symtabs, all_children.get (),
10046 all_type_symtabs.get (), ptr,
10047 per_objfile, cust);
ec94af83 10048 }
95554aad 10049
ec94af83 10050 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 10051 len = result_symtabs.size ();
43f3e411 10052 cust->includes
f6e649dd 10053 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 10054 struct compunit_symtab *, len + 1);
4c39bc03
TT
10055 memcpy (cust->includes, result_symtabs.data (),
10056 len * sizeof (compunit_symtab *));
43f3e411 10057 cust->includes[len] = NULL;
95554aad
TT
10058 }
10059}
10060
10061/* Compute the 'includes' field for the symtabs of all the CUs we just
10062 read. */
10063
10064static void
976ca316 10065process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 10066{
976ca316 10067 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
10068 {
10069 if (! iter->is_debug_types)
976ca316 10070 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 10071 }
95554aad 10072
976ca316 10073 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
10074}
10075
8fc0b21d 10076/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
10077 already been loaded into memory. */
10078
10079static void
8fc0b21d 10080process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 10081{
976ca316
SM
10082 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10083 struct objfile *objfile = per_objfile->objfile;
08feed99 10084 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 10085 CORE_ADDR lowpc, highpc;
43f3e411 10086 struct compunit_symtab *cust;
10b3939b 10087 CORE_ADDR baseaddr;
4359dff1 10088 struct block *static_block;
3e29f34a 10089 CORE_ADDR addr;
10b3939b 10090
b3b3bada 10091 baseaddr = objfile->text_section_offset ();
10b3939b 10092
c89b44cd
TT
10093 /* Clear the list here in case something was left over. */
10094 cu->method_list.clear ();
10b3939b 10095
95554aad
TT
10096 cu->language = pretend_language;
10097 cu->language_defn = language_def (cu->language);
10098
1c47ec3e
TV
10099 dwarf2_find_base_address (cu->dies, cu);
10100
c906108c 10101 /* Do line number decoding in read_file_scope () */
10b3939b 10102 process_die (cu->dies, cu);
c906108c 10103
a766d390
DE
10104 /* For now fudge the Go package. */
10105 if (cu->language == language_go)
10106 fixup_go_packaging (cu);
10107
5f48f8f3 10108 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10109 should be complete, and it should now be safe to compute all of the
10110 physnames. */
10111 compute_delayed_physnames (cu);
3da10d80 10112
c9317f21
TT
10113 if (cu->language == language_rust)
10114 rust_union_quirks (cu);
10115
fae299cd
DC
10116 /* Some compilers don't define a DW_AT_high_pc attribute for the
10117 compilation unit. If the DW_AT_high_pc is missing, synthesize
10118 it, by scanning the DIE's below the compilation unit. */
10b3939b 10119 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10120
3e29f34a 10121 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10122 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10123
10124 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10125 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10126 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10127 addrmap to help ensure it has an accurate map of pc values belonging to
10128 this comp unit. */
10129 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10130
c24bdb02 10131 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10132 SECT_OFF_TEXT (objfile),
10133 0);
c906108c 10134
43f3e411 10135 if (cust != NULL)
c906108c 10136 {
df15bd07 10137 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10138
8be455d7
JK
10139 /* Set symtab language to language from DW_AT_language. If the
10140 compilation is from a C file generated by language preprocessors, do
10141 not set the language if it was already deduced by start_subfile. */
43f3e411 10142 if (!(cu->language == language_c
40e3ad0e 10143 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10144 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10145
10146 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10147 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10148 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10149 there were bugs in prologue debug info, fixed later in GCC-4.5
10150 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10151
10152 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10153 needed, it would be wrong due to missing DW_AT_producer there.
10154
10155 Still one can confuse GDB by using non-standard GCC compilation
10156 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10157 */
ab260dad 10158 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10159 cust->locations_valid = 1;
e0d00bc7
JK
10160
10161 if (gcc_4_minor >= 5)
43f3e411 10162 cust->epilogue_unwind_valid = 1;
96408a79 10163
43f3e411 10164 cust->call_site_htab = cu->call_site_htab;
c906108c 10165 }
9291a0cd 10166
976ca316 10167 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 10168
95554aad 10169 /* Push it for inclusion processing later. */
976ca316 10170 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
10171
10172 /* Not needed any more. */
c24bdb02 10173 cu->reset_builder ();
f4dc4d17 10174}
45cfd468 10175
8fc0b21d 10176/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
10177 already been loaded into memory. */
10178
10179static void
8fc0b21d 10180process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
10181 enum language pretend_language)
10182{
976ca316
SM
10183 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10184 struct objfile *objfile = per_objfile->objfile;
43f3e411 10185 struct compunit_symtab *cust;
0186c6a7
DE
10186 struct signatured_type *sig_type;
10187
8fc0b21d
SM
10188 gdb_assert (cu->per_cu->is_debug_types);
10189 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 10190
c89b44cd
TT
10191 /* Clear the list here in case something was left over. */
10192 cu->method_list.clear ();
f4dc4d17 10193
f4dc4d17
DE
10194 cu->language = pretend_language;
10195 cu->language_defn = language_def (cu->language);
10196
10197 /* The symbol tables are set up in read_type_unit_scope. */
10198 process_die (cu->dies, cu);
10199
10200 /* For now fudge the Go package. */
10201 if (cu->language == language_go)
10202 fixup_go_packaging (cu);
10203
5f48f8f3 10204 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10205 should be complete, and it should now be safe to compute all of the
10206 physnames. */
10207 compute_delayed_physnames (cu);
f4dc4d17 10208
c9317f21
TT
10209 if (cu->language == language_rust)
10210 rust_union_quirks (cu);
10211
f4dc4d17
DE
10212 /* TUs share symbol tables.
10213 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10214 of it with end_expandable_symtab. Otherwise, complete the addition of
10215 this TU's symbols to the existing symtab. */
8adb8487 10216 type_unit_group_unshareable *tug_unshare =
976ca316 10217 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 10218 if (tug_unshare->compunit_symtab == NULL)
45cfd468 10219 {
c24bdb02
KS
10220 buildsym_compunit *builder = cu->get_builder ();
10221 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 10222 tug_unshare->compunit_symtab = cust;
f4dc4d17 10223
43f3e411 10224 if (cust != NULL)
f4dc4d17
DE
10225 {
10226 /* Set symtab language to language from DW_AT_language. If the
10227 compilation is from a C file generated by language preprocessors,
10228 do not set the language if it was already deduced by
10229 start_subfile. */
43f3e411
DE
10230 if (!(cu->language == language_c
10231 && COMPUNIT_FILETABS (cust)->language != language_c))
10232 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10233 }
10234 }
10235 else
10236 {
c24bdb02 10237 cu->get_builder ()->augment_type_symtab ();
8adb8487 10238 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
10239 }
10240
976ca316 10241 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
10242
10243 /* Not needed any more. */
c24bdb02 10244 cu->reset_builder ();
c906108c
SS
10245}
10246
95554aad
TT
10247/* Process an imported unit DIE. */
10248
10249static void
10250process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10251{
10252 struct attribute *attr;
10253
f4dc4d17
DE
10254 /* For now we don't handle imported units in type units. */
10255 if (cu->per_cu->is_debug_types)
10256 {
10257 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10258 " supported in type units [in module %s]"),
5e22e966 10259 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
10260 }
10261
95554aad
TT
10262 attr = dwarf2_attr (die, DW_AT_import, cu);
10263 if (attr != NULL)
10264 {
0826b30a 10265 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 10266 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 10267 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 10268 dwarf2_per_cu_data *per_cu
ab432490 10269 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 10270
58990295
TV
10271 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10272 into another compilation unit, at root level. Regard this as a hint,
10273 and ignore it. */
10274 if (die->parent && die->parent->parent == NULL
10275 && per_cu->unit_type == DW_UT_compile
10276 && per_cu->lang == language_cplus)
10277 return;
10278
69d751e3 10279 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 10280 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
10281 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10282 false, cu->language);
95554aad 10283
ae640021 10284 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10285 }
10286}
10287
4c8aa72d
PA
10288/* RAII object that represents a process_die scope: i.e.,
10289 starts/finishes processing a DIE. */
10290class process_die_scope
adde2bff 10291{
4c8aa72d
PA
10292public:
10293 process_die_scope (die_info *die, dwarf2_cu *cu)
10294 : m_die (die), m_cu (cu)
10295 {
10296 /* We should only be processing DIEs not already in process. */
10297 gdb_assert (!m_die->in_process);
10298 m_die->in_process = true;
10299 }
8c3cb9fa 10300
4c8aa72d
PA
10301 ~process_die_scope ()
10302 {
10303 m_die->in_process = false;
10304
10305 /* If we're done processing the DIE for the CU that owns the line
10306 header, we don't need the line header anymore. */
10307 if (m_cu->line_header_die_owner == m_die)
10308 {
10309 delete m_cu->line_header;
10310 m_cu->line_header = NULL;
10311 m_cu->line_header_die_owner = NULL;
10312 }
10313 }
10314
10315private:
10316 die_info *m_die;
10317 dwarf2_cu *m_cu;
10318};
adde2bff 10319
c906108c
SS
10320/* Process a die and its children. */
10321
10322static void
e7c27a73 10323process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10324{
4c8aa72d 10325 process_die_scope scope (die, cu);
adde2bff 10326
c906108c
SS
10327 switch (die->tag)
10328 {
10329 case DW_TAG_padding:
10330 break;
10331 case DW_TAG_compile_unit:
95554aad 10332 case DW_TAG_partial_unit:
e7c27a73 10333 read_file_scope (die, cu);
c906108c 10334 break;
348e048f
DE
10335 case DW_TAG_type_unit:
10336 read_type_unit_scope (die, cu);
10337 break;
c906108c 10338 case DW_TAG_subprogram:
0a4b0913
AB
10339 /* Nested subprograms in Fortran get a prefix. */
10340 if (cu->language == language_fortran
10341 && die->parent != NULL
10342 && die->parent->tag == DW_TAG_subprogram)
10343 cu->processing_has_namespace_info = true;
10344 /* Fall through. */
c906108c 10345 case DW_TAG_inlined_subroutine:
edb3359d 10346 read_func_scope (die, cu);
c906108c
SS
10347 break;
10348 case DW_TAG_lexical_block:
14898363
L
10349 case DW_TAG_try_block:
10350 case DW_TAG_catch_block:
e7c27a73 10351 read_lexical_block_scope (die, cu);
c906108c 10352 break;
216f72a1 10353 case DW_TAG_call_site:
96408a79
SA
10354 case DW_TAG_GNU_call_site:
10355 read_call_site_scope (die, cu);
10356 break;
c906108c 10357 case DW_TAG_class_type:
680b30c7 10358 case DW_TAG_interface_type:
c906108c
SS
10359 case DW_TAG_structure_type:
10360 case DW_TAG_union_type:
134d01f1 10361 process_structure_scope (die, cu);
c906108c
SS
10362 break;
10363 case DW_TAG_enumeration_type:
134d01f1 10364 process_enumeration_scope (die, cu);
c906108c 10365 break;
134d01f1 10366
f792889a
DJ
10367 /* These dies have a type, but processing them does not create
10368 a symbol or recurse to process the children. Therefore we can
10369 read them on-demand through read_type_die. */
c906108c 10370 case DW_TAG_subroutine_type:
72019c9c 10371 case DW_TAG_set_type:
c906108c 10372 case DW_TAG_pointer_type:
c906108c 10373 case DW_TAG_ptr_to_member_type:
c906108c 10374 case DW_TAG_reference_type:
4297a3f0 10375 case DW_TAG_rvalue_reference_type:
c906108c 10376 case DW_TAG_string_type:
c906108c 10377 break;
134d01f1 10378
d8f62e84
TT
10379 case DW_TAG_array_type:
10380 /* We only need to handle this case for Ada -- in other
10381 languages, it's normal for the compiler to emit a typedef
10382 instead. */
10383 if (cu->language != language_ada)
10384 break;
10385 /* FALLTHROUGH */
c906108c 10386 case DW_TAG_base_type:
a02abb62 10387 case DW_TAG_subrange_type:
cb249c71 10388 case DW_TAG_typedef:
134d01f1 10389 /* Add a typedef symbol for the type definition, if it has a
dda83cd7 10390 DW_AT_name. */
f792889a 10391 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10392 break;
c906108c 10393 case DW_TAG_common_block:
e7c27a73 10394 read_common_block (die, cu);
c906108c
SS
10395 break;
10396 case DW_TAG_common_inclusion:
10397 break;
d9fa45fe 10398 case DW_TAG_namespace:
9068261f 10399 cu->processing_has_namespace_info = true;
e7c27a73 10400 read_namespace (die, cu);
d9fa45fe 10401 break;
5d7cb8df 10402 case DW_TAG_module:
9068261f 10403 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10404 read_module (die, cu);
10405 break;
d9fa45fe 10406 case DW_TAG_imported_declaration:
9068261f 10407 cu->processing_has_namespace_info = true;
74921315
KS
10408 if (read_namespace_alias (die, cu))
10409 break;
86a73007
TT
10410 /* The declaration is not a global namespace alias. */
10411 /* Fall through. */
d9fa45fe 10412 case DW_TAG_imported_module:
9068261f 10413 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10414 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10415 || cu->language != language_fortran))
b98664d3 10416 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10417 dwarf_tag_name (die->tag));
10418 read_import_statement (die, cu);
d9fa45fe 10419 break;
95554aad
TT
10420
10421 case DW_TAG_imported_unit:
10422 process_imported_unit_die (die, cu);
10423 break;
10424
71a3c369
TT
10425 case DW_TAG_variable:
10426 read_variable (die, cu);
10427 break;
10428
c906108c 10429 default:
e7c27a73 10430 new_symbol (die, NULL, cu);
c906108c
SS
10431 break;
10432 }
10433}
ca69b9e6
DE
10434\f
10435/* DWARF name computation. */
c906108c 10436
94af9270
KS
10437/* A helper function for dwarf2_compute_name which determines whether DIE
10438 needs to have the name of the scope prepended to the name listed in the
10439 die. */
10440
10441static int
10442die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10443{
1c809c68
TT
10444 struct attribute *attr;
10445
94af9270
KS
10446 switch (die->tag)
10447 {
10448 case DW_TAG_namespace:
10449 case DW_TAG_typedef:
10450 case DW_TAG_class_type:
10451 case DW_TAG_interface_type:
10452 case DW_TAG_structure_type:
10453 case DW_TAG_union_type:
10454 case DW_TAG_enumeration_type:
10455 case DW_TAG_enumerator:
10456 case DW_TAG_subprogram:
08a76f8a 10457 case DW_TAG_inlined_subroutine:
94af9270 10458 case DW_TAG_member:
74921315 10459 case DW_TAG_imported_declaration:
94af9270
KS
10460 return 1;
10461
10462 case DW_TAG_variable:
c2b0a229 10463 case DW_TAG_constant:
94af9270
KS
10464 /* We only need to prefix "globally" visible variables. These include
10465 any variable marked with DW_AT_external or any variable that
10466 lives in a namespace. [Variables in anonymous namespaces
10467 require prefixing, but they are not DW_AT_external.] */
10468
10469 if (dwarf2_attr (die, DW_AT_specification, cu))
10470 {
10471 struct dwarf2_cu *spec_cu = cu;
9a619af0 10472
94af9270
KS
10473 return die_needs_namespace (die_specification (die, &spec_cu),
10474 spec_cu);
10475 }
10476
1c809c68 10477 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10478 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10479 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10480 return 0;
10481 /* A variable in a lexical block of some kind does not need a
10482 namespace, even though in C++ such variables may be external
10483 and have a mangled name. */
10484 if (die->parent->tag == DW_TAG_lexical_block
10485 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10486 || die->parent->tag == DW_TAG_catch_block
10487 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10488 return 0;
10489 return 1;
94af9270
KS
10490
10491 default:
10492 return 0;
10493 }
10494}
10495
73b9be8b
KS
10496/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10497 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10498 defined for the given DIE. */
10499
10500static struct attribute *
10501dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10502{
10503 struct attribute *attr;
10504
10505 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10506 if (attr == NULL)
10507 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10508
10509 return attr;
10510}
10511
10512/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10513 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10514 defined for the given DIE. */
10515
10516static const char *
10517dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10518{
10519 const char *linkage_name;
10520
10521 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10522 if (linkage_name == NULL)
10523 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10524
787de330
TT
10525 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10526 See https://github.com/rust-lang/rust/issues/32925. */
10527 if (cu->language == language_rust && linkage_name != NULL
10528 && strchr (linkage_name, '{') != NULL)
10529 linkage_name = NULL;
10530
73b9be8b
KS
10531 return linkage_name;
10532}
10533
94af9270 10534/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10535 compute the physname for the object, which include a method's:
9c37b5ae 10536 - formal parameters (C++),
a766d390 10537 - receiver type (Go),
a766d390
DE
10538
10539 The term "physname" is a bit confusing.
10540 For C++, for example, it is the demangled name.
10541 For Go, for example, it's the mangled name.
94af9270 10542
af6b7be1
JB
10543 For Ada, return the DIE's linkage name rather than the fully qualified
10544 name. PHYSNAME is ignored..
10545
5989a64e 10546 The result is allocated on the objfile->per_bfd's obstack and
45940949 10547 canonicalized. */
94af9270
KS
10548
10549static const char *
15d034d0
TT
10550dwarf2_compute_name (const char *name,
10551 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10552 int physname)
10553{
5e22e966 10554 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10555
94af9270
KS
10556 if (name == NULL)
10557 name = dwarf2_name (die, cu);
10558
2ee7123e
DE
10559 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10560 but otherwise compute it by typename_concat inside GDB.
10561 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10562 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10563 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10564 will set the demangled name to the result of dwarf2_full_name, and it is
10565 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10566 if (cu->language == language_ada
10567 || (cu->language == language_fortran && physname))
10568 {
10569 /* For Ada unit, we prefer the linkage name over the name, as
10570 the former contains the exported name, which the user expects
10571 to be able to reference. Ideally, we want the user to be able
10572 to reference this entity using either natural or linkage name,
10573 but we haven't started looking at this enhancement yet. */
73b9be8b 10574 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10575
2ee7123e
DE
10576 if (linkage_name != NULL)
10577 return linkage_name;
f55ee35c
JK
10578 }
10579
94af9270
KS
10580 /* These are the only languages we know how to qualify names in. */
10581 if (name != NULL
9c37b5ae 10582 && (cu->language == language_cplus
c44af4eb
TT
10583 || cu->language == language_fortran || cu->language == language_d
10584 || cu->language == language_rust))
94af9270
KS
10585 {
10586 if (die_needs_namespace (die, cu))
10587 {
0d5cff50 10588 const char *prefix;
34a68019 10589 const char *canonical_name = NULL;
94af9270 10590
d7e74731
PA
10591 string_file buf;
10592
94af9270 10593 prefix = determine_prefix (die, cu);
94af9270
KS
10594 if (*prefix != '\0')
10595 {
43816ebc
TT
10596 gdb::unique_xmalloc_ptr<char> prefixed_name
10597 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10598
43816ebc 10599 buf.puts (prefixed_name.get ());
94af9270
KS
10600 }
10601 else
d7e74731 10602 buf.puts (name);
94af9270 10603
98bfdba5
PA
10604 /* Template parameters may be specified in the DIE's DW_AT_name, or
10605 as children with DW_TAG_template_type_param or
10606 DW_TAG_value_type_param. If the latter, add them to the name
10607 here. If the name already has template parameters, then
10608 skip this step; some versions of GCC emit both, and
10609 it is more efficient to use the pre-computed name.
10610
10611 Something to keep in mind about this process: it is very
10612 unlikely, or in some cases downright impossible, to produce
10613 something that will match the mangled name of a function.
10614 If the definition of the function has the same debug info,
10615 we should be able to match up with it anyway. But fallbacks
10616 using the minimal symbol, for instance to find a method
10617 implemented in a stripped copy of libstdc++, will not work.
10618 If we do not have debug info for the definition, we will have to
10619 match them up some other way.
10620
10621 When we do name matching there is a related problem with function
10622 templates; two instantiated function templates are allowed to
10623 differ only by their return types, which we do not add here. */
10624
10625 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10626 {
10627 struct attribute *attr;
10628 struct die_info *child;
10629 int first = 1;
2c75ccb2 10630 const language_defn *cplus_lang = language_def (cu->language);
98bfdba5
PA
10631
10632 die->building_fullname = 1;
10633
10634 for (child = die->child; child != NULL; child = child->sibling)
10635 {
10636 struct type *type;
12df843f 10637 LONGEST value;
d521ce57 10638 const gdb_byte *bytes;
98bfdba5
PA
10639 struct dwarf2_locexpr_baton *baton;
10640 struct value *v;
10641
10642 if (child->tag != DW_TAG_template_type_param
10643 && child->tag != DW_TAG_template_value_param)
10644 continue;
10645
10646 if (first)
10647 {
d7e74731 10648 buf.puts ("<");
98bfdba5
PA
10649 first = 0;
10650 }
10651 else
d7e74731 10652 buf.puts (", ");
98bfdba5
PA
10653
10654 attr = dwarf2_attr (child, DW_AT_type, cu);
10655 if (attr == NULL)
10656 {
b98664d3 10657 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10658 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10659 continue;
10660 }
10661 type = die_type (child, cu);
10662
10663 if (child->tag == DW_TAG_template_type_param)
10664 {
2c75ccb2
AB
10665 cplus_lang->print_type (type, "", &buf, -1, 0,
10666 &type_print_raw_options);
98bfdba5
PA
10667 continue;
10668 }
10669
10670 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10671 if (attr == NULL)
10672 {
b98664d3 10673 complaint (_("template parameter missing "
3e43a32a 10674 "DW_AT_const_value"));
d7e74731 10675 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10676 continue;
10677 }
10678
10679 dwarf2_const_value_attr (attr, type, name,
10680 &cu->comp_unit_obstack, cu,
10681 &value, &bytes, &baton);
10682
20ce4123 10683 if (type->has_no_signedness ())
98bfdba5
PA
10684 /* GDB prints characters as NUMBER 'CHAR'. If that's
10685 changed, this can use value_print instead. */
2c75ccb2 10686 cplus_lang->printchar (value, type, &buf);
98bfdba5
PA
10687 else
10688 {
10689 struct value_print_options opts;
10690
10691 if (baton != NULL)
10692 v = dwarf2_evaluate_loc_desc (type, NULL,
10693 baton->data,
10694 baton->size,
9f47c707
SM
10695 baton->per_cu,
10696 baton->per_objfile);
98bfdba5
PA
10697 else if (bytes != NULL)
10698 {
10699 v = allocate_value (type);
10700 memcpy (value_contents_writeable (v), bytes,
10701 TYPE_LENGTH (type));
10702 }
10703 else
10704 v = value_from_longest (type, value);
10705
3e43a32a
MS
10706 /* Specify decimal so that we do not depend on
10707 the radix. */
98bfdba5
PA
10708 get_formatted_print_options (&opts, 'd');
10709 opts.raw = 1;
d7e74731 10710 value_print (v, &buf, &opts);
98bfdba5 10711 release_value (v);
98bfdba5
PA
10712 }
10713 }
10714
10715 die->building_fullname = 0;
10716
10717 if (!first)
10718 {
10719 /* Close the argument list, with a space if necessary
10720 (nested templates). */
d7e74731
PA
10721 if (!buf.empty () && buf.string ().back () == '>')
10722 buf.puts (" >");
98bfdba5 10723 else
d7e74731 10724 buf.puts (">");
98bfdba5
PA
10725 }
10726 }
10727
9c37b5ae 10728 /* For C++ methods, append formal parameter type
94af9270 10729 information, if PHYSNAME. */
6e70227d 10730
94af9270 10731 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10732 && cu->language == language_cplus)
94af9270
KS
10733 {
10734 struct type *type = read_type_die (die, cu);
10735
d7e74731 10736 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10737 &type_print_raw_options);
94af9270 10738
9c37b5ae 10739 if (cu->language == language_cplus)
94af9270 10740 {
60430eff
DJ
10741 /* Assume that an artificial first parameter is
10742 "this", but do not crash if it is not. RealView
10743 marks unnamed (and thus unused) parameters as
10744 artificial; there is no way to differentiate
10745 the two cases. */
1f704f76 10746 if (type->num_fields () > 0
94af9270 10747 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
10748 && type->field (0).type ()->code () == TYPE_CODE_PTR
10749 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 10750 buf.puts (" const");
94af9270
KS
10751 }
10752 }
10753
d7e74731 10754 const std::string &intermediate_name = buf.string ();
94af9270
KS
10755
10756 if (cu->language == language_cplus)
34a68019 10757 canonical_name
322a8516 10758 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10759 objfile);
34a68019
TT
10760
10761 /* If we only computed INTERMEDIATE_NAME, or if
10762 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10763 intern it. */
322a8516 10764 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10765 name = objfile->intern (intermediate_name);
34a68019
TT
10766 else
10767 name = canonical_name;
94af9270
KS
10768 }
10769 }
10770
10771 return name;
10772}
10773
0114d602
DJ
10774/* Return the fully qualified name of DIE, based on its DW_AT_name.
10775 If scope qualifiers are appropriate they will be added. The result
34a68019 10776 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10777 not have a name. NAME may either be from a previous call to
10778 dwarf2_name or NULL.
10779
9c37b5ae 10780 The output string will be canonicalized (if C++). */
0114d602
DJ
10781
10782static const char *
15d034d0 10783dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10784{
94af9270
KS
10785 return dwarf2_compute_name (name, die, cu, 0);
10786}
0114d602 10787
94af9270
KS
10788/* Construct a physname for the given DIE in CU. NAME may either be
10789 from a previous call to dwarf2_name or NULL. The result will be
10790 allocated on the objfile_objstack or NULL if the DIE does not have a
10791 name.
0114d602 10792
9c37b5ae 10793 The output string will be canonicalized (if C++). */
0114d602 10794
94af9270 10795static const char *
15d034d0 10796dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10797{
5e22e966 10798 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10799 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10800 int need_copy = 1;
10801
10802 /* In this case dwarf2_compute_name is just a shortcut not building anything
10803 on its own. */
10804 if (!die_needs_namespace (die, cu))
10805 return dwarf2_compute_name (name, die, cu, 1);
10806
906bb4c5
TT
10807 if (cu->language != language_rust)
10808 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10809
10810 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10811 has computed. */
791afaa2 10812 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10813 if (mangled != NULL)
900e11f9 10814 {
900e11f9 10815
d3355e4d 10816 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
59cc4834
JB
10817 {
10818 /* Do nothing (do not demangle the symbol name). */
10819 }
a766d390
DE
10820 else
10821 {
0eb876f5
JB
10822 /* Use DMGL_RET_DROP for C++ template functions to suppress
10823 their return type. It is easier for GDB users to search
10824 for such functions as `name(params)' than `long name(params)'.
10825 In such case the minimal symbol names do not match the full
10826 symbol names but for template functions there is never a need
10827 to look up their definition from their declaration so
10828 the only disadvantage remains the minimal symbol variant
10829 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10830 demangled.reset (gdb_demangle (mangled,
10831 (DMGL_PARAMS | DMGL_ANSI
10832 | DMGL_RET_DROP)));
a766d390 10833 }
900e11f9 10834 if (demangled)
791afaa2 10835 canon = demangled.get ();
900e11f9
JK
10836 else
10837 {
10838 canon = mangled;
10839 need_copy = 0;
10840 }
10841 }
10842
10843 if (canon == NULL || check_physname)
10844 {
10845 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10846
10847 if (canon != NULL && strcmp (physname, canon) != 0)
10848 {
10849 /* It may not mean a bug in GDB. The compiler could also
10850 compute DW_AT_linkage_name incorrectly. But in such case
10851 GDB would need to be bug-to-bug compatible. */
10852
b98664d3 10853 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10854 "(from linkage <%s>) - DIE at %s [in module %s]"),
10855 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10856 objfile_name (objfile));
900e11f9
JK
10857
10858 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10859 is available here - over computed PHYSNAME. It is safer
10860 against both buggy GDB and buggy compilers. */
10861
10862 retval = canon;
10863 }
10864 else
10865 {
10866 retval = physname;
10867 need_copy = 0;
10868 }
10869 }
10870 else
10871 retval = canon;
10872
10873 if (need_copy)
be1e3d3e 10874 retval = objfile->intern (retval);
900e11f9 10875
900e11f9 10876 return retval;
0114d602
DJ
10877}
10878
74921315
KS
10879/* Inspect DIE in CU for a namespace alias. If one exists, record
10880 a new symbol for it.
10881
10882 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10883
10884static int
10885read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10886{
10887 struct attribute *attr;
10888
10889 /* If the die does not have a name, this is not a namespace
10890 alias. */
10891 attr = dwarf2_attr (die, DW_AT_name, cu);
10892 if (attr != NULL)
10893 {
10894 int num;
10895 struct die_info *d = die;
10896 struct dwarf2_cu *imported_cu = cu;
10897
10898 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10899 keep inspecting DIEs until we hit the underlying import. */
10900#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10901 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10902 {
10903 attr = dwarf2_attr (d, DW_AT_import, cu);
10904 if (attr == NULL)
10905 break;
10906
10907 d = follow_die_ref (d, attr, &imported_cu);
10908 if (d->tag != DW_TAG_imported_declaration)
10909 break;
10910 }
10911
10912 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10913 {
b98664d3 10914 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10915 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10916 return 0;
10917 }
10918
10919 if (attr != NULL)
10920 {
10921 struct type *type;
0826b30a 10922 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10923
aa66c379 10924 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10925 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10926 {
10927 /* This declaration is a global namespace alias. Add
10928 a symbol for it whose type is the aliased namespace. */
10929 new_symbol (die, type, cu);
10930 return 1;
10931 }
10932 }
10933 }
10934
10935 return 0;
10936}
10937
22cee43f 10938/* Return the using directives repository (global or local?) to use in the
804d2729 10939 current context for CU.
22cee43f
PMR
10940
10941 For Ada, imported declarations can materialize renamings, which *may* be
10942 global. However it is impossible (for now?) in DWARF to distinguish
10943 "external" imported declarations and "static" ones. As all imported
10944 declarations seem to be static in all other languages, make them all CU-wide
10945 global only in Ada. */
10946
10947static struct using_direct **
804d2729 10948using_directives (struct dwarf2_cu *cu)
22cee43f 10949{
c24bdb02
KS
10950 if (cu->language == language_ada
10951 && cu->get_builder ()->outermost_context_p ())
10952 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10953 else
c24bdb02 10954 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10955}
10956
27aa8d6a
SW
10957/* Read the import statement specified by the given die and record it. */
10958
10959static void
10960read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10961{
5e22e966 10962 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10963 struct attribute *import_attr;
32019081 10964 struct die_info *imported_die, *child_die;
de4affc9 10965 struct dwarf2_cu *imported_cu;
27aa8d6a 10966 const char *imported_name;
794684b6 10967 const char *imported_name_prefix;
13387711
SW
10968 const char *canonical_name;
10969 const char *import_alias;
10970 const char *imported_declaration = NULL;
794684b6 10971 const char *import_prefix;
eb1e02fd 10972 std::vector<const char *> excludes;
13387711 10973
27aa8d6a
SW
10974 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10975 if (import_attr == NULL)
10976 {
b98664d3 10977 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10978 dwarf_tag_name (die->tag));
10979 return;
10980 }
10981
de4affc9
CC
10982 imported_cu = cu;
10983 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10984 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10985 if (imported_name == NULL)
10986 {
10987 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10988
dda83cd7
SM
10989 The import in the following code:
10990 namespace A
10991 {
10992 typedef int B;
10993 }
10994
10995 int main ()
10996 {
10997 using A::B;
10998 B b;
10999 return b;
11000 }
11001
11002 ...
11003 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11004 <52> DW_AT_decl_file : 1
11005 <53> DW_AT_decl_line : 6
11006 <54> DW_AT_import : <0x75>
11007 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11008 <59> DW_AT_name : B
11009 <5b> DW_AT_decl_file : 1
11010 <5c> DW_AT_decl_line : 2
11011 <5d> DW_AT_type : <0x6e>
11012 ...
11013 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11014 <76> DW_AT_byte_size : 4
11015 <77> DW_AT_encoding : 5 (signed)
11016
11017 imports the wrong die ( 0x75 instead of 0x58 ).
11018 This case will be ignored until the gcc bug is fixed. */
27aa8d6a
SW
11019 return;
11020 }
11021
82856980
SW
11022 /* Figure out the local name after import. */
11023 import_alias = dwarf2_name (die, cu);
27aa8d6a 11024
794684b6
SW
11025 /* Figure out where the statement is being imported to. */
11026 import_prefix = determine_prefix (die, cu);
11027
11028 /* Figure out what the scope of the imported die is and prepend it
11029 to the name of the imported die. */
de4affc9 11030 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11031
f55ee35c
JK
11032 if (imported_die->tag != DW_TAG_namespace
11033 && imported_die->tag != DW_TAG_module)
794684b6 11034 {
13387711
SW
11035 imported_declaration = imported_name;
11036 canonical_name = imported_name_prefix;
794684b6 11037 }
13387711 11038 else if (strlen (imported_name_prefix) > 0)
12aaed36 11039 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11040 imported_name_prefix,
11041 (cu->language == language_d ? "." : "::"),
11042 imported_name, (char *) NULL);
13387711
SW
11043 else
11044 canonical_name = imported_name;
794684b6 11045
32019081
JK
11046 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11047 for (child_die = die->child; child_die && child_die->tag;
436c571c 11048 child_die = child_die->sibling)
32019081
JK
11049 {
11050 /* DWARF-4: A Fortran use statement with a “rename list” may be
11051 represented by an imported module entry with an import attribute
11052 referring to the module and owned entries corresponding to those
11053 entities that are renamed as part of being imported. */
11054
11055 if (child_die->tag != DW_TAG_imported_declaration)
11056 {
b98664d3 11057 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11058 "- DIE at %s [in module %s]"),
11059 sect_offset_str (child_die->sect_off),
11060 objfile_name (objfile));
32019081
JK
11061 continue;
11062 }
11063
11064 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11065 if (import_attr == NULL)
11066 {
b98664d3 11067 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11068 dwarf_tag_name (child_die->tag));
11069 continue;
11070 }
11071
11072 imported_cu = cu;
11073 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11074 &imported_cu);
11075 imported_name = dwarf2_name (imported_die, imported_cu);
11076 if (imported_name == NULL)
11077 {
b98664d3 11078 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11079 "imported name - DIE at %s [in module %s]"),
11080 sect_offset_str (child_die->sect_off),
11081 objfile_name (objfile));
32019081
JK
11082 continue;
11083 }
11084
eb1e02fd 11085 excludes.push_back (imported_name);
32019081
JK
11086
11087 process_die (child_die, cu);
11088 }
11089
804d2729 11090 add_using_directive (using_directives (cu),
22cee43f
PMR
11091 import_prefix,
11092 canonical_name,
11093 import_alias,
11094 imported_declaration,
11095 excludes,
11096 0,
11097 &objfile->objfile_obstack);
27aa8d6a
SW
11098}
11099
5230b05a
WT
11100/* ICC<14 does not output the required DW_AT_declaration on incomplete
11101 types, but gives them a size of zero. Starting with version 14,
11102 ICC is compatible with GCC. */
11103
9068261f 11104static bool
5230b05a
WT
11105producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11106{
11107 if (!cu->checked_producer)
11108 check_producer (cu);
11109
11110 return cu->producer_is_icc_lt_14;
11111}
11112
eb77c9df
AB
11113/* ICC generates a DW_AT_type for C void functions. This was observed on
11114 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11115 which says that void functions should not have a DW_AT_type. */
11116
11117static bool
11118producer_is_icc (struct dwarf2_cu *cu)
11119{
11120 if (!cu->checked_producer)
11121 check_producer (cu);
11122
11123 return cu->producer_is_icc;
11124}
11125
1b80a9fa
JK
11126/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11127 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11128 this, it was first present in GCC release 4.3.0. */
11129
9068261f 11130static bool
1b80a9fa
JK
11131producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11132{
11133 if (!cu->checked_producer)
11134 check_producer (cu);
11135
11136 return cu->producer_is_gcc_lt_4_3;
11137}
11138
d721ba37
PA
11139static file_and_directory
11140find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11141{
d721ba37
PA
11142 file_and_directory res;
11143
9291a0cd
TT
11144 /* Find the filename. Do not use dwarf2_name here, since the filename
11145 is not a source language identifier. */
d721ba37
PA
11146 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11147 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11148
d721ba37
PA
11149 if (res.comp_dir == NULL
11150 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11151 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11152 {
d721ba37
PA
11153 res.comp_dir_storage = ldirname (res.name);
11154 if (!res.comp_dir_storage.empty ())
11155 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11156 }
d721ba37 11157 if (res.comp_dir != NULL)
9291a0cd
TT
11158 {
11159 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11160 directory, get rid of it. */
d721ba37 11161 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11162
d721ba37
PA
11163 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11164 res.comp_dir = cp + 1;
9291a0cd
TT
11165 }
11166
d721ba37
PA
11167 if (res.name == NULL)
11168 res.name = "<unknown>";
11169
11170 return res;
9291a0cd
TT
11171}
11172
f4dc4d17
DE
11173/* Handle DW_AT_stmt_list for a compilation unit.
11174 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11175 COMP_DIR is the compilation directory. LOWPC is passed to
11176 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11177
11178static void
11179handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11180 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11181{
976ca316 11182 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 11183 struct attribute *attr;
527f3840
JK
11184 struct line_header line_header_local;
11185 hashval_t line_header_local_hash;
527f3840
JK
11186 void **slot;
11187 int decode_mapping;
2ab95328 11188
f4dc4d17
DE
11189 gdb_assert (! cu->per_cu->is_debug_types);
11190
2ab95328 11191 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e 11192 if (attr == NULL || !attr->form_is_unsigned ())
527f3840
JK
11193 return;
11194
d4df075e 11195 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
527f3840
JK
11196
11197 /* The line header hash table is only created if needed (it exists to
11198 prevent redundant reading of the line table for partial_units).
11199 If we're given a partial_unit, we'll need it. If we're given a
11200 compile_unit, then use the line header hash table if it's already
11201 created, but don't create one just yet. */
11202
976ca316 11203 if (per_objfile->line_header_hash == NULL
527f3840 11204 && die->tag == DW_TAG_partial_unit)
2ab95328 11205 {
976ca316 11206 per_objfile->line_header_hash
d15acc42
TT
11207 .reset (htab_create_alloc (127, line_header_hash_voidp,
11208 line_header_eq_voidp,
11209 free_line_header_voidp,
11210 xcalloc, xfree));
527f3840 11211 }
2ab95328 11212
9c541725 11213 line_header_local.sect_off = line_offset;
527f3840
JK
11214 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11215 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 11216 if (per_objfile->line_header_hash != NULL)
527f3840 11217 {
976ca316 11218 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11219 &line_header_local,
11220 line_header_local_hash, NO_INSERT);
11221
11222 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11223 is not present in *SLOT (since if there is something in *SLOT then
11224 it will be for a partial_unit). */
11225 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11226 {
527f3840 11227 gdb_assert (*slot != NULL);
9a3c8263 11228 cu->line_header = (struct line_header *) *slot;
527f3840 11229 return;
dee91e82 11230 }
2ab95328 11231 }
527f3840
JK
11232
11233 /* dwarf_decode_line_header does not yet provide sufficient information.
11234 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11235 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11236 if (lh == NULL)
527f3840 11237 return;
4c8aa72d
PA
11238
11239 cu->line_header = lh.release ();
11240 cu->line_header_die_owner = die;
527f3840 11241
976ca316 11242 if (per_objfile->line_header_hash == NULL)
527f3840
JK
11243 slot = NULL;
11244 else
11245 {
976ca316 11246 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11247 &line_header_local,
11248 line_header_local_hash, INSERT);
11249 gdb_assert (slot != NULL);
11250 }
11251 if (slot != NULL && *slot == NULL)
11252 {
11253 /* This newly decoded line number information unit will be owned
11254 by line_header_hash hash table. */
11255 *slot = cu->line_header;
4c8aa72d 11256 cu->line_header_die_owner = NULL;
527f3840
JK
11257 }
11258 else
11259 {
11260 /* We cannot free any current entry in (*slot) as that struct line_header
dda83cd7 11261 may be already used by multiple CUs. Create only temporary decoded
527f3840
JK
11262 line_header for this CU - it may happen at most once for each line
11263 number information unit. And if we're not using line_header_hash
11264 then this is what we want as well. */
11265 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11266 }
11267 decode_mapping = (die->tag != DW_TAG_partial_unit);
11268 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11269 decode_mapping);
fff8551c 11270
2ab95328
TT
11271}
11272
95554aad 11273/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11274
c906108c 11275static void
e7c27a73 11276read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11277{
976ca316
SM
11278 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11279 struct objfile *objfile = per_objfile->objfile;
08feed99 11280 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 11281 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11282 CORE_ADDR highpc = ((CORE_ADDR) 0);
11283 struct attribute *attr;
c906108c 11284 struct die_info *child_die;
e142c38c 11285 CORE_ADDR baseaddr;
6e70227d 11286
380618d6 11287 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11288 baseaddr = objfile->text_section_offset ();
c906108c 11289
fae299cd 11290 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11291
11292 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11293 from finish_block. */
2acceee2 11294 if (lowpc == ((CORE_ADDR) -1))
c906108c 11295 lowpc = highpc;
3e29f34a 11296 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11297
d721ba37 11298 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11299
f4b8a18d
KW
11300 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11301 standardised yet. As a workaround for the language detection we fall
11302 back to the DW_AT_producer string. */
11303 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11304 cu->language = language_opencl;
11305
3019eac3
DE
11306 /* Similar hack for Go. */
11307 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11308 set_cu_language (DW_LANG_Go, cu);
11309
c24bdb02 11310 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11311
11312 /* Decode line number information if present. We do this before
11313 processing child DIEs, so that the line header table is available
11314 for DW_AT_decl_file. */
d721ba37 11315 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11316
11317 /* Process all dies in compilation unit. */
11318 if (die->child != NULL)
11319 {
11320 child_die = die->child;
11321 while (child_die && child_die->tag)
11322 {
11323 process_die (child_die, cu);
436c571c 11324 child_die = child_die->sibling;
3019eac3
DE
11325 }
11326 }
11327
11328 /* Decode macro information, if present. Dwarf 2 macro information
11329 refers to information in the line number info statement program
11330 header, so we can only read it if we've read the header
11331 successfully. */
0af92d60
JK
11332 attr = dwarf2_attr (die, DW_AT_macros, cu);
11333 if (attr == NULL)
11334 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
529908cb 11335 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3
DE
11336 {
11337 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11338 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11339
529908cb 11340 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
3019eac3
DE
11341 }
11342 else
11343 {
11344 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
529908cb 11345 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3 11346 {
529908cb 11347 unsigned int macro_offset = attr->as_unsigned ();
3019eac3 11348
43f3e411 11349 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11350 }
11351 }
3019eac3
DE
11352}
11353
c24bdb02
KS
11354void
11355dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11356{
f4dc4d17
DE
11357 struct type_unit_group *tu_group;
11358 int first_time;
3019eac3 11359 struct attribute *attr;
9c541725 11360 unsigned int i;
0186c6a7 11361 struct signatured_type *sig_type;
3019eac3 11362
f4dc4d17 11363 gdb_assert (per_cu->is_debug_types);
0186c6a7 11364 sig_type = (struct signatured_type *) per_cu;
3019eac3 11365
c24bdb02 11366 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11367
f4dc4d17 11368 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11369 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11370 if (sig_type->type_unit_group == NULL)
c24bdb02 11371 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11372 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11373
11374 /* If we've already processed this stmt_list there's no real need to
11375 do it again, we could fake it and just recreate the part we need
11376 (file name,index -> symtab mapping). If data shows this optimization
11377 is useful we can do it then. */
8adb8487
TT
11378 type_unit_group_unshareable *tug_unshare
11379 = per_objfile->get_type_unit_group_unshareable (tu_group);
11380 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11381
11382 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11383 debug info. */
fff8551c 11384 line_header_up lh;
d4df075e 11385 if (attr != NULL && attr->form_is_unsigned ())
3019eac3 11386 {
d4df075e 11387 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
c24bdb02 11388 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11389 }
11390 if (lh == NULL)
11391 {
11392 if (first_time)
c24bdb02 11393 start_symtab ("", NULL, 0);
f4dc4d17
DE
11394 else
11395 {
8adb8487 11396 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11397 gdb_assert (m_builder == nullptr);
8adb8487 11398 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11399 m_builder.reset (new struct buildsym_compunit
11400 (COMPUNIT_OBJFILE (cust), "",
11401 COMPUNIT_DIRNAME (cust),
11402 compunit_language (cust),
11403 0, cust));
770479f2 11404 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11405 }
f4dc4d17 11406 return;
3019eac3
DE
11407 }
11408
c24bdb02
KS
11409 line_header = lh.release ();
11410 line_header_die_owner = die;
3019eac3 11411
f4dc4d17
DE
11412 if (first_time)
11413 {
c24bdb02 11414 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11415
1fd60fc0
DE
11416 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11417 still initializing it, and our caller (a few levels up)
11418 process_full_type_unit still needs to know if this is the first
11419 time. */
11420
8adb8487 11421 tug_unshare->symtabs
4ac93832
TT
11422 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11423 struct symtab *, line_header->file_names_size ());
3019eac3 11424
7ba99d21
AT
11425 auto &file_names = line_header->file_names ();
11426 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11427 {
7ba99d21 11428 file_entry &fe = file_names[i];
c24bdb02
KS
11429 dwarf2_start_subfile (this, fe.name,
11430 fe.include_dir (line_header));
11431 buildsym_compunit *b = get_builder ();
11432 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11433 {
4c8aa72d
PA
11434 /* NOTE: start_subfile will recognize when it's been
11435 passed a file it has already seen. So we can't
11436 assume there's a simple mapping from
11437 cu->line_header->file_names to subfiles, plus
11438 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11439 b->get_current_subfile ()->symtab
11440 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11441 }
11442
c24bdb02 11443 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11444 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11445 }
11446 }
11447 else
3019eac3 11448 {
c24bdb02 11449 gdb_assert (m_builder == nullptr);
8adb8487 11450 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11451 m_builder.reset (new struct buildsym_compunit
11452 (COMPUNIT_OBJFILE (cust), "",
11453 COMPUNIT_DIRNAME (cust),
11454 compunit_language (cust),
11455 0, cust));
770479f2 11456 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11457
7ba99d21
AT
11458 auto &file_names = line_header->file_names ();
11459 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11460 {
7ba99d21 11461 file_entry &fe = file_names[i];
8adb8487 11462 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11463 }
3019eac3
DE
11464 }
11465
f4dc4d17
DE
11466 /* The main symtab is allocated last. Type units don't have DW_AT_name
11467 so they don't have a "real" (so to speak) symtab anyway.
11468 There is later code that will assign the main symtab to all symbols
11469 that don't have one. We need to handle the case of a symbol with a
11470 missing symtab (DW_AT_decl_file) anyway. */
11471}
3019eac3 11472
f4dc4d17
DE
11473/* Process DW_TAG_type_unit.
11474 For TUs we want to skip the first top level sibling if it's not the
11475 actual type being defined by this TU. In this case the first top
11476 level sibling is there to provide context only. */
3019eac3 11477
f4dc4d17
DE
11478static void
11479read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11480{
11481 struct die_info *child_die;
3019eac3 11482
f4dc4d17
DE
11483 prepare_one_comp_unit (cu, die, language_minimal);
11484
11485 /* Initialize (or reinitialize) the machinery for building symtabs.
11486 We do this before processing child DIEs, so that the line header table
11487 is available for DW_AT_decl_file. */
c24bdb02 11488 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11489
11490 if (die->child != NULL)
11491 {
11492 child_die = die->child;
11493 while (child_die && child_die->tag)
11494 {
11495 process_die (child_die, cu);
436c571c 11496 child_die = child_die->sibling;
f4dc4d17
DE
11497 }
11498 }
3019eac3
DE
11499}
11500\f
80626a55
DE
11501/* DWO/DWP files.
11502
11503 http://gcc.gnu.org/wiki/DebugFission
11504 http://gcc.gnu.org/wiki/DebugFissionDWP
11505
11506 To simplify handling of both DWO files ("object" files with the DWARF info)
11507 and DWP files (a file with the DWOs packaged up into one file), we treat
11508 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11509
11510static hashval_t
11511hash_dwo_file (const void *item)
11512{
9a3c8263 11513 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11514 hashval_t hash;
3019eac3 11515
a2ce51a0
DE
11516 hash = htab_hash_string (dwo_file->dwo_name);
11517 if (dwo_file->comp_dir != NULL)
11518 hash += htab_hash_string (dwo_file->comp_dir);
11519 return hash;
3019eac3
DE
11520}
11521
11522static int
11523eq_dwo_file (const void *item_lhs, const void *item_rhs)
11524{
9a3c8263
SM
11525 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11526 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11527
a2ce51a0
DE
11528 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11529 return 0;
11530 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11531 return lhs->comp_dir == rhs->comp_dir;
11532 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11533}
11534
11535/* Allocate a hash table for DWO files. */
11536
51ac9db5 11537static htab_up
298e9637 11538allocate_dwo_file_hash_table ()
3019eac3 11539{
51ac9db5
SM
11540 auto delete_dwo_file = [] (void *item)
11541 {
11542 struct dwo_file *dwo_file = (struct dwo_file *) item;
11543
11544 delete dwo_file;
11545 };
11546
bc68fb19
TT
11547 return htab_up (htab_create_alloc (41,
11548 hash_dwo_file,
11549 eq_dwo_file,
11550 delete_dwo_file,
11551 xcalloc, xfree));
3019eac3
DE
11552}
11553
80626a55
DE
11554/* Lookup DWO file DWO_NAME. */
11555
11556static void **
976ca316 11557lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11558 const char *dwo_name,
11559 const char *comp_dir)
80626a55
DE
11560{
11561 struct dwo_file find_entry;
11562 void **slot;
11563
976ca316
SM
11564 if (per_objfile->per_bfd->dwo_files == NULL)
11565 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11566
0ac5b59e
DE
11567 find_entry.dwo_name = dwo_name;
11568 find_entry.comp_dir = comp_dir;
976ca316 11569 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11570 INSERT);
80626a55
DE
11571
11572 return slot;
11573}
11574
3019eac3
DE
11575static hashval_t
11576hash_dwo_unit (const void *item)
11577{
9a3c8263 11578 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11579
11580 /* This drops the top 32 bits of the id, but is ok for a hash. */
11581 return dwo_unit->signature;
11582}
11583
11584static int
11585eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11586{
9a3c8263
SM
11587 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11588 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11589
11590 /* The signature is assumed to be unique within the DWO file.
11591 So while object file CU dwo_id's always have the value zero,
11592 that's OK, assuming each object file DWO file has only one CU,
11593 and that's the rule for now. */
11594 return lhs->signature == rhs->signature;
11595}
11596
11597/* Allocate a hash table for DWO CUs,TUs.
11598 There is one of these tables for each of CUs,TUs for each DWO file. */
11599
b0b6a987 11600static htab_up
298e9637 11601allocate_dwo_unit_table ()
3019eac3
DE
11602{
11603 /* Start out with a pretty small number.
11604 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11605 return htab_up (htab_create_alloc (3,
11606 hash_dwo_unit,
11607 eq_dwo_unit,
11608 NULL, xcalloc, xfree));
3019eac3
DE
11609}
11610
19c3d4c9 11611/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11612
11613static void
19c3d4c9
DE
11614create_dwo_cu_reader (const struct die_reader_specs *reader,
11615 const gdb_byte *info_ptr,
11616 struct die_info *comp_unit_die,
c0ab21c2
TT
11617 struct dwo_file *dwo_file,
11618 struct dwo_unit *dwo_unit)
3019eac3
DE
11619{
11620 struct dwarf2_cu *cu = reader->cu;
9c541725 11621 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11622 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11623
a084a2a6
AT
11624 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11625 if (!signature.has_value ())
3019eac3 11626 {
b98664d3 11627 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11628 " its dwo_id [in module %s]"),
9d8780f0 11629 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11630 return;
11631 }
11632
3019eac3 11633 dwo_unit->dwo_file = dwo_file;
a084a2a6 11634 dwo_unit->signature = *signature;
8a0459fd 11635 dwo_unit->section = section;
9c541725 11636 dwo_unit->sect_off = sect_off;
3019eac3
DE
11637 dwo_unit->length = cu->per_cu->length;
11638
6f738b01
SM
11639 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11640 sect_offset_str (sect_off),
11641 hex_string (dwo_unit->signature));
3019eac3
DE
11642}
11643
33c5cd75 11644/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11645 Note: This function processes DWO files only, not DWP files. */
3019eac3 11646
33c5cd75 11647static void
976ca316 11648create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11649 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11650 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11651{
976ca316
SM
11652 struct objfile *objfile = per_objfile->objfile;
11653 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11654 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11655
96b79293 11656 section.read (objfile);
33c5cd75 11657 info_ptr = section.buffer;
3019eac3
DE
11658
11659 if (info_ptr == NULL)
33c5cd75 11660 return;
3019eac3 11661
6f738b01
SM
11662 dwarf_read_debug_printf ("Reading %s for %s:",
11663 section.get_name (),
11664 section.get_file_name ());
3019eac3 11665
33c5cd75 11666 end_ptr = info_ptr + section.size;
3019eac3
DE
11667 while (info_ptr < end_ptr)
11668 {
11669 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11670 struct dwo_unit read_unit {};
33c5cd75
DB
11671 struct dwo_unit *dwo_unit;
11672 void **slot;
11673 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11674
11675 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11676 per_cu.per_bfd = per_bfd;
3019eac3 11677 per_cu.is_debug_types = 0;
33c5cd75
DB
11678 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11679 per_cu.section = &section;
11680
976ca316 11681 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11682 if (!reader.dummy_p)
11683 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11684 &dwo_file, &read_unit);
33c5cd75
DB
11685 info_ptr += per_cu.length;
11686
11687 // If the unit could not be parsed, skip it.
c0ab21c2 11688 if (read_unit.dwo_file == NULL)
33c5cd75 11689 continue;
3019eac3 11690
33c5cd75 11691 if (cus_htab == NULL)
298e9637 11692 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11693
1859c670 11694 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11695 struct dwo_unit);
c0ab21c2 11696 *dwo_unit = read_unit;
b0b6a987 11697 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11698 gdb_assert (slot != NULL);
11699 if (*slot != NULL)
19c3d4c9 11700 {
33c5cd75
DB
11701 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11702 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11703
b98664d3 11704 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11705 " the entry at offset %s, signature %s"),
11706 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11707 hex_string (dwo_unit->signature));
19c3d4c9 11708 }
33c5cd75 11709 *slot = (void *)dwo_unit;
3019eac3 11710 }
3019eac3
DE
11711}
11712
80626a55
DE
11713/* DWP file .debug_{cu,tu}_index section format:
11714 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
d2854d8d
CT
11715 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11716
11717 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11718 officially standard DWP format was published with DWARF v5 and is called
11719 Version 5. There are no versions 3 or 4.
80626a55 11720
d2415c6c
DE
11721 DWP Version 1:
11722
80626a55
DE
11723 Both index sections have the same format, and serve to map a 64-bit
11724 signature to a set of section numbers. Each section begins with a header,
11725 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11726 indexes, and a pool of 32-bit section numbers. The index sections will be
11727 aligned at 8-byte boundaries in the file.
11728
d2415c6c
DE
11729 The index section header consists of:
11730
11731 V, 32 bit version number
11732 -, 32 bits unused
11733 N, 32 bit number of compilation units or type units in the index
11734 M, 32 bit number of slots in the hash table
80626a55 11735
d2415c6c 11736 Numbers are recorded using the byte order of the application binary.
80626a55 11737
d2415c6c
DE
11738 The hash table begins at offset 16 in the section, and consists of an array
11739 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11740 order of the application binary). Unused slots in the hash table are 0.
11741 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11742
d2415c6c
DE
11743 The parallel table begins immediately after the hash table
11744 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11745 array of 32-bit indexes (using the byte order of the application binary),
11746 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11747 table contains a 32-bit index into the pool of section numbers. For unused
11748 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11749
73869dc2
DE
11750 The pool of section numbers begins immediately following the hash table
11751 (at offset 16 + 12 * M from the beginning of the section). The pool of
11752 section numbers consists of an array of 32-bit words (using the byte order
11753 of the application binary). Each item in the array is indexed starting
11754 from 0. The hash table entry provides the index of the first section
11755 number in the set. Additional section numbers in the set follow, and the
11756 set is terminated by a 0 entry (section number 0 is not used in ELF).
11757
11758 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11759 section must be the first entry in the set, and the .debug_abbrev.dwo must
11760 be the second entry. Other members of the set may follow in any order.
11761
11762 ---
11763
d2854d8d 11764 DWP Versions 2 and 5:
73869dc2 11765
d2854d8d 11766 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
73869dc2
DE
11767 and the entries in the index tables are now offsets into these sections.
11768 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11769 section.
11770
11771 Index Section Contents:
11772 Header
11773 Hash Table of Signatures dwp_hash_table.hash_table
11774 Parallel Table of Indices dwp_hash_table.unit_table
d2854d8d
CT
11775 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11776 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
73869dc2
DE
11777
11778 The index section header consists of:
11779
11780 V, 32 bit version number
11781 L, 32 bit number of columns in the table of section offsets
11782 N, 32 bit number of compilation units or type units in the index
11783 M, 32 bit number of slots in the hash table
11784
11785 Numbers are recorded using the byte order of the application binary.
11786
11787 The hash table has the same format as version 1.
11788 The parallel table of indices has the same format as version 1,
11789 except that the entries are origin-1 indices into the table of sections
11790 offsets and the table of section sizes.
11791
11792 The table of offsets begins immediately following the parallel table
11793 (at offset 16 + 12 * M from the beginning of the section). The table is
11794 a two-dimensional array of 32-bit words (using the byte order of the
11795 application binary), with L columns and N+1 rows, in row-major order.
11796 Each row in the array is indexed starting from 0. The first row provides
11797 a key to the remaining rows: each column in this row provides an identifier
11798 for a debug section, and the offsets in the same column of subsequent rows
d2854d8d 11799 refer to that section. The section identifiers for Version 2 are:
73869dc2
DE
11800
11801 DW_SECT_INFO 1 .debug_info.dwo
11802 DW_SECT_TYPES 2 .debug_types.dwo
11803 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11804 DW_SECT_LINE 4 .debug_line.dwo
11805 DW_SECT_LOC 5 .debug_loc.dwo
11806 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11807 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11808 DW_SECT_MACRO 8 .debug_macro.dwo
11809
d2854d8d
CT
11810 The section identifiers for Version 5 are:
11811
11812 DW_SECT_INFO_V5 1 .debug_info.dwo
11813 DW_SECT_RESERVED_V5 2 --
11814 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11815 DW_SECT_LINE_V5 4 .debug_line.dwo
11816 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11817 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11818 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11819 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11820
73869dc2
DE
11821 The offsets provided by the CU and TU index sections are the base offsets
11822 for the contributions made by each CU or TU to the corresponding section
11823 in the package file. Each CU and TU header contains an abbrev_offset
11824 field, used to find the abbreviations table for that CU or TU within the
11825 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11826 be interpreted as relative to the base offset given in the index section.
11827 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11828 should be interpreted as relative to the base offset for .debug_line.dwo,
11829 and offsets into other debug sections obtained from DWARF attributes should
11830 also be interpreted as relative to the corresponding base offset.
11831
11832 The table of sizes begins immediately following the table of offsets.
11833 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11834 with L columns and N rows, in row-major order. Each row in the array is
11835 indexed starting from 1 (row 0 is shared by the two tables).
11836
11837 ---
11838
11839 Hash table lookup is handled the same in version 1 and 2:
11840
11841 We assume that N and M will not exceed 2^32 - 1.
11842 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11843
d2415c6c
DE
11844 Given a 64-bit compilation unit signature or a type signature S, an entry
11845 in the hash table is located as follows:
80626a55 11846
d2415c6c
DE
11847 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11848 the low-order k bits all set to 1.
80626a55 11849
d2415c6c 11850 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11851
d2415c6c
DE
11852 3) If the hash table entry at index H matches the signature, use that
11853 entry. If the hash table entry at index H is unused (all zeroes),
11854 terminate the search: the signature is not present in the table.
80626a55 11855
d2415c6c 11856 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11857
d2415c6c 11858 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11859 to stop at an unused slot or find the match. */
80626a55
DE
11860
11861/* Create a hash table to map DWO IDs to their CU/TU entry in
11862 .debug_{info,types}.dwo in DWP_FILE.
11863 Returns NULL if there isn't one.
11864 Note: This function processes DWP files only, not DWO files. */
11865
11866static struct dwp_hash_table *
976ca316 11867create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11868 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11869{
976ca316 11870 struct objfile *objfile = per_objfile->objfile;
400174b1 11871 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11872 const gdb_byte *index_ptr, *index_end;
80626a55 11873 struct dwarf2_section_info *index;
73869dc2 11874 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11875 struct dwp_hash_table *htab;
11876
11877 if (is_debug_types)
11878 index = &dwp_file->sections.tu_index;
11879 else
11880 index = &dwp_file->sections.cu_index;
11881
96b79293 11882 if (index->empty ())
80626a55 11883 return NULL;
96b79293 11884 index->read (objfile);
80626a55
DE
11885
11886 index_ptr = index->buffer;
11887 index_end = index_ptr + index->size;
11888
d2854d8d
CT
11889 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11890 For now it's safe to just read 4 bytes (particularly as it's difficult to
11891 tell if you're dealing with Version 5 before you've read the version). */
80626a55 11892 version = read_4_bytes (dbfd, index_ptr);
73869dc2 11893 index_ptr += 4;
d2854d8d 11894 if (version == 2 || version == 5)
73869dc2
DE
11895 nr_columns = read_4_bytes (dbfd, index_ptr);
11896 else
11897 nr_columns = 0;
11898 index_ptr += 4;
80626a55
DE
11899 nr_units = read_4_bytes (dbfd, index_ptr);
11900 index_ptr += 4;
11901 nr_slots = read_4_bytes (dbfd, index_ptr);
11902 index_ptr += 4;
11903
d2854d8d 11904 if (version != 1 && version != 2 && version != 5)
80626a55 11905 {
21aa081e 11906 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11907 " [in module %s]"),
21aa081e 11908 pulongest (version), dwp_file->name);
80626a55
DE
11909 }
11910 if (nr_slots != (nr_slots & -nr_slots))
11911 {
21aa081e 11912 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11913 " is not power of 2 [in module %s]"),
21aa081e 11914 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11915 }
11916
976ca316 11917 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11918 htab->version = version;
11919 htab->nr_columns = nr_columns;
80626a55
DE
11920 htab->nr_units = nr_units;
11921 htab->nr_slots = nr_slots;
11922 htab->hash_table = index_ptr;
11923 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11924
11925 /* Exit early if the table is empty. */
11926 if (nr_slots == 0 || nr_units == 0
d2854d8d
CT
11927 || (version == 2 && nr_columns == 0)
11928 || (version == 5 && nr_columns == 0))
73869dc2
DE
11929 {
11930 /* All must be zero. */
11931 if (nr_slots != 0 || nr_units != 0
d2854d8d
CT
11932 || (version == 2 && nr_columns != 0)
11933 || (version == 5 && nr_columns != 0))
73869dc2 11934 {
b98664d3 11935 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11936 " all zero [in modules %s]"),
11937 dwp_file->name);
11938 }
11939 return htab;
11940 }
11941
11942 if (version == 1)
11943 {
11944 htab->section_pool.v1.indices =
11945 htab->unit_table + sizeof (uint32_t) * nr_slots;
11946 /* It's harder to decide whether the section is too small in v1.
11947 V1 is deprecated anyway so we punt. */
11948 }
d2854d8d 11949 else if (version == 2)
73869dc2
DE
11950 {
11951 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11952 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11953 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11954 /* Reverse map for error checking. */
11955 int ids_seen[DW_SECT_MAX + 1];
11956 int i;
11957
11958 if (nr_columns < 2)
11959 {
11960 error (_("Dwarf Error: bad DWP hash table, too few columns"
11961 " in section table [in module %s]"),
11962 dwp_file->name);
11963 }
11964 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11965 {
11966 error (_("Dwarf Error: bad DWP hash table, too many columns"
11967 " in section table [in module %s]"),
11968 dwp_file->name);
11969 }
04fd5eed
GB
11970 memset (ids, 255, sizeof_ids);
11971 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11972 for (i = 0; i < nr_columns; ++i)
11973 {
11974 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11975
11976 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11977 {
11978 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11979 " in section table [in module %s]"),
11980 id, dwp_file->name);
11981 }
11982 if (ids_seen[id] != -1)
11983 {
11984 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11985 " id %d in section table [in module %s]"),
11986 id, dwp_file->name);
11987 }
11988 ids_seen[id] = i;
11989 ids[i] = id;
11990 }
11991 /* Must have exactly one info or types section. */
11992 if (((ids_seen[DW_SECT_INFO] != -1)
11993 + (ids_seen[DW_SECT_TYPES] != -1))
11994 != 1)
11995 {
11996 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11997 " DWO info/types section [in module %s]"),
11998 dwp_file->name);
11999 }
12000 /* Must have an abbrev section. */
12001 if (ids_seen[DW_SECT_ABBREV] == -1)
12002 {
12003 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12004 " section [in module %s]"),
12005 dwp_file->name);
12006 }
12007 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12008 htab->section_pool.v2.sizes =
12009 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12010 * nr_units * nr_columns);
12011 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12012 * nr_units * nr_columns))
12013 > index_end)
12014 {
12015 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12016 " [in module %s]"),
12017 dwp_file->name);
12018 }
12019 }
d2854d8d
CT
12020 else /* version == 5 */
12021 {
12022 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12023 int *ids = htab->section_pool.v5.section_ids;
12024 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
12025 /* Reverse map for error checking. */
12026 int ids_seen[DW_SECT_MAX_V5 + 1];
12027
12028 if (nr_columns < 2)
12029 {
12030 error (_("Dwarf Error: bad DWP hash table, too few columns"
12031 " in section table [in module %s]"),
12032 dwp_file->name);
12033 }
12034 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
12035 {
12036 error (_("Dwarf Error: bad DWP hash table, too many columns"
12037 " in section table [in module %s]"),
12038 dwp_file->name);
12039 }
12040 memset (ids, 255, sizeof_ids);
12041 memset (ids_seen, 255, sizeof (ids_seen));
12042 for (int i = 0; i < nr_columns; ++i)
12043 {
12044 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12045
12046 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
12047 {
12048 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12049 " in section table [in module %s]"),
12050 id, dwp_file->name);
12051 }
12052 if (ids_seen[id] != -1)
12053 {
12054 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12055 " id %d in section table [in module %s]"),
12056 id, dwp_file->name);
12057 }
12058 ids_seen[id] = i;
12059 ids[i] = id;
12060 }
12061 /* Must have seen an info section. */
12062 if (ids_seen[DW_SECT_INFO_V5] == -1)
12063 {
12064 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12065 " DWO info/types section [in module %s]"),
12066 dwp_file->name);
12067 }
12068 /* Must have an abbrev section. */
12069 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
12070 {
12071 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12072 " section [in module %s]"),
12073 dwp_file->name);
12074 }
12075 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12076 htab->section_pool.v5.sizes
12077 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
12078 * nr_units * nr_columns);
12079 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
12080 * nr_units * nr_columns))
12081 > index_end)
12082 {
12083 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12084 " [in module %s]"),
12085 dwp_file->name);
12086 }
12087 }
80626a55
DE
12088
12089 return htab;
12090}
12091
12092/* Update SECTIONS with the data from SECTP.
12093
5bb6e9dd
TT
12094 This function is like the other "locate" section routines, but in
12095 this context the sections to read comes from the DWP V1 hash table,
12096 not the full ELF section table.
80626a55
DE
12097
12098 The result is non-zero for success, or zero if an error was found. */
12099
12100static int
73869dc2
DE
12101locate_v1_virtual_dwo_sections (asection *sectp,
12102 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12103{
12104 const struct dwop_section_names *names = &dwop_section_names;
12105
12106 if (section_is_p (sectp->name, &names->abbrev_dwo))
12107 {
12108 /* There can be only one. */
049412e3 12109 if (sections->abbrev.s.section != NULL)
80626a55 12110 return 0;
049412e3 12111 sections->abbrev.s.section = sectp;
fd361982 12112 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12113 }
12114 else if (section_is_p (sectp->name, &names->info_dwo)
12115 || section_is_p (sectp->name, &names->types_dwo))
12116 {
12117 /* There can be only one. */
049412e3 12118 if (sections->info_or_types.s.section != NULL)
80626a55 12119 return 0;
049412e3 12120 sections->info_or_types.s.section = sectp;
fd361982 12121 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
12122 }
12123 else if (section_is_p (sectp->name, &names->line_dwo))
12124 {
12125 /* There can be only one. */
049412e3 12126 if (sections->line.s.section != NULL)
80626a55 12127 return 0;
049412e3 12128 sections->line.s.section = sectp;
fd361982 12129 sections->line.size = bfd_section_size (sectp);
80626a55
DE
12130 }
12131 else if (section_is_p (sectp->name, &names->loc_dwo))
12132 {
12133 /* There can be only one. */
049412e3 12134 if (sections->loc.s.section != NULL)
80626a55 12135 return 0;
049412e3 12136 sections->loc.s.section = sectp;
fd361982 12137 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12138 }
12139 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12140 {
12141 /* There can be only one. */
049412e3 12142 if (sections->macinfo.s.section != NULL)
80626a55 12143 return 0;
049412e3 12144 sections->macinfo.s.section = sectp;
fd361982 12145 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12146 }
12147 else if (section_is_p (sectp->name, &names->macro_dwo))
12148 {
12149 /* There can be only one. */
049412e3 12150 if (sections->macro.s.section != NULL)
80626a55 12151 return 0;
049412e3 12152 sections->macro.s.section = sectp;
fd361982 12153 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12154 }
12155 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12156 {
12157 /* There can be only one. */
049412e3 12158 if (sections->str_offsets.s.section != NULL)
80626a55 12159 return 0;
049412e3 12160 sections->str_offsets.s.section = sectp;
fd361982 12161 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12162 }
12163 else
12164 {
12165 /* No other kind of section is valid. */
12166 return 0;
12167 }
12168
12169 return 1;
12170}
12171
73869dc2
DE
12172/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12173 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12174 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12175 This is for DWP version 1 files. */
80626a55
DE
12176
12177static struct dwo_unit *
976ca316 12178create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 12179 struct dwp_file *dwp_file,
73869dc2
DE
12180 uint32_t unit_index,
12181 const char *comp_dir,
12182 ULONGEST signature, int is_debug_types)
80626a55 12183{
73869dc2
DE
12184 const struct dwp_hash_table *dwp_htab =
12185 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12186 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12187 const char *kind = is_debug_types ? "TU" : "CU";
12188 struct dwo_file *dwo_file;
12189 struct dwo_unit *dwo_unit;
73869dc2 12190 struct virtual_v1_dwo_sections sections;
80626a55 12191 void **dwo_file_slot;
80626a55
DE
12192 int i;
12193
73869dc2
DE
12194 gdb_assert (dwp_file->version == 1);
12195
6f738b01
SM
12196 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12197 kind, pulongest (unit_index), hex_string (signature),
12198 dwp_file->name);
80626a55 12199
19ac8c2e 12200 /* Fetch the sections of this DWO unit.
80626a55
DE
12201 Put a limit on the number of sections we look for so that bad data
12202 doesn't cause us to loop forever. */
12203
73869dc2 12204#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12205 (1 /* .debug_info or .debug_types */ \
12206 + 1 /* .debug_abbrev */ \
12207 + 1 /* .debug_line */ \
12208 + 1 /* .debug_loc */ \
12209 + 1 /* .debug_str_offsets */ \
19ac8c2e 12210 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12211 + 1 /* trailing zero */)
12212
12213 memset (&sections, 0, sizeof (sections));
80626a55 12214
73869dc2 12215 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12216 {
12217 asection *sectp;
12218 uint32_t section_nr =
12219 read_4_bytes (dbfd,
73869dc2
DE
12220 dwp_htab->section_pool.v1.indices
12221 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12222
12223 if (section_nr == 0)
12224 break;
12225 if (section_nr >= dwp_file->num_sections)
12226 {
12227 error (_("Dwarf Error: bad DWP hash table, section number too large"
12228 " [in module %s]"),
12229 dwp_file->name);
12230 }
12231
12232 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12233 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12234 {
12235 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12236 " [in module %s]"),
12237 dwp_file->name);
12238 }
12239 }
12240
12241 if (i < 2
96b79293
TT
12242 || sections.info_or_types.empty ()
12243 || sections.abbrev.empty ())
80626a55
DE
12244 {
12245 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12246 " [in module %s]"),
12247 dwp_file->name);
12248 }
73869dc2 12249 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12250 {
12251 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12252 " [in module %s]"),
12253 dwp_file->name);
12254 }
12255
12256 /* It's easier for the rest of the code if we fake a struct dwo_file and
12257 have dwo_unit "live" in that. At least for now.
12258
12259 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12260 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12261 file, we can combine them back into a virtual DWO file to save space
12262 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12263 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12264
791afaa2
TT
12265 std::string virtual_dwo_name =
12266 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
12267 sections.abbrev.get_id (),
12268 sections.line.get_id (),
12269 sections.loc.get_id (),
12270 sections.str_offsets.get_id ());
80626a55 12271 /* Can we use an existing virtual DWO file? */
976ca316 12272 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12273 comp_dir);
80626a55
DE
12274 /* Create one if necessary. */
12275 if (*dwo_file_slot == NULL)
12276 {
6f738b01
SM
12277 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12278 virtual_dwo_name.c_str ());
12279
51ac9db5 12280 dwo_file = new struct dwo_file;
976ca316 12281 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 12282 dwo_file->comp_dir = comp_dir;
80626a55
DE
12283 dwo_file->sections.abbrev = sections.abbrev;
12284 dwo_file->sections.line = sections.line;
12285 dwo_file->sections.loc = sections.loc;
12286 dwo_file->sections.macinfo = sections.macinfo;
12287 dwo_file->sections.macro = sections.macro;
12288 dwo_file->sections.str_offsets = sections.str_offsets;
12289 /* The "str" section is global to the entire DWP file. */
12290 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12291 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12292 there's no need to record it in dwo_file.
12293 Also, we can't simply record type sections in dwo_file because
12294 we record a pointer into the vector in dwo_unit. As we collect more
12295 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12296 for it, invalidating all copies of pointers into the previous
12297 contents. */
80626a55
DE
12298 *dwo_file_slot = dwo_file;
12299 }
12300 else
12301 {
6f738b01
SM
12302 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12303 virtual_dwo_name.c_str ());
12304
9a3c8263 12305 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12306 }
80626a55 12307
976ca316 12308 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
12309 dwo_unit->dwo_file = dwo_file;
12310 dwo_unit->signature = signature;
8d749320 12311 dwo_unit->section =
976ca316 12312 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 12313 *dwo_unit->section = sections.info_or_types;
57d63ce2 12314 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12315
12316 return dwo_unit;
12317}
12318
d2854d8d
CT
12319/* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12320 simplify them. Given a pointer to the containing section SECTION, and
12321 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12322 virtual section of just that piece. */
73869dc2
DE
12323
12324static struct dwarf2_section_info
d2854d8d
CT
12325create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12326 struct dwarf2_section_info *section,
12327 bfd_size_type offset, bfd_size_type size)
73869dc2
DE
12328{
12329 struct dwarf2_section_info result;
12330 asection *sectp;
12331
12332 gdb_assert (section != NULL);
12333 gdb_assert (!section->is_virtual);
12334
12335 memset (&result, 0, sizeof (result));
12336 result.s.containing_section = section;
dc4ccb6f 12337 result.is_virtual = true;
73869dc2
DE
12338
12339 if (size == 0)
12340 return result;
12341
96b79293 12342 sectp = section->get_bfd_section ();
73869dc2
DE
12343
12344 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12345 bounds of the real section. This is a pretty-rare event, so just
12346 flag an error (easier) instead of a warning and trying to cope. */
12347 if (sectp == NULL
fd361982 12348 || offset + size > bfd_section_size (sectp))
73869dc2 12349 {
d2854d8d 12350 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
73869dc2 12351 " in section %s [in module %s]"),
fd361982 12352 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 12353 objfile_name (per_objfile->objfile));
73869dc2
DE
12354 }
12355
12356 result.virtual_offset = offset;
12357 result.size = size;
12358 return result;
12359}
12360
12361/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12362 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12363 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12364 This is for DWP version 2 files. */
12365
12366static struct dwo_unit *
976ca316 12367create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 12368 struct dwp_file *dwp_file,
73869dc2
DE
12369 uint32_t unit_index,
12370 const char *comp_dir,
12371 ULONGEST signature, int is_debug_types)
12372{
73869dc2
DE
12373 const struct dwp_hash_table *dwp_htab =
12374 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12375 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12376 const char *kind = is_debug_types ? "TU" : "CU";
12377 struct dwo_file *dwo_file;
12378 struct dwo_unit *dwo_unit;
d2854d8d 12379 struct virtual_v2_or_v5_dwo_sections sections;
73869dc2 12380 void **dwo_file_slot;
73869dc2
DE
12381 int i;
12382
12383 gdb_assert (dwp_file->version == 2);
12384
6f738b01
SM
12385 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12386 kind, pulongest (unit_index), hex_string (signature),
12387 dwp_file->name);
73869dc2
DE
12388
12389 /* Fetch the section offsets of this DWO unit. */
12390
12391 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12392
12393 for (i = 0; i < dwp_htab->nr_columns; ++i)
12394 {
12395 uint32_t offset = read_4_bytes (dbfd,
12396 dwp_htab->section_pool.v2.offsets
12397 + (((unit_index - 1) * dwp_htab->nr_columns
12398 + i)
12399 * sizeof (uint32_t)));
12400 uint32_t size = read_4_bytes (dbfd,
12401 dwp_htab->section_pool.v2.sizes
12402 + (((unit_index - 1) * dwp_htab->nr_columns
12403 + i)
12404 * sizeof (uint32_t)));
12405
12406 switch (dwp_htab->section_pool.v2.section_ids[i])
12407 {
12408 case DW_SECT_INFO:
12409 case DW_SECT_TYPES:
12410 sections.info_or_types_offset = offset;
12411 sections.info_or_types_size = size;
12412 break;
12413 case DW_SECT_ABBREV:
12414 sections.abbrev_offset = offset;
12415 sections.abbrev_size = size;
12416 break;
12417 case DW_SECT_LINE:
12418 sections.line_offset = offset;
12419 sections.line_size = size;
12420 break;
12421 case DW_SECT_LOC:
12422 sections.loc_offset = offset;
12423 sections.loc_size = size;
12424 break;
12425 case DW_SECT_STR_OFFSETS:
12426 sections.str_offsets_offset = offset;
12427 sections.str_offsets_size = size;
12428 break;
12429 case DW_SECT_MACINFO:
12430 sections.macinfo_offset = offset;
12431 sections.macinfo_size = size;
12432 break;
12433 case DW_SECT_MACRO:
12434 sections.macro_offset = offset;
12435 sections.macro_size = size;
12436 break;
12437 }
12438 }
12439
12440 /* It's easier for the rest of the code if we fake a struct dwo_file and
12441 have dwo_unit "live" in that. At least for now.
12442
12443 The DWP file can be made up of a random collection of CUs and TUs.
12444 However, for each CU + set of TUs that came from the same original DWO
12445 file, we can combine them back into a virtual DWO file to save space
12446 (fewer struct dwo_file objects to allocate). Remember that for really
12447 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12448
791afaa2
TT
12449 std::string virtual_dwo_name =
12450 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12451 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12452 (long) (sections.line_size ? sections.line_offset : 0),
12453 (long) (sections.loc_size ? sections.loc_offset : 0),
12454 (long) (sections.str_offsets_size
12455 ? sections.str_offsets_offset : 0));
73869dc2 12456 /* Can we use an existing virtual DWO file? */
976ca316 12457 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12458 comp_dir);
73869dc2
DE
12459 /* Create one if necessary. */
12460 if (*dwo_file_slot == NULL)
12461 {
6f738b01
SM
12462 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12463 virtual_dwo_name.c_str ());
12464
51ac9db5 12465 dwo_file = new struct dwo_file;
976ca316 12466 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12467 dwo_file->comp_dir = comp_dir;
12468 dwo_file->sections.abbrev =
d2854d8d
CT
12469 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12470 sections.abbrev_offset,
12471 sections.abbrev_size);
73869dc2 12472 dwo_file->sections.line =
d2854d8d
CT
12473 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12474 sections.line_offset,
12475 sections.line_size);
73869dc2 12476 dwo_file->sections.loc =
d2854d8d
CT
12477 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12478 sections.loc_offset, sections.loc_size);
73869dc2 12479 dwo_file->sections.macinfo =
d2854d8d
CT
12480 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12481 sections.macinfo_offset,
12482 sections.macinfo_size);
73869dc2 12483 dwo_file->sections.macro =
d2854d8d
CT
12484 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12485 sections.macro_offset,
12486 sections.macro_size);
73869dc2 12487 dwo_file->sections.str_offsets =
d2854d8d
CT
12488 create_dwp_v2_or_v5_section (per_objfile,
12489 &dwp_file->sections.str_offsets,
12490 sections.str_offsets_offset,
12491 sections.str_offsets_size);
73869dc2
DE
12492 /* The "str" section is global to the entire DWP file. */
12493 dwo_file->sections.str = dwp_file->sections.str;
12494 /* The info or types section is assigned below to dwo_unit,
12495 there's no need to record it in dwo_file.
12496 Also, we can't simply record type sections in dwo_file because
12497 we record a pointer into the vector in dwo_unit. As we collect more
12498 types we'll grow the vector and eventually have to reallocate space
12499 for it, invalidating all copies of pointers into the previous
12500 contents. */
12501 *dwo_file_slot = dwo_file;
12502 }
12503 else
12504 {
6f738b01
SM
12505 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12506 virtual_dwo_name.c_str ());
12507
9a3c8263 12508 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12509 }
73869dc2 12510
976ca316 12511 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12512 dwo_unit->dwo_file = dwo_file;
12513 dwo_unit->signature = signature;
8d749320 12514 dwo_unit->section =
976ca316 12515 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
d2854d8d 12516 *dwo_unit->section = create_dwp_v2_or_v5_section
dda83cd7 12517 (per_objfile,
d2854d8d
CT
12518 is_debug_types
12519 ? &dwp_file->sections.types
12520 : &dwp_file->sections.info,
12521 sections.info_or_types_offset,
12522 sections.info_or_types_size);
12523 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12524
12525 return dwo_unit;
12526}
12527
12528/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12529 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12530 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12531 This is for DWP version 5 files. */
12532
12533static struct dwo_unit *
12534create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12535 struct dwp_file *dwp_file,
12536 uint32_t unit_index,
12537 const char *comp_dir,
12538 ULONGEST signature, int is_debug_types)
12539{
12540 const struct dwp_hash_table *dwp_htab
12541 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12542 bfd *dbfd = dwp_file->dbfd.get ();
12543 const char *kind = is_debug_types ? "TU" : "CU";
12544 struct dwo_file *dwo_file;
12545 struct dwo_unit *dwo_unit;
12546 struct virtual_v2_or_v5_dwo_sections sections {};
12547 void **dwo_file_slot;
12548
12549 gdb_assert (dwp_file->version == 5);
12550
6f738b01
SM
12551 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12552 kind, pulongest (unit_index), hex_string (signature),
12553 dwp_file->name);
d2854d8d
CT
12554
12555 /* Fetch the section offsets of this DWO unit. */
12556
12557 /* memset (&sections, 0, sizeof (sections)); */
12558
12559 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12560 {
12561 uint32_t offset = read_4_bytes (dbfd,
dda83cd7
SM
12562 dwp_htab->section_pool.v5.offsets
12563 + (((unit_index - 1)
12564 * dwp_htab->nr_columns
12565 + i)
12566 * sizeof (uint32_t)));
d2854d8d 12567 uint32_t size = read_4_bytes (dbfd,
dda83cd7
SM
12568 dwp_htab->section_pool.v5.sizes
12569 + (((unit_index - 1) * dwp_htab->nr_columns
12570 + i)
12571 * sizeof (uint32_t)));
d2854d8d
CT
12572
12573 switch (dwp_htab->section_pool.v5.section_ids[i])
dda83cd7
SM
12574 {
12575 case DW_SECT_ABBREV_V5:
12576 sections.abbrev_offset = offset;
12577 sections.abbrev_size = size;
12578 break;
12579 case DW_SECT_INFO_V5:
12580 sections.info_or_types_offset = offset;
12581 sections.info_or_types_size = size;
12582 break;
12583 case DW_SECT_LINE_V5:
12584 sections.line_offset = offset;
12585 sections.line_size = size;
12586 break;
12587 case DW_SECT_LOCLISTS_V5:
12588 sections.loclists_offset = offset;
12589 sections.loclists_size = size;
12590 break;
12591 case DW_SECT_MACRO_V5:
12592 sections.macro_offset = offset;
12593 sections.macro_size = size;
12594 break;
12595 case DW_SECT_RNGLISTS_V5:
12596 sections.rnglists_offset = offset;
12597 sections.rnglists_size = size;
12598 break;
12599 case DW_SECT_STR_OFFSETS_V5:
12600 sections.str_offsets_offset = offset;
12601 sections.str_offsets_size = size;
12602 break;
12603 case DW_SECT_RESERVED_V5:
12604 default:
12605 break;
12606 }
d2854d8d
CT
12607 }
12608
12609 /* It's easier for the rest of the code if we fake a struct dwo_file and
12610 have dwo_unit "live" in that. At least for now.
12611
12612 The DWP file can be made up of a random collection of CUs and TUs.
12613 However, for each CU + set of TUs that came from the same original DWO
12614 file, we can combine them back into a virtual DWO file to save space
12615 (fewer struct dwo_file objects to allocate). Remember that for really
12616 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12617
12618 std::string virtual_dwo_name =
12619 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
dda83cd7
SM
12620 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12621 (long) (sections.line_size ? sections.line_offset : 0),
12622 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12623 (long) (sections.str_offsets_size
12624 ? sections.str_offsets_offset : 0),
12625 (long) (sections.macro_size ? sections.macro_offset : 0),
12626 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
d2854d8d
CT
12627 /* Can we use an existing virtual DWO file? */
12628 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
dda83cd7
SM
12629 virtual_dwo_name.c_str (),
12630 comp_dir);
d2854d8d
CT
12631 /* Create one if necessary. */
12632 if (*dwo_file_slot == NULL)
12633 {
6f738b01
SM
12634 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12635 virtual_dwo_name.c_str ());
12636
d2854d8d
CT
12637 dwo_file = new struct dwo_file;
12638 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12639 dwo_file->comp_dir = comp_dir;
12640 dwo_file->sections.abbrev =
dda83cd7
SM
12641 create_dwp_v2_or_v5_section (per_objfile,
12642 &dwp_file->sections.abbrev,
12643 sections.abbrev_offset,
12644 sections.abbrev_size);
d2854d8d 12645 dwo_file->sections.line =
dda83cd7
SM
12646 create_dwp_v2_or_v5_section (per_objfile,
12647 &dwp_file->sections.line,
12648 sections.line_offset, sections.line_size);
d2854d8d 12649 dwo_file->sections.macro =
dda83cd7
SM
12650 create_dwp_v2_or_v5_section (per_objfile,
12651 &dwp_file->sections.macro,
12652 sections.macro_offset,
12653 sections.macro_size);
d2854d8d 12654 dwo_file->sections.loclists =
dda83cd7
SM
12655 create_dwp_v2_or_v5_section (per_objfile,
12656 &dwp_file->sections.loclists,
12657 sections.loclists_offset,
12658 sections.loclists_size);
d2854d8d 12659 dwo_file->sections.rnglists =
dda83cd7
SM
12660 create_dwp_v2_or_v5_section (per_objfile,
12661 &dwp_file->sections.rnglists,
12662 sections.rnglists_offset,
12663 sections.rnglists_size);
d2854d8d 12664 dwo_file->sections.str_offsets =
dda83cd7
SM
12665 create_dwp_v2_or_v5_section (per_objfile,
12666 &dwp_file->sections.str_offsets,
12667 sections.str_offsets_offset,
12668 sections.str_offsets_size);
d2854d8d
CT
12669 /* The "str" section is global to the entire DWP file. */
12670 dwo_file->sections.str = dwp_file->sections.str;
12671 /* The info or types section is assigned below to dwo_unit,
dda83cd7
SM
12672 there's no need to record it in dwo_file.
12673 Also, we can't simply record type sections in dwo_file because
12674 we record a pointer into the vector in dwo_unit. As we collect more
12675 types we'll grow the vector and eventually have to reallocate space
12676 for it, invalidating all copies of pointers into the previous
12677 contents. */
d2854d8d
CT
12678 *dwo_file_slot = dwo_file;
12679 }
12680 else
12681 {
6f738b01
SM
12682 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12683 virtual_dwo_name.c_str ());
12684
d2854d8d
CT
12685 dwo_file = (struct dwo_file *) *dwo_file_slot;
12686 }
12687
12688 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12689 dwo_unit->dwo_file = dwo_file;
12690 dwo_unit->signature = signature;
12691 dwo_unit->section
12692 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12693 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
dda83cd7
SM
12694 &dwp_file->sections.info,
12695 sections.info_or_types_offset,
12696 sections.info_or_types_size);
73869dc2
DE
12697 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12698
12699 return dwo_unit;
12700}
12701
57d63ce2
DE
12702/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12703 Returns NULL if the signature isn't found. */
80626a55
DE
12704
12705static struct dwo_unit *
976ca316 12706lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12707 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12708 ULONGEST signature, int is_debug_types)
80626a55 12709{
57d63ce2
DE
12710 const struct dwp_hash_table *dwp_htab =
12711 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12712 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12713 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12714 uint32_t hash = signature & mask;
12715 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12716 unsigned int i;
12717 void **slot;
870f88f7 12718 struct dwo_unit find_dwo_cu;
80626a55
DE
12719
12720 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12721 find_dwo_cu.signature = signature;
19ac8c2e 12722 slot = htab_find_slot (is_debug_types
48b490f2
TT
12723 ? dwp_file->loaded_tus.get ()
12724 : dwp_file->loaded_cus.get (),
19ac8c2e 12725 &find_dwo_cu, INSERT);
80626a55
DE
12726
12727 if (*slot != NULL)
9a3c8263 12728 return (struct dwo_unit *) *slot;
80626a55
DE
12729
12730 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12731 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12732 {
12733 ULONGEST signature_in_table;
12734
12735 signature_in_table =
57d63ce2 12736 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12737 if (signature_in_table == signature)
12738 {
57d63ce2
DE
12739 uint32_t unit_index =
12740 read_4_bytes (dbfd,
12741 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12742
73869dc2
DE
12743 if (dwp_file->version == 1)
12744 {
976ca316
SM
12745 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12746 unit_index, comp_dir,
12747 signature, is_debug_types);
73869dc2 12748 }
d2854d8d 12749 else if (dwp_file->version == 2)
73869dc2 12750 {
976ca316
SM
12751 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12752 unit_index, comp_dir,
12753 signature, is_debug_types);
73869dc2 12754 }
d2854d8d
CT
12755 else /* version == 5 */
12756 {
12757 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12758 unit_index, comp_dir,
12759 signature, is_debug_types);
12760 }
9a3c8263 12761 return (struct dwo_unit *) *slot;
80626a55
DE
12762 }
12763 if (signature_in_table == 0)
12764 return NULL;
12765 hash = (hash + hash2) & mask;
12766 }
12767
12768 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12769 " [in module %s]"),
12770 dwp_file->name);
12771}
12772
ab5088bf 12773/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12774 Open the file specified by FILE_NAME and hand it off to BFD for
12775 preliminary analysis. Return a newly initialized bfd *, which
12776 includes a canonicalized copy of FILE_NAME.
80626a55 12777 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12778 SEARCH_CWD is true if the current directory is to be searched.
12779 It will be searched before debug-file-directory.
13aaf454
DE
12780 If successful, the file is added to the bfd include table of the
12781 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12782 If unable to find/open the file, return NULL.
3019eac3
DE
12783 NOTE: This function is derived from symfile_bfd_open. */
12784
192b62ce 12785static gdb_bfd_ref_ptr
976ca316 12786try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12787 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12788{
24b9144d 12789 int desc;
9c02c129
DE
12790 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12791 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12792 to debug_file_directory. */
e0cc99a6 12793 const char *search_path;
9c02c129
DE
12794 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12795
e0cc99a6 12796 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12797 if (search_cwd)
12798 {
12799 if (*debug_file_directory != '\0')
e0cc99a6
TT
12800 {
12801 search_path_holder.reset (concat (".", dirname_separator_string,
12802 debug_file_directory,
12803 (char *) NULL));
12804 search_path = search_path_holder.get ();
12805 }
6ac97d4c 12806 else
e0cc99a6 12807 search_path = ".";
6ac97d4c 12808 }
9c02c129 12809 else
e0cc99a6 12810 search_path = debug_file_directory;
3019eac3 12811
24b9144d 12812 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12813 if (is_dwp)
12814 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12815
12816 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12817 desc = openp (search_path, flags, file_name,
3019eac3
DE
12818 O_RDONLY | O_BINARY, &absolute_name);
12819 if (desc < 0)
12820 return NULL;
12821
e0cc99a6
TT
12822 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12823 gnutarget, desc));
9c02c129
DE
12824 if (sym_bfd == NULL)
12825 return NULL;
192b62ce 12826 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12827
192b62ce
TT
12828 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12829 return NULL;
3019eac3 12830
13aaf454
DE
12831 /* Success. Record the bfd as having been included by the objfile's bfd.
12832 This is important because things like demangled_names_hash lives in the
12833 objfile's per_bfd space and may have references to things like symbol
12834 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12835 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12836
3019eac3
DE
12837 return sym_bfd;
12838}
12839
ab5088bf 12840/* Try to open DWO file FILE_NAME.
3019eac3
DE
12841 COMP_DIR is the DW_AT_comp_dir attribute.
12842 The result is the bfd handle of the file.
12843 If there is a problem finding or opening the file, return NULL.
12844 Upon success, the canonicalized path of the file is stored in the bfd,
12845 same as symfile_bfd_open. */
12846
192b62ce 12847static gdb_bfd_ref_ptr
976ca316 12848open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12849 const char *file_name, const char *comp_dir)
3019eac3 12850{
80626a55 12851 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12852 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12853 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12854
12855 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12856
12857 if (comp_dir != NULL)
12858 {
43816ebc
TT
12859 gdb::unique_xmalloc_ptr<char> path_to_try
12860 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12861
12862 /* NOTE: If comp_dir is a relative path, this will also try the
12863 search path, which seems useful. */
976ca316 12864 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12865 0 /*is_dwp*/,
192b62ce 12866 1 /*search_cwd*/));
3019eac3
DE
12867 if (abfd != NULL)
12868 return abfd;
12869 }
12870
12871 /* That didn't work, try debug-file-directory, which, despite its name,
12872 is a list of paths. */
12873
12874 if (*debug_file_directory == '\0')
12875 return NULL;
12876
976ca316 12877 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12878 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12879}
12880
80626a55
DE
12881/* This function is mapped across the sections and remembers the offset and
12882 size of each of the DWO debugging sections we are interested in. */
12883
12884static void
5bb6e9dd
TT
12885dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12886 dwo_sections *dwo_sections)
80626a55 12887{
80626a55
DE
12888 const struct dwop_section_names *names = &dwop_section_names;
12889
12890 if (section_is_p (sectp->name, &names->abbrev_dwo))
12891 {
049412e3 12892 dwo_sections->abbrev.s.section = sectp;
fd361982 12893 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12894 }
12895 else if (section_is_p (sectp->name, &names->info_dwo))
12896 {
049412e3 12897 dwo_sections->info.s.section = sectp;
fd361982 12898 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12899 }
12900 else if (section_is_p (sectp->name, &names->line_dwo))
12901 {
049412e3 12902 dwo_sections->line.s.section = sectp;
fd361982 12903 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12904 }
12905 else if (section_is_p (sectp->name, &names->loc_dwo))
12906 {
049412e3 12907 dwo_sections->loc.s.section = sectp;
fd361982 12908 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12909 }
41144253 12910 else if (section_is_p (sectp->name, &names->loclists_dwo))
12911 {
12912 dwo_sections->loclists.s.section = sectp;
12913 dwo_sections->loclists.size = bfd_section_size (sectp);
12914 }
80626a55
DE
12915 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12916 {
049412e3 12917 dwo_sections->macinfo.s.section = sectp;
fd361982 12918 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12919 }
12920 else if (section_is_p (sectp->name, &names->macro_dwo))
12921 {
049412e3 12922 dwo_sections->macro.s.section = sectp;
fd361982 12923 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55 12924 }
d0ce17d8
CT
12925 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12926 {
12927 dwo_sections->rnglists.s.section = sectp;
12928 dwo_sections->rnglists.size = bfd_section_size (sectp);
12929 }
80626a55
DE
12930 else if (section_is_p (sectp->name, &names->str_dwo))
12931 {
049412e3 12932 dwo_sections->str.s.section = sectp;
fd361982 12933 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12934 }
12935 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12936 {
049412e3 12937 dwo_sections->str_offsets.s.section = sectp;
fd361982 12938 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12939 }
12940 else if (section_is_p (sectp->name, &names->types_dwo))
12941 {
12942 struct dwarf2_section_info type_section;
12943
12944 memset (&type_section, 0, sizeof (type_section));
049412e3 12945 type_section.s.section = sectp;
fd361982 12946 type_section.size = bfd_section_size (sectp);
fd5866f6 12947 dwo_sections->types.push_back (type_section);
80626a55
DE
12948 }
12949}
12950
ab5088bf 12951/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12952 by PER_CU. This is for the non-DWP case.
80626a55 12953 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12954
12955static struct dwo_file *
4ab09049
SM
12956open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12957 const char *comp_dir)
3019eac3 12958{
976ca316 12959 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12960
976ca316 12961 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12962 if (dbfd == NULL)
12963 {
6f738b01
SM
12964 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12965
80626a55
DE
12966 return NULL;
12967 }
263db9a1 12968
51ac9db5 12969 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12970 dwo_file->dwo_name = dwo_name;
12971 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12972 dwo_file->dbfd = std::move (dbfd);
3019eac3 12973
5bb6e9dd
TT
12974 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12975 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12976 &dwo_file->sections);
3019eac3 12977
976ca316
SM
12978 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12979 dwo_file->cus);
3019eac3 12980
d2854d8d
CT
12981 if (cu->per_cu->dwarf_version < 5)
12982 {
12983 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12984 dwo_file->sections.types, dwo_file->tus);
12985 }
12986 else
12987 {
12988 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12989 &dwo_file->sections.info, dwo_file->tus,
12990 rcuh_kind::TYPE);
12991 }
3019eac3 12992
6f738b01 12993 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
80626a55 12994
263db9a1 12995 return dwo_file.release ();
3019eac3
DE
12996}
12997
80626a55 12998/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12999 size of each of the DWP debugging sections common to version 1 and 2 that
13000 we are interested in. */
3019eac3 13001
80626a55 13002static void
73869dc2 13003dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
5bb6e9dd 13004 dwp_file *dwp_file)
3019eac3 13005{
80626a55
DE
13006 const struct dwop_section_names *names = &dwop_section_names;
13007 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13008
80626a55 13009 /* Record the ELF section number for later lookup: this is what the
73869dc2 13010 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13011 gdb_assert (elf_section_nr < dwp_file->num_sections);
13012 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13013
80626a55
DE
13014 /* Look for specific sections that we need. */
13015 if (section_is_p (sectp->name, &names->str_dwo))
13016 {
049412e3 13017 dwp_file->sections.str.s.section = sectp;
fd361982 13018 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
13019 }
13020 else if (section_is_p (sectp->name, &names->cu_index))
13021 {
049412e3 13022 dwp_file->sections.cu_index.s.section = sectp;
fd361982 13023 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
13024 }
13025 else if (section_is_p (sectp->name, &names->tu_index))
13026 {
049412e3 13027 dwp_file->sections.tu_index.s.section = sectp;
fd361982 13028 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
13029 }
13030}
3019eac3 13031
73869dc2
DE
13032/* This function is mapped across the sections and remembers the offset and
13033 size of each of the DWP version 2 debugging sections that we are interested
13034 in. This is split into a separate function because we don't know if we
d2854d8d 13035 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
73869dc2
DE
13036
13037static void
13038dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13039{
9a3c8263 13040 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13041 const struct dwop_section_names *names = &dwop_section_names;
13042 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13043
13044 /* Record the ELF section number for later lookup: this is what the
13045 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13046 gdb_assert (elf_section_nr < dwp_file->num_sections);
13047 dwp_file->elf_sections[elf_section_nr] = sectp;
13048
13049 /* Look for specific sections that we need. */
13050 if (section_is_p (sectp->name, &names->abbrev_dwo))
13051 {
049412e3 13052 dwp_file->sections.abbrev.s.section = sectp;
fd361982 13053 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
13054 }
13055 else if (section_is_p (sectp->name, &names->info_dwo))
13056 {
049412e3 13057 dwp_file->sections.info.s.section = sectp;
fd361982 13058 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
13059 }
13060 else if (section_is_p (sectp->name, &names->line_dwo))
13061 {
049412e3 13062 dwp_file->sections.line.s.section = sectp;
fd361982 13063 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
13064 }
13065 else if (section_is_p (sectp->name, &names->loc_dwo))
13066 {
049412e3 13067 dwp_file->sections.loc.s.section = sectp;
fd361982 13068 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
13069 }
13070 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13071 {
049412e3 13072 dwp_file->sections.macinfo.s.section = sectp;
fd361982 13073 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
13074 }
13075 else if (section_is_p (sectp->name, &names->macro_dwo))
13076 {
049412e3 13077 dwp_file->sections.macro.s.section = sectp;
fd361982 13078 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
13079 }
13080 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13081 {
049412e3 13082 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 13083 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
13084 }
13085 else if (section_is_p (sectp->name, &names->types_dwo))
13086 {
049412e3 13087 dwp_file->sections.types.s.section = sectp;
fd361982 13088 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
13089 }
13090}
13091
d2854d8d
CT
13092/* This function is mapped across the sections and remembers the offset and
13093 size of each of the DWP version 5 debugging sections that we are interested
13094 in. This is split into a separate function because we don't know if we
13095 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
13096
13097static void
13098dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13099{
13100 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13101 const struct dwop_section_names *names = &dwop_section_names;
13102 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13103
13104 /* Record the ELF section number for later lookup: this is what the
13105 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13106 gdb_assert (elf_section_nr < dwp_file->num_sections);
13107 dwp_file->elf_sections[elf_section_nr] = sectp;
13108
13109 /* Look for specific sections that we need. */
13110 if (section_is_p (sectp->name, &names->abbrev_dwo))
13111 {
13112 dwp_file->sections.abbrev.s.section = sectp;
13113 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13114 }
13115 else if (section_is_p (sectp->name, &names->info_dwo))
13116 {
13117 dwp_file->sections.info.s.section = sectp;
13118 dwp_file->sections.info.size = bfd_section_size (sectp);
13119 }
13120 else if (section_is_p (sectp->name, &names->line_dwo))
13121 {
13122 dwp_file->sections.line.s.section = sectp;
13123 dwp_file->sections.line.size = bfd_section_size (sectp);
13124 }
13125 else if (section_is_p (sectp->name, &names->loclists_dwo))
13126 {
13127 dwp_file->sections.loclists.s.section = sectp;
13128 dwp_file->sections.loclists.size = bfd_section_size (sectp);
13129 }
13130 else if (section_is_p (sectp->name, &names->macro_dwo))
13131 {
13132 dwp_file->sections.macro.s.section = sectp;
13133 dwp_file->sections.macro.size = bfd_section_size (sectp);
13134 }
13135 else if (section_is_p (sectp->name, &names->rnglists_dwo))
13136 {
13137 dwp_file->sections.rnglists.s.section = sectp;
13138 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
13139 }
13140 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13141 {
13142 dwp_file->sections.str_offsets.s.section = sectp;
13143 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13144 }
13145}
13146
80626a55 13147/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13148
80626a55
DE
13149static hashval_t
13150hash_dwp_loaded_cutus (const void *item)
13151{
9a3c8263 13152 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13153
80626a55
DE
13154 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13155 return dwo_unit->signature;
3019eac3
DE
13156}
13157
80626a55 13158/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13159
80626a55
DE
13160static int
13161eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13162{
9a3c8263
SM
13163 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13164 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13165
80626a55
DE
13166 return dua->signature == dub->signature;
13167}
3019eac3 13168
80626a55 13169/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13170
48b490f2 13171static htab_up
298e9637 13172allocate_dwp_loaded_cutus_table ()
80626a55 13173{
48b490f2
TT
13174 return htab_up (htab_create_alloc (3,
13175 hash_dwp_loaded_cutus,
13176 eq_dwp_loaded_cutus,
13177 NULL, xcalloc, xfree));
80626a55 13178}
3019eac3 13179
ab5088bf
DE
13180/* Try to open DWP file FILE_NAME.
13181 The result is the bfd handle of the file.
13182 If there is a problem finding or opening the file, return NULL.
13183 Upon success, the canonicalized path of the file is stored in the bfd,
13184 same as symfile_bfd_open. */
13185
192b62ce 13186static gdb_bfd_ref_ptr
976ca316 13187open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 13188{
976ca316 13189 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 13190 1 /*is_dwp*/,
192b62ce 13191 1 /*search_cwd*/));
6ac97d4c
DE
13192 if (abfd != NULL)
13193 return abfd;
13194
13195 /* Work around upstream bug 15652.
13196 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13197 [Whether that's a "bug" is debatable, but it is getting in our way.]
13198 We have no real idea where the dwp file is, because gdb's realpath-ing
13199 of the executable's path may have discarded the needed info.
13200 [IWBN if the dwp file name was recorded in the executable, akin to
13201 .gnu_debuglink, but that doesn't exist yet.]
13202 Strip the directory from FILE_NAME and search again. */
13203 if (*debug_file_directory != '\0')
13204 {
13205 /* Don't implicitly search the current directory here.
13206 If the user wants to search "." to handle this case,
13207 it must be added to debug-file-directory. */
976ca316
SM
13208 return try_open_dwop_file (per_objfile, lbasename (file_name),
13209 1 /*is_dwp*/,
6ac97d4c
DE
13210 0 /*search_cwd*/);
13211 }
13212
13213 return NULL;
ab5088bf
DE
13214}
13215
80626a55
DE
13216/* Initialize the use of the DWP file for the current objfile.
13217 By convention the name of the DWP file is ${objfile}.dwp.
13218 The result is NULL if it can't be found. */
a766d390 13219
400174b1 13220static std::unique_ptr<struct dwp_file>
976ca316 13221open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 13222{
976ca316 13223 struct objfile *objfile = per_objfile->objfile;
80626a55 13224
82bf32bc
JK
13225 /* Try to find first .dwp for the binary file before any symbolic links
13226 resolving. */
6c447423
DE
13227
13228 /* If the objfile is a debug file, find the name of the real binary
13229 file and get the name of dwp file from there. */
d721ba37 13230 std::string dwp_name;
6c447423
DE
13231 if (objfile->separate_debug_objfile_backlink != NULL)
13232 {
13233 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13234 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13235
d721ba37 13236 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13237 }
13238 else
d721ba37
PA
13239 dwp_name = objfile->original_name;
13240
13241 dwp_name += ".dwp";
80626a55 13242
976ca316 13243 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13244 if (dbfd == NULL
13245 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13246 {
13247 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13248 dwp_name = objfile_name (objfile);
13249 dwp_name += ".dwp";
976ca316 13250 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
13251 }
13252
80626a55
DE
13253 if (dbfd == NULL)
13254 {
6f738b01
SM
13255 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13256
400174b1 13257 return std::unique_ptr<dwp_file> ();
3019eac3 13258 }
400174b1
TT
13259
13260 const char *name = bfd_get_filename (dbfd.get ());
13261 std::unique_ptr<struct dwp_file> dwp_file
13262 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13263
0a0f4c01 13264 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 13265 dwp_file->elf_sections =
976ca316 13266 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
13267 dwp_file->num_sections, asection *);
13268
5bb6e9dd
TT
13269 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13270 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13271 dwp_file.get ());
80626a55 13272
976ca316 13273 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 13274
976ca316 13275 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 13276
73869dc2 13277 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13278 if (dwp_file->cus && dwp_file->tus
13279 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13280 {
13281 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13282 pretty bizarre. We use pulongest here because that's the established
4d65956b 13283 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13284 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13285 " TU version %s [in DWP file %s]"),
13286 pulongest (dwp_file->cus->version),
d721ba37 13287 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13288 }
08302ed2
DE
13289
13290 if (dwp_file->cus)
13291 dwp_file->version = dwp_file->cus->version;
13292 else if (dwp_file->tus)
13293 dwp_file->version = dwp_file->tus->version;
13294 else
13295 dwp_file->version = 2;
73869dc2 13296
5bb6e9dd
TT
13297 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13298 {
13299 if (dwp_file->version == 2)
13300 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13301 dwp_file.get ());
13302 else
13303 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13304 dwp_file.get ());
13305 }
73869dc2 13306
298e9637
SM
13307 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13308 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 13309
6f738b01
SM
13310 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13311 dwarf_read_debug_printf (" %s CUs, %s TUs",
13312 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13313 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13314
13315 return dwp_file;
3019eac3 13316}
c906108c 13317
ab5088bf
DE
13318/* Wrapper around open_and_init_dwp_file, only open it once. */
13319
13320static struct dwp_file *
976ca316 13321get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 13322{
976ca316 13323 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 13324 {
976ca316
SM
13325 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13326 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 13327 }
976ca316 13328 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
13329}
13330
80626a55
DE
13331/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13332 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13333 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13334 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13335 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13336
13337 This is called, for example, when wanting to read a variable with a
13338 complex location. Therefore we don't want to do file i/o for every call.
13339 Therefore we don't want to look for a DWO file on every call.
13340 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13341 then we check if we've already seen DWO_NAME, and only THEN do we check
13342 for a DWO file.
13343
1c658ad5 13344 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13345 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13346
3019eac3 13347static struct dwo_unit *
4ab09049 13348lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 13349 ULONGEST signature, int is_debug_types)
3019eac3 13350{
976ca316
SM
13351 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13352 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
13353 const char *kind = is_debug_types ? "TU" : "CU";
13354 void **dwo_file_slot;
3019eac3 13355 struct dwo_file *dwo_file;
80626a55 13356 struct dwp_file *dwp_file;
cb1df416 13357
6a506a2d
DE
13358 /* First see if there's a DWP file.
13359 If we have a DWP file but didn't find the DWO inside it, don't
13360 look for the original DWO file. It makes gdb behave differently
13361 depending on whether one is debugging in the build tree. */
cf2c3c16 13362
976ca316 13363 dwp_file = get_dwp_file (per_objfile);
80626a55 13364 if (dwp_file != NULL)
cf2c3c16 13365 {
80626a55
DE
13366 const struct dwp_hash_table *dwp_htab =
13367 is_debug_types ? dwp_file->tus : dwp_file->cus;
13368
13369 if (dwp_htab != NULL)
13370 {
13371 struct dwo_unit *dwo_cutu =
976ca316
SM
13372 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13373 is_debug_types);
80626a55
DE
13374
13375 if (dwo_cutu != NULL)
13376 {
6f738b01
SM
13377 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13378 kind, hex_string (signature),
13379 host_address_to_string (dwo_cutu));
13380
80626a55
DE
13381 return dwo_cutu;
13382 }
13383 }
13384 }
6a506a2d 13385 else
80626a55 13386 {
6a506a2d 13387 /* No DWP file, look for the DWO file. */
80626a55 13388
976ca316 13389 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 13390 if (*dwo_file_slot == NULL)
80626a55 13391 {
6a506a2d 13392 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 13393 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 13394 }
6a506a2d 13395 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13396 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13397
6a506a2d 13398 if (dwo_file != NULL)
19c3d4c9 13399 {
6a506a2d
DE
13400 struct dwo_unit *dwo_cutu = NULL;
13401
13402 if (is_debug_types && dwo_file->tus)
13403 {
13404 struct dwo_unit find_dwo_cutu;
13405
13406 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13407 find_dwo_cutu.signature = signature;
9a3c8263 13408 dwo_cutu
b0b6a987
TT
13409 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13410 &find_dwo_cutu);
6a506a2d 13411 }
33c5cd75 13412 else if (!is_debug_types && dwo_file->cus)
80626a55 13413 {
33c5cd75
DB
13414 struct dwo_unit find_dwo_cutu;
13415
13416 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13417 find_dwo_cutu.signature = signature;
b0b6a987 13418 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 13419 &find_dwo_cutu);
6a506a2d
DE
13420 }
13421
13422 if (dwo_cutu != NULL)
13423 {
6f738b01
SM
13424 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13425 kind, dwo_name, hex_string (signature),
13426 host_address_to_string (dwo_cutu));
13427
6a506a2d 13428 return dwo_cutu;
80626a55
DE
13429 }
13430 }
2e276125 13431 }
9cdd5dbd 13432
80626a55
DE
13433 /* We didn't find it. This could mean a dwo_id mismatch, or
13434 someone deleted the DWO/DWP file, or the search path isn't set up
13435 correctly to find the file. */
13436
6f738b01
SM
13437 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13438 kind, dwo_name, hex_string (signature));
3019eac3 13439
6656a72d
DE
13440 /* This is a warning and not a complaint because it can be caused by
13441 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13442 {
13443 /* Print the name of the DWP file if we looked there, helps the user
13444 better diagnose the problem. */
791afaa2 13445 std::string dwp_text;
43942612
DE
13446
13447 if (dwp_file != NULL)
791afaa2
TT
13448 dwp_text = string_printf (" [in DWP file %s]",
13449 lbasename (dwp_file->name));
43942612 13450
9d8780f0 13451 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 13452 " [in module %s]"),
4ab09049
SM
13453 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13454 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 13455 }
3019eac3 13456 return NULL;
5fb290d7
DJ
13457}
13458
80626a55
DE
13459/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13460 See lookup_dwo_cutu_unit for details. */
13461
13462static struct dwo_unit *
4ab09049 13463lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
13464 ULONGEST signature)
13465{
4ab09049
SM
13466 gdb_assert (!cu->per_cu->is_debug_types);
13467
13468 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
13469}
13470
13471/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13472 See lookup_dwo_cutu_unit for details. */
13473
13474static struct dwo_unit *
4ab09049 13475lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 13476{
4ab09049
SM
13477 gdb_assert (cu->per_cu->is_debug_types);
13478
13479 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13480
13481 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
13482}
13483
89e63ee4
DE
13484/* Traversal function for queue_and_load_all_dwo_tus. */
13485
13486static int
13487queue_and_load_dwo_tu (void **slot, void *info)
13488{
13489 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 13490 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 13491 ULONGEST signature = dwo_unit->signature;
d460f660 13492 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
13493
13494 if (sig_type != NULL)
13495 {
13496 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13497
13498 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13499 a real dependency of PER_CU on SIG_TYPE. That is detected later
13500 while processing PER_CU. */
120ce1b5 13501 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
13502 load_full_type_unit (sig_cu, cu->per_objfile);
13503 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13504 }
13505
13506 return 1;
13507}
13508
1b555f17 13509/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
13510 The DWO may have the only definition of the type, though it may not be
13511 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13512 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13513
13514static void
1b555f17 13515queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
13516{
13517 struct dwo_unit *dwo_unit;
13518 struct dwo_file *dwo_file;
13519
1b555f17
SM
13520 gdb_assert (cu != nullptr);
13521 gdb_assert (!cu->per_cu->is_debug_types);
13522 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 13523
1b555f17 13524 dwo_unit = cu->dwo_unit;
89e63ee4
DE
13525 gdb_assert (dwo_unit != NULL);
13526
13527 dwo_file = dwo_unit->dwo_file;
13528 if (dwo_file->tus != NULL)
1b555f17 13529 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
13530}
13531
3019eac3 13532/* Read in various DIEs. */
348e048f 13533
d389af10 13534/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13535 Inherit only the children of the DW_AT_abstract_origin DIE not being
13536 already referenced by DW_AT_abstract_origin from the children of the
13537 current DIE. */
d389af10
JK
13538
13539static void
13540inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13541{
13542 struct die_info *child_die;
791afaa2 13543 sect_offset *offsetp;
d389af10
JK
13544 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13545 struct die_info *origin_die;
13546 /* Iterator of the ORIGIN_DIE children. */
13547 struct die_info *origin_child_die;
d389af10 13548 struct attribute *attr;
cd02d79d
PA
13549 struct dwarf2_cu *origin_cu;
13550 struct pending **origin_previous_list_in_scope;
d389af10
JK
13551
13552 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13553 if (!attr)
13554 return;
13555
cd02d79d
PA
13556 /* Note that following die references may follow to a die in a
13557 different cu. */
13558
13559 origin_cu = cu;
13560 origin_die = follow_die_ref (die, attr, &origin_cu);
13561
13562 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13563 symbols in. */
13564 origin_previous_list_in_scope = origin_cu->list_in_scope;
13565 origin_cu->list_in_scope = cu->list_in_scope;
13566
edb3359d
DJ
13567 if (die->tag != origin_die->tag
13568 && !(die->tag == DW_TAG_inlined_subroutine
13569 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13570 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13571 sect_offset_str (die->sect_off),
13572 sect_offset_str (origin_die->sect_off));
d389af10 13573
791afaa2 13574 std::vector<sect_offset> offsets;
d389af10 13575
3ea89b92
PMR
13576 for (child_die = die->child;
13577 child_die && child_die->tag;
436c571c 13578 child_die = child_die->sibling)
3ea89b92
PMR
13579 {
13580 struct die_info *child_origin_die;
13581 struct dwarf2_cu *child_origin_cu;
13582
13583 /* We are trying to process concrete instance entries:
216f72a1 13584 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13585 it's not relevant to our analysis here. i.e. detecting DIEs that are
13586 present in the abstract instance but not referenced in the concrete
13587 one. */
216f72a1 13588 if (child_die->tag == DW_TAG_call_site
dda83cd7 13589 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13590 continue;
13591
c38f313d
DJ
13592 /* For each CHILD_DIE, find the corresponding child of
13593 ORIGIN_DIE. If there is more than one layer of
13594 DW_AT_abstract_origin, follow them all; there shouldn't be,
13595 but GCC versions at least through 4.4 generate this (GCC PR
13596 40573). */
3ea89b92
PMR
13597 child_origin_die = child_die;
13598 child_origin_cu = cu;
c38f313d
DJ
13599 while (1)
13600 {
cd02d79d
PA
13601 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13602 child_origin_cu);
c38f313d
DJ
13603 if (attr == NULL)
13604 break;
cd02d79d
PA
13605 child_origin_die = follow_die_ref (child_origin_die, attr,
13606 &child_origin_cu);
c38f313d
DJ
13607 }
13608
d389af10
JK
13609 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13610 counterpart may exist. */
c38f313d 13611 if (child_origin_die != child_die)
d389af10 13612 {
edb3359d
DJ
13613 if (child_die->tag != child_origin_die->tag
13614 && !(child_die->tag == DW_TAG_inlined_subroutine
13615 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13616 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13617 "different tags"),
9d8780f0
SM
13618 sect_offset_str (child_die->sect_off),
13619 sect_offset_str (child_origin_die->sect_off));
c38f313d 13620 if (child_origin_die->parent != origin_die)
b98664d3 13621 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13622 "different parents"),
9d8780f0
SM
13623 sect_offset_str (child_die->sect_off),
13624 sect_offset_str (child_origin_die->sect_off));
c38f313d 13625 else
791afaa2 13626 offsets.push_back (child_origin_die->sect_off);
d389af10 13627 }
d389af10 13628 }
791afaa2
TT
13629 std::sort (offsets.begin (), offsets.end ());
13630 sect_offset *offsets_end = offsets.data () + offsets.size ();
13631 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13632 if (offsetp[-1] == *offsetp)
b98664d3 13633 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13634 "to DIE %s as their abstract origin"),
13635 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13636
791afaa2 13637 offsetp = offsets.data ();
d389af10
JK
13638 origin_child_die = origin_die->child;
13639 while (origin_child_die && origin_child_die->tag)
13640 {
13641 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13642 while (offsetp < offsets_end
9c541725 13643 && *offsetp < origin_child_die->sect_off)
d389af10 13644 offsetp++;
b64f50a1 13645 if (offsetp >= offsets_end
9c541725 13646 || *offsetp > origin_child_die->sect_off)
d389af10 13647 {
adde2bff
DE
13648 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13649 Check whether we're already processing ORIGIN_CHILD_DIE.
13650 This can happen with mutually referenced abstract_origins.
13651 PR 16581. */
13652 if (!origin_child_die->in_process)
13653 process_die (origin_child_die, origin_cu);
d389af10 13654 }
436c571c 13655 origin_child_die = origin_child_die->sibling;
d389af10 13656 }
cd02d79d 13657 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13658
13659 if (cu != origin_cu)
13660 compute_delayed_physnames (origin_cu);
d389af10
JK
13661}
13662
c906108c 13663static void
e7c27a73 13664read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13665{
5e22e966 13666 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13667 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13668 struct context_stack *newobj;
c906108c
SS
13669 CORE_ADDR lowpc;
13670 CORE_ADDR highpc;
13671 struct die_info *child_die;
edb3359d 13672 struct attribute *attr, *call_line, *call_file;
15d034d0 13673 const char *name;
e142c38c 13674 CORE_ADDR baseaddr;
801e3a5b 13675 struct block *block;
edb3359d 13676 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13677 std::vector<struct symbol *> template_args;
34eaf542 13678 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13679
13680 if (inlined_func)
13681 {
13682 /* If we do not have call site information, we can't show the
13683 caller of this inlined function. That's too confusing, so
13684 only use the scope for local variables. */
13685 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13686 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13687 if (call_line == NULL || call_file == NULL)
13688 {
13689 read_lexical_block_scope (die, cu);
13690 return;
13691 }
13692 }
c906108c 13693
b3b3bada 13694 baseaddr = objfile->text_section_offset ();
e142c38c 13695
94af9270 13696 name = dwarf2_name (die, cu);
c906108c 13697
e8d05480
JB
13698 /* Ignore functions with missing or empty names. These are actually
13699 illegal according to the DWARF standard. */
13700 if (name == NULL)
13701 {
b98664d3 13702 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13703 sect_offset_str (die->sect_off));
e8d05480
JB
13704 return;
13705 }
13706
13707 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13708 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13709 <= PC_BOUNDS_INVALID)
e8d05480 13710 {
ae4d0c03 13711 attr = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 13712 if (attr == nullptr || !attr->as_boolean ())
b98664d3 13713 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13714 "for subprogram DIE at %s"),
13715 sect_offset_str (die->sect_off));
e8d05480
JB
13716 return;
13717 }
c906108c 13718
3e29f34a
MR
13719 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13720 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13721
34eaf542
TT
13722 /* If we have any template arguments, then we must allocate a
13723 different sort of symbol. */
436c571c 13724 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13725 {
13726 if (child_die->tag == DW_TAG_template_type_param
13727 || child_die->tag == DW_TAG_template_value_param)
13728 {
8c14c3a3 13729 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13730 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13731 break;
13732 }
13733 }
13734
c24bdb02 13735 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13736 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13737 (struct symbol *) templ_func);
4c2df51b 13738
81873cc8 13739 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13740 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13741 cu->language);
13742
4cecd739
DJ
13743 /* If there is a location expression for DW_AT_frame_base, record
13744 it. */
e142c38c 13745 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13746 if (attr != nullptr)
fe978cb0 13747 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13748
63e43d3a
PMR
13749 /* If there is a location for the static link, record it. */
13750 newobj->static_link = NULL;
13751 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13752 if (attr != nullptr)
63e43d3a 13753 {
224c3ddb
SM
13754 newobj->static_link
13755 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13756 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13757 cu->addr_type ());
63e43d3a
PMR
13758 }
13759
c24bdb02 13760 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13761
639d11d3 13762 if (die->child != NULL)
c906108c 13763 {
639d11d3 13764 child_die = die->child;
c906108c
SS
13765 while (child_die && child_die->tag)
13766 {
34eaf542
TT
13767 if (child_die->tag == DW_TAG_template_type_param
13768 || child_die->tag == DW_TAG_template_value_param)
13769 {
13770 struct symbol *arg = new_symbol (child_die, NULL, cu);
13771
f1078f66 13772 if (arg != NULL)
2f4732b0 13773 template_args.push_back (arg);
34eaf542
TT
13774 }
13775 else
13776 process_die (child_die, cu);
436c571c 13777 child_die = child_die->sibling;
c906108c
SS
13778 }
13779 }
13780
d389af10
JK
13781 inherit_abstract_dies (die, cu);
13782
4a811a97
UW
13783 /* If we have a DW_AT_specification, we might need to import using
13784 directives from the context of the specification DIE. See the
13785 comment in determine_prefix. */
13786 if (cu->language == language_cplus
13787 && dwarf2_attr (die, DW_AT_specification, cu))
13788 {
13789 struct dwarf2_cu *spec_cu = cu;
13790 struct die_info *spec_die = die_specification (die, &spec_cu);
13791
13792 while (spec_die)
13793 {
13794 child_die = spec_die->child;
13795 while (child_die && child_die->tag)
13796 {
13797 if (child_die->tag == DW_TAG_imported_module)
13798 process_die (child_die, spec_cu);
436c571c 13799 child_die = child_die->sibling;
4a811a97
UW
13800 }
13801
13802 /* In some cases, GCC generates specification DIEs that
13803 themselves contain DW_AT_specification attributes. */
13804 spec_die = die_specification (spec_die, &spec_cu);
13805 }
13806 }
13807
c24bdb02 13808 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13809 /* Make a block for the local symbols within. */
c24bdb02 13810 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13811 cstk.static_link, lowpc, highpc);
801e3a5b 13812
df8a16a1 13813 /* For C++, set the block's scope. */
45280282
IB
13814 if ((cu->language == language_cplus
13815 || cu->language == language_fortran
c44af4eb
TT
13816 || cu->language == language_d
13817 || cu->language == language_rust)
4d4ec4e5 13818 && cu->processing_has_namespace_info)
195a3f6c
TT
13819 block_set_scope (block, determine_prefix (die, cu),
13820 &objfile->objfile_obstack);
df8a16a1 13821
801e3a5b
JB
13822 /* If we have address ranges, record them. */
13823 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13824
a60f3166 13825 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13826
34eaf542 13827 /* Attach template arguments to function. */
2f4732b0 13828 if (!template_args.empty ())
34eaf542
TT
13829 {
13830 gdb_assert (templ_func != NULL);
13831
2f4732b0 13832 templ_func->n_template_arguments = template_args.size ();
34eaf542 13833 templ_func->template_arguments
dda83cd7 13834 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
8d749320 13835 templ_func->n_template_arguments);
34eaf542 13836 memcpy (templ_func->template_arguments,
2f4732b0 13837 template_args.data (),
34eaf542 13838 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13839
13840 /* Make sure that the symtab is set on the new symbols. Even
13841 though they don't appear in this symtab directly, other parts
13842 of gdb assume that symbols do, and this is reasonably
13843 true. */
8634679f 13844 for (symbol *sym : template_args)
3e1d3d8c 13845 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13846 }
13847
208d8187
JB
13848 /* In C++, we can have functions nested inside functions (e.g., when
13849 a function declares a class that has methods). This means that
13850 when we finish processing a function scope, we may need to go
13851 back to building a containing block's symbol lists. */
c24bdb02
KS
13852 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13853 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13854
921e78cf
JB
13855 /* If we've finished processing a top-level function, subsequent
13856 symbols go in the file symbol list. */
c24bdb02
KS
13857 if (cu->get_builder ()->outermost_context_p ())
13858 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13859}
13860
13861/* Process all the DIES contained within a lexical block scope. Start
13862 a new scope, process the dies, and then close the scope. */
13863
13864static void
e7c27a73 13865read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13866{
5e22e966 13867 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13868 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13869 CORE_ADDR lowpc, highpc;
13870 struct die_info *child_die;
e142c38c
DJ
13871 CORE_ADDR baseaddr;
13872
b3b3bada 13873 baseaddr = objfile->text_section_offset ();
c906108c
SS
13874
13875 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13876 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13877 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13878 be nasty. Might be easier to properly extend generic blocks to
af34e669 13879 describe ranges. */
e385593e
JK
13880 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13881 {
13882 case PC_BOUNDS_NOT_PRESENT:
13883 /* DW_TAG_lexical_block has no attributes, process its children as if
13884 there was no wrapping by that DW_TAG_lexical_block.
13885 GCC does no longer produces such DWARF since GCC r224161. */
13886 for (child_die = die->child;
13887 child_die != NULL && child_die->tag;
436c571c 13888 child_die = child_die->sibling)
4f7bc5ed
TT
13889 {
13890 /* We might already be processing this DIE. This can happen
13891 in an unusual circumstance -- where a subroutine A
13892 appears lexically in another subroutine B, but A actually
13893 inlines B. The recursion is broken here, rather than in
13894 inherit_abstract_dies, because it seems better to simply
13895 drop concrete children here. */
13896 if (!child_die->in_process)
13897 process_die (child_die, cu);
13898 }
e385593e
JK
13899 return;
13900 case PC_BOUNDS_INVALID:
13901 return;
13902 }
3e29f34a
MR
13903 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13904 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13905
c24bdb02 13906 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13907 if (die->child != NULL)
c906108c 13908 {
639d11d3 13909 child_die = die->child;
c906108c
SS
13910 while (child_die && child_die->tag)
13911 {
e7c27a73 13912 process_die (child_die, cu);
436c571c 13913 child_die = child_die->sibling;
c906108c
SS
13914 }
13915 }
3ea89b92 13916 inherit_abstract_dies (die, cu);
c24bdb02 13917 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13918
c24bdb02
KS
13919 if (*cu->get_builder ()->get_local_symbols () != NULL
13920 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13921 {
801e3a5b 13922 struct block *block
dda83cd7 13923 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13924 cstk.start_addr, highpc);
801e3a5b
JB
13925
13926 /* Note that recording ranges after traversing children, as we
dda83cd7
SM
13927 do here, means that recording a parent's ranges entails
13928 walking across all its children's ranges as they appear in
13929 the address map, which is quadratic behavior.
13930
13931 It would be nicer to record the parent's ranges before
13932 traversing its children, simply overriding whatever you find
13933 there. But since we don't even decide whether to create a
13934 block until after we've traversed its children, that's hard
13935 to do. */
801e3a5b 13936 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13937 }
c24bdb02
KS
13938 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13939 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13940}
13941
216f72a1 13942/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13943
13944static void
13945read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13946{
5e22e966 13947 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13948 struct objfile *objfile = per_objfile->objfile;
08feed99 13949 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13950 CORE_ADDR pc, baseaddr;
13951 struct attribute *attr;
13952 struct call_site *call_site, call_site_local;
13953 void **slot;
13954 int nparams;
13955 struct die_info *child_die;
13956
b3b3bada 13957 baseaddr = objfile->text_section_offset ();
96408a79 13958
216f72a1
JK
13959 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13960 if (attr == NULL)
13961 {
13962 /* This was a pre-DWARF-5 GNU extension alias
13963 for DW_AT_call_return_pc. */
13964 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13965 }
96408a79
SA
13966 if (!attr)
13967 {
b98664d3 13968 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13969 "DIE %s [in module %s]"),
13970 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13971 return;
13972 }
95f982e5 13973 pc = attr->as_address () + baseaddr;
3e29f34a 13974 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13975
13976 if (cu->call_site_htab == NULL)
13977 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13978 NULL, &objfile->objfile_obstack,
13979 hashtab_obstack_allocate, NULL);
13980 call_site_local.pc = pc;
13981 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13982 if (*slot != NULL)
13983 {
b98664d3 13984 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13985 "DIE %s [in module %s]"),
13986 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13987 objfile_name (objfile));
96408a79
SA
13988 return;
13989 }
13990
13991 /* Count parameters at the caller. */
13992
13993 nparams = 0;
13994 for (child_die = die->child; child_die && child_die->tag;
436c571c 13995 child_die = child_die->sibling)
96408a79 13996 {
216f72a1 13997 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13998 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13999 {
b98664d3 14000 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
14001 "DW_TAG_call_site child DIE %s [in module %s]"),
14002 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 14003 objfile_name (objfile));
96408a79
SA
14004 continue;
14005 }
14006
14007 nparams++;
14008 }
14009
224c3ddb
SM
14010 call_site
14011 = ((struct call_site *)
14012 obstack_alloc (&objfile->objfile_obstack,
14013 sizeof (*call_site)
14014 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
14015 *slot = call_site;
14016 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14017 call_site->pc = pc;
14018
216f72a1
JK
14019 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14020 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
14021 {
14022 struct die_info *func_die;
14023
14024 /* Skip also over DW_TAG_inlined_subroutine. */
14025 for (func_die = die->parent;
14026 func_die && func_die->tag != DW_TAG_subprogram
14027 && func_die->tag != DW_TAG_subroutine_type;
14028 func_die = func_die->parent);
14029
216f72a1
JK
14030 /* DW_AT_call_all_calls is a superset
14031 of DW_AT_call_all_tail_calls. */
96408a79 14032 if (func_die
dda83cd7
SM
14033 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14034 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14035 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14036 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14037 {
14038 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14039 not complete. But keep CALL_SITE for look ups via call_site_htab,
14040 both the initial caller containing the real return address PC and
14041 the final callee containing the current PC of a chain of tail
14042 calls do not need to have the tail call list complete. But any
14043 function candidate for a virtual tail call frame searched via
14044 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14045 determined unambiguously. */
14046 }
14047 else
14048 {
14049 struct type *func_type = NULL;
14050
14051 if (func_die)
14052 func_type = get_die_type (func_die, cu);
14053 if (func_type != NULL)
14054 {
78134374 14055 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
14056
14057 /* Enlist this call site to the function. */
14058 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14059 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14060 }
14061 else
b98664d3 14062 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14063 "DIE %s [in module %s]"),
14064 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14065 }
14066 }
14067
216f72a1
JK
14068 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14069 if (attr == NULL)
14070 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14071 if (attr == NULL)
14072 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14073 if (attr == NULL)
216f72a1
JK
14074 {
14075 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14076 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14077 }
96408a79 14078 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9d2246fc 14079 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
96408a79 14080 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 14081 else if (attr->form_is_block ())
96408a79
SA
14082 {
14083 struct dwarf2_locexpr_baton *dlbaton;
9d2246fc 14084 struct dwarf_block *block = attr->as_block ();
96408a79 14085
8d749320 14086 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9d2246fc
TT
14087 dlbaton->data = block->data;
14088 dlbaton->size = block->size;
a50264ba 14089 dlbaton->per_objfile = per_objfile;
96408a79
SA
14090 dlbaton->per_cu = cu->per_cu;
14091
14092 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14093 }
cd6c91b4 14094 else if (attr->form_is_ref ())
96408a79 14095 {
96408a79
SA
14096 struct dwarf2_cu *target_cu = cu;
14097 struct die_info *target_die;
14098
ac9ec31b 14099 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 14100 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
14101 if (die_is_declaration (target_die, target_cu))
14102 {
7d45c7c3 14103 const char *target_physname;
9112db09
JK
14104
14105 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14106 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14107 if (target_physname == NULL)
9112db09 14108 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14109 if (target_physname == NULL)
b98664d3 14110 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 14111 "physname, for referencing DIE %s [in module %s]"),
9d8780f0 14112 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14113 else
7d455152 14114 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14115 }
14116 else
14117 {
14118 CORE_ADDR lowpc;
14119
14120 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14121 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14122 <= PC_BOUNDS_INVALID)
b98664d3 14123 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 14124 "low pc, for referencing DIE %s [in module %s]"),
9d8780f0 14125 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14126 else
3e29f34a
MR
14127 {
14128 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14129 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14130 }
96408a79
SA
14131 }
14132 }
14133 else
b98664d3 14134 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14135 "block nor reference, for DIE %s [in module %s]"),
14136 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14137
14138 call_site->per_cu = cu->per_cu;
9f47c707 14139 call_site->per_objfile = per_objfile;
96408a79
SA
14140
14141 for (child_die = die->child;
14142 child_die && child_die->tag;
436c571c 14143 child_die = child_die->sibling)
96408a79 14144 {
96408a79 14145 struct call_site_parameter *parameter;
1788b2d3 14146 struct attribute *loc, *origin;
96408a79 14147
216f72a1 14148 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 14149 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14150 {
14151 /* Already printed the complaint above. */
14152 continue;
14153 }
14154
14155 gdb_assert (call_site->parameter_count < nparams);
14156 parameter = &call_site->parameter[call_site->parameter_count];
14157
1788b2d3
JK
14158 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14159 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14160 register is contained in DW_AT_call_value. */
96408a79 14161
24c5c679 14162 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14163 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14164 if (origin == NULL)
14165 {
14166 /* This was a pre-DWARF-5 GNU extension alias
14167 for DW_AT_call_parameter. */
14168 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14169 }
cd6c91b4 14170 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 14171 {
1788b2d3 14172 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 14173
0826b30a 14174 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 14175 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
14176 {
14177 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14178 binding can be done only inside one CU. Such referenced DIE
14179 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14180 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14181 "DW_TAG_call_site child DIE %s [in module %s]"),
14182 sect_offset_str (child_die->sect_off),
9c541725 14183 objfile_name (objfile));
d76b7dbc
JK
14184 continue;
14185 }
9c541725
PA
14186 parameter->u.param_cu_off
14187 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 14188 }
4fc6c0d5 14189 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 14190 {
b98664d3 14191 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14192 "DW_TAG_call_site child DIE %s [in module %s]"),
14193 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14194 continue;
14195 }
24c5c679 14196 else
96408a79 14197 {
9d2246fc
TT
14198 struct dwarf_block *block = loc->as_block ();
14199
24c5c679 14200 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9d2246fc 14201 (block->data, &block->data[block->size]);
24c5c679
JK
14202 if (parameter->u.dwarf_reg != -1)
14203 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9d2246fc
TT
14204 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14205 &block->data[block->size],
24c5c679
JK
14206 &parameter->u.fb_offset))
14207 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14208 else
14209 {
b98664d3 14210 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14211 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14212 "DW_TAG_call_site child DIE %s "
24c5c679 14213 "[in module %s]"),
9d8780f0 14214 sect_offset_str (child_die->sect_off),
9c541725 14215 objfile_name (objfile));
24c5c679
JK
14216 continue;
14217 }
96408a79
SA
14218 }
14219
216f72a1
JK
14220 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14221 if (attr == NULL)
14222 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 14223 if (attr == NULL || !attr->form_is_block ())
96408a79 14224 {
b98664d3 14225 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14226 "DW_TAG_call_site child DIE %s [in module %s]"),
14227 sect_offset_str (child_die->sect_off),
9c541725 14228 objfile_name (objfile));
96408a79
SA
14229 continue;
14230 }
9d2246fc
TT
14231
14232 struct dwarf_block *block = attr->as_block ();
14233 parameter->value = block->data;
14234 parameter->value_size = block->size;
96408a79
SA
14235
14236 /* Parameters are not pre-cleared by memset above. */
14237 parameter->data_value = NULL;
14238 parameter->data_value_size = 0;
14239 call_site->parameter_count++;
14240
216f72a1
JK
14241 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14242 if (attr == NULL)
14243 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14244 if (attr != nullptr)
96408a79 14245 {
4fc6c0d5 14246 if (!attr->form_is_block ())
b98664d3 14247 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14248 "DW_TAG_call_site child DIE %s [in module %s]"),
14249 sect_offset_str (child_die->sect_off),
9c541725 14250 objfile_name (objfile));
96408a79
SA
14251 else
14252 {
9d2246fc
TT
14253 block = attr->as_block ();
14254 parameter->data_value = block->data;
14255 parameter->data_value_size = block->size;
96408a79
SA
14256 }
14257 }
14258 }
14259}
14260
71a3c369
TT
14261/* Helper function for read_variable. If DIE represents a virtual
14262 table, then return the type of the concrete object that is
14263 associated with the virtual table. Otherwise, return NULL. */
14264
14265static struct type *
14266rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14267{
14268 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14269 if (attr == NULL)
14270 return NULL;
14271
14272 /* Find the type DIE. */
14273 struct die_info *type_die = NULL;
14274 struct dwarf2_cu *type_cu = cu;
14275
cd6c91b4 14276 if (attr->form_is_ref ())
71a3c369
TT
14277 type_die = follow_die_ref (die, attr, &type_cu);
14278 if (type_die == NULL)
14279 return NULL;
14280
14281 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14282 return NULL;
14283 return die_containing_type (type_die, type_cu);
14284}
14285
14286/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14287
14288static void
14289read_variable (struct die_info *die, struct dwarf2_cu *cu)
14290{
14291 struct rust_vtable_symbol *storage = NULL;
14292
14293 if (cu->language == language_rust)
14294 {
14295 struct type *containing_type = rust_containing_type (die, cu);
14296
14297 if (containing_type != NULL)
14298 {
5e22e966 14299 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 14300
8c14c3a3 14301 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 14302 storage->concrete_type = containing_type;
cf724bc9 14303 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14304 }
14305 }
14306
e4a62c65
TV
14307 struct symbol *res = new_symbol (die, NULL, cu, storage);
14308 struct attribute *abstract_origin
14309 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14310 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14311 if (res == NULL && loc && abstract_origin)
14312 {
14313 /* We have a variable without a name, but with a location and an abstract
14314 origin. This may be a concrete instance of an abstract variable
14315 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14316 later. */
14317 struct dwarf2_cu *origin_cu = cu;
14318 struct die_info *origin_die
14319 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
14320 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14321 per_objfile->per_bfd->abstract_to_concrete
14322 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14323 }
71a3c369
TT
14324}
14325
43988095
JK
14326/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14327 reading .debug_rnglists.
14328 Callback's type should be:
14329 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14330 Return true if the attributes are present and valid, otherwise,
14331 return false. */
14332
14333template <typename Callback>
14334static bool
14335dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
d0ce17d8 14336 dwarf_tag tag, Callback &&callback)
43988095 14337{
976ca316
SM
14338 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14339 struct objfile *objfile = per_objfile->objfile;
43988095 14340 bfd *obfd = objfile->obfd;
43988095 14341 /* Base address selection entry. */
2b24b6e4 14342 gdb::optional<CORE_ADDR> base;
43988095 14343 const gdb_byte *buffer;
43988095
JK
14344 CORE_ADDR baseaddr;
14345 bool overflow = false;
d0ce17d8
CT
14346 ULONGEST addr_index;
14347 struct dwarf2_section_info *rnglists_section;
43988095 14348
43988095 14349 base = cu->base_address;
d0ce17d8
CT
14350 rnglists_section = cu_debug_rnglists_section (cu, tag);
14351 rnglists_section->read (objfile);
43988095 14352
d0ce17d8 14353 if (offset >= rnglists_section->size)
43988095 14354 {
b98664d3 14355 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14356 offset);
14357 return false;
14358 }
d0ce17d8 14359 buffer = rnglists_section->buffer + offset;
43988095 14360
b3b3bada 14361 baseaddr = objfile->text_section_offset ();
43988095
JK
14362
14363 while (1)
14364 {
7814882a
JK
14365 /* Initialize it due to a false compiler warning. */
14366 CORE_ADDR range_beginning = 0, range_end = 0;
d0ce17d8
CT
14367 const gdb_byte *buf_end = (rnglists_section->buffer
14368 + rnglists_section->size);
43988095
JK
14369 unsigned int bytes_read;
14370
14371 if (buffer == buf_end)
14372 {
14373 overflow = true;
14374 break;
14375 }
14376 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14377 switch (rlet)
14378 {
14379 case DW_RLE_end_of_list:
14380 break;
14381 case DW_RLE_base_address:
14382 if (buffer + cu->header.addr_size > buf_end)
14383 {
14384 overflow = true;
14385 break;
14386 }
c8a7a66f 14387 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14388 buffer += bytes_read;
14389 break;
dda83cd7
SM
14390 case DW_RLE_base_addressx:
14391 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14392 buffer += bytes_read;
14393 base = read_addr_index (cu, addr_index);
14394 break;
43988095
JK
14395 case DW_RLE_start_length:
14396 if (buffer + cu->header.addr_size > buf_end)
14397 {
14398 overflow = true;
14399 break;
14400 }
c8a7a66f
TT
14401 range_beginning = cu->header.read_address (obfd, buffer,
14402 &bytes_read);
43988095
JK
14403 buffer += bytes_read;
14404 range_end = (range_beginning
14405 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14406 buffer += bytes_read;
14407 if (buffer > buf_end)
14408 {
14409 overflow = true;
14410 break;
14411 }
14412 break;
d0ce17d8 14413 case DW_RLE_startx_length:
dda83cd7
SM
14414 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14415 buffer += bytes_read;
14416 range_beginning = read_addr_index (cu, addr_index);
14417 if (buffer > buf_end)
14418 {
14419 overflow = true;
14420 break;
14421 }
14422 range_end = (range_beginning
14423 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14424 buffer += bytes_read;
14425 break;
43988095
JK
14426 case DW_RLE_offset_pair:
14427 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14428 buffer += bytes_read;
14429 if (buffer > buf_end)
14430 {
14431 overflow = true;
14432 break;
14433 }
14434 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14435 buffer += bytes_read;
14436 if (buffer > buf_end)
14437 {
14438 overflow = true;
14439 break;
14440 }
14441 break;
14442 case DW_RLE_start_end:
14443 if (buffer + 2 * cu->header.addr_size > buf_end)
14444 {
14445 overflow = true;
14446 break;
14447 }
c8a7a66f
TT
14448 range_beginning = cu->header.read_address (obfd, buffer,
14449 &bytes_read);
43988095 14450 buffer += bytes_read;
c8a7a66f 14451 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14452 buffer += bytes_read;
14453 break;
d0ce17d8 14454 case DW_RLE_startx_endx:
dda83cd7
SM
14455 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14456 buffer += bytes_read;
14457 range_beginning = read_addr_index (cu, addr_index);
14458 if (buffer > buf_end)
14459 {
14460 overflow = true;
14461 break;
14462 }
14463 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14464 buffer += bytes_read;
14465 range_end = read_addr_index (cu, addr_index);
14466 break;
43988095 14467 default:
b98664d3 14468 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14469 return false;
14470 }
14471 if (rlet == DW_RLE_end_of_list || overflow)
14472 break;
14473 if (rlet == DW_RLE_base_address)
14474 continue;
14475
43988095
JK
14476 if (range_beginning > range_end)
14477 {
14478 /* Inverted range entries are invalid. */
b98664d3 14479 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14480 return false;
14481 }
14482
14483 /* Empty range entries have no effect. */
14484 if (range_beginning == range_end)
14485 continue;
14486
d0ce17d8
CT
14487 /* Only DW_RLE_offset_pair needs the base address added. */
14488 if (rlet == DW_RLE_offset_pair)
14489 {
14490 if (!base.has_value ())
14491 {
14492 /* We have no valid base address for the DW_RLE_offset_pair. */
14493 complaint (_("Invalid .debug_rnglists data (no base address for "
14494 "DW_RLE_offset_pair)"));
14495 return false;
14496 }
14497
14498 range_beginning += *base;
14499 range_end += *base;
14500 }
43988095
JK
14501
14502 /* A not-uncommon case of bad debug info.
14503 Don't pollute the addrmap with bad data. */
14504 if (range_beginning + baseaddr == 0
976ca316 14505 && !per_objfile->per_bfd->has_section_at_zero)
43988095 14506 {
b98664d3 14507 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14508 " [in module %s]"), objfile_name (objfile));
14509 continue;
14510 }
14511
14512 callback (range_beginning, range_end);
14513 }
14514
14515 if (overflow)
14516 {
b98664d3 14517 complaint (_("Offset %d is not terminated "
43988095
JK
14518 "for DW_AT_ranges attribute"),
14519 offset);
14520 return false;
14521 }
14522
14523 return true;
14524}
14525
14526/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14527 Callback's type should be:
14528 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14529 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14530
43988095 14531template <typename Callback>
43039443 14532static int
d0ce17d8 14533dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
43988095 14534 Callback &&callback)
43039443 14535{
5e22e966
SM
14536 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14537 struct objfile *objfile = per_objfile->objfile;
43039443
JK
14538 struct comp_unit_head *cu_header = &cu->header;
14539 bfd *obfd = objfile->obfd;
14540 unsigned int addr_size = cu_header->addr_size;
14541 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14542 /* Base address selection entry. */
2b24b6e4 14543 gdb::optional<CORE_ADDR> base;
43039443 14544 unsigned int dummy;
d521ce57 14545 const gdb_byte *buffer;
ff013f42 14546 CORE_ADDR baseaddr;
43039443 14547
43988095 14548 if (cu_header->version >= 5)
d0ce17d8 14549 return dwarf2_rnglists_process (offset, cu, tag, callback);
43988095 14550
d00adf39 14551 base = cu->base_address;
43039443 14552
5e22e966
SM
14553 per_objfile->per_bfd->ranges.read (objfile);
14554 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 14555 {
b98664d3 14556 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14557 offset);
14558 return 0;
14559 }
5e22e966 14560 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 14561
b3b3bada 14562 baseaddr = objfile->text_section_offset ();
ff013f42 14563
43039443
JK
14564 while (1)
14565 {
14566 CORE_ADDR range_beginning, range_end;
14567
c8a7a66f 14568 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 14569 buffer += addr_size;
c8a7a66f 14570 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
14571 buffer += addr_size;
14572 offset += 2 * addr_size;
14573
14574 /* An end of list marker is a pair of zero addresses. */
14575 if (range_beginning == 0 && range_end == 0)
14576 /* Found the end of list entry. */
14577 break;
14578
14579 /* Each base address selection entry is a pair of 2 values.
14580 The first is the largest possible address, the second is
14581 the base address. Check for a base address here. */
14582 if ((range_beginning & mask) == mask)
14583 {
28d2bfb9
AB
14584 /* If we found the largest possible address, then we already
14585 have the base address in range_end. */
14586 base = range_end;
43039443
JK
14587 continue;
14588 }
14589
2b24b6e4 14590 if (!base.has_value ())
43039443
JK
14591 {
14592 /* We have no valid base address for the ranges
14593 data. */
b98664d3 14594 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14595 return 0;
14596 }
14597
9277c30c
UW
14598 if (range_beginning > range_end)
14599 {
14600 /* Inverted range entries are invalid. */
b98664d3 14601 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14602 return 0;
14603 }
14604
14605 /* Empty range entries have no effect. */
14606 if (range_beginning == range_end)
14607 continue;
14608
2b24b6e4
TT
14609 range_beginning += *base;
14610 range_end += *base;
43039443 14611
01093045
DE
14612 /* A not-uncommon case of bad debug info.
14613 Don't pollute the addrmap with bad data. */
14614 if (range_beginning + baseaddr == 0
5e22e966 14615 && !per_objfile->per_bfd->has_section_at_zero)
01093045 14616 {
b98664d3 14617 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14618 " [in module %s]"), objfile_name (objfile));
01093045
DE
14619 continue;
14620 }
14621
5f46c5a5
JK
14622 callback (range_beginning, range_end);
14623 }
14624
14625 return 1;
14626}
14627
14628/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14629 Return 1 if the attributes are present and valid, otherwise, return 0.
14630 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14631
14632static int
14633dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14634 CORE_ADDR *high_return, struct dwarf2_cu *cu,
d0ce17d8 14635 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
5f46c5a5 14636{
5e22e966 14637 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14638 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14639 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14640 int low_set = 0;
14641 CORE_ADDR low = 0;
14642 CORE_ADDR high = 0;
14643 int retval;
14644
d0ce17d8 14645 retval = dwarf2_ranges_process (offset, cu, tag,
5f46c5a5
JK
14646 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14647 {
9277c30c 14648 if (ranges_pst != NULL)
3e29f34a
MR
14649 {
14650 CORE_ADDR lowpc;
14651 CORE_ADDR highpc;
14652
79748972
TT
14653 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14654 range_beginning + baseaddr)
14655 - baseaddr);
14656 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14657 range_end + baseaddr)
14658 - baseaddr);
d320c2b5
TT
14659 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14660 lowpc, highpc - 1, ranges_pst);
3e29f34a 14661 }
ff013f42 14662
43039443
JK
14663 /* FIXME: This is recording everything as a low-high
14664 segment of consecutive addresses. We should have a
14665 data structure for discontiguous block ranges
14666 instead. */
14667 if (! low_set)
14668 {
14669 low = range_beginning;
14670 high = range_end;
14671 low_set = 1;
14672 }
14673 else
14674 {
14675 if (range_beginning < low)
14676 low = range_beginning;
14677 if (range_end > high)
14678 high = range_end;
14679 }
5f46c5a5
JK
14680 });
14681 if (!retval)
14682 return 0;
43039443
JK
14683
14684 if (! low_set)
14685 /* If the first entry is an end-of-list marker, the range
14686 describes an empty scope, i.e. no instructions. */
14687 return 0;
14688
14689 if (low_return)
14690 *low_return = low;
14691 if (high_return)
14692 *high_return = high;
14693 return 1;
14694}
14695
3a2b436a
JK
14696/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14697 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14698 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14699
3a2b436a 14700static enum pc_bounds_kind
af34e669 14701dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14702 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14703 dwarf2_psymtab *pst)
c906108c 14704{
976ca316 14705 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14706 struct attribute *attr;
91da1414 14707 struct attribute *attr_high;
af34e669
DJ
14708 CORE_ADDR low = 0;
14709 CORE_ADDR high = 0;
e385593e 14710 enum pc_bounds_kind ret;
c906108c 14711
91da1414
MW
14712 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14713 if (attr_high)
af34e669 14714 {
e142c38c 14715 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14716 if (attr != nullptr)
dda83cd7 14717 {
95f982e5
TT
14718 low = attr->as_address ();
14719 high = attr_high->as_address ();
cd6c91b4 14720 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14721 high += low;
91da1414 14722 }
af34e669
DJ
14723 else
14724 /* Found high w/o low attribute. */
e385593e 14725 return PC_BOUNDS_INVALID;
af34e669
DJ
14726
14727 /* Found consecutive range of addresses. */
3a2b436a 14728 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14729 }
c906108c 14730 else
af34e669 14731 {
e142c38c 14732 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14733 if (attr != nullptr && attr->form_is_unsigned ())
af34e669 14734 {
2b0c7f41
SM
14735 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14736 on DWARF version). */
14737 ULONGEST ranges_offset = attr->as_unsigned ();
14738
14739 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14740 this value. */
14741 if (die->tag != DW_TAG_compile_unit)
14742 ranges_offset += cu->gnu_ranges_base;
2e3cf129 14743
af34e669 14744 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14745 .debug_ranges section. */
d0ce17d8
CT
14746 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14747 die->tag))
e385593e 14748 return PC_BOUNDS_INVALID;
43039443 14749 /* Found discontinuous range of addresses. */
3a2b436a 14750 ret = PC_BOUNDS_RANGES;
af34e669 14751 }
e385593e
JK
14752 else
14753 return PC_BOUNDS_NOT_PRESENT;
af34e669 14754 }
c906108c 14755
48fbe735 14756 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14757 if (high <= low)
e385593e 14758 return PC_BOUNDS_INVALID;
c906108c
SS
14759
14760 /* When using the GNU linker, .gnu.linkonce. sections are used to
14761 eliminate duplicate copies of functions and vtables and such.
14762 The linker will arbitrarily choose one and discard the others.
14763 The AT_*_pc values for such functions refer to local labels in
14764 these sections. If the section from that file was discarded, the
14765 labels are not in the output, so the relocs get a value of 0.
14766 If this is a discarded function, mark the pc bounds as invalid,
14767 so that GDB will ignore it. */
976ca316 14768 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14769 return PC_BOUNDS_INVALID;
c906108c
SS
14770
14771 *lowpc = low;
96408a79
SA
14772 if (highpc)
14773 *highpc = high;
af34e669 14774 return ret;
c906108c
SS
14775}
14776
b084d499
JB
14777/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14778 its low and high PC addresses. Do nothing if these addresses could not
14779 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14780 and HIGHPC to the high address if greater than HIGHPC. */
14781
14782static void
14783dwarf2_get_subprogram_pc_bounds (struct die_info *die,
dda83cd7
SM
14784 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14785 struct dwarf2_cu *cu)
b084d499
JB
14786{
14787 CORE_ADDR low, high;
14788 struct die_info *child = die->child;
14789
e385593e 14790 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14791 {
325fac50
PA
14792 *lowpc = std::min (*lowpc, low);
14793 *highpc = std::max (*highpc, high);
b084d499
JB
14794 }
14795
14796 /* If the language does not allow nested subprograms (either inside
14797 subprograms or lexical blocks), we're done. */
14798 if (cu->language != language_ada)
14799 return;
6e70227d 14800
b084d499
JB
14801 /* Check all the children of the given DIE. If it contains nested
14802 subprograms, then check their pc bounds. Likewise, we need to
14803 check lexical blocks as well, as they may also contain subprogram
14804 definitions. */
14805 while (child && child->tag)
14806 {
14807 if (child->tag == DW_TAG_subprogram
dda83cd7
SM
14808 || child->tag == DW_TAG_lexical_block)
14809 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14810 child = child->sibling;
b084d499
JB
14811 }
14812}
14813
fae299cd
DC
14814/* Get the low and high pc's represented by the scope DIE, and store
14815 them in *LOWPC and *HIGHPC. If the correct values can't be
14816 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14817
14818static void
14819get_scope_pc_bounds (struct die_info *die,
14820 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14821 struct dwarf2_cu *cu)
14822{
14823 CORE_ADDR best_low = (CORE_ADDR) -1;
14824 CORE_ADDR best_high = (CORE_ADDR) 0;
14825 CORE_ADDR current_low, current_high;
14826
3a2b436a 14827 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14828 >= PC_BOUNDS_RANGES)
fae299cd
DC
14829 {
14830 best_low = current_low;
14831 best_high = current_high;
14832 }
14833 else
14834 {
14835 struct die_info *child = die->child;
14836
14837 while (child && child->tag)
14838 {
14839 switch (child->tag) {
14840 case DW_TAG_subprogram:
dda83cd7 14841 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14842 break;
14843 case DW_TAG_namespace:
f55ee35c 14844 case DW_TAG_module:
fae299cd
DC
14845 /* FIXME: carlton/2004-01-16: Should we do this for
14846 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14847 that current GCC's always emit the DIEs corresponding
14848 to definitions of methods of classes as children of a
14849 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14850 the DIEs giving the declarations, which could be
14851 anywhere). But I don't see any reason why the
14852 standards says that they have to be there. */
14853 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14854
14855 if (current_low != ((CORE_ADDR) -1))
14856 {
325fac50
PA
14857 best_low = std::min (best_low, current_low);
14858 best_high = std::max (best_high, current_high);
fae299cd
DC
14859 }
14860 break;
14861 default:
0963b4bd 14862 /* Ignore. */
fae299cd
DC
14863 break;
14864 }
14865
436c571c 14866 child = child->sibling;
fae299cd
DC
14867 }
14868 }
14869
14870 *lowpc = best_low;
14871 *highpc = best_high;
14872}
14873
801e3a5b
JB
14874/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14875 in DIE. */
380bca97 14876
801e3a5b
JB
14877static void
14878dwarf2_record_block_ranges (struct die_info *die, struct block *block,
dda83cd7 14879 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
801e3a5b 14880{
5e22e966 14881 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14882 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14883 struct attribute *attr;
91da1414 14884 struct attribute *attr_high;
801e3a5b 14885
91da1414
MW
14886 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14887 if (attr_high)
801e3a5b 14888 {
801e3a5b 14889 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14890 if (attr != nullptr)
dda83cd7 14891 {
95f982e5
TT
14892 CORE_ADDR low = attr->as_address ();
14893 CORE_ADDR high = attr_high->as_address ();
31aa7e4e 14894
cd6c91b4 14895 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14896 high += low;
9a619af0 14897
3e29f34a
MR
14898 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14899 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14900 cu->get_builder ()->record_block_range (block, low, high - 1);
dda83cd7 14901 }
801e3a5b
JB
14902 }
14903
14904 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14905 if (attr != nullptr && attr->form_is_unsigned ())
801e3a5b 14906 {
2b0c7f41
SM
14907 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14908 on DWARF version). */
14909 ULONGEST ranges_offset = attr->as_unsigned ();
801e3a5b 14910
2b0c7f41
SM
14911 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14912 this value. */
14913 if (die->tag != DW_TAG_compile_unit)
14914 ranges_offset += cu->gnu_ranges_base;
801e3a5b 14915
2d5f09ec 14916 std::vector<blockrange> blockvec;
2b0c7f41 14917 dwarf2_ranges_process (ranges_offset, cu, die->tag,
5f46c5a5
JK
14918 [&] (CORE_ADDR start, CORE_ADDR end)
14919 {
58fdfd2c
JK
14920 start += baseaddr;
14921 end += baseaddr;
5f46c5a5
JK
14922 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14923 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14924 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14925 blockvec.emplace_back (start, end);
5f46c5a5 14926 });
2d5f09ec
KB
14927
14928 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14929 }
14930}
14931
685b1105
JK
14932/* Check whether the producer field indicates either of GCC < 4.6, or the
14933 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14934
685b1105
JK
14935static void
14936check_producer (struct dwarf2_cu *cu)
60d5a603 14937{
38360086 14938 int major, minor;
60d5a603
JK
14939
14940 if (cu->producer == NULL)
14941 {
14942 /* For unknown compilers expect their behavior is DWARF version
14943 compliant.
14944
14945 GCC started to support .debug_types sections by -gdwarf-4 since
14946 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14947 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14948 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14949 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14950 }
b1ffba5a 14951 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14952 {
38360086
MW
14953 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14954 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14955 }
5230b05a 14956 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14957 {
14958 cu->producer_is_icc = true;
14959 cu->producer_is_icc_lt_14 = major < 14;
14960 }
c258c396
JD
14961 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14962 cu->producer_is_codewarrior = true;
685b1105
JK
14963 else
14964 {
14965 /* For other non-GCC compilers, expect their behavior is DWARF version
14966 compliant. */
60d5a603
JK
14967 }
14968
9068261f 14969 cu->checked_producer = true;
685b1105 14970}
ba919b58 14971
685b1105
JK
14972/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14973 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14974 during 4.6.0 experimental. */
14975
9068261f 14976static bool
685b1105
JK
14977producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14978{
14979 if (!cu->checked_producer)
14980 check_producer (cu);
14981
14982 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14983}
14984
c258c396
JD
14985
14986/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14987 with incorrect is_stmt attributes. */
14988
14989static bool
14990producer_is_codewarrior (struct dwarf2_cu *cu)
14991{
14992 if (!cu->checked_producer)
14993 check_producer (cu);
14994
14995 return cu->producer_is_codewarrior;
14996}
14997
bf23a268
TT
14998/* Return the accessibility of DIE, as given by DW_AT_accessibility.
14999 If that attribute is not available, return the appropriate
15000 default. */
60d5a603
JK
15001
15002static enum dwarf_access_attribute
bf23a268 15003dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
60d5a603 15004{
bf23a268
TT
15005 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15006 if (attr != nullptr)
15007 {
15008 LONGEST value = attr->constant_value (-1);
15009 if (value == DW_ACCESS_public
15010 || value == DW_ACCESS_protected
15011 || value == DW_ACCESS_private)
15012 return (dwarf_access_attribute) value;
15013 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
15014 plongest (value));
15015 }
15016
60d5a603
JK
15017 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15018 {
15019 /* The default DWARF 2 accessibility for members is public, the default
15020 accessibility for inheritance is private. */
15021
15022 if (die->tag != DW_TAG_inheritance)
15023 return DW_ACCESS_public;
15024 else
15025 return DW_ACCESS_private;
15026 }
15027 else
15028 {
15029 /* DWARF 3+ defines the default accessibility a different way. The same
15030 rules apply now for DW_TAG_inheritance as for the members and it only
15031 depends on the container kind. */
15032
15033 if (die->parent->tag == DW_TAG_class_type)
15034 return DW_ACCESS_private;
15035 else
15036 return DW_ACCESS_public;
15037 }
15038}
15039
74ac6d43
TT
15040/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15041 offset. If the attribute was not found return 0, otherwise return
15042 1. If it was found but could not properly be handled, set *OFFSET
15043 to 0. */
15044
15045static int
15046handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15047 LONGEST *offset)
15048{
15049 struct attribute *attr;
15050
15051 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15052 if (attr != NULL)
15053 {
15054 *offset = 0;
15055
15056 /* Note that we do not check for a section offset first here.
15057 This is because DW_AT_data_member_location is new in DWARF 4,
15058 so if we see it, we can assume that a constant form is really
15059 a constant and not a section offset. */
cd6c91b4 15060 if (attr->form_is_constant ())
0826b30a 15061 *offset = attr->constant_value (0);
cd6c91b4 15062 else if (attr->form_is_section_offset ())
74ac6d43 15063 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 15064 else if (attr->form_is_block ())
9d2246fc 15065 *offset = decode_locdesc (attr->as_block (), cu);
74ac6d43
TT
15066 else
15067 dwarf2_complex_location_expr_complaint ();
15068
15069 return 1;
15070 }
15071
15072 return 0;
15073}
15074
7d79de9a
TT
15075/* Look for DW_AT_data_member_location and store the results in FIELD. */
15076
15077static void
15078handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15079 struct field *field)
15080{
15081 struct attribute *attr;
15082
15083 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15084 if (attr != NULL)
15085 {
15086 if (attr->form_is_constant ())
15087 {
15088 LONGEST offset = attr->constant_value (0);
15089 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15090 }
15091 else if (attr->form_is_section_offset ())
15092 dwarf2_complex_location_expr_complaint ();
15093 else if (attr->form_is_block ())
15094 {
15095 bool handled;
9d2246fc 15096 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
7d79de9a
TT
15097 if (handled)
15098 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15099 else
15100 {
5e22e966
SM
15101 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15102 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
15103 struct dwarf2_locexpr_baton *dlbaton
15104 = XOBNEW (&objfile->objfile_obstack,
15105 struct dwarf2_locexpr_baton);
9d2246fc
TT
15106 dlbaton->data = attr->as_block ()->data;
15107 dlbaton->size = attr->as_block ()->size;
7d79de9a
TT
15108 /* When using this baton, we want to compute the address
15109 of the field, not the value. This is why
15110 is_reference is set to false here. */
15111 dlbaton->is_reference = false;
5e22e966 15112 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
15113 dlbaton->per_cu = cu->per_cu;
15114
15115 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
15116 }
15117 }
15118 else
15119 dwarf2_complex_location_expr_complaint ();
15120 }
15121}
15122
c906108c
SS
15123/* Add an aggregate field to the field list. */
15124
15125static void
107d2387 15126dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15127 struct dwarf2_cu *cu)
6e70227d 15128{
5e22e966 15129 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 15130 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
15131 struct nextfield *new_field;
15132 struct attribute *attr;
15133 struct field *fp;
15d034d0 15134 const char *fieldname = "";
c906108c 15135
7d0ccb61
DJ
15136 if (die->tag == DW_TAG_inheritance)
15137 {
be2daae6
TT
15138 fip->baseclasses.emplace_back ();
15139 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15140 }
15141 else
15142 {
be2daae6
TT
15143 fip->fields.emplace_back ();
15144 new_field = &fip->fields.back ();
7d0ccb61 15145 }
be2daae6 15146
9c6a1327
TT
15147 new_field->offset = die->sect_off;
15148
bf23a268 15149 new_field->accessibility = dwarf2_access_attribute (die, cu);
c906108c 15150 if (new_field->accessibility != DW_ACCESS_public)
264fc0e2 15151 fip->non_public_fields = true;
60d5a603 15152
e142c38c 15153 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 15154 if (attr != nullptr)
23dca5c3 15155 new_field->virtuality = attr->as_virtuality ();
60d5a603
JK
15156 else
15157 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15158
15159 fp = &new_field->field;
a9a9bd0f 15160
e142c38c 15161 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15162 {
a9a9bd0f 15163 /* Data member other than a C++ static data member. */
6e70227d 15164
c906108c 15165 /* Get type of field. */
5d14b6e5 15166 fp->set_type (die_type (die, cu));
c906108c 15167
d6a843b5 15168 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15169
c906108c 15170 /* Get bit size of field (zero if none). */
e142c38c 15171 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 15172 if (attr != nullptr)
c906108c 15173 {
529908cb 15174 FIELD_BITSIZE (*fp) = attr->constant_value (0);
c906108c
SS
15175 }
15176 else
15177 {
15178 FIELD_BITSIZE (*fp) = 0;
15179 }
15180
15181 /* Get bit offset of field. */
7d79de9a 15182 handle_data_member_location (die, cu, fp);
e142c38c 15183 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7c184d33 15184 if (attr != nullptr && attr->form_is_constant ())
c906108c 15185 {
d5a22e77 15186 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
15187 {
15188 /* For big endian bits, the DW_AT_bit_offset gives the
dda83cd7
SM
15189 additional bit offset from the MSB of the containing
15190 anonymous object to the MSB of the field. We don't
15191 have to do anything special since we don't need to
15192 know the size of the anonymous object. */
529908cb 15193 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
7c184d33 15194 + attr->constant_value (0)));
c906108c
SS
15195 }
15196 else
15197 {
15198 /* For little endian bits, compute the bit offset to the
dda83cd7
SM
15199 MSB of the anonymous object, subtract off the number of
15200 bits from the MSB of the field to the MSB of the
15201 object, and then subtract off the number of bits of
15202 the field itself. The result is the bit offset of
15203 the LSB of the field. */
c906108c 15204 int anonymous_size;
7c184d33 15205 int bit_offset = attr->constant_value (0);
c906108c 15206
e142c38c 15207 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7c184d33 15208 if (attr != nullptr && attr->form_is_constant ())
c906108c
SS
15209 {
15210 /* The size of the anonymous object containing
15211 the bit field is explicit, so use the
15212 indicated size (in bytes). */
7c184d33 15213 anonymous_size = attr->constant_value (0);
c906108c
SS
15214 }
15215 else
15216 {
15217 /* The size of the anonymous object containing
15218 the bit field must be inferred from the type
15219 attribute of the data member containing the
15220 bit field. */
5d14b6e5 15221 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 15222 }
f41f5e61
PA
15223 SET_FIELD_BITPOS (*fp,
15224 (FIELD_BITPOS (*fp)
15225 + anonymous_size * bits_per_byte
15226 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15227 }
15228 }
da5b30da
AA
15229 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15230 if (attr != NULL)
15231 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 15232 + attr->constant_value (0)));
c906108c
SS
15233
15234 /* Get name of field. */
39cbfefa
DJ
15235 fieldname = dwarf2_name (die, cu);
15236 if (fieldname == NULL)
15237 fieldname = "";
d8151005
DJ
15238
15239 /* The name is already allocated along with this objfile, so we don't
15240 need to duplicate it for the type. */
15241 fp->name = fieldname;
c906108c
SS
15242
15243 /* Change accessibility for artificial fields (e.g. virtual table
dda83cd7 15244 pointer or virtual base class pointer) to private. */
e142c38c 15245 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15246 {
d48cc9dd 15247 FIELD_ARTIFICIAL (*fp) = 1;
c906108c 15248 new_field->accessibility = DW_ACCESS_private;
264fc0e2 15249 fip->non_public_fields = true;
c906108c
SS
15250 }
15251 }
a9a9bd0f 15252 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15253 {
a9a9bd0f
DC
15254 /* C++ static member. */
15255
15256 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15257 is a declaration, but all versions of G++ as of this writing
15258 (so through at least 3.2.1) incorrectly generate
15259 DW_TAG_variable tags. */
6e70227d 15260
ff355380 15261 const char *physname;
c906108c 15262
a9a9bd0f 15263 /* Get name of field. */
39cbfefa
DJ
15264 fieldname = dwarf2_name (die, cu);
15265 if (fieldname == NULL)
c906108c
SS
15266 return;
15267
254e6b9e 15268 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15269 if (attr
15270 /* Only create a symbol if this is an external value.
15271 new_symbol checks this and puts the value in the global symbol
15272 table, which we want. If it is not external, new_symbol
15273 will try to put the value in cu->list_in_scope which is wrong. */
15274 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15275 {
15276 /* A static const member, not much different than an enum as far as
15277 we're concerned, except that we can support more types. */
15278 new_symbol (die, NULL, cu);
15279 }
15280
2df3850c 15281 /* Get physical name. */
ff355380 15282 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15283
d8151005
DJ
15284 /* The name is already allocated along with this objfile, so we don't
15285 need to duplicate it for the type. */
15286 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 15287 fp->set_type (die_type (die, cu));
d8151005 15288 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15289 }
15290 else if (die->tag == DW_TAG_inheritance)
15291 {
74ac6d43 15292 /* C++ base class field. */
7d79de9a 15293 handle_data_member_location (die, cu, fp);
c906108c 15294 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
15295 fp->set_type (die_type (die, cu));
15296 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 15297 }
2ddeaf8a
TT
15298 else
15299 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15300}
15301
883fd55a
KS
15302/* Can the type given by DIE define another type? */
15303
15304static bool
15305type_can_define_types (const struct die_info *die)
15306{
15307 switch (die->tag)
15308 {
15309 case DW_TAG_typedef:
15310 case DW_TAG_class_type:
15311 case DW_TAG_structure_type:
15312 case DW_TAG_union_type:
15313 case DW_TAG_enumeration_type:
15314 return true;
15315
15316 default:
15317 return false;
15318 }
15319}
15320
15321/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15322
15323static void
883fd55a
KS
15324dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15325 struct dwarf2_cu *cu)
6e70227d 15326{
be2daae6
TT
15327 struct decl_field fp;
15328 memset (&fp, 0, sizeof (fp));
98751a41 15329
883fd55a 15330 gdb_assert (type_can_define_types (die));
98751a41 15331
883fd55a 15332 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15333 fp.name = dwarf2_name (die, cu);
15334 fp.type = read_type_die (die, cu);
98751a41 15335
c191a687 15336 /* Save accessibility. */
bf23a268 15337 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
c191a687
KS
15338 switch (accessibility)
15339 {
15340 case DW_ACCESS_public:
15341 /* The assumed value if neither private nor protected. */
15342 break;
15343 case DW_ACCESS_private:
be2daae6 15344 fp.is_private = 1;
c191a687
KS
15345 break;
15346 case DW_ACCESS_protected:
be2daae6 15347 fp.is_protected = 1;
c191a687 15348 break;
c191a687
KS
15349 }
15350
883fd55a 15351 if (die->tag == DW_TAG_typedef)
be2daae6 15352 fip->typedef_field_list.push_back (fp);
883fd55a 15353 else
be2daae6 15354 fip->nested_types_list.push_back (fp);
98751a41
JK
15355}
15356
9c6a1327
TT
15357/* A convenience typedef that's used when finding the discriminant
15358 field for a variant part. */
1b95cdb7
SM
15359typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15360 offset_map_type;
9c6a1327
TT
15361
15362/* Compute the discriminant range for a given variant. OBSTACK is
15363 where the results will be stored. VARIANT is the variant to
15364 process. IS_UNSIGNED indicates whether the discriminant is signed
15365 or unsigned. */
15366
15367static const gdb::array_view<discriminant_range>
15368convert_variant_range (struct obstack *obstack, const variant_field &variant,
15369 bool is_unsigned)
15370{
15371 std::vector<discriminant_range> ranges;
15372
15373 if (variant.default_branch)
15374 return {};
15375
15376 if (variant.discr_list_data == nullptr)
15377 {
15378 discriminant_range r
15379 = {variant.discriminant_value, variant.discriminant_value};
15380 ranges.push_back (r);
15381 }
15382 else
15383 {
15384 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15385 variant.discr_list_data->size);
15386 while (!data.empty ())
15387 {
15388 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15389 {
15390 complaint (_("invalid discriminant marker: %d"), data[0]);
15391 break;
15392 }
15393 bool is_range = data[0] == DW_DSC_range;
15394 data = data.slice (1);
15395
15396 ULONGEST low, high;
15397 unsigned int bytes_read;
15398
15399 if (data.empty ())
15400 {
15401 complaint (_("DW_AT_discr_list missing low value"));
15402 break;
15403 }
15404 if (is_unsigned)
15405 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15406 else
15407 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15408 &bytes_read);
15409 data = data.slice (bytes_read);
15410
15411 if (is_range)
15412 {
15413 if (data.empty ())
15414 {
15415 complaint (_("DW_AT_discr_list missing high value"));
15416 break;
15417 }
15418 if (is_unsigned)
15419 high = read_unsigned_leb128 (nullptr, data.data (),
15420 &bytes_read);
15421 else
15422 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15423 &bytes_read);
15424 data = data.slice (bytes_read);
15425 }
15426 else
15427 high = low;
15428
15429 ranges.push_back ({ low, high });
15430 }
15431 }
15432
15433 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15434 ranges.size ());
15435 std::copy (ranges.begin (), ranges.end (), result);
15436 return gdb::array_view<discriminant_range> (result, ranges.size ());
15437}
15438
15439static const gdb::array_view<variant_part> create_variant_parts
15440 (struct obstack *obstack,
15441 const offset_map_type &offset_map,
15442 struct field_info *fi,
15443 const std::vector<variant_part_builder> &variant_parts);
15444
15445/* Fill in a "struct variant" for a given variant field. RESULT is
15446 the variant to fill in. OBSTACK is where any needed allocations
15447 will be done. OFFSET_MAP holds the mapping from section offsets to
15448 fields for the type. FI describes the fields of the type we're
15449 processing. FIELD is the variant field we're converting. */
15450
15451static void
15452create_one_variant (variant &result, struct obstack *obstack,
15453 const offset_map_type &offset_map,
15454 struct field_info *fi, const variant_field &field)
15455{
15456 result.discriminants = convert_variant_range (obstack, field, false);
15457 result.first_field = field.first_field + fi->baseclasses.size ();
15458 result.last_field = field.last_field + fi->baseclasses.size ();
15459 result.parts = create_variant_parts (obstack, offset_map, fi,
15460 field.variant_parts);
15461}
15462
15463/* Fill in a "struct variant_part" for a given variant part. RESULT
15464 is the variant part to fill in. OBSTACK is where any needed
15465 allocations will be done. OFFSET_MAP holds the mapping from
15466 section offsets to fields for the type. FI describes the fields of
15467 the type we're processing. BUILDER is the variant part to be
15468 converted. */
15469
15470static void
15471create_one_variant_part (variant_part &result,
15472 struct obstack *obstack,
15473 const offset_map_type &offset_map,
15474 struct field_info *fi,
15475 const variant_part_builder &builder)
15476{
15477 auto iter = offset_map.find (builder.discriminant_offset);
15478 if (iter == offset_map.end ())
15479 {
15480 result.discriminant_index = -1;
15481 /* Doesn't matter. */
15482 result.is_unsigned = false;
15483 }
15484 else
15485 {
15486 result.discriminant_index = iter->second;
15487 result.is_unsigned
c6d940a9 15488 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
9c6a1327
TT
15489 }
15490
15491 size_t n = builder.variants.size ();
15492 variant *output = new (obstack) variant[n];
15493 for (size_t i = 0; i < n; ++i)
15494 create_one_variant (output[i], obstack, offset_map, fi,
15495 builder.variants[i]);
15496
15497 result.variants = gdb::array_view<variant> (output, n);
15498}
15499
15500/* Create a vector of variant parts that can be attached to a type.
15501 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15502 holds the mapping from section offsets to fields for the type. FI
15503 describes the fields of the type we're processing. VARIANT_PARTS
15504 is the vector to convert. */
15505
15506static const gdb::array_view<variant_part>
15507create_variant_parts (struct obstack *obstack,
15508 const offset_map_type &offset_map,
15509 struct field_info *fi,
15510 const std::vector<variant_part_builder> &variant_parts)
15511{
15512 if (variant_parts.empty ())
15513 return {};
15514
15515 size_t n = variant_parts.size ();
15516 variant_part *result = new (obstack) variant_part[n];
15517 for (size_t i = 0; i < n; ++i)
15518 create_one_variant_part (result[i], obstack, offset_map, fi,
15519 variant_parts[i]);
15520
15521 return gdb::array_view<variant_part> (result, n);
15522}
15523
15524/* Compute the variant part vector for FIP, attaching it to TYPE when
15525 done. */
15526
15527static void
15528add_variant_property (struct field_info *fip, struct type *type,
15529 struct dwarf2_cu *cu)
15530{
15531 /* Map section offsets of fields to their field index. Note the
15532 field index here does not take the number of baseclasses into
15533 account. */
15534 offset_map_type offset_map;
15535 for (int i = 0; i < fip->fields.size (); ++i)
15536 offset_map[fip->fields[i].offset] = i;
15537
5e22e966 15538 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
15539 gdb::array_view<variant_part> parts
15540 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15541 fip->variant_parts);
15542
15543 struct dynamic_prop prop;
8c2e4e06
SM
15544 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15545 obstack_copy (&objfile->objfile_obstack, &parts,
15546 sizeof (parts)));
9c6a1327 15547
5c54719c 15548 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
15549}
15550
c906108c
SS
15551/* Create the vector of fields, and attach it to the type. */
15552
15553static void
fba45db2 15554dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15555 struct dwarf2_cu *cu)
c906108c 15556{
317f7127 15557 int nfields = fip->nfields ();
c906108c
SS
15558
15559 /* Record the field count, allocate space for the array of fields,
15560 and create blank accessibility bitfields if necessary. */
5e33d5f4 15561 type->set_num_fields (nfields);
3cabb6b0
SM
15562 type->set_fields
15563 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 15564
b4ba55a1 15565 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15566 {
15567 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15568
15569 TYPE_FIELD_PRIVATE_BITS (type) =
15570 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15571 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15572
15573 TYPE_FIELD_PROTECTED_BITS (type) =
15574 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15575 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15576
774b6a14
TT
15577 TYPE_FIELD_IGNORE_BITS (type) =
15578 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15579 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15580 }
15581
15582 /* If the type has baseclasses, allocate and clear a bit vector for
15583 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15584 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15585 {
be2daae6 15586 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15587 unsigned char *pointer;
c906108c
SS
15588
15589 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15590 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15591 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15592 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15593 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15594 }
15595
9c6a1327
TT
15596 if (!fip->variant_parts.empty ())
15597 add_variant_property (fip, type, cu);
2ddeaf8a 15598
be2daae6
TT
15599 /* Copy the saved-up fields into the field vector. */
15600 for (int i = 0; i < nfields; ++i)
c906108c 15601 {
be2daae6
TT
15602 struct nextfield &field
15603 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15604 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15605
ceacbf6e 15606 type->field (i) = field.field;
be2daae6 15607 switch (field.accessibility)
c906108c 15608 {
c5aa993b 15609 case DW_ACCESS_private:
b4ba55a1 15610 if (cu->language != language_ada)
be2daae6 15611 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15612 break;
c906108c 15613
c5aa993b 15614 case DW_ACCESS_protected:
b4ba55a1 15615 if (cu->language != language_ada)
be2daae6 15616 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15617 break;
c906108c 15618
c5aa993b
JM
15619 case DW_ACCESS_public:
15620 break;
c906108c 15621
c5aa993b
JM
15622 default:
15623 /* Unknown accessibility. Complain and treat it as public. */
15624 {
b98664d3 15625 complaint (_("unsupported accessibility %d"),
be2daae6 15626 field.accessibility);
c5aa993b
JM
15627 }
15628 break;
c906108c 15629 }
be2daae6 15630 if (i < fip->baseclasses.size ())
c906108c 15631 {
be2daae6 15632 switch (field.virtuality)
c906108c 15633 {
c5aa993b
JM
15634 case DW_VIRTUALITY_virtual:
15635 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15636 if (cu->language == language_ada)
a73c6dcd 15637 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15638 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15639 break;
c906108c
SS
15640 }
15641 }
c906108c
SS
15642 }
15643}
15644
7d27a96d
TT
15645/* Return true if this member function is a constructor, false
15646 otherwise. */
15647
15648static int
15649dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15650{
15651 const char *fieldname;
fe978cb0 15652 const char *type_name;
7d27a96d
TT
15653 int len;
15654
15655 if (die->parent == NULL)
15656 return 0;
15657
15658 if (die->parent->tag != DW_TAG_structure_type
15659 && die->parent->tag != DW_TAG_union_type
15660 && die->parent->tag != DW_TAG_class_type)
15661 return 0;
15662
15663 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15664 type_name = dwarf2_name (die->parent, cu);
15665 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15666 return 0;
15667
15668 len = strlen (fieldname);
fe978cb0
PA
15669 return (strncmp (fieldname, type_name, len) == 0
15670 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15671}
15672
c906108c
SS
15673/* Add a member function to the proper fieldlist. */
15674
15675static void
107d2387 15676dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15677 struct type *type, struct dwarf2_cu *cu)
c906108c 15678{
5e22e966 15679 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15680 struct attribute *attr;
c906108c 15681 int i;
be2daae6 15682 struct fnfieldlist *flp = nullptr;
c906108c 15683 struct fn_field *fnp;
15d034d0 15684 const char *fieldname;
f792889a 15685 struct type *this_type;
c906108c 15686
b4ba55a1 15687 if (cu->language == language_ada)
a73c6dcd 15688 error (_("unexpected member function in Ada type"));
b4ba55a1 15689
2df3850c 15690 /* Get name of member function. */
39cbfefa
DJ
15691 fieldname = dwarf2_name (die, cu);
15692 if (fieldname == NULL)
2df3850c 15693 return;
c906108c 15694
c906108c 15695 /* Look up member function name in fieldlist. */
be2daae6 15696 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15697 {
27bfe10e 15698 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15699 {
15700 flp = &fip->fnfieldlists[i];
15701 break;
15702 }
c906108c
SS
15703 }
15704
be2daae6
TT
15705 /* Create a new fnfieldlist if necessary. */
15706 if (flp == nullptr)
c906108c 15707 {
be2daae6
TT
15708 fip->fnfieldlists.emplace_back ();
15709 flp = &fip->fnfieldlists.back ();
c906108c 15710 flp->name = fieldname;
be2daae6 15711 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15712 }
15713
be2daae6
TT
15714 /* Create a new member function field and add it to the vector of
15715 fnfieldlists. */
15716 flp->fnfields.emplace_back ();
15717 fnp = &flp->fnfields.back ();
3da10d80
KS
15718
15719 /* Delay processing of the physname until later. */
9c37b5ae 15720 if (cu->language == language_cplus)
be2daae6
TT
15721 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15722 die, cu);
3da10d80
KS
15723 else
15724 {
1d06ead6 15725 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15726 fnp->physname = physname ? physname : "";
15727 }
15728
c906108c 15729 fnp->type = alloc_type (objfile);
f792889a 15730 this_type = read_type_die (die, cu);
78134374 15731 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15732 {
1f704f76 15733 int nparams = this_type->num_fields ();
c906108c 15734
f792889a 15735 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15736 of the method itself (TYPE_CODE_METHOD). */
15737 smash_to_method_type (fnp->type, type,
f792889a 15738 TYPE_TARGET_TYPE (this_type),
80fc5e77 15739 this_type->fields (),
1f704f76 15740 this_type->num_fields (),
a409645d 15741 this_type->has_varargs ());
c906108c
SS
15742
15743 /* Handle static member functions.
dda83cd7
SM
15744 Dwarf2 has no clean way to discern C++ static and non-static
15745 member functions. G++ helps GDB by marking the first
15746 parameter for non-static member functions (which is the this
15747 pointer) as artificial. We obtain this information from
15748 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15749 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15750 fnp->voffset = VOFFSET_STATIC;
15751 }
15752 else
b98664d3 15753 complaint (_("member function type missing for '%s'"),
3da10d80 15754 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15755
15756 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15757 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15758 fnp->fcontext = die_containing_type (die, cu);
c906108c 15759
3e43a32a
MS
15760 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15761 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15762
15763 /* Get accessibility. */
bf23a268 15764 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
60d5a603 15765 switch (accessibility)
c906108c 15766 {
60d5a603
JK
15767 case DW_ACCESS_private:
15768 fnp->is_private = 1;
15769 break;
15770 case DW_ACCESS_protected:
15771 fnp->is_protected = 1;
15772 break;
c906108c
SS
15773 }
15774
b02dede2 15775 /* Check for artificial methods. */
e142c38c 15776 attr = dwarf2_attr (die, DW_AT_artificial, cu);
c45bc3f8 15777 if (attr && attr->as_boolean ())
b02dede2
DJ
15778 fnp->is_artificial = 1;
15779
e35000a7
TBA
15780 /* Check for defaulted methods. */
15781 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
e8e5c158
TT
15782 if (attr != nullptr)
15783 fnp->defaulted = attr->defaulted ();
e35000a7
TBA
15784
15785 /* Check for deleted methods. */
15786 attr = dwarf2_attr (die, DW_AT_deleted, cu);
c45bc3f8 15787 if (attr != nullptr && attr->as_boolean ())
e35000a7
TBA
15788 fnp->is_deleted = 1;
15789
7d27a96d
TT
15790 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15791
0d564a31 15792 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15793 function. For older versions of GCC, this is an offset in the
15794 appropriate virtual table, as specified by DW_AT_containing_type.
15795 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15796 to the object address. */
15797
e142c38c 15798 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15799 if (attr != nullptr)
8e19ed76 15800 {
9d2246fc 15801 if (attr->form_is_block () && attr->as_block ()->size > 0)
dda83cd7 15802 {
9d2246fc
TT
15803 struct dwarf_block *block = attr->as_block ();
15804
15805 if (block->data[0] == DW_OP_constu)
aec5aa8b
TT
15806 {
15807 /* Old-style GCC. */
9d2246fc 15808 fnp->voffset = decode_locdesc (block, cu) + 2;
aec5aa8b 15809 }
9d2246fc
TT
15810 else if (block->data[0] == DW_OP_deref
15811 || (block->size > 1
15812 && block->data[0] == DW_OP_deref_size
15813 && block->data[1] == cu->header.addr_size))
aec5aa8b 15814 {
9d2246fc 15815 fnp->voffset = decode_locdesc (block, cu);
aec5aa8b
TT
15816 if ((fnp->voffset % cu->header.addr_size) != 0)
15817 dwarf2_complex_location_expr_complaint ();
15818 else
15819 fnp->voffset /= cu->header.addr_size;
15820 fnp->voffset += 2;
15821 }
15822 else
15823 dwarf2_complex_location_expr_complaint ();
15824
15825 if (!fnp->fcontext)
7e993ebf
KS
15826 {
15827 /* If there is no `this' field and no DW_AT_containing_type,
15828 we cannot actually find a base class context for the
15829 vtable! */
1f704f76 15830 if (this_type->num_fields () == 0
7e993ebf
KS
15831 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15832 {
b98664d3 15833 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15834 "function \"%s\" (offset %s)"),
15835 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15836 }
15837 else
15838 {
15839 fnp->fcontext
940da03e 15840 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15841 }
15842 }
aec5aa8b 15843 }
cd6c91b4 15844 else if (attr->form_is_section_offset ())
dda83cd7 15845 {
4d3c2250 15846 dwarf2_complex_location_expr_complaint ();
dda83cd7 15847 }
8e19ed76 15848 else
dda83cd7 15849 {
4d3c2250
KB
15850 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15851 fieldname);
dda83cd7 15852 }
0d564a31 15853 }
d48cc9dd
DJ
15854 else
15855 {
15856 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
23dca5c3 15857 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
d48cc9dd
DJ
15858 {
15859 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15860 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15861 "but the vtable offset is not specified"),
9d8780f0 15862 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15863 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15864 TYPE_CPLUS_DYNAMIC (type) = 1;
15865 }
15866 }
c906108c
SS
15867}
15868
15869/* Create the vector of member function fields, and attach it to the type. */
15870
15871static void
fba45db2 15872dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15873 struct dwarf2_cu *cu)
c906108c 15874{
b4ba55a1 15875 if (cu->language == language_ada)
a73c6dcd 15876 error (_("unexpected member functions in Ada type"));
b4ba55a1 15877
c906108c
SS
15878 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15879 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15880 TYPE_ALLOC (type,
15881 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15882
be2daae6 15883 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15884 {
be2daae6 15885 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15886 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15887
be2daae6
TT
15888 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15889 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15890 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15891 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15892
15893 for (int k = 0; k < nf.fnfields.size (); ++k)
15894 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15895 }
15896
be2daae6 15897 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15898}
15899
1168df01
JB
15900/* Returns non-zero if NAME is the name of a vtable member in CU's
15901 language, zero otherwise. */
15902static int
15903is_vtable_name (const char *name, struct dwarf2_cu *cu)
15904{
15905 static const char vptr[] = "_vptr";
15906
9c37b5ae
TT
15907 /* Look for the C++ form of the vtable. */
15908 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15909 return 1;
15910
15911 return 0;
15912}
15913
c0dd20ea 15914/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15915 functions, with the ABI-specified layout. If TYPE describes
15916 such a structure, smash it into a member function type.
61049d3b
DJ
15917
15918 GCC shouldn't do this; it should just output pointer to member DIEs.
15919 This is GCC PR debug/28767. */
c0dd20ea 15920
0b92b5bb
TT
15921static void
15922quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15923{
09e2d7c7 15924 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15925
15926 /* Check for a structure with no name and two children. */
1f704f76 15927 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15928 return;
c0dd20ea
DJ
15929
15930 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15931 if (TYPE_FIELD_NAME (type, 0) == NULL
15932 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15933 || TYPE_FIELD_NAME (type, 1) == NULL
15934 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15935 return;
c0dd20ea
DJ
15936
15937 /* Find the type of the method. */
940da03e 15938 pfn_type = type->field (0).type ();
c0dd20ea 15939 if (pfn_type == NULL
78134374
SM
15940 || pfn_type->code () != TYPE_CODE_PTR
15941 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15942 return;
c0dd20ea
DJ
15943
15944 /* Look for the "this" argument. */
15945 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15946 if (pfn_type->num_fields () == 0
940da03e
SM
15947 /* || pfn_type->field (0).type () == NULL */
15948 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15949 return;
c0dd20ea 15950
940da03e 15951 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15952 new_type = alloc_type (objfile);
09e2d7c7 15953 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15954 pfn_type->fields (), pfn_type->num_fields (),
a409645d 15955 pfn_type->has_varargs ());
0b92b5bb 15956 smash_to_methodptr_type (type, new_type);
c0dd20ea 15957}
1168df01 15958
e26624c6
TT
15959/* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15960 requires rewriting, then copy it and return the updated copy.
15961 Otherwise return nullptr. */
15962
15963static struct type *
15964rewrite_array_type (struct type *type)
15965{
15966 if (type->code () != TYPE_CODE_ARRAY)
15967 return nullptr;
15968
15969 struct type *index_type = type->index_type ();
15970 range_bounds *current_bounds = index_type->bounds ();
15971
15972 /* Handle multi-dimensional arrays. */
15973 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15974 if (new_target == nullptr)
15975 {
15976 /* Maybe we don't need to rewrite this array. */
15977 if (current_bounds->low.kind () == PROP_CONST
15978 && current_bounds->high.kind () == PROP_CONST)
15979 return nullptr;
15980 }
15981
15982 /* Either the target type was rewritten, or the bounds have to be
15983 updated. Either way we want to copy the type and update
15984 everything. */
15985 struct type *copy = copy_type (type);
15986 int nfields = copy->num_fields ();
15987 field *new_fields
15988 = ((struct field *) TYPE_ZALLOC (copy,
15989 nfields * sizeof (struct field)));
15990 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15991 copy->set_fields (new_fields);
15992 if (new_target != nullptr)
15993 TYPE_TARGET_TYPE (copy) = new_target;
15994
15995 struct type *index_copy = copy_type (index_type);
15996 range_bounds *bounds
15997 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15998 sizeof (range_bounds));
15999 *bounds = *current_bounds;
16000 bounds->low.set_const_val (1);
16001 bounds->high.set_const_val (0);
16002 index_copy->set_bounds (bounds);
16003 copy->set_index_type (index_copy);
16004
16005 return copy;
16006}
16007
57567375
TT
16008/* While some versions of GCC will generate complicated DWARF for an
16009 array (see quirk_ada_thick_pointer), more recent versions were
16010 modified to emit an explicit thick pointer structure. However, in
16011 this case, the array still has DWARF expressions for its ranges,
16012 and these must be ignored. */
16013
16014static void
16015quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
16016 struct type *type)
16017{
16018 gdb_assert (cu->language == language_ada);
16019
16020 /* Check for a structure with two children. */
16021 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
16022 return;
16023
16024 /* Check for P_ARRAY and P_BOUNDS members. */
16025 if (TYPE_FIELD_NAME (type, 0) == NULL
16026 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
16027 || TYPE_FIELD_NAME (type, 1) == NULL
16028 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
16029 return;
16030
16031 /* Make sure we're looking at a pointer to an array. */
16032 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
16033 return;
57567375 16034
e26624c6
TT
16035 /* The Ada code already knows how to handle these types, so all that
16036 we need to do is turn the bounds into static bounds. However, we
16037 don't want to rewrite existing array or index types in-place,
16038 because those may be referenced in other contexts where this
16039 rewriting is undesirable. */
16040 struct type *new_ary_type
16041 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
16042 if (new_ary_type != nullptr)
16043 type->field (0).set_type (lookup_pointer_type (new_ary_type));
57567375
TT
16044}
16045
2b4424c3
TT
16046/* If the DIE has a DW_AT_alignment attribute, return its value, doing
16047 appropriate error checking and issuing complaints if there is a
16048 problem. */
16049
16050static ULONGEST
16051get_alignment (struct dwarf2_cu *cu, struct die_info *die)
16052{
16053 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
16054
16055 if (attr == nullptr)
16056 return 0;
16057
cd6c91b4 16058 if (!attr->form_is_constant ())
2b4424c3 16059 {
b98664d3 16060 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
16061 " - DIE at %s [in module %s]"),
16062 sect_offset_str (die->sect_off),
5e22e966 16063 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
16064 return 0;
16065 }
16066
529908cb
TT
16067 LONGEST val = attr->constant_value (0);
16068 if (val < 0)
2b4424c3 16069 {
529908cb
TT
16070 complaint (_("DW_AT_alignment value must not be negative"
16071 " - DIE at %s [in module %s]"),
16072 sect_offset_str (die->sect_off),
16073 objfile_name (cu->per_objfile->objfile));
16074 return 0;
2b4424c3 16075 }
529908cb 16076 ULONGEST align = val;
2b4424c3
TT
16077
16078 if (align == 0)
16079 {
b98664d3 16080 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
16081 " - DIE at %s [in module %s]"),
16082 sect_offset_str (die->sect_off),
5e22e966 16083 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
16084 return 0;
16085 }
16086 if ((align & (align - 1)) != 0)
16087 {
b98664d3 16088 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
16089 " - DIE at %s [in module %s]"),
16090 sect_offset_str (die->sect_off),
5e22e966 16091 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
16092 return 0;
16093 }
16094
16095 return align;
16096}
16097
16098/* If the DIE has a DW_AT_alignment attribute, use its value to set
16099 the alignment for TYPE. */
16100
16101static void
16102maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
16103 struct type *type)
16104{
16105 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 16106 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
16107 " - DIE at %s [in module %s]"),
16108 sect_offset_str (die->sect_off),
5e22e966 16109 objfile_name (cu->per_objfile->objfile));
2b4424c3 16110}
685b1105 16111
e35000a7
TBA
16112/* Check if the given VALUE is a valid enum dwarf_calling_convention
16113 constant for a type, according to DWARF5 spec, Table 5.5. */
16114
16115static bool
16116is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
16117{
16118 switch (value)
16119 {
16120 case DW_CC_normal:
16121 case DW_CC_pass_by_reference:
16122 case DW_CC_pass_by_value:
16123 return true;
16124
16125 default:
16126 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 16127 "(%s) for a type"), pulongest (value));
e35000a7
TBA
16128 return false;
16129 }
16130}
16131
d0922fcf
TBA
16132/* Check if the given VALUE is a valid enum dwarf_calling_convention
16133 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
16134 also according to GNU-specific values (see include/dwarf2.h). */
16135
16136static bool
16137is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
16138{
16139 switch (value)
16140 {
16141 case DW_CC_normal:
16142 case DW_CC_program:
16143 case DW_CC_nocall:
16144 return true;
16145
16146 case DW_CC_GNU_renesas_sh:
16147 case DW_CC_GNU_borland_fastcall_i386:
16148 case DW_CC_GDB_IBM_OpenCL:
16149 return true;
16150
16151 default:
16152 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 16153 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
16154 return false;
16155 }
16156}
16157
c906108c 16158/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
16159 (definition) to create a type for the structure or union. Fill in
16160 the type's name and general properties; the members will not be
83655187
DE
16161 processed until process_structure_scope. A symbol table entry for
16162 the type will also not be done until process_structure_scope (assuming
16163 the type has a name).
c906108c 16164
c767944b
DJ
16165 NOTE: we need to call these functions regardless of whether or not the
16166 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 16167 structure or union. This gets the type entered into our set of
83655187 16168 user defined types. */
c906108c 16169
f792889a 16170static struct type *
134d01f1 16171read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16172{
5e22e966 16173 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
16174 struct type *type;
16175 struct attribute *attr;
15d034d0 16176 const char *name;
c906108c 16177
348e048f
DE
16178 /* If the definition of this type lives in .debug_types, read that type.
16179 Don't follow DW_AT_specification though, that will take us back up
16180 the chain and we want to go down. */
052c8bb8 16181 attr = die->attr (DW_AT_signature);
435d3d88 16182 if (attr != nullptr)
348e048f 16183 {
ac9ec31b 16184 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16185
ac9ec31b 16186 /* The type's CU may not be the same as CU.
02142a6c 16187 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16188 return set_die_type (die, type, cu);
16189 }
16190
c0dd20ea 16191 type = alloc_type (objfile);
c906108c 16192 INIT_CPLUS_SPECIFIC (type);
93311388 16193
39cbfefa
DJ
16194 name = dwarf2_name (die, cu);
16195 if (name != NULL)
c906108c 16196 {
987504bb 16197 if (cu->language == language_cplus
c44af4eb
TT
16198 || cu->language == language_d
16199 || cu->language == language_rust)
63d06c5c 16200 {
15d034d0 16201 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
16202
16203 /* dwarf2_full_name might have already finished building the DIE's
16204 type. If so, there is no need to continue. */
16205 if (get_die_type (die, cu) != NULL)
16206 return get_die_type (die, cu);
16207
d0e39ea2 16208 type->set_name (full_name);
63d06c5c
DC
16209 }
16210 else
16211 {
d8151005
DJ
16212 /* The name is already allocated along with this objfile, so
16213 we don't need to duplicate it for the type. */
d0e39ea2 16214 type->set_name (name);
63d06c5c 16215 }
c906108c
SS
16216 }
16217
16218 if (die->tag == DW_TAG_structure_type)
16219 {
67607e24 16220 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16221 }
16222 else if (die->tag == DW_TAG_union_type)
16223 {
67607e24 16224 type->set_code (TYPE_CODE_UNION);
c906108c
SS
16225 }
16226 else
16227 {
67607e24 16228 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16229 }
16230
0cc2414c
TT
16231 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16232 TYPE_DECLARED_CLASS (type) = 1;
16233
e35000a7
TBA
16234 /* Store the calling convention in the type if it's available in
16235 the die. Otherwise the calling convention remains set to
16236 the default value DW_CC_normal. */
16237 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16238 if (attr != nullptr
529908cb 16239 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
e35000a7
TBA
16240 {
16241 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16242 TYPE_CPLUS_CALLING_CONVENTION (type)
529908cb 16243 = (enum dwarf_calling_convention) (attr->constant_value (0));
e35000a7
TBA
16244 }
16245
e142c38c 16246 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16247 if (attr != nullptr)
c906108c 16248 {
cd6c91b4 16249 if (attr->form_is_constant ())
dda83cd7 16250 TYPE_LENGTH (type) = attr->constant_value (0);
155bfbd3
JB
16251 else
16252 {
f8e89861 16253 struct dynamic_prop prop;
293e7e51 16254 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 16255 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
dda83cd7 16256 TYPE_LENGTH (type) = 0;
155bfbd3 16257 }
c906108c
SS
16258 }
16259 else
16260 {
16261 TYPE_LENGTH (type) = 0;
16262 }
16263
2b4424c3
TT
16264 maybe_set_alignment (cu, die, type);
16265
5230b05a 16266 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 16267 {
5230b05a
WT
16268 /* ICC<14 does not output the required DW_AT_declaration on
16269 incomplete types, but gives them a size of zero. */
b4b73759 16270 type->set_is_stub (true);
685b1105
JK
16271 }
16272 else
9baccff6 16273 type->set_stub_is_supported (true);
685b1105 16274
dc718098 16275 if (die_is_declaration (die, cu))
b4b73759 16276 type->set_is_stub (true);
a6c727b2
DJ
16277 else if (attr == NULL && die->child == NULL
16278 && producer_is_realview (cu->producer))
16279 /* RealView does not output the required DW_AT_declaration
16280 on incomplete types. */
b4b73759 16281 type->set_is_stub (true);
dc718098 16282
c906108c
SS
16283 /* We need to add the type field to the die immediately so we don't
16284 infinitely recurse when dealing with pointers to the structure
0963b4bd 16285 type within the structure itself. */
1c379e20 16286 set_die_type (die, type, cu);
c906108c 16287
7e314c57
JK
16288 /* set_die_type should be already done. */
16289 set_descriptive_type (type, die, cu);
16290
c767944b
DJ
16291 return type;
16292}
16293
9c6a1327
TT
16294static void handle_struct_member_die
16295 (struct die_info *child_die,
16296 struct type *type,
16297 struct field_info *fi,
16298 std::vector<struct symbol *> *template_args,
16299 struct dwarf2_cu *cu);
16300
16301/* A helper for handle_struct_member_die that handles
16302 DW_TAG_variant_part. */
16303
16304static void
16305handle_variant_part (struct die_info *die, struct type *type,
16306 struct field_info *fi,
16307 std::vector<struct symbol *> *template_args,
16308 struct dwarf2_cu *cu)
16309{
16310 variant_part_builder *new_part;
16311 if (fi->current_variant_part == nullptr)
16312 {
16313 fi->variant_parts.emplace_back ();
16314 new_part = &fi->variant_parts.back ();
16315 }
16316 else if (!fi->current_variant_part->processing_variant)
16317 {
16318 complaint (_("nested DW_TAG_variant_part seen "
16319 "- DIE at %s [in module %s]"),
16320 sect_offset_str (die->sect_off),
5e22e966 16321 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16322 return;
16323 }
16324 else
16325 {
16326 variant_field &current = fi->current_variant_part->variants.back ();
16327 current.variant_parts.emplace_back ();
16328 new_part = &current.variant_parts.back ();
16329 }
16330
16331 /* When we recurse, we want callees to add to this new variant
16332 part. */
16333 scoped_restore save_current_variant_part
16334 = make_scoped_restore (&fi->current_variant_part, new_part);
16335
16336 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16337 if (discr == NULL)
16338 {
16339 /* It's a univariant form, an extension we support. */
16340 }
16341 else if (discr->form_is_ref ())
16342 {
16343 struct dwarf2_cu *target_cu = cu;
16344 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16345
16346 new_part->discriminant_offset = target_die->sect_off;
16347 }
16348 else
16349 {
16350 complaint (_("DW_AT_discr does not have DIE reference form"
16351 " - DIE at %s [in module %s]"),
16352 sect_offset_str (die->sect_off),
5e22e966 16353 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16354 }
16355
16356 for (die_info *child_die = die->child;
16357 child_die != NULL;
16358 child_die = child_die->sibling)
16359 handle_struct_member_die (child_die, type, fi, template_args, cu);
16360}
16361
16362/* A helper for handle_struct_member_die that handles
16363 DW_TAG_variant. */
16364
16365static void
16366handle_variant (struct die_info *die, struct type *type,
16367 struct field_info *fi,
16368 std::vector<struct symbol *> *template_args,
16369 struct dwarf2_cu *cu)
16370{
16371 if (fi->current_variant_part == nullptr)
16372 {
16373 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16374 "- DIE at %s [in module %s]"),
16375 sect_offset_str (die->sect_off),
5e22e966 16376 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16377 return;
16378 }
16379 if (fi->current_variant_part->processing_variant)
16380 {
16381 complaint (_("nested DW_TAG_variant seen "
16382 "- DIE at %s [in module %s]"),
16383 sect_offset_str (die->sect_off),
5e22e966 16384 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16385 return;
16386 }
16387
16388 scoped_restore save_processing_variant
16389 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16390 true);
16391
16392 fi->current_variant_part->variants.emplace_back ();
16393 variant_field &variant = fi->current_variant_part->variants.back ();
16394 variant.first_field = fi->fields.size ();
16395
16396 /* In a variant we want to get the discriminant and also add a
16397 field for our sole member child. */
16398 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
cae21f8e 16399 if (discr == nullptr || !discr->form_is_constant ())
9c6a1327
TT
16400 {
16401 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
9d2246fc 16402 if (discr == nullptr || discr->as_block ()->size == 0)
9c6a1327
TT
16403 variant.default_branch = true;
16404 else
9d2246fc 16405 variant.discr_list_data = discr->as_block ();
9c6a1327
TT
16406 }
16407 else
cae21f8e 16408 variant.discriminant_value = discr->constant_value (0);
9c6a1327
TT
16409
16410 for (die_info *variant_child = die->child;
16411 variant_child != NULL;
16412 variant_child = variant_child->sibling)
16413 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16414
16415 variant.last_field = fi->fields.size ();
16416}
16417
2ddeaf8a
TT
16418/* A helper for process_structure_scope that handles a single member
16419 DIE. */
16420
16421static void
16422handle_struct_member_die (struct die_info *child_die, struct type *type,
16423 struct field_info *fi,
16424 std::vector<struct symbol *> *template_args,
16425 struct dwarf2_cu *cu)
16426{
16427 if (child_die->tag == DW_TAG_member
9c6a1327 16428 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
16429 {
16430 /* NOTE: carlton/2002-11-05: A C++ static data member
16431 should be a DW_TAG_member that is a declaration, but
16432 all versions of G++ as of this writing (so through at
16433 least 3.2.1) incorrectly generate DW_TAG_variable
16434 tags for them instead. */
16435 dwarf2_add_field (fi, child_die, cu);
16436 }
16437 else if (child_die->tag == DW_TAG_subprogram)
16438 {
16439 /* Rust doesn't have member functions in the C++ sense.
16440 However, it does emit ordinary functions as children
16441 of a struct DIE. */
16442 if (cu->language == language_rust)
16443 read_func_scope (child_die, cu);
16444 else
16445 {
16446 /* C++ member function. */
16447 dwarf2_add_member_fn (fi, child_die, type, cu);
16448 }
16449 }
16450 else if (child_die->tag == DW_TAG_inheritance)
16451 {
16452 /* C++ base class field. */
16453 dwarf2_add_field (fi, child_die, cu);
16454 }
16455 else if (type_can_define_types (child_die))
16456 dwarf2_add_type_defn (fi, child_die, cu);
16457 else if (child_die->tag == DW_TAG_template_type_param
16458 || child_die->tag == DW_TAG_template_value_param)
16459 {
16460 struct symbol *arg = new_symbol (child_die, NULL, cu);
16461
16462 if (arg != NULL)
16463 template_args->push_back (arg);
16464 }
9c6a1327
TT
16465 else if (child_die->tag == DW_TAG_variant_part)
16466 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 16467 else if (child_die->tag == DW_TAG_variant)
9c6a1327 16468 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
16469}
16470
c767944b
DJ
16471/* Finish creating a structure or union type, including filling in
16472 its members and creating a symbol for it. */
16473
16474static void
16475process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16476{
5e22e966 16477 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 16478 struct die_info *child_die;
c767944b
DJ
16479 struct type *type;
16480
16481 type = get_die_type (die, cu);
16482 if (type == NULL)
16483 type = read_structure_type (die, cu);
16484
3e1d3d8c 16485 bool has_template_parameters = false;
e142c38c 16486 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16487 {
16488 struct field_info fi;
2f4732b0 16489 std::vector<struct symbol *> template_args;
c906108c 16490
639d11d3 16491 child_die = die->child;
c906108c
SS
16492
16493 while (child_die && child_die->tag)
16494 {
2ddeaf8a 16495 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 16496 child_die = child_die->sibling;
c906108c
SS
16497 }
16498
34eaf542 16499 /* Attach template arguments to type. */
2f4732b0 16500 if (!template_args.empty ())
34eaf542 16501 {
3e1d3d8c 16502 has_template_parameters = true;
34eaf542 16503 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16504 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16505 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16506 = XOBNEWVEC (&objfile->objfile_obstack,
16507 struct symbol *,
16508 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16509 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16510 template_args.data (),
34eaf542
TT
16511 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16512 * sizeof (struct symbol *)));
34eaf542
TT
16513 }
16514
c906108c 16515 /* Attach fields and member functions to the type. */
317f7127 16516 if (fi.nfields () > 0)
e7c27a73 16517 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16518 if (!fi.fnfieldlists.empty ())
c906108c 16519 {
e7c27a73 16520 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16521
c5aa993b 16522 /* Get the type which refers to the base class (possibly this
c906108c 16523 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16524 class from the DW_AT_containing_type attribute. This use of
16525 DW_AT_containing_type is a GNU extension. */
c906108c 16526
e142c38c 16527 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16528 {
e7c27a73 16529 struct type *t = die_containing_type (die, cu);
c906108c 16530
ae6ae975 16531 set_type_vptr_basetype (type, t);
c906108c
SS
16532 if (type == t)
16533 {
c906108c
SS
16534 int i;
16535
16536 /* Our own class provides vtbl ptr. */
1f704f76 16537 for (i = t->num_fields () - 1;
c906108c
SS
16538 i >= TYPE_N_BASECLASSES (t);
16539 --i)
16540 {
0d5cff50 16541 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16542
dda83cd7 16543 if (is_vtable_name (fieldname, cu))
c906108c 16544 {
ae6ae975 16545 set_type_vptr_fieldno (type, i);
c906108c
SS
16546 break;
16547 }
16548 }
16549
16550 /* Complain if virtual function table field not found. */
16551 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16552 complaint (_("virtual function table pointer "
3e43a32a 16553 "not found when defining class '%s'"),
7d93a1e0 16554 type->name () ? type->name () : "");
c906108c
SS
16555 }
16556 else
16557 {
ae6ae975 16558 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16559 }
16560 }
f6235d4c 16561 else if (cu->producer
61012eef 16562 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16563 {
16564 /* The IBM XLC compiler does not provide direct indication
dda83cd7
SM
16565 of the containing type, but the vtable pointer is
16566 always named __vfp. */
f6235d4c
EZ
16567
16568 int i;
16569
1f704f76 16570 for (i = type->num_fields () - 1;
f6235d4c
EZ
16571 i >= TYPE_N_BASECLASSES (type);
16572 --i)
16573 {
16574 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16575 {
ae6ae975
DE
16576 set_type_vptr_fieldno (type, i);
16577 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16578 break;
16579 }
16580 }
16581 }
c906108c 16582 }
98751a41
JK
16583
16584 /* Copy fi.typedef_field_list linked list elements content into the
16585 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16586 if (!fi.typedef_field_list.empty ())
98751a41 16587 {
be2daae6 16588 int count = fi.typedef_field_list.size ();
98751a41 16589
a0d7a4ff 16590 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16591 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16592 = ((struct decl_field *)
be2daae6
TT
16593 TYPE_ALLOC (type,
16594 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16595 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16596
be2daae6
TT
16597 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16598 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16599 }
c767944b 16600
883fd55a
KS
16601 /* Copy fi.nested_types_list linked list elements content into the
16602 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16603 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16604 {
be2daae6 16605 int count = fi.nested_types_list.size ();
883fd55a
KS
16606
16607 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16608 TYPE_NESTED_TYPES_ARRAY (type)
16609 = ((struct decl_field *)
be2daae6
TT
16610 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16611 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16612
be2daae6
TT
16613 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16614 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16615 }
c906108c 16616 }
63d06c5c 16617
bb5ed363 16618 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16619 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16620 cu->rust_unions.push_back (type);
57567375
TT
16621 else if (cu->language == language_ada)
16622 quirk_ada_thick_pointer_struct (die, cu, type);
0b92b5bb 16623
90aeadfc
DC
16624 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16625 snapshots) has been known to create a die giving a declaration
16626 for a class that has, as a child, a die giving a definition for a
16627 nested class. So we have to process our children even if the
16628 current die is a declaration. Normally, of course, a declaration
16629 won't have any children at all. */
134d01f1 16630
ca040673
DE
16631 child_die = die->child;
16632
90aeadfc
DC
16633 while (child_die != NULL && child_die->tag)
16634 {
16635 if (child_die->tag == DW_TAG_member
16636 || child_die->tag == DW_TAG_variable
34eaf542
TT
16637 || child_die->tag == DW_TAG_inheritance
16638 || child_die->tag == DW_TAG_template_value_param
16639 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16640 {
90aeadfc 16641 /* Do nothing. */
134d01f1 16642 }
90aeadfc
DC
16643 else
16644 process_die (child_die, cu);
134d01f1 16645
436c571c 16646 child_die = child_die->sibling;
134d01f1
DJ
16647 }
16648
fa4028e9
JB
16649 /* Do not consider external references. According to the DWARF standard,
16650 these DIEs are identified by the fact that they have no byte_size
16651 attribute, and a declaration attribute. */
16652 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
16653 || !die_is_declaration (die, cu)
16654 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
16655 {
16656 struct symbol *sym = new_symbol (die, type, cu);
16657
16658 if (has_template_parameters)
16659 {
a776957c
TT
16660 struct symtab *symtab;
16661 if (sym != nullptr)
16662 symtab = symbol_symtab (sym);
16663 else if (cu->line_header != nullptr)
16664 {
16665 /* Any related symtab will do. */
16666 symtab
7ba99d21 16667 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16668 }
16669 else
16670 {
16671 symtab = nullptr;
16672 complaint (_("could not find suitable "
16673 "symtab for template parameter"
16674 " - DIE at %s [in module %s]"),
16675 sect_offset_str (die->sect_off),
16676 objfile_name (objfile));
16677 }
16678
16679 if (symtab != nullptr)
16680 {
16681 /* Make sure that the symtab is set on the new symbols.
16682 Even though they don't appear in this symtab directly,
16683 other parts of gdb assume that symbols do, and this is
16684 reasonably true. */
16685 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16686 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16687 }
3e1d3d8c
TT
16688 }
16689 }
134d01f1
DJ
16690}
16691
ed6acedd
TT
16692/* Assuming DIE is an enumeration type, and TYPE is its associated
16693 type, update TYPE using some information only available in DIE's
16694 children. In particular, the fields are computed. */
55426c9d
JB
16695
16696static void
16697update_enumeration_type_from_children (struct die_info *die,
16698 struct type *type,
16699 struct dwarf2_cu *cu)
16700{
60f7655a 16701 struct die_info *child_die;
55426c9d
JB
16702 int unsigned_enum = 1;
16703 int flag_enum = 1;
55426c9d 16704
8268c778 16705 auto_obstack obstack;
ed6acedd 16706 std::vector<struct field> fields;
55426c9d 16707
60f7655a
DE
16708 for (child_die = die->child;
16709 child_die != NULL && child_die->tag;
436c571c 16710 child_die = child_die->sibling)
55426c9d
JB
16711 {
16712 struct attribute *attr;
16713 LONGEST value;
16714 const gdb_byte *bytes;
16715 struct dwarf2_locexpr_baton *baton;
16716 const char *name;
60f7655a 16717
55426c9d
JB
16718 if (child_die->tag != DW_TAG_enumerator)
16719 continue;
16720
16721 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16722 if (attr == NULL)
16723 continue;
16724
16725 name = dwarf2_name (child_die, cu);
16726 if (name == NULL)
16727 name = "<anonymous enumerator>";
16728
16729 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16730 &value, &bytes, &baton);
16731 if (value < 0)
16732 {
16733 unsigned_enum = 0;
16734 flag_enum = 0;
16735 }
55426c9d 16736 else
edd45eb0
SM
16737 {
16738 if (count_one_bits_ll (value) >= 2)
16739 flag_enum = 0;
edd45eb0 16740 }
55426c9d 16741
ed6acedd
TT
16742 fields.emplace_back ();
16743 struct field &field = fields.back ();
16744 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16745 SET_FIELD_ENUMVAL (field, value);
16746 }
16747
16748 if (!fields.empty ())
16749 {
5e33d5f4 16750 type->set_num_fields (fields.size ());
3cabb6b0
SM
16751 type->set_fields
16752 ((struct field *)
16753 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16754 memcpy (type->fields (), fields.data (),
ed6acedd 16755 sizeof (struct field) * fields.size ());
55426c9d
JB
16756 }
16757
16758 if (unsigned_enum)
653223d3
SM
16759 type->set_is_unsigned (true);
16760
55426c9d
JB
16761 if (flag_enum)
16762 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16763}
16764
134d01f1
DJ
16765/* Given a DW_AT_enumeration_type die, set its type. We do not
16766 complete the type's fields yet, or create any symbols. */
c906108c 16767
f792889a 16768static struct type *
134d01f1 16769read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16770{
5e22e966 16771 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16772 struct type *type;
c906108c 16773 struct attribute *attr;
0114d602 16774 const char *name;
134d01f1 16775
348e048f
DE
16776 /* If the definition of this type lives in .debug_types, read that type.
16777 Don't follow DW_AT_specification though, that will take us back up
16778 the chain and we want to go down. */
052c8bb8 16779 attr = die->attr (DW_AT_signature);
435d3d88 16780 if (attr != nullptr)
348e048f 16781 {
ac9ec31b 16782 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16783
ac9ec31b 16784 /* The type's CU may not be the same as CU.
02142a6c 16785 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16786 return set_die_type (die, type, cu);
16787 }
16788
c906108c
SS
16789 type = alloc_type (objfile);
16790
67607e24 16791 type->set_code (TYPE_CODE_ENUM);
94af9270 16792 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16793 if (name != NULL)
d0e39ea2 16794 type->set_name (name);
c906108c 16795
0626fc76
TT
16796 attr = dwarf2_attr (die, DW_AT_type, cu);
16797 if (attr != NULL)
16798 {
16799 struct type *underlying_type = die_type (die, cu);
16800
16801 TYPE_TARGET_TYPE (type) = underlying_type;
16802 }
16803
e142c38c 16804 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16805 if (attr != nullptr)
c906108c 16806 {
529908cb 16807 TYPE_LENGTH (type) = attr->constant_value (0);
c906108c
SS
16808 }
16809 else
16810 {
16811 TYPE_LENGTH (type) = 0;
16812 }
16813
2b4424c3
TT
16814 maybe_set_alignment (cu, die, type);
16815
137033e9
JB
16816 /* The enumeration DIE can be incomplete. In Ada, any type can be
16817 declared as private in the package spec, and then defined only
16818 inside the package body. Such types are known as Taft Amendment
16819 Types. When another package uses such a type, an incomplete DIE
16820 may be generated by the compiler. */
02eb380e 16821 if (die_is_declaration (die, cu))
b4b73759 16822 type->set_is_stub (true);
02eb380e 16823
0626fc76
TT
16824 /* If this type has an underlying type that is not a stub, then we
16825 may use its attributes. We always use the "unsigned" attribute
16826 in this situation, because ordinarily we guess whether the type
16827 is unsigned -- but the guess can be wrong and the underlying type
16828 can tell us the reality. However, we defer to a local size
16829 attribute if one exists, because this lets the compiler override
16830 the underlying type if needed. */
e46d3488 16831 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
0626fc76 16832 {
9e7c9a03
HD
16833 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16834 underlying_type = check_typedef (underlying_type);
653223d3
SM
16835
16836 type->set_is_unsigned (underlying_type->is_unsigned ());
16837
0626fc76 16838 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16839 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
653223d3 16840
2b4424c3 16841 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16842 && TYPE_RAW_ALIGN (underlying_type) != 0)
16843 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16844 }
16845
3d567982
TT
16846 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16847
ed6acedd
TT
16848 set_die_type (die, type, cu);
16849
16850 /* Finish the creation of this type by using the enum's children.
16851 Note that, as usual, this must come after set_die_type to avoid
16852 infinite recursion when trying to compute the names of the
16853 enumerators. */
16854 update_enumeration_type_from_children (die, type, cu);
16855
16856 return type;
134d01f1
DJ
16857}
16858
16859/* Given a pointer to a die which begins an enumeration, process all
16860 the dies that define the members of the enumeration, and create the
16861 symbol for the enumeration type.
16862
16863 NOTE: We reverse the order of the element list. */
16864
16865static void
16866process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16867{
f792889a 16868 struct type *this_type;
134d01f1 16869
f792889a
DJ
16870 this_type = get_die_type (die, cu);
16871 if (this_type == NULL)
16872 this_type = read_enumeration_type (die, cu);
9dc481d3 16873
639d11d3 16874 if (die->child != NULL)
c906108c 16875 {
9dc481d3 16876 struct die_info *child_die;
15d034d0 16877 const char *name;
9dc481d3 16878
639d11d3 16879 child_die = die->child;
c906108c
SS
16880 while (child_die && child_die->tag)
16881 {
16882 if (child_die->tag != DW_TAG_enumerator)
16883 {
e7c27a73 16884 process_die (child_die, cu);
c906108c
SS
16885 }
16886 else
16887 {
39cbfefa
DJ
16888 name = dwarf2_name (child_die, cu);
16889 if (name)
ed6acedd 16890 new_symbol (child_die, this_type, cu);
c906108c
SS
16891 }
16892
436c571c 16893 child_die = child_die->sibling;
c906108c 16894 }
c906108c 16895 }
134d01f1 16896
6c83ed52
TT
16897 /* If we are reading an enum from a .debug_types unit, and the enum
16898 is a declaration, and the enum is not the signatured type in the
16899 unit, then we do not want to add a symbol for it. Adding a
16900 symbol would in some cases obscure the true definition of the
16901 enum, giving users an incomplete type when the definition is
16902 actually available. Note that we do not want to do this for all
16903 enums which are just declarations, because C++0x allows forward
16904 enum declarations. */
3019eac3 16905 if (cu->per_cu->is_debug_types
6c83ed52
TT
16906 && die_is_declaration (die, cu))
16907 {
52dc124a 16908 struct signatured_type *sig_type;
6c83ed52 16909
c0f78cd4 16910 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16911 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16912 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16913 return;
16914 }
16915
f792889a 16916 new_symbol (die, this_type, cu);
c906108c
SS
16917}
16918
57567375
TT
16919/* Helper function for quirk_ada_thick_pointer that examines a bounds
16920 expression for an index type and finds the corresponding field
16921 offset in the hidden "P_BOUNDS" structure. Returns true on success
16922 and updates *FIELD, false if it fails to recognize an
16923 expression. */
16924
16925static bool
16926recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16927 int *bounds_offset, struct field *field,
16928 struct dwarf2_cu *cu)
16929{
16930 struct attribute *attr = dwarf2_attr (die, name, cu);
16931 if (attr == nullptr || !attr->form_is_block ())
16932 return false;
16933
16934 const struct dwarf_block *block = attr->as_block ();
16935 const gdb_byte *start = block->data;
16936 const gdb_byte *end = block->data + block->size;
16937
16938 /* The expression to recognize generally looks like:
16939
16940 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16941 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16942
16943 However, the second "plus_uconst" may be missing:
16944
16945 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16946 DW_OP_deref_size: 4)
16947
16948 This happens when the field is at the start of the structure.
16949
16950 Also, the final deref may not be sized:
16951
16952 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16953 DW_OP_deref)
16954
16955 This happens when the size of the index type happens to be the
16956 same as the architecture's word size. This can occur with or
16957 without the second plus_uconst. */
16958
16959 if (end - start < 2)
16960 return false;
16961 if (*start++ != DW_OP_push_object_address)
16962 return false;
16963 if (*start++ != DW_OP_plus_uconst)
16964 return false;
16965
16966 uint64_t this_bound_off;
16967 start = gdb_read_uleb128 (start, end, &this_bound_off);
16968 if (start == nullptr || (int) this_bound_off != this_bound_off)
16969 return false;
16970 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16971 is consistent among all bounds. */
16972 if (*bounds_offset == -1)
16973 *bounds_offset = this_bound_off;
16974 else if (*bounds_offset != this_bound_off)
16975 return false;
16976
16977 if (start == end || *start++ != DW_OP_deref)
16978 return false;
16979
16980 int offset = 0;
16981 if (start ==end)
16982 return false;
16983 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16984 {
16985 /* This means an offset of 0. */
16986 }
16987 else if (*start++ != DW_OP_plus_uconst)
16988 return false;
16989 else
16990 {
16991 /* The size is the parameter to DW_OP_plus_uconst. */
16992 uint64_t val;
16993 start = gdb_read_uleb128 (start, end, &val);
16994 if (start == nullptr)
16995 return false;
16996 if ((int) val != val)
16997 return false;
16998 offset = val;
16999 }
17000
17001 if (start == end)
17002 return false;
17003
17004 uint64_t size;
17005 if (*start == DW_OP_deref_size)
17006 {
17007 start = gdb_read_uleb128 (start + 1, end, &size);
17008 if (start == nullptr)
17009 return false;
17010 }
17011 else if (*start == DW_OP_deref)
17012 {
17013 size = cu->header.addr_size;
17014 ++start;
17015 }
17016 else
17017 return false;
17018
17019 SET_FIELD_BITPOS (*field, 8 * offset);
17020 if (size != TYPE_LENGTH (field->type ()))
17021 FIELD_BITSIZE (*field) = 8 * size;
17022
17023 return true;
17024}
17025
17026/* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
17027 some kinds of Ada arrays:
17028
17029 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
17030 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
17031 <11e0> DW_AT_data_location: 2 byte block: 97 6
17032 (DW_OP_push_object_address; DW_OP_deref)
17033 <11e3> DW_AT_type : <0x1173>
17034 <11e7> DW_AT_sibling : <0x1201>
17035 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
17036 <11ec> DW_AT_type : <0x1206>
17037 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
17038 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
17039 DW_OP_deref_size: 4)
17040 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
17041 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
17042 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
17043
17044 This actually represents a "thick pointer", which is a structure
17045 with two elements: one that is a pointer to the array data, and one
17046 that is a pointer to another structure; this second structure holds
17047 the array bounds.
17048
17049 This returns a new type on success, or nullptr if this didn't
17050 recognize the type. */
17051
17052static struct type *
17053quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
17054 struct type *type)
17055{
17056 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
17057 /* So far we've only seen this with block form. */
17058 if (attr == nullptr || !attr->form_is_block ())
17059 return nullptr;
17060
17061 /* Note that this will fail if the structure layout is changed by
17062 the compiler. However, we have no good way to recognize some
17063 other layout, because we don't know what expression the compiler
17064 might choose to emit should this happen. */
17065 struct dwarf_block *blk = attr->as_block ();
17066 if (blk->size != 2
17067 || blk->data[0] != DW_OP_push_object_address
17068 || blk->data[1] != DW_OP_deref)
17069 return nullptr;
17070
17071 int bounds_offset = -1;
17072 int max_align = -1;
17073 std::vector<struct field> range_fields;
17074 for (struct die_info *child_die = die->child;
17075 child_die;
17076 child_die = child_die->sibling)
17077 {
17078 if (child_die->tag == DW_TAG_subrange_type)
17079 {
17080 struct type *underlying = read_subrange_index_type (child_die, cu);
17081
17082 int this_align = type_align (underlying);
17083 if (this_align > max_align)
17084 max_align = this_align;
17085
17086 range_fields.emplace_back ();
17087 range_fields.emplace_back ();
17088
17089 struct field &lower = range_fields[range_fields.size () - 2];
17090 struct field &upper = range_fields[range_fields.size () - 1];
17091
17092 lower.set_type (underlying);
17093 FIELD_ARTIFICIAL (lower) = 1;
17094
17095 upper.set_type (underlying);
17096 FIELD_ARTIFICIAL (upper) = 1;
17097
17098 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
17099 &bounds_offset, &lower, cu)
17100 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
17101 &bounds_offset, &upper, cu))
17102 return nullptr;
17103 }
17104 }
17105
17106 /* This shouldn't really happen, but double-check that we found
17107 where the bounds are stored. */
17108 if (bounds_offset == -1)
17109 return nullptr;
17110
17111 struct objfile *objfile = cu->per_objfile->objfile;
17112 for (int i = 0; i < range_fields.size (); i += 2)
17113 {
17114 char name[20];
17115
17116 /* Set the name of each field in the bounds. */
17117 xsnprintf (name, sizeof (name), "LB%d", i / 2);
17118 FIELD_NAME (range_fields[i]) = objfile->intern (name);
17119 xsnprintf (name, sizeof (name), "UB%d", i / 2);
17120 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
17121 }
17122
17123 struct type *bounds = alloc_type (objfile);
17124 bounds->set_code (TYPE_CODE_STRUCT);
17125
17126 bounds->set_num_fields (range_fields.size ());
17127 bounds->set_fields
17128 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
17129 * sizeof (struct field))));
17130 memcpy (bounds->fields (), range_fields.data (),
17131 bounds->num_fields () * sizeof (struct field));
17132
17133 int last_fieldno = range_fields.size () - 1;
17134 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
17135 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
17136 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
17137
17138 /* Rewrite the existing array type in place. Specifically, we
17139 remove any dynamic properties we might have read, and we replace
17140 the index types. */
17141 struct type *iter = type;
17142 for (int i = 0; i < range_fields.size (); i += 2)
17143 {
17144 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
17145 iter->main_type->dyn_prop_list = nullptr;
17146 iter->set_index_type
17147 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
17148 iter = TYPE_TARGET_TYPE (iter);
17149 }
17150
17151 struct type *result = alloc_type (objfile);
17152 result->set_code (TYPE_CODE_STRUCT);
17153
17154 result->set_num_fields (2);
17155 result->set_fields
17156 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
17157 * sizeof (struct field))));
17158
17159 /* The names are chosen to coincide with what the compiler does with
17160 -fgnat-encodings=all, which the Ada code in gdb already
17161 understands. */
17162 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
17163 result->field (0).set_type (lookup_pointer_type (type));
17164
17165 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
17166 result->field (1).set_type (lookup_pointer_type (bounds));
17167 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
17168
17169 result->set_name (type->name ());
17170 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17171 + TYPE_LENGTH (result->field (1).type ()));
17172
17173 return result;
17174}
17175
c906108c
SS
17176/* Extract all information from a DW_TAG_array_type DIE and put it in
17177 the DIE's type field. For now, this only handles one dimensional
17178 arrays. */
17179
f792889a 17180static struct type *
e7c27a73 17181read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17182{
5e22e966 17183 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 17184 struct die_info *child_die;
7e314c57 17185 struct type *type;
c906108c 17186 struct type *element_type, *range_type, *index_type;
c906108c 17187 struct attribute *attr;
15d034d0 17188 const char *name;
a405673c 17189 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 17190 unsigned int bit_stride = 0;
c906108c 17191
e7c27a73 17192 element_type = die_type (die, cu);
c906108c 17193
7e314c57
JK
17194 /* The die_type call above may have already set the type for this DIE. */
17195 type = get_die_type (die, cu);
17196 if (type)
17197 return type;
17198
dc53a7ad
JB
17199 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17200 if (attr != NULL)
a405673c
JB
17201 {
17202 int stride_ok;
293e7e51 17203 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
17204
17205 byte_stride_prop
17206 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
17207 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17208 prop_type);
a405673c
JB
17209 if (!stride_ok)
17210 {
b98664d3 17211 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
17212 " - DIE at %s [in module %s]"),
17213 sect_offset_str (die->sect_off),
5e22e966 17214 objfile_name (cu->per_objfile->objfile));
a405673c
JB
17215 /* Ignore this attribute. We will likely not be able to print
17216 arrays of this type correctly, but there is little we can do
17217 to help if we cannot read the attribute's value. */
17218 byte_stride_prop = NULL;
17219 }
17220 }
dc53a7ad
JB
17221
17222 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17223 if (attr != NULL)
529908cb 17224 bit_stride = attr->constant_value (0);
dc53a7ad 17225
c906108c
SS
17226 /* Irix 6.2 native cc creates array types without children for
17227 arrays with unspecified length. */
639d11d3 17228 if (die->child == NULL)
c906108c 17229 {
46bf5051 17230 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17231 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 17232 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 17233 byte_stride_prop, bit_stride);
f792889a 17234 return set_die_type (die, type, cu);
c906108c
SS
17235 }
17236
791afaa2 17237 std::vector<struct type *> range_types;
639d11d3 17238 child_die = die->child;
c906108c
SS
17239 while (child_die && child_die->tag)
17240 {
17241 if (child_die->tag == DW_TAG_subrange_type)
17242 {
f792889a 17243 struct type *child_type = read_type_die (child_die, cu);
9a619af0 17244
dda83cd7
SM
17245 if (child_type != NULL)
17246 {
0963b4bd 17247 /* The range type was succesfully read. Save it for the
dda83cd7 17248 array type creation. */
791afaa2 17249 range_types.push_back (child_type);
dda83cd7 17250 }
c906108c 17251 }
436c571c 17252 child_die = child_die->sibling;
c906108c
SS
17253 }
17254
cf2b2075
TV
17255 if (range_types.empty ())
17256 {
17257 complaint (_("unable to find array range - DIE at %s [in module %s]"),
17258 sect_offset_str (die->sect_off),
17259 objfile_name (cu->per_objfile->objfile));
17260 return NULL;
17261 }
17262
c906108c
SS
17263 /* Dwarf2 dimensions are output from left to right, create the
17264 necessary array types in backwards order. */
7ca2d3a3 17265
c906108c 17266 type = element_type;
7ca2d3a3
DL
17267
17268 if (read_array_order (die, cu) == DW_ORD_col_major)
17269 {
17270 int i = 0;
9a619af0 17271
791afaa2 17272 while (i < range_types.size ())
10f6a3ad
TT
17273 {
17274 type = create_array_type_with_stride (NULL, type, range_types[i++],
17275 byte_stride_prop, bit_stride);
17276 bit_stride = 0;
17277 byte_stride_prop = nullptr;
17278 }
7ca2d3a3
DL
17279 }
17280 else
17281 {
791afaa2 17282 size_t ndim = range_types.size ();
7ca2d3a3 17283 while (ndim-- > 0)
10f6a3ad
TT
17284 {
17285 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17286 byte_stride_prop, bit_stride);
17287 bit_stride = 0;
17288 byte_stride_prop = nullptr;
17289 }
7ca2d3a3 17290 }
c906108c 17291
cf2b2075
TV
17292 gdb_assert (type != element_type);
17293
f5f8a009
EZ
17294 /* Understand Dwarf2 support for vector types (like they occur on
17295 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17296 array type. This is not part of the Dwarf2/3 standard yet, but a
17297 custom vendor extension. The main difference between a regular
17298 array and the vector variant is that vectors are passed by value
17299 to functions. */
e142c38c 17300 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 17301 if (attr != nullptr)
ea37ba09 17302 make_vector_type (type);
f5f8a009 17303
dbc98a8b
KW
17304 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17305 implementation may choose to implement triple vectors using this
17306 attribute. */
17307 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb 17308 if (attr != nullptr && attr->form_is_unsigned ())
dbc98a8b 17309 {
529908cb
TT
17310 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17311 TYPE_LENGTH (type) = attr->as_unsigned ();
dbc98a8b 17312 else
b98664d3 17313 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 17314 "than the total size of elements"));
dbc98a8b
KW
17315 }
17316
39cbfefa
DJ
17317 name = dwarf2_name (die, cu);
17318 if (name)
d0e39ea2 17319 type->set_name (name);
6e70227d 17320
2b4424c3
TT
17321 maybe_set_alignment (cu, die, type);
17322
57567375
TT
17323 struct type *replacement_type = nullptr;
17324 if (cu->language == language_ada)
17325 {
17326 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17327 if (replacement_type != nullptr)
17328 type = replacement_type;
17329 }
17330
0963b4bd 17331 /* Install the type in the die. */
57567375 17332 set_die_type (die, type, cu, replacement_type != nullptr);
7e314c57
JK
17333
17334 /* set_die_type should be already done. */
b4ba55a1
JB
17335 set_descriptive_type (type, die, cu);
17336
7e314c57 17337 return type;
c906108c
SS
17338}
17339
7ca2d3a3 17340static enum dwarf_array_dim_ordering
6e70227d 17341read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
17342{
17343 struct attribute *attr;
17344
17345 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17346
435d3d88 17347 if (attr != nullptr)
1bc397c5
TT
17348 {
17349 LONGEST val = attr->constant_value (-1);
17350 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17351 return (enum dwarf_array_dim_ordering) val;
17352 }
7ca2d3a3 17353
0963b4bd
MS
17354 /* GNU F77 is a special case, as at 08/2004 array type info is the
17355 opposite order to the dwarf2 specification, but data is still
17356 laid out as per normal fortran.
7ca2d3a3 17357
0963b4bd
MS
17358 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17359 version checking. */
7ca2d3a3 17360
905e0470
PM
17361 if (cu->language == language_fortran
17362 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
17363 {
17364 return DW_ORD_row_major;
17365 }
17366
3a3440fb 17367 switch (cu->language_defn->array_ordering ())
7ca2d3a3
DL
17368 {
17369 case array_column_major:
17370 return DW_ORD_col_major;
17371 case array_row_major:
17372 default:
17373 return DW_ORD_row_major;
17374 };
17375}
17376
72019c9c 17377/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 17378 the DIE's type field. */
72019c9c 17379
f792889a 17380static struct type *
72019c9c
GM
17381read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17382{
7e314c57
JK
17383 struct type *domain_type, *set_type;
17384 struct attribute *attr;
f792889a 17385
7e314c57
JK
17386 domain_type = die_type (die, cu);
17387
17388 /* The die_type call above may have already set the type for this DIE. */
17389 set_type = get_die_type (die, cu);
17390 if (set_type)
17391 return set_type;
17392
17393 set_type = create_set_type (NULL, domain_type);
17394
17395 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb
TT
17396 if (attr != nullptr && attr->form_is_unsigned ())
17397 TYPE_LENGTH (set_type) = attr->as_unsigned ();
7e314c57 17398
2b4424c3
TT
17399 maybe_set_alignment (cu, die, set_type);
17400
f792889a 17401 return set_die_type (die, set_type, cu);
72019c9c 17402}
7ca2d3a3 17403
0971de02
TT
17404/* A helper for read_common_block that creates a locexpr baton.
17405 SYM is the symbol which we are marking as computed.
17406 COMMON_DIE is the DIE for the common block.
17407 COMMON_LOC is the location expression attribute for the common
17408 block itself.
17409 MEMBER_LOC is the location expression attribute for the particular
17410 member of the common block that we are processing.
17411 CU is the CU from which the above come. */
17412
17413static void
17414mark_common_block_symbol_computed (struct symbol *sym,
17415 struct die_info *common_die,
17416 struct attribute *common_loc,
17417 struct attribute *member_loc,
17418 struct dwarf2_cu *cu)
17419{
5e22e966 17420 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 17421 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
17422 struct dwarf2_locexpr_baton *baton;
17423 gdb_byte *ptr;
17424 unsigned int cu_off;
08feed99 17425 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
17426 LONGEST offset = 0;
17427
17428 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
17429 gdb_assert (common_loc->form_is_block ());
17430 gdb_assert (member_loc->form_is_block ()
cd6c91b4 17431 || member_loc->form_is_constant ());
0971de02 17432
8d749320 17433 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 17434 baton->per_objfile = per_objfile;
0971de02
TT
17435 baton->per_cu = cu->per_cu;
17436 gdb_assert (baton->per_cu);
17437
17438 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17439
cd6c91b4 17440 if (member_loc->form_is_constant ())
0971de02 17441 {
0826b30a 17442 offset = member_loc->constant_value (0);
0971de02
TT
17443 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17444 }
17445 else
9d2246fc 17446 baton->size += member_loc->as_block ()->size;
0971de02 17447
224c3ddb 17448 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
17449 baton->data = ptr;
17450
17451 *ptr++ = DW_OP_call4;
9c541725 17452 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
17453 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17454 ptr += 4;
17455
cd6c91b4 17456 if (member_loc->form_is_constant ())
0971de02
TT
17457 {
17458 *ptr++ = DW_OP_addr;
17459 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17460 ptr += cu->header.addr_size;
17461 }
17462 else
17463 {
17464 /* We have to copy the data here, because DW_OP_call4 will only
17465 use a DW_AT_location attribute. */
9d2246fc
TT
17466 struct dwarf_block *block = member_loc->as_block ();
17467 memcpy (ptr, block->data, block->size);
17468 ptr += block->size;
0971de02
TT
17469 }
17470
17471 *ptr++ = DW_OP_plus;
17472 gdb_assert (ptr - baton->data == baton->size);
17473
0971de02 17474 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 17475 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
17476}
17477
4357ac6c
TT
17478/* Create appropriate locally-scoped variables for all the
17479 DW_TAG_common_block entries. Also create a struct common_block
17480 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 17481 is used to separate the common blocks name namespace from regular
4357ac6c 17482 variable names. */
c906108c
SS
17483
17484static void
e7c27a73 17485read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17486{
0971de02
TT
17487 struct attribute *attr;
17488
17489 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 17490 if (attr != nullptr)
0971de02
TT
17491 {
17492 /* Support the .debug_loc offsets. */
4fc6c0d5 17493 if (attr->form_is_block ())
dda83cd7 17494 {
0971de02 17495 /* Ok. */
dda83cd7 17496 }
cd6c91b4 17497 else if (attr->form_is_section_offset ())
dda83cd7 17498 {
0971de02
TT
17499 dwarf2_complex_location_expr_complaint ();
17500 attr = NULL;
dda83cd7 17501 }
0971de02 17502 else
dda83cd7 17503 {
0971de02
TT
17504 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17505 "common block member");
17506 attr = NULL;
dda83cd7 17507 }
0971de02
TT
17508 }
17509
639d11d3 17510 if (die->child != NULL)
c906108c 17511 {
5e22e966 17512 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
17513 struct die_info *child_die;
17514 size_t n_entries = 0, size;
17515 struct common_block *common_block;
17516 struct symbol *sym;
74ac6d43 17517
4357ac6c
TT
17518 for (child_die = die->child;
17519 child_die && child_die->tag;
436c571c 17520 child_die = child_die->sibling)
4357ac6c
TT
17521 ++n_entries;
17522
17523 size = (sizeof (struct common_block)
17524 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
17525 common_block
17526 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17527 size);
4357ac6c
TT
17528 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17529 common_block->n_entries = 0;
17530
17531 for (child_die = die->child;
17532 child_die && child_die->tag;
436c571c 17533 child_die = child_die->sibling)
4357ac6c
TT
17534 {
17535 /* Create the symbol in the DW_TAG_common_block block in the current
17536 symbol scope. */
e7c27a73 17537 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
17538 if (sym != NULL)
17539 {
17540 struct attribute *member_loc;
17541
17542 common_block->contents[common_block->n_entries++] = sym;
17543
17544 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17545 cu);
17546 if (member_loc)
17547 {
17548 /* GDB has handled this for a long time, but it is
17549 not specified by DWARF. It seems to have been
17550 emitted by gfortran at least as recently as:
17551 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 17552 complaint (_("Variable in common block has "
0971de02 17553 "DW_AT_data_member_location "
9d8780f0
SM
17554 "- DIE at %s [in module %s]"),
17555 sect_offset_str (child_die->sect_off),
518817b3 17556 objfile_name (objfile));
0971de02 17557
cd6c91b4 17558 if (member_loc->form_is_section_offset ())
0971de02 17559 dwarf2_complex_location_expr_complaint ();
cd6c91b4 17560 else if (member_loc->form_is_constant ()
4fc6c0d5 17561 || member_loc->form_is_block ())
0971de02 17562 {
435d3d88 17563 if (attr != nullptr)
0971de02
TT
17564 mark_common_block_symbol_computed (sym, die, attr,
17565 member_loc, cu);
17566 }
17567 else
17568 dwarf2_complex_location_expr_complaint ();
17569 }
17570 }
c906108c 17571 }
4357ac6c
TT
17572
17573 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17574 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
17575 }
17576}
17577
0114d602 17578/* Create a type for a C++ namespace. */
d9fa45fe 17579
0114d602
DJ
17580static struct type *
17581read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 17582{
5e22e966 17583 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17584 const char *previous_prefix, *name;
9219021c 17585 int is_anonymous;
0114d602
DJ
17586 struct type *type;
17587
17588 /* For extensions, reuse the type of the original namespace. */
17589 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17590 {
17591 struct die_info *ext_die;
17592 struct dwarf2_cu *ext_cu = cu;
9a619af0 17593
0114d602
DJ
17594 ext_die = dwarf2_extension (die, &ext_cu);
17595 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
17596
17597 /* EXT_CU may not be the same as CU.
02142a6c 17598 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
17599 return set_die_type (die, type, cu);
17600 }
9219021c 17601
e142c38c 17602 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
17603
17604 /* Now build the name of the current namespace. */
17605
0114d602
DJ
17606 previous_prefix = determine_prefix (die, cu);
17607 if (previous_prefix[0] != '\0')
17608 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 17609 previous_prefix, name, 0, cu);
0114d602
DJ
17610
17611 /* Create the type. */
19f392bc 17612 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 17613
60531b24 17614 return set_die_type (die, type, cu);
0114d602
DJ
17615}
17616
22cee43f 17617/* Read a namespace scope. */
0114d602
DJ
17618
17619static void
17620read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17621{
5e22e966 17622 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17623 int is_anonymous;
9219021c 17624
5c4e30ca
DC
17625 /* Add a symbol associated to this if we haven't seen the namespace
17626 before. Also, add a using directive if it's an anonymous
17627 namespace. */
9219021c 17628
f2f0e013 17629 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17630 {
17631 struct type *type;
17632
0114d602 17633 type = read_type_die (die, cu);
e7c27a73 17634 new_symbol (die, type, cu);
5c4e30ca 17635
e8e80198 17636 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17637 if (is_anonymous)
0114d602
DJ
17638 {
17639 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17640
eb1e02fd 17641 std::vector<const char *> excludes;
804d2729 17642 add_using_directive (using_directives (cu),
7d93a1e0 17643 previous_prefix, type->name (), NULL,
eb1e02fd 17644 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17645 }
5c4e30ca 17646 }
9219021c 17647
639d11d3 17648 if (die->child != NULL)
d9fa45fe 17649 {
639d11d3 17650 struct die_info *child_die = die->child;
6e70227d 17651
d9fa45fe
DC
17652 while (child_die && child_die->tag)
17653 {
e7c27a73 17654 process_die (child_die, cu);
436c571c 17655 child_die = child_die->sibling;
d9fa45fe
DC
17656 }
17657 }
38d518c9
EZ
17658}
17659
f55ee35c
JK
17660/* Read a Fortran module as type. This DIE can be only a declaration used for
17661 imported module. Still we need that type as local Fortran "use ... only"
17662 declaration imports depend on the created type in determine_prefix. */
17663
17664static struct type *
17665read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17666{
5e22e966 17667 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 17668 const char *module_name;
f55ee35c
JK
17669 struct type *type;
17670
17671 module_name = dwarf2_name (die, cu);
19f392bc 17672 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17673
f55ee35c
JK
17674 return set_die_type (die, type, cu);
17675}
17676
5d7cb8df
JK
17677/* Read a Fortran module. */
17678
17679static void
17680read_module (struct die_info *die, struct dwarf2_cu *cu)
17681{
17682 struct die_info *child_die = die->child;
530e8392
KB
17683 struct type *type;
17684
17685 type = read_type_die (die, cu);
17686 new_symbol (die, type, cu);
5d7cb8df 17687
5d7cb8df
JK
17688 while (child_die && child_die->tag)
17689 {
17690 process_die (child_die, cu);
436c571c 17691 child_die = child_die->sibling;
5d7cb8df
JK
17692 }
17693}
17694
38d518c9
EZ
17695/* Return the name of the namespace represented by DIE. Set
17696 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17697 namespace. */
17698
17699static const char *
e142c38c 17700namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17701{
17702 struct die_info *current_die;
17703 const char *name = NULL;
17704
17705 /* Loop through the extensions until we find a name. */
17706
17707 for (current_die = die;
17708 current_die != NULL;
f2f0e013 17709 current_die = dwarf2_extension (die, &cu))
38d518c9 17710 {
96553a0c
DE
17711 /* We don't use dwarf2_name here so that we can detect the absence
17712 of a name -> anonymous namespace. */
7d45c7c3 17713 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17714
38d518c9
EZ
17715 if (name != NULL)
17716 break;
17717 }
17718
17719 /* Is it an anonymous namespace? */
17720
17721 *is_anonymous = (name == NULL);
17722 if (*is_anonymous)
2b1dbab0 17723 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17724
17725 return name;
d9fa45fe
DC
17726}
17727
c906108c
SS
17728/* Extract all information from a DW_TAG_pointer_type DIE and add to
17729 the user defined type vector. */
17730
f792889a 17731static struct type *
e7c27a73 17732read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17733{
5e22e966 17734 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 17735 struct comp_unit_head *cu_header = &cu->header;
c906108c 17736 struct type *type;
8b2dbe47
KB
17737 struct attribute *attr_byte_size;
17738 struct attribute *attr_address_class;
17739 int byte_size, addr_class;
7e314c57
JK
17740 struct type *target_type;
17741
17742 target_type = die_type (die, cu);
c906108c 17743
7e314c57
JK
17744 /* The die_type call above may have already set the type for this DIE. */
17745 type = get_die_type (die, cu);
17746 if (type)
17747 return type;
17748
17749 type = lookup_pointer_type (target_type);
8b2dbe47 17750
e142c38c 17751 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47 17752 if (attr_byte_size)
529908cb 17753 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
c906108c 17754 else
8b2dbe47
KB
17755 byte_size = cu_header->addr_size;
17756
e142c38c 17757 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47 17758 if (attr_address_class)
529908cb 17759 addr_class = attr_address_class->constant_value (DW_ADDR_none);
8b2dbe47
KB
17760 else
17761 addr_class = DW_ADDR_none;
17762
2b4424c3
TT
17763 ULONGEST alignment = get_alignment (cu, die);
17764
17765 /* If the pointer size, alignment, or address class is different
17766 than the default, create a type variant marked as such and set
17767 the length accordingly. */
17768 if (TYPE_LENGTH (type) != byte_size
17769 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17770 && alignment != TYPE_RAW_ALIGN (type))
17771 || addr_class != DW_ADDR_none)
c906108c 17772 {
5e2b427d 17773 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47 17774 {
314ad88d
PA
17775 type_instance_flags type_flags
17776 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17777 addr_class);
876cecd0
TT
17778 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17779 == 0);
8b2dbe47
KB
17780 type = make_type_with_address_space (type, type_flags);
17781 }
17782 else if (TYPE_LENGTH (type) != byte_size)
17783 {
b98664d3 17784 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17785 }
2b4424c3
TT
17786 else if (TYPE_RAW_ALIGN (type) != alignment)
17787 {
b98664d3 17788 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17789 " - DIE at %s [in module %s]"),
17790 sect_offset_str (die->sect_off),
5e22e966 17791 objfile_name (cu->per_objfile->objfile));
2b4424c3 17792 }
6e70227d 17793 else
9a619af0
MS
17794 {
17795 /* Should we also complain about unhandled address classes? */
17796 }
c906108c 17797 }
8b2dbe47
KB
17798
17799 TYPE_LENGTH (type) = byte_size;
2b4424c3 17800 set_type_align (type, alignment);
f792889a 17801 return set_die_type (die, type, cu);
c906108c
SS
17802}
17803
17804/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17805 the user defined type vector. */
17806
f792889a 17807static struct type *
e7c27a73 17808read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17809{
17810 struct type *type;
17811 struct type *to_type;
17812 struct type *domain;
17813
e7c27a73
DJ
17814 to_type = die_type (die, cu);
17815 domain = die_containing_type (die, cu);
0d5de010 17816
7e314c57
JK
17817 /* The calls above may have already set the type for this DIE. */
17818 type = get_die_type (die, cu);
17819 if (type)
17820 return type;
17821
78134374 17822 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 17823 type = lookup_methodptr_type (to_type);
78134374 17824 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 17825 {
5e22e966 17826 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
17827
17828 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 17829 to_type->fields (), to_type->num_fields (),
a409645d 17830 to_type->has_varargs ());
7078baeb
TT
17831 type = lookup_methodptr_type (new_type);
17832 }
0d5de010
DJ
17833 else
17834 type = lookup_memberptr_type (to_type, domain);
c906108c 17835
f792889a 17836 return set_die_type (die, type, cu);
c906108c
SS
17837}
17838
4297a3f0 17839/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17840 the user defined type vector. */
17841
f792889a 17842static struct type *
4297a3f0 17843read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
dda83cd7 17844 enum type_code refcode)
c906108c 17845{
e7c27a73 17846 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17847 struct type *type, *target_type;
c906108c
SS
17848 struct attribute *attr;
17849
4297a3f0
AV
17850 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17851
7e314c57
JK
17852 target_type = die_type (die, cu);
17853
17854 /* The die_type call above may have already set the type for this DIE. */
17855 type = get_die_type (die, cu);
17856 if (type)
17857 return type;
17858
4297a3f0 17859 type = lookup_reference_type (target_type, refcode);
e142c38c 17860 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17861 if (attr != nullptr)
c906108c 17862 {
529908cb 17863 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
c906108c
SS
17864 }
17865 else
17866 {
107d2387 17867 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17868 }
2b4424c3 17869 maybe_set_alignment (cu, die, type);
f792889a 17870 return set_die_type (die, type, cu);
c906108c
SS
17871}
17872
cf363f18
MW
17873/* Add the given cv-qualifiers to the element type of the array. GCC
17874 outputs DWARF type qualifiers that apply to an array, not the
17875 element type. But GDB relies on the array element type to carry
17876 the cv-qualifiers. This mimics section 6.7.3 of the C99
17877 specification. */
17878
17879static struct type *
17880add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17881 struct type *base_type, int cnst, int voltl)
17882{
17883 struct type *el_type, *inner_array;
17884
17885 base_type = copy_type (base_type);
17886 inner_array = base_type;
17887
78134374 17888 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17889 {
17890 TYPE_TARGET_TYPE (inner_array) =
17891 copy_type (TYPE_TARGET_TYPE (inner_array));
17892 inner_array = TYPE_TARGET_TYPE (inner_array);
17893 }
17894
17895 el_type = TYPE_TARGET_TYPE (inner_array);
17896 cnst |= TYPE_CONST (el_type);
17897 voltl |= TYPE_VOLATILE (el_type);
17898 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17899
17900 return set_die_type (die, base_type, cu);
17901}
17902
f792889a 17903static struct type *
e7c27a73 17904read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17905{
f792889a 17906 struct type *base_type, *cv_type;
c906108c 17907
e7c27a73 17908 base_type = die_type (die, cu);
7e314c57
JK
17909
17910 /* The die_type call above may have already set the type for this DIE. */
17911 cv_type = get_die_type (die, cu);
17912 if (cv_type)
17913 return cv_type;
17914
2f608a3a
KW
17915 /* In case the const qualifier is applied to an array type, the element type
17916 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 17917 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 17918 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17919
f792889a
DJ
17920 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17921 return set_die_type (die, cv_type, cu);
c906108c
SS
17922}
17923
f792889a 17924static struct type *
e7c27a73 17925read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17926{
f792889a 17927 struct type *base_type, *cv_type;
c906108c 17928
e7c27a73 17929 base_type = die_type (die, cu);
7e314c57
JK
17930
17931 /* The die_type call above may have already set the type for this DIE. */
17932 cv_type = get_die_type (die, cu);
17933 if (cv_type)
17934 return cv_type;
17935
cf363f18
MW
17936 /* In case the volatile qualifier is applied to an array type, the
17937 element type is so qualified, not the array type (section 6.7.3
17938 of C99). */
78134374 17939 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17940 return add_array_cv_type (die, cu, base_type, 0, 1);
17941
f792889a
DJ
17942 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17943 return set_die_type (die, cv_type, cu);
c906108c
SS
17944}
17945
06d66ee9
TT
17946/* Handle DW_TAG_restrict_type. */
17947
17948static struct type *
17949read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17950{
17951 struct type *base_type, *cv_type;
17952
17953 base_type = die_type (die, cu);
17954
17955 /* The die_type call above may have already set the type for this DIE. */
17956 cv_type = get_die_type (die, cu);
17957 if (cv_type)
17958 return cv_type;
17959
17960 cv_type = make_restrict_type (base_type);
17961 return set_die_type (die, cv_type, cu);
17962}
17963
a2c2acaf
MW
17964/* Handle DW_TAG_atomic_type. */
17965
17966static struct type *
17967read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17968{
17969 struct type *base_type, *cv_type;
17970
17971 base_type = die_type (die, cu);
17972
17973 /* The die_type call above may have already set the type for this DIE. */
17974 cv_type = get_die_type (die, cu);
17975 if (cv_type)
17976 return cv_type;
17977
17978 cv_type = make_atomic_type (base_type);
17979 return set_die_type (die, cv_type, cu);
17980}
17981
c906108c
SS
17982/* Extract all information from a DW_TAG_string_type DIE and add to
17983 the user defined type vector. It isn't really a user defined type,
17984 but it behaves like one, with other DIE's using an AT_user_def_type
17985 attribute to reference it. */
17986
f792889a 17987static struct type *
e7c27a73 17988read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17989{
5e22e966 17990 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17991 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17992 struct type *type, *range_type, *index_type, *char_type;
17993 struct attribute *attr;
216a7e6b
AB
17994 struct dynamic_prop prop;
17995 bool length_is_constant = true;
17996 LONGEST length;
17997
17998 /* There are a couple of places where bit sizes might be made use of
17999 when parsing a DW_TAG_string_type, however, no producer that we know
18000 of make use of these. Handling bit sizes that are a multiple of the
18001 byte size is easy enough, but what about other bit sizes? Lets deal
18002 with that problem when we have to. Warn about these attributes being
18003 unsupported, then parse the type and ignore them like we always
18004 have. */
18005 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
18006 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
18007 {
18008 static bool warning_printed = false;
18009 if (!warning_printed)
18010 {
18011 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
18012 "currently supported on DW_TAG_string_type."));
18013 warning_printed = true;
18014 }
18015 }
c906108c 18016
e142c38c 18017 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 18018 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
18019 {
18020 /* The string length describes the location at which the length of
18021 the string can be found. The size of the length field can be
18022 specified with one of the attributes below. */
18023 struct type *prop_type;
18024 struct attribute *len
18025 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
18026 if (len == nullptr)
18027 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 18028 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
18029 {
18030 /* Pass 0 as the default as we know this attribute is constant
18031 and the default value will not be returned. */
0826b30a 18032 LONGEST sz = len->constant_value (0);
293e7e51 18033 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
18034 }
18035 else
18036 {
18037 /* If the size is not specified then we assume it is the size of
18038 an address on this target. */
293e7e51 18039 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
18040 }
18041
18042 /* Convert the attribute into a dynamic property. */
18043 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
18044 length = 1;
18045 else
18046 length_is_constant = false;
18047 }
18048 else if (attr != nullptr)
18049 {
18050 /* This DW_AT_string_length just contains the length with no
18051 indirection. There's no need to create a dynamic property in this
18052 case. Pass 0 for the default value as we know it will not be
18053 returned in this case. */
0826b30a 18054 length = attr->constant_value (0);
216a7e6b
AB
18055 }
18056 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 18057 {
216a7e6b 18058 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 18059 length = attr->constant_value (1);
c906108c
SS
18060 }
18061 else
18062 {
216a7e6b
AB
18063 /* Use 1 as a fallback length if we have nothing else. */
18064 length = 1;
c906108c 18065 }
6ccb9162 18066
46bf5051 18067 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
18068 if (length_is_constant)
18069 range_type = create_static_range_type (NULL, index_type, 1, length);
18070 else
18071 {
18072 struct dynamic_prop low_bound;
18073
8c2e4e06 18074 low_bound.set_const_val (1);
216a7e6b
AB
18075 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
18076 }
3b7538c0
UW
18077 char_type = language_string_char_type (cu->language_defn, gdbarch);
18078 type = create_string_type (NULL, char_type, range_type);
6ccb9162 18079
f792889a 18080 return set_die_type (die, type, cu);
c906108c
SS
18081}
18082
4d804846
JB
18083/* Assuming that DIE corresponds to a function, returns nonzero
18084 if the function is prototyped. */
18085
18086static int
18087prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
18088{
18089 struct attribute *attr;
18090
18091 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c45bc3f8 18092 if (attr && attr->as_boolean ())
4d804846
JB
18093 return 1;
18094
18095 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 18096 is only meaningful for C, but the concept also extends to other
4d804846
JB
18097 languages that allow unprototyped functions (Eg: Objective C).
18098 For all other languages, assume that functions are always
18099 prototyped. */
18100 if (cu->language != language_c
18101 && cu->language != language_objc
18102 && cu->language != language_opencl)
18103 return 1;
18104
18105 /* RealView does not emit DW_AT_prototyped. We can not distinguish
18106 prototyped and unprototyped functions; default to prototyped,
18107 since that is more common in modern code (and RealView warns
18108 about unprototyped functions). */
18109 if (producer_is_realview (cu->producer))
18110 return 1;
18111
18112 return 0;
18113}
18114
c906108c
SS
18115/* Handle DIES due to C code like:
18116
18117 struct foo
c5aa993b
JM
18118 {
18119 int (*funcp)(int a, long l);
18120 int b;
18121 };
c906108c 18122
0963b4bd 18123 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 18124
f792889a 18125static struct type *
e7c27a73 18126read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18127{
5e22e966 18128 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
18129 struct type *type; /* Type that this function returns. */
18130 struct type *ftype; /* Function that returns above type. */
c906108c
SS
18131 struct attribute *attr;
18132
e7c27a73 18133 type = die_type (die, cu);
7e314c57
JK
18134
18135 /* The die_type call above may have already set the type for this DIE. */
18136 ftype = get_die_type (die, cu);
18137 if (ftype)
18138 return ftype;
18139
0c8b41f1 18140 ftype = lookup_function_type (type);
c906108c 18141
4d804846 18142 if (prototyped_function_p (die, cu))
27e69b7a 18143 ftype->set_is_prototyped (true);
c906108c 18144
c055b101
CV
18145 /* Store the calling convention in the type if it's available in
18146 the subroutine die. Otherwise set the calling convention to
18147 the default value DW_CC_normal. */
18148 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf 18149 if (attr != nullptr
529908cb 18150 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
d0922fcf 18151 TYPE_CALLING_CONVENTION (ftype)
529908cb 18152 = (enum dwarf_calling_convention) attr->constant_value (0);
54fcddd0
UW
18153 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
18154 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
18155 else
18156 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 18157
743649fd
MW
18158 /* Record whether the function returns normally to its caller or not
18159 if the DWARF producer set that information. */
18160 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
c45bc3f8 18161 if (attr && attr->as_boolean ())
743649fd
MW
18162 TYPE_NO_RETURN (ftype) = 1;
18163
76c10ea2
GM
18164 /* We need to add the subroutine type to the die immediately so
18165 we don't infinitely recurse when dealing with parameters
0963b4bd 18166 declared as the same subroutine type. */
76c10ea2 18167 set_die_type (die, ftype, cu);
6e70227d 18168
639d11d3 18169 if (die->child != NULL)
c906108c 18170 {
bb5ed363 18171 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 18172 struct die_info *child_die;
8072405b 18173 int nparams, iparams;
c906108c
SS
18174
18175 /* Count the number of parameters.
dda83cd7
SM
18176 FIXME: GDB currently ignores vararg functions, but knows about
18177 vararg member functions. */
8072405b 18178 nparams = 0;
639d11d3 18179 child_die = die->child;
c906108c
SS
18180 while (child_die && child_die->tag)
18181 {
18182 if (child_die->tag == DW_TAG_formal_parameter)
18183 nparams++;
18184 else if (child_die->tag == DW_TAG_unspecified_parameters)
1d6286ed
SM
18185 ftype->set_has_varargs (true);
18186
436c571c 18187 child_die = child_die->sibling;
c906108c
SS
18188 }
18189
18190 /* Allocate storage for parameters and fill them in. */
5e33d5f4 18191 ftype->set_num_fields (nparams);
3cabb6b0
SM
18192 ftype->set_fields
18193 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 18194
8072405b
JK
18195 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18196 even if we error out during the parameters reading below. */
18197 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 18198 ftype->field (iparams).set_type (void_type);
8072405b
JK
18199
18200 iparams = 0;
639d11d3 18201 child_die = die->child;
c906108c
SS
18202 while (child_die && child_die->tag)
18203 {
18204 if (child_die->tag == DW_TAG_formal_parameter)
18205 {
3ce3b1ba
PA
18206 struct type *arg_type;
18207
18208 /* DWARF version 2 has no clean way to discern C++
18209 static and non-static member functions. G++ helps
18210 GDB by marking the first parameter for non-static
18211 member functions (which is the this pointer) as
18212 artificial. We pass this information to
18213 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18214
18215 DWARF version 3 added DW_AT_object_pointer, which GCC
18216 4.5 does not yet generate. */
e142c38c 18217 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 18218 if (attr != nullptr)
c45bc3f8 18219 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
c906108c 18220 else
9c37b5ae 18221 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
18222 arg_type = die_type (child_die, cu);
18223
18224 /* RealView does not mark THIS as const, which the testsuite
18225 expects. GCC marks THIS as const in method definitions,
18226 but not in the class specifications (GCC PR 43053). */
18227 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18228 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18229 {
18230 int is_this = 0;
18231 struct dwarf2_cu *arg_cu = cu;
18232 const char *name = dwarf2_name (child_die, cu);
18233
18234 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 18235 if (attr != nullptr)
3ce3b1ba
PA
18236 {
18237 /* If the compiler emits this, use it. */
18238 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18239 is_this = 1;
18240 }
18241 else if (name && strcmp (name, "this") == 0)
18242 /* Function definitions will have the argument names. */
18243 is_this = 1;
18244 else if (name == NULL && iparams == 0)
18245 /* Declarations may not have the names, so like
18246 elsewhere in GDB, assume an artificial first
18247 argument is "this". */
18248 is_this = 1;
18249
18250 if (is_this)
18251 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18252 arg_type, 0);
18253 }
18254
5d14b6e5 18255 ftype->field (iparams).set_type (arg_type);
c906108c
SS
18256 iparams++;
18257 }
436c571c 18258 child_die = child_die->sibling;
c906108c
SS
18259 }
18260 }
18261
76c10ea2 18262 return ftype;
c906108c
SS
18263}
18264
f792889a 18265static struct type *
e7c27a73 18266read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18267{
5e22e966 18268 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 18269 const char *name = NULL;
3c8e0968 18270 struct type *this_type, *target_type;
c906108c 18271
94af9270 18272 name = dwarf2_full_name (NULL, die, cu);
19f392bc 18273 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
8f53807e 18274 this_type->set_target_is_stub (true);
f792889a 18275 set_die_type (die, this_type, cu);
3c8e0968
DE
18276 target_type = die_type (die, cu);
18277 if (target_type != this_type)
18278 TYPE_TARGET_TYPE (this_type) = target_type;
18279 else
18280 {
18281 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18282 spec and cause infinite loops in GDB. */
b98664d3 18283 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
18284 "- DIE at %s [in module %s]"),
18285 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
18286 TYPE_TARGET_TYPE (this_type) = NULL;
18287 }
e4003a34
TV
18288 if (name == NULL)
18289 {
18290 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18291 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18292 Handle these by just returning the target type, rather than
18293 constructing an anonymous typedef type and trying to handle this
18294 elsewhere. */
18295 set_die_type (die, target_type, cu);
18296 return target_type;
18297 }
f792889a 18298 return this_type;
c906108c
SS
18299}
18300
a625a8c9
TT
18301/* Helper for get_dwarf2_rational_constant that computes the value of
18302 a given gmp_mpz given an attribute. */
18303
18304static void
18305get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
18306{
18307 /* GCC will sometimes emit a 16-byte constant value as a DWARF
18308 location expression that pushes an implicit value. */
18309 if (attr->form == DW_FORM_exprloc)
18310 {
18311 dwarf_block *blk = attr->as_block ();
18312 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18313 {
18314 uint64_t len;
18315 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18316 blk->data + blk->size,
18317 &len);
18318 if (ptr - blk->data + len <= blk->size)
18319 {
18320 mpz_import (value->val, len,
18321 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18322 1, 0, 0, ptr);
18323 return;
18324 }
18325 }
18326
18327 /* On failure set it to 1. */
18328 *value = gdb_mpz (1);
18329 }
18330 else if (attr->form_is_block ())
18331 {
18332 dwarf_block *blk = attr->as_block ();
18333 mpz_import (value->val, blk->size,
18334 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18335 1, 0, 0, blk->data);
18336 }
18337 else
18338 *value = gdb_mpz (attr->constant_value (1));
18339}
18340
09584414
JB
18341/* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18342 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18343
18344 If the numerator and/or numerator attribute is missing,
18345 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18346 untouched. */
18347
18348static void
18349get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
5cde1d82 18350 gdb_mpz *numerator, gdb_mpz *denominator)
09584414
JB
18351{
18352 struct attribute *num_attr, *denom_attr;
18353
18354 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18355 if (num_attr == nullptr)
18356 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18357 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18358
18359 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18360 if (denom_attr == nullptr)
18361 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18362 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18363
18364 if (num_attr == nullptr || denom_attr == nullptr)
18365 return;
18366
a625a8c9
TT
18367 get_mpz (cu, numerator, num_attr);
18368 get_mpz (cu, denominator, denom_attr);
09584414
JB
18369}
18370
18371/* Same as get_dwarf2_rational_constant, but extracting an unsigned
18372 rational constant, rather than a signed one.
18373
18374 If the rational constant has a negative value, a complaint
18375 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18376
18377static void
18378get_dwarf2_unsigned_rational_constant (struct die_info *die,
18379 struct dwarf2_cu *cu,
5cde1d82
TT
18380 gdb_mpz *numerator,
18381 gdb_mpz *denominator)
09584414 18382{
5cde1d82
TT
18383 gdb_mpz num (1);
18384 gdb_mpz denom (1);
09584414
JB
18385
18386 get_dwarf2_rational_constant (die, cu, &num, &denom);
5cde1d82 18387 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
09584414 18388 {
5cde1d82
TT
18389 mpz_neg (num.val, num.val);
18390 mpz_neg (denom.val, denom.val);
09584414 18391 }
5cde1d82 18392 else if (mpz_sgn (num.val) == -1)
09584414
JB
18393 {
18394 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18395 " in DIE at %s"),
18396 sect_offset_str (die->sect_off));
18397 return;
18398 }
5cde1d82 18399 else if (mpz_sgn (denom.val) == -1)
09584414
JB
18400 {
18401 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18402 " in DIE at %s"),
18403 sect_offset_str (die->sect_off));
18404 return;
18405 }
18406
5cde1d82
TT
18407 *numerator = std::move (num);
18408 *denominator = std::move (denom);
09584414
JB
18409}
18410
18411/* Assuming DIE corresponds to a fixed point type, finish the creation
2a12c336 18412 of the corresponding TYPE by setting its type-specific data.
09584414
JB
18413 CU is the DIE's CU. */
18414
18415static void
18416finish_fixed_point_type (struct type *type, struct die_info *die,
18417 struct dwarf2_cu *cu)
18418{
18419 struct attribute *attr;
09584414
JB
18420
18421 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18422 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18423
18424 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18425 if (!attr)
18426 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18427 if (!attr)
18428 attr = dwarf2_attr (die, DW_AT_small, cu);
18429
5cde1d82
TT
18430 /* Numerator and denominator of our fixed-point type's scaling factor.
18431 The default is a scaling factor of 1, which we use as a fallback
18432 when we are not able to decode it (problem with the debugging info,
18433 unsupported forms, bug in GDB, etc...). Using that as the default
18434 allows us to at least print the unscaled value, which might still
18435 be useful to a user. */
18436 gdb_mpz scale_num (1);
18437 gdb_mpz scale_denom (1);
18438
09584414
JB
18439 if (attr == nullptr)
18440 {
18441 /* Scaling factor not found. Assume a scaling factor of 1,
18442 and hope for the best. At least the user will be able to see
18443 the encoded value. */
18444 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18445 sect_offset_str (die->sect_off));
18446 }
18447 else if (attr->name == DW_AT_binary_scale)
18448 {
18449 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18450 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18451
5cde1d82 18452 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
09584414
JB
18453 }
18454 else if (attr->name == DW_AT_decimal_scale)
18455 {
18456 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18457 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18458
5cde1d82 18459 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
09584414
JB
18460 }
18461 else if (attr->name == DW_AT_small)
18462 {
18463 struct die_info *scale_die;
18464 struct dwarf2_cu *scale_cu = cu;
18465
18466 scale_die = follow_die_ref (die, attr, &scale_cu);
18467 if (scale_die->tag == DW_TAG_constant)
18468 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18469 &scale_num, &scale_denom);
18470 else
18471 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18472 " (DIE at %s)"),
18473 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18474 }
18475 else
18476 {
18477 complaint (_("unsupported scale attribute %s for fixed-point type"
18478 " (DIE at %s)"),
18479 dwarf_attr_name (attr->name),
18480 sect_offset_str (die->sect_off));
18481 }
18482
2a12c336 18483 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
5cde1d82
TT
18484 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18485 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
09584414
JB
18486 mpq_canonicalize (scaling_factor.val);
18487}
18488
9b790ce7
UW
18489/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18490 (which may be different from NAME) to the architecture back-end to allow
18491 it to guess the correct format if necessary. */
18492
18493static struct type *
18494dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 18495 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 18496{
08feed99 18497 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
18498 const struct floatformat **format;
18499 struct type *type;
18500
18501 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18502 if (format)
103a685e 18503 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 18504 else
77b7c781 18505 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
18506
18507 return type;
18508}
18509
eb77c9df
AB
18510/* Allocate an integer type of size BITS and name NAME. */
18511
18512static struct type *
18513dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18514 int bits, int unsigned_p, const char *name)
18515{
18516 struct type *type;
18517
18518 /* Versions of Intel's C Compiler generate an integer type called "void"
18519 instead of using DW_TAG_unspecified_type. This has been seen on
18520 at least versions 14, 17, and 18. */
35ee2dc2
AB
18521 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18522 && strcmp (name, "void") == 0)
eb77c9df
AB
18523 type = objfile_type (objfile)->builtin_void;
18524 else
18525 type = init_integer_type (objfile, bits, unsigned_p, name);
18526
18527 return type;
18528}
18529
09584414
JB
18530/* Return true if DIE has a DW_AT_small attribute whose value is
18531 a constant rational, where both the numerator and denominator
18532 are equal to zero.
18533
18534 CU is the DIE's Compilation Unit. */
18535
18536static bool
18537has_zero_over_zero_small_attribute (struct die_info *die,
18538 struct dwarf2_cu *cu)
18539{
18540 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18541 if (attr == nullptr)
18542 return false;
18543
18544 struct dwarf2_cu *scale_cu = cu;
18545 struct die_info *scale_die
18546 = follow_die_ref (die, attr, &scale_cu);
18547
18548 if (scale_die->tag != DW_TAG_constant)
18549 return false;
18550
5cde1d82 18551 gdb_mpz num (1), denom (1);
09584414 18552 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
5cde1d82 18553 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
09584414
JB
18554}
18555
8bdc1658
AB
18556/* Initialise and return a floating point type of size BITS suitable for
18557 use as a component of a complex number. The NAME_HINT is passed through
18558 when initialising the floating point type and is the name of the complex
18559 type.
18560
18561 As DWARF doesn't currently provide an explicit name for the components
18562 of a complex number, but it can be helpful to have these components
18563 named, we try to select a suitable name based on the size of the
18564 component. */
18565static struct type *
18566dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18567 struct objfile *objfile,
103a685e
TT
18568 int bits, const char *name_hint,
18569 enum bfd_endian byte_order)
8bdc1658 18570{
08feed99 18571 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
18572 struct type *tt = nullptr;
18573
35add35e
AB
18574 /* Try to find a suitable floating point builtin type of size BITS.
18575 We're going to use the name of this type as the name for the complex
18576 target type that we are about to create. */
1db455a7 18577 switch (cu->language)
8bdc1658 18578 {
1db455a7
AB
18579 case language_fortran:
18580 switch (bits)
18581 {
18582 case 32:
18583 tt = builtin_f_type (gdbarch)->builtin_real;
18584 break;
18585 case 64:
18586 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18587 break;
18588 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18589 case 128:
18590 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18591 break;
18592 }
8bdc1658 18593 break;
1db455a7
AB
18594 default:
18595 switch (bits)
18596 {
18597 case 32:
18598 tt = builtin_type (gdbarch)->builtin_float;
18599 break;
18600 case 64:
18601 tt = builtin_type (gdbarch)->builtin_double;
18602 break;
18603 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18604 case 128:
18605 tt = builtin_type (gdbarch)->builtin_long_double;
18606 break;
18607 }
8bdc1658
AB
18608 break;
18609 }
18610
35add35e
AB
18611 /* If the type we found doesn't match the size we were looking for, then
18612 pretend we didn't find a type at all, the complex target type we
18613 create will then be nameless. */
a12e5744 18614 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
18615 tt = nullptr;
18616
7d93a1e0 18617 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 18618 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
18619}
18620
c906108c
SS
18621/* Find a representation of a given base type and install
18622 it in the TYPE field of the die. */
18623
f792889a 18624static struct type *
e7c27a73 18625read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18626{
5e22e966 18627 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
18628 struct type *type;
18629 struct attribute *attr;
19f392bc 18630 int encoding = 0, bits = 0;
15d034d0 18631 const char *name;
34877895 18632 gdbarch *arch;
c906108c 18633
e142c38c 18634 attr = dwarf2_attr (die, DW_AT_encoding, cu);
529908cb
TT
18635 if (attr != nullptr && attr->form_is_constant ())
18636 encoding = attr->constant_value (0);
e142c38c 18637 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18638 if (attr != nullptr)
529908cb 18639 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
39cbfefa 18640 name = dwarf2_name (die, cu);
6ccb9162 18641 if (!name)
34877895 18642 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 18643
08feed99 18644 arch = objfile->arch ();
103a685e
TT
18645 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18646
34877895 18647 attr = dwarf2_attr (die, DW_AT_endianity, cu);
529908cb 18648 if (attr != nullptr && attr->form_is_constant ())
103a685e 18649 {
529908cb 18650 int endianity = attr->constant_value (0);
103a685e
TT
18651
18652 switch (endianity)
18653 {
18654 case DW_END_big:
18655 byte_order = BFD_ENDIAN_BIG;
18656 break;
18657 case DW_END_little:
18658 byte_order = BFD_ENDIAN_LITTLE;
18659 break;
18660 default:
18661 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18662 break;
18663 }
18664 }
6ccb9162 18665
09584414
JB
18666 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18667 && cu->language == language_ada
18668 && has_zero_over_zero_small_attribute (die, cu))
18669 {
18670 /* brobecker/2018-02-24: This is a fixed point type for which
18671 the scaling factor is represented as fraction whose value
18672 does not make sense (zero divided by zero), so we should
18673 normally never see these. However, there is a small category
18674 of fixed point types for which GNAT is unable to provide
18675 the scaling factor via the standard DWARF mechanisms, and
18676 for which the info is provided via the GNAT encodings instead.
18677 This is likely what this DIE is about.
18678
18679 Ideally, GNAT should be declaring this type the same way
18680 it declares other fixed point types when using the legacy
18681 GNAT encoding, which is to use a simple signed or unsigned
18682 base type. A report to the GNAT team has been created to
18683 look into it. In the meantime, pretend this type is a simple
18684 signed or unsigned integral, rather than a fixed point type,
18685 to avoid any confusion later on as to how to process this type. */
18686 encoding = (encoding == DW_ATE_signed_fixed
18687 ? DW_ATE_signed
18688 : DW_ATE_unsigned);
18689 }
18690
6ccb9162 18691 switch (encoding)
c906108c 18692 {
6ccb9162
UW
18693 case DW_ATE_address:
18694 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 18695 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 18696 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
18697 break;
18698 case DW_ATE_boolean:
19f392bc 18699 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
18700 break;
18701 case DW_ATE_complex_float:
103a685e
TT
18702 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18703 byte_order);
78134374 18704 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
18705 {
18706 if (name == nullptr)
18707 {
18708 struct obstack *obstack
5e22e966 18709 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 18710 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
18711 nullptr);
18712 }
18713 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18714 }
18715 else
18716 type = init_complex_type (name, type);
6ccb9162
UW
18717 break;
18718 case DW_ATE_decimal_float:
19f392bc 18719 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
18720 break;
18721 case DW_ATE_float:
103a685e 18722 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
18723 break;
18724 case DW_ATE_signed:
eb77c9df 18725 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18726 break;
18727 case DW_ATE_unsigned:
3b2b8fea
TT
18728 if (cu->language == language_fortran
18729 && name
61012eef 18730 && startswith (name, "character("))
19f392bc
UW
18731 type = init_character_type (objfile, bits, 1, name);
18732 else
eb77c9df 18733 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
18734 break;
18735 case DW_ATE_signed_char:
6e70227d 18736 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
18737 || cu->language == language_pascal
18738 || cu->language == language_fortran)
19f392bc
UW
18739 type = init_character_type (objfile, bits, 0, name);
18740 else
eb77c9df 18741 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18742 break;
18743 case DW_ATE_unsigned_char:
868a0084 18744 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 18745 || cu->language == language_pascal
c44af4eb
TT
18746 || cu->language == language_fortran
18747 || cu->language == language_rust)
19f392bc
UW
18748 type = init_character_type (objfile, bits, 1, name);
18749 else
eb77c9df 18750 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 18751 break;
75079b2b 18752 case DW_ATE_UTF:
53e710ac 18753 {
53e710ac
PA
18754 if (bits == 16)
18755 type = builtin_type (arch)->builtin_char16;
18756 else if (bits == 32)
18757 type = builtin_type (arch)->builtin_char32;
18758 else
18759 {
b98664d3 18760 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 18761 bits);
eb77c9df 18762 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
18763 }
18764 return set_die_type (die, type, cu);
18765 }
75079b2b 18766 break;
09584414
JB
18767 case DW_ATE_signed_fixed:
18768 type = init_fixed_point_type (objfile, bits, 0, name);
18769 finish_fixed_point_type (type, die, cu);
18770 break;
18771 case DW_ATE_unsigned_fixed:
18772 type = init_fixed_point_type (objfile, bits, 1, name);
18773 finish_fixed_point_type (type, die, cu);
18774 break;
75079b2b 18775
6ccb9162 18776 default:
b98664d3 18777 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 18778 dwarf_type_encoding_name (encoding));
77b7c781 18779 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18780 break;
c906108c 18781 }
6ccb9162 18782
0114d602 18783 if (name && strcmp (name, "char") == 0)
15152a54 18784 type->set_has_no_signedness (true);
0114d602 18785
2b4424c3
TT
18786 maybe_set_alignment (cu, die, type);
18787
db558e34 18788 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
34877895 18789
20a5fcbd
TT
18790 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18791 {
18792 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
529908cb 18793 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
20a5fcbd 18794 {
529908cb 18795 unsigned real_bit_size = attr->as_unsigned ();
20a5fcbd
TT
18796 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18797 /* Only use the attributes if they make sense together. */
18798 if (attr == nullptr
529908cb
TT
18799 || (attr->as_unsigned () + real_bit_size
18800 <= 8 * TYPE_LENGTH (type)))
20a5fcbd
TT
18801 {
18802 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18803 = real_bit_size;
18804 if (attr != nullptr)
18805 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
529908cb 18806 = attr->as_unsigned ();
20a5fcbd
TT
18807 }
18808 }
18809 }
18810
f792889a 18811 return set_die_type (die, type, cu);
c906108c
SS
18812}
18813
80180f79
SA
18814/* Parse dwarf attribute if it's a block, reference or constant and put the
18815 resulting value of the attribute into struct bound_prop.
18816 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18817
18818static int
18819attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
18820 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18821 struct type *default_type)
80180f79
SA
18822{
18823 struct dwarf2_property_baton *baton;
5e22e966 18824 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
18825 struct objfile *objfile = per_objfile->objfile;
18826 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 18827
9a49df9d
AB
18828 gdb_assert (default_type != NULL);
18829
80180f79
SA
18830 if (attr == NULL || prop == NULL)
18831 return 0;
18832
4fc6c0d5 18833 if (attr->form_is_block ())
80180f79 18834 {
8d749320 18835 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18836 baton->property_type = default_type;
80180f79 18837 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18838 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18839
18840 struct dwarf_block *block = attr->as_block ();
18841 baton->locexpr.size = block->size;
18842 baton->locexpr.data = block->data;
216a7e6b
AB
18843 switch (attr->name)
18844 {
18845 case DW_AT_string_length:
18846 baton->locexpr.is_reference = true;
18847 break;
18848 default:
18849 baton->locexpr.is_reference = false;
18850 break;
18851 }
8c2e4e06
SM
18852
18853 prop->set_locexpr (baton);
18854 gdb_assert (prop->baton () != NULL);
80180f79 18855 }
cd6c91b4 18856 else if (attr->form_is_ref ())
80180f79
SA
18857 {
18858 struct dwarf2_cu *target_cu = cu;
18859 struct die_info *target_die;
18860 struct attribute *target_attr;
18861
18862 target_die = follow_die_ref (die, attr, &target_cu);
18863 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18864 if (target_attr == NULL)
18865 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18866 target_cu);
80180f79
SA
18867 if (target_attr == NULL)
18868 return 0;
18869
df25ebbd 18870 switch (target_attr->name)
80180f79 18871 {
df25ebbd 18872 case DW_AT_location:
cd6c91b4 18873 if (target_attr->form_is_section_offset ())
df25ebbd 18874 {
8d749320 18875 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18876 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18877 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
8c2e4e06
SM
18878 prop->set_loclist (baton);
18879 gdb_assert (prop->baton () != NULL);
df25ebbd 18880 }
4fc6c0d5 18881 else if (target_attr->form_is_block ())
df25ebbd 18882 {
8d749320 18883 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18884 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18885 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18886 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18887 struct dwarf_block *block = target_attr->as_block ();
18888 baton->locexpr.size = block->size;
18889 baton->locexpr.data = block->data;
9a49df9d 18890 baton->locexpr.is_reference = true;
8c2e4e06
SM
18891 prop->set_locexpr (baton);
18892 gdb_assert (prop->baton () != NULL);
df25ebbd
JB
18893 }
18894 else
18895 {
18896 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18897 "dynamic property");
18898 return 0;
18899 }
18900 break;
18901 case DW_AT_data_member_location:
18902 {
18903 LONGEST offset;
18904
18905 if (!handle_data_member_location (target_die, target_cu,
18906 &offset))
18907 return 0;
18908
8d749320 18909 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18910 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18911 target_cu);
df25ebbd
JB
18912 baton->offset_info.offset = offset;
18913 baton->offset_info.type = die_type (target_die, target_cu);
8c2e4e06 18914 prop->set_addr_offset (baton);
df25ebbd
JB
18915 break;
18916 }
80180f79
SA
18917 }
18918 }
cd6c91b4 18919 else if (attr->form_is_constant ())
8c2e4e06 18920 prop->set_const_val (attr->constant_value (0));
80180f79
SA
18921 else
18922 {
18923 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18924 dwarf2_name (die, cu));
18925 return 0;
18926 }
18927
18928 return 1;
18929}
18930
09ba997f 18931/* See read.h. */
9a49df9d 18932
09ba997f 18933struct type *
293e7e51 18934dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 18935{
9a49df9d
AB
18936 struct type *int_type;
18937
18938 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18939#define TRY_TYPE(F) \
18940 int_type = (unsigned_p \
18941 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18942 : objfile_type (objfile)->builtin_ ## F); \
18943 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18944 return int_type
18945
18946 TRY_TYPE (char);
18947 TRY_TYPE (short);
18948 TRY_TYPE (int);
18949 TRY_TYPE (long);
18950 TRY_TYPE (long_long);
18951
18952#undef TRY_TYPE
18953
18954 gdb_assert_not_reached ("unable to find suitable integer type");
18955}
18956
09ba997f 18957/* See read.h. */
11a8b164 18958
09ba997f 18959struct type *
293e7e51 18960dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 18961{
293e7e51
SM
18962 int addr_size = this->per_cu->addr_size ();
18963 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
18964}
18965
b86352cf
AB
18966/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18967 present (which is valid) then compute the default type based on the
18968 compilation units address size. */
18969
18970static struct type *
18971read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18972{
18973 struct type *index_type = die_type (die, cu);
18974
18975 /* Dwarf-2 specifications explicitly allows to create subrange types
18976 without specifying a base type.
18977 In that case, the base type must be set to the type of
18978 the lower bound, upper bound or count, in that order, if any of these
18979 three attributes references an object that has a type.
18980 If no base type is found, the Dwarf-2 specifications say that
18981 a signed integer type of size equal to the size of an address should
18982 be used.
18983 For the following C code: `extern char gdb_int [];'
18984 GCC produces an empty range DIE.
18985 FIXME: muller/2010-05-28: Possible references to object for low bound,
18986 high bound or count are not yet handled by this code. */
78134374 18987 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 18988 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
18989
18990 return index_type;
18991}
18992
a02abb62
JB
18993/* Read the given DW_AT_subrange DIE. */
18994
f792889a 18995static struct type *
a02abb62
JB
18996read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18997{
4c9ad8c2 18998 struct type *base_type, *orig_base_type;
a02abb62
JB
18999 struct type *range_type;
19000 struct attribute *attr;
729efb13 19001 struct dynamic_prop low, high;
4fae6e18 19002 int low_default_is_valid;
c451ebe5 19003 int high_bound_is_count = 0;
15d034d0 19004 const char *name;
d359392f 19005 ULONGEST negative_mask;
e77813c8 19006
b86352cf
AB
19007 orig_base_type = read_subrange_index_type (die, cu);
19008
4c9ad8c2
TT
19009 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
19010 whereas the real type might be. So, we use ORIG_BASE_TYPE when
19011 creating the range type, but we use the result of check_typedef
19012 when examining properties of the type. */
19013 base_type = check_typedef (orig_base_type);
a02abb62 19014
7e314c57
JK
19015 /* The die_type call above may have already set the type for this DIE. */
19016 range_type = get_die_type (die, cu);
19017 if (range_type)
19018 return range_type;
19019
8c2e4e06 19020 high.set_const_val (0);
729efb13 19021
4fae6e18
JK
19022 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
19023 omitting DW_AT_lower_bound. */
19024 switch (cu->language)
6e70227d 19025 {
4fae6e18
JK
19026 case language_c:
19027 case language_cplus:
8c2e4e06 19028 low.set_const_val (0);
4fae6e18
JK
19029 low_default_is_valid = 1;
19030 break;
19031 case language_fortran:
8c2e4e06 19032 low.set_const_val (1);
4fae6e18
JK
19033 low_default_is_valid = 1;
19034 break;
19035 case language_d:
4fae6e18 19036 case language_objc:
c44af4eb 19037 case language_rust:
8c2e4e06 19038 low.set_const_val (0);
4fae6e18
JK
19039 low_default_is_valid = (cu->header.version >= 4);
19040 break;
19041 case language_ada:
19042 case language_m2:
19043 case language_pascal:
8c2e4e06 19044 low.set_const_val (1);
4fae6e18
JK
19045 low_default_is_valid = (cu->header.version >= 4);
19046 break;
19047 default:
8c2e4e06 19048 low.set_const_val (0);
4fae6e18
JK
19049 low_default_is_valid = 0;
19050 break;
a02abb62
JB
19051 }
19052
e142c38c 19053 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 19054 if (attr != nullptr)
9a49df9d 19055 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 19056 else if (!low_default_is_valid)
b98664d3 19057 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
19058 "- DIE at %s [in module %s]"),
19059 sect_offset_str (die->sect_off),
5e22e966 19060 objfile_name (cu->per_objfile->objfile));
a02abb62 19061
506f5c41
TV
19062 struct attribute *attr_ub, *attr_count;
19063 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 19064 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 19065 {
506f5c41 19066 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 19067 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 19068 {
c451ebe5 19069 /* If bounds are constant do the final calculation here. */
8c2e4e06
SM
19070 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
19071 high.set_const_val (low.const_val () + high.const_val () - 1);
c451ebe5
SA
19072 else
19073 high_bound_is_count = 1;
c2ff108b 19074 }
506f5c41
TV
19075 else
19076 {
19077 if (attr_ub != NULL)
19078 complaint (_("Unresolved DW_AT_upper_bound "
19079 "- DIE at %s [in module %s]"),
19080 sect_offset_str (die->sect_off),
5e22e966 19081 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
19082 if (attr_count != NULL)
19083 complaint (_("Unresolved DW_AT_count "
19084 "- DIE at %s [in module %s]"),
19085 sect_offset_str (die->sect_off),
5e22e966 19086 objfile_name (cu->per_objfile->objfile));
506f5c41 19087 }
e77813c8 19088 }
a02abb62 19089
4e962e74
TT
19090 LONGEST bias = 0;
19091 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 19092 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 19093 bias = bias_attr->constant_value (0);
4e962e74 19094
dbb9c2b1
JB
19095 /* Normally, the DWARF producers are expected to use a signed
19096 constant form (Eg. DW_FORM_sdata) to express negative bounds.
19097 But this is unfortunately not always the case, as witnessed
19098 with GCC, for instance, where the ambiguous DW_FORM_dataN form
19099 is used instead. To work around that ambiguity, we treat
19100 the bounds as signed, and thus sign-extend their values, when
19101 the base type is signed. */
6e70227d 19102 negative_mask =
d359392f 19103 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
8c2e4e06 19104 if (low.kind () == PROP_CONST
c6d940a9 19105 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
8c2e4e06
SM
19106 low.set_const_val (low.const_val () | negative_mask);
19107 if (high.kind () == PROP_CONST
c6d940a9 19108 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
8c2e4e06 19109 high.set_const_val (high.const_val () | negative_mask);
43bbcdc2 19110
5bbd8269
AB
19111 /* Check for bit and byte strides. */
19112 struct dynamic_prop byte_stride_prop;
19113 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
19114 if (attr_byte_stride != nullptr)
19115 {
293e7e51 19116 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
19117 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
19118 prop_type);
19119 }
19120
19121 struct dynamic_prop bit_stride_prop;
19122 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
19123 if (attr_bit_stride != nullptr)
19124 {
19125 /* It only makes sense to have either a bit or byte stride. */
19126 if (attr_byte_stride != nullptr)
19127 {
19128 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19129 "- DIE at %s [in module %s]"),
19130 sect_offset_str (die->sect_off),
5e22e966 19131 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
19132 attr_bit_stride = nullptr;
19133 }
19134 else
19135 {
293e7e51 19136 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
19137 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19138 prop_type);
19139 }
19140 }
19141
19142 if (attr_byte_stride != nullptr
19143 || attr_bit_stride != nullptr)
19144 {
19145 bool byte_stride_p = (attr_byte_stride != nullptr);
19146 struct dynamic_prop *stride
19147 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19148
19149 range_type
19150 = create_range_type_with_stride (NULL, orig_base_type, &low,
19151 &high, bias, stride, byte_stride_p);
19152 }
19153 else
19154 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 19155
c451ebe5 19156 if (high_bound_is_count)
599088e3 19157 range_type->bounds ()->flag_upper_bound_is_count = 1;
c451ebe5 19158
c2ff108b
JK
19159 /* Ada expects an empty array on no boundary attributes. */
19160 if (attr == NULL && cu->language != language_ada)
8c2e4e06 19161 range_type->bounds ()->high.set_undefined ();
c2ff108b 19162
39cbfefa
DJ
19163 name = dwarf2_name (die, cu);
19164 if (name)
d0e39ea2 19165 range_type->set_name (name);
6e70227d 19166
e142c38c 19167 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 19168 if (attr != nullptr)
529908cb 19169 TYPE_LENGTH (range_type) = attr->constant_value (0);
a02abb62 19170
2b4424c3
TT
19171 maybe_set_alignment (cu, die, range_type);
19172
7e314c57
JK
19173 set_die_type (die, range_type, cu);
19174
19175 /* set_die_type should be already done. */
b4ba55a1
JB
19176 set_descriptive_type (range_type, die, cu);
19177
7e314c57 19178 return range_type;
a02abb62 19179}
6e70227d 19180
f792889a 19181static struct type *
81a17f79
JB
19182read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19183{
19184 struct type *type;
81a17f79 19185
5e22e966 19186 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 19187 type->set_name (dwarf2_name (die, cu));
81a17f79 19188
74a2f8ff 19189 /* In Ada, an unspecified type is typically used when the description
85102364 19190 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
19191 such a type, we treat it as a stub, and try to resolve it later on,
19192 when needed. */
19193 if (cu->language == language_ada)
b4b73759 19194 type->set_is_stub (true);
74a2f8ff 19195
f792889a 19196 return set_die_type (die, type, cu);
81a17f79 19197}
a02abb62 19198
639d11d3
DC
19199/* Read a single die and all its descendents. Set the die's sibling
19200 field to NULL; set other fields in the die correctly, and set all
19201 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19202 location of the info_ptr after reading all of those dies. PARENT
19203 is the parent of the die in question. */
19204
19205static struct die_info *
dee91e82 19206read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
19207 const gdb_byte *info_ptr,
19208 const gdb_byte **new_info_ptr,
dee91e82 19209 struct die_info *parent)
639d11d3
DC
19210{
19211 struct die_info *die;
d521ce57 19212 const gdb_byte *cur_ptr;
639d11d3 19213
3e225074 19214 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
19215 if (die == NULL)
19216 {
19217 *new_info_ptr = cur_ptr;
19218 return NULL;
19219 }
93311388 19220 store_in_ref_table (die, reader->cu);
639d11d3 19221
3e225074 19222 if (die->has_children)
bf6af496 19223 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
19224 else
19225 {
19226 die->child = NULL;
19227 *new_info_ptr = cur_ptr;
19228 }
19229
19230 die->sibling = NULL;
19231 die->parent = parent;
19232 return die;
19233}
19234
19235/* Read a die, all of its descendents, and all of its siblings; set
19236 all of the fields of all of the dies correctly. Arguments are as
19237 in read_die_and_children. */
19238
19239static struct die_info *
bf6af496 19240read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
19241 const gdb_byte *info_ptr,
19242 const gdb_byte **new_info_ptr,
bf6af496 19243 struct die_info *parent)
639d11d3
DC
19244{
19245 struct die_info *first_die, *last_sibling;
d521ce57 19246 const gdb_byte *cur_ptr;
639d11d3 19247
c906108c 19248 cur_ptr = info_ptr;
639d11d3
DC
19249 first_die = last_sibling = NULL;
19250
19251 while (1)
c906108c 19252 {
639d11d3 19253 struct die_info *die
dee91e82 19254 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 19255
1d325ec1 19256 if (die == NULL)
c906108c 19257 {
639d11d3
DC
19258 *new_info_ptr = cur_ptr;
19259 return first_die;
c906108c 19260 }
1d325ec1
DJ
19261
19262 if (!first_die)
19263 first_die = die;
c906108c 19264 else
1d325ec1
DJ
19265 last_sibling->sibling = die;
19266
19267 last_sibling = die;
c906108c 19268 }
c906108c
SS
19269}
19270
bf6af496
DE
19271/* Read a die, all of its descendents, and all of its siblings; set
19272 all of the fields of all of the dies correctly. Arguments are as
19273 in read_die_and_children.
19274 This the main entry point for reading a DIE and all its children. */
19275
19276static struct die_info *
19277read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
19278 const gdb_byte *info_ptr,
19279 const gdb_byte **new_info_ptr,
bf6af496
DE
19280 struct die_info *parent)
19281{
19282 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19283 new_info_ptr, parent);
19284
b4f54984 19285 if (dwarf_die_debug)
bf6af496
DE
19286 {
19287 fprintf_unfiltered (gdb_stdlog,
19288 "Read die from %s@0x%x of %s:\n",
96b79293 19289 reader->die_section->get_name (),
bf6af496
DE
19290 (unsigned) (info_ptr - reader->die_section->buffer),
19291 bfd_get_filename (reader->abfd));
b4f54984 19292 dump_die (die, dwarf_die_debug);
bf6af496
DE
19293 }
19294
19295 return die;
19296}
19297
3019eac3
DE
19298/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19299 attributes.
19300 The caller is responsible for filling in the extra attributes
19301 and updating (*DIEP)->num_attrs.
19302 Set DIEP to point to a newly allocated die with its information,
3e225074 19303 except for its child, sibling, and parent fields. */
93311388 19304
d521ce57 19305static const gdb_byte *
3019eac3 19306read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 19307 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 19308 int num_extra_attrs)
93311388 19309{
b64f50a1 19310 unsigned int abbrev_number, bytes_read, i;
93311388
DE
19311 struct abbrev_info *abbrev;
19312 struct die_info *die;
19313 struct dwarf2_cu *cu = reader->cu;
19314 bfd *abfd = reader->abfd;
19315
9c541725 19316 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
19317 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19318 info_ptr += bytes_read;
19319 if (!abbrev_number)
19320 {
19321 *diep = NULL;
93311388
DE
19322 return info_ptr;
19323 }
19324
685af9cd 19325 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 19326 if (!abbrev)
348e048f
DE
19327 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19328 abbrev_number,
19329 bfd_get_filename (abfd));
19330
3019eac3 19331 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 19332 die->sect_off = sect_off;
93311388
DE
19333 die->tag = abbrev->tag;
19334 die->abbrev = abbrev_number;
3e225074 19335 die->has_children = abbrev->has_children;
93311388 19336
3019eac3
DE
19337 /* Make the result usable.
19338 The caller needs to update num_attrs after adding the extra
19339 attributes. */
93311388
DE
19340 die->num_attrs = abbrev->num_attrs;
19341
7a5f294d 19342 bool any_need_reprocess = false;
93311388 19343 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e 19344 {
7a5f294d
TT
19345 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19346 info_ptr);
19347 if (die->attrs[i].requires_reprocessing_p ())
19348 any_need_reprocess = true;
18a8505e
AT
19349 }
19350
052c8bb8 19351 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
529908cb
TT
19352 if (attr != nullptr && attr->form_is_unsigned ())
19353 cu->str_offsets_base = attr->as_unsigned ();
93311388 19354
41144253 19355 attr = die->attr (DW_AT_loclists_base);
19356 if (attr != nullptr)
529908cb 19357 cu->loclist_base = attr->as_unsigned ();
41144253 19358
a39fdb41 19359 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
19360 if (maybe_addr_base.has_value ())
19361 cu->addr_base = *maybe_addr_base;
d0ce17d8
CT
19362
19363 attr = die->attr (DW_AT_rnglists_base);
19364 if (attr != nullptr)
2b0c7f41 19365 cu->rnglists_base = attr->as_unsigned ();
d0ce17d8 19366
7a5f294d
TT
19367 if (any_need_reprocess)
19368 {
19369 for (i = 0; i < abbrev->num_attrs; ++i)
19370 {
19371 if (die->attrs[i].requires_reprocessing_p ())
19372 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19373 }
19374 }
93311388 19375 *diep = die;
93311388
DE
19376 return info_ptr;
19377}
19378
3019eac3
DE
19379/* Read a die and all its attributes.
19380 Set DIEP to point to a newly allocated die with its information,
3e225074 19381 except for its child, sibling, and parent fields. */
3019eac3 19382
d521ce57 19383static const gdb_byte *
3019eac3 19384read_full_die (const struct die_reader_specs *reader,
3e225074 19385 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 19386{
d521ce57 19387 const gdb_byte *result;
bf6af496 19388
3e225074 19389 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 19390
b4f54984 19391 if (dwarf_die_debug)
bf6af496
DE
19392 {
19393 fprintf_unfiltered (gdb_stdlog,
19394 "Read die from %s@0x%x of %s:\n",
96b79293 19395 reader->die_section->get_name (),
bf6af496
DE
19396 (unsigned) (info_ptr - reader->die_section->buffer),
19397 bfd_get_filename (reader->abfd));
b4f54984 19398 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
19399 }
19400
19401 return result;
3019eac3 19402}
433df2d4 19403\f
c906108c 19404
72bf9492
DJ
19405/* Returns nonzero if TAG represents a type that we might generate a partial
19406 symbol for. */
19407
19408static int
d8f62e84 19409is_type_tag_for_partial (int tag, enum language lang)
72bf9492
DJ
19410{
19411 switch (tag)
19412 {
19413#if 0
19414 /* Some types that would be reasonable to generate partial symbols for,
d8f62e84
TT
19415 that we don't at present. Note that normally this does not
19416 matter, mainly because C compilers don't give names to these
19417 types, but instead emit DW_TAG_typedef. */
72bf9492
DJ
19418 case DW_TAG_file_type:
19419 case DW_TAG_ptr_to_member_type:
19420 case DW_TAG_set_type:
19421 case DW_TAG_string_type:
19422 case DW_TAG_subroutine_type:
19423#endif
d8f62e84
TT
19424
19425 /* GNAT may emit an array with a name, but no typedef, so we
19426 need to make a symbol in this case. */
19427 case DW_TAG_array_type:
19428 return lang == language_ada;
19429
72bf9492
DJ
19430 case DW_TAG_base_type:
19431 case DW_TAG_class_type:
680b30c7 19432 case DW_TAG_interface_type:
72bf9492
DJ
19433 case DW_TAG_enumeration_type:
19434 case DW_TAG_structure_type:
19435 case DW_TAG_subrange_type:
19436 case DW_TAG_typedef:
19437 case DW_TAG_union_type:
19438 return 1;
19439 default:
19440 return 0;
19441 }
19442}
19443
19444/* Load all DIEs that are interesting for partial symbols into memory. */
19445
19446static struct partial_die_info *
dee91e82 19447load_partial_dies (const struct die_reader_specs *reader,
d521ce57 19448 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 19449{
dee91e82 19450 struct dwarf2_cu *cu = reader->cu;
5e22e966 19451 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 19452 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 19453 unsigned int bytes_read;
5afb4e99 19454 unsigned int load_all = 0;
72bf9492
DJ
19455 int nesting_level = 1;
19456
19457 parent_die = NULL;
19458 last_die = NULL;
19459
7adf1e79
DE
19460 gdb_assert (cu->per_cu != NULL);
19461 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
19462 load_all = 1;
19463
72bf9492
DJ
19464 cu->partial_dies
19465 = htab_create_alloc_ex (cu->header.length / 12,
19466 partial_die_hash,
19467 partial_die_eq,
19468 NULL,
19469 &cu->comp_unit_obstack,
19470 hashtab_obstack_allocate,
19471 dummy_obstack_deallocate);
19472
72bf9492
DJ
19473 while (1)
19474 {
685af9cd 19475 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
19476
19477 /* A NULL abbrev means the end of a series of children. */
19478 if (abbrev == NULL)
19479 {
19480 if (--nesting_level == 0)
cd9983dd
YQ
19481 return first_die;
19482
72bf9492
DJ
19483 info_ptr += bytes_read;
19484 last_die = parent_die;
19485 parent_die = parent_die->die_parent;
19486 continue;
19487 }
19488
98bfdba5
PA
19489 /* Check for template arguments. We never save these; if
19490 they're seen, we just mark the parent, and go on our way. */
19491 if (parent_die != NULL
19492 && cu->language == language_cplus
19493 && (abbrev->tag == DW_TAG_template_type_param
19494 || abbrev->tag == DW_TAG_template_value_param))
19495 {
19496 parent_die->has_template_arguments = 1;
19497
19498 if (!load_all)
19499 {
19500 /* We don't need a partial DIE for the template argument. */
dee91e82 19501 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19502 continue;
19503 }
19504 }
19505
0d99eb77 19506 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
19507 Skip their other children. */
19508 if (!load_all
19509 && cu->language == language_cplus
19510 && parent_die != NULL
f9b5d5ea
TV
19511 && parent_die->tag == DW_TAG_subprogram
19512 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 19513 {
dee91e82 19514 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19515 continue;
19516 }
19517
5afb4e99
DJ
19518 /* Check whether this DIE is interesting enough to save. Normally
19519 we would not be interested in members here, but there may be
19520 later variables referencing them via DW_AT_specification (for
19521 static members). */
19522 if (!load_all
d8f62e84 19523 && !is_type_tag_for_partial (abbrev->tag, cu->language)
72929c62 19524 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
19525 && abbrev->tag != DW_TAG_enumerator
19526 && abbrev->tag != DW_TAG_subprogram
b1dc1806 19527 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 19528 && abbrev->tag != DW_TAG_lexical_block
72bf9492 19529 && abbrev->tag != DW_TAG_variable
5afb4e99 19530 && abbrev->tag != DW_TAG_namespace
f55ee35c 19531 && abbrev->tag != DW_TAG_module
95554aad 19532 && abbrev->tag != DW_TAG_member
74921315
KS
19533 && abbrev->tag != DW_TAG_imported_unit
19534 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
19535 {
19536 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19537 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
19538 continue;
19539 }
19540
6f06d47b
YQ
19541 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19542 abbrev);
cd9983dd 19543
48fbe735 19544 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
19545
19546 /* This two-pass algorithm for processing partial symbols has a
19547 high cost in cache pressure. Thus, handle some simple cases
19548 here which cover the majority of C partial symbols. DIEs
19549 which neither have specification tags in them, nor could have
19550 specification tags elsewhere pointing at them, can simply be
19551 processed and discarded.
19552
19553 This segment is also optional; scan_partial_symbols and
19554 add_partial_symbol will handle these DIEs if we chain
19555 them in normally. When compilers which do not emit large
19556 quantities of duplicate debug information are more common,
19557 this code can probably be removed. */
19558
19559 /* Any complete simple types at the top level (pretty much all
19560 of them, for a language without namespaces), can be processed
19561 directly. */
19562 if (parent_die == NULL
cd9983dd
YQ
19563 && pdi.has_specification == 0
19564 && pdi.is_declaration == 0
19565 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19566 || pdi.tag == DW_TAG_base_type
d8f62e84 19567 || pdi.tag == DW_TAG_array_type
cd9983dd 19568 || pdi.tag == DW_TAG_subrange_type))
72bf9492 19569 {
7d00ffec 19570 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
19571 add_partial_symbol (&pdi, cu);
19572
cd9983dd 19573 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19574 continue;
19575 }
19576
d8228535
JK
19577 /* The exception for DW_TAG_typedef with has_children above is
19578 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 19579 type_name_or_error will error on such types later.
d8228535
JK
19580
19581 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19582 it could not find the child DIEs referenced later, this is checked
19583 above. In correct DWARF DW_TAG_typedef should have no children. */
19584
cd9983dd 19585 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 19586 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 19587 "- DIE at %s [in module %s]"),
cd9983dd 19588 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 19589
72bf9492
DJ
19590 /* If we're at the second level, and we're an enumerator, and
19591 our parent has no specification (meaning possibly lives in a
19592 namespace elsewhere), then we can add the partial symbol now
19593 instead of queueing it. */
cd9983dd 19594 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
19595 && parent_die != NULL
19596 && parent_die->die_parent == NULL
19597 && parent_die->tag == DW_TAG_enumeration_type
19598 && parent_die->has_specification == 0)
19599 {
7d00ffec 19600 if (pdi.raw_name == NULL)
b98664d3 19601 complaint (_("malformed enumerator DIE ignored"));
72bf9492 19602 else if (building_psymtab)
f0fbb768 19603 add_partial_symbol (&pdi, cu);
72bf9492 19604
cd9983dd 19605 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19606 continue;
19607 }
19608
cd9983dd 19609 struct partial_die_info *part_die
6f06d47b 19610 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 19611
72bf9492
DJ
19612 /* We'll save this DIE so link it in. */
19613 part_die->die_parent = parent_die;
19614 part_die->die_sibling = NULL;
19615 part_die->die_child = NULL;
19616
19617 if (last_die && last_die == parent_die)
19618 last_die->die_child = part_die;
19619 else if (last_die)
19620 last_die->die_sibling = part_die;
19621
19622 last_die = part_die;
19623
19624 if (first_die == NULL)
19625 first_die = part_die;
19626
19627 /* Maybe add the DIE to the hash table. Not all DIEs that we
19628 find interesting need to be in the hash table, because we
19629 also have the parent/sibling/child chains; only those that we
19630 might refer to by offset later during partial symbol reading.
19631
19632 For now this means things that might have be the target of a
19633 DW_AT_specification, DW_AT_abstract_origin, or
19634 DW_AT_extension. DW_AT_extension will refer only to
19635 namespaces; DW_AT_abstract_origin refers to functions (and
19636 many things under the function DIE, but we do not recurse
19637 into function DIEs during partial symbol reading) and
19638 possibly variables as well; DW_AT_specification refers to
19639 declarations. Declarations ought to have the DW_AT_declaration
19640 flag. It happens that GCC forgets to put it in sometimes, but
19641 only for functions, not for types.
19642
19643 Adding more things than necessary to the hash table is harmless
19644 except for the performance cost. Adding too few will result in
5afb4e99
DJ
19645 wasted time in find_partial_die, when we reread the compilation
19646 unit with load_all_dies set. */
72bf9492 19647
5afb4e99 19648 if (load_all
72929c62 19649 || abbrev->tag == DW_TAG_constant
5afb4e99 19650 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
19651 || abbrev->tag == DW_TAG_variable
19652 || abbrev->tag == DW_TAG_namespace
19653 || part_die->is_declaration)
19654 {
19655 void **slot;
19656
19657 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
19658 to_underlying (part_die->sect_off),
19659 INSERT);
72bf9492
DJ
19660 *slot = part_die;
19661 }
19662
72bf9492 19663 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 19664 we have no reason to follow the children of structures; for other
98bfdba5
PA
19665 languages we have to, so that we can get at method physnames
19666 to infer fully qualified class names, for DW_AT_specification,
19667 and for C++ template arguments. For C++, we also look one level
19668 inside functions to find template arguments (if the name of the
19669 function does not already contain the template arguments).
bc30ff58 19670
0a4b0913
AB
19671 For Ada and Fortran, we need to scan the children of subprograms
19672 and lexical blocks as well because these languages allow the
19673 definition of nested entities that could be interesting for the
19674 debugger, such as nested subprograms for instance. */
72bf9492 19675 if (last_die->has_children
5afb4e99
DJ
19676 && (load_all
19677 || last_die->tag == DW_TAG_namespace
f55ee35c 19678 || last_die->tag == DW_TAG_module
72bf9492 19679 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
19680 || (cu->language == language_cplus
19681 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
19682 && (last_die->raw_name == NULL
19683 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
19684 || (cu->language != language_c
19685 && (last_die->tag == DW_TAG_class_type
680b30c7 19686 || last_die->tag == DW_TAG_interface_type
72bf9492 19687 || last_die->tag == DW_TAG_structure_type
bc30ff58 19688 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
19689 || ((cu->language == language_ada
19690 || cu->language == language_fortran)
bc30ff58
JB
19691 && (last_die->tag == DW_TAG_subprogram
19692 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
19693 {
19694 nesting_level++;
19695 parent_die = last_die;
19696 continue;
19697 }
19698
19699 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19700 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
19701
19702 /* Back to the top, do it again. */
19703 }
19704}
19705
6f06d47b
YQ
19706partial_die_info::partial_die_info (sect_offset sect_off_,
19707 struct abbrev_info *abbrev)
19708 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19709{
19710}
19711
7d00ffec
TT
19712/* See class definition. */
19713
19714const char *
19715partial_die_info::name (dwarf2_cu *cu)
19716{
19717 if (!canonical_name && raw_name != nullptr)
19718 {
19719 struct objfile *objfile = cu->per_objfile->objfile;
19720 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19721 canonical_name = 1;
19722 }
19723
19724 return raw_name;
19725}
19726
35cc7ed7
YQ
19727/* Read a minimal amount of information into the minimal die structure.
19728 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 19729
48fbe735
YQ
19730const gdb_byte *
19731partial_die_info::read (const struct die_reader_specs *reader,
19732 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 19733{
dee91e82 19734 struct dwarf2_cu *cu = reader->cu;
976ca316 19735 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 19736 unsigned int i;
c5aa993b 19737 int has_low_pc_attr = 0;
c906108c 19738 int has_high_pc_attr = 0;
91da1414 19739 int high_pc_relative = 0;
c906108c 19740
fd0a254f 19741 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 19742 {
e7da7f8f 19743 attribute attr;
7a5f294d 19744 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18a8505e 19745 /* String and address offsets that need to do the reprocessing have
dda83cd7 19746 already been read at this point, so there is no need to wait until
18a8505e 19747 the loop terminates to do the reprocessing. */
7a5f294d 19748 if (attr.requires_reprocessing_p ())
d0ce17d8 19749 read_attribute_reprocess (reader, &attr, tag);
c906108c 19750 /* Store the data if it is of an attribute we want to keep in a
dda83cd7 19751 partial symbol table. */
c906108c
SS
19752 switch (attr.name)
19753 {
19754 case DW_AT_name:
48fbe735 19755 switch (tag)
71c25dea
TT
19756 {
19757 case DW_TAG_compile_unit:
95554aad 19758 case DW_TAG_partial_unit:
348e048f 19759 case DW_TAG_type_unit:
71c25dea
TT
19760 /* Compilation units have a DW_AT_name that is a filename, not
19761 a source language identifier. */
19762 case DW_TAG_enumeration_type:
19763 case DW_TAG_enumerator:
19764 /* These tags always have simple identifiers already; no need
19765 to canonicalize them. */
7d00ffec 19766 canonical_name = 1;
2c830f54 19767 raw_name = attr.as_string ();
71c25dea
TT
19768 break;
19769 default:
7d00ffec 19770 canonical_name = 0;
2c830f54 19771 raw_name = attr.as_string ();
71c25dea
TT
19772 break;
19773 }
c906108c 19774 break;
31ef98ae 19775 case DW_AT_linkage_name:
c906108c 19776 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19777 /* Note that both forms of linkage name might appear. We
19778 assume they will be the same, and we only store the last
19779 one we see. */
95f982e5 19780 linkage_name = attr.as_string ();
c906108c
SS
19781 break;
19782 case DW_AT_low_pc:
19783 has_low_pc_attr = 1;
95f982e5 19784 lowpc = attr.as_address ();
c906108c
SS
19785 break;
19786 case DW_AT_high_pc:
19787 has_high_pc_attr = 1;
95f982e5 19788 highpc = attr.as_address ();
cd6c91b4 19789 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 19790 high_pc_relative = 1;
c906108c
SS
19791 break;
19792 case DW_AT_location:
dda83cd7
SM
19793 /* Support the .debug_loc offsets. */
19794 if (attr.form_is_block ())
19795 {
9d2246fc 19796 d.locdesc = attr.as_block ();
dda83cd7
SM
19797 }
19798 else if (attr.form_is_section_offset ())
19799 {
4d3c2250 19800 dwarf2_complex_location_expr_complaint ();
dda83cd7
SM
19801 }
19802 else
19803 {
4d3c2250
KB
19804 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19805 "partial symbol information");
dda83cd7 19806 }
c906108c 19807 break;
c906108c 19808 case DW_AT_external:
c45bc3f8 19809 is_external = attr.as_boolean ();
c906108c
SS
19810 break;
19811 case DW_AT_declaration:
c45bc3f8 19812 is_declaration = attr.as_boolean ();
c906108c
SS
19813 break;
19814 case DW_AT_type:
48fbe735 19815 has_type = 1;
c906108c
SS
19816 break;
19817 case DW_AT_abstract_origin:
19818 case DW_AT_specification:
72bf9492 19819 case DW_AT_extension:
48fbe735 19820 has_specification = 1;
0826b30a 19821 spec_offset = attr.get_ref_die_offset ();
48fbe735 19822 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19823 || cu->per_cu->is_dwz);
c906108c
SS
19824 break;
19825 case DW_AT_sibling:
19826 /* Ignore absolute siblings, they might point outside of
19827 the current compile unit. */
19828 if (attr.form == DW_FORM_ref_addr)
b98664d3 19829 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19830 else
b9502d3f 19831 {
48fbe735 19832 const gdb_byte *buffer = reader->buffer;
0826b30a 19833 sect_offset off = attr.get_ref_die_offset ();
9c541725 19834 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19835
19836 if (sibling_ptr < info_ptr)
b98664d3 19837 complaint (_("DW_AT_sibling points backwards"));
22869d73 19838 else if (sibling_ptr > reader->buffer_end)
a0194fa8 19839 reader->die_section->overflow_complaint ();
b9502d3f 19840 else
48fbe735 19841 sibling = sibling_ptr;
b9502d3f 19842 }
c906108c 19843 break;
dda83cd7
SM
19844 case DW_AT_byte_size:
19845 has_byte_size = 1;
19846 break;
19847 case DW_AT_const_value:
19848 has_const_value = 1;
19849 break;
68511cec
CES
19850 case DW_AT_calling_convention:
19851 /* DWARF doesn't provide a way to identify a program's source-level
19852 entry point. DW_AT_calling_convention attributes are only meant
19853 to describe functions' calling conventions.
19854
19855 However, because it's a necessary piece of information in
0c1b455e
TT
19856 Fortran, and before DWARF 4 DW_CC_program was the only
19857 piece of debugging information whose definition refers to
19858 a 'main program' at all, several compilers marked Fortran
19859 main programs with DW_CC_program --- even when those
19860 functions use the standard calling conventions.
19861
19862 Although DWARF now specifies a way to provide this
19863 information, we support this practice for backward
19864 compatibility. */
529908cb 19865 if (attr.constant_value (0) == DW_CC_program
0c1b455e 19866 && cu->language == language_fortran)
48fbe735 19867 main_subprogram = 1;
68511cec 19868 break;
481860b3 19869 case DW_AT_inline:
529908cb
TT
19870 {
19871 LONGEST value = attr.constant_value (-1);
19872 if (value == DW_INL_inlined
19873 || value == DW_INL_declared_inlined)
19874 may_be_inlined = 1;
19875 }
481860b3 19876 break;
95554aad
TT
19877
19878 case DW_AT_import:
48fbe735 19879 if (tag == DW_TAG_imported_unit)
36586728 19880 {
0826b30a 19881 d.sect_off = attr.get_ref_die_offset ();
48fbe735 19882 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19883 || cu->per_cu->is_dwz);
19884 }
95554aad
TT
19885 break;
19886
0c1b455e 19887 case DW_AT_main_subprogram:
c45bc3f8 19888 main_subprogram = attr.as_boolean ();
0c1b455e
TT
19889 break;
19890
05caa1d2
TT
19891 case DW_AT_ranges:
19892 {
2b0c7f41
SM
19893 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19894 on DWARF version). */
19895 ULONGEST ranges_offset = attr.as_unsigned ();
19896
19897 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19898 this value. */
19899 if (tag != DW_TAG_compile_unit)
19900 ranges_offset += cu->gnu_ranges_base;
19901
05caa1d2 19902 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
d0ce17d8 19903 nullptr, tag))
05caa1d2
TT
19904 has_pc_info = 1;
19905 }
19906 break;
19907
c906108c
SS
19908 default:
19909 break;
19910 }
19911 }
19912
10d06d82
TT
19913 /* For Ada, if both the name and the linkage name appear, we prefer
19914 the latter. This lets "catch exception" work better, regardless
19915 of the order in which the name and linkage name were emitted.
19916 Really, though, this is just a workaround for the fact that gdb
19917 doesn't store both the name and the linkage name. */
19918 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 19919 raw_name = linkage_name;
10d06d82 19920
91da1414 19921 if (high_pc_relative)
48fbe735 19922 highpc += lowpc;
91da1414 19923
9373cf26
JK
19924 if (has_low_pc_attr && has_high_pc_attr)
19925 {
19926 /* When using the GNU linker, .gnu.linkonce. sections are used to
19927 eliminate duplicate copies of functions and vtables and such.
19928 The linker will arbitrarily choose one and discard the others.
19929 The AT_*_pc values for such functions refer to local labels in
19930 these sections. If the section from that file was discarded, the
19931 labels are not in the output, so the relocs get a value of 0.
19932 If this is a discarded function, mark the pc bounds as invalid,
19933 so that GDB will ignore it. */
976ca316 19934 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 19935 {
976ca316 19936 struct objfile *objfile = per_objfile->objfile;
08feed99 19937 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19938
b98664d3 19939 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19940 "for DIE at %s [in module %s]"),
48fbe735
YQ
19941 paddress (gdbarch, lowpc),
19942 sect_offset_str (sect_off),
9d8780f0 19943 objfile_name (objfile));
9373cf26
JK
19944 }
19945 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19946 else if (lowpc >= highpc)
9373cf26 19947 {
976ca316 19948 struct objfile *objfile = per_objfile->objfile;
08feed99 19949 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19950
b98664d3 19951 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19952 "for DIE at %s [in module %s]"),
48fbe735
YQ
19953 paddress (gdbarch, lowpc),
19954 paddress (gdbarch, highpc),
19955 sect_offset_str (sect_off),
9c541725 19956 objfile_name (objfile));
9373cf26
JK
19957 }
19958 else
48fbe735 19959 has_pc_info = 1;
9373cf26 19960 }
85cbf3d3 19961
c906108c
SS
19962 return info_ptr;
19963}
19964
72bf9492
DJ
19965/* Find a cached partial DIE at OFFSET in CU. */
19966
d590ff25
YQ
19967struct partial_die_info *
19968dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19969{
19970 struct partial_die_info *lookup_die = NULL;
6f06d47b 19971 struct partial_die_info part_die (sect_off);
72bf9492 19972
9a3c8263 19973 lookup_die = ((struct partial_die_info *)
d590ff25 19974 htab_find_with_hash (partial_dies, &part_die,
9c541725 19975 to_underlying (sect_off)));
72bf9492 19976
72bf9492
DJ
19977 return lookup_die;
19978}
19979
348e048f
DE
19980/* Find a partial DIE at OFFSET, which may or may not be in CU,
19981 except in the case of .debug_types DIEs which do not reference
19982 outside their CU (they do however referencing other types via
55f1336d 19983 DW_FORM_ref_sig8). */
72bf9492 19984
122cf0f2 19985static const struct cu_partial_die_info
9c541725 19986find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19987{
976ca316
SM
19988 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19989 struct objfile *objfile = per_objfile->objfile;
5afb4e99 19990 struct partial_die_info *pd = NULL;
72bf9492 19991
36586728 19992 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 19993 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 19994 {
d590ff25 19995 pd = cu->find_partial_die (sect_off);
5afb4e99 19996 if (pd != NULL)
fb816e8b 19997 return { cu, pd };
0d99eb77
DE
19998 /* We missed recording what we needed.
19999 Load all dies and try again. */
5afb4e99 20000 }
0d99eb77
DE
20001 else
20002 {
20003 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 20004 if (cu->per_cu->is_debug_types)
0d99eb77 20005 {
9d8780f0
SM
20006 error (_("Dwarf Error: Type Unit at offset %s contains"
20007 " external reference to offset %s [in module %s].\n"),
20008 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
20009 bfd_get_filename (objfile->obfd));
20010 }
7188ed02
SM
20011 dwarf2_per_cu_data *per_cu
20012 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 20013 per_objfile);
72bf9492 20014
976ca316 20015 cu = per_objfile->get_cu (per_cu);
7188ed02 20016 if (cu == NULL || cu->partial_dies == NULL)
976ca316 20017 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 20018
976ca316 20019 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
20020
20021 cu->last_used = 0;
20022 pd = cu->find_partial_die (sect_off);
0d99eb77 20023 }
5afb4e99 20024
dee91e82
DE
20025 /* If we didn't find it, and not all dies have been loaded,
20026 load them all and try again. */
20027
7188ed02 20028 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 20029 {
7188ed02 20030 cu->per_cu->load_all_dies = 1;
fd820528
DE
20031
20032 /* This is nasty. When we reread the DIEs, somewhere up the call chain
20033 THIS_CU->cu may already be in use. So we can't just free it and
20034 replace its DIEs with the ones we read in. Instead, we leave those
20035 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
20036 and clobber THIS_CU->cu->partial_dies with the hash table for the new
20037 set. */
976ca316 20038 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 20039
7188ed02 20040 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
20041 }
20042
20043 if (pd == NULL)
521894aa 20044 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
9d8780f0 20045 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 20046 return { cu, pd };
72bf9492
DJ
20047}
20048
abc72ce4
DE
20049/* See if we can figure out if the class lives in a namespace. We do
20050 this by looking for a member function; its demangled name will
20051 contain namespace info, if there is any. */
20052
20053static void
20054guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
20055 struct dwarf2_cu *cu)
20056{
20057 /* NOTE: carlton/2003-10-07: Getting the info this way changes
20058 what template types look like, because the demangler
20059 frequently doesn't give the same name as the debug info. We
20060 could fix this by only using the demangled name to get the
20061 prefix (but see comment in read_structure_type). */
20062
20063 struct partial_die_info *real_pdi;
20064 struct partial_die_info *child_pdi;
20065
20066 /* If this DIE (this DIE's specification, if any) has a parent, then
20067 we should not do this. We'll prepend the parent's fully qualified
20068 name when we create the partial symbol. */
20069
20070 real_pdi = struct_pdi;
20071 while (real_pdi->has_specification)
fb816e8b 20072 {
122cf0f2
AB
20073 auto res = find_partial_die (real_pdi->spec_offset,
20074 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
20075 real_pdi = res.pdi;
20076 cu = res.cu;
20077 }
abc72ce4
DE
20078
20079 if (real_pdi->die_parent != NULL)
20080 return;
20081
20082 for (child_pdi = struct_pdi->die_child;
20083 child_pdi != NULL;
20084 child_pdi = child_pdi->die_sibling)
20085 {
20086 if (child_pdi->tag == DW_TAG_subprogram
20087 && child_pdi->linkage_name != NULL)
20088 {
43816ebc 20089 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
20090 (cu->language_defn->class_name_from_physname
20091 (child_pdi->linkage_name));
abc72ce4
DE
20092 if (actual_class_name != NULL)
20093 {
5e22e966 20094 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
20095 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
20096 struct_pdi->canonical_name = 1;
abc72ce4
DE
20097 }
20098 break;
20099 }
20100 }
20101}
20102
25c11aca
TV
20103/* Return true if a DIE with TAG may have the DW_AT_const_value
20104 attribute. */
20105
20106static bool
20107can_have_DW_AT_const_value_p (enum dwarf_tag tag)
20108{
20109 switch (tag)
20110 {
20111 case DW_TAG_constant:
20112 case DW_TAG_enumerator:
20113 case DW_TAG_formal_parameter:
20114 case DW_TAG_template_value_param:
20115 case DW_TAG_variable:
20116 return true;
20117 }
20118
20119 return false;
20120}
20121
52356b79
YQ
20122void
20123partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 20124{
abc72ce4
DE
20125 /* Once we've fixed up a die, there's no point in doing so again.
20126 This also avoids a memory leak if we were to call
20127 guess_partial_die_structure_name multiple times. */
52356b79 20128 if (fixup_called)
abc72ce4
DE
20129 return;
20130
72bf9492
DJ
20131 /* If we found a reference attribute and the DIE has no name, try
20132 to find a name in the referred to DIE. */
20133
7d00ffec 20134 if (raw_name == NULL && has_specification)
72bf9492
DJ
20135 {
20136 struct partial_die_info *spec_die;
72bf9492 20137
122cf0f2 20138 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
20139 spec_die = res.pdi;
20140 cu = res.cu;
72bf9492 20141
52356b79 20142 spec_die->fixup (cu);
72bf9492 20143
7d00ffec 20144 if (spec_die->raw_name)
72bf9492 20145 {
7d00ffec
TT
20146 raw_name = spec_die->raw_name;
20147 canonical_name = spec_die->canonical_name;
72bf9492
DJ
20148
20149 /* Copy DW_AT_external attribute if it is set. */
20150 if (spec_die->is_external)
52356b79 20151 is_external = spec_die->is_external;
72bf9492
DJ
20152 }
20153 }
20154
25c11aca
TV
20155 if (!has_const_value && has_specification
20156 && can_have_DW_AT_const_value_p (tag))
20157 {
20158 struct partial_die_info *spec_die;
20159
20160 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20161 spec_die = res.pdi;
20162 cu = res.cu;
20163
20164 spec_die->fixup (cu);
20165
20166 if (spec_die->has_const_value)
20167 {
20168 /* Copy DW_AT_const_value attribute if it is set. */
20169 has_const_value = spec_die->has_const_value;
20170 }
20171 }
20172
72bf9492 20173 /* Set default names for some unnamed DIEs. */
72bf9492 20174
7d00ffec
TT
20175 if (raw_name == NULL && tag == DW_TAG_namespace)
20176 {
20177 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20178 canonical_name = 1;
20179 }
72bf9492 20180
abc72ce4
DE
20181 /* If there is no parent die to provide a namespace, and there are
20182 children, see if we can determine the namespace from their linkage
122d1940 20183 name. */
abc72ce4 20184 if (cu->language == language_cplus
5e22e966 20185 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
20186 && die_parent == NULL
20187 && has_children
20188 && (tag == DW_TAG_class_type
20189 || tag == DW_TAG_structure_type
20190 || tag == DW_TAG_union_type))
20191 guess_partial_die_structure_name (this, cu);
abc72ce4 20192
53832f31
TT
20193 /* GCC might emit a nameless struct or union that has a linkage
20194 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 20195 if (raw_name == NULL
52356b79
YQ
20196 && (tag == DW_TAG_class_type
20197 || tag == DW_TAG_interface_type
20198 || tag == DW_TAG_structure_type
20199 || tag == DW_TAG_union_type)
20200 && linkage_name != NULL)
53832f31 20201 {
43816ebc
TT
20202 gdb::unique_xmalloc_ptr<char> demangled
20203 (gdb_demangle (linkage_name, DMGL_TYPES));
20204 if (demangled != nullptr)
53832f31 20205 {
96408a79
SA
20206 const char *base;
20207
20208 /* Strip any leading namespaces/classes, keep only the base name.
20209 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
20210 base = strrchr (demangled.get (), ':');
20211 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
20212 base++;
20213 else
43816ebc 20214 base = demangled.get ();
96408a79 20215
5e22e966 20216 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
20217 raw_name = objfile->intern (base);
20218 canonical_name = 1;
53832f31
TT
20219 }
20220 }
20221
52356b79 20222 fixup_called = 1;
72bf9492
DJ
20223}
20224
d0ce17d8 20225/* Read the .debug_loclists or .debug_rnglists header (they are the same format)
a1c40103
SM
20226 contents from the given SECTION in the HEADER.
20227
20228 HEADER_OFFSET is the offset of the header in the section. */
41144253 20229static void
d0ce17d8 20230read_loclists_rnglists_header (struct loclists_rnglists_header *header,
a1c40103
SM
20231 struct dwarf2_section_info *section,
20232 sect_offset header_offset)
41144253 20233{
20234 unsigned int bytes_read;
20235 bfd *abfd = section->get_bfd_owner ();
a1c40103
SM
20236 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20237
41144253 20238 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20239 info_ptr += bytes_read;
a1c40103 20240
41144253 20241 header->version = read_2_bytes (abfd, info_ptr);
20242 info_ptr += 2;
a1c40103 20243
41144253 20244 header->addr_size = read_1_byte (abfd, info_ptr);
20245 info_ptr += 1;
a1c40103 20246
41144253 20247 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20248 info_ptr += 1;
a1c40103 20249
41144253 20250 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20251}
20252
20253/* Return the DW_AT_loclists_base value for the CU. */
20254static ULONGEST
20255lookup_loclist_base (struct dwarf2_cu *cu)
20256{
20257 /* For the .dwo unit, the loclist_base points to the first offset following
20258 the header. The header consists of the following entities-
20259 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20260 bit format)
20261 2. version (2 bytes)
20262 3. address size (1 byte)
20263 4. segment selector size (1 byte)
20264 5. offset entry count (4 bytes)
20265 These sizes are derived as per the DWARFv5 standard. */
20266 if (cu->dwo_unit != nullptr)
20267 {
20268 if (cu->header.initial_length_size == 4)
20269 return LOCLIST_HEADER_SIZE32;
20270 return LOCLIST_HEADER_SIZE64;
20271 }
20272 return cu->loclist_base;
20273}
20274
20275/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20276 array of offsets in the .debug_loclists section. */
e57933dc
SM
20277
20278static sect_offset
41144253 20279read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20280{
976ca316
SM
20281 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20282 struct objfile *objfile = per_objfile->objfile;
41144253 20283 bfd *abfd = objfile->obfd;
a1c40103
SM
20284 ULONGEST loclist_header_size =
20285 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20286 : LOCLIST_HEADER_SIZE64);
41144253 20287 ULONGEST loclist_base = lookup_loclist_base (cu);
05787bad
SM
20288
20289 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20290 ULONGEST start_offset =
20291 loclist_base + loclist_index * cu->header.offset_size;
20292
a1c40103 20293 /* Get loclists section. */
41144253 20294 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20295
a1c40103 20296 /* Read the loclists section content. */
41144253 20297 section->read (objfile);
20298 if (section->buffer == NULL)
a0c1eeba
SM
20299 error (_("DW_FORM_loclistx used without .debug_loclists "
20300 "section [in module %s]"), objfile_name (objfile));
20301
a1c40103
SM
20302 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20303 so if loclist_base is smaller than the header size, we have a problem. */
20304 if (loclist_base < loclist_header_size)
20305 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20306 objfile_name (objfile));
20307
20308 /* Read the header of the loclists contribution. */
d0ce17d8 20309 struct loclists_rnglists_header header;
a1c40103
SM
20310 read_loclists_rnglists_header (&header, section,
20311 (sect_offset) (loclist_base - loclist_header_size));
20312
20313 /* Verify the loclist index is valid. */
41144253 20314 if (loclist_index >= header.offset_entry_count)
a0c1eeba
SM
20315 error (_("DW_FORM_loclistx pointing outside of "
20316 ".debug_loclists offset array [in module %s]"),
20317 objfile_name (objfile));
20318
05787bad
SM
20319 /* Validate that reading won't go beyond the end of the section. */
20320 if (start_offset + cu->header.offset_size > section->size)
20321 error (_("Reading DW_FORM_loclistx index beyond end of"
20322 ".debug_loclists section [in module %s]"),
20323 objfile_name (objfile));
20324
20325 const gdb_byte *info_ptr = section->buffer + start_offset;
41144253 20326
20327 if (cu->header.offset_size == 4)
e57933dc 20328 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
41144253 20329 else
e57933dc 20330 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
41144253 20331}
20332
d0ce17d8
CT
20333/* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20334 array of offsets in the .debug_rnglists section. */
e57933dc
SM
20335
20336static sect_offset
d0ce17d8
CT
20337read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20338 dwarf_tag tag)
20339{
20340 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20341 struct objfile *objfile = dwarf2_per_objfile->objfile;
20342 bfd *abfd = objfile->obfd;
20343 ULONGEST rnglist_header_size =
20344 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20345 : RNGLIST_HEADER_SIZE64);
2b0c7f41
SM
20346
20347 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20348 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20349 doesn't apply. */
d0ce17d8 20350 ULONGEST rnglist_base =
2b0c7f41 20351 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
5e4d9bbc
SM
20352
20353 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
d0ce17d8
CT
20354 ULONGEST start_offset =
20355 rnglist_base + rnglist_index * cu->header.offset_size;
20356
20357 /* Get rnglists section. */
20358 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20359
20360 /* Read the rnglists section content. */
20361 section->read (objfile);
20362 if (section->buffer == nullptr)
20363 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20364 "[in module %s]"),
20365 objfile_name (objfile));
20366
a1c40103
SM
20367 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20368 so if rnglist_base is smaller than the header size, we have a problem. */
20369 if (rnglist_base < rnglist_header_size)
20370 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20371 objfile_name (objfile));
20372
20373 /* Read the header of the rnglists contribution. */
d0ce17d8 20374 struct loclists_rnglists_header header;
a1c40103
SM
20375 read_loclists_rnglists_header (&header, section,
20376 (sect_offset) (rnglist_base - rnglist_header_size));
20377
20378 /* Verify the rnglist index is valid. */
d0ce17d8
CT
20379 if (rnglist_index >= header.offset_entry_count)
20380 error (_("DW_FORM_rnglistx index pointing outside of "
20381 ".debug_rnglists offset array [in module %s]"),
20382 objfile_name (objfile));
20383
d0ce17d8 20384 /* Validate that reading won't go beyond the end of the section. */
5e4d9bbc 20385 if (start_offset + cu->header.offset_size > section->size)
d0ce17d8
CT
20386 error (_("Reading DW_FORM_rnglistx index beyond end of"
20387 ".debug_rnglists section [in module %s]"),
20388 objfile_name (objfile));
20389
20390 const gdb_byte *info_ptr = section->buffer + start_offset;
20391
20392 if (cu->header.offset_size == 4)
e57933dc 20393 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8 20394 else
e57933dc 20395 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8
CT
20396}
20397
18a8505e
AT
20398/* Process the attributes that had to be skipped in the first round. These
20399 attributes are the ones that need str_offsets_base or addr_base attributes.
20400 They could not have been processed in the first round, because at the time
20401 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
20402static void
20403read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 20404 struct attribute *attr, dwarf_tag tag)
18a8505e
AT
20405{
20406 struct dwarf2_cu *cu = reader->cu;
20407 switch (attr->form)
20408 {
20409 case DW_FORM_addrx:
20410 case DW_FORM_GNU_addr_index:
36d378cf
TT
20411 attr->set_address (read_addr_index (cu,
20412 attr->as_unsigned_reprocess ()));
dda83cd7 20413 break;
41144253 20414 case DW_FORM_loclistx:
b1829e1b 20415 {
e57933dc 20416 sect_offset loclists_sect_off
b1829e1b
SM
20417 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20418
e57933dc 20419 attr->set_unsigned (to_underlying (loclists_sect_off));
b1829e1b
SM
20420 }
20421 break;
d0ce17d8 20422 case DW_FORM_rnglistx:
b1829e1b 20423 {
e57933dc 20424 sect_offset rnglists_sect_off
b1829e1b
SM
20425 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20426
e57933dc 20427 attr->set_unsigned (to_underlying (rnglists_sect_off));
b1829e1b 20428 }
dda83cd7 20429 break;
18a8505e
AT
20430 case DW_FORM_strx:
20431 case DW_FORM_strx1:
20432 case DW_FORM_strx2:
20433 case DW_FORM_strx3:
20434 case DW_FORM_strx4:
20435 case DW_FORM_GNU_str_index:
20436 {
fe56917a 20437 unsigned int str_index = attr->as_unsigned_reprocess ();
c6481205 20438 gdb_assert (!attr->canonical_string_p ());
18a8505e 20439 if (reader->dwo_file != NULL)
c6481205
TT
20440 attr->set_string_noncanonical (read_dwo_str_index (reader,
20441 str_index));
18a8505e 20442 else
c6481205
TT
20443 attr->set_string_noncanonical (read_stub_str_index (cu,
20444 str_index));
18a8505e
AT
20445 break;
20446 }
20447 default:
20448 gdb_assert_not_reached (_("Unexpected DWARF form."));
20449 }
20450}
20451
a8329558 20452/* Read an attribute value described by an attribute form. */
c906108c 20453
d521ce57 20454static const gdb_byte *
dee91e82
DE
20455read_attribute_value (const struct die_reader_specs *reader,
20456 struct attribute *attr, unsigned form,
7a5f294d 20457 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 20458{
dee91e82 20459 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
20460 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20461 struct objfile *objfile = per_objfile->objfile;
dee91e82 20462 bfd *abfd = reader->abfd;
e7c27a73 20463 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
20464 unsigned int bytes_read;
20465 struct dwarf_block *blk;
20466
aead7601 20467 attr->form = (enum dwarf_form) form;
a8329558 20468 switch (form)
c906108c 20469 {
c906108c 20470 case DW_FORM_ref_addr:
ae411497 20471 if (cu->header.version == 2)
414ad644
TT
20472 attr->set_unsigned (cu->header.read_address (abfd, info_ptr,
20473 &bytes_read));
ae411497 20474 else
414ad644
TT
20475 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20476 &bytes_read));
ae411497
TT
20477 info_ptr += bytes_read;
20478 break;
36586728 20479 case DW_FORM_GNU_ref_alt:
414ad644
TT
20480 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20481 &bytes_read));
36586728
TT
20482 info_ptr += bytes_read;
20483 break;
ae411497 20484 case DW_FORM_addr:
08feed99
TT
20485 {
20486 struct gdbarch *gdbarch = objfile->arch ();
36d378cf
TT
20487 CORE_ADDR addr = cu->header.read_address (abfd, info_ptr, &bytes_read);
20488 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20489 attr->set_address (addr);
08feed99
TT
20490 info_ptr += bytes_read;
20491 }
c906108c
SS
20492 break;
20493 case DW_FORM_block2:
7b5a2f43 20494 blk = dwarf_alloc_block (cu);
c906108c
SS
20495 blk->size = read_2_bytes (abfd, info_ptr);
20496 info_ptr += 2;
20497 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20498 info_ptr += blk->size;
9d2246fc 20499 attr->set_block (blk);
c906108c
SS
20500 break;
20501 case DW_FORM_block4:
7b5a2f43 20502 blk = dwarf_alloc_block (cu);
c906108c
SS
20503 blk->size = read_4_bytes (abfd, info_ptr);
20504 info_ptr += 4;
20505 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20506 info_ptr += blk->size;
9d2246fc 20507 attr->set_block (blk);
c906108c
SS
20508 break;
20509 case DW_FORM_data2:
414ad644 20510 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
c906108c
SS
20511 info_ptr += 2;
20512 break;
20513 case DW_FORM_data4:
414ad644 20514 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
c906108c
SS
20515 info_ptr += 4;
20516 break;
20517 case DW_FORM_data8:
414ad644 20518 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
c906108c
SS
20519 info_ptr += 8;
20520 break;
0224619f
JK
20521 case DW_FORM_data16:
20522 blk = dwarf_alloc_block (cu);
20523 blk->size = 16;
20524 blk->data = read_n_bytes (abfd, info_ptr, 16);
20525 info_ptr += 16;
9d2246fc 20526 attr->set_block (blk);
0224619f 20527 break;
2dc7f7b3 20528 case DW_FORM_sec_offset:
414ad644
TT
20529 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20530 &bytes_read));
2dc7f7b3
TT
20531 info_ptr += bytes_read;
20532 break;
41144253 20533 case DW_FORM_loclistx:
20534 {
7a5f294d
TT
20535 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20536 &bytes_read));
20537 info_ptr += bytes_read;
41144253 20538 }
20539 break;
c906108c 20540 case DW_FORM_string:
c6481205
TT
20541 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20542 &bytes_read));
c906108c
SS
20543 info_ptr += bytes_read;
20544 break;
4bdf3d34 20545 case DW_FORM_strp:
36586728
TT
20546 if (!cu->per_cu->is_dwz)
20547 {
c6481205
TT
20548 attr->set_string_noncanonical
20549 (read_indirect_string (per_objfile,
20550 abfd, info_ptr, cu_header,
20551 &bytes_read));
36586728
TT
20552 info_ptr += bytes_read;
20553 break;
20554 }
20555 /* FALLTHROUGH */
43988095
JK
20556 case DW_FORM_line_strp:
20557 if (!cu->per_cu->is_dwz)
20558 {
c6481205
TT
20559 attr->set_string_noncanonical
20560 (per_objfile->read_line_string (info_ptr, cu_header,
20561 &bytes_read));
43988095
JK
20562 info_ptr += bytes_read;
20563 break;
20564 }
20565 /* FALLTHROUGH */
36586728
TT
20566 case DW_FORM_GNU_strp_alt:
20567 {
976ca316 20568 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8266302d
TT
20569 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20570 &bytes_read);
36586728 20571
c6481205
TT
20572 attr->set_string_noncanonical
20573 (dwz->read_string (objfile, str_offset));
36586728
TT
20574 info_ptr += bytes_read;
20575 }
4bdf3d34 20576 break;
2dc7f7b3 20577 case DW_FORM_exprloc:
c906108c 20578 case DW_FORM_block:
7b5a2f43 20579 blk = dwarf_alloc_block (cu);
c906108c
SS
20580 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20581 info_ptr += bytes_read;
20582 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20583 info_ptr += blk->size;
9d2246fc 20584 attr->set_block (blk);
c906108c
SS
20585 break;
20586 case DW_FORM_block1:
7b5a2f43 20587 blk = dwarf_alloc_block (cu);
c906108c
SS
20588 blk->size = read_1_byte (abfd, info_ptr);
20589 info_ptr += 1;
20590 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20591 info_ptr += blk->size;
9d2246fc 20592 attr->set_block (blk);
c906108c
SS
20593 break;
20594 case DW_FORM_data1:
c906108c 20595 case DW_FORM_flag:
414ad644 20596 attr->set_unsigned (read_1_byte (abfd, info_ptr));
c906108c
SS
20597 info_ptr += 1;
20598 break;
2dc7f7b3 20599 case DW_FORM_flag_present:
414ad644 20600 attr->set_unsigned (1);
2dc7f7b3 20601 break;
c906108c 20602 case DW_FORM_sdata:
1bc397c5 20603 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20604 info_ptr += bytes_read;
20605 break;
18a8505e 20606 case DW_FORM_rnglistx:
7a5f294d
TT
20607 {
20608 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20609 &bytes_read));
20610 info_ptr += bytes_read;
20611 }
20612 break;
d0ce17d8 20613 case DW_FORM_udata:
414ad644 20614 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20615 info_ptr += bytes_read;
20616 break;
20617 case DW_FORM_ref1:
414ad644
TT
20618 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20619 + read_1_byte (abfd, info_ptr)));
c906108c
SS
20620 info_ptr += 1;
20621 break;
20622 case DW_FORM_ref2:
414ad644
TT
20623 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20624 + read_2_bytes (abfd, info_ptr)));
c906108c
SS
20625 info_ptr += 2;
20626 break;
20627 case DW_FORM_ref4:
414ad644
TT
20628 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20629 + read_4_bytes (abfd, info_ptr)));
c906108c
SS
20630 info_ptr += 4;
20631 break;
613e1657 20632 case DW_FORM_ref8:
414ad644
TT
20633 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20634 + read_8_bytes (abfd, info_ptr)));
613e1657
KB
20635 info_ptr += 8;
20636 break;
55f1336d 20637 case DW_FORM_ref_sig8:
630ed6b9 20638 attr->set_signature (read_8_bytes (abfd, info_ptr));
348e048f
DE
20639 info_ptr += 8;
20640 break;
c906108c 20641 case DW_FORM_ref_udata:
414ad644
TT
20642 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20643 + read_unsigned_leb128 (abfd, info_ptr,
20644 &bytes_read)));
c906108c
SS
20645 info_ptr += bytes_read;
20646 break;
c906108c 20647 case DW_FORM_indirect:
a8329558
KW
20648 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20649 info_ptr += bytes_read;
43988095
JK
20650 if (form == DW_FORM_implicit_const)
20651 {
20652 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20653 info_ptr += bytes_read;
20654 }
20655 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
7a5f294d 20656 info_ptr);
43988095
JK
20657 break;
20658 case DW_FORM_implicit_const:
1bc397c5 20659 attr->set_signed (implicit_const);
a8329558 20660 break;
336d760d 20661 case DW_FORM_addrx:
3019eac3 20662 case DW_FORM_GNU_addr_index:
fe56917a
TT
20663 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20664 &bytes_read));
3019eac3
DE
20665 info_ptr += bytes_read;
20666 break;
cf532bd1 20667 case DW_FORM_strx:
15f18d14
AT
20668 case DW_FORM_strx1:
20669 case DW_FORM_strx2:
20670 case DW_FORM_strx3:
20671 case DW_FORM_strx4:
3019eac3 20672 case DW_FORM_GNU_str_index:
3019eac3 20673 {
15f18d14
AT
20674 ULONGEST str_index;
20675 if (form == DW_FORM_strx1)
20676 {
20677 str_index = read_1_byte (abfd, info_ptr);
20678 info_ptr += 1;
20679 }
20680 else if (form == DW_FORM_strx2)
20681 {
20682 str_index = read_2_bytes (abfd, info_ptr);
20683 info_ptr += 2;
20684 }
20685 else if (form == DW_FORM_strx3)
20686 {
20687 str_index = read_3_bytes (abfd, info_ptr);
20688 info_ptr += 3;
20689 }
20690 else if (form == DW_FORM_strx4)
20691 {
20692 str_index = read_4_bytes (abfd, info_ptr);
20693 info_ptr += 4;
20694 }
20695 else
20696 {
20697 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20698 info_ptr += bytes_read;
20699 }
fe56917a 20700 attr->set_unsigned_reprocess (str_index);
7a5f294d 20701 }
3019eac3 20702 break;
c906108c 20703 default:
8a3fe4f8 20704 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
20705 dwarf_form_name (form),
20706 bfd_get_filename (abfd));
c906108c 20707 }
28e94949 20708
36586728 20709 /* Super hack. */
cd6c91b4 20710 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
20711 attr->form = DW_FORM_GNU_ref_alt;
20712
28e94949
JB
20713 /* We have seen instances where the compiler tried to emit a byte
20714 size attribute of -1 which ended up being encoded as an unsigned
20715 0xffffffff. Although 0xffffffff is technically a valid size value,
20716 an object of this size seems pretty unlikely so we can relatively
20717 safely treat these cases as if the size attribute was invalid and
20718 treat them as zero by default. */
20719 if (attr->name == DW_AT_byte_size
20720 && form == DW_FORM_data4
529908cb 20721 && attr->as_unsigned () >= 0xffffffff)
01c66ae6
JB
20722 {
20723 complaint
dda83cd7
SM
20724 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20725 hex_string (attr->as_unsigned ()));
414ad644 20726 attr->set_unsigned (0);
01c66ae6 20727 }
28e94949 20728
c906108c
SS
20729 return info_ptr;
20730}
20731
a8329558
KW
20732/* Read an attribute described by an abbreviated attribute. */
20733
d521ce57 20734static const gdb_byte *
dee91e82
DE
20735read_attribute (const struct die_reader_specs *reader,
20736 struct attribute *attr, struct attr_abbrev *abbrev,
7a5f294d 20737 const gdb_byte *info_ptr)
a8329558
KW
20738{
20739 attr->name = abbrev->name;
c6481205 20740 attr->string_is_canonical = 0;
fe56917a 20741 attr->requires_reprocessing = 0;
43988095 20742 return read_attribute_value (reader, attr, abbrev->form,
7a5f294d 20743 abbrev->implicit_const, info_ptr);
a8329558
KW
20744}
20745
43988095
JK
20746/* Return pointer to string at .debug_str offset STR_OFFSET. */
20747
20748static const char *
976ca316 20749read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 20750 LONGEST str_offset)
43988095 20751{
976ca316
SM
20752 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20753 str_offset, "DW_FORM_strp");
c906108c
SS
20754}
20755
43988095
JK
20756/* Return pointer to string at .debug_str offset as read from BUF.
20757 BUF is assumed to be in a compilation unit described by CU_HEADER.
20758 Return *BYTES_READ_PTR count of bytes read from BUF. */
20759
d521ce57 20760static const char *
976ca316 20761read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 20762 const gdb_byte *buf,
cf2c3c16
TT
20763 const struct comp_unit_head *cu_header,
20764 unsigned int *bytes_read_ptr)
20765{
8266302d 20766 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 20767
976ca316 20768 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
20769}
20770
86c0bb4c 20771/* See read.h. */
43988095 20772
86c0bb4c
TT
20773const char *
20774dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
20775 const struct comp_unit_head *cu_header,
20776 unsigned int *bytes_read_ptr)
43988095 20777{
86c0bb4c 20778 bfd *abfd = objfile->obfd;
8266302d 20779 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 20780
5989a64e 20781 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
20782}
20783
3019eac3 20784/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20785 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20786 ADDR_SIZE is the size of addresses from the CU header. */
20787
20788static CORE_ADDR
976ca316
SM
20789read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20790 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 20791{
976ca316 20792 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
20793 bfd *abfd = objfile->obfd;
20794 const gdb_byte *info_ptr;
18a8505e 20795 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 20796
976ca316
SM
20797 per_objfile->per_bfd->addr.read (objfile);
20798 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 20799 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20800 objfile_name (objfile));
18a8505e 20801 if (addr_base_or_zero + addr_index * addr_size
976ca316 20802 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
20803 error (_("DW_FORM_addr_index pointing outside of "
20804 ".debug_addr section [in module %s]"),
4262abfb 20805 objfile_name (objfile));
976ca316
SM
20806 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20807 + addr_index * addr_size);
3019eac3
DE
20808 if (addr_size == 4)
20809 return bfd_get_32 (abfd, info_ptr);
20810 else
20811 return bfd_get_64 (abfd, info_ptr);
20812}
20813
20814/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20815
20816static CORE_ADDR
20817read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20818{
5e22e966 20819 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 20820 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20821}
20822
20823/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20824
20825static CORE_ADDR
d521ce57 20826read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20827 unsigned int *bytes_read)
20828{
5e22e966 20829 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
20830 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20831
20832 return read_addr_index (cu, addr_index);
20833}
20834
450a1bfc 20835/* See read.h. */
3019eac3
DE
20836
20837CORE_ADDR
82ca3f51 20838dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 20839 dwarf2_per_objfile *per_objfile,
82ca3f51 20840 unsigned int addr_index)
3019eac3 20841{
976ca316 20842 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 20843 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20844 int addr_size;
20845
3019eac3
DE
20846 /* We need addr_base and addr_size.
20847 If we don't have PER_CU->cu, we have to get it.
20848 Nasty, but the alternative is storing the needed info in PER_CU,
20849 which at this point doesn't seem justified: it's not clear how frequently
20850 it would get used and it would increase the size of every PER_CU.
20851 Entry points like dwarf2_per_cu_addr_size do a similar thing
20852 so we're not in uncharted territory here.
20853 Alas we need to be a bit more complicated as addr_base is contained
20854 in the DIE.
20855
20856 We don't need to read the entire CU(/TU).
20857 We just need the header and top level die.
a1b64ce1 20858
3019eac3 20859 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20860 For now we skip this optimization. */
3019eac3
DE
20861
20862 if (cu != NULL)
20863 {
20864 addr_base = cu->addr_base;
20865 addr_size = cu->header.addr_size;
20866 }
20867 else
20868 {
976ca316 20869 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
20870 addr_base = reader.cu->addr_base;
20871 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20872 }
20873
976ca316 20874 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
20875}
20876
18a8505e
AT
20877/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20878 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20879 DWO file. */
3019eac3 20880
d521ce57 20881static const char *
18a8505e
AT
20882read_str_index (struct dwarf2_cu *cu,
20883 struct dwarf2_section_info *str_section,
20884 struct dwarf2_section_info *str_offsets_section,
20885 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20886{
976ca316
SM
20887 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20888 struct objfile *objfile = per_objfile->objfile;
c5164cbc 20889 const char *objf_name = objfile_name (objfile);
3019eac3 20890 bfd *abfd = objfile->obfd;
d521ce57 20891 const gdb_byte *info_ptr;
3019eac3 20892 ULONGEST str_offset;
cf532bd1 20893 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20894
96b79293
TT
20895 str_section->read (objfile);
20896 str_offsets_section->read (objfile);
73869dc2 20897 if (str_section->buffer == NULL)
18a8505e 20898 error (_("%s used without %s section"
9d8780f0 20899 " in CU at offset %s [in module %s]"),
96b79293 20900 form_name, str_section->get_name (),
dda83cd7 20901 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20902 if (str_offsets_section->buffer == NULL)
18a8505e 20903 error (_("%s used without %s section"
9d8780f0 20904 " in CU at offset %s [in module %s]"),
96b79293 20905 form_name, str_section->get_name (),
dda83cd7 20906 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20907 info_ptr = (str_offsets_section->buffer
18a8505e 20908 + str_offsets_base
3019eac3
DE
20909 + str_index * cu->header.offset_size);
20910 if (cu->header.offset_size == 4)
20911 str_offset = bfd_get_32 (abfd, info_ptr);
20912 else
20913 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20914 if (str_offset >= str_section->size)
57d63ce2 20915 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20916 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20917 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20918 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20919}
20920
18a8505e
AT
20921/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20922
20923static const char *
20924read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20925{
20926 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20927 ? reader->cu->header.addr_size : 0;
20928 return read_str_index (reader->cu,
20929 &reader->dwo_file->sections.str,
20930 &reader->dwo_file->sections.str_offsets,
20931 str_offsets_base, str_index);
20932}
20933
20934/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20935
20936static const char *
20937read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20938{
5e22e966 20939 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
20940 const char *objf_name = objfile_name (objfile);
20941 static const char form_name[] = "DW_FORM_GNU_str_index";
20942 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20943
20944 if (!cu->str_offsets_base.has_value ())
20945 error (_("%s used in Fission stub without %s"
20946 " in CU at offset 0x%lx [in module %s]"),
20947 form_name, str_offsets_attr_name,
20948 (long) cu->header.offset_size, objf_name);
20949
20950 return read_str_index (cu,
5e22e966
SM
20951 &cu->per_objfile->per_bfd->str,
20952 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
20953 *cu->str_offsets_base, str_index);
20954}
20955
3019eac3
DE
20956/* Return the length of an LEB128 number in BUF. */
20957
20958static int
20959leb128_size (const gdb_byte *buf)
20960{
20961 const gdb_byte *begin = buf;
20962 gdb_byte byte;
20963
20964 while (1)
20965 {
20966 byte = *buf++;
20967 if ((byte & 128) == 0)
20968 return buf - begin;
20969 }
20970}
20971
c906108c 20972static void
e142c38c 20973set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20974{
20975 switch (lang)
20976 {
20977 case DW_LANG_C89:
76bee0cc 20978 case DW_LANG_C99:
0cfd832f 20979 case DW_LANG_C11:
c906108c 20980 case DW_LANG_C:
d1be3247 20981 case DW_LANG_UPC:
e142c38c 20982 cu->language = language_c;
c906108c 20983 break;
9c37b5ae 20984 case DW_LANG_Java:
c906108c 20985 case DW_LANG_C_plus_plus:
0cfd832f
MW
20986 case DW_LANG_C_plus_plus_11:
20987 case DW_LANG_C_plus_plus_14:
e142c38c 20988 cu->language = language_cplus;
c906108c 20989 break;
6aecb9c2
JB
20990 case DW_LANG_D:
20991 cu->language = language_d;
20992 break;
c906108c
SS
20993 case DW_LANG_Fortran77:
20994 case DW_LANG_Fortran90:
b21b22e0 20995 case DW_LANG_Fortran95:
f7de9aab
MW
20996 case DW_LANG_Fortran03:
20997 case DW_LANG_Fortran08:
e142c38c 20998 cu->language = language_fortran;
c906108c 20999 break;
a766d390
DE
21000 case DW_LANG_Go:
21001 cu->language = language_go;
21002 break;
c906108c 21003 case DW_LANG_Mips_Assembler:
e142c38c 21004 cu->language = language_asm;
c906108c
SS
21005 break;
21006 case DW_LANG_Ada83:
8aaf0b47 21007 case DW_LANG_Ada95:
bc5f45f8
JB
21008 cu->language = language_ada;
21009 break;
72019c9c
GM
21010 case DW_LANG_Modula2:
21011 cu->language = language_m2;
21012 break;
fe8e67fd
PM
21013 case DW_LANG_Pascal83:
21014 cu->language = language_pascal;
21015 break;
22566fbd
DJ
21016 case DW_LANG_ObjC:
21017 cu->language = language_objc;
21018 break;
c44af4eb
TT
21019 case DW_LANG_Rust:
21020 case DW_LANG_Rust_old:
21021 cu->language = language_rust;
21022 break;
c906108c
SS
21023 case DW_LANG_Cobol74:
21024 case DW_LANG_Cobol85:
c906108c 21025 default:
e142c38c 21026 cu->language = language_minimal;
c906108c
SS
21027 break;
21028 }
e142c38c 21029 cu->language_defn = language_def (cu->language);
c906108c
SS
21030}
21031
21032/* Return the named attribute or NULL if not there. */
21033
21034static struct attribute *
e142c38c 21035dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 21036{
a48e046c 21037 for (;;)
c906108c 21038 {
a48e046c
TT
21039 unsigned int i;
21040 struct attribute *spec = NULL;
21041
21042 for (i = 0; i < die->num_attrs; ++i)
21043 {
21044 if (die->attrs[i].name == name)
21045 return &die->attrs[i];
21046 if (die->attrs[i].name == DW_AT_specification
21047 || die->attrs[i].name == DW_AT_abstract_origin)
21048 spec = &die->attrs[i];
21049 }
21050
21051 if (!spec)
21052 break;
c906108c 21053
f2f0e013 21054 die = follow_die_ref (die, spec, &cu);
f2f0e013 21055 }
c5aa993b 21056
c906108c
SS
21057 return NULL;
21058}
21059
7d45c7c3
KB
21060/* Return the string associated with a string-typed attribute, or NULL if it
21061 is either not found or is of an incorrect type. */
21062
21063static const char *
21064dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21065{
21066 struct attribute *attr;
21067 const char *str = NULL;
21068
21069 attr = dwarf2_attr (die, name, cu);
21070
21071 if (attr != NULL)
21072 {
95f982e5 21073 str = attr->as_string ();
e61108c9 21074 if (str == nullptr)
dda83cd7 21075 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
21076 "DIE at %s in module %s"),
21077 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 21078 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
21079 }
21080
21081 return str;
21082}
21083
a084a2a6 21084/* Return the dwo name or NULL if not present. If present, it is in either
85102364 21085 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
21086static const char *
21087dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
21088{
21089 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
21090 if (dwo_name == nullptr)
21091 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
21092 return dwo_name;
21093}
21094
05cf31d1
JB
21095/* Return non-zero iff the attribute NAME is defined for the given DIE,
21096 and holds a non-zero value. This function should only be used for
2dc7f7b3 21097 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
21098
21099static int
21100dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
21101{
21102 struct attribute *attr = dwarf2_attr (die, name, cu);
21103
c45bc3f8 21104 return attr != nullptr && attr->as_boolean ();
05cf31d1
JB
21105}
21106
3ca72b44 21107static int
e142c38c 21108die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 21109{
05cf31d1
JB
21110 /* A DIE is a declaration if it has a DW_AT_declaration attribute
21111 which value is non-zero. However, we have to be careful with
21112 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
21113 (via dwarf2_flag_true_p) follows this attribute. So we may
21114 end up accidently finding a declaration attribute that belongs
21115 to a different DIE referenced by the specification attribute,
21116 even though the given DIE does not have a declaration attribute. */
21117 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
21118 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
21119}
21120
63d06c5c 21121/* Return the die giving the specification for DIE, if there is
f2f0e013 21122 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
21123 containing the return value on output. If there is no
21124 specification, but there is an abstract origin, that is
21125 returned. */
63d06c5c
DC
21126
21127static struct die_info *
f2f0e013 21128die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 21129{
f2f0e013
DJ
21130 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
21131 *spec_cu);
63d06c5c 21132
edb3359d
DJ
21133 if (spec_attr == NULL)
21134 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
21135
63d06c5c
DC
21136 if (spec_attr == NULL)
21137 return NULL;
21138 else
f2f0e013 21139 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 21140}
c906108c 21141
527f3840
JK
21142/* Stub for free_line_header to match void * callback types. */
21143
21144static void
21145free_line_header_voidp (void *arg)
21146{
9a3c8263 21147 struct line_header *lh = (struct line_header *) arg;
527f3840 21148
fff8551c 21149 delete lh;
527f3840
JK
21150}
21151
83769d0b 21152/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
21153
21154static struct dwarf2_section_info *
21155get_debug_line_section (struct dwarf2_cu *cu)
21156{
21157 struct dwarf2_section_info *section;
976ca316 21158 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
21159
21160 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
21161 DWO file. */
21162 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21163 section = &cu->dwo_unit->dwo_file->sections.line;
21164 else if (cu->per_cu->is_dwz)
21165 {
976ca316 21166 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
36586728
TT
21167
21168 section = &dwz->line;
21169 }
21170 else
976ca316 21171 section = &per_objfile->per_bfd->line;
36586728
TT
21172
21173 return section;
21174}
21175
debd256d 21176/* Read the statement program header starting at OFFSET in
3019eac3 21177 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 21178 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
21179 Returns NULL if there is a problem reading the header, e.g., if it
21180 has a version we don't understand.
debd256d
JB
21181
21182 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
21183 the returned object point into the dwarf line section buffer,
21184 and must not be freed. */
ae2de4f8 21185
fff8551c 21186static line_header_up
9c541725 21187dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 21188{
3019eac3 21189 struct dwarf2_section_info *section;
976ca316 21190 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 21191
36586728 21192 section = get_debug_line_section (cu);
976ca316 21193 section->read (per_objfile->objfile);
3019eac3 21194 if (section->buffer == NULL)
debd256d 21195 {
3019eac3 21196 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 21197 complaint (_("missing .debug_line.dwo section"));
3019eac3 21198 else
b98664d3 21199 complaint (_("missing .debug_line section"));
debd256d
JB
21200 return 0;
21201 }
21202
0df7ad3a 21203 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 21204 per_objfile, section, &cu->header);
debd256d 21205}
c906108c 21206
c6da4cef 21207/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 21208 Return the file name of the psymtab for the given file_entry.
c6da4cef 21209 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
21210 If space for the result is malloc'd, *NAME_HOLDER will be set.
21211 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 21212
d521ce57 21213static const char *
7ba99d21 21214psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 21215 const dwarf2_psymtab *pst,
c89b44cd
TT
21216 const char *comp_dir,
21217 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 21218{
d521ce57
TT
21219 const char *include_name = fe.name;
21220 const char *include_name_to_compare = include_name;
72b9f47f 21221 const char *pst_filename;
c6da4cef
DE
21222 int file_is_pst;
21223
8c43009f 21224 const char *dir_name = fe.include_dir (lh);
c6da4cef 21225
c89b44cd 21226 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
21227 if (!IS_ABSOLUTE_PATH (include_name)
21228 && (dir_name != NULL || comp_dir != NULL))
21229 {
21230 /* Avoid creating a duplicate psymtab for PST.
21231 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21232 Before we do the comparison, however, we need to account
21233 for DIR_NAME and COMP_DIR.
21234 First prepend dir_name (if non-NULL). If we still don't
21235 have an absolute path prepend comp_dir (if non-NULL).
21236 However, the directory we record in the include-file's
21237 psymtab does not contain COMP_DIR (to match the
21238 corresponding symtab(s)).
21239
21240 Example:
21241
21242 bash$ cd /tmp
21243 bash$ gcc -g ./hello.c
21244 include_name = "hello.c"
21245 dir_name = "."
21246 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
21247 DW_AT_name = "./hello.c"
21248
21249 */
c6da4cef
DE
21250
21251 if (dir_name != NULL)
21252 {
c89b44cd
TT
21253 name_holder->reset (concat (dir_name, SLASH_STRING,
21254 include_name, (char *) NULL));
21255 include_name = name_holder->get ();
c6da4cef 21256 include_name_to_compare = include_name;
c6da4cef
DE
21257 }
21258 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21259 {
c89b44cd
TT
21260 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21261 include_name, (char *) NULL));
21262 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
21263 }
21264 }
21265
21266 pst_filename = pst->filename;
c89b44cd 21267 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
21268 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21269 {
c89b44cd
TT
21270 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21271 pst_filename, (char *) NULL));
21272 pst_filename = copied_name.get ();
c6da4cef
DE
21273 }
21274
1e3fad37 21275 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 21276
c6da4cef
DE
21277 if (file_is_pst)
21278 return NULL;
21279 return include_name;
21280}
21281
d9b3de22
DE
21282/* State machine to track the state of the line number program. */
21283
6f77053d 21284class lnp_state_machine
d9b3de22 21285{
6f77053d
PA
21286public:
21287 /* Initialize a machine state for the start of a line number
21288 program. */
804d2729
TT
21289 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21290 bool record_lines_p);
6f77053d 21291
8c43009f
PA
21292 file_entry *current_file ()
21293 {
21294 /* lh->file_names is 0-based, but the file name numbers in the
21295 statement program are 1-based. */
6f77053d
PA
21296 return m_line_header->file_name_at (m_file);
21297 }
21298
21299 /* Record the line in the state machine. END_SEQUENCE is true if
21300 we're processing the end of a sequence. */
21301 void record_line (bool end_sequence);
21302
a8caed5d 21303 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
7ab6656f 21304 nop-out rest of the lines in this sequence. */
6f77053d
PA
21305 void check_line_address (struct dwarf2_cu *cu,
21306 const gdb_byte *line_ptr,
7ab6656f 21307 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
21308
21309 void handle_set_discriminator (unsigned int discriminator)
21310 {
21311 m_discriminator = discriminator;
21312 m_line_has_non_zero_discriminator |= discriminator != 0;
21313 }
21314
21315 /* Handle DW_LNE_set_address. */
21316 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21317 {
21318 m_op_index = 0;
21319 address += baseaddr;
21320 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21321 }
21322
21323 /* Handle DW_LNS_advance_pc. */
21324 void handle_advance_pc (CORE_ADDR adjust);
21325
21326 /* Handle a special opcode. */
21327 void handle_special_opcode (unsigned char op_code);
21328
21329 /* Handle DW_LNS_advance_line. */
21330 void handle_advance_line (int line_delta)
21331 {
21332 advance_line (line_delta);
21333 }
21334
21335 /* Handle DW_LNS_set_file. */
21336 void handle_set_file (file_name_index file);
21337
21338 /* Handle DW_LNS_negate_stmt. */
21339 void handle_negate_stmt ()
21340 {
21341 m_is_stmt = !m_is_stmt;
21342 }
21343
21344 /* Handle DW_LNS_const_add_pc. */
21345 void handle_const_add_pc ();
21346
21347 /* Handle DW_LNS_fixed_advance_pc. */
21348 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21349 {
21350 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21351 m_op_index = 0;
21352 }
21353
21354 /* Handle DW_LNS_copy. */
21355 void handle_copy ()
21356 {
21357 record_line (false);
21358 m_discriminator = 0;
21359 }
21360
21361 /* Handle DW_LNE_end_sequence. */
21362 void handle_end_sequence ()
21363 {
804d2729 21364 m_currently_recording_lines = true;
6f77053d
PA
21365 }
21366
21367private:
21368 /* Advance the line by LINE_DELTA. */
21369 void advance_line (int line_delta)
21370 {
21371 m_line += line_delta;
21372
21373 if (line_delta != 0)
21374 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21375 }
21376
804d2729
TT
21377 struct dwarf2_cu *m_cu;
21378
6f77053d
PA
21379 gdbarch *m_gdbarch;
21380
21381 /* True if we're recording lines.
21382 Otherwise we're building partial symtabs and are just interested in
21383 finding include files mentioned by the line number program. */
21384 bool m_record_lines_p;
21385
8c43009f 21386 /* The line number header. */
6f77053d 21387 line_header *m_line_header;
8c43009f 21388
6f77053d
PA
21389 /* These are part of the standard DWARF line number state machine,
21390 and initialized according to the DWARF spec. */
d9b3de22 21391
6f77053d 21392 unsigned char m_op_index = 0;
7ba99d21
AT
21393 /* The line table index of the current file. */
21394 file_name_index m_file = 1;
6f77053d
PA
21395 unsigned int m_line = 1;
21396
21397 /* These are initialized in the constructor. */
21398
21399 CORE_ADDR m_address;
21400 bool m_is_stmt;
21401 unsigned int m_discriminator;
d9b3de22
DE
21402
21403 /* Additional bits of state we need to track. */
21404
21405 /* The last file that we called dwarf2_start_subfile for.
21406 This is only used for TLLs. */
6f77053d 21407 unsigned int m_last_file = 0;
d9b3de22 21408 /* The last file a line number was recorded for. */
6f77053d 21409 struct subfile *m_last_subfile = NULL;
d9b3de22 21410
1313c56e
AB
21411 /* The address of the last line entry. */
21412 CORE_ADDR m_last_address;
21413
21414 /* Set to true when a previous line at the same address (using
21415 m_last_address) had m_is_stmt true. This is reset to false when a
21416 line entry at a new address (m_address different to m_last_address) is
21417 processed. */
21418 bool m_stmt_at_address = false;
21419
804d2729
TT
21420 /* When true, record the lines we decode. */
21421 bool m_currently_recording_lines = false;
d9b3de22
DE
21422
21423 /* The last line number that was recorded, used to coalesce
21424 consecutive entries for the same line. This can happen, for
21425 example, when discriminators are present. PR 17276. */
6f77053d
PA
21426 unsigned int m_last_line = 0;
21427 bool m_line_has_non_zero_discriminator = false;
8c43009f 21428};
d9b3de22 21429
6f77053d
PA
21430void
21431lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21432{
21433 CORE_ADDR addr_adj = (((m_op_index + adjust)
21434 / m_line_header->maximum_ops_per_instruction)
21435 * m_line_header->minimum_instruction_length);
21436 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21437 m_op_index = ((m_op_index + adjust)
21438 % m_line_header->maximum_ops_per_instruction);
21439}
d9b3de22 21440
6f77053d
PA
21441void
21442lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21443{
6f77053d 21444 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
21445 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21446 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21447 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
21448 / m_line_header->maximum_ops_per_instruction)
21449 * m_line_header->minimum_instruction_length);
21450 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 21451 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 21452 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21453
258bf0ee 21454 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
21455 advance_line (line_delta);
21456 record_line (false);
21457 m_discriminator = 0;
21458}
d9b3de22 21459
6f77053d
PA
21460void
21461lnp_state_machine::handle_set_file (file_name_index file)
21462{
21463 m_file = file;
21464
21465 const file_entry *fe = current_file ();
21466 if (fe == NULL)
21467 dwarf2_debug_line_missing_file_complaint ();
21468 else if (m_record_lines_p)
21469 {
21470 const char *dir = fe->include_dir (m_line_header);
21471
c24bdb02 21472 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21473 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21474 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21475 }
21476}
21477
21478void
21479lnp_state_machine::handle_const_add_pc ()
21480{
21481 CORE_ADDR adjust
21482 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21483
21484 CORE_ADDR addr_adj
21485 = (((m_op_index + adjust)
21486 / m_line_header->maximum_ops_per_instruction)
21487 * m_line_header->minimum_instruction_length);
21488
21489 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21490 m_op_index = ((m_op_index + adjust)
21491 % m_line_header->maximum_ops_per_instruction);
21492}
d9b3de22 21493
a05a36a5
DE
21494/* Return non-zero if we should add LINE to the line number table.
21495 LINE is the line to add, LAST_LINE is the last line that was added,
21496 LAST_SUBFILE is the subfile for LAST_LINE.
21497 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21498 had a non-zero discriminator.
21499
21500 We have to be careful in the presence of discriminators.
21501 E.g., for this line:
21502
21503 for (i = 0; i < 100000; i++);
21504
21505 clang can emit four line number entries for that one line,
21506 each with a different discriminator.
21507 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21508
21509 However, we want gdb to coalesce all four entries into one.
21510 Otherwise the user could stepi into the middle of the line and
21511 gdb would get confused about whether the pc really was in the
21512 middle of the line.
21513
21514 Things are further complicated by the fact that two consecutive
21515 line number entries for the same line is a heuristic used by gcc
21516 to denote the end of the prologue. So we can't just discard duplicate
21517 entries, we have to be selective about it. The heuristic we use is
21518 that we only collapse consecutive entries for the same line if at least
21519 one of those entries has a non-zero discriminator. PR 17276.
21520
21521 Note: Addresses in the line number state machine can never go backwards
21522 within one sequence, thus this coalescing is ok. */
21523
21524static int
804d2729
TT
21525dwarf_record_line_p (struct dwarf2_cu *cu,
21526 unsigned int line, unsigned int last_line,
a05a36a5
DE
21527 int line_has_non_zero_discriminator,
21528 struct subfile *last_subfile)
21529{
c24bdb02 21530 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21531 return 1;
21532 if (line != last_line)
21533 return 1;
21534 /* Same line for the same file that we've seen already.
21535 As a last check, for pr 17276, only record the line if the line
21536 has never had a non-zero discriminator. */
21537 if (!line_has_non_zero_discriminator)
21538 return 1;
21539 return 0;
21540}
21541
804d2729
TT
21542/* Use the CU's builder to record line number LINE beginning at
21543 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21544
21545static void
d9b3de22 21546dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 21547 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 21548 struct dwarf2_cu *cu)
252a6764
DE
21549{
21550 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21551
27e0867f
DE
21552 if (dwarf_line_debug)
21553 {
21554 fprintf_unfiltered (gdb_stdlog,
21555 "Recording line %u, file %s, address %s\n",
21556 line, lbasename (subfile->name),
21557 paddress (gdbarch, address));
21558 }
21559
804d2729 21560 if (cu != nullptr)
8c95582d 21561 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
21562}
21563
21564/* Subroutine of dwarf_decode_lines_1 to simplify it.
21565 Mark the end of a set of line number records.
d9b3de22 21566 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21567 If SUBFILE is NULL the request is ignored. */
21568
21569static void
21570dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21571 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21572{
27e0867f
DE
21573 if (subfile == NULL)
21574 return;
21575
21576 if (dwarf_line_debug)
21577 {
21578 fprintf_unfiltered (gdb_stdlog,
21579 "Finishing current line, file %s, address %s\n",
21580 lbasename (subfile->name),
21581 paddress (gdbarch, address));
21582 }
21583
8c95582d 21584 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
21585}
21586
6f77053d
PA
21587void
21588lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21589{
d9b3de22
DE
21590 if (dwarf_line_debug)
21591 {
21592 fprintf_unfiltered (gdb_stdlog,
21593 "Processing actual line %u: file %u,"
94a72be7 21594 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 21595 m_line, m_file,
6f77053d 21596 paddress (m_gdbarch, m_address),
94a72be7
AB
21597 m_is_stmt, m_discriminator,
21598 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
21599 }
21600
6f77053d 21601 file_entry *fe = current_file ();
8c43009f
PA
21602
21603 if (fe == NULL)
d9b3de22
DE
21604 dwarf2_debug_line_missing_file_complaint ();
21605 /* For now we ignore lines not starting on an instruction boundary.
21606 But not when processing end_sequence for compatibility with the
21607 previous version of the code. */
6f77053d 21608 else if (m_op_index == 0 || end_sequence)
d9b3de22 21609 {
8c43009f 21610 fe->included_p = 1;
8c95582d 21611 if (m_record_lines_p)
d9b3de22 21612 {
1313c56e
AB
21613 /* When we switch files we insert an end maker in the first file,
21614 switch to the second file and add a new line entry. The
21615 problem is that the end marker inserted in the first file will
21616 discard any previous line entries at the same address. If the
21617 line entries in the first file are marked as is-stmt, while
21618 the new line in the second file is non-stmt, then this means
21619 the end marker will discard is-stmt lines so we can have a
21620 non-stmt line. This means that there are less addresses at
21621 which the user can insert a breakpoint.
21622
21623 To improve this we track the last address in m_last_address,
21624 and whether we have seen an is-stmt at this address. Then
21625 when switching files, if we have seen a stmt at the current
21626 address, and we are switching to create a non-stmt line, then
21627 discard the new line. */
21628 bool file_changed
21629 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21630 bool ignore_this_line
dda83cd7
SM
21631 = ((file_changed && !end_sequence && m_last_address == m_address
21632 && !m_is_stmt && m_stmt_at_address)
21633 || (!end_sequence && m_line == 0));
1313c56e
AB
21634
21635 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 21636 {
804d2729
TT
21637 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21638 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21639 }
21640
1313c56e 21641 if (!end_sequence && !ignore_this_line)
d9b3de22 21642 {
8c95582d
AB
21643 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21644
804d2729 21645 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21646 m_line_has_non_zero_discriminator,
21647 m_last_subfile))
d9b3de22 21648 {
c24bdb02 21649 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21650 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21651 builder->get_current_subfile (),
8c95582d 21652 m_line, m_address, is_stmt,
804d2729 21653 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21654 }
c24bdb02 21655 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21656 m_last_line = m_line;
d9b3de22
DE
21657 }
21658 }
21659 }
1313c56e
AB
21660
21661 /* Track whether we have seen any m_is_stmt true at m_address in case we
21662 have multiple line table entries all at m_address. */
21663 if (m_last_address != m_address)
21664 {
21665 m_stmt_at_address = false;
21666 m_last_address = m_address;
21667 }
21668 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
21669}
21670
804d2729
TT
21671lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21672 line_header *lh, bool record_lines_p)
d9b3de22 21673{
804d2729 21674 m_cu = cu;
6f77053d
PA
21675 m_gdbarch = arch;
21676 m_record_lines_p = record_lines_p;
21677 m_line_header = lh;
d9b3de22 21678
804d2729 21679 m_currently_recording_lines = true;
d9b3de22 21680
d9b3de22
DE
21681 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21682 was a line entry for it so that the backend has a chance to adjust it
21683 and also record it in case it needs it. This is currently used by MIPS
21684 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21685 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21686 m_is_stmt = lh->default_is_stmt;
21687 m_discriminator = 0;
1313c56e
AB
21688
21689 m_last_address = m_address;
21690 m_stmt_at_address = false;
252a6764
DE
21691}
21692
6f77053d
PA
21693void
21694lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21695 const gdb_byte *line_ptr,
7ab6656f 21696 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21697{
a8caed5d
FS
21698 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21699 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21700 located at 0x0. In this case, additionally check that if
21701 ADDRESS < UNRELOCATED_LOWPC. */
924c2928 21702
a8caed5d
FS
21703 if ((address == 0 && address < unrelocated_lowpc)
21704 || address == (CORE_ADDR) -1)
924c2928
DE
21705 {
21706 /* This line table is for a function which has been
21707 GCd by the linker. Ignore it. PR gdb/12528 */
21708
5e22e966 21709 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
21710 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21711
b98664d3 21712 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21713 line_offset, objfile_name (objfile));
804d2729
TT
21714 m_currently_recording_lines = false;
21715 /* Note: m_currently_recording_lines is left as false until we see
21716 DW_LNE_end_sequence. */
924c2928
DE
21717 }
21718}
21719
f3f5162e 21720/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21721 Process the line number information in LH.
21722 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21723 program in order to set included_p for every referenced header. */
debd256d 21724
c906108c 21725static void
43f3e411
DE
21726dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21727 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21728{
d521ce57
TT
21729 const gdb_byte *line_ptr, *extended_end;
21730 const gdb_byte *line_end;
a8c50c1f 21731 unsigned int bytes_read, extended_len;
699ca60a 21732 unsigned char op_code, extended_op;
e142c38c 21733 CORE_ADDR baseaddr;
5e22e966 21734 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21735 bfd *abfd = objfile->obfd;
08feed99 21736 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
21737 /* True if we're recording line info (as opposed to building partial
21738 symtabs and just interested in finding include files mentioned by
21739 the line number program). */
21740 bool record_lines_p = !decode_for_pst_p;
e142c38c 21741
b3b3bada 21742 baseaddr = objfile->text_section_offset ();
c906108c 21743
debd256d
JB
21744 line_ptr = lh->statement_program_start;
21745 line_end = lh->statement_program_end;
c906108c
SS
21746
21747 /* Read the statement sequences until there's nothing left. */
21748 while (line_ptr < line_end)
21749 {
6f77053d
PA
21750 /* The DWARF line number program state machine. Reset the state
21751 machine at the start of each sequence. */
804d2729 21752 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21753 bool end_sequence = false;
d9b3de22 21754
8c43009f 21755 if (record_lines_p)
c906108c 21756 {
8c43009f
PA
21757 /* Start a subfile for the current file of the state
21758 machine. */
21759 const file_entry *fe = state_machine.current_file ();
21760
21761 if (fe != NULL)
804d2729 21762 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21763 }
21764
a738430d 21765 /* Decode the table. */
d9b3de22 21766 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21767 {
21768 op_code = read_1_byte (abfd, line_ptr);
21769 line_ptr += 1;
9aa1fe7e 21770
debd256d 21771 if (op_code >= lh->opcode_base)
6e70227d 21772 {
8e07a239 21773 /* Special opcode. */
6f77053d 21774 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21775 }
21776 else switch (op_code)
c906108c
SS
21777 {
21778 case DW_LNS_extended_op:
3e43a32a
MS
21779 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21780 &bytes_read);
473b7be6 21781 line_ptr += bytes_read;
a8c50c1f 21782 extended_end = line_ptr + extended_len;
c906108c
SS
21783 extended_op = read_1_byte (abfd, line_ptr);
21784 line_ptr += 1;
8f34b746
TV
21785 if (DW_LNE_lo_user <= extended_op
21786 && extended_op <= DW_LNE_hi_user)
21787 {
21788 /* Vendor extension, ignore. */
21789 line_ptr = extended_end;
21790 break;
21791 }
c906108c
SS
21792 switch (extended_op)
21793 {
21794 case DW_LNE_end_sequence:
6f77053d
PA
21795 state_machine.handle_end_sequence ();
21796 end_sequence = true;
c906108c
SS
21797 break;
21798 case DW_LNE_set_address:
d9b3de22
DE
21799 {
21800 CORE_ADDR address
c8a7a66f 21801 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 21802 line_ptr += bytes_read;
6f77053d
PA
21803
21804 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21805 lowpc - baseaddr, address);
6f77053d 21806 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21807 }
c906108c
SS
21808 break;
21809 case DW_LNE_define_file:
dda83cd7
SM
21810 {
21811 const char *cur_file;
ecfb656c
PA
21812 unsigned int mod_time, length;
21813 dir_index dindex;
6e70227d 21814
dda83cd7 21815 cur_file = read_direct_string (abfd, line_ptr,
3e43a32a 21816 &bytes_read);
dda83cd7
SM
21817 line_ptr += bytes_read;
21818 dindex = (dir_index)
21819 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21820 line_ptr += bytes_read;
21821 mod_time =
21822 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21823 line_ptr += bytes_read;
21824 length =
21825 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21826 line_ptr += bytes_read;
21827 lh->add_file_name (cur_file, dindex, mod_time, length);
21828 }
c906108c 21829 break;
d0c6ba3d 21830 case DW_LNE_set_discriminator:
6f77053d
PA
21831 {
21832 /* The discriminator is not interesting to the
21833 debugger; just ignore it. We still need to
21834 check its value though:
21835 if there are consecutive entries for the same
21836 (non-prologue) line we want to coalesce them.
21837 PR 17276. */
21838 unsigned int discr
21839 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21840 line_ptr += bytes_read;
21841
21842 state_machine.handle_set_discriminator (discr);
21843 }
d0c6ba3d 21844 break;
c906108c 21845 default:
b98664d3 21846 complaint (_("mangled .debug_line section"));
debd256d 21847 return;
c906108c 21848 }
a8c50c1f
DJ
21849 /* Make sure that we parsed the extended op correctly. If e.g.
21850 we expected a different address size than the producer used,
21851 we may have read the wrong number of bytes. */
21852 if (line_ptr != extended_end)
21853 {
b98664d3 21854 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21855 return;
21856 }
c906108c
SS
21857 break;
21858 case DW_LNS_copy:
6f77053d 21859 state_machine.handle_copy ();
c906108c
SS
21860 break;
21861 case DW_LNS_advance_pc:
2dc7f7b3
TT
21862 {
21863 CORE_ADDR adjust
21864 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21865 line_ptr += bytes_read;
6f77053d
PA
21866
21867 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21868 }
c906108c
SS
21869 break;
21870 case DW_LNS_advance_line:
a05a36a5
DE
21871 {
21872 int line_delta
21873 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21874 line_ptr += bytes_read;
6f77053d
PA
21875
21876 state_machine.handle_advance_line (line_delta);
a05a36a5 21877 }
c906108c
SS
21878 break;
21879 case DW_LNS_set_file:
d9b3de22 21880 {
6f77053d 21881 file_name_index file
ecfb656c
PA
21882 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21883 &bytes_read);
d9b3de22 21884 line_ptr += bytes_read;
8c43009f 21885
6f77053d 21886 state_machine.handle_set_file (file);
d9b3de22 21887 }
c906108c
SS
21888 break;
21889 case DW_LNS_set_column:
0ad93d4f 21890 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21891 line_ptr += bytes_read;
21892 break;
21893 case DW_LNS_negate_stmt:
6f77053d 21894 state_machine.handle_negate_stmt ();
c906108c
SS
21895 break;
21896 case DW_LNS_set_basic_block:
c906108c 21897 break;
c2c6d25f
JM
21898 /* Add to the address register of the state machine the
21899 address increment value corresponding to special opcode
a738430d
MK
21900 255. I.e., this value is scaled by the minimum
21901 instruction length since special opcode 255 would have
b021a221 21902 scaled the increment. */
c906108c 21903 case DW_LNS_const_add_pc:
6f77053d 21904 state_machine.handle_const_add_pc ();
c906108c
SS
21905 break;
21906 case DW_LNS_fixed_advance_pc:
3e29f34a 21907 {
6f77053d 21908 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21909 line_ptr += 2;
6f77053d
PA
21910
21911 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21912 }
c906108c 21913 break;
9aa1fe7e 21914 default:
a738430d
MK
21915 {
21916 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21917 int i;
a738430d 21918
debd256d 21919 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21920 {
21921 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21922 line_ptr += bytes_read;
21923 }
21924 }
c906108c
SS
21925 }
21926 }
d9b3de22
DE
21927
21928 if (!end_sequence)
21929 dwarf2_debug_line_missing_end_sequence_complaint ();
21930
21931 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21932 in which case we still finish recording the last line). */
6f77053d 21933 state_machine.record_line (true);
c906108c 21934 }
f3f5162e
DE
21935}
21936
21937/* Decode the Line Number Program (LNP) for the given line_header
21938 structure and CU. The actual information extracted and the type
21939 of structures created from the LNP depends on the value of PST.
21940
21941 1. If PST is NULL, then this procedure uses the data from the program
21942 to create all necessary symbol tables, and their linetables.
21943
21944 2. If PST is not NULL, this procedure reads the program to determine
21945 the list of files included by the unit represented by PST, and
21946 builds all the associated partial symbol tables.
21947
21948 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21949 It is used for relative paths in the line table.
21950 NOTE: When processing partial symtabs (pst != NULL),
21951 comp_dir == pst->dirname.
21952
21953 NOTE: It is important that psymtabs have the same file name (via strcmp)
21954 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21955 symtab we don't use it in the name of the psymtabs we create.
21956 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21957 A good testcase for this is mb-inline.exp.
21958
527f3840
JK
21959 LOWPC is the lowest address in CU (or 0 if not known).
21960
21961 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21962 for its PC<->lines mapping information. Otherwise only the filename
21963 table is read in. */
f3f5162e
DE
21964
21965static void
21966dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21967 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21968 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21969{
5e22e966 21970 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21971 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21972
527f3840
JK
21973 if (decode_mapping)
21974 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21975
21976 if (decode_for_pst_p)
21977 {
aaa75496 21978 /* Now that we're done scanning the Line Header Program, we can
dda83cd7 21979 create the psymtab of each included file. */
7ba99d21 21980 for (auto &file_entry : lh->file_names ())
dda83cd7
SM
21981 if (file_entry.included_p == 1)
21982 {
c89b44cd 21983 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21984 const char *include_name =
7ba99d21
AT
21985 psymtab_include_file_name (lh, file_entry, pst,
21986 comp_dir, &name_holder);
c6da4cef 21987 if (include_name != NULL)
dda83cd7
SM
21988 dwarf2_create_include_psymtab (include_name, pst, objfile);
21989 }
aaa75496 21990 }
cb1df416
DJ
21991 else
21992 {
21993 /* Make sure a symtab is created for every file, even files
21994 which contain only variables (i.e. no code with associated
21995 line numbers). */
c24bdb02
KS
21996 buildsym_compunit *builder = cu->get_builder ();
21997 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21998
7ba99d21 21999 for (auto &fe : lh->file_names ())
cb1df416 22000 {
804d2729 22001 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 22002 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 22003 {
c24bdb02 22004 builder->get_current_subfile ()->symtab
804d2729 22005 = allocate_symtab (cust,
c24bdb02 22006 builder->get_current_subfile ()->name);
43f3e411 22007 }
c24bdb02 22008 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
22009 }
22010 }
c906108c
SS
22011}
22012
22013/* Start a subfile for DWARF. FILENAME is the name of the file and
22014 DIRNAME the name of the source directory which contains FILENAME
4d663531 22015 or NULL if not known.
c906108c
SS
22016 This routine tries to keep line numbers from identical absolute and
22017 relative file names in a common subfile.
22018
22019 Using the `list' example from the GDB testsuite, which resides in
22020 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
22021 of /srcdir/list0.c yields the following debugging information for list0.c:
22022
c5aa993b 22023 DW_AT_name: /srcdir/list0.c
4d663531 22024 DW_AT_comp_dir: /compdir
357e46e7 22025 files.files[0].name: list0.h
c5aa993b 22026 files.files[0].dir: /srcdir
357e46e7 22027 files.files[1].name: list0.c
c5aa993b 22028 files.files[1].dir: /srcdir
c906108c
SS
22029
22030 The line number information for list0.c has to end up in a single
4f1520fb
FR
22031 subfile, so that `break /srcdir/list0.c:1' works as expected.
22032 start_subfile will ensure that this happens provided that we pass the
22033 concatenation of files.files[1].dir and files.files[1].name as the
22034 subfile's name. */
c906108c
SS
22035
22036static void
804d2729
TT
22037dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
22038 const char *dirname)
c906108c 22039{
43816ebc 22040 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 22041
4d663531 22042 /* In order not to lose the line information directory,
4f1520fb
FR
22043 we concatenate it to the filename when it makes sense.
22044 Note that the Dwarf3 standard says (speaking of filenames in line
22045 information): ``The directory index is ignored for file names
22046 that represent full path names''. Thus ignoring dirname in the
22047 `else' branch below isn't an issue. */
c906108c 22048
d5166ae1 22049 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 22050 {
43816ebc
TT
22051 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
22052 filename = copy.get ();
d521ce57 22053 }
c906108c 22054
c24bdb02 22055 cu->get_builder ()->start_subfile (filename);
c906108c
SS
22056}
22057
804d2729
TT
22058/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
22059 buildsym_compunit constructor. */
f4dc4d17 22060
c24bdb02
KS
22061struct compunit_symtab *
22062dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
22063 CORE_ADDR low_pc)
f4dc4d17 22064{
c24bdb02 22065 gdb_assert (m_builder == nullptr);
43f3e411 22066
c24bdb02 22067 m_builder.reset (new struct buildsym_compunit
f6e649dd 22068 (this->per_objfile->objfile,
c24bdb02 22069 name, comp_dir, language, low_pc));
93b8bea4 22070
c24bdb02 22071 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 22072
c24bdb02
KS
22073 get_builder ()->record_debugformat ("DWARF 2");
22074 get_builder ()->record_producer (producer);
f4dc4d17 22075
c24bdb02 22076 processing_has_namespace_info = false;
43f3e411 22077
c24bdb02 22078 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
22079}
22080
4c2df51b
DJ
22081static void
22082var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 22083 struct dwarf2_cu *cu)
4c2df51b 22084{
5e22e966 22085 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
22086 struct comp_unit_head *cu_header = &cu->header;
22087
4c2df51b
DJ
22088 /* NOTE drow/2003-01-30: There used to be a comment and some special
22089 code here to turn a symbol with DW_AT_external and a
22090 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
22091 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
22092 with some versions of binutils) where shared libraries could have
22093 relocations against symbols in their debug information - the
22094 minimal symbol would have the right address, but the debug info
22095 would not. It's no longer necessary, because we will explicitly
22096 apply relocations when we read in the debug information now. */
22097
22098 /* A DW_AT_location attribute with no contents indicates that a
22099 variable has been optimized away. */
9d2246fc 22100 if (attr->form_is_block () && attr->as_block ()->size == 0)
4c2df51b 22101 {
f1e6e072 22102 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
22103 return;
22104 }
22105
22106 /* Handle one degenerate form of location expression specially, to
22107 preserve GDB's previous behavior when section offsets are
336d760d
AT
22108 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
22109 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 22110
9d2246fc
TT
22111 if (attr->form_is_block ())
22112 {
22113 struct dwarf_block *block = attr->as_block ();
22114
22115 if ((block->data[0] == DW_OP_addr
22116 && block->size == 1 + cu_header->addr_size)
22117 || ((block->data[0] == DW_OP_GNU_addr_index
dda83cd7 22118 || block->data[0] == DW_OP_addrx)
9d2246fc
TT
22119 && (block->size
22120 == 1 + leb128_size (&block->data[1]))))
22121 {
22122 unsigned int dummy;
22123
22124 if (block->data[0] == DW_OP_addr)
22125 SET_SYMBOL_VALUE_ADDRESS
22126 (sym, cu->header.read_address (objfile->obfd,
22127 block->data + 1,
38583298 22128 &dummy));
9d2246fc
TT
22129 else
22130 SET_SYMBOL_VALUE_ADDRESS
22131 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
22132 &dummy));
22133 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
22134 fixup_symbol_section (sym, objfile);
22135 SET_SYMBOL_VALUE_ADDRESS
22136 (sym,
22137 SYMBOL_VALUE_ADDRESS (sym)
a52d653e 22138 + objfile->section_offsets[sym->section_index ()]);
9d2246fc
TT
22139 return;
22140 }
4c2df51b
DJ
22141 }
22142
22143 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
22144 expression evaluator, and use LOC_COMPUTED only when necessary
22145 (i.e. when the value of a register or memory location is
22146 referenced, or a thread-local block, etc.). Then again, it might
22147 not be worthwhile. I'm assuming that it isn't unless performance
22148 or memory numbers show me otherwise. */
22149
f1e6e072 22150 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 22151
f1e6e072 22152 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 22153 cu->has_loclist = true;
4c2df51b
DJ
22154}
22155
c906108c
SS
22156/* Given a pointer to a DWARF information entry, figure out if we need
22157 to make a symbol table entry for it, and if so, create a new entry
22158 and return a pointer to it.
22159 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
22160 used the passed type.
22161 If SPACE is not NULL, use it to hold the new symbol. If it is
22162 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
22163
22164static struct symbol *
5e2db402
TT
22165new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
22166 struct symbol *space)
c906108c 22167{
976ca316
SM
22168 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22169 struct objfile *objfile = per_objfile->objfile;
08feed99 22170 struct gdbarch *gdbarch = objfile->arch ();
c906108c 22171 struct symbol *sym = NULL;
15d034d0 22172 const char *name;
c906108c
SS
22173 struct attribute *attr = NULL;
22174 struct attribute *attr2 = NULL;
e142c38c 22175 CORE_ADDR baseaddr;
e37fd15a
SW
22176 struct pending **list_to_add = NULL;
22177
edb3359d 22178 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 22179
b3b3bada 22180 baseaddr = objfile->text_section_offset ();
c906108c 22181
94af9270 22182 name = dwarf2_name (die, cu);
c906108c
SS
22183 if (name)
22184 {
34eaf542 22185 int suppress_add = 0;
94af9270 22186
34eaf542
TT
22187 if (space)
22188 sym = space;
22189 else
8c14c3a3 22190 sym = new (&objfile->objfile_obstack) symbol;
c906108c 22191 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
22192
22193 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 22194 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
22195 /* Fortran does not have mangling standard and the mangling does differ
22196 between gfortran, iFort etc. */
bcfe6157
TT
22197 const char *physname
22198 = (cu->language == language_fortran
22199 ? dwarf2_full_name (name, die, cu)
22200 : dwarf2_physname (name, die, cu));
22201 const char *linkagename = dw2_linkage_name (die, cu);
22202
22203 if (linkagename == nullptr || cu->language == language_ada)
22204 sym->set_linkage_name (physname);
22205 else
22206 {
22207 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22208 sym->set_linkage_name (linkagename);
22209 }
f55ee35c 22210
c906108c 22211 /* Default assumptions.
dda83cd7 22212 Use the passed type or decode it from the die. */
176620f1 22213 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 22214 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
22215 if (type != NULL)
22216 SYMBOL_TYPE (sym) = type;
22217 else
e7c27a73 22218 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
22219 attr = dwarf2_attr (die,
22220 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22221 cu);
435d3d88 22222 if (attr != nullptr)
529908cb 22223 SYMBOL_LINE (sym) = attr->constant_value (0);
cb1df416 22224
edb3359d
DJ
22225 attr = dwarf2_attr (die,
22226 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22227 cu);
aa659cfa 22228 if (attr != nullptr && attr->is_nonnegative ())
cb1df416 22229 {
529908cb 22230 file_name_index file_index
aa659cfa 22231 = (file_name_index) attr->as_nonnegative ();
8c43009f 22232 struct file_entry *fe;
9a619af0 22233
ecfb656c
PA
22234 if (cu->line_header != NULL)
22235 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
22236 else
22237 fe = NULL;
22238
22239 if (fe == NULL)
b98664d3 22240 complaint (_("file index out of range"));
8c43009f
PA
22241 else
22242 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
22243 }
22244
c906108c
SS
22245 switch (die->tag)
22246 {
22247 case DW_TAG_label:
e142c38c 22248 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 22249 if (attr != nullptr)
3e29f34a
MR
22250 {
22251 CORE_ADDR addr;
22252
95f982e5 22253 addr = attr->as_address ();
3e29f34a 22254 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 22255 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
8f5c6526 22256 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
3e29f34a 22257 }
8f5c6526
TV
22258 else
22259 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
0f5238ed
TT
22260 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22261 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
d3cb6808 22262 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
22263 break;
22264 case DW_TAG_subprogram:
22265 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22266 finish_block. */
f1e6e072 22267 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 22268 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 22269 if ((attr2 != nullptr && attr2->as_boolean ())
0a4b0913
AB
22270 || cu->language == language_ada
22271 || cu->language == language_fortran)
c906108c 22272 {
dda83cd7
SM
22273 /* Subprograms marked external are stored as a global symbol.
22274 Ada and Fortran subprograms, whether marked external or
22275 not, are always stored as a global symbol, because we want
22276 to be able to access them globally. For instance, we want
22277 to be able to break on a nested subprogram without having
22278 to specify the context. */
c24bdb02 22279 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
22280 }
22281 else
22282 {
e37fd15a 22283 list_to_add = cu->list_in_scope;
c906108c
SS
22284 }
22285 break;
edb3359d
DJ
22286 case DW_TAG_inlined_subroutine:
22287 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22288 finish_block. */
f1e6e072 22289 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 22290 SYMBOL_INLINED (sym) = 1;
481860b3 22291 list_to_add = cu->list_in_scope;
edb3359d 22292 break;
34eaf542
TT
22293 case DW_TAG_template_value_param:
22294 suppress_add = 1;
22295 /* Fall through. */
72929c62 22296 case DW_TAG_constant:
c906108c 22297 case DW_TAG_variable:
254e6b9e 22298 case DW_TAG_member:
0963b4bd
MS
22299 /* Compilation with minimal debug info may result in
22300 variables with missing type entries. Change the
22301 misleading `void' type to something sensible. */
78134374 22302 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 22303 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 22304
e142c38c 22305 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
22306 /* In the case of DW_TAG_member, we should only be called for
22307 static const members. */
22308 if (die->tag == DW_TAG_member)
22309 {
3863f96c
DE
22310 /* dwarf2_add_field uses die_is_declaration,
22311 so we do the same. */
254e6b9e
DE
22312 gdb_assert (die_is_declaration (die, cu));
22313 gdb_assert (attr);
22314 }
435d3d88 22315 if (attr != nullptr)
c906108c 22316 {
e7c27a73 22317 dwarf2_const_value (attr, sym, cu);
e142c38c 22318 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 22319 if (!suppress_add)
34eaf542 22320 {
c45bc3f8 22321 if (attr2 != nullptr && attr2->as_boolean ())
c24bdb02 22322 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 22323 else
e37fd15a 22324 list_to_add = cu->list_in_scope;
34eaf542 22325 }
c906108c
SS
22326 break;
22327 }
e142c38c 22328 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22329 if (attr != nullptr)
c906108c 22330 {
e7c27a73 22331 var_decode_location (attr, sym, cu);
e142c38c 22332 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
22333
22334 /* Fortran explicitly imports any global symbols to the local
22335 scope by DW_TAG_common_block. */
22336 if (cu->language == language_fortran && die->parent
22337 && die->parent->tag == DW_TAG_common_block)
22338 attr2 = NULL;
22339
caac4577
JG
22340 if (SYMBOL_CLASS (sym) == LOC_STATIC
22341 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 22342 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
22343 {
22344 /* When a static variable is eliminated by the linker,
22345 the corresponding debug information is not stripped
22346 out, but the variable address is set to null;
22347 do not add such variables into symbol table. */
22348 }
c45bc3f8 22349 else if (attr2 != nullptr && attr2->as_boolean ())
1c809c68 22350 {
4b610737
TT
22351 if (SYMBOL_CLASS (sym) == LOC_STATIC
22352 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 22353 && per_objfile->per_bfd->can_copy)
4b610737
TT
22354 {
22355 /* A global static variable might be subject to
22356 copy relocation. We first check for a local
22357 minsym, though, because maybe the symbol was
22358 marked hidden, in which case this would not
22359 apply. */
22360 bound_minimal_symbol found
22361 = (lookup_minimal_symbol_linkage
987012b8 22362 (sym->linkage_name (), objfile));
4b610737
TT
22363 if (found.minsym != nullptr)
22364 sym->maybe_copied = 1;
22365 }
f55ee35c 22366
1c809c68
TT
22367 /* A variable with DW_AT_external is never static,
22368 but it may be block-scoped. */
804d2729 22369 list_to_add
c24bdb02
KS
22370 = ((cu->list_in_scope
22371 == cu->get_builder ()->get_file_symbols ())
22372 ? cu->get_builder ()->get_global_symbols ()
804d2729 22373 : cu->list_in_scope);
1c809c68 22374 }
c906108c 22375 else
e37fd15a 22376 list_to_add = cu->list_in_scope;
c906108c
SS
22377 }
22378 else
22379 {
22380 /* We do not know the address of this symbol.
dda83cd7
SM
22381 If it is an external symbol and we have type information
22382 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22383 The address of the variable will then be determined from
22384 the minimal symbol table whenever the variable is
22385 referenced. */
e142c38c 22386 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
22387
22388 /* Fortran explicitly imports any global symbols to the local
22389 scope by DW_TAG_common_block. */
22390 if (cu->language == language_fortran && die->parent
22391 && die->parent->tag == DW_TAG_common_block)
22392 {
22393 /* SYMBOL_CLASS doesn't matter here because
22394 read_common_block is going to reset it. */
22395 if (!suppress_add)
22396 list_to_add = cu->list_in_scope;
22397 }
c45bc3f8 22398 else if (attr2 != nullptr && attr2->as_boolean ()
0971de02 22399 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 22400 {
0fe7935b
DJ
22401 /* A variable with DW_AT_external is never static, but it
22402 may be block-scoped. */
804d2729 22403 list_to_add
c24bdb02
KS
22404 = ((cu->list_in_scope
22405 == cu->get_builder ()->get_file_symbols ())
22406 ? cu->get_builder ()->get_global_symbols ()
804d2729 22407 : cu->list_in_scope);
0fe7935b 22408
f1e6e072 22409 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 22410 }
442ddf59
JK
22411 else if (!die_is_declaration (die, cu))
22412 {
22413 /* Use the default LOC_OPTIMIZED_OUT class. */
22414 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22415 if (!suppress_add)
22416 list_to_add = cu->list_in_scope;
442ddf59 22417 }
c906108c
SS
22418 }
22419 break;
22420 case DW_TAG_formal_parameter:
a60f3166
TT
22421 {
22422 /* If we are inside a function, mark this as an argument. If
22423 not, we might be looking at an argument to an inlined function
22424 when we do not have enough information to show inlined frames;
22425 pretend it's a local variable in that case so that the user can
22426 still see it. */
804d2729 22427 struct context_stack *curr
c24bdb02 22428 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
22429 if (curr != nullptr && curr->name != nullptr)
22430 SYMBOL_IS_ARGUMENT (sym) = 1;
22431 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22432 if (attr != nullptr)
a60f3166
TT
22433 {
22434 var_decode_location (attr, sym, cu);
22435 }
22436 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22437 if (attr != nullptr)
a60f3166
TT
22438 {
22439 dwarf2_const_value (attr, sym, cu);
22440 }
f346a30d 22441
a60f3166
TT
22442 list_to_add = cu->list_in_scope;
22443 }
c906108c
SS
22444 break;
22445 case DW_TAG_unspecified_parameters:
22446 /* From varargs functions; gdb doesn't seem to have any
22447 interest in this information, so just ignore it for now.
22448 (FIXME?) */
22449 break;
34eaf542
TT
22450 case DW_TAG_template_type_param:
22451 suppress_add = 1;
22452 /* Fall through. */
c906108c 22453 case DW_TAG_class_type:
680b30c7 22454 case DW_TAG_interface_type:
c906108c
SS
22455 case DW_TAG_structure_type:
22456 case DW_TAG_union_type:
72019c9c 22457 case DW_TAG_set_type:
c906108c 22458 case DW_TAG_enumeration_type:
f1e6e072 22459 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22460 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22461
63d06c5c 22462 {
9c37b5ae 22463 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22464 really ever be static objects: otherwise, if you try
22465 to, say, break of a class's method and you're in a file
22466 which doesn't mention that class, it won't work unless
22467 the check for all static symbols in lookup_symbol_aux
22468 saves you. See the OtherFileClass tests in
22469 gdb.c++/namespace.exp. */
22470
e37fd15a 22471 if (!suppress_add)
34eaf542 22472 {
c24bdb02 22473 buildsym_compunit *builder = cu->get_builder ();
804d2729 22474 list_to_add
c24bdb02 22475 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22476 && cu->language == language_cplus
c24bdb02 22477 ? builder->get_global_symbols ()
804d2729 22478 : cu->list_in_scope);
63d06c5c 22479
64382290 22480 /* The semantics of C++ state that "struct foo {
9c37b5ae 22481 ... }" also defines a typedef for "foo". */
64382290 22482 if (cu->language == language_cplus
45280282 22483 || cu->language == language_ada
c44af4eb
TT
22484 || cu->language == language_d
22485 || cu->language == language_rust)
64382290
TT
22486 {
22487 /* The symbol's name is already allocated along
22488 with this objfile, so we don't need to
22489 duplicate it for the type. */
7d93a1e0 22490 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 22491 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 22492 }
63d06c5c
DC
22493 }
22494 }
c906108c
SS
22495 break;
22496 case DW_TAG_typedef:
f1e6e072 22497 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22498 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22499 list_to_add = cu->list_in_scope;
63d06c5c 22500 break;
d8f62e84 22501 case DW_TAG_array_type:
c906108c 22502 case DW_TAG_base_type:
dda83cd7 22503 case DW_TAG_subrange_type:
f1e6e072 22504 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22505 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22506 list_to_add = cu->list_in_scope;
c906108c
SS
22507 break;
22508 case DW_TAG_enumerator:
e142c38c 22509 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22510 if (attr != nullptr)
c906108c 22511 {
e7c27a73 22512 dwarf2_const_value (attr, sym, cu);
c906108c 22513 }
63d06c5c
DC
22514 {
22515 /* NOTE: carlton/2003-11-10: See comment above in the
22516 DW_TAG_class_type, etc. block. */
22517
804d2729 22518 list_to_add
c24bdb02 22519 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22520 && cu->language == language_cplus
c24bdb02 22521 ? cu->get_builder ()->get_global_symbols ()
804d2729 22522 : cu->list_in_scope);
63d06c5c 22523 }
c906108c 22524 break;
74921315 22525 case DW_TAG_imported_declaration:
5c4e30ca 22526 case DW_TAG_namespace:
f1e6e072 22527 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22528 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22529 break;
530e8392
KB
22530 case DW_TAG_module:
22531 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22532 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22533 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22534 break;
4357ac6c 22535 case DW_TAG_common_block:
f1e6e072 22536 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22537 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22538 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22539 break;
c906108c
SS
22540 default:
22541 /* Not a tag we recognize. Hopefully we aren't processing
22542 trash data, but since we must specifically ignore things
22543 we don't recognize, there is nothing else we should do at
0963b4bd 22544 this point. */
b98664d3 22545 complaint (_("unsupported tag: '%s'"),
4d3c2250 22546 dwarf_tag_name (die->tag));
c906108c
SS
22547 break;
22548 }
df8a16a1 22549
e37fd15a
SW
22550 if (suppress_add)
22551 {
22552 sym->hash_next = objfile->template_symbols;
22553 objfile->template_symbols = sym;
22554 list_to_add = NULL;
22555 }
22556
22557 if (list_to_add != NULL)
d3cb6808 22558 add_symbol_to_list (sym, list_to_add);
e37fd15a 22559
df8a16a1
DJ
22560 /* For the benefit of old versions of GCC, check for anonymous
22561 namespaces based on the demangled name. */
4d4ec4e5 22562 if (!cu->processing_has_namespace_info
94af9270 22563 && cu->language == language_cplus)
c24bdb02 22564 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22565 }
22566 return (sym);
22567}
22568
98bfdba5
PA
22569/* Given an attr with a DW_FORM_dataN value in host byte order,
22570 zero-extend it as appropriate for the symbol's type. The DWARF
22571 standard (v4) is not entirely clear about the meaning of using
22572 DW_FORM_dataN for a constant with a signed type, where the type is
22573 wider than the data. The conclusion of a discussion on the DWARF
22574 list was that this is unspecified. We choose to always zero-extend
22575 because that is the interpretation long in use by GCC. */
c906108c 22576
98bfdba5 22577static gdb_byte *
ff39bb5e 22578dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22579 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22580{
5e22e966 22581 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
22582 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22583 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
529908cb 22584 LONGEST l = attr->constant_value (0);
98bfdba5
PA
22585
22586 if (bits < sizeof (*value) * 8)
22587 {
22588 l &= ((LONGEST) 1 << bits) - 1;
22589 *value = l;
22590 }
22591 else if (bits == sizeof (*value) * 8)
22592 *value = l;
22593 else
22594 {
224c3ddb 22595 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22596 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22597 return bytes;
22598 }
22599
22600 return NULL;
22601}
22602
22603/* Read a constant value from an attribute. Either set *VALUE, or if
22604 the value does not fit in *VALUE, set *BYTES - either already
22605 allocated on the objfile obstack, or newly allocated on OBSTACK,
22606 or, set *BATON, if we translated the constant to a location
22607 expression. */
22608
22609static void
ff39bb5e 22610dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22611 const char *name, struct obstack *obstack,
22612 struct dwarf2_cu *cu,
d521ce57 22613 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22614 struct dwarf2_locexpr_baton **baton)
22615{
5e22e966 22616 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 22617 struct objfile *objfile = per_objfile->objfile;
98bfdba5 22618 struct comp_unit_head *cu_header = &cu->header;
c906108c 22619 struct dwarf_block *blk;
98bfdba5
PA
22620 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22621 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22622
22623 *value = 0;
22624 *bytes = NULL;
22625 *baton = NULL;
c906108c
SS
22626
22627 switch (attr->form)
22628 {
22629 case DW_FORM_addr:
336d760d 22630 case DW_FORM_addrx:
3019eac3 22631 case DW_FORM_GNU_addr_index:
ac56253d 22632 {
ac56253d
TT
22633 gdb_byte *data;
22634
98bfdba5
PA
22635 if (TYPE_LENGTH (type) != cu_header->addr_size)
22636 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22637 cu_header->addr_size,
98bfdba5 22638 TYPE_LENGTH (type));
ac56253d
TT
22639 /* Symbols of this form are reasonably rare, so we just
22640 piggyback on the existing location code rather than writing
22641 a new implementation of symbol_computed_ops. */
8d749320 22642 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 22643 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
22644 (*baton)->per_cu = cu->per_cu;
22645 gdb_assert ((*baton)->per_cu);
ac56253d 22646
98bfdba5 22647 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22648 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22649 (*baton)->data = data;
ac56253d
TT
22650
22651 data[0] = DW_OP_addr;
22652 store_unsigned_integer (&data[1], cu_header->addr_size,
36d378cf 22653 byte_order, attr->as_address ());
ac56253d 22654 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22655 }
c906108c 22656 break;
4ac36638 22657 case DW_FORM_string:
93b5768b 22658 case DW_FORM_strp:
cf532bd1 22659 case DW_FORM_strx:
3019eac3 22660 case DW_FORM_GNU_str_index:
36586728 22661 case DW_FORM_GNU_strp_alt:
c6481205 22662 /* The string is already allocated on the objfile obstack, point
98bfdba5 22663 directly to it. */
2c830f54 22664 *bytes = (const gdb_byte *) attr->as_string ();
93b5768b 22665 break;
c906108c
SS
22666 case DW_FORM_block1:
22667 case DW_FORM_block2:
22668 case DW_FORM_block4:
22669 case DW_FORM_block:
2dc7f7b3 22670 case DW_FORM_exprloc:
0224619f 22671 case DW_FORM_data16:
9d2246fc 22672 blk = attr->as_block ();
98bfdba5
PA
22673 if (TYPE_LENGTH (type) != blk->size)
22674 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22675 TYPE_LENGTH (type));
22676 *bytes = blk->data;
c906108c 22677 break;
2df3850c
JM
22678
22679 /* The DW_AT_const_value attributes are supposed to carry the
22680 symbol's value "represented as it would be on the target
22681 architecture." By the time we get here, it's already been
22682 converted to host endianness, so we just need to sign- or
22683 zero-extend it as appropriate. */
22684 case DW_FORM_data1:
3aef2284 22685 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22686 break;
c906108c 22687 case DW_FORM_data2:
3aef2284 22688 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22689 break;
c906108c 22690 case DW_FORM_data4:
3aef2284 22691 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22692 break;
c906108c 22693 case DW_FORM_data8:
3aef2284 22694 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22695 break;
22696
c906108c 22697 case DW_FORM_sdata:
663c44ac 22698 case DW_FORM_implicit_const:
1bc397c5 22699 *value = attr->as_signed ();
2df3850c
JM
22700 break;
22701
c906108c 22702 case DW_FORM_udata:
529908cb 22703 *value = attr->as_unsigned ();
c906108c 22704 break;
2df3850c 22705
c906108c 22706 default:
b98664d3 22707 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22708 dwarf_form_name (attr->form));
98bfdba5 22709 *value = 0;
c906108c
SS
22710 break;
22711 }
22712}
22713
2df3850c 22714
98bfdba5
PA
22715/* Copy constant value from an attribute to a symbol. */
22716
2df3850c 22717static void
ff39bb5e 22718dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22719 struct dwarf2_cu *cu)
2df3850c 22720{
5e22e966 22721 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 22722 LONGEST value;
d521ce57 22723 const gdb_byte *bytes;
98bfdba5 22724 struct dwarf2_locexpr_baton *baton;
2df3850c 22725
98bfdba5 22726 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22727 sym->print_name (),
98bfdba5
PA
22728 &objfile->objfile_obstack, cu,
22729 &value, &bytes, &baton);
2df3850c 22730
98bfdba5
PA
22731 if (baton != NULL)
22732 {
98bfdba5 22733 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22734 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22735 }
22736 else if (bytes != NULL)
22737 {
22738 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22739 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22740 }
22741 else
22742 {
22743 SYMBOL_VALUE (sym) = value;
f1e6e072 22744 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22745 }
2df3850c
JM
22746}
22747
c906108c
SS
22748/* Return the type of the die in question using its DW_AT_type attribute. */
22749
22750static struct type *
e7c27a73 22751die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22752{
c906108c 22753 struct attribute *type_attr;
c906108c 22754
e142c38c 22755 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22756 if (!type_attr)
22757 {
5e22e966 22758 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22759 /* A missing DW_AT_type represents a void type. */
518817b3 22760 return objfile_type (objfile)->builtin_void;
c906108c 22761 }
348e048f 22762
673bfd45 22763 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22764}
22765
b4ba55a1
JB
22766/* True iff CU's producer generates GNAT Ada auxiliary information
22767 that allows to find parallel types through that information instead
22768 of having to do expensive parallel lookups by type name. */
22769
22770static int
22771need_gnat_info (struct dwarf2_cu *cu)
22772{
de4cb04a
JB
22773 /* Assume that the Ada compiler was GNAT, which always produces
22774 the auxiliary information. */
22775 return (cu->language == language_ada);
b4ba55a1
JB
22776}
22777
b4ba55a1
JB
22778/* Return the auxiliary type of the die in question using its
22779 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22780 attribute is not present. */
22781
22782static struct type *
22783die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22784{
b4ba55a1 22785 struct attribute *type_attr;
b4ba55a1
JB
22786
22787 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22788 if (!type_attr)
22789 return NULL;
22790
673bfd45 22791 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22792}
22793
22794/* If DIE has a descriptive_type attribute, then set the TYPE's
22795 descriptive type accordingly. */
22796
22797static void
22798set_descriptive_type (struct type *type, struct die_info *die,
22799 struct dwarf2_cu *cu)
22800{
22801 struct type *descriptive_type = die_descriptive_type (die, cu);
22802
22803 if (descriptive_type)
22804 {
22805 ALLOCATE_GNAT_AUX_TYPE (type);
22806 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22807 }
22808}
22809
c906108c
SS
22810/* Return the containing type of the die in question using its
22811 DW_AT_containing_type attribute. */
22812
22813static struct type *
e7c27a73 22814die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22815{
c906108c 22816 struct attribute *type_attr;
5e22e966 22817 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22818
e142c38c 22819 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22820 if (!type_attr)
22821 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22822 "[in module %s]"), objfile_name (objfile));
33ac96f0 22823
673bfd45 22824 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22825}
22826
ac9ec31b
DE
22827/* Return an error marker type to use for the ill formed type in DIE/CU. */
22828
22829static struct type *
22830build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22831{
976ca316
SM
22832 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22833 struct objfile *objfile = per_objfile->objfile;
528e1572 22834 char *saved;
ac9ec31b 22835
528e1572
SM
22836 std::string message
22837 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22838 objfile_name (objfile),
22839 sect_offset_str (cu->header.sect_off),
22840 sect_offset_str (die->sect_off));
efba19b0 22841 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22842
19f392bc 22843 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22844}
22845
673bfd45 22846/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22847 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22848 DW_AT_containing_type.
673bfd45
DE
22849 If there is no type substitute an error marker. */
22850
c906108c 22851static struct type *
ff39bb5e 22852lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22853 struct dwarf2_cu *cu)
c906108c 22854{
976ca316
SM
22855 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22856 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
22857 struct type *this_type;
22858
ac9ec31b
DE
22859 gdb_assert (attr->name == DW_AT_type
22860 || attr->name == DW_AT_GNAT_descriptive_type
22861 || attr->name == DW_AT_containing_type);
22862
673bfd45
DE
22863 /* First see if we have it cached. */
22864
36586728
TT
22865 if (attr->form == DW_FORM_GNU_ref_alt)
22866 {
22867 struct dwarf2_per_cu_data *per_cu;
0826b30a 22868 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 22869
976ca316
SM
22870 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22871 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 22872 }
cd6c91b4 22873 else if (attr->form_is_ref ())
673bfd45 22874 {
0826b30a 22875 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 22876
976ca316 22877 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 22878 }
55f1336d 22879 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22880 {
630ed6b9 22881 ULONGEST signature = attr->as_signature ();
673bfd45 22882
ac9ec31b 22883 return get_signatured_type (die, signature, cu);
673bfd45
DE
22884 }
22885 else
22886 {
b98664d3 22887 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22888 " at %s [in module %s]"),
22889 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22890 objfile_name (objfile));
ac9ec31b 22891 return build_error_marker_type (cu, die);
673bfd45
DE
22892 }
22893
22894 /* If not cached we need to read it in. */
22895
22896 if (this_type == NULL)
22897 {
ac9ec31b 22898 struct die_info *type_die = NULL;
673bfd45
DE
22899 struct dwarf2_cu *type_cu = cu;
22900
cd6c91b4 22901 if (attr->form_is_ref ())
ac9ec31b
DE
22902 type_die = follow_die_ref (die, attr, &type_cu);
22903 if (type_die == NULL)
22904 return build_error_marker_type (cu, die);
22905 /* If we find the type now, it's probably because the type came
3019eac3
DE
22906 from an inter-CU reference and the type's CU got expanded before
22907 ours. */
ac9ec31b 22908 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22909 }
22910
22911 /* If we still don't have a type use an error marker. */
22912
22913 if (this_type == NULL)
ac9ec31b 22914 return build_error_marker_type (cu, die);
673bfd45 22915
f792889a 22916 return this_type;
c906108c
SS
22917}
22918
673bfd45
DE
22919/* Return the type in DIE, CU.
22920 Returns NULL for invalid types.
22921
02142a6c 22922 This first does a lookup in die_type_hash,
673bfd45
DE
22923 and only reads the die in if necessary.
22924
22925 NOTE: This can be called when reading in partial or full symbols. */
22926
f792889a 22927static struct type *
e7c27a73 22928read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22929{
f792889a
DJ
22930 struct type *this_type;
22931
22932 this_type = get_die_type (die, cu);
22933 if (this_type)
22934 return this_type;
22935
673bfd45
DE
22936 return read_type_die_1 (die, cu);
22937}
22938
22939/* Read the type in DIE, CU.
22940 Returns NULL for invalid types. */
22941
22942static struct type *
22943read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22944{
22945 struct type *this_type = NULL;
22946
c906108c
SS
22947 switch (die->tag)
22948 {
22949 case DW_TAG_class_type:
680b30c7 22950 case DW_TAG_interface_type:
c906108c
SS
22951 case DW_TAG_structure_type:
22952 case DW_TAG_union_type:
f792889a 22953 this_type = read_structure_type (die, cu);
c906108c
SS
22954 break;
22955 case DW_TAG_enumeration_type:
f792889a 22956 this_type = read_enumeration_type (die, cu);
c906108c
SS
22957 break;
22958 case DW_TAG_subprogram:
22959 case DW_TAG_subroutine_type:
edb3359d 22960 case DW_TAG_inlined_subroutine:
f792889a 22961 this_type = read_subroutine_type (die, cu);
c906108c
SS
22962 break;
22963 case DW_TAG_array_type:
f792889a 22964 this_type = read_array_type (die, cu);
c906108c 22965 break;
72019c9c 22966 case DW_TAG_set_type:
f792889a 22967 this_type = read_set_type (die, cu);
72019c9c 22968 break;
c906108c 22969 case DW_TAG_pointer_type:
f792889a 22970 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22971 break;
22972 case DW_TAG_ptr_to_member_type:
f792889a 22973 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22974 break;
22975 case DW_TAG_reference_type:
4297a3f0
AV
22976 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22977 break;
22978 case DW_TAG_rvalue_reference_type:
22979 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22980 break;
22981 case DW_TAG_const_type:
f792889a 22982 this_type = read_tag_const_type (die, cu);
c906108c
SS
22983 break;
22984 case DW_TAG_volatile_type:
f792889a 22985 this_type = read_tag_volatile_type (die, cu);
c906108c 22986 break;
06d66ee9
TT
22987 case DW_TAG_restrict_type:
22988 this_type = read_tag_restrict_type (die, cu);
22989 break;
c906108c 22990 case DW_TAG_string_type:
f792889a 22991 this_type = read_tag_string_type (die, cu);
c906108c
SS
22992 break;
22993 case DW_TAG_typedef:
f792889a 22994 this_type = read_typedef (die, cu);
c906108c 22995 break;
a02abb62 22996 case DW_TAG_subrange_type:
f792889a 22997 this_type = read_subrange_type (die, cu);
a02abb62 22998 break;
c906108c 22999 case DW_TAG_base_type:
f792889a 23000 this_type = read_base_type (die, cu);
c906108c 23001 break;
81a17f79 23002 case DW_TAG_unspecified_type:
f792889a 23003 this_type = read_unspecified_type (die, cu);
81a17f79 23004 break;
0114d602
DJ
23005 case DW_TAG_namespace:
23006 this_type = read_namespace_type (die, cu);
23007 break;
f55ee35c
JK
23008 case DW_TAG_module:
23009 this_type = read_module_type (die, cu);
23010 break;
a2c2acaf
MW
23011 case DW_TAG_atomic_type:
23012 this_type = read_tag_atomic_type (die, cu);
23013 break;
c906108c 23014 default:
b98664d3 23015 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 23016 dwarf_tag_name (die->tag));
c906108c
SS
23017 break;
23018 }
63d06c5c 23019
f792889a 23020 return this_type;
63d06c5c
DC
23021}
23022
abc72ce4
DE
23023/* See if we can figure out if the class lives in a namespace. We do
23024 this by looking for a member function; its demangled name will
23025 contain namespace info, if there is any.
23026 Return the computed name or NULL.
23027 Space for the result is allocated on the objfile's obstack.
23028 This is the full-die version of guess_partial_die_structure_name.
23029 In this case we know DIE has no useful parent. */
23030
43816ebc 23031static const char *
abc72ce4
DE
23032guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
23033{
23034 struct die_info *spec_die;
23035 struct dwarf2_cu *spec_cu;
23036 struct die_info *child;
5e22e966 23037 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
23038
23039 spec_cu = cu;
23040 spec_die = die_specification (die, &spec_cu);
23041 if (spec_die != NULL)
23042 {
23043 die = spec_die;
23044 cu = spec_cu;
23045 }
23046
23047 for (child = die->child;
23048 child != NULL;
23049 child = child->sibling)
23050 {
23051 if (child->tag == DW_TAG_subprogram)
23052 {
73b9be8b 23053 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 23054
7d45c7c3 23055 if (linkage_name != NULL)
abc72ce4 23056 {
43816ebc 23057 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 23058 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 23059 const char *name = NULL;
abc72ce4
DE
23060
23061 if (actual_name != NULL)
23062 {
15d034d0 23063 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
23064
23065 if (die_name != NULL
43816ebc 23066 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
23067 {
23068 /* Strip off the class name from the full name.
23069 We want the prefix. */
23070 int die_name_len = strlen (die_name);
43816ebc
TT
23071 int actual_name_len = strlen (actual_name.get ());
23072 const char *ptr = actual_name.get ();
abc72ce4
DE
23073
23074 /* Test for '::' as a sanity check. */
23075 if (actual_name_len > die_name_len + 2
43816ebc 23076 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 23077 name = obstack_strndup (
e3b94546 23078 &objfile->per_bfd->storage_obstack,
43816ebc 23079 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
23080 }
23081 }
abc72ce4
DE
23082 return name;
23083 }
23084 }
23085 }
23086
23087 return NULL;
23088}
23089
96408a79
SA
23090/* GCC might emit a nameless typedef that has a linkage name. Determine the
23091 prefix part in such case. See
23092 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23093
a121b7c1 23094static const char *
96408a79
SA
23095anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
23096{
23097 struct attribute *attr;
e6a959d6 23098 const char *base;
96408a79
SA
23099
23100 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
23101 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
23102 return NULL;
23103
7d45c7c3 23104 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
23105 return NULL;
23106
73b9be8b 23107 attr = dw2_linkage_name_attr (die, cu);
2c830f54
TT
23108 const char *attr_name = attr->as_string ();
23109 if (attr == NULL || attr_name == NULL)
96408a79
SA
23110 return NULL;
23111
23112 /* dwarf2_name had to be already called. */
3b64bf15 23113 gdb_assert (attr->canonical_string_p ());
96408a79
SA
23114
23115 /* Strip the base name, keep any leading namespaces/classes. */
2c830f54
TT
23116 base = strrchr (attr_name, ':');
23117 if (base == NULL || base == attr_name || base[-1] != ':')
96408a79
SA
23118 return "";
23119
5e22e966 23120 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9 23121 return obstack_strndup (&objfile->per_bfd->storage_obstack,
2c830f54
TT
23122 attr_name,
23123 &base[-1] - attr_name);
96408a79
SA
23124}
23125
fdde2d81 23126/* Return the name of the namespace/class that DIE is defined within,
0114d602 23127 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 23128
0114d602
DJ
23129 For example, if we're within the method foo() in the following
23130 code:
23131
23132 namespace N {
23133 class C {
23134 void foo () {
23135 }
23136 };
23137 }
23138
23139 then determine_prefix on foo's die will return "N::C". */
fdde2d81 23140
0d5cff50 23141static const char *
e142c38c 23142determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 23143{
976ca316 23144 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
23145 struct die_info *parent, *spec_die;
23146 struct dwarf2_cu *spec_cu;
23147 struct type *parent_type;
a121b7c1 23148 const char *retval;
63d06c5c 23149
9c37b5ae 23150 if (cu->language != language_cplus
c44af4eb
TT
23151 && cu->language != language_fortran && cu->language != language_d
23152 && cu->language != language_rust)
0114d602
DJ
23153 return "";
23154
96408a79
SA
23155 retval = anonymous_struct_prefix (die, cu);
23156 if (retval)
23157 return retval;
23158
0114d602
DJ
23159 /* We have to be careful in the presence of DW_AT_specification.
23160 For example, with GCC 3.4, given the code
23161
23162 namespace N {
23163 void foo() {
23164 // Definition of N::foo.
23165 }
23166 }
23167
23168 then we'll have a tree of DIEs like this:
23169
23170 1: DW_TAG_compile_unit
23171 2: DW_TAG_namespace // N
23172 3: DW_TAG_subprogram // declaration of N::foo
23173 4: DW_TAG_subprogram // definition of N::foo
23174 DW_AT_specification // refers to die #3
23175
23176 Thus, when processing die #4, we have to pretend that we're in
23177 the context of its DW_AT_specification, namely the contex of die
23178 #3. */
23179 spec_cu = cu;
23180 spec_die = die_specification (die, &spec_cu);
23181 if (spec_die == NULL)
23182 parent = die->parent;
23183 else
63d06c5c 23184 {
0114d602
DJ
23185 parent = spec_die->parent;
23186 cu = spec_cu;
63d06c5c 23187 }
0114d602
DJ
23188
23189 if (parent == NULL)
23190 return "";
98bfdba5
PA
23191 else if (parent->building_fullname)
23192 {
23193 const char *name;
23194 const char *parent_name;
23195
23196 /* It has been seen on RealView 2.2 built binaries,
23197 DW_TAG_template_type_param types actually _defined_ as
23198 children of the parent class:
23199
23200 enum E {};
23201 template class <class Enum> Class{};
23202 Class<enum E> class_e;
23203
dda83cd7
SM
23204 1: DW_TAG_class_type (Class)
23205 2: DW_TAG_enumeration_type (E)
23206 3: DW_TAG_enumerator (enum1:0)
23207 3: DW_TAG_enumerator (enum2:1)
23208 ...
23209 2: DW_TAG_template_type_param
23210 DW_AT_type DW_FORM_ref_udata (E)
98bfdba5
PA
23211
23212 Besides being broken debug info, it can put GDB into an
23213 infinite loop. Consider:
23214
23215 When we're building the full name for Class<E>, we'll start
23216 at Class, and go look over its template type parameters,
23217 finding E. We'll then try to build the full name of E, and
23218 reach here. We're now trying to build the full name of E,
23219 and look over the parent DIE for containing scope. In the
23220 broken case, if we followed the parent DIE of E, we'd again
23221 find Class, and once again go look at its template type
23222 arguments, etc., etc. Simply don't consider such parent die
23223 as source-level parent of this die (it can't be, the language
23224 doesn't allow it), and break the loop here. */
23225 name = dwarf2_name (die, cu);
23226 parent_name = dwarf2_name (parent, cu);
b98664d3 23227 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
23228 name ? name : "<unknown>",
23229 parent_name ? parent_name : "<unknown>");
23230 return "";
23231 }
63d06c5c 23232 else
0114d602
DJ
23233 switch (parent->tag)
23234 {
63d06c5c 23235 case DW_TAG_namespace:
0114d602 23236 parent_type = read_type_die (parent, cu);
acebe513
UW
23237 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23238 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23239 Work around this problem here. */
23240 if (cu->language == language_cplus
7d93a1e0 23241 && strcmp (parent_type->name (), "::") == 0)
acebe513 23242 return "";
0114d602 23243 /* We give a name to even anonymous namespaces. */
7d93a1e0 23244 return parent_type->name ();
63d06c5c 23245 case DW_TAG_class_type:
680b30c7 23246 case DW_TAG_interface_type:
63d06c5c 23247 case DW_TAG_structure_type:
0114d602 23248 case DW_TAG_union_type:
f55ee35c 23249 case DW_TAG_module:
0114d602 23250 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
23251 if (parent_type->name () != NULL)
23252 return parent_type->name ();
0114d602
DJ
23253 else
23254 /* An anonymous structure is only allowed non-static data
23255 members; no typedefs, no member functions, et cetera.
23256 So it does not need a prefix. */
23257 return "";
abc72ce4 23258 case DW_TAG_compile_unit:
95554aad 23259 case DW_TAG_partial_unit:
abc72ce4
DE
23260 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23261 if (cu->language == language_cplus
976ca316 23262 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
23263 && die->child != NULL
23264 && (die->tag == DW_TAG_class_type
23265 || die->tag == DW_TAG_structure_type
23266 || die->tag == DW_TAG_union_type))
23267 {
43816ebc 23268 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
23269 if (name != NULL)
23270 return name;
23271 }
23272 return "";
0a4b0913
AB
23273 case DW_TAG_subprogram:
23274 /* Nested subroutines in Fortran get a prefix with the name
23275 of the parent's subroutine. */
23276 if (cu->language == language_fortran)
23277 {
23278 if ((die->tag == DW_TAG_subprogram)
23279 && (dwarf2_name (parent, cu) != NULL))
23280 return dwarf2_name (parent, cu);
23281 }
23282 return determine_prefix (parent, cu);
3d567982
TT
23283 case DW_TAG_enumeration_type:
23284 parent_type = read_type_die (parent, cu);
23285 if (TYPE_DECLARED_CLASS (parent_type))
23286 {
7d93a1e0
SM
23287 if (parent_type->name () != NULL)
23288 return parent_type->name ();
3d567982
TT
23289 return "";
23290 }
23291 /* Fall through. */
63d06c5c 23292 default:
8176b9b8 23293 return determine_prefix (parent, cu);
63d06c5c 23294 }
63d06c5c
DC
23295}
23296
3e43a32a
MS
23297/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23298 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23299 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23300 an obconcat, otherwise allocate storage for the result. The CU argument is
23301 used to determine the language and hence, the appropriate separator. */
987504bb 23302
f55ee35c 23303#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
23304
23305static char *
f55ee35c 23306typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
dda83cd7 23307 int physname, struct dwarf2_cu *cu)
63d06c5c 23308{
f55ee35c 23309 const char *lead = "";
5c315b68 23310 const char *sep;
63d06c5c 23311
3e43a32a
MS
23312 if (suffix == NULL || suffix[0] == '\0'
23313 || prefix == NULL || prefix[0] == '\0')
987504bb 23314 sep = "";
45280282
IB
23315 else if (cu->language == language_d)
23316 {
23317 /* For D, the 'main' function could be defined in any module, but it
23318 should never be prefixed. */
23319 if (strcmp (suffix, "D main") == 0)
23320 {
23321 prefix = "";
23322 sep = "";
23323 }
23324 else
23325 sep = ".";
23326 }
f55ee35c
JK
23327 else if (cu->language == language_fortran && physname)
23328 {
23329 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23330 DW_AT_MIPS_linkage_name is preferred and used instead. */
23331
23332 lead = "__";
23333 sep = "_MOD_";
23334 }
987504bb
JJ
23335 else
23336 sep = "::";
63d06c5c 23337
6dd47d34
DE
23338 if (prefix == NULL)
23339 prefix = "";
23340 if (suffix == NULL)
23341 suffix = "";
23342
987504bb
JJ
23343 if (obs == NULL)
23344 {
3e43a32a 23345 char *retval
224c3ddb
SM
23346 = ((char *)
23347 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 23348
f55ee35c
JK
23349 strcpy (retval, lead);
23350 strcat (retval, prefix);
6dd47d34
DE
23351 strcat (retval, sep);
23352 strcat (retval, suffix);
63d06c5c
DC
23353 return retval;
23354 }
987504bb
JJ
23355 else
23356 {
23357 /* We have an obstack. */
f55ee35c 23358 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 23359 }
63d06c5c
DC
23360}
23361
71c25dea
TT
23362/* Get name of a die, return NULL if not found. */
23363
15d034d0
TT
23364static const char *
23365dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 23366 struct objfile *objfile)
71c25dea
TT
23367{
23368 if (name && cu->language == language_cplus)
23369 {
596dc4ad
TT
23370 gdb::unique_xmalloc_ptr<char> canon_name
23371 = cp_canonicalize_string (name);
71c25dea 23372
596dc4ad
TT
23373 if (canon_name != nullptr)
23374 name = objfile->intern (canon_name.get ());
71c25dea
TT
23375 }
23376
23377 return name;
c906108c
SS
23378}
23379
96553a0c
DE
23380/* Get name of a die, return NULL if not found.
23381 Anonymous namespaces are converted to their magic string. */
9219021c 23382
15d034d0 23383static const char *
e142c38c 23384dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
23385{
23386 struct attribute *attr;
5e22e966 23387 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 23388
e142c38c 23389 attr = dwarf2_attr (die, DW_AT_name, cu);
2c830f54
TT
23390 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23391 if (attr_name == nullptr
96553a0c 23392 && die->tag != DW_TAG_namespace
53832f31
TT
23393 && die->tag != DW_TAG_class_type
23394 && die->tag != DW_TAG_interface_type
23395 && die->tag != DW_TAG_structure_type
23396 && die->tag != DW_TAG_union_type)
71c25dea
TT
23397 return NULL;
23398
23399 switch (die->tag)
23400 {
23401 case DW_TAG_compile_unit:
95554aad 23402 case DW_TAG_partial_unit:
71c25dea
TT
23403 /* Compilation units have a DW_AT_name that is a filename, not
23404 a source language identifier. */
23405 case DW_TAG_enumeration_type:
23406 case DW_TAG_enumerator:
23407 /* These tags always have simple identifiers already; no need
23408 to canonicalize them. */
2c830f54 23409 return attr_name;
907af001 23410
96553a0c 23411 case DW_TAG_namespace:
2c830f54
TT
23412 if (attr_name != nullptr)
23413 return attr_name;
96553a0c
DE
23414 return CP_ANONYMOUS_NAMESPACE_STR;
23415
907af001
UW
23416 case DW_TAG_class_type:
23417 case DW_TAG_interface_type:
23418 case DW_TAG_structure_type:
23419 case DW_TAG_union_type:
23420 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23421 structures or unions. These were of the form "._%d" in GCC 4.1,
23422 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23423 and GCC 4.4. We work around this problem by ignoring these. */
2c830f54
TT
23424 if (attr_name != nullptr
23425 && (startswith (attr_name, "._")
23426 || startswith (attr_name, "<anonymous")))
907af001 23427 return NULL;
53832f31
TT
23428
23429 /* GCC might emit a nameless typedef that has a linkage name. See
23430 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
2c830f54 23431 if (!attr || attr_name == NULL)
53832f31 23432 {
73b9be8b 23433 attr = dw2_linkage_name_attr (die, cu);
95eb9e54 23434 attr_name = attr == nullptr ? nullptr : attr->as_string ();
2c830f54 23435 if (attr == NULL || attr_name == NULL)
53832f31
TT
23436 return NULL;
23437
2c830f54 23438 /* Avoid demangling attr_name the second time on a second
df5c6c50 23439 call for the same DIE. */
3b64bf15 23440 if (!attr->canonical_string_p ())
53832f31 23441 {
43816ebc 23442 gdb::unique_xmalloc_ptr<char> demangled
2c830f54 23443 (gdb_demangle (attr_name, DMGL_TYPES));
4f180d53
AT
23444 if (demangled == nullptr)
23445 return nullptr;
43816ebc 23446
c6481205 23447 attr->set_string_canonical (objfile->intern (demangled.get ()));
95eb9e54 23448 attr_name = attr->as_string ();
53832f31 23449 }
67430cd0 23450
2c830f54
TT
23451 /* Strip any leading namespaces/classes, keep only the
23452 base name. DW_AT_name for named DIEs does not
23453 contain the prefixes. */
23454 const char *base = strrchr (attr_name, ':');
23455 if (base && base > attr_name && base[-1] == ':')
67430cd0
TT
23456 return &base[1];
23457 else
2c830f54 23458 return attr_name;
53832f31 23459 }
907af001
UW
23460 break;
23461
71c25dea 23462 default:
907af001
UW
23463 break;
23464 }
23465
3b64bf15 23466 if (!attr->canonical_string_p ())
c6481205
TT
23467 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23468 objfile));
2c830f54 23469 return attr->as_string ();
9219021c
DC
23470}
23471
23472/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23473 is none. *EXT_CU is the CU containing DIE on input, and the CU
23474 containing the return value on output. */
9219021c
DC
23475
23476static struct die_info *
f2f0e013 23477dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23478{
23479 struct attribute *attr;
9219021c 23480
f2f0e013 23481 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23482 if (attr == NULL)
23483 return NULL;
23484
f2f0e013 23485 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23486}
23487
f9aca02d 23488static void
d97bc12b 23489dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23490{
23491 unsigned int i;
23492
d97bc12b 23493 print_spaces (indent, f);
9d8780f0 23494 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23495 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23496 sect_offset_str (die->sect_off));
d97bc12b
DE
23497
23498 if (die->parent != NULL)
23499 {
23500 print_spaces (indent, f);
9d8780f0
SM
23501 fprintf_unfiltered (f, " parent at offset: %s\n",
23502 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23503 }
23504
23505 print_spaces (indent, f);
23506 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23507 dwarf_bool_name (die->child != NULL));
c906108c 23508
d97bc12b
DE
23509 print_spaces (indent, f);
23510 fprintf_unfiltered (f, " attributes:\n");
23511
c906108c
SS
23512 for (i = 0; i < die->num_attrs; ++i)
23513 {
d97bc12b
DE
23514 print_spaces (indent, f);
23515 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23516 dwarf_attr_name (die->attrs[i].name),
23517 dwarf_form_name (die->attrs[i].form));
d97bc12b 23518
c906108c
SS
23519 switch (die->attrs[i].form)
23520 {
c906108c 23521 case DW_FORM_addr:
336d760d 23522 case DW_FORM_addrx:
3019eac3 23523 case DW_FORM_GNU_addr_index:
d97bc12b 23524 fprintf_unfiltered (f, "address: ");
36d378cf 23525 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
c906108c
SS
23526 break;
23527 case DW_FORM_block2:
23528 case DW_FORM_block4:
23529 case DW_FORM_block:
23530 case DW_FORM_block1:
56eb65bd 23531 fprintf_unfiltered (f, "block: size %s",
9d2246fc 23532 pulongest (die->attrs[i].as_block ()->size));
c906108c 23533 break;
2dc7f7b3 23534 case DW_FORM_exprloc:
56eb65bd 23535 fprintf_unfiltered (f, "expression: size %s",
9d2246fc 23536 pulongest (die->attrs[i].as_block ()->size));
2dc7f7b3 23537 break;
0224619f
JK
23538 case DW_FORM_data16:
23539 fprintf_unfiltered (f, "constant of 16 bytes");
23540 break;
4568ecf9
DE
23541 case DW_FORM_ref_addr:
23542 fprintf_unfiltered (f, "ref address: ");
529908cb 23543 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
4568ecf9 23544 break;
36586728
TT
23545 case DW_FORM_GNU_ref_alt:
23546 fprintf_unfiltered (f, "alt ref address: ");
529908cb 23547 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
36586728 23548 break;
10b3939b
DJ
23549 case DW_FORM_ref1:
23550 case DW_FORM_ref2:
23551 case DW_FORM_ref4:
4568ecf9
DE
23552 case DW_FORM_ref8:
23553 case DW_FORM_ref_udata:
d97bc12b 23554 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
529908cb 23555 (long) (die->attrs[i].as_unsigned ()));
10b3939b 23556 break;
c906108c
SS
23557 case DW_FORM_data1:
23558 case DW_FORM_data2:
23559 case DW_FORM_data4:
ce5d95e1 23560 case DW_FORM_data8:
c906108c 23561 case DW_FORM_udata:
43bbcdc2 23562 fprintf_unfiltered (f, "constant: %s",
529908cb 23563 pulongest (die->attrs[i].as_unsigned ()));
c906108c 23564 break;
2dc7f7b3
TT
23565 case DW_FORM_sec_offset:
23566 fprintf_unfiltered (f, "section offset: %s",
529908cb 23567 pulongest (die->attrs[i].as_unsigned ()));
2dc7f7b3 23568 break;
55f1336d 23569 case DW_FORM_ref_sig8:
ac9ec31b 23570 fprintf_unfiltered (f, "signature: %s",
630ed6b9 23571 hex_string (die->attrs[i].as_signature ()));
348e048f 23572 break;
c906108c 23573 case DW_FORM_string:
4bdf3d34 23574 case DW_FORM_strp:
43988095 23575 case DW_FORM_line_strp:
cf532bd1 23576 case DW_FORM_strx:
3019eac3 23577 case DW_FORM_GNU_str_index:
36586728 23578 case DW_FORM_GNU_strp_alt:
8285870a 23579 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c6481205
TT
23580 die->attrs[i].as_string ()
23581 ? die->attrs[i].as_string () : "",
23582 die->attrs[i].canonical_string_p () ? "is" : "not");
c906108c
SS
23583 break;
23584 case DW_FORM_flag:
c45bc3f8 23585 if (die->attrs[i].as_boolean ())
d97bc12b 23586 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23587 else
d97bc12b 23588 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23589 break;
2dc7f7b3
TT
23590 case DW_FORM_flag_present:
23591 fprintf_unfiltered (f, "flag: TRUE");
23592 break;
a8329558 23593 case DW_FORM_indirect:
0963b4bd
MS
23594 /* The reader will have reduced the indirect form to
23595 the "base form" so this form should not occur. */
5f48f8f3 23596 fprintf_unfiltered (f,
3e43a32a 23597 "unexpected attribute form: DW_FORM_indirect");
a8329558 23598 break;
1bc397c5 23599 case DW_FORM_sdata:
663c44ac
JK
23600 case DW_FORM_implicit_const:
23601 fprintf_unfiltered (f, "constant: %s",
1bc397c5 23602 plongest (die->attrs[i].as_signed ()));
663c44ac 23603 break;
c906108c 23604 default:
d97bc12b 23605 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23606 die->attrs[i].form);
d97bc12b 23607 break;
c906108c 23608 }
d97bc12b 23609 fprintf_unfiltered (f, "\n");
c906108c
SS
23610 }
23611}
23612
f9aca02d 23613static void
d97bc12b 23614dump_die_for_error (struct die_info *die)
c906108c 23615{
d97bc12b
DE
23616 dump_die_shallow (gdb_stderr, 0, die);
23617}
23618
23619static void
23620dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23621{
23622 int indent = level * 4;
23623
23624 gdb_assert (die != NULL);
23625
23626 if (level >= max_level)
23627 return;
23628
23629 dump_die_shallow (f, indent, die);
23630
23631 if (die->child != NULL)
c906108c 23632 {
d97bc12b
DE
23633 print_spaces (indent, f);
23634 fprintf_unfiltered (f, " Children:");
23635 if (level + 1 < max_level)
23636 {
23637 fprintf_unfiltered (f, "\n");
23638 dump_die_1 (f, level + 1, max_level, die->child);
23639 }
23640 else
23641 {
3e43a32a
MS
23642 fprintf_unfiltered (f,
23643 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23644 }
23645 }
23646
23647 if (die->sibling != NULL && level > 0)
23648 {
23649 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23650 }
23651}
23652
d97bc12b
DE
23653/* This is called from the pdie macro in gdbinit.in.
23654 It's not static so gcc will keep a copy callable from gdb. */
23655
23656void
23657dump_die (struct die_info *die, int max_level)
23658{
23659 dump_die_1 (gdb_stdlog, 0, max_level, die);
23660}
23661
f9aca02d 23662static void
51545339 23663store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23664{
51545339 23665 void **slot;
c906108c 23666
9c541725
PA
23667 slot = htab_find_slot_with_hash (cu->die_hash, die,
23668 to_underlying (die->sect_off),
b64f50a1 23669 INSERT);
51545339
DJ
23670
23671 *slot = die;
c906108c
SS
23672}
23673
348e048f
DE
23674/* Follow reference or signature attribute ATTR of SRC_DIE.
23675 On entry *REF_CU is the CU of SRC_DIE.
23676 On exit *REF_CU is the CU of the result. */
23677
23678static struct die_info *
ff39bb5e 23679follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23680 struct dwarf2_cu **ref_cu)
23681{
23682 struct die_info *die;
23683
cd6c91b4 23684 if (attr->form_is_ref ())
348e048f 23685 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23686 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23687 die = follow_die_sig (src_die, attr, ref_cu);
23688 else
23689 {
23690 dump_die_for_error (src_die);
23691 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 23692 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
23693 }
23694
23695 return die;
03dd20cc
DJ
23696}
23697
5c631832 23698/* Follow reference OFFSET.
673bfd45
DE
23699 On entry *REF_CU is the CU of the source die referencing OFFSET.
23700 On exit *REF_CU is the CU of the result.
23701 Returns NULL if OFFSET is invalid. */
f504f079 23702
f9aca02d 23703static struct die_info *
9c541725 23704follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23705 struct dwarf2_cu **ref_cu)
c906108c 23706{
10b3939b 23707 struct die_info temp_die;
f2f0e013 23708 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 23709 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 23710
348e048f
DE
23711 gdb_assert (cu->per_cu != NULL);
23712
98bfdba5
PA
23713 target_cu = cu;
23714
17e593e9
SM
23715 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23716 "source CU contains target offset: %d",
23717 sect_offset_str (cu->per_cu->sect_off),
23718 sect_offset_str (sect_off),
23719 cu->header.offset_in_cu_p (sect_off));
23720
3019eac3 23721 if (cu->per_cu->is_debug_types)
348e048f
DE
23722 {
23723 /* .debug_types CUs cannot reference anything outside their CU.
23724 If they need to, they have to reference a signatured type via
55f1336d 23725 DW_FORM_ref_sig8. */
4057dfde 23726 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 23727 return NULL;
348e048f 23728 }
36586728 23729 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 23730 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
23731 {
23732 struct dwarf2_per_cu_data *per_cu;
9a619af0 23733
9c541725 23734 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 23735 per_objfile);
03dd20cc 23736
17e593e9
SM
23737 dwarf_read_debug_printf_v ("target CU offset: %s, "
23738 "target CU DIEs loaded: %d",
23739 sect_offset_str (per_cu->sect_off),
23740 per_objfile->get_cu (per_cu) != nullptr);
23741
616c069a
SM
23742 /* If necessary, add it to the queue and load its DIEs.
23743
23744 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23745 it doesn't mean they are currently loaded. Since we require them
23746 to be loaded, we must check for ourselves. */
23747 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23748 || per_objfile->get_cu (per_cu) == nullptr)
4a636814
SM
23749 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23750 false, cu->language);
03dd20cc 23751
976ca316 23752 target_cu = per_objfile->get_cu (per_cu);
616c069a 23753 gdb_assert (target_cu != nullptr);
10b3939b 23754 }
98bfdba5
PA
23755 else if (cu->dies == NULL)
23756 {
23757 /* We're loading full DIEs during partial symbol reading. */
976ca316 23758 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
4a636814
SM
23759 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23760 language_minimal);
98bfdba5 23761 }
c906108c 23762
f2f0e013 23763 *ref_cu = target_cu;
9c541725 23764 temp_die.sect_off = sect_off;
c24bdb02
KS
23765
23766 if (target_cu != cu)
23767 target_cu->ancestor = cu;
23768
9a3c8263 23769 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23770 &temp_die,
23771 to_underlying (sect_off));
5c631832 23772}
10b3939b 23773
5c631832
JK
23774/* Follow reference attribute ATTR of SRC_DIE.
23775 On entry *REF_CU is the CU of SRC_DIE.
23776 On exit *REF_CU is the CU of the result. */
23777
23778static struct die_info *
ff39bb5e 23779follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23780 struct dwarf2_cu **ref_cu)
23781{
0826b30a 23782 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
23783 struct dwarf2_cu *cu = *ref_cu;
23784 struct die_info *die;
23785
9c541725 23786 die = follow_die_offset (sect_off,
36586728
TT
23787 (attr->form == DW_FORM_GNU_ref_alt
23788 || cu->per_cu->is_dwz),
23789 ref_cu);
5c631832 23790 if (!die)
9d8780f0
SM
23791 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23792 "at %s [in module %s]"),
23793 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 23794 objfile_name (cu->per_objfile->objfile));
348e048f 23795
5c631832
JK
23796 return die;
23797}
23798
d4c9a4f8 23799/* See read.h. */
5c631832
JK
23800
23801struct dwarf2_locexpr_baton
9c541725 23802dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 23803 dwarf2_per_cu_data *per_cu,
976ca316 23804 dwarf2_per_objfile *per_objfile,
041d9819
SM
23805 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23806 bool resolve_abstract_p)
5c631832 23807{
5c631832
JK
23808 struct die_info *die;
23809 struct attribute *attr;
23810 struct dwarf2_locexpr_baton retval;
976ca316 23811 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 23812
976ca316 23813 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 23814 if (cu == nullptr)
976ca316 23815 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
23816
23817 if (cu == nullptr)
cc12ce38
DE
23818 {
23819 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23820 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23821 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23822 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23823 }
918dd910 23824
9c541725 23825 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23826 if (!die)
9d8780f0
SM
23827 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23828 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23829
23830 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23831 if (!attr && resolve_abstract_p
976ca316
SM
23832 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23833 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65 23834 {
041d9819 23835 CORE_ADDR pc = get_frame_pc ();
b3b3bada 23836 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 23837 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 23838
3360b6e7 23839 for (const auto &cand_off
976ca316 23840 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 23841 {
3360b6e7
TV
23842 struct dwarf2_cu *cand_cu = cu;
23843 struct die_info *cand
23844 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23845 if (!cand
23846 || !cand->parent
e4a62c65
TV
23847 || cand->parent->tag != DW_TAG_subprogram)
23848 continue;
23849
23850 CORE_ADDR pc_low, pc_high;
23851 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23852 if (pc_low == ((CORE_ADDR) -1))
23853 continue;
23854 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23855 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23856 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23857 continue;
23858
23859 die = cand;
23860 attr = dwarf2_attr (die, DW_AT_location, cu);
23861 break;
23862 }
23863 }
23864
5c631832
JK
23865 if (!attr)
23866 {
e103e986
JK
23867 /* DWARF: "If there is no such attribute, then there is no effect.".
23868 DATA is ignored if SIZE is 0. */
5c631832 23869
e103e986 23870 retval.data = NULL;
5c631832
JK
23871 retval.size = 0;
23872 }
cd6c91b4 23873 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23874 {
23875 struct dwarf2_loclist_baton loclist_baton;
041d9819 23876 CORE_ADDR pc = get_frame_pc ();
8cf6f0b1
TT
23877 size_t size;
23878
23879 fill_in_loclist_baton (cu, &loclist_baton, attr);
23880
23881 retval.data = dwarf2_find_location_expression (&loclist_baton,
23882 &size, pc);
23883 retval.size = size;
23884 }
5c631832
JK
23885 else
23886 {
4fc6c0d5 23887 if (!attr->form_is_block ())
9d8780f0 23888 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23889 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23890 sect_offset_str (sect_off), objfile_name (objfile));
5c631832 23891
9d2246fc
TT
23892 struct dwarf_block *block = attr->as_block ();
23893 retval.data = block->data;
23894 retval.size = block->size;
5c631832 23895 }
976ca316 23896 retval.per_objfile = per_objfile;
5c631832 23897 retval.per_cu = cu->per_cu;
918dd910 23898
976ca316 23899 per_objfile->age_comp_units ();
918dd910 23900
5c631832 23901 return retval;
348e048f
DE
23902}
23903
d4c9a4f8 23904/* See read.h. */
8b9737bf
TT
23905
23906struct dwarf2_locexpr_baton
23907dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 23908 dwarf2_per_cu_data *per_cu,
14095eb3 23909 dwarf2_per_objfile *per_objfile,
041d9819 23910 gdb::function_view<CORE_ADDR ()> get_frame_pc)
8b9737bf 23911{
9c541725 23912 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23913
14095eb3 23914 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
041d9819 23915 get_frame_pc);
8b9737bf
TT
23916}
23917
b6807d98
TT
23918/* Write a constant of a given type as target-ordered bytes into
23919 OBSTACK. */
23920
23921static const gdb_byte *
23922write_constant_as_bytes (struct obstack *obstack,
23923 enum bfd_endian byte_order,
23924 struct type *type,
23925 ULONGEST value,
23926 LONGEST *len)
23927{
23928 gdb_byte *result;
23929
23930 *len = TYPE_LENGTH (type);
224c3ddb 23931 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23932 store_unsigned_integer (result, *len, byte_order, value);
23933
23934 return result;
23935}
23936
d4c9a4f8 23937/* See read.h. */
b6807d98
TT
23938
23939const gdb_byte *
9c541725 23940dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 23941 dwarf2_per_cu_data *per_cu,
14095eb3 23942 dwarf2_per_objfile *per_objfile,
d4c9a4f8 23943 obstack *obstack,
b6807d98
TT
23944 LONGEST *len)
23945{
b6807d98
TT
23946 struct die_info *die;
23947 struct attribute *attr;
23948 const gdb_byte *result = NULL;
23949 struct type *type;
23950 LONGEST value;
23951 enum bfd_endian byte_order;
14095eb3 23952 struct objfile *objfile = per_objfile->objfile;
b6807d98 23953
7188ed02 23954 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23955 if (cu == nullptr)
23956 cu = load_cu (per_cu, per_objfile, false);
23957
23958 if (cu == nullptr)
cc12ce38
DE
23959 {
23960 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23961 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23962 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23963 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23964 }
b6807d98 23965
9c541725 23966 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23967 if (!die)
9d8780f0
SM
23968 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23969 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23970
23971 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23972 if (attr == NULL)
23973 return NULL;
23974
e3b94546 23975 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23976 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23977
23978 switch (attr->form)
23979 {
23980 case DW_FORM_addr:
336d760d 23981 case DW_FORM_addrx:
b6807d98
TT
23982 case DW_FORM_GNU_addr_index:
23983 {
23984 gdb_byte *tem;
23985
23986 *len = cu->header.addr_size;
224c3ddb 23987 tem = (gdb_byte *) obstack_alloc (obstack, *len);
36d378cf 23988 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
b6807d98
TT
23989 result = tem;
23990 }
23991 break;
23992 case DW_FORM_string:
23993 case DW_FORM_strp:
cf532bd1 23994 case DW_FORM_strx:
b6807d98
TT
23995 case DW_FORM_GNU_str_index:
23996 case DW_FORM_GNU_strp_alt:
c6481205 23997 /* The string is already allocated on the objfile obstack, point
b6807d98 23998 directly to it. */
2c830f54
TT
23999 {
24000 const char *attr_name = attr->as_string ();
24001 result = (const gdb_byte *) attr_name;
24002 *len = strlen (attr_name);
24003 }
b6807d98
TT
24004 break;
24005 case DW_FORM_block1:
24006 case DW_FORM_block2:
24007 case DW_FORM_block4:
24008 case DW_FORM_block:
24009 case DW_FORM_exprloc:
0224619f 24010 case DW_FORM_data16:
9d2246fc
TT
24011 {
24012 struct dwarf_block *block = attr->as_block ();
24013 result = block->data;
24014 *len = block->size;
24015 }
b6807d98
TT
24016 break;
24017
24018 /* The DW_AT_const_value attributes are supposed to carry the
24019 symbol's value "represented as it would be on the target
24020 architecture." By the time we get here, it's already been
24021 converted to host endianness, so we just need to sign- or
24022 zero-extend it as appropriate. */
24023 case DW_FORM_data1:
24024 type = die_type (die, cu);
24025 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
24026 if (result == NULL)
24027 result = write_constant_as_bytes (obstack, byte_order,
24028 type, value, len);
24029 break;
24030 case DW_FORM_data2:
24031 type = die_type (die, cu);
24032 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
24033 if (result == NULL)
24034 result = write_constant_as_bytes (obstack, byte_order,
24035 type, value, len);
24036 break;
24037 case DW_FORM_data4:
24038 type = die_type (die, cu);
24039 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
24040 if (result == NULL)
24041 result = write_constant_as_bytes (obstack, byte_order,
24042 type, value, len);
24043 break;
24044 case DW_FORM_data8:
24045 type = die_type (die, cu);
24046 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
24047 if (result == NULL)
24048 result = write_constant_as_bytes (obstack, byte_order,
24049 type, value, len);
24050 break;
24051
24052 case DW_FORM_sdata:
663c44ac 24053 case DW_FORM_implicit_const:
b6807d98
TT
24054 type = die_type (die, cu);
24055 result = write_constant_as_bytes (obstack, byte_order,
1bc397c5 24056 type, attr->as_signed (), len);
b6807d98
TT
24057 break;
24058
24059 case DW_FORM_udata:
24060 type = die_type (die, cu);
24061 result = write_constant_as_bytes (obstack, byte_order,
529908cb 24062 type, attr->as_unsigned (), len);
b6807d98
TT
24063 break;
24064
24065 default:
b98664d3 24066 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
24067 dwarf_form_name (attr->form));
24068 break;
24069 }
24070
24071 return result;
24072}
24073
d4c9a4f8 24074/* See read.h. */
7942e96e
AA
24075
24076struct type *
9c541725 24077dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
24078 dwarf2_per_cu_data *per_cu,
24079 dwarf2_per_objfile *per_objfile)
7942e96e 24080{
7942e96e
AA
24081 struct die_info *die;
24082
7188ed02 24083 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
24084 if (cu == nullptr)
24085 cu = load_cu (per_cu, per_objfile, false);
24086
24087 if (cu == nullptr)
24088 return nullptr;
7942e96e 24089
9c541725 24090 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
24091 if (!die)
24092 return NULL;
24093
24094 return die_type (die, cu);
24095}
24096
8cb5117c 24097/* See read.h. */
8a9b8146
TT
24098
24099struct type *
b64f50a1 24100dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
24101 dwarf2_per_cu_data *per_cu,
24102 dwarf2_per_objfile *per_objfile)
8a9b8146 24103{
9c541725 24104 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 24105 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
24106}
24107
ac9ec31b 24108/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 24109 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
24110 On exit *REF_CU is the CU of the result.
24111 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
24112
24113static struct die_info *
ac9ec31b
DE
24114follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
24115 struct dwarf2_cu **ref_cu)
348e048f 24116{
348e048f 24117 struct die_info temp_die;
c24bdb02 24118 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 24119 struct die_info *die;
976ca316 24120 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 24121
348e048f 24122
ac9ec31b
DE
24123 /* While it might be nice to assert sig_type->type == NULL here,
24124 we can get here for DW_AT_imported_declaration where we need
24125 the DIE not the type. */
348e048f 24126
616c069a 24127 /* If necessary, add it to the queue and load its DIEs.
348e048f 24128
616c069a
SM
24129 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
24130 it doesn't mean they are currently loaded. Since we require them
24131 to be loaded, we must check for ourselves. */
976ca316 24132 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
616c069a
SM
24133 language_minimal)
24134 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
976ca316 24135 read_signatured_type (sig_type, per_objfile);
348e048f 24136
976ca316 24137 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
69d751e3 24138 gdb_assert (sig_cu != NULL);
9c541725
PA
24139 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
24140 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 24141 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 24142 to_underlying (temp_die.sect_off));
348e048f
DE
24143 if (die)
24144 {
796a7ff8
DE
24145 /* For .gdb_index version 7 keep track of included TUs.
24146 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
24147 if (per_objfile->per_bfd->index_table != NULL
24148 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 24149 {
ae640021 24150 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
24151 }
24152
348e048f 24153 *ref_cu = sig_cu;
c24bdb02
KS
24154 if (sig_cu != cu)
24155 sig_cu->ancestor = cu;
24156
348e048f
DE
24157 return die;
24158 }
24159
ac9ec31b
DE
24160 return NULL;
24161}
24162
24163/* Follow signatured type referenced by ATTR in SRC_DIE.
24164 On entry *REF_CU is the CU of SRC_DIE.
24165 On exit *REF_CU is the CU of the result.
24166 The result is the DIE of the type.
24167 If the referenced type cannot be found an error is thrown. */
24168
24169static struct die_info *
ff39bb5e 24170follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
24171 struct dwarf2_cu **ref_cu)
24172{
630ed6b9 24173 ULONGEST signature = attr->as_signature ();
ac9ec31b
DE
24174 struct signatured_type *sig_type;
24175 struct die_info *die;
24176
24177 gdb_assert (attr->form == DW_FORM_ref_sig8);
24178
a2ce51a0 24179 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
24180 /* sig_type will be NULL if the signatured type is missing from
24181 the debug info. */
24182 if (sig_type == NULL)
24183 {
24184 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
dda83cd7
SM
24185 " from DIE at %s [in module %s]"),
24186 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 24187 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
24188 }
24189
24190 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24191 if (die == NULL)
24192 {
24193 dump_die_for_error (src_die);
24194 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24195 " from DIE at %s [in module %s]"),
24196 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 24197 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
24198 }
24199
24200 return die;
24201}
24202
24203/* Get the type specified by SIGNATURE referenced in DIE/CU,
24204 reading in and processing the type unit if necessary. */
24205
24206static struct type *
24207get_signatured_type (struct die_info *die, ULONGEST signature,
24208 struct dwarf2_cu *cu)
24209{
976ca316 24210 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
24211 struct signatured_type *sig_type;
24212 struct dwarf2_cu *type_cu;
24213 struct die_info *type_die;
24214 struct type *type;
24215
a2ce51a0 24216 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
24217 /* sig_type will be NULL if the signatured type is missing from
24218 the debug info. */
24219 if (sig_type == NULL)
24220 {
b98664d3 24221 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
24222 " from DIE at %s [in module %s]"),
24223 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24224 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24225 return build_error_marker_type (cu, die);
24226 }
24227
24228 /* If we already know the type we're done. */
976ca316 24229 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
24230 if (type != nullptr)
24231 return type;
ac9ec31b
DE
24232
24233 type_cu = cu;
24234 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24235 if (type_die != NULL)
24236 {
24237 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24238 is created. This is important, for example, because for c++ classes
24239 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24240 type = read_type_die (type_die, type_cu);
24241 if (type == NULL)
24242 {
b98664d3 24243 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
24244 " referenced from DIE at %s [in module %s]"),
24245 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24246 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24247 type = build_error_marker_type (cu, die);
24248 }
24249 }
24250 else
24251 {
b98664d3 24252 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24253 " from DIE at %s [in module %s]"),
24254 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24255 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24256 type = build_error_marker_type (cu, die);
24257 }
e286671b 24258
976ca316 24259 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
24260
24261 return type;
24262}
24263
24264/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24265 reading in and processing the type unit if necessary. */
24266
24267static struct type *
ff39bb5e 24268get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 24269 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
24270{
24271 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 24272 if (attr->form_is_ref ())
ac9ec31b
DE
24273 {
24274 struct dwarf2_cu *type_cu = cu;
24275 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24276
24277 return read_type_die (type_die, type_cu);
24278 }
24279 else if (attr->form == DW_FORM_ref_sig8)
24280 {
630ed6b9 24281 return get_signatured_type (die, attr->as_signature (), cu);
ac9ec31b
DE
24282 }
24283 else
24284 {
976ca316 24285 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24286
b98664d3 24287 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
24288 " at %s [in module %s]"),
24289 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 24290 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24291 return build_error_marker_type (cu, die);
24292 }
348e048f
DE
24293}
24294
e5fe5e75 24295/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
24296
24297static void
ab432490
SM
24298load_full_type_unit (dwarf2_per_cu_data *per_cu,
24299 dwarf2_per_objfile *per_objfile)
348e048f 24300{
52dc124a 24301 struct signatured_type *sig_type;
348e048f 24302
f4dc4d17 24303 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 24304 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 24305
6721b2ec
DE
24306 /* We have the per_cu, but we need the signatured_type.
24307 Fortunately this is an easy translation. */
24308 gdb_assert (per_cu->is_debug_types);
24309 sig_type = (struct signatured_type *) per_cu;
348e048f 24310
7188ed02 24311 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24312
ab432490 24313 read_signatured_type (sig_type, per_objfile);
348e048f 24314
7188ed02 24315 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
24316}
24317
3019eac3
DE
24318/* Read in a signatured type and build its CU and DIEs.
24319 If the type is a stub for the real type in a DWO file,
24320 read in the real type from the DWO file as well. */
dee91e82
DE
24321
24322static void
ab432490
SM
24323read_signatured_type (signatured_type *sig_type,
24324 dwarf2_per_objfile *per_objfile)
dee91e82
DE
24325{
24326 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24327
3019eac3 24328 gdb_assert (per_cu->is_debug_types);
7188ed02 24329 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24330
2e671100 24331 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
24332
24333 if (!reader.dummy_p)
24334 {
24335 struct dwarf2_cu *cu = reader.cu;
24336 const gdb_byte *info_ptr = reader.info_ptr;
24337
24338 gdb_assert (cu->die_hash == NULL);
24339 cu->die_hash =
24340 htab_create_alloc_ex (cu->header.length / 12,
24341 die_hash,
24342 die_eq,
24343 NULL,
24344 &cu->comp_unit_obstack,
24345 hashtab_obstack_allocate,
24346 dummy_obstack_deallocate);
24347
3e225074 24348 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
24349 reader.comp_unit_die->child
24350 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24351 reader.comp_unit_die);
24352 cu->dies = reader.comp_unit_die;
24353 /* comp_unit_die is not stored in die_hash, no need. */
24354
24355 /* We try not to read any attributes in this function, because
24356 not all CUs needed for references have been loaded yet, and
24357 symbol table processing isn't initialized. But we have to
24358 set the CU language, or we won't be able to build types
24359 correctly. Similarly, if we do not read the producer, we can
24360 not apply producer-specific interpretation. */
24361 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
24362
24363 reader.keep ();
c0ab21c2
TT
24364 }
24365
7ee85ab1 24366 sig_type->per_cu.tu_read = 1;
c906108c
SS
24367}
24368
c906108c
SS
24369/* Decode simple location descriptions.
24370 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
24371 the location and return the value. If COMPUTED is non-null, it is
24372 set to true to indicate that decoding was successful, and false
24373 otherwise. If COMPUTED is null, then this function may emit a
24374 complaint. */
c906108c
SS
24375
24376static CORE_ADDR
7d79de9a 24377decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 24378{
5e22e966 24379 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
24380 size_t i;
24381 size_t size = blk->size;
d521ce57 24382 const gdb_byte *data = blk->data;
21ae7a4d
JK
24383 CORE_ADDR stack[64];
24384 int stacki;
24385 unsigned int bytes_read, unsnd;
24386 gdb_byte op;
c906108c 24387
7d79de9a
TT
24388 if (computed != nullptr)
24389 *computed = false;
24390
21ae7a4d
JK
24391 i = 0;
24392 stacki = 0;
24393 stack[stacki] = 0;
24394 stack[++stacki] = 0;
24395
24396 while (i < size)
24397 {
24398 op = data[i++];
24399 switch (op)
24400 {
24401 case DW_OP_lit0:
24402 case DW_OP_lit1:
24403 case DW_OP_lit2:
24404 case DW_OP_lit3:
24405 case DW_OP_lit4:
24406 case DW_OP_lit5:
24407 case DW_OP_lit6:
24408 case DW_OP_lit7:
24409 case DW_OP_lit8:
24410 case DW_OP_lit9:
24411 case DW_OP_lit10:
24412 case DW_OP_lit11:
24413 case DW_OP_lit12:
24414 case DW_OP_lit13:
24415 case DW_OP_lit14:
24416 case DW_OP_lit15:
24417 case DW_OP_lit16:
24418 case DW_OP_lit17:
24419 case DW_OP_lit18:
24420 case DW_OP_lit19:
24421 case DW_OP_lit20:
24422 case DW_OP_lit21:
24423 case DW_OP_lit22:
24424 case DW_OP_lit23:
24425 case DW_OP_lit24:
24426 case DW_OP_lit25:
24427 case DW_OP_lit26:
24428 case DW_OP_lit27:
24429 case DW_OP_lit28:
24430 case DW_OP_lit29:
24431 case DW_OP_lit30:
24432 case DW_OP_lit31:
24433 stack[++stacki] = op - DW_OP_lit0;
24434 break;
f1bea926 24435
21ae7a4d
JK
24436 case DW_OP_reg0:
24437 case DW_OP_reg1:
24438 case DW_OP_reg2:
24439 case DW_OP_reg3:
24440 case DW_OP_reg4:
24441 case DW_OP_reg5:
24442 case DW_OP_reg6:
24443 case DW_OP_reg7:
24444 case DW_OP_reg8:
24445 case DW_OP_reg9:
24446 case DW_OP_reg10:
24447 case DW_OP_reg11:
24448 case DW_OP_reg12:
24449 case DW_OP_reg13:
24450 case DW_OP_reg14:
24451 case DW_OP_reg15:
24452 case DW_OP_reg16:
24453 case DW_OP_reg17:
24454 case DW_OP_reg18:
24455 case DW_OP_reg19:
24456 case DW_OP_reg20:
24457 case DW_OP_reg21:
24458 case DW_OP_reg22:
24459 case DW_OP_reg23:
24460 case DW_OP_reg24:
24461 case DW_OP_reg25:
24462 case DW_OP_reg26:
24463 case DW_OP_reg27:
24464 case DW_OP_reg28:
24465 case DW_OP_reg29:
24466 case DW_OP_reg30:
24467 case DW_OP_reg31:
24468 stack[++stacki] = op - DW_OP_reg0;
24469 if (i < size)
7d79de9a
TT
24470 {
24471 if (computed == nullptr)
24472 dwarf2_complex_location_expr_complaint ();
24473 else
24474 return 0;
24475 }
21ae7a4d 24476 break;
c906108c 24477
21ae7a4d
JK
24478 case DW_OP_regx:
24479 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24480 i += bytes_read;
24481 stack[++stacki] = unsnd;
24482 if (i < size)
7d79de9a
TT
24483 {
24484 if (computed == nullptr)
24485 dwarf2_complex_location_expr_complaint ();
24486 else
24487 return 0;
24488 }
21ae7a4d 24489 break;
c906108c 24490
21ae7a4d 24491 case DW_OP_addr:
c8a7a66f
TT
24492 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24493 &bytes_read);
21ae7a4d
JK
24494 i += bytes_read;
24495 break;
d53d4ac5 24496
21ae7a4d
JK
24497 case DW_OP_const1u:
24498 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24499 i += 1;
24500 break;
24501
24502 case DW_OP_const1s:
24503 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24504 i += 1;
24505 break;
24506
24507 case DW_OP_const2u:
24508 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24509 i += 2;
24510 break;
24511
24512 case DW_OP_const2s:
24513 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24514 i += 2;
24515 break;
d53d4ac5 24516
21ae7a4d
JK
24517 case DW_OP_const4u:
24518 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24519 i += 4;
24520 break;
24521
24522 case DW_OP_const4s:
24523 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24524 i += 4;
24525 break;
24526
585861ea
JK
24527 case DW_OP_const8u:
24528 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24529 i += 8;
24530 break;
24531
21ae7a4d
JK
24532 case DW_OP_constu:
24533 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24534 &bytes_read);
24535 i += bytes_read;
24536 break;
24537
24538 case DW_OP_consts:
24539 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24540 i += bytes_read;
24541 break;
24542
24543 case DW_OP_dup:
24544 stack[stacki + 1] = stack[stacki];
24545 stacki++;
24546 break;
24547
24548 case DW_OP_plus:
24549 stack[stacki - 1] += stack[stacki];
24550 stacki--;
24551 break;
24552
24553 case DW_OP_plus_uconst:
24554 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24555 &bytes_read);
24556 i += bytes_read;
24557 break;
24558
24559 case DW_OP_minus:
24560 stack[stacki - 1] -= stack[stacki];
24561 stacki--;
24562 break;
24563
24564 case DW_OP_deref:
24565 /* If we're not the last op, then we definitely can't encode
24566 this using GDB's address_class enum. This is valid for partial
24567 global symbols, although the variable's address will be bogus
24568 in the psymtab. */
24569 if (i < size)
7d79de9a
TT
24570 {
24571 if (computed == nullptr)
24572 dwarf2_complex_location_expr_complaint ();
24573 else
24574 return 0;
24575 }
21ae7a4d
JK
24576 break;
24577
dda83cd7 24578 case DW_OP_GNU_push_tls_address:
4aa4e28b 24579 case DW_OP_form_tls_address:
21ae7a4d
JK
24580 /* The top of the stack has the offset from the beginning
24581 of the thread control block at which the variable is located. */
24582 /* Nothing should follow this operator, so the top of stack would
24583 be returned. */
24584 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24585 address will be bogus in the psymtab. Make it always at least
24586 non-zero to not look as a variable garbage collected by linker
24587 which have DW_OP_addr 0. */
21ae7a4d 24588 if (i < size)
7d79de9a
TT
24589 {
24590 if (computed == nullptr)
24591 dwarf2_complex_location_expr_complaint ();
24592 else
24593 return 0;
24594 }
585861ea 24595 stack[stacki]++;
dda83cd7 24596 break;
21ae7a4d
JK
24597
24598 case DW_OP_GNU_uninit:
7d79de9a
TT
24599 if (computed != nullptr)
24600 return 0;
21ae7a4d
JK
24601 break;
24602
336d760d 24603 case DW_OP_addrx:
3019eac3 24604 case DW_OP_GNU_addr_index:
49f6c839 24605 case DW_OP_GNU_const_index:
3019eac3
DE
24606 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24607 &bytes_read);
24608 i += bytes_read;
24609 break;
24610
21ae7a4d 24611 default:
7d79de9a
TT
24612 if (computed == nullptr)
24613 {
24614 const char *name = get_DW_OP_name (op);
21ae7a4d 24615
7d79de9a
TT
24616 if (name)
24617 complaint (_("unsupported stack op: '%s'"),
24618 name);
24619 else
24620 complaint (_("unsupported stack op: '%02x'"),
24621 op);
24622 }
21ae7a4d
JK
24623
24624 return (stack[stacki]);
d53d4ac5 24625 }
3c6e0cb3 24626
21ae7a4d 24627 /* Enforce maximum stack depth of SIZE-1 to avoid writing
dda83cd7 24628 outside of the allocated space. Also enforce minimum>0. */
21ae7a4d
JK
24629 if (stacki >= ARRAY_SIZE (stack) - 1)
24630 {
7d79de9a
TT
24631 if (computed == nullptr)
24632 complaint (_("location description stack overflow"));
21ae7a4d
JK
24633 return 0;
24634 }
24635
24636 if (stacki <= 0)
24637 {
7d79de9a
TT
24638 if (computed == nullptr)
24639 complaint (_("location description stack underflow"));
21ae7a4d
JK
24640 return 0;
24641 }
24642 }
7d79de9a
TT
24643
24644 if (computed != nullptr)
24645 *computed = true;
21ae7a4d 24646 return (stack[stacki]);
c906108c
SS
24647}
24648
24649/* memory allocation interface */
24650
c906108c 24651static struct dwarf_block *
7b5a2f43 24652dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24653{
8d749320 24654 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24655}
24656
c906108c 24657static struct die_info *
b60c80d6 24658dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24659{
24660 struct die_info *die;
b60c80d6
DJ
24661 size_t size = sizeof (struct die_info);
24662
24663 if (num_attrs > 1)
24664 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24665
b60c80d6 24666 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24667 memset (die, 0, sizeof (struct die_info));
24668 return (die);
24669}
2e276125
JB
24670
24671\f
a036ba48 24672
c90ec28a 24673/* Macro support. */
cf2c3c16 24674
9eac9650
TT
24675/* An overload of dwarf_decode_macros that finds the correct section
24676 and ensures it is read in before calling the other overload. */
24677
24678static void
24679dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24680 int section_is_gnu)
24681{
976ca316
SM
24682 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24683 struct objfile *objfile = per_objfile->objfile;
5a0e026f 24684 const struct line_header *lh = cu->line_header;
9eac9650
TT
24685 unsigned int offset_size = cu->header.offset_size;
24686 struct dwarf2_section_info *section;
24687 const char *section_name;
24688
24689 if (cu->dwo_unit != nullptr)
24690 {
24691 if (section_is_gnu)
24692 {
24693 section = &cu->dwo_unit->dwo_file->sections.macro;
24694 section_name = ".debug_macro.dwo";
24695 }
24696 else
24697 {
24698 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24699 section_name = ".debug_macinfo.dwo";
24700 }
24701 }
24702 else
24703 {
24704 if (section_is_gnu)
24705 {
976ca316 24706 section = &per_objfile->per_bfd->macro;
9eac9650
TT
24707 section_name = ".debug_macro";
24708 }
24709 else
24710 {
976ca316 24711 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
24712 section_name = ".debug_macinfo";
24713 }
24714 }
24715
24716 section->read (objfile);
24717 if (section->buffer == nullptr)
24718 {
24719 complaint (_("missing %s section"), section_name);
24720 return;
24721 }
24722
24723 buildsym_compunit *builder = cu->get_builder ();
24724
048fde1e 24725 struct dwarf2_section_info *str_offsets_section;
24726 struct dwarf2_section_info *str_section;
24727 ULONGEST str_offsets_base;
24728
24729 if (cu->dwo_unit != nullptr)
24730 {
24731 str_offsets_section = &cu->dwo_unit->dwo_file
24732 ->sections.str_offsets;
24733 str_section = &cu->dwo_unit->dwo_file->sections.str;
24734 str_offsets_base = cu->header.addr_size;
24735 }
24736 else
24737 {
24738 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24739 str_section = &per_objfile->per_bfd->str;
24740 str_offsets_base = *cu->str_offsets_base;
24741 }
24742
976ca316 24743 dwarf_decode_macros (per_objfile, builder, section, lh,
048fde1e 24744 offset_size, offset, str_section, str_offsets_section,
24745 str_offsets_base, section_is_gnu);
9eac9650
TT
24746}
24747
3019eac3
DE
24748/* Return the .debug_loc section to use for CU.
24749 For DWO files use .debug_loc.dwo. */
24750
24751static struct dwarf2_section_info *
24752cu_debug_loc_section (struct dwarf2_cu *cu)
24753{
976ca316 24754 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24755
3019eac3 24756 if (cu->dwo_unit)
43988095
JK
24757 {
24758 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24759
43988095
JK
24760 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24761 }
976ca316
SM
24762 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24763 : &per_objfile->per_bfd->loc);
3019eac3
DE
24764}
24765
d0ce17d8
CT
24766/* Return the .debug_rnglists section to use for CU. */
24767static struct dwarf2_section_info *
24768cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24769{
24770 if (cu->header.version < 5)
24771 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24772 cu->header.version);
24773 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24774
24775 /* Make sure we read the .debug_rnglists section from the file that
24776 contains the DW_AT_ranges attribute we are reading. Normally that
24777 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24778 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24779 program. */
24780 if (cu->dwo_unit != nullptr
24781 && tag != DW_TAG_compile_unit
24782 && tag != DW_TAG_skeleton_unit)
24783 {
24784 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24785
24786 if (sections->rnglists.size > 0)
24787 return &sections->rnglists;
24788 else
24789 error (_(".debug_rnglists section is missing from .dwo file."));
24790 }
24791 return &dwarf2_per_objfile->per_bfd->rnglists;
24792}
24793
8cf6f0b1
TT
24794/* A helper function that fills in a dwarf2_loclist_baton. */
24795
24796static void
24797fill_in_loclist_baton (struct dwarf2_cu *cu,
24798 struct dwarf2_loclist_baton *baton,
ff39bb5e 24799 const struct attribute *attr)
8cf6f0b1 24800{
976ca316 24801 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
24802 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24803
976ca316 24804 section->read (per_objfile->objfile);
8cf6f0b1 24805
976ca316 24806 baton->per_objfile = per_objfile;
8cf6f0b1
TT
24807 baton->per_cu = cu->per_cu;
24808 gdb_assert (baton->per_cu);
24809 /* We don't know how long the location list is, but make sure we
24810 don't run off the edge of the section. */
d4df075e
TT
24811 baton->size = section->size - attr->as_unsigned ();
24812 baton->data = section->buffer + attr->as_unsigned ();
2b24b6e4
TT
24813 if (cu->base_address.has_value ())
24814 baton->base_address = *cu->base_address;
24815 else
24816 baton->base_address = 0;
f664829e 24817 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24818}
24819
4c2df51b 24820static void
ff39bb5e 24821dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24822 struct dwarf2_cu *cu, int is_block)
4c2df51b 24823{
976ca316
SM
24824 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24825 struct objfile *objfile = per_objfile->objfile;
3019eac3 24826 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24827
cd6c91b4 24828 if (attr->form_is_section_offset ()
3019eac3 24829 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24830 the section. If so, fall through to the complaint in the
24831 other branch. */
d4df075e 24832 && attr->as_unsigned () < section->get_size (objfile))
4c2df51b 24833 {
0d53c4c4 24834 struct dwarf2_loclist_baton *baton;
4c2df51b 24835
8d749320 24836 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24837
8cf6f0b1 24838 fill_in_loclist_baton (cu, baton, attr);
be391dca 24839
2b24b6e4 24840 if (!cu->base_address.has_value ())
b98664d3 24841 complaint (_("Location list used without "
3e43a32a 24842 "specifying the CU base address."));
4c2df51b 24843
f1e6e072
TT
24844 SYMBOL_ACLASS_INDEX (sym) = (is_block
24845 ? dwarf2_loclist_block_index
24846 : dwarf2_loclist_index);
0d53c4c4
DJ
24847 SYMBOL_LOCATION_BATON (sym) = baton;
24848 }
24849 else
24850 {
24851 struct dwarf2_locexpr_baton *baton;
24852
8d749320 24853 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 24854 baton->per_objfile = per_objfile;
ae0d2f24
UW
24855 baton->per_cu = cu->per_cu;
24856 gdb_assert (baton->per_cu);
0d53c4c4 24857
4fc6c0d5 24858 if (attr->form_is_block ())
0d53c4c4
DJ
24859 {
24860 /* Note that we're just copying the block's data pointer
24861 here, not the actual data. We're still pointing into the
6502dd73
DJ
24862 info_buffer for SYM's objfile; right now we never release
24863 that buffer, but when we do clean up properly this may
24864 need to change. */
9d2246fc
TT
24865 struct dwarf_block *block = attr->as_block ();
24866 baton->size = block->size;
24867 baton->data = block->data;
0d53c4c4
DJ
24868 }
24869 else
24870 {
24871 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24872 sym->natural_name ());
0d53c4c4 24873 baton->size = 0;
0d53c4c4 24874 }
6e70227d 24875
f1e6e072
TT
24876 SYMBOL_ACLASS_INDEX (sym) = (is_block
24877 ? dwarf2_locexpr_block_index
24878 : dwarf2_locexpr_index);
0d53c4c4
DJ
24879 SYMBOL_LOCATION_BATON (sym) = baton;
24880 }
4c2df51b 24881}
6502dd73 24882
2e6a9f79 24883/* See read.h. */
96408a79 24884
2e6a9f79
SM
24885const comp_unit_head *
24886dwarf2_per_cu_data::get_header () const
96408a79 24887{
2e6a9f79
SM
24888 if (!m_header_read_in)
24889 {
24890 const gdb_byte *info_ptr
24891 = this->section->buffer + to_underlying (this->sect_off);
96408a79 24892
2e6a9f79 24893 memset (&m_header, 0, sizeof (m_header));
96408a79 24894
2e6a9f79
SM
24895 read_comp_unit_head (&m_header, info_ptr, this->section,
24896 rcuh_kind::COMPILE);
24897 }
96408a79 24898
2e6a9f79 24899 return &m_header;
96408a79
SA
24900}
24901
09ba997f 24902/* See read.h. */
ae0d2f24 24903
98714339 24904int
09ba997f 24905dwarf2_per_cu_data::addr_size () const
ae0d2f24 24906{
2e6a9f79 24907 return this->get_header ()->addr_size;
ae0d2f24
UW
24908}
24909
09ba997f 24910/* See read.h. */
9eae7c52
TT
24911
24912int
09ba997f 24913dwarf2_per_cu_data::offset_size () const
9eae7c52 24914{
2e6a9f79 24915 return this->get_header ()->offset_size;
96408a79
SA
24916}
24917
09ba997f 24918/* See read.h. */
96408a79
SA
24919
24920int
09ba997f 24921dwarf2_per_cu_data::ref_addr_size () const
96408a79 24922{
2e6a9f79 24923 const comp_unit_head *header = this->get_header ();
96408a79 24924
2e6a9f79
SM
24925 if (header->version == 2)
24926 return header->addr_size;
96408a79 24927 else
2e6a9f79 24928 return header->offset_size;
181cebd4
JK
24929}
24930
09ba997f 24931/* See read.h. */
9aa1f1e3 24932
09ba997f 24933struct type *
293e7e51 24934dwarf2_cu::addr_type () const
9a49df9d 24935{
293e7e51 24936 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
24937 struct type *void_type = objfile_type (objfile)->builtin_void;
24938 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 24939 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
24940
24941 if (TYPE_LENGTH (addr_type) == addr_size)
24942 return addr_type;
24943
c6d940a9 24944 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
9a49df9d
AB
24945 return addr_type;
24946}
24947
22b6cd70
TT
24948/* A helper function for dwarf2_find_containing_comp_unit that returns
24949 the index of the result, and that searches a vector. It will
24950 return a result even if the offset in question does not actually
24951 occur in any CU. This is separate so that it can be unit
24952 tested. */
ae038cb0 24953
22b6cd70
TT
24954static int
24955dwarf2_find_containing_comp_unit
24956 (sect_offset sect_off,
24957 unsigned int offset_in_dwz,
24958 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 24959{
ae038cb0
DJ
24960 int low, high;
24961
ae038cb0 24962 low = 0;
22b6cd70 24963 high = all_comp_units.size () - 1;
ae038cb0
DJ
24964 while (high > low)
24965 {
36586728 24966 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24967 int mid = low + (high - low) / 2;
9a619af0 24968
22b6cd70 24969 mid_cu = all_comp_units[mid];
36586728 24970 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24971 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24972 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24973 high = mid;
24974 else
24975 low = mid + 1;
24976 }
24977 gdb_assert (low == high);
22b6cd70
TT
24978 return low;
24979}
24980
24981/* Locate the .debug_info compilation unit from CU's objfile which contains
24982 the DIE at OFFSET. Raises an error on failure. */
24983
24984static struct dwarf2_per_cu_data *
24985dwarf2_find_containing_comp_unit (sect_offset sect_off,
24986 unsigned int offset_in_dwz,
976ca316 24987 dwarf2_per_objfile *per_objfile)
22b6cd70 24988{
976ca316
SM
24989 int low = dwarf2_find_containing_comp_unit
24990 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24991 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
22b6cd70 24992
45b8ae0c 24993 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24994 {
36586728 24995 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24996 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24997 "offset %s [in module %s]"),
24998 sect_offset_str (sect_off),
976ca316 24999 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 25000
976ca316 25001 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 25002 <= sect_off);
976ca316 25003 return per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
25004 }
25005 else
25006 {
976ca316 25007 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 25008 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25009 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25010 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25011 return this_cu;
25012 }
25013}
25014
22b6cd70
TT
25015#if GDB_SELF_TEST
25016
25017namespace selftests {
25018namespace find_containing_comp_unit {
25019
25020static void
25021run_test ()
25022{
25023 struct dwarf2_per_cu_data one {};
25024 struct dwarf2_per_cu_data two {};
25025 struct dwarf2_per_cu_data three {};
25026 struct dwarf2_per_cu_data four {};
25027
25028 one.length = 5;
25029 two.sect_off = sect_offset (one.length);
25030 two.length = 7;
25031
25032 three.length = 5;
25033 three.is_dwz = 1;
25034 four.sect_off = sect_offset (three.length);
25035 four.length = 7;
25036 four.is_dwz = 1;
25037
25038 std::vector<dwarf2_per_cu_data *> units;
25039 units.push_back (&one);
25040 units.push_back (&two);
25041 units.push_back (&three);
25042 units.push_back (&four);
25043
25044 int result;
25045
25046 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
25047 SELF_CHECK (units[result] == &one);
25048 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
25049 SELF_CHECK (units[result] == &one);
25050 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
25051 SELF_CHECK (units[result] == &two);
25052
25053 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
25054 SELF_CHECK (units[result] == &three);
25055 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
25056 SELF_CHECK (units[result] == &three);
25057 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
25058 SELF_CHECK (units[result] == &four);
25059}
25060
25061}
25062}
25063
25064#endif /* GDB_SELF_TEST */
25065
9e021579 25066/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 25067
9e021579
SM
25068dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
25069 dwarf2_per_objfile *per_objfile)
25070 : per_cu (per_cu),
25071 per_objfile (per_objfile),
9068261f
AB
25072 mark (false),
25073 has_loclist (false),
25074 checked_producer (false),
25075 producer_is_gxx_lt_4_6 (false),
25076 producer_is_gcc_lt_4_3 (false),
eb77c9df 25077 producer_is_icc (false),
9068261f 25078 producer_is_icc_lt_14 (false),
c258c396 25079 producer_is_codewarrior (false),
9068261f 25080 processing_has_namespace_info (false)
93311388 25081{
9816fde3
JK
25082}
25083
25084/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25085
25086static void
95554aad
TT
25087prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25088 enum language pretend_language)
9816fde3
JK
25089{
25090 struct attribute *attr;
25091
25092 /* Set the language we're debugging. */
25093 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 25094 if (attr != nullptr)
529908cb 25095 set_cu_language (attr->constant_value (0), cu);
9816fde3 25096 else
9cded63f 25097 {
95554aad 25098 cu->language = pretend_language;
9cded63f
TT
25099 cu->language_defn = language_def (cu->language);
25100 }
dee91e82 25101
7d45c7c3 25102 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25103}
25104
7188ed02 25105/* See read.h. */
ae038cb0 25106
7188ed02
SM
25107dwarf2_cu *
25108dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 25109{
7188ed02
SM
25110 auto it = m_dwarf2_cus.find (per_cu);
25111 if (it == m_dwarf2_cus.end ())
25112 return nullptr;
ae038cb0 25113
7188ed02
SM
25114 return it->second;
25115}
25116
25117/* See read.h. */
25118
25119void
25120dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
25121{
25122 gdb_assert (this->get_cu (per_cu) == nullptr);
25123
25124 m_dwarf2_cus[per_cu] = cu;
25125}
25126
25127/* See read.h. */
25128
25129void
25130dwarf2_per_objfile::age_comp_units ()
25131{
17e593e9
SM
25132 dwarf_read_debug_printf_v ("running");
25133
08ac5771
SM
25134 /* This is not expected to be called in the middle of CU expansion. There is
25135 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
25136 loaded in memory. Calling age_comp_units while the queue is in use could
25137 make us free the DIEs for a CU that is in the queue and therefore break
25138 that invariant. */
25139 gdb_assert (!this->per_bfd->queue.has_value ());
25140
7188ed02
SM
25141 /* Start by clearing all marks. */
25142 for (auto pair : m_dwarf2_cus)
25143 pair.second->mark = false;
25144
25145 /* Traverse all CUs, mark them and their dependencies if used recently
25146 enough. */
25147 for (auto pair : m_dwarf2_cus)
ae038cb0 25148 {
7188ed02
SM
25149 dwarf2_cu *cu = pair.second;
25150
25151 cu->last_used++;
25152 if (cu->last_used <= dwarf_max_cache_age)
25153 dwarf2_mark (cu);
ae038cb0
DJ
25154 }
25155
7188ed02
SM
25156 /* Delete all CUs still not marked. */
25157 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 25158 {
7188ed02 25159 dwarf2_cu *cu = it->second;
ae038cb0 25160
7188ed02 25161 if (!cu->mark)
ae038cb0 25162 {
17e593e9
SM
25163 dwarf_read_debug_printf_v ("deleting old CU %s",
25164 sect_offset_str (cu->per_cu->sect_off));
7188ed02
SM
25165 delete cu;
25166 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
25167 }
25168 else
7188ed02 25169 it++;
ae038cb0
DJ
25170 }
25171}
25172
7188ed02 25173/* See read.h. */
ae038cb0 25174
7188ed02
SM
25175void
25176dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 25177{
7188ed02
SM
25178 auto it = m_dwarf2_cus.find (per_cu);
25179 if (it == m_dwarf2_cus.end ())
25180 return;
ae038cb0 25181
7188ed02 25182 delete it->second;
ae038cb0 25183
7188ed02
SM
25184 m_dwarf2_cus.erase (it);
25185}
ae038cb0 25186
7188ed02
SM
25187dwarf2_per_objfile::~dwarf2_per_objfile ()
25188{
25189 remove_all_cus ();
ae038cb0
DJ
25190}
25191
dee91e82
DE
25192/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25193 We store these in a hash table separate from the DIEs, and preserve them
25194 when the DIEs are flushed out of cache.
25195
25196 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25197 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25198 or the type may come from a DWO file. Furthermore, while it's more logical
25199 to use per_cu->section+offset, with Fission the section with the data is in
25200 the DWO file but we don't know that section at the point we need it.
25201 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25202 because we can enter the lookup routine, get_die_type_at_offset, from
25203 outside this file, and thus won't necessarily have PER_CU->cu.
25204 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25205
dee91e82 25206struct dwarf2_per_cu_offset_and_type
1c379e20 25207{
dee91e82 25208 const struct dwarf2_per_cu_data *per_cu;
9c541725 25209 sect_offset sect_off;
1c379e20
DJ
25210 struct type *type;
25211};
25212
dee91e82 25213/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25214
25215static hashval_t
dee91e82 25216per_cu_offset_and_type_hash (const void *item)
1c379e20 25217{
9a3c8263
SM
25218 const struct dwarf2_per_cu_offset_and_type *ofs
25219 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25220
9c541725 25221 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25222}
25223
dee91e82 25224/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25225
25226static int
dee91e82 25227per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25228{
9a3c8263
SM
25229 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25230 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25231 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25232 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25233
dee91e82 25234 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25235 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25236}
25237
25238/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25239 table if necessary. For convenience, return TYPE.
25240
25241 The DIEs reading must have careful ordering to:
85102364 25242 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25243 reading current DIE.
25244 * Not trying to dereference contents of still incompletely read in types
25245 while reading in other DIEs.
25246 * Enable referencing still incompletely read in types just by a pointer to
25247 the type without accessing its fields.
25248
25249 Therefore caller should follow these rules:
25250 * Try to fetch any prerequisite types we may need to build this DIE type
25251 before building the type and calling set_die_type.
e71ec853 25252 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25253 possible before fetching more types to complete the current type.
25254 * Make the type as complete as possible before fetching more types. */
1c379e20 25255
f792889a 25256static struct type *
57567375
TT
25257set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25258 bool skip_data_location)
1c379e20 25259{
976ca316 25260 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 25261 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 25262 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
25263 struct attribute *attr;
25264 struct dynamic_prop prop;
1c379e20 25265
b4ba55a1
JB
25266 /* For Ada types, make sure that the gnat-specific data is always
25267 initialized (if not already set). There are a few types where
25268 we should not be doing so, because the type-specific area is
25269 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25270 where the type-specific area is used to store the floatformat).
25271 But this is not a problem, because the gnat-specific information
25272 is actually not needed for these types. */
25273 if (need_gnat_info (cu)
78134374
SM
25274 && type->code () != TYPE_CODE_FUNC
25275 && type->code () != TYPE_CODE_FLT
25276 && type->code () != TYPE_CODE_METHODPTR
25277 && type->code () != TYPE_CODE_MEMBERPTR
25278 && type->code () != TYPE_CODE_METHOD
09584414 25279 && type->code () != TYPE_CODE_FIXED_POINT
b4ba55a1
JB
25280 && !HAVE_GNAT_AUX_INFO (type))
25281 INIT_GNAT_SPECIFIC (type);
25282
3f2f83dd
KB
25283 /* Read DW_AT_allocated and set in type. */
25284 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 25285 if (attr != NULL)
3f2f83dd 25286 {
293e7e51 25287 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25288 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25289 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 25290 }
3f2f83dd
KB
25291
25292 /* Read DW_AT_associated and set in type. */
25293 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 25294 if (attr != NULL)
3f2f83dd 25295 {
293e7e51 25296 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25297 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25298 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 25299 }
3f2f83dd 25300
3cdcd0ce 25301 /* Read DW_AT_data_location and set in type. */
57567375
TT
25302 if (!skip_data_location)
25303 {
25304 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25305 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25306 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25307 }
3cdcd0ce 25308
976ca316
SM
25309 if (per_objfile->die_type_hash == NULL)
25310 per_objfile->die_type_hash
0335378b
TT
25311 = htab_up (htab_create_alloc (127,
25312 per_cu_offset_and_type_hash,
25313 per_cu_offset_and_type_eq,
25314 NULL, xcalloc, xfree));
1c379e20 25315
dee91e82 25316 ofs.per_cu = cu->per_cu;
9c541725 25317 ofs.sect_off = die->sect_off;
1c379e20 25318 ofs.type = type;
dee91e82 25319 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 25320 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 25321 if (*slot)
b98664d3 25322 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25323 sect_offset_str (die->sect_off));
8d749320
SM
25324 *slot = XOBNEW (&objfile->objfile_obstack,
25325 struct dwarf2_per_cu_offset_and_type);
1c379e20 25326 **slot = ofs;
f792889a 25327 return type;
1c379e20
DJ
25328}
25329
9c541725 25330/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25331 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25332
25333static struct type *
9c541725 25334get_die_type_at_offset (sect_offset sect_off,
aa66c379 25335 dwarf2_per_cu_data *per_cu,
976ca316 25336 dwarf2_per_objfile *per_objfile)
1c379e20 25337{
dee91e82 25338 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 25339
976ca316 25340 if (per_objfile->die_type_hash == NULL)
f792889a 25341 return NULL;
1c379e20 25342
dee91e82 25343 ofs.per_cu = per_cu;
9c541725 25344 ofs.sect_off = sect_off;
9a3c8263 25345 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 25346 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
25347 if (slot)
25348 return slot->type;
25349 else
25350 return NULL;
25351}
25352
02142a6c 25353/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25354 or return NULL if DIE does not have a saved type. */
25355
25356static struct type *
25357get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25358{
aa66c379 25359 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
25360}
25361
10b3939b
DJ
25362/* Add a dependence relationship from CU to REF_PER_CU. */
25363
25364static void
25365dwarf2_add_dependence (struct dwarf2_cu *cu,
25366 struct dwarf2_per_cu_data *ref_per_cu)
25367{
25368 void **slot;
25369
25370 if (cu->dependencies == NULL)
25371 cu->dependencies
25372 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25373 NULL, &cu->comp_unit_obstack,
25374 hashtab_obstack_allocate,
25375 dummy_obstack_deallocate);
25376
25377 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25378 if (*slot == NULL)
25379 *slot = ref_per_cu;
25380}
1c379e20 25381
f504f079
DE
25382/* Subroutine of dwarf2_mark to pass to htab_traverse.
25383 Set the mark field in every compilation unit in the
7188ed02
SM
25384 cache that we must keep because we are keeping CU.
25385
25386 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 25387
10b3939b
DJ
25388static int
25389dwarf2_mark_helper (void **slot, void *data)
25390{
7188ed02
SM
25391 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25392 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25393 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
25394
25395 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25396 reading of the chain. As such dependencies remain valid it is not much
25397 useful to track and undo them during QUIT cleanups. */
7188ed02 25398 if (cu == nullptr)
d07ed419
JK
25399 return 1;
25400
7188ed02 25401 if (cu->mark)
10b3939b 25402 return 1;
10b3939b 25403
7188ed02
SM
25404 cu->mark = true;
25405
25406 if (cu->dependencies != nullptr)
25407 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
25408
25409 return 1;
25410}
25411
f504f079
DE
25412/* Set the mark field in CU and in every other compilation unit in the
25413 cache that we must keep because we are keeping CU. */
25414
ae038cb0
DJ
25415static void
25416dwarf2_mark (struct dwarf2_cu *cu)
25417{
25418 if (cu->mark)
25419 return;
7188ed02 25420
9068261f 25421 cu->mark = true;
ae038cb0 25422
7188ed02
SM
25423 if (cu->dependencies != nullptr)
25424 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
25425}
25426
72bf9492
DJ
25427/* Trivial hash function for partial_die_info: the hash value of a DIE
25428 is its offset in .debug_info for this objfile. */
25429
25430static hashval_t
25431partial_die_hash (const void *item)
25432{
9a3c8263
SM
25433 const struct partial_die_info *part_die
25434 = (const struct partial_die_info *) item;
9a619af0 25435
9c541725 25436 return to_underlying (part_die->sect_off);
72bf9492
DJ
25437}
25438
25439/* Trivial comparison function for partial_die_info structures: two DIEs
25440 are equal if they have the same offset. */
25441
25442static int
25443partial_die_eq (const void *item_lhs, const void *item_rhs)
25444{
9a3c8263
SM
25445 const struct partial_die_info *part_die_lhs
25446 = (const struct partial_die_info *) item_lhs;
25447 const struct partial_die_info *part_die_rhs
25448 = (const struct partial_die_info *) item_rhs;
9a619af0 25449
9c541725 25450 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25451}
25452
3c3bb058
AB
25453struct cmd_list_element *set_dwarf_cmdlist;
25454struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 25455
9291a0cd 25456static void
cd4fb1b2
SM
25457show_check_physname (struct ui_file *file, int from_tty,
25458 struct cmd_list_element *c, const char *value)
9291a0cd 25459{
cd4fb1b2
SM
25460 fprintf_filtered (file,
25461 _("Whether to check \"physname\" is %s.\n"),
25462 value);
9291a0cd
TT
25463}
25464
6c265988 25465void _initialize_dwarf2_read ();
cd4fb1b2 25466void
6c265988 25467_initialize_dwarf2_read ()
9291a0cd 25468{
0743fc83 25469 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 25470Set DWARF specific variables.\n\
590042fc 25471Configure DWARF variables such as the cache size."),
0743fc83
TT
25472 &set_dwarf_cmdlist, "maintenance set dwarf ",
25473 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25474
0743fc83 25475 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
25476Show DWARF specific variables.\n\
25477Show DWARF variables such as the cache size."),
0743fc83
TT
25478 &show_dwarf_cmdlist, "maintenance show dwarf ",
25479 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25480
cd4fb1b2
SM
25481 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25482 &dwarf_max_cache_age, _("\
25483Set the upper bound on the age of cached DWARF compilation units."), _("\
25484Show the upper bound on the age of cached DWARF compilation units."), _("\
25485A higher limit means that cached compilation units will be stored\n\
25486in memory longer, and more total memory will be used. Zero disables\n\
25487caching, which can slow down startup."),
25488 NULL,
25489 show_dwarf_max_cache_age,
25490 &set_dwarf_cmdlist,
25491 &show_dwarf_cmdlist);
156942c7 25492
cd4fb1b2
SM
25493 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25494Set debugging of the DWARF reader."), _("\
25495Show debugging of the DWARF reader."), _("\
25496When enabled (non-zero), debugging messages are printed during DWARF\n\
25497reading and symtab expansion. A value of 1 (one) provides basic\n\
25498information. A value greater than 1 provides more verbose information."),
25499 NULL,
25500 NULL,
25501 &setdebuglist, &showdebuglist);
9291a0cd 25502
cd4fb1b2
SM
25503 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25504Set debugging of the DWARF DIE reader."), _("\
25505Show debugging of the DWARF DIE reader."), _("\
25506When enabled (non-zero), DIEs are dumped after they are read in.\n\
25507The value is the maximum depth to print."),
25508 NULL,
25509 NULL,
25510 &setdebuglist, &showdebuglist);
9291a0cd 25511
cd4fb1b2
SM
25512 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25513Set debugging of the dwarf line reader."), _("\
25514Show debugging of the dwarf line reader."), _("\
25515When enabled (non-zero), line number entries are dumped as they are read in.\n\
25516A value of 1 (one) provides basic information.\n\
25517A value greater than 1 provides more verbose information."),
25518 NULL,
25519 NULL,
25520 &setdebuglist, &showdebuglist);
437afbb8 25521
cd4fb1b2
SM
25522 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25523Set cross-checking of \"physname\" code against demangler."), _("\
25524Show cross-checking of \"physname\" code against demangler."), _("\
25525When enabled, GDB's internal \"physname\" code is checked against\n\
25526the demangler."),
25527 NULL, show_check_physname,
25528 &setdebuglist, &showdebuglist);
900e11f9 25529
e615022a
DE
25530 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25531 no_class, &use_deprecated_index_sections, _("\
25532Set whether to use deprecated gdb_index sections."), _("\
25533Show whether to use deprecated gdb_index sections."), _("\
25534When enabled, deprecated .gdb_index sections are used anyway.\n\
25535Normally they are ignored either because of a missing feature or\n\
25536performance issue.\n\
25537Warning: This option must be enabled before gdb reads the file."),
25538 NULL,
25539 NULL,
25540 &setlist, &showlist);
25541
f1e6e072
TT
25542 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25543 &dwarf2_locexpr_funcs);
25544 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25545 &dwarf2_loclist_funcs);
25546
25547 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25548 &dwarf2_block_frame_base_locexpr_funcs);
25549 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25550 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25551
25552#if GDB_SELF_TEST
25553 selftests::register_test ("dw2_expand_symtabs_matching",
25554 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
25555 selftests::register_test ("dwarf2_find_containing_comp_unit",
25556 selftests::find_containing_comp_unit::run_test);
c62446b1 25557#endif
6502dd73 25558}
This page took 7.515722 seconds and 4 git commands to generate.