[gdb/symtab] Fix missing breakpoint location for inlined function
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
b811d2c2 3 Copyright (C) 1994-2020 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
d97bc12b 96/* When non-zero, dump DIEs after they are read in. */
b4f54984 97static unsigned int dwarf_die_debug = 0;
d97bc12b 98
27e0867f 99/* When non-zero, dump line number entries as they are read in. */
8fdd972c 100unsigned int dwarf_line_debug = 0;
27e0867f 101
491144b5
CB
102/* When true, cross-check physname against demangler. */
103static bool check_physname = false;
900e11f9 104
491144b5
CB
105/* When true, do not reject deprecated .gdb_index sections. */
106static bool use_deprecated_index_sections = false;
481860b3 107
17ee85fc
TT
108/* This is used to store the data that is always per objfile. */
109static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
110
111/* These are used to store the dwarf2_per_bfd objects.
112
113 objfiles having the same BFD, which doesn't require relocations, are going to
114 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
115
116 Other objfiles are not going to share a dwarf2_per_bfd with any other
117 objfiles, so they'll have their own version kept in the _objfile_data_key
118 version. */
119static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
120static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
6502dd73 121
f1e6e072
TT
122/* The "aclass" indices for various kinds of computed DWARF symbols. */
123
124static int dwarf2_locexpr_index;
125static int dwarf2_loclist_index;
126static int dwarf2_locexpr_block_index;
127static int dwarf2_loclist_block_index;
128
41144253 129/* Size of .debug_loclists section header for 32-bit DWARF format. */
130#define LOCLIST_HEADER_SIZE32 12
131
132/* Size of .debug_loclists section header for 64-bit DWARF format. */
133#define LOCLIST_HEADER_SIZE64 20
134
3f563c84
PA
135/* An index into a (C++) symbol name component in a symbol name as
136 recorded in the mapped_index's symbol table. For each C++ symbol
137 in the symbol table, we record one entry for the start of each
138 component in the symbol in a table of name components, and then
139 sort the table, in order to be able to binary search symbol names,
140 ignoring leading namespaces, both completion and regular look up.
141 For example, for symbol "A::B::C", we'll have an entry that points
142 to "A::B::C", another that points to "B::C", and another for "C".
143 Note that function symbols in GDB index have no parameter
144 information, just the function/method names. You can convert a
145 name_component to a "const char *" using the
146 'mapped_index::symbol_name_at(offset_type)' method. */
147
148struct name_component
149{
150 /* Offset in the symbol name where the component starts. Stored as
151 a (32-bit) offset instead of a pointer to save memory and improve
152 locality on 64-bit architectures. */
153 offset_type name_offset;
154
155 /* The symbol's index in the symbol and constant pool tables of a
156 mapped_index. */
157 offset_type idx;
158};
159
44ed8f3e
PA
160/* Base class containing bits shared by both .gdb_index and
161 .debug_name indexes. */
162
163struct mapped_index_base
164{
22ca247e
TT
165 mapped_index_base () = default;
166 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
167
44ed8f3e
PA
168 /* The name_component table (a sorted vector). See name_component's
169 description above. */
170 std::vector<name_component> name_components;
171
172 /* How NAME_COMPONENTS is sorted. */
173 enum case_sensitivity name_components_casing;
174
175 /* Return the number of names in the symbol table. */
176 virtual size_t symbol_name_count () const = 0;
177
178 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
179 virtual const char *symbol_name_at
180 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
44ed8f3e
PA
181
182 /* Return whether the name at IDX in the symbol table should be
183 ignored. */
184 virtual bool symbol_name_slot_invalid (offset_type idx) const
185 {
186 return false;
187 }
188
189 /* Build the symbol name component sorted vector, if we haven't
190 yet. */
fcf23d5b 191 void build_name_components (dwarf2_per_objfile *per_objfile);
44ed8f3e
PA
192
193 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
194 possible matches for LN_NO_PARAMS in the name component
195 vector. */
196 std::pair<std::vector<name_component>::const_iterator,
197 std::vector<name_component>::const_iterator>
3b00ef10 198 find_name_components_bounds (const lookup_name_info &ln_no_params,
fcf23d5b
SM
199 enum language lang,
200 dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
201
202 /* Prevent deleting/destroying via a base class pointer. */
203protected:
204 ~mapped_index_base() = default;
205};
206
9291a0cd
TT
207/* A description of the mapped index. The file format is described in
208 a comment by the code that writes the index. */
fc898b42 209struct mapped_index final : public mapped_index_base
9291a0cd 210{
f00a2de2
PA
211 /* A slot/bucket in the symbol table hash. */
212 struct symbol_table_slot
213 {
214 const offset_type name;
215 const offset_type vec;
216 };
217
559a7a62 218 /* Index data format version. */
3063847f 219 int version = 0;
559a7a62 220
f00a2de2
PA
221 /* The address table data. */
222 gdb::array_view<const gdb_byte> address_table;
b11b1f88 223
3876f04e 224 /* The symbol table, implemented as a hash table. */
f00a2de2 225 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 226
9291a0cd 227 /* A pointer to the constant pool. */
3063847f 228 const char *constant_pool = nullptr;
3f563c84 229
44ed8f3e
PA
230 bool symbol_name_slot_invalid (offset_type idx) const override
231 {
232 const auto &bucket = this->symbol_table[idx];
9ab08412 233 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 234 }
5c58de74 235
3f563c84
PA
236 /* Convenience method to get at the name of the symbol at IDX in the
237 symbol table. */
fcf23d5b
SM
238 const char *symbol_name_at
239 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
f00a2de2 240 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 241
44ed8f3e
PA
242 size_t symbol_name_count () const override
243 { return this->symbol_table.size (); }
9291a0cd
TT
244};
245
927aa2e7
JK
246/* A description of the mapped .debug_names.
247 Uninitialized map has CU_COUNT 0. */
fc898b42 248struct mapped_debug_names final : public mapped_index_base
927aa2e7
JK
249{
250 bfd_endian dwarf5_byte_order;
251 bool dwarf5_is_dwarf64;
252 bool augmentation_is_gdb;
253 uint8_t offset_size;
254 uint32_t cu_count = 0;
255 uint32_t tu_count, bucket_count, name_count;
256 const gdb_byte *cu_table_reordered, *tu_table_reordered;
257 const uint32_t *bucket_table_reordered, *hash_table_reordered;
258 const gdb_byte *name_table_string_offs_reordered;
259 const gdb_byte *name_table_entry_offs_reordered;
260 const gdb_byte *entry_pool;
261
262 struct index_val
263 {
264 ULONGEST dwarf_tag;
265 struct attr
266 {
267 /* Attribute name DW_IDX_*. */
268 ULONGEST dw_idx;
269
270 /* Attribute form DW_FORM_*. */
271 ULONGEST form;
272
273 /* Value if FORM is DW_FORM_implicit_const. */
274 LONGEST implicit_const;
275 };
276 std::vector<attr> attr_vec;
277 };
278
279 std::unordered_map<ULONGEST, index_val> abbrev_map;
280
fcf23d5b
SM
281 const char *namei_to_name
282 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
283
284 /* Implementation of the mapped_index_base virtual interface, for
285 the name_components cache. */
286
fcf23d5b
SM
287 const char *symbol_name_at
288 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
289 { return namei_to_name (idx, per_objfile); }
44ed8f3e
PA
290
291 size_t symbol_name_count () const override
292 { return this->name_count; }
927aa2e7
JK
293};
294
cd4fb1b2 295/* See dwarf2read.h. */
ed2dc618 296
cd4fb1b2 297dwarf2_per_objfile *
ed2dc618
SM
298get_dwarf2_per_objfile (struct objfile *objfile)
299{
5bfd760d 300 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 301}
c906108c 302
251d32d9 303/* Default names of the debugging sections. */
c906108c 304
233a11ab
CS
305/* Note that if the debugging section has been compressed, it might
306 have a name like .zdebug_info. */
307
9cdd5dbd
DE
308static const struct dwarf2_debug_sections dwarf2_elf_names =
309{
251d32d9
TG
310 { ".debug_info", ".zdebug_info" },
311 { ".debug_abbrev", ".zdebug_abbrev" },
312 { ".debug_line", ".zdebug_line" },
313 { ".debug_loc", ".zdebug_loc" },
43988095 314 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 315 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 316 { ".debug_macro", ".zdebug_macro" },
251d32d9 317 { ".debug_str", ".zdebug_str" },
18a8505e 318 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 319 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 320 { ".debug_ranges", ".zdebug_ranges" },
43988095 321 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 322 { ".debug_types", ".zdebug_types" },
3019eac3 323 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
324 { ".debug_frame", ".zdebug_frame" },
325 { ".eh_frame", NULL },
24d3216f 326 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
327 { ".debug_names", ".zdebug_names" },
328 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 329 23
251d32d9 330};
c906108c 331
80626a55 332/* List of DWO/DWP sections. */
3019eac3 333
80626a55 334static const struct dwop_section_names
3019eac3
DE
335{
336 struct dwarf2_section_names abbrev_dwo;
337 struct dwarf2_section_names info_dwo;
338 struct dwarf2_section_names line_dwo;
339 struct dwarf2_section_names loc_dwo;
43988095 340 struct dwarf2_section_names loclists_dwo;
09262596
DE
341 struct dwarf2_section_names macinfo_dwo;
342 struct dwarf2_section_names macro_dwo;
3019eac3
DE
343 struct dwarf2_section_names str_dwo;
344 struct dwarf2_section_names str_offsets_dwo;
345 struct dwarf2_section_names types_dwo;
80626a55
DE
346 struct dwarf2_section_names cu_index;
347 struct dwarf2_section_names tu_index;
3019eac3 348}
80626a55 349dwop_section_names =
3019eac3
DE
350{
351 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
352 { ".debug_info.dwo", ".zdebug_info.dwo" },
353 { ".debug_line.dwo", ".zdebug_line.dwo" },
354 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 355 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
356 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
357 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
358 { ".debug_str.dwo", ".zdebug_str.dwo" },
359 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
360 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
361 { ".debug_cu_index", ".zdebug_cu_index" },
362 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
363};
364
c906108c
SS
365/* local data types */
366
41144253 367/* The location list section (.debug_loclists) begins with a header,
368 which contains the following information. */
369struct loclist_header
370{
371 /* A 4-byte or 12-byte length containing the length of the
372 set of entries for this compilation unit, not including the
373 length field itself. */
374 unsigned int length;
375
376 /* A 2-byte version identifier. */
377 short version;
378
379 /* A 1-byte unsigned integer containing the size in bytes of an address on
380 the target system. */
381 unsigned char addr_size;
382
383 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
384 on the target system. */
385 unsigned char segment_collector_size;
386
387 /* A 4-byte count of the number of offsets that follow the header. */
388 unsigned int offset_entry_count;
389};
390
3da10d80
KS
391/* Type used for delaying computation of method physnames.
392 See comments for compute_delayed_physnames. */
393struct delayed_method_info
394{
395 /* The type to which the method is attached, i.e., its parent class. */
396 struct type *type;
397
398 /* The index of the method in the type's function fieldlists. */
399 int fnfield_index;
400
401 /* The index of the method in the fieldlist. */
402 int index;
403
404 /* The name of the DIE. */
405 const char *name;
406
407 /* The DIE associated with this method. */
408 struct die_info *die;
409};
410
e7c27a73
DJ
411/* Internal state when decoding a particular compilation unit. */
412struct dwarf2_cu
413{
9e021579
SM
414 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
415 dwarf2_per_objfile *per_objfile);
fcd3b13d
SM
416
417 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
418
c24bdb02
KS
419 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
420 Create the set of symtabs used by this TU, or if this TU is sharing
421 symtabs with another TU and the symtabs have already been created
422 then restore those symtabs in the line header.
423 We don't need the pc/line-number mapping for type units. */
424 void setup_type_unit_groups (struct die_info *die);
425
426 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
427 buildsym_compunit constructor. */
428 struct compunit_symtab *start_symtab (const char *name,
429 const char *comp_dir,
430 CORE_ADDR low_pc);
431
432 /* Reset the builder. */
433 void reset_builder () { m_builder.reset (); }
434
293e7e51
SM
435 /* Return a type that is a generic pointer type, the size of which
436 matches the address size given in the compilation unit header for
437 this CU. */
438 struct type *addr_type () const;
439
440 /* Find an integer type the same size as the address size given in
441 the compilation unit header for this CU. UNSIGNED_P controls if
442 the integer is unsigned or not. */
443 struct type *addr_sized_int_type (bool unsigned_p) const;
444
d00adf39 445 /* The header of the compilation unit. */
fcd3b13d 446 struct comp_unit_head header {};
e142c38c 447
d00adf39 448 /* Base address of this compilation unit. */
2b24b6e4 449 gdb::optional<CORE_ADDR> base_address;
d00adf39 450
e142c38c 451 /* The language we are debugging. */
fcd3b13d
SM
452 enum language language = language_unknown;
453 const struct language_defn *language_defn = nullptr;
e142c38c 454
fcd3b13d 455 const char *producer = nullptr;
b0f35d58 456
c24bdb02 457private:
804d2729
TT
458 /* The symtab builder for this CU. This is only non-NULL when full
459 symbols are being read. */
c24bdb02 460 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 461
c24bdb02 462public:
e142c38c
DJ
463 /* The generic symbol table building routines have separate lists for
464 file scope symbols and all all other scopes (local scopes). So
465 we need to select the right one to pass to add_symbol_to_list().
466 We do it by keeping a pointer to the correct list in list_in_scope.
467
468 FIXME: The original dwarf code just treated the file scope as the
469 first local scope, and all other local scopes as nested local
470 scopes, and worked fine. Check to see if we really need to
471 distinguish these in buildsym.c. */
fcd3b13d 472 struct pending **list_in_scope = nullptr;
e142c38c 473
b64f50a1
JK
474 /* Hash table holding all the loaded partial DIEs
475 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 476 htab_t partial_dies = nullptr;
72bf9492
DJ
477
478 /* Storage for things with the same lifetime as this read-in compilation
479 unit, including partial DIEs. */
fcd3b13d 480 auto_obstack comp_unit_obstack;
72bf9492 481
69d751e3 482 /* Backlink to our per_cu entry. */
ae038cb0
DJ
483 struct dwarf2_per_cu_data *per_cu;
484
9e021579 485 /* The dwarf2_per_objfile that owns this. */
976ca316 486 dwarf2_per_objfile *per_objfile;
9e021579 487
ae038cb0 488 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 489 int last_used = 0;
ae038cb0 490
b64f50a1
JK
491 /* A hash table of DIE cu_offset for following references with
492 die_info->offset.sect_off as hash. */
fcd3b13d 493 htab_t die_hash = nullptr;
10b3939b
DJ
494
495 /* Full DIEs if read in. */
fcd3b13d 496 struct die_info *dies = nullptr;
10b3939b
DJ
497
498 /* A set of pointers to dwarf2_per_cu_data objects for compilation
499 units referenced by this one. Only set during full symbol processing;
500 partial symbol tables do not have dependencies. */
fcd3b13d 501 htab_t dependencies = nullptr;
10b3939b 502
cb1df416 503 /* Header data from the line table, during full symbol processing. */
fcd3b13d 504 struct line_header *line_header = nullptr;
4c8aa72d 505 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
5989a64e 506 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
4c8aa72d
PA
507 this is the DW_TAG_compile_unit die for this CU. We'll hold on
508 to the line header as long as this DIE is being processed. See
509 process_die_scope. */
fcd3b13d 510 die_info *line_header_die_owner = nullptr;
cb1df416 511
3da10d80
KS
512 /* A list of methods which need to have physnames computed
513 after all type information has been read. */
c89b44cd 514 std::vector<delayed_method_info> method_list;
3da10d80 515
96408a79 516 /* To be copied to symtab->call_site_htab. */
fcd3b13d 517 htab_t call_site_htab = nullptr;
96408a79 518
034e5797
DE
519 /* Non-NULL if this CU came from a DWO file.
520 There is an invariant here that is important to remember:
521 Except for attributes copied from the top level DIE in the "main"
522 (or "stub") file in preparation for reading the DWO file
18a8505e 523 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
524 Either there isn't a DWO file (in which case this is NULL and the point
525 is moot), or there is and either we're not going to read it (in which
526 case this is NULL) or there is and we are reading it (in which case this
527 is non-NULL). */
fcd3b13d 528 struct dwo_unit *dwo_unit = nullptr;
3019eac3 529
18a8505e 530 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 531 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 532 gdb::optional<ULONGEST> addr_base;
3019eac3 533
18a8505e 534 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 535 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 536 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
537 be used without needing to know whether DWO files are in use or not.
538 N.B. This does not apply to DW_AT_ranges appearing in
539 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
540 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 541 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 542 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 543 ULONGEST ranges_base = 0;
2e3cf129 544
41144253 545 /* The DW_AT_loclists_base attribute if present. */
546 ULONGEST loclist_base = 0;
547
c9317f21
TT
548 /* When reading debug info generated by older versions of rustc, we
549 have to rewrite some union types to be struct types with a
550 variant part. This rewriting must be done after the CU is fully
551 read in, because otherwise at the point of rewriting some struct
552 type might not have been fully processed. So, we keep a list of
553 all such types here and process them after expansion. */
554 std::vector<struct type *> rust_unions;
555
18a8505e
AT
556 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
557 files, the value is implicitly zero. For DWARF 5 version DWO files, the
558 value is often implicit and is the size of the header of
559 .debug_str_offsets section (8 or 4, depending on the address size). */
560 gdb::optional<ULONGEST> str_offsets_base;
561
ae038cb0 562 /* Mark used when releasing cached dies. */
9068261f 563 bool mark : 1;
ae038cb0 564
8be455d7
JK
565 /* This CU references .debug_loc. See the symtab->locations_valid field.
566 This test is imperfect as there may exist optimized debug code not using
567 any location list and still facing inlining issues if handled as
568 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 569 bool has_loclist : 1;
ba919b58 570
9068261f 571 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
572 if all the producer_is_* fields are valid. This information is cached
573 because profiling CU expansion showed excessive time spent in
574 producer_is_gxx_lt_4_6. */
9068261f
AB
575 bool checked_producer : 1;
576 bool producer_is_gxx_lt_4_6 : 1;
577 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 578 bool producer_is_icc : 1;
9068261f 579 bool producer_is_icc_lt_14 : 1;
c258c396 580 bool producer_is_codewarrior : 1;
4d4ec4e5 581
9068261f 582 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
583 debugging info for C++ namespaces. GCC 3.3.x did not produce
584 this information, but later versions do. */
585
9068261f 586 bool processing_has_namespace_info : 1;
d590ff25
YQ
587
588 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
589
590 /* If this CU was inherited by another CU (via specification,
591 abstract_origin, etc), this is the ancestor CU. */
592 dwarf2_cu *ancestor;
593
594 /* Get the buildsym_compunit for this CU. */
595 buildsym_compunit *get_builder ()
596 {
597 /* If this CU has a builder associated with it, use that. */
598 if (m_builder != nullptr)
599 return m_builder.get ();
600
601 /* Otherwise, search ancestors for a valid builder. */
602 if (ancestor != nullptr)
603 return ancestor->get_builder ();
604
605 return nullptr;
606 }
e7c27a73
DJ
607};
608
094b34ac
DE
609/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
610 This includes type_unit_group and quick_file_names. */
611
612struct stmt_list_hash
613{
614 /* The DWO unit this table is from or NULL if there is none. */
615 struct dwo_unit *dwo_unit;
616
617 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 618 sect_offset line_sect_off;
094b34ac
DE
619};
620
5989a64e 621/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
8adb8487
TT
622 an object of this type. This contains elements of type unit groups
623 that can be shared across objfiles. The non-shareable parts are in
624 type_unit_group_unshareable. */
f4dc4d17
DE
625
626struct type_unit_group
627{
0186c6a7 628 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
629 To simplify things we create an artificial CU that "includes" all the
630 type units using this stmt_list so that the rest of the code still has
197400e8 631 a "per_cu" handle on the symtab. */
094b34ac
DE
632 struct dwarf2_per_cu_data per_cu;
633
0186c6a7
DE
634 /* The TUs that share this DW_AT_stmt_list entry.
635 This is added to while parsing type units to build partial symtabs,
636 and is deleted afterwards and not used again. */
a8b3b8e9 637 std::vector<signatured_type *> *tus;
f4dc4d17 638
094b34ac
DE
639 /* The data used to construct the hash key. */
640 struct stmt_list_hash hash;
f4dc4d17
DE
641};
642
73869dc2 643/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
644
645struct dwo_sections
646{
647 struct dwarf2_section_info abbrev;
3019eac3
DE
648 struct dwarf2_section_info line;
649 struct dwarf2_section_info loc;
43988095 650 struct dwarf2_section_info loclists;
09262596
DE
651 struct dwarf2_section_info macinfo;
652 struct dwarf2_section_info macro;
3019eac3
DE
653 struct dwarf2_section_info str;
654 struct dwarf2_section_info str_offsets;
80626a55
DE
655 /* In the case of a virtual DWO file, these two are unused. */
656 struct dwarf2_section_info info;
fd5866f6 657 std::vector<dwarf2_section_info> types;
3019eac3
DE
658};
659
c88ee1f0 660/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
661
662struct dwo_unit
663{
664 /* Backlink to the containing struct dwo_file. */
665 struct dwo_file *dwo_file;
666
667 /* The "id" that distinguishes this CU/TU.
668 .debug_info calls this "dwo_id", .debug_types calls this "signature".
669 Since signatures came first, we stick with it for consistency. */
670 ULONGEST signature;
671
672 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 673 struct dwarf2_section_info *section;
3019eac3 674
9c541725
PA
675 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
676 sect_offset sect_off;
3019eac3
DE
677 unsigned int length;
678
679 /* For types, offset in the type's DIE of the type defined by this TU. */
680 cu_offset type_offset_in_tu;
681};
682
73869dc2
DE
683/* include/dwarf2.h defines the DWP section codes.
684 It defines a max value but it doesn't define a min value, which we
685 use for error checking, so provide one. */
686
687enum dwp_v2_section_ids
688{
689 DW_SECT_MIN = 1
690};
691
80626a55 692/* Data for one DWO file.
57d63ce2
DE
693
694 This includes virtual DWO files (a virtual DWO file is a DWO file as it
695 appears in a DWP file). DWP files don't really have DWO files per se -
696 comdat folding of types "loses" the DWO file they came from, and from
697 a high level view DWP files appear to contain a mass of random types.
698 However, to maintain consistency with the non-DWP case we pretend DWP
699 files contain virtual DWO files, and we assign each TU with one virtual
700 DWO file (generally based on the line and abbrev section offsets -
701 a heuristic that seems to work in practice). */
3019eac3
DE
702
703struct dwo_file
704{
51ac9db5
SM
705 dwo_file () = default;
706 DISABLE_COPY_AND_ASSIGN (dwo_file);
707
18a8505e 708 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
709 For virtual DWO files the name is constructed from the section offsets
710 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
711 from related CU+TUs. */
51ac9db5 712 const char *dwo_name = nullptr;
0ac5b59e
DE
713
714 /* The DW_AT_comp_dir attribute. */
51ac9db5 715 const char *comp_dir = nullptr;
3019eac3 716
80626a55
DE
717 /* The bfd, when the file is open. Otherwise this is NULL.
718 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 719 gdb_bfd_ref_ptr dbfd;
3019eac3 720
73869dc2
DE
721 /* The sections that make up this DWO file.
722 Remember that for virtual DWO files in DWP V2, these are virtual
723 sections (for lack of a better name). */
51ac9db5 724 struct dwo_sections sections {};
3019eac3 725
33c5cd75
DB
726 /* The CUs in the file.
727 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
728 an extension to handle LLVM's Link Time Optimization output (where
729 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 730 htab_up cus;
3019eac3
DE
731
732 /* Table of TUs in the file.
733 Each element is a struct dwo_unit. */
b0b6a987 734 htab_up tus;
3019eac3
DE
735};
736
80626a55
DE
737/* These sections are what may appear in a DWP file. */
738
739struct dwp_sections
740{
73869dc2 741 /* These are used by both DWP version 1 and 2. */
80626a55
DE
742 struct dwarf2_section_info str;
743 struct dwarf2_section_info cu_index;
744 struct dwarf2_section_info tu_index;
73869dc2
DE
745
746 /* These are only used by DWP version 2 files.
747 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
748 sections are referenced by section number, and are not recorded here.
749 In DWP version 2 there is at most one copy of all these sections, each
750 section being (effectively) comprised of the concatenation of all of the
751 individual sections that exist in the version 1 format.
752 To keep the code simple we treat each of these concatenated pieces as a
753 section itself (a virtual section?). */
754 struct dwarf2_section_info abbrev;
755 struct dwarf2_section_info info;
756 struct dwarf2_section_info line;
757 struct dwarf2_section_info loc;
758 struct dwarf2_section_info macinfo;
759 struct dwarf2_section_info macro;
760 struct dwarf2_section_info str_offsets;
761 struct dwarf2_section_info types;
80626a55
DE
762};
763
73869dc2
DE
764/* These sections are what may appear in a virtual DWO file in DWP version 1.
765 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 766
73869dc2 767struct virtual_v1_dwo_sections
80626a55
DE
768{
769 struct dwarf2_section_info abbrev;
770 struct dwarf2_section_info line;
771 struct dwarf2_section_info loc;
772 struct dwarf2_section_info macinfo;
773 struct dwarf2_section_info macro;
774 struct dwarf2_section_info str_offsets;
775 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 776 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
777 struct dwarf2_section_info info_or_types;
778};
779
73869dc2
DE
780/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
781 In version 2, the sections of the DWO files are concatenated together
782 and stored in one section of that name. Thus each ELF section contains
783 several "virtual" sections. */
784
785struct virtual_v2_dwo_sections
786{
787 bfd_size_type abbrev_offset;
788 bfd_size_type abbrev_size;
789
790 bfd_size_type line_offset;
791 bfd_size_type line_size;
792
793 bfd_size_type loc_offset;
794 bfd_size_type loc_size;
795
796 bfd_size_type macinfo_offset;
797 bfd_size_type macinfo_size;
798
799 bfd_size_type macro_offset;
800 bfd_size_type macro_size;
801
802 bfd_size_type str_offsets_offset;
803 bfd_size_type str_offsets_size;
804
805 /* Each DWP hash table entry records one CU or one TU.
806 That is recorded here, and copied to dwo_unit.section. */
807 bfd_size_type info_or_types_offset;
808 bfd_size_type info_or_types_size;
809};
810
80626a55
DE
811/* Contents of DWP hash tables. */
812
813struct dwp_hash_table
814{
73869dc2 815 uint32_t version, nr_columns;
80626a55 816 uint32_t nr_units, nr_slots;
73869dc2
DE
817 const gdb_byte *hash_table, *unit_table;
818 union
819 {
820 struct
821 {
822 const gdb_byte *indices;
823 } v1;
824 struct
825 {
826 /* This is indexed by column number and gives the id of the section
827 in that column. */
828#define MAX_NR_V2_DWO_SECTIONS \
829 (1 /* .debug_info or .debug_types */ \
830 + 1 /* .debug_abbrev */ \
831 + 1 /* .debug_line */ \
832 + 1 /* .debug_loc */ \
833 + 1 /* .debug_str_offsets */ \
834 + 1 /* .debug_macro or .debug_macinfo */)
835 int section_ids[MAX_NR_V2_DWO_SECTIONS];
836 const gdb_byte *offsets;
837 const gdb_byte *sizes;
838 } v2;
839 } section_pool;
80626a55
DE
840};
841
842/* Data for one DWP file. */
843
844struct dwp_file
845{
400174b1
TT
846 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
847 : name (name_),
848 dbfd (std::move (abfd))
849 {
850 }
851
80626a55
DE
852 /* Name of the file. */
853 const char *name;
854
73869dc2 855 /* File format version. */
400174b1 856 int version = 0;
73869dc2 857
93417882 858 /* The bfd. */
400174b1 859 gdb_bfd_ref_ptr dbfd;
80626a55
DE
860
861 /* Section info for this file. */
400174b1 862 struct dwp_sections sections {};
80626a55 863
57d63ce2 864 /* Table of CUs in the file. */
400174b1 865 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
866
867 /* Table of TUs in the file. */
400174b1 868 const struct dwp_hash_table *tus = nullptr;
80626a55 869
19ac8c2e 870 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
871 htab_up loaded_cus;
872 htab_up loaded_tus;
80626a55 873
73869dc2
DE
874 /* Table to map ELF section numbers to their sections.
875 This is only needed for the DWP V1 file format. */
400174b1
TT
876 unsigned int num_sections = 0;
877 asection **elf_sections = nullptr;
80626a55
DE
878};
879
0963b4bd
MS
880/* Struct used to pass misc. parameters to read_die_and_children, et
881 al. which are used for both .debug_info and .debug_types dies.
882 All parameters here are unchanging for the life of the call. This
dee91e82 883 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
884
885struct die_reader_specs
886{
a32a8923 887 /* The bfd of die_section. */
93311388
DE
888 bfd* abfd;
889
890 /* The CU of the DIE we are parsing. */
891 struct dwarf2_cu *cu;
892
80626a55 893 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
894 struct dwo_file *dwo_file;
895
dee91e82 896 /* The section the die comes from.
3019eac3 897 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
898 struct dwarf2_section_info *die_section;
899
900 /* die_section->buffer. */
d521ce57 901 const gdb_byte *buffer;
f664829e
DE
902
903 /* The end of the buffer. */
904 const gdb_byte *buffer_end;
a2ce51a0 905
685af9cd
TT
906 /* The abbreviation table to use when reading the DIEs. */
907 struct abbrev_table *abbrev_table;
93311388
DE
908};
909
c0ab21c2
TT
910/* A subclass of die_reader_specs that holds storage and has complex
911 constructor and destructor behavior. */
912
913class cutu_reader : public die_reader_specs
914{
915public:
916
ab432490
SM
917 cutu_reader (dwarf2_per_cu_data *this_cu,
918 dwarf2_per_objfile *per_objfile,
c0ab21c2 919 struct abbrev_table *abbrev_table,
2e671100 920 dwarf2_cu *existing_cu,
c0ab21c2
TT
921 bool skip_partial);
922
923 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 924 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
925 struct dwarf2_cu *parent_cu = nullptr,
926 struct dwo_file *dwo_file = nullptr);
927
c0ab21c2
TT
928 DISABLE_COPY_AND_ASSIGN (cutu_reader);
929
930 const gdb_byte *info_ptr = nullptr;
931 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
932 bool dummy_p = false;
933
6751ebae
TT
934 /* Release the new CU, putting it on the chain. This cannot be done
935 for dummy CUs. */
936 void keep ();
937
c0ab21c2 938private:
9e021579
SM
939 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
940 dwarf2_per_objfile *per_objfile,
2e671100 941 dwarf2_cu *existing_cu);
c0ab21c2
TT
942
943 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
944 std::unique_ptr<dwarf2_cu> m_new_cu;
945
946 /* The ordinary abbreviation table. */
947 abbrev_table_up m_abbrev_table_holder;
948
949 /* The DWO abbreviation table. */
950 abbrev_table_up m_dwo_abbrev_table;
951};
dee91e82 952
c906108c 953/* When we construct a partial symbol table entry we only
0963b4bd 954 need this much information. */
6f06d47b 955struct partial_die_info : public allocate_on_obstack
c906108c 956 {
6f06d47b
YQ
957 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
958
959 /* Disable assign but still keep copy ctor, which is needed
960 load_partial_dies. */
961 partial_die_info& operator=(const partial_die_info& rhs) = delete;
962
52356b79
YQ
963 /* Adjust the partial die before generating a symbol for it. This
964 function may set the is_external flag or change the DIE's
965 name. */
966 void fixup (struct dwarf2_cu *cu);
967
48fbe735
YQ
968 /* Read a minimal amount of information into the minimal die
969 structure. */
970 const gdb_byte *read (const struct die_reader_specs *reader,
971 const struct abbrev_info &abbrev,
972 const gdb_byte *info_ptr);
973
7d00ffec
TT
974 /* Compute the name of this partial DIE. This memoizes the
975 result, so it is safe to call multiple times. */
976 const char *name (dwarf2_cu *cu);
977
72bf9492 978 /* Offset of this DIE. */
6f06d47b 979 const sect_offset sect_off;
72bf9492
DJ
980
981 /* DWARF-2 tag for this DIE. */
6f06d47b 982 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 983
72bf9492 984 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
985 const unsigned int has_children : 1;
986
72bf9492
DJ
987 unsigned int is_external : 1;
988 unsigned int is_declaration : 1;
989 unsigned int has_type : 1;
990 unsigned int has_specification : 1;
991 unsigned int has_pc_info : 1;
481860b3 992 unsigned int may_be_inlined : 1;
72bf9492 993
0c1b455e
TT
994 /* This DIE has been marked DW_AT_main_subprogram. */
995 unsigned int main_subprogram : 1;
996
72bf9492
DJ
997 /* Flag set if the SCOPE field of this structure has been
998 computed. */
999 unsigned int scope_set : 1;
1000
fa4028e9
JB
1001 /* Flag set if the DIE has a byte_size attribute. */
1002 unsigned int has_byte_size : 1;
1003
ff908ebf
AW
1004 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1005 unsigned int has_const_value : 1;
1006
98bfdba5
PA
1007 /* Flag set if any of the DIE's children are template arguments. */
1008 unsigned int has_template_arguments : 1;
1009
52356b79 1010 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1011 unsigned int fixup_called : 1;
1012
36586728
TT
1013 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1014 unsigned int is_dwz : 1;
1015
1016 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1017 unsigned int spec_is_dwz : 1;
1018
7d00ffec
TT
1019 unsigned int canonical_name : 1;
1020
72bf9492 1021 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1022 sometimes a default name for unnamed DIEs. */
7d00ffec 1023 const char *raw_name = nullptr;
72bf9492 1024
abc72ce4 1025 /* The linkage name, if present. */
6f06d47b 1026 const char *linkage_name = nullptr;
abc72ce4 1027
72bf9492
DJ
1028 /* The scope to prepend to our children. This is generally
1029 allocated on the comp_unit_obstack, so will disappear
1030 when this compilation unit leaves the cache. */
6f06d47b 1031 const char *scope = nullptr;
72bf9492 1032
95554aad
TT
1033 /* Some data associated with the partial DIE. The tag determines
1034 which field is live. */
1035 union
1036 {
1037 /* The location description associated with this DIE, if any. */
1038 struct dwarf_block *locdesc;
1039 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1040 sect_offset sect_off;
6f06d47b 1041 } d {};
72bf9492
DJ
1042
1043 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1044 CORE_ADDR lowpc = 0;
1045 CORE_ADDR highpc = 0;
72bf9492 1046
93311388 1047 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1048 DW_AT_sibling, if any. */
48fbe735
YQ
1049 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1050 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1051 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1052
1053 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1054 DW_AT_specification (or DW_AT_abstract_origin or
1055 DW_AT_extension). */
6f06d47b 1056 sect_offset spec_offset {};
72bf9492
DJ
1057
1058 /* Pointers to this DIE's parent, first child, and next sibling,
1059 if any. */
6f06d47b
YQ
1060 struct partial_die_info *die_parent = nullptr;
1061 struct partial_die_info *die_child = nullptr;
1062 struct partial_die_info *die_sibling = nullptr;
1063
1064 friend struct partial_die_info *
1065 dwarf2_cu::find_partial_die (sect_offset sect_off);
1066
1067 private:
1068 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1069 partial_die_info (sect_offset sect_off)
1070 : partial_die_info (sect_off, DW_TAG_padding, 0)
1071 {
1072 }
1073
1074 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1075 int has_children_)
1076 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1077 {
1078 is_external = 0;
1079 is_declaration = 0;
1080 has_type = 0;
1081 has_specification = 0;
1082 has_pc_info = 0;
1083 may_be_inlined = 0;
1084 main_subprogram = 0;
1085 scope_set = 0;
1086 has_byte_size = 0;
1087 has_const_value = 0;
1088 has_template_arguments = 0;
1089 fixup_called = 0;
1090 is_dwz = 0;
1091 spec_is_dwz = 0;
7d00ffec 1092 canonical_name = 0;
6f06d47b 1093 }
c906108c
SS
1094 };
1095
c906108c
SS
1096/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1097 but this would require a corresponding change in unpack_field_as_long
1098 and friends. */
1099static int bits_per_byte = 8;
1100
9c6a1327
TT
1101struct variant_part_builder;
1102
1103/* When reading a variant, we track a bit more information about the
1104 field, and store it in an object of this type. */
2ddeaf8a
TT
1105
1106struct variant_field
1107{
9c6a1327
TT
1108 int first_field = -1;
1109 int last_field = -1;
1110
1111 /* A variant can contain other variant parts. */
1112 std::vector<variant_part_builder> variant_parts;
1113
2ddeaf8a
TT
1114 /* If we see a DW_TAG_variant, then this will be set if this is the
1115 default branch. */
9c6a1327
TT
1116 bool default_branch = false;
1117 /* If we see a DW_AT_discr_value, then this will be the discriminant
1118 value. */
1119 ULONGEST discriminant_value = 0;
1120 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1121 data. */
1122 struct dwarf_block *discr_list_data = nullptr;
1123};
1124
1125/* This represents a DW_TAG_variant_part. */
1126
1127struct variant_part_builder
1128{
1129 /* The offset of the discriminant field. */
1130 sect_offset discriminant_offset {};
1131
1132 /* Variants that are direct children of this variant part. */
1133 std::vector<variant_field> variants;
1134
1135 /* True if we're currently reading a variant. */
1136 bool processing_variant = false;
2ddeaf8a
TT
1137};
1138
52059ffd
TT
1139struct nextfield
1140{
be2daae6
TT
1141 int accessibility = 0;
1142 int virtuality = 0;
9c6a1327
TT
1143 /* Variant parts need to find the discriminant, which is a DIE
1144 reference. We track the section offset of each field to make
1145 this link. */
1146 sect_offset offset;
be2daae6 1147 struct field field {};
52059ffd
TT
1148};
1149
1150struct fnfieldlist
1151{
be2daae6
TT
1152 const char *name = nullptr;
1153 std::vector<struct fn_field> fnfields;
52059ffd
TT
1154};
1155
c906108c
SS
1156/* The routines that read and process dies for a C struct or C++ class
1157 pass lists of data member fields and lists of member function fields
1158 in an instance of a field_info structure, as defined below. */
1159struct field_info
c5aa993b 1160 {
0963b4bd 1161 /* List of data member and baseclasses fields. */
be2daae6
TT
1162 std::vector<struct nextfield> fields;
1163 std::vector<struct nextfield> baseclasses;
c906108c 1164
85102364 1165 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1166 int non_public_fields = 0;
c906108c 1167
c5aa993b
JM
1168 /* Member function fieldlist array, contains name of possibly overloaded
1169 member function, number of overloaded member functions and a pointer
1170 to the head of the member function field chain. */
be2daae6 1171 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1172
1173 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1174 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1175 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1176
1177 /* Nested types defined by this class and the number of elements in this
1178 list. */
be2daae6 1179 std::vector<struct decl_field> nested_types_list;
317f7127 1180
9c6a1327
TT
1181 /* If non-null, this is the variant part we are currently
1182 reading. */
1183 variant_part_builder *current_variant_part = nullptr;
1184 /* This holds all the top-level variant parts attached to the type
1185 we're reading. */
1186 std::vector<variant_part_builder> variant_parts;
1187
317f7127
TT
1188 /* Return the total number of fields (including baseclasses). */
1189 int nfields () const
1190 {
1191 return fields.size () + baseclasses.size ();
1192 }
c5aa993b 1193 };
c906108c 1194
ae038cb0
DJ
1195/* Loaded secondary compilation units are kept in memory until they
1196 have not been referenced for the processing of this many
1197 compilation units. Set this to zero to disable caching. Cache
1198 sizes of up to at least twenty will improve startup time for
1199 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1200static int dwarf_max_cache_age = 5;
920d2a44 1201static void
b4f54984
DE
1202show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1203 struct cmd_list_element *c, const char *value)
920d2a44 1204{
3e43a32a 1205 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1206 "DWARF compilation units is %s.\n"),
920d2a44
AC
1207 value);
1208}
4390d890 1209\f
c906108c
SS
1210/* local function prototypes */
1211
918dd910
JK
1212static void dwarf2_find_base_address (struct die_info *die,
1213 struct dwarf2_cu *cu);
1214
891813be 1215static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1216 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1217 const char *name);
0018ea6f 1218
f1902523
JK
1219static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1220 const gdb_byte *info_ptr,
3e225074 1221 struct die_info *type_unit_die);
f1902523 1222
976ca316 1223static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
c906108c 1224
72bf9492
DJ
1225static void scan_partial_symbols (struct partial_die_info *,
1226 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1227 int, struct dwarf2_cu *);
c906108c 1228
72bf9492
DJ
1229static void add_partial_symbol (struct partial_die_info *,
1230 struct dwarf2_cu *);
63d06c5c 1231
72bf9492
DJ
1232static void add_partial_namespace (struct partial_die_info *pdi,
1233 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1234 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1235
5d7cb8df 1236static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1237 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1238 struct dwarf2_cu *cu);
1239
72bf9492
DJ
1240static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1241 struct dwarf2_cu *cu);
91c24f0a 1242
bc30ff58
JB
1243static void add_partial_subprogram (struct partial_die_info *pdi,
1244 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1245 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1246
d521ce57 1247static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1248
dee91e82 1249static struct partial_die_info *load_partial_dies
d521ce57 1250 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1251
fb816e8b
TV
1252/* A pair of partial_die_info and compilation unit. */
1253struct cu_partial_die_info
1254{
1255 /* The compilation unit of the partial_die_info. */
1256 struct dwarf2_cu *cu;
1257 /* A partial_die_info. */
1258 struct partial_die_info *pdi;
122cf0f2
AB
1259
1260 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1261 : cu (cu),
1262 pdi (pdi)
405feb71 1263 { /* Nothing. */ }
122cf0f2
AB
1264
1265private:
1266 cu_partial_die_info () = delete;
fb816e8b
TV
1267};
1268
122cf0f2
AB
1269static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1270 struct dwarf2_cu *);
72bf9492 1271
d521ce57
TT
1272static const gdb_byte *read_attribute (const struct die_reader_specs *,
1273 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1274 const gdb_byte *, bool *need_reprocess);
1275
1276static void read_attribute_reprocess (const struct die_reader_specs *reader,
1277 struct attribute *attr);
1278
1279static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1280
976ca316
SM
1281static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1282 dwarf2_section_info *, sect_offset);
f4dc4d17 1283
ed2dc618 1284static const char *read_indirect_string
976ca316 1285 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
ed2dc618 1286 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1287
ed2dc618 1288static const char *read_indirect_string_at_offset
976ca316 1289 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
927aa2e7 1290
d521ce57
TT
1291static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1292 const gdb_byte *,
3019eac3
DE
1293 unsigned int *);
1294
18a8505e
AT
1295static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1296 ULONGEST str_index);
1297
1298static const char *read_stub_str_index (struct dwarf2_cu *cu,
1299 ULONGEST str_index);
3019eac3 1300
e142c38c 1301static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1302
e142c38c
DJ
1303static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1304 struct dwarf2_cu *);
c906108c 1305
7d45c7c3
KB
1306static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1307 struct dwarf2_cu *cu);
1308
a084a2a6
AT
1309static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1310
05cf31d1
JB
1311static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1312 struct dwarf2_cu *cu);
1313
e142c38c 1314static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1315
e142c38c 1316static struct die_info *die_specification (struct die_info *die,
f2f0e013 1317 struct dwarf2_cu **);
63d06c5c 1318
9c541725 1319static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1320 struct dwarf2_cu *cu);
debd256d 1321
f3f5162e 1322static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1323 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1324 CORE_ADDR, int decode_mapping);
c906108c 1325
804d2729
TT
1326static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1327 const char *);
c906108c 1328
a14ed312 1329static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1330 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1331
ff39bb5e 1332static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1333 struct dwarf2_cu *);
c906108c 1334
ff39bb5e 1335static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1336 struct type *type,
1337 const char *name,
1338 struct obstack *obstack,
12df843f 1339 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1340 const gdb_byte **bytes,
98bfdba5 1341 struct dwarf2_locexpr_baton **baton);
2df3850c 1342
e7c27a73 1343static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1344
b4ba55a1
JB
1345static int need_gnat_info (struct dwarf2_cu *);
1346
3e43a32a
MS
1347static struct type *die_descriptive_type (struct die_info *,
1348 struct dwarf2_cu *);
b4ba55a1
JB
1349
1350static void set_descriptive_type (struct type *, struct die_info *,
1351 struct dwarf2_cu *);
1352
e7c27a73
DJ
1353static struct type *die_containing_type (struct die_info *,
1354 struct dwarf2_cu *);
c906108c 1355
ff39bb5e 1356static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1357 struct dwarf2_cu *);
c906108c 1358
f792889a 1359static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1360
673bfd45
DE
1361static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1362
0d5cff50 1363static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1364
6e70227d 1365static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1366 const char *suffix, int physname,
1367 struct dwarf2_cu *cu);
63d06c5c 1368
e7c27a73 1369static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1370
348e048f
DE
1371static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1372
e7c27a73 1373static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1374
e7c27a73 1375static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1376
96408a79
SA
1377static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1378
71a3c369
TT
1379static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1380
ff013f42 1381static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1382 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1383
41144253 1384/* Return the .debug_loclists section to use for cu. */
1385static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1386
3a2b436a 1387/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1388 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1389enum pc_bounds_kind
1390{
e385593e 1391 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1392 PC_BOUNDS_NOT_PRESENT,
1393
e385593e
JK
1394 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1395 were present but they do not form a valid range of PC addresses. */
1396 PC_BOUNDS_INVALID,
1397
3a2b436a
JK
1398 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1399 PC_BOUNDS_RANGES,
1400
1401 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1402 PC_BOUNDS_HIGH_LOW,
1403};
1404
1405static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1406 CORE_ADDR *, CORE_ADDR *,
1407 struct dwarf2_cu *,
891813be 1408 dwarf2_psymtab *);
c906108c 1409
fae299cd
DC
1410static void get_scope_pc_bounds (struct die_info *,
1411 CORE_ADDR *, CORE_ADDR *,
1412 struct dwarf2_cu *);
1413
801e3a5b
JB
1414static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1415 CORE_ADDR, struct dwarf2_cu *);
1416
a14ed312 1417static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1418 struct dwarf2_cu *);
c906108c 1419
a14ed312 1420static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1421 struct type *, struct dwarf2_cu *);
c906108c 1422
a14ed312 1423static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1424 struct die_info *, struct type *,
e7c27a73 1425 struct dwarf2_cu *);
c906108c 1426
a14ed312 1427static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1428 struct type *,
1429 struct dwarf2_cu *);
c906108c 1430
134d01f1 1431static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1432
e7c27a73 1433static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1434
e7c27a73 1435static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1436
5d7cb8df
JK
1437static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1438
804d2729 1439static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1440
27aa8d6a
SW
1441static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1442
74921315
KS
1443static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1444
f55ee35c
JK
1445static struct type *read_module_type (struct die_info *die,
1446 struct dwarf2_cu *cu);
1447
38d518c9 1448static const char *namespace_name (struct die_info *die,
e142c38c 1449 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1450
134d01f1 1451static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1452
7d79de9a
TT
1453static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1454 bool * = nullptr);
c906108c 1455
6e70227d 1456static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1457 struct dwarf2_cu *);
1458
bf6af496 1459static struct die_info *read_die_and_siblings_1
d521ce57 1460 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1461 struct die_info *);
639d11d3 1462
dee91e82 1463static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1464 const gdb_byte *info_ptr,
1465 const gdb_byte **new_info_ptr,
639d11d3
DC
1466 struct die_info *parent);
1467
d521ce57
TT
1468static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1469 struct die_info **, const gdb_byte *,
3e225074 1470 int);
3019eac3 1471
d521ce57 1472static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1473 struct die_info **, const gdb_byte *);
93311388 1474
e7c27a73 1475static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1476
15d034d0 1477static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1478 struct objfile *);
71c25dea 1479
15d034d0 1480static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1481
15d034d0 1482static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1483 struct die_info *die,
1484 struct dwarf2_cu *cu);
1485
ca69b9e6
DE
1486static const char *dwarf2_physname (const char *name, struct die_info *die,
1487 struct dwarf2_cu *cu);
1488
e142c38c 1489static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1490 struct dwarf2_cu **);
9219021c 1491
d97bc12b
DE
1492static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1493
1494static void dump_die_for_error (struct die_info *);
1495
1496static void dump_die_1 (struct ui_file *, int level, int max_level,
1497 struct die_info *);
c906108c 1498
d97bc12b 1499/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1500
51545339 1501static void store_in_ref_table (struct die_info *,
10b3939b 1502 struct dwarf2_cu *);
c906108c 1503
348e048f 1504static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1505 const struct attribute *,
348e048f
DE
1506 struct dwarf2_cu **);
1507
10b3939b 1508static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1509 const struct attribute *,
f2f0e013 1510 struct dwarf2_cu **);
c906108c 1511
348e048f 1512static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1513 const struct attribute *,
348e048f
DE
1514 struct dwarf2_cu **);
1515
ac9ec31b
DE
1516static struct type *get_signatured_type (struct die_info *, ULONGEST,
1517 struct dwarf2_cu *);
1518
1519static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1520 const struct attribute *,
ac9ec31b
DE
1521 struct dwarf2_cu *);
1522
ab432490
SM
1523static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1524 dwarf2_per_objfile *per_objfile);
348e048f 1525
ab432490
SM
1526static void read_signatured_type (signatured_type *sig_type,
1527 dwarf2_per_objfile *per_objfile);
348e048f 1528
63e43d3a
PMR
1529static int attr_to_dynamic_prop (const struct attribute *attr,
1530 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1531 struct dynamic_prop *prop, struct type *type);
63e43d3a 1532
c906108c
SS
1533/* memory allocation interface */
1534
7b5a2f43 1535static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1536
b60c80d6 1537static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1538
43f3e411 1539static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1540
8cf6f0b1
TT
1541static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1542 struct dwarf2_loclist_baton *baton,
ff39bb5e 1543 const struct attribute *attr);
8cf6f0b1 1544
ff39bb5e 1545static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1546 struct symbol *sym,
f1e6e072
TT
1547 struct dwarf2_cu *cu,
1548 int is_block);
4c2df51b 1549
d521ce57
TT
1550static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1551 const gdb_byte *info_ptr,
1552 struct abbrev_info *abbrev);
4bb7a0a7 1553
72bf9492
DJ
1554static hashval_t partial_die_hash (const void *item);
1555
1556static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1557
ae038cb0 1558static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618 1559 (sect_offset sect_off, unsigned int offset_in_dwz,
976ca316 1560 dwarf2_per_objfile *per_objfile);
ae038cb0 1561
9816fde3 1562static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1563 struct die_info *comp_unit_die,
1564 enum language pretend_language);
93311388 1565
f792889a
DJ
1566static struct type *set_die_type (struct die_info *, struct type *,
1567 struct dwarf2_cu *);
1c379e20 1568
976ca316 1569static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
ae038cb0 1570
976ca316 1571static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1fd400ff 1572
ab432490
SM
1573static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1574 dwarf2_per_objfile *per_objfile,
1575 bool skip_partial,
1576 enum language pretend_language);
10b3939b 1577
8fc0b21d 1578static void process_full_comp_unit (dwarf2_cu *cu,
47b14e86 1579 enum language pretend_language);
10b3939b 1580
8fc0b21d 1581static void process_full_type_unit (dwarf2_cu *cu,
47b14e86 1582 enum language pretend_language);
f4dc4d17 1583
10b3939b
DJ
1584static void dwarf2_add_dependence (struct dwarf2_cu *,
1585 struct dwarf2_per_cu_data *);
1586
ae038cb0
DJ
1587static void dwarf2_mark (struct dwarf2_cu *);
1588
b64f50a1 1589static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1590 dwarf2_per_cu_data *per_cu,
1591 dwarf2_per_objfile *per_objfile);
673bfd45 1592
f792889a 1593static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1594
120ce1b5
SM
1595static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1596 dwarf2_per_objfile *per_objfile,
95554aad
TT
1597 enum language pretend_language);
1598
976ca316 1599static void process_queue (dwarf2_per_objfile *per_objfile);
9291a0cd 1600
b303c6f6
AB
1601/* Class, the destructor of which frees all allocated queue entries. This
1602 will only have work to do if an error was thrown while processing the
1603 dwarf. If no error was thrown then the queue entries should have all
1604 been processed, and freed, as we went along. */
1605
1606class dwarf2_queue_guard
1607{
1608public:
39856def
TT
1609 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1610 : m_per_objfile (per_objfile)
1611 {
1612 }
b303c6f6
AB
1613
1614 /* Free any entries remaining on the queue. There should only be
1615 entries left if we hit an error while processing the dwarf. */
1616 ~dwarf2_queue_guard ()
1617 {
39856def
TT
1618 /* Ensure that no memory is allocated by the queue. */
1619 std::queue<dwarf2_queue_item> empty;
5989a64e 1620 std::swap (m_per_objfile->per_bfd->queue, empty);
39856def 1621 }
b303c6f6 1622
39856def 1623 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1624
39856def
TT
1625private:
1626 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1627};
1628
39856def
TT
1629dwarf2_queue_item::~dwarf2_queue_item ()
1630{
1631 /* Anything still marked queued is likely to be in an
1632 inconsistent state, so discard it. */
1633 if (per_cu->queued)
1634 {
7188ed02 1635 per_objfile->remove_cu (per_cu);
39856def
TT
1636 per_cu->queued = 0;
1637 }
1638}
1639
d721ba37
PA
1640/* The return type of find_file_and_directory. Note, the enclosed
1641 string pointers are only valid while this object is valid. */
1642
1643struct file_and_directory
1644{
1645 /* The filename. This is never NULL. */
1646 const char *name;
1647
1648 /* The compilation directory. NULL if not known. If we needed to
1649 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1650 points directly to the DW_AT_comp_dir string attribute owned by
1651 the obstack that owns the DIE. */
1652 const char *comp_dir;
1653
1654 /* If we needed to build a new string for comp_dir, this is what
1655 owns the storage. */
1656 std::string comp_dir_storage;
1657};
1658
1659static file_and_directory find_file_and_directory (struct die_info *die,
1660 struct dwarf2_cu *cu);
9291a0cd 1661
298e9637 1662static htab_up allocate_signatured_type_table ();
1fd400ff 1663
298e9637 1664static htab_up allocate_dwo_unit_table ();
3019eac3 1665
57d63ce2 1666static struct dwo_unit *lookup_dwo_unit_in_dwp
976ca316
SM
1667 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1668 const char *comp_dir, ULONGEST signature, int is_debug_types);
a2ce51a0 1669
976ca316 1670static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
a2ce51a0 1671
3019eac3 1672static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1673 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1674 ULONGEST signature);
3019eac3
DE
1675
1676static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1677 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1678
1b555f17 1679static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
89e63ee4 1680
263db9a1
TT
1681/* A unique pointer to a dwo_file. */
1682
51ac9db5 1683typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1684
976ca316 1685static void process_cu_includes (dwarf2_per_objfile *per_objfile);
95554aad 1686
1b80a9fa 1687static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1688
1689static void free_line_header_voidp (void *arg);
4390d890
DE
1690\f
1691/* Various complaints about symbol reading that don't abort the process. */
1692
4390d890
DE
1693static void
1694dwarf2_debug_line_missing_file_complaint (void)
1695{
b98664d3 1696 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1697}
1698
1699static void
1700dwarf2_debug_line_missing_end_sequence_complaint (void)
1701{
b98664d3 1702 complaint (_(".debug_line section has line "
4390d890
DE
1703 "program sequence without an end"));
1704}
1705
1706static void
1707dwarf2_complex_location_expr_complaint (void)
1708{
b98664d3 1709 complaint (_("location expression too complex"));
4390d890
DE
1710}
1711
1712static void
1713dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1714 int arg3)
1715{
b98664d3 1716 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1717 arg1, arg2, arg3);
1718}
1719
4390d890
DE
1720static void
1721dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1722{
b98664d3 1723 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1724 arg1, arg2);
1725}
527f3840
JK
1726
1727/* Hash function for line_header_hash. */
1728
1729static hashval_t
1730line_header_hash (const struct line_header *ofs)
1731{
9c541725 1732 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1733}
1734
1735/* Hash function for htab_create_alloc_ex for line_header_hash. */
1736
1737static hashval_t
1738line_header_hash_voidp (const void *item)
1739{
9a3c8263 1740 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1741
1742 return line_header_hash (ofs);
1743}
1744
1745/* Equality function for line_header_hash. */
1746
1747static int
1748line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1749{
9a3c8263
SM
1750 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1751 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1752
9c541725 1753 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1754 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1755}
1756
4390d890 1757\f
9291a0cd 1758
330cdd98
PA
1759/* See declaration. */
1760
5989a64e
SM
1761dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1762 bool can_copy_)
c3699833
SM
1763 : obfd (obfd),
1764 can_copy (can_copy_)
330cdd98
PA
1765{
1766 if (names == NULL)
1767 names = &dwarf2_elf_names;
1768
330cdd98
PA
1769 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1770 locate_sections (obfd, sec, *names);
1771}
1772
5989a64e 1773dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98 1774{
b76e467d 1775 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1776 per_cu->imported_symtabs_free ();
fc8e7e75 1777
b2bdb8cf 1778 for (signatured_type *sig_type : all_type_units)
ae640021 1779 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1780
5989a64e 1781 /* Everything else should be on this->obstack. */
330cdd98
PA
1782}
1783
7188ed02 1784/* See read.h. */
330cdd98
PA
1785
1786void
7188ed02 1787dwarf2_per_objfile::remove_all_cus ()
330cdd98 1788{
7188ed02
SM
1789 for (auto pair : m_dwarf2_cus)
1790 delete pair.second;
330cdd98 1791
7188ed02 1792 m_dwarf2_cus.clear ();
330cdd98
PA
1793}
1794
11ed8cad
TT
1795/* A helper class that calls free_cached_comp_units on
1796 destruction. */
1797
1798class free_cached_comp_units
1799{
1800public:
1801
1802 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1803 : m_per_objfile (per_objfile)
1804 {
1805 }
1806
1807 ~free_cached_comp_units ()
1808 {
7188ed02 1809 m_per_objfile->remove_all_cus ();
11ed8cad
TT
1810 }
1811
1812 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1813
1814private:
1815
1816 dwarf2_per_objfile *m_per_objfile;
1817};
1818
af758d11
SM
1819/* See read.h. */
1820
1821bool
1822dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1823{
1824 gdb_assert (per_cu->index < this->m_symtabs.size ());
1825
1826 return this->m_symtabs[per_cu->index] != nullptr;
1827}
1828
1829/* See read.h. */
1830
1831compunit_symtab *
1832dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1833{
1834 gdb_assert (per_cu->index < this->m_symtabs.size ());
1835
1836 return this->m_symtabs[per_cu->index];
1837}
1838
1839/* See read.h. */
1840
1841void
1842dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1843 compunit_symtab *symtab)
1844{
1845 gdb_assert (per_cu->index < this->m_symtabs.size ());
1846 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1847
1848 this->m_symtabs[per_cu->index] = symtab;
1849}
1850
c906108c 1851/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1852 information and return true if we have enough to do something.
1853 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1854 ELF names are used. CAN_COPY is true for formats where symbol
1855 interposition is possible and so symbol values must follow copy
1856 relocation rules. */
c906108c
SS
1857
1858int
251d32d9 1859dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1860 const struct dwarf2_debug_sections *names,
1861 bool can_copy)
c906108c 1862{
97cbe998
SDJ
1863 if (objfile->flags & OBJF_READNEVER)
1864 return 0;
1865
976ca316 1866 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 1867
976ca316 1868 if (per_objfile == NULL)
5989a64e 1869 {
17ee85fc
TT
1870 dwarf2_per_bfd *per_bfd;
1871
1872 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
1873 doesn't require relocations and if there aren't partial symbols
1874 from some other reader. */
1875 if (!objfile_has_partial_symbols (objfile)
1876 && !gdb_bfd_requires_relocations (objfile->obfd))
1877 {
1878 /* See if one has been created for this BFD yet. */
1879 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1880
1881 if (per_bfd == nullptr)
1882 {
1883 /* No, create it now. */
1884 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1885 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1886 }
1887 }
1888 else
1889 {
1890 /* No sharing possible, create one specifically for this objfile. */
1891 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1892 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1893 }
5989a64e 1894
976ca316 1895 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 1896 }
5bfd760d 1897
976ca316
SM
1898 return (!per_objfile->per_bfd->info.is_virtual
1899 && per_objfile->per_bfd->info.s.section != NULL
1900 && !per_objfile->per_bfd->abbrev.is_virtual
1901 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1902}
1903
251d32d9
TG
1904/* When loading sections, we look either for uncompressed section or for
1905 compressed section names. */
233a11ab
CS
1906
1907static int
251d32d9
TG
1908section_is_p (const char *section_name,
1909 const struct dwarf2_section_names *names)
233a11ab 1910{
251d32d9
TG
1911 if (names->normal != NULL
1912 && strcmp (section_name, names->normal) == 0)
1913 return 1;
1914 if (names->compressed != NULL
1915 && strcmp (section_name, names->compressed) == 0)
1916 return 1;
1917 return 0;
233a11ab
CS
1918}
1919
330cdd98 1920/* See declaration. */
c906108c 1921
330cdd98 1922void
5989a64e
SM
1923dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1924 const dwarf2_debug_sections &names)
c906108c 1925{
fd361982 1926 flagword aflag = bfd_section_flags (sectp);
251d32d9 1927
dc7650b8
JK
1928 if ((aflag & SEC_HAS_CONTENTS) == 0)
1929 {
1930 }
950b7495
KS
1931 else if (elf_section_data (sectp)->this_hdr.sh_size
1932 > bfd_get_file_size (abfd))
1933 {
1934 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1935 warning (_("Discarding section %s which has a section size (%s"
1936 ") larger than the file size [in module %s]"),
1937 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1938 bfd_get_filename (abfd));
1939 }
330cdd98 1940 else if (section_is_p (sectp->name, &names.info))
c906108c 1941 {
330cdd98 1942 this->info.s.section = sectp;
fd361982 1943 this->info.size = bfd_section_size (sectp);
c906108c 1944 }
330cdd98 1945 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1946 {
330cdd98 1947 this->abbrev.s.section = sectp;
fd361982 1948 this->abbrev.size = bfd_section_size (sectp);
c906108c 1949 }
330cdd98 1950 else if (section_is_p (sectp->name, &names.line))
c906108c 1951 {
330cdd98 1952 this->line.s.section = sectp;
fd361982 1953 this->line.size = bfd_section_size (sectp);
c906108c 1954 }
330cdd98 1955 else if (section_is_p (sectp->name, &names.loc))
c906108c 1956 {
330cdd98 1957 this->loc.s.section = sectp;
fd361982 1958 this->loc.size = bfd_section_size (sectp);
c906108c 1959 }
330cdd98 1960 else if (section_is_p (sectp->name, &names.loclists))
43988095 1961 {
330cdd98 1962 this->loclists.s.section = sectp;
fd361982 1963 this->loclists.size = bfd_section_size (sectp);
43988095 1964 }
330cdd98 1965 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1966 {
330cdd98 1967 this->macinfo.s.section = sectp;
fd361982 1968 this->macinfo.size = bfd_section_size (sectp);
c906108c 1969 }
330cdd98 1970 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1971 {
330cdd98 1972 this->macro.s.section = sectp;
fd361982 1973 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1974 }
330cdd98 1975 else if (section_is_p (sectp->name, &names.str))
c906108c 1976 {
330cdd98 1977 this->str.s.section = sectp;
fd361982 1978 this->str.size = bfd_section_size (sectp);
c906108c 1979 }
18a8505e
AT
1980 else if (section_is_p (sectp->name, &names.str_offsets))
1981 {
1982 this->str_offsets.s.section = sectp;
1983 this->str_offsets.size = bfd_section_size (sectp);
1984 }
330cdd98 1985 else if (section_is_p (sectp->name, &names.line_str))
43988095 1986 {
330cdd98 1987 this->line_str.s.section = sectp;
fd361982 1988 this->line_str.size = bfd_section_size (sectp);
43988095 1989 }
330cdd98 1990 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1991 {
330cdd98 1992 this->addr.s.section = sectp;
fd361982 1993 this->addr.size = bfd_section_size (sectp);
3019eac3 1994 }
330cdd98 1995 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1996 {
330cdd98 1997 this->frame.s.section = sectp;
fd361982 1998 this->frame.size = bfd_section_size (sectp);
b6af0555 1999 }
330cdd98 2000 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2001 {
330cdd98 2002 this->eh_frame.s.section = sectp;
fd361982 2003 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2004 }
330cdd98 2005 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2006 {
330cdd98 2007 this->ranges.s.section = sectp;
fd361982 2008 this->ranges.size = bfd_section_size (sectp);
af34e669 2009 }
330cdd98 2010 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2011 {
330cdd98 2012 this->rnglists.s.section = sectp;
fd361982 2013 this->rnglists.size = bfd_section_size (sectp);
43988095 2014 }
330cdd98 2015 else if (section_is_p (sectp->name, &names.types))
348e048f 2016 {
8b70b953
TT
2017 struct dwarf2_section_info type_section;
2018
2019 memset (&type_section, 0, sizeof (type_section));
049412e3 2020 type_section.s.section = sectp;
fd361982 2021 type_section.size = bfd_section_size (sectp);
8b70b953 2022
fd5866f6 2023 this->types.push_back (type_section);
348e048f 2024 }
330cdd98 2025 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2026 {
330cdd98 2027 this->gdb_index.s.section = sectp;
fd361982 2028 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2029 }
927aa2e7
JK
2030 else if (section_is_p (sectp->name, &names.debug_names))
2031 {
2032 this->debug_names.s.section = sectp;
fd361982 2033 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2034 }
2035 else if (section_is_p (sectp->name, &names.debug_aranges))
2036 {
2037 this->debug_aranges.s.section = sectp;
fd361982 2038 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2039 }
dce234bc 2040
fd361982
AM
2041 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2042 && bfd_section_vma (sectp) == 0)
330cdd98 2043 this->has_section_at_zero = true;
c906108c
SS
2044}
2045
dce234bc 2046/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2047 SECTION_NAME. */
af34e669 2048
dce234bc 2049void
3017a003
TG
2050dwarf2_get_section_info (struct objfile *objfile,
2051 enum dwarf2_section_enum sect,
d521ce57 2052 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2053 bfd_size_type *sizep)
2054{
976ca316 2055 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 2056 struct dwarf2_section_info *info;
a3b2a86b
TT
2057
2058 /* We may see an objfile without any DWARF, in which case we just
2059 return nothing. */
976ca316 2060 if (per_objfile == NULL)
a3b2a86b
TT
2061 {
2062 *sectp = NULL;
2063 *bufp = NULL;
2064 *sizep = 0;
2065 return;
2066 }
3017a003
TG
2067 switch (sect)
2068 {
2069 case DWARF2_DEBUG_FRAME:
976ca316 2070 info = &per_objfile->per_bfd->frame;
3017a003
TG
2071 break;
2072 case DWARF2_EH_FRAME:
976ca316 2073 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
2074 break;
2075 default:
2076 gdb_assert_not_reached ("unexpected section");
2077 }
dce234bc 2078
96b79293 2079 info->read (objfile);
dce234bc 2080
96b79293 2081 *sectp = info->get_bfd_section ();
dce234bc
PP
2082 *bufp = info->buffer;
2083 *sizep = info->size;
2084}
2085
36586728
TT
2086/* A helper function to find the sections for a .dwz file. */
2087
2088static void
2089locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2090{
9a3c8263 2091 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2092
2093 /* Note that we only support the standard ELF names, because .dwz
2094 is ELF-only (at the time of writing). */
2095 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2096 {
049412e3 2097 dwz_file->abbrev.s.section = sectp;
fd361982 2098 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2099 }
2100 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2101 {
049412e3 2102 dwz_file->info.s.section = sectp;
fd361982 2103 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2104 }
2105 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2106 {
049412e3 2107 dwz_file->str.s.section = sectp;
fd361982 2108 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2109 }
2110 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2111 {
049412e3 2112 dwz_file->line.s.section = sectp;
fd361982 2113 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2114 }
2115 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2116 {
049412e3 2117 dwz_file->macro.s.section = sectp;
fd361982 2118 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2119 }
2ec9a5e0
TT
2120 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2121 {
049412e3 2122 dwz_file->gdb_index.s.section = sectp;
fd361982 2123 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2124 }
927aa2e7
JK
2125 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2126 {
2127 dwz_file->debug_names.s.section = sectp;
fd361982 2128 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2129 }
36586728
TT
2130}
2131
c4973306 2132/* See dwarf2read.h. */
36586728 2133
c4973306 2134struct dwz_file *
c3699833 2135dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
36586728 2136{
36586728 2137 const char *filename;
acd13123 2138 bfd_size_type buildid_len_arg;
dc294be5
TT
2139 size_t buildid_len;
2140 bfd_byte *buildid;
36586728 2141
c3699833
SM
2142 if (per_bfd->dwz_file != NULL)
2143 return per_bfd->dwz_file.get ();
36586728 2144
4db1a1dc 2145 bfd_set_error (bfd_error_no_error);
791afaa2 2146 gdb::unique_xmalloc_ptr<char> data
c3699833 2147 (bfd_get_alt_debug_link_info (per_bfd->obfd,
791afaa2 2148 &buildid_len_arg, &buildid));
4db1a1dc
TT
2149 if (data == NULL)
2150 {
2151 if (bfd_get_error () == bfd_error_no_error)
2152 return NULL;
2153 error (_("could not read '.gnu_debugaltlink' section: %s"),
2154 bfd_errmsg (bfd_get_error ()));
2155 }
791afaa2
TT
2156
2157 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2158
acd13123
TT
2159 buildid_len = (size_t) buildid_len_arg;
2160
791afaa2 2161 filename = data.get ();
d721ba37
PA
2162
2163 std::string abs_storage;
36586728
TT
2164 if (!IS_ABSOLUTE_PATH (filename))
2165 {
14278e1f 2166 gdb::unique_xmalloc_ptr<char> abs
c3699833 2167 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
36586728 2168
14278e1f 2169 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2170 filename = abs_storage.c_str ();
36586728
TT
2171 }
2172
dc294be5
TT
2173 /* First try the file name given in the section. If that doesn't
2174 work, try to use the build-id instead. */
ad80db5b 2175 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
dc294be5 2176 if (dwz_bfd != NULL)
36586728 2177 {
192b62ce 2178 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2179 dwz_bfd.reset (nullptr);
36586728
TT
2180 }
2181
dc294be5
TT
2182 if (dwz_bfd == NULL)
2183 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2184
0d79cdc4
AM
2185 if (dwz_bfd == nullptr)
2186 {
2187 gdb::unique_xmalloc_ptr<char> alt_filename;
c3699833 2188 const char *origname = bfd_get_filename (per_bfd->obfd);
0d79cdc4
AM
2189
2190 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2191 buildid_len,
2192 origname,
2193 &alt_filename));
2194
2195 if (fd.get () >= 0)
2196 {
2197 /* File successfully retrieved from server. */
ad80db5b 2198 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2199
2200 if (dwz_bfd == nullptr)
2201 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2202 alt_filename.get ());
2203 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2204 dwz_bfd.reset (nullptr);
2205 }
2206 }
2207
dc294be5
TT
2208 if (dwz_bfd == NULL)
2209 error (_("could not find '.gnu_debugaltlink' file for %s"),
c3699833 2210 bfd_get_filename (per_bfd->obfd));
dc294be5 2211
7ff8cb8c
TT
2212 std::unique_ptr<struct dwz_file> result
2213 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2214
7ff8cb8c
TT
2215 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2216 result.get ());
36586728 2217
c3699833
SM
2218 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2219 per_bfd->dwz_file = std::move (result);
2220 return per_bfd->dwz_file.get ();
36586728 2221}
9291a0cd 2222\f
7b9f3c50
DE
2223/* DWARF quick_symbols_functions support. */
2224
2225/* TUs can share .debug_line entries, and there can be a lot more TUs than
2226 unique line tables, so we maintain a separate table of all .debug_line
2227 derived entries to support the sharing.
2228 All the quick functions need is the list of file names. We discard the
2229 line_header when we're done and don't need to record it here. */
2230struct quick_file_names
2231{
094b34ac
DE
2232 /* The data used to construct the hash key. */
2233 struct stmt_list_hash hash;
7b9f3c50
DE
2234
2235 /* The number of entries in file_names, real_names. */
2236 unsigned int num_file_names;
2237
2238 /* The file names from the line table, after being run through
2239 file_full_name. */
2240 const char **file_names;
2241
2242 /* The file names from the line table after being run through
2243 gdb_realpath. These are computed lazily. */
2244 const char **real_names;
2245};
2246
2247/* When using the index (and thus not using psymtabs), each CU has an
2248 object of this type. This is used to hold information needed by
2249 the various "quick" methods. */
2250struct dwarf2_per_cu_quick_data
2251{
2252 /* The file table. This can be NULL if there was no file table
2253 or it's currently not read in.
5989a64e 2254 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2255 struct quick_file_names *file_names;
2256
7b9f3c50
DE
2257 /* A temporary mark bit used when iterating over all CUs in
2258 expand_symtabs_matching. */
2259 unsigned int mark : 1;
2260
2261 /* True if we've tried to read the file table and found there isn't one.
2262 There will be no point in trying to read it again next time. */
2263 unsigned int no_file_data : 1;
2264};
2265
094b34ac
DE
2266/* Utility hash function for a stmt_list_hash. */
2267
2268static hashval_t
2269hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2270{
2271 hashval_t v = 0;
2272
2273 if (stmt_list_hash->dwo_unit != NULL)
2274 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2275 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2276 return v;
2277}
2278
2279/* Utility equality function for a stmt_list_hash. */
2280
2281static int
2282eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2283 const struct stmt_list_hash *rhs)
2284{
2285 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2286 return 0;
2287 if (lhs->dwo_unit != NULL
2288 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2289 return 0;
2290
9c541725 2291 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2292}
2293
7b9f3c50
DE
2294/* Hash function for a quick_file_names. */
2295
2296static hashval_t
2297hash_file_name_entry (const void *e)
2298{
9a3c8263
SM
2299 const struct quick_file_names *file_data
2300 = (const struct quick_file_names *) e;
7b9f3c50 2301
094b34ac 2302 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2303}
2304
2305/* Equality function for a quick_file_names. */
2306
2307static int
2308eq_file_name_entry (const void *a, const void *b)
2309{
9a3c8263
SM
2310 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2311 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2312
094b34ac 2313 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2314}
2315
2316/* Delete function for a quick_file_names. */
2317
2318static void
2319delete_file_name_entry (void *e)
2320{
9a3c8263 2321 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2322 int i;
2323
2324 for (i = 0; i < file_data->num_file_names; ++i)
2325 {
2326 xfree ((void*) file_data->file_names[i]);
2327 if (file_data->real_names)
2328 xfree ((void*) file_data->real_names[i]);
2329 }
2330
45940949
TT
2331 /* The space for the struct itself lives on the obstack, so we don't
2332 free it here. */
7b9f3c50
DE
2333}
2334
2335/* Create a quick_file_names hash table. */
2336
5895093f 2337static htab_up
7b9f3c50
DE
2338create_quick_file_names_table (unsigned int nr_initial_entries)
2339{
5895093f
TT
2340 return htab_up (htab_create_alloc (nr_initial_entries,
2341 hash_file_name_entry, eq_file_name_entry,
2342 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2343}
9291a0cd 2344
ab432490
SM
2345/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2346 function is unrelated to symtabs, symtab would have to be created afterwards.
2347 You should call age_cached_comp_units after processing the CU. */
918dd910 2348
1b555f17 2349static dwarf2_cu *
ab432490
SM
2350load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2351 bool skip_partial)
918dd910 2352{
3019eac3 2353 if (per_cu->is_debug_types)
ab432490 2354 load_full_type_unit (per_cu, per_objfile);
918dd910 2355 else
ab432490 2356 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
918dd910 2357
7188ed02
SM
2358 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2359 if (cu == nullptr)
1b555f17 2360 return nullptr; /* Dummy CU. */
2dc860c0 2361
7188ed02 2362 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2363
7188ed02 2364 return cu;
918dd910
JK
2365}
2366
97a1449a 2367/* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2fdf6df6 2368
9291a0cd 2369static void
97a1449a 2370dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2371 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2372{
f4dc4d17
DE
2373 /* Skip type_unit_groups, reading the type units they contain
2374 is handled elsewhere. */
197400e8 2375 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2376 return;
2377
b303c6f6
AB
2378 /* The destructor of dwarf2_queue_guard frees any entries left on
2379 the queue. After this point we're guaranteed to leave this function
2380 with the dwarf queue empty. */
39856def 2381 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2382
976ca316 2383 if (!per_objfile->symtab_set_p (per_cu))
95554aad 2384 {
976ca316
SM
2385 queue_comp_unit (per_cu, per_objfile, language_minimal);
2386 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
89e63ee4
DE
2387
2388 /* If we just loaded a CU from a DWO, and we're working with an index
2389 that may badly handle TUs, load all the TUs in that DWO as well.
2390 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2391 if (!per_cu->is_debug_types
1b555f17
SM
2392 && cu != NULL
2393 && cu->dwo_unit != NULL
976ca316
SM
2394 && per_objfile->per_bfd->index_table != NULL
2395 && per_objfile->per_bfd->index_table->version <= 7
89e63ee4 2396 /* DWP files aren't supported yet. */
976ca316 2397 && get_dwp_file (per_objfile) == NULL)
1b555f17 2398 queue_and_load_all_dwo_tus (cu);
95554aad 2399 }
9291a0cd 2400
976ca316 2401 process_queue (per_objfile);
9291a0cd
TT
2402
2403 /* Age the cache, releasing compilation units that have not
2404 been used recently. */
976ca316 2405 per_objfile->age_comp_units ();
9291a0cd
TT
2406}
2407
97a1449a
SM
2408/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2409 the per-objfile for which this symtab is instantiated.
2410
2411 Returns the resulting symbol table. */
2fdf6df6 2412
43f3e411 2413static struct compunit_symtab *
97a1449a 2414dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2415 dwarf2_per_objfile *per_objfile,
97a1449a 2416 bool skip_partial)
9291a0cd 2417{
976ca316 2418 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2419
976ca316 2420 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2421 {
976ca316 2422 free_cached_comp_units freer (per_objfile);
c83dd867 2423 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2424 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2425 process_cu_includes (per_objfile);
9291a0cd 2426 }
f194fefb 2427
976ca316 2428 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2429}
2430
ff4c9fec 2431/* See declaration. */
f4dc4d17 2432
ff4c9fec 2433dwarf2_per_cu_data *
5989a64e 2434dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2435{
b76e467d 2436 if (index >= this->all_comp_units.size ())
ff4c9fec 2437 {
b76e467d 2438 index -= this->all_comp_units.size ();
b2bdb8cf 2439 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2440 return &this->all_type_units[index]->per_cu;
2441 }
f4dc4d17 2442
ff4c9fec
SM
2443 return this->all_comp_units[index];
2444}
f4dc4d17 2445
ff4c9fec 2446/* See declaration. */
2fdf6df6 2447
ff4c9fec 2448dwarf2_per_cu_data *
5989a64e 2449dwarf2_per_bfd::get_cu (int index)
1fd400ff 2450{
b76e467d 2451 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2452
ff4c9fec 2453 return this->all_comp_units[index];
f4dc4d17
DE
2454}
2455
ff4c9fec 2456/* See declaration. */
f4dc4d17 2457
ff4c9fec 2458signatured_type *
5989a64e 2459dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2460{
b2bdb8cf 2461 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2462
ff4c9fec 2463 return this->all_type_units[index];
1fd400ff
TT
2464}
2465
d3473f0c
TT
2466/* See read.h. */
2467
2468dwarf2_per_cu_data *
5989a64e 2469dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2470{
2471 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2472 result->per_bfd = this;
d3473f0c
TT
2473 result->index = m_num_psymtabs++;
2474 return result;
2475}
2476
2477/* See read.h. */
2478
2479signatured_type *
5989a64e 2480dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2481{
2482 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2483 result->per_cu.per_bfd = this;
d3473f0c
TT
2484 result->per_cu.index = m_num_psymtabs++;
2485 return result;
2486}
2487
168c9250 2488/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2489 obstack, and constructed with the specified field values. */
4b514bc8
JK
2490
2491static dwarf2_per_cu_data *
168c9250
SM
2492create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2493 struct dwarf2_section_info *section,
2494 int is_dwz,
2495 sect_offset sect_off, ULONGEST length)
4b514bc8 2496{
168c9250 2497 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2498 the_cu->sect_off = sect_off;
2499 the_cu->length = length;
4b514bc8 2500 the_cu->section = section;
168c9250 2501 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2502 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2503 the_cu->is_dwz = is_dwz;
2504 return the_cu;
2505}
2506
2ec9a5e0
TT
2507/* A helper for create_cus_from_index that handles a given list of
2508 CUs. */
2fdf6df6 2509
74a0d9f6 2510static void
168c9250 2511create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2512 const gdb_byte *cu_list, offset_type n_elements,
2513 struct dwarf2_section_info *section,
b76e467d 2514 int is_dwz)
9291a0cd 2515{
12359b5e 2516 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2517 {
74a0d9f6 2518 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2519
2520 sect_offset sect_off
2521 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2522 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2523 cu_list += 2 * 8;
2524
b76e467d 2525 dwarf2_per_cu_data *per_cu
168c9250
SM
2526 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2527 length);
2528 per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2529 }
9291a0cd
TT
2530}
2531
2ec9a5e0 2532/* Read the CU list from the mapped index, and use it to create all
168c9250 2533 the CU objects for PER_BFD. */
2ec9a5e0 2534
74a0d9f6 2535static void
168c9250 2536create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2537 const gdb_byte *cu_list, offset_type cu_list_elements,
2538 const gdb_byte *dwz_list, offset_type dwz_elements)
2539{
168c9250
SM
2540 gdb_assert (per_bfd->all_comp_units.empty ());
2541 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2542
168c9250
SM
2543 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2544 &per_bfd->info, 0);
2ec9a5e0
TT
2545
2546 if (dwz_elements == 0)
74a0d9f6 2547 return;
2ec9a5e0 2548
168c9250
SM
2549 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2550 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2551 &dwz->info, 1);
2ec9a5e0
TT
2552}
2553
1fd400ff 2554/* Create the signatured type hash table from the index. */
673bfd45 2555
74a0d9f6 2556static void
12359b5e 2557create_signatured_type_table_from_index
168c9250
SM
2558 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2559 const gdb_byte *bytes, offset_type elements)
1fd400ff 2560{
168c9250
SM
2561 gdb_assert (per_bfd->all_type_units.empty ());
2562 per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2563
298e9637 2564 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2565
12359b5e 2566 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2567 {
52dc124a 2568 struct signatured_type *sig_type;
9c541725 2569 ULONGEST signature;
1fd400ff 2570 void **slot;
9c541725 2571 cu_offset type_offset_in_tu;
1fd400ff 2572
74a0d9f6 2573 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2574 sect_offset sect_off
2575 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2576 type_offset_in_tu
2577 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2578 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2579 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2580 bytes += 3 * 8;
2581
168c9250 2582 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2583 sig_type->signature = signature;
9c541725 2584 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2585 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2586 sig_type->per_cu.section = section;
9c541725 2587 sig_type->per_cu.sect_off = sect_off;
52dc124a 2588 sig_type->per_cu.v.quick
168c9250 2589 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2590 struct dwarf2_per_cu_quick_data);
2591
b0b6a987 2592 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2593 *slot = sig_type;
1fd400ff 2594
168c9250 2595 per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2596 }
2597
168c9250 2598 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2599}
2600
927aa2e7
JK
2601/* Create the signatured type hash table from .debug_names. */
2602
2603static void
2604create_signatured_type_table_from_debug_names
976ca316 2605 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2606 const mapped_debug_names &map,
2607 struct dwarf2_section_info *section,
2608 struct dwarf2_section_info *abbrev_section)
2609{
976ca316 2610 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2611
96b79293
TT
2612 section->read (objfile);
2613 abbrev_section->read (objfile);
927aa2e7 2614
976ca316
SM
2615 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2616 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2617
298e9637 2618 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2619
2620 for (uint32_t i = 0; i < map.tu_count; ++i)
2621 {
2622 struct signatured_type *sig_type;
927aa2e7 2623 void **slot;
927aa2e7
JK
2624
2625 sect_offset sect_off
2626 = (sect_offset) (extract_unsigned_integer
2627 (map.tu_table_reordered + i * map.offset_size,
2628 map.offset_size,
2629 map.dwarf5_byte_order));
2630
2631 comp_unit_head cu_header;
976ca316 2632 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2633 abbrev_section,
927aa2e7
JK
2634 section->buffer + to_underlying (sect_off),
2635 rcuh_kind::TYPE);
2636
976ca316 2637 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2638 sig_type->signature = cu_header.signature;
2639 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2640 sig_type->per_cu.is_debug_types = 1;
2641 sig_type->per_cu.section = section;
2642 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2643 sig_type->per_cu.v.quick
976ca316 2644 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2645 struct dwarf2_per_cu_quick_data);
2646
b0b6a987 2647 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2648 *slot = sig_type;
2649
976ca316 2650 per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2651 }
2652
976ca316 2653 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2654}
2655
9291a0cd
TT
2656/* Read the address map data from the mapped index, and use it to
2657 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2658
9291a0cd 2659static void
976ca316 2660create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2661 struct mapped_index *index)
9291a0cd 2662{
976ca316 2663 struct objfile *objfile = per_objfile->objfile;
08feed99 2664 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2665 const gdb_byte *iter, *end;
9291a0cd 2666 struct addrmap *mutable_map;
9291a0cd
TT
2667 CORE_ADDR baseaddr;
2668
8268c778
PA
2669 auto_obstack temp_obstack;
2670
9291a0cd
TT
2671 mutable_map = addrmap_create_mutable (&temp_obstack);
2672
f00a2de2
PA
2673 iter = index->address_table.data ();
2674 end = iter + index->address_table.size ();
9291a0cd 2675
b3b3bada 2676 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2677
2678 while (iter < end)
2679 {
2680 ULONGEST hi, lo, cu_index;
2681 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2682 iter += 8;
2683 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2684 iter += 8;
2685 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2686 iter += 4;
f652bce2 2687
24a55014 2688 if (lo > hi)
f652bce2 2689 {
b98664d3 2690 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2691 hex_string (lo), hex_string (hi));
24a55014 2692 continue;
f652bce2 2693 }
24a55014 2694
976ca316 2695 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2696 {
b98664d3 2697 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2698 (unsigned) cu_index);
24a55014 2699 continue;
f652bce2 2700 }
24a55014 2701
79748972
TT
2702 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2703 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2704 addrmap_set_empty (mutable_map, lo, hi - 1,
976ca316 2705 per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2706 }
2707
d320c2b5 2708 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2709 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2710}
2711
927aa2e7
JK
2712/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2713 populate the objfile's psymtabs_addrmap. */
2714
2715static void
976ca316 2716create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2717 struct dwarf2_section_info *section)
2718{
976ca316 2719 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2720 bfd *abfd = objfile->obfd;
08feed99 2721 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2722 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2723
2724 auto_obstack temp_obstack;
2725 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2726
2727 std::unordered_map<sect_offset,
2728 dwarf2_per_cu_data *,
2729 gdb::hash_enum<sect_offset>>
2730 debug_info_offset_to_per_cu;
976ca316 2731 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 2732 {
927aa2e7
JK
2733 const auto insertpair
2734 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2735 if (!insertpair.second)
2736 {
2737 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2738 "debug_info_offset %s, ignoring .debug_aranges."),
2739 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2740 return;
2741 }
2742 }
2743
96b79293 2744 section->read (objfile);
927aa2e7
JK
2745
2746 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2747
2748 const gdb_byte *addr = section->buffer;
2749
2750 while (addr < section->buffer + section->size)
2751 {
2752 const gdb_byte *const entry_addr = addr;
2753 unsigned int bytes_read;
2754
2755 const LONGEST entry_length = read_initial_length (abfd, addr,
2756 &bytes_read);
2757 addr += bytes_read;
2758
2759 const gdb_byte *const entry_end = addr + entry_length;
2760 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2761 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2762 if (addr + entry_length > section->buffer + section->size)
2763 {
47e3f474 2764 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2765 "length %s exceeds section length %s, "
2766 "ignoring .debug_aranges."),
47e3f474
TV
2767 objfile_name (objfile),
2768 plongest (entry_addr - section->buffer),
927aa2e7
JK
2769 plongest (bytes_read + entry_length),
2770 pulongest (section->size));
2771 return;
2772 }
2773
2774 /* The version number. */
2775 const uint16_t version = read_2_bytes (abfd, addr);
2776 addr += 2;
2777 if (version != 2)
2778 {
47e3f474 2779 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2780 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2781 objfile_name (objfile),
2782 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2783 return;
2784 }
2785
2786 const uint64_t debug_info_offset
2787 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2788 addr += offset_size;
2789 const auto per_cu_it
2790 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2791 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2792 {
47e3f474 2793 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2794 "debug_info_offset %s does not exists, "
2795 "ignoring .debug_aranges."),
47e3f474
TV
2796 objfile_name (objfile),
2797 plongest (entry_addr - section->buffer),
927aa2e7
JK
2798 pulongest (debug_info_offset));
2799 return;
2800 }
2801 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2802
2803 const uint8_t address_size = *addr++;
2804 if (address_size < 1 || address_size > 8)
2805 {
47e3f474 2806 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2807 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2808 objfile_name (objfile),
2809 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2810 return;
2811 }
2812
2813 const uint8_t segment_selector_size = *addr++;
2814 if (segment_selector_size != 0)
2815 {
47e3f474 2816 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2817 "segment_selector_size %u is not supported, "
2818 "ignoring .debug_aranges."),
47e3f474
TV
2819 objfile_name (objfile),
2820 plongest (entry_addr - section->buffer),
927aa2e7
JK
2821 segment_selector_size);
2822 return;
2823 }
2824
2825 /* Must pad to an alignment boundary that is twice the address
2826 size. It is undocumented by the DWARF standard but GCC does
2827 use it. */
2828 for (size_t padding = ((-(addr - section->buffer))
2829 & (2 * address_size - 1));
2830 padding > 0; padding--)
2831 if (*addr++ != 0)
2832 {
47e3f474 2833 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2834 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2835 objfile_name (objfile),
2836 plongest (entry_addr - section->buffer));
927aa2e7
JK
2837 return;
2838 }
2839
2840 for (;;)
2841 {
2842 if (addr + 2 * address_size > entry_end)
2843 {
47e3f474 2844 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2845 "address list is not properly terminated, "
2846 "ignoring .debug_aranges."),
47e3f474
TV
2847 objfile_name (objfile),
2848 plongest (entry_addr - section->buffer));
927aa2e7
JK
2849 return;
2850 }
2851 ULONGEST start = extract_unsigned_integer (addr, address_size,
2852 dwarf5_byte_order);
2853 addr += address_size;
2854 ULONGEST length = extract_unsigned_integer (addr, address_size,
2855 dwarf5_byte_order);
2856 addr += address_size;
2857 if (start == 0 && length == 0)
2858 break;
976ca316 2859 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
2860 {
2861 /* Symbol was eliminated due to a COMDAT group. */
2862 continue;
2863 }
2864 ULONGEST end = start + length;
79748972
TT
2865 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2866 - baseaddr);
2867 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2868 - baseaddr);
927aa2e7
JK
2869 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2870 }
2871 }
2872
d320c2b5 2873 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2874 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2875}
2876
9291a0cd
TT
2877/* Find a slot in the mapped index INDEX for the object named NAME.
2878 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2879 constant pool and return true. If NAME cannot be found, return
2880 false. */
2fdf6df6 2881
109483d9 2882static bool
9291a0cd
TT
2883find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2884 offset_type **vec_out)
2885{
0cf03b49 2886 offset_type hash;
9291a0cd 2887 offset_type slot, step;
559a7a62 2888 int (*cmp) (const char *, const char *);
9291a0cd 2889
791afaa2 2890 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2891 if (current_language->la_language == language_cplus
45280282
IB
2892 || current_language->la_language == language_fortran
2893 || current_language->la_language == language_d)
0cf03b49
JK
2894 {
2895 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2896 not contain any. */
a8719064 2897
72998fb3 2898 if (strchr (name, '(') != NULL)
0cf03b49 2899 {
109483d9 2900 without_params = cp_remove_params (name);
0cf03b49 2901
72998fb3 2902 if (without_params != NULL)
791afaa2 2903 name = without_params.get ();
0cf03b49
JK
2904 }
2905 }
2906
559a7a62 2907 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2908 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2909 simulate our NAME being searched is also lowercased. */
2910 hash = mapped_index_string_hash ((index->version == 4
2911 && case_sensitivity == case_sensitive_off
2912 ? 5 : index->version),
2913 name);
2914
f00a2de2
PA
2915 slot = hash & (index->symbol_table.size () - 1);
2916 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2917 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2918
2919 for (;;)
2920 {
9291a0cd 2921 const char *str;
f00a2de2
PA
2922
2923 const auto &bucket = index->symbol_table[slot];
2924 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2925 return false;
9291a0cd 2926
f00a2de2 2927 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2928 if (!cmp (name, str))
9291a0cd
TT
2929 {
2930 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2931 + MAYBE_SWAP (bucket.vec));
109483d9 2932 return true;
9291a0cd
TT
2933 }
2934
f00a2de2 2935 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2936 }
2937}
2938
4485a1c1
SM
2939/* A helper function that reads the .gdb_index from BUFFER and fills
2940 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2941 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2942 ok to use deprecated sections.
2943
2944 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2945 out parameters that are filled in with information about the CU and
2946 TU lists in the section.
2947
4485a1c1 2948 Returns true if all went well, false otherwise. */
2fdf6df6 2949
d33bc52e 2950static bool
3810f182 2951read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2952 bool deprecated_ok,
2953 gdb::array_view<const gdb_byte> buffer,
2954 struct mapped_index *map,
2955 const gdb_byte **cu_list,
2956 offset_type *cu_list_elements,
2957 const gdb_byte **types_list,
2958 offset_type *types_list_elements)
2959{
2960 const gdb_byte *addr = &buffer[0];
82430852 2961
9291a0cd 2962 /* Version check. */
4485a1c1 2963 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2964 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2965 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2966 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2967 indices. */
831adc1f 2968 if (version < 4)
481860b3
GB
2969 {
2970 static int warning_printed = 0;
2971 if (!warning_printed)
2972 {
2973 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2974 filename);
481860b3
GB
2975 warning_printed = 1;
2976 }
2977 return 0;
2978 }
2979 /* Index version 4 uses a different hash function than index version
2980 5 and later.
2981
2982 Versions earlier than 6 did not emit psymbols for inlined
2983 functions. Using these files will cause GDB not to be able to
2984 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2985 indices unless the user has done
2986 "set use-deprecated-index-sections on". */
2ec9a5e0 2987 if (version < 6 && !deprecated_ok)
481860b3
GB
2988 {
2989 static int warning_printed = 0;
2990 if (!warning_printed)
2991 {
e615022a
DE
2992 warning (_("\
2993Skipping deprecated .gdb_index section in %s.\n\
2994Do \"set use-deprecated-index-sections on\" before the file is read\n\
2995to use the section anyway."),
2ec9a5e0 2996 filename);
481860b3
GB
2997 warning_printed = 1;
2998 }
2999 return 0;
3000 }
796a7ff8 3001 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3002 of the TU (for symbols coming from TUs),
3003 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3004 Plus gold-generated indices can have duplicate entries for global symbols,
3005 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3006 These are just performance bugs, and we can't distinguish gdb-generated
3007 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3008
481860b3 3009 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3010 longer backward compatible. */
796a7ff8 3011 if (version > 8)
594e8718 3012 return 0;
9291a0cd 3013
559a7a62 3014 map->version = version;
9291a0cd 3015
4485a1c1 3016 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3017
4485a1c1 3018 int i = 0;
2ec9a5e0
TT
3019 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3020 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3021 / 8);
1fd400ff
TT
3022 ++i;
3023
2ec9a5e0
TT
3024 *types_list = addr + MAYBE_SWAP (metadata[i]);
3025 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3026 - MAYBE_SWAP (metadata[i]))
3027 / 8);
987d643c 3028 ++i;
1fd400ff 3029
f00a2de2
PA
3030 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3031 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3032 map->address_table
3033 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3034 ++i;
3035
f00a2de2
PA
3036 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3037 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3038 map->symbol_table
3039 = gdb::array_view<mapped_index::symbol_table_slot>
3040 ((mapped_index::symbol_table_slot *) symbol_table,
3041 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3042
f00a2de2 3043 ++i;
f9d83a0b 3044 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3045
2ec9a5e0
TT
3046 return 1;
3047}
3048
4485a1c1
SM
3049/* Callback types for dwarf2_read_gdb_index. */
3050
3051typedef gdb::function_view
5989a64e 3052 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3053 get_gdb_index_contents_ftype;
3054typedef gdb::function_view
3055 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3056 get_gdb_index_contents_dwz_ftype;
3057
927aa2e7 3058/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3059 elements of all the CUs and return 1. Otherwise, return 0. */
3060
3061static int
4485a1c1 3062dwarf2_read_gdb_index
976ca316 3063 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
3064 get_gdb_index_contents_ftype get_gdb_index_contents,
3065 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3066{
2ec9a5e0
TT
3067 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3068 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3069 struct dwz_file *dwz;
976ca316 3070 struct objfile *objfile = per_objfile->objfile;
2ec9a5e0 3071
4485a1c1 3072 gdb::array_view<const gdb_byte> main_index_contents
976ca316 3073 = get_gdb_index_contents (objfile, per_objfile->per_bfd);
4485a1c1
SM
3074
3075 if (main_index_contents.empty ())
3076 return 0;
3077
3063847f 3078 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3079 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3080 use_deprecated_index_sections,
3081 main_index_contents, map.get (), &cu_list,
3082 &cu_list_elements, &types_list,
3083 &types_list_elements))
2ec9a5e0
TT
3084 return 0;
3085
0fefef59 3086 /* Don't use the index if it's empty. */
3063847f 3087 if (map->symbol_table.empty ())
0fefef59
DE
3088 return 0;
3089
2ec9a5e0
TT
3090 /* If there is a .dwz file, read it so we can get its CU list as
3091 well. */
976ca316 3092 dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 3093 if (dwz != NULL)
2ec9a5e0 3094 {
2ec9a5e0
TT
3095 struct mapped_index dwz_map;
3096 const gdb_byte *dwz_types_ignore;
3097 offset_type dwz_types_elements_ignore;
3098
4485a1c1
SM
3099 gdb::array_view<const gdb_byte> dwz_index_content
3100 = get_gdb_index_contents_dwz (objfile, dwz);
3101
3102 if (dwz_index_content.empty ())
3103 return 0;
3104
3810f182 3105 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3106 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3107 &dwz_list, &dwz_list_elements,
3108 &dwz_types_ignore,
3109 &dwz_types_elements_ignore))
2ec9a5e0
TT
3110 {
3111 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3112 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3113 return 0;
3114 }
3115 }
3116
976ca316 3117 create_cus_from_index (per_objfile->per_bfd, cu_list, cu_list_elements,
12359b5e 3118 dwz_list, dwz_list_elements);
1fd400ff 3119
8b70b953
TT
3120 if (types_list_elements)
3121 {
8b70b953
TT
3122 /* We can only handle a single .debug_types when we have an
3123 index. */
976ca316 3124 if (per_objfile->per_bfd->types.size () != 1)
8b70b953
TT
3125 return 0;
3126
976ca316 3127 dwarf2_section_info *section = &per_objfile->per_bfd->types[0];
8b70b953 3128
976ca316 3129 create_signatured_type_table_from_index (per_objfile->per_bfd,
168c9250
SM
3130 section, types_list,
3131 types_list_elements);
8b70b953 3132 }
9291a0cd 3133
976ca316 3134 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 3135
976ca316
SM
3136 per_objfile->per_bfd->index_table = std::move (map);
3137 per_objfile->per_bfd->using_index = 1;
3138 per_objfile->per_bfd->quick_file_names_table =
3139 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
9291a0cd
TT
3140
3141 return 1;
3142}
3143
dee91e82 3144/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3145
dee91e82
DE
3146static void
3147dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3148 const gdb_byte *info_ptr,
3e225074 3149 struct die_info *comp_unit_die)
9291a0cd 3150{
dee91e82 3151 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3152 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 3153 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 3154 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3155 struct attribute *attr;
7b9f3c50
DE
3156 void **slot;
3157 struct quick_file_names *qfn;
9291a0cd 3158
0186c6a7
DE
3159 gdb_assert (! this_cu->is_debug_types);
3160
07261596
TT
3161 /* Our callers never want to match partial units -- instead they
3162 will match the enclosing full CU. */
3163 if (comp_unit_die->tag == DW_TAG_partial_unit)
3164 {
3165 this_cu->v.quick->no_file_data = 1;
3166 return;
3167 }
3168
0186c6a7 3169 lh_cu = this_cu;
7b9f3c50 3170 slot = NULL;
dee91e82 3171
fff8551c 3172 line_header_up lh;
9c541725 3173 sect_offset line_offset {};
fff8551c 3174
dee91e82 3175 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3176 if (attr != nullptr)
9291a0cd 3177 {
7b9f3c50
DE
3178 struct quick_file_names find_entry;
3179
9c541725 3180 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3181
3182 /* We may have already read in this line header (TU line header sharing).
3183 If we have we're done. */
094b34ac 3184 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3185 find_entry.hash.line_sect_off = line_offset;
976ca316 3186 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3187 &find_entry, INSERT);
3188 if (*slot != NULL)
3189 {
9a3c8263 3190 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3191 return;
7b9f3c50
DE
3192 }
3193
3019eac3 3194 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3195 }
3196 if (lh == NULL)
3197 {
094b34ac 3198 lh_cu->v.quick->no_file_data = 1;
dee91e82 3199 return;
9291a0cd
TT
3200 }
3201
976ca316 3202 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3203 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3204 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3205 gdb_assert (slot != NULL);
3206 *slot = qfn;
9291a0cd 3207
d721ba37 3208 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3209
aa391654
TT
3210 int offset = 0;
3211 if (strcmp (fnd.name, "<unknown>") != 0)
3212 ++offset;
3213
7ba99d21 3214 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3215 qfn->file_names =
976ca316 3216 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 3217 qfn->num_file_names);
aa391654
TT
3218 if (offset != 0)
3219 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3220 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3221 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3222 fnd.comp_dir).release ();
7b9f3c50 3223 qfn->real_names = NULL;
9291a0cd 3224
094b34ac 3225 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3226}
3227
3228/* A helper for the "quick" functions which attempts to read the line
3229 table for THIS_CU. */
3230
3231static struct quick_file_names *
ab432490
SM
3232dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3233 dwarf2_per_objfile *per_objfile)
dee91e82 3234{
0186c6a7
DE
3235 /* This should never be called for TUs. */
3236 gdb_assert (! this_cu->is_debug_types);
3237 /* Nor type unit groups. */
197400e8 3238 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3239
dee91e82
DE
3240 if (this_cu->v.quick->file_names != NULL)
3241 return this_cu->v.quick->file_names;
3242 /* If we know there is no line data, no point in looking again. */
3243 if (this_cu->v.quick->no_file_data)
3244 return NULL;
3245
ab432490 3246 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3247 if (!reader.dummy_p)
3e225074 3248 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3249
3250 if (this_cu->v.quick->no_file_data)
3251 return NULL;
3252 return this_cu->v.quick->file_names;
9291a0cd
TT
3253}
3254
3255/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3256 real path for a given file name from the line table. */
2fdf6df6 3257
9291a0cd 3258static const char *
976ca316 3259dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3260 struct quick_file_names *qfn, int index)
9291a0cd 3261{
7b9f3c50 3262 if (qfn->real_names == NULL)
976ca316 3263 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3264 qfn->num_file_names, const char *);
9291a0cd 3265
7b9f3c50 3266 if (qfn->real_names[index] == NULL)
14278e1f 3267 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3268
7b9f3c50 3269 return qfn->real_names[index];
9291a0cd
TT
3270}
3271
3272static struct symtab *
3273dw2_find_last_source_symtab (struct objfile *objfile)
3274{
976ca316
SM
3275 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3276 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3277 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3278
43f3e411
DE
3279 if (cust == NULL)
3280 return NULL;
ed2dc618 3281
43f3e411 3282 return compunit_primary_filetab (cust);
9291a0cd
TT
3283}
3284
7b9f3c50
DE
3285/* Traversal function for dw2_forget_cached_source_info. */
3286
3287static int
3288dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3289{
7b9f3c50 3290 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3291
7b9f3c50 3292 if (file_data->real_names)
9291a0cd 3293 {
7b9f3c50 3294 int i;
9291a0cd 3295
7b9f3c50 3296 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3297 {
7b9f3c50
DE
3298 xfree ((void*) file_data->real_names[i]);
3299 file_data->real_names[i] = NULL;
9291a0cd
TT
3300 }
3301 }
7b9f3c50
DE
3302
3303 return 1;
3304}
3305
3306static void
3307dw2_forget_cached_source_info (struct objfile *objfile)
3308{
976ca316 3309 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3310
976ca316 3311 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3312 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3313}
3314
f8eba3c6
TT
3315/* Helper function for dw2_map_symtabs_matching_filename that expands
3316 the symtabs and calls the iterator. */
3317
3318static int
3319dw2_map_expand_apply (struct objfile *objfile,
3320 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3321 const char *name, const char *real_path,
14bc53a8 3322 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3323{
43f3e411 3324 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3325
3326 /* Don't visit already-expanded CUs. */
af758d11
SM
3327 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3328 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3329 return 0;
3330
3331 /* This may expand more than one symtab, and we want to iterate over
3332 all of them. */
97a1449a 3333 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3334
14bc53a8
PA
3335 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3336 last_made, callback);
f8eba3c6
TT
3337}
3338
3339/* Implementation of the map_symtabs_matching_filename method. */
3340
14bc53a8
PA
3341static bool
3342dw2_map_symtabs_matching_filename
3343 (struct objfile *objfile, const char *name, const char *real_path,
3344 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3345{
c011a4f4 3346 const char *name_basename = lbasename (name);
976ca316 3347 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 3348
848e3e78
DE
3349 /* The rule is CUs specify all the files, including those used by
3350 any TU, so there's no need to scan TUs here. */
f4dc4d17 3351
976ca316 3352 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3353 {
3d7bb9d9 3354 /* We only need to look at symtabs not already expanded. */
976ca316 3355 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3356 continue;
3357
976ca316 3358 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3359 if (file_data == NULL)
9291a0cd
TT
3360 continue;
3361
b76e467d 3362 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3363 {
7b9f3c50 3364 const char *this_name = file_data->file_names[j];
da235a7c 3365 const char *this_real_name;
9291a0cd 3366
af529f8f 3367 if (compare_filenames_for_search (this_name, name))
9291a0cd 3368 {
f5b95b50 3369 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3370 callback))
3371 return true;
288e77a7 3372 continue;
4aac40c8 3373 }
9291a0cd 3374
c011a4f4
DE
3375 /* Before we invoke realpath, which can get expensive when many
3376 files are involved, do a quick comparison of the basenames. */
3377 if (! basenames_may_differ
3378 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3379 continue;
3380
976ca316 3381 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
da235a7c 3382 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3383 {
da235a7c 3384 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3385 callback))
3386 return true;
288e77a7 3387 continue;
da235a7c 3388 }
9291a0cd 3389
da235a7c
JK
3390 if (real_path != NULL)
3391 {
af529f8f
JK
3392 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3393 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3394 if (this_real_name != NULL
af529f8f 3395 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3396 {
f5b95b50 3397 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3398 callback))
3399 return true;
288e77a7 3400 continue;
9291a0cd
TT
3401 }
3402 }
3403 }
3404 }
3405
14bc53a8 3406 return false;
9291a0cd
TT
3407}
3408
da51c347
DE
3409/* Struct used to manage iterating over all CUs looking for a symbol. */
3410
3411struct dw2_symtab_iterator
9291a0cd 3412{
ed2dc618 3413 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3414 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3415 /* If set, only look for symbols that match that block. Valid values are
3416 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3417 gdb::optional<block_enum> block_index;
da51c347
DE
3418 /* The kind of symbol we're looking for. */
3419 domain_enum domain;
3420 /* The list of CUs from the index entry of the symbol,
3421 or NULL if not found. */
3422 offset_type *vec;
3423 /* The next element in VEC to look at. */
3424 int next;
3425 /* The number of elements in VEC, or zero if there is no match. */
3426 int length;
8943b874
DE
3427 /* Have we seen a global version of the symbol?
3428 If so we can ignore all further global instances.
3429 This is to work around gold/15646, inefficient gold-generated
3430 indices. */
3431 int global_seen;
da51c347 3432};
9291a0cd 3433
2b79f376 3434/* Initialize the index symtab iterator ITER. */
2fdf6df6 3435
9291a0cd 3436static void
da51c347 3437dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
976ca316 3438 dwarf2_per_objfile *per_objfile,
c7f839cb 3439 gdb::optional<block_enum> block_index,
da51c347
DE
3440 domain_enum domain,
3441 const char *name)
3442{
976ca316 3443 iter->per_objfile = per_objfile;
da51c347
DE
3444 iter->block_index = block_index;
3445 iter->domain = domain;
3446 iter->next = 0;
8943b874 3447 iter->global_seen = 0;
da51c347 3448
976ca316 3449 mapped_index *index = per_objfile->per_bfd->index_table.get ();
ed2dc618
SM
3450
3451 /* index is NULL if OBJF_READNOW. */
3452 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3453 iter->length = MAYBE_SWAP (*iter->vec);
3454 else
3455 {
3456 iter->vec = NULL;
3457 iter->length = 0;
3458 }
3459}
3460
3461/* Return the next matching CU or NULL if there are no more. */
3462
3463static struct dwarf2_per_cu_data *
3464dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3465{
976ca316 3466 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3467
da51c347
DE
3468 for ( ; iter->next < iter->length; ++iter->next)
3469 {
3470 offset_type cu_index_and_attrs =
3471 MAYBE_SWAP (iter->vec[iter->next + 1]);
3472 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3473 gdb_index_symbol_kind symbol_kind =
3474 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3475 /* Only check the symbol attributes if they're present.
3476 Indices prior to version 7 don't record them,
3477 and indices >= 7 may elide them for certain symbols
3478 (gold does this). */
3479 int attrs_valid =
976ca316 3480 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3481 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3482
3190f0c6 3483 /* Don't crash on bad data. */
976ca316
SM
3484 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3485 + per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3486 {
b98664d3 3487 complaint (_(".gdb_index entry has bad CU index"
976ca316 3488 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3489 continue;
3490 }
3491
976ca316 3492 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3493
da51c347 3494 /* Skip if already read in. */
976ca316 3495 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3496 continue;
3497
8943b874
DE
3498 /* Check static vs global. */
3499 if (attrs_valid)
3500 {
2b79f376
SM
3501 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3502
3503 if (iter->block_index.has_value ())
3504 {
3505 bool want_static = *iter->block_index == STATIC_BLOCK;
3506
3507 if (is_static != want_static)
3508 continue;
3509 }
3510
8943b874 3511 /* Work around gold/15646. */
f030440d
TV
3512 if (!is_static
3513 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3514 {
3515 if (iter->global_seen)
3516 continue;
3517
3518 iter->global_seen = 1;
3519 }
8943b874 3520 }
da51c347
DE
3521
3522 /* Only check the symbol's kind if it has one. */
3523 if (attrs_valid)
3524 {
3525 switch (iter->domain)
3526 {
3527 case VAR_DOMAIN:
3528 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3529 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3530 /* Some types are also in VAR_DOMAIN. */
3531 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3532 continue;
3533 break;
3534 case STRUCT_DOMAIN:
3535 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3536 continue;
3537 break;
3538 case LABEL_DOMAIN:
3539 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3540 continue;
3541 break;
59c35742
AB
3542 case MODULE_DOMAIN:
3543 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3544 continue;
3545 break;
da51c347
DE
3546 default:
3547 break;
3548 }
3549 }
3550
3551 ++iter->next;
3552 return per_cu;
3553 }
3554
3555 return NULL;
3556}
3557
43f3e411 3558static struct compunit_symtab *
c7f839cb 3559dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3560 const char *name, domain_enum domain)
9291a0cd 3561{
43f3e411 3562 struct compunit_symtab *stab_best = NULL;
976ca316 3563 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 3564
b5ec771e
PA
3565 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3566
ed2dc618
SM
3567 struct dw2_symtab_iterator iter;
3568 struct dwarf2_per_cu_data *per_cu;
da51c347 3569
976ca316 3570 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
9291a0cd 3571
ed2dc618
SM
3572 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3573 {
3574 struct symbol *sym, *with_opaque = NULL;
97a1449a 3575 struct compunit_symtab *stab
976ca316 3576 = dw2_instantiate_symtab (per_cu, per_objfile, false);
ed2dc618 3577 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3578 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3579
ed2dc618
SM
3580 sym = block_find_symbol (block, name, domain,
3581 block_find_non_opaque_type_preferred,
3582 &with_opaque);
b2e2f908 3583
ed2dc618
SM
3584 /* Some caution must be observed with overloaded functions
3585 and methods, since the index will not contain any overload
3586 information (but NAME might contain it). */
da51c347 3587
ed2dc618
SM
3588 if (sym != NULL
3589 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3590 return stab;
3591 if (with_opaque != NULL
3592 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3593 stab_best = stab;
da51c347 3594
ed2dc618 3595 /* Keep looking through other CUs. */
9291a0cd 3596 }
9291a0cd 3597
da51c347 3598 return stab_best;
9291a0cd
TT
3599}
3600
3601static void
3602dw2_print_stats (struct objfile *objfile)
3603{
976ca316
SM
3604 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3605 int total = (per_objfile->per_bfd->all_comp_units.size ()
3606 + per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3607 int count = 0;
9291a0cd 3608
ed2dc618 3609 for (int i = 0; i < total; ++i)
9291a0cd 3610 {
976ca316 3611 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3612
976ca316 3613 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3614 ++count;
3615 }
e4a48d9d 3616 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3617 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3618}
3619
779bd270
DE
3620/* This dumps minimal information about the index.
3621 It is called via "mt print objfiles".
3622 One use is to verify .gdb_index has been loaded by the
3623 gdb.dwarf2/gdb-index.exp testcase. */
3624
9291a0cd
TT
3625static void
3626dw2_dump (struct objfile *objfile)
3627{
976ca316 3628 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3629
976ca316 3630 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3631 printf_filtered (".gdb_index:");
976ca316 3632 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3633 {
3634 printf_filtered (" version %d\n",
976ca316 3635 per_objfile->per_bfd->index_table->version);
779bd270
DE
3636 }
3637 else
3638 printf_filtered (" faked for \"readnow\"\n");
3639 printf_filtered ("\n");
9291a0cd
TT
3640}
3641
9291a0cd
TT
3642static void
3643dw2_expand_symtabs_for_function (struct objfile *objfile,
3644 const char *func_name)
3645{
976ca316 3646 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
da51c347 3647
ed2dc618
SM
3648 struct dw2_symtab_iterator iter;
3649 struct dwarf2_per_cu_data *per_cu;
da51c347 3650
976ca316 3651 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3652
ed2dc618 3653 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3654 dw2_instantiate_symtab (per_cu, per_objfile, false);
da51c347 3655
9291a0cd
TT
3656}
3657
3658static void
3659dw2_expand_all_symtabs (struct objfile *objfile)
3660{
976ca316
SM
3661 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3662 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3663 + per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3664
ed2dc618 3665 for (int i = 0; i < total_units; ++i)
9291a0cd 3666 {
976ca316 3667 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3668
58f0c718
TT
3669 /* We don't want to directly expand a partial CU, because if we
3670 read it with the wrong language, then assertion failures can
3671 be triggered later on. See PR symtab/23010. So, tell
3672 dw2_instantiate_symtab to skip partial CUs -- any important
3673 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3674 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3675 }
3676}
3677
3678static void
652a8996
JK
3679dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3680 const char *fullname)
9291a0cd 3681{
976ca316 3682 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3683
3684 /* We don't need to consider type units here.
3685 This is only called for examining code, e.g. expand_line_sal.
3686 There can be an order of magnitude (or more) more type units
3687 than comp units, and we avoid them if we can. */
3688
976ca316 3689 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3690 {
3d7bb9d9 3691 /* We only need to look at symtabs not already expanded. */
976ca316 3692 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3693 continue;
3694
976ca316 3695 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3696 if (file_data == NULL)
9291a0cd
TT
3697 continue;
3698
b76e467d 3699 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3700 {
652a8996
JK
3701 const char *this_fullname = file_data->file_names[j];
3702
3703 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3704 {
976ca316 3705 dw2_instantiate_symtab (per_cu, per_objfile, false);
9291a0cd
TT
3706 break;
3707 }
3708 }
3709 }
3710}
3711
9a0bacfb
TV
3712static void
3713dw2_expand_symtabs_matching_symbol
3714 (mapped_index_base &index,
3715 const lookup_name_info &lookup_name_in,
3716 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3717 enum search_domain kind,
fcf23d5b
SM
3718 gdb::function_view<bool (offset_type)> match_callback,
3719 dwarf2_per_objfile *per_objfile);
9a0bacfb
TV
3720
3721static void
3722dw2_expand_symtabs_matching_one
97a1449a
SM
3723 (dwarf2_per_cu_data *per_cu,
3724 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3725 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3726 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3727
9291a0cd 3728static void
199b4314
TT
3729dw2_map_matching_symbols
3730 (struct objfile *objfile,
b054970d 3731 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3732 int global,
3733 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3734 symbol_compare_ftype *ordered_compare)
9291a0cd 3735{
1aa98955 3736 /* Used for Ada. */
976ca316 3737 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3738
9a0bacfb
TV
3739 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3740
976ca316 3741 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955
TV
3742 {
3743 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3744 here though if the current language is Ada for a non-Ada objfile
9a0bacfb 3745 using GNU index. */
976ca316 3746 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3747
9a0bacfb
TV
3748 const char *match_name = name.ada ().lookup_name ().c_str ();
3749 auto matcher = [&] (const char *symname)
3750 {
3751 if (ordered_compare == nullptr)
3752 return true;
3753 return ordered_compare (symname, match_name) == 0;
3754 };
3755
3756 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3757 [&] (offset_type namei)
3758 {
3759 struct dw2_symtab_iterator iter;
3760 struct dwarf2_per_cu_data *per_cu;
3761
976ca316 3762 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
9a0bacfb
TV
3763 match_name);
3764 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3765 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3766 nullptr);
9a0bacfb 3767 return true;
976ca316 3768 }, per_objfile);
9a0bacfb
TV
3769 }
3770 else
3771 {
3772 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3773 proceed assuming all symtabs have been read in. */
3774 }
1aa98955
TV
3775
3776 for (compunit_symtab *cust : objfile->compunits ())
3777 {
3778 const struct block *block;
3779
3780 if (cust == NULL)
3781 continue;
3782 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3783 if (!iterate_over_symbols_terminated (block, name,
3784 domain, callback))
3785 return;
3786 }
9291a0cd
TT
3787}
3788
e1ef7d7a
PA
3789/* Starting from a search name, return the string that finds the upper
3790 bound of all strings that start with SEARCH_NAME in a sorted name
3791 list. Returns the empty string to indicate that the upper bound is
3792 the end of the list. */
3793
3794static std::string
3795make_sort_after_prefix_name (const char *search_name)
3796{
3797 /* When looking to complete "func", we find the upper bound of all
3798 symbols that start with "func" by looking for where we'd insert
3799 the closest string that would follow "func" in lexicographical
3800 order. Usually, that's "func"-with-last-character-incremented,
3801 i.e. "fund". Mind non-ASCII characters, though. Usually those
3802 will be UTF-8 multi-byte sequences, but we can't be certain.
3803 Especially mind the 0xff character, which is a valid character in
3804 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3805 rule out compilers allowing it in identifiers. Note that
3806 conveniently, strcmp/strcasecmp are specified to compare
3807 characters interpreted as unsigned char. So what we do is treat
3808 the whole string as a base 256 number composed of a sequence of
3809 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3810 to 0, and carries 1 to the following more-significant position.
3811 If the very first character in SEARCH_NAME ends up incremented
3812 and carries/overflows, then the upper bound is the end of the
3813 list. The string after the empty string is also the empty
3814 string.
3815
3816 Some examples of this operation:
3817
3818 SEARCH_NAME => "+1" RESULT
3819
3820 "abc" => "abd"
3821 "ab\xff" => "ac"
3822 "\xff" "a" "\xff" => "\xff" "b"
3823 "\xff" => ""
3824 "\xff\xff" => ""
3825 "" => ""
3826
3827 Then, with these symbols for example:
3828
3829 func
3830 func1
3831 fund
3832
3833 completing "func" looks for symbols between "func" and
3834 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3835 which finds "func" and "func1", but not "fund".
3836
3837 And with:
3838
3839 funcÿ (Latin1 'ÿ' [0xff])
3840 funcÿ1
3841 fund
3842
3843 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3844 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3845
3846 And with:
3847
3848 ÿÿ (Latin1 'ÿ' [0xff])
3849 ÿÿ1
3850
3851 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3852 the end of the list.
3853 */
3854 std::string after = search_name;
3855 while (!after.empty () && (unsigned char) after.back () == 0xff)
3856 after.pop_back ();
3857 if (!after.empty ())
3858 after.back () = (unsigned char) after.back () + 1;
3859 return after;
3860}
3861
5c58de74 3862/* See declaration. */
61d96d7e 3863
5c58de74
PA
3864std::pair<std::vector<name_component>::const_iterator,
3865 std::vector<name_component>::const_iterator>
44ed8f3e 3866mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3867 (const lookup_name_info &lookup_name_without_params, language lang,
3868 dwarf2_per_objfile *per_objfile) const
3f563c84 3869{
5c58de74
PA
3870 auto *name_cmp
3871 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3872
3b00ef10 3873 const char *lang_name
e0802d59 3874 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3875
3f563c84
PA
3876 /* Comparison function object for lower_bound that matches against a
3877 given symbol name. */
3878 auto lookup_compare_lower = [&] (const name_component &elem,
3879 const char *name)
3880 {
fcf23d5b 3881 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3882 const char *elem_name = elem_qualified + elem.name_offset;
3883 return name_cmp (elem_name, name) < 0;
3884 };
3885
3886 /* Comparison function object for upper_bound that matches against a
3887 given symbol name. */
3888 auto lookup_compare_upper = [&] (const char *name,
3889 const name_component &elem)
3890 {
fcf23d5b 3891 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3892 const char *elem_name = elem_qualified + elem.name_offset;
3893 return name_cmp (name, elem_name) < 0;
3894 };
3895
5c58de74
PA
3896 auto begin = this->name_components.begin ();
3897 auto end = this->name_components.end ();
3f563c84
PA
3898
3899 /* Find the lower bound. */
3900 auto lower = [&] ()
3901 {
3b00ef10 3902 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3903 return begin;
3904 else
3b00ef10 3905 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3906 } ();
3907
3908 /* Find the upper bound. */
3909 auto upper = [&] ()
3910 {
5c58de74 3911 if (lookup_name_without_params.completion_mode ())
3f563c84 3912 {
e1ef7d7a
PA
3913 /* In completion mode, we want UPPER to point past all
3914 symbols names that have the same prefix. I.e., with
3915 these symbols, and completing "func":
3916
3917 function << lower bound
3918 function1
3919 other_function << upper bound
3920
3921 We find the upper bound by looking for the insertion
3922 point of "func"-with-last-character-incremented,
3923 i.e. "fund". */
3b00ef10 3924 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3925 if (after.empty ())
3f563c84 3926 return end;
e6b2f5ef
PA
3927 return std::lower_bound (lower, end, after.c_str (),
3928 lookup_compare_lower);
3f563c84
PA
3929 }
3930 else
3b00ef10 3931 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3932 } ();
3933
5c58de74
PA
3934 return {lower, upper};
3935}
3936
3937/* See declaration. */
3938
3939void
fcf23d5b 3940mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
3941{
3942 if (!this->name_components.empty ())
3943 return;
3944
3945 this->name_components_casing = case_sensitivity;
3946 auto *name_cmp
3947 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3948
3949 /* The code below only knows how to break apart components of C++
3950 symbol names (and other languages that use '::' as
3b00ef10 3951 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3952 auto count = this->symbol_name_count ();
3953 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3954 {
44ed8f3e 3955 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3956 continue;
3957
fcf23d5b 3958 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
3959
3960 /* Add each name component to the name component table. */
3961 unsigned int previous_len = 0;
3b00ef10
TT
3962
3963 if (strstr (name, "::") != nullptr)
3964 {
3965 for (unsigned int current_len = cp_find_first_component (name);
3966 name[current_len] != '\0';
3967 current_len += cp_find_first_component (name + current_len))
3968 {
3969 gdb_assert (name[current_len] == ':');
3970 this->name_components.push_back ({previous_len, idx});
3971 /* Skip the '::'. */
3972 current_len += 2;
3973 previous_len = current_len;
3974 }
3975 }
3976 else
5c58de74 3977 {
3b00ef10
TT
3978 /* Handle the Ada encoded (aka mangled) form here. */
3979 for (const char *iter = strstr (name, "__");
3980 iter != nullptr;
3981 iter = strstr (iter, "__"))
3982 {
3983 this->name_components.push_back ({previous_len, idx});
3984 iter += 2;
3985 previous_len = iter - name;
3986 }
5c58de74 3987 }
3b00ef10 3988
5c58de74
PA
3989 this->name_components.push_back ({previous_len, idx});
3990 }
3991
3992 /* Sort name_components elements by name. */
3993 auto name_comp_compare = [&] (const name_component &left,
3994 const name_component &right)
3995 {
fcf23d5b
SM
3996 const char *left_qualified
3997 = this->symbol_name_at (left.idx, per_objfile);
3998 const char *right_qualified
3999 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
4000
4001 const char *left_name = left_qualified + left.name_offset;
4002 const char *right_name = right_qualified + right.name_offset;
4003
4004 return name_cmp (left_name, right_name) < 0;
4005 };
4006
4007 std::sort (this->name_components.begin (),
4008 this->name_components.end (),
4009 name_comp_compare);
4010}
4011
4012/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4013 mapped_index_base instead of the containing objfile. This is split
4014 to a separate function in order to be able to unit test the
4015 name_components matching using a mock mapped_index_base. For each
5c58de74 4016 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4017 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4018
4019static void
4020dw2_expand_symtabs_matching_symbol
44ed8f3e 4021 (mapped_index_base &index,
5c58de74
PA
4022 const lookup_name_info &lookup_name_in,
4023 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4024 enum search_domain kind,
fcf23d5b
SM
4025 gdb::function_view<bool (offset_type)> match_callback,
4026 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4027{
4028 lookup_name_info lookup_name_without_params
4029 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4030
4031 /* Build the symbol name component sorted vector, if we haven't
4032 yet. */
fcf23d5b 4033 index.build_name_components (per_objfile);
5c58de74 4034
3f563c84
PA
4035 /* The same symbol may appear more than once in the range though.
4036 E.g., if we're looking for symbols that complete "w", and we have
4037 a symbol named "w1::w2", we'll find the two name components for
4038 that same symbol in the range. To be sure we only call the
4039 callback once per symbol, we first collect the symbol name
4040 indexes that matched in a temporary vector and ignore
4041 duplicates. */
4042 std::vector<offset_type> matches;
3f563c84 4043
3b00ef10
TT
4044 struct name_and_matcher
4045 {
4046 symbol_name_matcher_ftype *matcher;
ecc6c606 4047 const char *name;
3b00ef10
TT
4048
4049 bool operator== (const name_and_matcher &other) const
3f563c84 4050 {
ecc6c606 4051 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4052 }
4053 };
4054
4055 /* A vector holding all the different symbol name matchers, for all
4056 languages. */
4057 std::vector<name_and_matcher> matchers;
4058
4059 for (int i = 0; i < nr_languages; i++)
4060 {
4061 enum language lang_e = (enum language) i;
4062
4063 const language_defn *lang = language_def (lang_e);
4064 symbol_name_matcher_ftype *name_matcher
4065 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4066
3b00ef10
TT
4067 name_and_matcher key {
4068 name_matcher,
4069 lookup_name_without_params.language_lookup_name (lang_e)
4070 };
4071
4072 /* Don't insert the same comparison routine more than once.
4073 Note that we do this linear walk. This is not a problem in
4074 practice because the number of supported languages is
4075 low. */
4076 if (std::find (matchers.begin (), matchers.end (), key)
4077 != matchers.end ())
9291a0cd 4078 continue;
3b00ef10
TT
4079 matchers.push_back (std::move (key));
4080
4081 auto bounds
4082 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 4083 lang_e, per_objfile);
3b00ef10
TT
4084
4085 /* Now for each symbol name in range, check to see if we have a name
4086 match, and if so, call the MATCH_CALLBACK callback. */
4087
4088 for (; bounds.first != bounds.second; ++bounds.first)
4089 {
fcf23d5b
SM
4090 const char *qualified
4091 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
4092
4093 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4094 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4095 continue;
9291a0cd 4096
3b00ef10
TT
4097 matches.push_back (bounds.first->idx);
4098 }
3f563c84
PA
4099 }
4100
4101 std::sort (matches.begin (), matches.end ());
4102
4103 /* Finally call the callback, once per match. */
4104 ULONGEST prev = -1;
4105 for (offset_type idx : matches)
4106 {
4107 if (prev != idx)
4108 {
3b00ef10
TT
4109 if (!match_callback (idx))
4110 break;
3f563c84
PA
4111 prev = idx;
4112 }
4113 }
4114
4115 /* Above we use a type wider than idx's for 'prev', since 0 and
4116 (offset_type)-1 are both possible values. */
4117 static_assert (sizeof (prev) > sizeof (offset_type), "");
4118}
4119
c62446b1
PA
4120#if GDB_SELF_TEST
4121
4122namespace selftests { namespace dw2_expand_symtabs_matching {
4123
a3c5fafd
PA
4124/* A mock .gdb_index/.debug_names-like name index table, enough to
4125 exercise dw2_expand_symtabs_matching_symbol, which works with the
4126 mapped_index_base interface. Builds an index from the symbol list
4127 passed as parameter to the constructor. */
4128class mock_mapped_index : public mapped_index_base
c62446b1
PA
4129{
4130public:
a3c5fafd
PA
4131 mock_mapped_index (gdb::array_view<const char *> symbols)
4132 : m_symbol_table (symbols)
c62446b1
PA
4133 {}
4134
a3c5fafd 4135 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4136
a3c5fafd 4137 /* Return the number of names in the symbol table. */
632e107b 4138 size_t symbol_name_count () const override
c62446b1 4139 {
a3c5fafd 4140 return m_symbol_table.size ();
c62446b1
PA
4141 }
4142
a3c5fafd 4143 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
4144 const char *symbol_name_at
4145 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
4146 {
4147 return m_symbol_table[idx];
4148 }
c62446b1 4149
a3c5fafd
PA
4150private:
4151 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4152};
4153
4154/* Convenience function that converts a NULL pointer to a "<null>"
4155 string, to pass to print routines. */
4156
4157static const char *
4158string_or_null (const char *str)
4159{
4160 return str != NULL ? str : "<null>";
4161}
4162
4163/* Check if a lookup_name_info built from
4164 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4165 index. EXPECTED_LIST is the list of expected matches, in expected
4166 matching order. If no match expected, then an empty list is
4167 specified. Returns true on success. On failure prints a warning
4168 indicating the file:line that failed, and returns false. */
4169
4170static bool
4171check_match (const char *file, int line,
4172 mock_mapped_index &mock_index,
4173 const char *name, symbol_name_match_type match_type,
4174 bool completion_mode,
fcf23d5b
SM
4175 std::initializer_list<const char *> expected_list,
4176 dwarf2_per_objfile *per_objfile)
c62446b1
PA
4177{
4178 lookup_name_info lookup_name (name, match_type, completion_mode);
4179
4180 bool matched = true;
4181
4182 auto mismatch = [&] (const char *expected_str,
4183 const char *got)
4184 {
4185 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4186 "expected=\"%s\", got=\"%s\"\n"),
4187 file, line,
4188 (match_type == symbol_name_match_type::FULL
4189 ? "FULL" : "WILD"),
4190 name, string_or_null (expected_str), string_or_null (got));
4191 matched = false;
4192 };
4193
4194 auto expected_it = expected_list.begin ();
4195 auto expected_end = expected_list.end ();
4196
a3c5fafd 4197 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4198 NULL, ALL_DOMAIN,
4199 [&] (offset_type idx)
4200 {
fcf23d5b 4201 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
4202 const char *expected_str
4203 = expected_it == expected_end ? NULL : *expected_it++;
4204
4205 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4206 mismatch (expected_str, matched_name);
3b00ef10 4207 return true;
fcf23d5b 4208 }, per_objfile);
c62446b1
PA
4209
4210 const char *expected_str
4211 = expected_it == expected_end ? NULL : *expected_it++;
4212 if (expected_str != NULL)
4213 mismatch (expected_str, NULL);
4214
4215 return matched;
4216}
4217
4218/* The symbols added to the mock mapped_index for testing (in
4219 canonical form). */
4220static const char *test_symbols[] = {
4221 "function",
4222 "std::bar",
4223 "std::zfunction",
4224 "std::zfunction2",
4225 "w1::w2",
4226 "ns::foo<char*>",
4227 "ns::foo<int>",
4228 "ns::foo<long>",
a20714ff
PA
4229 "ns2::tmpl<int>::foo2",
4230 "(anonymous namespace)::A::B::C",
c62446b1 4231
e1ef7d7a
PA
4232 /* These are used to check that the increment-last-char in the
4233 matching algorithm for completion doesn't match "t1_fund" when
4234 completing "t1_func". */
4235 "t1_func",
4236 "t1_func1",
4237 "t1_fund",
4238 "t1_fund1",
4239
4240 /* A UTF-8 name with multi-byte sequences to make sure that
4241 cp-name-parser understands this as a single identifier ("função"
4242 is "function" in PT). */
4243 u8"u8função",
4244
4245 /* \377 (0xff) is Latin1 'ÿ'. */
4246 "yfunc\377",
4247
4248 /* \377 (0xff) is Latin1 'ÿ'. */
4249 "\377",
4250 "\377\377123",
4251
c62446b1
PA
4252 /* A name with all sorts of complications. Starts with "z" to make
4253 it easier for the completion tests below. */
4254#define Z_SYM_NAME \
4255 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4256 "::tuple<(anonymous namespace)::ui*, " \
4257 "std::default_delete<(anonymous namespace)::ui>, void>"
4258
4259 Z_SYM_NAME
4260};
4261
a3c5fafd
PA
4262/* Returns true if the mapped_index_base::find_name_component_bounds
4263 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4264 in completion mode. */
5c58de74
PA
4265
4266static bool
a3c5fafd 4267check_find_bounds_finds (mapped_index_base &index,
5c58de74 4268 const char *search_name,
fcf23d5b
SM
4269 gdb::array_view<const char *> expected_syms,
4270 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4271{
4272 lookup_name_info lookup_name (search_name,
4273 symbol_name_match_type::FULL, true);
4274
3b00ef10 4275 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4276 language_cplus,
4277 per_objfile);
5c58de74
PA
4278
4279 size_t distance = std::distance (bounds.first, bounds.second);
4280 if (distance != expected_syms.size ())
4281 return false;
4282
4283 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4284 {
4285 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4286 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4287 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4288 return false;
4289 }
4290
4291 return true;
4292}
4293
4294/* Test the lower-level mapped_index::find_name_component_bounds
4295 method. */
4296
c62446b1 4297static void
5c58de74
PA
4298test_mapped_index_find_name_component_bounds ()
4299{
4300 mock_mapped_index mock_index (test_symbols);
4301
fcf23d5b 4302 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4303
4304 /* Test the lower-level mapped_index::find_name_component_bounds
4305 method in completion mode. */
4306 {
4307 static const char *expected_syms[] = {
4308 "t1_func",
4309 "t1_func1",
5c58de74
PA
4310 };
4311
fcf23d5b
SM
4312 SELF_CHECK (check_find_bounds_finds
4313 (mock_index, "t1_func", expected_syms,
4314 NULL /* per_objfile */));
5c58de74
PA
4315 }
4316
4317 /* Check that the increment-last-char in the name matching algorithm
4318 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4319 {
4320 static const char *expected_syms1[] = {
4321 "\377",
4322 "\377\377123",
4323 };
fcf23d5b
SM
4324 SELF_CHECK (check_find_bounds_finds
4325 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4326
4327 static const char *expected_syms2[] = {
4328 "\377\377123",
4329 };
fcf23d5b
SM
4330 SELF_CHECK (check_find_bounds_finds
4331 (mock_index, "\377\377", expected_syms2,
4332 NULL /* per_objfile */));
5c58de74
PA
4333 }
4334}
4335
4336/* Test dw2_expand_symtabs_matching_symbol. */
4337
4338static void
4339test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4340{
4341 mock_mapped_index mock_index (test_symbols);
4342
4343 /* We let all tests run until the end even if some fails, for debug
4344 convenience. */
4345 bool any_mismatch = false;
4346
4347 /* Create the expected symbols list (an initializer_list). Needed
4348 because lists have commas, and we need to pass them to CHECK,
4349 which is a macro. */
4350#define EXPECT(...) { __VA_ARGS__ }
4351
4352 /* Wrapper for check_match that passes down the current
4353 __FILE__/__LINE__. */
4354#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4355 any_mismatch |= !check_match (__FILE__, __LINE__, \
4356 mock_index, \
4357 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4358 EXPECTED_LIST, NULL)
c62446b1
PA
4359
4360 /* Identity checks. */
4361 for (const char *sym : test_symbols)
4362 {
4363 /* Should be able to match all existing symbols. */
4364 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4365 EXPECT (sym));
4366
4367 /* Should be able to match all existing symbols with
4368 parameters. */
4369 std::string with_params = std::string (sym) + "(int)";
4370 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4371 EXPECT (sym));
4372
4373 /* Should be able to match all existing symbols with
4374 parameters and qualifiers. */
4375 with_params = std::string (sym) + " ( int ) const";
4376 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4377 EXPECT (sym));
4378
4379 /* This should really find sym, but cp-name-parser.y doesn't
4380 know about lvalue/rvalue qualifiers yet. */
4381 with_params = std::string (sym) + " ( int ) &&";
4382 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4383 {});
4384 }
4385
e1ef7d7a
PA
4386 /* Check that the name matching algorithm for completion doesn't get
4387 confused with Latin1 'ÿ' / 0xff. */
4388 {
4389 static const char str[] = "\377";
4390 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4391 EXPECT ("\377", "\377\377123"));
4392 }
4393
4394 /* Check that the increment-last-char in the matching algorithm for
4395 completion doesn't match "t1_fund" when completing "t1_func". */
4396 {
4397 static const char str[] = "t1_func";
4398 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4399 EXPECT ("t1_func", "t1_func1"));
4400 }
4401
c62446b1
PA
4402 /* Check that completion mode works at each prefix of the expected
4403 symbol name. */
4404 {
4405 static const char str[] = "function(int)";
4406 size_t len = strlen (str);
4407 std::string lookup;
4408
4409 for (size_t i = 1; i < len; i++)
4410 {
4411 lookup.assign (str, i);
4412 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4413 EXPECT ("function"));
4414 }
4415 }
4416
4417 /* While "w" is a prefix of both components, the match function
4418 should still only be called once. */
4419 {
4420 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4421 EXPECT ("w1::w2"));
a20714ff
PA
4422 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4423 EXPECT ("w1::w2"));
c62446b1
PA
4424 }
4425
4426 /* Same, with a "complicated" symbol. */
4427 {
4428 static const char str[] = Z_SYM_NAME;
4429 size_t len = strlen (str);
4430 std::string lookup;
4431
4432 for (size_t i = 1; i < len; i++)
4433 {
4434 lookup.assign (str, i);
4435 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4436 EXPECT (Z_SYM_NAME));
4437 }
4438 }
4439
4440 /* In FULL mode, an incomplete symbol doesn't match. */
4441 {
4442 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4443 {});
4444 }
4445
4446 /* A complete symbol with parameters matches any overload, since the
4447 index has no overload info. */
4448 {
4449 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4450 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4451 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4452 EXPECT ("std::zfunction", "std::zfunction2"));
4453 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4454 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4455 }
4456
4457 /* Check that whitespace is ignored appropriately. A symbol with a
4458 template argument list. */
4459 {
4460 static const char expected[] = "ns::foo<int>";
4461 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4462 EXPECT (expected));
a20714ff
PA
4463 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4464 EXPECT (expected));
c62446b1
PA
4465 }
4466
4467 /* Check that whitespace is ignored appropriately. A symbol with a
4468 template argument list that includes a pointer. */
4469 {
4470 static const char expected[] = "ns::foo<char*>";
4471 /* Try both completion and non-completion modes. */
4472 static const bool completion_mode[2] = {false, true};
4473 for (size_t i = 0; i < 2; i++)
4474 {
4475 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4476 completion_mode[i], EXPECT (expected));
a20714ff
PA
4477 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4478 completion_mode[i], EXPECT (expected));
c62446b1
PA
4479
4480 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4481 completion_mode[i], EXPECT (expected));
a20714ff
PA
4482 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4483 completion_mode[i], EXPECT (expected));
c62446b1
PA
4484 }
4485 }
4486
4487 {
4488 /* Check method qualifiers are ignored. */
4489 static const char expected[] = "ns::foo<char*>";
4490 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4491 symbol_name_match_type::FULL, true, EXPECT (expected));
4492 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4493 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4494 CHECK_MATCH ("foo < char * > ( int ) const",
4495 symbol_name_match_type::WILD, true, EXPECT (expected));
4496 CHECK_MATCH ("foo < char * > ( int ) &&",
4497 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4498 }
4499
4500 /* Test lookup names that don't match anything. */
4501 {
a20714ff
PA
4502 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4503 {});
4504
c62446b1
PA
4505 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4506 {});
4507 }
4508
a20714ff
PA
4509 /* Some wild matching tests, exercising "(anonymous namespace)",
4510 which should not be confused with a parameter list. */
4511 {
4512 static const char *syms[] = {
4513 "A::B::C",
4514 "B::C",
4515 "C",
4516 "A :: B :: C ( int )",
4517 "B :: C ( int )",
4518 "C ( int )",
4519 };
4520
4521 for (const char *s : syms)
4522 {
4523 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4524 EXPECT ("(anonymous namespace)::A::B::C"));
4525 }
4526 }
4527
4528 {
4529 static const char expected[] = "ns2::tmpl<int>::foo2";
4530 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4531 EXPECT (expected));
4532 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4533 EXPECT (expected));
4534 }
4535
c62446b1
PA
4536 SELF_CHECK (!any_mismatch);
4537
4538#undef EXPECT
4539#undef CHECK_MATCH
4540}
4541
5c58de74
PA
4542static void
4543run_test ()
4544{
4545 test_mapped_index_find_name_component_bounds ();
4546 test_dw2_expand_symtabs_matching_symbol ();
4547}
4548
c62446b1
PA
4549}} // namespace selftests::dw2_expand_symtabs_matching
4550
4551#endif /* GDB_SELF_TEST */
4552
4b514bc8
JK
4553/* If FILE_MATCHER is NULL or if PER_CU has
4554 dwarf2_per_cu_quick_data::MARK set (see
4555 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4556 EXPANSION_NOTIFY on it. */
4557
4558static void
4559dw2_expand_symtabs_matching_one
97a1449a
SM
4560 (dwarf2_per_cu_data *per_cu,
4561 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4562 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4563 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4564{
4565 if (file_matcher == NULL || per_cu->v.quick->mark)
4566 {
af758d11 4567 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4568
97a1449a
SM
4569 compunit_symtab *symtab
4570 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4571 gdb_assert (symtab != nullptr);
4b514bc8 4572
af758d11
SM
4573 if (expansion_notify != NULL && symtab_was_null)
4574 expansion_notify (symtab);
4b514bc8
JK
4575 }
4576}
4577
3f563c84
PA
4578/* Helper for dw2_expand_matching symtabs. Called on each symbol
4579 matched, to expand corresponding CUs that were marked. IDX is the
4580 index of the symbol name that matched. */
4581
4582static void
4583dw2_expand_marked_cus
976ca316 4584 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4585 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4586 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4587 search_domain kind)
4588{
3f563c84
PA
4589 offset_type *vec, vec_len, vec_idx;
4590 bool global_seen = false;
976ca316 4591 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4592
61920122 4593 vec = (offset_type *) (index.constant_pool
f00a2de2 4594 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4595 vec_len = MAYBE_SWAP (vec[0]);
4596 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4597 {
61920122
PA
4598 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4599 /* This value is only valid for index versions >= 7. */
4600 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4601 gdb_index_symbol_kind symbol_kind =
4602 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4603 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4604 /* Only check the symbol attributes if they're present.
4605 Indices prior to version 7 don't record them,
4606 and indices >= 7 may elide them for certain symbols
4607 (gold does this). */
4608 int attrs_valid =
4609 (index.version >= 7
4610 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4611
4612 /* Work around gold/15646. */
f030440d
TV
4613 if (attrs_valid
4614 && !is_static
4615 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4616 {
f030440d 4617 if (global_seen)
61920122 4618 continue;
f030440d
TV
4619
4620 global_seen = true;
61920122 4621 }
3190f0c6 4622
61920122
PA
4623 /* Only check the symbol's kind if it has one. */
4624 if (attrs_valid)
4625 {
4626 switch (kind)
8943b874 4627 {
61920122
PA
4628 case VARIABLES_DOMAIN:
4629 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4630 continue;
4631 break;
4632 case FUNCTIONS_DOMAIN:
4633 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4634 continue;
61920122
PA
4635 break;
4636 case TYPES_DOMAIN:
4637 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4638 continue;
4639 break;
59c35742
AB
4640 case MODULES_DOMAIN:
4641 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4642 continue;
4643 break;
61920122
PA
4644 default:
4645 break;
8943b874 4646 }
61920122 4647 }
8943b874 4648
61920122 4649 /* Don't crash on bad data. */
976ca316
SM
4650 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4651 + per_objfile->per_bfd->all_type_units.size ()))
61920122 4652 {
b98664d3 4653 complaint (_(".gdb_index entry has bad CU index"
976ca316 4654 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4655 continue;
4656 }
4657
976ca316
SM
4658 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4659 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4b514bc8 4660 expansion_notify);
61920122
PA
4661 }
4662}
4663
4b514bc8
JK
4664/* If FILE_MATCHER is non-NULL, set all the
4665 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4666 that match FILE_MATCHER. */
4667
61920122 4668static void
4b514bc8 4669dw_expand_symtabs_matching_file_matcher
976ca316 4670 (dwarf2_per_objfile *per_objfile,
ed2dc618 4671 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4672{
4b514bc8 4673 if (file_matcher == NULL)
61920122
PA
4674 return;
4675
4b514bc8
JK
4676 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4677 htab_eq_pointer,
4678 NULL, xcalloc, xfree));
4679 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4680 htab_eq_pointer,
4681 NULL, xcalloc, xfree));
61920122 4682
4b514bc8
JK
4683 /* The rule is CUs specify all the files, including those used by
4684 any TU, so there's no need to scan TUs here. */
61920122 4685
976ca316 4686 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4687 {
927aa2e7
JK
4688 QUIT;
4689
4690 per_cu->v.quick->mark = 0;
4691
4692 /* We only need to look at symtabs not already expanded. */
976ca316 4693 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4694 continue;
4695
976ca316 4696 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4697 if (file_data == NULL)
4698 continue;
4699
4700 if (htab_find (visited_not_found.get (), file_data) != NULL)
4701 continue;
4702 else if (htab_find (visited_found.get (), file_data) != NULL)
4703 {
4704 per_cu->v.quick->mark = 1;
4705 continue;
4706 }
4707
b76e467d 4708 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4709 {
4710 const char *this_real_name;
4711
4712 if (file_matcher (file_data->file_names[j], false))
4713 {
4714 per_cu->v.quick->mark = 1;
4715 break;
4716 }
4717
4718 /* Before we invoke realpath, which can get expensive when many
4719 files are involved, do a quick comparison of the basenames. */
4720 if (!basenames_may_differ
4721 && !file_matcher (lbasename (file_data->file_names[j]),
4722 true))
4723 continue;
4724
976ca316 4725 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4726 if (file_matcher (this_real_name, false))
4727 {
4728 per_cu->v.quick->mark = 1;
4729 break;
4730 }
4731 }
4732
b76e467d
SM
4733 void **slot = htab_find_slot (per_cu->v.quick->mark
4734 ? visited_found.get ()
4735 : visited_not_found.get (),
4736 file_data, INSERT);
927aa2e7
JK
4737 *slot = file_data;
4738 }
4739}
4740
4741static void
4742dw2_expand_symtabs_matching
4743 (struct objfile *objfile,
4744 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4745 const lookup_name_info *lookup_name,
927aa2e7
JK
4746 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4747 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4748 enum search_domain kind)
4749{
976ca316 4750 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4751
4752 /* index_table is NULL if OBJF_READNOW. */
976ca316 4753 if (!per_objfile->per_bfd->index_table)
927aa2e7
JK
4754 return;
4755
976ca316 4756 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4757
c1a66c06
TV
4758 if (symbol_matcher == NULL && lookup_name == NULL)
4759 {
976ca316 4760 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4761 {
4762 QUIT;
4763
976ca316 4764 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
97a1449a 4765 file_matcher, expansion_notify);
c1a66c06
TV
4766 }
4767 return;
4768 }
4769
976ca316 4770 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4771
c1a66c06 4772 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4773 symbol_matcher,
4774 kind, [&] (offset_type idx)
4775 {
976ca316
SM
4776 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4777 kind);
3b00ef10 4778 return true;
976ca316 4779 }, per_objfile);
927aa2e7
JK
4780}
4781
4782/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4783 symtab. */
4784
4785static struct compunit_symtab *
4786recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4787 CORE_ADDR pc)
4788{
4789 int i;
4790
4791 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4792 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4793 return cust;
4794
4795 if (cust->includes == NULL)
4796 return NULL;
4797
4798 for (i = 0; cust->includes[i]; ++i)
4799 {
4800 struct compunit_symtab *s = cust->includes[i];
4801
4802 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4803 if (s != NULL)
4804 return s;
4805 }
4806
4807 return NULL;
4808}
4809
4810static struct compunit_symtab *
4811dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4812 struct bound_minimal_symbol msymbol,
4813 CORE_ADDR pc,
4814 struct obj_section *section,
4815 int warn_if_readin)
4816{
4817 struct dwarf2_per_cu_data *data;
4818 struct compunit_symtab *result;
4819
d320c2b5 4820 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4821 return NULL;
4822
b3b3bada 4823 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4824 data = (struct dwarf2_per_cu_data *) addrmap_find
4825 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4826 if (!data)
4827 return NULL;
4828
af758d11
SM
4829 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4830 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4831 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4832 paddress (objfile->arch (), pc));
927aa2e7 4833
97a1449a
SM
4834 result = recursively_find_pc_sect_compunit_symtab
4835 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4836
927aa2e7
JK
4837 gdb_assert (result != NULL);
4838 return result;
4839}
4840
4841static void
4842dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4843 void *data, int need_fullname)
4844{
976ca316 4845 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4846
976ca316 4847 if (!per_objfile->per_bfd->filenames_cache)
927aa2e7 4848 {
976ca316 4849 per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4850
4851 htab_up visited (htab_create_alloc (10,
4852 htab_hash_pointer, htab_eq_pointer,
4853 NULL, xcalloc, xfree));
4854
4855 /* The rule is CUs specify all the files, including those used
4856 by any TU, so there's no need to scan TUs here. We can
4857 ignore file names coming from already-expanded CUs. */
4858
976ca316 4859 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4860 {
976ca316 4861 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4862 {
4863 void **slot = htab_find_slot (visited.get (),
4864 per_cu->v.quick->file_names,
4865 INSERT);
4866
4867 *slot = per_cu->v.quick->file_names;
4868 }
4869 }
4870
976ca316 4871 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4872 {
927aa2e7 4873 /* We only need to look at symtabs not already expanded. */
976ca316 4874 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4875 continue;
4876
ab432490 4877 quick_file_names *file_data
976ca316 4878 = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4879 if (file_data == NULL)
4880 continue;
4881
b76e467d 4882 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4883 if (*slot)
4884 {
4885 /* Already visited. */
4886 continue;
4887 }
4888 *slot = file_data;
4889
4890 for (int j = 0; j < file_data->num_file_names; ++j)
4891 {
4892 const char *filename = file_data->file_names[j];
976ca316 4893 per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4894 }
4895 }
4896 }
4897
976ca316 4898 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4899 {
4900 gdb::unique_xmalloc_ptr<char> this_real_name;
4901
4902 if (need_fullname)
4903 this_real_name = gdb_realpath (filename);
4904 (*fun) (filename, this_real_name.get (), data);
4905 });
4906}
4907
4908static int
4909dw2_has_symbols (struct objfile *objfile)
4910{
4911 return 1;
4912}
4913
4914const struct quick_symbol_functions dwarf2_gdb_index_functions =
4915{
4916 dw2_has_symbols,
4917 dw2_find_last_source_symtab,
4918 dw2_forget_cached_source_info,
4919 dw2_map_symtabs_matching_filename,
4920 dw2_lookup_symbol,
d3214198 4921 NULL,
927aa2e7
JK
4922 dw2_print_stats,
4923 dw2_dump,
927aa2e7
JK
4924 dw2_expand_symtabs_for_function,
4925 dw2_expand_all_symtabs,
4926 dw2_expand_symtabs_with_fullname,
4927 dw2_map_matching_symbols,
4928 dw2_expand_symtabs_matching,
4929 dw2_find_pc_sect_compunit_symtab,
4930 NULL,
4931 dw2_map_symbol_filenames
4932};
4933
4934/* DWARF-5 debug_names reader. */
4935
4936/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4937static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4938
4939/* A helper function that reads the .debug_names section in SECTION
4940 and fills in MAP. FILENAME is the name of the file containing the
4941 section; it is used for error reporting.
4942
4943 Returns true if all went well, false otherwise. */
4944
4945static bool
4946read_debug_names_from_section (struct objfile *objfile,
4947 const char *filename,
4948 struct dwarf2_section_info *section,
4949 mapped_debug_names &map)
4950{
96b79293 4951 if (section->empty ())
927aa2e7
JK
4952 return false;
4953
4954 /* Older elfutils strip versions could keep the section in the main
4955 executable while splitting it for the separate debug info file. */
96b79293 4956 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4957 return false;
4958
96b79293 4959 section->read (objfile);
927aa2e7 4960
08feed99 4961 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4962
4963 const gdb_byte *addr = section->buffer;
4964
96b79293 4965 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4966
4967 unsigned int bytes_read;
4968 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4969 addr += bytes_read;
4970
4971 map.dwarf5_is_dwarf64 = bytes_read != 4;
4972 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4973 if (bytes_read + length != section->size)
4974 {
4975 /* There may be multiple per-CU indices. */
4976 warning (_("Section .debug_names in %s length %s does not match "
4977 "section length %s, ignoring .debug_names."),
4978 filename, plongest (bytes_read + length),
4979 pulongest (section->size));
4980 return false;
4981 }
4982
4983 /* The version number. */
4984 uint16_t version = read_2_bytes (abfd, addr);
4985 addr += 2;
4986 if (version != 5)
4987 {
4988 warning (_("Section .debug_names in %s has unsupported version %d, "
4989 "ignoring .debug_names."),
4990 filename, version);
4991 return false;
4992 }
4993
4994 /* Padding. */
4995 uint16_t padding = read_2_bytes (abfd, addr);
4996 addr += 2;
4997 if (padding != 0)
4998 {
4999 warning (_("Section .debug_names in %s has unsupported padding %d, "
5000 "ignoring .debug_names."),
5001 filename, padding);
5002 return false;
5003 }
5004
5005 /* comp_unit_count - The number of CUs in the CU list. */
5006 map.cu_count = read_4_bytes (abfd, addr);
5007 addr += 4;
5008
5009 /* local_type_unit_count - The number of TUs in the local TU
5010 list. */
5011 map.tu_count = read_4_bytes (abfd, addr);
5012 addr += 4;
5013
5014 /* foreign_type_unit_count - The number of TUs in the foreign TU
5015 list. */
5016 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5017 addr += 4;
5018 if (foreign_tu_count != 0)
5019 {
5020 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5021 "ignoring .debug_names."),
5022 filename, static_cast<unsigned long> (foreign_tu_count));
5023 return false;
5024 }
5025
5026 /* bucket_count - The number of hash buckets in the hash lookup
5027 table. */
5028 map.bucket_count = read_4_bytes (abfd, addr);
5029 addr += 4;
5030
5031 /* name_count - The number of unique names in the index. */
5032 map.name_count = read_4_bytes (abfd, addr);
5033 addr += 4;
5034
5035 /* abbrev_table_size - The size in bytes of the abbreviations
5036 table. */
5037 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5038 addr += 4;
5039
5040 /* augmentation_string_size - The size in bytes of the augmentation
5041 string. This value is rounded up to a multiple of 4. */
5042 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5043 addr += 4;
5044 map.augmentation_is_gdb = ((augmentation_string_size
5045 == sizeof (dwarf5_augmentation))
5046 && memcmp (addr, dwarf5_augmentation,
5047 sizeof (dwarf5_augmentation)) == 0);
5048 augmentation_string_size += (-augmentation_string_size) & 3;
5049 addr += augmentation_string_size;
5050
5051 /* List of CUs */
5052 map.cu_table_reordered = addr;
5053 addr += map.cu_count * map.offset_size;
5054
5055 /* List of Local TUs */
5056 map.tu_table_reordered = addr;
5057 addr += map.tu_count * map.offset_size;
5058
5059 /* Hash Lookup Table */
5060 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5061 addr += map.bucket_count * 4;
5062 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5063 addr += map.name_count * 4;
5064
5065 /* Name Table */
5066 map.name_table_string_offs_reordered = addr;
5067 addr += map.name_count * map.offset_size;
5068 map.name_table_entry_offs_reordered = addr;
5069 addr += map.name_count * map.offset_size;
5070
5071 const gdb_byte *abbrev_table_start = addr;
5072 for (;;)
5073 {
927aa2e7
JK
5074 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5075 addr += bytes_read;
5076 if (index_num == 0)
5077 break;
5078
5079 const auto insertpair
5080 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5081 if (!insertpair.second)
5082 {
5083 warning (_("Section .debug_names in %s has duplicate index %s, "
5084 "ignoring .debug_names."),
5085 filename, pulongest (index_num));
5086 return false;
5087 }
5088 mapped_debug_names::index_val &indexval = insertpair.first->second;
5089 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5090 addr += bytes_read;
5091
5092 for (;;)
5093 {
5094 mapped_debug_names::index_val::attr attr;
5095 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5096 addr += bytes_read;
5097 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5098 addr += bytes_read;
5099 if (attr.form == DW_FORM_implicit_const)
5100 {
5101 attr.implicit_const = read_signed_leb128 (abfd, addr,
5102 &bytes_read);
5103 addr += bytes_read;
5104 }
5105 if (attr.dw_idx == 0 && attr.form == 0)
5106 break;
5107 indexval.attr_vec.push_back (std::move (attr));
5108 }
5109 }
5110 if (addr != abbrev_table_start + abbrev_table_size)
5111 {
5112 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5113 "of size %s vs. written as %u, ignoring .debug_names."),
5114 filename, plongest (addr - abbrev_table_start),
5115 abbrev_table_size);
927aa2e7
JK
5116 return false;
5117 }
5118 map.entry_pool = addr;
5119
5120 return true;
5121}
5122
5123/* A helper for create_cus_from_debug_names that handles the MAP's CU
5124 list. */
5125
5126static void
168c9250 5127create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5128 const mapped_debug_names &map,
5129 dwarf2_section_info &section,
b76e467d 5130 bool is_dwz)
927aa2e7 5131{
3ee6bb11
TV
5132 if (!map.augmentation_is_gdb)
5133 {
5134 for (uint32_t i = 0; i < map.cu_count; ++i)
5135 {
5136 sect_offset sect_off
5137 = (sect_offset) (extract_unsigned_integer
5138 (map.cu_table_reordered + i * map.offset_size,
5139 map.offset_size,
5140 map.dwarf5_byte_order));
5141 /* We don't know the length of the CU, because the CU list in a
5142 .debug_names index can be incomplete, so we can't use the start of
5143 the next CU as end of this CU. We create the CUs here with length 0,
5144 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5145 dwarf2_per_cu_data *per_cu
168c9250
SM
5146 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5147 per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5148 }
5149 }
5150
927aa2e7
JK
5151 sect_offset sect_off_prev;
5152 for (uint32_t i = 0; i <= map.cu_count; ++i)
5153 {
5154 sect_offset sect_off_next;
5155 if (i < map.cu_count)
5156 {
5157 sect_off_next
5158 = (sect_offset) (extract_unsigned_integer
5159 (map.cu_table_reordered + i * map.offset_size,
5160 map.offset_size,
5161 map.dwarf5_byte_order));
5162 }
5163 else
5164 sect_off_next = (sect_offset) section.size;
5165 if (i >= 1)
5166 {
5167 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5168 dwarf2_per_cu_data *per_cu
168c9250 5169 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5170 sect_off_prev, length);
168c9250 5171 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5172 }
5173 sect_off_prev = sect_off_next;
5174 }
5175}
5176
5177/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5178 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5179
5180static void
168c9250 5181create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5182 const mapped_debug_names &map,
5183 const mapped_debug_names &dwz_map)
5184{
168c9250
SM
5185 gdb_assert (per_bfd->all_comp_units.empty ());
5186 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5187
168c9250 5188 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5189 false /* is_dwz */);
927aa2e7
JK
5190
5191 if (dwz_map.cu_count == 0)
5192 return;
5193
168c9250
SM
5194 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5195 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5196 true /* is_dwz */);
927aa2e7
JK
5197}
5198
5199/* Read .debug_names. If everything went ok, initialize the "quick"
5200 elements of all the CUs and return true. Otherwise, return false. */
5201
5202static bool
976ca316 5203dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 5204{
fcf23d5b
SM
5205 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5206 mapped_debug_names dwz_map;
976ca316 5207 struct objfile *objfile = per_objfile->objfile;
927aa2e7
JK
5208
5209 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
976ca316 5210 &per_objfile->per_bfd->debug_names, *map))
927aa2e7
JK
5211 return false;
5212
5213 /* Don't use the index if it's empty. */
22ca247e 5214 if (map->name_count == 0)
927aa2e7
JK
5215 return false;
5216
5217 /* If there is a .dwz file, read it so we can get its CU list as
5218 well. */
976ca316 5219 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
927aa2e7
JK
5220 if (dwz != NULL)
5221 {
5222 if (!read_debug_names_from_section (objfile,
00f93c44 5223 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5224 &dwz->debug_names, dwz_map))
5225 {
5226 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5227 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5228 return false;
5229 }
5230 }
5231
976ca316 5232 create_cus_from_debug_names (per_objfile->per_bfd, *map, dwz_map);
927aa2e7 5233
22ca247e 5234 if (map->tu_count != 0)
927aa2e7
JK
5235 {
5236 /* We can only handle a single .debug_types when we have an
5237 index. */
976ca316 5238 if (per_objfile->per_bfd->types.size () != 1)
927aa2e7
JK
5239 return false;
5240
976ca316 5241 dwarf2_section_info *section = &per_objfile->per_bfd->types[0];
927aa2e7
JK
5242
5243 create_signatured_type_table_from_debug_names
976ca316 5244 (per_objfile, *map, section, &per_objfile->per_bfd->abbrev);
927aa2e7
JK
5245 }
5246
976ca316
SM
5247 create_addrmap_from_aranges (per_objfile,
5248 &per_objfile->per_bfd->debug_aranges);
927aa2e7 5249
976ca316
SM
5250 per_objfile->per_bfd->debug_names_table = std::move (map);
5251 per_objfile->per_bfd->using_index = 1;
5252 per_objfile->per_bfd->quick_file_names_table =
5253 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
927aa2e7
JK
5254
5255 return true;
5256}
5257
927aa2e7
JK
5258/* Type used to manage iterating over all CUs looking for a symbol for
5259 .debug_names. */
5260
5261class dw2_debug_names_iterator
5262{
5263public:
927aa2e7 5264 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5265 gdb::optional<block_enum> block_index,
5266 domain_enum domain,
fcf23d5b 5267 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5268 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5269 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5270 m_per_objfile (per_objfile)
927aa2e7
JK
5271 {}
5272
5273 dw2_debug_names_iterator (const mapped_debug_names &map,
fcf23d5b 5274 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5275 : m_map (map),
5276 m_search (search),
fcf23d5b
SM
5277 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5278 m_per_objfile (per_objfile)
927aa2e7
JK
5279 {}
5280
3b00ef10
TT
5281 dw2_debug_names_iterator (const mapped_debug_names &map,
5282 block_enum block_index, domain_enum domain,
fcf23d5b 5283 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5284 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5285 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5286 m_per_objfile (per_objfile)
3b00ef10
TT
5287 {}
5288
927aa2e7
JK
5289 /* Return the next matching CU or NULL if there are no more. */
5290 dwarf2_per_cu_data *next ();
5291
5292private:
5293 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5294 const char *name,
5295 dwarf2_per_objfile *per_objfile);
927aa2e7 5296 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5297 uint32_t namei,
5298 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5299
5300 /* The internalized form of .debug_names. */
5301 const mapped_debug_names &m_map;
5302
2b79f376
SM
5303 /* If set, only look for symbols that match that block. Valid values are
5304 GLOBAL_BLOCK and STATIC_BLOCK. */
5305 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5306
5307 /* The kind of symbol we're looking for. */
5308 const domain_enum m_domain = UNDEF_DOMAIN;
5309 const search_domain m_search = ALL_DOMAIN;
5310
5311 /* The list of CUs from the index entry of the symbol, or NULL if
5312 not found. */
5313 const gdb_byte *m_addr;
fcf23d5b
SM
5314
5315 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5316};
5317
5318const char *
fcf23d5b 5319mapped_debug_names::namei_to_name
976ca316 5320 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5321{
5322 const ULONGEST namei_string_offs
5323 = extract_unsigned_integer ((name_table_string_offs_reordered
5324 + namei * offset_size),
5325 offset_size,
5326 dwarf5_byte_order);
976ca316 5327 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5328}
5329
5330/* Find a slot in .debug_names for the object named NAME. If NAME is
5331 found, return pointer to its pool data. If NAME cannot be found,
5332 return NULL. */
5333
5334const gdb_byte *
5335dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5336 (const mapped_debug_names &map, const char *name,
5337 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5338{
5339 int (*cmp) (const char *, const char *);
5340
54ee4252 5341 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5342 if (current_language->la_language == language_cplus
5343 || current_language->la_language == language_fortran
5344 || current_language->la_language == language_d)
5345 {
5346 /* NAME is already canonical. Drop any qualifiers as
5347 .debug_names does not contain any. */
5348
5349 if (strchr (name, '(') != NULL)
5350 {
54ee4252 5351 without_params = cp_remove_params (name);
927aa2e7 5352 if (without_params != NULL)
54ee4252 5353 name = without_params.get ();
927aa2e7
JK
5354 }
5355 }
5356
5357 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5358
5359 const uint32_t full_hash = dwarf5_djb_hash (name);
5360 uint32_t namei
5361 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5362 (map.bucket_table_reordered
5363 + (full_hash % map.bucket_count)), 4,
5364 map.dwarf5_byte_order);
5365 if (namei == 0)
5366 return NULL;
5367 --namei;
5368 if (namei >= map.name_count)
5369 {
b98664d3 5370 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5371 "[in module %s]"),
5372 namei, map.name_count,
fcf23d5b 5373 objfile_name (per_objfile->objfile));
927aa2e7
JK
5374 return NULL;
5375 }
5376
5377 for (;;)
5378 {
5379 const uint32_t namei_full_hash
5380 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5381 (map.hash_table_reordered + namei), 4,
5382 map.dwarf5_byte_order);
5383 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5384 return NULL;
5385
5386 if (full_hash == namei_full_hash)
5387 {
fcf23d5b 5388 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5389
5390#if 0 /* An expensive sanity check. */
5391 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5392 {
b98664d3 5393 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5394 "[in module %s]"),
5395 namei, objfile_name (dwarf2_per_objfile->objfile));
5396 return NULL;
5397 }
5398#endif
5399
5400 if (cmp (namei_string, name) == 0)
5401 {
5402 const ULONGEST namei_entry_offs
5403 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5404 + namei * map.offset_size),
5405 map.offset_size, map.dwarf5_byte_order);
5406 return map.entry_pool + namei_entry_offs;
5407 }
5408 }
5409
5410 ++namei;
5411 if (namei >= map.name_count)
5412 return NULL;
5413 }
5414}
5415
5416const gdb_byte *
5417dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5418 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5419{
5420 if (namei >= map.name_count)
5421 {
b98664d3 5422 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5423 "[in module %s]"),
5424 namei, map.name_count,
fcf23d5b 5425 objfile_name (per_objfile->objfile));
927aa2e7
JK
5426 return NULL;
5427 }
5428
5429 const ULONGEST namei_entry_offs
5430 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5431 + namei * map.offset_size),
5432 map.offset_size, map.dwarf5_byte_order);
5433 return map.entry_pool + namei_entry_offs;
5434}
5435
5436/* See dw2_debug_names_iterator. */
5437
5438dwarf2_per_cu_data *
5439dw2_debug_names_iterator::next ()
5440{
5441 if (m_addr == NULL)
5442 return NULL;
5443
fcf23d5b
SM
5444 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5445 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5446 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5447
5448 again:
5449
5450 unsigned int bytes_read;
5451 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5452 m_addr += bytes_read;
5453 if (abbrev == 0)
5454 return NULL;
5455
5456 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5457 if (indexval_it == m_map.abbrev_map.cend ())
5458 {
b98664d3 5459 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5460 "[in module %s]"),
ed2dc618 5461 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5462 return NULL;
5463 }
5464 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5465 enum class symbol_linkage {
5466 unknown,
5467 static_,
5468 extern_,
23c13d42 5469 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5470 dwarf2_per_cu_data *per_cu = NULL;
5471 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5472 {
5473 ULONGEST ull;
5474 switch (attr.form)
5475 {
5476 case DW_FORM_implicit_const:
5477 ull = attr.implicit_const;
5478 break;
5479 case DW_FORM_flag_present:
5480 ull = 1;
5481 break;
5482 case DW_FORM_udata:
5483 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5484 m_addr += bytes_read;
5485 break;
6dc55ce9 5486 case DW_FORM_ref4:
5487 ull = read_4_bytes (abfd, m_addr);
5488 m_addr += 4;
5489 break;
5490 case DW_FORM_ref8:
5491 ull = read_8_bytes (abfd, m_addr);
5492 m_addr += 8;
5493 break;
5494 case DW_FORM_ref_sig8:
5495 ull = read_8_bytes (abfd, m_addr);
5496 m_addr += 8;
5497 break;
927aa2e7 5498 default:
b98664d3 5499 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5500 dwarf_form_name (attr.form),
ed2dc618 5501 objfile_name (objfile));
927aa2e7
JK
5502 return NULL;
5503 }
5504 switch (attr.dw_idx)
5505 {
5506 case DW_IDX_compile_unit:
5507 /* Don't crash on bad data. */
fcf23d5b 5508 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5509 {
b98664d3 5510 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5511 " [in module %s]"),
5512 pulongest (ull),
fcf23d5b 5513 objfile_name (objfile));
927aa2e7
JK
5514 continue;
5515 }
fcf23d5b 5516 per_cu = per_bfd->get_cutu (ull);
927aa2e7 5517 break;
8af5c486
JK
5518 case DW_IDX_type_unit:
5519 /* Don't crash on bad data. */
fcf23d5b 5520 if (ull >= per_bfd->all_type_units.size ())
8af5c486 5521 {
b98664d3 5522 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5523 " [in module %s]"),
5524 pulongest (ull),
fcf23d5b 5525 objfile_name (objfile));
8af5c486
JK
5526 continue;
5527 }
fcf23d5b 5528 per_cu = &per_bfd->get_tu (ull)->per_cu;
8af5c486 5529 break;
6dc55ce9 5530 case DW_IDX_die_offset:
5531 /* In a per-CU index (as opposed to a per-module index), index
5532 entries without CU attribute implicitly refer to the single CU. */
5533 if (per_cu == NULL)
fcf23d5b 5534 per_cu = per_bfd->get_cu (0);
6dc55ce9 5535 break;
927aa2e7
JK
5536 case DW_IDX_GNU_internal:
5537 if (!m_map.augmentation_is_gdb)
5538 break;
23c13d42 5539 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5540 break;
5541 case DW_IDX_GNU_external:
5542 if (!m_map.augmentation_is_gdb)
5543 break;
23c13d42 5544 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5545 break;
5546 }
5547 }
5548
5549 /* Skip if already read in. */
fcf23d5b 5550 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5551 goto again;
5552
5553 /* Check static vs global. */
23c13d42 5554 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5555 {
2b79f376 5556 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5557 const bool symbol_is_static =
5558 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5559 if (want_static != symbol_is_static)
2b79f376 5560 goto again;
927aa2e7
JK
5561 }
5562
5563 /* Match dw2_symtab_iter_next, symbol_kind
5564 and debug_names::psymbol_tag. */
5565 switch (m_domain)
5566 {
5567 case VAR_DOMAIN:
5568 switch (indexval.dwarf_tag)
5569 {
5570 case DW_TAG_variable:
5571 case DW_TAG_subprogram:
5572 /* Some types are also in VAR_DOMAIN. */
5573 case DW_TAG_typedef:
5574 case DW_TAG_structure_type:
5575 break;
5576 default:
5577 goto again;
5578 }
5579 break;
5580 case STRUCT_DOMAIN:
5581 switch (indexval.dwarf_tag)
5582 {
5583 case DW_TAG_typedef:
5584 case DW_TAG_structure_type:
5585 break;
5586 default:
5587 goto again;
5588 }
5589 break;
5590 case LABEL_DOMAIN:
5591 switch (indexval.dwarf_tag)
5592 {
5593 case 0:
5594 case DW_TAG_variable:
5595 break;
5596 default:
5597 goto again;
5598 }
5599 break;
59c35742
AB
5600 case MODULE_DOMAIN:
5601 switch (indexval.dwarf_tag)
5602 {
5603 case DW_TAG_module:
5604 break;
5605 default:
5606 goto again;
5607 }
5608 break;
927aa2e7
JK
5609 default:
5610 break;
5611 }
5612
5613 /* Match dw2_expand_symtabs_matching, symbol_kind and
5614 debug_names::psymbol_tag. */
5615 switch (m_search)
4b514bc8 5616 {
927aa2e7
JK
5617 case VARIABLES_DOMAIN:
5618 switch (indexval.dwarf_tag)
4b514bc8 5619 {
927aa2e7
JK
5620 case DW_TAG_variable:
5621 break;
5622 default:
5623 goto again;
4b514bc8 5624 }
927aa2e7
JK
5625 break;
5626 case FUNCTIONS_DOMAIN:
5627 switch (indexval.dwarf_tag)
4b514bc8 5628 {
927aa2e7
JK
5629 case DW_TAG_subprogram:
5630 break;
5631 default:
5632 goto again;
4b514bc8 5633 }
927aa2e7
JK
5634 break;
5635 case TYPES_DOMAIN:
5636 switch (indexval.dwarf_tag)
5637 {
5638 case DW_TAG_typedef:
5639 case DW_TAG_structure_type:
5640 break;
5641 default:
5642 goto again;
5643 }
5644 break;
59c35742
AB
5645 case MODULES_DOMAIN:
5646 switch (indexval.dwarf_tag)
5647 {
5648 case DW_TAG_module:
5649 break;
5650 default:
5651 goto again;
5652 }
927aa2e7
JK
5653 default:
5654 break;
4b514bc8 5655 }
927aa2e7
JK
5656
5657 return per_cu;
4b514bc8 5658}
61920122 5659
927aa2e7 5660static struct compunit_symtab *
c7f839cb 5661dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5662 const char *name, domain_enum domain)
4b514bc8 5663{
976ca316 5664 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
61920122 5665
976ca316 5666 const auto &mapp = per_objfile->per_bfd->debug_names_table;
927aa2e7 5667 if (!mapp)
61920122 5668 {
927aa2e7
JK
5669 /* index is NULL if OBJF_READNOW. */
5670 return NULL;
5671 }
5672 const auto &map = *mapp;
9291a0cd 5673
976ca316 5674 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
9703b513 5675
927aa2e7
JK
5676 struct compunit_symtab *stab_best = NULL;
5677 struct dwarf2_per_cu_data *per_cu;
5678 while ((per_cu = iter.next ()) != NULL)
5679 {
5680 struct symbol *sym, *with_opaque = NULL;
97a1449a 5681 compunit_symtab *stab
976ca316 5682 = dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7 5683 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5684 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5685
927aa2e7
JK
5686 sym = block_find_symbol (block, name, domain,
5687 block_find_non_opaque_type_preferred,
5688 &with_opaque);
9703b513 5689
927aa2e7
JK
5690 /* Some caution must be observed with overloaded functions and
5691 methods, since the index will not contain any overload
5692 information (but NAME might contain it). */
a3ec0bb1 5693
927aa2e7 5694 if (sym != NULL
987012b8 5695 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5696 return stab;
5697 if (with_opaque != NULL
987012b8 5698 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5699 stab_best = stab;
9703b513 5700
927aa2e7 5701 /* Keep looking through other CUs. */
9703b513
TT
5702 }
5703
927aa2e7 5704 return stab_best;
9703b513
TT
5705}
5706
927aa2e7
JK
5707/* This dumps minimal information about .debug_names. It is called
5708 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5709 uses this to verify that .debug_names has been loaded. */
9291a0cd 5710
927aa2e7
JK
5711static void
5712dw2_debug_names_dump (struct objfile *objfile)
5713{
976ca316 5714 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5715
976ca316 5716 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5717 printf_filtered (".debug_names:");
976ca316 5718 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5719 printf_filtered (" exists\n");
5720 else
5721 printf_filtered (" faked for \"readnow\"\n");
5722 printf_filtered ("\n");
9291a0cd
TT
5723}
5724
9291a0cd 5725static void
927aa2e7
JK
5726dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5727 const char *func_name)
9291a0cd 5728{
976ca316 5729 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 5730
976ca316
SM
5731 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5732 if (per_objfile->per_bfd->debug_names_table)
24c79950 5733 {
976ca316 5734 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
24c79950 5735
fcf23d5b 5736 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
976ca316 5737 per_objfile);
24c79950 5738
927aa2e7
JK
5739 struct dwarf2_per_cu_data *per_cu;
5740 while ((per_cu = iter.next ()) != NULL)
976ca316 5741 dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7
JK
5742 }
5743}
24c79950 5744
3b00ef10
TT
5745static void
5746dw2_debug_names_map_matching_symbols
5747 (struct objfile *objfile,
5748 const lookup_name_info &name, domain_enum domain,
5749 int global,
5750 gdb::function_view<symbol_found_callback_ftype> callback,
5751 symbol_compare_ftype *ordered_compare)
5752{
976ca316 5753 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5754
5755 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5756 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5757 return;
5758
976ca316 5759 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5760 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5761
5762 const char *match_name = name.ada ().lookup_name ().c_str ();
5763 auto matcher = [&] (const char *symname)
5764 {
5765 if (ordered_compare == nullptr)
5766 return true;
5767 return ordered_compare (symname, match_name) == 0;
5768 };
5769
5770 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5771 [&] (offset_type namei)
5772 {
5773 /* The name was matched, now expand corresponding CUs that were
5774 marked. */
fcf23d5b 5775 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
976ca316 5776 per_objfile);
3b00ef10
TT
5777
5778 struct dwarf2_per_cu_data *per_cu;
5779 while ((per_cu = iter.next ()) != NULL)
976ca316 5780 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5781 nullptr);
3b00ef10 5782 return true;
976ca316 5783 }, per_objfile);
3b00ef10
TT
5784
5785 /* It's a shame we couldn't do this inside the
5786 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5787 that have already been expanded. Instead, this loop matches what
5788 the psymtab code does. */
976ca316 5789 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3b00ef10 5790 {
976ca316 5791 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
af758d11 5792 if (symtab != nullptr)
3b00ef10
TT
5793 {
5794 const struct block *block
af758d11 5795 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5796 if (!iterate_over_symbols_terminated (block, name,
5797 domain, callback))
5798 break;
5799 }
5800 }
5801}
5802
927aa2e7
JK
5803static void
5804dw2_debug_names_expand_symtabs_matching
5805 (struct objfile *objfile,
5806 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5807 const lookup_name_info *lookup_name,
927aa2e7
JK
5808 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5809 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5810 enum search_domain kind)
5811{
976ca316 5812 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5813
927aa2e7 5814 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5815 if (!per_objfile->per_bfd->debug_names_table)
927aa2e7 5816 return;
9291a0cd 5817
976ca316 5818 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5819
c1a66c06
TV
5820 if (symbol_matcher == NULL && lookup_name == NULL)
5821 {
976ca316 5822 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5823 {
5824 QUIT;
5825
976ca316
SM
5826 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5827 expansion_notify);
c1a66c06
TV
5828 }
5829 return;
5830 }
5831
976ca316 5832 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5833
c1a66c06 5834 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5835 symbol_matcher,
5836 kind, [&] (offset_type namei)
927aa2e7 5837 {
927aa2e7
JK
5838 /* The name was matched, now expand corresponding CUs that were
5839 marked. */
976ca316 5840 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
bbf2f4df 5841
927aa2e7
JK
5842 struct dwarf2_per_cu_data *per_cu;
5843 while ((per_cu = iter.next ()) != NULL)
976ca316
SM
5844 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5845 expansion_notify);
3b00ef10 5846 return true;
976ca316 5847 }, per_objfile);
9291a0cd
TT
5848}
5849
927aa2e7 5850const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5851{
5852 dw2_has_symbols,
5853 dw2_find_last_source_symtab,
5854 dw2_forget_cached_source_info,
f8eba3c6 5855 dw2_map_symtabs_matching_filename,
927aa2e7 5856 dw2_debug_names_lookup_symbol,
d3214198 5857 NULL,
9291a0cd 5858 dw2_print_stats,
927aa2e7 5859 dw2_debug_names_dump,
927aa2e7 5860 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5861 dw2_expand_all_symtabs,
652a8996 5862 dw2_expand_symtabs_with_fullname,
3b00ef10 5863 dw2_debug_names_map_matching_symbols,
927aa2e7 5864 dw2_debug_names_expand_symtabs_matching,
43f3e411 5865 dw2_find_pc_sect_compunit_symtab,
71a3c369 5866 NULL,
9291a0cd
TT
5867 dw2_map_symbol_filenames
5868};
5869
4485a1c1 5870/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5871 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5872
5873template <typename T>
5874static gdb::array_view<const gdb_byte>
5875get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5876{
5877 dwarf2_section_info *section = &section_owner->gdb_index;
5878
96b79293 5879 if (section->empty ())
4485a1c1
SM
5880 return {};
5881
5882 /* Older elfutils strip versions could keep the section in the main
5883 executable while splitting it for the separate debug info file. */
96b79293 5884 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5885 return {};
5886
96b79293 5887 section->read (obj);
4485a1c1 5888
8bebfcda
PA
5889 /* dwarf2_section_info::size is a bfd_size_type, while
5890 gdb::array_view works with size_t. On 32-bit hosts, with
5891 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5892 is 32-bit. So we need an explicit narrowing conversion here.
5893 This is fine, because it's impossible to allocate or mmap an
5894 array/buffer larger than what size_t can represent. */
5895 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5896}
5897
87d6a7aa
SM
5898/* Lookup the index cache for the contents of the index associated to
5899 DWARF2_OBJ. */
5900
5901static gdb::array_view<const gdb_byte>
5989a64e 5902get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5903{
5904 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5905 if (build_id == nullptr)
5906 return {};
5907
5908 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5909 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5910}
5911
5912/* Same as the above, but for DWZ. */
5913
5914static gdb::array_view<const gdb_byte>
5915get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5916{
5917 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5918 if (build_id == nullptr)
5919 return {};
5920
5921 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5922}
5923
3c0aa29a 5924/* See symfile.h. */
9291a0cd 5925
3c0aa29a
PA
5926bool
5927dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5928{
976ca316
SM
5929 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5930 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 5931
9291a0cd
TT
5932 /* If we're about to read full symbols, don't bother with the
5933 indices. In this case we also don't care if some other debug
5934 format is making psymtabs, because they are all about to be
5935 expanded anyway. */
5936 if ((objfile->flags & OBJF_READNOW))
5937 {
17ee85fc
TT
5938 /* When using READNOW, the using_index flag (set below) indicates that
5939 PER_BFD was already initialized, when we loaded some other objfile. */
5940 if (per_bfd->using_index)
5941 {
5942 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 5943 per_objfile->resize_symtabs ();
17ee85fc
TT
5944 return true;
5945 }
5946
5947 per_bfd->using_index = 1;
976ca316
SM
5948 create_all_comp_units (per_objfile);
5949 create_all_type_units (per_objfile);
17ee85fc
TT
5950 per_bfd->quick_file_names_table
5951 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 5952 per_objfile->resize_symtabs ();
9291a0cd 5953
17ee85fc
TT
5954 for (int i = 0; i < (per_bfd->all_comp_units.size ()
5955 + per_bfd->all_type_units.size ()); ++i)
9291a0cd 5956 {
17ee85fc 5957 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
9291a0cd 5958
17ee85fc 5959 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 5960 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5961 }
5962
5963 /* Return 1 so that gdb sees the "quick" functions. However,
5964 these functions will be no-ops because we will have expanded
5965 all symtabs. */
3c0aa29a
PA
5966 *index_kind = dw_index_kind::GDB_INDEX;
5967 return true;
9291a0cd
TT
5968 }
5969
17ee85fc
TT
5970 /* Was a debug names index already read when we processed an objfile sharing
5971 PER_BFD? */
5972 if (per_bfd->debug_names_table != nullptr)
5973 {
5974 *index_kind = dw_index_kind::DEBUG_NAMES;
976ca316 5975 per_objfile->resize_symtabs ();
17ee85fc
TT
5976 return true;
5977 }
5978
5979 /* Was a GDB index already read when we processed an objfile sharing
5980 PER_BFD? */
5981 if (per_bfd->index_table != nullptr)
5982 {
5983 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 5984 per_objfile->resize_symtabs ();
17ee85fc
TT
5985 return true;
5986 }
5987
976ca316 5988 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a
PA
5989 {
5990 *index_kind = dw_index_kind::DEBUG_NAMES;
976ca316 5991 per_objfile->resize_symtabs ();
3c0aa29a
PA
5992 return true;
5993 }
927aa2e7 5994
976ca316 5995 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 5996 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5997 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5998 {
5999 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6000 per_objfile->resize_symtabs ();
3c0aa29a
PA
6001 return true;
6002 }
9291a0cd 6003
87d6a7aa 6004 /* ... otherwise, try to find the index in the index cache. */
976ca316 6005 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
6006 get_gdb_index_contents_from_cache,
6007 get_gdb_index_contents_from_cache_dwz))
6008 {
6009 global_index_cache.hit ();
6010 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6011 per_objfile->resize_symtabs ();
87d6a7aa
SM
6012 return true;
6013 }
6014
6015 global_index_cache.miss ();
3c0aa29a 6016 return false;
9291a0cd
TT
6017}
6018
6019\f
6020
dce234bc
PP
6021/* Build a partial symbol table. */
6022
6023void
f29dff0a 6024dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6025{
976ca316
SM
6026 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6027 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
6028
6029 if (per_bfd->partial_symtabs != nullptr)
6030 {
6031 /* Partial symbols were already read, so now we can simply
6032 attach them. */
6033 objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6034 per_objfile->resize_symtabs ();
17ee85fc
TT
6035 return;
6036 }
c9bf0622 6037
6eee24ce 6038 init_psymbol_list (objfile, 1024);
c906108c 6039
a70b8144 6040 try
c9bf0622
TT
6041 {
6042 /* This isn't really ideal: all the data we allocate on the
6043 objfile's obstack is still uselessly kept around. However,
6044 freeing it seems unsafe. */
906768f9 6045 psymtab_discarder psymtabs (objfile);
976ca316 6046 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 6047 psymtabs.keep ();
87d6a7aa 6048
976ca316 6049 per_objfile->resize_symtabs ();
af758d11 6050
87d6a7aa 6051 /* (maybe) store an index in the cache. */
976ca316 6052 global_index_cache.store (per_objfile);
c9bf0622 6053 }
230d2906 6054 catch (const gdb_exception_error &except)
492d29ea
PA
6055 {
6056 exception_print (gdb_stderr, except);
6057 }
17ee85fc
TT
6058
6059 /* Finish by setting the local reference to partial symtabs, so that
6060 we don't try to read them again if reading another objfile with the same
6061 BFD. If we can't in fact share, this won't make a difference anyway as
6062 the dwarf2_per_bfd object won't be shared. */
6063 per_bfd->partial_symtabs = objfile->partial_symtabs;
c906108c 6064}
c906108c 6065
3b80fe9b
DE
6066/* Find the base address of the compilation unit for range lists and
6067 location lists. It will normally be specified by DW_AT_low_pc.
6068 In DWARF-3 draft 4, the base address could be overridden by
6069 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6070 compilation units with discontinuous ranges. */
6071
6072static void
6073dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6074{
6075 struct attribute *attr;
6076
2b24b6e4 6077 cu->base_address.reset ();
3b80fe9b
DE
6078
6079 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6080 if (attr != nullptr)
2b24b6e4 6081 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6082 else
6083 {
6084 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6085 if (attr != nullptr)
2b24b6e4 6086 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6087 }
6088}
6089
36586728
TT
6090/* Helper function that returns the proper abbrev section for
6091 THIS_CU. */
6092
6093static struct dwarf2_section_info *
6094get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6095{
6096 struct dwarf2_section_info *abbrev;
c3699833 6097 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6098
6099 if (this_cu->is_dwz)
c3699833 6100 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6101 else
c3699833 6102 abbrev = &per_bfd->abbrev;
36586728
TT
6103
6104 return abbrev;
6105}
6106
f4dc4d17
DE
6107/* Fetch the abbreviation table offset from a comp or type unit header. */
6108
6109static sect_offset
976ca316 6110read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 6111 struct dwarf2_section_info *section,
9c541725 6112 sect_offset sect_off)
f4dc4d17 6113{
96b79293 6114 bfd *abfd = section->get_bfd_owner ();
d521ce57 6115 const gdb_byte *info_ptr;
ac298888 6116 unsigned int initial_length_size, offset_size;
43988095 6117 uint16_t version;
f4dc4d17 6118
976ca316 6119 section->read (per_objfile->objfile);
9c541725 6120 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6121 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6122 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6123 info_ptr += initial_length_size;
6124
6125 version = read_2_bytes (abfd, info_ptr);
6126 info_ptr += 2;
6127 if (version >= 5)
6128 {
6129 /* Skip unit type and address size. */
6130 info_ptr += 2;
6131 }
6132
24aa364d 6133 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6134}
6135
b83470bf
TT
6136/* A partial symtab that is used only for include files. */
6137struct dwarf2_include_psymtab : public partial_symtab
6138{
6139 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6140 : partial_symtab (filename, objfile)
6141 {
6142 }
6143
6144 void read_symtab (struct objfile *objfile) override
6145 {
194d088f
TV
6146 /* It's an include file, no symbols to read for it.
6147 Everything is in the includer symtab. */
6148
6149 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6150 expansion of the includer psymtab. We use the dependencies[0] field to
6151 model the includer. But if we go the regular route of calling
6152 expand_psymtab here, and having expand_psymtab call expand_dependencies
6153 to expand the includer, we'll only use expand_psymtab on the includer
6154 (making it a non-toplevel psymtab), while if we expand the includer via
6155 another path, we'll use read_symtab (making it a toplevel psymtab).
6156 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6157 psymtab, and trigger read_symtab on the includer here directly. */
6158 includer ()->read_symtab (objfile);
b83470bf
TT
6159 }
6160
6161 void expand_psymtab (struct objfile *objfile) override
6162 {
194d088f
TV
6163 /* This is not called by read_symtab, and should not be called by any
6164 expand_dependencies. */
6165 gdb_assert (false);
b83470bf
TT
6166 }
6167
5717c425 6168 bool readin_p (struct objfile *objfile) const override
b83470bf 6169 {
5717c425 6170 return includer ()->readin_p (objfile);
b83470bf
TT
6171 }
6172
5717c425 6173 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6174 {
6175 return nullptr;
6176 }
6177
6178private:
194d088f
TV
6179 partial_symtab *includer () const
6180 {
6181 /* An include psymtab has exactly one dependency: the psymtab that
6182 includes it. */
6183 gdb_assert (this->number_of_dependencies == 1);
6184 return this->dependencies[0];
6185 }
b83470bf
TT
6186};
6187
aaa75496
JB
6188/* Allocate a new partial symtab for file named NAME and mark this new
6189 partial symtab as being an include of PST. */
6190
6191static void
891813be 6192dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6193 struct objfile *objfile)
6194{
b83470bf 6195 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6196
fbd9ab74 6197 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6198 subpst->dirname = pst->dirname;
fbd9ab74 6199
a9342b62 6200 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6201 subpst->dependencies[0] = pst;
6202 subpst->number_of_dependencies = 1;
aaa75496
JB
6203}
6204
6205/* Read the Line Number Program data and extract the list of files
6206 included by the source file represented by PST. Build an include
d85a05f0 6207 partial symtab for each of these included files. */
aaa75496
JB
6208
6209static void
6210dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6211 struct die_info *die,
891813be 6212 dwarf2_psymtab *pst)
aaa75496 6213{
fff8551c 6214 line_header_up lh;
d85a05f0 6215 struct attribute *attr;
aaa75496 6216
d85a05f0 6217 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6218 if (attr != nullptr)
9c541725 6219 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6220 if (lh == NULL)
6221 return; /* No linetable, so no includes. */
6222
79748972
TT
6223 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6224 that we pass in the raw text_low here; that is ok because we're
6225 only decoding the line table to make include partial symtabs, and
6226 so the addresses aren't really used. */
4ae976d1 6227 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6228 pst->raw_text_low (), 1);
aaa75496
JB
6229}
6230
348e048f 6231static hashval_t
52dc124a 6232hash_signatured_type (const void *item)
348e048f 6233{
9a3c8263
SM
6234 const struct signatured_type *sig_type
6235 = (const struct signatured_type *) item;
9a619af0 6236
348e048f 6237 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6238 return sig_type->signature;
348e048f
DE
6239}
6240
6241static int
52dc124a 6242eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6243{
9a3c8263
SM
6244 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6245 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6246
348e048f
DE
6247 return lhs->signature == rhs->signature;
6248}
6249
1fd400ff
TT
6250/* Allocate a hash table for signatured types. */
6251
b0b6a987 6252static htab_up
298e9637 6253allocate_signatured_type_table ()
1fd400ff 6254{
b0b6a987
TT
6255 return htab_up (htab_create_alloc (41,
6256 hash_signatured_type,
6257 eq_signatured_type,
6258 NULL, xcalloc, xfree));
1fd400ff
TT
6259}
6260
d467dd73 6261/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6262
6263static int
d467dd73 6264add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6265{
9a3c8263 6266 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6267 std::vector<signatured_type *> *all_type_units
6268 = (std::vector<signatured_type *> *) datum;
1fd400ff 6269
b2bdb8cf 6270 all_type_units->push_back (sigt);
1fd400ff
TT
6271
6272 return 1;
6273}
6274
78d4d2c5 6275/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6276 and fill them into TYPES_HTAB. It will process only type units,
6277 therefore DW_UT_type. */
c88ee1f0 6278
78d4d2c5 6279static void
976ca316 6280create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6281 struct dwo_file *dwo_file,
b0b6a987 6282 dwarf2_section_info *section, htab_up &types_htab,
43988095 6283 rcuh_kind section_kind)
348e048f 6284{
976ca316 6285 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6286 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6287 bfd *abfd;
6288 const gdb_byte *info_ptr, *end_ptr;
348e048f 6289
4bdcc0c1
DE
6290 abbrev_section = (dwo_file != NULL
6291 ? &dwo_file->sections.abbrev
976ca316 6292 : &per_objfile->per_bfd->abbrev);
4bdcc0c1 6293
b4f54984 6294 if (dwarf_read_debug)
43988095 6295 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6296 section->get_name (),
6297 abbrev_section->get_file_name ());
09406207 6298
96b79293 6299 section->read (objfile);
78d4d2c5 6300 info_ptr = section->buffer;
348e048f 6301
78d4d2c5
JK
6302 if (info_ptr == NULL)
6303 return;
348e048f 6304
78d4d2c5
JK
6305 /* We can't set abfd until now because the section may be empty or
6306 not present, in which case the bfd is unknown. */
96b79293 6307 abfd = section->get_bfd_owner ();
348e048f 6308
c0ab21c2
TT
6309 /* We don't use cutu_reader here because we don't need to read
6310 any dies: the signature is in the header. */
3019eac3 6311
78d4d2c5
JK
6312 end_ptr = info_ptr + section->size;
6313 while (info_ptr < end_ptr)
6314 {
78d4d2c5
JK
6315 struct signatured_type *sig_type;
6316 struct dwo_unit *dwo_tu;
6317 void **slot;
6318 const gdb_byte *ptr = info_ptr;
6319 struct comp_unit_head header;
6320 unsigned int length;
8b70b953 6321
9c541725 6322 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6323
a49dd8dd
JK
6324 /* Initialize it due to a false compiler warning. */
6325 header.signature = -1;
9c541725 6326 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6327
78d4d2c5
JK
6328 /* We need to read the type's signature in order to build the hash
6329 table, but we don't need anything else just yet. */
348e048f 6330
976ca316 6331 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6332 abbrev_section, ptr, section_kind);
348e048f 6333
4057dfde 6334 length = header.get_length ();
6caca83c 6335
78d4d2c5
JK
6336 /* Skip dummy type units. */
6337 if (ptr >= info_ptr + length
43988095
JK
6338 || peek_abbrev_code (abfd, ptr) == 0
6339 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6340 {
6341 info_ptr += length;
6342 continue;
6343 }
dee91e82 6344
78d4d2c5
JK
6345 if (types_htab == NULL)
6346 {
6347 if (dwo_file)
298e9637 6348 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6349 else
298e9637 6350 types_htab = allocate_signatured_type_table ();
78d4d2c5 6351 }
8b70b953 6352
78d4d2c5
JK
6353 if (dwo_file)
6354 {
6355 sig_type = NULL;
976ca316 6356 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
78d4d2c5 6357 dwo_tu->dwo_file = dwo_file;
43988095 6358 dwo_tu->signature = header.signature;
9c541725 6359 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6360 dwo_tu->section = section;
9c541725 6361 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6362 dwo_tu->length = length;
6363 }
6364 else
6365 {
6366 /* N.B.: type_offset is not usable if this type uses a DWO file.
6367 The real type_offset is in the DWO file. */
6368 dwo_tu = NULL;
976ca316 6369 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
43988095 6370 sig_type->signature = header.signature;
9c541725 6371 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6372 sig_type->per_cu.is_debug_types = 1;
6373 sig_type->per_cu.section = section;
9c541725 6374 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6375 sig_type->per_cu.length = length;
6376 }
6377
b0b6a987 6378 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6379 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6380 INSERT);
6381 gdb_assert (slot != NULL);
6382 if (*slot != NULL)
6383 {
9c541725 6384 sect_offset dup_sect_off;
0349ea22 6385
3019eac3
DE
6386 if (dwo_file)
6387 {
78d4d2c5
JK
6388 const struct dwo_unit *dup_tu
6389 = (const struct dwo_unit *) *slot;
6390
9c541725 6391 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6392 }
6393 else
6394 {
78d4d2c5
JK
6395 const struct signatured_type *dup_tu
6396 = (const struct signatured_type *) *slot;
6397
9c541725 6398 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6399 }
8b70b953 6400
b98664d3 6401 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6402 " the entry at offset %s, signature %s"),
6403 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6404 hex_string (header.signature));
78d4d2c5
JK
6405 }
6406 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6407
78d4d2c5 6408 if (dwarf_read_debug > 1)
9d8780f0
SM
6409 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6410 sect_offset_str (sect_off),
43988095 6411 hex_string (header.signature));
3019eac3 6412
78d4d2c5
JK
6413 info_ptr += length;
6414 }
6415}
3019eac3 6416
78d4d2c5
JK
6417/* Create the hash table of all entries in the .debug_types
6418 (or .debug_types.dwo) section(s).
6419 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6420 otherwise it is NULL.
b3c8eb43 6421
78d4d2c5 6422 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6423
78d4d2c5 6424 Note: This function processes DWO files only, not DWP files. */
348e048f 6425
78d4d2c5 6426static void
976ca316 6427create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6428 struct dwo_file *dwo_file,
fd5866f6 6429 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6430 htab_up &types_htab)
78d4d2c5 6431{
fd5866f6 6432 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6433 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6434 rcuh_kind::TYPE);
3019eac3
DE
6435}
6436
6437/* Create the hash table of all entries in the .debug_types section,
6438 and initialize all_type_units.
6439 The result is zero if there is an error (e.g. missing .debug_types section),
6440 otherwise non-zero. */
6441
6442static int
976ca316 6443create_all_type_units (dwarf2_per_objfile *per_objfile)
3019eac3 6444{
b0b6a987 6445 htab_up types_htab;
3019eac3 6446
976ca316
SM
6447 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6448 types_htab, rcuh_kind::COMPILE);
6449 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6450 types_htab);
3019eac3
DE
6451 if (types_htab == NULL)
6452 {
976ca316 6453 per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6454 return 0;
6455 }
6456
976ca316 6457 per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6458
976ca316
SM
6459 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6460 per_objfile->per_bfd->all_type_units.reserve
6461 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6462
976ca316 6463 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6464 add_signatured_type_cu_to_table,
976ca316 6465 &per_objfile->per_bfd->all_type_units);
1fd400ff 6466
348e048f
DE
6467 return 1;
6468}
6469
5989a64e 6470/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6471 If SLOT is non-NULL, it is the entry to use in the hash table.
6472 Otherwise we find one. */
6473
6474static struct signatured_type *
976ca316 6475add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6476{
976ca316
SM
6477 if (per_objfile->per_bfd->all_type_units.size ()
6478 == per_objfile->per_bfd->all_type_units.capacity ())
6479 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6480
976ca316 6481 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6482
976ca316 6483 per_objfile->resize_symtabs ();
af758d11 6484
976ca316 6485 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6486 sig_type->signature = sig;
6487 sig_type->per_cu.is_debug_types = 1;
976ca316 6488 if (per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6489 {
6490 sig_type->per_cu.v.quick =
976ca316 6491 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6492 struct dwarf2_per_cu_quick_data);
6493 }
6494
6495 if (slot == NULL)
6496 {
976ca316 6497 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6498 sig_type, INSERT);
6499 }
6500 gdb_assert (*slot == NULL);
6501 *slot = sig_type;
6502 /* The rest of sig_type must be filled in by the caller. */
6503 return sig_type;
6504}
6505
a2ce51a0
DE
6506/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6507 Fill in SIG_ENTRY with DWO_ENTRY. */
6508
6509static void
976ca316 6510fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6511 struct signatured_type *sig_entry,
6512 struct dwo_unit *dwo_entry)
6513{
976ca316 6514 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6515
7ee85ab1 6516 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6517 gdb_assert (! sig_entry->per_cu.queued);
976ca316 6518 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6519 if (per_bfd->using_index)
6aa5f3a6
DE
6520 {
6521 gdb_assert (sig_entry->per_cu.v.quick != NULL);
976ca316 6522 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6523 }
6524 else
6525 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6526 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6527 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6528 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6529 gdb_assert (sig_entry->dwo_unit == NULL);
6530
6531 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6532 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6533 sig_entry->per_cu.length = dwo_entry->length;
6534 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6535 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6536 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6537 sig_entry->dwo_unit = dwo_entry;
6538}
6539
6540/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6541 If we haven't read the TU yet, create the signatured_type data structure
6542 for a TU to be read in directly from a DWO file, bypassing the stub.
6543 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6544 using .gdb_index, then when reading a CU we want to stay in the DWO file
6545 containing that CU. Otherwise we could end up reading several other DWO
6546 files (due to comdat folding) to process the transitive closure of all the
6547 mentioned TUs, and that can be slow. The current DWO file will have every
6548 type signature that it needs.
a2ce51a0
DE
6549 We only do this for .gdb_index because in the psymtab case we already have
6550 to read all the DWOs to build the type unit groups. */
6551
6552static struct signatured_type *
6553lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6554{
976ca316 6555 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6556 struct dwo_file *dwo_file;
6557 struct dwo_unit find_dwo_entry, *dwo_entry;
6558 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6559 void **slot;
a2ce51a0 6560
976ca316 6561 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6562
6aa5f3a6
DE
6563 /* If TU skeletons have been removed then we may not have read in any
6564 TUs yet. */
976ca316
SM
6565 if (per_objfile->per_bfd->signatured_types == NULL)
6566 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6567
6568 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6569 Use the global signatured_types array to do our own comdat-folding
6570 of types. If this is the first time we're reading this TU, and
6571 the TU has an entry in .gdb_index, replace the recorded data from
6572 .gdb_index with this TU. */
a2ce51a0 6573
a2ce51a0 6574 find_sig_entry.signature = sig;
976ca316 6575 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6576 &find_sig_entry, INSERT);
9a3c8263 6577 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6578
6579 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6580 read. Don't reassign the global entry to point to this DWO if that's
6581 the case. Also note that if the TU is already being read, it may not
6582 have come from a DWO, the program may be a mix of Fission-compiled
6583 code and non-Fission-compiled code. */
6584
6585 /* Have we already tried to read this TU?
6586 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6587 needn't exist in the global table yet). */
6588 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6589 return sig_entry;
6590
6aa5f3a6
DE
6591 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6592 dwo_unit of the TU itself. */
6593 dwo_file = cu->dwo_unit->dwo_file;
6594
a2ce51a0
DE
6595 /* Ok, this is the first time we're reading this TU. */
6596 if (dwo_file->tus == NULL)
6597 return NULL;
6598 find_dwo_entry.signature = sig;
b0b6a987
TT
6599 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6600 &find_dwo_entry);
a2ce51a0
DE
6601 if (dwo_entry == NULL)
6602 return NULL;
6603
6aa5f3a6
DE
6604 /* If the global table doesn't have an entry for this TU, add one. */
6605 if (sig_entry == NULL)
976ca316 6606 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6607
976ca316 6608 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
89e63ee4 6609 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6610 return sig_entry;
6611}
6612
a2ce51a0
DE
6613/* Subroutine of lookup_signatured_type.
6614 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6615 then try the DWP file. If the TU stub (skeleton) has been removed then
6616 it won't be in .gdb_index. */
a2ce51a0
DE
6617
6618static struct signatured_type *
6619lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6620{
976ca316
SM
6621 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6622 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6623 struct dwo_unit *dwo_entry;
6624 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6625 void **slot;
a2ce51a0 6626
976ca316 6627 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6628 gdb_assert (dwp_file != NULL);
6629
6aa5f3a6
DE
6630 /* If TU skeletons have been removed then we may not have read in any
6631 TUs yet. */
976ca316
SM
6632 if (per_objfile->per_bfd->signatured_types == NULL)
6633 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6634
6aa5f3a6 6635 find_sig_entry.signature = sig;
976ca316 6636 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6637 &find_sig_entry, INSERT);
9a3c8263 6638 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6639
6640 /* Have we already tried to read this TU?
6641 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6642 needn't exist in the global table yet). */
6643 if (sig_entry != NULL)
6644 return sig_entry;
6645
a2ce51a0
DE
6646 if (dwp_file->tus == NULL)
6647 return NULL;
976ca316
SM
6648 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6649 1 /* is_debug_types */);
a2ce51a0
DE
6650 if (dwo_entry == NULL)
6651 return NULL;
6652
976ca316
SM
6653 sig_entry = add_type_unit (per_objfile, sig, slot);
6654 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6655
a2ce51a0
DE
6656 return sig_entry;
6657}
6658
380bca97 6659/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6660 Returns NULL if signature SIG is not present in the table.
6661 It is up to the caller to complain about this. */
348e048f
DE
6662
6663static struct signatured_type *
a2ce51a0 6664lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6665{
976ca316 6666 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6667
976ca316 6668 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6669 {
6670 /* We're in a DWO/DWP file, and we're using .gdb_index.
6671 These cases require special processing. */
976ca316 6672 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6673 return lookup_dwo_signatured_type (cu, sig);
6674 else
6675 return lookup_dwp_signatured_type (cu, sig);
6676 }
6677 else
6678 {
6679 struct signatured_type find_entry, *entry;
348e048f 6680
976ca316 6681 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6682 return NULL;
6683 find_entry.signature = sig;
9a3c8263 6684 entry = ((struct signatured_type *)
976ca316 6685 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6686 &find_entry));
a2ce51a0
DE
6687 return entry;
6688 }
348e048f 6689}
18a8505e 6690
42e7ad6c 6691/* Low level DIE reading support. */
348e048f 6692
d85a05f0
DJ
6693/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6694
6695static void
6696init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6697 struct dwarf2_cu *cu,
3019eac3 6698 struct dwarf2_section_info *section,
685af9cd
TT
6699 struct dwo_file *dwo_file,
6700 struct abbrev_table *abbrev_table)
d85a05f0 6701{
fceca515 6702 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6703 reader->abfd = section->get_bfd_owner ();
d85a05f0 6704 reader->cu = cu;
3019eac3 6705 reader->dwo_file = dwo_file;
dee91e82
DE
6706 reader->die_section = section;
6707 reader->buffer = section->buffer;
f664829e 6708 reader->buffer_end = section->buffer + section->size;
685af9cd 6709 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6710}
6711
c0ab21c2 6712/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6713 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6714 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6715 already.
6716
6717 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6718 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6719 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6720 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6721 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6722 STUB_COMP_DIR may be non-NULL.
3e225074 6723 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6724 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6725 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6726 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6727 kept around for at least as long as *RESULT_READER.
6728
b0c7bfa9
DE
6729 The result is non-zero if a valid (non-dummy) DIE was found. */
6730
6731static int
4ab09049 6732read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6733 struct dwo_unit *dwo_unit,
b0c7bfa9 6734 struct die_info *stub_comp_unit_die,
a2ce51a0 6735 const char *stub_comp_dir,
b0c7bfa9 6736 struct die_reader_specs *result_reader,
d521ce57 6737 const gdb_byte **result_info_ptr,
b0c7bfa9 6738 struct die_info **result_comp_unit_die,
685af9cd 6739 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6740{
976ca316 6741 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6742 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6743 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6744 bfd *abfd;
d521ce57 6745 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6746 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6747 int i,num_extra_attrs;
6748 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6749 struct die_info *comp_unit_die;
6750
b0aeadb3
DE
6751 /* At most one of these may be provided. */
6752 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6753
b0c7bfa9
DE
6754 /* These attributes aren't processed until later:
6755 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6756 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6757 referenced later. However, these attributes are found in the stub
6758 which we won't have later. In order to not impose this complication
6759 on the rest of the code, we read them here and copy them to the
6760 DWO CU/TU die. */
b0c7bfa9
DE
6761
6762 stmt_list = NULL;
6763 low_pc = NULL;
6764 high_pc = NULL;
6765 ranges = NULL;
6766 comp_dir = NULL;
6767
6768 if (stub_comp_unit_die != NULL)
6769 {
6770 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6771 DWO file. */
4ab09049 6772 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6773 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6774 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6775 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6776 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6777 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6778
a39fdb41 6779 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6780
18a8505e
AT
6781 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6782 here (if needed). We need the value before we can process
6783 DW_AT_ranges. */
a39fdb41 6784 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6785 }
a2ce51a0
DE
6786 else if (stub_comp_dir != NULL)
6787 {
6788 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6789 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6790 comp_dir->name = DW_AT_comp_dir;
6791 comp_dir->form = DW_FORM_string;
6792 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6793 DW_STRING (comp_dir) = stub_comp_dir;
6794 }
b0c7bfa9
DE
6795
6796 /* Set up for reading the DWO CU/TU. */
6797 cu->dwo_unit = dwo_unit;
685af9cd 6798 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6799 section->read (objfile);
6800 abfd = section->get_bfd_owner ();
9c541725
PA
6801 begin_info_ptr = info_ptr = (section->buffer
6802 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6803 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6804
4ab09049 6805 if (per_cu->is_debug_types)
b0c7bfa9 6806 {
4ab09049 6807 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6808
976ca316
SM
6809 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6810 section, dwo_abbrev_section,
43988095 6811 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6812 /* This is not an assert because it can be caused by bad debug info. */
43988095 6813 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6814 {
6815 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6816 " TU at offset %s [in module %s]"),
a2ce51a0 6817 hex_string (sig_type->signature),
43988095 6818 hex_string (cu->header.signature),
9d8780f0 6819 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6820 bfd_get_filename (abfd));
6821 }
9c541725 6822 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6823 /* For DWOs coming from DWP files, we don't know the CU length
6824 nor the type's offset in the TU until now. */
4057dfde 6825 dwo_unit->length = cu->header.get_length ();
9c541725 6826 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6827
6828 /* Establish the type offset that can be used to lookup the type.
6829 For DWO files, we don't know it until now. */
9c541725
PA
6830 sig_type->type_offset_in_section
6831 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6832 }
6833 else
6834 {
976ca316
SM
6835 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6836 section, dwo_abbrev_section,
43988095 6837 info_ptr, rcuh_kind::COMPILE);
9c541725 6838 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6839 /* For DWOs coming from DWP files, we don't know the CU length
6840 until now. */
4057dfde 6841 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6842 }
6843
685af9cd 6844 *result_dwo_abbrev_table
86de1d91
TT
6845 = abbrev_table::read (objfile, dwo_abbrev_section,
6846 cu->header.abbrev_sect_off);
685af9cd
TT
6847 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6848 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6849
6850 /* Read in the die, but leave space to copy over the attributes
6851 from the stub. This has the benefit of simplifying the rest of
6852 the code - all the work to maintain the illusion of a single
6853 DW_TAG_{compile,type}_unit DIE is done here. */
6854 num_extra_attrs = ((stmt_list != NULL)
6855 + (low_pc != NULL)
6856 + (high_pc != NULL)
6857 + (ranges != NULL)
6858 + (comp_dir != NULL));
6859 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6860 num_extra_attrs);
b0c7bfa9
DE
6861
6862 /* Copy over the attributes from the stub to the DIE we just read in. */
6863 comp_unit_die = *result_comp_unit_die;
6864 i = comp_unit_die->num_attrs;
6865 if (stmt_list != NULL)
6866 comp_unit_die->attrs[i++] = *stmt_list;
6867 if (low_pc != NULL)
6868 comp_unit_die->attrs[i++] = *low_pc;
6869 if (high_pc != NULL)
6870 comp_unit_die->attrs[i++] = *high_pc;
6871 if (ranges != NULL)
6872 comp_unit_die->attrs[i++] = *ranges;
6873 if (comp_dir != NULL)
6874 comp_unit_die->attrs[i++] = *comp_dir;
6875 comp_unit_die->num_attrs += num_extra_attrs;
6876
b4f54984 6877 if (dwarf_die_debug)
bf6af496
DE
6878 {
6879 fprintf_unfiltered (gdb_stdlog,
6880 "Read die from %s@0x%x of %s:\n",
96b79293 6881 section->get_name (),
bf6af496
DE
6882 (unsigned) (begin_info_ptr - section->buffer),
6883 bfd_get_filename (abfd));
b4f54984 6884 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6885 }
6886
b0c7bfa9
DE
6887 /* Skip dummy compilation units. */
6888 if (info_ptr >= begin_info_ptr + dwo_unit->length
6889 || peek_abbrev_code (abfd, info_ptr) == 0)
6890 return 0;
6891
6892 *result_info_ptr = info_ptr;
6893 return 1;
6894}
6895
a084a2a6
AT
6896/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6897 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6898 signature is part of the header. */
6899static gdb::optional<ULONGEST>
6900lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6901{
6902 if (cu->header.version >= 5)
6903 return cu->header.signature;
6904 struct attribute *attr;
6905 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6906 if (attr == nullptr)
6907 return gdb::optional<ULONGEST> ();
6908 return DW_UNSND (attr);
6909}
6910
c0ab21c2 6911/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6912 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6913 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6914
6915static struct dwo_unit *
4ab09049 6916lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6917{
4ab09049 6918 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6919 struct dwo_unit *dwo_unit;
c0ab21c2 6920 const char *comp_dir;
b0c7bfa9 6921
a2ce51a0
DE
6922 gdb_assert (cu != NULL);
6923
b0c7bfa9 6924 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6925 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6926 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6927
4ab09049
SM
6928 if (per_cu->is_debug_types)
6929 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6930 else
6931 {
a084a2a6 6932 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6933
a084a2a6 6934 if (!signature.has_value ())
b0c7bfa9
DE
6935 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6936 " [in module %s]"),
4ab09049
SM
6937 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6938
6939 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6940 }
6941
b0c7bfa9
DE
6942 return dwo_unit;
6943}
6944
c0ab21c2 6945/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6946 See it for a description of the parameters.
fcd3b13d 6947 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6948
c0ab21c2 6949void
9e021579
SM
6950cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6951 dwarf2_per_objfile *per_objfile,
2e671100 6952 dwarf2_cu *existing_cu)
a2ce51a0 6953{
a2ce51a0 6954 struct signatured_type *sig_type;
a2ce51a0
DE
6955
6956 /* Verify we can do the following downcast, and that we have the
6957 data we need. */
6958 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6959 sig_type = (struct signatured_type *) this_cu;
6960 gdb_assert (sig_type->dwo_unit != NULL);
6961
2e671100
SM
6962 dwarf2_cu *cu;
6963
6964 if (existing_cu != nullptr)
6aa5f3a6 6965 {
2e671100
SM
6966 cu = existing_cu;
6967 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6968 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6969 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6970 }
6971 else
6972 {
7188ed02
SM
6973 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6974 in per_objfile yet. */
6975 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 6976 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 6977 cu = m_new_cu.get ();
6aa5f3a6
DE
6978 }
6979
6980 /* A future optimization, if needed, would be to use an existing
6981 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6982 could share abbrev tables. */
a2ce51a0 6983
2e671100 6984 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
6985 NULL /* stub_comp_unit_die */,
6986 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6987 this, &info_ptr,
3e225074 6988 &comp_unit_die,
c0ab21c2 6989 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6990 {
6991 /* Dummy die. */
c0ab21c2 6992 dummy_p = true;
a2ce51a0 6993 }
a2ce51a0
DE
6994}
6995
fd820528 6996/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6997 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6998
f4dc4d17
DE
6999 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7000 Otherwise the table specified in the comp unit header is read in and used.
7001 This is an optimization for when we already have the abbrev table.
7002
2e671100
SM
7003 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7004 allocated. */
aaa75496 7005
ab432490 7006cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7007 dwarf2_per_objfile *per_objfile,
c0ab21c2 7008 struct abbrev_table *abbrev_table,
2e671100 7009 dwarf2_cu *existing_cu,
c0ab21c2
TT
7010 bool skip_partial)
7011 : die_reader_specs {},
6751ebae 7012 m_this_cu (this_cu)
c906108c 7013{
976ca316 7014 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7015 struct dwarf2_section_info *section = this_cu->section;
96b79293 7016 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 7017 const gdb_byte *begin_info_ptr;
dee91e82 7018 struct signatured_type *sig_type = NULL;
4bdcc0c1 7019 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7020 /* Non-zero if CU currently points to a DWO file and we need to
7021 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7022 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7023 int rereading_dwo_cu = 0;
c906108c 7024
b4f54984 7025 if (dwarf_die_debug)
9d8780f0 7026 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7027 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7028 sect_offset_str (this_cu->sect_off));
09406207 7029
a2ce51a0
DE
7030 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7031 file (instead of going through the stub), short-circuit all of this. */
7032 if (this_cu->reading_dwo_directly)
7033 {
7034 /* Narrow down the scope of possibilities to have to understand. */
7035 gdb_assert (this_cu->is_debug_types);
7036 gdb_assert (abbrev_table == NULL);
976ca316 7037 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
7038 return;
7039 }
7040
dee91e82 7041 /* This is cheap if the section is already read in. */
96b79293 7042 section->read (objfile);
dee91e82 7043
9c541725 7044 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7045
7046 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7047
2e671100
SM
7048 dwarf2_cu *cu;
7049
7050 if (existing_cu != nullptr)
dee91e82 7051 {
2e671100 7052 cu = existing_cu;
42e7ad6c
DE
7053 /* If this CU is from a DWO file we need to start over, we need to
7054 refetch the attributes from the skeleton CU.
7055 This could be optimized by retrieving those attributes from when we
7056 were here the first time: the previous comp_unit_die was stored in
7057 comp_unit_obstack. But there's no data yet that we need this
7058 optimization. */
7059 if (cu->dwo_unit != NULL)
7060 rereading_dwo_cu = 1;
dee91e82
DE
7061 }
7062 else
7063 {
7188ed02
SM
7064 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7065 in per_objfile yet. */
976ca316
SM
7066 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7067 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 7068 cu = m_new_cu.get ();
42e7ad6c 7069 }
dee91e82 7070
b0c7bfa9 7071 /* Get the header. */
9c541725 7072 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7073 {
7074 /* We already have the header, there's no need to read it in again. */
9c541725 7075 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7076 }
7077 else
7078 {
3019eac3 7079 if (this_cu->is_debug_types)
dee91e82 7080 {
976ca316
SM
7081 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7082 section, abbrev_section,
7083 info_ptr, rcuh_kind::TYPE);
dee91e82 7084
42e7ad6c
DE
7085 /* Since per_cu is the first member of struct signatured_type,
7086 we can go from a pointer to one to a pointer to the other. */
7087 sig_type = (struct signatured_type *) this_cu;
43988095 7088 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7089 gdb_assert (sig_type->type_offset_in_tu
7090 == cu->header.type_cu_offset_in_tu);
7091 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7092
42e7ad6c
DE
7093 /* LENGTH has not been set yet for type units if we're
7094 using .gdb_index. */
4057dfde 7095 this_cu->length = cu->header.get_length ();
3019eac3
DE
7096
7097 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7098 sig_type->type_offset_in_section =
7099 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7100
7101 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7102 }
7103 else
7104 {
976ca316
SM
7105 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7106 section, abbrev_section,
43988095
JK
7107 info_ptr,
7108 rcuh_kind::COMPILE);
dee91e82 7109
9c541725 7110 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7111 if (this_cu->length == 0)
7112 this_cu->length = cu->header.get_length ();
7113 else
7114 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7115 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7116 }
7117 }
10b3939b 7118
6caca83c 7119 /* Skip dummy compilation units. */
dee91e82 7120 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7121 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7122 {
7123 dummy_p = true;
7124 return;
7125 }
6caca83c 7126
433df2d4
DE
7127 /* If we don't have them yet, read the abbrevs for this compilation unit.
7128 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7129 done. */
f4dc4d17 7130 if (abbrev_table != NULL)
685af9cd
TT
7131 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7132 else
f4dc4d17 7133 {
c0ab21c2 7134 m_abbrev_table_holder
86de1d91
TT
7135 = abbrev_table::read (objfile, abbrev_section,
7136 cu->header.abbrev_sect_off);
c0ab21c2 7137 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7138 }
af703f96 7139
dee91e82 7140 /* Read the top level CU/TU die. */
c0ab21c2 7141 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7142 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7143
58f0c718 7144 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7145 {
7146 dummy_p = true;
7147 return;
7148 }
58f0c718 7149
b0c7bfa9 7150 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7151 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7152 table from the DWO file and pass the ownership over to us. It will be
7153 referenced from READER, so we must make sure to free it after we're done
7154 with READER.
7155
b0c7bfa9
DE
7156 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7157 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7158 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7159 if (dwo_name != nullptr)
3019eac3 7160 {
3019eac3 7161 struct dwo_unit *dwo_unit;
b0c7bfa9 7162 struct die_info *dwo_comp_unit_die;
3019eac3 7163
3e225074 7164 if (comp_unit_die->has_children)
6a506a2d 7165 {
b98664d3 7166 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7167 " has children (offset %s) [in module %s]"),
7168 sect_offset_str (this_cu->sect_off),
7169 bfd_get_filename (abfd));
6a506a2d 7170 }
4ab09049 7171 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7172 if (dwo_unit != NULL)
3019eac3 7173 {
4ab09049 7174 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7175 comp_unit_die, NULL,
c0ab21c2 7176 this, &info_ptr,
3e225074 7177 &dwo_comp_unit_die,
c0ab21c2 7178 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7179 {
7180 /* Dummy die. */
c0ab21c2 7181 dummy_p = true;
6a506a2d
DE
7182 return;
7183 }
7184 comp_unit_die = dwo_comp_unit_die;
7185 }
7186 else
7187 {
7188 /* Yikes, we couldn't find the rest of the DIE, we only have
7189 the stub. A complaint has already been logged. There's
7190 not much more we can do except pass on the stub DIE to
7191 die_reader_func. We don't want to throw an error on bad
7192 debug info. */
3019eac3
DE
7193 }
7194 }
c0ab21c2 7195}
3019eac3 7196
6751ebae
TT
7197void
7198cutu_reader::keep ()
c0ab21c2 7199{
b0c7bfa9 7200 /* Done, clean up. */
6751ebae
TT
7201 gdb_assert (!dummy_p);
7202 if (m_new_cu != NULL)
348e048f 7203 {
7188ed02
SM
7204 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7205 now. */
7206 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7207 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 7208 }
dee91e82
DE
7209}
7210
18a8505e
AT
7211/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7212 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7213 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7214
7215 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7216 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7217
7218 We fill in THIS_CU->length.
7219
dee91e82 7220 THIS_CU->cu is always freed when done.
3019eac3 7221 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7222 to care whether it refers to the "main" CU or the DWO CU.
7223
7224 When parent_cu is passed, it is used to provide a default value for
7225 str_offsets_base and addr_base from the parent. */
dee91e82 7226
ab432490 7227cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7228 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
7229 struct dwarf2_cu *parent_cu,
7230 struct dwo_file *dwo_file)
7231 : die_reader_specs {},
7232 m_this_cu (this_cu)
dee91e82 7233{
976ca316 7234 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7235 struct dwarf2_section_info *section = this_cu->section;
96b79293 7236 bfd *abfd = section->get_bfd_owner ();
33e80786 7237 struct dwarf2_section_info *abbrev_section;
d521ce57 7238 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7239
b4f54984 7240 if (dwarf_die_debug)
9d8780f0 7241 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7242 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7243 sect_offset_str (this_cu->sect_off));
09406207 7244
976ca316 7245 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7246
33e80786
DE
7247 abbrev_section = (dwo_file != NULL
7248 ? &dwo_file->sections.abbrev
7249 : get_abbrev_section_for_cu (this_cu));
7250
dee91e82 7251 /* This is cheap if the section is already read in. */
96b79293 7252 section->read (objfile);
dee91e82 7253
976ca316 7254 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 7255
9c541725 7256 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
7257 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7258 section, abbrev_section, info_ptr,
43988095
JK
7259 (this_cu->is_debug_types
7260 ? rcuh_kind::TYPE
7261 : rcuh_kind::COMPILE));
dee91e82 7262
18a8505e
AT
7263 if (parent_cu != nullptr)
7264 {
c0ab21c2
TT
7265 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7266 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7267 }
4057dfde 7268 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7269
7270 /* Skip dummy compilation units. */
7271 if (info_ptr >= begin_info_ptr + this_cu->length
7272 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7273 {
7274 dummy_p = true;
7275 return;
7276 }
72bf9492 7277
c0ab21c2 7278 m_abbrev_table_holder
86de1d91
TT
7279 = abbrev_table::read (objfile, abbrev_section,
7280 m_new_cu->header.abbrev_sect_off);
dee91e82 7281
c0ab21c2
TT
7282 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7283 m_abbrev_table_holder.get ());
3e225074 7284 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7285}
7286
0018ea6f
DE
7287\f
7288/* Type Unit Groups.
dee91e82 7289
0018ea6f
DE
7290 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7291 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7292 so that all types coming from the same compilation (.o file) are grouped
7293 together. A future step could be to put the types in the same symtab as
7294 the CU the types ultimately came from. */
ff013f42 7295
f4dc4d17
DE
7296static hashval_t
7297hash_type_unit_group (const void *item)
7298{
9a3c8263
SM
7299 const struct type_unit_group *tu_group
7300 = (const struct type_unit_group *) item;
f4dc4d17 7301
094b34ac 7302 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7303}
348e048f
DE
7304
7305static int
f4dc4d17 7306eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7307{
9a3c8263
SM
7308 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7309 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7310
094b34ac 7311 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7312}
348e048f 7313
f4dc4d17
DE
7314/* Allocate a hash table for type unit groups. */
7315
eaa5fa8b 7316static htab_up
298e9637 7317allocate_type_unit_groups_table ()
f4dc4d17 7318{
eaa5fa8b
TT
7319 return htab_up (htab_create_alloc (3,
7320 hash_type_unit_group,
7321 eq_type_unit_group,
7322 NULL, xcalloc, xfree));
f4dc4d17 7323}
dee91e82 7324
f4dc4d17
DE
7325/* Type units that don't have DW_AT_stmt_list are grouped into their own
7326 partial symtabs. We combine several TUs per psymtab to not let the size
7327 of any one psymtab grow too big. */
7328#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7329#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7330
094b34ac 7331/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7332 Create the type_unit_group object used to hold one or more TUs. */
7333
7334static struct type_unit_group *
094b34ac 7335create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7336{
976ca316
SM
7337 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7338 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
094b34ac 7339 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7340 struct type_unit_group *tu_group;
f4dc4d17 7341
976ca316 7342 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
094b34ac 7343 per_cu = &tu_group->per_cu;
1859c670 7344 per_cu->per_bfd = per_bfd;
f4dc4d17 7345
1859c670 7346 if (per_bfd->using_index)
094b34ac 7347 {
1859c670 7348 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7349 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7350 }
7351 else
7352 {
9c541725 7353 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7354 dwarf2_psymtab *pst;
528e1572 7355 std::string name;
094b34ac
DE
7356
7357 /* Give the symtab a useful name for debug purposes. */
7358 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7359 name = string_printf ("<type_units_%d>",
7360 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7361 else
528e1572 7362 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7363
976ca316 7364 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
6d94535f 7365 pst->anonymous = true;
094b34ac 7366 }
f4dc4d17 7367
094b34ac 7368 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7369 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7370
7371 return tu_group;
7372}
7373
094b34ac
DE
7374/* Look up the type_unit_group for type unit CU, and create it if necessary.
7375 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7376
7377static struct type_unit_group *
ff39bb5e 7378get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7379{
976ca316
SM
7380 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7381 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7382 struct type_unit_group *tu_group;
7383 void **slot;
7384 unsigned int line_offset;
7385 struct type_unit_group type_unit_group_for_lookup;
7386
976ca316
SM
7387 if (per_objfile->per_bfd->type_unit_groups == NULL)
7388 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7389
7390 /* Do we need to create a new group, or can we use an existing one? */
7391
7392 if (stmt_list)
7393 {
7394 line_offset = DW_UNSND (stmt_list);
7395 ++tu_stats->nr_symtab_sharers;
7396 }
7397 else
7398 {
7399 /* Ugh, no stmt_list. Rare, but we have to handle it.
7400 We can do various things here like create one group per TU or
7401 spread them over multiple groups to split up the expansion work.
7402 To avoid worst case scenarios (too many groups or too large groups)
7403 we, umm, group them in bunches. */
7404 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7405 | (tu_stats->nr_stmt_less_type_units
7406 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7407 ++tu_stats->nr_stmt_less_type_units;
7408 }
7409
094b34ac 7410 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7411 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7412 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7413 &type_unit_group_for_lookup, INSERT);
7414 if (*slot != NULL)
7415 {
9a3c8263 7416 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7417 gdb_assert (tu_group != NULL);
7418 }
7419 else
7420 {
9c541725 7421 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7422 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7423 *slot = tu_group;
7424 ++tu_stats->nr_symtabs;
7425 }
7426
7427 return tu_group;
7428}
0018ea6f
DE
7429\f
7430/* Partial symbol tables. */
7431
7432/* Create a psymtab named NAME and assign it to PER_CU.
7433
7434 The caller must fill in the following details:
7435 dirname, textlow, texthigh. */
7436
891813be 7437static dwarf2_psymtab *
7aa104c4
SM
7438create_partial_symtab (dwarf2_per_cu_data *per_cu,
7439 dwarf2_per_objfile *per_objfile,
7440 const char *name)
0018ea6f 7441{
7aa104c4 7442 struct objfile *objfile = per_objfile->objfile;
891813be 7443 dwarf2_psymtab *pst;
0018ea6f 7444
9f4e76a4 7445 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7446
6d94535f 7447 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7448
7449 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7450 per_cu->v.psymtab = pst;
7451
7452 return pst;
7453}
7454
c0ab21c2 7455/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7456
7457static void
7458process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7459 const gdb_byte *info_ptr,
0018ea6f 7460 struct die_info *comp_unit_die,
c0ab21c2 7461 enum language pretend_language)
0018ea6f
DE
7462{
7463 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7464 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7465 struct objfile *objfile = per_objfile->objfile;
08feed99 7466 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7467 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7468 CORE_ADDR baseaddr;
7469 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7470 dwarf2_psymtab *pst;
3a2b436a 7471 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7472 const char *filename;
0018ea6f 7473
0018ea6f
DE
7474 gdb_assert (! per_cu->is_debug_types);
7475
c0ab21c2 7476 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7477
0018ea6f 7478 /* Allocate a new partial symbol table structure. */
2e927613
TV
7479 gdb::unique_xmalloc_ptr<char> debug_filename;
7480 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7481 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7482 if (filename == NULL)
0018ea6f 7483 filename = "";
2e927613
TV
7484 else if (strcmp (filename, artificial) == 0)
7485 {
7486 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7487 sect_offset_str (per_cu->sect_off),
7488 (char *) NULL));
2e927613
TV
7489 filename = debug_filename.get ();
7490 }
0018ea6f 7491
7aa104c4 7492 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7493
7494 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7495 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7496
b3b3bada 7497 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7498
7499 dwarf2_find_base_address (comp_unit_die, cu);
7500
7501 /* Possibly set the default values of LOWPC and HIGHPC from
7502 `DW_AT_ranges'. */
3a2b436a
JK
7503 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7504 &best_highpc, cu, pst);
7505 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7506 {
7507 CORE_ADDR low
7508 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7509 - baseaddr);
7510 CORE_ADDR high
7511 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7512 - baseaddr - 1);
7513 /* Store the contiguous range if it is not empty; it can be
7514 empty for CUs with no code. */
d320c2b5
TT
7515 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7516 low, high, pst);
79748972 7517 }
0018ea6f
DE
7518
7519 /* Check if comp unit has_children.
7520 If so, read the rest of the partial symbols from this comp unit.
7521 If not, there's no more debug_info for this comp unit. */
3e225074 7522 if (comp_unit_die->has_children)
0018ea6f
DE
7523 {
7524 struct partial_die_info *first_die;
7525 CORE_ADDR lowpc, highpc;
7526
7527 lowpc = ((CORE_ADDR) -1);
7528 highpc = ((CORE_ADDR) 0);
7529
7530 first_die = load_partial_dies (reader, info_ptr, 1);
7531
7532 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7533 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7534
7535 /* If we didn't find a lowpc, set it to highpc to avoid
7536 complaints from `maint check'. */
7537 if (lowpc == ((CORE_ADDR) -1))
7538 lowpc = highpc;
7539
7540 /* If the compilation unit didn't have an explicit address range,
7541 then use the information extracted from its child dies. */
e385593e 7542 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7543 {
7544 best_lowpc = lowpc;
7545 best_highpc = highpc;
7546 }
7547 }
4ae976d1 7548 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7549 best_lowpc + baseaddr)
7550 - baseaddr);
4ae976d1 7551 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7552 best_highpc + baseaddr)
7553 - baseaddr);
0018ea6f 7554
8763cede 7555 end_psymtab_common (objfile, pst);
0018ea6f 7556
ae640021 7557 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7558 {
7559 int i;
ae640021 7560 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7561
7562 /* Fill in 'dependencies' here; we fill in 'users' in a
7563 post-pass. */
7564 pst->number_of_dependencies = len;
a9342b62
TT
7565 pst->dependencies
7566 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7567 for (i = 0; i < len; ++i)
7568 {
7569 pst->dependencies[i]
7570 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7571 }
0018ea6f 7572
ae640021 7573 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7574 }
7575
7576 /* Get the list of files included in the current compilation unit,
7577 and build a psymtab for each of them. */
7578 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7579
b4f54984 7580 if (dwarf_read_debug)
b926417a
TT
7581 fprintf_unfiltered (gdb_stdlog,
7582 "Psymtab for %s unit @%s: %s - %s"
7583 ", %d global, %d static syms\n",
7584 per_cu->is_debug_types ? "type" : "comp",
7585 sect_offset_str (per_cu->sect_off),
7586 paddress (gdbarch, pst->text_low (objfile)),
7587 paddress (gdbarch, pst->text_high (objfile)),
7588 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7589}
7590
7591/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7592 Process compilation unit THIS_CU for a psymtab. */
7593
7594static void
ab432490
SM
7595process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7596 dwarf2_per_objfile *per_objfile,
135f5437 7597 bool want_partial_unit,
b93601f3 7598 enum language pretend_language)
0018ea6f
DE
7599{
7600 /* If this compilation unit was already read in, free the
7601 cached copy in order to read it in again. This is
7602 necessary because we skipped some symbols when we first
7603 read in the compilation unit (see load_partial_dies).
7604 This problem could be avoided, but the benefit is unclear. */
7188ed02 7605 per_objfile->remove_cu (this_cu);
0018ea6f 7606
2e671100 7607 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7608
58990295
TV
7609 switch (reader.comp_unit_die->tag)
7610 {
7611 case DW_TAG_compile_unit:
7612 this_cu->unit_type = DW_UT_compile;
7613 break;
7614 case DW_TAG_partial_unit:
7615 this_cu->unit_type = DW_UT_partial;
7616 break;
7617 default:
7618 abort ();
7619 }
7620
c0ab21c2 7621 if (reader.dummy_p)
f1902523 7622 {
c0ab21c2 7623 /* Nothing. */
f1902523 7624 }
c0ab21c2 7625 else if (this_cu->is_debug_types)
3e225074
TT
7626 build_type_psymtabs_reader (&reader, reader.info_ptr,
7627 reader.comp_unit_die);
135f5437
TT
7628 else if (want_partial_unit
7629 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7630 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7631 reader.comp_unit_die,
c0ab21c2 7632 pretend_language);
0018ea6f 7633
7188ed02 7634 this_cu->lang = reader.cu->language;
58990295 7635
0018ea6f 7636 /* Age out any secondary CUs. */
7188ed02 7637 per_objfile->age_comp_units ();
0018ea6f 7638}
f4dc4d17
DE
7639
7640/* Reader function for build_type_psymtabs. */
7641
7642static void
7643build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7644 const gdb_byte *info_ptr,
3e225074 7645 struct die_info *type_unit_die)
f4dc4d17 7646{
976ca316
SM
7647 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7648 struct objfile *objfile = per_objfile->objfile;
f4dc4d17
DE
7649 struct dwarf2_cu *cu = reader->cu;
7650 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7651 struct signatured_type *sig_type;
f4dc4d17
DE
7652 struct type_unit_group *tu_group;
7653 struct attribute *attr;
7654 struct partial_die_info *first_die;
7655 CORE_ADDR lowpc, highpc;
891813be 7656 dwarf2_psymtab *pst;
f4dc4d17 7657
0186c6a7
DE
7658 gdb_assert (per_cu->is_debug_types);
7659 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7660
3e225074 7661 if (! type_unit_die->has_children)
f4dc4d17
DE
7662 return;
7663
052c8bb8 7664 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7665 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7666
df07e2c7 7667 if (tu_group->tus == nullptr)
a8b3b8e9 7668 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7669 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7670
7671 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7672 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7673 pst->anonymous = true;
f4dc4d17
DE
7674
7675 first_die = load_partial_dies (reader, info_ptr, 1);
7676
7677 lowpc = (CORE_ADDR) -1;
7678 highpc = (CORE_ADDR) 0;
7679 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7680
8763cede 7681 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7682}
7683
73051182
DE
7684/* Struct used to sort TUs by their abbreviation table offset. */
7685
7686struct tu_abbrev_offset
7687{
b2bdb8cf
SM
7688 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7689 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7690 {}
7691
7692 signatured_type *sig_type;
73051182
DE
7693 sect_offset abbrev_offset;
7694};
7695
484cf504 7696/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7697
484cf504
TT
7698static bool
7699sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7700 const struct tu_abbrev_offset &b)
73051182 7701{
484cf504 7702 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7703}
7704
7705/* Efficiently read all the type units.
7706 This does the bulk of the work for build_type_psymtabs.
7707
7708 The efficiency is because we sort TUs by the abbrev table they use and
7709 only read each abbrev table once. In one program there are 200K TUs
7710 sharing 8K abbrev tables.
7711
7712 The main purpose of this function is to support building the
5989a64e 7713 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7714 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7715 can collapse the search space by grouping them by stmt_list.
7716 The savings can be significant, in the same program from above the 200K TUs
7717 share 8K stmt_list tables.
7718
7719 FUNC is expected to call get_type_unit_group, which will create the
7720 struct type_unit_group if necessary and add it to
5989a64e 7721 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7722
7723static void
976ca316 7724build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
73051182 7725{
976ca316 7726 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7727 abbrev_table_up abbrev_table;
73051182 7728 sect_offset abbrev_offset;
73051182
DE
7729
7730 /* It's up to the caller to not call us multiple times. */
976ca316 7731 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7732
976ca316 7733 if (per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7734 return;
7735
7736 /* TUs typically share abbrev tables, and there can be way more TUs than
7737 abbrev tables. Sort by abbrev table to reduce the number of times we
7738 read each abbrev table in.
7739 Alternatives are to punt or to maintain a cache of abbrev tables.
7740 This is simpler and efficient enough for now.
7741
7742 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7743 symtab to use). Typically TUs with the same abbrev offset have the same
7744 stmt_list value too so in practice this should work well.
7745
7746 The basic algorithm here is:
7747
7748 sort TUs by abbrev table
7749 for each TU with same abbrev table:
7750 read abbrev table if first user
7751 read TU top level DIE
7752 [IWBN if DWO skeletons had DW_AT_stmt_list]
7753 call FUNC */
7754
b4f54984 7755 if (dwarf_read_debug)
73051182
DE
7756 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7757
7758 /* Sort in a separate table to maintain the order of all_type_units
7759 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7760 std::vector<tu_abbrev_offset> sorted_by_abbrev;
976ca316 7761 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7762
976ca316 7763 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
b2bdb8cf 7764 sorted_by_abbrev.emplace_back
976ca316 7765 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
b2bdb8cf 7766 sig_type->per_cu.sect_off));
73051182 7767
484cf504
TT
7768 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7769 sort_tu_by_abbrev_offset);
73051182 7770
9c541725 7771 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7772
b2bdb8cf 7773 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7774 {
73051182
DE
7775 /* Switch to the next abbrev table if necessary. */
7776 if (abbrev_table == NULL
b2bdb8cf 7777 || tu.abbrev_offset != abbrev_offset)
73051182 7778 {
b2bdb8cf 7779 abbrev_offset = tu.abbrev_offset;
73051182 7780 abbrev_table =
976ca316
SM
7781 abbrev_table::read (per_objfile->objfile,
7782 &per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7783 ++tu_stats->nr_uniq_abbrev_tables;
7784 }
7785
976ca316 7786 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
2e671100 7787 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7788 if (!reader.dummy_p)
7789 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7790 reader.comp_unit_die);
73051182 7791 }
6aa5f3a6 7792}
73051182 7793
6aa5f3a6
DE
7794/* Print collected type unit statistics. */
7795
7796static void
976ca316 7797print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7798{
976ca316 7799 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6
DE
7800
7801 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf 7802 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
976ca316 7803 per_objfile->per_bfd->all_type_units.size ());
6aa5f3a6
DE
7804 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7805 tu_stats->nr_uniq_abbrev_tables);
7806 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7807 tu_stats->nr_symtabs);
7808 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7809 tu_stats->nr_symtab_sharers);
7810 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7811 tu_stats->nr_stmt_less_type_units);
7812 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7813 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7814}
7815
f4dc4d17
DE
7816/* Traversal function for build_type_psymtabs. */
7817
7818static int
7819build_type_psymtab_dependencies (void **slot, void *info)
7820{
976ca316
SM
7821 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7822 struct objfile *objfile = per_objfile->objfile;
f4dc4d17 7823 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7824 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7825 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7826 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7827 int i;
7828
7829 gdb_assert (len > 0);
197400e8 7830 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7831
7832 pst->number_of_dependencies = len;
a9342b62 7833 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7834 for (i = 0; i < len; ++i)
f4dc4d17 7835 {
df07e2c7 7836 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7837 gdb_assert (iter->per_cu.is_debug_types);
7838 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7839 iter->type_unit_group = tu_group;
f4dc4d17
DE
7840 }
7841
df07e2c7
AB
7842 delete tu_group->tus;
7843 tu_group->tus = nullptr;
348e048f
DE
7844
7845 return 1;
7846}
7847
7848/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7849 Build partial symbol tables for the .debug_types comp-units. */
7850
7851static void
976ca316 7852build_type_psymtabs (dwarf2_per_objfile *per_objfile)
348e048f 7853{
976ca316 7854 if (! create_all_type_units (per_objfile))
348e048f
DE
7855 return;
7856
976ca316 7857 build_type_psymtabs_1 (per_objfile);
6aa5f3a6 7858}
f4dc4d17 7859
6aa5f3a6
DE
7860/* Traversal function for process_skeletonless_type_unit.
7861 Read a TU in a DWO file and build partial symbols for it. */
7862
7863static int
7864process_skeletonless_type_unit (void **slot, void *info)
7865{
7866 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 7867 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
7868 struct signatured_type find_entry, *entry;
7869
7870 /* If this TU doesn't exist in the global table, add it and read it in. */
7871
976ca316
SM
7872 if (per_objfile->per_bfd->signatured_types == NULL)
7873 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7874
7875 find_entry.signature = dwo_unit->signature;
976ca316 7876 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7877 &find_entry, INSERT);
6aa5f3a6
DE
7878 /* If we've already seen this type there's nothing to do. What's happening
7879 is we're doing our own version of comdat-folding here. */
7880 if (*slot != NULL)
7881 return 1;
7882
7883 /* This does the job that create_all_type_units would have done for
7884 this TU. */
976ca316
SM
7885 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7886 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7887 *slot = entry;
7888
7889 /* This does the job that build_type_psymtabs_1 would have done. */
976ca316 7890 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
7891 if (!reader.dummy_p)
7892 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7893 reader.comp_unit_die);
6aa5f3a6
DE
7894
7895 return 1;
7896}
7897
7898/* Traversal function for process_skeletonless_type_units. */
7899
7900static int
7901process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7902{
7903 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7904
7905 if (dwo_file->tus != NULL)
b0b6a987
TT
7906 htab_traverse_noresize (dwo_file->tus.get (),
7907 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7908
7909 return 1;
7910}
7911
7912/* Scan all TUs of DWO files, verifying we've processed them.
7913 This is needed in case a TU was emitted without its skeleton.
7914 Note: This can't be done until we know what all the DWO files are. */
7915
7916static void
976ca316 7917process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
7918{
7919 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
7920 if (get_dwp_file (per_objfile) == NULL
7921 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7922 {
976ca316 7923 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7924 process_dwo_file_for_skeletonless_type_units,
976ca316 7925 per_objfile);
6aa5f3a6 7926 }
348e048f
DE
7927}
7928
ed2dc618 7929/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7930
7931static void
976ca316 7932set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 7933{
976ca316 7934 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 7935 {
891813be 7936 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7937
36586728
TT
7938 if (pst == NULL)
7939 continue;
7940
b76e467d 7941 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7942 {
7943 /* Set the 'user' field only if it is not already set. */
7944 if (pst->dependencies[j]->user == NULL)
7945 pst->dependencies[j]->user = pst;
7946 }
7947 }
7948}
7949
93311388
DE
7950/* Build the partial symbol table by doing a quick pass through the
7951 .debug_info and .debug_abbrev sections. */
72bf9492 7952
93311388 7953static void
976ca316 7954dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 7955{
976ca316 7956 struct objfile *objfile = per_objfile->objfile;
93311388 7957
b4f54984 7958 if (dwarf_read_debug)
45cfd468
DE
7959 {
7960 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7961 objfile_name (objfile));
45cfd468
DE
7962 }
7963
76935768 7964 scoped_restore restore_reading_psyms
976ca316 7965 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
76935768 7966 true);
98bfdba5 7967
976ca316 7968 per_objfile->per_bfd->info.read (objfile);
91c24f0a 7969
93311388
DE
7970 /* Any cached compilation units will be linked by the per-objfile
7971 read_in_chain. Make sure to free them when we're done. */
976ca316 7972 free_cached_comp_units freer (per_objfile);
72bf9492 7973
976ca316 7974 build_type_psymtabs (per_objfile);
348e048f 7975
976ca316 7976 create_all_comp_units (per_objfile);
c906108c 7977
60606b2c
TT
7978 /* Create a temporary address map on a temporary obstack. We later
7979 copy this to the final obstack. */
8268c778 7980 auto_obstack temp_obstack;
791afaa2
TT
7981
7982 scoped_restore save_psymtabs_addrmap
d320c2b5 7983 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7984 addrmap_create_mutable (&temp_obstack));
72bf9492 7985
976ca316 7986 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
7987 {
7988 if (per_cu->v.psymtab != NULL)
7989 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7990 continue;
976ca316 7991 process_psymtab_comp_unit (per_cu, per_objfile, false,
ab432490 7992 language_minimal);
3d5afab3 7993 }
ff013f42 7994
6aa5f3a6 7995 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 7996 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
7997
7998 /* Now that all TUs have been processed we can fill in the dependencies. */
976ca316 7999 if (per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 8000 {
976ca316
SM
8001 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8002 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
8003 }
8004
b4f54984 8005 if (dwarf_read_debug)
976ca316 8006 print_tu_stats (per_objfile);
6aa5f3a6 8007
976ca316 8008 set_partial_user (per_objfile);
95554aad 8009
d320c2b5
TT
8010 objfile->partial_symtabs->psymtabs_addrmap
8011 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8012 objfile->partial_symtabs->obstack ());
791afaa2
TT
8013 /* At this point we want to keep the address map. */
8014 save_psymtabs_addrmap.release ();
ff013f42 8015
b4f54984 8016 if (dwarf_read_debug)
45cfd468 8017 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8018 objfile_name (objfile));
ae038cb0
DJ
8019}
8020
dee91e82
DE
8021/* Load the partial DIEs for a secondary CU into memory.
8022 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8023
dee91e82 8024static void
ab432490 8025load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
8026 dwarf2_per_objfile *per_objfile,
8027 dwarf2_cu *existing_cu)
dee91e82 8028{
2e671100 8029 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
8030
8031 if (!reader.dummy_p)
8032 {
8033 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8034 language_minimal);
8035
8036 /* Check if comp unit has_children.
8037 If so, read the rest of the partial symbols from this comp unit.
8038 If not, there's no more debug_info for this comp unit. */
3e225074 8039 if (reader.comp_unit_die->has_children)
c0ab21c2 8040 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8041
8042 reader.keep ();
c0ab21c2 8043 }
ae038cb0
DJ
8044}
8045
ae038cb0 8046static void
976ca316 8047read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 8048 struct dwarf2_section_info *section,
f1902523 8049 struct dwarf2_section_info *abbrev_section,
b76e467d 8050 unsigned int is_dwz)
ae038cb0 8051{
d521ce57 8052 const gdb_byte *info_ptr;
976ca316 8053 struct objfile *objfile = per_objfile->objfile;
be391dca 8054
b4f54984 8055 if (dwarf_read_debug)
bf6af496 8056 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
8057 section->get_name (),
8058 section->get_file_name ());
bf6af496 8059
96b79293 8060 section->read (objfile);
ae038cb0 8061
36586728 8062 info_ptr = section->buffer;
6e70227d 8063
36586728 8064 while (info_ptr < section->buffer + section->size)
ae038cb0 8065 {
ae038cb0 8066 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8067
9c541725 8068 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8069
f1902523 8070 comp_unit_head cu_header;
976ca316 8071 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618
SM
8072 abbrev_section, info_ptr,
8073 rcuh_kind::COMPILE);
ae038cb0
DJ
8074
8075 /* Save the compilation unit for later lookup. */
f1902523 8076 if (cu_header.unit_type != DW_UT_type)
976ca316 8077 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8078 else
8079 {
976ca316 8080 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8081 sig_type->signature = cu_header.signature;
8082 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8083 this_cu = &sig_type->per_cu;
8084 }
8085 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8086 this_cu->sect_off = sect_off;
f1902523 8087 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8088 this_cu->is_dwz = is_dwz;
8a0459fd 8089 this_cu->section = section;
ae038cb0 8090
976ca316 8091 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8092
8093 info_ptr = info_ptr + this_cu->length;
8094 }
36586728
TT
8095}
8096
8097/* Create a list of all compilation units in OBJFILE.
8098 This is only done for -readnow and building partial symtabs. */
8099
8100static void
976ca316 8101create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 8102{
976ca316
SM
8103 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8104 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8105 &per_objfile->per_bfd->abbrev, 0);
36586728 8106
976ca316 8107 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 8108 if (dwz != NULL)
976ca316 8109 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
c906108c
SS
8110}
8111
5734ee8b 8112/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8113 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8114 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8115 DW_AT_ranges). See the comments of add_partial_subprogram on how
8116 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8117
72bf9492
DJ
8118static void
8119scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8120 CORE_ADDR *highpc, int set_addrmap,
8121 struct dwarf2_cu *cu)
c906108c 8122{
72bf9492 8123 struct partial_die_info *pdi;
c906108c 8124
91c24f0a
DC
8125 /* Now, march along the PDI's, descending into ones which have
8126 interesting children but skipping the children of the other ones,
8127 until we reach the end of the compilation unit. */
c906108c 8128
72bf9492 8129 pdi = first_die;
91c24f0a 8130
72bf9492
DJ
8131 while (pdi != NULL)
8132 {
52356b79 8133 pdi->fixup (cu);
c906108c 8134
f55ee35c 8135 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8136 children, so we need to look at them. Ditto for anonymous
8137 enums. */
933c6fe4 8138
7d00ffec 8139 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8140 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8141 || pdi->tag == DW_TAG_imported_unit
8142 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8143 {
72bf9492 8144 switch (pdi->tag)
c906108c
SS
8145 {
8146 case DW_TAG_subprogram:
b1dc1806 8147 case DW_TAG_inlined_subroutine:
cdc07690 8148 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
8149 if (cu->language == language_cplus)
8150 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8151 set_addrmap, cu);
c906108c 8152 break;
72929c62 8153 case DW_TAG_constant:
c906108c
SS
8154 case DW_TAG_variable:
8155 case DW_TAG_typedef:
91c24f0a 8156 case DW_TAG_union_type:
317d2668
TV
8157 if (!pdi->is_declaration
8158 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8159 {
72bf9492 8160 add_partial_symbol (pdi, cu);
63d06c5c
DC
8161 }
8162 break;
c906108c 8163 case DW_TAG_class_type:
680b30c7 8164 case DW_TAG_interface_type:
c906108c 8165 case DW_TAG_structure_type:
72bf9492 8166 if (!pdi->is_declaration)
c906108c 8167 {
72bf9492 8168 add_partial_symbol (pdi, cu);
c906108c 8169 }
b7fee5a3
KS
8170 if ((cu->language == language_rust
8171 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8172 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8173 set_addrmap, cu);
c906108c 8174 break;
91c24f0a 8175 case DW_TAG_enumeration_type:
72bf9492
DJ
8176 if (!pdi->is_declaration)
8177 add_partial_enumeration (pdi, cu);
c906108c
SS
8178 break;
8179 case DW_TAG_base_type:
a02abb62 8180 case DW_TAG_subrange_type:
c906108c 8181 /* File scope base type definitions are added to the partial
c5aa993b 8182 symbol table. */
72bf9492 8183 add_partial_symbol (pdi, cu);
c906108c 8184 break;
d9fa45fe 8185 case DW_TAG_namespace:
cdc07690 8186 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8187 break;
5d7cb8df 8188 case DW_TAG_module:
59c35742
AB
8189 if (!pdi->is_declaration)
8190 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8191 break;
95554aad
TT
8192 case DW_TAG_imported_unit:
8193 {
8194 struct dwarf2_per_cu_data *per_cu;
8195
f4dc4d17
DE
8196 /* For now we don't handle imported units in type units. */
8197 if (cu->per_cu->is_debug_types)
8198 {
8199 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8200 " supported in type units [in module %s]"),
5e22e966 8201 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8202 }
8203
e3b94546 8204 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8205 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8206
8207 /* Go read the partial unit, if needed. */
8208 if (per_cu->v.psymtab == NULL)
ab432490
SM
8209 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8210 cu->language);
95554aad 8211
ae640021 8212 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8213 }
8214 break;
74921315
KS
8215 case DW_TAG_imported_declaration:
8216 add_partial_symbol (pdi, cu);
8217 break;
c906108c
SS
8218 default:
8219 break;
8220 }
8221 }
8222
72bf9492
DJ
8223 /* If the die has a sibling, skip to the sibling. */
8224
8225 pdi = pdi->die_sibling;
8226 }
8227}
8228
8229/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8230
72bf9492 8231 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8232 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8233 Enumerators are an exception; they use the scope of their parent
8234 enumeration type, i.e. the name of the enumeration type is not
8235 prepended to the enumerator.
91c24f0a 8236
72bf9492
DJ
8237 There are two complexities. One is DW_AT_specification; in this
8238 case "parent" means the parent of the target of the specification,
8239 instead of the direct parent of the DIE. The other is compilers
8240 which do not emit DW_TAG_namespace; in this case we try to guess
8241 the fully qualified name of structure types from their members'
8242 linkage names. This must be done using the DIE's children rather
8243 than the children of any DW_AT_specification target. We only need
8244 to do this for structures at the top level, i.e. if the target of
8245 any DW_AT_specification (if any; otherwise the DIE itself) does not
8246 have a parent. */
8247
8248/* Compute the scope prefix associated with PDI's parent, in
8249 compilation unit CU. The result will be allocated on CU's
8250 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8251 field. NULL is returned if no prefix is necessary. */
15d034d0 8252static const char *
72bf9492
DJ
8253partial_die_parent_scope (struct partial_die_info *pdi,
8254 struct dwarf2_cu *cu)
8255{
15d034d0 8256 const char *grandparent_scope;
72bf9492 8257 struct partial_die_info *parent, *real_pdi;
91c24f0a 8258
72bf9492
DJ
8259 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8260 then this means the parent of the specification DIE. */
8261
8262 real_pdi = pdi;
72bf9492 8263 while (real_pdi->has_specification)
fb816e8b 8264 {
122cf0f2
AB
8265 auto res = find_partial_die (real_pdi->spec_offset,
8266 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8267 real_pdi = res.pdi;
8268 cu = res.cu;
8269 }
72bf9492
DJ
8270
8271 parent = real_pdi->die_parent;
8272 if (parent == NULL)
8273 return NULL;
8274
8275 if (parent->scope_set)
8276 return parent->scope;
8277
52356b79 8278 parent->fixup (cu);
72bf9492 8279
10b3939b 8280 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8281
acebe513
UW
8282 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8283 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8284 Work around this problem here. */
8285 if (cu->language == language_cplus
6e70227d 8286 && parent->tag == DW_TAG_namespace
7d00ffec 8287 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
8288 && grandparent_scope == NULL)
8289 {
8290 parent->scope = NULL;
8291 parent->scope_set = 1;
8292 return NULL;
8293 }
8294
0a4b0913 8295 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8296 if (pdi->tag == DW_TAG_enumerator)
8297 /* Enumerators should not get the name of the enumeration as a prefix. */
8298 parent->scope = grandparent_scope;
8299 else if (parent->tag == DW_TAG_namespace
f55ee35c 8300 || parent->tag == DW_TAG_module
72bf9492
DJ
8301 || parent->tag == DW_TAG_structure_type
8302 || parent->tag == DW_TAG_class_type
680b30c7 8303 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8304 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8305 || parent->tag == DW_TAG_enumeration_type
8306 || (cu->language == language_fortran
8307 && parent->tag == DW_TAG_subprogram
8308 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8309 {
8310 if (grandparent_scope == NULL)
7d00ffec 8311 parent->scope = parent->name (cu);
72bf9492 8312 else
3e43a32a
MS
8313 parent->scope = typename_concat (&cu->comp_unit_obstack,
8314 grandparent_scope,
7d00ffec 8315 parent->name (cu), 0, cu);
72bf9492 8316 }
72bf9492
DJ
8317 else
8318 {
8319 /* FIXME drow/2004-04-01: What should we be doing with
8320 function-local names? For partial symbols, we should probably be
8321 ignoring them. */
fa9c3fa0
TT
8322 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8323 dwarf_tag_name (parent->tag),
8324 sect_offset_str (pdi->sect_off));
72bf9492 8325 parent->scope = grandparent_scope;
c906108c
SS
8326 }
8327
72bf9492
DJ
8328 parent->scope_set = 1;
8329 return parent->scope;
8330}
8331
8332/* Return the fully scoped name associated with PDI, from compilation unit
8333 CU. The result will be allocated with malloc. */
4568ecf9 8334
43816ebc 8335static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8336partial_die_full_name (struct partial_die_info *pdi,
8337 struct dwarf2_cu *cu)
8338{
15d034d0 8339 const char *parent_scope;
72bf9492 8340
98bfdba5
PA
8341 /* If this is a template instantiation, we can not work out the
8342 template arguments from partial DIEs. So, unfortunately, we have
8343 to go through the full DIEs. At least any work we do building
8344 types here will be reused if full symbols are loaded later. */
8345 if (pdi->has_template_arguments)
8346 {
52356b79 8347 pdi->fixup (cu);
98bfdba5 8348
7d00ffec 8349 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8350 {
8351 struct die_info *die;
8352 struct attribute attr;
8353 struct dwarf2_cu *ref_cu = cu;
8354
b64f50a1 8355 /* DW_FORM_ref_addr is using section offset. */
b4069958 8356 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8357 attr.form = DW_FORM_ref_addr;
9c541725 8358 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8359 die = follow_die_ref (NULL, &attr, &ref_cu);
8360
43816ebc 8361 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8362 }
8363 }
8364
72bf9492
DJ
8365 parent_scope = partial_die_parent_scope (pdi, cu);
8366 if (parent_scope == NULL)
8367 return NULL;
8368 else
43816ebc 8369 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8370 pdi->name (cu),
8371 0, cu));
c906108c
SS
8372}
8373
8374static void
72bf9492 8375add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8376{
976ca316
SM
8377 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8378 struct objfile *objfile = per_objfile->objfile;
08feed99 8379 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8380 CORE_ADDR addr = 0;
15d034d0 8381 const char *actual_name = NULL;
e142c38c
DJ
8382 CORE_ADDR baseaddr;
8383
b3b3bada 8384 baseaddr = objfile->text_section_offset ();
c906108c 8385
43816ebc
TT
8386 gdb::unique_xmalloc_ptr<char> built_actual_name
8387 = partial_die_full_name (pdi, cu);
15d034d0 8388 if (built_actual_name != NULL)
43816ebc 8389 actual_name = built_actual_name.get ();
63d06c5c 8390
72bf9492 8391 if (actual_name == NULL)
7d00ffec 8392 actual_name = pdi->name (cu);
72bf9492 8393
76e288d1
TT
8394 partial_symbol psymbol;
8395 memset (&psymbol, 0, sizeof (psymbol));
8396 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8397 psymbol.ginfo.section = -1;
8398
8399 /* The code below indicates that the psymbol should be installed by
8400 setting this. */
8401 gdb::optional<psymbol_placement> where;
8402
c906108c
SS
8403 switch (pdi->tag)
8404 {
b1dc1806 8405 case DW_TAG_inlined_subroutine:
c906108c 8406 case DW_TAG_subprogram:
79748972
TT
8407 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8408 - baseaddr);
0a4b0913
AB
8409 if (pdi->is_external
8410 || cu->language == language_ada
8411 || (cu->language == language_fortran
8412 && pdi->die_parent != NULL
8413 && pdi->die_parent->tag == DW_TAG_subprogram))
8414 {
8415 /* Normally, only "external" DIEs are part of the global scope.
8416 But in Ada and Fortran, we want to be able to access nested
8417 procedures globally. So all Ada and Fortran subprograms are
8418 stored in the global scope. */
76e288d1 8419 where = psymbol_placement::GLOBAL;
c906108c
SS
8420 }
8421 else
76e288d1
TT
8422 where = psymbol_placement::STATIC;
8423
8424 psymbol.domain = VAR_DOMAIN;
8425 psymbol.aclass = LOC_BLOCK;
8426 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8427 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8428
8429 if (pdi->main_subprogram && actual_name != NULL)
8430 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8431 break;
72929c62 8432 case DW_TAG_constant:
76e288d1
TT
8433 psymbol.domain = VAR_DOMAIN;
8434 psymbol.aclass = LOC_STATIC;
8435 where = (pdi->is_external
8436 ? psymbol_placement::GLOBAL
8437 : psymbol_placement::STATIC);
72929c62 8438 break;
c906108c 8439 case DW_TAG_variable:
95554aad
TT
8440 if (pdi->d.locdesc)
8441 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8442
95554aad 8443 if (pdi->d.locdesc
caac4577 8444 && addr == 0
976ca316 8445 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8446 {
8447 /* A global or static variable may also have been stripped
8448 out by the linker if unused, in which case its address
8449 will be nullified; do not add such variables into partial
8450 symbol table then. */
8451 }
8452 else if (pdi->is_external)
c906108c
SS
8453 {
8454 /* Global Variable.
8455 Don't enter into the minimal symbol tables as there is
8456 a minimal symbol table entry from the ELF symbols already.
8457 Enter into partial symbol table if it has a location
8458 descriptor or a type.
8459 If the location descriptor is missing, new_symbol will create
8460 a LOC_UNRESOLVED symbol, the address of the variable will then
8461 be determined from the minimal symbol table whenever the variable
8462 is referenced.
8463 The address for the partial symbol table entry is not
8464 used by GDB, but it comes in handy for debugging partial symbol
8465 table building. */
8466
95554aad 8467 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8468 {
8469 psymbol.domain = VAR_DOMAIN;
8470 psymbol.aclass = LOC_STATIC;
8471 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8472 psymbol.ginfo.value.address = addr;
8473 where = psymbol_placement::GLOBAL;
8474 }
c906108c
SS
8475 }
8476 else
8477 {
ff908ebf
AW
8478 int has_loc = pdi->d.locdesc != NULL;
8479
8480 /* Static Variable. Skip symbols whose value we cannot know (those
8481 without location descriptors or constant values). */
8482 if (!has_loc && !pdi->has_const_value)
43816ebc 8483 return;
ff908ebf 8484
76e288d1
TT
8485 psymbol.domain = VAR_DOMAIN;
8486 psymbol.aclass = LOC_STATIC;
8487 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8488 if (has_loc)
8489 psymbol.ginfo.value.address = addr;
8490 where = psymbol_placement::STATIC;
c906108c
SS
8491 }
8492 break;
8493 case DW_TAG_typedef:
8494 case DW_TAG_base_type:
a02abb62 8495 case DW_TAG_subrange_type:
76e288d1
TT
8496 psymbol.domain = VAR_DOMAIN;
8497 psymbol.aclass = LOC_TYPEDEF;
8498 where = psymbol_placement::STATIC;
c906108c 8499 break;
74921315 8500 case DW_TAG_imported_declaration:
72bf9492 8501 case DW_TAG_namespace:
76e288d1
TT
8502 psymbol.domain = VAR_DOMAIN;
8503 psymbol.aclass = LOC_TYPEDEF;
8504 where = psymbol_placement::GLOBAL;
72bf9492 8505 break;
530e8392 8506 case DW_TAG_module:
a5fd13a9
BH
8507 /* With Fortran 77 there might be a "BLOCK DATA" module
8508 available without any name. If so, we skip the module as it
8509 doesn't bring any value. */
8510 if (actual_name != nullptr)
76e288d1
TT
8511 {
8512 psymbol.domain = MODULE_DOMAIN;
8513 psymbol.aclass = LOC_TYPEDEF;
8514 where = psymbol_placement::GLOBAL;
8515 }
530e8392 8516 break;
c906108c 8517 case DW_TAG_class_type:
680b30c7 8518 case DW_TAG_interface_type:
c906108c
SS
8519 case DW_TAG_structure_type:
8520 case DW_TAG_union_type:
8521 case DW_TAG_enumeration_type:
fa4028e9
JB
8522 /* Skip external references. The DWARF standard says in the section
8523 about "Structure, Union, and Class Type Entries": "An incomplete
8524 structure, union or class type is represented by a structure,
8525 union or class entry that does not have a byte size attribute
8526 and that has a DW_AT_declaration attribute." */
8527 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8528 return;
fa4028e9 8529
63d06c5c
DC
8530 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8531 static vs. global. */
76e288d1
TT
8532 psymbol.domain = STRUCT_DOMAIN;
8533 psymbol.aclass = LOC_TYPEDEF;
8534 where = (cu->language == language_cplus
8535 ? psymbol_placement::GLOBAL
8536 : psymbol_placement::STATIC);
c906108c
SS
8537 break;
8538 case DW_TAG_enumerator:
76e288d1
TT
8539 psymbol.domain = VAR_DOMAIN;
8540 psymbol.aclass = LOC_CONST;
8541 where = (cu->language == language_cplus
8542 ? psymbol_placement::GLOBAL
8543 : psymbol_placement::STATIC);
c906108c
SS
8544 break;
8545 default:
8546 break;
8547 }
76e288d1
TT
8548
8549 if (where.has_value ())
8550 {
f049a313
TT
8551 if (built_actual_name != nullptr)
8552 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8553 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8554 psymbol.ginfo.set_linkage_name (actual_name);
8555 else
8556 {
8557 psymbol.ginfo.set_demangled_name (actual_name,
8558 &objfile->objfile_obstack);
8559 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8560 }
76e288d1
TT
8561 add_psymbol_to_list (psymbol, *where, objfile);
8562 }
c906108c
SS
8563}
8564
5c4e30ca
DC
8565/* Read a partial die corresponding to a namespace; also, add a symbol
8566 corresponding to that namespace to the symbol table. NAMESPACE is
8567 the name of the enclosing namespace. */
91c24f0a 8568
72bf9492
DJ
8569static void
8570add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8571 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8572 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8573{
72bf9492 8574 /* Add a symbol for the namespace. */
e7c27a73 8575
72bf9492 8576 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8577
8578 /* Now scan partial symbols in that namespace. */
8579
91c24f0a 8580 if (pdi->has_children)
cdc07690 8581 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8582}
8583
5d7cb8df
JK
8584/* Read a partial die corresponding to a Fortran module. */
8585
8586static void
8587add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8588 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8589{
530e8392
KB
8590 /* Add a symbol for the namespace. */
8591
8592 add_partial_symbol (pdi, cu);
8593
f55ee35c 8594 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8595
8596 if (pdi->has_children)
cdc07690 8597 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8598}
8599
b1dc1806
XR
8600/* Read a partial die corresponding to a subprogram or an inlined
8601 subprogram and create a partial symbol for that subprogram.
8602 When the CU language allows it, this routine also defines a partial
8603 symbol for each nested subprogram that this subprogram contains.
8604 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8605 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8606
cdc07690
YQ
8607 PDI may also be a lexical block, in which case we simply search
8608 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8609 Again, this is only performed when the CU language allows this
8610 type of definitions. */
8611
8612static void
8613add_partial_subprogram (struct partial_die_info *pdi,
8614 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8615 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8616{
b1dc1806 8617 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8618 {
8619 if (pdi->has_pc_info)
8620 {
8621 if (pdi->lowpc < *lowpc)
8622 *lowpc = pdi->lowpc;
8623 if (pdi->highpc > *highpc)
8624 *highpc = pdi->highpc;
cdc07690 8625 if (set_addrmap)
5734ee8b 8626 {
5e22e966 8627 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8628 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8629 CORE_ADDR baseaddr;
b926417a
TT
8630 CORE_ADDR this_highpc;
8631 CORE_ADDR this_lowpc;
5734ee8b 8632
b3b3bada 8633 baseaddr = objfile->text_section_offset ();
b926417a
TT
8634 this_lowpc
8635 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8636 pdi->lowpc + baseaddr)
8637 - baseaddr);
8638 this_highpc
8639 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8640 pdi->highpc + baseaddr)
8641 - baseaddr);
d320c2b5 8642 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8643 this_lowpc, this_highpc - 1,
9291a0cd 8644 cu->per_cu->v.psymtab);
5734ee8b 8645 }
481860b3
GB
8646 }
8647
8648 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8649 {
bc30ff58 8650 if (!pdi->is_declaration)
e8d05480
JB
8651 /* Ignore subprogram DIEs that do not have a name, they are
8652 illegal. Do not emit a complaint at this point, we will
8653 do so when we convert this psymtab into a symtab. */
7d00ffec 8654 if (pdi->name (cu))
e8d05480 8655 add_partial_symbol (pdi, cu);
bc30ff58
JB
8656 }
8657 }
6e70227d 8658
bc30ff58
JB
8659 if (! pdi->has_children)
8660 return;
8661
0a4b0913 8662 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8663 {
8664 pdi = pdi->die_child;
8665 while (pdi != NULL)
8666 {
52356b79 8667 pdi->fixup (cu);
bc30ff58 8668 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8669 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8670 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8671 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8672 pdi = pdi->die_sibling;
8673 }
8674 }
8675}
8676
91c24f0a
DC
8677/* Read a partial die corresponding to an enumeration type. */
8678
72bf9492
DJ
8679static void
8680add_partial_enumeration (struct partial_die_info *enum_pdi,
8681 struct dwarf2_cu *cu)
91c24f0a 8682{
72bf9492 8683 struct partial_die_info *pdi;
91c24f0a 8684
7d00ffec 8685 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8686 add_partial_symbol (enum_pdi, cu);
8687
8688 pdi = enum_pdi->die_child;
8689 while (pdi)
91c24f0a 8690 {
7d00ffec 8691 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8692 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8693 else
72bf9492
DJ
8694 add_partial_symbol (pdi, cu);
8695 pdi = pdi->die_sibling;
91c24f0a 8696 }
91c24f0a
DC
8697}
8698
6caca83c
CC
8699/* Return the initial uleb128 in the die at INFO_PTR. */
8700
8701static unsigned int
d521ce57 8702peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8703{
8704 unsigned int bytes_read;
8705
8706 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8707}
8708
685af9cd
TT
8709/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8710 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8711
4bb7a0a7
DJ
8712 Return the corresponding abbrev, or NULL if the number is zero (indicating
8713 an empty DIE). In either case *BYTES_READ will be set to the length of
8714 the initial number. */
8715
8716static struct abbrev_info *
685af9cd
TT
8717peek_die_abbrev (const die_reader_specs &reader,
8718 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8719{
685af9cd 8720 dwarf2_cu *cu = reader.cu;
5e22e966 8721 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8722 unsigned int abbrev_number
8723 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8724
8725 if (abbrev_number == 0)
8726 return NULL;
8727
685af9cd 8728 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8729 if (!abbrev)
8730 {
422b9917 8731 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8732 " at offset %s [in module %s]"),
422b9917 8733 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8734 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8735 }
8736
8737 return abbrev;
8738}
8739
93311388
DE
8740/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8741 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8742 DIE. Any children of the skipped DIEs will also be skipped. */
8743
d521ce57
TT
8744static const gdb_byte *
8745skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8746{
4bb7a0a7
DJ
8747 while (1)
8748 {
685af9cd
TT
8749 unsigned int bytes_read;
8750 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8751
4bb7a0a7
DJ
8752 if (abbrev == NULL)
8753 return info_ptr + bytes_read;
8754 else
dee91e82 8755 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8756 }
8757}
8758
93311388
DE
8759/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8760 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8761 abbrev corresponding to that skipped uleb128 should be passed in
8762 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8763 children. */
8764
d521ce57
TT
8765static const gdb_byte *
8766skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8767 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8768{
8769 unsigned int bytes_read;
8770 struct attribute attr;
dee91e82
DE
8771 bfd *abfd = reader->abfd;
8772 struct dwarf2_cu *cu = reader->cu;
d521ce57 8773 const gdb_byte *buffer = reader->buffer;
f664829e 8774 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8775 unsigned int form, i;
8776
8777 for (i = 0; i < abbrev->num_attrs; i++)
8778 {
8779 /* The only abbrev we care about is DW_AT_sibling. */
8780 if (abbrev->attrs[i].name == DW_AT_sibling)
8781 {
18a8505e
AT
8782 bool ignored;
8783 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8784 &ignored);
4bb7a0a7 8785 if (attr.form == DW_FORM_ref_addr)
b98664d3 8786 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8787 else
b9502d3f 8788 {
0826b30a 8789 sect_offset off = attr.get_ref_die_offset ();
9c541725 8790 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8791
8792 if (sibling_ptr < info_ptr)
b98664d3 8793 complaint (_("DW_AT_sibling points backwards"));
22869d73 8794 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8795 reader->die_section->overflow_complaint ();
b9502d3f
WN
8796 else
8797 return sibling_ptr;
8798 }
4bb7a0a7
DJ
8799 }
8800
8801 /* If it isn't DW_AT_sibling, skip this attribute. */
8802 form = abbrev->attrs[i].form;
8803 skip_attribute:
8804 switch (form)
8805 {
4bb7a0a7 8806 case DW_FORM_ref_addr:
ae411497
TT
8807 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8808 and later it is offset sized. */
8809 if (cu->header.version == 2)
8810 info_ptr += cu->header.addr_size;
8811 else
8812 info_ptr += cu->header.offset_size;
8813 break;
36586728
TT
8814 case DW_FORM_GNU_ref_alt:
8815 info_ptr += cu->header.offset_size;
8816 break;
ae411497 8817 case DW_FORM_addr:
4bb7a0a7
DJ
8818 info_ptr += cu->header.addr_size;
8819 break;
8820 case DW_FORM_data1:
8821 case DW_FORM_ref1:
8822 case DW_FORM_flag:
8fe0f950 8823 case DW_FORM_strx1:
4bb7a0a7
DJ
8824 info_ptr += 1;
8825 break;
2dc7f7b3 8826 case DW_FORM_flag_present:
43988095 8827 case DW_FORM_implicit_const:
2dc7f7b3 8828 break;
4bb7a0a7
DJ
8829 case DW_FORM_data2:
8830 case DW_FORM_ref2:
8fe0f950 8831 case DW_FORM_strx2:
4bb7a0a7
DJ
8832 info_ptr += 2;
8833 break;
8fe0f950
AT
8834 case DW_FORM_strx3:
8835 info_ptr += 3;
8836 break;
4bb7a0a7
DJ
8837 case DW_FORM_data4:
8838 case DW_FORM_ref4:
8fe0f950 8839 case DW_FORM_strx4:
4bb7a0a7
DJ
8840 info_ptr += 4;
8841 break;
8842 case DW_FORM_data8:
8843 case DW_FORM_ref8:
55f1336d 8844 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8845 info_ptr += 8;
8846 break;
0224619f
JK
8847 case DW_FORM_data16:
8848 info_ptr += 16;
8849 break;
4bb7a0a7 8850 case DW_FORM_string:
9b1c24c8 8851 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8852 info_ptr += bytes_read;
8853 break;
2dc7f7b3 8854 case DW_FORM_sec_offset:
4bb7a0a7 8855 case DW_FORM_strp:
36586728 8856 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8857 info_ptr += cu->header.offset_size;
8858 break;
2dc7f7b3 8859 case DW_FORM_exprloc:
4bb7a0a7
DJ
8860 case DW_FORM_block:
8861 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8862 info_ptr += bytes_read;
8863 break;
8864 case DW_FORM_block1:
8865 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8866 break;
8867 case DW_FORM_block2:
8868 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8869 break;
8870 case DW_FORM_block4:
8871 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8872 break;
336d760d 8873 case DW_FORM_addrx:
cf532bd1 8874 case DW_FORM_strx:
4bb7a0a7
DJ
8875 case DW_FORM_sdata:
8876 case DW_FORM_udata:
8877 case DW_FORM_ref_udata:
3019eac3
DE
8878 case DW_FORM_GNU_addr_index:
8879 case DW_FORM_GNU_str_index:
18a8505e 8880 case DW_FORM_rnglistx:
41144253 8881 case DW_FORM_loclistx:
d521ce57 8882 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8883 break;
8884 case DW_FORM_indirect:
8885 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8886 info_ptr += bytes_read;
8887 /* We need to continue parsing from here, so just go back to
8888 the top. */
8889 goto skip_attribute;
8890
8891 default:
3e43a32a
MS
8892 error (_("Dwarf Error: Cannot handle %s "
8893 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8894 dwarf_form_name (form),
8895 bfd_get_filename (abfd));
8896 }
8897 }
8898
8899 if (abbrev->has_children)
dee91e82 8900 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8901 else
8902 return info_ptr;
8903}
8904
93311388 8905/* Locate ORIG_PDI's sibling.
dee91e82 8906 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8907
d521ce57 8908static const gdb_byte *
dee91e82
DE
8909locate_pdi_sibling (const struct die_reader_specs *reader,
8910 struct partial_die_info *orig_pdi,
d521ce57 8911 const gdb_byte *info_ptr)
91c24f0a
DC
8912{
8913 /* Do we know the sibling already? */
72bf9492 8914
91c24f0a
DC
8915 if (orig_pdi->sibling)
8916 return orig_pdi->sibling;
8917
8918 /* Are there any children to deal with? */
8919
8920 if (!orig_pdi->has_children)
8921 return info_ptr;
8922
4bb7a0a7 8923 /* Skip the children the long way. */
91c24f0a 8924
dee91e82 8925 return skip_children (reader, info_ptr);
91c24f0a
DC
8926}
8927
257e7a09 8928/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8929 not NULL. */
c906108c 8930
891813be
TT
8931void
8932dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8933{
976ca316 8934 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 8935
976ca316 8936 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 8937
077cbab2
TT
8938 /* If this psymtab is constructed from a debug-only objfile, the
8939 has_section_at_zero flag will not necessarily be correct. We
8940 can get the correct value for this flag by looking at the data
8941 associated with the (presumably stripped) associated objfile. */
8942 if (objfile->separate_debug_objfile_backlink)
c906108c 8943 {
976ca316 8944 dwarf2_per_objfile *per_objfile_backlink
077cbab2 8945 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8946
976ca316
SM
8947 per_objfile->per_bfd->has_section_at_zero
8948 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 8949 }
98bfdba5 8950
8566b89b 8951 expand_psymtab (objfile);
95554aad 8952
976ca316 8953 process_cu_includes (per_objfile);
c906108c 8954}
9cdd5dbd
DE
8955\f
8956/* Reading in full CUs. */
c906108c 8957
10b3939b
DJ
8958/* Add PER_CU to the queue. */
8959
8960static void
120ce1b5
SM
8961queue_comp_unit (dwarf2_per_cu_data *per_cu,
8962 dwarf2_per_objfile *per_objfile,
95554aad 8963 enum language pretend_language)
10b3939b 8964{
10b3939b 8965 per_cu->queued = 1;
120ce1b5 8966 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
8967}
8968
89e63ee4
DE
8969/* If PER_CU is not yet queued, add it to the queue.
8970 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8971 dependency.
0907af0c 8972 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8973 meaning either PER_CU is already queued or it is already loaded.
8974
8975 N.B. There is an invariant here that if a CU is queued then it is loaded.
8976 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8977
8978static int
89e63ee4 8979maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
8980 dwarf2_per_cu_data *per_cu,
8981 dwarf2_per_objfile *per_objfile,
0907af0c
DE
8982 enum language pretend_language)
8983{
8984 /* We may arrive here during partial symbol reading, if we need full
8985 DIEs to process an unusual case (e.g. template arguments). Do
8986 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8987 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 8988 {
7188ed02
SM
8989 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8990
8991 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
8992 return 1;
8993 return 0;
8994 }
8995
8996 /* Mark the dependence relation so that we don't flush PER_CU
8997 too early. */
89e63ee4
DE
8998 if (dependent_cu != NULL)
8999 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9000
9001 /* If it's already on the queue, we have nothing to do. */
9002 if (per_cu->queued)
9003 return 0;
9004
9005 /* If the compilation unit is already loaded, just mark it as
9006 used. */
7188ed02
SM
9007 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9008 if (cu != nullptr)
0907af0c 9009 {
7188ed02 9010 cu->last_used = 0;
0907af0c
DE
9011 return 0;
9012 }
9013
9014 /* Add it to the queue. */
120ce1b5 9015 queue_comp_unit (per_cu, per_objfile, pretend_language);
0907af0c
DE
9016
9017 return 1;
9018}
9019
10b3939b
DJ
9020/* Process the queue. */
9021
9022static void
976ca316 9023process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 9024{
b4f54984 9025 if (dwarf_read_debug)
45cfd468
DE
9026 {
9027 fprintf_unfiltered (gdb_stdlog,
9028 "Expanding one or more symtabs of objfile %s ...\n",
976ca316 9029 objfile_name (per_objfile->objfile));
45cfd468
DE
9030 }
9031
03dd20cc
DJ
9032 /* The queue starts out with one item, but following a DIE reference
9033 may load a new CU, adding it to the end of the queue. */
976ca316 9034 while (!per_objfile->per_bfd->queue.empty ())
10b3939b 9035 {
976ca316 9036 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
7188ed02 9037 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 9038
976ca316 9039 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 9040 {
976ca316 9041 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 9042
7188ed02
SM
9043 /* Skip dummy CUs. */
9044 if (cu != nullptr)
73be47f5 9045 {
7188ed02
SM
9046 unsigned int debug_print_threshold;
9047 char buf[100];
9048
9049 if (per_cu->is_debug_types)
9050 {
9051 struct signatured_type *sig_type =
9052 (struct signatured_type *) per_cu;
9053
9054 sprintf (buf, "TU %s at offset %s",
9055 hex_string (sig_type->signature),
9056 sect_offset_str (per_cu->sect_off));
9057 /* There can be 100s of TUs.
9058 Only print them in verbose mode. */
9059 debug_print_threshold = 2;
9060 }
9061 else
9062 {
9063 sprintf (buf, "CU at offset %s",
9064 sect_offset_str (per_cu->sect_off));
9065 debug_print_threshold = 1;
9066 }
247f5c4f 9067
7188ed02
SM
9068 if (dwarf_read_debug >= debug_print_threshold)
9069 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17 9070
7188ed02
SM
9071 if (per_cu->is_debug_types)
9072 process_full_type_unit (cu, item.pretend_language);
9073 else
9074 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 9075
7188ed02
SM
9076 if (dwarf_read_debug >= debug_print_threshold)
9077 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9078 }
f4dc4d17 9079 }
10b3939b 9080
7188ed02 9081 per_cu->queued = 0;
976ca316 9082 per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9083 }
9084
b4f54984 9085 if (dwarf_read_debug)
45cfd468
DE
9086 {
9087 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
976ca316 9088 objfile_name (per_objfile->objfile));
45cfd468 9089 }
10b3939b
DJ
9090}
9091
10b3939b
DJ
9092/* Read in full symbols for PST, and anything it depends on. */
9093
8566b89b
TT
9094void
9095dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9096{
af758d11 9097 gdb_assert (!readin_p (objfile));
95554aad 9098
17ee85fc
TT
9099 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9100 free_cached_comp_units freer (per_objfile);
48993951 9101 expand_dependencies (objfile);
aaa75496 9102
97a1449a 9103 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9104 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9105}
9106
af758d11
SM
9107/* See psympriv.h. */
9108
9109bool
9110dwarf2_psymtab::readin_p (struct objfile *objfile) const
9111{
9112 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9113 return per_objfile->symtab_set_p (per_cu_data);
9114}
9115
9116/* See psympriv.h. */
9117
9118compunit_symtab *
9119dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9120{
9121 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9122 return per_objfile->get_symtab (per_cu_data);
9123}
9124
dee91e82
DE
9125/* Trivial hash function for die_info: the hash value of a DIE
9126 is its offset in .debug_info for this objfile. */
10b3939b 9127
dee91e82
DE
9128static hashval_t
9129die_hash (const void *item)
10b3939b 9130{
9a3c8263 9131 const struct die_info *die = (const struct die_info *) item;
6502dd73 9132
9c541725 9133 return to_underlying (die->sect_off);
dee91e82 9134}
63d06c5c 9135
dee91e82
DE
9136/* Trivial comparison function for die_info structures: two DIEs
9137 are equal if they have the same offset. */
98bfdba5 9138
dee91e82
DE
9139static int
9140die_eq (const void *item_lhs, const void *item_rhs)
9141{
9a3c8263
SM
9142 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9143 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9144
9c541725 9145 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9146}
c906108c 9147
c0ab21c2 9148/* Load the DIEs associated with PER_CU into memory. */
c906108c 9149
dee91e82 9150static void
ab432490
SM
9151load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9152 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
9153 bool skip_partial,
9154 enum language pretend_language)
dee91e82 9155{
c0ab21c2
TT
9156 gdb_assert (! this_cu->is_debug_types);
9157
7188ed02
SM
9158 dwarf2_cu *existing_cu = per_objfile->get_cu (this_cu);
9159 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
9160 if (reader.dummy_p)
9161 return;
9162
9163 struct dwarf2_cu *cu = reader.cu;
9164 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9165
dee91e82
DE
9166 gdb_assert (cu->die_hash == NULL);
9167 cu->die_hash =
9168 htab_create_alloc_ex (cu->header.length / 12,
9169 die_hash,
9170 die_eq,
9171 NULL,
9172 &cu->comp_unit_obstack,
9173 hashtab_obstack_allocate,
9174 dummy_obstack_deallocate);
e142c38c 9175
3e225074 9176 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9177 reader.comp_unit_die->child
9178 = read_die_and_siblings (&reader, reader.info_ptr,
9179 &info_ptr, reader.comp_unit_die);
9180 cu->dies = reader.comp_unit_die;
dee91e82 9181 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9182
9183 /* We try not to read any attributes in this function, because not
9cdd5dbd 9184 all CUs needed for references have been loaded yet, and symbol
10b3939b 9185 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9186 or we won't be able to build types correctly.
9187 Similarly, if we do not read the producer, we can not apply
9188 producer-specific interpretation. */
c0ab21c2 9189 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9190
9191 reader.keep ();
10b3939b
DJ
9192}
9193
3da10d80
KS
9194/* Add a DIE to the delayed physname list. */
9195
9196static void
9197add_to_method_list (struct type *type, int fnfield_index, int index,
9198 const char *name, struct die_info *die,
9199 struct dwarf2_cu *cu)
9200{
9201 struct delayed_method_info mi;
9202 mi.type = type;
9203 mi.fnfield_index = fnfield_index;
9204 mi.index = index;
9205 mi.name = name;
9206 mi.die = die;
c89b44cd 9207 cu->method_list.push_back (mi);
3da10d80
KS
9208}
9209
3693fdb3
PA
9210/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9211 "const" / "volatile". If so, decrements LEN by the length of the
9212 modifier and return true. Otherwise return false. */
9213
9214template<size_t N>
9215static bool
9216check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9217{
9218 size_t mod_len = sizeof (mod) - 1;
9219 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9220 {
9221 len -= mod_len;
9222 return true;
9223 }
9224 return false;
9225}
9226
3da10d80
KS
9227/* Compute the physnames of any methods on the CU's method list.
9228
9229 The computation of method physnames is delayed in order to avoid the
9230 (bad) condition that one of the method's formal parameters is of an as yet
9231 incomplete type. */
9232
9233static void
9234compute_delayed_physnames (struct dwarf2_cu *cu)
9235{
3693fdb3 9236 /* Only C++ delays computing physnames. */
c89b44cd 9237 if (cu->method_list.empty ())
3693fdb3
PA
9238 return;
9239 gdb_assert (cu->language == language_cplus);
9240
52941706 9241 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9242 {
1d06ead6 9243 const char *physname;
3da10d80 9244 struct fn_fieldlist *fn_flp
c89b44cd
TT
9245 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9246 physname = dwarf2_physname (mi.name, mi.die, cu);
9247 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9248 = physname ? physname : "";
3693fdb3
PA
9249
9250 /* Since there's no tag to indicate whether a method is a
9251 const/volatile overload, extract that information out of the
9252 demangled name. */
9253 if (physname != NULL)
9254 {
9255 size_t len = strlen (physname);
9256
9257 while (1)
9258 {
9259 if (physname[len] == ')') /* shortcut */
9260 break;
9261 else if (check_modifier (physname, len, " const"))
c89b44cd 9262 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9263 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9264 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9265 else
9266 break;
9267 }
9268 }
3da10d80 9269 }
c89b44cd
TT
9270
9271 /* The list is no longer needed. */
9272 cu->method_list.clear ();
3da10d80
KS
9273}
9274
a766d390
DE
9275/* Go objects should be embedded in a DW_TAG_module DIE,
9276 and it's not clear if/how imported objects will appear.
9277 To keep Go support simple until that's worked out,
9278 go back through what we've read and create something usable.
9279 We could do this while processing each DIE, and feels kinda cleaner,
9280 but that way is more invasive.
9281 This is to, for example, allow the user to type "p var" or "b main"
9282 without having to specify the package name, and allow lookups
9283 of module.object to work in contexts that use the expression
9284 parser. */
9285
9286static void
9287fixup_go_packaging (struct dwarf2_cu *cu)
9288{
421d1616 9289 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9290 struct pending *list;
9291 int i;
9292
c24bdb02 9293 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9294 list != NULL;
9295 list = list->next)
a766d390
DE
9296 {
9297 for (i = 0; i < list->nsyms; ++i)
9298 {
9299 struct symbol *sym = list->symbol[i];
9300
c1b5c1eb 9301 if (sym->language () == language_go
a766d390
DE
9302 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9303 {
421d1616
TT
9304 gdb::unique_xmalloc_ptr<char> this_package_name
9305 (go_symbol_package_name (sym));
a766d390
DE
9306
9307 if (this_package_name == NULL)
9308 continue;
9309 if (package_name == NULL)
421d1616 9310 package_name = std::move (this_package_name);
a766d390
DE
9311 else
9312 {
5e22e966 9313 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9314 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9315 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9316 (symbol_symtab (sym) != NULL
9317 ? symtab_to_filename_for_display
9318 (symbol_symtab (sym))
e3b94546 9319 : objfile_name (objfile)),
421d1616 9320 this_package_name.get (), package_name.get ());
a766d390
DE
9321 }
9322 }
9323 }
9324 }
9325
9326 if (package_name != NULL)
9327 {
5e22e966 9328 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9329 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9330 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9331 saved_package_name);
a766d390
DE
9332 struct symbol *sym;
9333
8c14c3a3 9334 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9335 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9336 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9337 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9338 e.g., "main" finds the "main" module and not C's main(). */
9339 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9340 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9341 SYMBOL_TYPE (sym) = type;
9342
c24bdb02 9343 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9344 }
9345}
9346
c9317f21
TT
9347/* Allocate a fully-qualified name consisting of the two parts on the
9348 obstack. */
9349
9350static const char *
9351rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9352{
9353 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9354}
9355
9c6a1327
TT
9356/* A helper that allocates a variant part to attach to a Rust enum
9357 type. OBSTACK is where the results should be allocated. TYPE is
9358 the type we're processing. DISCRIMINANT_INDEX is the index of the
9359 discriminant. It must be the index of one of the fields of TYPE.
9360 DEFAULT_INDEX is the index of the default field; or -1 if there is
9361 no default. RANGES is indexed by "effective" field number (the
9362 field index, but omitting the discriminant and default fields) and
9363 must hold the discriminant values used by the variants. Note that
9364 RANGES must have a lifetime at least as long as OBSTACK -- either
9365 already allocated on it, or static. */
c9317f21 9366
9c6a1327
TT
9367static void
9368alloc_rust_variant (struct obstack *obstack, struct type *type,
9369 int discriminant_index, int default_index,
9370 gdb::array_view<discriminant_range> ranges)
9371{
9372 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9373 must be handled by the caller. */
9374 gdb_assert (discriminant_index >= 0
1f704f76 9375 && discriminant_index < type->num_fields ());
c9317f21 9376 gdb_assert (default_index == -1
1f704f76 9377 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9378
9c6a1327 9379 /* We have one variant for each non-discriminant field. */
1f704f76 9380 int n_variants = type->num_fields () - 1;
c9317f21 9381
9c6a1327
TT
9382 variant *variants = new (obstack) variant[n_variants];
9383 int var_idx = 0;
9384 int range_idx = 0;
1f704f76 9385 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9386 {
9387 if (i == discriminant_index)
9388 continue;
c9317f21 9389
9c6a1327
TT
9390 variants[var_idx].first_field = i;
9391 variants[var_idx].last_field = i + 1;
9392
9393 /* The default field does not need a range, but other fields do.
9394 We skipped the discriminant above. */
9395 if (i != default_index)
9396 {
9397 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9398 ++range_idx;
9399 }
c9317f21 9400
9c6a1327
TT
9401 ++var_idx;
9402 }
9403
9404 gdb_assert (range_idx == ranges.size ());
9405 gdb_assert (var_idx == n_variants);
9406
9407 variant_part *part = new (obstack) variant_part;
9408 part->discriminant_index = discriminant_index;
9409 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9410 discriminant_index));
9411 part->variants = gdb::array_view<variant> (variants, n_variants);
9412
9413 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9414 gdb::array_view<variant_part> *prop_value
9415 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9416
9c6a1327
TT
9417 struct dynamic_prop prop;
9418 prop.kind = PROP_VARIANT_PARTS;
9419 prop.data.variant_parts = prop_value;
9420
5c54719c 9421 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9422}
9423
9424/* Some versions of rustc emitted enums in an unusual way.
9425
9426 Ordinary enums were emitted as unions. The first element of each
9427 structure in the union was named "RUST$ENUM$DISR". This element
9428 held the discriminant.
9429
9430 These versions of Rust also implemented the "non-zero"
9431 optimization. When the enum had two values, and one is empty and
9432 the other holds a pointer that cannot be zero, the pointer is used
9433 as the discriminant, with a zero value meaning the empty variant.
9434 Here, the union's first member is of the form
9435 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9436 where the fieldnos are the indices of the fields that should be
9437 traversed in order to find the field (which may be several fields deep)
9438 and the variantname is the name of the variant of the case when the
9439 field is zero.
9440
9441 This function recognizes whether TYPE is of one of these forms,
9442 and, if so, smashes it to be a variant type. */
9443
9444static void
9445quirk_rust_enum (struct type *type, struct objfile *objfile)
9446{
78134374 9447 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9448
9449 /* We don't need to deal with empty enums. */
1f704f76 9450 if (type->num_fields () == 0)
c9317f21
TT
9451 return;
9452
9453#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9454 if (type->num_fields () == 1
c9317f21
TT
9455 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9456 {
9457 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9458
9459 /* Decode the field name to find the offset of the
9460 discriminant. */
9461 ULONGEST bit_offset = 0;
9462 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9463 while (name[0] >= '0' && name[0] <= '9')
9464 {
9465 char *tail;
9466 unsigned long index = strtoul (name, &tail, 10);
9467 name = tail;
9468 if (*name != '$'
1f704f76 9469 || index >= field_type->num_fields ()
c9317f21
TT
9470 || (TYPE_FIELD_LOC_KIND (field_type, index)
9471 != FIELD_LOC_KIND_BITPOS))
9472 {
b98664d3 9473 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9474 "[in module %s]"),
9475 TYPE_FIELD_NAME (type, 0),
9476 objfile_name (objfile));
9477 return;
9478 }
9479 ++name;
9480
9481 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9482 field_type = TYPE_FIELD_TYPE (field_type, index);
9483 }
9484
9c6a1327
TT
9485 /* Smash this type to be a structure type. We have to do this
9486 because the type has already been recorded. */
67607e24 9487 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9488 type->set_num_fields (3);
9c6a1327 9489 /* Save the field we care about. */
ceacbf6e 9490 struct field saved_field = type->field (0);
3cabb6b0
SM
9491 type->set_fields
9492 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9493
9c6a1327
TT
9494 /* Put the discriminant at index 0. */
9495 TYPE_FIELD_TYPE (type, 0) = field_type;
9496 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9497 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9498 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9499
9500 /* The order of fields doesn't really matter, so put the real
9501 field at index 1 and the data-less field at index 2. */
ceacbf6e 9502 type->field (1) = saved_field;
9c6a1327 9503 TYPE_FIELD_NAME (type, 1)
7d93a1e0 9504 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
d0e39ea2 9505 TYPE_FIELD_TYPE (type, 1)->set_name
7d93a1e0 9506 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9507 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9508
9509 const char *dataless_name
7d93a1e0 9510 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9511 name);
9512 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9513 dataless_name);
9c6a1327 9514 TYPE_FIELD_TYPE (type, 2) = dataless_type;
c9317f21
TT
9515 /* NAME points into the original discriminant name, which
9516 already has the correct lifetime. */
9c6a1327 9517 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9518 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9519
9c6a1327
TT
9520 /* Indicate that this is a variant type. */
9521 static discriminant_range ranges[1] = { { 0, 0 } };
9522 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9523 }
77c2dba3
TT
9524 /* A union with a single anonymous field is probably an old-style
9525 univariant enum. */
1f704f76 9526 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9527 {
c9317f21
TT
9528 /* Smash this type to be a structure type. We have to do this
9529 because the type has already been recorded. */
67607e24 9530 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9531
9c6a1327 9532 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
c9317f21 9533 const char *variant_name
7d93a1e0 9534 = rust_last_path_segment (field_type->name ());
9c6a1327 9535 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9536 field_type->set_name
9537 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9538 type->name (), variant_name));
c9317f21
TT
9539 }
9540 else
9541 {
9542 struct type *disr_type = nullptr;
1f704f76 9543 for (int i = 0; i < type->num_fields (); ++i)
c9317f21
TT
9544 {
9545 disr_type = TYPE_FIELD_TYPE (type, i);
9546
78134374 9547 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9548 {
9549 /* All fields of a true enum will be structs. */
9550 return;
9551 }
1f704f76 9552 else if (disr_type->num_fields () == 0)
c9317f21
TT
9553 {
9554 /* Could be data-less variant, so keep going. */
a037790e 9555 disr_type = nullptr;
c9317f21
TT
9556 }
9557 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9558 "RUST$ENUM$DISR") != 0)
9559 {
9560 /* Not a Rust enum. */
9561 return;
9562 }
9563 else
9564 {
9565 /* Found one. */
9566 break;
9567 }
9568 }
9569
9570 /* If we got here without a discriminant, then it's probably
9571 just a union. */
9572 if (disr_type == nullptr)
9573 return;
9574
9575 /* Smash this type to be a structure type. We have to do this
9576 because the type has already been recorded. */
67607e24 9577 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9578
9c6a1327 9579 /* Make space for the discriminant field. */
ceacbf6e 9580 struct field *disr_field = &disr_type->field (0);
9c6a1327 9581 field *new_fields
1f704f76 9582 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9583 * sizeof (struct field)));
80fc5e77 9584 memcpy (new_fields + 1, type->fields (),
1f704f76 9585 type->num_fields () * sizeof (struct field));
3cabb6b0 9586 type->set_fields (new_fields);
1f704f76 9587 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9588
9589 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9590 type->field (0) = *disr_field;
9c6a1327
TT
9591 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9592 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9593
9594 /* We need a way to find the correct discriminant given a
9595 variant name. For convenience we build a map here. */
9596 struct type *enum_type = FIELD_TYPE (*disr_field);
9597 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9598 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9599 {
9600 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9601 {
9602 const char *name
9603 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9604 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9605 }
9606 }
9607
1f704f76 9608 int n_fields = type->num_fields ();
9c6a1327
TT
9609 /* We don't need a range entry for the discriminant, but we do
9610 need one for every other field, as there is no default
9611 variant. */
9612 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9613 discriminant_range,
9614 n_fields - 1);
c9317f21
TT
9615 /* Skip the discriminant here. */
9616 for (int i = 1; i < n_fields; ++i)
9617 {
9618 /* Find the final word in the name of this variant's type.
9619 That name can be used to look up the correct
9620 discriminant. */
9621 const char *variant_name
7d93a1e0 9622 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
c9317f21
TT
9623
9624 auto iter = discriminant_map.find (variant_name);
9625 if (iter != discriminant_map.end ())
9c6a1327
TT
9626 {
9627 ranges[i].low = iter->second;
9628 ranges[i].high = iter->second;
9629 }
c9317f21 9630
bedda9ac 9631 /* Remove the discriminant field, if it exists. */
9c6a1327 9632 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
1f704f76 9633 if (sub_type->num_fields () > 0)
bedda9ac 9634 {
5e33d5f4 9635 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9636 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9637 }
9c6a1327 9638 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9639 sub_type->set_name
9640 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9641 type->name (), variant_name));
c9317f21 9642 }
9c6a1327
TT
9643
9644 /* Indicate that this is a variant type. */
9645 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9646 gdb::array_view<discriminant_range> (ranges,
9647 n_fields - 1));
c9317f21
TT
9648 }
9649}
9650
9651/* Rewrite some Rust unions to be structures with variants parts. */
9652
9653static void
9654rust_union_quirks (struct dwarf2_cu *cu)
9655{
9656 gdb_assert (cu->language == language_rust);
52941706 9657 for (type *type_ : cu->rust_unions)
5e22e966 9658 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9659 /* We don't need this any more. */
9660 cu->rust_unions.clear ();
c9317f21
TT
9661}
9662
8adb8487
TT
9663/* See read.h. */
9664
9665type_unit_group_unshareable *
9666dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9667{
9668 auto iter = this->m_type_units.find (tu_group);
9669 if (iter != this->m_type_units.end ())
9670 return iter->second.get ();
9671
9672 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9673 type_unit_group_unshareable *result = uniq.get ();
9674 this->m_type_units[tu_group] = std::move (uniq);
9675 return result;
9676}
9677
e286671b
TT
9678struct type *
9679dwarf2_per_objfile::get_type_for_signatured_type
9680 (signatured_type *sig_type) const
9681{
9682 auto iter = this->m_type_map.find (sig_type);
9683 if (iter == this->m_type_map.end ())
9684 return nullptr;
9685
9686 return iter->second;
9687}
9688
9689void dwarf2_per_objfile::set_type_for_signatured_type
9690 (signatured_type *sig_type, struct type *type)
9691{
9692 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9693
9694 this->m_type_map[sig_type] = type;
9695}
9696
95554aad
TT
9697/* A helper function for computing the list of all symbol tables
9698 included by PER_CU. */
9699
9700static void
4c39bc03 9701recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9702 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9703 dwarf2_per_cu_data *per_cu,
9704 dwarf2_per_objfile *per_objfile,
43f3e411 9705 struct compunit_symtab *immediate_parent)
95554aad 9706{
af758d11 9707 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9708 if (*slot != NULL)
9709 {
9710 /* This inclusion and its children have been processed. */
9711 return;
9712 }
9713
9714 *slot = per_cu;
af758d11 9715
95554aad 9716 /* Only add a CU if it has a symbol table. */
43182c09 9717 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9718 if (cust != NULL)
ec94af83
DE
9719 {
9720 /* If this is a type unit only add its symbol table if we haven't
9721 seen it yet (type unit per_cu's can share symtabs). */
9722 if (per_cu->is_debug_types)
9723 {
43f3e411 9724 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9725 if (*slot == NULL)
9726 {
43f3e411 9727 *slot = cust;
4c39bc03 9728 result->push_back (cust);
43f3e411
DE
9729 if (cust->user == NULL)
9730 cust->user = immediate_parent;
ec94af83
DE
9731 }
9732 }
9733 else
f9125b6c 9734 {
4c39bc03 9735 result->push_back (cust);
43f3e411
DE
9736 if (cust->user == NULL)
9737 cust->user = immediate_parent;
f9125b6c 9738 }
ec94af83 9739 }
95554aad 9740
ae640021
AB
9741 if (!per_cu->imported_symtabs_empty ())
9742 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9743 {
9744 recursively_compute_inclusions (result, all_children,
43182c09
SM
9745 all_type_symtabs, ptr, per_objfile,
9746 cust);
ae640021 9747 }
95554aad
TT
9748}
9749
43f3e411 9750/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9751 PER_CU. */
9752
9753static void
43182c09
SM
9754compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9755 dwarf2_per_objfile *per_objfile)
95554aad 9756{
f4dc4d17
DE
9757 gdb_assert (! per_cu->is_debug_types);
9758
ae640021 9759 if (!per_cu->imported_symtabs_empty ())
95554aad 9760 {
ae640021 9761 int len;
4c39bc03 9762 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9763 htab_t all_children, all_type_symtabs;
43182c09 9764 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9765
9766 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9767 if (cust == NULL)
95554aad
TT
9768 return;
9769
9770 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9771 NULL, xcalloc, xfree);
ec94af83
DE
9772 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9773 NULL, xcalloc, xfree);
95554aad 9774
ae640021 9775 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9776 {
9777 recursively_compute_inclusions (&result_symtabs, all_children,
43182c09
SM
9778 all_type_symtabs, ptr, per_objfile,
9779 cust);
ec94af83 9780 }
95554aad 9781
ec94af83 9782 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9783 len = result_symtabs.size ();
43f3e411 9784 cust->includes
f6e649dd 9785 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9786 struct compunit_symtab *, len + 1);
4c39bc03
TT
9787 memcpy (cust->includes, result_symtabs.data (),
9788 len * sizeof (compunit_symtab *));
43f3e411 9789 cust->includes[len] = NULL;
95554aad 9790
95554aad 9791 htab_delete (all_children);
ec94af83 9792 htab_delete (all_type_symtabs);
95554aad
TT
9793 }
9794}
9795
9796/* Compute the 'includes' field for the symtabs of all the CUs we just
9797 read. */
9798
9799static void
976ca316 9800process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9801{
976ca316 9802 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9803 {
9804 if (! iter->is_debug_types)
976ca316 9805 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9806 }
95554aad 9807
976ca316 9808 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9809}
9810
8fc0b21d 9811/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9812 already been loaded into memory. */
9813
9814static void
8fc0b21d 9815process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9816{
976ca316
SM
9817 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9818 struct objfile *objfile = per_objfile->objfile;
08feed99 9819 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9820 CORE_ADDR lowpc, highpc;
43f3e411 9821 struct compunit_symtab *cust;
10b3939b 9822 CORE_ADDR baseaddr;
4359dff1 9823 struct block *static_block;
3e29f34a 9824 CORE_ADDR addr;
10b3939b 9825
b3b3bada 9826 baseaddr = objfile->text_section_offset ();
10b3939b 9827
c89b44cd
TT
9828 /* Clear the list here in case something was left over. */
9829 cu->method_list.clear ();
10b3939b 9830
95554aad
TT
9831 cu->language = pretend_language;
9832 cu->language_defn = language_def (cu->language);
9833
c906108c 9834 /* Do line number decoding in read_file_scope () */
10b3939b 9835 process_die (cu->dies, cu);
c906108c 9836
a766d390
DE
9837 /* For now fudge the Go package. */
9838 if (cu->language == language_go)
9839 fixup_go_packaging (cu);
9840
5f48f8f3 9841 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9842 should be complete, and it should now be safe to compute all of the
9843 physnames. */
9844 compute_delayed_physnames (cu);
3da10d80 9845
c9317f21
TT
9846 if (cu->language == language_rust)
9847 rust_union_quirks (cu);
9848
fae299cd
DC
9849 /* Some compilers don't define a DW_AT_high_pc attribute for the
9850 compilation unit. If the DW_AT_high_pc is missing, synthesize
9851 it, by scanning the DIE's below the compilation unit. */
10b3939b 9852 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9853
3e29f34a 9854 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9855 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9856
9857 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9858 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9859 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9860 addrmap to help ensure it has an accurate map of pc values belonging to
9861 this comp unit. */
9862 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9863
c24bdb02 9864 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9865 SECT_OFF_TEXT (objfile),
9866 0);
c906108c 9867
43f3e411 9868 if (cust != NULL)
c906108c 9869 {
df15bd07 9870 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9871
8be455d7
JK
9872 /* Set symtab language to language from DW_AT_language. If the
9873 compilation is from a C file generated by language preprocessors, do
9874 not set the language if it was already deduced by start_subfile. */
43f3e411 9875 if (!(cu->language == language_c
40e3ad0e 9876 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9877 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9878
9879 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9880 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9881 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9882 there were bugs in prologue debug info, fixed later in GCC-4.5
9883 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9884
9885 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9886 needed, it would be wrong due to missing DW_AT_producer there.
9887
9888 Still one can confuse GDB by using non-standard GCC compilation
9889 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9890 */
ab260dad 9891 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9892 cust->locations_valid = 1;
e0d00bc7
JK
9893
9894 if (gcc_4_minor >= 5)
43f3e411 9895 cust->epilogue_unwind_valid = 1;
96408a79 9896
43f3e411 9897 cust->call_site_htab = cu->call_site_htab;
c906108c 9898 }
9291a0cd 9899
976ca316 9900 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 9901
95554aad 9902 /* Push it for inclusion processing later. */
976ca316 9903 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
9904
9905 /* Not needed any more. */
c24bdb02 9906 cu->reset_builder ();
f4dc4d17 9907}
45cfd468 9908
8fc0b21d 9909/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
9910 already been loaded into memory. */
9911
9912static void
8fc0b21d 9913process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
9914 enum language pretend_language)
9915{
976ca316
SM
9916 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9917 struct objfile *objfile = per_objfile->objfile;
43f3e411 9918 struct compunit_symtab *cust;
0186c6a7
DE
9919 struct signatured_type *sig_type;
9920
8fc0b21d
SM
9921 gdb_assert (cu->per_cu->is_debug_types);
9922 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 9923
c89b44cd
TT
9924 /* Clear the list here in case something was left over. */
9925 cu->method_list.clear ();
f4dc4d17 9926
f4dc4d17
DE
9927 cu->language = pretend_language;
9928 cu->language_defn = language_def (cu->language);
9929
9930 /* The symbol tables are set up in read_type_unit_scope. */
9931 process_die (cu->dies, cu);
9932
9933 /* For now fudge the Go package. */
9934 if (cu->language == language_go)
9935 fixup_go_packaging (cu);
9936
5f48f8f3 9937 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9938 should be complete, and it should now be safe to compute all of the
9939 physnames. */
9940 compute_delayed_physnames (cu);
f4dc4d17 9941
c9317f21
TT
9942 if (cu->language == language_rust)
9943 rust_union_quirks (cu);
9944
f4dc4d17
DE
9945 /* TUs share symbol tables.
9946 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9947 of it with end_expandable_symtab. Otherwise, complete the addition of
9948 this TU's symbols to the existing symtab. */
8adb8487 9949 type_unit_group_unshareable *tug_unshare =
976ca316 9950 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 9951 if (tug_unshare->compunit_symtab == NULL)
45cfd468 9952 {
c24bdb02
KS
9953 buildsym_compunit *builder = cu->get_builder ();
9954 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 9955 tug_unshare->compunit_symtab = cust;
f4dc4d17 9956
43f3e411 9957 if (cust != NULL)
f4dc4d17
DE
9958 {
9959 /* Set symtab language to language from DW_AT_language. If the
9960 compilation is from a C file generated by language preprocessors,
9961 do not set the language if it was already deduced by
9962 start_subfile. */
43f3e411
DE
9963 if (!(cu->language == language_c
9964 && COMPUNIT_FILETABS (cust)->language != language_c))
9965 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9966 }
9967 }
9968 else
9969 {
c24bdb02 9970 cu->get_builder ()->augment_type_symtab ();
8adb8487 9971 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
9972 }
9973
976ca316 9974 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
9975
9976 /* Not needed any more. */
c24bdb02 9977 cu->reset_builder ();
c906108c
SS
9978}
9979
95554aad
TT
9980/* Process an imported unit DIE. */
9981
9982static void
9983process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9984{
9985 struct attribute *attr;
9986
f4dc4d17
DE
9987 /* For now we don't handle imported units in type units. */
9988 if (cu->per_cu->is_debug_types)
9989 {
9990 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9991 " supported in type units [in module %s]"),
5e22e966 9992 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
9993 }
9994
95554aad
TT
9995 attr = dwarf2_attr (die, DW_AT_import, cu);
9996 if (attr != NULL)
9997 {
0826b30a 9998 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 9999 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 10000 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 10001 dwarf2_per_cu_data *per_cu
ab432490 10002 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 10003
58990295
TV
10004 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10005 into another compilation unit, at root level. Regard this as a hint,
10006 and ignore it. */
10007 if (die->parent && die->parent->parent == NULL
10008 && per_cu->unit_type == DW_UT_compile
10009 && per_cu->lang == language_cplus)
10010 return;
10011
69d751e3 10012 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 10013 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
ab432490 10014 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
95554aad 10015
ae640021 10016 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10017 }
10018}
10019
4c8aa72d
PA
10020/* RAII object that represents a process_die scope: i.e.,
10021 starts/finishes processing a DIE. */
10022class process_die_scope
adde2bff 10023{
4c8aa72d
PA
10024public:
10025 process_die_scope (die_info *die, dwarf2_cu *cu)
10026 : m_die (die), m_cu (cu)
10027 {
10028 /* We should only be processing DIEs not already in process. */
10029 gdb_assert (!m_die->in_process);
10030 m_die->in_process = true;
10031 }
8c3cb9fa 10032
4c8aa72d
PA
10033 ~process_die_scope ()
10034 {
10035 m_die->in_process = false;
10036
10037 /* If we're done processing the DIE for the CU that owns the line
10038 header, we don't need the line header anymore. */
10039 if (m_cu->line_header_die_owner == m_die)
10040 {
10041 delete m_cu->line_header;
10042 m_cu->line_header = NULL;
10043 m_cu->line_header_die_owner = NULL;
10044 }
10045 }
10046
10047private:
10048 die_info *m_die;
10049 dwarf2_cu *m_cu;
10050};
adde2bff 10051
c906108c
SS
10052/* Process a die and its children. */
10053
10054static void
e7c27a73 10055process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10056{
4c8aa72d 10057 process_die_scope scope (die, cu);
adde2bff 10058
c906108c
SS
10059 switch (die->tag)
10060 {
10061 case DW_TAG_padding:
10062 break;
10063 case DW_TAG_compile_unit:
95554aad 10064 case DW_TAG_partial_unit:
e7c27a73 10065 read_file_scope (die, cu);
c906108c 10066 break;
348e048f
DE
10067 case DW_TAG_type_unit:
10068 read_type_unit_scope (die, cu);
10069 break;
c906108c 10070 case DW_TAG_subprogram:
0a4b0913
AB
10071 /* Nested subprograms in Fortran get a prefix. */
10072 if (cu->language == language_fortran
10073 && die->parent != NULL
10074 && die->parent->tag == DW_TAG_subprogram)
10075 cu->processing_has_namespace_info = true;
10076 /* Fall through. */
c906108c 10077 case DW_TAG_inlined_subroutine:
edb3359d 10078 read_func_scope (die, cu);
c906108c
SS
10079 break;
10080 case DW_TAG_lexical_block:
14898363
L
10081 case DW_TAG_try_block:
10082 case DW_TAG_catch_block:
e7c27a73 10083 read_lexical_block_scope (die, cu);
c906108c 10084 break;
216f72a1 10085 case DW_TAG_call_site:
96408a79
SA
10086 case DW_TAG_GNU_call_site:
10087 read_call_site_scope (die, cu);
10088 break;
c906108c 10089 case DW_TAG_class_type:
680b30c7 10090 case DW_TAG_interface_type:
c906108c
SS
10091 case DW_TAG_structure_type:
10092 case DW_TAG_union_type:
134d01f1 10093 process_structure_scope (die, cu);
c906108c
SS
10094 break;
10095 case DW_TAG_enumeration_type:
134d01f1 10096 process_enumeration_scope (die, cu);
c906108c 10097 break;
134d01f1 10098
f792889a
DJ
10099 /* These dies have a type, but processing them does not create
10100 a symbol or recurse to process the children. Therefore we can
10101 read them on-demand through read_type_die. */
c906108c 10102 case DW_TAG_subroutine_type:
72019c9c 10103 case DW_TAG_set_type:
c906108c 10104 case DW_TAG_array_type:
c906108c 10105 case DW_TAG_pointer_type:
c906108c 10106 case DW_TAG_ptr_to_member_type:
c906108c 10107 case DW_TAG_reference_type:
4297a3f0 10108 case DW_TAG_rvalue_reference_type:
c906108c 10109 case DW_TAG_string_type:
c906108c 10110 break;
134d01f1 10111
c906108c 10112 case DW_TAG_base_type:
a02abb62 10113 case DW_TAG_subrange_type:
cb249c71 10114 case DW_TAG_typedef:
134d01f1
DJ
10115 /* Add a typedef symbol for the type definition, if it has a
10116 DW_AT_name. */
f792889a 10117 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10118 break;
c906108c 10119 case DW_TAG_common_block:
e7c27a73 10120 read_common_block (die, cu);
c906108c
SS
10121 break;
10122 case DW_TAG_common_inclusion:
10123 break;
d9fa45fe 10124 case DW_TAG_namespace:
9068261f 10125 cu->processing_has_namespace_info = true;
e7c27a73 10126 read_namespace (die, cu);
d9fa45fe 10127 break;
5d7cb8df 10128 case DW_TAG_module:
9068261f 10129 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10130 read_module (die, cu);
10131 break;
d9fa45fe 10132 case DW_TAG_imported_declaration:
9068261f 10133 cu->processing_has_namespace_info = true;
74921315
KS
10134 if (read_namespace_alias (die, cu))
10135 break;
86a73007
TT
10136 /* The declaration is not a global namespace alias. */
10137 /* Fall through. */
d9fa45fe 10138 case DW_TAG_imported_module:
9068261f 10139 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10140 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10141 || cu->language != language_fortran))
b98664d3 10142 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10143 dwarf_tag_name (die->tag));
10144 read_import_statement (die, cu);
d9fa45fe 10145 break;
95554aad
TT
10146
10147 case DW_TAG_imported_unit:
10148 process_imported_unit_die (die, cu);
10149 break;
10150
71a3c369
TT
10151 case DW_TAG_variable:
10152 read_variable (die, cu);
10153 break;
10154
c906108c 10155 default:
e7c27a73 10156 new_symbol (die, NULL, cu);
c906108c
SS
10157 break;
10158 }
10159}
ca69b9e6
DE
10160\f
10161/* DWARF name computation. */
c906108c 10162
94af9270
KS
10163/* A helper function for dwarf2_compute_name which determines whether DIE
10164 needs to have the name of the scope prepended to the name listed in the
10165 die. */
10166
10167static int
10168die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10169{
1c809c68
TT
10170 struct attribute *attr;
10171
94af9270
KS
10172 switch (die->tag)
10173 {
10174 case DW_TAG_namespace:
10175 case DW_TAG_typedef:
10176 case DW_TAG_class_type:
10177 case DW_TAG_interface_type:
10178 case DW_TAG_structure_type:
10179 case DW_TAG_union_type:
10180 case DW_TAG_enumeration_type:
10181 case DW_TAG_enumerator:
10182 case DW_TAG_subprogram:
08a76f8a 10183 case DW_TAG_inlined_subroutine:
94af9270 10184 case DW_TAG_member:
74921315 10185 case DW_TAG_imported_declaration:
94af9270
KS
10186 return 1;
10187
10188 case DW_TAG_variable:
c2b0a229 10189 case DW_TAG_constant:
94af9270
KS
10190 /* We only need to prefix "globally" visible variables. These include
10191 any variable marked with DW_AT_external or any variable that
10192 lives in a namespace. [Variables in anonymous namespaces
10193 require prefixing, but they are not DW_AT_external.] */
10194
10195 if (dwarf2_attr (die, DW_AT_specification, cu))
10196 {
10197 struct dwarf2_cu *spec_cu = cu;
9a619af0 10198
94af9270
KS
10199 return die_needs_namespace (die_specification (die, &spec_cu),
10200 spec_cu);
10201 }
10202
1c809c68 10203 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10204 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10205 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10206 return 0;
10207 /* A variable in a lexical block of some kind does not need a
10208 namespace, even though in C++ such variables may be external
10209 and have a mangled name. */
10210 if (die->parent->tag == DW_TAG_lexical_block
10211 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10212 || die->parent->tag == DW_TAG_catch_block
10213 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10214 return 0;
10215 return 1;
94af9270
KS
10216
10217 default:
10218 return 0;
10219 }
10220}
10221
73b9be8b
KS
10222/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10223 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10224 defined for the given DIE. */
10225
10226static struct attribute *
10227dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10228{
10229 struct attribute *attr;
10230
10231 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10232 if (attr == NULL)
10233 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10234
10235 return attr;
10236}
10237
10238/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10239 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10240 defined for the given DIE. */
10241
10242static const char *
10243dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10244{
10245 const char *linkage_name;
10246
10247 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10248 if (linkage_name == NULL)
10249 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10250
787de330
TT
10251 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10252 See https://github.com/rust-lang/rust/issues/32925. */
10253 if (cu->language == language_rust && linkage_name != NULL
10254 && strchr (linkage_name, '{') != NULL)
10255 linkage_name = NULL;
10256
73b9be8b
KS
10257 return linkage_name;
10258}
10259
94af9270 10260/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10261 compute the physname for the object, which include a method's:
9c37b5ae 10262 - formal parameters (C++),
a766d390 10263 - receiver type (Go),
a766d390
DE
10264
10265 The term "physname" is a bit confusing.
10266 For C++, for example, it is the demangled name.
10267 For Go, for example, it's the mangled name.
94af9270 10268
af6b7be1
JB
10269 For Ada, return the DIE's linkage name rather than the fully qualified
10270 name. PHYSNAME is ignored..
10271
5989a64e 10272 The result is allocated on the objfile->per_bfd's obstack and
45940949 10273 canonicalized. */
94af9270
KS
10274
10275static const char *
15d034d0
TT
10276dwarf2_compute_name (const char *name,
10277 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10278 int physname)
10279{
5e22e966 10280 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10281
94af9270
KS
10282 if (name == NULL)
10283 name = dwarf2_name (die, cu);
10284
2ee7123e
DE
10285 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10286 but otherwise compute it by typename_concat inside GDB.
10287 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10288 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10289 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10290 will set the demangled name to the result of dwarf2_full_name, and it is
10291 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10292 if (cu->language == language_ada
10293 || (cu->language == language_fortran && physname))
10294 {
10295 /* For Ada unit, we prefer the linkage name over the name, as
10296 the former contains the exported name, which the user expects
10297 to be able to reference. Ideally, we want the user to be able
10298 to reference this entity using either natural or linkage name,
10299 but we haven't started looking at this enhancement yet. */
73b9be8b 10300 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10301
2ee7123e
DE
10302 if (linkage_name != NULL)
10303 return linkage_name;
f55ee35c
JK
10304 }
10305
94af9270
KS
10306 /* These are the only languages we know how to qualify names in. */
10307 if (name != NULL
9c37b5ae 10308 && (cu->language == language_cplus
c44af4eb
TT
10309 || cu->language == language_fortran || cu->language == language_d
10310 || cu->language == language_rust))
94af9270
KS
10311 {
10312 if (die_needs_namespace (die, cu))
10313 {
0d5cff50 10314 const char *prefix;
34a68019 10315 const char *canonical_name = NULL;
94af9270 10316
d7e74731
PA
10317 string_file buf;
10318
94af9270 10319 prefix = determine_prefix (die, cu);
94af9270
KS
10320 if (*prefix != '\0')
10321 {
43816ebc
TT
10322 gdb::unique_xmalloc_ptr<char> prefixed_name
10323 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10324
43816ebc 10325 buf.puts (prefixed_name.get ());
94af9270
KS
10326 }
10327 else
d7e74731 10328 buf.puts (name);
94af9270 10329
98bfdba5
PA
10330 /* Template parameters may be specified in the DIE's DW_AT_name, or
10331 as children with DW_TAG_template_type_param or
10332 DW_TAG_value_type_param. If the latter, add them to the name
10333 here. If the name already has template parameters, then
10334 skip this step; some versions of GCC emit both, and
10335 it is more efficient to use the pre-computed name.
10336
10337 Something to keep in mind about this process: it is very
10338 unlikely, or in some cases downright impossible, to produce
10339 something that will match the mangled name of a function.
10340 If the definition of the function has the same debug info,
10341 we should be able to match up with it anyway. But fallbacks
10342 using the minimal symbol, for instance to find a method
10343 implemented in a stripped copy of libstdc++, will not work.
10344 If we do not have debug info for the definition, we will have to
10345 match them up some other way.
10346
10347 When we do name matching there is a related problem with function
10348 templates; two instantiated function templates are allowed to
10349 differ only by their return types, which we do not add here. */
10350
10351 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10352 {
10353 struct attribute *attr;
10354 struct die_info *child;
10355 int first = 1;
10356
10357 die->building_fullname = 1;
10358
10359 for (child = die->child; child != NULL; child = child->sibling)
10360 {
10361 struct type *type;
12df843f 10362 LONGEST value;
d521ce57 10363 const gdb_byte *bytes;
98bfdba5
PA
10364 struct dwarf2_locexpr_baton *baton;
10365 struct value *v;
10366
10367 if (child->tag != DW_TAG_template_type_param
10368 && child->tag != DW_TAG_template_value_param)
10369 continue;
10370
10371 if (first)
10372 {
d7e74731 10373 buf.puts ("<");
98bfdba5
PA
10374 first = 0;
10375 }
10376 else
d7e74731 10377 buf.puts (", ");
98bfdba5
PA
10378
10379 attr = dwarf2_attr (child, DW_AT_type, cu);
10380 if (attr == NULL)
10381 {
b98664d3 10382 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10383 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10384 continue;
10385 }
10386 type = die_type (child, cu);
10387
10388 if (child->tag == DW_TAG_template_type_param)
10389 {
c1ec8cea
TT
10390 c_print_type (type, "", &buf, -1, 0, cu->language,
10391 &type_print_raw_options);
98bfdba5
PA
10392 continue;
10393 }
10394
10395 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10396 if (attr == NULL)
10397 {
b98664d3 10398 complaint (_("template parameter missing "
3e43a32a 10399 "DW_AT_const_value"));
d7e74731 10400 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10401 continue;
10402 }
10403
10404 dwarf2_const_value_attr (attr, type, name,
10405 &cu->comp_unit_obstack, cu,
10406 &value, &bytes, &baton);
10407
10408 if (TYPE_NOSIGN (type))
10409 /* GDB prints characters as NUMBER 'CHAR'. If that's
10410 changed, this can use value_print instead. */
d7e74731 10411 c_printchar (value, type, &buf);
98bfdba5
PA
10412 else
10413 {
10414 struct value_print_options opts;
10415
10416 if (baton != NULL)
10417 v = dwarf2_evaluate_loc_desc (type, NULL,
10418 baton->data,
10419 baton->size,
9f47c707
SM
10420 baton->per_cu,
10421 baton->per_objfile);
98bfdba5
PA
10422 else if (bytes != NULL)
10423 {
10424 v = allocate_value (type);
10425 memcpy (value_contents_writeable (v), bytes,
10426 TYPE_LENGTH (type));
10427 }
10428 else
10429 v = value_from_longest (type, value);
10430
3e43a32a
MS
10431 /* Specify decimal so that we do not depend on
10432 the radix. */
98bfdba5
PA
10433 get_formatted_print_options (&opts, 'd');
10434 opts.raw = 1;
d7e74731 10435 value_print (v, &buf, &opts);
98bfdba5 10436 release_value (v);
98bfdba5
PA
10437 }
10438 }
10439
10440 die->building_fullname = 0;
10441
10442 if (!first)
10443 {
10444 /* Close the argument list, with a space if necessary
10445 (nested templates). */
d7e74731
PA
10446 if (!buf.empty () && buf.string ().back () == '>')
10447 buf.puts (" >");
98bfdba5 10448 else
d7e74731 10449 buf.puts (">");
98bfdba5
PA
10450 }
10451 }
10452
9c37b5ae 10453 /* For C++ methods, append formal parameter type
94af9270 10454 information, if PHYSNAME. */
6e70227d 10455
94af9270 10456 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10457 && cu->language == language_cplus)
94af9270
KS
10458 {
10459 struct type *type = read_type_die (die, cu);
10460
d7e74731 10461 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10462 &type_print_raw_options);
94af9270 10463
9c37b5ae 10464 if (cu->language == language_cplus)
94af9270 10465 {
60430eff
DJ
10466 /* Assume that an artificial first parameter is
10467 "this", but do not crash if it is not. RealView
10468 marks unnamed (and thus unused) parameters as
10469 artificial; there is no way to differentiate
10470 the two cases. */
1f704f76 10471 if (type->num_fields () > 0
94af9270 10472 && TYPE_FIELD_ARTIFICIAL (type, 0)
78134374 10473 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
3e43a32a
MS
10474 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10475 0))))
d7e74731 10476 buf.puts (" const");
94af9270
KS
10477 }
10478 }
10479
d7e74731 10480 const std::string &intermediate_name = buf.string ();
94af9270
KS
10481
10482 if (cu->language == language_cplus)
34a68019 10483 canonical_name
322a8516 10484 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10485 objfile);
34a68019
TT
10486
10487 /* If we only computed INTERMEDIATE_NAME, or if
10488 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10489 intern it. */
322a8516 10490 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10491 name = objfile->intern (intermediate_name);
34a68019
TT
10492 else
10493 name = canonical_name;
94af9270
KS
10494 }
10495 }
10496
10497 return name;
10498}
10499
0114d602
DJ
10500/* Return the fully qualified name of DIE, based on its DW_AT_name.
10501 If scope qualifiers are appropriate they will be added. The result
34a68019 10502 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10503 not have a name. NAME may either be from a previous call to
10504 dwarf2_name or NULL.
10505
9c37b5ae 10506 The output string will be canonicalized (if C++). */
0114d602
DJ
10507
10508static const char *
15d034d0 10509dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10510{
94af9270
KS
10511 return dwarf2_compute_name (name, die, cu, 0);
10512}
0114d602 10513
94af9270
KS
10514/* Construct a physname for the given DIE in CU. NAME may either be
10515 from a previous call to dwarf2_name or NULL. The result will be
10516 allocated on the objfile_objstack or NULL if the DIE does not have a
10517 name.
0114d602 10518
9c37b5ae 10519 The output string will be canonicalized (if C++). */
0114d602 10520
94af9270 10521static const char *
15d034d0 10522dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10523{
5e22e966 10524 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10525 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10526 int need_copy = 1;
10527
10528 /* In this case dwarf2_compute_name is just a shortcut not building anything
10529 on its own. */
10530 if (!die_needs_namespace (die, cu))
10531 return dwarf2_compute_name (name, die, cu, 1);
10532
906bb4c5
TT
10533 if (cu->language != language_rust)
10534 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10535
10536 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10537 has computed. */
791afaa2 10538 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10539 if (mangled != NULL)
900e11f9 10540 {
900e11f9 10541
59cc4834
JB
10542 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10543 {
10544 /* Do nothing (do not demangle the symbol name). */
10545 }
10546 else if (cu->language == language_go)
a766d390 10547 {
5e2db402
TT
10548 /* This is a lie, but we already lie to the caller new_symbol.
10549 new_symbol assumes we return the mangled name.
a766d390 10550 This just undoes that lie until things are cleaned up. */
a766d390
DE
10551 }
10552 else
10553 {
0eb876f5
JB
10554 /* Use DMGL_RET_DROP for C++ template functions to suppress
10555 their return type. It is easier for GDB users to search
10556 for such functions as `name(params)' than `long name(params)'.
10557 In such case the minimal symbol names do not match the full
10558 symbol names but for template functions there is never a need
10559 to look up their definition from their declaration so
10560 the only disadvantage remains the minimal symbol variant
10561 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10562 demangled.reset (gdb_demangle (mangled,
10563 (DMGL_PARAMS | DMGL_ANSI
10564 | DMGL_RET_DROP)));
a766d390 10565 }
900e11f9 10566 if (demangled)
791afaa2 10567 canon = demangled.get ();
900e11f9
JK
10568 else
10569 {
10570 canon = mangled;
10571 need_copy = 0;
10572 }
10573 }
10574
10575 if (canon == NULL || check_physname)
10576 {
10577 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10578
10579 if (canon != NULL && strcmp (physname, canon) != 0)
10580 {
10581 /* It may not mean a bug in GDB. The compiler could also
10582 compute DW_AT_linkage_name incorrectly. But in such case
10583 GDB would need to be bug-to-bug compatible. */
10584
b98664d3 10585 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10586 "(from linkage <%s>) - DIE at %s [in module %s]"),
10587 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10588 objfile_name (objfile));
900e11f9
JK
10589
10590 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10591 is available here - over computed PHYSNAME. It is safer
10592 against both buggy GDB and buggy compilers. */
10593
10594 retval = canon;
10595 }
10596 else
10597 {
10598 retval = physname;
10599 need_copy = 0;
10600 }
10601 }
10602 else
10603 retval = canon;
10604
10605 if (need_copy)
be1e3d3e 10606 retval = objfile->intern (retval);
900e11f9 10607
900e11f9 10608 return retval;
0114d602
DJ
10609}
10610
74921315
KS
10611/* Inspect DIE in CU for a namespace alias. If one exists, record
10612 a new symbol for it.
10613
10614 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10615
10616static int
10617read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10618{
10619 struct attribute *attr;
10620
10621 /* If the die does not have a name, this is not a namespace
10622 alias. */
10623 attr = dwarf2_attr (die, DW_AT_name, cu);
10624 if (attr != NULL)
10625 {
10626 int num;
10627 struct die_info *d = die;
10628 struct dwarf2_cu *imported_cu = cu;
10629
10630 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10631 keep inspecting DIEs until we hit the underlying import. */
10632#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10633 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10634 {
10635 attr = dwarf2_attr (d, DW_AT_import, cu);
10636 if (attr == NULL)
10637 break;
10638
10639 d = follow_die_ref (d, attr, &imported_cu);
10640 if (d->tag != DW_TAG_imported_declaration)
10641 break;
10642 }
10643
10644 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10645 {
b98664d3 10646 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10647 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10648 return 0;
10649 }
10650
10651 if (attr != NULL)
10652 {
10653 struct type *type;
0826b30a 10654 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10655
aa66c379 10656 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10657 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10658 {
10659 /* This declaration is a global namespace alias. Add
10660 a symbol for it whose type is the aliased namespace. */
10661 new_symbol (die, type, cu);
10662 return 1;
10663 }
10664 }
10665 }
10666
10667 return 0;
10668}
10669
22cee43f 10670/* Return the using directives repository (global or local?) to use in the
804d2729 10671 current context for CU.
22cee43f
PMR
10672
10673 For Ada, imported declarations can materialize renamings, which *may* be
10674 global. However it is impossible (for now?) in DWARF to distinguish
10675 "external" imported declarations and "static" ones. As all imported
10676 declarations seem to be static in all other languages, make them all CU-wide
10677 global only in Ada. */
10678
10679static struct using_direct **
804d2729 10680using_directives (struct dwarf2_cu *cu)
22cee43f 10681{
c24bdb02
KS
10682 if (cu->language == language_ada
10683 && cu->get_builder ()->outermost_context_p ())
10684 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10685 else
c24bdb02 10686 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10687}
10688
27aa8d6a
SW
10689/* Read the import statement specified by the given die and record it. */
10690
10691static void
10692read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10693{
5e22e966 10694 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10695 struct attribute *import_attr;
32019081 10696 struct die_info *imported_die, *child_die;
de4affc9 10697 struct dwarf2_cu *imported_cu;
27aa8d6a 10698 const char *imported_name;
794684b6 10699 const char *imported_name_prefix;
13387711
SW
10700 const char *canonical_name;
10701 const char *import_alias;
10702 const char *imported_declaration = NULL;
794684b6 10703 const char *import_prefix;
eb1e02fd 10704 std::vector<const char *> excludes;
13387711 10705
27aa8d6a
SW
10706 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10707 if (import_attr == NULL)
10708 {
b98664d3 10709 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10710 dwarf_tag_name (die->tag));
10711 return;
10712 }
10713
de4affc9
CC
10714 imported_cu = cu;
10715 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10716 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10717 if (imported_name == NULL)
10718 {
10719 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10720
10721 The import in the following code:
10722 namespace A
10723 {
10724 typedef int B;
10725 }
10726
10727 int main ()
10728 {
10729 using A::B;
10730 B b;
10731 return b;
10732 }
10733
10734 ...
10735 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10736 <52> DW_AT_decl_file : 1
10737 <53> DW_AT_decl_line : 6
10738 <54> DW_AT_import : <0x75>
10739 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10740 <59> DW_AT_name : B
10741 <5b> DW_AT_decl_file : 1
10742 <5c> DW_AT_decl_line : 2
10743 <5d> DW_AT_type : <0x6e>
10744 ...
10745 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10746 <76> DW_AT_byte_size : 4
10747 <77> DW_AT_encoding : 5 (signed)
10748
10749 imports the wrong die ( 0x75 instead of 0x58 ).
10750 This case will be ignored until the gcc bug is fixed. */
10751 return;
10752 }
10753
82856980
SW
10754 /* Figure out the local name after import. */
10755 import_alias = dwarf2_name (die, cu);
27aa8d6a 10756
794684b6
SW
10757 /* Figure out where the statement is being imported to. */
10758 import_prefix = determine_prefix (die, cu);
10759
10760 /* Figure out what the scope of the imported die is and prepend it
10761 to the name of the imported die. */
de4affc9 10762 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10763
f55ee35c
JK
10764 if (imported_die->tag != DW_TAG_namespace
10765 && imported_die->tag != DW_TAG_module)
794684b6 10766 {
13387711
SW
10767 imported_declaration = imported_name;
10768 canonical_name = imported_name_prefix;
794684b6 10769 }
13387711 10770 else if (strlen (imported_name_prefix) > 0)
12aaed36 10771 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10772 imported_name_prefix,
10773 (cu->language == language_d ? "." : "::"),
10774 imported_name, (char *) NULL);
13387711
SW
10775 else
10776 canonical_name = imported_name;
794684b6 10777
32019081
JK
10778 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10779 for (child_die = die->child; child_die && child_die->tag;
436c571c 10780 child_die = child_die->sibling)
32019081
JK
10781 {
10782 /* DWARF-4: A Fortran use statement with a “rename list” may be
10783 represented by an imported module entry with an import attribute
10784 referring to the module and owned entries corresponding to those
10785 entities that are renamed as part of being imported. */
10786
10787 if (child_die->tag != DW_TAG_imported_declaration)
10788 {
b98664d3 10789 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10790 "- DIE at %s [in module %s]"),
10791 sect_offset_str (child_die->sect_off),
10792 objfile_name (objfile));
32019081
JK
10793 continue;
10794 }
10795
10796 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10797 if (import_attr == NULL)
10798 {
b98664d3 10799 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10800 dwarf_tag_name (child_die->tag));
10801 continue;
10802 }
10803
10804 imported_cu = cu;
10805 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10806 &imported_cu);
10807 imported_name = dwarf2_name (imported_die, imported_cu);
10808 if (imported_name == NULL)
10809 {
b98664d3 10810 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10811 "imported name - DIE at %s [in module %s]"),
10812 sect_offset_str (child_die->sect_off),
10813 objfile_name (objfile));
32019081
JK
10814 continue;
10815 }
10816
eb1e02fd 10817 excludes.push_back (imported_name);
32019081
JK
10818
10819 process_die (child_die, cu);
10820 }
10821
804d2729 10822 add_using_directive (using_directives (cu),
22cee43f
PMR
10823 import_prefix,
10824 canonical_name,
10825 import_alias,
10826 imported_declaration,
10827 excludes,
10828 0,
10829 &objfile->objfile_obstack);
27aa8d6a
SW
10830}
10831
5230b05a
WT
10832/* ICC<14 does not output the required DW_AT_declaration on incomplete
10833 types, but gives them a size of zero. Starting with version 14,
10834 ICC is compatible with GCC. */
10835
9068261f 10836static bool
5230b05a
WT
10837producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10838{
10839 if (!cu->checked_producer)
10840 check_producer (cu);
10841
10842 return cu->producer_is_icc_lt_14;
10843}
10844
eb77c9df
AB
10845/* ICC generates a DW_AT_type for C void functions. This was observed on
10846 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10847 which says that void functions should not have a DW_AT_type. */
10848
10849static bool
10850producer_is_icc (struct dwarf2_cu *cu)
10851{
10852 if (!cu->checked_producer)
10853 check_producer (cu);
10854
10855 return cu->producer_is_icc;
10856}
10857
1b80a9fa
JK
10858/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10859 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10860 this, it was first present in GCC release 4.3.0. */
10861
9068261f 10862static bool
1b80a9fa
JK
10863producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10864{
10865 if (!cu->checked_producer)
10866 check_producer (cu);
10867
10868 return cu->producer_is_gcc_lt_4_3;
10869}
10870
d721ba37
PA
10871static file_and_directory
10872find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10873{
d721ba37
PA
10874 file_and_directory res;
10875
9291a0cd
TT
10876 /* Find the filename. Do not use dwarf2_name here, since the filename
10877 is not a source language identifier. */
d721ba37
PA
10878 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10879 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10880
d721ba37
PA
10881 if (res.comp_dir == NULL
10882 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10883 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10884 {
d721ba37
PA
10885 res.comp_dir_storage = ldirname (res.name);
10886 if (!res.comp_dir_storage.empty ())
10887 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10888 }
d721ba37 10889 if (res.comp_dir != NULL)
9291a0cd
TT
10890 {
10891 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10892 directory, get rid of it. */
d721ba37 10893 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10894
d721ba37
PA
10895 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10896 res.comp_dir = cp + 1;
9291a0cd
TT
10897 }
10898
d721ba37
PA
10899 if (res.name == NULL)
10900 res.name = "<unknown>";
10901
10902 return res;
9291a0cd
TT
10903}
10904
f4dc4d17
DE
10905/* Handle DW_AT_stmt_list for a compilation unit.
10906 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10907 COMP_DIR is the compilation directory. LOWPC is passed to
10908 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10909
10910static void
10911handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10912 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10913{
976ca316 10914 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 10915 struct attribute *attr;
527f3840
JK
10916 struct line_header line_header_local;
10917 hashval_t line_header_local_hash;
527f3840
JK
10918 void **slot;
10919 int decode_mapping;
2ab95328 10920
f4dc4d17
DE
10921 gdb_assert (! cu->per_cu->is_debug_types);
10922
2ab95328 10923 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10924 if (attr == NULL)
10925 return;
10926
9c541725 10927 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10928
10929 /* The line header hash table is only created if needed (it exists to
10930 prevent redundant reading of the line table for partial_units).
10931 If we're given a partial_unit, we'll need it. If we're given a
10932 compile_unit, then use the line header hash table if it's already
10933 created, but don't create one just yet. */
10934
976ca316 10935 if (per_objfile->line_header_hash == NULL
527f3840 10936 && die->tag == DW_TAG_partial_unit)
2ab95328 10937 {
976ca316 10938 per_objfile->line_header_hash
d15acc42
TT
10939 .reset (htab_create_alloc (127, line_header_hash_voidp,
10940 line_header_eq_voidp,
10941 free_line_header_voidp,
10942 xcalloc, xfree));
527f3840 10943 }
2ab95328 10944
9c541725 10945 line_header_local.sect_off = line_offset;
527f3840
JK
10946 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10947 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 10948 if (per_objfile->line_header_hash != NULL)
527f3840 10949 {
976ca316 10950 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10951 &line_header_local,
10952 line_header_local_hash, NO_INSERT);
10953
10954 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10955 is not present in *SLOT (since if there is something in *SLOT then
10956 it will be for a partial_unit). */
10957 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10958 {
527f3840 10959 gdb_assert (*slot != NULL);
9a3c8263 10960 cu->line_header = (struct line_header *) *slot;
527f3840 10961 return;
dee91e82 10962 }
2ab95328 10963 }
527f3840
JK
10964
10965 /* dwarf_decode_line_header does not yet provide sufficient information.
10966 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10967 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10968 if (lh == NULL)
527f3840 10969 return;
4c8aa72d
PA
10970
10971 cu->line_header = lh.release ();
10972 cu->line_header_die_owner = die;
527f3840 10973
976ca316 10974 if (per_objfile->line_header_hash == NULL)
527f3840
JK
10975 slot = NULL;
10976 else
10977 {
976ca316 10978 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10979 &line_header_local,
10980 line_header_local_hash, INSERT);
10981 gdb_assert (slot != NULL);
10982 }
10983 if (slot != NULL && *slot == NULL)
10984 {
10985 /* This newly decoded line number information unit will be owned
10986 by line_header_hash hash table. */
10987 *slot = cu->line_header;
4c8aa72d 10988 cu->line_header_die_owner = NULL;
527f3840
JK
10989 }
10990 else
10991 {
10992 /* We cannot free any current entry in (*slot) as that struct line_header
10993 may be already used by multiple CUs. Create only temporary decoded
10994 line_header for this CU - it may happen at most once for each line
10995 number information unit. And if we're not using line_header_hash
10996 then this is what we want as well. */
10997 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10998 }
10999 decode_mapping = (die->tag != DW_TAG_partial_unit);
11000 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11001 decode_mapping);
fff8551c 11002
2ab95328
TT
11003}
11004
95554aad 11005/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11006
c906108c 11007static void
e7c27a73 11008read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11009{
976ca316
SM
11010 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11011 struct objfile *objfile = per_objfile->objfile;
08feed99 11012 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 11013 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11014 CORE_ADDR highpc = ((CORE_ADDR) 0);
11015 struct attribute *attr;
c906108c 11016 struct die_info *child_die;
e142c38c 11017 CORE_ADDR baseaddr;
6e70227d 11018
380618d6 11019 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11020 baseaddr = objfile->text_section_offset ();
c906108c 11021
fae299cd 11022 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11023
11024 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11025 from finish_block. */
2acceee2 11026 if (lowpc == ((CORE_ADDR) -1))
c906108c 11027 lowpc = highpc;
3e29f34a 11028 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11029
d721ba37 11030 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11031
f4b8a18d
KW
11032 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11033 standardised yet. As a workaround for the language detection we fall
11034 back to the DW_AT_producer string. */
11035 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11036 cu->language = language_opencl;
11037
3019eac3
DE
11038 /* Similar hack for Go. */
11039 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11040 set_cu_language (DW_LANG_Go, cu);
11041
c24bdb02 11042 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11043
11044 /* Decode line number information if present. We do this before
11045 processing child DIEs, so that the line header table is available
11046 for DW_AT_decl_file. */
d721ba37 11047 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11048
11049 /* Process all dies in compilation unit. */
11050 if (die->child != NULL)
11051 {
11052 child_die = die->child;
11053 while (child_die && child_die->tag)
11054 {
11055 process_die (child_die, cu);
436c571c 11056 child_die = child_die->sibling;
3019eac3
DE
11057 }
11058 }
11059
11060 /* Decode macro information, if present. Dwarf 2 macro information
11061 refers to information in the line number info statement program
11062 header, so we can only read it if we've read the header
11063 successfully. */
0af92d60
JK
11064 attr = dwarf2_attr (die, DW_AT_macros, cu);
11065 if (attr == NULL)
11066 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11067 if (attr && cu->line_header)
11068 {
11069 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11070 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11071
43f3e411 11072 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11073 }
11074 else
11075 {
11076 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11077 if (attr && cu->line_header)
11078 {
11079 unsigned int macro_offset = DW_UNSND (attr);
11080
43f3e411 11081 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11082 }
11083 }
3019eac3
DE
11084}
11085
c24bdb02
KS
11086void
11087dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11088{
f4dc4d17
DE
11089 struct type_unit_group *tu_group;
11090 int first_time;
3019eac3 11091 struct attribute *attr;
9c541725 11092 unsigned int i;
0186c6a7 11093 struct signatured_type *sig_type;
3019eac3 11094
f4dc4d17 11095 gdb_assert (per_cu->is_debug_types);
0186c6a7 11096 sig_type = (struct signatured_type *) per_cu;
3019eac3 11097
c24bdb02 11098 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11099
f4dc4d17 11100 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11101 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11102 if (sig_type->type_unit_group == NULL)
c24bdb02 11103 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11104 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11105
11106 /* If we've already processed this stmt_list there's no real need to
11107 do it again, we could fake it and just recreate the part we need
11108 (file name,index -> symtab mapping). If data shows this optimization
11109 is useful we can do it then. */
8adb8487
TT
11110 type_unit_group_unshareable *tug_unshare
11111 = per_objfile->get_type_unit_group_unshareable (tu_group);
11112 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11113
11114 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11115 debug info. */
fff8551c 11116 line_header_up lh;
f4dc4d17 11117 if (attr != NULL)
3019eac3 11118 {
9c541725 11119 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11120 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11121 }
11122 if (lh == NULL)
11123 {
11124 if (first_time)
c24bdb02 11125 start_symtab ("", NULL, 0);
f4dc4d17
DE
11126 else
11127 {
8adb8487 11128 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11129 gdb_assert (m_builder == nullptr);
8adb8487 11130 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11131 m_builder.reset (new struct buildsym_compunit
11132 (COMPUNIT_OBJFILE (cust), "",
11133 COMPUNIT_DIRNAME (cust),
11134 compunit_language (cust),
11135 0, cust));
770479f2 11136 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11137 }
f4dc4d17 11138 return;
3019eac3
DE
11139 }
11140
c24bdb02
KS
11141 line_header = lh.release ();
11142 line_header_die_owner = die;
3019eac3 11143
f4dc4d17
DE
11144 if (first_time)
11145 {
c24bdb02 11146 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11147
1fd60fc0
DE
11148 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11149 still initializing it, and our caller (a few levels up)
11150 process_full_type_unit still needs to know if this is the first
11151 time. */
11152
8adb8487 11153 tug_unshare->symtabs
4ac93832
TT
11154 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11155 struct symtab *, line_header->file_names_size ());
3019eac3 11156
7ba99d21
AT
11157 auto &file_names = line_header->file_names ();
11158 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11159 {
7ba99d21 11160 file_entry &fe = file_names[i];
c24bdb02
KS
11161 dwarf2_start_subfile (this, fe.name,
11162 fe.include_dir (line_header));
11163 buildsym_compunit *b = get_builder ();
11164 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11165 {
4c8aa72d
PA
11166 /* NOTE: start_subfile will recognize when it's been
11167 passed a file it has already seen. So we can't
11168 assume there's a simple mapping from
11169 cu->line_header->file_names to subfiles, plus
11170 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11171 b->get_current_subfile ()->symtab
11172 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11173 }
11174
c24bdb02 11175 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11176 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11177 }
11178 }
11179 else
3019eac3 11180 {
c24bdb02 11181 gdb_assert (m_builder == nullptr);
8adb8487 11182 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11183 m_builder.reset (new struct buildsym_compunit
11184 (COMPUNIT_OBJFILE (cust), "",
11185 COMPUNIT_DIRNAME (cust),
11186 compunit_language (cust),
11187 0, cust));
770479f2 11188 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11189
7ba99d21
AT
11190 auto &file_names = line_header->file_names ();
11191 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11192 {
7ba99d21 11193 file_entry &fe = file_names[i];
8adb8487 11194 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11195 }
3019eac3
DE
11196 }
11197
f4dc4d17
DE
11198 /* The main symtab is allocated last. Type units don't have DW_AT_name
11199 so they don't have a "real" (so to speak) symtab anyway.
11200 There is later code that will assign the main symtab to all symbols
11201 that don't have one. We need to handle the case of a symbol with a
11202 missing symtab (DW_AT_decl_file) anyway. */
11203}
3019eac3 11204
f4dc4d17
DE
11205/* Process DW_TAG_type_unit.
11206 For TUs we want to skip the first top level sibling if it's not the
11207 actual type being defined by this TU. In this case the first top
11208 level sibling is there to provide context only. */
3019eac3 11209
f4dc4d17
DE
11210static void
11211read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11212{
11213 struct die_info *child_die;
3019eac3 11214
f4dc4d17
DE
11215 prepare_one_comp_unit (cu, die, language_minimal);
11216
11217 /* Initialize (or reinitialize) the machinery for building symtabs.
11218 We do this before processing child DIEs, so that the line header table
11219 is available for DW_AT_decl_file. */
c24bdb02 11220 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11221
11222 if (die->child != NULL)
11223 {
11224 child_die = die->child;
11225 while (child_die && child_die->tag)
11226 {
11227 process_die (child_die, cu);
436c571c 11228 child_die = child_die->sibling;
f4dc4d17
DE
11229 }
11230 }
3019eac3
DE
11231}
11232\f
80626a55
DE
11233/* DWO/DWP files.
11234
11235 http://gcc.gnu.org/wiki/DebugFission
11236 http://gcc.gnu.org/wiki/DebugFissionDWP
11237
11238 To simplify handling of both DWO files ("object" files with the DWARF info)
11239 and DWP files (a file with the DWOs packaged up into one file), we treat
11240 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11241
11242static hashval_t
11243hash_dwo_file (const void *item)
11244{
9a3c8263 11245 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11246 hashval_t hash;
3019eac3 11247
a2ce51a0
DE
11248 hash = htab_hash_string (dwo_file->dwo_name);
11249 if (dwo_file->comp_dir != NULL)
11250 hash += htab_hash_string (dwo_file->comp_dir);
11251 return hash;
3019eac3
DE
11252}
11253
11254static int
11255eq_dwo_file (const void *item_lhs, const void *item_rhs)
11256{
9a3c8263
SM
11257 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11258 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11259
a2ce51a0
DE
11260 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11261 return 0;
11262 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11263 return lhs->comp_dir == rhs->comp_dir;
11264 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11265}
11266
11267/* Allocate a hash table for DWO files. */
11268
51ac9db5 11269static htab_up
298e9637 11270allocate_dwo_file_hash_table ()
3019eac3 11271{
51ac9db5
SM
11272 auto delete_dwo_file = [] (void *item)
11273 {
11274 struct dwo_file *dwo_file = (struct dwo_file *) item;
11275
11276 delete dwo_file;
11277 };
11278
bc68fb19
TT
11279 return htab_up (htab_create_alloc (41,
11280 hash_dwo_file,
11281 eq_dwo_file,
11282 delete_dwo_file,
11283 xcalloc, xfree));
3019eac3
DE
11284}
11285
80626a55
DE
11286/* Lookup DWO file DWO_NAME. */
11287
11288static void **
976ca316 11289lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11290 const char *dwo_name,
11291 const char *comp_dir)
80626a55
DE
11292{
11293 struct dwo_file find_entry;
11294 void **slot;
11295
976ca316
SM
11296 if (per_objfile->per_bfd->dwo_files == NULL)
11297 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11298
0ac5b59e
DE
11299 find_entry.dwo_name = dwo_name;
11300 find_entry.comp_dir = comp_dir;
976ca316 11301 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11302 INSERT);
80626a55
DE
11303
11304 return slot;
11305}
11306
3019eac3
DE
11307static hashval_t
11308hash_dwo_unit (const void *item)
11309{
9a3c8263 11310 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11311
11312 /* This drops the top 32 bits of the id, but is ok for a hash. */
11313 return dwo_unit->signature;
11314}
11315
11316static int
11317eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11318{
9a3c8263
SM
11319 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11320 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11321
11322 /* The signature is assumed to be unique within the DWO file.
11323 So while object file CU dwo_id's always have the value zero,
11324 that's OK, assuming each object file DWO file has only one CU,
11325 and that's the rule for now. */
11326 return lhs->signature == rhs->signature;
11327}
11328
11329/* Allocate a hash table for DWO CUs,TUs.
11330 There is one of these tables for each of CUs,TUs for each DWO file. */
11331
b0b6a987 11332static htab_up
298e9637 11333allocate_dwo_unit_table ()
3019eac3
DE
11334{
11335 /* Start out with a pretty small number.
11336 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11337 return htab_up (htab_create_alloc (3,
11338 hash_dwo_unit,
11339 eq_dwo_unit,
11340 NULL, xcalloc, xfree));
3019eac3
DE
11341}
11342
19c3d4c9 11343/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11344
11345static void
19c3d4c9
DE
11346create_dwo_cu_reader (const struct die_reader_specs *reader,
11347 const gdb_byte *info_ptr,
11348 struct die_info *comp_unit_die,
c0ab21c2
TT
11349 struct dwo_file *dwo_file,
11350 struct dwo_unit *dwo_unit)
3019eac3
DE
11351{
11352 struct dwarf2_cu *cu = reader->cu;
9c541725 11353 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11354 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11355
a084a2a6
AT
11356 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11357 if (!signature.has_value ())
3019eac3 11358 {
b98664d3 11359 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11360 " its dwo_id [in module %s]"),
9d8780f0 11361 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11362 return;
11363 }
11364
3019eac3 11365 dwo_unit->dwo_file = dwo_file;
a084a2a6 11366 dwo_unit->signature = *signature;
8a0459fd 11367 dwo_unit->section = section;
9c541725 11368 dwo_unit->sect_off = sect_off;
3019eac3
DE
11369 dwo_unit->length = cu->per_cu->length;
11370
b4f54984 11371 if (dwarf_read_debug)
9d8780f0
SM
11372 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11373 sect_offset_str (sect_off),
9c541725 11374 hex_string (dwo_unit->signature));
3019eac3
DE
11375}
11376
33c5cd75 11377/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11378 Note: This function processes DWO files only, not DWP files. */
3019eac3 11379
33c5cd75 11380static void
976ca316 11381create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11382 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11383 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11384{
976ca316
SM
11385 struct objfile *objfile = per_objfile->objfile;
11386 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11387 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11388
96b79293 11389 section.read (objfile);
33c5cd75 11390 info_ptr = section.buffer;
3019eac3
DE
11391
11392 if (info_ptr == NULL)
33c5cd75 11393 return;
3019eac3 11394
b4f54984 11395 if (dwarf_read_debug)
19c3d4c9
DE
11396 {
11397 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11398 section.get_name (),
11399 section.get_file_name ());
19c3d4c9 11400 }
3019eac3 11401
33c5cd75 11402 end_ptr = info_ptr + section.size;
3019eac3
DE
11403 while (info_ptr < end_ptr)
11404 {
11405 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11406 struct dwo_unit read_unit {};
33c5cd75
DB
11407 struct dwo_unit *dwo_unit;
11408 void **slot;
11409 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11410
11411 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11412 per_cu.per_bfd = per_bfd;
3019eac3 11413 per_cu.is_debug_types = 0;
33c5cd75
DB
11414 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11415 per_cu.section = &section;
11416
976ca316 11417 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11418 if (!reader.dummy_p)
11419 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11420 &dwo_file, &read_unit);
33c5cd75
DB
11421 info_ptr += per_cu.length;
11422
11423 // If the unit could not be parsed, skip it.
c0ab21c2 11424 if (read_unit.dwo_file == NULL)
33c5cd75 11425 continue;
3019eac3 11426
33c5cd75 11427 if (cus_htab == NULL)
298e9637 11428 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11429
1859c670 11430 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11431 struct dwo_unit);
c0ab21c2 11432 *dwo_unit = read_unit;
b0b6a987 11433 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11434 gdb_assert (slot != NULL);
11435 if (*slot != NULL)
19c3d4c9 11436 {
33c5cd75
DB
11437 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11438 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11439
b98664d3 11440 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11441 " the entry at offset %s, signature %s"),
11442 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11443 hex_string (dwo_unit->signature));
19c3d4c9 11444 }
33c5cd75 11445 *slot = (void *)dwo_unit;
3019eac3 11446 }
3019eac3
DE
11447}
11448
80626a55
DE
11449/* DWP file .debug_{cu,tu}_index section format:
11450 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11451
d2415c6c
DE
11452 DWP Version 1:
11453
80626a55
DE
11454 Both index sections have the same format, and serve to map a 64-bit
11455 signature to a set of section numbers. Each section begins with a header,
11456 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11457 indexes, and a pool of 32-bit section numbers. The index sections will be
11458 aligned at 8-byte boundaries in the file.
11459
d2415c6c
DE
11460 The index section header consists of:
11461
11462 V, 32 bit version number
11463 -, 32 bits unused
11464 N, 32 bit number of compilation units or type units in the index
11465 M, 32 bit number of slots in the hash table
80626a55 11466
d2415c6c 11467 Numbers are recorded using the byte order of the application binary.
80626a55 11468
d2415c6c
DE
11469 The hash table begins at offset 16 in the section, and consists of an array
11470 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11471 order of the application binary). Unused slots in the hash table are 0.
11472 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11473
d2415c6c
DE
11474 The parallel table begins immediately after the hash table
11475 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11476 array of 32-bit indexes (using the byte order of the application binary),
11477 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11478 table contains a 32-bit index into the pool of section numbers. For unused
11479 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11480
73869dc2
DE
11481 The pool of section numbers begins immediately following the hash table
11482 (at offset 16 + 12 * M from the beginning of the section). The pool of
11483 section numbers consists of an array of 32-bit words (using the byte order
11484 of the application binary). Each item in the array is indexed starting
11485 from 0. The hash table entry provides the index of the first section
11486 number in the set. Additional section numbers in the set follow, and the
11487 set is terminated by a 0 entry (section number 0 is not used in ELF).
11488
11489 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11490 section must be the first entry in the set, and the .debug_abbrev.dwo must
11491 be the second entry. Other members of the set may follow in any order.
11492
11493 ---
11494
11495 DWP Version 2:
11496
11497 DWP Version 2 combines all the .debug_info, etc. sections into one,
11498 and the entries in the index tables are now offsets into these sections.
11499 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11500 section.
11501
11502 Index Section Contents:
11503 Header
11504 Hash Table of Signatures dwp_hash_table.hash_table
11505 Parallel Table of Indices dwp_hash_table.unit_table
11506 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11507 Table of Section Sizes dwp_hash_table.v2.sizes
11508
11509 The index section header consists of:
11510
11511 V, 32 bit version number
11512 L, 32 bit number of columns in the table of section offsets
11513 N, 32 bit number of compilation units or type units in the index
11514 M, 32 bit number of slots in the hash table
11515
11516 Numbers are recorded using the byte order of the application binary.
11517
11518 The hash table has the same format as version 1.
11519 The parallel table of indices has the same format as version 1,
11520 except that the entries are origin-1 indices into the table of sections
11521 offsets and the table of section sizes.
11522
11523 The table of offsets begins immediately following the parallel table
11524 (at offset 16 + 12 * M from the beginning of the section). The table is
11525 a two-dimensional array of 32-bit words (using the byte order of the
11526 application binary), with L columns and N+1 rows, in row-major order.
11527 Each row in the array is indexed starting from 0. The first row provides
11528 a key to the remaining rows: each column in this row provides an identifier
11529 for a debug section, and the offsets in the same column of subsequent rows
11530 refer to that section. The section identifiers are:
11531
11532 DW_SECT_INFO 1 .debug_info.dwo
11533 DW_SECT_TYPES 2 .debug_types.dwo
11534 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11535 DW_SECT_LINE 4 .debug_line.dwo
11536 DW_SECT_LOC 5 .debug_loc.dwo
11537 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11538 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11539 DW_SECT_MACRO 8 .debug_macro.dwo
11540
11541 The offsets provided by the CU and TU index sections are the base offsets
11542 for the contributions made by each CU or TU to the corresponding section
11543 in the package file. Each CU and TU header contains an abbrev_offset
11544 field, used to find the abbreviations table for that CU or TU within the
11545 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11546 be interpreted as relative to the base offset given in the index section.
11547 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11548 should be interpreted as relative to the base offset for .debug_line.dwo,
11549 and offsets into other debug sections obtained from DWARF attributes should
11550 also be interpreted as relative to the corresponding base offset.
11551
11552 The table of sizes begins immediately following the table of offsets.
11553 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11554 with L columns and N rows, in row-major order. Each row in the array is
11555 indexed starting from 1 (row 0 is shared by the two tables).
11556
11557 ---
11558
11559 Hash table lookup is handled the same in version 1 and 2:
11560
11561 We assume that N and M will not exceed 2^32 - 1.
11562 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11563
d2415c6c
DE
11564 Given a 64-bit compilation unit signature or a type signature S, an entry
11565 in the hash table is located as follows:
80626a55 11566
d2415c6c
DE
11567 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11568 the low-order k bits all set to 1.
80626a55 11569
d2415c6c 11570 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11571
d2415c6c
DE
11572 3) If the hash table entry at index H matches the signature, use that
11573 entry. If the hash table entry at index H is unused (all zeroes),
11574 terminate the search: the signature is not present in the table.
80626a55 11575
d2415c6c 11576 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11577
d2415c6c 11578 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11579 to stop at an unused slot or find the match. */
80626a55
DE
11580
11581/* Create a hash table to map DWO IDs to their CU/TU entry in
11582 .debug_{info,types}.dwo in DWP_FILE.
11583 Returns NULL if there isn't one.
11584 Note: This function processes DWP files only, not DWO files. */
11585
11586static struct dwp_hash_table *
976ca316 11587create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11588 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11589{
976ca316 11590 struct objfile *objfile = per_objfile->objfile;
400174b1 11591 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11592 const gdb_byte *index_ptr, *index_end;
80626a55 11593 struct dwarf2_section_info *index;
73869dc2 11594 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11595 struct dwp_hash_table *htab;
11596
11597 if (is_debug_types)
11598 index = &dwp_file->sections.tu_index;
11599 else
11600 index = &dwp_file->sections.cu_index;
11601
96b79293 11602 if (index->empty ())
80626a55 11603 return NULL;
96b79293 11604 index->read (objfile);
80626a55
DE
11605
11606 index_ptr = index->buffer;
11607 index_end = index_ptr + index->size;
11608
11609 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11610 index_ptr += 4;
11611 if (version == 2)
11612 nr_columns = read_4_bytes (dbfd, index_ptr);
11613 else
11614 nr_columns = 0;
11615 index_ptr += 4;
80626a55
DE
11616 nr_units = read_4_bytes (dbfd, index_ptr);
11617 index_ptr += 4;
11618 nr_slots = read_4_bytes (dbfd, index_ptr);
11619 index_ptr += 4;
11620
73869dc2 11621 if (version != 1 && version != 2)
80626a55 11622 {
21aa081e 11623 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11624 " [in module %s]"),
21aa081e 11625 pulongest (version), dwp_file->name);
80626a55
DE
11626 }
11627 if (nr_slots != (nr_slots & -nr_slots))
11628 {
21aa081e 11629 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11630 " is not power of 2 [in module %s]"),
21aa081e 11631 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11632 }
11633
976ca316 11634 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11635 htab->version = version;
11636 htab->nr_columns = nr_columns;
80626a55
DE
11637 htab->nr_units = nr_units;
11638 htab->nr_slots = nr_slots;
11639 htab->hash_table = index_ptr;
11640 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11641
11642 /* Exit early if the table is empty. */
11643 if (nr_slots == 0 || nr_units == 0
11644 || (version == 2 && nr_columns == 0))
11645 {
11646 /* All must be zero. */
11647 if (nr_slots != 0 || nr_units != 0
11648 || (version == 2 && nr_columns != 0))
11649 {
b98664d3 11650 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11651 " all zero [in modules %s]"),
11652 dwp_file->name);
11653 }
11654 return htab;
11655 }
11656
11657 if (version == 1)
11658 {
11659 htab->section_pool.v1.indices =
11660 htab->unit_table + sizeof (uint32_t) * nr_slots;
11661 /* It's harder to decide whether the section is too small in v1.
11662 V1 is deprecated anyway so we punt. */
11663 }
11664 else
11665 {
11666 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11667 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11668 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11669 /* Reverse map for error checking. */
11670 int ids_seen[DW_SECT_MAX + 1];
11671 int i;
11672
11673 if (nr_columns < 2)
11674 {
11675 error (_("Dwarf Error: bad DWP hash table, too few columns"
11676 " in section table [in module %s]"),
11677 dwp_file->name);
11678 }
11679 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11680 {
11681 error (_("Dwarf Error: bad DWP hash table, too many columns"
11682 " in section table [in module %s]"),
11683 dwp_file->name);
11684 }
04fd5eed
GB
11685 memset (ids, 255, sizeof_ids);
11686 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11687 for (i = 0; i < nr_columns; ++i)
11688 {
11689 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11690
11691 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11692 {
11693 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11694 " in section table [in module %s]"),
11695 id, dwp_file->name);
11696 }
11697 if (ids_seen[id] != -1)
11698 {
11699 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11700 " id %d in section table [in module %s]"),
11701 id, dwp_file->name);
11702 }
11703 ids_seen[id] = i;
11704 ids[i] = id;
11705 }
11706 /* Must have exactly one info or types section. */
11707 if (((ids_seen[DW_SECT_INFO] != -1)
11708 + (ids_seen[DW_SECT_TYPES] != -1))
11709 != 1)
11710 {
11711 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11712 " DWO info/types section [in module %s]"),
11713 dwp_file->name);
11714 }
11715 /* Must have an abbrev section. */
11716 if (ids_seen[DW_SECT_ABBREV] == -1)
11717 {
11718 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11719 " section [in module %s]"),
11720 dwp_file->name);
11721 }
11722 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11723 htab->section_pool.v2.sizes =
11724 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11725 * nr_units * nr_columns);
11726 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11727 * nr_units * nr_columns))
11728 > index_end)
11729 {
11730 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11731 " [in module %s]"),
11732 dwp_file->name);
11733 }
11734 }
80626a55
DE
11735
11736 return htab;
11737}
11738
11739/* Update SECTIONS with the data from SECTP.
11740
11741 This function is like the other "locate" section routines that are
11742 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11743 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11744
11745 The result is non-zero for success, or zero if an error was found. */
11746
11747static int
73869dc2
DE
11748locate_v1_virtual_dwo_sections (asection *sectp,
11749 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11750{
11751 const struct dwop_section_names *names = &dwop_section_names;
11752
11753 if (section_is_p (sectp->name, &names->abbrev_dwo))
11754 {
11755 /* There can be only one. */
049412e3 11756 if (sections->abbrev.s.section != NULL)
80626a55 11757 return 0;
049412e3 11758 sections->abbrev.s.section = sectp;
fd361982 11759 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11760 }
11761 else if (section_is_p (sectp->name, &names->info_dwo)
11762 || section_is_p (sectp->name, &names->types_dwo))
11763 {
11764 /* There can be only one. */
049412e3 11765 if (sections->info_or_types.s.section != NULL)
80626a55 11766 return 0;
049412e3 11767 sections->info_or_types.s.section = sectp;
fd361982 11768 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11769 }
11770 else if (section_is_p (sectp->name, &names->line_dwo))
11771 {
11772 /* There can be only one. */
049412e3 11773 if (sections->line.s.section != NULL)
80626a55 11774 return 0;
049412e3 11775 sections->line.s.section = sectp;
fd361982 11776 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11777 }
11778 else if (section_is_p (sectp->name, &names->loc_dwo))
11779 {
11780 /* There can be only one. */
049412e3 11781 if (sections->loc.s.section != NULL)
80626a55 11782 return 0;
049412e3 11783 sections->loc.s.section = sectp;
fd361982 11784 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11785 }
11786 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11787 {
11788 /* There can be only one. */
049412e3 11789 if (sections->macinfo.s.section != NULL)
80626a55 11790 return 0;
049412e3 11791 sections->macinfo.s.section = sectp;
fd361982 11792 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11793 }
11794 else if (section_is_p (sectp->name, &names->macro_dwo))
11795 {
11796 /* There can be only one. */
049412e3 11797 if (sections->macro.s.section != NULL)
80626a55 11798 return 0;
049412e3 11799 sections->macro.s.section = sectp;
fd361982 11800 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11801 }
11802 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11803 {
11804 /* There can be only one. */
049412e3 11805 if (sections->str_offsets.s.section != NULL)
80626a55 11806 return 0;
049412e3 11807 sections->str_offsets.s.section = sectp;
fd361982 11808 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11809 }
11810 else
11811 {
11812 /* No other kind of section is valid. */
11813 return 0;
11814 }
11815
11816 return 1;
11817}
11818
73869dc2
DE
11819/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11820 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11821 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11822 This is for DWP version 1 files. */
80626a55
DE
11823
11824static struct dwo_unit *
976ca316 11825create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 11826 struct dwp_file *dwp_file,
73869dc2
DE
11827 uint32_t unit_index,
11828 const char *comp_dir,
11829 ULONGEST signature, int is_debug_types)
80626a55 11830{
73869dc2
DE
11831 const struct dwp_hash_table *dwp_htab =
11832 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11833 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11834 const char *kind = is_debug_types ? "TU" : "CU";
11835 struct dwo_file *dwo_file;
11836 struct dwo_unit *dwo_unit;
73869dc2 11837 struct virtual_v1_dwo_sections sections;
80626a55 11838 void **dwo_file_slot;
80626a55
DE
11839 int i;
11840
73869dc2
DE
11841 gdb_assert (dwp_file->version == 1);
11842
b4f54984 11843 if (dwarf_read_debug)
80626a55 11844 {
73869dc2 11845 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11846 kind,
73869dc2 11847 pulongest (unit_index), hex_string (signature),
80626a55
DE
11848 dwp_file->name);
11849 }
11850
19ac8c2e 11851 /* Fetch the sections of this DWO unit.
80626a55
DE
11852 Put a limit on the number of sections we look for so that bad data
11853 doesn't cause us to loop forever. */
11854
73869dc2 11855#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11856 (1 /* .debug_info or .debug_types */ \
11857 + 1 /* .debug_abbrev */ \
11858 + 1 /* .debug_line */ \
11859 + 1 /* .debug_loc */ \
11860 + 1 /* .debug_str_offsets */ \
19ac8c2e 11861 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11862 + 1 /* trailing zero */)
11863
11864 memset (&sections, 0, sizeof (sections));
80626a55 11865
73869dc2 11866 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11867 {
11868 asection *sectp;
11869 uint32_t section_nr =
11870 read_4_bytes (dbfd,
73869dc2
DE
11871 dwp_htab->section_pool.v1.indices
11872 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11873
11874 if (section_nr == 0)
11875 break;
11876 if (section_nr >= dwp_file->num_sections)
11877 {
11878 error (_("Dwarf Error: bad DWP hash table, section number too large"
11879 " [in module %s]"),
11880 dwp_file->name);
11881 }
11882
11883 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11884 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11885 {
11886 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11887 " [in module %s]"),
11888 dwp_file->name);
11889 }
11890 }
11891
11892 if (i < 2
96b79293
TT
11893 || sections.info_or_types.empty ()
11894 || sections.abbrev.empty ())
80626a55
DE
11895 {
11896 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11897 " [in module %s]"),
11898 dwp_file->name);
11899 }
73869dc2 11900 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11901 {
11902 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11903 " [in module %s]"),
11904 dwp_file->name);
11905 }
11906
11907 /* It's easier for the rest of the code if we fake a struct dwo_file and
11908 have dwo_unit "live" in that. At least for now.
11909
11910 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11911 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11912 file, we can combine them back into a virtual DWO file to save space
11913 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11914 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11915
791afaa2
TT
11916 std::string virtual_dwo_name =
11917 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11918 sections.abbrev.get_id (),
11919 sections.line.get_id (),
11920 sections.loc.get_id (),
11921 sections.str_offsets.get_id ());
80626a55 11922 /* Can we use an existing virtual DWO file? */
976ca316 11923 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11924 comp_dir);
80626a55
DE
11925 /* Create one if necessary. */
11926 if (*dwo_file_slot == NULL)
11927 {
b4f54984 11928 if (dwarf_read_debug)
80626a55
DE
11929 {
11930 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11931 virtual_dwo_name.c_str ());
80626a55 11932 }
51ac9db5 11933 dwo_file = new struct dwo_file;
976ca316 11934 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11935 dwo_file->comp_dir = comp_dir;
80626a55
DE
11936 dwo_file->sections.abbrev = sections.abbrev;
11937 dwo_file->sections.line = sections.line;
11938 dwo_file->sections.loc = sections.loc;
11939 dwo_file->sections.macinfo = sections.macinfo;
11940 dwo_file->sections.macro = sections.macro;
11941 dwo_file->sections.str_offsets = sections.str_offsets;
11942 /* The "str" section is global to the entire DWP file. */
11943 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11944 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11945 there's no need to record it in dwo_file.
11946 Also, we can't simply record type sections in dwo_file because
11947 we record a pointer into the vector in dwo_unit. As we collect more
11948 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11949 for it, invalidating all copies of pointers into the previous
11950 contents. */
80626a55
DE
11951 *dwo_file_slot = dwo_file;
11952 }
11953 else
11954 {
b4f54984 11955 if (dwarf_read_debug)
80626a55
DE
11956 {
11957 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11958 virtual_dwo_name.c_str ());
80626a55 11959 }
9a3c8263 11960 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11961 }
80626a55 11962
976ca316 11963 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11964 dwo_unit->dwo_file = dwo_file;
11965 dwo_unit->signature = signature;
8d749320 11966 dwo_unit->section =
976ca316 11967 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11968 *dwo_unit->section = sections.info_or_types;
57d63ce2 11969 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11970
11971 return dwo_unit;
11972}
11973
73869dc2
DE
11974/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11975 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11976 piece within that section used by a TU/CU, return a virtual section
11977 of just that piece. */
11978
11979static struct dwarf2_section_info
976ca316 11980create_dwp_v2_section (dwarf2_per_objfile *per_objfile,
ed2dc618 11981 struct dwarf2_section_info *section,
73869dc2
DE
11982 bfd_size_type offset, bfd_size_type size)
11983{
11984 struct dwarf2_section_info result;
11985 asection *sectp;
11986
11987 gdb_assert (section != NULL);
11988 gdb_assert (!section->is_virtual);
11989
11990 memset (&result, 0, sizeof (result));
11991 result.s.containing_section = section;
dc4ccb6f 11992 result.is_virtual = true;
73869dc2
DE
11993
11994 if (size == 0)
11995 return result;
11996
96b79293 11997 sectp = section->get_bfd_section ();
73869dc2
DE
11998
11999 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12000 bounds of the real section. This is a pretty-rare event, so just
12001 flag an error (easier) instead of a warning and trying to cope. */
12002 if (sectp == NULL
fd361982 12003 || offset + size > bfd_section_size (sectp))
73869dc2 12004 {
73869dc2
DE
12005 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12006 " in section %s [in module %s]"),
fd361982 12007 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 12008 objfile_name (per_objfile->objfile));
73869dc2
DE
12009 }
12010
12011 result.virtual_offset = offset;
12012 result.size = size;
12013 return result;
12014}
12015
12016/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12017 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12018 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12019 This is for DWP version 2 files. */
12020
12021static struct dwo_unit *
976ca316 12022create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 12023 struct dwp_file *dwp_file,
73869dc2
DE
12024 uint32_t unit_index,
12025 const char *comp_dir,
12026 ULONGEST signature, int is_debug_types)
12027{
73869dc2
DE
12028 const struct dwp_hash_table *dwp_htab =
12029 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12030 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12031 const char *kind = is_debug_types ? "TU" : "CU";
12032 struct dwo_file *dwo_file;
12033 struct dwo_unit *dwo_unit;
12034 struct virtual_v2_dwo_sections sections;
12035 void **dwo_file_slot;
73869dc2
DE
12036 int i;
12037
12038 gdb_assert (dwp_file->version == 2);
12039
b4f54984 12040 if (dwarf_read_debug)
73869dc2
DE
12041 {
12042 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12043 kind,
12044 pulongest (unit_index), hex_string (signature),
12045 dwp_file->name);
12046 }
12047
12048 /* Fetch the section offsets of this DWO unit. */
12049
12050 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12051
12052 for (i = 0; i < dwp_htab->nr_columns; ++i)
12053 {
12054 uint32_t offset = read_4_bytes (dbfd,
12055 dwp_htab->section_pool.v2.offsets
12056 + (((unit_index - 1) * dwp_htab->nr_columns
12057 + i)
12058 * sizeof (uint32_t)));
12059 uint32_t size = read_4_bytes (dbfd,
12060 dwp_htab->section_pool.v2.sizes
12061 + (((unit_index - 1) * dwp_htab->nr_columns
12062 + i)
12063 * sizeof (uint32_t)));
12064
12065 switch (dwp_htab->section_pool.v2.section_ids[i])
12066 {
12067 case DW_SECT_INFO:
12068 case DW_SECT_TYPES:
12069 sections.info_or_types_offset = offset;
12070 sections.info_or_types_size = size;
12071 break;
12072 case DW_SECT_ABBREV:
12073 sections.abbrev_offset = offset;
12074 sections.abbrev_size = size;
12075 break;
12076 case DW_SECT_LINE:
12077 sections.line_offset = offset;
12078 sections.line_size = size;
12079 break;
12080 case DW_SECT_LOC:
12081 sections.loc_offset = offset;
12082 sections.loc_size = size;
12083 break;
12084 case DW_SECT_STR_OFFSETS:
12085 sections.str_offsets_offset = offset;
12086 sections.str_offsets_size = size;
12087 break;
12088 case DW_SECT_MACINFO:
12089 sections.macinfo_offset = offset;
12090 sections.macinfo_size = size;
12091 break;
12092 case DW_SECT_MACRO:
12093 sections.macro_offset = offset;
12094 sections.macro_size = size;
12095 break;
12096 }
12097 }
12098
12099 /* It's easier for the rest of the code if we fake a struct dwo_file and
12100 have dwo_unit "live" in that. At least for now.
12101
12102 The DWP file can be made up of a random collection of CUs and TUs.
12103 However, for each CU + set of TUs that came from the same original DWO
12104 file, we can combine them back into a virtual DWO file to save space
12105 (fewer struct dwo_file objects to allocate). Remember that for really
12106 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12107
791afaa2
TT
12108 std::string virtual_dwo_name =
12109 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12110 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12111 (long) (sections.line_size ? sections.line_offset : 0),
12112 (long) (sections.loc_size ? sections.loc_offset : 0),
12113 (long) (sections.str_offsets_size
12114 ? sections.str_offsets_offset : 0));
73869dc2 12115 /* Can we use an existing virtual DWO file? */
976ca316 12116 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12117 comp_dir);
73869dc2
DE
12118 /* Create one if necessary. */
12119 if (*dwo_file_slot == NULL)
12120 {
b4f54984 12121 if (dwarf_read_debug)
73869dc2
DE
12122 {
12123 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12124 virtual_dwo_name.c_str ());
73869dc2 12125 }
51ac9db5 12126 dwo_file = new struct dwo_file;
976ca316 12127 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12128 dwo_file->comp_dir = comp_dir;
12129 dwo_file->sections.abbrev =
976ca316 12130 create_dwp_v2_section (per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12131 sections.abbrev_offset, sections.abbrev_size);
12132 dwo_file->sections.line =
976ca316 12133 create_dwp_v2_section (per_objfile, &dwp_file->sections.line,
73869dc2
DE
12134 sections.line_offset, sections.line_size);
12135 dwo_file->sections.loc =
976ca316 12136 create_dwp_v2_section (per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12137 sections.loc_offset, sections.loc_size);
12138 dwo_file->sections.macinfo =
976ca316 12139 create_dwp_v2_section (per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12140 sections.macinfo_offset, sections.macinfo_size);
12141 dwo_file->sections.macro =
976ca316 12142 create_dwp_v2_section (per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12143 sections.macro_offset, sections.macro_size);
12144 dwo_file->sections.str_offsets =
976ca316 12145 create_dwp_v2_section (per_objfile,
ed2dc618 12146 &dwp_file->sections.str_offsets,
73869dc2
DE
12147 sections.str_offsets_offset,
12148 sections.str_offsets_size);
12149 /* The "str" section is global to the entire DWP file. */
12150 dwo_file->sections.str = dwp_file->sections.str;
12151 /* The info or types section is assigned below to dwo_unit,
12152 there's no need to record it in dwo_file.
12153 Also, we can't simply record type sections in dwo_file because
12154 we record a pointer into the vector in dwo_unit. As we collect more
12155 types we'll grow the vector and eventually have to reallocate space
12156 for it, invalidating all copies of pointers into the previous
12157 contents. */
12158 *dwo_file_slot = dwo_file;
12159 }
12160 else
12161 {
b4f54984 12162 if (dwarf_read_debug)
73869dc2
DE
12163 {
12164 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12165 virtual_dwo_name.c_str ());
73869dc2 12166 }
9a3c8263 12167 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12168 }
73869dc2 12169
976ca316 12170 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12171 dwo_unit->dwo_file = dwo_file;
12172 dwo_unit->signature = signature;
8d749320 12173 dwo_unit->section =
976ca316
SM
12174 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12175 *dwo_unit->section = create_dwp_v2_section (per_objfile,
ed2dc618 12176 is_debug_types
73869dc2
DE
12177 ? &dwp_file->sections.types
12178 : &dwp_file->sections.info,
12179 sections.info_or_types_offset,
12180 sections.info_or_types_size);
12181 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12182
12183 return dwo_unit;
12184}
12185
57d63ce2
DE
12186/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12187 Returns NULL if the signature isn't found. */
80626a55
DE
12188
12189static struct dwo_unit *
976ca316 12190lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12191 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12192 ULONGEST signature, int is_debug_types)
80626a55 12193{
57d63ce2
DE
12194 const struct dwp_hash_table *dwp_htab =
12195 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12196 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12197 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12198 uint32_t hash = signature & mask;
12199 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12200 unsigned int i;
12201 void **slot;
870f88f7 12202 struct dwo_unit find_dwo_cu;
80626a55
DE
12203
12204 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12205 find_dwo_cu.signature = signature;
19ac8c2e 12206 slot = htab_find_slot (is_debug_types
48b490f2
TT
12207 ? dwp_file->loaded_tus.get ()
12208 : dwp_file->loaded_cus.get (),
19ac8c2e 12209 &find_dwo_cu, INSERT);
80626a55
DE
12210
12211 if (*slot != NULL)
9a3c8263 12212 return (struct dwo_unit *) *slot;
80626a55
DE
12213
12214 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12215 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12216 {
12217 ULONGEST signature_in_table;
12218
12219 signature_in_table =
57d63ce2 12220 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12221 if (signature_in_table == signature)
12222 {
57d63ce2
DE
12223 uint32_t unit_index =
12224 read_4_bytes (dbfd,
12225 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12226
73869dc2
DE
12227 if (dwp_file->version == 1)
12228 {
976ca316
SM
12229 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12230 unit_index, comp_dir,
12231 signature, is_debug_types);
73869dc2
DE
12232 }
12233 else
12234 {
976ca316
SM
12235 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12236 unit_index, comp_dir,
12237 signature, is_debug_types);
73869dc2 12238 }
9a3c8263 12239 return (struct dwo_unit *) *slot;
80626a55
DE
12240 }
12241 if (signature_in_table == 0)
12242 return NULL;
12243 hash = (hash + hash2) & mask;
12244 }
12245
12246 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12247 " [in module %s]"),
12248 dwp_file->name);
12249}
12250
ab5088bf 12251/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12252 Open the file specified by FILE_NAME and hand it off to BFD for
12253 preliminary analysis. Return a newly initialized bfd *, which
12254 includes a canonicalized copy of FILE_NAME.
80626a55 12255 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12256 SEARCH_CWD is true if the current directory is to be searched.
12257 It will be searched before debug-file-directory.
13aaf454
DE
12258 If successful, the file is added to the bfd include table of the
12259 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12260 If unable to find/open the file, return NULL.
3019eac3
DE
12261 NOTE: This function is derived from symfile_bfd_open. */
12262
192b62ce 12263static gdb_bfd_ref_ptr
976ca316 12264try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12265 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12266{
24b9144d 12267 int desc;
9c02c129
DE
12268 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12269 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12270 to debug_file_directory. */
e0cc99a6 12271 const char *search_path;
9c02c129
DE
12272 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12273
e0cc99a6 12274 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12275 if (search_cwd)
12276 {
12277 if (*debug_file_directory != '\0')
e0cc99a6
TT
12278 {
12279 search_path_holder.reset (concat (".", dirname_separator_string,
12280 debug_file_directory,
12281 (char *) NULL));
12282 search_path = search_path_holder.get ();
12283 }
6ac97d4c 12284 else
e0cc99a6 12285 search_path = ".";
6ac97d4c 12286 }
9c02c129 12287 else
e0cc99a6 12288 search_path = debug_file_directory;
3019eac3 12289
24b9144d 12290 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12291 if (is_dwp)
12292 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12293
12294 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12295 desc = openp (search_path, flags, file_name,
3019eac3
DE
12296 O_RDONLY | O_BINARY, &absolute_name);
12297 if (desc < 0)
12298 return NULL;
12299
e0cc99a6
TT
12300 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12301 gnutarget, desc));
9c02c129
DE
12302 if (sym_bfd == NULL)
12303 return NULL;
192b62ce 12304 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12305
192b62ce
TT
12306 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12307 return NULL;
3019eac3 12308
13aaf454
DE
12309 /* Success. Record the bfd as having been included by the objfile's bfd.
12310 This is important because things like demangled_names_hash lives in the
12311 objfile's per_bfd space and may have references to things like symbol
12312 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12313 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12314
3019eac3
DE
12315 return sym_bfd;
12316}
12317
ab5088bf 12318/* Try to open DWO file FILE_NAME.
3019eac3
DE
12319 COMP_DIR is the DW_AT_comp_dir attribute.
12320 The result is the bfd handle of the file.
12321 If there is a problem finding or opening the file, return NULL.
12322 Upon success, the canonicalized path of the file is stored in the bfd,
12323 same as symfile_bfd_open. */
12324
192b62ce 12325static gdb_bfd_ref_ptr
976ca316 12326open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12327 const char *file_name, const char *comp_dir)
3019eac3 12328{
80626a55 12329 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12330 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12331 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12332
12333 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12334
12335 if (comp_dir != NULL)
12336 {
43816ebc
TT
12337 gdb::unique_xmalloc_ptr<char> path_to_try
12338 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12339
12340 /* NOTE: If comp_dir is a relative path, this will also try the
12341 search path, which seems useful. */
976ca316 12342 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12343 0 /*is_dwp*/,
192b62ce 12344 1 /*search_cwd*/));
3019eac3
DE
12345 if (abfd != NULL)
12346 return abfd;
12347 }
12348
12349 /* That didn't work, try debug-file-directory, which, despite its name,
12350 is a list of paths. */
12351
12352 if (*debug_file_directory == '\0')
12353 return NULL;
12354
976ca316 12355 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12356 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12357}
12358
80626a55
DE
12359/* This function is mapped across the sections and remembers the offset and
12360 size of each of the DWO debugging sections we are interested in. */
12361
12362static void
12363dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12364{
9a3c8263 12365 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12366 const struct dwop_section_names *names = &dwop_section_names;
12367
12368 if (section_is_p (sectp->name, &names->abbrev_dwo))
12369 {
049412e3 12370 dwo_sections->abbrev.s.section = sectp;
fd361982 12371 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12372 }
12373 else if (section_is_p (sectp->name, &names->info_dwo))
12374 {
049412e3 12375 dwo_sections->info.s.section = sectp;
fd361982 12376 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12377 }
12378 else if (section_is_p (sectp->name, &names->line_dwo))
12379 {
049412e3 12380 dwo_sections->line.s.section = sectp;
fd361982 12381 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12382 }
12383 else if (section_is_p (sectp->name, &names->loc_dwo))
12384 {
049412e3 12385 dwo_sections->loc.s.section = sectp;
fd361982 12386 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12387 }
41144253 12388 else if (section_is_p (sectp->name, &names->loclists_dwo))
12389 {
12390 dwo_sections->loclists.s.section = sectp;
12391 dwo_sections->loclists.size = bfd_section_size (sectp);
12392 }
80626a55
DE
12393 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12394 {
049412e3 12395 dwo_sections->macinfo.s.section = sectp;
fd361982 12396 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12397 }
12398 else if (section_is_p (sectp->name, &names->macro_dwo))
12399 {
049412e3 12400 dwo_sections->macro.s.section = sectp;
fd361982 12401 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12402 }
12403 else if (section_is_p (sectp->name, &names->str_dwo))
12404 {
049412e3 12405 dwo_sections->str.s.section = sectp;
fd361982 12406 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12407 }
12408 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12409 {
049412e3 12410 dwo_sections->str_offsets.s.section = sectp;
fd361982 12411 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12412 }
12413 else if (section_is_p (sectp->name, &names->types_dwo))
12414 {
12415 struct dwarf2_section_info type_section;
12416
12417 memset (&type_section, 0, sizeof (type_section));
049412e3 12418 type_section.s.section = sectp;
fd361982 12419 type_section.size = bfd_section_size (sectp);
fd5866f6 12420 dwo_sections->types.push_back (type_section);
80626a55
DE
12421 }
12422}
12423
ab5088bf 12424/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12425 by PER_CU. This is for the non-DWP case.
80626a55 12426 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12427
12428static struct dwo_file *
4ab09049
SM
12429open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12430 const char *comp_dir)
3019eac3 12431{
976ca316 12432 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12433
976ca316 12434 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12435 if (dbfd == NULL)
12436 {
b4f54984 12437 if (dwarf_read_debug)
80626a55
DE
12438 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12439 return NULL;
12440 }
263db9a1 12441
51ac9db5 12442 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12443 dwo_file->dwo_name = dwo_name;
12444 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12445 dwo_file->dbfd = std::move (dbfd);
3019eac3 12446
fb1eb2f9 12447 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12448 &dwo_file->sections);
3019eac3 12449
976ca316
SM
12450 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12451 dwo_file->cus);
3019eac3 12452
976ca316 12453 create_debug_types_hash_table (per_objfile, dwo_file.get (),
ed2dc618 12454 dwo_file->sections.types, dwo_file->tus);
3019eac3 12455
b4f54984 12456 if (dwarf_read_debug)
80626a55
DE
12457 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12458
263db9a1 12459 return dwo_file.release ();
3019eac3
DE
12460}
12461
80626a55 12462/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12463 size of each of the DWP debugging sections common to version 1 and 2 that
12464 we are interested in. */
3019eac3 12465
80626a55 12466static void
73869dc2
DE
12467dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12468 void *dwp_file_ptr)
3019eac3 12469{
9a3c8263 12470 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12471 const struct dwop_section_names *names = &dwop_section_names;
12472 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12473
80626a55 12474 /* Record the ELF section number for later lookup: this is what the
73869dc2 12475 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12476 gdb_assert (elf_section_nr < dwp_file->num_sections);
12477 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12478
80626a55
DE
12479 /* Look for specific sections that we need. */
12480 if (section_is_p (sectp->name, &names->str_dwo))
12481 {
049412e3 12482 dwp_file->sections.str.s.section = sectp;
fd361982 12483 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12484 }
12485 else if (section_is_p (sectp->name, &names->cu_index))
12486 {
049412e3 12487 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12488 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12489 }
12490 else if (section_is_p (sectp->name, &names->tu_index))
12491 {
049412e3 12492 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12493 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12494 }
12495}
3019eac3 12496
73869dc2
DE
12497/* This function is mapped across the sections and remembers the offset and
12498 size of each of the DWP version 2 debugging sections that we are interested
12499 in. This is split into a separate function because we don't know if we
12500 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12501
12502static void
12503dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12504{
9a3c8263 12505 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12506 const struct dwop_section_names *names = &dwop_section_names;
12507 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12508
12509 /* Record the ELF section number for later lookup: this is what the
12510 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12511 gdb_assert (elf_section_nr < dwp_file->num_sections);
12512 dwp_file->elf_sections[elf_section_nr] = sectp;
12513
12514 /* Look for specific sections that we need. */
12515 if (section_is_p (sectp->name, &names->abbrev_dwo))
12516 {
049412e3 12517 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12518 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12519 }
12520 else if (section_is_p (sectp->name, &names->info_dwo))
12521 {
049412e3 12522 dwp_file->sections.info.s.section = sectp;
fd361982 12523 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12524 }
12525 else if (section_is_p (sectp->name, &names->line_dwo))
12526 {
049412e3 12527 dwp_file->sections.line.s.section = sectp;
fd361982 12528 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12529 }
12530 else if (section_is_p (sectp->name, &names->loc_dwo))
12531 {
049412e3 12532 dwp_file->sections.loc.s.section = sectp;
fd361982 12533 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12534 }
12535 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12536 {
049412e3 12537 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12538 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12539 }
12540 else if (section_is_p (sectp->name, &names->macro_dwo))
12541 {
049412e3 12542 dwp_file->sections.macro.s.section = sectp;
fd361982 12543 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12544 }
12545 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12546 {
049412e3 12547 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12548 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12549 }
12550 else if (section_is_p (sectp->name, &names->types_dwo))
12551 {
049412e3 12552 dwp_file->sections.types.s.section = sectp;
fd361982 12553 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12554 }
12555}
12556
80626a55 12557/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12558
80626a55
DE
12559static hashval_t
12560hash_dwp_loaded_cutus (const void *item)
12561{
9a3c8263 12562 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12563
80626a55
DE
12564 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12565 return dwo_unit->signature;
3019eac3
DE
12566}
12567
80626a55 12568/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12569
80626a55
DE
12570static int
12571eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12572{
9a3c8263
SM
12573 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12574 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12575
80626a55
DE
12576 return dua->signature == dub->signature;
12577}
3019eac3 12578
80626a55 12579/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12580
48b490f2 12581static htab_up
298e9637 12582allocate_dwp_loaded_cutus_table ()
80626a55 12583{
48b490f2
TT
12584 return htab_up (htab_create_alloc (3,
12585 hash_dwp_loaded_cutus,
12586 eq_dwp_loaded_cutus,
12587 NULL, xcalloc, xfree));
80626a55 12588}
3019eac3 12589
ab5088bf
DE
12590/* Try to open DWP file FILE_NAME.
12591 The result is the bfd handle of the file.
12592 If there is a problem finding or opening the file, return NULL.
12593 Upon success, the canonicalized path of the file is stored in the bfd,
12594 same as symfile_bfd_open. */
12595
192b62ce 12596static gdb_bfd_ref_ptr
976ca316 12597open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 12598{
976ca316 12599 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 12600 1 /*is_dwp*/,
192b62ce 12601 1 /*search_cwd*/));
6ac97d4c
DE
12602 if (abfd != NULL)
12603 return abfd;
12604
12605 /* Work around upstream bug 15652.
12606 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12607 [Whether that's a "bug" is debatable, but it is getting in our way.]
12608 We have no real idea where the dwp file is, because gdb's realpath-ing
12609 of the executable's path may have discarded the needed info.
12610 [IWBN if the dwp file name was recorded in the executable, akin to
12611 .gnu_debuglink, but that doesn't exist yet.]
12612 Strip the directory from FILE_NAME and search again. */
12613 if (*debug_file_directory != '\0')
12614 {
12615 /* Don't implicitly search the current directory here.
12616 If the user wants to search "." to handle this case,
12617 it must be added to debug-file-directory. */
976ca316
SM
12618 return try_open_dwop_file (per_objfile, lbasename (file_name),
12619 1 /*is_dwp*/,
6ac97d4c
DE
12620 0 /*search_cwd*/);
12621 }
12622
12623 return NULL;
ab5088bf
DE
12624}
12625
80626a55
DE
12626/* Initialize the use of the DWP file for the current objfile.
12627 By convention the name of the DWP file is ${objfile}.dwp.
12628 The result is NULL if it can't be found. */
a766d390 12629
400174b1 12630static std::unique_ptr<struct dwp_file>
976ca316 12631open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 12632{
976ca316 12633 struct objfile *objfile = per_objfile->objfile;
80626a55 12634
82bf32bc
JK
12635 /* Try to find first .dwp for the binary file before any symbolic links
12636 resolving. */
6c447423
DE
12637
12638 /* If the objfile is a debug file, find the name of the real binary
12639 file and get the name of dwp file from there. */
d721ba37 12640 std::string dwp_name;
6c447423
DE
12641 if (objfile->separate_debug_objfile_backlink != NULL)
12642 {
12643 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12644 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12645
d721ba37 12646 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12647 }
12648 else
d721ba37
PA
12649 dwp_name = objfile->original_name;
12650
12651 dwp_name += ".dwp";
80626a55 12652
976ca316 12653 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12654 if (dbfd == NULL
12655 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12656 {
12657 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12658 dwp_name = objfile_name (objfile);
12659 dwp_name += ".dwp";
976ca316 12660 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
12661 }
12662
80626a55
DE
12663 if (dbfd == NULL)
12664 {
b4f54984 12665 if (dwarf_read_debug)
d721ba37 12666 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12667 return std::unique_ptr<dwp_file> ();
3019eac3 12668 }
400174b1
TT
12669
12670 const char *name = bfd_get_filename (dbfd.get ());
12671 std::unique_ptr<struct dwp_file> dwp_file
12672 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12673
0a0f4c01 12674 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12675 dwp_file->elf_sections =
976ca316 12676 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
12677 dwp_file->num_sections, asection *);
12678
400174b1
TT
12679 bfd_map_over_sections (dwp_file->dbfd.get (),
12680 dwarf2_locate_common_dwp_sections,
12681 dwp_file.get ());
80626a55 12682
976ca316 12683 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 12684
976ca316 12685 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 12686
73869dc2 12687 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12688 if (dwp_file->cus && dwp_file->tus
12689 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12690 {
12691 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12692 pretty bizarre. We use pulongest here because that's the established
4d65956b 12693 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12694 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12695 " TU version %s [in DWP file %s]"),
12696 pulongest (dwp_file->cus->version),
d721ba37 12697 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12698 }
08302ed2
DE
12699
12700 if (dwp_file->cus)
12701 dwp_file->version = dwp_file->cus->version;
12702 else if (dwp_file->tus)
12703 dwp_file->version = dwp_file->tus->version;
12704 else
12705 dwp_file->version = 2;
73869dc2
DE
12706
12707 if (dwp_file->version == 2)
400174b1
TT
12708 bfd_map_over_sections (dwp_file->dbfd.get (),
12709 dwarf2_locate_v2_dwp_sections,
12710 dwp_file.get ());
73869dc2 12711
298e9637
SM
12712 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12713 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12714
b4f54984 12715 if (dwarf_read_debug)
80626a55
DE
12716 {
12717 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12718 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12719 " %s CUs, %s TUs\n",
12720 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12721 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12722 }
12723
12724 return dwp_file;
3019eac3 12725}
c906108c 12726
ab5088bf
DE
12727/* Wrapper around open_and_init_dwp_file, only open it once. */
12728
12729static struct dwp_file *
976ca316 12730get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 12731{
976ca316 12732 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 12733 {
976ca316
SM
12734 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12735 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12736 }
976ca316 12737 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12738}
12739
80626a55
DE
12740/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12741 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12742 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12743 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12744 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12745
12746 This is called, for example, when wanting to read a variable with a
12747 complex location. Therefore we don't want to do file i/o for every call.
12748 Therefore we don't want to look for a DWO file on every call.
12749 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12750 then we check if we've already seen DWO_NAME, and only THEN do we check
12751 for a DWO file.
12752
1c658ad5 12753 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12754 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12755
3019eac3 12756static struct dwo_unit *
4ab09049 12757lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12758 ULONGEST signature, int is_debug_types)
3019eac3 12759{
976ca316
SM
12760 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12761 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
12762 const char *kind = is_debug_types ? "TU" : "CU";
12763 void **dwo_file_slot;
3019eac3 12764 struct dwo_file *dwo_file;
80626a55 12765 struct dwp_file *dwp_file;
cb1df416 12766
6a506a2d
DE
12767 /* First see if there's a DWP file.
12768 If we have a DWP file but didn't find the DWO inside it, don't
12769 look for the original DWO file. It makes gdb behave differently
12770 depending on whether one is debugging in the build tree. */
cf2c3c16 12771
976ca316 12772 dwp_file = get_dwp_file (per_objfile);
80626a55 12773 if (dwp_file != NULL)
cf2c3c16 12774 {
80626a55
DE
12775 const struct dwp_hash_table *dwp_htab =
12776 is_debug_types ? dwp_file->tus : dwp_file->cus;
12777
12778 if (dwp_htab != NULL)
12779 {
12780 struct dwo_unit *dwo_cutu =
976ca316
SM
12781 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12782 is_debug_types);
80626a55
DE
12783
12784 if (dwo_cutu != NULL)
12785 {
b4f54984 12786 if (dwarf_read_debug)
80626a55
DE
12787 {
12788 fprintf_unfiltered (gdb_stdlog,
12789 "Virtual DWO %s %s found: @%s\n",
12790 kind, hex_string (signature),
12791 host_address_to_string (dwo_cutu));
12792 }
12793 return dwo_cutu;
12794 }
12795 }
12796 }
6a506a2d 12797 else
80626a55 12798 {
6a506a2d 12799 /* No DWP file, look for the DWO file. */
80626a55 12800
976ca316 12801 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 12802 if (*dwo_file_slot == NULL)
80626a55 12803 {
6a506a2d 12804 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 12805 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 12806 }
6a506a2d 12807 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12808 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12809
6a506a2d 12810 if (dwo_file != NULL)
19c3d4c9 12811 {
6a506a2d
DE
12812 struct dwo_unit *dwo_cutu = NULL;
12813
12814 if (is_debug_types && dwo_file->tus)
12815 {
12816 struct dwo_unit find_dwo_cutu;
12817
12818 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12819 find_dwo_cutu.signature = signature;
9a3c8263 12820 dwo_cutu
b0b6a987
TT
12821 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12822 &find_dwo_cutu);
6a506a2d 12823 }
33c5cd75 12824 else if (!is_debug_types && dwo_file->cus)
80626a55 12825 {
33c5cd75
DB
12826 struct dwo_unit find_dwo_cutu;
12827
12828 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12829 find_dwo_cutu.signature = signature;
b0b6a987 12830 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12831 &find_dwo_cutu);
6a506a2d
DE
12832 }
12833
12834 if (dwo_cutu != NULL)
12835 {
b4f54984 12836 if (dwarf_read_debug)
6a506a2d
DE
12837 {
12838 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12839 kind, dwo_name, hex_string (signature),
12840 host_address_to_string (dwo_cutu));
12841 }
12842 return dwo_cutu;
80626a55
DE
12843 }
12844 }
2e276125 12845 }
9cdd5dbd 12846
80626a55
DE
12847 /* We didn't find it. This could mean a dwo_id mismatch, or
12848 someone deleted the DWO/DWP file, or the search path isn't set up
12849 correctly to find the file. */
12850
b4f54984 12851 if (dwarf_read_debug)
80626a55
DE
12852 {
12853 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12854 kind, dwo_name, hex_string (signature));
12855 }
3019eac3 12856
6656a72d
DE
12857 /* This is a warning and not a complaint because it can be caused by
12858 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12859 {
12860 /* Print the name of the DWP file if we looked there, helps the user
12861 better diagnose the problem. */
791afaa2 12862 std::string dwp_text;
43942612
DE
12863
12864 if (dwp_file != NULL)
791afaa2
TT
12865 dwp_text = string_printf (" [in DWP file %s]",
12866 lbasename (dwp_file->name));
43942612 12867
9d8780f0 12868 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 12869 " [in module %s]"),
4ab09049
SM
12870 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12871 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 12872 }
3019eac3 12873 return NULL;
5fb290d7
DJ
12874}
12875
80626a55
DE
12876/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12877 See lookup_dwo_cutu_unit for details. */
12878
12879static struct dwo_unit *
4ab09049 12880lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
12881 ULONGEST signature)
12882{
4ab09049
SM
12883 gdb_assert (!cu->per_cu->is_debug_types);
12884
12885 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
12886}
12887
12888/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12889 See lookup_dwo_cutu_unit for details. */
12890
12891static struct dwo_unit *
4ab09049 12892lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 12893{
4ab09049
SM
12894 gdb_assert (cu->per_cu->is_debug_types);
12895
12896 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12897
12898 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
12899}
12900
89e63ee4
DE
12901/* Traversal function for queue_and_load_all_dwo_tus. */
12902
12903static int
12904queue_and_load_dwo_tu (void **slot, void *info)
12905{
12906 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 12907 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 12908 ULONGEST signature = dwo_unit->signature;
d460f660 12909 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
12910
12911 if (sig_type != NULL)
12912 {
12913 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12914
12915 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12916 a real dependency of PER_CU on SIG_TYPE. That is detected later
12917 while processing PER_CU. */
120ce1b5 12918 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
12919 load_full_type_unit (sig_cu, cu->per_objfile);
12920 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12921 }
12922
12923 return 1;
12924}
12925
1b555f17 12926/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
12927 The DWO may have the only definition of the type, though it may not be
12928 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12929 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12930
12931static void
1b555f17 12932queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
12933{
12934 struct dwo_unit *dwo_unit;
12935 struct dwo_file *dwo_file;
12936
1b555f17
SM
12937 gdb_assert (cu != nullptr);
12938 gdb_assert (!cu->per_cu->is_debug_types);
12939 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 12940
1b555f17 12941 dwo_unit = cu->dwo_unit;
89e63ee4
DE
12942 gdb_assert (dwo_unit != NULL);
12943
12944 dwo_file = dwo_unit->dwo_file;
12945 if (dwo_file->tus != NULL)
1b555f17 12946 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
12947}
12948
3019eac3 12949/* Read in various DIEs. */
348e048f 12950
d389af10 12951/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12952 Inherit only the children of the DW_AT_abstract_origin DIE not being
12953 already referenced by DW_AT_abstract_origin from the children of the
12954 current DIE. */
d389af10
JK
12955
12956static void
12957inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12958{
12959 struct die_info *child_die;
791afaa2 12960 sect_offset *offsetp;
d389af10
JK
12961 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12962 struct die_info *origin_die;
12963 /* Iterator of the ORIGIN_DIE children. */
12964 struct die_info *origin_child_die;
d389af10 12965 struct attribute *attr;
cd02d79d
PA
12966 struct dwarf2_cu *origin_cu;
12967 struct pending **origin_previous_list_in_scope;
d389af10
JK
12968
12969 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12970 if (!attr)
12971 return;
12972
cd02d79d
PA
12973 /* Note that following die references may follow to a die in a
12974 different cu. */
12975
12976 origin_cu = cu;
12977 origin_die = follow_die_ref (die, attr, &origin_cu);
12978
12979 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12980 symbols in. */
12981 origin_previous_list_in_scope = origin_cu->list_in_scope;
12982 origin_cu->list_in_scope = cu->list_in_scope;
12983
edb3359d
DJ
12984 if (die->tag != origin_die->tag
12985 && !(die->tag == DW_TAG_inlined_subroutine
12986 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12987 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12988 sect_offset_str (die->sect_off),
12989 sect_offset_str (origin_die->sect_off));
d389af10 12990
791afaa2 12991 std::vector<sect_offset> offsets;
d389af10 12992
3ea89b92
PMR
12993 for (child_die = die->child;
12994 child_die && child_die->tag;
436c571c 12995 child_die = child_die->sibling)
3ea89b92
PMR
12996 {
12997 struct die_info *child_origin_die;
12998 struct dwarf2_cu *child_origin_cu;
12999
13000 /* We are trying to process concrete instance entries:
216f72a1 13001 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13002 it's not relevant to our analysis here. i.e. detecting DIEs that are
13003 present in the abstract instance but not referenced in the concrete
13004 one. */
216f72a1
JK
13005 if (child_die->tag == DW_TAG_call_site
13006 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13007 continue;
13008
c38f313d
DJ
13009 /* For each CHILD_DIE, find the corresponding child of
13010 ORIGIN_DIE. If there is more than one layer of
13011 DW_AT_abstract_origin, follow them all; there shouldn't be,
13012 but GCC versions at least through 4.4 generate this (GCC PR
13013 40573). */
3ea89b92
PMR
13014 child_origin_die = child_die;
13015 child_origin_cu = cu;
c38f313d
DJ
13016 while (1)
13017 {
cd02d79d
PA
13018 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13019 child_origin_cu);
c38f313d
DJ
13020 if (attr == NULL)
13021 break;
cd02d79d
PA
13022 child_origin_die = follow_die_ref (child_origin_die, attr,
13023 &child_origin_cu);
c38f313d
DJ
13024 }
13025
d389af10
JK
13026 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13027 counterpart may exist. */
c38f313d 13028 if (child_origin_die != child_die)
d389af10 13029 {
edb3359d
DJ
13030 if (child_die->tag != child_origin_die->tag
13031 && !(child_die->tag == DW_TAG_inlined_subroutine
13032 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13033 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13034 "different tags"),
9d8780f0
SM
13035 sect_offset_str (child_die->sect_off),
13036 sect_offset_str (child_origin_die->sect_off));
c38f313d 13037 if (child_origin_die->parent != origin_die)
b98664d3 13038 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13039 "different parents"),
9d8780f0
SM
13040 sect_offset_str (child_die->sect_off),
13041 sect_offset_str (child_origin_die->sect_off));
c38f313d 13042 else
791afaa2 13043 offsets.push_back (child_origin_die->sect_off);
d389af10 13044 }
d389af10 13045 }
791afaa2
TT
13046 std::sort (offsets.begin (), offsets.end ());
13047 sect_offset *offsets_end = offsets.data () + offsets.size ();
13048 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13049 if (offsetp[-1] == *offsetp)
b98664d3 13050 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13051 "to DIE %s as their abstract origin"),
13052 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13053
791afaa2 13054 offsetp = offsets.data ();
d389af10
JK
13055 origin_child_die = origin_die->child;
13056 while (origin_child_die && origin_child_die->tag)
13057 {
13058 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13059 while (offsetp < offsets_end
9c541725 13060 && *offsetp < origin_child_die->sect_off)
d389af10 13061 offsetp++;
b64f50a1 13062 if (offsetp >= offsets_end
9c541725 13063 || *offsetp > origin_child_die->sect_off)
d389af10 13064 {
adde2bff
DE
13065 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13066 Check whether we're already processing ORIGIN_CHILD_DIE.
13067 This can happen with mutually referenced abstract_origins.
13068 PR 16581. */
13069 if (!origin_child_die->in_process)
13070 process_die (origin_child_die, origin_cu);
d389af10 13071 }
436c571c 13072 origin_child_die = origin_child_die->sibling;
d389af10 13073 }
cd02d79d 13074 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13075
13076 if (cu != origin_cu)
13077 compute_delayed_physnames (origin_cu);
d389af10
JK
13078}
13079
c906108c 13080static void
e7c27a73 13081read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13082{
5e22e966 13083 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13084 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13085 struct context_stack *newobj;
c906108c
SS
13086 CORE_ADDR lowpc;
13087 CORE_ADDR highpc;
13088 struct die_info *child_die;
edb3359d 13089 struct attribute *attr, *call_line, *call_file;
15d034d0 13090 const char *name;
e142c38c 13091 CORE_ADDR baseaddr;
801e3a5b 13092 struct block *block;
edb3359d 13093 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13094 std::vector<struct symbol *> template_args;
34eaf542 13095 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13096
13097 if (inlined_func)
13098 {
13099 /* If we do not have call site information, we can't show the
13100 caller of this inlined function. That's too confusing, so
13101 only use the scope for local variables. */
13102 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13103 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13104 if (call_line == NULL || call_file == NULL)
13105 {
13106 read_lexical_block_scope (die, cu);
13107 return;
13108 }
13109 }
c906108c 13110
b3b3bada 13111 baseaddr = objfile->text_section_offset ();
e142c38c 13112
94af9270 13113 name = dwarf2_name (die, cu);
c906108c 13114
e8d05480
JB
13115 /* Ignore functions with missing or empty names. These are actually
13116 illegal according to the DWARF standard. */
13117 if (name == NULL)
13118 {
b98664d3 13119 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13120 sect_offset_str (die->sect_off));
e8d05480
JB
13121 return;
13122 }
13123
13124 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13125 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13126 <= PC_BOUNDS_INVALID)
e8d05480 13127 {
ae4d0c03
PM
13128 attr = dwarf2_attr (die, DW_AT_external, cu);
13129 if (!attr || !DW_UNSND (attr))
b98664d3 13130 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13131 "for subprogram DIE at %s"),
13132 sect_offset_str (die->sect_off));
e8d05480
JB
13133 return;
13134 }
c906108c 13135
3e29f34a
MR
13136 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13137 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13138
34eaf542
TT
13139 /* If we have any template arguments, then we must allocate a
13140 different sort of symbol. */
436c571c 13141 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13142 {
13143 if (child_die->tag == DW_TAG_template_type_param
13144 || child_die->tag == DW_TAG_template_value_param)
13145 {
8c14c3a3 13146 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13147 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13148 break;
13149 }
13150 }
13151
c24bdb02 13152 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13153 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13154 (struct symbol *) templ_func);
4c2df51b 13155
81873cc8 13156 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13157 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13158 cu->language);
13159
4cecd739
DJ
13160 /* If there is a location expression for DW_AT_frame_base, record
13161 it. */
e142c38c 13162 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13163 if (attr != nullptr)
fe978cb0 13164 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13165
63e43d3a
PMR
13166 /* If there is a location for the static link, record it. */
13167 newobj->static_link = NULL;
13168 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13169 if (attr != nullptr)
63e43d3a 13170 {
224c3ddb
SM
13171 newobj->static_link
13172 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13173 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13174 cu->addr_type ());
63e43d3a
PMR
13175 }
13176
c24bdb02 13177 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13178
639d11d3 13179 if (die->child != NULL)
c906108c 13180 {
639d11d3 13181 child_die = die->child;
c906108c
SS
13182 while (child_die && child_die->tag)
13183 {
34eaf542
TT
13184 if (child_die->tag == DW_TAG_template_type_param
13185 || child_die->tag == DW_TAG_template_value_param)
13186 {
13187 struct symbol *arg = new_symbol (child_die, NULL, cu);
13188
f1078f66 13189 if (arg != NULL)
2f4732b0 13190 template_args.push_back (arg);
34eaf542
TT
13191 }
13192 else
13193 process_die (child_die, cu);
436c571c 13194 child_die = child_die->sibling;
c906108c
SS
13195 }
13196 }
13197
d389af10
JK
13198 inherit_abstract_dies (die, cu);
13199
4a811a97
UW
13200 /* If we have a DW_AT_specification, we might need to import using
13201 directives from the context of the specification DIE. See the
13202 comment in determine_prefix. */
13203 if (cu->language == language_cplus
13204 && dwarf2_attr (die, DW_AT_specification, cu))
13205 {
13206 struct dwarf2_cu *spec_cu = cu;
13207 struct die_info *spec_die = die_specification (die, &spec_cu);
13208
13209 while (spec_die)
13210 {
13211 child_die = spec_die->child;
13212 while (child_die && child_die->tag)
13213 {
13214 if (child_die->tag == DW_TAG_imported_module)
13215 process_die (child_die, spec_cu);
436c571c 13216 child_die = child_die->sibling;
4a811a97
UW
13217 }
13218
13219 /* In some cases, GCC generates specification DIEs that
13220 themselves contain DW_AT_specification attributes. */
13221 spec_die = die_specification (spec_die, &spec_cu);
13222 }
13223 }
13224
c24bdb02 13225 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13226 /* Make a block for the local symbols within. */
c24bdb02 13227 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13228 cstk.static_link, lowpc, highpc);
801e3a5b 13229
df8a16a1 13230 /* For C++, set the block's scope. */
45280282
IB
13231 if ((cu->language == language_cplus
13232 || cu->language == language_fortran
c44af4eb
TT
13233 || cu->language == language_d
13234 || cu->language == language_rust)
4d4ec4e5 13235 && cu->processing_has_namespace_info)
195a3f6c
TT
13236 block_set_scope (block, determine_prefix (die, cu),
13237 &objfile->objfile_obstack);
df8a16a1 13238
801e3a5b
JB
13239 /* If we have address ranges, record them. */
13240 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13241
a60f3166 13242 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13243
34eaf542 13244 /* Attach template arguments to function. */
2f4732b0 13245 if (!template_args.empty ())
34eaf542
TT
13246 {
13247 gdb_assert (templ_func != NULL);
13248
2f4732b0 13249 templ_func->n_template_arguments = template_args.size ();
34eaf542 13250 templ_func->template_arguments
8d749320
SM
13251 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13252 templ_func->n_template_arguments);
34eaf542 13253 memcpy (templ_func->template_arguments,
2f4732b0 13254 template_args.data (),
34eaf542 13255 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13256
13257 /* Make sure that the symtab is set on the new symbols. Even
13258 though they don't appear in this symtab directly, other parts
13259 of gdb assume that symbols do, and this is reasonably
13260 true. */
8634679f 13261 for (symbol *sym : template_args)
3e1d3d8c 13262 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13263 }
13264
208d8187
JB
13265 /* In C++, we can have functions nested inside functions (e.g., when
13266 a function declares a class that has methods). This means that
13267 when we finish processing a function scope, we may need to go
13268 back to building a containing block's symbol lists. */
c24bdb02
KS
13269 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13270 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13271
921e78cf
JB
13272 /* If we've finished processing a top-level function, subsequent
13273 symbols go in the file symbol list. */
c24bdb02
KS
13274 if (cu->get_builder ()->outermost_context_p ())
13275 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13276}
13277
13278/* Process all the DIES contained within a lexical block scope. Start
13279 a new scope, process the dies, and then close the scope. */
13280
13281static void
e7c27a73 13282read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13283{
5e22e966 13284 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13285 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13286 CORE_ADDR lowpc, highpc;
13287 struct die_info *child_die;
e142c38c
DJ
13288 CORE_ADDR baseaddr;
13289
b3b3bada 13290 baseaddr = objfile->text_section_offset ();
c906108c
SS
13291
13292 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13293 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13294 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13295 be nasty. Might be easier to properly extend generic blocks to
af34e669 13296 describe ranges. */
e385593e
JK
13297 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13298 {
13299 case PC_BOUNDS_NOT_PRESENT:
13300 /* DW_TAG_lexical_block has no attributes, process its children as if
13301 there was no wrapping by that DW_TAG_lexical_block.
13302 GCC does no longer produces such DWARF since GCC r224161. */
13303 for (child_die = die->child;
13304 child_die != NULL && child_die->tag;
436c571c 13305 child_die = child_die->sibling)
4f7bc5ed
TT
13306 {
13307 /* We might already be processing this DIE. This can happen
13308 in an unusual circumstance -- where a subroutine A
13309 appears lexically in another subroutine B, but A actually
13310 inlines B. The recursion is broken here, rather than in
13311 inherit_abstract_dies, because it seems better to simply
13312 drop concrete children here. */
13313 if (!child_die->in_process)
13314 process_die (child_die, cu);
13315 }
e385593e
JK
13316 return;
13317 case PC_BOUNDS_INVALID:
13318 return;
13319 }
3e29f34a
MR
13320 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13321 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13322
c24bdb02 13323 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13324 if (die->child != NULL)
c906108c 13325 {
639d11d3 13326 child_die = die->child;
c906108c
SS
13327 while (child_die && child_die->tag)
13328 {
e7c27a73 13329 process_die (child_die, cu);
436c571c 13330 child_die = child_die->sibling;
c906108c
SS
13331 }
13332 }
3ea89b92 13333 inherit_abstract_dies (die, cu);
c24bdb02 13334 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13335
c24bdb02
KS
13336 if (*cu->get_builder ()->get_local_symbols () != NULL
13337 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13338 {
801e3a5b 13339 struct block *block
c24bdb02 13340 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13341 cstk.start_addr, highpc);
801e3a5b
JB
13342
13343 /* Note that recording ranges after traversing children, as we
13344 do here, means that recording a parent's ranges entails
13345 walking across all its children's ranges as they appear in
13346 the address map, which is quadratic behavior.
13347
13348 It would be nicer to record the parent's ranges before
13349 traversing its children, simply overriding whatever you find
13350 there. But since we don't even decide whether to create a
13351 block until after we've traversed its children, that's hard
13352 to do. */
13353 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13354 }
c24bdb02
KS
13355 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13356 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13357}
13358
216f72a1 13359/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13360
13361static void
13362read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13363{
5e22e966 13364 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13365 struct objfile *objfile = per_objfile->objfile;
08feed99 13366 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13367 CORE_ADDR pc, baseaddr;
13368 struct attribute *attr;
13369 struct call_site *call_site, call_site_local;
13370 void **slot;
13371 int nparams;
13372 struct die_info *child_die;
13373
b3b3bada 13374 baseaddr = objfile->text_section_offset ();
96408a79 13375
216f72a1
JK
13376 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13377 if (attr == NULL)
13378 {
13379 /* This was a pre-DWARF-5 GNU extension alias
13380 for DW_AT_call_return_pc. */
13381 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13382 }
96408a79
SA
13383 if (!attr)
13384 {
b98664d3 13385 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13386 "DIE %s [in module %s]"),
13387 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13388 return;
13389 }
cd6c91b4 13390 pc = attr->value_as_address () + baseaddr;
3e29f34a 13391 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13392
13393 if (cu->call_site_htab == NULL)
13394 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13395 NULL, &objfile->objfile_obstack,
13396 hashtab_obstack_allocate, NULL);
13397 call_site_local.pc = pc;
13398 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13399 if (*slot != NULL)
13400 {
b98664d3 13401 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13402 "DIE %s [in module %s]"),
13403 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13404 objfile_name (objfile));
96408a79
SA
13405 return;
13406 }
13407
13408 /* Count parameters at the caller. */
13409
13410 nparams = 0;
13411 for (child_die = die->child; child_die && child_die->tag;
436c571c 13412 child_die = child_die->sibling)
96408a79 13413 {
216f72a1
JK
13414 if (child_die->tag != DW_TAG_call_site_parameter
13415 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13416 {
b98664d3 13417 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13418 "DW_TAG_call_site child DIE %s [in module %s]"),
13419 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13420 objfile_name (objfile));
96408a79
SA
13421 continue;
13422 }
13423
13424 nparams++;
13425 }
13426
224c3ddb
SM
13427 call_site
13428 = ((struct call_site *)
13429 obstack_alloc (&objfile->objfile_obstack,
13430 sizeof (*call_site)
13431 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13432 *slot = call_site;
13433 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13434 call_site->pc = pc;
13435
216f72a1
JK
13436 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13437 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13438 {
13439 struct die_info *func_die;
13440
13441 /* Skip also over DW_TAG_inlined_subroutine. */
13442 for (func_die = die->parent;
13443 func_die && func_die->tag != DW_TAG_subprogram
13444 && func_die->tag != DW_TAG_subroutine_type;
13445 func_die = func_die->parent);
13446
216f72a1
JK
13447 /* DW_AT_call_all_calls is a superset
13448 of DW_AT_call_all_tail_calls. */
96408a79 13449 if (func_die
216f72a1 13450 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13451 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13452 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13453 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13454 {
13455 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13456 not complete. But keep CALL_SITE for look ups via call_site_htab,
13457 both the initial caller containing the real return address PC and
13458 the final callee containing the current PC of a chain of tail
13459 calls do not need to have the tail call list complete. But any
13460 function candidate for a virtual tail call frame searched via
13461 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13462 determined unambiguously. */
13463 }
13464 else
13465 {
13466 struct type *func_type = NULL;
13467
13468 if (func_die)
13469 func_type = get_die_type (func_die, cu);
13470 if (func_type != NULL)
13471 {
78134374 13472 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13473
13474 /* Enlist this call site to the function. */
13475 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13476 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13477 }
13478 else
b98664d3 13479 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13480 "DIE %s [in module %s]"),
13481 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13482 }
13483 }
13484
216f72a1
JK
13485 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13486 if (attr == NULL)
13487 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13488 if (attr == NULL)
13489 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13490 if (attr == NULL)
216f72a1
JK
13491 {
13492 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13493 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13494 }
96408a79 13495 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13496 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13497 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13498 else if (attr->form_is_block ())
96408a79
SA
13499 {
13500 struct dwarf2_locexpr_baton *dlbaton;
13501
8d749320 13502 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13503 dlbaton->data = DW_BLOCK (attr)->data;
13504 dlbaton->size = DW_BLOCK (attr)->size;
a50264ba 13505 dlbaton->per_objfile = per_objfile;
96408a79
SA
13506 dlbaton->per_cu = cu->per_cu;
13507
13508 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13509 }
cd6c91b4 13510 else if (attr->form_is_ref ())
96408a79 13511 {
96408a79
SA
13512 struct dwarf2_cu *target_cu = cu;
13513 struct die_info *target_die;
13514
ac9ec31b 13515 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13516 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13517 if (die_is_declaration (target_die, target_cu))
13518 {
7d45c7c3 13519 const char *target_physname;
9112db09
JK
13520
13521 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13522 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13523 if (target_physname == NULL)
9112db09 13524 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13525 if (target_physname == NULL)
b98664d3 13526 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13527 "physname, for referencing DIE %s [in module %s]"),
13528 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13529 else
7d455152 13530 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13531 }
13532 else
13533 {
13534 CORE_ADDR lowpc;
13535
13536 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13537 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13538 <= PC_BOUNDS_INVALID)
b98664d3 13539 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13540 "low pc, for referencing DIE %s [in module %s]"),
13541 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13542 else
3e29f34a
MR
13543 {
13544 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13545 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13546 }
96408a79
SA
13547 }
13548 }
13549 else
b98664d3 13550 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13551 "block nor reference, for DIE %s [in module %s]"),
13552 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13553
13554 call_site->per_cu = cu->per_cu;
9f47c707 13555 call_site->per_objfile = per_objfile;
96408a79
SA
13556
13557 for (child_die = die->child;
13558 child_die && child_die->tag;
436c571c 13559 child_die = child_die->sibling)
96408a79 13560 {
96408a79 13561 struct call_site_parameter *parameter;
1788b2d3 13562 struct attribute *loc, *origin;
96408a79 13563
216f72a1
JK
13564 if (child_die->tag != DW_TAG_call_site_parameter
13565 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13566 {
13567 /* Already printed the complaint above. */
13568 continue;
13569 }
13570
13571 gdb_assert (call_site->parameter_count < nparams);
13572 parameter = &call_site->parameter[call_site->parameter_count];
13573
1788b2d3
JK
13574 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13575 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13576 register is contained in DW_AT_call_value. */
96408a79 13577
24c5c679 13578 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13579 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13580 if (origin == NULL)
13581 {
13582 /* This was a pre-DWARF-5 GNU extension alias
13583 for DW_AT_call_parameter. */
13584 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13585 }
cd6c91b4 13586 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13587 {
1788b2d3 13588 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13589
0826b30a 13590 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13591 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13592 {
13593 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13594 binding can be done only inside one CU. Such referenced DIE
13595 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13596 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13597 "DW_TAG_call_site child DIE %s [in module %s]"),
13598 sect_offset_str (child_die->sect_off),
9c541725 13599 objfile_name (objfile));
d76b7dbc
JK
13600 continue;
13601 }
9c541725
PA
13602 parameter->u.param_cu_off
13603 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13604 }
4fc6c0d5 13605 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13606 {
b98664d3 13607 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13608 "DW_TAG_call_site child DIE %s [in module %s]"),
13609 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13610 continue;
13611 }
24c5c679 13612 else
96408a79 13613 {
24c5c679
JK
13614 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13615 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13616 if (parameter->u.dwarf_reg != -1)
13617 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13618 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13619 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13620 &parameter->u.fb_offset))
13621 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13622 else
13623 {
b98664d3 13624 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13625 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13626 "DW_TAG_call_site child DIE %s "
24c5c679 13627 "[in module %s]"),
9d8780f0 13628 sect_offset_str (child_die->sect_off),
9c541725 13629 objfile_name (objfile));
24c5c679
JK
13630 continue;
13631 }
96408a79
SA
13632 }
13633
216f72a1
JK
13634 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13635 if (attr == NULL)
13636 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13637 if (attr == NULL || !attr->form_is_block ())
96408a79 13638 {
b98664d3 13639 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13640 "DW_TAG_call_site child DIE %s [in module %s]"),
13641 sect_offset_str (child_die->sect_off),
9c541725 13642 objfile_name (objfile));
96408a79
SA
13643 continue;
13644 }
13645 parameter->value = DW_BLOCK (attr)->data;
13646 parameter->value_size = DW_BLOCK (attr)->size;
13647
13648 /* Parameters are not pre-cleared by memset above. */
13649 parameter->data_value = NULL;
13650 parameter->data_value_size = 0;
13651 call_site->parameter_count++;
13652
216f72a1
JK
13653 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13654 if (attr == NULL)
13655 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13656 if (attr != nullptr)
96408a79 13657 {
4fc6c0d5 13658 if (!attr->form_is_block ())
b98664d3 13659 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13660 "DW_TAG_call_site child DIE %s [in module %s]"),
13661 sect_offset_str (child_die->sect_off),
9c541725 13662 objfile_name (objfile));
96408a79
SA
13663 else
13664 {
13665 parameter->data_value = DW_BLOCK (attr)->data;
13666 parameter->data_value_size = DW_BLOCK (attr)->size;
13667 }
13668 }
13669 }
13670}
13671
71a3c369
TT
13672/* Helper function for read_variable. If DIE represents a virtual
13673 table, then return the type of the concrete object that is
13674 associated with the virtual table. Otherwise, return NULL. */
13675
13676static struct type *
13677rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13678{
13679 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13680 if (attr == NULL)
13681 return NULL;
13682
13683 /* Find the type DIE. */
13684 struct die_info *type_die = NULL;
13685 struct dwarf2_cu *type_cu = cu;
13686
cd6c91b4 13687 if (attr->form_is_ref ())
71a3c369
TT
13688 type_die = follow_die_ref (die, attr, &type_cu);
13689 if (type_die == NULL)
13690 return NULL;
13691
13692 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13693 return NULL;
13694 return die_containing_type (type_die, type_cu);
13695}
13696
13697/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13698
13699static void
13700read_variable (struct die_info *die, struct dwarf2_cu *cu)
13701{
13702 struct rust_vtable_symbol *storage = NULL;
13703
13704 if (cu->language == language_rust)
13705 {
13706 struct type *containing_type = rust_containing_type (die, cu);
13707
13708 if (containing_type != NULL)
13709 {
5e22e966 13710 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13711
8c14c3a3 13712 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13713 storage->concrete_type = containing_type;
cf724bc9 13714 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13715 }
13716 }
13717
e4a62c65
TV
13718 struct symbol *res = new_symbol (die, NULL, cu, storage);
13719 struct attribute *abstract_origin
13720 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13721 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13722 if (res == NULL && loc && abstract_origin)
13723 {
13724 /* We have a variable without a name, but with a location and an abstract
13725 origin. This may be a concrete instance of an abstract variable
13726 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13727 later. */
13728 struct dwarf2_cu *origin_cu = cu;
13729 struct die_info *origin_die
13730 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13731 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13732 per_objfile->per_bfd->abstract_to_concrete
13733 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13734 }
71a3c369
TT
13735}
13736
43988095
JK
13737/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13738 reading .debug_rnglists.
13739 Callback's type should be:
13740 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13741 Return true if the attributes are present and valid, otherwise,
13742 return false. */
13743
13744template <typename Callback>
13745static bool
13746dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13747 Callback &&callback)
13748{
976ca316
SM
13749 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13750 struct objfile *objfile = per_objfile->objfile;
43988095 13751 bfd *obfd = objfile->obfd;
43988095 13752 /* Base address selection entry. */
2b24b6e4 13753 gdb::optional<CORE_ADDR> base;
43988095 13754 const gdb_byte *buffer;
43988095
JK
13755 CORE_ADDR baseaddr;
13756 bool overflow = false;
13757
43988095
JK
13758 base = cu->base_address;
13759
976ca316
SM
13760 per_objfile->per_bfd->rnglists.read (objfile);
13761 if (offset >= per_objfile->per_bfd->rnglists.size)
43988095 13762 {
b98664d3 13763 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13764 offset);
13765 return false;
13766 }
976ca316 13767 buffer = per_objfile->per_bfd->rnglists.buffer + offset;
43988095 13768
b3b3bada 13769 baseaddr = objfile->text_section_offset ();
43988095
JK
13770
13771 while (1)
13772 {
7814882a
JK
13773 /* Initialize it due to a false compiler warning. */
13774 CORE_ADDR range_beginning = 0, range_end = 0;
976ca316
SM
13775 const gdb_byte *buf_end = (per_objfile->per_bfd->rnglists.buffer
13776 + per_objfile->per_bfd->rnglists.size);
43988095
JK
13777 unsigned int bytes_read;
13778
13779 if (buffer == buf_end)
13780 {
13781 overflow = true;
13782 break;
13783 }
13784 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13785 switch (rlet)
13786 {
13787 case DW_RLE_end_of_list:
13788 break;
13789 case DW_RLE_base_address:
13790 if (buffer + cu->header.addr_size > buf_end)
13791 {
13792 overflow = true;
13793 break;
13794 }
c8a7a66f 13795 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13796 buffer += bytes_read;
13797 break;
13798 case DW_RLE_start_length:
13799 if (buffer + cu->header.addr_size > buf_end)
13800 {
13801 overflow = true;
13802 break;
13803 }
c8a7a66f
TT
13804 range_beginning = cu->header.read_address (obfd, buffer,
13805 &bytes_read);
43988095
JK
13806 buffer += bytes_read;
13807 range_end = (range_beginning
13808 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13809 buffer += bytes_read;
13810 if (buffer > buf_end)
13811 {
13812 overflow = true;
13813 break;
13814 }
13815 break;
13816 case DW_RLE_offset_pair:
13817 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13818 buffer += bytes_read;
13819 if (buffer > buf_end)
13820 {
13821 overflow = true;
13822 break;
13823 }
13824 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13825 buffer += bytes_read;
13826 if (buffer > buf_end)
13827 {
13828 overflow = true;
13829 break;
13830 }
13831 break;
13832 case DW_RLE_start_end:
13833 if (buffer + 2 * cu->header.addr_size > buf_end)
13834 {
13835 overflow = true;
13836 break;
13837 }
c8a7a66f
TT
13838 range_beginning = cu->header.read_address (obfd, buffer,
13839 &bytes_read);
43988095 13840 buffer += bytes_read;
c8a7a66f 13841 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13842 buffer += bytes_read;
13843 break;
13844 default:
b98664d3 13845 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13846 return false;
13847 }
13848 if (rlet == DW_RLE_end_of_list || overflow)
13849 break;
13850 if (rlet == DW_RLE_base_address)
13851 continue;
13852
2b24b6e4 13853 if (!base.has_value ())
43988095
JK
13854 {
13855 /* We have no valid base address for the ranges
13856 data. */
b98664d3 13857 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13858 return false;
13859 }
13860
13861 if (range_beginning > range_end)
13862 {
13863 /* Inverted range entries are invalid. */
b98664d3 13864 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13865 return false;
13866 }
13867
13868 /* Empty range entries have no effect. */
13869 if (range_beginning == range_end)
13870 continue;
13871
2b24b6e4
TT
13872 range_beginning += *base;
13873 range_end += *base;
43988095
JK
13874
13875 /* A not-uncommon case of bad debug info.
13876 Don't pollute the addrmap with bad data. */
13877 if (range_beginning + baseaddr == 0
976ca316 13878 && !per_objfile->per_bfd->has_section_at_zero)
43988095 13879 {
b98664d3 13880 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13881 " [in module %s]"), objfile_name (objfile));
13882 continue;
13883 }
13884
13885 callback (range_beginning, range_end);
13886 }
13887
13888 if (overflow)
13889 {
b98664d3 13890 complaint (_("Offset %d is not terminated "
43988095
JK
13891 "for DW_AT_ranges attribute"),
13892 offset);
13893 return false;
13894 }
13895
13896 return true;
13897}
13898
13899/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13900 Callback's type should be:
13901 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13902 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13903
43988095 13904template <typename Callback>
43039443 13905static int
5f46c5a5 13906dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13907 Callback &&callback)
43039443 13908{
5e22e966
SM
13909 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13910 struct objfile *objfile = per_objfile->objfile;
43039443
JK
13911 struct comp_unit_head *cu_header = &cu->header;
13912 bfd *obfd = objfile->obfd;
13913 unsigned int addr_size = cu_header->addr_size;
13914 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13915 /* Base address selection entry. */
2b24b6e4 13916 gdb::optional<CORE_ADDR> base;
43039443 13917 unsigned int dummy;
d521ce57 13918 const gdb_byte *buffer;
ff013f42 13919 CORE_ADDR baseaddr;
43039443 13920
43988095
JK
13921 if (cu_header->version >= 5)
13922 return dwarf2_rnglists_process (offset, cu, callback);
13923
d00adf39 13924 base = cu->base_address;
43039443 13925
5e22e966
SM
13926 per_objfile->per_bfd->ranges.read (objfile);
13927 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 13928 {
b98664d3 13929 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13930 offset);
13931 return 0;
13932 }
5e22e966 13933 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 13934
b3b3bada 13935 baseaddr = objfile->text_section_offset ();
ff013f42 13936
43039443
JK
13937 while (1)
13938 {
13939 CORE_ADDR range_beginning, range_end;
13940
c8a7a66f 13941 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13942 buffer += addr_size;
c8a7a66f 13943 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13944 buffer += addr_size;
13945 offset += 2 * addr_size;
13946
13947 /* An end of list marker is a pair of zero addresses. */
13948 if (range_beginning == 0 && range_end == 0)
13949 /* Found the end of list entry. */
13950 break;
13951
13952 /* Each base address selection entry is a pair of 2 values.
13953 The first is the largest possible address, the second is
13954 the base address. Check for a base address here. */
13955 if ((range_beginning & mask) == mask)
13956 {
28d2bfb9
AB
13957 /* If we found the largest possible address, then we already
13958 have the base address in range_end. */
13959 base = range_end;
43039443
JK
13960 continue;
13961 }
13962
2b24b6e4 13963 if (!base.has_value ())
43039443
JK
13964 {
13965 /* We have no valid base address for the ranges
13966 data. */
b98664d3 13967 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13968 return 0;
13969 }
13970
9277c30c
UW
13971 if (range_beginning > range_end)
13972 {
13973 /* Inverted range entries are invalid. */
b98664d3 13974 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13975 return 0;
13976 }
13977
13978 /* Empty range entries have no effect. */
13979 if (range_beginning == range_end)
13980 continue;
13981
2b24b6e4
TT
13982 range_beginning += *base;
13983 range_end += *base;
43039443 13984
01093045
DE
13985 /* A not-uncommon case of bad debug info.
13986 Don't pollute the addrmap with bad data. */
13987 if (range_beginning + baseaddr == 0
5e22e966 13988 && !per_objfile->per_bfd->has_section_at_zero)
01093045 13989 {
b98664d3 13990 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13991 " [in module %s]"), objfile_name (objfile));
01093045
DE
13992 continue;
13993 }
13994
5f46c5a5
JK
13995 callback (range_beginning, range_end);
13996 }
13997
13998 return 1;
13999}
14000
14001/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14002 Return 1 if the attributes are present and valid, otherwise, return 0.
14003 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14004
14005static int
14006dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14007 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 14008 dwarf2_psymtab *ranges_pst)
5f46c5a5 14009{
5e22e966 14010 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14011 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14012 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14013 int low_set = 0;
14014 CORE_ADDR low = 0;
14015 CORE_ADDR high = 0;
14016 int retval;
14017
14018 retval = dwarf2_ranges_process (offset, cu,
14019 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14020 {
9277c30c 14021 if (ranges_pst != NULL)
3e29f34a
MR
14022 {
14023 CORE_ADDR lowpc;
14024 CORE_ADDR highpc;
14025
79748972
TT
14026 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14027 range_beginning + baseaddr)
14028 - baseaddr);
14029 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14030 range_end + baseaddr)
14031 - baseaddr);
d320c2b5
TT
14032 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14033 lowpc, highpc - 1, ranges_pst);
3e29f34a 14034 }
ff013f42 14035
43039443
JK
14036 /* FIXME: This is recording everything as a low-high
14037 segment of consecutive addresses. We should have a
14038 data structure for discontiguous block ranges
14039 instead. */
14040 if (! low_set)
14041 {
14042 low = range_beginning;
14043 high = range_end;
14044 low_set = 1;
14045 }
14046 else
14047 {
14048 if (range_beginning < low)
14049 low = range_beginning;
14050 if (range_end > high)
14051 high = range_end;
14052 }
5f46c5a5
JK
14053 });
14054 if (!retval)
14055 return 0;
43039443
JK
14056
14057 if (! low_set)
14058 /* If the first entry is an end-of-list marker, the range
14059 describes an empty scope, i.e. no instructions. */
14060 return 0;
14061
14062 if (low_return)
14063 *low_return = low;
14064 if (high_return)
14065 *high_return = high;
14066 return 1;
14067}
14068
3a2b436a
JK
14069/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14070 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14071 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14072
3a2b436a 14073static enum pc_bounds_kind
af34e669 14074dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14075 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14076 dwarf2_psymtab *pst)
c906108c 14077{
976ca316 14078 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14079 struct attribute *attr;
91da1414 14080 struct attribute *attr_high;
af34e669
DJ
14081 CORE_ADDR low = 0;
14082 CORE_ADDR high = 0;
e385593e 14083 enum pc_bounds_kind ret;
c906108c 14084
91da1414
MW
14085 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14086 if (attr_high)
af34e669 14087 {
e142c38c 14088 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14089 if (attr != nullptr)
91da1414 14090 {
cd6c91b4
TT
14091 low = attr->value_as_address ();
14092 high = attr_high->value_as_address ();
14093 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14094 high += low;
91da1414 14095 }
af34e669
DJ
14096 else
14097 /* Found high w/o low attribute. */
e385593e 14098 return PC_BOUNDS_INVALID;
af34e669
DJ
14099
14100 /* Found consecutive range of addresses. */
3a2b436a 14101 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14102 }
c906108c 14103 else
af34e669 14104 {
e142c38c 14105 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14106 if (attr != NULL)
14107 {
18a8505e 14108 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14109 We take advantage of the fact that DW_AT_ranges does not appear
14110 in DW_TAG_compile_unit of DWO files. */
14111 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14112 unsigned int ranges_offset = (DW_UNSND (attr)
14113 + (need_ranges_base
14114 ? cu->ranges_base
14115 : 0));
2e3cf129 14116
af34e669 14117 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14118 .debug_ranges section. */
2e3cf129 14119 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14120 return PC_BOUNDS_INVALID;
43039443 14121 /* Found discontinuous range of addresses. */
3a2b436a 14122 ret = PC_BOUNDS_RANGES;
af34e669 14123 }
e385593e
JK
14124 else
14125 return PC_BOUNDS_NOT_PRESENT;
af34e669 14126 }
c906108c 14127
48fbe735 14128 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14129 if (high <= low)
e385593e 14130 return PC_BOUNDS_INVALID;
c906108c
SS
14131
14132 /* When using the GNU linker, .gnu.linkonce. sections are used to
14133 eliminate duplicate copies of functions and vtables and such.
14134 The linker will arbitrarily choose one and discard the others.
14135 The AT_*_pc values for such functions refer to local labels in
14136 these sections. If the section from that file was discarded, the
14137 labels are not in the output, so the relocs get a value of 0.
14138 If this is a discarded function, mark the pc bounds as invalid,
14139 so that GDB will ignore it. */
976ca316 14140 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14141 return PC_BOUNDS_INVALID;
c906108c
SS
14142
14143 *lowpc = low;
96408a79
SA
14144 if (highpc)
14145 *highpc = high;
af34e669 14146 return ret;
c906108c
SS
14147}
14148
b084d499
JB
14149/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14150 its low and high PC addresses. Do nothing if these addresses could not
14151 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14152 and HIGHPC to the high address if greater than HIGHPC. */
14153
14154static void
14155dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14156 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14157 struct dwarf2_cu *cu)
14158{
14159 CORE_ADDR low, high;
14160 struct die_info *child = die->child;
14161
e385593e 14162 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14163 {
325fac50
PA
14164 *lowpc = std::min (*lowpc, low);
14165 *highpc = std::max (*highpc, high);
b084d499
JB
14166 }
14167
14168 /* If the language does not allow nested subprograms (either inside
14169 subprograms or lexical blocks), we're done. */
14170 if (cu->language != language_ada)
14171 return;
6e70227d 14172
b084d499
JB
14173 /* Check all the children of the given DIE. If it contains nested
14174 subprograms, then check their pc bounds. Likewise, we need to
14175 check lexical blocks as well, as they may also contain subprogram
14176 definitions. */
14177 while (child && child->tag)
14178 {
14179 if (child->tag == DW_TAG_subprogram
14180 || child->tag == DW_TAG_lexical_block)
14181 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14182 child = child->sibling;
b084d499
JB
14183 }
14184}
14185
fae299cd
DC
14186/* Get the low and high pc's represented by the scope DIE, and store
14187 them in *LOWPC and *HIGHPC. If the correct values can't be
14188 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14189
14190static void
14191get_scope_pc_bounds (struct die_info *die,
14192 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14193 struct dwarf2_cu *cu)
14194{
14195 CORE_ADDR best_low = (CORE_ADDR) -1;
14196 CORE_ADDR best_high = (CORE_ADDR) 0;
14197 CORE_ADDR current_low, current_high;
14198
3a2b436a 14199 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14200 >= PC_BOUNDS_RANGES)
fae299cd
DC
14201 {
14202 best_low = current_low;
14203 best_high = current_high;
14204 }
14205 else
14206 {
14207 struct die_info *child = die->child;
14208
14209 while (child && child->tag)
14210 {
14211 switch (child->tag) {
14212 case DW_TAG_subprogram:
b084d499 14213 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14214 break;
14215 case DW_TAG_namespace:
f55ee35c 14216 case DW_TAG_module:
fae299cd
DC
14217 /* FIXME: carlton/2004-01-16: Should we do this for
14218 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14219 that current GCC's always emit the DIEs corresponding
14220 to definitions of methods of classes as children of a
14221 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14222 the DIEs giving the declarations, which could be
14223 anywhere). But I don't see any reason why the
14224 standards says that they have to be there. */
14225 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14226
14227 if (current_low != ((CORE_ADDR) -1))
14228 {
325fac50
PA
14229 best_low = std::min (best_low, current_low);
14230 best_high = std::max (best_high, current_high);
fae299cd
DC
14231 }
14232 break;
14233 default:
0963b4bd 14234 /* Ignore. */
fae299cd
DC
14235 break;
14236 }
14237
436c571c 14238 child = child->sibling;
fae299cd
DC
14239 }
14240 }
14241
14242 *lowpc = best_low;
14243 *highpc = best_high;
14244}
14245
801e3a5b
JB
14246/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14247 in DIE. */
380bca97 14248
801e3a5b
JB
14249static void
14250dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14251 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14252{
5e22e966 14253 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14254 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14255 struct attribute *attr;
91da1414 14256 struct attribute *attr_high;
801e3a5b 14257
91da1414
MW
14258 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14259 if (attr_high)
801e3a5b 14260 {
801e3a5b 14261 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14262 if (attr != nullptr)
801e3a5b 14263 {
cd6c91b4
TT
14264 CORE_ADDR low = attr->value_as_address ();
14265 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14266
cd6c91b4 14267 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14268 high += low;
9a619af0 14269
3e29f34a
MR
14270 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14271 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14272 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14273 }
14274 }
14275
14276 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14277 if (attr != nullptr)
801e3a5b 14278 {
18a8505e 14279 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14280 We take advantage of the fact that DW_AT_ranges does not appear
14281 in DW_TAG_compile_unit of DWO files. */
14282 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14283
14284 /* The value of the DW_AT_ranges attribute is the offset of the
14285 address range list in the .debug_ranges section. */
ab435259
DE
14286 unsigned long offset = (DW_UNSND (attr)
14287 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14288
2d5f09ec 14289 std::vector<blockrange> blockvec;
5f46c5a5
JK
14290 dwarf2_ranges_process (offset, cu,
14291 [&] (CORE_ADDR start, CORE_ADDR end)
14292 {
58fdfd2c
JK
14293 start += baseaddr;
14294 end += baseaddr;
5f46c5a5
JK
14295 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14296 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14297 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14298 blockvec.emplace_back (start, end);
5f46c5a5 14299 });
2d5f09ec
KB
14300
14301 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14302 }
14303}
14304
685b1105
JK
14305/* Check whether the producer field indicates either of GCC < 4.6, or the
14306 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14307
685b1105
JK
14308static void
14309check_producer (struct dwarf2_cu *cu)
60d5a603 14310{
38360086 14311 int major, minor;
60d5a603
JK
14312
14313 if (cu->producer == NULL)
14314 {
14315 /* For unknown compilers expect their behavior is DWARF version
14316 compliant.
14317
14318 GCC started to support .debug_types sections by -gdwarf-4 since
14319 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14320 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14321 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14322 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14323 }
b1ffba5a 14324 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14325 {
38360086
MW
14326 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14327 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14328 }
5230b05a 14329 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14330 {
14331 cu->producer_is_icc = true;
14332 cu->producer_is_icc_lt_14 = major < 14;
14333 }
c258c396
JD
14334 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14335 cu->producer_is_codewarrior = true;
685b1105
JK
14336 else
14337 {
14338 /* For other non-GCC compilers, expect their behavior is DWARF version
14339 compliant. */
60d5a603
JK
14340 }
14341
9068261f 14342 cu->checked_producer = true;
685b1105 14343}
ba919b58 14344
685b1105
JK
14345/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14346 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14347 during 4.6.0 experimental. */
14348
9068261f 14349static bool
685b1105
JK
14350producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14351{
14352 if (!cu->checked_producer)
14353 check_producer (cu);
14354
14355 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14356}
14357
c258c396
JD
14358
14359/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14360 with incorrect is_stmt attributes. */
14361
14362static bool
14363producer_is_codewarrior (struct dwarf2_cu *cu)
14364{
14365 if (!cu->checked_producer)
14366 check_producer (cu);
14367
14368 return cu->producer_is_codewarrior;
14369}
14370
405feb71 14371/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14372 DW_AT_accessibility. */
14373
14374static enum dwarf_access_attribute
14375dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14376{
14377 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14378 {
14379 /* The default DWARF 2 accessibility for members is public, the default
14380 accessibility for inheritance is private. */
14381
14382 if (die->tag != DW_TAG_inheritance)
14383 return DW_ACCESS_public;
14384 else
14385 return DW_ACCESS_private;
14386 }
14387 else
14388 {
14389 /* DWARF 3+ defines the default accessibility a different way. The same
14390 rules apply now for DW_TAG_inheritance as for the members and it only
14391 depends on the container kind. */
14392
14393 if (die->parent->tag == DW_TAG_class_type)
14394 return DW_ACCESS_private;
14395 else
14396 return DW_ACCESS_public;
14397 }
14398}
14399
74ac6d43
TT
14400/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14401 offset. If the attribute was not found return 0, otherwise return
14402 1. If it was found but could not properly be handled, set *OFFSET
14403 to 0. */
14404
14405static int
14406handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14407 LONGEST *offset)
14408{
14409 struct attribute *attr;
14410
14411 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14412 if (attr != NULL)
14413 {
14414 *offset = 0;
14415
14416 /* Note that we do not check for a section offset first here.
14417 This is because DW_AT_data_member_location is new in DWARF 4,
14418 so if we see it, we can assume that a constant form is really
14419 a constant and not a section offset. */
cd6c91b4 14420 if (attr->form_is_constant ())
0826b30a 14421 *offset = attr->constant_value (0);
cd6c91b4 14422 else if (attr->form_is_section_offset ())
74ac6d43 14423 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14424 else if (attr->form_is_block ())
74ac6d43
TT
14425 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14426 else
14427 dwarf2_complex_location_expr_complaint ();
14428
14429 return 1;
14430 }
14431
14432 return 0;
14433}
14434
7d79de9a
TT
14435/* Look for DW_AT_data_member_location and store the results in FIELD. */
14436
14437static void
14438handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14439 struct field *field)
14440{
14441 struct attribute *attr;
14442
14443 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14444 if (attr != NULL)
14445 {
14446 if (attr->form_is_constant ())
14447 {
14448 LONGEST offset = attr->constant_value (0);
14449 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14450 }
14451 else if (attr->form_is_section_offset ())
14452 dwarf2_complex_location_expr_complaint ();
14453 else if (attr->form_is_block ())
14454 {
14455 bool handled;
14456 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14457 if (handled)
14458 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14459 else
14460 {
5e22e966
SM
14461 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14462 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14463 struct dwarf2_locexpr_baton *dlbaton
14464 = XOBNEW (&objfile->objfile_obstack,
14465 struct dwarf2_locexpr_baton);
14466 dlbaton->data = DW_BLOCK (attr)->data;
14467 dlbaton->size = DW_BLOCK (attr)->size;
14468 /* When using this baton, we want to compute the address
14469 of the field, not the value. This is why
14470 is_reference is set to false here. */
14471 dlbaton->is_reference = false;
5e22e966 14472 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14473 dlbaton->per_cu = cu->per_cu;
14474
14475 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14476 }
14477 }
14478 else
14479 dwarf2_complex_location_expr_complaint ();
14480 }
14481}
14482
c906108c
SS
14483/* Add an aggregate field to the field list. */
14484
14485static void
107d2387 14486dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14487 struct dwarf2_cu *cu)
6e70227d 14488{
5e22e966 14489 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14490 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14491 struct nextfield *new_field;
14492 struct attribute *attr;
14493 struct field *fp;
15d034d0 14494 const char *fieldname = "";
c906108c 14495
7d0ccb61
DJ
14496 if (die->tag == DW_TAG_inheritance)
14497 {
be2daae6
TT
14498 fip->baseclasses.emplace_back ();
14499 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14500 }
14501 else
14502 {
be2daae6
TT
14503 fip->fields.emplace_back ();
14504 new_field = &fip->fields.back ();
7d0ccb61 14505 }
be2daae6 14506
9c6a1327
TT
14507 new_field->offset = die->sect_off;
14508
e142c38c 14509 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14510 if (attr != nullptr)
c906108c 14511 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14512 else
14513 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14514 if (new_field->accessibility != DW_ACCESS_public)
14515 fip->non_public_fields = 1;
60d5a603 14516
e142c38c 14517 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14518 if (attr != nullptr)
c906108c 14519 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14520 else
14521 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14522
14523 fp = &new_field->field;
a9a9bd0f 14524
e142c38c 14525 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14526 {
a9a9bd0f 14527 /* Data member other than a C++ static data member. */
6e70227d 14528
c906108c 14529 /* Get type of field. */
e7c27a73 14530 fp->type = die_type (die, cu);
c906108c 14531
d6a843b5 14532 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14533
c906108c 14534 /* Get bit size of field (zero if none). */
e142c38c 14535 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14536 if (attr != nullptr)
c906108c
SS
14537 {
14538 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14539 }
14540 else
14541 {
14542 FIELD_BITSIZE (*fp) = 0;
14543 }
14544
14545 /* Get bit offset of field. */
7d79de9a 14546 handle_data_member_location (die, cu, fp);
e142c38c 14547 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14548 if (attr != nullptr)
c906108c 14549 {
d5a22e77 14550 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14551 {
14552 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14553 additional bit offset from the MSB of the containing
14554 anonymous object to the MSB of the field. We don't
14555 have to do anything special since we don't need to
14556 know the size of the anonymous object. */
f41f5e61 14557 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14558 }
14559 else
14560 {
14561 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14562 MSB of the anonymous object, subtract off the number of
14563 bits from the MSB of the field to the MSB of the
14564 object, and then subtract off the number of bits of
14565 the field itself. The result is the bit offset of
14566 the LSB of the field. */
c906108c
SS
14567 int anonymous_size;
14568 int bit_offset = DW_UNSND (attr);
14569
e142c38c 14570 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14571 if (attr != nullptr)
c906108c
SS
14572 {
14573 /* The size of the anonymous object containing
14574 the bit field is explicit, so use the
14575 indicated size (in bytes). */
14576 anonymous_size = DW_UNSND (attr);
14577 }
14578 else
14579 {
14580 /* The size of the anonymous object containing
14581 the bit field must be inferred from the type
14582 attribute of the data member containing the
14583 bit field. */
14584 anonymous_size = TYPE_LENGTH (fp->type);
14585 }
f41f5e61
PA
14586 SET_FIELD_BITPOS (*fp,
14587 (FIELD_BITPOS (*fp)
14588 + anonymous_size * bits_per_byte
14589 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14590 }
14591 }
da5b30da
AA
14592 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14593 if (attr != NULL)
14594 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14595 + attr->constant_value (0)));
c906108c
SS
14596
14597 /* Get name of field. */
39cbfefa
DJ
14598 fieldname = dwarf2_name (die, cu);
14599 if (fieldname == NULL)
14600 fieldname = "";
d8151005
DJ
14601
14602 /* The name is already allocated along with this objfile, so we don't
14603 need to duplicate it for the type. */
14604 fp->name = fieldname;
c906108c
SS
14605
14606 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14607 pointer or virtual base class pointer) to private. */
e142c38c 14608 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14609 {
d48cc9dd 14610 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14611 new_field->accessibility = DW_ACCESS_private;
14612 fip->non_public_fields = 1;
14613 }
14614 }
a9a9bd0f 14615 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14616 {
a9a9bd0f
DC
14617 /* C++ static member. */
14618
14619 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14620 is a declaration, but all versions of G++ as of this writing
14621 (so through at least 3.2.1) incorrectly generate
14622 DW_TAG_variable tags. */
6e70227d 14623
ff355380 14624 const char *physname;
c906108c 14625
a9a9bd0f 14626 /* Get name of field. */
39cbfefa
DJ
14627 fieldname = dwarf2_name (die, cu);
14628 if (fieldname == NULL)
c906108c
SS
14629 return;
14630
254e6b9e 14631 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14632 if (attr
14633 /* Only create a symbol if this is an external value.
14634 new_symbol checks this and puts the value in the global symbol
14635 table, which we want. If it is not external, new_symbol
14636 will try to put the value in cu->list_in_scope which is wrong. */
14637 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14638 {
14639 /* A static const member, not much different than an enum as far as
14640 we're concerned, except that we can support more types. */
14641 new_symbol (die, NULL, cu);
14642 }
14643
2df3850c 14644 /* Get physical name. */
ff355380 14645 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14646
d8151005
DJ
14647 /* The name is already allocated along with this objfile, so we don't
14648 need to duplicate it for the type. */
14649 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14650 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14651 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14652 }
14653 else if (die->tag == DW_TAG_inheritance)
14654 {
74ac6d43 14655 /* C++ base class field. */
7d79de9a 14656 handle_data_member_location (die, cu, fp);
c906108c 14657 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14658 FIELD_TYPE (*fp) = die_type (die, cu);
7d93a1e0 14659 FIELD_NAME (*fp) = fp->type->name ();
c906108c 14660 }
2ddeaf8a
TT
14661 else
14662 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14663}
14664
883fd55a
KS
14665/* Can the type given by DIE define another type? */
14666
14667static bool
14668type_can_define_types (const struct die_info *die)
14669{
14670 switch (die->tag)
14671 {
14672 case DW_TAG_typedef:
14673 case DW_TAG_class_type:
14674 case DW_TAG_structure_type:
14675 case DW_TAG_union_type:
14676 case DW_TAG_enumeration_type:
14677 return true;
14678
14679 default:
14680 return false;
14681 }
14682}
14683
14684/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14685
14686static void
883fd55a
KS
14687dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14688 struct dwarf2_cu *cu)
6e70227d 14689{
be2daae6
TT
14690 struct decl_field fp;
14691 memset (&fp, 0, sizeof (fp));
98751a41 14692
883fd55a 14693 gdb_assert (type_can_define_types (die));
98751a41 14694
883fd55a 14695 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14696 fp.name = dwarf2_name (die, cu);
14697 fp.type = read_type_die (die, cu);
98751a41 14698
c191a687
KS
14699 /* Save accessibility. */
14700 enum dwarf_access_attribute accessibility;
14701 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14702 if (attr != NULL)
14703 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14704 else
14705 accessibility = dwarf2_default_access_attribute (die, cu);
14706 switch (accessibility)
14707 {
14708 case DW_ACCESS_public:
14709 /* The assumed value if neither private nor protected. */
14710 break;
14711 case DW_ACCESS_private:
be2daae6 14712 fp.is_private = 1;
c191a687
KS
14713 break;
14714 case DW_ACCESS_protected:
be2daae6 14715 fp.is_protected = 1;
c191a687
KS
14716 break;
14717 default:
b98664d3 14718 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14719 }
14720
883fd55a 14721 if (die->tag == DW_TAG_typedef)
be2daae6 14722 fip->typedef_field_list.push_back (fp);
883fd55a 14723 else
be2daae6 14724 fip->nested_types_list.push_back (fp);
98751a41
JK
14725}
14726
9c6a1327
TT
14727/* A convenience typedef that's used when finding the discriminant
14728 field for a variant part. */
1b95cdb7
SM
14729typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14730 offset_map_type;
9c6a1327
TT
14731
14732/* Compute the discriminant range for a given variant. OBSTACK is
14733 where the results will be stored. VARIANT is the variant to
14734 process. IS_UNSIGNED indicates whether the discriminant is signed
14735 or unsigned. */
14736
14737static const gdb::array_view<discriminant_range>
14738convert_variant_range (struct obstack *obstack, const variant_field &variant,
14739 bool is_unsigned)
14740{
14741 std::vector<discriminant_range> ranges;
14742
14743 if (variant.default_branch)
14744 return {};
14745
14746 if (variant.discr_list_data == nullptr)
14747 {
14748 discriminant_range r
14749 = {variant.discriminant_value, variant.discriminant_value};
14750 ranges.push_back (r);
14751 }
14752 else
14753 {
14754 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14755 variant.discr_list_data->size);
14756 while (!data.empty ())
14757 {
14758 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14759 {
14760 complaint (_("invalid discriminant marker: %d"), data[0]);
14761 break;
14762 }
14763 bool is_range = data[0] == DW_DSC_range;
14764 data = data.slice (1);
14765
14766 ULONGEST low, high;
14767 unsigned int bytes_read;
14768
14769 if (data.empty ())
14770 {
14771 complaint (_("DW_AT_discr_list missing low value"));
14772 break;
14773 }
14774 if (is_unsigned)
14775 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14776 else
14777 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14778 &bytes_read);
14779 data = data.slice (bytes_read);
14780
14781 if (is_range)
14782 {
14783 if (data.empty ())
14784 {
14785 complaint (_("DW_AT_discr_list missing high value"));
14786 break;
14787 }
14788 if (is_unsigned)
14789 high = read_unsigned_leb128 (nullptr, data.data (),
14790 &bytes_read);
14791 else
14792 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14793 &bytes_read);
14794 data = data.slice (bytes_read);
14795 }
14796 else
14797 high = low;
14798
14799 ranges.push_back ({ low, high });
14800 }
14801 }
14802
14803 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14804 ranges.size ());
14805 std::copy (ranges.begin (), ranges.end (), result);
14806 return gdb::array_view<discriminant_range> (result, ranges.size ());
14807}
14808
14809static const gdb::array_view<variant_part> create_variant_parts
14810 (struct obstack *obstack,
14811 const offset_map_type &offset_map,
14812 struct field_info *fi,
14813 const std::vector<variant_part_builder> &variant_parts);
14814
14815/* Fill in a "struct variant" for a given variant field. RESULT is
14816 the variant to fill in. OBSTACK is where any needed allocations
14817 will be done. OFFSET_MAP holds the mapping from section offsets to
14818 fields for the type. FI describes the fields of the type we're
14819 processing. FIELD is the variant field we're converting. */
14820
14821static void
14822create_one_variant (variant &result, struct obstack *obstack,
14823 const offset_map_type &offset_map,
14824 struct field_info *fi, const variant_field &field)
14825{
14826 result.discriminants = convert_variant_range (obstack, field, false);
14827 result.first_field = field.first_field + fi->baseclasses.size ();
14828 result.last_field = field.last_field + fi->baseclasses.size ();
14829 result.parts = create_variant_parts (obstack, offset_map, fi,
14830 field.variant_parts);
14831}
14832
14833/* Fill in a "struct variant_part" for a given variant part. RESULT
14834 is the variant part to fill in. OBSTACK is where any needed
14835 allocations will be done. OFFSET_MAP holds the mapping from
14836 section offsets to fields for the type. FI describes the fields of
14837 the type we're processing. BUILDER is the variant part to be
14838 converted. */
14839
14840static void
14841create_one_variant_part (variant_part &result,
14842 struct obstack *obstack,
14843 const offset_map_type &offset_map,
14844 struct field_info *fi,
14845 const variant_part_builder &builder)
14846{
14847 auto iter = offset_map.find (builder.discriminant_offset);
14848 if (iter == offset_map.end ())
14849 {
14850 result.discriminant_index = -1;
14851 /* Doesn't matter. */
14852 result.is_unsigned = false;
14853 }
14854 else
14855 {
14856 result.discriminant_index = iter->second;
14857 result.is_unsigned
14858 = TYPE_UNSIGNED (FIELD_TYPE
14859 (fi->fields[result.discriminant_index].field));
14860 }
14861
14862 size_t n = builder.variants.size ();
14863 variant *output = new (obstack) variant[n];
14864 for (size_t i = 0; i < n; ++i)
14865 create_one_variant (output[i], obstack, offset_map, fi,
14866 builder.variants[i]);
14867
14868 result.variants = gdb::array_view<variant> (output, n);
14869}
14870
14871/* Create a vector of variant parts that can be attached to a type.
14872 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14873 holds the mapping from section offsets to fields for the type. FI
14874 describes the fields of the type we're processing. VARIANT_PARTS
14875 is the vector to convert. */
14876
14877static const gdb::array_view<variant_part>
14878create_variant_parts (struct obstack *obstack,
14879 const offset_map_type &offset_map,
14880 struct field_info *fi,
14881 const std::vector<variant_part_builder> &variant_parts)
14882{
14883 if (variant_parts.empty ())
14884 return {};
14885
14886 size_t n = variant_parts.size ();
14887 variant_part *result = new (obstack) variant_part[n];
14888 for (size_t i = 0; i < n; ++i)
14889 create_one_variant_part (result[i], obstack, offset_map, fi,
14890 variant_parts[i]);
14891
14892 return gdb::array_view<variant_part> (result, n);
14893}
14894
14895/* Compute the variant part vector for FIP, attaching it to TYPE when
14896 done. */
14897
14898static void
14899add_variant_property (struct field_info *fip, struct type *type,
14900 struct dwarf2_cu *cu)
14901{
14902 /* Map section offsets of fields to their field index. Note the
14903 field index here does not take the number of baseclasses into
14904 account. */
14905 offset_map_type offset_map;
14906 for (int i = 0; i < fip->fields.size (); ++i)
14907 offset_map[fip->fields[i].offset] = i;
14908
5e22e966 14909 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
14910 gdb::array_view<variant_part> parts
14911 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14912 fip->variant_parts);
14913
14914 struct dynamic_prop prop;
14915 prop.kind = PROP_VARIANT_PARTS;
14916 prop.data.variant_parts
14917 = ((gdb::array_view<variant_part> *)
14918 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14919
5c54719c 14920 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14921}
14922
c906108c
SS
14923/* Create the vector of fields, and attach it to the type. */
14924
14925static void
fba45db2 14926dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14927 struct dwarf2_cu *cu)
c906108c 14928{
317f7127 14929 int nfields = fip->nfields ();
c906108c
SS
14930
14931 /* Record the field count, allocate space for the array of fields,
14932 and create blank accessibility bitfields if necessary. */
5e33d5f4 14933 type->set_num_fields (nfields);
3cabb6b0
SM
14934 type->set_fields
14935 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14936
b4ba55a1 14937 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14938 {
14939 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14940
14941 TYPE_FIELD_PRIVATE_BITS (type) =
14942 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14943 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14944
14945 TYPE_FIELD_PROTECTED_BITS (type) =
14946 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14947 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14948
774b6a14
TT
14949 TYPE_FIELD_IGNORE_BITS (type) =
14950 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14951 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14952 }
14953
14954 /* If the type has baseclasses, allocate and clear a bit vector for
14955 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14956 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14957 {
be2daae6 14958 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14959 unsigned char *pointer;
c906108c
SS
14960
14961 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14962 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14963 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14964 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14965 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14966 }
14967
9c6a1327
TT
14968 if (!fip->variant_parts.empty ())
14969 add_variant_property (fip, type, cu);
2ddeaf8a 14970
be2daae6
TT
14971 /* Copy the saved-up fields into the field vector. */
14972 for (int i = 0; i < nfields; ++i)
c906108c 14973 {
be2daae6
TT
14974 struct nextfield &field
14975 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14976 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14977
ceacbf6e 14978 type->field (i) = field.field;
be2daae6 14979 switch (field.accessibility)
c906108c 14980 {
c5aa993b 14981 case DW_ACCESS_private:
b4ba55a1 14982 if (cu->language != language_ada)
be2daae6 14983 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14984 break;
c906108c 14985
c5aa993b 14986 case DW_ACCESS_protected:
b4ba55a1 14987 if (cu->language != language_ada)
be2daae6 14988 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14989 break;
c906108c 14990
c5aa993b
JM
14991 case DW_ACCESS_public:
14992 break;
c906108c 14993
c5aa993b
JM
14994 default:
14995 /* Unknown accessibility. Complain and treat it as public. */
14996 {
b98664d3 14997 complaint (_("unsupported accessibility %d"),
be2daae6 14998 field.accessibility);
c5aa993b
JM
14999 }
15000 break;
c906108c 15001 }
be2daae6 15002 if (i < fip->baseclasses.size ())
c906108c 15003 {
be2daae6 15004 switch (field.virtuality)
c906108c 15005 {
c5aa993b
JM
15006 case DW_VIRTUALITY_virtual:
15007 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15008 if (cu->language == language_ada)
a73c6dcd 15009 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15010 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15011 break;
c906108c
SS
15012 }
15013 }
c906108c
SS
15014 }
15015}
15016
7d27a96d
TT
15017/* Return true if this member function is a constructor, false
15018 otherwise. */
15019
15020static int
15021dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15022{
15023 const char *fieldname;
fe978cb0 15024 const char *type_name;
7d27a96d
TT
15025 int len;
15026
15027 if (die->parent == NULL)
15028 return 0;
15029
15030 if (die->parent->tag != DW_TAG_structure_type
15031 && die->parent->tag != DW_TAG_union_type
15032 && die->parent->tag != DW_TAG_class_type)
15033 return 0;
15034
15035 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15036 type_name = dwarf2_name (die->parent, cu);
15037 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15038 return 0;
15039
15040 len = strlen (fieldname);
fe978cb0
PA
15041 return (strncmp (fieldname, type_name, len) == 0
15042 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15043}
15044
e35000a7
TBA
15045/* Check if the given VALUE is a recognized enum
15046 dwarf_defaulted_attribute constant according to DWARF5 spec,
15047 Table 7.24. */
15048
15049static bool
15050is_valid_DW_AT_defaulted (ULONGEST value)
15051{
15052 switch (value)
15053 {
15054 case DW_DEFAULTED_no:
15055 case DW_DEFAULTED_in_class:
15056 case DW_DEFAULTED_out_of_class:
15057 return true;
15058 }
15059
3142e908 15060 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15061 return false;
15062}
15063
c906108c
SS
15064/* Add a member function to the proper fieldlist. */
15065
15066static void
107d2387 15067dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15068 struct type *type, struct dwarf2_cu *cu)
c906108c 15069{
5e22e966 15070 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15071 struct attribute *attr;
c906108c 15072 int i;
be2daae6 15073 struct fnfieldlist *flp = nullptr;
c906108c 15074 struct fn_field *fnp;
15d034d0 15075 const char *fieldname;
f792889a 15076 struct type *this_type;
60d5a603 15077 enum dwarf_access_attribute accessibility;
c906108c 15078
b4ba55a1 15079 if (cu->language == language_ada)
a73c6dcd 15080 error (_("unexpected member function in Ada type"));
b4ba55a1 15081
2df3850c 15082 /* Get name of member function. */
39cbfefa
DJ
15083 fieldname = dwarf2_name (die, cu);
15084 if (fieldname == NULL)
2df3850c 15085 return;
c906108c 15086
c906108c 15087 /* Look up member function name in fieldlist. */
be2daae6 15088 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15089 {
27bfe10e 15090 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15091 {
15092 flp = &fip->fnfieldlists[i];
15093 break;
15094 }
c906108c
SS
15095 }
15096
be2daae6
TT
15097 /* Create a new fnfieldlist if necessary. */
15098 if (flp == nullptr)
c906108c 15099 {
be2daae6
TT
15100 fip->fnfieldlists.emplace_back ();
15101 flp = &fip->fnfieldlists.back ();
c906108c 15102 flp->name = fieldname;
be2daae6 15103 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15104 }
15105
be2daae6
TT
15106 /* Create a new member function field and add it to the vector of
15107 fnfieldlists. */
15108 flp->fnfields.emplace_back ();
15109 fnp = &flp->fnfields.back ();
3da10d80
KS
15110
15111 /* Delay processing of the physname until later. */
9c37b5ae 15112 if (cu->language == language_cplus)
be2daae6
TT
15113 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15114 die, cu);
3da10d80
KS
15115 else
15116 {
1d06ead6 15117 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15118 fnp->physname = physname ? physname : "";
15119 }
15120
c906108c 15121 fnp->type = alloc_type (objfile);
f792889a 15122 this_type = read_type_die (die, cu);
78134374 15123 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15124 {
1f704f76 15125 int nparams = this_type->num_fields ();
c906108c 15126
f792889a 15127 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15128 of the method itself (TYPE_CODE_METHOD). */
15129 smash_to_method_type (fnp->type, type,
f792889a 15130 TYPE_TARGET_TYPE (this_type),
80fc5e77 15131 this_type->fields (),
1f704f76 15132 this_type->num_fields (),
f792889a 15133 TYPE_VARARGS (this_type));
c906108c
SS
15134
15135 /* Handle static member functions.
c5aa993b 15136 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15137 member functions. G++ helps GDB by marking the first
15138 parameter for non-static member functions (which is the this
15139 pointer) as artificial. We obtain this information from
15140 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15141 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15142 fnp->voffset = VOFFSET_STATIC;
15143 }
15144 else
b98664d3 15145 complaint (_("member function type missing for '%s'"),
3da10d80 15146 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15147
15148 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15149 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15150 fnp->fcontext = die_containing_type (die, cu);
c906108c 15151
3e43a32a
MS
15152 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15153 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15154
15155 /* Get accessibility. */
e142c38c 15156 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15157 if (attr != nullptr)
aead7601 15158 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15159 else
15160 accessibility = dwarf2_default_access_attribute (die, cu);
15161 switch (accessibility)
c906108c 15162 {
60d5a603
JK
15163 case DW_ACCESS_private:
15164 fnp->is_private = 1;
15165 break;
15166 case DW_ACCESS_protected:
15167 fnp->is_protected = 1;
15168 break;
c906108c
SS
15169 }
15170
b02dede2 15171 /* Check for artificial methods. */
e142c38c 15172 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15173 if (attr && DW_UNSND (attr) != 0)
15174 fnp->is_artificial = 1;
15175
e35000a7
TBA
15176 /* Check for defaulted methods. */
15177 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15178 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15179 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15180
15181 /* Check for deleted methods. */
15182 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15183 if (attr != nullptr && DW_UNSND (attr) != 0)
15184 fnp->is_deleted = 1;
15185
7d27a96d
TT
15186 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15187
0d564a31 15188 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15189 function. For older versions of GCC, this is an offset in the
15190 appropriate virtual table, as specified by DW_AT_containing_type.
15191 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15192 to the object address. */
15193
e142c38c 15194 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15195 if (attr != nullptr)
8e19ed76 15196 {
4fc6c0d5 15197 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15198 {
aec5aa8b
TT
15199 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15200 {
15201 /* Old-style GCC. */
15202 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15203 }
15204 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15205 || (DW_BLOCK (attr)->size > 1
15206 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15207 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15208 {
aec5aa8b
TT
15209 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15210 if ((fnp->voffset % cu->header.addr_size) != 0)
15211 dwarf2_complex_location_expr_complaint ();
15212 else
15213 fnp->voffset /= cu->header.addr_size;
15214 fnp->voffset += 2;
15215 }
15216 else
15217 dwarf2_complex_location_expr_complaint ();
15218
15219 if (!fnp->fcontext)
7e993ebf
KS
15220 {
15221 /* If there is no `this' field and no DW_AT_containing_type,
15222 we cannot actually find a base class context for the
15223 vtable! */
1f704f76 15224 if (this_type->num_fields () == 0
7e993ebf
KS
15225 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15226 {
b98664d3 15227 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15228 "function \"%s\" (offset %s)"),
15229 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15230 }
15231 else
15232 {
15233 fnp->fcontext
15234 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15235 }
15236 }
aec5aa8b 15237 }
cd6c91b4 15238 else if (attr->form_is_section_offset ())
8e19ed76 15239 {
4d3c2250 15240 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15241 }
15242 else
15243 {
4d3c2250
KB
15244 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15245 fieldname);
8e19ed76 15246 }
0d564a31 15247 }
d48cc9dd
DJ
15248 else
15249 {
15250 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15251 if (attr && DW_UNSND (attr))
15252 {
15253 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15254 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15255 "but the vtable offset is not specified"),
9d8780f0 15256 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15257 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15258 TYPE_CPLUS_DYNAMIC (type) = 1;
15259 }
15260 }
c906108c
SS
15261}
15262
15263/* Create the vector of member function fields, and attach it to the type. */
15264
15265static void
fba45db2 15266dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15267 struct dwarf2_cu *cu)
c906108c 15268{
b4ba55a1 15269 if (cu->language == language_ada)
a73c6dcd 15270 error (_("unexpected member functions in Ada type"));
b4ba55a1 15271
c906108c
SS
15272 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15273 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15274 TYPE_ALLOC (type,
15275 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15276
be2daae6 15277 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15278 {
be2daae6 15279 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15280 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15281
be2daae6
TT
15282 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15283 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15284 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15285 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15286
15287 for (int k = 0; k < nf.fnfields.size (); ++k)
15288 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15289 }
15290
be2daae6 15291 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15292}
15293
1168df01
JB
15294/* Returns non-zero if NAME is the name of a vtable member in CU's
15295 language, zero otherwise. */
15296static int
15297is_vtable_name (const char *name, struct dwarf2_cu *cu)
15298{
15299 static const char vptr[] = "_vptr";
15300
9c37b5ae
TT
15301 /* Look for the C++ form of the vtable. */
15302 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15303 return 1;
15304
15305 return 0;
15306}
15307
c0dd20ea 15308/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15309 functions, with the ABI-specified layout. If TYPE describes
15310 such a structure, smash it into a member function type.
61049d3b
DJ
15311
15312 GCC shouldn't do this; it should just output pointer to member DIEs.
15313 This is GCC PR debug/28767. */
c0dd20ea 15314
0b92b5bb
TT
15315static void
15316quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15317{
09e2d7c7 15318 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15319
15320 /* Check for a structure with no name and two children. */
1f704f76 15321 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15322 return;
c0dd20ea
DJ
15323
15324 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15325 if (TYPE_FIELD_NAME (type, 0) == NULL
15326 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15327 || TYPE_FIELD_NAME (type, 1) == NULL
15328 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15329 return;
c0dd20ea
DJ
15330
15331 /* Find the type of the method. */
0b92b5bb 15332 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea 15333 if (pfn_type == NULL
78134374
SM
15334 || pfn_type->code () != TYPE_CODE_PTR
15335 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15336 return;
c0dd20ea
DJ
15337
15338 /* Look for the "this" argument. */
15339 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15340 if (pfn_type->num_fields () == 0
0b92b5bb 15341 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
78134374 15342 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
0b92b5bb 15343 return;
c0dd20ea 15344
09e2d7c7 15345 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15346 new_type = alloc_type (objfile);
09e2d7c7 15347 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15348 pfn_type->fields (), pfn_type->num_fields (),
c0dd20ea 15349 TYPE_VARARGS (pfn_type));
0b92b5bb 15350 smash_to_methodptr_type (type, new_type);
c0dd20ea 15351}
1168df01 15352
2b4424c3
TT
15353/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15354 appropriate error checking and issuing complaints if there is a
15355 problem. */
15356
15357static ULONGEST
15358get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15359{
15360 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15361
15362 if (attr == nullptr)
15363 return 0;
15364
cd6c91b4 15365 if (!attr->form_is_constant ())
2b4424c3 15366 {
b98664d3 15367 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15368 " - DIE at %s [in module %s]"),
15369 sect_offset_str (die->sect_off),
5e22e966 15370 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15371 return 0;
15372 }
15373
15374 ULONGEST align;
15375 if (attr->form == DW_FORM_sdata)
15376 {
15377 LONGEST val = DW_SND (attr);
15378 if (val < 0)
15379 {
b98664d3 15380 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15381 " - DIE at %s [in module %s]"),
15382 sect_offset_str (die->sect_off),
5e22e966 15383 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15384 return 0;
15385 }
15386 align = val;
15387 }
15388 else
15389 align = DW_UNSND (attr);
15390
15391 if (align == 0)
15392 {
b98664d3 15393 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15394 " - DIE at %s [in module %s]"),
15395 sect_offset_str (die->sect_off),
5e22e966 15396 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15397 return 0;
15398 }
15399 if ((align & (align - 1)) != 0)
15400 {
b98664d3 15401 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15402 " - DIE at %s [in module %s]"),
15403 sect_offset_str (die->sect_off),
5e22e966 15404 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15405 return 0;
15406 }
15407
15408 return align;
15409}
15410
15411/* If the DIE has a DW_AT_alignment attribute, use its value to set
15412 the alignment for TYPE. */
15413
15414static void
15415maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15416 struct type *type)
15417{
15418 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15419 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15420 " - DIE at %s [in module %s]"),
15421 sect_offset_str (die->sect_off),
5e22e966 15422 objfile_name (cu->per_objfile->objfile));
2b4424c3 15423}
685b1105 15424
e35000a7
TBA
15425/* Check if the given VALUE is a valid enum dwarf_calling_convention
15426 constant for a type, according to DWARF5 spec, Table 5.5. */
15427
15428static bool
15429is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15430{
15431 switch (value)
15432 {
15433 case DW_CC_normal:
15434 case DW_CC_pass_by_reference:
15435 case DW_CC_pass_by_value:
15436 return true;
15437
15438 default:
15439 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15440 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15441 return false;
15442 }
15443}
15444
d0922fcf
TBA
15445/* Check if the given VALUE is a valid enum dwarf_calling_convention
15446 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15447 also according to GNU-specific values (see include/dwarf2.h). */
15448
15449static bool
15450is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15451{
15452 switch (value)
15453 {
15454 case DW_CC_normal:
15455 case DW_CC_program:
15456 case DW_CC_nocall:
15457 return true;
15458
15459 case DW_CC_GNU_renesas_sh:
15460 case DW_CC_GNU_borland_fastcall_i386:
15461 case DW_CC_GDB_IBM_OpenCL:
15462 return true;
15463
15464 default:
15465 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15466 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15467 return false;
15468 }
15469}
15470
c906108c 15471/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15472 (definition) to create a type for the structure or union. Fill in
15473 the type's name and general properties; the members will not be
83655187
DE
15474 processed until process_structure_scope. A symbol table entry for
15475 the type will also not be done until process_structure_scope (assuming
15476 the type has a name).
c906108c 15477
c767944b
DJ
15478 NOTE: we need to call these functions regardless of whether or not the
15479 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15480 structure or union. This gets the type entered into our set of
83655187 15481 user defined types. */
c906108c 15482
f792889a 15483static struct type *
134d01f1 15484read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15485{
5e22e966 15486 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15487 struct type *type;
15488 struct attribute *attr;
15d034d0 15489 const char *name;
c906108c 15490
348e048f
DE
15491 /* If the definition of this type lives in .debug_types, read that type.
15492 Don't follow DW_AT_specification though, that will take us back up
15493 the chain and we want to go down. */
052c8bb8 15494 attr = die->attr (DW_AT_signature);
435d3d88 15495 if (attr != nullptr)
348e048f 15496 {
ac9ec31b 15497 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15498
ac9ec31b 15499 /* The type's CU may not be the same as CU.
02142a6c 15500 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15501 return set_die_type (die, type, cu);
15502 }
15503
c0dd20ea 15504 type = alloc_type (objfile);
c906108c 15505 INIT_CPLUS_SPECIFIC (type);
93311388 15506
39cbfefa
DJ
15507 name = dwarf2_name (die, cu);
15508 if (name != NULL)
c906108c 15509 {
987504bb 15510 if (cu->language == language_cplus
c44af4eb
TT
15511 || cu->language == language_d
15512 || cu->language == language_rust)
63d06c5c 15513 {
15d034d0 15514 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15515
15516 /* dwarf2_full_name might have already finished building the DIE's
15517 type. If so, there is no need to continue. */
15518 if (get_die_type (die, cu) != NULL)
15519 return get_die_type (die, cu);
15520
d0e39ea2 15521 type->set_name (full_name);
63d06c5c
DC
15522 }
15523 else
15524 {
d8151005
DJ
15525 /* The name is already allocated along with this objfile, so
15526 we don't need to duplicate it for the type. */
d0e39ea2 15527 type->set_name (name);
63d06c5c 15528 }
c906108c
SS
15529 }
15530
15531 if (die->tag == DW_TAG_structure_type)
15532 {
67607e24 15533 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15534 }
15535 else if (die->tag == DW_TAG_union_type)
15536 {
67607e24 15537 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15538 }
15539 else
15540 {
67607e24 15541 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15542 }
15543
0cc2414c
TT
15544 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15545 TYPE_DECLARED_CLASS (type) = 1;
15546
e35000a7
TBA
15547 /* Store the calling convention in the type if it's available in
15548 the die. Otherwise the calling convention remains set to
15549 the default value DW_CC_normal. */
15550 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15551 if (attr != nullptr
15552 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15553 {
15554 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15555 TYPE_CPLUS_CALLING_CONVENTION (type)
15556 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15557 }
15558
e142c38c 15559 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15560 if (attr != nullptr)
c906108c 15561 {
cd6c91b4 15562 if (attr->form_is_constant ())
155bfbd3
JB
15563 TYPE_LENGTH (type) = DW_UNSND (attr);
15564 else
15565 {
f8e89861 15566 struct dynamic_prop prop;
293e7e51 15567 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15568 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
15569 TYPE_LENGTH (type) = 0;
15570 }
c906108c
SS
15571 }
15572 else
15573 {
15574 TYPE_LENGTH (type) = 0;
15575 }
15576
2b4424c3
TT
15577 maybe_set_alignment (cu, die, type);
15578
5230b05a 15579 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15580 {
5230b05a
WT
15581 /* ICC<14 does not output the required DW_AT_declaration on
15582 incomplete types, but gives them a size of zero. */
422b1cb0 15583 TYPE_STUB (type) = 1;
685b1105
JK
15584 }
15585 else
15586 TYPE_STUB_SUPPORTED (type) = 1;
15587
dc718098 15588 if (die_is_declaration (die, cu))
876cecd0 15589 TYPE_STUB (type) = 1;
a6c727b2
DJ
15590 else if (attr == NULL && die->child == NULL
15591 && producer_is_realview (cu->producer))
15592 /* RealView does not output the required DW_AT_declaration
15593 on incomplete types. */
15594 TYPE_STUB (type) = 1;
dc718098 15595
c906108c
SS
15596 /* We need to add the type field to the die immediately so we don't
15597 infinitely recurse when dealing with pointers to the structure
0963b4bd 15598 type within the structure itself. */
1c379e20 15599 set_die_type (die, type, cu);
c906108c 15600
7e314c57
JK
15601 /* set_die_type should be already done. */
15602 set_descriptive_type (type, die, cu);
15603
c767944b
DJ
15604 return type;
15605}
15606
9c6a1327
TT
15607static void handle_struct_member_die
15608 (struct die_info *child_die,
15609 struct type *type,
15610 struct field_info *fi,
15611 std::vector<struct symbol *> *template_args,
15612 struct dwarf2_cu *cu);
15613
15614/* A helper for handle_struct_member_die that handles
15615 DW_TAG_variant_part. */
15616
15617static void
15618handle_variant_part (struct die_info *die, struct type *type,
15619 struct field_info *fi,
15620 std::vector<struct symbol *> *template_args,
15621 struct dwarf2_cu *cu)
15622{
15623 variant_part_builder *new_part;
15624 if (fi->current_variant_part == nullptr)
15625 {
15626 fi->variant_parts.emplace_back ();
15627 new_part = &fi->variant_parts.back ();
15628 }
15629 else if (!fi->current_variant_part->processing_variant)
15630 {
15631 complaint (_("nested DW_TAG_variant_part seen "
15632 "- DIE at %s [in module %s]"),
15633 sect_offset_str (die->sect_off),
5e22e966 15634 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15635 return;
15636 }
15637 else
15638 {
15639 variant_field &current = fi->current_variant_part->variants.back ();
15640 current.variant_parts.emplace_back ();
15641 new_part = &current.variant_parts.back ();
15642 }
15643
15644 /* When we recurse, we want callees to add to this new variant
15645 part. */
15646 scoped_restore save_current_variant_part
15647 = make_scoped_restore (&fi->current_variant_part, new_part);
15648
15649 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15650 if (discr == NULL)
15651 {
15652 /* It's a univariant form, an extension we support. */
15653 }
15654 else if (discr->form_is_ref ())
15655 {
15656 struct dwarf2_cu *target_cu = cu;
15657 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15658
15659 new_part->discriminant_offset = target_die->sect_off;
15660 }
15661 else
15662 {
15663 complaint (_("DW_AT_discr does not have DIE reference form"
15664 " - DIE at %s [in module %s]"),
15665 sect_offset_str (die->sect_off),
5e22e966 15666 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15667 }
15668
15669 for (die_info *child_die = die->child;
15670 child_die != NULL;
15671 child_die = child_die->sibling)
15672 handle_struct_member_die (child_die, type, fi, template_args, cu);
15673}
15674
15675/* A helper for handle_struct_member_die that handles
15676 DW_TAG_variant. */
15677
15678static void
15679handle_variant (struct die_info *die, struct type *type,
15680 struct field_info *fi,
15681 std::vector<struct symbol *> *template_args,
15682 struct dwarf2_cu *cu)
15683{
15684 if (fi->current_variant_part == nullptr)
15685 {
15686 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15687 "- DIE at %s [in module %s]"),
15688 sect_offset_str (die->sect_off),
5e22e966 15689 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15690 return;
15691 }
15692 if (fi->current_variant_part->processing_variant)
15693 {
15694 complaint (_("nested DW_TAG_variant seen "
15695 "- DIE at %s [in module %s]"),
15696 sect_offset_str (die->sect_off),
5e22e966 15697 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15698 return;
15699 }
15700
15701 scoped_restore save_processing_variant
15702 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15703 true);
15704
15705 fi->current_variant_part->variants.emplace_back ();
15706 variant_field &variant = fi->current_variant_part->variants.back ();
15707 variant.first_field = fi->fields.size ();
15708
15709 /* In a variant we want to get the discriminant and also add a
15710 field for our sole member child. */
15711 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15712 if (discr == nullptr)
15713 {
15714 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15715 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15716 variant.default_branch = true;
15717 else
15718 variant.discr_list_data = DW_BLOCK (discr);
15719 }
15720 else
15721 variant.discriminant_value = DW_UNSND (discr);
15722
15723 for (die_info *variant_child = die->child;
15724 variant_child != NULL;
15725 variant_child = variant_child->sibling)
15726 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15727
15728 variant.last_field = fi->fields.size ();
15729}
15730
2ddeaf8a
TT
15731/* A helper for process_structure_scope that handles a single member
15732 DIE. */
15733
15734static void
15735handle_struct_member_die (struct die_info *child_die, struct type *type,
15736 struct field_info *fi,
15737 std::vector<struct symbol *> *template_args,
15738 struct dwarf2_cu *cu)
15739{
15740 if (child_die->tag == DW_TAG_member
9c6a1327 15741 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15742 {
15743 /* NOTE: carlton/2002-11-05: A C++ static data member
15744 should be a DW_TAG_member that is a declaration, but
15745 all versions of G++ as of this writing (so through at
15746 least 3.2.1) incorrectly generate DW_TAG_variable
15747 tags for them instead. */
15748 dwarf2_add_field (fi, child_die, cu);
15749 }
15750 else if (child_die->tag == DW_TAG_subprogram)
15751 {
15752 /* Rust doesn't have member functions in the C++ sense.
15753 However, it does emit ordinary functions as children
15754 of a struct DIE. */
15755 if (cu->language == language_rust)
15756 read_func_scope (child_die, cu);
15757 else
15758 {
15759 /* C++ member function. */
15760 dwarf2_add_member_fn (fi, child_die, type, cu);
15761 }
15762 }
15763 else if (child_die->tag == DW_TAG_inheritance)
15764 {
15765 /* C++ base class field. */
15766 dwarf2_add_field (fi, child_die, cu);
15767 }
15768 else if (type_can_define_types (child_die))
15769 dwarf2_add_type_defn (fi, child_die, cu);
15770 else if (child_die->tag == DW_TAG_template_type_param
15771 || child_die->tag == DW_TAG_template_value_param)
15772 {
15773 struct symbol *arg = new_symbol (child_die, NULL, cu);
15774
15775 if (arg != NULL)
15776 template_args->push_back (arg);
15777 }
9c6a1327
TT
15778 else if (child_die->tag == DW_TAG_variant_part)
15779 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15780 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15781 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15782}
15783
c767944b
DJ
15784/* Finish creating a structure or union type, including filling in
15785 its members and creating a symbol for it. */
15786
15787static void
15788process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15789{
5e22e966 15790 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15791 struct die_info *child_die;
c767944b
DJ
15792 struct type *type;
15793
15794 type = get_die_type (die, cu);
15795 if (type == NULL)
15796 type = read_structure_type (die, cu);
15797
3e1d3d8c 15798 bool has_template_parameters = false;
e142c38c 15799 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15800 {
15801 struct field_info fi;
2f4732b0 15802 std::vector<struct symbol *> template_args;
c906108c 15803
639d11d3 15804 child_die = die->child;
c906108c
SS
15805
15806 while (child_die && child_die->tag)
15807 {
2ddeaf8a 15808 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15809 child_die = child_die->sibling;
c906108c
SS
15810 }
15811
34eaf542 15812 /* Attach template arguments to type. */
2f4732b0 15813 if (!template_args.empty ())
34eaf542 15814 {
3e1d3d8c 15815 has_template_parameters = true;
34eaf542 15816 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15817 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15818 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15819 = XOBNEWVEC (&objfile->objfile_obstack,
15820 struct symbol *,
15821 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15822 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15823 template_args.data (),
34eaf542
TT
15824 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15825 * sizeof (struct symbol *)));
34eaf542
TT
15826 }
15827
c906108c 15828 /* Attach fields and member functions to the type. */
317f7127 15829 if (fi.nfields () > 0)
e7c27a73 15830 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15831 if (!fi.fnfieldlists.empty ())
c906108c 15832 {
e7c27a73 15833 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15834
c5aa993b 15835 /* Get the type which refers to the base class (possibly this
c906108c 15836 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15837 class from the DW_AT_containing_type attribute. This use of
15838 DW_AT_containing_type is a GNU extension. */
c906108c 15839
e142c38c 15840 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15841 {
e7c27a73 15842 struct type *t = die_containing_type (die, cu);
c906108c 15843
ae6ae975 15844 set_type_vptr_basetype (type, t);
c906108c
SS
15845 if (type == t)
15846 {
c906108c
SS
15847 int i;
15848
15849 /* Our own class provides vtbl ptr. */
1f704f76 15850 for (i = t->num_fields () - 1;
c906108c
SS
15851 i >= TYPE_N_BASECLASSES (t);
15852 --i)
15853 {
0d5cff50 15854 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15855
1168df01 15856 if (is_vtable_name (fieldname, cu))
c906108c 15857 {
ae6ae975 15858 set_type_vptr_fieldno (type, i);
c906108c
SS
15859 break;
15860 }
15861 }
15862
15863 /* Complain if virtual function table field not found. */
15864 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15865 complaint (_("virtual function table pointer "
3e43a32a 15866 "not found when defining class '%s'"),
7d93a1e0 15867 type->name () ? type->name () : "");
c906108c
SS
15868 }
15869 else
15870 {
ae6ae975 15871 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15872 }
15873 }
f6235d4c 15874 else if (cu->producer
61012eef 15875 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15876 {
15877 /* The IBM XLC compiler does not provide direct indication
15878 of the containing type, but the vtable pointer is
15879 always named __vfp. */
15880
15881 int i;
15882
1f704f76 15883 for (i = type->num_fields () - 1;
f6235d4c
EZ
15884 i >= TYPE_N_BASECLASSES (type);
15885 --i)
15886 {
15887 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15888 {
ae6ae975
DE
15889 set_type_vptr_fieldno (type, i);
15890 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15891 break;
15892 }
15893 }
15894 }
c906108c 15895 }
98751a41
JK
15896
15897 /* Copy fi.typedef_field_list linked list elements content into the
15898 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15899 if (!fi.typedef_field_list.empty ())
98751a41 15900 {
be2daae6 15901 int count = fi.typedef_field_list.size ();
98751a41 15902
a0d7a4ff 15903 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15904 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15905 = ((struct decl_field *)
be2daae6
TT
15906 TYPE_ALLOC (type,
15907 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15908 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15909
be2daae6
TT
15910 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15911 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15912 }
c767944b 15913
883fd55a
KS
15914 /* Copy fi.nested_types_list linked list elements content into the
15915 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15916 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15917 {
be2daae6 15918 int count = fi.nested_types_list.size ();
883fd55a
KS
15919
15920 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15921 TYPE_NESTED_TYPES_ARRAY (type)
15922 = ((struct decl_field *)
be2daae6
TT
15923 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15924 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15925
be2daae6
TT
15926 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15927 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15928 }
c906108c 15929 }
63d06c5c 15930
bb5ed363 15931 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15932 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15933 cu->rust_unions.push_back (type);
0b92b5bb 15934
90aeadfc
DC
15935 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15936 snapshots) has been known to create a die giving a declaration
15937 for a class that has, as a child, a die giving a definition for a
15938 nested class. So we have to process our children even if the
15939 current die is a declaration. Normally, of course, a declaration
15940 won't have any children at all. */
134d01f1 15941
ca040673
DE
15942 child_die = die->child;
15943
90aeadfc
DC
15944 while (child_die != NULL && child_die->tag)
15945 {
15946 if (child_die->tag == DW_TAG_member
15947 || child_die->tag == DW_TAG_variable
34eaf542
TT
15948 || child_die->tag == DW_TAG_inheritance
15949 || child_die->tag == DW_TAG_template_value_param
15950 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15951 {
90aeadfc 15952 /* Do nothing. */
134d01f1 15953 }
90aeadfc
DC
15954 else
15955 process_die (child_die, cu);
134d01f1 15956
436c571c 15957 child_die = child_die->sibling;
134d01f1
DJ
15958 }
15959
fa4028e9
JB
15960 /* Do not consider external references. According to the DWARF standard,
15961 these DIEs are identified by the fact that they have no byte_size
15962 attribute, and a declaration attribute. */
15963 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
15964 || !die_is_declaration (die, cu)
15965 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
15966 {
15967 struct symbol *sym = new_symbol (die, type, cu);
15968
15969 if (has_template_parameters)
15970 {
a776957c
TT
15971 struct symtab *symtab;
15972 if (sym != nullptr)
15973 symtab = symbol_symtab (sym);
15974 else if (cu->line_header != nullptr)
15975 {
15976 /* Any related symtab will do. */
15977 symtab
7ba99d21 15978 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15979 }
15980 else
15981 {
15982 symtab = nullptr;
15983 complaint (_("could not find suitable "
15984 "symtab for template parameter"
15985 " - DIE at %s [in module %s]"),
15986 sect_offset_str (die->sect_off),
15987 objfile_name (objfile));
15988 }
15989
15990 if (symtab != nullptr)
15991 {
15992 /* Make sure that the symtab is set on the new symbols.
15993 Even though they don't appear in this symtab directly,
15994 other parts of gdb assume that symbols do, and this is
15995 reasonably true. */
15996 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15997 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15998 }
3e1d3d8c
TT
15999 }
16000 }
134d01f1
DJ
16001}
16002
ed6acedd
TT
16003/* Assuming DIE is an enumeration type, and TYPE is its associated
16004 type, update TYPE using some information only available in DIE's
16005 children. In particular, the fields are computed. */
55426c9d
JB
16006
16007static void
16008update_enumeration_type_from_children (struct die_info *die,
16009 struct type *type,
16010 struct dwarf2_cu *cu)
16011{
60f7655a 16012 struct die_info *child_die;
55426c9d
JB
16013 int unsigned_enum = 1;
16014 int flag_enum = 1;
55426c9d 16015
8268c778 16016 auto_obstack obstack;
ed6acedd 16017 std::vector<struct field> fields;
55426c9d 16018
60f7655a
DE
16019 for (child_die = die->child;
16020 child_die != NULL && child_die->tag;
436c571c 16021 child_die = child_die->sibling)
55426c9d
JB
16022 {
16023 struct attribute *attr;
16024 LONGEST value;
16025 const gdb_byte *bytes;
16026 struct dwarf2_locexpr_baton *baton;
16027 const char *name;
60f7655a 16028
55426c9d
JB
16029 if (child_die->tag != DW_TAG_enumerator)
16030 continue;
16031
16032 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16033 if (attr == NULL)
16034 continue;
16035
16036 name = dwarf2_name (child_die, cu);
16037 if (name == NULL)
16038 name = "<anonymous enumerator>";
16039
16040 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16041 &value, &bytes, &baton);
16042 if (value < 0)
16043 {
16044 unsigned_enum = 0;
16045 flag_enum = 0;
16046 }
55426c9d 16047 else
edd45eb0
SM
16048 {
16049 if (count_one_bits_ll (value) >= 2)
16050 flag_enum = 0;
edd45eb0 16051 }
55426c9d 16052
ed6acedd
TT
16053 fields.emplace_back ();
16054 struct field &field = fields.back ();
16055 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16056 SET_FIELD_ENUMVAL (field, value);
16057 }
16058
16059 if (!fields.empty ())
16060 {
5e33d5f4 16061 type->set_num_fields (fields.size ());
3cabb6b0
SM
16062 type->set_fields
16063 ((struct field *)
16064 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16065 memcpy (type->fields (), fields.data (),
ed6acedd 16066 sizeof (struct field) * fields.size ());
55426c9d
JB
16067 }
16068
16069 if (unsigned_enum)
16070 TYPE_UNSIGNED (type) = 1;
16071 if (flag_enum)
16072 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16073}
16074
134d01f1
DJ
16075/* Given a DW_AT_enumeration_type die, set its type. We do not
16076 complete the type's fields yet, or create any symbols. */
c906108c 16077
f792889a 16078static struct type *
134d01f1 16079read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16080{
5e22e966 16081 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16082 struct type *type;
c906108c 16083 struct attribute *attr;
0114d602 16084 const char *name;
134d01f1 16085
348e048f
DE
16086 /* If the definition of this type lives in .debug_types, read that type.
16087 Don't follow DW_AT_specification though, that will take us back up
16088 the chain and we want to go down. */
052c8bb8 16089 attr = die->attr (DW_AT_signature);
435d3d88 16090 if (attr != nullptr)
348e048f 16091 {
ac9ec31b 16092 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16093
ac9ec31b 16094 /* The type's CU may not be the same as CU.
02142a6c 16095 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16096 return set_die_type (die, type, cu);
16097 }
16098
c906108c
SS
16099 type = alloc_type (objfile);
16100
67607e24 16101 type->set_code (TYPE_CODE_ENUM);
94af9270 16102 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16103 if (name != NULL)
d0e39ea2 16104 type->set_name (name);
c906108c 16105
0626fc76
TT
16106 attr = dwarf2_attr (die, DW_AT_type, cu);
16107 if (attr != NULL)
16108 {
16109 struct type *underlying_type = die_type (die, cu);
16110
16111 TYPE_TARGET_TYPE (type) = underlying_type;
16112 }
16113
e142c38c 16114 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16115 if (attr != nullptr)
c906108c
SS
16116 {
16117 TYPE_LENGTH (type) = DW_UNSND (attr);
16118 }
16119 else
16120 {
16121 TYPE_LENGTH (type) = 0;
16122 }
16123
2b4424c3
TT
16124 maybe_set_alignment (cu, die, type);
16125
137033e9
JB
16126 /* The enumeration DIE can be incomplete. In Ada, any type can be
16127 declared as private in the package spec, and then defined only
16128 inside the package body. Such types are known as Taft Amendment
16129 Types. When another package uses such a type, an incomplete DIE
16130 may be generated by the compiler. */
02eb380e 16131 if (die_is_declaration (die, cu))
876cecd0 16132 TYPE_STUB (type) = 1;
02eb380e 16133
0626fc76
TT
16134 /* If this type has an underlying type that is not a stub, then we
16135 may use its attributes. We always use the "unsigned" attribute
16136 in this situation, because ordinarily we guess whether the type
16137 is unsigned -- but the guess can be wrong and the underlying type
16138 can tell us the reality. However, we defer to a local size
16139 attribute if one exists, because this lets the compiler override
16140 the underlying type if needed. */
16141 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16142 {
9e7c9a03
HD
16143 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16144 underlying_type = check_typedef (underlying_type);
16145 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 16146 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16147 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 16148 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16149 && TYPE_RAW_ALIGN (underlying_type) != 0)
16150 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16151 }
16152
3d567982
TT
16153 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16154
ed6acedd
TT
16155 set_die_type (die, type, cu);
16156
16157 /* Finish the creation of this type by using the enum's children.
16158 Note that, as usual, this must come after set_die_type to avoid
16159 infinite recursion when trying to compute the names of the
16160 enumerators. */
16161 update_enumeration_type_from_children (die, type, cu);
16162
16163 return type;
134d01f1
DJ
16164}
16165
16166/* Given a pointer to a die which begins an enumeration, process all
16167 the dies that define the members of the enumeration, and create the
16168 symbol for the enumeration type.
16169
16170 NOTE: We reverse the order of the element list. */
16171
16172static void
16173process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16174{
f792889a 16175 struct type *this_type;
134d01f1 16176
f792889a
DJ
16177 this_type = get_die_type (die, cu);
16178 if (this_type == NULL)
16179 this_type = read_enumeration_type (die, cu);
9dc481d3 16180
639d11d3 16181 if (die->child != NULL)
c906108c 16182 {
9dc481d3 16183 struct die_info *child_die;
15d034d0 16184 const char *name;
9dc481d3 16185
639d11d3 16186 child_die = die->child;
c906108c
SS
16187 while (child_die && child_die->tag)
16188 {
16189 if (child_die->tag != DW_TAG_enumerator)
16190 {
e7c27a73 16191 process_die (child_die, cu);
c906108c
SS
16192 }
16193 else
16194 {
39cbfefa
DJ
16195 name = dwarf2_name (child_die, cu);
16196 if (name)
ed6acedd 16197 new_symbol (child_die, this_type, cu);
c906108c
SS
16198 }
16199
436c571c 16200 child_die = child_die->sibling;
c906108c 16201 }
c906108c 16202 }
134d01f1 16203
6c83ed52
TT
16204 /* If we are reading an enum from a .debug_types unit, and the enum
16205 is a declaration, and the enum is not the signatured type in the
16206 unit, then we do not want to add a symbol for it. Adding a
16207 symbol would in some cases obscure the true definition of the
16208 enum, giving users an incomplete type when the definition is
16209 actually available. Note that we do not want to do this for all
16210 enums which are just declarations, because C++0x allows forward
16211 enum declarations. */
3019eac3 16212 if (cu->per_cu->is_debug_types
6c83ed52
TT
16213 && die_is_declaration (die, cu))
16214 {
52dc124a 16215 struct signatured_type *sig_type;
6c83ed52 16216
c0f78cd4 16217 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16218 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16219 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16220 return;
16221 }
16222
f792889a 16223 new_symbol (die, this_type, cu);
c906108c
SS
16224}
16225
16226/* Extract all information from a DW_TAG_array_type DIE and put it in
16227 the DIE's type field. For now, this only handles one dimensional
16228 arrays. */
16229
f792889a 16230static struct type *
e7c27a73 16231read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16232{
5e22e966 16233 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16234 struct die_info *child_die;
7e314c57 16235 struct type *type;
c906108c 16236 struct type *element_type, *range_type, *index_type;
c906108c 16237 struct attribute *attr;
15d034d0 16238 const char *name;
a405673c 16239 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16240 unsigned int bit_stride = 0;
c906108c 16241
e7c27a73 16242 element_type = die_type (die, cu);
c906108c 16243
7e314c57
JK
16244 /* The die_type call above may have already set the type for this DIE. */
16245 type = get_die_type (die, cu);
16246 if (type)
16247 return type;
16248
dc53a7ad
JB
16249 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16250 if (attr != NULL)
a405673c
JB
16251 {
16252 int stride_ok;
293e7e51 16253 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16254
16255 byte_stride_prop
16256 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16257 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16258 prop_type);
a405673c
JB
16259 if (!stride_ok)
16260 {
b98664d3 16261 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16262 " - DIE at %s [in module %s]"),
16263 sect_offset_str (die->sect_off),
5e22e966 16264 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16265 /* Ignore this attribute. We will likely not be able to print
16266 arrays of this type correctly, but there is little we can do
16267 to help if we cannot read the attribute's value. */
16268 byte_stride_prop = NULL;
16269 }
16270 }
dc53a7ad
JB
16271
16272 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16273 if (attr != NULL)
16274 bit_stride = DW_UNSND (attr);
16275
c906108c
SS
16276 /* Irix 6.2 native cc creates array types without children for
16277 arrays with unspecified length. */
639d11d3 16278 if (die->child == NULL)
c906108c 16279 {
46bf5051 16280 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16281 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16282 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16283 byte_stride_prop, bit_stride);
f792889a 16284 return set_die_type (die, type, cu);
c906108c
SS
16285 }
16286
791afaa2 16287 std::vector<struct type *> range_types;
639d11d3 16288 child_die = die->child;
c906108c
SS
16289 while (child_die && child_die->tag)
16290 {
16291 if (child_die->tag == DW_TAG_subrange_type)
16292 {
f792889a 16293 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16294
f792889a 16295 if (child_type != NULL)
a02abb62 16296 {
0963b4bd
MS
16297 /* The range type was succesfully read. Save it for the
16298 array type creation. */
791afaa2 16299 range_types.push_back (child_type);
a02abb62 16300 }
c906108c 16301 }
436c571c 16302 child_die = child_die->sibling;
c906108c
SS
16303 }
16304
16305 /* Dwarf2 dimensions are output from left to right, create the
16306 necessary array types in backwards order. */
7ca2d3a3 16307
c906108c 16308 type = element_type;
7ca2d3a3
DL
16309
16310 if (read_array_order (die, cu) == DW_ORD_col_major)
16311 {
16312 int i = 0;
9a619af0 16313
791afaa2 16314 while (i < range_types.size ())
dc53a7ad 16315 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16316 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16317 }
16318 else
16319 {
791afaa2 16320 size_t ndim = range_types.size ();
7ca2d3a3 16321 while (ndim-- > 0)
dc53a7ad 16322 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16323 byte_stride_prop, bit_stride);
7ca2d3a3 16324 }
c906108c 16325
f5f8a009
EZ
16326 /* Understand Dwarf2 support for vector types (like they occur on
16327 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16328 array type. This is not part of the Dwarf2/3 standard yet, but a
16329 custom vendor extension. The main difference between a regular
16330 array and the vector variant is that vectors are passed by value
16331 to functions. */
e142c38c 16332 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16333 if (attr != nullptr)
ea37ba09 16334 make_vector_type (type);
f5f8a009 16335
dbc98a8b
KW
16336 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16337 implementation may choose to implement triple vectors using this
16338 attribute. */
16339 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16340 if (attr != nullptr)
dbc98a8b
KW
16341 {
16342 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16343 TYPE_LENGTH (type) = DW_UNSND (attr);
16344 else
b98664d3 16345 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16346 "than the total size of elements"));
dbc98a8b
KW
16347 }
16348
39cbfefa
DJ
16349 name = dwarf2_name (die, cu);
16350 if (name)
d0e39ea2 16351 type->set_name (name);
6e70227d 16352
2b4424c3
TT
16353 maybe_set_alignment (cu, die, type);
16354
0963b4bd 16355 /* Install the type in the die. */
7e314c57
JK
16356 set_die_type (die, type, cu);
16357
16358 /* set_die_type should be already done. */
b4ba55a1
JB
16359 set_descriptive_type (type, die, cu);
16360
7e314c57 16361 return type;
c906108c
SS
16362}
16363
7ca2d3a3 16364static enum dwarf_array_dim_ordering
6e70227d 16365read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16366{
16367 struct attribute *attr;
16368
16369 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16370
435d3d88 16371 if (attr != nullptr)
aead7601 16372 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16373
0963b4bd
MS
16374 /* GNU F77 is a special case, as at 08/2004 array type info is the
16375 opposite order to the dwarf2 specification, but data is still
16376 laid out as per normal fortran.
7ca2d3a3 16377
0963b4bd
MS
16378 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16379 version checking. */
7ca2d3a3 16380
905e0470
PM
16381 if (cu->language == language_fortran
16382 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16383 {
16384 return DW_ORD_row_major;
16385 }
16386
6e70227d 16387 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16388 {
16389 case array_column_major:
16390 return DW_ORD_col_major;
16391 case array_row_major:
16392 default:
16393 return DW_ORD_row_major;
16394 };
16395}
16396
72019c9c 16397/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16398 the DIE's type field. */
72019c9c 16399
f792889a 16400static struct type *
72019c9c
GM
16401read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16402{
7e314c57
JK
16403 struct type *domain_type, *set_type;
16404 struct attribute *attr;
f792889a 16405
7e314c57
JK
16406 domain_type = die_type (die, cu);
16407
16408 /* The die_type call above may have already set the type for this DIE. */
16409 set_type = get_die_type (die, cu);
16410 if (set_type)
16411 return set_type;
16412
16413 set_type = create_set_type (NULL, domain_type);
16414
16415 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16416 if (attr != nullptr)
d09039dd 16417 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16418
2b4424c3
TT
16419 maybe_set_alignment (cu, die, set_type);
16420
f792889a 16421 return set_die_type (die, set_type, cu);
72019c9c 16422}
7ca2d3a3 16423
0971de02
TT
16424/* A helper for read_common_block that creates a locexpr baton.
16425 SYM is the symbol which we are marking as computed.
16426 COMMON_DIE is the DIE for the common block.
16427 COMMON_LOC is the location expression attribute for the common
16428 block itself.
16429 MEMBER_LOC is the location expression attribute for the particular
16430 member of the common block that we are processing.
16431 CU is the CU from which the above come. */
16432
16433static void
16434mark_common_block_symbol_computed (struct symbol *sym,
16435 struct die_info *common_die,
16436 struct attribute *common_loc,
16437 struct attribute *member_loc,
16438 struct dwarf2_cu *cu)
16439{
5e22e966 16440 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16441 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16442 struct dwarf2_locexpr_baton *baton;
16443 gdb_byte *ptr;
16444 unsigned int cu_off;
08feed99 16445 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16446 LONGEST offset = 0;
16447
16448 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16449 gdb_assert (common_loc->form_is_block ());
16450 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16451 || member_loc->form_is_constant ());
0971de02 16452
8d749320 16453 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16454 baton->per_objfile = per_objfile;
0971de02
TT
16455 baton->per_cu = cu->per_cu;
16456 gdb_assert (baton->per_cu);
16457
16458 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16459
cd6c91b4 16460 if (member_loc->form_is_constant ())
0971de02 16461 {
0826b30a 16462 offset = member_loc->constant_value (0);
0971de02
TT
16463 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16464 }
16465 else
16466 baton->size += DW_BLOCK (member_loc)->size;
16467
224c3ddb 16468 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16469 baton->data = ptr;
16470
16471 *ptr++ = DW_OP_call4;
9c541725 16472 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16473 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16474 ptr += 4;
16475
cd6c91b4 16476 if (member_loc->form_is_constant ())
0971de02
TT
16477 {
16478 *ptr++ = DW_OP_addr;
16479 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16480 ptr += cu->header.addr_size;
16481 }
16482 else
16483 {
16484 /* We have to copy the data here, because DW_OP_call4 will only
16485 use a DW_AT_location attribute. */
16486 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16487 ptr += DW_BLOCK (member_loc)->size;
16488 }
16489
16490 *ptr++ = DW_OP_plus;
16491 gdb_assert (ptr - baton->data == baton->size);
16492
0971de02 16493 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16494 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16495}
16496
4357ac6c
TT
16497/* Create appropriate locally-scoped variables for all the
16498 DW_TAG_common_block entries. Also create a struct common_block
16499 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16500 is used to separate the common blocks name namespace from regular
4357ac6c 16501 variable names. */
c906108c
SS
16502
16503static void
e7c27a73 16504read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16505{
0971de02
TT
16506 struct attribute *attr;
16507
16508 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16509 if (attr != nullptr)
0971de02
TT
16510 {
16511 /* Support the .debug_loc offsets. */
4fc6c0d5 16512 if (attr->form_is_block ())
0971de02
TT
16513 {
16514 /* Ok. */
16515 }
cd6c91b4 16516 else if (attr->form_is_section_offset ())
0971de02
TT
16517 {
16518 dwarf2_complex_location_expr_complaint ();
16519 attr = NULL;
16520 }
16521 else
16522 {
16523 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16524 "common block member");
16525 attr = NULL;
16526 }
16527 }
16528
639d11d3 16529 if (die->child != NULL)
c906108c 16530 {
5e22e966 16531 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
16532 struct die_info *child_die;
16533 size_t n_entries = 0, size;
16534 struct common_block *common_block;
16535 struct symbol *sym;
74ac6d43 16536
4357ac6c
TT
16537 for (child_die = die->child;
16538 child_die && child_die->tag;
436c571c 16539 child_die = child_die->sibling)
4357ac6c
TT
16540 ++n_entries;
16541
16542 size = (sizeof (struct common_block)
16543 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16544 common_block
16545 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16546 size);
4357ac6c
TT
16547 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16548 common_block->n_entries = 0;
16549
16550 for (child_die = die->child;
16551 child_die && child_die->tag;
436c571c 16552 child_die = child_die->sibling)
4357ac6c
TT
16553 {
16554 /* Create the symbol in the DW_TAG_common_block block in the current
16555 symbol scope. */
e7c27a73 16556 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16557 if (sym != NULL)
16558 {
16559 struct attribute *member_loc;
16560
16561 common_block->contents[common_block->n_entries++] = sym;
16562
16563 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16564 cu);
16565 if (member_loc)
16566 {
16567 /* GDB has handled this for a long time, but it is
16568 not specified by DWARF. It seems to have been
16569 emitted by gfortran at least as recently as:
16570 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16571 complaint (_("Variable in common block has "
0971de02 16572 "DW_AT_data_member_location "
9d8780f0
SM
16573 "- DIE at %s [in module %s]"),
16574 sect_offset_str (child_die->sect_off),
518817b3 16575 objfile_name (objfile));
0971de02 16576
cd6c91b4 16577 if (member_loc->form_is_section_offset ())
0971de02 16578 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16579 else if (member_loc->form_is_constant ()
4fc6c0d5 16580 || member_loc->form_is_block ())
0971de02 16581 {
435d3d88 16582 if (attr != nullptr)
0971de02
TT
16583 mark_common_block_symbol_computed (sym, die, attr,
16584 member_loc, cu);
16585 }
16586 else
16587 dwarf2_complex_location_expr_complaint ();
16588 }
16589 }
c906108c 16590 }
4357ac6c
TT
16591
16592 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16593 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16594 }
16595}
16596
0114d602 16597/* Create a type for a C++ namespace. */
d9fa45fe 16598
0114d602
DJ
16599static struct type *
16600read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16601{
5e22e966 16602 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16603 const char *previous_prefix, *name;
9219021c 16604 int is_anonymous;
0114d602
DJ
16605 struct type *type;
16606
16607 /* For extensions, reuse the type of the original namespace. */
16608 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16609 {
16610 struct die_info *ext_die;
16611 struct dwarf2_cu *ext_cu = cu;
9a619af0 16612
0114d602
DJ
16613 ext_die = dwarf2_extension (die, &ext_cu);
16614 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16615
16616 /* EXT_CU may not be the same as CU.
02142a6c 16617 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16618 return set_die_type (die, type, cu);
16619 }
9219021c 16620
e142c38c 16621 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16622
16623 /* Now build the name of the current namespace. */
16624
0114d602
DJ
16625 previous_prefix = determine_prefix (die, cu);
16626 if (previous_prefix[0] != '\0')
16627 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16628 previous_prefix, name, 0, cu);
0114d602
DJ
16629
16630 /* Create the type. */
19f392bc 16631 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16632
60531b24 16633 return set_die_type (die, type, cu);
0114d602
DJ
16634}
16635
22cee43f 16636/* Read a namespace scope. */
0114d602
DJ
16637
16638static void
16639read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16640{
5e22e966 16641 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16642 int is_anonymous;
9219021c 16643
5c4e30ca
DC
16644 /* Add a symbol associated to this if we haven't seen the namespace
16645 before. Also, add a using directive if it's an anonymous
16646 namespace. */
9219021c 16647
f2f0e013 16648 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16649 {
16650 struct type *type;
16651
0114d602 16652 type = read_type_die (die, cu);
e7c27a73 16653 new_symbol (die, type, cu);
5c4e30ca 16654
e8e80198 16655 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16656 if (is_anonymous)
0114d602
DJ
16657 {
16658 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16659
eb1e02fd 16660 std::vector<const char *> excludes;
804d2729 16661 add_using_directive (using_directives (cu),
7d93a1e0 16662 previous_prefix, type->name (), NULL,
eb1e02fd 16663 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16664 }
5c4e30ca 16665 }
9219021c 16666
639d11d3 16667 if (die->child != NULL)
d9fa45fe 16668 {
639d11d3 16669 struct die_info *child_die = die->child;
6e70227d 16670
d9fa45fe
DC
16671 while (child_die && child_die->tag)
16672 {
e7c27a73 16673 process_die (child_die, cu);
436c571c 16674 child_die = child_die->sibling;
d9fa45fe
DC
16675 }
16676 }
38d518c9
EZ
16677}
16678
f55ee35c
JK
16679/* Read a Fortran module as type. This DIE can be only a declaration used for
16680 imported module. Still we need that type as local Fortran "use ... only"
16681 declaration imports depend on the created type in determine_prefix. */
16682
16683static struct type *
16684read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16685{
5e22e966 16686 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 16687 const char *module_name;
f55ee35c
JK
16688 struct type *type;
16689
16690 module_name = dwarf2_name (die, cu);
19f392bc 16691 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16692
f55ee35c
JK
16693 return set_die_type (die, type, cu);
16694}
16695
5d7cb8df
JK
16696/* Read a Fortran module. */
16697
16698static void
16699read_module (struct die_info *die, struct dwarf2_cu *cu)
16700{
16701 struct die_info *child_die = die->child;
530e8392
KB
16702 struct type *type;
16703
16704 type = read_type_die (die, cu);
16705 new_symbol (die, type, cu);
5d7cb8df 16706
5d7cb8df
JK
16707 while (child_die && child_die->tag)
16708 {
16709 process_die (child_die, cu);
436c571c 16710 child_die = child_die->sibling;
5d7cb8df
JK
16711 }
16712}
16713
38d518c9
EZ
16714/* Return the name of the namespace represented by DIE. Set
16715 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16716 namespace. */
16717
16718static const char *
e142c38c 16719namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16720{
16721 struct die_info *current_die;
16722 const char *name = NULL;
16723
16724 /* Loop through the extensions until we find a name. */
16725
16726 for (current_die = die;
16727 current_die != NULL;
f2f0e013 16728 current_die = dwarf2_extension (die, &cu))
38d518c9 16729 {
96553a0c
DE
16730 /* We don't use dwarf2_name here so that we can detect the absence
16731 of a name -> anonymous namespace. */
7d45c7c3 16732 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16733
38d518c9
EZ
16734 if (name != NULL)
16735 break;
16736 }
16737
16738 /* Is it an anonymous namespace? */
16739
16740 *is_anonymous = (name == NULL);
16741 if (*is_anonymous)
2b1dbab0 16742 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16743
16744 return name;
d9fa45fe
DC
16745}
16746
c906108c
SS
16747/* Extract all information from a DW_TAG_pointer_type DIE and add to
16748 the user defined type vector. */
16749
f792889a 16750static struct type *
e7c27a73 16751read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16752{
5e22e966 16753 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 16754 struct comp_unit_head *cu_header = &cu->header;
c906108c 16755 struct type *type;
8b2dbe47
KB
16756 struct attribute *attr_byte_size;
16757 struct attribute *attr_address_class;
16758 int byte_size, addr_class;
7e314c57
JK
16759 struct type *target_type;
16760
16761 target_type = die_type (die, cu);
c906108c 16762
7e314c57
JK
16763 /* The die_type call above may have already set the type for this DIE. */
16764 type = get_die_type (die, cu);
16765 if (type)
16766 return type;
16767
16768 type = lookup_pointer_type (target_type);
8b2dbe47 16769
e142c38c 16770 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16771 if (attr_byte_size)
16772 byte_size = DW_UNSND (attr_byte_size);
c906108c 16773 else
8b2dbe47
KB
16774 byte_size = cu_header->addr_size;
16775
e142c38c 16776 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16777 if (attr_address_class)
16778 addr_class = DW_UNSND (attr_address_class);
16779 else
16780 addr_class = DW_ADDR_none;
16781
2b4424c3
TT
16782 ULONGEST alignment = get_alignment (cu, die);
16783
16784 /* If the pointer size, alignment, or address class is different
16785 than the default, create a type variant marked as such and set
16786 the length accordingly. */
16787 if (TYPE_LENGTH (type) != byte_size
16788 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16789 && alignment != TYPE_RAW_ALIGN (type))
16790 || addr_class != DW_ADDR_none)
c906108c 16791 {
5e2b427d 16792 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16793 {
16794 int type_flags;
16795
849957d9 16796 type_flags = gdbarch_address_class_type_flags
5e2b427d 16797 (gdbarch, byte_size, addr_class);
876cecd0
TT
16798 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16799 == 0);
8b2dbe47
KB
16800 type = make_type_with_address_space (type, type_flags);
16801 }
16802 else if (TYPE_LENGTH (type) != byte_size)
16803 {
b98664d3 16804 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16805 }
2b4424c3
TT
16806 else if (TYPE_RAW_ALIGN (type) != alignment)
16807 {
b98664d3 16808 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16809 " - DIE at %s [in module %s]"),
16810 sect_offset_str (die->sect_off),
5e22e966 16811 objfile_name (cu->per_objfile->objfile));
2b4424c3 16812 }
6e70227d 16813 else
9a619af0
MS
16814 {
16815 /* Should we also complain about unhandled address classes? */
16816 }
c906108c 16817 }
8b2dbe47
KB
16818
16819 TYPE_LENGTH (type) = byte_size;
2b4424c3 16820 set_type_align (type, alignment);
f792889a 16821 return set_die_type (die, type, cu);
c906108c
SS
16822}
16823
16824/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16825 the user defined type vector. */
16826
f792889a 16827static struct type *
e7c27a73 16828read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16829{
16830 struct type *type;
16831 struct type *to_type;
16832 struct type *domain;
16833
e7c27a73
DJ
16834 to_type = die_type (die, cu);
16835 domain = die_containing_type (die, cu);
0d5de010 16836
7e314c57
JK
16837 /* The calls above may have already set the type for this DIE. */
16838 type = get_die_type (die, cu);
16839 if (type)
16840 return type;
16841
78134374 16842 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 16843 type = lookup_methodptr_type (to_type);
78134374 16844 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 16845 {
5e22e966 16846 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
16847
16848 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 16849 to_type->fields (), to_type->num_fields (),
7078baeb
TT
16850 TYPE_VARARGS (to_type));
16851 type = lookup_methodptr_type (new_type);
16852 }
0d5de010
DJ
16853 else
16854 type = lookup_memberptr_type (to_type, domain);
c906108c 16855
f792889a 16856 return set_die_type (die, type, cu);
c906108c
SS
16857}
16858
4297a3f0 16859/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16860 the user defined type vector. */
16861
f792889a 16862static struct type *
4297a3f0
AV
16863read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16864 enum type_code refcode)
c906108c 16865{
e7c27a73 16866 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16867 struct type *type, *target_type;
c906108c
SS
16868 struct attribute *attr;
16869
4297a3f0
AV
16870 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16871
7e314c57
JK
16872 target_type = die_type (die, cu);
16873
16874 /* The die_type call above may have already set the type for this DIE. */
16875 type = get_die_type (die, cu);
16876 if (type)
16877 return type;
16878
4297a3f0 16879 type = lookup_reference_type (target_type, refcode);
e142c38c 16880 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16881 if (attr != nullptr)
c906108c
SS
16882 {
16883 TYPE_LENGTH (type) = DW_UNSND (attr);
16884 }
16885 else
16886 {
107d2387 16887 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16888 }
2b4424c3 16889 maybe_set_alignment (cu, die, type);
f792889a 16890 return set_die_type (die, type, cu);
c906108c
SS
16891}
16892
cf363f18
MW
16893/* Add the given cv-qualifiers to the element type of the array. GCC
16894 outputs DWARF type qualifiers that apply to an array, not the
16895 element type. But GDB relies on the array element type to carry
16896 the cv-qualifiers. This mimics section 6.7.3 of the C99
16897 specification. */
16898
16899static struct type *
16900add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16901 struct type *base_type, int cnst, int voltl)
16902{
16903 struct type *el_type, *inner_array;
16904
16905 base_type = copy_type (base_type);
16906 inner_array = base_type;
16907
78134374 16908 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16909 {
16910 TYPE_TARGET_TYPE (inner_array) =
16911 copy_type (TYPE_TARGET_TYPE (inner_array));
16912 inner_array = TYPE_TARGET_TYPE (inner_array);
16913 }
16914
16915 el_type = TYPE_TARGET_TYPE (inner_array);
16916 cnst |= TYPE_CONST (el_type);
16917 voltl |= TYPE_VOLATILE (el_type);
16918 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16919
16920 return set_die_type (die, base_type, cu);
16921}
16922
f792889a 16923static struct type *
e7c27a73 16924read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16925{
f792889a 16926 struct type *base_type, *cv_type;
c906108c 16927
e7c27a73 16928 base_type = die_type (die, cu);
7e314c57
JK
16929
16930 /* The die_type call above may have already set the type for this DIE. */
16931 cv_type = get_die_type (die, cu);
16932 if (cv_type)
16933 return cv_type;
16934
2f608a3a
KW
16935 /* In case the const qualifier is applied to an array type, the element type
16936 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 16937 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 16938 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16939
f792889a
DJ
16940 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16941 return set_die_type (die, cv_type, cu);
c906108c
SS
16942}
16943
f792889a 16944static struct type *
e7c27a73 16945read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16946{
f792889a 16947 struct type *base_type, *cv_type;
c906108c 16948
e7c27a73 16949 base_type = die_type (die, cu);
7e314c57
JK
16950
16951 /* The die_type call above may have already set the type for this DIE. */
16952 cv_type = get_die_type (die, cu);
16953 if (cv_type)
16954 return cv_type;
16955
cf363f18
MW
16956 /* In case the volatile qualifier is applied to an array type, the
16957 element type is so qualified, not the array type (section 6.7.3
16958 of C99). */
78134374 16959 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16960 return add_array_cv_type (die, cu, base_type, 0, 1);
16961
f792889a
DJ
16962 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16963 return set_die_type (die, cv_type, cu);
c906108c
SS
16964}
16965
06d66ee9
TT
16966/* Handle DW_TAG_restrict_type. */
16967
16968static struct type *
16969read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16970{
16971 struct type *base_type, *cv_type;
16972
16973 base_type = die_type (die, cu);
16974
16975 /* The die_type call above may have already set the type for this DIE. */
16976 cv_type = get_die_type (die, cu);
16977 if (cv_type)
16978 return cv_type;
16979
16980 cv_type = make_restrict_type (base_type);
16981 return set_die_type (die, cv_type, cu);
16982}
16983
a2c2acaf
MW
16984/* Handle DW_TAG_atomic_type. */
16985
16986static struct type *
16987read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16988{
16989 struct type *base_type, *cv_type;
16990
16991 base_type = die_type (die, cu);
16992
16993 /* The die_type call above may have already set the type for this DIE. */
16994 cv_type = get_die_type (die, cu);
16995 if (cv_type)
16996 return cv_type;
16997
16998 cv_type = make_atomic_type (base_type);
16999 return set_die_type (die, cv_type, cu);
17000}
17001
c906108c
SS
17002/* Extract all information from a DW_TAG_string_type DIE and add to
17003 the user defined type vector. It isn't really a user defined type,
17004 but it behaves like one, with other DIE's using an AT_user_def_type
17005 attribute to reference it. */
17006
f792889a 17007static struct type *
e7c27a73 17008read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17009{
5e22e966 17010 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17011 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17012 struct type *type, *range_type, *index_type, *char_type;
17013 struct attribute *attr;
216a7e6b
AB
17014 struct dynamic_prop prop;
17015 bool length_is_constant = true;
17016 LONGEST length;
17017
17018 /* There are a couple of places where bit sizes might be made use of
17019 when parsing a DW_TAG_string_type, however, no producer that we know
17020 of make use of these. Handling bit sizes that are a multiple of the
17021 byte size is easy enough, but what about other bit sizes? Lets deal
17022 with that problem when we have to. Warn about these attributes being
17023 unsupported, then parse the type and ignore them like we always
17024 have. */
17025 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17026 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17027 {
17028 static bool warning_printed = false;
17029 if (!warning_printed)
17030 {
17031 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17032 "currently supported on DW_TAG_string_type."));
17033 warning_printed = true;
17034 }
17035 }
c906108c 17036
e142c38c 17037 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17038 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17039 {
17040 /* The string length describes the location at which the length of
17041 the string can be found. The size of the length field can be
17042 specified with one of the attributes below. */
17043 struct type *prop_type;
17044 struct attribute *len
17045 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17046 if (len == nullptr)
17047 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17048 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17049 {
17050 /* Pass 0 as the default as we know this attribute is constant
17051 and the default value will not be returned. */
0826b30a 17052 LONGEST sz = len->constant_value (0);
293e7e51 17053 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17054 }
17055 else
17056 {
17057 /* If the size is not specified then we assume it is the size of
17058 an address on this target. */
293e7e51 17059 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17060 }
17061
17062 /* Convert the attribute into a dynamic property. */
17063 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17064 length = 1;
17065 else
17066 length_is_constant = false;
17067 }
17068 else if (attr != nullptr)
17069 {
17070 /* This DW_AT_string_length just contains the length with no
17071 indirection. There's no need to create a dynamic property in this
17072 case. Pass 0 for the default value as we know it will not be
17073 returned in this case. */
0826b30a 17074 length = attr->constant_value (0);
216a7e6b
AB
17075 }
17076 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17077 {
216a7e6b 17078 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17079 length = attr->constant_value (1);
c906108c
SS
17080 }
17081 else
17082 {
216a7e6b
AB
17083 /* Use 1 as a fallback length if we have nothing else. */
17084 length = 1;
c906108c 17085 }
6ccb9162 17086
46bf5051 17087 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17088 if (length_is_constant)
17089 range_type = create_static_range_type (NULL, index_type, 1, length);
17090 else
17091 {
17092 struct dynamic_prop low_bound;
17093
17094 low_bound.kind = PROP_CONST;
17095 low_bound.data.const_val = 1;
17096 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17097 }
3b7538c0
UW
17098 char_type = language_string_char_type (cu->language_defn, gdbarch);
17099 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17100
f792889a 17101 return set_die_type (die, type, cu);
c906108c
SS
17102}
17103
4d804846
JB
17104/* Assuming that DIE corresponds to a function, returns nonzero
17105 if the function is prototyped. */
17106
17107static int
17108prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17109{
17110 struct attribute *attr;
17111
17112 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17113 if (attr && (DW_UNSND (attr) != 0))
17114 return 1;
17115
17116 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17117 is only meaningful for C, but the concept also extends to other
4d804846
JB
17118 languages that allow unprototyped functions (Eg: Objective C).
17119 For all other languages, assume that functions are always
17120 prototyped. */
17121 if (cu->language != language_c
17122 && cu->language != language_objc
17123 && cu->language != language_opencl)
17124 return 1;
17125
17126 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17127 prototyped and unprototyped functions; default to prototyped,
17128 since that is more common in modern code (and RealView warns
17129 about unprototyped functions). */
17130 if (producer_is_realview (cu->producer))
17131 return 1;
17132
17133 return 0;
17134}
17135
c906108c
SS
17136/* Handle DIES due to C code like:
17137
17138 struct foo
c5aa993b
JM
17139 {
17140 int (*funcp)(int a, long l);
17141 int b;
17142 };
c906108c 17143
0963b4bd 17144 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17145
f792889a 17146static struct type *
e7c27a73 17147read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17148{
5e22e966 17149 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17150 struct type *type; /* Type that this function returns. */
17151 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17152 struct attribute *attr;
17153
e7c27a73 17154 type = die_type (die, cu);
7e314c57
JK
17155
17156 /* The die_type call above may have already set the type for this DIE. */
17157 ftype = get_die_type (die, cu);
17158 if (ftype)
17159 return ftype;
17160
0c8b41f1 17161 ftype = lookup_function_type (type);
c906108c 17162
4d804846 17163 if (prototyped_function_p (die, cu))
a6c727b2 17164 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17165
c055b101
CV
17166 /* Store the calling convention in the type if it's available in
17167 the subroutine die. Otherwise set the calling convention to
17168 the default value DW_CC_normal. */
17169 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17170 if (attr != nullptr
17171 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17172 TYPE_CALLING_CONVENTION (ftype)
17173 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17174 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17175 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17176 else
17177 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17178
743649fd
MW
17179 /* Record whether the function returns normally to its caller or not
17180 if the DWARF producer set that information. */
17181 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17182 if (attr && (DW_UNSND (attr) != 0))
17183 TYPE_NO_RETURN (ftype) = 1;
17184
76c10ea2
GM
17185 /* We need to add the subroutine type to the die immediately so
17186 we don't infinitely recurse when dealing with parameters
0963b4bd 17187 declared as the same subroutine type. */
76c10ea2 17188 set_die_type (die, ftype, cu);
6e70227d 17189
639d11d3 17190 if (die->child != NULL)
c906108c 17191 {
bb5ed363 17192 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17193 struct die_info *child_die;
8072405b 17194 int nparams, iparams;
c906108c
SS
17195
17196 /* Count the number of parameters.
17197 FIXME: GDB currently ignores vararg functions, but knows about
17198 vararg member functions. */
8072405b 17199 nparams = 0;
639d11d3 17200 child_die = die->child;
c906108c
SS
17201 while (child_die && child_die->tag)
17202 {
17203 if (child_die->tag == DW_TAG_formal_parameter)
17204 nparams++;
17205 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17206 TYPE_VARARGS (ftype) = 1;
436c571c 17207 child_die = child_die->sibling;
c906108c
SS
17208 }
17209
17210 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17211 ftype->set_num_fields (nparams);
3cabb6b0
SM
17212 ftype->set_fields
17213 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17214
8072405b
JK
17215 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17216 even if we error out during the parameters reading below. */
17217 for (iparams = 0; iparams < nparams; iparams++)
17218 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17219
17220 iparams = 0;
639d11d3 17221 child_die = die->child;
c906108c
SS
17222 while (child_die && child_die->tag)
17223 {
17224 if (child_die->tag == DW_TAG_formal_parameter)
17225 {
3ce3b1ba
PA
17226 struct type *arg_type;
17227
17228 /* DWARF version 2 has no clean way to discern C++
17229 static and non-static member functions. G++ helps
17230 GDB by marking the first parameter for non-static
17231 member functions (which is the this pointer) as
17232 artificial. We pass this information to
17233 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17234
17235 DWARF version 3 added DW_AT_object_pointer, which GCC
17236 4.5 does not yet generate. */
e142c38c 17237 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17238 if (attr != nullptr)
c906108c
SS
17239 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17240 else
9c37b5ae 17241 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17242 arg_type = die_type (child_die, cu);
17243
17244 /* RealView does not mark THIS as const, which the testsuite
17245 expects. GCC marks THIS as const in method definitions,
17246 but not in the class specifications (GCC PR 43053). */
17247 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17248 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17249 {
17250 int is_this = 0;
17251 struct dwarf2_cu *arg_cu = cu;
17252 const char *name = dwarf2_name (child_die, cu);
17253
17254 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17255 if (attr != nullptr)
3ce3b1ba
PA
17256 {
17257 /* If the compiler emits this, use it. */
17258 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17259 is_this = 1;
17260 }
17261 else if (name && strcmp (name, "this") == 0)
17262 /* Function definitions will have the argument names. */
17263 is_this = 1;
17264 else if (name == NULL && iparams == 0)
17265 /* Declarations may not have the names, so like
17266 elsewhere in GDB, assume an artificial first
17267 argument is "this". */
17268 is_this = 1;
17269
17270 if (is_this)
17271 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17272 arg_type, 0);
17273 }
17274
17275 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17276 iparams++;
17277 }
436c571c 17278 child_die = child_die->sibling;
c906108c
SS
17279 }
17280 }
17281
76c10ea2 17282 return ftype;
c906108c
SS
17283}
17284
f792889a 17285static struct type *
e7c27a73 17286read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17287{
5e22e966 17288 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17289 const char *name = NULL;
3c8e0968 17290 struct type *this_type, *target_type;
c906108c 17291
94af9270 17292 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17293 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17294 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17295 set_die_type (die, this_type, cu);
3c8e0968
DE
17296 target_type = die_type (die, cu);
17297 if (target_type != this_type)
17298 TYPE_TARGET_TYPE (this_type) = target_type;
17299 else
17300 {
17301 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17302 spec and cause infinite loops in GDB. */
b98664d3 17303 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17304 "- DIE at %s [in module %s]"),
17305 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17306 TYPE_TARGET_TYPE (this_type) = NULL;
17307 }
e4003a34
TV
17308 if (name == NULL)
17309 {
17310 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17311 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17312 Handle these by just returning the target type, rather than
17313 constructing an anonymous typedef type and trying to handle this
17314 elsewhere. */
17315 set_die_type (die, target_type, cu);
17316 return target_type;
17317 }
f792889a 17318 return this_type;
c906108c
SS
17319}
17320
9b790ce7
UW
17321/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17322 (which may be different from NAME) to the architecture back-end to allow
17323 it to guess the correct format if necessary. */
17324
17325static struct type *
17326dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17327 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17328{
08feed99 17329 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17330 const struct floatformat **format;
17331 struct type *type;
17332
17333 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17334 if (format)
103a685e 17335 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17336 else
77b7c781 17337 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17338
17339 return type;
17340}
17341
eb77c9df
AB
17342/* Allocate an integer type of size BITS and name NAME. */
17343
17344static struct type *
17345dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17346 int bits, int unsigned_p, const char *name)
17347{
17348 struct type *type;
17349
17350 /* Versions of Intel's C Compiler generate an integer type called "void"
17351 instead of using DW_TAG_unspecified_type. This has been seen on
17352 at least versions 14, 17, and 18. */
35ee2dc2
AB
17353 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17354 && strcmp (name, "void") == 0)
eb77c9df
AB
17355 type = objfile_type (objfile)->builtin_void;
17356 else
17357 type = init_integer_type (objfile, bits, unsigned_p, name);
17358
17359 return type;
17360}
17361
8bdc1658
AB
17362/* Initialise and return a floating point type of size BITS suitable for
17363 use as a component of a complex number. The NAME_HINT is passed through
17364 when initialising the floating point type and is the name of the complex
17365 type.
17366
17367 As DWARF doesn't currently provide an explicit name for the components
17368 of a complex number, but it can be helpful to have these components
17369 named, we try to select a suitable name based on the size of the
17370 component. */
17371static struct type *
17372dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17373 struct objfile *objfile,
103a685e
TT
17374 int bits, const char *name_hint,
17375 enum bfd_endian byte_order)
8bdc1658 17376{
08feed99 17377 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17378 struct type *tt = nullptr;
17379
35add35e
AB
17380 /* Try to find a suitable floating point builtin type of size BITS.
17381 We're going to use the name of this type as the name for the complex
17382 target type that we are about to create. */
1db455a7 17383 switch (cu->language)
8bdc1658 17384 {
1db455a7
AB
17385 case language_fortran:
17386 switch (bits)
17387 {
17388 case 32:
17389 tt = builtin_f_type (gdbarch)->builtin_real;
17390 break;
17391 case 64:
17392 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17393 break;
17394 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17395 case 128:
17396 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17397 break;
17398 }
8bdc1658 17399 break;
1db455a7
AB
17400 default:
17401 switch (bits)
17402 {
17403 case 32:
17404 tt = builtin_type (gdbarch)->builtin_float;
17405 break;
17406 case 64:
17407 tt = builtin_type (gdbarch)->builtin_double;
17408 break;
17409 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17410 case 128:
17411 tt = builtin_type (gdbarch)->builtin_long_double;
17412 break;
17413 }
8bdc1658
AB
17414 break;
17415 }
17416
35add35e
AB
17417 /* If the type we found doesn't match the size we were looking for, then
17418 pretend we didn't find a type at all, the complex target type we
17419 create will then be nameless. */
a12e5744 17420 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17421 tt = nullptr;
17422
7d93a1e0 17423 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17424 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17425}
17426
c906108c
SS
17427/* Find a representation of a given base type and install
17428 it in the TYPE field of the die. */
17429
f792889a 17430static struct type *
e7c27a73 17431read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17432{
5e22e966 17433 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17434 struct type *type;
17435 struct attribute *attr;
19f392bc 17436 int encoding = 0, bits = 0;
15d034d0 17437 const char *name;
34877895 17438 gdbarch *arch;
c906108c 17439
e142c38c 17440 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17441 if (attr != nullptr)
34877895 17442 encoding = DW_UNSND (attr);
e142c38c 17443 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17444 if (attr != nullptr)
34877895 17445 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17446 name = dwarf2_name (die, cu);
6ccb9162 17447 if (!name)
34877895 17448 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17449
08feed99 17450 arch = objfile->arch ();
103a685e
TT
17451 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17452
34877895
PJ
17453 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17454 if (attr)
103a685e
TT
17455 {
17456 int endianity = DW_UNSND (attr);
17457
17458 switch (endianity)
17459 {
17460 case DW_END_big:
17461 byte_order = BFD_ENDIAN_BIG;
17462 break;
17463 case DW_END_little:
17464 byte_order = BFD_ENDIAN_LITTLE;
17465 break;
17466 default:
17467 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17468 break;
17469 }
17470 }
6ccb9162
UW
17471
17472 switch (encoding)
c906108c 17473 {
6ccb9162
UW
17474 case DW_ATE_address:
17475 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17476 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17477 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17478 break;
17479 case DW_ATE_boolean:
19f392bc 17480 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17481 break;
17482 case DW_ATE_complex_float:
103a685e
TT
17483 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17484 byte_order);
78134374 17485 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17486 {
17487 if (name == nullptr)
17488 {
17489 struct obstack *obstack
5e22e966 17490 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 17491 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17492 nullptr);
17493 }
17494 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17495 }
17496 else
17497 type = init_complex_type (name, type);
6ccb9162
UW
17498 break;
17499 case DW_ATE_decimal_float:
19f392bc 17500 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17501 break;
17502 case DW_ATE_float:
103a685e 17503 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17504 break;
17505 case DW_ATE_signed:
eb77c9df 17506 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17507 break;
17508 case DW_ATE_unsigned:
3b2b8fea
TT
17509 if (cu->language == language_fortran
17510 && name
61012eef 17511 && startswith (name, "character("))
19f392bc
UW
17512 type = init_character_type (objfile, bits, 1, name);
17513 else
eb77c9df 17514 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17515 break;
17516 case DW_ATE_signed_char:
6e70227d 17517 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17518 || cu->language == language_pascal
17519 || cu->language == language_fortran)
19f392bc
UW
17520 type = init_character_type (objfile, bits, 0, name);
17521 else
eb77c9df 17522 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17523 break;
17524 case DW_ATE_unsigned_char:
868a0084 17525 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17526 || cu->language == language_pascal
c44af4eb
TT
17527 || cu->language == language_fortran
17528 || cu->language == language_rust)
19f392bc
UW
17529 type = init_character_type (objfile, bits, 1, name);
17530 else
eb77c9df 17531 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17532 break;
75079b2b 17533 case DW_ATE_UTF:
53e710ac 17534 {
53e710ac
PA
17535 if (bits == 16)
17536 type = builtin_type (arch)->builtin_char16;
17537 else if (bits == 32)
17538 type = builtin_type (arch)->builtin_char32;
17539 else
17540 {
b98664d3 17541 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17542 bits);
eb77c9df 17543 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17544 }
17545 return set_die_type (die, type, cu);
17546 }
75079b2b
TT
17547 break;
17548
6ccb9162 17549 default:
b98664d3 17550 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17551 dwarf_type_encoding_name (encoding));
77b7c781 17552 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17553 break;
c906108c 17554 }
6ccb9162 17555
0114d602 17556 if (name && strcmp (name, "char") == 0)
876cecd0 17557 TYPE_NOSIGN (type) = 1;
0114d602 17558
2b4424c3
TT
17559 maybe_set_alignment (cu, die, type);
17560
103a685e 17561 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17562
f792889a 17563 return set_die_type (die, type, cu);
c906108c
SS
17564}
17565
80180f79
SA
17566/* Parse dwarf attribute if it's a block, reference or constant and put the
17567 resulting value of the attribute into struct bound_prop.
17568 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17569
17570static int
17571attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17572 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17573 struct type *default_type)
80180f79
SA
17574{
17575 struct dwarf2_property_baton *baton;
5e22e966 17576 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
17577 struct objfile *objfile = per_objfile->objfile;
17578 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 17579
9a49df9d
AB
17580 gdb_assert (default_type != NULL);
17581
80180f79
SA
17582 if (attr == NULL || prop == NULL)
17583 return 0;
17584
4fc6c0d5 17585 if (attr->form_is_block ())
80180f79 17586 {
8d749320 17587 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17588 baton->property_type = default_type;
80180f79 17589 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17590 baton->locexpr.per_objfile = per_objfile;
80180f79
SA
17591 baton->locexpr.size = DW_BLOCK (attr)->size;
17592 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17593 switch (attr->name)
17594 {
17595 case DW_AT_string_length:
17596 baton->locexpr.is_reference = true;
17597 break;
17598 default:
17599 baton->locexpr.is_reference = false;
17600 break;
17601 }
80180f79
SA
17602 prop->data.baton = baton;
17603 prop->kind = PROP_LOCEXPR;
17604 gdb_assert (prop->data.baton != NULL);
17605 }
cd6c91b4 17606 else if (attr->form_is_ref ())
80180f79
SA
17607 {
17608 struct dwarf2_cu *target_cu = cu;
17609 struct die_info *target_die;
17610 struct attribute *target_attr;
17611
17612 target_die = follow_die_ref (die, attr, &target_cu);
17613 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17614 if (target_attr == NULL)
17615 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17616 target_cu);
80180f79
SA
17617 if (target_attr == NULL)
17618 return 0;
17619
df25ebbd 17620 switch (target_attr->name)
80180f79 17621 {
df25ebbd 17622 case DW_AT_location:
cd6c91b4 17623 if (target_attr->form_is_section_offset ())
df25ebbd 17624 {
8d749320 17625 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17626 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17627 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17628 prop->data.baton = baton;
17629 prop->kind = PROP_LOCLIST;
17630 gdb_assert (prop->data.baton != NULL);
17631 }
4fc6c0d5 17632 else if (target_attr->form_is_block ())
df25ebbd 17633 {
8d749320 17634 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17635 baton->property_type = die_type (target_die, target_cu);
df25ebbd 17636 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17637 baton->locexpr.per_objfile = per_objfile;
df25ebbd
JB
17638 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17639 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17640 baton->locexpr.is_reference = true;
df25ebbd
JB
17641 prop->data.baton = baton;
17642 prop->kind = PROP_LOCEXPR;
17643 gdb_assert (prop->data.baton != NULL);
17644 }
17645 else
17646 {
17647 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17648 "dynamic property");
17649 return 0;
17650 }
17651 break;
17652 case DW_AT_data_member_location:
17653 {
17654 LONGEST offset;
17655
17656 if (!handle_data_member_location (target_die, target_cu,
17657 &offset))
17658 return 0;
17659
8d749320 17660 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17661 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17662 target_cu);
df25ebbd
JB
17663 baton->offset_info.offset = offset;
17664 baton->offset_info.type = die_type (target_die, target_cu);
17665 prop->data.baton = baton;
17666 prop->kind = PROP_ADDR_OFFSET;
17667 break;
17668 }
80180f79
SA
17669 }
17670 }
cd6c91b4 17671 else if (attr->form_is_constant ())
80180f79 17672 {
0826b30a 17673 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17674 prop->kind = PROP_CONST;
17675 }
17676 else
17677 {
17678 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17679 dwarf2_name (die, cu));
17680 return 0;
17681 }
17682
17683 return 1;
17684}
17685
09ba997f 17686/* See read.h. */
9a49df9d 17687
09ba997f 17688struct type *
293e7e51 17689dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17690{
9a49df9d
AB
17691 struct type *int_type;
17692
17693 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17694#define TRY_TYPE(F) \
17695 int_type = (unsigned_p \
17696 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17697 : objfile_type (objfile)->builtin_ ## F); \
17698 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17699 return int_type
17700
17701 TRY_TYPE (char);
17702 TRY_TYPE (short);
17703 TRY_TYPE (int);
17704 TRY_TYPE (long);
17705 TRY_TYPE (long_long);
17706
17707#undef TRY_TYPE
17708
17709 gdb_assert_not_reached ("unable to find suitable integer type");
17710}
17711
09ba997f 17712/* See read.h. */
11a8b164 17713
09ba997f 17714struct type *
293e7e51 17715dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 17716{
293e7e51
SM
17717 int addr_size = this->per_cu->addr_size ();
17718 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
17719}
17720
b86352cf
AB
17721/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17722 present (which is valid) then compute the default type based on the
17723 compilation units address size. */
17724
17725static struct type *
17726read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17727{
17728 struct type *index_type = die_type (die, cu);
17729
17730 /* Dwarf-2 specifications explicitly allows to create subrange types
17731 without specifying a base type.
17732 In that case, the base type must be set to the type of
17733 the lower bound, upper bound or count, in that order, if any of these
17734 three attributes references an object that has a type.
17735 If no base type is found, the Dwarf-2 specifications say that
17736 a signed integer type of size equal to the size of an address should
17737 be used.
17738 For the following C code: `extern char gdb_int [];'
17739 GCC produces an empty range DIE.
17740 FIXME: muller/2010-05-28: Possible references to object for low bound,
17741 high bound or count are not yet handled by this code. */
78134374 17742 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 17743 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
17744
17745 return index_type;
17746}
17747
a02abb62
JB
17748/* Read the given DW_AT_subrange DIE. */
17749
f792889a 17750static struct type *
a02abb62
JB
17751read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17752{
4c9ad8c2 17753 struct type *base_type, *orig_base_type;
a02abb62
JB
17754 struct type *range_type;
17755 struct attribute *attr;
729efb13 17756 struct dynamic_prop low, high;
4fae6e18 17757 int low_default_is_valid;
c451ebe5 17758 int high_bound_is_count = 0;
15d034d0 17759 const char *name;
d359392f 17760 ULONGEST negative_mask;
e77813c8 17761
b86352cf
AB
17762 orig_base_type = read_subrange_index_type (die, cu);
17763
4c9ad8c2
TT
17764 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17765 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17766 creating the range type, but we use the result of check_typedef
17767 when examining properties of the type. */
17768 base_type = check_typedef (orig_base_type);
a02abb62 17769
7e314c57
JK
17770 /* The die_type call above may have already set the type for this DIE. */
17771 range_type = get_die_type (die, cu);
17772 if (range_type)
17773 return range_type;
17774
729efb13
SA
17775 low.kind = PROP_CONST;
17776 high.kind = PROP_CONST;
17777 high.data.const_val = 0;
17778
4fae6e18
JK
17779 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17780 omitting DW_AT_lower_bound. */
17781 switch (cu->language)
6e70227d 17782 {
4fae6e18
JK
17783 case language_c:
17784 case language_cplus:
729efb13 17785 low.data.const_val = 0;
4fae6e18
JK
17786 low_default_is_valid = 1;
17787 break;
17788 case language_fortran:
729efb13 17789 low.data.const_val = 1;
4fae6e18
JK
17790 low_default_is_valid = 1;
17791 break;
17792 case language_d:
4fae6e18 17793 case language_objc:
c44af4eb 17794 case language_rust:
729efb13 17795 low.data.const_val = 0;
4fae6e18
JK
17796 low_default_is_valid = (cu->header.version >= 4);
17797 break;
17798 case language_ada:
17799 case language_m2:
17800 case language_pascal:
729efb13 17801 low.data.const_val = 1;
4fae6e18
JK
17802 low_default_is_valid = (cu->header.version >= 4);
17803 break;
17804 default:
729efb13 17805 low.data.const_val = 0;
4fae6e18
JK
17806 low_default_is_valid = 0;
17807 break;
a02abb62
JB
17808 }
17809
e142c38c 17810 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17811 if (attr != nullptr)
9a49df9d 17812 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17813 else if (!low_default_is_valid)
b98664d3 17814 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17815 "- DIE at %s [in module %s]"),
17816 sect_offset_str (die->sect_off),
5e22e966 17817 objfile_name (cu->per_objfile->objfile));
a02abb62 17818
506f5c41
TV
17819 struct attribute *attr_ub, *attr_count;
17820 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17821 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17822 {
506f5c41 17823 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17824 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17825 {
c451ebe5
SA
17826 /* If bounds are constant do the final calculation here. */
17827 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17828 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17829 else
17830 high_bound_is_count = 1;
c2ff108b 17831 }
506f5c41
TV
17832 else
17833 {
17834 if (attr_ub != NULL)
17835 complaint (_("Unresolved DW_AT_upper_bound "
17836 "- DIE at %s [in module %s]"),
17837 sect_offset_str (die->sect_off),
5e22e966 17838 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
17839 if (attr_count != NULL)
17840 complaint (_("Unresolved DW_AT_count "
17841 "- DIE at %s [in module %s]"),
17842 sect_offset_str (die->sect_off),
5e22e966 17843 objfile_name (cu->per_objfile->objfile));
506f5c41 17844 }
e77813c8 17845 }
a02abb62 17846
4e962e74
TT
17847 LONGEST bias = 0;
17848 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17849 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17850 bias = bias_attr->constant_value (0);
4e962e74 17851
dbb9c2b1
JB
17852 /* Normally, the DWARF producers are expected to use a signed
17853 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17854 But this is unfortunately not always the case, as witnessed
17855 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17856 is used instead. To work around that ambiguity, we treat
17857 the bounds as signed, and thus sign-extend their values, when
17858 the base type is signed. */
6e70227d 17859 negative_mask =
d359392f 17860 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17861 if (low.kind == PROP_CONST
17862 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17863 low.data.const_val |= negative_mask;
17864 if (high.kind == PROP_CONST
17865 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17866 high.data.const_val |= negative_mask;
43bbcdc2 17867
5bbd8269
AB
17868 /* Check for bit and byte strides. */
17869 struct dynamic_prop byte_stride_prop;
17870 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17871 if (attr_byte_stride != nullptr)
17872 {
293e7e51 17873 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17874 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17875 prop_type);
17876 }
17877
17878 struct dynamic_prop bit_stride_prop;
17879 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17880 if (attr_bit_stride != nullptr)
17881 {
17882 /* It only makes sense to have either a bit or byte stride. */
17883 if (attr_byte_stride != nullptr)
17884 {
17885 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17886 "- DIE at %s [in module %s]"),
17887 sect_offset_str (die->sect_off),
5e22e966 17888 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
17889 attr_bit_stride = nullptr;
17890 }
17891 else
17892 {
293e7e51 17893 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17894 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17895 prop_type);
17896 }
17897 }
17898
17899 if (attr_byte_stride != nullptr
17900 || attr_bit_stride != nullptr)
17901 {
17902 bool byte_stride_p = (attr_byte_stride != nullptr);
17903 struct dynamic_prop *stride
17904 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17905
17906 range_type
17907 = create_range_type_with_stride (NULL, orig_base_type, &low,
17908 &high, bias, stride, byte_stride_p);
17909 }
17910 else
17911 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17912
c451ebe5
SA
17913 if (high_bound_is_count)
17914 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17915
c2ff108b
JK
17916 /* Ada expects an empty array on no boundary attributes. */
17917 if (attr == NULL && cu->language != language_ada)
729efb13 17918 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17919
39cbfefa
DJ
17920 name = dwarf2_name (die, cu);
17921 if (name)
d0e39ea2 17922 range_type->set_name (name);
6e70227d 17923
e142c38c 17924 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17925 if (attr != nullptr)
a02abb62
JB
17926 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17927
2b4424c3
TT
17928 maybe_set_alignment (cu, die, range_type);
17929
7e314c57
JK
17930 set_die_type (die, range_type, cu);
17931
17932 /* set_die_type should be already done. */
b4ba55a1
JB
17933 set_descriptive_type (range_type, die, cu);
17934
7e314c57 17935 return range_type;
a02abb62 17936}
6e70227d 17937
f792889a 17938static struct type *
81a17f79
JB
17939read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17940{
17941 struct type *type;
81a17f79 17942
5e22e966 17943 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 17944 type->set_name (dwarf2_name (die, cu));
81a17f79 17945
74a2f8ff 17946 /* In Ada, an unspecified type is typically used when the description
85102364 17947 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17948 such a type, we treat it as a stub, and try to resolve it later on,
17949 when needed. */
17950 if (cu->language == language_ada)
17951 TYPE_STUB (type) = 1;
17952
f792889a 17953 return set_die_type (die, type, cu);
81a17f79 17954}
a02abb62 17955
639d11d3
DC
17956/* Read a single die and all its descendents. Set the die's sibling
17957 field to NULL; set other fields in the die correctly, and set all
17958 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17959 location of the info_ptr after reading all of those dies. PARENT
17960 is the parent of the die in question. */
17961
17962static struct die_info *
dee91e82 17963read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17964 const gdb_byte *info_ptr,
17965 const gdb_byte **new_info_ptr,
dee91e82 17966 struct die_info *parent)
639d11d3
DC
17967{
17968 struct die_info *die;
d521ce57 17969 const gdb_byte *cur_ptr;
639d11d3 17970
3e225074 17971 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17972 if (die == NULL)
17973 {
17974 *new_info_ptr = cur_ptr;
17975 return NULL;
17976 }
93311388 17977 store_in_ref_table (die, reader->cu);
639d11d3 17978
3e225074 17979 if (die->has_children)
bf6af496 17980 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17981 else
17982 {
17983 die->child = NULL;
17984 *new_info_ptr = cur_ptr;
17985 }
17986
17987 die->sibling = NULL;
17988 die->parent = parent;
17989 return die;
17990}
17991
17992/* Read a die, all of its descendents, and all of its siblings; set
17993 all of the fields of all of the dies correctly. Arguments are as
17994 in read_die_and_children. */
17995
17996static struct die_info *
bf6af496 17997read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17998 const gdb_byte *info_ptr,
17999 const gdb_byte **new_info_ptr,
bf6af496 18000 struct die_info *parent)
639d11d3
DC
18001{
18002 struct die_info *first_die, *last_sibling;
d521ce57 18003 const gdb_byte *cur_ptr;
639d11d3 18004
c906108c 18005 cur_ptr = info_ptr;
639d11d3
DC
18006 first_die = last_sibling = NULL;
18007
18008 while (1)
c906108c 18009 {
639d11d3 18010 struct die_info *die
dee91e82 18011 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18012
1d325ec1 18013 if (die == NULL)
c906108c 18014 {
639d11d3
DC
18015 *new_info_ptr = cur_ptr;
18016 return first_die;
c906108c 18017 }
1d325ec1
DJ
18018
18019 if (!first_die)
18020 first_die = die;
c906108c 18021 else
1d325ec1
DJ
18022 last_sibling->sibling = die;
18023
18024 last_sibling = die;
c906108c 18025 }
c906108c
SS
18026}
18027
bf6af496
DE
18028/* Read a die, all of its descendents, and all of its siblings; set
18029 all of the fields of all of the dies correctly. Arguments are as
18030 in read_die_and_children.
18031 This the main entry point for reading a DIE and all its children. */
18032
18033static struct die_info *
18034read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18035 const gdb_byte *info_ptr,
18036 const gdb_byte **new_info_ptr,
bf6af496
DE
18037 struct die_info *parent)
18038{
18039 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18040 new_info_ptr, parent);
18041
b4f54984 18042 if (dwarf_die_debug)
bf6af496
DE
18043 {
18044 fprintf_unfiltered (gdb_stdlog,
18045 "Read die from %s@0x%x of %s:\n",
96b79293 18046 reader->die_section->get_name (),
bf6af496
DE
18047 (unsigned) (info_ptr - reader->die_section->buffer),
18048 bfd_get_filename (reader->abfd));
b4f54984 18049 dump_die (die, dwarf_die_debug);
bf6af496
DE
18050 }
18051
18052 return die;
18053}
18054
3019eac3
DE
18055/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18056 attributes.
18057 The caller is responsible for filling in the extra attributes
18058 and updating (*DIEP)->num_attrs.
18059 Set DIEP to point to a newly allocated die with its information,
3e225074 18060 except for its child, sibling, and parent fields. */
93311388 18061
d521ce57 18062static const gdb_byte *
3019eac3 18063read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18064 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18065 int num_extra_attrs)
93311388 18066{
b64f50a1 18067 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18068 struct abbrev_info *abbrev;
18069 struct die_info *die;
18070 struct dwarf2_cu *cu = reader->cu;
18071 bfd *abfd = reader->abfd;
18072
9c541725 18073 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18074 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18075 info_ptr += bytes_read;
18076 if (!abbrev_number)
18077 {
18078 *diep = NULL;
93311388
DE
18079 return info_ptr;
18080 }
18081
685af9cd 18082 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18083 if (!abbrev)
348e048f
DE
18084 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18085 abbrev_number,
18086 bfd_get_filename (abfd));
18087
3019eac3 18088 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18089 die->sect_off = sect_off;
93311388
DE
18090 die->tag = abbrev->tag;
18091 die->abbrev = abbrev_number;
3e225074 18092 die->has_children = abbrev->has_children;
93311388 18093
3019eac3
DE
18094 /* Make the result usable.
18095 The caller needs to update num_attrs after adding the extra
18096 attributes. */
93311388
DE
18097 die->num_attrs = abbrev->num_attrs;
18098
18a8505e 18099 std::vector<int> indexes_that_need_reprocess;
93311388 18100 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18101 {
18102 bool need_reprocess;
18103 info_ptr =
18104 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18105 info_ptr, &need_reprocess);
18106 if (need_reprocess)
18107 indexes_that_need_reprocess.push_back (i);
18108 }
18109
052c8bb8 18110 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
18111 if (attr != nullptr)
18112 cu->str_offsets_base = DW_UNSND (attr);
93311388 18113
41144253 18114 attr = die->attr (DW_AT_loclists_base);
18115 if (attr != nullptr)
18116 cu->loclist_base = DW_UNSND (attr);
18117
a39fdb41 18118 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18119 if (maybe_addr_base.has_value ())
18120 cu->addr_base = *maybe_addr_base;
18121 for (int index : indexes_that_need_reprocess)
18122 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 18123 *diep = die;
93311388
DE
18124 return info_ptr;
18125}
18126
3019eac3
DE
18127/* Read a die and all its attributes.
18128 Set DIEP to point to a newly allocated die with its information,
3e225074 18129 except for its child, sibling, and parent fields. */
3019eac3 18130
d521ce57 18131static const gdb_byte *
3019eac3 18132read_full_die (const struct die_reader_specs *reader,
3e225074 18133 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18134{
d521ce57 18135 const gdb_byte *result;
bf6af496 18136
3e225074 18137 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18138
b4f54984 18139 if (dwarf_die_debug)
bf6af496
DE
18140 {
18141 fprintf_unfiltered (gdb_stdlog,
18142 "Read die from %s@0x%x of %s:\n",
96b79293 18143 reader->die_section->get_name (),
bf6af496
DE
18144 (unsigned) (info_ptr - reader->die_section->buffer),
18145 bfd_get_filename (reader->abfd));
b4f54984 18146 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18147 }
18148
18149 return result;
3019eac3 18150}
433df2d4 18151\f
c906108c 18152
72bf9492
DJ
18153/* Returns nonzero if TAG represents a type that we might generate a partial
18154 symbol for. */
18155
18156static int
18157is_type_tag_for_partial (int tag)
18158{
18159 switch (tag)
18160 {
18161#if 0
18162 /* Some types that would be reasonable to generate partial symbols for,
18163 that we don't at present. */
18164 case DW_TAG_array_type:
18165 case DW_TAG_file_type:
18166 case DW_TAG_ptr_to_member_type:
18167 case DW_TAG_set_type:
18168 case DW_TAG_string_type:
18169 case DW_TAG_subroutine_type:
18170#endif
18171 case DW_TAG_base_type:
18172 case DW_TAG_class_type:
680b30c7 18173 case DW_TAG_interface_type:
72bf9492
DJ
18174 case DW_TAG_enumeration_type:
18175 case DW_TAG_structure_type:
18176 case DW_TAG_subrange_type:
18177 case DW_TAG_typedef:
18178 case DW_TAG_union_type:
18179 return 1;
18180 default:
18181 return 0;
18182 }
18183}
18184
18185/* Load all DIEs that are interesting for partial symbols into memory. */
18186
18187static struct partial_die_info *
dee91e82 18188load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18189 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18190{
dee91e82 18191 struct dwarf2_cu *cu = reader->cu;
5e22e966 18192 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18193 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18194 unsigned int bytes_read;
5afb4e99 18195 unsigned int load_all = 0;
72bf9492
DJ
18196 int nesting_level = 1;
18197
18198 parent_die = NULL;
18199 last_die = NULL;
18200
7adf1e79
DE
18201 gdb_assert (cu->per_cu != NULL);
18202 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18203 load_all = 1;
18204
72bf9492
DJ
18205 cu->partial_dies
18206 = htab_create_alloc_ex (cu->header.length / 12,
18207 partial_die_hash,
18208 partial_die_eq,
18209 NULL,
18210 &cu->comp_unit_obstack,
18211 hashtab_obstack_allocate,
18212 dummy_obstack_deallocate);
18213
72bf9492
DJ
18214 while (1)
18215 {
685af9cd 18216 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18217
18218 /* A NULL abbrev means the end of a series of children. */
18219 if (abbrev == NULL)
18220 {
18221 if (--nesting_level == 0)
cd9983dd
YQ
18222 return first_die;
18223
72bf9492
DJ
18224 info_ptr += bytes_read;
18225 last_die = parent_die;
18226 parent_die = parent_die->die_parent;
18227 continue;
18228 }
18229
98bfdba5
PA
18230 /* Check for template arguments. We never save these; if
18231 they're seen, we just mark the parent, and go on our way. */
18232 if (parent_die != NULL
18233 && cu->language == language_cplus
18234 && (abbrev->tag == DW_TAG_template_type_param
18235 || abbrev->tag == DW_TAG_template_value_param))
18236 {
18237 parent_die->has_template_arguments = 1;
18238
18239 if (!load_all)
18240 {
18241 /* We don't need a partial DIE for the template argument. */
dee91e82 18242 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18243 continue;
18244 }
18245 }
18246
0d99eb77 18247 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18248 Skip their other children. */
18249 if (!load_all
18250 && cu->language == language_cplus
18251 && parent_die != NULL
f9b5d5ea
TV
18252 && parent_die->tag == DW_TAG_subprogram
18253 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 18254 {
dee91e82 18255 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18256 continue;
18257 }
18258
5afb4e99
DJ
18259 /* Check whether this DIE is interesting enough to save. Normally
18260 we would not be interested in members here, but there may be
18261 later variables referencing them via DW_AT_specification (for
18262 static members). */
18263 if (!load_all
18264 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18265 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18266 && abbrev->tag != DW_TAG_enumerator
18267 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18268 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18269 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18270 && abbrev->tag != DW_TAG_variable
5afb4e99 18271 && abbrev->tag != DW_TAG_namespace
f55ee35c 18272 && abbrev->tag != DW_TAG_module
95554aad 18273 && abbrev->tag != DW_TAG_member
74921315
KS
18274 && abbrev->tag != DW_TAG_imported_unit
18275 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18276 {
18277 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18278 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18279 continue;
18280 }
18281
6f06d47b
YQ
18282 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18283 abbrev);
cd9983dd 18284
48fbe735 18285 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18286
18287 /* This two-pass algorithm for processing partial symbols has a
18288 high cost in cache pressure. Thus, handle some simple cases
18289 here which cover the majority of C partial symbols. DIEs
18290 which neither have specification tags in them, nor could have
18291 specification tags elsewhere pointing at them, can simply be
18292 processed and discarded.
18293
18294 This segment is also optional; scan_partial_symbols and
18295 add_partial_symbol will handle these DIEs if we chain
18296 them in normally. When compilers which do not emit large
18297 quantities of duplicate debug information are more common,
18298 this code can probably be removed. */
18299
18300 /* Any complete simple types at the top level (pretty much all
18301 of them, for a language without namespaces), can be processed
18302 directly. */
18303 if (parent_die == NULL
cd9983dd
YQ
18304 && pdi.has_specification == 0
18305 && pdi.is_declaration == 0
18306 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18307 || pdi.tag == DW_TAG_base_type
18308 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18309 {
7d00ffec 18310 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
18311 add_partial_symbol (&pdi, cu);
18312
cd9983dd 18313 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18314 continue;
18315 }
18316
d8228535
JK
18317 /* The exception for DW_TAG_typedef with has_children above is
18318 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18319 type_name_or_error will error on such types later.
d8228535
JK
18320
18321 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18322 it could not find the child DIEs referenced later, this is checked
18323 above. In correct DWARF DW_TAG_typedef should have no children. */
18324
cd9983dd 18325 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18326 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18327 "- DIE at %s [in module %s]"),
cd9983dd 18328 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18329
72bf9492
DJ
18330 /* If we're at the second level, and we're an enumerator, and
18331 our parent has no specification (meaning possibly lives in a
18332 namespace elsewhere), then we can add the partial symbol now
18333 instead of queueing it. */
cd9983dd 18334 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18335 && parent_die != NULL
18336 && parent_die->die_parent == NULL
18337 && parent_die->tag == DW_TAG_enumeration_type
18338 && parent_die->has_specification == 0)
18339 {
7d00ffec 18340 if (pdi.raw_name == NULL)
b98664d3 18341 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18342 else if (building_psymtab)
f0fbb768 18343 add_partial_symbol (&pdi, cu);
72bf9492 18344
cd9983dd 18345 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18346 continue;
18347 }
18348
cd9983dd 18349 struct partial_die_info *part_die
6f06d47b 18350 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18351
72bf9492
DJ
18352 /* We'll save this DIE so link it in. */
18353 part_die->die_parent = parent_die;
18354 part_die->die_sibling = NULL;
18355 part_die->die_child = NULL;
18356
18357 if (last_die && last_die == parent_die)
18358 last_die->die_child = part_die;
18359 else if (last_die)
18360 last_die->die_sibling = part_die;
18361
18362 last_die = part_die;
18363
18364 if (first_die == NULL)
18365 first_die = part_die;
18366
18367 /* Maybe add the DIE to the hash table. Not all DIEs that we
18368 find interesting need to be in the hash table, because we
18369 also have the parent/sibling/child chains; only those that we
18370 might refer to by offset later during partial symbol reading.
18371
18372 For now this means things that might have be the target of a
18373 DW_AT_specification, DW_AT_abstract_origin, or
18374 DW_AT_extension. DW_AT_extension will refer only to
18375 namespaces; DW_AT_abstract_origin refers to functions (and
18376 many things under the function DIE, but we do not recurse
18377 into function DIEs during partial symbol reading) and
18378 possibly variables as well; DW_AT_specification refers to
18379 declarations. Declarations ought to have the DW_AT_declaration
18380 flag. It happens that GCC forgets to put it in sometimes, but
18381 only for functions, not for types.
18382
18383 Adding more things than necessary to the hash table is harmless
18384 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18385 wasted time in find_partial_die, when we reread the compilation
18386 unit with load_all_dies set. */
72bf9492 18387
5afb4e99 18388 if (load_all
72929c62 18389 || abbrev->tag == DW_TAG_constant
5afb4e99 18390 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18391 || abbrev->tag == DW_TAG_variable
18392 || abbrev->tag == DW_TAG_namespace
18393 || part_die->is_declaration)
18394 {
18395 void **slot;
18396
18397 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18398 to_underlying (part_die->sect_off),
18399 INSERT);
72bf9492
DJ
18400 *slot = part_die;
18401 }
18402
72bf9492 18403 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18404 we have no reason to follow the children of structures; for other
98bfdba5
PA
18405 languages we have to, so that we can get at method physnames
18406 to infer fully qualified class names, for DW_AT_specification,
18407 and for C++ template arguments. For C++, we also look one level
18408 inside functions to find template arguments (if the name of the
18409 function does not already contain the template arguments).
bc30ff58 18410
0a4b0913
AB
18411 For Ada and Fortran, we need to scan the children of subprograms
18412 and lexical blocks as well because these languages allow the
18413 definition of nested entities that could be interesting for the
18414 debugger, such as nested subprograms for instance. */
72bf9492 18415 if (last_die->has_children
5afb4e99
DJ
18416 && (load_all
18417 || last_die->tag == DW_TAG_namespace
f55ee35c 18418 || last_die->tag == DW_TAG_module
72bf9492 18419 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18420 || (cu->language == language_cplus
18421 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
18422 && (last_die->raw_name == NULL
18423 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
18424 || (cu->language != language_c
18425 && (last_die->tag == DW_TAG_class_type
680b30c7 18426 || last_die->tag == DW_TAG_interface_type
72bf9492 18427 || last_die->tag == DW_TAG_structure_type
bc30ff58 18428 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18429 || ((cu->language == language_ada
18430 || cu->language == language_fortran)
bc30ff58
JB
18431 && (last_die->tag == DW_TAG_subprogram
18432 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18433 {
18434 nesting_level++;
18435 parent_die = last_die;
18436 continue;
18437 }
18438
18439 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18440 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18441
18442 /* Back to the top, do it again. */
18443 }
18444}
18445
6f06d47b
YQ
18446partial_die_info::partial_die_info (sect_offset sect_off_,
18447 struct abbrev_info *abbrev)
18448 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18449{
18450}
18451
7d00ffec
TT
18452/* See class definition. */
18453
18454const char *
18455partial_die_info::name (dwarf2_cu *cu)
18456{
18457 if (!canonical_name && raw_name != nullptr)
18458 {
18459 struct objfile *objfile = cu->per_objfile->objfile;
18460 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
18461 canonical_name = 1;
18462 }
18463
18464 return raw_name;
18465}
18466
35cc7ed7
YQ
18467/* Read a minimal amount of information into the minimal die structure.
18468 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18469
48fbe735
YQ
18470const gdb_byte *
18471partial_die_info::read (const struct die_reader_specs *reader,
18472 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18473{
dee91e82 18474 struct dwarf2_cu *cu = reader->cu;
976ca316 18475 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 18476 unsigned int i;
c5aa993b 18477 int has_low_pc_attr = 0;
c906108c 18478 int has_high_pc_attr = 0;
91da1414 18479 int high_pc_relative = 0;
c906108c 18480
fd0a254f 18481 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18482 {
e7da7f8f 18483 attribute attr;
18a8505e 18484 bool need_reprocess;
e7da7f8f 18485 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
18486 info_ptr, &need_reprocess);
18487 /* String and address offsets that need to do the reprocessing have
18488 already been read at this point, so there is no need to wait until
18489 the loop terminates to do the reprocessing. */
18490 if (need_reprocess)
e7da7f8f 18491 read_attribute_reprocess (reader, &attr);
c906108c 18492 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18493 partial symbol table. */
c906108c
SS
18494 switch (attr.name)
18495 {
18496 case DW_AT_name:
48fbe735 18497 switch (tag)
71c25dea
TT
18498 {
18499 case DW_TAG_compile_unit:
95554aad 18500 case DW_TAG_partial_unit:
348e048f 18501 case DW_TAG_type_unit:
71c25dea
TT
18502 /* Compilation units have a DW_AT_name that is a filename, not
18503 a source language identifier. */
18504 case DW_TAG_enumeration_type:
18505 case DW_TAG_enumerator:
18506 /* These tags always have simple identifiers already; no need
18507 to canonicalize them. */
7d00ffec
TT
18508 canonical_name = 1;
18509 raw_name = DW_STRING (&attr);
71c25dea
TT
18510 break;
18511 default:
7d00ffec
TT
18512 canonical_name = 0;
18513 raw_name = DW_STRING (&attr);
71c25dea
TT
18514 break;
18515 }
c906108c 18516 break;
31ef98ae 18517 case DW_AT_linkage_name:
c906108c 18518 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18519 /* Note that both forms of linkage name might appear. We
18520 assume they will be the same, and we only store the last
18521 one we see. */
e61108c9 18522 linkage_name = attr.value_as_string ();
787de330
TT
18523 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18524 See https://github.com/rust-lang/rust/issues/32925. */
18525 if (cu->language == language_rust && linkage_name != NULL
18526 && strchr (linkage_name, '{') != NULL)
18527 linkage_name = NULL;
c906108c
SS
18528 break;
18529 case DW_AT_low_pc:
18530 has_low_pc_attr = 1;
cd6c91b4 18531 lowpc = attr.value_as_address ();
c906108c
SS
18532 break;
18533 case DW_AT_high_pc:
18534 has_high_pc_attr = 1;
cd6c91b4
TT
18535 highpc = attr.value_as_address ();
18536 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18537 high_pc_relative = 1;
c906108c
SS
18538 break;
18539 case DW_AT_location:
0963b4bd 18540 /* Support the .debug_loc offsets. */
4fc6c0d5 18541 if (attr.form_is_block ())
8e19ed76 18542 {
48fbe735 18543 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18544 }
cd6c91b4 18545 else if (attr.form_is_section_offset ())
8e19ed76 18546 {
4d3c2250 18547 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18548 }
18549 else
18550 {
4d3c2250
KB
18551 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18552 "partial symbol information");
8e19ed76 18553 }
c906108c 18554 break;
c906108c 18555 case DW_AT_external:
48fbe735 18556 is_external = DW_UNSND (&attr);
c906108c
SS
18557 break;
18558 case DW_AT_declaration:
48fbe735 18559 is_declaration = DW_UNSND (&attr);
c906108c
SS
18560 break;
18561 case DW_AT_type:
48fbe735 18562 has_type = 1;
c906108c
SS
18563 break;
18564 case DW_AT_abstract_origin:
18565 case DW_AT_specification:
72bf9492 18566 case DW_AT_extension:
48fbe735 18567 has_specification = 1;
0826b30a 18568 spec_offset = attr.get_ref_die_offset ();
48fbe735 18569 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18570 || cu->per_cu->is_dwz);
c906108c
SS
18571 break;
18572 case DW_AT_sibling:
18573 /* Ignore absolute siblings, they might point outside of
18574 the current compile unit. */
18575 if (attr.form == DW_FORM_ref_addr)
b98664d3 18576 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18577 else
b9502d3f 18578 {
48fbe735 18579 const gdb_byte *buffer = reader->buffer;
0826b30a 18580 sect_offset off = attr.get_ref_die_offset ();
9c541725 18581 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18582
18583 if (sibling_ptr < info_ptr)
b98664d3 18584 complaint (_("DW_AT_sibling points backwards"));
22869d73 18585 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18586 reader->die_section->overflow_complaint ();
b9502d3f 18587 else
48fbe735 18588 sibling = sibling_ptr;
b9502d3f 18589 }
c906108c 18590 break;
fa4028e9 18591 case DW_AT_byte_size:
48fbe735 18592 has_byte_size = 1;
fa4028e9 18593 break;
ff908ebf 18594 case DW_AT_const_value:
48fbe735 18595 has_const_value = 1;
ff908ebf 18596 break;
68511cec
CES
18597 case DW_AT_calling_convention:
18598 /* DWARF doesn't provide a way to identify a program's source-level
18599 entry point. DW_AT_calling_convention attributes are only meant
18600 to describe functions' calling conventions.
18601
18602 However, because it's a necessary piece of information in
0c1b455e
TT
18603 Fortran, and before DWARF 4 DW_CC_program was the only
18604 piece of debugging information whose definition refers to
18605 a 'main program' at all, several compilers marked Fortran
18606 main programs with DW_CC_program --- even when those
18607 functions use the standard calling conventions.
18608
18609 Although DWARF now specifies a way to provide this
18610 information, we support this practice for backward
18611 compatibility. */
68511cec 18612 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18613 && cu->language == language_fortran)
48fbe735 18614 main_subprogram = 1;
68511cec 18615 break;
481860b3
GB
18616 case DW_AT_inline:
18617 if (DW_UNSND (&attr) == DW_INL_inlined
18618 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18619 may_be_inlined = 1;
481860b3 18620 break;
95554aad
TT
18621
18622 case DW_AT_import:
48fbe735 18623 if (tag == DW_TAG_imported_unit)
36586728 18624 {
0826b30a 18625 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18626 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18627 || cu->per_cu->is_dwz);
18628 }
95554aad
TT
18629 break;
18630
0c1b455e 18631 case DW_AT_main_subprogram:
48fbe735 18632 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18633 break;
18634
05caa1d2
TT
18635 case DW_AT_ranges:
18636 {
18637 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18638 but that requires a full DIE, so instead we just
18639 reimplement it. */
18640 int need_ranges_base = tag != DW_TAG_compile_unit;
18641 unsigned int ranges_offset = (DW_UNSND (&attr)
18642 + (need_ranges_base
18643 ? cu->ranges_base
18644 : 0));
18645
18646 /* Value of the DW_AT_ranges attribute is the offset in the
18647 .debug_ranges section. */
18648 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18649 nullptr))
18650 has_pc_info = 1;
18651 }
18652 break;
18653
c906108c
SS
18654 default:
18655 break;
18656 }
18657 }
18658
10d06d82
TT
18659 /* For Ada, if both the name and the linkage name appear, we prefer
18660 the latter. This lets "catch exception" work better, regardless
18661 of the order in which the name and linkage name were emitted.
18662 Really, though, this is just a workaround for the fact that gdb
18663 doesn't store both the name and the linkage name. */
18664 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 18665 raw_name = linkage_name;
10d06d82 18666
91da1414 18667 if (high_pc_relative)
48fbe735 18668 highpc += lowpc;
91da1414 18669
9373cf26
JK
18670 if (has_low_pc_attr && has_high_pc_attr)
18671 {
18672 /* When using the GNU linker, .gnu.linkonce. sections are used to
18673 eliminate duplicate copies of functions and vtables and such.
18674 The linker will arbitrarily choose one and discard the others.
18675 The AT_*_pc values for such functions refer to local labels in
18676 these sections. If the section from that file was discarded, the
18677 labels are not in the output, so the relocs get a value of 0.
18678 If this is a discarded function, mark the pc bounds as invalid,
18679 so that GDB will ignore it. */
976ca316 18680 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 18681 {
976ca316 18682 struct objfile *objfile = per_objfile->objfile;
08feed99 18683 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18684
b98664d3 18685 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18686 "for DIE at %s [in module %s]"),
48fbe735
YQ
18687 paddress (gdbarch, lowpc),
18688 sect_offset_str (sect_off),
9d8780f0 18689 objfile_name (objfile));
9373cf26
JK
18690 }
18691 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18692 else if (lowpc >= highpc)
9373cf26 18693 {
976ca316 18694 struct objfile *objfile = per_objfile->objfile;
08feed99 18695 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18696
b98664d3 18697 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18698 "for DIE at %s [in module %s]"),
48fbe735
YQ
18699 paddress (gdbarch, lowpc),
18700 paddress (gdbarch, highpc),
18701 sect_offset_str (sect_off),
9c541725 18702 objfile_name (objfile));
9373cf26
JK
18703 }
18704 else
48fbe735 18705 has_pc_info = 1;
9373cf26 18706 }
85cbf3d3 18707
c906108c
SS
18708 return info_ptr;
18709}
18710
72bf9492
DJ
18711/* Find a cached partial DIE at OFFSET in CU. */
18712
d590ff25
YQ
18713struct partial_die_info *
18714dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18715{
18716 struct partial_die_info *lookup_die = NULL;
6f06d47b 18717 struct partial_die_info part_die (sect_off);
72bf9492 18718
9a3c8263 18719 lookup_die = ((struct partial_die_info *)
d590ff25 18720 htab_find_with_hash (partial_dies, &part_die,
9c541725 18721 to_underlying (sect_off)));
72bf9492 18722
72bf9492
DJ
18723 return lookup_die;
18724}
18725
348e048f
DE
18726/* Find a partial DIE at OFFSET, which may or may not be in CU,
18727 except in the case of .debug_types DIEs which do not reference
18728 outside their CU (they do however referencing other types via
55f1336d 18729 DW_FORM_ref_sig8). */
72bf9492 18730
122cf0f2 18731static const struct cu_partial_die_info
9c541725 18732find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18733{
976ca316
SM
18734 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18735 struct objfile *objfile = per_objfile->objfile;
5afb4e99 18736 struct partial_die_info *pd = NULL;
72bf9492 18737
36586728 18738 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18739 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18740 {
d590ff25 18741 pd = cu->find_partial_die (sect_off);
5afb4e99 18742 if (pd != NULL)
fb816e8b 18743 return { cu, pd };
0d99eb77
DE
18744 /* We missed recording what we needed.
18745 Load all dies and try again. */
5afb4e99 18746 }
0d99eb77
DE
18747 else
18748 {
18749 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18750 if (cu->per_cu->is_debug_types)
0d99eb77 18751 {
9d8780f0
SM
18752 error (_("Dwarf Error: Type Unit at offset %s contains"
18753 " external reference to offset %s [in module %s].\n"),
18754 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18755 bfd_get_filename (objfile->obfd));
18756 }
7188ed02
SM
18757 dwarf2_per_cu_data *per_cu
18758 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 18759 per_objfile);
72bf9492 18760
976ca316 18761 cu = per_objfile->get_cu (per_cu);
7188ed02 18762 if (cu == NULL || cu->partial_dies == NULL)
976ca316 18763 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 18764
976ca316 18765 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
18766
18767 cu->last_used = 0;
18768 pd = cu->find_partial_die (sect_off);
0d99eb77 18769 }
5afb4e99 18770
dee91e82
DE
18771 /* If we didn't find it, and not all dies have been loaded,
18772 load them all and try again. */
18773
7188ed02 18774 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 18775 {
7188ed02 18776 cu->per_cu->load_all_dies = 1;
fd820528
DE
18777
18778 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18779 THIS_CU->cu may already be in use. So we can't just free it and
18780 replace its DIEs with the ones we read in. Instead, we leave those
18781 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18782 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18783 set. */
976ca316 18784 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 18785
7188ed02 18786 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
18787 }
18788
18789 if (pd == NULL)
18790 internal_error (__FILE__, __LINE__,
9d8780f0 18791 _("could not find partial DIE %s "
3e43a32a 18792 "in cache [from module %s]\n"),
9d8780f0 18793 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 18794 return { cu, pd };
72bf9492
DJ
18795}
18796
abc72ce4
DE
18797/* See if we can figure out if the class lives in a namespace. We do
18798 this by looking for a member function; its demangled name will
18799 contain namespace info, if there is any. */
18800
18801static void
18802guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18803 struct dwarf2_cu *cu)
18804{
18805 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18806 what template types look like, because the demangler
18807 frequently doesn't give the same name as the debug info. We
18808 could fix this by only using the demangled name to get the
18809 prefix (but see comment in read_structure_type). */
18810
18811 struct partial_die_info *real_pdi;
18812 struct partial_die_info *child_pdi;
18813
18814 /* If this DIE (this DIE's specification, if any) has a parent, then
18815 we should not do this. We'll prepend the parent's fully qualified
18816 name when we create the partial symbol. */
18817
18818 real_pdi = struct_pdi;
18819 while (real_pdi->has_specification)
fb816e8b 18820 {
122cf0f2
AB
18821 auto res = find_partial_die (real_pdi->spec_offset,
18822 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18823 real_pdi = res.pdi;
18824 cu = res.cu;
18825 }
abc72ce4
DE
18826
18827 if (real_pdi->die_parent != NULL)
18828 return;
18829
18830 for (child_pdi = struct_pdi->die_child;
18831 child_pdi != NULL;
18832 child_pdi = child_pdi->die_sibling)
18833 {
18834 if (child_pdi->tag == DW_TAG_subprogram
18835 && child_pdi->linkage_name != NULL)
18836 {
43816ebc
TT
18837 gdb::unique_xmalloc_ptr<char> actual_class_name
18838 (language_class_name_from_physname (cu->language_defn,
18839 child_pdi->linkage_name));
abc72ce4
DE
18840 if (actual_class_name != NULL)
18841 {
5e22e966 18842 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
18843 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
18844 struct_pdi->canonical_name = 1;
abc72ce4
DE
18845 }
18846 break;
18847 }
18848 }
18849}
18850
25c11aca
TV
18851/* Return true if a DIE with TAG may have the DW_AT_const_value
18852 attribute. */
18853
18854static bool
18855can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18856{
18857 switch (tag)
18858 {
18859 case DW_TAG_constant:
18860 case DW_TAG_enumerator:
18861 case DW_TAG_formal_parameter:
18862 case DW_TAG_template_value_param:
18863 case DW_TAG_variable:
18864 return true;
18865 }
18866
18867 return false;
18868}
18869
52356b79
YQ
18870void
18871partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18872{
abc72ce4
DE
18873 /* Once we've fixed up a die, there's no point in doing so again.
18874 This also avoids a memory leak if we were to call
18875 guess_partial_die_structure_name multiple times. */
52356b79 18876 if (fixup_called)
abc72ce4
DE
18877 return;
18878
72bf9492
DJ
18879 /* If we found a reference attribute and the DIE has no name, try
18880 to find a name in the referred to DIE. */
18881
7d00ffec 18882 if (raw_name == NULL && has_specification)
72bf9492
DJ
18883 {
18884 struct partial_die_info *spec_die;
72bf9492 18885
122cf0f2 18886 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18887 spec_die = res.pdi;
18888 cu = res.cu;
72bf9492 18889
52356b79 18890 spec_die->fixup (cu);
72bf9492 18891
7d00ffec 18892 if (spec_die->raw_name)
72bf9492 18893 {
7d00ffec
TT
18894 raw_name = spec_die->raw_name;
18895 canonical_name = spec_die->canonical_name;
72bf9492
DJ
18896
18897 /* Copy DW_AT_external attribute if it is set. */
18898 if (spec_die->is_external)
52356b79 18899 is_external = spec_die->is_external;
72bf9492
DJ
18900 }
18901 }
18902
25c11aca
TV
18903 if (!has_const_value && has_specification
18904 && can_have_DW_AT_const_value_p (tag))
18905 {
18906 struct partial_die_info *spec_die;
18907
18908 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18909 spec_die = res.pdi;
18910 cu = res.cu;
18911
18912 spec_die->fixup (cu);
18913
18914 if (spec_die->has_const_value)
18915 {
18916 /* Copy DW_AT_const_value attribute if it is set. */
18917 has_const_value = spec_die->has_const_value;
18918 }
18919 }
18920
72bf9492 18921 /* Set default names for some unnamed DIEs. */
72bf9492 18922
7d00ffec
TT
18923 if (raw_name == NULL && tag == DW_TAG_namespace)
18924 {
18925 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
18926 canonical_name = 1;
18927 }
72bf9492 18928
abc72ce4
DE
18929 /* If there is no parent die to provide a namespace, and there are
18930 children, see if we can determine the namespace from their linkage
122d1940 18931 name. */
abc72ce4 18932 if (cu->language == language_cplus
5e22e966 18933 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
18934 && die_parent == NULL
18935 && has_children
18936 && (tag == DW_TAG_class_type
18937 || tag == DW_TAG_structure_type
18938 || tag == DW_TAG_union_type))
18939 guess_partial_die_structure_name (this, cu);
abc72ce4 18940
53832f31
TT
18941 /* GCC might emit a nameless struct or union that has a linkage
18942 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 18943 if (raw_name == NULL
52356b79
YQ
18944 && (tag == DW_TAG_class_type
18945 || tag == DW_TAG_interface_type
18946 || tag == DW_TAG_structure_type
18947 || tag == DW_TAG_union_type)
18948 && linkage_name != NULL)
53832f31 18949 {
43816ebc
TT
18950 gdb::unique_xmalloc_ptr<char> demangled
18951 (gdb_demangle (linkage_name, DMGL_TYPES));
18952 if (demangled != nullptr)
53832f31 18953 {
96408a79
SA
18954 const char *base;
18955
18956 /* Strip any leading namespaces/classes, keep only the base name.
18957 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18958 base = strrchr (demangled.get (), ':');
18959 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18960 base++;
18961 else
43816ebc 18962 base = demangled.get ();
96408a79 18963
5e22e966 18964 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
18965 raw_name = objfile->intern (base);
18966 canonical_name = 1;
53832f31
TT
18967 }
18968 }
18969
52356b79 18970 fixup_called = 1;
72bf9492
DJ
18971}
18972
41144253 18973/* Read the .debug_loclists header contents from the given SECTION in the
18974 HEADER. */
18975static void
18976read_loclist_header (struct loclist_header *header,
18977 struct dwarf2_section_info *section)
18978{
18979 unsigned int bytes_read;
18980 bfd *abfd = section->get_bfd_owner ();
18981 const gdb_byte *info_ptr = section->buffer;
18982 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18983 info_ptr += bytes_read;
18984 header->version = read_2_bytes (abfd, info_ptr);
18985 info_ptr += 2;
18986 header->addr_size = read_1_byte (abfd, info_ptr);
18987 info_ptr += 1;
18988 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18989 info_ptr += 1;
18990 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18991}
18992
18993/* Return the DW_AT_loclists_base value for the CU. */
18994static ULONGEST
18995lookup_loclist_base (struct dwarf2_cu *cu)
18996{
18997 /* For the .dwo unit, the loclist_base points to the first offset following
18998 the header. The header consists of the following entities-
18999 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19000 bit format)
19001 2. version (2 bytes)
19002 3. address size (1 byte)
19003 4. segment selector size (1 byte)
19004 5. offset entry count (4 bytes)
19005 These sizes are derived as per the DWARFv5 standard. */
19006 if (cu->dwo_unit != nullptr)
19007 {
19008 if (cu->header.initial_length_size == 4)
19009 return LOCLIST_HEADER_SIZE32;
19010 return LOCLIST_HEADER_SIZE64;
19011 }
19012 return cu->loclist_base;
19013}
19014
19015/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19016 array of offsets in the .debug_loclists section. */
19017static CORE_ADDR
19018read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19019{
976ca316
SM
19020 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19021 struct objfile *objfile = per_objfile->objfile;
41144253 19022 bfd *abfd = objfile->obfd;
19023 ULONGEST loclist_base = lookup_loclist_base (cu);
19024 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19025
19026 section->read (objfile);
19027 if (section->buffer == NULL)
19028 complaint (_("DW_FORM_loclistx used without .debug_loclists "
19029 "section [in module %s]"), objfile_name (objfile));
19030 struct loclist_header header;
19031 read_loclist_header (&header, section);
19032 if (loclist_index >= header.offset_entry_count)
19033 complaint (_("DW_FORM_loclistx pointing outside of "
19034 ".debug_loclists offset array [in module %s]"),
19035 objfile_name (objfile));
19036 if (loclist_base + loclist_index * cu->header.offset_size
19037 >= section->size)
19038 complaint (_("DW_FORM_loclistx pointing outside of "
19039 ".debug_loclists section [in module %s]"),
19040 objfile_name (objfile));
19041 const gdb_byte *info_ptr
19042 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
19043
19044 if (cu->header.offset_size == 4)
19045 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19046 else
19047 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19048}
19049
18a8505e
AT
19050/* Process the attributes that had to be skipped in the first round. These
19051 attributes are the ones that need str_offsets_base or addr_base attributes.
19052 They could not have been processed in the first round, because at the time
19053 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
19054static void
19055read_attribute_reprocess (const struct die_reader_specs *reader,
19056 struct attribute *attr)
18a8505e
AT
19057{
19058 struct dwarf2_cu *cu = reader->cu;
19059 switch (attr->form)
19060 {
19061 case DW_FORM_addrx:
19062 case DW_FORM_GNU_addr_index:
19063 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19064 break;
41144253 19065 case DW_FORM_loclistx:
19066 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19067 break;
18a8505e
AT
19068 case DW_FORM_strx:
19069 case DW_FORM_strx1:
19070 case DW_FORM_strx2:
19071 case DW_FORM_strx3:
19072 case DW_FORM_strx4:
19073 case DW_FORM_GNU_str_index:
19074 {
19075 unsigned int str_index = DW_UNSND (attr);
19076 if (reader->dwo_file != NULL)
19077 {
19078 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19079 DW_STRING_IS_CANONICAL (attr) = 0;
19080 }
19081 else
19082 {
19083 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19084 DW_STRING_IS_CANONICAL (attr) = 0;
19085 }
19086 break;
19087 }
19088 default:
19089 gdb_assert_not_reached (_("Unexpected DWARF form."));
19090 }
19091}
19092
a8329558 19093/* Read an attribute value described by an attribute form. */
c906108c 19094
d521ce57 19095static const gdb_byte *
dee91e82
DE
19096read_attribute_value (const struct die_reader_specs *reader,
19097 struct attribute *attr, unsigned form,
18a8505e
AT
19098 LONGEST implicit_const, const gdb_byte *info_ptr,
19099 bool *need_reprocess)
c906108c 19100{
dee91e82 19101 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
19102 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19103 struct objfile *objfile = per_objfile->objfile;
dee91e82 19104 bfd *abfd = reader->abfd;
e7c27a73 19105 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19106 unsigned int bytes_read;
19107 struct dwarf_block *blk;
18a8505e 19108 *need_reprocess = false;
c906108c 19109
aead7601 19110 attr->form = (enum dwarf_form) form;
a8329558 19111 switch (form)
c906108c 19112 {
c906108c 19113 case DW_FORM_ref_addr:
ae411497 19114 if (cu->header.version == 2)
c8a7a66f
TT
19115 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19116 &bytes_read);
ae411497 19117 else
8266302d
TT
19118 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19119 &bytes_read);
ae411497
TT
19120 info_ptr += bytes_read;
19121 break;
36586728 19122 case DW_FORM_GNU_ref_alt:
8266302d 19123 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
19124 info_ptr += bytes_read;
19125 break;
ae411497 19126 case DW_FORM_addr:
08feed99
TT
19127 {
19128 struct gdbarch *gdbarch = objfile->arch ();
19129 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19130 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19131 info_ptr += bytes_read;
19132 }
c906108c
SS
19133 break;
19134 case DW_FORM_block2:
7b5a2f43 19135 blk = dwarf_alloc_block (cu);
c906108c
SS
19136 blk->size = read_2_bytes (abfd, info_ptr);
19137 info_ptr += 2;
19138 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19139 info_ptr += blk->size;
19140 DW_BLOCK (attr) = blk;
19141 break;
19142 case DW_FORM_block4:
7b5a2f43 19143 blk = dwarf_alloc_block (cu);
c906108c
SS
19144 blk->size = read_4_bytes (abfd, info_ptr);
19145 info_ptr += 4;
19146 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19147 info_ptr += blk->size;
19148 DW_BLOCK (attr) = blk;
19149 break;
19150 case DW_FORM_data2:
19151 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19152 info_ptr += 2;
19153 break;
19154 case DW_FORM_data4:
19155 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19156 info_ptr += 4;
19157 break;
19158 case DW_FORM_data8:
19159 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19160 info_ptr += 8;
19161 break;
0224619f
JK
19162 case DW_FORM_data16:
19163 blk = dwarf_alloc_block (cu);
19164 blk->size = 16;
19165 blk->data = read_n_bytes (abfd, info_ptr, 16);
19166 info_ptr += 16;
19167 DW_BLOCK (attr) = blk;
19168 break;
2dc7f7b3 19169 case DW_FORM_sec_offset:
8266302d 19170 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
19171 info_ptr += bytes_read;
19172 break;
41144253 19173 case DW_FORM_loclistx:
19174 {
19175 *need_reprocess = true;
19176 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19177 info_ptr += bytes_read;
19178 }
19179 break;
c906108c 19180 case DW_FORM_string:
9b1c24c8 19181 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19182 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19183 info_ptr += bytes_read;
19184 break;
4bdf3d34 19185 case DW_FORM_strp:
36586728
TT
19186 if (!cu->per_cu->is_dwz)
19187 {
976ca316 19188 DW_STRING (attr) = read_indirect_string (per_objfile,
ed2dc618 19189 abfd, info_ptr, cu_header,
36586728
TT
19190 &bytes_read);
19191 DW_STRING_IS_CANONICAL (attr) = 0;
19192 info_ptr += bytes_read;
19193 break;
19194 }
19195 /* FALLTHROUGH */
43988095
JK
19196 case DW_FORM_line_strp:
19197 if (!cu->per_cu->is_dwz)
19198 {
976ca316
SM
19199 DW_STRING (attr) = per_objfile->read_line_string (info_ptr, cu_header,
19200 &bytes_read);
43988095
JK
19201 DW_STRING_IS_CANONICAL (attr) = 0;
19202 info_ptr += bytes_read;
19203 break;
19204 }
19205 /* FALLTHROUGH */
36586728
TT
19206 case DW_FORM_GNU_strp_alt:
19207 {
976ca316 19208 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8266302d
TT
19209 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19210 &bytes_read);
36586728 19211
0314b390 19212 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
19213 DW_STRING_IS_CANONICAL (attr) = 0;
19214 info_ptr += bytes_read;
19215 }
4bdf3d34 19216 break;
2dc7f7b3 19217 case DW_FORM_exprloc:
c906108c 19218 case DW_FORM_block:
7b5a2f43 19219 blk = dwarf_alloc_block (cu);
c906108c
SS
19220 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19221 info_ptr += bytes_read;
19222 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19223 info_ptr += blk->size;
19224 DW_BLOCK (attr) = blk;
19225 break;
19226 case DW_FORM_block1:
7b5a2f43 19227 blk = dwarf_alloc_block (cu);
c906108c
SS
19228 blk->size = read_1_byte (abfd, info_ptr);
19229 info_ptr += 1;
19230 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19231 info_ptr += blk->size;
19232 DW_BLOCK (attr) = blk;
19233 break;
19234 case DW_FORM_data1:
19235 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19236 info_ptr += 1;
19237 break;
19238 case DW_FORM_flag:
19239 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19240 info_ptr += 1;
19241 break;
2dc7f7b3
TT
19242 case DW_FORM_flag_present:
19243 DW_UNSND (attr) = 1;
19244 break;
c906108c
SS
19245 case DW_FORM_sdata:
19246 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19247 info_ptr += bytes_read;
19248 break;
19249 case DW_FORM_udata:
18a8505e 19250 case DW_FORM_rnglistx:
c906108c
SS
19251 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19252 info_ptr += bytes_read;
19253 break;
19254 case DW_FORM_ref1:
9c541725 19255 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19256 + read_1_byte (abfd, info_ptr));
c906108c
SS
19257 info_ptr += 1;
19258 break;
19259 case DW_FORM_ref2:
9c541725 19260 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19261 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19262 info_ptr += 2;
19263 break;
19264 case DW_FORM_ref4:
9c541725 19265 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19266 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19267 info_ptr += 4;
19268 break;
613e1657 19269 case DW_FORM_ref8:
9c541725 19270 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19271 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19272 info_ptr += 8;
19273 break;
55f1336d 19274 case DW_FORM_ref_sig8:
ac9ec31b 19275 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19276 info_ptr += 8;
19277 break;
c906108c 19278 case DW_FORM_ref_udata:
9c541725 19279 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19280 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19281 info_ptr += bytes_read;
19282 break;
c906108c 19283 case DW_FORM_indirect:
a8329558
KW
19284 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19285 info_ptr += bytes_read;
43988095
JK
19286 if (form == DW_FORM_implicit_const)
19287 {
19288 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19289 info_ptr += bytes_read;
19290 }
19291 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19292 info_ptr, need_reprocess);
43988095
JK
19293 break;
19294 case DW_FORM_implicit_const:
19295 DW_SND (attr) = implicit_const;
a8329558 19296 break;
336d760d 19297 case DW_FORM_addrx:
3019eac3 19298 case DW_FORM_GNU_addr_index:
18a8505e
AT
19299 *need_reprocess = true;
19300 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19301 info_ptr += bytes_read;
19302 break;
cf532bd1 19303 case DW_FORM_strx:
15f18d14
AT
19304 case DW_FORM_strx1:
19305 case DW_FORM_strx2:
19306 case DW_FORM_strx3:
19307 case DW_FORM_strx4:
3019eac3 19308 case DW_FORM_GNU_str_index:
3019eac3 19309 {
15f18d14
AT
19310 ULONGEST str_index;
19311 if (form == DW_FORM_strx1)
19312 {
19313 str_index = read_1_byte (abfd, info_ptr);
19314 info_ptr += 1;
19315 }
19316 else if (form == DW_FORM_strx2)
19317 {
19318 str_index = read_2_bytes (abfd, info_ptr);
19319 info_ptr += 2;
19320 }
19321 else if (form == DW_FORM_strx3)
19322 {
19323 str_index = read_3_bytes (abfd, info_ptr);
19324 info_ptr += 3;
19325 }
19326 else if (form == DW_FORM_strx4)
19327 {
19328 str_index = read_4_bytes (abfd, info_ptr);
19329 info_ptr += 4;
19330 }
19331 else
19332 {
19333 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19334 info_ptr += bytes_read;
19335 }
18a8505e
AT
19336 *need_reprocess = true;
19337 DW_UNSND (attr) = str_index;
19338 }
3019eac3 19339 break;
c906108c 19340 default:
8a3fe4f8 19341 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19342 dwarf_form_name (form),
19343 bfd_get_filename (abfd));
c906108c 19344 }
28e94949 19345
36586728 19346 /* Super hack. */
cd6c91b4 19347 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19348 attr->form = DW_FORM_GNU_ref_alt;
19349
28e94949
JB
19350 /* We have seen instances where the compiler tried to emit a byte
19351 size attribute of -1 which ended up being encoded as an unsigned
19352 0xffffffff. Although 0xffffffff is technically a valid size value,
19353 an object of this size seems pretty unlikely so we can relatively
19354 safely treat these cases as if the size attribute was invalid and
19355 treat them as zero by default. */
19356 if (attr->name == DW_AT_byte_size
19357 && form == DW_FORM_data4
19358 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19359 {
19360 complaint
b98664d3 19361 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19362 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19363 DW_UNSND (attr) = 0;
19364 }
28e94949 19365
c906108c
SS
19366 return info_ptr;
19367}
19368
a8329558
KW
19369/* Read an attribute described by an abbreviated attribute. */
19370
d521ce57 19371static const gdb_byte *
dee91e82
DE
19372read_attribute (const struct die_reader_specs *reader,
19373 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19374 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19375{
19376 attr->name = abbrev->name;
43988095 19377 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19378 abbrev->implicit_const, info_ptr,
19379 need_reprocess);
a8329558
KW
19380}
19381
43988095
JK
19382/* Return pointer to string at .debug_str offset STR_OFFSET. */
19383
19384static const char *
976ca316 19385read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 19386 LONGEST str_offset)
43988095 19387{
976ca316
SM
19388 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19389 str_offset, "DW_FORM_strp");
c906108c
SS
19390}
19391
43988095
JK
19392/* Return pointer to string at .debug_str offset as read from BUF.
19393 BUF is assumed to be in a compilation unit described by CU_HEADER.
19394 Return *BYTES_READ_PTR count of bytes read from BUF. */
19395
d521ce57 19396static const char *
976ca316 19397read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 19398 const gdb_byte *buf,
cf2c3c16
TT
19399 const struct comp_unit_head *cu_header,
19400 unsigned int *bytes_read_ptr)
19401{
8266302d 19402 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19403
976ca316 19404 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
19405}
19406
86c0bb4c 19407/* See read.h. */
43988095 19408
86c0bb4c
TT
19409const char *
19410dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
19411 const struct comp_unit_head *cu_header,
19412 unsigned int *bytes_read_ptr)
43988095 19413{
86c0bb4c 19414 bfd *abfd = objfile->obfd;
8266302d 19415 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19416
5989a64e 19417 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19418}
19419
3019eac3 19420/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19421 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19422 ADDR_SIZE is the size of addresses from the CU header. */
19423
19424static CORE_ADDR
976ca316
SM
19425read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
19426 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 19427{
976ca316 19428 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
19429 bfd *abfd = objfile->obfd;
19430 const gdb_byte *info_ptr;
18a8505e 19431 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19432
976ca316
SM
19433 per_objfile->per_bfd->addr.read (objfile);
19434 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 19435 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19436 objfile_name (objfile));
18a8505e 19437 if (addr_base_or_zero + addr_index * addr_size
976ca316 19438 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
19439 error (_("DW_FORM_addr_index pointing outside of "
19440 ".debug_addr section [in module %s]"),
4262abfb 19441 objfile_name (objfile));
976ca316
SM
19442 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
19443 + addr_index * addr_size);
3019eac3
DE
19444 if (addr_size == 4)
19445 return bfd_get_32 (abfd, info_ptr);
19446 else
19447 return bfd_get_64 (abfd, info_ptr);
19448}
19449
19450/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19451
19452static CORE_ADDR
19453read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19454{
5e22e966 19455 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 19456 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19457}
19458
19459/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19460
19461static CORE_ADDR
d521ce57 19462read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19463 unsigned int *bytes_read)
19464{
5e22e966 19465 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
19466 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19467
19468 return read_addr_index (cu, addr_index);
19469}
19470
450a1bfc 19471/* See read.h. */
3019eac3
DE
19472
19473CORE_ADDR
82ca3f51 19474dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 19475 dwarf2_per_objfile *per_objfile,
82ca3f51 19476 unsigned int addr_index)
3019eac3 19477{
976ca316 19478 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 19479 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19480 int addr_size;
19481
3019eac3
DE
19482 /* We need addr_base and addr_size.
19483 If we don't have PER_CU->cu, we have to get it.
19484 Nasty, but the alternative is storing the needed info in PER_CU,
19485 which at this point doesn't seem justified: it's not clear how frequently
19486 it would get used and it would increase the size of every PER_CU.
19487 Entry points like dwarf2_per_cu_addr_size do a similar thing
19488 so we're not in uncharted territory here.
19489 Alas we need to be a bit more complicated as addr_base is contained
19490 in the DIE.
19491
19492 We don't need to read the entire CU(/TU).
19493 We just need the header and top level die.
a1b64ce1 19494
3019eac3 19495 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19496 For now we skip this optimization. */
3019eac3
DE
19497
19498 if (cu != NULL)
19499 {
19500 addr_base = cu->addr_base;
19501 addr_size = cu->header.addr_size;
19502 }
19503 else
19504 {
976ca316 19505 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
19506 addr_base = reader.cu->addr_base;
19507 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19508 }
19509
976ca316 19510 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
19511}
19512
18a8505e
AT
19513/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19514 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19515 DWO file. */
3019eac3 19516
d521ce57 19517static const char *
18a8505e
AT
19518read_str_index (struct dwarf2_cu *cu,
19519 struct dwarf2_section_info *str_section,
19520 struct dwarf2_section_info *str_offsets_section,
19521 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19522{
976ca316
SM
19523 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19524 struct objfile *objfile = per_objfile->objfile;
c5164cbc 19525 const char *objf_name = objfile_name (objfile);
3019eac3 19526 bfd *abfd = objfile->obfd;
d521ce57 19527 const gdb_byte *info_ptr;
3019eac3 19528 ULONGEST str_offset;
cf532bd1 19529 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19530
96b79293
TT
19531 str_section->read (objfile);
19532 str_offsets_section->read (objfile);
73869dc2 19533 if (str_section->buffer == NULL)
18a8505e 19534 error (_("%s used without %s section"
9d8780f0 19535 " in CU at offset %s [in module %s]"),
96b79293 19536 form_name, str_section->get_name (),
18a8505e 19537 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19538 if (str_offsets_section->buffer == NULL)
18a8505e 19539 error (_("%s used without %s section"
9d8780f0 19540 " in CU at offset %s [in module %s]"),
96b79293 19541 form_name, str_section->get_name (),
18a8505e 19542 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19543 info_ptr = (str_offsets_section->buffer
18a8505e 19544 + str_offsets_base
3019eac3
DE
19545 + str_index * cu->header.offset_size);
19546 if (cu->header.offset_size == 4)
19547 str_offset = bfd_get_32 (abfd, info_ptr);
19548 else
19549 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19550 if (str_offset >= str_section->size)
57d63ce2 19551 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19552 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19553 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19554 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19555}
19556
18a8505e
AT
19557/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19558
19559static const char *
19560read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19561{
19562 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19563 ? reader->cu->header.addr_size : 0;
19564 return read_str_index (reader->cu,
19565 &reader->dwo_file->sections.str,
19566 &reader->dwo_file->sections.str_offsets,
19567 str_offsets_base, str_index);
19568}
19569
19570/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19571
19572static const char *
19573read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19574{
5e22e966 19575 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
19576 const char *objf_name = objfile_name (objfile);
19577 static const char form_name[] = "DW_FORM_GNU_str_index";
19578 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19579
19580 if (!cu->str_offsets_base.has_value ())
19581 error (_("%s used in Fission stub without %s"
19582 " in CU at offset 0x%lx [in module %s]"),
19583 form_name, str_offsets_attr_name,
19584 (long) cu->header.offset_size, objf_name);
19585
19586 return read_str_index (cu,
5e22e966
SM
19587 &cu->per_objfile->per_bfd->str,
19588 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
19589 *cu->str_offsets_base, str_index);
19590}
19591
3019eac3
DE
19592/* Return the length of an LEB128 number in BUF. */
19593
19594static int
19595leb128_size (const gdb_byte *buf)
19596{
19597 const gdb_byte *begin = buf;
19598 gdb_byte byte;
19599
19600 while (1)
19601 {
19602 byte = *buf++;
19603 if ((byte & 128) == 0)
19604 return buf - begin;
19605 }
19606}
19607
c906108c 19608static void
e142c38c 19609set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19610{
19611 switch (lang)
19612 {
19613 case DW_LANG_C89:
76bee0cc 19614 case DW_LANG_C99:
0cfd832f 19615 case DW_LANG_C11:
c906108c 19616 case DW_LANG_C:
d1be3247 19617 case DW_LANG_UPC:
e142c38c 19618 cu->language = language_c;
c906108c 19619 break;
9c37b5ae 19620 case DW_LANG_Java:
c906108c 19621 case DW_LANG_C_plus_plus:
0cfd832f
MW
19622 case DW_LANG_C_plus_plus_11:
19623 case DW_LANG_C_plus_plus_14:
e142c38c 19624 cu->language = language_cplus;
c906108c 19625 break;
6aecb9c2
JB
19626 case DW_LANG_D:
19627 cu->language = language_d;
19628 break;
c906108c
SS
19629 case DW_LANG_Fortran77:
19630 case DW_LANG_Fortran90:
b21b22e0 19631 case DW_LANG_Fortran95:
f7de9aab
MW
19632 case DW_LANG_Fortran03:
19633 case DW_LANG_Fortran08:
e142c38c 19634 cu->language = language_fortran;
c906108c 19635 break;
a766d390
DE
19636 case DW_LANG_Go:
19637 cu->language = language_go;
19638 break;
c906108c 19639 case DW_LANG_Mips_Assembler:
e142c38c 19640 cu->language = language_asm;
c906108c
SS
19641 break;
19642 case DW_LANG_Ada83:
8aaf0b47 19643 case DW_LANG_Ada95:
bc5f45f8
JB
19644 cu->language = language_ada;
19645 break;
72019c9c
GM
19646 case DW_LANG_Modula2:
19647 cu->language = language_m2;
19648 break;
fe8e67fd
PM
19649 case DW_LANG_Pascal83:
19650 cu->language = language_pascal;
19651 break;
22566fbd
DJ
19652 case DW_LANG_ObjC:
19653 cu->language = language_objc;
19654 break;
c44af4eb
TT
19655 case DW_LANG_Rust:
19656 case DW_LANG_Rust_old:
19657 cu->language = language_rust;
19658 break;
c906108c
SS
19659 case DW_LANG_Cobol74:
19660 case DW_LANG_Cobol85:
c906108c 19661 default:
e142c38c 19662 cu->language = language_minimal;
c906108c
SS
19663 break;
19664 }
e142c38c 19665 cu->language_defn = language_def (cu->language);
c906108c
SS
19666}
19667
19668/* Return the named attribute or NULL if not there. */
19669
19670static struct attribute *
e142c38c 19671dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19672{
a48e046c 19673 for (;;)
c906108c 19674 {
a48e046c
TT
19675 unsigned int i;
19676 struct attribute *spec = NULL;
19677
19678 for (i = 0; i < die->num_attrs; ++i)
19679 {
19680 if (die->attrs[i].name == name)
19681 return &die->attrs[i];
19682 if (die->attrs[i].name == DW_AT_specification
19683 || die->attrs[i].name == DW_AT_abstract_origin)
19684 spec = &die->attrs[i];
19685 }
19686
19687 if (!spec)
19688 break;
c906108c 19689
f2f0e013 19690 die = follow_die_ref (die, spec, &cu);
f2f0e013 19691 }
c5aa993b 19692
c906108c
SS
19693 return NULL;
19694}
19695
7d45c7c3
KB
19696/* Return the string associated with a string-typed attribute, or NULL if it
19697 is either not found or is of an incorrect type. */
19698
19699static const char *
19700dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19701{
19702 struct attribute *attr;
19703 const char *str = NULL;
19704
19705 attr = dwarf2_attr (die, name, cu);
19706
19707 if (attr != NULL)
19708 {
e61108c9
TT
19709 str = attr->value_as_string ();
19710 if (str == nullptr)
b98664d3 19711 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19712 "DIE at %s in module %s"),
19713 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 19714 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
19715 }
19716
19717 return str;
19718}
19719
a084a2a6 19720/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19721 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19722static const char *
19723dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19724{
19725 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19726 if (dwo_name == nullptr)
19727 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19728 return dwo_name;
19729}
19730
05cf31d1
JB
19731/* Return non-zero iff the attribute NAME is defined for the given DIE,
19732 and holds a non-zero value. This function should only be used for
2dc7f7b3 19733 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19734
19735static int
19736dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19737{
19738 struct attribute *attr = dwarf2_attr (die, name, cu);
19739
19740 return (attr && DW_UNSND (attr));
19741}
19742
3ca72b44 19743static int
e142c38c 19744die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19745{
05cf31d1
JB
19746 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19747 which value is non-zero. However, we have to be careful with
19748 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19749 (via dwarf2_flag_true_p) follows this attribute. So we may
19750 end up accidently finding a declaration attribute that belongs
19751 to a different DIE referenced by the specification attribute,
19752 even though the given DIE does not have a declaration attribute. */
19753 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19754 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19755}
19756
63d06c5c 19757/* Return the die giving the specification for DIE, if there is
f2f0e013 19758 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19759 containing the return value on output. If there is no
19760 specification, but there is an abstract origin, that is
19761 returned. */
63d06c5c
DC
19762
19763static struct die_info *
f2f0e013 19764die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19765{
f2f0e013
DJ
19766 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19767 *spec_cu);
63d06c5c 19768
edb3359d
DJ
19769 if (spec_attr == NULL)
19770 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19771
63d06c5c
DC
19772 if (spec_attr == NULL)
19773 return NULL;
19774 else
f2f0e013 19775 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19776}
c906108c 19777
527f3840
JK
19778/* Stub for free_line_header to match void * callback types. */
19779
19780static void
19781free_line_header_voidp (void *arg)
19782{
9a3c8263 19783 struct line_header *lh = (struct line_header *) arg;
527f3840 19784
fff8551c 19785 delete lh;
527f3840
JK
19786}
19787
83769d0b 19788/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19789
19790static struct dwarf2_section_info *
19791get_debug_line_section (struct dwarf2_cu *cu)
19792{
19793 struct dwarf2_section_info *section;
976ca316 19794 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
19795
19796 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19797 DWO file. */
19798 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19799 section = &cu->dwo_unit->dwo_file->sections.line;
19800 else if (cu->per_cu->is_dwz)
19801 {
976ca316 19802 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
36586728
TT
19803
19804 section = &dwz->line;
19805 }
19806 else
976ca316 19807 section = &per_objfile->per_bfd->line;
36586728
TT
19808
19809 return section;
19810}
19811
debd256d 19812/* Read the statement program header starting at OFFSET in
3019eac3 19813 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19814 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19815 Returns NULL if there is a problem reading the header, e.g., if it
19816 has a version we don't understand.
debd256d
JB
19817
19818 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19819 the returned object point into the dwarf line section buffer,
19820 and must not be freed. */
ae2de4f8 19821
fff8551c 19822static line_header_up
9c541725 19823dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19824{
3019eac3 19825 struct dwarf2_section_info *section;
976ca316 19826 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 19827
36586728 19828 section = get_debug_line_section (cu);
976ca316 19829 section->read (per_objfile->objfile);
3019eac3 19830 if (section->buffer == NULL)
debd256d 19831 {
3019eac3 19832 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19833 complaint (_("missing .debug_line.dwo section"));
3019eac3 19834 else
b98664d3 19835 complaint (_("missing .debug_line section"));
debd256d
JB
19836 return 0;
19837 }
19838
0df7ad3a 19839 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 19840 per_objfile, section, &cu->header);
debd256d 19841}
c906108c 19842
c6da4cef 19843/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19844 Return the file name of the psymtab for the given file_entry.
c6da4cef 19845 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19846 If space for the result is malloc'd, *NAME_HOLDER will be set.
19847 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19848
d521ce57 19849static const char *
7ba99d21 19850psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19851 const dwarf2_psymtab *pst,
c89b44cd
TT
19852 const char *comp_dir,
19853 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19854{
d521ce57
TT
19855 const char *include_name = fe.name;
19856 const char *include_name_to_compare = include_name;
72b9f47f 19857 const char *pst_filename;
c6da4cef
DE
19858 int file_is_pst;
19859
8c43009f 19860 const char *dir_name = fe.include_dir (lh);
c6da4cef 19861
c89b44cd 19862 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19863 if (!IS_ABSOLUTE_PATH (include_name)
19864 && (dir_name != NULL || comp_dir != NULL))
19865 {
19866 /* Avoid creating a duplicate psymtab for PST.
19867 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19868 Before we do the comparison, however, we need to account
19869 for DIR_NAME and COMP_DIR.
19870 First prepend dir_name (if non-NULL). If we still don't
19871 have an absolute path prepend comp_dir (if non-NULL).
19872 However, the directory we record in the include-file's
19873 psymtab does not contain COMP_DIR (to match the
19874 corresponding symtab(s)).
19875
19876 Example:
19877
19878 bash$ cd /tmp
19879 bash$ gcc -g ./hello.c
19880 include_name = "hello.c"
19881 dir_name = "."
19882 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19883 DW_AT_name = "./hello.c"
19884
19885 */
c6da4cef
DE
19886
19887 if (dir_name != NULL)
19888 {
c89b44cd
TT
19889 name_holder->reset (concat (dir_name, SLASH_STRING,
19890 include_name, (char *) NULL));
19891 include_name = name_holder->get ();
c6da4cef 19892 include_name_to_compare = include_name;
c6da4cef
DE
19893 }
19894 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19895 {
c89b44cd
TT
19896 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19897 include_name, (char *) NULL));
19898 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19899 }
19900 }
19901
19902 pst_filename = pst->filename;
c89b44cd 19903 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19904 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19905 {
c89b44cd
TT
19906 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19907 pst_filename, (char *) NULL));
19908 pst_filename = copied_name.get ();
c6da4cef
DE
19909 }
19910
1e3fad37 19911 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19912
c6da4cef
DE
19913 if (file_is_pst)
19914 return NULL;
19915 return include_name;
19916}
19917
d9b3de22
DE
19918/* State machine to track the state of the line number program. */
19919
6f77053d 19920class lnp_state_machine
d9b3de22 19921{
6f77053d
PA
19922public:
19923 /* Initialize a machine state for the start of a line number
19924 program. */
804d2729
TT
19925 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19926 bool record_lines_p);
6f77053d 19927
8c43009f
PA
19928 file_entry *current_file ()
19929 {
19930 /* lh->file_names is 0-based, but the file name numbers in the
19931 statement program are 1-based. */
6f77053d
PA
19932 return m_line_header->file_name_at (m_file);
19933 }
19934
19935 /* Record the line in the state machine. END_SEQUENCE is true if
19936 we're processing the end of a sequence. */
19937 void record_line (bool end_sequence);
19938
7ab6656f
OJ
19939 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19940 nop-out rest of the lines in this sequence. */
6f77053d
PA
19941 void check_line_address (struct dwarf2_cu *cu,
19942 const gdb_byte *line_ptr,
7ab6656f 19943 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19944
19945 void handle_set_discriminator (unsigned int discriminator)
19946 {
19947 m_discriminator = discriminator;
19948 m_line_has_non_zero_discriminator |= discriminator != 0;
19949 }
19950
19951 /* Handle DW_LNE_set_address. */
19952 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19953 {
19954 m_op_index = 0;
19955 address += baseaddr;
19956 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19957 }
19958
19959 /* Handle DW_LNS_advance_pc. */
19960 void handle_advance_pc (CORE_ADDR adjust);
19961
19962 /* Handle a special opcode. */
19963 void handle_special_opcode (unsigned char op_code);
19964
19965 /* Handle DW_LNS_advance_line. */
19966 void handle_advance_line (int line_delta)
19967 {
19968 advance_line (line_delta);
19969 }
19970
19971 /* Handle DW_LNS_set_file. */
19972 void handle_set_file (file_name_index file);
19973
19974 /* Handle DW_LNS_negate_stmt. */
19975 void handle_negate_stmt ()
19976 {
19977 m_is_stmt = !m_is_stmt;
19978 }
19979
19980 /* Handle DW_LNS_const_add_pc. */
19981 void handle_const_add_pc ();
19982
19983 /* Handle DW_LNS_fixed_advance_pc. */
19984 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19985 {
19986 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19987 m_op_index = 0;
19988 }
19989
19990 /* Handle DW_LNS_copy. */
19991 void handle_copy ()
19992 {
19993 record_line (false);
19994 m_discriminator = 0;
19995 }
19996
19997 /* Handle DW_LNE_end_sequence. */
19998 void handle_end_sequence ()
19999 {
804d2729 20000 m_currently_recording_lines = true;
6f77053d
PA
20001 }
20002
20003private:
20004 /* Advance the line by LINE_DELTA. */
20005 void advance_line (int line_delta)
20006 {
20007 m_line += line_delta;
20008
20009 if (line_delta != 0)
20010 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20011 }
20012
804d2729
TT
20013 struct dwarf2_cu *m_cu;
20014
6f77053d
PA
20015 gdbarch *m_gdbarch;
20016
20017 /* True if we're recording lines.
20018 Otherwise we're building partial symtabs and are just interested in
20019 finding include files mentioned by the line number program. */
20020 bool m_record_lines_p;
20021
8c43009f 20022 /* The line number header. */
6f77053d 20023 line_header *m_line_header;
8c43009f 20024
6f77053d
PA
20025 /* These are part of the standard DWARF line number state machine,
20026 and initialized according to the DWARF spec. */
d9b3de22 20027
6f77053d 20028 unsigned char m_op_index = 0;
7ba99d21
AT
20029 /* The line table index of the current file. */
20030 file_name_index m_file = 1;
6f77053d
PA
20031 unsigned int m_line = 1;
20032
20033 /* These are initialized in the constructor. */
20034
20035 CORE_ADDR m_address;
20036 bool m_is_stmt;
20037 unsigned int m_discriminator;
d9b3de22
DE
20038
20039 /* Additional bits of state we need to track. */
20040
20041 /* The last file that we called dwarf2_start_subfile for.
20042 This is only used for TLLs. */
6f77053d 20043 unsigned int m_last_file = 0;
d9b3de22 20044 /* The last file a line number was recorded for. */
6f77053d 20045 struct subfile *m_last_subfile = NULL;
d9b3de22 20046
1313c56e
AB
20047 /* The address of the last line entry. */
20048 CORE_ADDR m_last_address;
20049
20050 /* Set to true when a previous line at the same address (using
20051 m_last_address) had m_is_stmt true. This is reset to false when a
20052 line entry at a new address (m_address different to m_last_address) is
20053 processed. */
20054 bool m_stmt_at_address = false;
20055
804d2729
TT
20056 /* When true, record the lines we decode. */
20057 bool m_currently_recording_lines = false;
d9b3de22
DE
20058
20059 /* The last line number that was recorded, used to coalesce
20060 consecutive entries for the same line. This can happen, for
20061 example, when discriminators are present. PR 17276. */
6f77053d
PA
20062 unsigned int m_last_line = 0;
20063 bool m_line_has_non_zero_discriminator = false;
8c43009f 20064};
d9b3de22 20065
6f77053d
PA
20066void
20067lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20068{
20069 CORE_ADDR addr_adj = (((m_op_index + adjust)
20070 / m_line_header->maximum_ops_per_instruction)
20071 * m_line_header->minimum_instruction_length);
20072 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20073 m_op_index = ((m_op_index + adjust)
20074 % m_line_header->maximum_ops_per_instruction);
20075}
d9b3de22 20076
6f77053d
PA
20077void
20078lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20079{
6f77053d 20080 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
20081 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20082 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20083 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
20084 / m_line_header->maximum_ops_per_instruction)
20085 * m_line_header->minimum_instruction_length);
20086 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 20087 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 20088 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20089
258bf0ee 20090 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20091 advance_line (line_delta);
20092 record_line (false);
20093 m_discriminator = 0;
20094}
d9b3de22 20095
6f77053d
PA
20096void
20097lnp_state_machine::handle_set_file (file_name_index file)
20098{
20099 m_file = file;
20100
20101 const file_entry *fe = current_file ();
20102 if (fe == NULL)
20103 dwarf2_debug_line_missing_file_complaint ();
20104 else if (m_record_lines_p)
20105 {
20106 const char *dir = fe->include_dir (m_line_header);
20107
c24bdb02 20108 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20109 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20110 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20111 }
20112}
20113
20114void
20115lnp_state_machine::handle_const_add_pc ()
20116{
20117 CORE_ADDR adjust
20118 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20119
20120 CORE_ADDR addr_adj
20121 = (((m_op_index + adjust)
20122 / m_line_header->maximum_ops_per_instruction)
20123 * m_line_header->minimum_instruction_length);
20124
20125 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20126 m_op_index = ((m_op_index + adjust)
20127 % m_line_header->maximum_ops_per_instruction);
20128}
d9b3de22 20129
a05a36a5
DE
20130/* Return non-zero if we should add LINE to the line number table.
20131 LINE is the line to add, LAST_LINE is the last line that was added,
20132 LAST_SUBFILE is the subfile for LAST_LINE.
20133 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20134 had a non-zero discriminator.
20135
20136 We have to be careful in the presence of discriminators.
20137 E.g., for this line:
20138
20139 for (i = 0; i < 100000; i++);
20140
20141 clang can emit four line number entries for that one line,
20142 each with a different discriminator.
20143 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20144
20145 However, we want gdb to coalesce all four entries into one.
20146 Otherwise the user could stepi into the middle of the line and
20147 gdb would get confused about whether the pc really was in the
20148 middle of the line.
20149
20150 Things are further complicated by the fact that two consecutive
20151 line number entries for the same line is a heuristic used by gcc
20152 to denote the end of the prologue. So we can't just discard duplicate
20153 entries, we have to be selective about it. The heuristic we use is
20154 that we only collapse consecutive entries for the same line if at least
20155 one of those entries has a non-zero discriminator. PR 17276.
20156
20157 Note: Addresses in the line number state machine can never go backwards
20158 within one sequence, thus this coalescing is ok. */
20159
20160static int
804d2729
TT
20161dwarf_record_line_p (struct dwarf2_cu *cu,
20162 unsigned int line, unsigned int last_line,
a05a36a5
DE
20163 int line_has_non_zero_discriminator,
20164 struct subfile *last_subfile)
20165{
c24bdb02 20166 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20167 return 1;
20168 if (line != last_line)
20169 return 1;
20170 /* Same line for the same file that we've seen already.
20171 As a last check, for pr 17276, only record the line if the line
20172 has never had a non-zero discriminator. */
20173 if (!line_has_non_zero_discriminator)
20174 return 1;
20175 return 0;
20176}
20177
804d2729
TT
20178/* Use the CU's builder to record line number LINE beginning at
20179 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20180
20181static void
d9b3de22 20182dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20183 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20184 struct dwarf2_cu *cu)
252a6764
DE
20185{
20186 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20187
27e0867f
DE
20188 if (dwarf_line_debug)
20189 {
20190 fprintf_unfiltered (gdb_stdlog,
20191 "Recording line %u, file %s, address %s\n",
20192 line, lbasename (subfile->name),
20193 paddress (gdbarch, address));
20194 }
20195
804d2729 20196 if (cu != nullptr)
8c95582d 20197 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20198}
20199
20200/* Subroutine of dwarf_decode_lines_1 to simplify it.
20201 Mark the end of a set of line number records.
d9b3de22 20202 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20203 If SUBFILE is NULL the request is ignored. */
20204
20205static void
20206dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20207 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20208{
27e0867f
DE
20209 if (subfile == NULL)
20210 return;
20211
20212 if (dwarf_line_debug)
20213 {
20214 fprintf_unfiltered (gdb_stdlog,
20215 "Finishing current line, file %s, address %s\n",
20216 lbasename (subfile->name),
20217 paddress (gdbarch, address));
20218 }
20219
8c95582d 20220 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20221}
20222
6f77053d
PA
20223void
20224lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20225{
d9b3de22
DE
20226 if (dwarf_line_debug)
20227 {
20228 fprintf_unfiltered (gdb_stdlog,
20229 "Processing actual line %u: file %u,"
94a72be7 20230 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20231 m_line, m_file,
6f77053d 20232 paddress (m_gdbarch, m_address),
94a72be7
AB
20233 m_is_stmt, m_discriminator,
20234 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20235 }
20236
6f77053d 20237 file_entry *fe = current_file ();
8c43009f
PA
20238
20239 if (fe == NULL)
d9b3de22
DE
20240 dwarf2_debug_line_missing_file_complaint ();
20241 /* For now we ignore lines not starting on an instruction boundary.
20242 But not when processing end_sequence for compatibility with the
20243 previous version of the code. */
6f77053d 20244 else if (m_op_index == 0 || end_sequence)
d9b3de22 20245 {
8c43009f 20246 fe->included_p = 1;
8c95582d 20247 if (m_record_lines_p)
d9b3de22 20248 {
1313c56e
AB
20249 /* When we switch files we insert an end maker in the first file,
20250 switch to the second file and add a new line entry. The
20251 problem is that the end marker inserted in the first file will
20252 discard any previous line entries at the same address. If the
20253 line entries in the first file are marked as is-stmt, while
20254 the new line in the second file is non-stmt, then this means
20255 the end marker will discard is-stmt lines so we can have a
20256 non-stmt line. This means that there are less addresses at
20257 which the user can insert a breakpoint.
20258
20259 To improve this we track the last address in m_last_address,
20260 and whether we have seen an is-stmt at this address. Then
20261 when switching files, if we have seen a stmt at the current
20262 address, and we are switching to create a non-stmt line, then
20263 discard the new line. */
20264 bool file_changed
20265 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20266 bool ignore_this_line
20267 = (file_changed && !end_sequence && m_last_address == m_address
20268 && !m_is_stmt && m_stmt_at_address);
20269
20270 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 20271 {
804d2729
TT
20272 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20273 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20274 }
20275
1313c56e 20276 if (!end_sequence && !ignore_this_line)
d9b3de22 20277 {
8c95582d
AB
20278 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20279
804d2729 20280 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20281 m_line_has_non_zero_discriminator,
20282 m_last_subfile))
d9b3de22 20283 {
c24bdb02 20284 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20285 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20286 builder->get_current_subfile (),
8c95582d 20287 m_line, m_address, is_stmt,
804d2729 20288 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20289 }
c24bdb02 20290 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20291 m_last_line = m_line;
d9b3de22
DE
20292 }
20293 }
20294 }
1313c56e
AB
20295
20296 /* Track whether we have seen any m_is_stmt true at m_address in case we
20297 have multiple line table entries all at m_address. */
20298 if (m_last_address != m_address)
20299 {
20300 m_stmt_at_address = false;
20301 m_last_address = m_address;
20302 }
20303 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
20304}
20305
804d2729
TT
20306lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20307 line_header *lh, bool record_lines_p)
d9b3de22 20308{
804d2729 20309 m_cu = cu;
6f77053d
PA
20310 m_gdbarch = arch;
20311 m_record_lines_p = record_lines_p;
20312 m_line_header = lh;
d9b3de22 20313
804d2729 20314 m_currently_recording_lines = true;
d9b3de22 20315
d9b3de22
DE
20316 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20317 was a line entry for it so that the backend has a chance to adjust it
20318 and also record it in case it needs it. This is currently used by MIPS
20319 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20320 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20321 m_is_stmt = lh->default_is_stmt;
20322 m_discriminator = 0;
1313c56e
AB
20323
20324 m_last_address = m_address;
20325 m_stmt_at_address = false;
252a6764
DE
20326}
20327
6f77053d
PA
20328void
20329lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20330 const gdb_byte *line_ptr,
7ab6656f 20331 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20332{
7ab6656f
OJ
20333 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20334 the pc range of the CU. However, we restrict the test to only ADDRESS
20335 values of zero to preserve GDB's previous behaviour which is to handle
20336 the specific case of a function being GC'd by the linker. */
924c2928 20337
7ab6656f 20338 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20339 {
20340 /* This line table is for a function which has been
20341 GCd by the linker. Ignore it. PR gdb/12528 */
20342
5e22e966 20343 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
20344 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20345
b98664d3 20346 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20347 line_offset, objfile_name (objfile));
804d2729
TT
20348 m_currently_recording_lines = false;
20349 /* Note: m_currently_recording_lines is left as false until we see
20350 DW_LNE_end_sequence. */
924c2928
DE
20351 }
20352}
20353
f3f5162e 20354/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20355 Process the line number information in LH.
20356 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20357 program in order to set included_p for every referenced header. */
debd256d 20358
c906108c 20359static void
43f3e411
DE
20360dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20361 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20362{
d521ce57
TT
20363 const gdb_byte *line_ptr, *extended_end;
20364 const gdb_byte *line_end;
a8c50c1f 20365 unsigned int bytes_read, extended_len;
699ca60a 20366 unsigned char op_code, extended_op;
e142c38c 20367 CORE_ADDR baseaddr;
5e22e966 20368 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20369 bfd *abfd = objfile->obfd;
08feed99 20370 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20371 /* True if we're recording line info (as opposed to building partial
20372 symtabs and just interested in finding include files mentioned by
20373 the line number program). */
20374 bool record_lines_p = !decode_for_pst_p;
e142c38c 20375
b3b3bada 20376 baseaddr = objfile->text_section_offset ();
c906108c 20377
debd256d
JB
20378 line_ptr = lh->statement_program_start;
20379 line_end = lh->statement_program_end;
c906108c
SS
20380
20381 /* Read the statement sequences until there's nothing left. */
20382 while (line_ptr < line_end)
20383 {
6f77053d
PA
20384 /* The DWARF line number program state machine. Reset the state
20385 machine at the start of each sequence. */
804d2729 20386 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20387 bool end_sequence = false;
d9b3de22 20388
8c43009f 20389 if (record_lines_p)
c906108c 20390 {
8c43009f
PA
20391 /* Start a subfile for the current file of the state
20392 machine. */
20393 const file_entry *fe = state_machine.current_file ();
20394
20395 if (fe != NULL)
804d2729 20396 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20397 }
20398
a738430d 20399 /* Decode the table. */
d9b3de22 20400 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20401 {
20402 op_code = read_1_byte (abfd, line_ptr);
20403 line_ptr += 1;
9aa1fe7e 20404
debd256d 20405 if (op_code >= lh->opcode_base)
6e70227d 20406 {
8e07a239 20407 /* Special opcode. */
6f77053d 20408 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20409 }
20410 else switch (op_code)
c906108c
SS
20411 {
20412 case DW_LNS_extended_op:
3e43a32a
MS
20413 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20414 &bytes_read);
473b7be6 20415 line_ptr += bytes_read;
a8c50c1f 20416 extended_end = line_ptr + extended_len;
c906108c
SS
20417 extended_op = read_1_byte (abfd, line_ptr);
20418 line_ptr += 1;
20419 switch (extended_op)
20420 {
20421 case DW_LNE_end_sequence:
6f77053d
PA
20422 state_machine.handle_end_sequence ();
20423 end_sequence = true;
c906108c
SS
20424 break;
20425 case DW_LNE_set_address:
d9b3de22
DE
20426 {
20427 CORE_ADDR address
c8a7a66f 20428 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20429 line_ptr += bytes_read;
6f77053d
PA
20430
20431 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20432 lowpc - baseaddr, address);
6f77053d 20433 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20434 }
c906108c
SS
20435 break;
20436 case DW_LNE_define_file:
debd256d 20437 {
d521ce57 20438 const char *cur_file;
ecfb656c
PA
20439 unsigned int mod_time, length;
20440 dir_index dindex;
6e70227d 20441
3e43a32a
MS
20442 cur_file = read_direct_string (abfd, line_ptr,
20443 &bytes_read);
debd256d 20444 line_ptr += bytes_read;
ecfb656c 20445 dindex = (dir_index)
debd256d
JB
20446 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20447 line_ptr += bytes_read;
20448 mod_time =
20449 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20450 line_ptr += bytes_read;
20451 length =
20452 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20453 line_ptr += bytes_read;
ecfb656c 20454 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20455 }
c906108c 20456 break;
d0c6ba3d 20457 case DW_LNE_set_discriminator:
6f77053d
PA
20458 {
20459 /* The discriminator is not interesting to the
20460 debugger; just ignore it. We still need to
20461 check its value though:
20462 if there are consecutive entries for the same
20463 (non-prologue) line we want to coalesce them.
20464 PR 17276. */
20465 unsigned int discr
20466 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20467 line_ptr += bytes_read;
20468
20469 state_machine.handle_set_discriminator (discr);
20470 }
d0c6ba3d 20471 break;
c906108c 20472 default:
b98664d3 20473 complaint (_("mangled .debug_line section"));
debd256d 20474 return;
c906108c 20475 }
a8c50c1f
DJ
20476 /* Make sure that we parsed the extended op correctly. If e.g.
20477 we expected a different address size than the producer used,
20478 we may have read the wrong number of bytes. */
20479 if (line_ptr != extended_end)
20480 {
b98664d3 20481 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20482 return;
20483 }
c906108c
SS
20484 break;
20485 case DW_LNS_copy:
6f77053d 20486 state_machine.handle_copy ();
c906108c
SS
20487 break;
20488 case DW_LNS_advance_pc:
2dc7f7b3
TT
20489 {
20490 CORE_ADDR adjust
20491 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20492 line_ptr += bytes_read;
6f77053d
PA
20493
20494 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20495 }
c906108c
SS
20496 break;
20497 case DW_LNS_advance_line:
a05a36a5
DE
20498 {
20499 int line_delta
20500 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20501 line_ptr += bytes_read;
6f77053d
PA
20502
20503 state_machine.handle_advance_line (line_delta);
a05a36a5 20504 }
c906108c
SS
20505 break;
20506 case DW_LNS_set_file:
d9b3de22 20507 {
6f77053d 20508 file_name_index file
ecfb656c
PA
20509 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20510 &bytes_read);
d9b3de22 20511 line_ptr += bytes_read;
8c43009f 20512
6f77053d 20513 state_machine.handle_set_file (file);
d9b3de22 20514 }
c906108c
SS
20515 break;
20516 case DW_LNS_set_column:
0ad93d4f 20517 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20518 line_ptr += bytes_read;
20519 break;
20520 case DW_LNS_negate_stmt:
6f77053d 20521 state_machine.handle_negate_stmt ();
c906108c
SS
20522 break;
20523 case DW_LNS_set_basic_block:
c906108c 20524 break;
c2c6d25f
JM
20525 /* Add to the address register of the state machine the
20526 address increment value corresponding to special opcode
a738430d
MK
20527 255. I.e., this value is scaled by the minimum
20528 instruction length since special opcode 255 would have
b021a221 20529 scaled the increment. */
c906108c 20530 case DW_LNS_const_add_pc:
6f77053d 20531 state_machine.handle_const_add_pc ();
c906108c
SS
20532 break;
20533 case DW_LNS_fixed_advance_pc:
3e29f34a 20534 {
6f77053d 20535 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20536 line_ptr += 2;
6f77053d
PA
20537
20538 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20539 }
c906108c 20540 break;
9aa1fe7e 20541 default:
a738430d
MK
20542 {
20543 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20544 int i;
a738430d 20545
debd256d 20546 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20547 {
20548 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20549 line_ptr += bytes_read;
20550 }
20551 }
c906108c
SS
20552 }
20553 }
d9b3de22
DE
20554
20555 if (!end_sequence)
20556 dwarf2_debug_line_missing_end_sequence_complaint ();
20557
20558 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20559 in which case we still finish recording the last line). */
6f77053d 20560 state_machine.record_line (true);
c906108c 20561 }
f3f5162e
DE
20562}
20563
20564/* Decode the Line Number Program (LNP) for the given line_header
20565 structure and CU. The actual information extracted and the type
20566 of structures created from the LNP depends on the value of PST.
20567
20568 1. If PST is NULL, then this procedure uses the data from the program
20569 to create all necessary symbol tables, and their linetables.
20570
20571 2. If PST is not NULL, this procedure reads the program to determine
20572 the list of files included by the unit represented by PST, and
20573 builds all the associated partial symbol tables.
20574
20575 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20576 It is used for relative paths in the line table.
20577 NOTE: When processing partial symtabs (pst != NULL),
20578 comp_dir == pst->dirname.
20579
20580 NOTE: It is important that psymtabs have the same file name (via strcmp)
20581 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20582 symtab we don't use it in the name of the psymtabs we create.
20583 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20584 A good testcase for this is mb-inline.exp.
20585
527f3840
JK
20586 LOWPC is the lowest address in CU (or 0 if not known).
20587
20588 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20589 for its PC<->lines mapping information. Otherwise only the filename
20590 table is read in. */
f3f5162e
DE
20591
20592static void
20593dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20594 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20595 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20596{
5e22e966 20597 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20598 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20599
527f3840
JK
20600 if (decode_mapping)
20601 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20602
20603 if (decode_for_pst_p)
20604 {
aaa75496
JB
20605 /* Now that we're done scanning the Line Header Program, we can
20606 create the psymtab of each included file. */
7ba99d21
AT
20607 for (auto &file_entry : lh->file_names ())
20608 if (file_entry.included_p == 1)
aaa75496 20609 {
c89b44cd 20610 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20611 const char *include_name =
7ba99d21
AT
20612 psymtab_include_file_name (lh, file_entry, pst,
20613 comp_dir, &name_holder);
c6da4cef 20614 if (include_name != NULL)
aaa75496
JB
20615 dwarf2_create_include_psymtab (include_name, pst, objfile);
20616 }
20617 }
cb1df416
DJ
20618 else
20619 {
20620 /* Make sure a symtab is created for every file, even files
20621 which contain only variables (i.e. no code with associated
20622 line numbers). */
c24bdb02
KS
20623 buildsym_compunit *builder = cu->get_builder ();
20624 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20625
7ba99d21 20626 for (auto &fe : lh->file_names ())
cb1df416 20627 {
804d2729 20628 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20629 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20630 {
c24bdb02 20631 builder->get_current_subfile ()->symtab
804d2729 20632 = allocate_symtab (cust,
c24bdb02 20633 builder->get_current_subfile ()->name);
43f3e411 20634 }
c24bdb02 20635 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20636 }
20637 }
c906108c
SS
20638}
20639
20640/* Start a subfile for DWARF. FILENAME is the name of the file and
20641 DIRNAME the name of the source directory which contains FILENAME
4d663531 20642 or NULL if not known.
c906108c
SS
20643 This routine tries to keep line numbers from identical absolute and
20644 relative file names in a common subfile.
20645
20646 Using the `list' example from the GDB testsuite, which resides in
20647 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20648 of /srcdir/list0.c yields the following debugging information for list0.c:
20649
c5aa993b 20650 DW_AT_name: /srcdir/list0.c
4d663531 20651 DW_AT_comp_dir: /compdir
357e46e7 20652 files.files[0].name: list0.h
c5aa993b 20653 files.files[0].dir: /srcdir
357e46e7 20654 files.files[1].name: list0.c
c5aa993b 20655 files.files[1].dir: /srcdir
c906108c
SS
20656
20657 The line number information for list0.c has to end up in a single
4f1520fb
FR
20658 subfile, so that `break /srcdir/list0.c:1' works as expected.
20659 start_subfile will ensure that this happens provided that we pass the
20660 concatenation of files.files[1].dir and files.files[1].name as the
20661 subfile's name. */
c906108c
SS
20662
20663static void
804d2729
TT
20664dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20665 const char *dirname)
c906108c 20666{
43816ebc 20667 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20668
4d663531 20669 /* In order not to lose the line information directory,
4f1520fb
FR
20670 we concatenate it to the filename when it makes sense.
20671 Note that the Dwarf3 standard says (speaking of filenames in line
20672 information): ``The directory index is ignored for file names
20673 that represent full path names''. Thus ignoring dirname in the
20674 `else' branch below isn't an issue. */
c906108c 20675
d5166ae1 20676 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20677 {
43816ebc
TT
20678 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20679 filename = copy.get ();
d521ce57 20680 }
c906108c 20681
c24bdb02 20682 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20683}
20684
804d2729
TT
20685/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20686 buildsym_compunit constructor. */
f4dc4d17 20687
c24bdb02
KS
20688struct compunit_symtab *
20689dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20690 CORE_ADDR low_pc)
f4dc4d17 20691{
c24bdb02 20692 gdb_assert (m_builder == nullptr);
43f3e411 20693
c24bdb02 20694 m_builder.reset (new struct buildsym_compunit
f6e649dd 20695 (this->per_objfile->objfile,
c24bdb02 20696 name, comp_dir, language, low_pc));
93b8bea4 20697
c24bdb02 20698 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20699
c24bdb02
KS
20700 get_builder ()->record_debugformat ("DWARF 2");
20701 get_builder ()->record_producer (producer);
f4dc4d17 20702
c24bdb02 20703 processing_has_namespace_info = false;
43f3e411 20704
c24bdb02 20705 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20706}
20707
4c2df51b
DJ
20708static void
20709var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20710 struct dwarf2_cu *cu)
4c2df51b 20711{
5e22e966 20712 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
20713 struct comp_unit_head *cu_header = &cu->header;
20714
4c2df51b
DJ
20715 /* NOTE drow/2003-01-30: There used to be a comment and some special
20716 code here to turn a symbol with DW_AT_external and a
20717 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20718 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20719 with some versions of binutils) where shared libraries could have
20720 relocations against symbols in their debug information - the
20721 minimal symbol would have the right address, but the debug info
20722 would not. It's no longer necessary, because we will explicitly
20723 apply relocations when we read in the debug information now. */
20724
20725 /* A DW_AT_location attribute with no contents indicates that a
20726 variable has been optimized away. */
4fc6c0d5 20727 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20728 {
f1e6e072 20729 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20730 return;
20731 }
20732
20733 /* Handle one degenerate form of location expression specially, to
20734 preserve GDB's previous behavior when section offsets are
336d760d
AT
20735 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20736 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20737
4fc6c0d5 20738 if (attr->form_is_block ()
3019eac3
DE
20739 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20740 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20741 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20742 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20743 && (DW_BLOCK (attr)->size
20744 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20745 {
891d2f0b 20746 unsigned int dummy;
4c2df51b 20747
3019eac3 20748 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20749 SET_SYMBOL_VALUE_ADDRESS
20750 (sym, cu->header.read_address (objfile->obfd,
20751 DW_BLOCK (attr)->data + 1,
20752 &dummy));
3019eac3 20753 else
38583298
TT
20754 SET_SYMBOL_VALUE_ADDRESS
20755 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20756 &dummy));
f1e6e072 20757 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20758 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20759 SET_SYMBOL_VALUE_ADDRESS
20760 (sym,
20761 SYMBOL_VALUE_ADDRESS (sym)
20762 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20763 return;
20764 }
20765
20766 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20767 expression evaluator, and use LOC_COMPUTED only when necessary
20768 (i.e. when the value of a register or memory location is
20769 referenced, or a thread-local block, etc.). Then again, it might
20770 not be worthwhile. I'm assuming that it isn't unless performance
20771 or memory numbers show me otherwise. */
20772
f1e6e072 20773 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20774
f1e6e072 20775 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20776 cu->has_loclist = true;
4c2df51b
DJ
20777}
20778
c906108c
SS
20779/* Given a pointer to a DWARF information entry, figure out if we need
20780 to make a symbol table entry for it, and if so, create a new entry
20781 and return a pointer to it.
20782 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20783 used the passed type.
20784 If SPACE is not NULL, use it to hold the new symbol. If it is
20785 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20786
20787static struct symbol *
5e2db402
TT
20788new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20789 struct symbol *space)
c906108c 20790{
976ca316
SM
20791 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20792 struct objfile *objfile = per_objfile->objfile;
08feed99 20793 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20794 struct symbol *sym = NULL;
15d034d0 20795 const char *name;
c906108c
SS
20796 struct attribute *attr = NULL;
20797 struct attribute *attr2 = NULL;
e142c38c 20798 CORE_ADDR baseaddr;
e37fd15a
SW
20799 struct pending **list_to_add = NULL;
20800
edb3359d 20801 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20802
b3b3bada 20803 baseaddr = objfile->text_section_offset ();
c906108c 20804
94af9270 20805 name = dwarf2_name (die, cu);
c906108c
SS
20806 if (name)
20807 {
34eaf542 20808 int suppress_add = 0;
94af9270 20809
34eaf542
TT
20810 if (space)
20811 sym = space;
20812 else
8c14c3a3 20813 sym = new (&objfile->objfile_obstack) symbol;
c906108c 20814 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20815
20816 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20817 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
20818 /* Fortran does not have mangling standard and the mangling does differ
20819 between gfortran, iFort etc. */
bcfe6157
TT
20820 const char *physname
20821 = (cu->language == language_fortran
20822 ? dwarf2_full_name (name, die, cu)
20823 : dwarf2_physname (name, die, cu));
20824 const char *linkagename = dw2_linkage_name (die, cu);
20825
20826 if (linkagename == nullptr || cu->language == language_ada)
20827 sym->set_linkage_name (physname);
20828 else
20829 {
20830 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20831 sym->set_linkage_name (linkagename);
20832 }
f55ee35c 20833
c906108c 20834 /* Default assumptions.
c5aa993b 20835 Use the passed type or decode it from the die. */
176620f1 20836 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20837 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20838 if (type != NULL)
20839 SYMBOL_TYPE (sym) = type;
20840 else
e7c27a73 20841 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20842 attr = dwarf2_attr (die,
20843 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20844 cu);
435d3d88 20845 if (attr != nullptr)
c906108c
SS
20846 {
20847 SYMBOL_LINE (sym) = DW_UNSND (attr);
20848 }
cb1df416 20849
edb3359d
DJ
20850 attr = dwarf2_attr (die,
20851 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20852 cu);
435d3d88 20853 if (attr != nullptr)
cb1df416 20854 {
ecfb656c 20855 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20856 struct file_entry *fe;
9a619af0 20857
ecfb656c
PA
20858 if (cu->line_header != NULL)
20859 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20860 else
20861 fe = NULL;
20862
20863 if (fe == NULL)
b98664d3 20864 complaint (_("file index out of range"));
8c43009f
PA
20865 else
20866 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20867 }
20868
c906108c
SS
20869 switch (die->tag)
20870 {
20871 case DW_TAG_label:
e142c38c 20872 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20873 if (attr != nullptr)
3e29f34a
MR
20874 {
20875 CORE_ADDR addr;
20876
cd6c91b4 20877 addr = attr->value_as_address ();
3e29f34a 20878 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20879 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20880 }
0f5238ed
TT
20881 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20882 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20883 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20884 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20885 break;
20886 case DW_TAG_subprogram:
20887 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20888 finish_block. */
f1e6e072 20889 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20890 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20891 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20892 || cu->language == language_ada
20893 || cu->language == language_fortran)
c906108c 20894 {
2cfa0c8d 20895 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20896 Ada and Fortran subprograms, whether marked external or
20897 not, are always stored as a global symbol, because we want
20898 to be able to access them globally. For instance, we want
20899 to be able to break on a nested subprogram without having
20900 to specify the context. */
c24bdb02 20901 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20902 }
20903 else
20904 {
e37fd15a 20905 list_to_add = cu->list_in_scope;
c906108c
SS
20906 }
20907 break;
edb3359d
DJ
20908 case DW_TAG_inlined_subroutine:
20909 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20910 finish_block. */
f1e6e072 20911 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20912 SYMBOL_INLINED (sym) = 1;
481860b3 20913 list_to_add = cu->list_in_scope;
edb3359d 20914 break;
34eaf542
TT
20915 case DW_TAG_template_value_param:
20916 suppress_add = 1;
20917 /* Fall through. */
72929c62 20918 case DW_TAG_constant:
c906108c 20919 case DW_TAG_variable:
254e6b9e 20920 case DW_TAG_member:
0963b4bd
MS
20921 /* Compilation with minimal debug info may result in
20922 variables with missing type entries. Change the
20923 misleading `void' type to something sensible. */
78134374 20924 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 20925 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20926
e142c38c 20927 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20928 /* In the case of DW_TAG_member, we should only be called for
20929 static const members. */
20930 if (die->tag == DW_TAG_member)
20931 {
3863f96c
DE
20932 /* dwarf2_add_field uses die_is_declaration,
20933 so we do the same. */
254e6b9e
DE
20934 gdb_assert (die_is_declaration (die, cu));
20935 gdb_assert (attr);
20936 }
435d3d88 20937 if (attr != nullptr)
c906108c 20938 {
e7c27a73 20939 dwarf2_const_value (attr, sym, cu);
e142c38c 20940 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20941 if (!suppress_add)
34eaf542
TT
20942 {
20943 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20944 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20945 else
e37fd15a 20946 list_to_add = cu->list_in_scope;
34eaf542 20947 }
c906108c
SS
20948 break;
20949 }
e142c38c 20950 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20951 if (attr != nullptr)
c906108c 20952 {
e7c27a73 20953 var_decode_location (attr, sym, cu);
e142c38c 20954 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20955
20956 /* Fortran explicitly imports any global symbols to the local
20957 scope by DW_TAG_common_block. */
20958 if (cu->language == language_fortran && die->parent
20959 && die->parent->tag == DW_TAG_common_block)
20960 attr2 = NULL;
20961
caac4577
JG
20962 if (SYMBOL_CLASS (sym) == LOC_STATIC
20963 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 20964 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
20965 {
20966 /* When a static variable is eliminated by the linker,
20967 the corresponding debug information is not stripped
20968 out, but the variable address is set to null;
20969 do not add such variables into symbol table. */
20970 }
20971 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20972 {
4b610737
TT
20973 if (SYMBOL_CLASS (sym) == LOC_STATIC
20974 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 20975 && per_objfile->per_bfd->can_copy)
4b610737
TT
20976 {
20977 /* A global static variable might be subject to
20978 copy relocation. We first check for a local
20979 minsym, though, because maybe the symbol was
20980 marked hidden, in which case this would not
20981 apply. */
20982 bound_minimal_symbol found
20983 = (lookup_minimal_symbol_linkage
987012b8 20984 (sym->linkage_name (), objfile));
4b610737
TT
20985 if (found.minsym != nullptr)
20986 sym->maybe_copied = 1;
20987 }
f55ee35c 20988
1c809c68
TT
20989 /* A variable with DW_AT_external is never static,
20990 but it may be block-scoped. */
804d2729 20991 list_to_add
c24bdb02
KS
20992 = ((cu->list_in_scope
20993 == cu->get_builder ()->get_file_symbols ())
20994 ? cu->get_builder ()->get_global_symbols ()
804d2729 20995 : cu->list_in_scope);
1c809c68 20996 }
c906108c 20997 else
e37fd15a 20998 list_to_add = cu->list_in_scope;
c906108c
SS
20999 }
21000 else
21001 {
21002 /* We do not know the address of this symbol.
c5aa993b
JM
21003 If it is an external symbol and we have type information
21004 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21005 The address of the variable will then be determined from
21006 the minimal symbol table whenever the variable is
21007 referenced. */
e142c38c 21008 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21009
21010 /* Fortran explicitly imports any global symbols to the local
21011 scope by DW_TAG_common_block. */
21012 if (cu->language == language_fortran && die->parent
21013 && die->parent->tag == DW_TAG_common_block)
21014 {
21015 /* SYMBOL_CLASS doesn't matter here because
21016 read_common_block is going to reset it. */
21017 if (!suppress_add)
21018 list_to_add = cu->list_in_scope;
21019 }
21020 else if (attr2 && (DW_UNSND (attr2) != 0)
21021 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21022 {
0fe7935b
DJ
21023 /* A variable with DW_AT_external is never static, but it
21024 may be block-scoped. */
804d2729 21025 list_to_add
c24bdb02
KS
21026 = ((cu->list_in_scope
21027 == cu->get_builder ()->get_file_symbols ())
21028 ? cu->get_builder ()->get_global_symbols ()
804d2729 21029 : cu->list_in_scope);
0fe7935b 21030
f1e6e072 21031 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21032 }
442ddf59
JK
21033 else if (!die_is_declaration (die, cu))
21034 {
21035 /* Use the default LOC_OPTIMIZED_OUT class. */
21036 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21037 if (!suppress_add)
21038 list_to_add = cu->list_in_scope;
442ddf59 21039 }
c906108c
SS
21040 }
21041 break;
21042 case DW_TAG_formal_parameter:
a60f3166
TT
21043 {
21044 /* If we are inside a function, mark this as an argument. If
21045 not, we might be looking at an argument to an inlined function
21046 when we do not have enough information to show inlined frames;
21047 pretend it's a local variable in that case so that the user can
21048 still see it. */
804d2729 21049 struct context_stack *curr
c24bdb02 21050 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
21051 if (curr != nullptr && curr->name != nullptr)
21052 SYMBOL_IS_ARGUMENT (sym) = 1;
21053 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21054 if (attr != nullptr)
a60f3166
TT
21055 {
21056 var_decode_location (attr, sym, cu);
21057 }
21058 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21059 if (attr != nullptr)
a60f3166
TT
21060 {
21061 dwarf2_const_value (attr, sym, cu);
21062 }
f346a30d 21063
a60f3166
TT
21064 list_to_add = cu->list_in_scope;
21065 }
c906108c
SS
21066 break;
21067 case DW_TAG_unspecified_parameters:
21068 /* From varargs functions; gdb doesn't seem to have any
21069 interest in this information, so just ignore it for now.
21070 (FIXME?) */
21071 break;
34eaf542
TT
21072 case DW_TAG_template_type_param:
21073 suppress_add = 1;
21074 /* Fall through. */
c906108c 21075 case DW_TAG_class_type:
680b30c7 21076 case DW_TAG_interface_type:
c906108c
SS
21077 case DW_TAG_structure_type:
21078 case DW_TAG_union_type:
72019c9c 21079 case DW_TAG_set_type:
c906108c 21080 case DW_TAG_enumeration_type:
f1e6e072 21081 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21082 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21083
63d06c5c 21084 {
9c37b5ae 21085 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21086 really ever be static objects: otherwise, if you try
21087 to, say, break of a class's method and you're in a file
21088 which doesn't mention that class, it won't work unless
21089 the check for all static symbols in lookup_symbol_aux
21090 saves you. See the OtherFileClass tests in
21091 gdb.c++/namespace.exp. */
21092
e37fd15a 21093 if (!suppress_add)
34eaf542 21094 {
c24bdb02 21095 buildsym_compunit *builder = cu->get_builder ();
804d2729 21096 list_to_add
c24bdb02 21097 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21098 && cu->language == language_cplus
c24bdb02 21099 ? builder->get_global_symbols ()
804d2729 21100 : cu->list_in_scope);
63d06c5c 21101
64382290 21102 /* The semantics of C++ state that "struct foo {
9c37b5ae 21103 ... }" also defines a typedef for "foo". */
64382290 21104 if (cu->language == language_cplus
45280282 21105 || cu->language == language_ada
c44af4eb
TT
21106 || cu->language == language_d
21107 || cu->language == language_rust)
64382290
TT
21108 {
21109 /* The symbol's name is already allocated along
21110 with this objfile, so we don't need to
21111 duplicate it for the type. */
7d93a1e0 21112 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 21113 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 21114 }
63d06c5c
DC
21115 }
21116 }
c906108c
SS
21117 break;
21118 case DW_TAG_typedef:
f1e6e072 21119 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21120 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21121 list_to_add = cu->list_in_scope;
63d06c5c 21122 break;
c906108c 21123 case DW_TAG_base_type:
a02abb62 21124 case DW_TAG_subrange_type:
f1e6e072 21125 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21126 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21127 list_to_add = cu->list_in_scope;
c906108c
SS
21128 break;
21129 case DW_TAG_enumerator:
e142c38c 21130 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21131 if (attr != nullptr)
c906108c 21132 {
e7c27a73 21133 dwarf2_const_value (attr, sym, cu);
c906108c 21134 }
63d06c5c
DC
21135 {
21136 /* NOTE: carlton/2003-11-10: See comment above in the
21137 DW_TAG_class_type, etc. block. */
21138
804d2729 21139 list_to_add
c24bdb02 21140 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21141 && cu->language == language_cplus
c24bdb02 21142 ? cu->get_builder ()->get_global_symbols ()
804d2729 21143 : cu->list_in_scope);
63d06c5c 21144 }
c906108c 21145 break;
74921315 21146 case DW_TAG_imported_declaration:
5c4e30ca 21147 case DW_TAG_namespace:
f1e6e072 21148 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21149 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21150 break;
530e8392
KB
21151 case DW_TAG_module:
21152 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21153 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21154 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21155 break;
4357ac6c 21156 case DW_TAG_common_block:
f1e6e072 21157 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21158 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21159 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21160 break;
c906108c
SS
21161 default:
21162 /* Not a tag we recognize. Hopefully we aren't processing
21163 trash data, but since we must specifically ignore things
21164 we don't recognize, there is nothing else we should do at
0963b4bd 21165 this point. */
b98664d3 21166 complaint (_("unsupported tag: '%s'"),
4d3c2250 21167 dwarf_tag_name (die->tag));
c906108c
SS
21168 break;
21169 }
df8a16a1 21170
e37fd15a
SW
21171 if (suppress_add)
21172 {
21173 sym->hash_next = objfile->template_symbols;
21174 objfile->template_symbols = sym;
21175 list_to_add = NULL;
21176 }
21177
21178 if (list_to_add != NULL)
d3cb6808 21179 add_symbol_to_list (sym, list_to_add);
e37fd15a 21180
df8a16a1
DJ
21181 /* For the benefit of old versions of GCC, check for anonymous
21182 namespaces based on the demangled name. */
4d4ec4e5 21183 if (!cu->processing_has_namespace_info
94af9270 21184 && cu->language == language_cplus)
c24bdb02 21185 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21186 }
21187 return (sym);
21188}
21189
98bfdba5
PA
21190/* Given an attr with a DW_FORM_dataN value in host byte order,
21191 zero-extend it as appropriate for the symbol's type. The DWARF
21192 standard (v4) is not entirely clear about the meaning of using
21193 DW_FORM_dataN for a constant with a signed type, where the type is
21194 wider than the data. The conclusion of a discussion on the DWARF
21195 list was that this is unspecified. We choose to always zero-extend
21196 because that is the interpretation long in use by GCC. */
c906108c 21197
98bfdba5 21198static gdb_byte *
ff39bb5e 21199dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21200 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21201{
5e22e966 21202 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21203 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21204 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21205 LONGEST l = DW_UNSND (attr);
21206
21207 if (bits < sizeof (*value) * 8)
21208 {
21209 l &= ((LONGEST) 1 << bits) - 1;
21210 *value = l;
21211 }
21212 else if (bits == sizeof (*value) * 8)
21213 *value = l;
21214 else
21215 {
224c3ddb 21216 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21217 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21218 return bytes;
21219 }
21220
21221 return NULL;
21222}
21223
21224/* Read a constant value from an attribute. Either set *VALUE, or if
21225 the value does not fit in *VALUE, set *BYTES - either already
21226 allocated on the objfile obstack, or newly allocated on OBSTACK,
21227 or, set *BATON, if we translated the constant to a location
21228 expression. */
21229
21230static void
ff39bb5e 21231dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21232 const char *name, struct obstack *obstack,
21233 struct dwarf2_cu *cu,
d521ce57 21234 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21235 struct dwarf2_locexpr_baton **baton)
21236{
5e22e966 21237 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21238 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21239 struct comp_unit_head *cu_header = &cu->header;
c906108c 21240 struct dwarf_block *blk;
98bfdba5
PA
21241 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21242 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21243
21244 *value = 0;
21245 *bytes = NULL;
21246 *baton = NULL;
c906108c
SS
21247
21248 switch (attr->form)
21249 {
21250 case DW_FORM_addr:
336d760d 21251 case DW_FORM_addrx:
3019eac3 21252 case DW_FORM_GNU_addr_index:
ac56253d 21253 {
ac56253d
TT
21254 gdb_byte *data;
21255
98bfdba5
PA
21256 if (TYPE_LENGTH (type) != cu_header->addr_size)
21257 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21258 cu_header->addr_size,
98bfdba5 21259 TYPE_LENGTH (type));
ac56253d
TT
21260 /* Symbols of this form are reasonably rare, so we just
21261 piggyback on the existing location code rather than writing
21262 a new implementation of symbol_computed_ops. */
8d749320 21263 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21264 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21265 (*baton)->per_cu = cu->per_cu;
21266 gdb_assert ((*baton)->per_cu);
ac56253d 21267
98bfdba5 21268 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21269 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21270 (*baton)->data = data;
ac56253d
TT
21271
21272 data[0] = DW_OP_addr;
21273 store_unsigned_integer (&data[1], cu_header->addr_size,
21274 byte_order, DW_ADDR (attr));
21275 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21276 }
c906108c 21277 break;
4ac36638 21278 case DW_FORM_string:
93b5768b 21279 case DW_FORM_strp:
cf532bd1 21280 case DW_FORM_strx:
3019eac3 21281 case DW_FORM_GNU_str_index:
36586728 21282 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21283 /* DW_STRING is already allocated on the objfile obstack, point
21284 directly to it. */
d521ce57 21285 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21286 break;
c906108c
SS
21287 case DW_FORM_block1:
21288 case DW_FORM_block2:
21289 case DW_FORM_block4:
21290 case DW_FORM_block:
2dc7f7b3 21291 case DW_FORM_exprloc:
0224619f 21292 case DW_FORM_data16:
c906108c 21293 blk = DW_BLOCK (attr);
98bfdba5
PA
21294 if (TYPE_LENGTH (type) != blk->size)
21295 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21296 TYPE_LENGTH (type));
21297 *bytes = blk->data;
c906108c 21298 break;
2df3850c
JM
21299
21300 /* The DW_AT_const_value attributes are supposed to carry the
21301 symbol's value "represented as it would be on the target
21302 architecture." By the time we get here, it's already been
21303 converted to host endianness, so we just need to sign- or
21304 zero-extend it as appropriate. */
21305 case DW_FORM_data1:
3aef2284 21306 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21307 break;
c906108c 21308 case DW_FORM_data2:
3aef2284 21309 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21310 break;
c906108c 21311 case DW_FORM_data4:
3aef2284 21312 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21313 break;
c906108c 21314 case DW_FORM_data8:
3aef2284 21315 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21316 break;
21317
c906108c 21318 case DW_FORM_sdata:
663c44ac 21319 case DW_FORM_implicit_const:
98bfdba5 21320 *value = DW_SND (attr);
2df3850c
JM
21321 break;
21322
c906108c 21323 case DW_FORM_udata:
98bfdba5 21324 *value = DW_UNSND (attr);
c906108c 21325 break;
2df3850c 21326
c906108c 21327 default:
b98664d3 21328 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21329 dwarf_form_name (attr->form));
98bfdba5 21330 *value = 0;
c906108c
SS
21331 break;
21332 }
21333}
21334
2df3850c 21335
98bfdba5
PA
21336/* Copy constant value from an attribute to a symbol. */
21337
2df3850c 21338static void
ff39bb5e 21339dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21340 struct dwarf2_cu *cu)
2df3850c 21341{
5e22e966 21342 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 21343 LONGEST value;
d521ce57 21344 const gdb_byte *bytes;
98bfdba5 21345 struct dwarf2_locexpr_baton *baton;
2df3850c 21346
98bfdba5 21347 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21348 sym->print_name (),
98bfdba5
PA
21349 &objfile->objfile_obstack, cu,
21350 &value, &bytes, &baton);
2df3850c 21351
98bfdba5
PA
21352 if (baton != NULL)
21353 {
98bfdba5 21354 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21355 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21356 }
21357 else if (bytes != NULL)
21358 {
21359 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21360 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21361 }
21362 else
21363 {
21364 SYMBOL_VALUE (sym) = value;
f1e6e072 21365 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21366 }
2df3850c
JM
21367}
21368
c906108c
SS
21369/* Return the type of the die in question using its DW_AT_type attribute. */
21370
21371static struct type *
e7c27a73 21372die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21373{
c906108c 21374 struct attribute *type_attr;
c906108c 21375
e142c38c 21376 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21377 if (!type_attr)
21378 {
5e22e966 21379 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21380 /* A missing DW_AT_type represents a void type. */
518817b3 21381 return objfile_type (objfile)->builtin_void;
c906108c 21382 }
348e048f 21383
673bfd45 21384 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21385}
21386
b4ba55a1
JB
21387/* True iff CU's producer generates GNAT Ada auxiliary information
21388 that allows to find parallel types through that information instead
21389 of having to do expensive parallel lookups by type name. */
21390
21391static int
21392need_gnat_info (struct dwarf2_cu *cu)
21393{
de4cb04a
JB
21394 /* Assume that the Ada compiler was GNAT, which always produces
21395 the auxiliary information. */
21396 return (cu->language == language_ada);
b4ba55a1
JB
21397}
21398
b4ba55a1
JB
21399/* Return the auxiliary type of the die in question using its
21400 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21401 attribute is not present. */
21402
21403static struct type *
21404die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21405{
b4ba55a1 21406 struct attribute *type_attr;
b4ba55a1
JB
21407
21408 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21409 if (!type_attr)
21410 return NULL;
21411
673bfd45 21412 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21413}
21414
21415/* If DIE has a descriptive_type attribute, then set the TYPE's
21416 descriptive type accordingly. */
21417
21418static void
21419set_descriptive_type (struct type *type, struct die_info *die,
21420 struct dwarf2_cu *cu)
21421{
21422 struct type *descriptive_type = die_descriptive_type (die, cu);
21423
21424 if (descriptive_type)
21425 {
21426 ALLOCATE_GNAT_AUX_TYPE (type);
21427 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21428 }
21429}
21430
c906108c
SS
21431/* Return the containing type of the die in question using its
21432 DW_AT_containing_type attribute. */
21433
21434static struct type *
e7c27a73 21435die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21436{
c906108c 21437 struct attribute *type_attr;
5e22e966 21438 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21439
e142c38c 21440 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21441 if (!type_attr)
21442 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21443 "[in module %s]"), objfile_name (objfile));
33ac96f0 21444
673bfd45 21445 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21446}
21447
ac9ec31b
DE
21448/* Return an error marker type to use for the ill formed type in DIE/CU. */
21449
21450static struct type *
21451build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21452{
976ca316
SM
21453 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21454 struct objfile *objfile = per_objfile->objfile;
528e1572 21455 char *saved;
ac9ec31b 21456
528e1572
SM
21457 std::string message
21458 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21459 objfile_name (objfile),
21460 sect_offset_str (cu->header.sect_off),
21461 sect_offset_str (die->sect_off));
efba19b0 21462 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21463
19f392bc 21464 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21465}
21466
673bfd45 21467/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21468 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21469 DW_AT_containing_type.
673bfd45
DE
21470 If there is no type substitute an error marker. */
21471
c906108c 21472static struct type *
ff39bb5e 21473lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21474 struct dwarf2_cu *cu)
c906108c 21475{
976ca316
SM
21476 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21477 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
21478 struct type *this_type;
21479
ac9ec31b
DE
21480 gdb_assert (attr->name == DW_AT_type
21481 || attr->name == DW_AT_GNAT_descriptive_type
21482 || attr->name == DW_AT_containing_type);
21483
673bfd45
DE
21484 /* First see if we have it cached. */
21485
36586728
TT
21486 if (attr->form == DW_FORM_GNU_ref_alt)
21487 {
21488 struct dwarf2_per_cu_data *per_cu;
0826b30a 21489 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 21490
976ca316
SM
21491 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
21492 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 21493 }
cd6c91b4 21494 else if (attr->form_is_ref ())
673bfd45 21495 {
0826b30a 21496 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 21497
976ca316 21498 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 21499 }
55f1336d 21500 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21501 {
ac9ec31b 21502 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21503
ac9ec31b 21504 return get_signatured_type (die, signature, cu);
673bfd45
DE
21505 }
21506 else
21507 {
b98664d3 21508 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21509 " at %s [in module %s]"),
21510 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21511 objfile_name (objfile));
ac9ec31b 21512 return build_error_marker_type (cu, die);
673bfd45
DE
21513 }
21514
21515 /* If not cached we need to read it in. */
21516
21517 if (this_type == NULL)
21518 {
ac9ec31b 21519 struct die_info *type_die = NULL;
673bfd45
DE
21520 struct dwarf2_cu *type_cu = cu;
21521
cd6c91b4 21522 if (attr->form_is_ref ())
ac9ec31b
DE
21523 type_die = follow_die_ref (die, attr, &type_cu);
21524 if (type_die == NULL)
21525 return build_error_marker_type (cu, die);
21526 /* If we find the type now, it's probably because the type came
3019eac3
DE
21527 from an inter-CU reference and the type's CU got expanded before
21528 ours. */
ac9ec31b 21529 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21530 }
21531
21532 /* If we still don't have a type use an error marker. */
21533
21534 if (this_type == NULL)
ac9ec31b 21535 return build_error_marker_type (cu, die);
673bfd45 21536
f792889a 21537 return this_type;
c906108c
SS
21538}
21539
673bfd45
DE
21540/* Return the type in DIE, CU.
21541 Returns NULL for invalid types.
21542
02142a6c 21543 This first does a lookup in die_type_hash,
673bfd45
DE
21544 and only reads the die in if necessary.
21545
21546 NOTE: This can be called when reading in partial or full symbols. */
21547
f792889a 21548static struct type *
e7c27a73 21549read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21550{
f792889a
DJ
21551 struct type *this_type;
21552
21553 this_type = get_die_type (die, cu);
21554 if (this_type)
21555 return this_type;
21556
673bfd45
DE
21557 return read_type_die_1 (die, cu);
21558}
21559
21560/* Read the type in DIE, CU.
21561 Returns NULL for invalid types. */
21562
21563static struct type *
21564read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21565{
21566 struct type *this_type = NULL;
21567
c906108c
SS
21568 switch (die->tag)
21569 {
21570 case DW_TAG_class_type:
680b30c7 21571 case DW_TAG_interface_type:
c906108c
SS
21572 case DW_TAG_structure_type:
21573 case DW_TAG_union_type:
f792889a 21574 this_type = read_structure_type (die, cu);
c906108c
SS
21575 break;
21576 case DW_TAG_enumeration_type:
f792889a 21577 this_type = read_enumeration_type (die, cu);
c906108c
SS
21578 break;
21579 case DW_TAG_subprogram:
21580 case DW_TAG_subroutine_type:
edb3359d 21581 case DW_TAG_inlined_subroutine:
f792889a 21582 this_type = read_subroutine_type (die, cu);
c906108c
SS
21583 break;
21584 case DW_TAG_array_type:
f792889a 21585 this_type = read_array_type (die, cu);
c906108c 21586 break;
72019c9c 21587 case DW_TAG_set_type:
f792889a 21588 this_type = read_set_type (die, cu);
72019c9c 21589 break;
c906108c 21590 case DW_TAG_pointer_type:
f792889a 21591 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21592 break;
21593 case DW_TAG_ptr_to_member_type:
f792889a 21594 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21595 break;
21596 case DW_TAG_reference_type:
4297a3f0
AV
21597 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21598 break;
21599 case DW_TAG_rvalue_reference_type:
21600 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21601 break;
21602 case DW_TAG_const_type:
f792889a 21603 this_type = read_tag_const_type (die, cu);
c906108c
SS
21604 break;
21605 case DW_TAG_volatile_type:
f792889a 21606 this_type = read_tag_volatile_type (die, cu);
c906108c 21607 break;
06d66ee9
TT
21608 case DW_TAG_restrict_type:
21609 this_type = read_tag_restrict_type (die, cu);
21610 break;
c906108c 21611 case DW_TAG_string_type:
f792889a 21612 this_type = read_tag_string_type (die, cu);
c906108c
SS
21613 break;
21614 case DW_TAG_typedef:
f792889a 21615 this_type = read_typedef (die, cu);
c906108c 21616 break;
a02abb62 21617 case DW_TAG_subrange_type:
f792889a 21618 this_type = read_subrange_type (die, cu);
a02abb62 21619 break;
c906108c 21620 case DW_TAG_base_type:
f792889a 21621 this_type = read_base_type (die, cu);
c906108c 21622 break;
81a17f79 21623 case DW_TAG_unspecified_type:
f792889a 21624 this_type = read_unspecified_type (die, cu);
81a17f79 21625 break;
0114d602
DJ
21626 case DW_TAG_namespace:
21627 this_type = read_namespace_type (die, cu);
21628 break;
f55ee35c
JK
21629 case DW_TAG_module:
21630 this_type = read_module_type (die, cu);
21631 break;
a2c2acaf
MW
21632 case DW_TAG_atomic_type:
21633 this_type = read_tag_atomic_type (die, cu);
21634 break;
c906108c 21635 default:
b98664d3 21636 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21637 dwarf_tag_name (die->tag));
c906108c
SS
21638 break;
21639 }
63d06c5c 21640
f792889a 21641 return this_type;
63d06c5c
DC
21642}
21643
abc72ce4
DE
21644/* See if we can figure out if the class lives in a namespace. We do
21645 this by looking for a member function; its demangled name will
21646 contain namespace info, if there is any.
21647 Return the computed name or NULL.
21648 Space for the result is allocated on the objfile's obstack.
21649 This is the full-die version of guess_partial_die_structure_name.
21650 In this case we know DIE has no useful parent. */
21651
43816ebc 21652static const char *
abc72ce4
DE
21653guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21654{
21655 struct die_info *spec_die;
21656 struct dwarf2_cu *spec_cu;
21657 struct die_info *child;
5e22e966 21658 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
21659
21660 spec_cu = cu;
21661 spec_die = die_specification (die, &spec_cu);
21662 if (spec_die != NULL)
21663 {
21664 die = spec_die;
21665 cu = spec_cu;
21666 }
21667
21668 for (child = die->child;
21669 child != NULL;
21670 child = child->sibling)
21671 {
21672 if (child->tag == DW_TAG_subprogram)
21673 {
73b9be8b 21674 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21675
7d45c7c3 21676 if (linkage_name != NULL)
abc72ce4 21677 {
43816ebc
TT
21678 gdb::unique_xmalloc_ptr<char> actual_name
21679 (language_class_name_from_physname (cu->language_defn,
21680 linkage_name));
21681 const char *name = NULL;
abc72ce4
DE
21682
21683 if (actual_name != NULL)
21684 {
15d034d0 21685 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21686
21687 if (die_name != NULL
43816ebc 21688 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21689 {
21690 /* Strip off the class name from the full name.
21691 We want the prefix. */
21692 int die_name_len = strlen (die_name);
43816ebc
TT
21693 int actual_name_len = strlen (actual_name.get ());
21694 const char *ptr = actual_name.get ();
abc72ce4
DE
21695
21696 /* Test for '::' as a sanity check. */
21697 if (actual_name_len > die_name_len + 2
43816ebc 21698 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21699 name = obstack_strndup (
e3b94546 21700 &objfile->per_bfd->storage_obstack,
43816ebc 21701 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21702 }
21703 }
abc72ce4
DE
21704 return name;
21705 }
21706 }
21707 }
21708
21709 return NULL;
21710}
21711
96408a79
SA
21712/* GCC might emit a nameless typedef that has a linkage name. Determine the
21713 prefix part in such case. See
21714 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21715
a121b7c1 21716static const char *
96408a79
SA
21717anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21718{
21719 struct attribute *attr;
e6a959d6 21720 const char *base;
96408a79
SA
21721
21722 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21723 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21724 return NULL;
21725
7d45c7c3 21726 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21727 return NULL;
21728
73b9be8b 21729 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21730 if (attr == NULL || DW_STRING (attr) == NULL)
21731 return NULL;
21732
21733 /* dwarf2_name had to be already called. */
21734 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21735
21736 /* Strip the base name, keep any leading namespaces/classes. */
21737 base = strrchr (DW_STRING (attr), ':');
21738 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21739 return "";
21740
5e22e966 21741 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9
TT
21742 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21743 DW_STRING (attr),
21744 &base[-1] - DW_STRING (attr));
96408a79
SA
21745}
21746
fdde2d81 21747/* Return the name of the namespace/class that DIE is defined within,
0114d602 21748 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21749
0114d602
DJ
21750 For example, if we're within the method foo() in the following
21751 code:
21752
21753 namespace N {
21754 class C {
21755 void foo () {
21756 }
21757 };
21758 }
21759
21760 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21761
0d5cff50 21762static const char *
e142c38c 21763determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21764{
976ca316 21765 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
21766 struct die_info *parent, *spec_die;
21767 struct dwarf2_cu *spec_cu;
21768 struct type *parent_type;
a121b7c1 21769 const char *retval;
63d06c5c 21770
9c37b5ae 21771 if (cu->language != language_cplus
c44af4eb
TT
21772 && cu->language != language_fortran && cu->language != language_d
21773 && cu->language != language_rust)
0114d602
DJ
21774 return "";
21775
96408a79
SA
21776 retval = anonymous_struct_prefix (die, cu);
21777 if (retval)
21778 return retval;
21779
0114d602
DJ
21780 /* We have to be careful in the presence of DW_AT_specification.
21781 For example, with GCC 3.4, given the code
21782
21783 namespace N {
21784 void foo() {
21785 // Definition of N::foo.
21786 }
21787 }
21788
21789 then we'll have a tree of DIEs like this:
21790
21791 1: DW_TAG_compile_unit
21792 2: DW_TAG_namespace // N
21793 3: DW_TAG_subprogram // declaration of N::foo
21794 4: DW_TAG_subprogram // definition of N::foo
21795 DW_AT_specification // refers to die #3
21796
21797 Thus, when processing die #4, we have to pretend that we're in
21798 the context of its DW_AT_specification, namely the contex of die
21799 #3. */
21800 spec_cu = cu;
21801 spec_die = die_specification (die, &spec_cu);
21802 if (spec_die == NULL)
21803 parent = die->parent;
21804 else
63d06c5c 21805 {
0114d602
DJ
21806 parent = spec_die->parent;
21807 cu = spec_cu;
63d06c5c 21808 }
0114d602
DJ
21809
21810 if (parent == NULL)
21811 return "";
98bfdba5
PA
21812 else if (parent->building_fullname)
21813 {
21814 const char *name;
21815 const char *parent_name;
21816
21817 /* It has been seen on RealView 2.2 built binaries,
21818 DW_TAG_template_type_param types actually _defined_ as
21819 children of the parent class:
21820
21821 enum E {};
21822 template class <class Enum> Class{};
21823 Class<enum E> class_e;
21824
21825 1: DW_TAG_class_type (Class)
21826 2: DW_TAG_enumeration_type (E)
21827 3: DW_TAG_enumerator (enum1:0)
21828 3: DW_TAG_enumerator (enum2:1)
21829 ...
21830 2: DW_TAG_template_type_param
21831 DW_AT_type DW_FORM_ref_udata (E)
21832
21833 Besides being broken debug info, it can put GDB into an
21834 infinite loop. Consider:
21835
21836 When we're building the full name for Class<E>, we'll start
21837 at Class, and go look over its template type parameters,
21838 finding E. We'll then try to build the full name of E, and
21839 reach here. We're now trying to build the full name of E,
21840 and look over the parent DIE for containing scope. In the
21841 broken case, if we followed the parent DIE of E, we'd again
21842 find Class, and once again go look at its template type
21843 arguments, etc., etc. Simply don't consider such parent die
21844 as source-level parent of this die (it can't be, the language
21845 doesn't allow it), and break the loop here. */
21846 name = dwarf2_name (die, cu);
21847 parent_name = dwarf2_name (parent, cu);
b98664d3 21848 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21849 name ? name : "<unknown>",
21850 parent_name ? parent_name : "<unknown>");
21851 return "";
21852 }
63d06c5c 21853 else
0114d602
DJ
21854 switch (parent->tag)
21855 {
63d06c5c 21856 case DW_TAG_namespace:
0114d602 21857 parent_type = read_type_die (parent, cu);
acebe513
UW
21858 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21859 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21860 Work around this problem here. */
21861 if (cu->language == language_cplus
7d93a1e0 21862 && strcmp (parent_type->name (), "::") == 0)
acebe513 21863 return "";
0114d602 21864 /* We give a name to even anonymous namespaces. */
7d93a1e0 21865 return parent_type->name ();
63d06c5c 21866 case DW_TAG_class_type:
680b30c7 21867 case DW_TAG_interface_type:
63d06c5c 21868 case DW_TAG_structure_type:
0114d602 21869 case DW_TAG_union_type:
f55ee35c 21870 case DW_TAG_module:
0114d602 21871 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
21872 if (parent_type->name () != NULL)
21873 return parent_type->name ();
0114d602
DJ
21874 else
21875 /* An anonymous structure is only allowed non-static data
21876 members; no typedefs, no member functions, et cetera.
21877 So it does not need a prefix. */
21878 return "";
abc72ce4 21879 case DW_TAG_compile_unit:
95554aad 21880 case DW_TAG_partial_unit:
abc72ce4
DE
21881 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21882 if (cu->language == language_cplus
976ca316 21883 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
21884 && die->child != NULL
21885 && (die->tag == DW_TAG_class_type
21886 || die->tag == DW_TAG_structure_type
21887 || die->tag == DW_TAG_union_type))
21888 {
43816ebc 21889 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21890 if (name != NULL)
21891 return name;
21892 }
21893 return "";
0a4b0913
AB
21894 case DW_TAG_subprogram:
21895 /* Nested subroutines in Fortran get a prefix with the name
21896 of the parent's subroutine. */
21897 if (cu->language == language_fortran)
21898 {
21899 if ((die->tag == DW_TAG_subprogram)
21900 && (dwarf2_name (parent, cu) != NULL))
21901 return dwarf2_name (parent, cu);
21902 }
21903 return determine_prefix (parent, cu);
3d567982
TT
21904 case DW_TAG_enumeration_type:
21905 parent_type = read_type_die (parent, cu);
21906 if (TYPE_DECLARED_CLASS (parent_type))
21907 {
7d93a1e0
SM
21908 if (parent_type->name () != NULL)
21909 return parent_type->name ();
3d567982
TT
21910 return "";
21911 }
21912 /* Fall through. */
63d06c5c 21913 default:
8176b9b8 21914 return determine_prefix (parent, cu);
63d06c5c 21915 }
63d06c5c
DC
21916}
21917
3e43a32a
MS
21918/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21919 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21920 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21921 an obconcat, otherwise allocate storage for the result. The CU argument is
21922 used to determine the language and hence, the appropriate separator. */
987504bb 21923
f55ee35c 21924#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21925
21926static char *
f55ee35c
JK
21927typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21928 int physname, struct dwarf2_cu *cu)
63d06c5c 21929{
f55ee35c 21930 const char *lead = "";
5c315b68 21931 const char *sep;
63d06c5c 21932
3e43a32a
MS
21933 if (suffix == NULL || suffix[0] == '\0'
21934 || prefix == NULL || prefix[0] == '\0')
987504bb 21935 sep = "";
45280282
IB
21936 else if (cu->language == language_d)
21937 {
21938 /* For D, the 'main' function could be defined in any module, but it
21939 should never be prefixed. */
21940 if (strcmp (suffix, "D main") == 0)
21941 {
21942 prefix = "";
21943 sep = "";
21944 }
21945 else
21946 sep = ".";
21947 }
f55ee35c
JK
21948 else if (cu->language == language_fortran && physname)
21949 {
21950 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21951 DW_AT_MIPS_linkage_name is preferred and used instead. */
21952
21953 lead = "__";
21954 sep = "_MOD_";
21955 }
987504bb
JJ
21956 else
21957 sep = "::";
63d06c5c 21958
6dd47d34
DE
21959 if (prefix == NULL)
21960 prefix = "";
21961 if (suffix == NULL)
21962 suffix = "";
21963
987504bb
JJ
21964 if (obs == NULL)
21965 {
3e43a32a 21966 char *retval
224c3ddb
SM
21967 = ((char *)
21968 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21969
f55ee35c
JK
21970 strcpy (retval, lead);
21971 strcat (retval, prefix);
6dd47d34
DE
21972 strcat (retval, sep);
21973 strcat (retval, suffix);
63d06c5c
DC
21974 return retval;
21975 }
987504bb
JJ
21976 else
21977 {
21978 /* We have an obstack. */
f55ee35c 21979 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21980 }
63d06c5c
DC
21981}
21982
71c25dea
TT
21983/* Get name of a die, return NULL if not found. */
21984
15d034d0
TT
21985static const char *
21986dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21987 struct objfile *objfile)
71c25dea
TT
21988{
21989 if (name && cu->language == language_cplus)
21990 {
596dc4ad
TT
21991 gdb::unique_xmalloc_ptr<char> canon_name
21992 = cp_canonicalize_string (name);
71c25dea 21993
596dc4ad
TT
21994 if (canon_name != nullptr)
21995 name = objfile->intern (canon_name.get ());
71c25dea
TT
21996 }
21997
21998 return name;
c906108c
SS
21999}
22000
96553a0c
DE
22001/* Get name of a die, return NULL if not found.
22002 Anonymous namespaces are converted to their magic string. */
9219021c 22003
15d034d0 22004static const char *
e142c38c 22005dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22006{
22007 struct attribute *attr;
5e22e966 22008 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 22009
e142c38c 22010 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22011 if ((!attr || !DW_STRING (attr))
96553a0c 22012 && die->tag != DW_TAG_namespace
53832f31
TT
22013 && die->tag != DW_TAG_class_type
22014 && die->tag != DW_TAG_interface_type
22015 && die->tag != DW_TAG_structure_type
22016 && die->tag != DW_TAG_union_type)
71c25dea
TT
22017 return NULL;
22018
22019 switch (die->tag)
22020 {
22021 case DW_TAG_compile_unit:
95554aad 22022 case DW_TAG_partial_unit:
71c25dea
TT
22023 /* Compilation units have a DW_AT_name that is a filename, not
22024 a source language identifier. */
22025 case DW_TAG_enumeration_type:
22026 case DW_TAG_enumerator:
22027 /* These tags always have simple identifiers already; no need
22028 to canonicalize them. */
22029 return DW_STRING (attr);
907af001 22030
96553a0c
DE
22031 case DW_TAG_namespace:
22032 if (attr != NULL && DW_STRING (attr) != NULL)
22033 return DW_STRING (attr);
22034 return CP_ANONYMOUS_NAMESPACE_STR;
22035
907af001
UW
22036 case DW_TAG_class_type:
22037 case DW_TAG_interface_type:
22038 case DW_TAG_structure_type:
22039 case DW_TAG_union_type:
22040 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22041 structures or unions. These were of the form "._%d" in GCC 4.1,
22042 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22043 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22044 if (attr && DW_STRING (attr)
61012eef
GB
22045 && (startswith (DW_STRING (attr), "._")
22046 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22047 return NULL;
53832f31
TT
22048
22049 /* GCC might emit a nameless typedef that has a linkage name. See
22050 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22051 if (!attr || DW_STRING (attr) == NULL)
22052 {
73b9be8b 22053 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22054 if (attr == NULL || DW_STRING (attr) == NULL)
22055 return NULL;
22056
df5c6c50
JK
22057 /* Avoid demangling DW_STRING (attr) the second time on a second
22058 call for the same DIE. */
22059 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 22060 {
43816ebc
TT
22061 gdb::unique_xmalloc_ptr<char> demangled
22062 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
22063 if (demangled == nullptr)
22064 return nullptr;
43816ebc 22065
be1e3d3e 22066 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 22067 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 22068 }
67430cd0
TT
22069
22070 /* Strip any leading namespaces/classes, keep only the base name.
22071 DW_AT_name for named DIEs does not contain the prefixes. */
22072 const char *base = strrchr (DW_STRING (attr), ':');
22073 if (base && base > DW_STRING (attr) && base[-1] == ':')
22074 return &base[1];
22075 else
22076 return DW_STRING (attr);
53832f31 22077 }
907af001
UW
22078 break;
22079
71c25dea 22080 default:
907af001
UW
22081 break;
22082 }
22083
22084 if (!DW_STRING_IS_CANONICAL (attr))
22085 {
be1e3d3e
TT
22086 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22087 objfile);
907af001 22088 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22089 }
907af001 22090 return DW_STRING (attr);
9219021c
DC
22091}
22092
22093/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22094 is none. *EXT_CU is the CU containing DIE on input, and the CU
22095 containing the return value on output. */
9219021c
DC
22096
22097static struct die_info *
f2f0e013 22098dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22099{
22100 struct attribute *attr;
9219021c 22101
f2f0e013 22102 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22103 if (attr == NULL)
22104 return NULL;
22105
f2f0e013 22106 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22107}
22108
f9aca02d 22109static void
d97bc12b 22110dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22111{
22112 unsigned int i;
22113
d97bc12b 22114 print_spaces (indent, f);
9d8780f0 22115 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22116 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22117 sect_offset_str (die->sect_off));
d97bc12b
DE
22118
22119 if (die->parent != NULL)
22120 {
22121 print_spaces (indent, f);
9d8780f0
SM
22122 fprintf_unfiltered (f, " parent at offset: %s\n",
22123 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22124 }
22125
22126 print_spaces (indent, f);
22127 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22128 dwarf_bool_name (die->child != NULL));
c906108c 22129
d97bc12b
DE
22130 print_spaces (indent, f);
22131 fprintf_unfiltered (f, " attributes:\n");
22132
c906108c
SS
22133 for (i = 0; i < die->num_attrs; ++i)
22134 {
d97bc12b
DE
22135 print_spaces (indent, f);
22136 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22137 dwarf_attr_name (die->attrs[i].name),
22138 dwarf_form_name (die->attrs[i].form));
d97bc12b 22139
c906108c
SS
22140 switch (die->attrs[i].form)
22141 {
c906108c 22142 case DW_FORM_addr:
336d760d 22143 case DW_FORM_addrx:
3019eac3 22144 case DW_FORM_GNU_addr_index:
d97bc12b 22145 fprintf_unfiltered (f, "address: ");
5af949e3 22146 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22147 break;
22148 case DW_FORM_block2:
22149 case DW_FORM_block4:
22150 case DW_FORM_block:
22151 case DW_FORM_block1:
56eb65bd
SP
22152 fprintf_unfiltered (f, "block: size %s",
22153 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22154 break;
2dc7f7b3 22155 case DW_FORM_exprloc:
56eb65bd
SP
22156 fprintf_unfiltered (f, "expression: size %s",
22157 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22158 break;
0224619f
JK
22159 case DW_FORM_data16:
22160 fprintf_unfiltered (f, "constant of 16 bytes");
22161 break;
4568ecf9
DE
22162 case DW_FORM_ref_addr:
22163 fprintf_unfiltered (f, "ref address: ");
22164 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22165 break;
36586728
TT
22166 case DW_FORM_GNU_ref_alt:
22167 fprintf_unfiltered (f, "alt ref address: ");
22168 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22169 break;
10b3939b
DJ
22170 case DW_FORM_ref1:
22171 case DW_FORM_ref2:
22172 case DW_FORM_ref4:
4568ecf9
DE
22173 case DW_FORM_ref8:
22174 case DW_FORM_ref_udata:
d97bc12b 22175 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22176 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22177 break;
c906108c
SS
22178 case DW_FORM_data1:
22179 case DW_FORM_data2:
22180 case DW_FORM_data4:
ce5d95e1 22181 case DW_FORM_data8:
c906108c
SS
22182 case DW_FORM_udata:
22183 case DW_FORM_sdata:
43bbcdc2
PH
22184 fprintf_unfiltered (f, "constant: %s",
22185 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22186 break;
2dc7f7b3
TT
22187 case DW_FORM_sec_offset:
22188 fprintf_unfiltered (f, "section offset: %s",
22189 pulongest (DW_UNSND (&die->attrs[i])));
22190 break;
55f1336d 22191 case DW_FORM_ref_sig8:
ac9ec31b
DE
22192 fprintf_unfiltered (f, "signature: %s",
22193 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22194 break;
c906108c 22195 case DW_FORM_string:
4bdf3d34 22196 case DW_FORM_strp:
43988095 22197 case DW_FORM_line_strp:
cf532bd1 22198 case DW_FORM_strx:
3019eac3 22199 case DW_FORM_GNU_str_index:
36586728 22200 case DW_FORM_GNU_strp_alt:
8285870a 22201 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22202 DW_STRING (&die->attrs[i])
8285870a
JK
22203 ? DW_STRING (&die->attrs[i]) : "",
22204 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22205 break;
22206 case DW_FORM_flag:
22207 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22208 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22209 else
d97bc12b 22210 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22211 break;
2dc7f7b3
TT
22212 case DW_FORM_flag_present:
22213 fprintf_unfiltered (f, "flag: TRUE");
22214 break;
a8329558 22215 case DW_FORM_indirect:
0963b4bd
MS
22216 /* The reader will have reduced the indirect form to
22217 the "base form" so this form should not occur. */
5f48f8f3 22218 fprintf_unfiltered (f,
3e43a32a 22219 "unexpected attribute form: DW_FORM_indirect");
a8329558 22220 break;
663c44ac
JK
22221 case DW_FORM_implicit_const:
22222 fprintf_unfiltered (f, "constant: %s",
22223 plongest (DW_SND (&die->attrs[i])));
22224 break;
c906108c 22225 default:
d97bc12b 22226 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22227 die->attrs[i].form);
d97bc12b 22228 break;
c906108c 22229 }
d97bc12b 22230 fprintf_unfiltered (f, "\n");
c906108c
SS
22231 }
22232}
22233
f9aca02d 22234static void
d97bc12b 22235dump_die_for_error (struct die_info *die)
c906108c 22236{
d97bc12b
DE
22237 dump_die_shallow (gdb_stderr, 0, die);
22238}
22239
22240static void
22241dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22242{
22243 int indent = level * 4;
22244
22245 gdb_assert (die != NULL);
22246
22247 if (level >= max_level)
22248 return;
22249
22250 dump_die_shallow (f, indent, die);
22251
22252 if (die->child != NULL)
c906108c 22253 {
d97bc12b
DE
22254 print_spaces (indent, f);
22255 fprintf_unfiltered (f, " Children:");
22256 if (level + 1 < max_level)
22257 {
22258 fprintf_unfiltered (f, "\n");
22259 dump_die_1 (f, level + 1, max_level, die->child);
22260 }
22261 else
22262 {
3e43a32a
MS
22263 fprintf_unfiltered (f,
22264 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22265 }
22266 }
22267
22268 if (die->sibling != NULL && level > 0)
22269 {
22270 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22271 }
22272}
22273
d97bc12b
DE
22274/* This is called from the pdie macro in gdbinit.in.
22275 It's not static so gcc will keep a copy callable from gdb. */
22276
22277void
22278dump_die (struct die_info *die, int max_level)
22279{
22280 dump_die_1 (gdb_stdlog, 0, max_level, die);
22281}
22282
f9aca02d 22283static void
51545339 22284store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22285{
51545339 22286 void **slot;
c906108c 22287
9c541725
PA
22288 slot = htab_find_slot_with_hash (cu->die_hash, die,
22289 to_underlying (die->sect_off),
b64f50a1 22290 INSERT);
51545339
DJ
22291
22292 *slot = die;
c906108c
SS
22293}
22294
348e048f
DE
22295/* Follow reference or signature attribute ATTR of SRC_DIE.
22296 On entry *REF_CU is the CU of SRC_DIE.
22297 On exit *REF_CU is the CU of the result. */
22298
22299static struct die_info *
ff39bb5e 22300follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22301 struct dwarf2_cu **ref_cu)
22302{
22303 struct die_info *die;
22304
cd6c91b4 22305 if (attr->form_is_ref ())
348e048f 22306 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22307 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22308 die = follow_die_sig (src_die, attr, ref_cu);
22309 else
22310 {
22311 dump_die_for_error (src_die);
22312 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 22313 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
22314 }
22315
22316 return die;
03dd20cc
DJ
22317}
22318
5c631832 22319/* Follow reference OFFSET.
673bfd45
DE
22320 On entry *REF_CU is the CU of the source die referencing OFFSET.
22321 On exit *REF_CU is the CU of the result.
22322 Returns NULL if OFFSET is invalid. */
f504f079 22323
f9aca02d 22324static struct die_info *
9c541725 22325follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22326 struct dwarf2_cu **ref_cu)
c906108c 22327{
10b3939b 22328 struct die_info temp_die;
f2f0e013 22329 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 22330 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 22331
348e048f
DE
22332 gdb_assert (cu->per_cu != NULL);
22333
98bfdba5
PA
22334 target_cu = cu;
22335
3019eac3 22336 if (cu->per_cu->is_debug_types)
348e048f
DE
22337 {
22338 /* .debug_types CUs cannot reference anything outside their CU.
22339 If they need to, they have to reference a signatured type via
55f1336d 22340 DW_FORM_ref_sig8. */
4057dfde 22341 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22342 return NULL;
348e048f 22343 }
36586728 22344 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22345 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22346 {
22347 struct dwarf2_per_cu_data *per_cu;
9a619af0 22348
9c541725 22349 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 22350 per_objfile);
03dd20cc
DJ
22351
22352 /* If necessary, add it to the queue and load its DIEs. */
976ca316
SM
22353 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
22354 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
03dd20cc 22355
976ca316 22356 target_cu = per_objfile->get_cu (per_cu);
10b3939b 22357 }
98bfdba5
PA
22358 else if (cu->dies == NULL)
22359 {
22360 /* We're loading full DIEs during partial symbol reading. */
976ca316
SM
22361 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
22362 load_full_comp_unit (cu->per_cu, per_objfile, false, language_minimal);
98bfdba5 22363 }
c906108c 22364
f2f0e013 22365 *ref_cu = target_cu;
9c541725 22366 temp_die.sect_off = sect_off;
c24bdb02
KS
22367
22368 if (target_cu != cu)
22369 target_cu->ancestor = cu;
22370
9a3c8263 22371 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22372 &temp_die,
22373 to_underlying (sect_off));
5c631832 22374}
10b3939b 22375
5c631832
JK
22376/* Follow reference attribute ATTR of SRC_DIE.
22377 On entry *REF_CU is the CU of SRC_DIE.
22378 On exit *REF_CU is the CU of the result. */
22379
22380static struct die_info *
ff39bb5e 22381follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22382 struct dwarf2_cu **ref_cu)
22383{
0826b30a 22384 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22385 struct dwarf2_cu *cu = *ref_cu;
22386 struct die_info *die;
22387
9c541725 22388 die = follow_die_offset (sect_off,
36586728
TT
22389 (attr->form == DW_FORM_GNU_ref_alt
22390 || cu->per_cu->is_dwz),
22391 ref_cu);
5c631832 22392 if (!die)
9d8780f0
SM
22393 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22394 "at %s [in module %s]"),
22395 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 22396 objfile_name (cu->per_objfile->objfile));
348e048f 22397
5c631832
JK
22398 return die;
22399}
22400
d4c9a4f8 22401/* See read.h. */
5c631832
JK
22402
22403struct dwarf2_locexpr_baton
9c541725 22404dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22405 dwarf2_per_cu_data *per_cu,
976ca316 22406 dwarf2_per_objfile *per_objfile,
8b9737bf 22407 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22408 void *baton, bool resolve_abstract_p)
5c631832 22409{
5c631832
JK
22410 struct die_info *die;
22411 struct attribute *attr;
22412 struct dwarf2_locexpr_baton retval;
976ca316 22413 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 22414
976ca316 22415 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 22416 if (cu == nullptr)
976ca316 22417 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
22418
22419 if (cu == nullptr)
cc12ce38
DE
22420 {
22421 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22422 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22423 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22424 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22425 }
918dd910 22426
9c541725 22427 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22428 if (!die)
9d8780f0
SM
22429 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22430 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22431
22432 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22433 if (!attr && resolve_abstract_p
976ca316
SM
22434 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22435 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65
TV
22436 {
22437 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22438 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 22439 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 22440
3360b6e7 22441 for (const auto &cand_off
976ca316 22442 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 22443 {
3360b6e7
TV
22444 struct dwarf2_cu *cand_cu = cu;
22445 struct die_info *cand
22446 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22447 if (!cand
22448 || !cand->parent
e4a62c65
TV
22449 || cand->parent->tag != DW_TAG_subprogram)
22450 continue;
22451
22452 CORE_ADDR pc_low, pc_high;
22453 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22454 if (pc_low == ((CORE_ADDR) -1))
22455 continue;
22456 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22457 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22458 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22459 continue;
22460
22461 die = cand;
22462 attr = dwarf2_attr (die, DW_AT_location, cu);
22463 break;
22464 }
22465 }
22466
5c631832
JK
22467 if (!attr)
22468 {
e103e986
JK
22469 /* DWARF: "If there is no such attribute, then there is no effect.".
22470 DATA is ignored if SIZE is 0. */
5c631832 22471
e103e986 22472 retval.data = NULL;
5c631832
JK
22473 retval.size = 0;
22474 }
cd6c91b4 22475 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22476 {
22477 struct dwarf2_loclist_baton loclist_baton;
22478 CORE_ADDR pc = (*get_frame_pc) (baton);
22479 size_t size;
22480
22481 fill_in_loclist_baton (cu, &loclist_baton, attr);
22482
22483 retval.data = dwarf2_find_location_expression (&loclist_baton,
22484 &size, pc);
22485 retval.size = size;
22486 }
5c631832
JK
22487 else
22488 {
4fc6c0d5 22489 if (!attr->form_is_block ())
9d8780f0 22490 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22491 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22492 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22493
22494 retval.data = DW_BLOCK (attr)->data;
22495 retval.size = DW_BLOCK (attr)->size;
22496 }
976ca316 22497 retval.per_objfile = per_objfile;
5c631832 22498 retval.per_cu = cu->per_cu;
918dd910 22499
976ca316 22500 per_objfile->age_comp_units ();
918dd910 22501
5c631832 22502 return retval;
348e048f
DE
22503}
22504
d4c9a4f8 22505/* See read.h. */
8b9737bf
TT
22506
22507struct dwarf2_locexpr_baton
22508dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22509 dwarf2_per_cu_data *per_cu,
14095eb3 22510 dwarf2_per_objfile *per_objfile,
8b9737bf
TT
22511 CORE_ADDR (*get_frame_pc) (void *baton),
22512 void *baton)
22513{
9c541725 22514 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22515
14095eb3
SM
22516 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22517 get_frame_pc, baton);
8b9737bf
TT
22518}
22519
b6807d98
TT
22520/* Write a constant of a given type as target-ordered bytes into
22521 OBSTACK. */
22522
22523static const gdb_byte *
22524write_constant_as_bytes (struct obstack *obstack,
22525 enum bfd_endian byte_order,
22526 struct type *type,
22527 ULONGEST value,
22528 LONGEST *len)
22529{
22530 gdb_byte *result;
22531
22532 *len = TYPE_LENGTH (type);
224c3ddb 22533 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22534 store_unsigned_integer (result, *len, byte_order, value);
22535
22536 return result;
22537}
22538
d4c9a4f8 22539/* See read.h. */
b6807d98
TT
22540
22541const gdb_byte *
9c541725 22542dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 22543 dwarf2_per_cu_data *per_cu,
14095eb3 22544 dwarf2_per_objfile *per_objfile,
d4c9a4f8 22545 obstack *obstack,
b6807d98
TT
22546 LONGEST *len)
22547{
b6807d98
TT
22548 struct die_info *die;
22549 struct attribute *attr;
22550 const gdb_byte *result = NULL;
22551 struct type *type;
22552 LONGEST value;
22553 enum bfd_endian byte_order;
14095eb3 22554 struct objfile *objfile = per_objfile->objfile;
b6807d98 22555
7188ed02 22556 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
22557 if (cu == nullptr)
22558 cu = load_cu (per_cu, per_objfile, false);
22559
22560 if (cu == nullptr)
cc12ce38
DE
22561 {
22562 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22563 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22564 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22565 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22566 }
b6807d98 22567
9c541725 22568 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22569 if (!die)
9d8780f0
SM
22570 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22571 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22572
22573 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22574 if (attr == NULL)
22575 return NULL;
22576
e3b94546 22577 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22578 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22579
22580 switch (attr->form)
22581 {
22582 case DW_FORM_addr:
336d760d 22583 case DW_FORM_addrx:
b6807d98
TT
22584 case DW_FORM_GNU_addr_index:
22585 {
22586 gdb_byte *tem;
22587
22588 *len = cu->header.addr_size;
224c3ddb 22589 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22590 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22591 result = tem;
22592 }
22593 break;
22594 case DW_FORM_string:
22595 case DW_FORM_strp:
cf532bd1 22596 case DW_FORM_strx:
b6807d98
TT
22597 case DW_FORM_GNU_str_index:
22598 case DW_FORM_GNU_strp_alt:
22599 /* DW_STRING is already allocated on the objfile obstack, point
22600 directly to it. */
22601 result = (const gdb_byte *) DW_STRING (attr);
22602 *len = strlen (DW_STRING (attr));
22603 break;
22604 case DW_FORM_block1:
22605 case DW_FORM_block2:
22606 case DW_FORM_block4:
22607 case DW_FORM_block:
22608 case DW_FORM_exprloc:
0224619f 22609 case DW_FORM_data16:
b6807d98
TT
22610 result = DW_BLOCK (attr)->data;
22611 *len = DW_BLOCK (attr)->size;
22612 break;
22613
22614 /* The DW_AT_const_value attributes are supposed to carry the
22615 symbol's value "represented as it would be on the target
22616 architecture." By the time we get here, it's already been
22617 converted to host endianness, so we just need to sign- or
22618 zero-extend it as appropriate. */
22619 case DW_FORM_data1:
22620 type = die_type (die, cu);
22621 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22622 if (result == NULL)
22623 result = write_constant_as_bytes (obstack, byte_order,
22624 type, value, len);
22625 break;
22626 case DW_FORM_data2:
22627 type = die_type (die, cu);
22628 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22629 if (result == NULL)
22630 result = write_constant_as_bytes (obstack, byte_order,
22631 type, value, len);
22632 break;
22633 case DW_FORM_data4:
22634 type = die_type (die, cu);
22635 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22636 if (result == NULL)
22637 result = write_constant_as_bytes (obstack, byte_order,
22638 type, value, len);
22639 break;
22640 case DW_FORM_data8:
22641 type = die_type (die, cu);
22642 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22643 if (result == NULL)
22644 result = write_constant_as_bytes (obstack, byte_order,
22645 type, value, len);
22646 break;
22647
22648 case DW_FORM_sdata:
663c44ac 22649 case DW_FORM_implicit_const:
b6807d98
TT
22650 type = die_type (die, cu);
22651 result = write_constant_as_bytes (obstack, byte_order,
22652 type, DW_SND (attr), len);
22653 break;
22654
22655 case DW_FORM_udata:
22656 type = die_type (die, cu);
22657 result = write_constant_as_bytes (obstack, byte_order,
22658 type, DW_UNSND (attr), len);
22659 break;
22660
22661 default:
b98664d3 22662 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22663 dwarf_form_name (attr->form));
22664 break;
22665 }
22666
22667 return result;
22668}
22669
d4c9a4f8 22670/* See read.h. */
7942e96e
AA
22671
22672struct type *
9c541725 22673dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
22674 dwarf2_per_cu_data *per_cu,
22675 dwarf2_per_objfile *per_objfile)
7942e96e 22676{
7942e96e
AA
22677 struct die_info *die;
22678
7188ed02 22679 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
22680 if (cu == nullptr)
22681 cu = load_cu (per_cu, per_objfile, false);
22682
22683 if (cu == nullptr)
22684 return nullptr;
7942e96e 22685
9c541725 22686 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22687 if (!die)
22688 return NULL;
22689
22690 return die_type (die, cu);
22691}
22692
8cb5117c 22693/* See read.h. */
8a9b8146
TT
22694
22695struct type *
b64f50a1 22696dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
22697 dwarf2_per_cu_data *per_cu,
22698 dwarf2_per_objfile *per_objfile)
8a9b8146 22699{
9c541725 22700 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 22701 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
22702}
22703
ac9ec31b 22704/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22705 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22706 On exit *REF_CU is the CU of the result.
22707 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22708
22709static struct die_info *
ac9ec31b
DE
22710follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22711 struct dwarf2_cu **ref_cu)
348e048f 22712{
348e048f 22713 struct die_info temp_die;
c24bdb02 22714 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 22715 struct die_info *die;
976ca316 22716 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 22717
348e048f 22718
ac9ec31b
DE
22719 /* While it might be nice to assert sig_type->type == NULL here,
22720 we can get here for DW_AT_imported_declaration where we need
22721 the DIE not the type. */
348e048f
DE
22722
22723 /* If necessary, add it to the queue and load its DIEs. */
22724
976ca316 22725 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
120ce1b5 22726 language_minimal))
976ca316 22727 read_signatured_type (sig_type, per_objfile);
348e048f 22728
976ca316 22729 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
69d751e3 22730 gdb_assert (sig_cu != NULL);
9c541725
PA
22731 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22732 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22733 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22734 to_underlying (temp_die.sect_off));
348e048f
DE
22735 if (die)
22736 {
796a7ff8
DE
22737 /* For .gdb_index version 7 keep track of included TUs.
22738 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
22739 if (per_objfile->per_bfd->index_table != NULL
22740 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 22741 {
ae640021 22742 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22743 }
22744
348e048f 22745 *ref_cu = sig_cu;
c24bdb02
KS
22746 if (sig_cu != cu)
22747 sig_cu->ancestor = cu;
22748
348e048f
DE
22749 return die;
22750 }
22751
ac9ec31b
DE
22752 return NULL;
22753}
22754
22755/* Follow signatured type referenced by ATTR in SRC_DIE.
22756 On entry *REF_CU is the CU of SRC_DIE.
22757 On exit *REF_CU is the CU of the result.
22758 The result is the DIE of the type.
22759 If the referenced type cannot be found an error is thrown. */
22760
22761static struct die_info *
ff39bb5e 22762follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22763 struct dwarf2_cu **ref_cu)
22764{
22765 ULONGEST signature = DW_SIGNATURE (attr);
22766 struct signatured_type *sig_type;
22767 struct die_info *die;
22768
22769 gdb_assert (attr->form == DW_FORM_ref_sig8);
22770
a2ce51a0 22771 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22772 /* sig_type will be NULL if the signatured type is missing from
22773 the debug info. */
22774 if (sig_type == NULL)
22775 {
22776 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22777 " from DIE at %s [in module %s]"),
22778 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22779 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22780 }
22781
22782 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22783 if (die == NULL)
22784 {
22785 dump_die_for_error (src_die);
22786 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22787 " from DIE at %s [in module %s]"),
22788 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22789 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22790 }
22791
22792 return die;
22793}
22794
22795/* Get the type specified by SIGNATURE referenced in DIE/CU,
22796 reading in and processing the type unit if necessary. */
22797
22798static struct type *
22799get_signatured_type (struct die_info *die, ULONGEST signature,
22800 struct dwarf2_cu *cu)
22801{
976ca316 22802 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
22803 struct signatured_type *sig_type;
22804 struct dwarf2_cu *type_cu;
22805 struct die_info *type_die;
22806 struct type *type;
22807
a2ce51a0 22808 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22809 /* sig_type will be NULL if the signatured type is missing from
22810 the debug info. */
22811 if (sig_type == NULL)
22812 {
b98664d3 22813 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22814 " from DIE at %s [in module %s]"),
22815 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 22816 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22817 return build_error_marker_type (cu, die);
22818 }
22819
22820 /* If we already know the type we're done. */
976ca316 22821 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
22822 if (type != nullptr)
22823 return type;
ac9ec31b
DE
22824
22825 type_cu = cu;
22826 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22827 if (type_die != NULL)
22828 {
22829 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22830 is created. This is important, for example, because for c++ classes
22831 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22832 type = read_type_die (type_die, type_cu);
22833 if (type == NULL)
22834 {
b98664d3 22835 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22836 " referenced from DIE at %s [in module %s]"),
22837 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 22838 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22839 type = build_error_marker_type (cu, die);
22840 }
22841 }
22842 else
22843 {
b98664d3 22844 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22845 " from DIE at %s [in module %s]"),
22846 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 22847 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22848 type = build_error_marker_type (cu, die);
22849 }
e286671b 22850
976ca316 22851 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
22852
22853 return type;
22854}
22855
22856/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22857 reading in and processing the type unit if necessary. */
22858
22859static struct type *
ff39bb5e 22860get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22861 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22862{
22863 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22864 if (attr->form_is_ref ())
ac9ec31b
DE
22865 {
22866 struct dwarf2_cu *type_cu = cu;
22867 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22868
22869 return read_type_die (type_die, type_cu);
22870 }
22871 else if (attr->form == DW_FORM_ref_sig8)
22872 {
22873 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22874 }
22875 else
22876 {
976ca316 22877 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 22878
b98664d3 22879 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22880 " at %s [in module %s]"),
22881 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 22882 objfile_name (per_objfile->objfile));
ac9ec31b
DE
22883 return build_error_marker_type (cu, die);
22884 }
348e048f
DE
22885}
22886
e5fe5e75 22887/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22888
22889static void
ab432490
SM
22890load_full_type_unit (dwarf2_per_cu_data *per_cu,
22891 dwarf2_per_objfile *per_objfile)
348e048f 22892{
52dc124a 22893 struct signatured_type *sig_type;
348e048f 22894
f4dc4d17 22895 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22896 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22897
6721b2ec
DE
22898 /* We have the per_cu, but we need the signatured_type.
22899 Fortunately this is an easy translation. */
22900 gdb_assert (per_cu->is_debug_types);
22901 sig_type = (struct signatured_type *) per_cu;
348e048f 22902
7188ed02 22903 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 22904
ab432490 22905 read_signatured_type (sig_type, per_objfile);
348e048f 22906
7188ed02 22907 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
22908}
22909
3019eac3
DE
22910/* Read in a signatured type and build its CU and DIEs.
22911 If the type is a stub for the real type in a DWO file,
22912 read in the real type from the DWO file as well. */
dee91e82
DE
22913
22914static void
ab432490
SM
22915read_signatured_type (signatured_type *sig_type,
22916 dwarf2_per_objfile *per_objfile)
dee91e82
DE
22917{
22918 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22919
3019eac3 22920 gdb_assert (per_cu->is_debug_types);
7188ed02 22921 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 22922
2e671100 22923 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
22924
22925 if (!reader.dummy_p)
22926 {
22927 struct dwarf2_cu *cu = reader.cu;
22928 const gdb_byte *info_ptr = reader.info_ptr;
22929
22930 gdb_assert (cu->die_hash == NULL);
22931 cu->die_hash =
22932 htab_create_alloc_ex (cu->header.length / 12,
22933 die_hash,
22934 die_eq,
22935 NULL,
22936 &cu->comp_unit_obstack,
22937 hashtab_obstack_allocate,
22938 dummy_obstack_deallocate);
22939
3e225074 22940 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22941 reader.comp_unit_die->child
22942 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22943 reader.comp_unit_die);
22944 cu->dies = reader.comp_unit_die;
22945 /* comp_unit_die is not stored in die_hash, no need. */
22946
22947 /* We try not to read any attributes in this function, because
22948 not all CUs needed for references have been loaded yet, and
22949 symbol table processing isn't initialized. But we have to
22950 set the CU language, or we won't be able to build types
22951 correctly. Similarly, if we do not read the producer, we can
22952 not apply producer-specific interpretation. */
22953 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22954
22955 reader.keep ();
c0ab21c2
TT
22956 }
22957
7ee85ab1 22958 sig_type->per_cu.tu_read = 1;
c906108c
SS
22959}
22960
c906108c
SS
22961/* Decode simple location descriptions.
22962 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
22963 the location and return the value. If COMPUTED is non-null, it is
22964 set to true to indicate that decoding was successful, and false
22965 otherwise. If COMPUTED is null, then this function may emit a
22966 complaint. */
c906108c
SS
22967
22968static CORE_ADDR
7d79de9a 22969decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 22970{
5e22e966 22971 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
22972 size_t i;
22973 size_t size = blk->size;
d521ce57 22974 const gdb_byte *data = blk->data;
21ae7a4d
JK
22975 CORE_ADDR stack[64];
22976 int stacki;
22977 unsigned int bytes_read, unsnd;
22978 gdb_byte op;
c906108c 22979
7d79de9a
TT
22980 if (computed != nullptr)
22981 *computed = false;
22982
21ae7a4d
JK
22983 i = 0;
22984 stacki = 0;
22985 stack[stacki] = 0;
22986 stack[++stacki] = 0;
22987
22988 while (i < size)
22989 {
22990 op = data[i++];
22991 switch (op)
22992 {
22993 case DW_OP_lit0:
22994 case DW_OP_lit1:
22995 case DW_OP_lit2:
22996 case DW_OP_lit3:
22997 case DW_OP_lit4:
22998 case DW_OP_lit5:
22999 case DW_OP_lit6:
23000 case DW_OP_lit7:
23001 case DW_OP_lit8:
23002 case DW_OP_lit9:
23003 case DW_OP_lit10:
23004 case DW_OP_lit11:
23005 case DW_OP_lit12:
23006 case DW_OP_lit13:
23007 case DW_OP_lit14:
23008 case DW_OP_lit15:
23009 case DW_OP_lit16:
23010 case DW_OP_lit17:
23011 case DW_OP_lit18:
23012 case DW_OP_lit19:
23013 case DW_OP_lit20:
23014 case DW_OP_lit21:
23015 case DW_OP_lit22:
23016 case DW_OP_lit23:
23017 case DW_OP_lit24:
23018 case DW_OP_lit25:
23019 case DW_OP_lit26:
23020 case DW_OP_lit27:
23021 case DW_OP_lit28:
23022 case DW_OP_lit29:
23023 case DW_OP_lit30:
23024 case DW_OP_lit31:
23025 stack[++stacki] = op - DW_OP_lit0;
23026 break;
f1bea926 23027
21ae7a4d
JK
23028 case DW_OP_reg0:
23029 case DW_OP_reg1:
23030 case DW_OP_reg2:
23031 case DW_OP_reg3:
23032 case DW_OP_reg4:
23033 case DW_OP_reg5:
23034 case DW_OP_reg6:
23035 case DW_OP_reg7:
23036 case DW_OP_reg8:
23037 case DW_OP_reg9:
23038 case DW_OP_reg10:
23039 case DW_OP_reg11:
23040 case DW_OP_reg12:
23041 case DW_OP_reg13:
23042 case DW_OP_reg14:
23043 case DW_OP_reg15:
23044 case DW_OP_reg16:
23045 case DW_OP_reg17:
23046 case DW_OP_reg18:
23047 case DW_OP_reg19:
23048 case DW_OP_reg20:
23049 case DW_OP_reg21:
23050 case DW_OP_reg22:
23051 case DW_OP_reg23:
23052 case DW_OP_reg24:
23053 case DW_OP_reg25:
23054 case DW_OP_reg26:
23055 case DW_OP_reg27:
23056 case DW_OP_reg28:
23057 case DW_OP_reg29:
23058 case DW_OP_reg30:
23059 case DW_OP_reg31:
23060 stack[++stacki] = op - DW_OP_reg0;
23061 if (i < size)
7d79de9a
TT
23062 {
23063 if (computed == nullptr)
23064 dwarf2_complex_location_expr_complaint ();
23065 else
23066 return 0;
23067 }
21ae7a4d 23068 break;
c906108c 23069
21ae7a4d
JK
23070 case DW_OP_regx:
23071 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23072 i += bytes_read;
23073 stack[++stacki] = unsnd;
23074 if (i < size)
7d79de9a
TT
23075 {
23076 if (computed == nullptr)
23077 dwarf2_complex_location_expr_complaint ();
23078 else
23079 return 0;
23080 }
21ae7a4d 23081 break;
c906108c 23082
21ae7a4d 23083 case DW_OP_addr:
c8a7a66f
TT
23084 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23085 &bytes_read);
21ae7a4d
JK
23086 i += bytes_read;
23087 break;
d53d4ac5 23088
21ae7a4d
JK
23089 case DW_OP_const1u:
23090 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23091 i += 1;
23092 break;
23093
23094 case DW_OP_const1s:
23095 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23096 i += 1;
23097 break;
23098
23099 case DW_OP_const2u:
23100 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23101 i += 2;
23102 break;
23103
23104 case DW_OP_const2s:
23105 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23106 i += 2;
23107 break;
d53d4ac5 23108
21ae7a4d
JK
23109 case DW_OP_const4u:
23110 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23111 i += 4;
23112 break;
23113
23114 case DW_OP_const4s:
23115 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23116 i += 4;
23117 break;
23118
585861ea
JK
23119 case DW_OP_const8u:
23120 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23121 i += 8;
23122 break;
23123
21ae7a4d
JK
23124 case DW_OP_constu:
23125 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23126 &bytes_read);
23127 i += bytes_read;
23128 break;
23129
23130 case DW_OP_consts:
23131 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23132 i += bytes_read;
23133 break;
23134
23135 case DW_OP_dup:
23136 stack[stacki + 1] = stack[stacki];
23137 stacki++;
23138 break;
23139
23140 case DW_OP_plus:
23141 stack[stacki - 1] += stack[stacki];
23142 stacki--;
23143 break;
23144
23145 case DW_OP_plus_uconst:
23146 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23147 &bytes_read);
23148 i += bytes_read;
23149 break;
23150
23151 case DW_OP_minus:
23152 stack[stacki - 1] -= stack[stacki];
23153 stacki--;
23154 break;
23155
23156 case DW_OP_deref:
23157 /* If we're not the last op, then we definitely can't encode
23158 this using GDB's address_class enum. This is valid for partial
23159 global symbols, although the variable's address will be bogus
23160 in the psymtab. */
23161 if (i < size)
7d79de9a
TT
23162 {
23163 if (computed == nullptr)
23164 dwarf2_complex_location_expr_complaint ();
23165 else
23166 return 0;
23167 }
21ae7a4d
JK
23168 break;
23169
23170 case DW_OP_GNU_push_tls_address:
4aa4e28b 23171 case DW_OP_form_tls_address:
21ae7a4d
JK
23172 /* The top of the stack has the offset from the beginning
23173 of the thread control block at which the variable is located. */
23174 /* Nothing should follow this operator, so the top of stack would
23175 be returned. */
23176 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23177 address will be bogus in the psymtab. Make it always at least
23178 non-zero to not look as a variable garbage collected by linker
23179 which have DW_OP_addr 0. */
21ae7a4d 23180 if (i < size)
7d79de9a
TT
23181 {
23182 if (computed == nullptr)
23183 dwarf2_complex_location_expr_complaint ();
23184 else
23185 return 0;
23186 }
585861ea 23187 stack[stacki]++;
21ae7a4d
JK
23188 break;
23189
23190 case DW_OP_GNU_uninit:
7d79de9a
TT
23191 if (computed != nullptr)
23192 return 0;
21ae7a4d
JK
23193 break;
23194
336d760d 23195 case DW_OP_addrx:
3019eac3 23196 case DW_OP_GNU_addr_index:
49f6c839 23197 case DW_OP_GNU_const_index:
3019eac3
DE
23198 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23199 &bytes_read);
23200 i += bytes_read;
23201 break;
23202
21ae7a4d 23203 default:
7d79de9a
TT
23204 if (computed == nullptr)
23205 {
23206 const char *name = get_DW_OP_name (op);
21ae7a4d 23207
7d79de9a
TT
23208 if (name)
23209 complaint (_("unsupported stack op: '%s'"),
23210 name);
23211 else
23212 complaint (_("unsupported stack op: '%02x'"),
23213 op);
23214 }
21ae7a4d
JK
23215
23216 return (stack[stacki]);
d53d4ac5 23217 }
3c6e0cb3 23218
21ae7a4d
JK
23219 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23220 outside of the allocated space. Also enforce minimum>0. */
23221 if (stacki >= ARRAY_SIZE (stack) - 1)
23222 {
7d79de9a
TT
23223 if (computed == nullptr)
23224 complaint (_("location description stack overflow"));
21ae7a4d
JK
23225 return 0;
23226 }
23227
23228 if (stacki <= 0)
23229 {
7d79de9a
TT
23230 if (computed == nullptr)
23231 complaint (_("location description stack underflow"));
21ae7a4d
JK
23232 return 0;
23233 }
23234 }
7d79de9a
TT
23235
23236 if (computed != nullptr)
23237 *computed = true;
21ae7a4d 23238 return (stack[stacki]);
c906108c
SS
23239}
23240
23241/* memory allocation interface */
23242
c906108c 23243static struct dwarf_block *
7b5a2f43 23244dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23245{
8d749320 23246 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23247}
23248
c906108c 23249static struct die_info *
b60c80d6 23250dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23251{
23252 struct die_info *die;
b60c80d6
DJ
23253 size_t size = sizeof (struct die_info);
23254
23255 if (num_attrs > 1)
23256 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23257
b60c80d6 23258 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23259 memset (die, 0, sizeof (struct die_info));
23260 return (die);
23261}
2e276125
JB
23262
23263\f
a036ba48 23264
c90ec28a 23265/* Macro support. */
cf2c3c16 23266
9eac9650
TT
23267/* An overload of dwarf_decode_macros that finds the correct section
23268 and ensures it is read in before calling the other overload. */
23269
23270static void
23271dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23272 int section_is_gnu)
23273{
976ca316
SM
23274 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23275 struct objfile *objfile = per_objfile->objfile;
5a0e026f 23276 const struct line_header *lh = cu->line_header;
9eac9650
TT
23277 unsigned int offset_size = cu->header.offset_size;
23278 struct dwarf2_section_info *section;
23279 const char *section_name;
23280
23281 if (cu->dwo_unit != nullptr)
23282 {
23283 if (section_is_gnu)
23284 {
23285 section = &cu->dwo_unit->dwo_file->sections.macro;
23286 section_name = ".debug_macro.dwo";
23287 }
23288 else
23289 {
23290 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23291 section_name = ".debug_macinfo.dwo";
23292 }
23293 }
23294 else
23295 {
23296 if (section_is_gnu)
23297 {
976ca316 23298 section = &per_objfile->per_bfd->macro;
9eac9650
TT
23299 section_name = ".debug_macro";
23300 }
23301 else
23302 {
976ca316 23303 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
23304 section_name = ".debug_macinfo";
23305 }
23306 }
23307
23308 section->read (objfile);
23309 if (section->buffer == nullptr)
23310 {
23311 complaint (_("missing %s section"), section_name);
23312 return;
23313 }
23314
23315 buildsym_compunit *builder = cu->get_builder ();
23316
976ca316 23317 dwarf_decode_macros (per_objfile, builder, section, lh,
9eac9650
TT
23318 offset_size, offset, section_is_gnu);
23319}
23320
3019eac3
DE
23321/* Return the .debug_loc section to use for CU.
23322 For DWO files use .debug_loc.dwo. */
23323
23324static struct dwarf2_section_info *
23325cu_debug_loc_section (struct dwarf2_cu *cu)
23326{
976ca316 23327 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 23328
3019eac3 23329 if (cu->dwo_unit)
43988095
JK
23330 {
23331 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23332
43988095
JK
23333 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23334 }
976ca316
SM
23335 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23336 : &per_objfile->per_bfd->loc);
3019eac3
DE
23337}
23338
8cf6f0b1
TT
23339/* A helper function that fills in a dwarf2_loclist_baton. */
23340
23341static void
23342fill_in_loclist_baton (struct dwarf2_cu *cu,
23343 struct dwarf2_loclist_baton *baton,
ff39bb5e 23344 const struct attribute *attr)
8cf6f0b1 23345{
976ca316 23346 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
23347 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23348
976ca316 23349 section->read (per_objfile->objfile);
8cf6f0b1 23350
976ca316 23351 baton->per_objfile = per_objfile;
8cf6f0b1
TT
23352 baton->per_cu = cu->per_cu;
23353 gdb_assert (baton->per_cu);
23354 /* We don't know how long the location list is, but make sure we
23355 don't run off the edge of the section. */
3019eac3
DE
23356 baton->size = section->size - DW_UNSND (attr);
23357 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
23358 if (cu->base_address.has_value ())
23359 baton->base_address = *cu->base_address;
23360 else
23361 baton->base_address = 0;
f664829e 23362 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23363}
23364
4c2df51b 23365static void
ff39bb5e 23366dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23367 struct dwarf2_cu *cu, int is_block)
4c2df51b 23368{
976ca316
SM
23369 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23370 struct objfile *objfile = per_objfile->objfile;
3019eac3 23371 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23372
cd6c91b4 23373 if (attr->form_is_section_offset ()
3019eac3 23374 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23375 the section. If so, fall through to the complaint in the
23376 other branch. */
2c7d5afc 23377 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23378 {
0d53c4c4 23379 struct dwarf2_loclist_baton *baton;
4c2df51b 23380
8d749320 23381 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23382
8cf6f0b1 23383 fill_in_loclist_baton (cu, baton, attr);
be391dca 23384
2b24b6e4 23385 if (!cu->base_address.has_value ())
b98664d3 23386 complaint (_("Location list used without "
3e43a32a 23387 "specifying the CU base address."));
4c2df51b 23388
f1e6e072
TT
23389 SYMBOL_ACLASS_INDEX (sym) = (is_block
23390 ? dwarf2_loclist_block_index
23391 : dwarf2_loclist_index);
0d53c4c4
DJ
23392 SYMBOL_LOCATION_BATON (sym) = baton;
23393 }
23394 else
23395 {
23396 struct dwarf2_locexpr_baton *baton;
23397
8d749320 23398 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 23399 baton->per_objfile = per_objfile;
ae0d2f24
UW
23400 baton->per_cu = cu->per_cu;
23401 gdb_assert (baton->per_cu);
0d53c4c4 23402
4fc6c0d5 23403 if (attr->form_is_block ())
0d53c4c4
DJ
23404 {
23405 /* Note that we're just copying the block's data pointer
23406 here, not the actual data. We're still pointing into the
6502dd73
DJ
23407 info_buffer for SYM's objfile; right now we never release
23408 that buffer, but when we do clean up properly this may
23409 need to change. */
0d53c4c4
DJ
23410 baton->size = DW_BLOCK (attr)->size;
23411 baton->data = DW_BLOCK (attr)->data;
23412 }
23413 else
23414 {
23415 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23416 sym->natural_name ());
0d53c4c4 23417 baton->size = 0;
0d53c4c4 23418 }
6e70227d 23419
f1e6e072
TT
23420 SYMBOL_ACLASS_INDEX (sym) = (is_block
23421 ? dwarf2_locexpr_block_index
23422 : dwarf2_locexpr_index);
0d53c4c4
DJ
23423 SYMBOL_LOCATION_BATON (sym) = baton;
23424 }
4c2df51b 23425}
6502dd73 23426
2e6a9f79 23427/* See read.h. */
96408a79 23428
2e6a9f79
SM
23429const comp_unit_head *
23430dwarf2_per_cu_data::get_header () const
96408a79 23431{
2e6a9f79
SM
23432 if (!m_header_read_in)
23433 {
23434 const gdb_byte *info_ptr
23435 = this->section->buffer + to_underlying (this->sect_off);
96408a79 23436
2e6a9f79 23437 memset (&m_header, 0, sizeof (m_header));
96408a79 23438
2e6a9f79
SM
23439 read_comp_unit_head (&m_header, info_ptr, this->section,
23440 rcuh_kind::COMPILE);
23441 }
96408a79 23442
2e6a9f79 23443 return &m_header;
96408a79
SA
23444}
23445
09ba997f 23446/* See read.h. */
ae0d2f24 23447
98714339 23448int
09ba997f 23449dwarf2_per_cu_data::addr_size () const
ae0d2f24 23450{
2e6a9f79 23451 return this->get_header ()->addr_size;
ae0d2f24
UW
23452}
23453
09ba997f 23454/* See read.h. */
9eae7c52
TT
23455
23456int
09ba997f 23457dwarf2_per_cu_data::offset_size () const
9eae7c52 23458{
2e6a9f79 23459 return this->get_header ()->offset_size;
96408a79
SA
23460}
23461
09ba997f 23462/* See read.h. */
96408a79
SA
23463
23464int
09ba997f 23465dwarf2_per_cu_data::ref_addr_size () const
96408a79 23466{
2e6a9f79 23467 const comp_unit_head *header = this->get_header ();
96408a79 23468
2e6a9f79
SM
23469 if (header->version == 2)
23470 return header->addr_size;
96408a79 23471 else
2e6a9f79 23472 return header->offset_size;
181cebd4
JK
23473}
23474
09ba997f 23475/* See read.h. */
9aa1f1e3 23476
09ba997f 23477struct type *
293e7e51 23478dwarf2_cu::addr_type () const
9a49df9d 23479{
293e7e51 23480 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
23481 struct type *void_type = objfile_type (objfile)->builtin_void;
23482 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 23483 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
23484
23485 if (TYPE_LENGTH (addr_type) == addr_size)
23486 return addr_type;
23487
09ba997f 23488 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23489 return addr_type;
23490}
23491
22b6cd70
TT
23492/* A helper function for dwarf2_find_containing_comp_unit that returns
23493 the index of the result, and that searches a vector. It will
23494 return a result even if the offset in question does not actually
23495 occur in any CU. This is separate so that it can be unit
23496 tested. */
ae038cb0 23497
22b6cd70
TT
23498static int
23499dwarf2_find_containing_comp_unit
23500 (sect_offset sect_off,
23501 unsigned int offset_in_dwz,
23502 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23503{
ae038cb0
DJ
23504 int low, high;
23505
ae038cb0 23506 low = 0;
22b6cd70 23507 high = all_comp_units.size () - 1;
ae038cb0
DJ
23508 while (high > low)
23509 {
36586728 23510 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23511 int mid = low + (high - low) / 2;
9a619af0 23512
22b6cd70 23513 mid_cu = all_comp_units[mid];
36586728 23514 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23515 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23516 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23517 high = mid;
23518 else
23519 low = mid + 1;
23520 }
23521 gdb_assert (low == high);
22b6cd70
TT
23522 return low;
23523}
23524
23525/* Locate the .debug_info compilation unit from CU's objfile which contains
23526 the DIE at OFFSET. Raises an error on failure. */
23527
23528static struct dwarf2_per_cu_data *
23529dwarf2_find_containing_comp_unit (sect_offset sect_off,
23530 unsigned int offset_in_dwz,
976ca316 23531 dwarf2_per_objfile *per_objfile)
22b6cd70 23532{
976ca316
SM
23533 int low = dwarf2_find_containing_comp_unit
23534 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
23535 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
22b6cd70 23536
45b8ae0c 23537 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23538 {
36586728 23539 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23540 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23541 "offset %s [in module %s]"),
23542 sect_offset_str (sect_off),
976ca316 23543 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 23544
976ca316 23545 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 23546 <= sect_off);
976ca316 23547 return per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
23548 }
23549 else
23550 {
976ca316 23551 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 23552 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23553 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23554 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23555 return this_cu;
23556 }
23557}
23558
22b6cd70
TT
23559#if GDB_SELF_TEST
23560
23561namespace selftests {
23562namespace find_containing_comp_unit {
23563
23564static void
23565run_test ()
23566{
23567 struct dwarf2_per_cu_data one {};
23568 struct dwarf2_per_cu_data two {};
23569 struct dwarf2_per_cu_data three {};
23570 struct dwarf2_per_cu_data four {};
23571
23572 one.length = 5;
23573 two.sect_off = sect_offset (one.length);
23574 two.length = 7;
23575
23576 three.length = 5;
23577 three.is_dwz = 1;
23578 four.sect_off = sect_offset (three.length);
23579 four.length = 7;
23580 four.is_dwz = 1;
23581
23582 std::vector<dwarf2_per_cu_data *> units;
23583 units.push_back (&one);
23584 units.push_back (&two);
23585 units.push_back (&three);
23586 units.push_back (&four);
23587
23588 int result;
23589
23590 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23591 SELF_CHECK (units[result] == &one);
23592 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23593 SELF_CHECK (units[result] == &one);
23594 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23595 SELF_CHECK (units[result] == &two);
23596
23597 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23598 SELF_CHECK (units[result] == &three);
23599 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23600 SELF_CHECK (units[result] == &three);
23601 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23602 SELF_CHECK (units[result] == &four);
23603}
23604
23605}
23606}
23607
23608#endif /* GDB_SELF_TEST */
23609
9e021579 23610/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 23611
9e021579
SM
23612dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23613 dwarf2_per_objfile *per_objfile)
23614 : per_cu (per_cu),
23615 per_objfile (per_objfile),
9068261f
AB
23616 mark (false),
23617 has_loclist (false),
23618 checked_producer (false),
23619 producer_is_gxx_lt_4_6 (false),
23620 producer_is_gcc_lt_4_3 (false),
eb77c9df 23621 producer_is_icc (false),
9068261f 23622 producer_is_icc_lt_14 (false),
c258c396 23623 producer_is_codewarrior (false),
9068261f 23624 processing_has_namespace_info (false)
93311388 23625{
9816fde3
JK
23626}
23627
23628/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23629
23630static void
95554aad
TT
23631prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23632 enum language pretend_language)
9816fde3
JK
23633{
23634 struct attribute *attr;
23635
23636 /* Set the language we're debugging. */
23637 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23638 if (attr != nullptr)
9816fde3
JK
23639 set_cu_language (DW_UNSND (attr), cu);
23640 else
9cded63f 23641 {
95554aad 23642 cu->language = pretend_language;
9cded63f
TT
23643 cu->language_defn = language_def (cu->language);
23644 }
dee91e82 23645
7d45c7c3 23646 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23647}
23648
7188ed02 23649/* See read.h. */
ae038cb0 23650
7188ed02
SM
23651dwarf2_cu *
23652dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 23653{
7188ed02
SM
23654 auto it = m_dwarf2_cus.find (per_cu);
23655 if (it == m_dwarf2_cus.end ())
23656 return nullptr;
ae038cb0 23657
7188ed02
SM
23658 return it->second;
23659}
23660
23661/* See read.h. */
23662
23663void
23664dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
23665{
23666 gdb_assert (this->get_cu (per_cu) == nullptr);
23667
23668 m_dwarf2_cus[per_cu] = cu;
23669}
23670
23671/* See read.h. */
23672
23673void
23674dwarf2_per_objfile::age_comp_units ()
23675{
23676 /* Start by clearing all marks. */
23677 for (auto pair : m_dwarf2_cus)
23678 pair.second->mark = false;
23679
23680 /* Traverse all CUs, mark them and their dependencies if used recently
23681 enough. */
23682 for (auto pair : m_dwarf2_cus)
ae038cb0 23683 {
7188ed02
SM
23684 dwarf2_cu *cu = pair.second;
23685
23686 cu->last_used++;
23687 if (cu->last_used <= dwarf_max_cache_age)
23688 dwarf2_mark (cu);
ae038cb0
DJ
23689 }
23690
7188ed02
SM
23691 /* Delete all CUs still not marked. */
23692 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 23693 {
7188ed02 23694 dwarf2_cu *cu = it->second;
ae038cb0 23695
7188ed02 23696 if (!cu->mark)
ae038cb0 23697 {
7188ed02
SM
23698 delete cu;
23699 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
23700 }
23701 else
7188ed02 23702 it++;
ae038cb0
DJ
23703 }
23704}
23705
7188ed02 23706/* See read.h. */
ae038cb0 23707
7188ed02
SM
23708void
23709dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 23710{
7188ed02
SM
23711 auto it = m_dwarf2_cus.find (per_cu);
23712 if (it == m_dwarf2_cus.end ())
23713 return;
ae038cb0 23714
7188ed02 23715 delete it->second;
ae038cb0 23716
7188ed02
SM
23717 m_dwarf2_cus.erase (it);
23718}
ae038cb0 23719
7188ed02
SM
23720dwarf2_per_objfile::~dwarf2_per_objfile ()
23721{
23722 remove_all_cus ();
ae038cb0
DJ
23723}
23724
dee91e82
DE
23725/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23726 We store these in a hash table separate from the DIEs, and preserve them
23727 when the DIEs are flushed out of cache.
23728
23729 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23730 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23731 or the type may come from a DWO file. Furthermore, while it's more logical
23732 to use per_cu->section+offset, with Fission the section with the data is in
23733 the DWO file but we don't know that section at the point we need it.
23734 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23735 because we can enter the lookup routine, get_die_type_at_offset, from
23736 outside this file, and thus won't necessarily have PER_CU->cu.
23737 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23738
dee91e82 23739struct dwarf2_per_cu_offset_and_type
1c379e20 23740{
dee91e82 23741 const struct dwarf2_per_cu_data *per_cu;
9c541725 23742 sect_offset sect_off;
1c379e20
DJ
23743 struct type *type;
23744};
23745
dee91e82 23746/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23747
23748static hashval_t
dee91e82 23749per_cu_offset_and_type_hash (const void *item)
1c379e20 23750{
9a3c8263
SM
23751 const struct dwarf2_per_cu_offset_and_type *ofs
23752 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23753
9c541725 23754 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23755}
23756
dee91e82 23757/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23758
23759static int
dee91e82 23760per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23761{
9a3c8263
SM
23762 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23763 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23764 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23765 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23766
dee91e82 23767 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23768 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23769}
23770
23771/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23772 table if necessary. For convenience, return TYPE.
23773
23774 The DIEs reading must have careful ordering to:
85102364 23775 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23776 reading current DIE.
23777 * Not trying to dereference contents of still incompletely read in types
23778 while reading in other DIEs.
23779 * Enable referencing still incompletely read in types just by a pointer to
23780 the type without accessing its fields.
23781
23782 Therefore caller should follow these rules:
23783 * Try to fetch any prerequisite types we may need to build this DIE type
23784 before building the type and calling set_die_type.
e71ec853 23785 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23786 possible before fetching more types to complete the current type.
23787 * Make the type as complete as possible before fetching more types. */
1c379e20 23788
f792889a 23789static struct type *
1c379e20
DJ
23790set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23791{
976ca316 23792 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 23793 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 23794 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
23795 struct attribute *attr;
23796 struct dynamic_prop prop;
1c379e20 23797
b4ba55a1
JB
23798 /* For Ada types, make sure that the gnat-specific data is always
23799 initialized (if not already set). There are a few types where
23800 we should not be doing so, because the type-specific area is
23801 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23802 where the type-specific area is used to store the floatformat).
23803 But this is not a problem, because the gnat-specific information
23804 is actually not needed for these types. */
23805 if (need_gnat_info (cu)
78134374
SM
23806 && type->code () != TYPE_CODE_FUNC
23807 && type->code () != TYPE_CODE_FLT
23808 && type->code () != TYPE_CODE_METHODPTR
23809 && type->code () != TYPE_CODE_MEMBERPTR
23810 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
23811 && !HAVE_GNAT_AUX_INFO (type))
23812 INIT_GNAT_SPECIFIC (type);
23813
3f2f83dd
KB
23814 /* Read DW_AT_allocated and set in type. */
23815 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23816 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23817 {
293e7e51 23818 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23819 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23820 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd
KB
23821 }
23822 else if (attr != NULL)
23823 {
b98664d3 23824 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23825 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23826 sect_offset_str (die->sect_off));
3f2f83dd
KB
23827 }
23828
23829 /* Read DW_AT_associated and set in type. */
23830 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23831 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23832 {
293e7e51 23833 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23834 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23835 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd
KB
23836 }
23837 else if (attr != NULL)
23838 {
b98664d3 23839 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23840 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23841 sect_offset_str (die->sect_off));
3f2f83dd
KB
23842 }
23843
3cdcd0ce
JB
23844 /* Read DW_AT_data_location and set in type. */
23845 attr = dwarf2_attr (die, DW_AT_data_location, cu);
293e7e51 23846 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 23847 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 23848
976ca316
SM
23849 if (per_objfile->die_type_hash == NULL)
23850 per_objfile->die_type_hash
0335378b
TT
23851 = htab_up (htab_create_alloc (127,
23852 per_cu_offset_and_type_hash,
23853 per_cu_offset_and_type_eq,
23854 NULL, xcalloc, xfree));
1c379e20 23855
dee91e82 23856 ofs.per_cu = cu->per_cu;
9c541725 23857 ofs.sect_off = die->sect_off;
1c379e20 23858 ofs.type = type;
dee91e82 23859 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 23860 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23861 if (*slot)
b98664d3 23862 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23863 sect_offset_str (die->sect_off));
8d749320
SM
23864 *slot = XOBNEW (&objfile->objfile_obstack,
23865 struct dwarf2_per_cu_offset_and_type);
1c379e20 23866 **slot = ofs;
f792889a 23867 return type;
1c379e20
DJ
23868}
23869
9c541725 23870/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23871 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23872
23873static struct type *
9c541725 23874get_die_type_at_offset (sect_offset sect_off,
aa66c379 23875 dwarf2_per_cu_data *per_cu,
976ca316 23876 dwarf2_per_objfile *per_objfile)
1c379e20 23877{
dee91e82 23878 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23879
976ca316 23880 if (per_objfile->die_type_hash == NULL)
f792889a 23881 return NULL;
1c379e20 23882
dee91e82 23883 ofs.per_cu = per_cu;
9c541725 23884 ofs.sect_off = sect_off;
9a3c8263 23885 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 23886 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23887 if (slot)
23888 return slot->type;
23889 else
23890 return NULL;
23891}
23892
02142a6c 23893/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23894 or return NULL if DIE does not have a saved type. */
23895
23896static struct type *
23897get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23898{
aa66c379 23899 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
23900}
23901
10b3939b
DJ
23902/* Add a dependence relationship from CU to REF_PER_CU. */
23903
23904static void
23905dwarf2_add_dependence (struct dwarf2_cu *cu,
23906 struct dwarf2_per_cu_data *ref_per_cu)
23907{
23908 void **slot;
23909
23910 if (cu->dependencies == NULL)
23911 cu->dependencies
23912 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23913 NULL, &cu->comp_unit_obstack,
23914 hashtab_obstack_allocate,
23915 dummy_obstack_deallocate);
23916
23917 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23918 if (*slot == NULL)
23919 *slot = ref_per_cu;
23920}
1c379e20 23921
f504f079
DE
23922/* Subroutine of dwarf2_mark to pass to htab_traverse.
23923 Set the mark field in every compilation unit in the
7188ed02
SM
23924 cache that we must keep because we are keeping CU.
23925
23926 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 23927
10b3939b
DJ
23928static int
23929dwarf2_mark_helper (void **slot, void *data)
23930{
7188ed02
SM
23931 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
23932 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
23933 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
23934
23935 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23936 reading of the chain. As such dependencies remain valid it is not much
23937 useful to track and undo them during QUIT cleanups. */
7188ed02 23938 if (cu == nullptr)
d07ed419
JK
23939 return 1;
23940
7188ed02 23941 if (cu->mark)
10b3939b 23942 return 1;
10b3939b 23943
7188ed02
SM
23944 cu->mark = true;
23945
23946 if (cu->dependencies != nullptr)
23947 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
23948
23949 return 1;
23950}
23951
f504f079
DE
23952/* Set the mark field in CU and in every other compilation unit in the
23953 cache that we must keep because we are keeping CU. */
23954
ae038cb0
DJ
23955static void
23956dwarf2_mark (struct dwarf2_cu *cu)
23957{
23958 if (cu->mark)
23959 return;
7188ed02 23960
9068261f 23961 cu->mark = true;
ae038cb0 23962
7188ed02
SM
23963 if (cu->dependencies != nullptr)
23964 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
23965}
23966
72bf9492
DJ
23967/* Trivial hash function for partial_die_info: the hash value of a DIE
23968 is its offset in .debug_info for this objfile. */
23969
23970static hashval_t
23971partial_die_hash (const void *item)
23972{
9a3c8263
SM
23973 const struct partial_die_info *part_die
23974 = (const struct partial_die_info *) item;
9a619af0 23975
9c541725 23976 return to_underlying (part_die->sect_off);
72bf9492
DJ
23977}
23978
23979/* Trivial comparison function for partial_die_info structures: two DIEs
23980 are equal if they have the same offset. */
23981
23982static int
23983partial_die_eq (const void *item_lhs, const void *item_rhs)
23984{
9a3c8263
SM
23985 const struct partial_die_info *part_die_lhs
23986 = (const struct partial_die_info *) item_lhs;
23987 const struct partial_die_info *part_die_rhs
23988 = (const struct partial_die_info *) item_rhs;
9a619af0 23989
9c541725 23990 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23991}
23992
3c3bb058
AB
23993struct cmd_list_element *set_dwarf_cmdlist;
23994struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 23995
9291a0cd 23996static void
cd4fb1b2
SM
23997show_check_physname (struct ui_file *file, int from_tty,
23998 struct cmd_list_element *c, const char *value)
9291a0cd 23999{
cd4fb1b2
SM
24000 fprintf_filtered (file,
24001 _("Whether to check \"physname\" is %s.\n"),
24002 value);
9291a0cd
TT
24003}
24004
6c265988 24005void _initialize_dwarf2_read ();
cd4fb1b2 24006void
6c265988 24007_initialize_dwarf2_read ()
9291a0cd 24008{
0743fc83 24009 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 24010Set DWARF specific variables.\n\
590042fc 24011Configure DWARF variables such as the cache size."),
0743fc83
TT
24012 &set_dwarf_cmdlist, "maintenance set dwarf ",
24013 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 24014
0743fc83 24015 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
24016Show DWARF specific variables.\n\
24017Show DWARF variables such as the cache size."),
0743fc83
TT
24018 &show_dwarf_cmdlist, "maintenance show dwarf ",
24019 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 24020
cd4fb1b2
SM
24021 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24022 &dwarf_max_cache_age, _("\
24023Set the upper bound on the age of cached DWARF compilation units."), _("\
24024Show the upper bound on the age of cached DWARF compilation units."), _("\
24025A higher limit means that cached compilation units will be stored\n\
24026in memory longer, and more total memory will be used. Zero disables\n\
24027caching, which can slow down startup."),
24028 NULL,
24029 show_dwarf_max_cache_age,
24030 &set_dwarf_cmdlist,
24031 &show_dwarf_cmdlist);
156942c7 24032
cd4fb1b2
SM
24033 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24034Set debugging of the DWARF reader."), _("\
24035Show debugging of the DWARF reader."), _("\
24036When enabled (non-zero), debugging messages are printed during DWARF\n\
24037reading and symtab expansion. A value of 1 (one) provides basic\n\
24038information. A value greater than 1 provides more verbose information."),
24039 NULL,
24040 NULL,
24041 &setdebuglist, &showdebuglist);
9291a0cd 24042
cd4fb1b2
SM
24043 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24044Set debugging of the DWARF DIE reader."), _("\
24045Show debugging of the DWARF DIE reader."), _("\
24046When enabled (non-zero), DIEs are dumped after they are read in.\n\
24047The value is the maximum depth to print."),
24048 NULL,
24049 NULL,
24050 &setdebuglist, &showdebuglist);
9291a0cd 24051
cd4fb1b2
SM
24052 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24053Set debugging of the dwarf line reader."), _("\
24054Show debugging of the dwarf line reader."), _("\
24055When enabled (non-zero), line number entries are dumped as they are read in.\n\
24056A value of 1 (one) provides basic information.\n\
24057A value greater than 1 provides more verbose information."),
24058 NULL,
24059 NULL,
24060 &setdebuglist, &showdebuglist);
437afbb8 24061
cd4fb1b2
SM
24062 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24063Set cross-checking of \"physname\" code against demangler."), _("\
24064Show cross-checking of \"physname\" code against demangler."), _("\
24065When enabled, GDB's internal \"physname\" code is checked against\n\
24066the demangler."),
24067 NULL, show_check_physname,
24068 &setdebuglist, &showdebuglist);
900e11f9 24069
e615022a
DE
24070 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24071 no_class, &use_deprecated_index_sections, _("\
24072Set whether to use deprecated gdb_index sections."), _("\
24073Show whether to use deprecated gdb_index sections."), _("\
24074When enabled, deprecated .gdb_index sections are used anyway.\n\
24075Normally they are ignored either because of a missing feature or\n\
24076performance issue.\n\
24077Warning: This option must be enabled before gdb reads the file."),
24078 NULL,
24079 NULL,
24080 &setlist, &showlist);
24081
f1e6e072
TT
24082 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24083 &dwarf2_locexpr_funcs);
24084 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24085 &dwarf2_loclist_funcs);
24086
24087 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24088 &dwarf2_block_frame_base_locexpr_funcs);
24089 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24090 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24091
24092#if GDB_SELF_TEST
24093 selftests::register_test ("dw2_expand_symtabs_matching",
24094 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24095 selftests::register_test ("dwarf2_find_containing_comp_unit",
24096 selftests::find_containing_comp_unit::run_test);
c62446b1 24097#endif
6502dd73 24098}
This page took 5.360159 seconds and 4 git commands to generate.